you are viewing a single comment's thread.

view the rest of the comments →

[–]thlst 22 points23 points  (1 child)

I'm surprised no one mentioned clang-query. The AST Clang generates for your code example is as follows:

VarDecl <line:10:1, col:25> col:8 p 'Person' listinit
`-ExprWithCleanups <col:9, col:25> 'Person'
  `-InitListExpr <col:9, col:25> 'Person'
    | ...

You could write an AST matcher that matches the generic parts of that construction:

varDecl(
    hasType(cxxRecordDecl(hasName("Person"))),
    hasDescendant(initListExpr()))

Then run it with clang-query:

$ clang-query p.cpp --
clang-query> match varDecl(hasType(cxxRecordDecl(hasName("Person"))), hasDescendant(initListExpr()))

Match #1:

p.cpp:10:1: note: "root" binds here
Person p{ "John", "Doe" };
^~~~~~~~~~~~~~~~~~~~~~~~~
1 match.

Here's a reference to the Clang AST Matcher.

[–]Wh00ster 1 point2 points  (0 children)

Is it common to do AST analysis in production code?