finding the source position (range) of a variable name

Hi,

Finding the name and position of a function/method is easy:

DeclarationNameInfo dni = fd -> getNameInfo();
SourceRange dniSr = dni.getSourceRange(); // dniSr is position and length of name in source file
std::string name = dni.getName().getAsString(); // name of function/method

But how can I get this information for variables? Both the name and
sourcerange.
I looked in getQualifier +Loc of DeclaratorDecl but they're always
missing it seems?

Folkert van Heusden

DeclarationNameInfo dni = fd -> getNameInfo();
SourceRange dniSr = dni.getSourceRange(); // dniSr is position and length of name in source file
std::string name = dni.getName().getAsString(); // name of function/method

But how can I get this information for variables? Both the name and
sourcerange.
I looked in getQualifier +Loc of DeclaratorDecl but they're always
missing it seems?

What I mean is:
DeclaratorDecl::getQualifierLoc().getNestedNameSpecifier() always
returns NULL while I expect a NestedNameSpecifier object.

Folkert van Heusden

The problem can be reproduced with this small example code:
http://files.slimwinnen.nl/n-nns.tgz

Just run make and then ./parse and you'll see it saying that "! NO
NestedNameSpecifier".
The test-code is test.cpp and the file it parses is example.cpp.

> DeclarationNameInfo dni = fd -> getNameInfo();
> SourceRange dniSr = dni.getSourceRange(); // dniSr is position and length of name in source file
> std::string name = dni.getName().getAsString(); // name of function/method
>
> But how can I get this information for variables? Both the name and
> sourcerange.
> I looked in getQualifier +Loc of DeclaratorDecl but they're always
> missing it seems?

What I mean is:
DeclaratorDecl::getQualifierLoc().getNestedNameSpecifier() always
returns NULL while I expect a NestedNameSpecifier object.

Folkert van Heusden