Parent of an AST node?

Hi,

how can I get the parent of an AST node?

I have a DeclRefExpr object and would like the name of the function it's contained in. Does clang offer this functionality or do I have to keep track of parents myself?

Thanks,
Nico

Hi,

how can I get the parent of an AST node?

I have a DeclRefExpr object and would like the name of the function
it's contained in. Does clang offer this functionality or do I have to
keep track of parents myself?

You need to keep track of it yourself...

snaroff

Hi Nico,

Check out the ParentMap class in libAST. It represents a map from Stmt* -> Stmt* (children to parents). To construct it, you just provide the root of the AST:

   ParentMap PM(root);

To query for a parent:

   Stmt* parent = PM.getParent(child);

The parent map will only contain the backmapping from children to parents that are descendants of the root AST node that you specified when you constructed the parent map.

Ted

Hi Ted,

Hi Nico,

There is no root statement (Stmt*) since at the top-level there are only declarations (Decl*). You could consider TranslationUnit to be the root (which contains references to all the top-level declarations).

Ted