From 0d09c4944e448958ab1ff1941948777805df95e7 Mon Sep 17 00:00:00 2001 From: Argyrios Kyrtzidis Date: Wed, 19 Nov 2008 18:01:13 +0000 Subject: [PATCH] Take care another assert: struct A { struct B; }; struct A::B { void m() {} // Assertion failed: getContainingDC(DC) == CurContext && "The next DeclContext should be lexically contained in the current one." }; Introduce DeclContext::getLexicalParent which may be different from DeclContext::getParent when nested-names are involved, e.g: namespace A { struct S; } struct A::S {}; // getParent() == namespace 'A' // getLexicalParent() == translation unit llvm-svn: 59650 --- clang/include/clang/AST/DeclBase.h | 15 +++++++++++++++ clang/lib/AST/DeclBase.cpp | 9 +++++++++ clang/lib/Sema/SemaDecl.cpp | 6 +++--- clang/test/SemaCXX/nested-name-spec.cpp | 7 +++++++ 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/clang/include/clang/AST/DeclBase.h b/clang/include/clang/AST/DeclBase.h index bfc7607b74fc..12ed5ff174f2 100644 --- a/clang/include/clang/AST/DeclBase.h +++ b/clang/include/clang/AST/DeclBase.h @@ -300,6 +300,21 @@ public: const_cast(this)->getParent()); } + /// getLexicalParent - Returns the containing lexical DeclContext. May be + /// different from getParent, e.g.: + /// + /// namespace A { + /// struct S; + /// } + /// struct A::S {}; // getParent() == namespace 'A' + /// // getLexicalParent() == translation unit + /// + const DeclContext *getLexicalParent() const; + DeclContext *getLexicalParent() { + return const_cast( + const_cast(this)->getLexicalParent()); + } + bool isFunctionOrMethod() const { switch (DeclKind) { case Decl::Block: diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp index 88d9a208553a..c0655cf9ceb6 100644 --- a/clang/lib/AST/DeclBase.cpp +++ b/clang/lib/AST/DeclBase.cpp @@ -361,3 +361,12 @@ const DeclContext *DeclContext::getParent() const { else return NULL; } + +const DeclContext *DeclContext::getLexicalParent() const { + if (const ScopedDecl *SD = dyn_cast(this)) + return SD->getLexicalDeclContext(); + else if (const BlockDecl *BD = dyn_cast(this)) + return BD->getParentContext(); + else + return NULL; +} diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index d6d9845af796..941f40fa52a7 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -53,10 +53,10 @@ DeclContext *Sema::getContainingDC(DeclContext *DC) { // A C++ inline method is parsed *after* the topmost class it was declared in // is fully parsed (it's "complete"). // The parsing of a C++ inline method happens at the declaration context of - // the topmost (non-nested) class it is declared in. + // the topmost (non-nested) class it is lexically declared in. assert(isa(MD->getParent()) && "C++ method not in Record."); DC = MD->getParent(); - while (CXXRecordDecl *RD = dyn_cast(DC->getParent())) + while (CXXRecordDecl *RD = dyn_cast(DC->getLexicalParent())) DC = RD; // Return the declaration context of the topmost class the inline method is @@ -70,7 +70,7 @@ DeclContext *Sema::getContainingDC(DeclContext *DC) { if (ScopedDecl *SD = dyn_cast(DC)) return SD->getLexicalDeclContext(); - return DC->getParent(); + return DC->getLexicalParent(); } void Sema::PushDeclContext(DeclContext *DC) { diff --git a/clang/test/SemaCXX/nested-name-spec.cpp b/clang/test/SemaCXX/nested-name-spec.cpp index b22d8dc3d4d3..18cb3b418a6a 100644 --- a/clang/test/SemaCXX/nested-name-spec.cpp +++ b/clang/test/SemaCXX/nested-name-spec.cpp @@ -45,12 +45,19 @@ struct A::undef; // expected-error {{'undef' does not name a tag member in the s namespace A2 { typedef int INT; struct RC; + struct CC { + struct NC; + }; } struct A2::RC { INT x; }; +struct A2::CC::NC { + void m() {} +}; + void f3() { N::x = 0; // expected-error {{use of undeclared identifier 'N'}} int N;