]> granicus.if.org Git - clang/commitdiff
Take care another assert:
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Wed, 19 Nov 2008 18:01:13 +0000 (18:01 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Wed, 19 Nov 2008 18:01:13 +0000 (18:01 +0000)
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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@59650 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/DeclBase.h
lib/AST/DeclBase.cpp
lib/Sema/SemaDecl.cpp
test/SemaCXX/nested-name-spec.cpp

index bfc7607b74fc49861ed65a3de4caf5c3656d4b11..12ed5ff174f281b8d46686006a97185348210881 100644 (file)
@@ -300,6 +300,21 @@ public:
                              const_cast<const DeclContext*>(this)->getParent());
   }
 
+  /// getLexicalParent - Returns the containing lexical DeclContext. May be
+  /// different from getParent, e.g.:\r
+  ///\r
+  ///   namespace A {\r
+  ///      struct S;\r
+  ///   }\r
+  ///   struct A::S {}; // getParent() == namespace 'A'\r
+  ///                   // getLexicalParent() == translation unit
+  ///
+  const DeclContext *getLexicalParent() const;
+  DeclContext *getLexicalParent() {
+    return const_cast<DeclContext*>(
+                      const_cast<const DeclContext*>(this)->getLexicalParent());
+  }
+
   bool isFunctionOrMethod() const {
     switch (DeclKind) {
       case Decl::Block:
index 88d9a208553af6dd66980db249fa933a79a6835a..c0655cf9ceb6153ae771c9350a6e40bf86ad3176 100644 (file)
@@ -361,3 +361,12 @@ const DeclContext *DeclContext::getParent() const {
   else
     return NULL;
 }
+
+const DeclContext *DeclContext::getLexicalParent() const {
+  if (const ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
+    return SD->getLexicalDeclContext();
+  else if (const BlockDecl *BD = dyn_cast<BlockDecl>(this))
+    return BD->getParentContext();
+  else
+    return NULL;
+}
index d6d9845af79662dbd78778ca4ed2509bead90cf9..941f40fa52a769600f9bd91aea6f90393ee2f6b4 100644 (file)
@@ -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<CXXRecordDecl>(MD->getParent()) && "C++ method not in Record.");
     DC = MD->getParent();
-    while (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC->getParent()))
+    while (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(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<ScopedDecl>(DC))
     return SD->getLexicalDeclContext();
 
-  return DC->getParent();
+  return DC->getLexicalParent();
 }
 
 void Sema::PushDeclContext(DeclContext *DC) {
index b22d8dc3d4d3073186ac524c76efbb29bca287c0..18cb3b418a6a6cb1c94ae0d719227aabe0535f40 100644 (file)
@@ -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;