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

Richard Smith richard at metafoo.co.uk
Thu Jun 11 14:20:11 PDT 2015


On Mon, Jun 8, 2015 at 8:01 AM, Betts, Adam <a.betts at imperial.ac.uk> wrote:

> 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.


We do not provide any way to do that; the Scope objects used while parsing
are transient and short-lived. You can in principle rebuild them by walking
the lexically-enclosing function(s). We would happily accept a patch to add
such functionality; this is a commonly-requested feature.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150611/dd276383/attachment.html>


More information about the cfe-dev mailing list