aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouglas Gregor <dgregor@apple.com>2012-01-09 18:07:24 +0000
committerDouglas Gregor <dgregor@apple.com>2012-01-09 18:07:24 +0000
commit1c3875dab92f3b0a25212c80863e452cb269d3f1 (patch)
tree7aa3c4d982f43b9547ce364c510aad874da690ef
parent0fdc09fe680787b855cf20183c4bd3b83f2c907f (diff)
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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@147782 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/Serialization/ASTReaderDecl.cpp24
-rw-r--r--test/Modules/Inputs/namespaces-left.h14
-rw-r--r--test/Modules/Inputs/namespaces-right.h20
-rw-r--r--test/Modules/Inputs/namespaces-top.h3
-rw-r--r--test/Modules/namespaces.cpp11
5 files changed, 64 insertions, 8 deletions
diff --git a/lib/Serialization/ASTReaderDecl.cpp b/lib/Serialization/ASTReaderDecl.cpp
index f6c9bfaa57..6b8a72f807 100644
--- a/lib/Serialization/ASTReaderDecl.cpp
+++ b/lib/Serialization/ASTReaderDecl.cpp
@@ -978,10 +978,12 @@ void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
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 @@ void ASTDeclReader::UpdateDecl(Decl *D, ModuleFile &ModuleFile,
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;
}
diff --git a/test/Modules/Inputs/namespaces-left.h b/test/Modules/Inputs/namespaces-left.h
index ea2ae2b9ce..7e9002aea8 100644
--- a/test/Modules/Inputs/namespaces-left.h
+++ b/test/Modules/Inputs/namespaces-left.h
@@ -37,3 +37,17 @@ namespace N9 {
namespace N10 {
int &f(int);
}
+
+namespace N11 {
+ namespace {
+ class Foo;
+ }
+ Foo *getFoo();
+}
+
+namespace N12 {
+ namespace {
+ class Foo;
+ }
+ Foo *getFoo();
+}
diff --git a/test/Modules/Inputs/namespaces-right.h b/test/Modules/Inputs/namespaces-right.h
index d103c00c82..b18aeb4478 100644
--- a/test/Modules/Inputs/namespaces-right.h
+++ b/test/Modules/Inputs/namespaces-right.h
@@ -39,3 +39,23 @@ namespace N9 {
namespace N10 {
int &f(int);
}
+
+
+
+
+
+
+
+namespace N11 {
+ namespace {
+ class Foo;
+ }
+ void consumeFoo(Foo*);
+}
+
+namespace N12 {
+ namespace {
+ class Foo;
+ }
+ void consumeFoo(Foo*);
+}
diff --git a/test/Modules/Inputs/namespaces-top.h b/test/Modules/Inputs/namespaces-top.h
index a69f43f183..0c607f5285 100644
--- a/test/Modules/Inputs/namespaces-top.h
+++ b/test/Modules/Inputs/namespaces-top.h
@@ -9,3 +9,6 @@ namespace N2 {
namespace N3 {
int& f(int);
}
+
+namespace N12 { }
+
diff --git a/test/Modules/namespaces.cpp b/test/Modules/namespaces.cpp
index 9ef966ac01..b60f75cff6 100644
--- a/test/Modules/namespaces.cpp
+++ b/test/Modules/namespaces.cpp
@@ -47,3 +47,14 @@ void testMergedMerged() {
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}}