Skip to content

Commit 4bb04d4

Browse files
authored
[clang][modules] Fix local submodule visibility of macros from transitive import (llvm#122955)
When we mark a module visible, we normally mark all of its non-explicit submodules and other exports as visible. However, when we first enter a submodule we should not make them visible to the submodule itself until they are actually imported. Marking exports visible before import would cause bizarre behaviour with local submodule visibility, because it happened before we discovered the submodule's transitive imports and could fail to make them visible in the parent module depending on whether the submodules involved were explicitly defined (module X) or implicitly defined from an umbrella (module *). rdar://136524433
1 parent 9738f20 commit 4bb04d4

File tree

6 files changed

+85
-16
lines changed

6 files changed

+85
-16
lines changed

clang/include/clang/Basic/Module.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -881,10 +881,11 @@ class VisibleModuleSet {
881881
StringRef Message)>;
882882

883883
/// Make a specific module visible.
884-
void setVisible(Module *M, SourceLocation Loc,
885-
VisibleCallback Vis = [](Module *) {},
886-
ConflictCallback Cb = [](ArrayRef<Module *>, Module *,
887-
StringRef) {});
884+
void setVisible(
885+
Module *M, SourceLocation Loc, bool IncludeExports = true,
886+
VisibleCallback Vis = [](Module *) {},
887+
ConflictCallback Cb = [](ArrayRef<Module *>, Module *, StringRef) {});
888+
888889
private:
889890
/// Import locations for each visible module. Indexed by the module's
890891
/// VisibilityID.

clang/include/clang/Lex/Preprocessor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1755,7 +1755,8 @@ class Preprocessor {
17551755
bool LexAfterModuleImport(Token &Result);
17561756
void CollectPpImportSuffix(SmallVectorImpl<Token> &Toks);
17571757

1758-
void makeModuleVisible(Module *M, SourceLocation Loc);
1758+
void makeModuleVisible(Module *M, SourceLocation Loc,
1759+
bool IncludeExports = true);
17591760

17601761
SourceLocation getModuleImportLoc(Module *M) const {
17611762
return CurSubmoduleState->VisibleModules.getImportLoc(M);

clang/lib/Basic/Module.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,8 @@ LLVM_DUMP_METHOD void Module::dump() const {
662662
}
663663

664664
void VisibleModuleSet::setVisible(Module *M, SourceLocation Loc,
665-
VisibleCallback Vis, ConflictCallback Cb) {
665+
bool IncludeExports, VisibleCallback Vis,
666+
ConflictCallback Cb) {
666667
// We can't import a global module fragment so the location can be invalid.
667668
assert((M->isGlobalModule() || Loc.isValid()) &&
668669
"setVisible expects a valid import location");
@@ -688,12 +689,14 @@ void VisibleModuleSet::setVisible(Module *M, SourceLocation Loc,
688689
Vis(V.M);
689690

690691
// Make any exported modules visible.
691-
SmallVector<Module *, 16> Exports;
692-
V.M->getExportedModules(Exports);
693-
for (Module *E : Exports) {
694-
// Don't import non-importable modules.
695-
if (!E->isUnimportable())
696-
VisitModule({E, &V});
692+
if (IncludeExports) {
693+
SmallVector<Module *, 16> Exports;
694+
V.M->getExportedModules(Exports);
695+
for (Module *E : Exports) {
696+
// Don't import non-importable modules.
697+
if (!E->isUnimportable())
698+
VisitModule({E, &V});
699+
}
697700
}
698701

699702
for (auto &C : V.M->Conflicts) {

clang/lib/Lex/PPLexerChange.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -754,9 +754,10 @@ void Preprocessor::EnterSubmodule(Module *M, SourceLocation ImportLoc,
754754
// Switch to this submodule as the current submodule.
755755
CurSubmoduleState = &State;
756756

757-
// This module is visible to itself.
757+
// This module is visible to itself, but exports should not be made visible
758+
// until they are imported.
758759
if (FirstTime)
759-
makeModuleVisible(M, ImportLoc);
760+
makeModuleVisible(M, ImportLoc, /*IncludeExports=*/false);
760761
}
761762

762763
bool Preprocessor::needModuleMacros() const {

clang/lib/Lex/Preprocessor.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,9 +1331,10 @@ bool Preprocessor::LexAfterModuleImport(Token &Result) {
13311331
return true;
13321332
}
13331333

1334-
void Preprocessor::makeModuleVisible(Module *M, SourceLocation Loc) {
1334+
void Preprocessor::makeModuleVisible(Module *M, SourceLocation Loc,
1335+
bool IncludeExports) {
13351336
CurSubmoduleState->VisibleModules.setVisible(
1336-
M, Loc, [](Module *) {},
1337+
M, Loc, IncludeExports, [](Module *) {},
13371338
[&](ArrayRef<Module *> Path, Module *Conflict, StringRef Message) {
13381339
// FIXME: Include the path in the diagnostic.
13391340
// FIXME: Include the import location for the conflicting module.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Checks that macros from transitive imports work with local submodule
2+
// visibility. In the below test, previously a() and d() failed because
3+
// OTHER_MACRO1 and OTHER_MACRO3 were not visible at the use site.
4+
5+
// RUN: rm -rf %t
6+
// RUN: split-file %s %t
7+
// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t \
8+
// RUN: -fmodules-local-submodule-visibility -I%t %t/tu.c -verify
9+
10+
//--- Other1.h
11+
#define OTHER_MACRO1(...)
12+
13+
//--- Other2.h
14+
#define OTHER_MACRO2(...)
15+
16+
//--- Other3.h
17+
#define OTHER_MACRO3(...)
18+
19+
//--- module.modulemap
20+
module Other {
21+
module O1 { header "Other1.h" }
22+
module O2 { header "Other2.h" }
23+
module O3 { header "Other3.h" }
24+
}
25+
26+
//--- Top/A.h
27+
#include "Other1.h"
28+
#define MACRO_A OTHER_MACRO1(x, y)
29+
30+
//--- Top/B.h
31+
#include "Other2.h"
32+
#define MACRO_B OTHER_MACRO2(x, y)
33+
34+
//--- Top/C.h
35+
#include "D.h"
36+
37+
//--- Top/D.h
38+
#include "Other3.h"
39+
#define MACRO_D OTHER_MACRO3(x, y)
40+
41+
//--- Top/Top.h
42+
#include "A.h"
43+
#include "B.h"
44+
#include "C.h"
45+
46+
void a() MACRO_A;
47+
void b() MACRO_B;
48+
void d() MACRO_D;
49+
50+
//--- Top/module.modulemap
51+
module Top {
52+
umbrella header "Top.h"
53+
module A { header "A.h" export * }
54+
module D { header "D.h" export * }
55+
module * { export * }
56+
export *
57+
export Other.O3
58+
}
59+
60+
//--- tu.c
61+
#include "Top/Top.h"
62+
// expected-no-diagnostics

0 commit comments

Comments
 (0)