[cfe-dev] Getting multiple AST matches as a list

Jesper Eskilson jesper.eskilson at iar.com
Tue Sep 3 01:02:36 PDT 2013


On 09/02/2013 02:50 PM, Gábor Kozár wrote:
> Hi,
>
> The AST dump above clearly shows how the AST is structured, and that 
> should be all the information you need.
>
> Basically, you can see that the children of the InitListExpr are the 
> nodes you're interested in. So you bind the whole InitListExpr that 
> you're interested, and you iterate through its children. According to 
> the AST dump, the actual StringLiteral nodes are wrapped by 
> ImplicitCastExpr-s - a phenomenon that's very common in Clang's AST - 
> so you need to throw these away: Expr-s have a lot of methods to do 
> this, IgnoreImpCasts() and so on - see the documentation for the exact 
> naming and descriptions.
> After this, all you need to do is llvm::dyn_cast the Expr to 
> StringLiteral, and the StringLiteral class has methods to get the 
> actual string value.
>
> AST matchers are usually used to give you a rough approximation of the 
> information you need - you'll find that usually you will need to 
> perform some additional logic to get to the data you actually want. 
> AST matchers give you a substitute to the complex state machines you'd 
> have to implement in RecursiveASTVisitor, but they're not meant to be 
> a complete way of getting all the information from the AST.
> Nonetheless, if you find that you still want to do this using AST 
> matchers, feel free to implement them yourself using the macros and 
> classes defined in ASTMatchersMacros.h. For examples, the best place 
> to look at is ASTMatchers.h. However, you may find this confusing, and 
> the compiler error messages are not that helpful, due to the fact that 
> AST matchers rely heavily on C++ templates (and some macros).
>
>

Thanks, this was very helpful.

/Jesper
-- 
*Jesper Eskilson* /Development Engineer/
IAR Systems AB
Box 23051, Strandbodgatan 1
SE-750 23 Uppsala, SWEDEN
E-mail: jesper.eskilson at iar.com <mailto:jesper.eskilson at iar.com> 
Website: www.iar.com
<http://www.iar.com> Twitter: www.twitter.com/iarsystems 
<http://www.twitter.com/iarsystems>



More information about the cfe-dev mailing list