How do I find a variable declaration using Clang?

I am trying to parse some C ++ code using Clang. For the following code, I can get Expr* x

as an argument f-> bar(x)

from AST. Now I want to search for a declaration x

to get its initial value, but I'm not sure how. I've also looked at the documentation Expr

here http://clang.llvm.org/doxygen/classclang_1_1Expr.html but I couldn't find anything useful.

struct foo {
  int bar(int x) { return x; }
};

int main(){
  foo *f;
  int x = 0, y = 0;
  int y = f->bar(x); 
  return 0;
}

      

+3


source to share


1 answer


Something like



dynamic_cast<const VarDecl*>(dynamic_cast<const DeclRefExpr*>(x)->getDecl())->getInit();

      

+1


source







All Articles