[cfe-commits] r59650 - in /cfe/trunk: include/clang/AST/DeclBase.h lib/AST/DeclBase.cpp lib/Sema/SemaDecl.cpp test/SemaCXX/nested-name-spec.cpp

Argiris Kirtzidis akyrtzi at gmail.com
Wed Nov 19 10:01:13 PST 2008


Author: akirtzidis
Date: Wed Nov 19 12:01:13 2008
New Revision: 59650

URL: http://llvm.org/viewvc/llvm-project?rev=59650&view=rev
Log:
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

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

Modified: cfe/trunk/include/clang/AST/DeclBase.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclBase.h?rev=59650&r1=59649&r2=59650&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/DeclBase.h (original)
+++ cfe/trunk/include/clang/AST/DeclBase.h Wed Nov 19 12:01:13 2008
@@ -300,6 +300,21 @@
                              const_cast<const DeclContext*>(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<DeclContext*>(
+                      const_cast<const DeclContext*>(this)->getLexicalParent());
+  }
+
   bool isFunctionOrMethod() const {
     switch (DeclKind) {
       case Decl::Block:

Modified: cfe/trunk/lib/AST/DeclBase.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclBase.cpp?rev=59650&r1=59649&r2=59650&view=diff

==============================================================================
--- cfe/trunk/lib/AST/DeclBase.cpp (original)
+++ cfe/trunk/lib/AST/DeclBase.cpp Wed Nov 19 12:01:13 2008
@@ -361,3 +361,12 @@
   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;
+}

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=59650&r1=59649&r2=59650&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Wed Nov 19 12:01:13 2008
@@ -53,10 +53,10 @@
     // 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 @@
   if (ScopedDecl *SD = dyn_cast<ScopedDecl>(DC))
     return SD->getLexicalDeclContext();
 
-  return DC->getParent();
+  return DC->getLexicalParent();
 }
 
 void Sema::PushDeclContext(DeclContext *DC) {

Modified: cfe/trunk/test/SemaCXX/nested-name-spec.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/nested-name-spec.cpp?rev=59650&r1=59649&r2=59650&view=diff

==============================================================================
--- cfe/trunk/test/SemaCXX/nested-name-spec.cpp (original)
+++ cfe/trunk/test/SemaCXX/nested-name-spec.cpp Wed Nov 19 12:01:13 2008
@@ -45,12 +45,19 @@
 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;





More information about the cfe-commits mailing list