Matching all the DeclRefExpr referring to a certain VarDecl

Hi all,

supposing I have this code:

void foo(int i){
  int a;
  a = i;

int main(){

I would like to match all the DeclRefExpr related to the variable a,
but not to the parameter i.

This is the AST dump of the code

-FunctionDecl 0x6261720 <, line:4:1> line:1:6 used foo 'void (int)'
>-ParmVarDecl 0x6261698 <col:10, col:14> col:14 used i 'int'
`-CompoundStmt 0x62618e8 <col:16, line:4:1>
  >-DeclStmt 0x6261840 <line:2:2, col:7>
  > `-VarDecl 0x62617e0 <col:2, col:6> col:6 used a 'int'
  `-BinaryOperator 0x62618c0 <line:3:2, col:6> 'int' lvalue '='
    >-DeclRefExpr 0x6261858 <col:2> 'int' lvalue Var 0x62617e0 'a' 'int'
    `-ImplicitCastExpr 0x62618a8 <col:6> 'int' <LValueToRValue>
      `-DeclRefExpr 0x6261880 <col:6> 'int' lvalue ParmVar 0x6261698 'i' 'int'

`-FunctionDecl 0x6261930 <line:6:1, line:8:1> line:6:5 main 'int (void)'
  `-CompoundStmt 0x6261ac0 <col:11, line:8:1>
    `-CallExpr 0x6261a90 <line:7:2, col:7> 'void'
      >-ImplicitCastExpr 0x6261a78 <col:2> 'void (*)(int)' <FunctionToPointerDecay>
      > `-DeclRefExpr 0x6261a50 <col:2> 'void (int)' lvalue Function 0x6261720 'foo' 'void (int)'
      `-IntegerLiteral 0x6261a30 <col:6> 'int' 0

It seems that I have to exploit the "address" (is it correct?) of the
VarDecl, namely 0x62617e0, that guarantees me that I'm referring
exactly that variable, because I'm not really sure the name will be
enough for avoiding ambiguity.

I'm using an ASTConsumer, and I have a Stmt* variable representing the
body of the function foo (i'm not using matchers' DSL).

Any idea on how can i get those addresses? (my guess is something
related to VarDecl class' getASTContext method).

Any help would be very much appreciated.

Luca A.


More, hopefully useful, details: my goal is to "vectorize" those variables, so the output must be:

void foo(int i){
  int a[N];
  loop(id: 0..N-1){
    a[id] = i;

Note that ‘a’ is a VarDecl whereas ‘i’ is a ParamVarDecl (that derives from VarDecl). Using isa sounds better to me if all you’re trying to do is differentiate between local variables and parameters.