[cfe-dev] Getting the pointer to an out-of-class VarDecl

Mihai Draghicioiu mihai.draghicioiu at gmail.com
Tue Sep 30 02:34:43 PDT 2014


> There is a helper like getDefiningDecl which will get the second vardecl
if you've already parsed it.

Can you be more specific please? I can't find this helper method in any
class.


> A redeclaration chain contains all redeclarations, that's the idea, it is
a circular singly-linked list.
> You should be able to traverse the chain
with getFirstDecl(), getMostRecentDecl() and friends from Redeclarable.h.
I am able to traverse the chain, but if I traverse it inside VisitVarDecl()
or TraverseVarDecl(), upon the first VarDecl, the list only contains
itself, and uopn the second VarDecl, the list contains both.

The code I used to test is this:

void printSourceRange(SourceRange r) {
llvm::outs()
<< r.getBegin().printToString(ci.getSourceManager())
<< "---"
<< r.getEnd().printToString(ci.getSourceManager());
}

bool TraverseVarDecl(VarDecl *d) {
llvm::outs() << "VarDecl " << d->getName() << " @";
printSourceRange(d->getSourceRange());
llvm::outs() << "\n";
llvm::outs() << "first decl @";
printSourceRange(d->getFirstDecl()->getSourceRange());
llvm::outs() << "\n";
llvm::outs() << "most recent decl @";
printSourceRange(d->getMostRecentDecl()->getSourceRange());
llvm::outs() << "\n";
for(auto r: d->redecls()) {
llvm::outs() << "redecl " << d->getName() << " @";
printSourceRange(r->getSourceRange());
llvm::outs() << "\n";
}
return true;
}

Output looks like this:

VarDecl poop @foo.cpp:2:2---foo.cpp:2:13
first decl @foo.cpp:2:2---foo.cpp:2:13
most recent decl @foo.cpp:2:2---foo.cpp:2:13
redecl poop @foo.cpp:2:2---foo.cpp:2:13

VarDecl poop @foo.cpp:5:1---foo.cpp:5:17
first decl @foo.cpp:2:2---foo.cpp:2:13
most recent decl @foo.cpp:5:1---foo.cpp:5:17
redecl poop @foo.cpp:5:1---foo.cpp:5:17
redecl poop @foo.cpp:2:2---foo.cpp:2:13

As you can see, the only reference to the VarDecl at foo.cpp:5:1 i get is
upon the second call of TraverseVarDecl, and I want to get it at the first
call.

Am I misunderstanding something, or doing something wrong? It looks like
the two VarDecls have two different lists of redeclarations, which do not
correspond.

Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140930/c51be4be/attachment.html>


More information about the cfe-dev mailing list