I have a Decl object called decl that represents int b = a; If i do decl->getInit() i can have an Expr pointer that represents a I think. From there how can i retrieve the variable name ‘a’? Maybe using getNameAsString()? I tried but it didn’t work.
I have a Decl object called decl that represents int b = a; If i do decl->getInit() i can have an Expr pointer that represents a I think. From there how can i retrieve the variable name ‘a’? Maybe using getNameAsString()? I tried but it didn’t work.
As -ast-dump shows, getInit is a ImplicitCastExpr which has a DeclRefExpr which points to ‘a’.
Hi,
I hope this can help someone. I solved in this way:
Expr *e = (dyn_cast<ImplicitCastExpr>(d->getInit()))->getSubExpr(); //
return an object that points to b
dyn_cast<DeclRefExpr>(e)->getNameInfo().getAsString(); //this return b
If you're going to assume that 'e' is a DeclRefExpr (and likewise for the
ImplicitCastExpr case), use cast<> rather than dyn_cast. Otherwise, you
should cope with dyn_cast returning nullptr.