summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Smith <richard-llvm@metafoo.co.uk>2014-07-18 04:53:37 +0000
committerRichard Smith <richard-llvm@metafoo.co.uk>2014-07-18 04:53:37 +0000
commitfdee4dcca3472941066cceb288fcb7c8f4856fa1 (patch)
tree703268dcf40b8b3ec66b62598b9adde37553b25f
parente0eea0c6faf28d50c6e8f7ed5cf25ff0dd03f2cc (diff)
[modules] Fix macro hiding bug exposed if:
* A submodule of module A is imported into module B * Another submodule of module A that is not imported into B exports a macro * Some submodule of module B also exports a definition of the macro, and happens to be the first submodule of B that imports module A. In this case, we would incorrectly determine that A's macro redefines B's macro, and so we don't need to re-export B's macro at all. This happens with the 'assert' macro in an LLVM self-host. =( git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@213348 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/clang/Serialization/ASTReader.h5
-rw-r--r--lib/Serialization/ASTReader.cpp19
-rw-r--r--lib/Serialization/ASTWriter.cpp12
-rw-r--r--test/Modules/Inputs/macro-hiding/a1.h1
-rw-r--r--test/Modules/Inputs/macro-hiding/a2.h0
-rw-r--r--test/Modules/Inputs/macro-hiding/b1.h0
-rw-r--r--test/Modules/Inputs/macro-hiding/b2.h2
-rw-r--r--test/Modules/Inputs/macro-hiding/c1.h2
-rw-r--r--test/Modules/Inputs/macro-hiding/module.modulemap11
-rw-r--r--test/Modules/macro-hiding.cpp6
10 files changed, 48 insertions, 10 deletions
diff --git a/include/clang/Serialization/ASTReader.h b/include/clang/Serialization/ASTReader.h
index 0698b98abb..3f44440d4b 100644
--- a/include/clang/Serialization/ASTReader.h
+++ b/include/clang/Serialization/ASTReader.h
@@ -1366,7 +1366,8 @@ public:
bool Complain);
/// \brief Make the names within this set of hidden names visible.
- void makeNamesVisible(const HiddenNames &Names, Module *Owner);
+ void makeNamesVisible(const HiddenNames &Names, Module *Owner,
+ bool FromFinalization);
/// \brief Set the AST callbacks listener.
void setListener(ASTReaderListener *listener) {
@@ -1831,7 +1832,7 @@ public:
ModuleFile &M, uint64_t Offset);
void installImportedMacro(IdentifierInfo *II, ModuleMacroInfo *MMI,
- Module *Owner);
+ Module *Owner, bool FromFinalization);
typedef llvm::TinyPtrVector<DefMacroDirective *> AmbiguousMacros;
llvm::DenseMap<IdentifierInfo*, AmbiguousMacros> AmbiguousMacroDefs;
diff --git a/lib/Serialization/ASTReader.cpp b/lib/Serialization/ASTReader.cpp
index f18122dd94..419f6b3200 100644
--- a/lib/Serialization/ASTReader.cpp
+++ b/lib/Serialization/ASTReader.cpp
@@ -1790,7 +1790,7 @@ void ASTReader::resolvePendingMacro(IdentifierInfo *II,
// install if we make this module visible.
HiddenNamesMap[Owner].HiddenMacros.insert(std::make_pair(II, MMI));
} else {
- installImportedMacro(II, MMI, Owner);
+ installImportedMacro(II, MMI, Owner, /*FromFinalization*/false);
}
}
@@ -1941,11 +1941,11 @@ ASTReader::removeOverriddenMacros(IdentifierInfo *II,
}
void ASTReader::installImportedMacro(IdentifierInfo *II, ModuleMacroInfo *MMI,
- Module *Owner) {
+ Module *Owner, bool FromFinalization) {
assert(II && Owner);
SourceLocation ImportLoc = Owner->MacroVisibilityLoc;
- if (ImportLoc.isInvalid()) {
+ if (ImportLoc.isInvalid() && !FromFinalization) {
// FIXME: If we made macros from this module visible but didn't provide a
// source location for the import, we don't have a location for the macro.
// Use the location at which the containing module file was first imported
@@ -3320,7 +3320,9 @@ static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
}
}
-void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
+void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner,
+ bool FromFinalization) {
+ // FIXME: Only do this if Owner->NameVisibility == AllVisible.
for (unsigned I = 0, N = Names.HiddenDecls.size(); I != N; ++I) {
Decl *D = Names.HiddenDecls[I];
bool wasHidden = D->Hidden;
@@ -3333,10 +3335,12 @@ void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
}
}
+ assert((FromFinalization || Owner->NameVisibility >= Module::MacrosVisible) &&
+ "nothing to make visible?");
for (HiddenMacrosMap::const_iterator I = Names.HiddenMacros.begin(),
E = Names.HiddenMacros.end();
I != E; ++I)
- installImportedMacro(I->first, I->second, Owner);
+ installImportedMacro(I->first, I->second, Owner, FromFinalization);
}
void ASTReader::makeModuleVisible(Module *Mod,
@@ -3370,7 +3374,8 @@ void ASTReader::makeModuleVisible(Module *Mod,
// mark them as visible.
HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
if (Hidden != HiddenNamesMap.end()) {
- makeNamesVisible(Hidden->second, Hidden->first);
+ makeNamesVisible(Hidden->second, Hidden->first,
+ /*FromFinalization*/false);
HiddenNamesMap.erase(Hidden);
}
@@ -3897,7 +3902,7 @@ void ASTReader::finalizeForWriting() {
for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
HiddenEnd = HiddenNamesMap.end();
Hidden != HiddenEnd; ++Hidden) {
- makeNamesVisible(Hidden->second, Hidden->first);
+ makeNamesVisible(Hidden->second, Hidden->first, /*FromFinalization*/true);
}
HiddenNamesMap.clear();
}
diff --git a/lib/Serialization/ASTWriter.cpp b/lib/Serialization/ASTWriter.cpp
index 97a5d0e9d2..f0b1752368 100644
--- a/lib/Serialization/ASTWriter.cpp
+++ b/lib/Serialization/ASTWriter.cpp
@@ -3089,7 +3089,17 @@ class ASTIdentifierTableTrait {
}
SubmoduleID getSubmoduleID(MacroDirective *MD) {
- return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
+ if (MD->getLocation().isValid())
+ return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
+
+ // If we have no directive location, this macro was installed when
+ // finalizing the ASTReader.
+ if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD))
+ return DefMD->getInfo()->getOwningModuleID();
+
+ // Skip imports that only produce #undefs for now.
+ // FIXME: We should still re-export them!
+ return 0;
}
public:
diff --git a/test/Modules/Inputs/macro-hiding/a1.h b/test/Modules/Inputs/macro-hiding/a1.h
new file mode 100644
index 0000000000..b17c8eeb69
--- /dev/null
+++ b/test/Modules/Inputs/macro-hiding/a1.h
@@ -0,0 +1 @@
+#define assert(x)
diff --git a/test/Modules/Inputs/macro-hiding/a2.h b/test/Modules/Inputs/macro-hiding/a2.h
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/test/Modules/Inputs/macro-hiding/a2.h
diff --git a/test/Modules/Inputs/macro-hiding/b1.h b/test/Modules/Inputs/macro-hiding/b1.h
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/test/Modules/Inputs/macro-hiding/b1.h
diff --git a/test/Modules/Inputs/macro-hiding/b2.h b/test/Modules/Inputs/macro-hiding/b2.h
new file mode 100644
index 0000000000..83918489e9
--- /dev/null
+++ b/test/Modules/Inputs/macro-hiding/b2.h
@@ -0,0 +1,2 @@
+#include "a2.h"
+#define assert(x)
diff --git a/test/Modules/Inputs/macro-hiding/c1.h b/test/Modules/Inputs/macro-hiding/c1.h
new file mode 100644
index 0000000000..4b78b3c273
--- /dev/null
+++ b/test/Modules/Inputs/macro-hiding/c1.h
@@ -0,0 +1,2 @@
+#include "b1.h"
+#define assert(x)
diff --git a/test/Modules/Inputs/macro-hiding/module.modulemap b/test/Modules/Inputs/macro-hiding/module.modulemap
new file mode 100644
index 0000000000..2657ab1692
--- /dev/null
+++ b/test/Modules/Inputs/macro-hiding/module.modulemap
@@ -0,0 +1,11 @@
+module a {
+ module a1 { header "a1.h" export * }
+ module a2 { header "a2.h" export * }
+}
+module b {
+ module b1 { header "b1.h" export * }
+ module b2 { header "b2.h" export * }
+}
+module c {
+ module c1 { header "c1.h" export * }
+}
diff --git a/test/Modules/macro-hiding.cpp b/test/Modules/macro-hiding.cpp
new file mode 100644
index 0000000000..a7a6064e64
--- /dev/null
+++ b/test/Modules/macro-hiding.cpp
@@ -0,0 +1,6 @@
+// RUN: rm -rf %t
+// RUN: %clang_cc1 -fmodules -fmodules-cache-path=%t -I%S/Inputs/macro-hiding %s
+#include "c1.h"
+#include "b2.h"
+
+void h() { assert(true); }