[cfe-dev] Variable scope

James Dennett james.dennett at gmail.com
Sat Sep 1 08:49:36 PDT 2012


On Sat, Sep 1, 2012 at 8:20 AM, Balou <cleanconcept83 at gmail.com> wrote:
> Hello everyone,
>
> I'm writting some code for visiting variable, struct, ... declarations (I'm
> using C Language). For that, my class derives from DeclVisitor. My goal is
> to determine the scope of each variable at its declaration (ie inside the
> VisitVarDecl method). The method getSourceRangeonly of VarDecl only returns
> the range of the declaration in terms of caracters, not the real scope.

Right, the notion of a source range is about a position in the source
text, not in the AST structure.

> Is
> there method for getting the VarDecl scope ?

You might look at getParentFunctionOrMethod and/or getDeclContext (and
if you use getDeclContext you might also want DeclContext::getParent).
 I'm assuming here that you're writing C++ code that visits C code,
given that you spoke of a class derived from DeclVisitor.

-- James



More information about the cfe-dev mailing list