[cfe-dev] Accessing scope/symbol table information at any statement

Betts, Adam a.betts at imperial.ac.uk
Mon Jun 8 08:01:11 PDT 2015

I have a RecursiveASTVisitor, and, during the visit, I want to inspect which variables are currently in scope at a particular (random) statement.

For example, in the following code:

int a = 1, b = 2;
  int c;
  c = a + b; // statement 1
    char a = 'a';
    c = a + b;  // statement 2

At statement 1, I want to know that a, b, c are visible and have type ‘int'.
At statement 2, I want to know that a, b, c are visible, and that b, c have type ‘int' but a has type ‘char’.

In my visitor class I have the following, amongst other code:

ASTContext &Context;
bool VisitStmt(Stmt *s) {
  // Scope code goes here
  return true;

I have looked through several classes defined in Clang, e.g., Scope, IdentifierTable and DeclContext, and I think a Scope object will serve my needs. But, even assuming that is correct, I have no idea how to retrieve a Scope object at a Stmt object.

Help very much appreciated.

More information about the cfe-dev mailing list