[cfe-dev] Clang - get type and identifier from Expr class object

David Blaikie dblaikie at gmail.com
Tue Nov 6 08:44:28 PST 2012


On Tue, Nov 6, 2012 at 8:24 AM, Rajendra <rks at cse.iitb.ac.in> wrote:
> Hi,
>
> I am using RecursiveASTVisitor class from clang.
>
> I have following code in an overridden method
> VisitBinaryOperator(BinaryOperator *E):
>
> Expr* lhs = E->getLHS();
> Expr* rhs = E->getRHS();
>
> I want to get type and variable name from expression lhs and rhs.
>
> Say I have Stmt x = 10;, then I want to get identifier x from lhs and 10
> from rhs.
> If I have Stmt x = x + 10; then I want to get identifier x from lhs and x +
> 10 as sub expression from rhs
>
> How this can be done using clang API? Any pointer is welcome.

Try dumping the AST (if you're in a debugger just "p lhs->dump()", if
not, you can use the clang command line argument to dump ASTs (I
forget the exact name of it)) to see what it looks like, then write
code to walk it - probably dyn_casting the lhs to DeclRefExpr then
asking that for its name and dyn_casting the rhs to an IntegerLiteral
(or it could be something else, I forget the exact AST node names).

Also - try looking at existing code for this sort of thing. There's
lots of Clang diagnostics that care about certain values/types in
certain expressions, if you go & look at how those diagnostics are
implemented you shuold get a feel for how to walk the AST to achieve
your goals.



More information about the cfe-dev mailing list