Meta-programming with clang-query?

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?

Edited by Aphetres on
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.

Edited by Mārtiņš Možeiko on
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


Edited by Aphetres on
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.

Edited by Mārtiņš Možeiko on
Thanks for that.