Register
handmade.network»Forums»Meta-programming with clang-query?
23 posts
Meta-programming with clang-query?
4 months ago Edited by Aphetres on April 5, 2021, 2:22 a.m.
I watched Casey's video where he wrote a basic C parser and saw Ryan's video where he demoed his own metaprogaming. Then I tried clang-query and used it to pull out a struct. Then I thought there must be a reason programmers are making their own C parsers, instead of using clang-query. It seems strange that programmers are forced to make their own C parsers, when compilers already parse C so should provide access to the data in that step. So my question. What is clang-query missing?
Mārtiņš Možeiko
2244 posts / 1 project
Meta-programming with clang-query?
4 months ago Edited by Mārtiņš Možeiko on April 5, 2021, 3:20 a.m.
Nothing :)

You can use clang for your metaprogramming needs just fine if you're willing to buy into clang ecosystem. Which obviously means modern C++ and maintaining build system in some form (at least compile_commands.json).

I've used clang to parse and gather information about C++ code, and modify and generate new C++ code from it. And all that before clang-query or LibTooling/LibASTMatchers existed - and it worked great.
23 posts
Meta-programming with clang-query?
4 months ago Edited by Aphetres on April 5, 2021, 4:09 a.m.
Cool. I thought the problem might be it doesn't show tagged variables like Casey was doing in his video, as I couldn't find the setting for that if there is one in Clang 10.

At the moment all I want it for is to generate AOSOA/SOA/AOS versions of a basic struct with setters and getters, so tagging isn't something I need right now, but it would be nice to know its possible. Heck for what I am doing I don't even need clang-query. lol

Mārtiņš Možeiko
2244 posts / 1 project
Meta-programming with clang-query?
4 months ago Edited by Mārtiņš Možeiko on April 5, 2021, 8:51 p.m.
clang has generic "annotate" attribute with arbitrary string as value. And you can stick it on variables/functions/arguments/structures/members and it will be available in AST tree.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
__attribute__((annotate("function")))
void f(
       int arg0 __attribute__((annotate("arg0")))
       )
{
}

struct __attribute__((annotate("struct"))) S
S 
{
    int x __attribute__((annotate("struct member")));
};

For simpler syntax you can create macro for it.
23 posts
Meta-programming with clang-query?
3 months, 4 weeks ago
Thanks for that.