[cfe-dev] Nested forward declarations of self

Kim Gräsman kim.grasman at gmail.com
Thu Dec 13 12:13:05 PST 2012


Hello,

When I try to use shouldVisitImplicitCode() with a
RecursiveASTVisitor, I get some results I don't understand.

I can reproduce this with the following code, modelled after
ToolingTests/RecursiveASTVisitorTest.cpp;

--
class ImplicitCXXRecordDeclVisitor
  : public TestVisitor<ImplicitCXXRecordDeclVisitor> {
public:
  bool shouldVisitImplicitCode() const { return true; }

  bool VisitDecl(Decl* Decl) {
    llvm::errs() << "Dumping decl at " << Decl << " of kind " <<
Decl->getDeclKindName() << ", with " <<
std::distance(Decl->redecls_begin(), Decl->redecls_end()) << "
redecls.\n";
    Decl->dump(llvm::errs());
    llvm::errs() << "\n";
    return true;
  }
};

TEST(RecursiveASTVisitor, DumpRecordDecl) {
  ImplicitCXXRecordDeclVisitor Visitor;
  Visitor.runOver(
    "class A {}; \n");
}
--

Given the minimal class definition, "class A {};", I get the following
output (skipping over less relevant decls):

--
...
Dumping decl at 0xbac8b0 of kind CXXRecord, with 1 redecls.
class A {
    class A;
}
Dumping decl at 0xbac990 of kind CXXRecord, with 1 redecls.
class A
--

I'm surprised by the nested forward declaration in the class -- it
seems like it creates a nested forward declaration to itself.

Should it be there?

I'd like to skip over it, any ideas on how I can identify these? It
doesn't seem to have any redecl relation to the outer class. Would I
have to compare its name to its parent name (which seems brittle)?

Many thanks,
- Kim



More information about the cfe-dev mailing list