19 posts
Meta-programming with clang-query?
Edited by Aphetres on
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
2446 posts / 2 projects
Meta-programming with clang-query?
Edited by Mārtiņš Možeiko 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.
19 posts
Meta-programming with clang-query?
Edited by Aphetres 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

Mārtiņš Možeiko
2446 posts / 2 projects
Meta-programming with clang-query?
Edited by Mārtiņš Možeiko 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.
19 posts
Meta-programming with clang-query?
Thanks for that.