Shadowed local variables

Hi,

Referring to the code below, as I climb the Clang AST, when I arrive at the
DeclRefExpr refering to x on the lhs of the assignment x=2, how do I detect
that it is the block scope x, rather than the function scope x?

void f()
{
  int x;
  x=1;
  {
    int x;
    x=2;
  }
  gx=x;
}

I need unique identifiers for the each x, so my symbolic execution
calculates the correct value for gx. Currently I am calling getDecl() and
then getQualifiedNameAsString(). I have tried calling getDeclContext, but
in references to both xs, I get a CXXMethod as the context.
Presumably I could use the value of the pointer returned by getDecl(), but
this doesn’t feel right.

Any help much appreciated.

Thanks,
Peter

Hi Peter,

the pointer returned by getDecl() is a perfect unique identifier inside your program. Should feel all right! :wink: If you want to report to the user, you can refer with the name and the line number it was declared.

Regards,
Laszlo

Hi Laszlo,

Thanks for this.

Regards,
Peter