[cfe-users] Block scope

Jean-Louis Leroy jl at yorel.be
Mon May 19 07:35:25 PDT 2014


Hi,

Consider:

void foo() {
    // a scope
    {
        enum { Foo };
    }

    // another scope
    {
        int x;
        enum { Foo };
        int y;
    }
}

I want to identify the source ranges where the two (distinct) Foo are
visible, given a pointer to their respective EnumConstantDecl. I have
tried with the various Parent() methods - getParentFunctionOrMethod()
looked like a good candidate - but it doesn't look like blocks
constitute scopes. For example, calling
getParentFunctionOrMethod()->dumpCeclContext() on either enumerators
prints the same thing, namely:

enum  {
    Foo
};
int x;
enum  {
    Foo
};
int y;

I found a solution that involves going up to the nearest DeclContext,
then look down the tree (using an AST matcher) for the CompoundStmt
that contains a DeclStmt that declares an EnumDecl which has the
target EnumConstantDecl.

However, I am thinking that there must be an easier way to do this -
after all, the two Foo *are* in different scopes and the code
generation back-end needs to know this. I would expect the scopes to
be represented in the AST and it looks like DeclContext is just that.

I am new to the AST library so I may be missing something obvious...

Jean-Louis



More information about the cfe-users mailing list