Skip to content

Commit 83973cf

Browse files
committed
[clang][modules][deps] Including module maps are affecting
With this patch, we mark module maps that include an affecting `extern` module map as also affecting. This is a generalization of D137197: now we don't require the importing module map to describe parent of the extern module. Depends on D137198. Reviewed By: Bigcheese Differential Revision: https://reviews.llvm.org/D137206
1 parent f99e5a9 commit 83973cf

File tree

2 files changed

+155
-4
lines changed

2 files changed

+155
-4
lines changed

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,14 @@ static TypeCode getTypeCodeForTypeClass(Type::TypeClass id) {
161161

162162
namespace {
163163

164-
std::set<const FileEntry *> GetAffectingModuleMaps(const HeaderSearch &HS,
164+
std::set<const FileEntry *> GetAffectingModuleMaps(const Preprocessor &PP,
165165
Module *RootModule) {
166166
std::set<const FileEntry *> ModuleMaps{};
167167
std::set<const Module *> ProcessedModules;
168168
SmallVector<const Module *> ModulesToProcess{RootModule};
169169

170+
const HeaderSearch &HS = PP.getHeaderSearchInfo();
171+
170172
SmallVector<const FileEntry *, 16> FilesByUID;
171173
HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
172174

@@ -191,12 +193,27 @@ std::set<const FileEntry *> GetAffectingModuleMaps(const HeaderSearch &HS,
191193
}
192194

193195
const ModuleMap &MM = HS.getModuleMap();
196+
SourceManager &SourceMgr = PP.getSourceManager();
197+
198+
auto ForIncludeChain = [&](FileEntryRef F,
199+
llvm::function_ref<void(FileEntryRef)> CB) {
200+
CB(F);
201+
FileID FID = SourceMgr.translateFile(F);
202+
SourceLocation Loc = SourceMgr.getIncludeLoc(FID);
203+
while (Loc.isValid()) {
204+
FID = SourceMgr.getFileID(Loc);
205+
CB(*SourceMgr.getFileEntryRefForID(FID));
206+
Loc = SourceMgr.getIncludeLoc(FID);
207+
}
208+
};
194209

195210
auto ProcessModuleOnce = [&](const Module *M) {
196211
for (const Module *Mod = M; Mod; Mod = Mod->Parent)
197212
if (ProcessedModules.insert(Mod).second)
198213
if (auto ModuleMapFile = MM.getModuleMapFileForUniquing(Mod))
199-
ModuleMaps.insert(*ModuleMapFile);
214+
ForIncludeChain(*ModuleMapFile, [&](FileEntryRef F) {
215+
ModuleMaps.insert(F);
216+
});
200217
};
201218

202219
for (const Module *CurrentModule : ModulesToProcess) {
@@ -4545,8 +4562,7 @@ void ASTWriter::collectNonAffectingInputFiles() {
45454562
if (!WritingModule)
45464563
return;
45474564

4548-
auto AffectingModuleMaps =
4549-
GetAffectingModuleMaps(PP->getHeaderSearchInfo(), WritingModule);
4565+
auto AffectingModuleMaps = GetAffectingModuleMaps(*PP, WritingModule);
45504566

45514567
unsigned FileIDAdjustment = 0;
45524568
unsigned OffsetAdjustment = 0;
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// RUN: rm -rf %t
2+
// RUN: split-file %s %t
3+
4+
//--- tu.m
5+
@import zeroth;
6+
7+
//--- zeroth/module.modulemap
8+
module zeroth { header "zeroth.h" }
9+
//--- zeroth/zeroth.h
10+
@import first;
11+
#include "second.h"
12+
13+
//--- first/module.modulemap
14+
module first {}
15+
module first_other { header "first_other.h" }
16+
//--- first/first_other.h
17+
18+
//--- second/module.modulemap
19+
extern module second "second.modulemap"
20+
//--- second/second.modulemap
21+
module second { header "second.h" }
22+
//--- second/second.h
23+
#include "first_other.h"
24+
25+
//--- cdb.json.template
26+
[{
27+
"directory": "DIR",
28+
"file": "DIR/tu.m",
29+
"command": "clang -fmodules -fmodules-cache-path=DIR/cache -I DIR/zeroth -I DIR/first -I DIR/second -c DIR/tu.m -o DIR/tu.o"
30+
}]
31+
32+
// RUN: sed -e "s|DIR|%/t|g" -e "s|INPUTS|%/S/Inputs|g" %t/cdb.json.template > %t/cdb.json
33+
// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-full > %t/result.json
34+
// RUN: cat %t/result.json | sed 's:\\\\\?:/:g' | FileCheck %s -DPREFIX=%/t
35+
36+
// CHECK: {
37+
// CHECK-NEXT: "modules": [
38+
// CHECK-NEXT: {
39+
// CHECK-NEXT: "clang-module-deps": [],
40+
// CHECK-NEXT: "clang-modulemap-file": "[[PREFIX]]/first/module.modulemap",
41+
// CHECK-NEXT: "command-line": [
42+
// CHECK: ],
43+
// CHECK-NEXT: "context-hash": "{{.*}}",
44+
// CHECK-NEXT: "file-deps": [
45+
// CHECK-NEXT: "[[PREFIX]]/first/module.modulemap"
46+
// CHECK-NEXT: ],
47+
// CHECK-NEXT: "name": "first"
48+
// CHECK-NEXT: },
49+
// CHECK-NEXT: {
50+
// CHECK-NEXT: "clang-module-deps": [],
51+
// CHECK-NEXT: "clang-modulemap-file": "[[PREFIX]]/first/module.modulemap",
52+
// CHECK-NEXT: "command-line": [
53+
// CHECK: ],
54+
// CHECK-NEXT: "context-hash": "{{.*}}",
55+
// CHECK-NEXT: "file-deps": [
56+
// CHECK-NEXT: "[[PREFIX]]/first/first_other.h",
57+
// CHECK-NEXT: "[[PREFIX]]/first/module.modulemap"
58+
// CHECK-NEXT: ],
59+
// CHECK-NEXT: "name": "first_other"
60+
// CHECK-NEXT: },
61+
// CHECK-NEXT: {
62+
// CHECK-NEXT: "clang-module-deps": [
63+
// CHECK-NEXT: {
64+
// CHECK-NEXT: "context-hash": "{{.*}}",
65+
// CHECK-NEXT: "module-name": "first_other"
66+
// CHECK-NEXT: }
67+
// CHECK-NEXT: ],
68+
// CHECK-NEXT: "clang-modulemap-file": "[[PREFIX]]/second/second.modulemap",
69+
// CHECK-NEXT: "command-line": [
70+
// CHECK: ],
71+
// CHECK-NEXT: "context-hash": "{{.*}}",
72+
// CHECK-NEXT: "file-deps": [
73+
// CHECK-NEXT: "[[PREFIX]]/first/module.modulemap",
74+
// CHECK-NEXT: "[[PREFIX]]/second/second.h",
75+
// CHECK-NEXT: "[[PREFIX]]/second/second.modulemap"
76+
// CHECK-NEXT: ],
77+
// CHECK-NEXT: "name": "second"
78+
// CHECK-NEXT: },
79+
// CHECK-NEXT: {
80+
// CHECK-NEXT: "clang-module-deps": [
81+
// CHECK-NEXT: {
82+
// CHECK-NEXT: "context-hash": "{{.*}}",
83+
// CHECK-NEXT: "module-name": "first"
84+
// CHECK-NEXT: },
85+
// CHECK-NEXT: {
86+
// CHECK-NEXT: "context-hash": "{{.*}}",
87+
// CHECK-NEXT: "module-name": "second"
88+
// CHECK-NEXT: }
89+
// CHECK-NEXT: ],
90+
// CHECK-NEXT: "clang-modulemap-file": "[[PREFIX]]/zeroth/module.modulemap",
91+
// CHECK-NEXT: "command-line": [
92+
// CHECK: ],
93+
// CHECK-NEXT: "context-hash": "{{.*}}",
94+
// CHECK-NEXT: "file-deps": [
95+
// CHECK-NEXT: "[[PREFIX]]/first/module.modulemap",
96+
// CHECK-NEXT: "[[PREFIX]]/second/module.modulemap",
97+
// CHECK-NEXT: "[[PREFIX]]/second/second.modulemap",
98+
// CHECK-NEXT: "[[PREFIX]]/zeroth/module.modulemap",
99+
// CHECK-NEXT: "[[PREFIX]]/zeroth/zeroth.h"
100+
// CHECK-NEXT: ],
101+
// CHECK-NEXT: "name": "zeroth"
102+
// CHECK-NEXT: }
103+
// CHECK-NEXT: ],
104+
// CHECK-NEXT: "translation-units": [
105+
// CHECK-NEXT: {
106+
// CHECK-NEXT: "commands": [
107+
// CHECK-NEXT: {
108+
// CHECK-NEXT: "clang-context-hash": "{{.*}}",
109+
// CHECK-NEXT: "clang-module-deps": [
110+
// CHECK-NEXT: {
111+
// CHECK-NEXT: "context-hash": "{{.*}}",
112+
// CHECK-NEXT: "module-name": "zeroth"
113+
// CHECK-NEXT: }
114+
// CHECK-NEXT: ],
115+
// CHECK-NEXT: "command-line": [
116+
// CHECK: ],
117+
// CHECK-NEXT: "executable": "clang",
118+
// CHECK-NEXT: "file-deps": [
119+
// CHECK-NEXT: "[[PREFIX]]/tu.m"
120+
// CHECK-NEXT: ],
121+
// CHECK-NEXT: "input-file": "[[PREFIX]]/tu.m"
122+
// CHECK-NEXT: }
123+
// CHECK-NEXT: ]
124+
// CHECK-NEXT: }
125+
// CHECK-NEXT: ]
126+
// CHECK-NEXT: }
127+
128+
// RUN: %deps-to-rsp --module-name=first %t/result.json > %t/first.cc1.rsp
129+
// RUN: %deps-to-rsp --module-name=first_other %t/result.json > %t/first_other.cc1.rsp
130+
// RUN: %deps-to-rsp --module-name=second %t/result.json > %t/second.cc1.rsp
131+
// RUN: %deps-to-rsp --module-name=zeroth %t/result.json > %t/zeroth.cc1.rsp
132+
// RUN: %clang @%t/first.cc1.rsp
133+
// RUN: %clang @%t/first_other.cc1.rsp
134+
// RUN: %clang @%t/second.cc1.rsp
135+
// RUN: %clang @%t/zeroth.cc1.rsp

0 commit comments

Comments
 (0)