Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get location of variable name in clang::VarDecl

Tags:

c++

clang

I'm using clang 3.0 library for some analysis of C/C++ code, and I need to get location of variable declaration, I tried this code:

clang::VarDecl * vd = ...;
clang::SourceManager & srcMgr = ...;

clang::SourceRange loc = vd->getSourceRange();
clang::PresumedLoc locStart = srcMgr.getPresumedLoc(loc.getBegin());
clang::PresumedLoc locEnd = srcMgr.getPresumedLoc(loc.getEnd());

But locStart and locEnd points to the beginning (and ending) of declaration variable (with type, and, possibly initialiser). For example:

const char * ptr = 0;
^            ^ ^   ^

locStart will point at the first pointer(^) , and locEnd will point at the last pointer. How can I get the location of the second and third pointers (only name, without type and initialiser)?

like image 365
Alexey Avatar asked Jan 29 '12 16:01

Alexey


1 Answers

I'm currently not in the position to test it but I think you want to extract the SourceLocation obtained by getLocation() from your VarDecl. This function is actually defined by the Decl base class. Although it seems to identify just one location it seems to be usable to identify the entire name (I haven't tried to extract its individual ends, however, just used it to indicate the variable).

like image 173
Dietmar Kühl Avatar answered Oct 23 '22 05:10

Dietmar Kühl