]> granicus.if.org Git - clang/commitdiff
Implement merging of namespace-scope declarations across modules, so
authorDouglas Gregor <dgregor@apple.com>
Mon, 9 Jan 2012 17:38:47 +0000 (17:38 +0000)
committerDouglas Gregor <dgregor@apple.com>
Mon, 9 Jan 2012 17:38:47 +0000 (17:38 +0000)
that we can merge, for example, two occurrences of

  namespace N { void f(); }

in two disjoint modules.

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

lib/Serialization/ASTReaderDecl.cpp
test/Modules/Inputs/namespaces-left.h
test/Modules/Inputs/namespaces-right.h
test/Modules/namespaces.cpp

index ffd768a94d287865e8a646b500d7fcefb346e113..f6c9bfaa572367c2bc1f8a492549a622aaf1c761 100644 (file)
@@ -1741,14 +1741,14 @@ static bool isSameEntity(NamedDecl *X, NamedDecl *Y) {
 }
 
 ASTDeclReader::FindExistingResult::~FindExistingResult() {
-  if (!AddResult)
+  if (!AddResult || Existing)
     return;
   
   DeclContext *DC = New->getDeclContext()->getRedeclContext();
   if (DC->isTranslationUnit() && Reader.SemaObj) {
-    if (!Existing) {
-      Reader.SemaObj->IdResolver.tryAddTopLevelDecl(New, New->getDeclName());
-    }
+    Reader.SemaObj->IdResolver.tryAddTopLevelDecl(New, New->getDeclName());
+  } else if (DC->isNamespace()) {
+    DC->addDecl(New);
   }
 }
 
@@ -1775,7 +1775,13 @@ ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) {
     }
   }
 
-  // FIXME: Search in the DeclContext.
+  if (DC->isNamespace()) {
+    for (DeclContext::lookup_result R = DC->lookup(Name);
+         R.first != R.second; ++R.first) {
+      if (isSameEntity(*R.first, D))
+        return FindExistingResult(Reader, D, *R.first);
+    }
+  }
   
   return FindExistingResult(Reader, D, /*Existing=*/0);
 }
index 6835cda0c4f52acaf9bd7e9ff97ffc0ab0719f54..ea2ae2b9ced120e8a68fb0942a939b982e3f9965 100644 (file)
@@ -25,3 +25,15 @@ namespace N6 {
 namespace N7 {
   int &f(int);
 }
+
+namespace N8 {
+  int &f(int);
+}
+
+namespace N9 {
+  int &f(int);
+}
+
+namespace N10 {
+  int &f(int);
+}
index 0afef073c31cea14f00628c232af1883040086e8..d103c00c820b2ad5eeca1c2ec795e600f87ec834 100644 (file)
@@ -27,3 +27,15 @@ namespace N6 {
 namespace N7 {
   double &f(double);
 }
+
+namespace N8 {
+  int &f(int);
+}
+
+namespace N9 {
+  int &f(int);
+}
+
+namespace N10 {
+  int &f(int);
+}
index e1a8d6e8b09c81cb33d031630a8ea97bf64a9205..9ef966ac019d61175f4714aedef3d358b4b67d00 100644 (file)
@@ -5,6 +5,8 @@ namespace N6 {
   char &f(char);
 }
 
+namespace N8 { }
+
 @import namespaces_left;
 @import namespaces_right;
 
@@ -23,6 +25,10 @@ namespace N5 {
   char &f(char);
 }
 
+namespace N10 { 
+  int &f(int);
+}
+
 void testMerged() {
   int &ir1 = N5::f(17);
   int &ir2 = N6::f(17);
@@ -34,3 +40,10 @@ void testMerged() {
   char &cr2 = N6::f('b');
 }
 
+// Test merging of declarations within namespaces that themselves were
+// merged without a common first declaration.
+void testMergedMerged() {
+  int &ir1 = N8::f(17);
+  int &ir2 = N9::f(17);
+  int &ir3 = N10::f(17);
+}