How to get the details of DeclRefExpr from VarDecl in clang?

166 Views Asked by At

My ast node looks like this:

|-VarDecl 0x158f5190 <line:7:1, col:14> col:6 used ptr1 'int *' cinit

| |-UnaryOperator 0x158f62c0 <col:13, col:14> 'int *' prefix '&' cannot overflow

| | `-DeclRefExpr 0x158f62a0 col:14 'int' lvalue Var 0x158f50f0 'foo' 'int'

How can I get the address and name of 'foo' from VarDecl?

I have used DeclContext::decl_iterator to iterate through all the nodes and I can dump the entire VarDecl. I am interested in extracting information from DeclRefExpr with VarDecl.

1

There are 1 best solutions below

0
Thien Tran On

You can use getNameAsString to get the name of declaration.

If you are writing something like LibTooling so it's better to use MatchFinder for searching and matching nodes in AST. For example:

varDecl(hasDescendant(
     declRefExpr( ... ).bind("found DeclRefExpr!!!")))

And write a MatchCallback to manipulate the found results.