[cfe-dev] Nested forward declarations of self

Dmitri Gribenko gribozavr at gmail.com
Thu Dec 13 13:03:40 PST 2012


On Thu, Dec 13, 2012 at 10:13 PM, Kim Gräsman <kim.grasman at gmail.com> wrote:
> 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?

Hello Kim,

C++11 [class] p2:
A class-name is inserted into the scope in which it is declared
immediately after the class-name is seen. The class-name is also
inserted into the scope of the class itself; this is known as the
injected-class-name. [...]

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/




More information about the cfe-dev mailing list