[cfe-commits] r147782 - in /cfe/trunk: lib/Serialization/ASTReaderDecl.cpp test/Modules/Inputs/namespaces-left.h test/Modules/Inputs/namespaces-right.h test/Modules/Inputs/namespaces-top.h test/Modules/namespaces.cpp

Douglas Gregor dgregor at apple.com
Mon Jan 9 10:07:25 PST 2012


Author: dgregor
Date: Mon Jan  9 12:07:24 2012
New Revision: 147782

URL: http://llvm.org/viewvc/llvm-project?rev=147782&view=rev
Log:
When deserializing an anonymous namespace from a module, do not attach
the anonymous namespace to its parent. Semantically, this means that
the anonymous namespaces defined in one module are distinct from the
anonymous namespaces defined in another module.


Modified:
    cfe/trunk/lib/Serialization/ASTReaderDecl.cpp
    cfe/trunk/test/Modules/Inputs/namespaces-left.h
    cfe/trunk/test/Modules/Inputs/namespaces-right.h
    cfe/trunk/test/Modules/Inputs/namespaces-top.h
    cfe/trunk/test/Modules/namespaces.cpp

Modified: cfe/trunk/lib/Serialization/ASTReaderDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReaderDecl.cpp?rev=147782&r1=147781&r2=147782&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTReaderDecl.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTReaderDecl.cpp Mon Jan  9 12:07:24 2012
@@ -978,10 +978,12 @@
   mergeRedeclarable(D, Redecl);
 
   if (Redecl.getFirstID() == ThisDeclID) {
-    // FIXME: If there's already an anonymous namespace, do we merge it with
-    // this one? Or do we, when loading modules, just forget about anonymous
-    // namespace entirely?
-    D->setAnonymousNamespace(ReadDeclAs<NamespaceDecl>(Record, Idx));
+    // Each module has its own anonymous namespace, which is disjoint from
+    // any other module's anonymous namespaces, so don't attach the anonymous
+    // namespace at all.
+    NamespaceDecl *Anon = ReadDeclAs<NamespaceDecl>(Record, Idx);
+    if (F.Kind != MK_Module)
+      D->setAnonymousNamespace(Anon);
   } else {
     // Link this namespace back to the first declaration, which has already
     // been deserialized.
@@ -2484,10 +2486,16 @@
     case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
       NamespaceDecl *Anon
         = Reader.ReadDeclAs<NamespaceDecl>(ModuleFile, Record, Idx);
-      if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(D))
-        TU->setAnonymousNamespace(Anon);
-      else
-        cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon);
+      
+      // Each module has its own anonymous namespace, which is disjoint from
+      // any other module's anonymous namespaces, so don't attach the anonymous
+      // namespace at all.
+      if (ModuleFile.Kind != MK_Module) {
+        if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(D))
+          TU->setAnonymousNamespace(Anon);
+        else
+          cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon);
+      }
       break;
     }
 

Modified: cfe/trunk/test/Modules/Inputs/namespaces-left.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/namespaces-left.h?rev=147782&r1=147781&r2=147782&view=diff
==============================================================================
--- cfe/trunk/test/Modules/Inputs/namespaces-left.h (original)
+++ cfe/trunk/test/Modules/Inputs/namespaces-left.h Mon Jan  9 12:07:24 2012
@@ -37,3 +37,17 @@
 namespace N10 {
   int &f(int);
 }
+
+namespace N11 {
+  namespace {
+    class Foo;
+  }
+  Foo *getFoo();
+}
+
+namespace N12 {
+  namespace {
+    class Foo;
+  }
+  Foo *getFoo();
+}

Modified: cfe/trunk/test/Modules/Inputs/namespaces-right.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/namespaces-right.h?rev=147782&r1=147781&r2=147782&view=diff
==============================================================================
--- cfe/trunk/test/Modules/Inputs/namespaces-right.h (original)
+++ cfe/trunk/test/Modules/Inputs/namespaces-right.h Mon Jan  9 12:07:24 2012
@@ -39,3 +39,23 @@
 namespace N10 {
   int &f(int);
 }
+
+
+
+
+
+
+
+namespace N11 {
+  namespace {
+    class Foo;
+  }
+  void consumeFoo(Foo*);
+}
+
+namespace N12 {
+  namespace {
+    class Foo;
+  }
+  void consumeFoo(Foo*);
+}

Modified: cfe/trunk/test/Modules/Inputs/namespaces-top.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/namespaces-top.h?rev=147782&r1=147781&r2=147782&view=diff
==============================================================================
--- cfe/trunk/test/Modules/Inputs/namespaces-top.h (original)
+++ cfe/trunk/test/Modules/Inputs/namespaces-top.h Mon Jan  9 12:07:24 2012
@@ -9,3 +9,6 @@
 namespace N3 { 
   int& f(int);
 }
+
+namespace N12 { }
+

Modified: cfe/trunk/test/Modules/namespaces.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/namespaces.cpp?rev=147782&r1=147781&r2=147782&view=diff
==============================================================================
--- cfe/trunk/test/Modules/namespaces.cpp (original)
+++ cfe/trunk/test/Modules/namespaces.cpp Mon Jan  9 12:07:24 2012
@@ -47,3 +47,14 @@
   int &ir2 = N9::f(17);
   int &ir3 = N10::f(17);
 }
+
+// Test merging when using anonymous namespaces, which does not
+// actually perform any merging.
+// other file: expected-note{{passing argument to parameter here}}
+void testAnonymousNotMerged() {
+  N11::consumeFoo(N11::getFoo()); // expected-error{{cannot initialize a parameter of type 'N11::<anonymous>::Foo *' with an rvalue of type 'N11::<anonymous>::Foo *'}}
+  N12::consumeFoo(N12::getFoo()); // expected-error{{cannot initialize a parameter of type 'N12::<anonymous>::Foo *' with an rvalue of type 'N12::<anonymous>::Foo *'}}  
+}
+
+
+// other file: expected-note{{passing argument to parameter here}}





More information about the cfe-commits mailing list