Skip to content

Commit 94e56ed

Browse files
committed
[clang][deps] Use top-level modules as precompiled dependencies
The `ASTReader` populates `Module::PresumedModuleMapFile` only for top-level modules, not submodules. To avoid generating empty `-fmodule-map-file=` arguments, make discovered modules depend on top-level precompiled modules. The granularity of submodules is not important here. The documentation of `Module::PresumedModuleMapFile` says this field is non-empty only when building from preprocessed source. This means there can still be cases where the dependency scanner generates empty `-fmodule-map-file=` arguments. That's being addressed in separate patch: D108544. Reviewed By: dexonsmith Differential Revision: https://reviews.llvm.org/D108647
1 parent 015bfe2 commit 94e56ed

File tree

10 files changed

+171
-3
lines changed

10 files changed

+171
-3
lines changed

clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,9 @@ void ModuleDepCollectorPP::addModulePrebuiltDeps(
275275
llvm::DenseSet<const Module *> &SeenSubmodules) {
276276
for (const Module *Import : M->Imports)
277277
if (Import->getTopLevelModule() != M->getTopLevelModule())
278-
if (MDC.isPrebuiltModule(Import))
279-
if (SeenSubmodules.insert(Import).second)
280-
MD.PrebuiltModuleDeps.emplace_back(Import);
278+
if (MDC.isPrebuiltModule(Import->getTopLevelModule()))
279+
if (SeenSubmodules.insert(Import->getTopLevelModule()).second)
280+
MD.PrebuiltModuleDeps.emplace_back(Import->getTopLevelModule());
281281
}
282282

283283
void ModuleDepCollectorPP::addAllSubmoduleDeps(
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"directory": "DIR",
4+
"command": "clang -x c-header DIR/pch.h -fmodules -gmodules -fimplicit-module-maps -fmodules-cache-path=DIR/cache -o DIR/pch.h.gch",
5+
"file": "DIR/pch.h"
6+
}
7+
]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"directory": "DIR",
4+
"command": "clang -fsyntax-only DIR/tu.c -fmodules -gmodules -fimplicit-module-maps -fmodules-cache-path=DIR/cache -include DIR/pch.h -o DIR/tu.o",
5+
"file": "DIR/tu.c"
6+
}
7+
]

clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/mod_common.h

Whitespace-only changes.

clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/mod_common_sub.h

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "mod_common_sub.h"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module ModCommon {
2+
header "mod_common.h"
3+
4+
module ModCommonSub {
5+
header "mod_common_sub.h"
6+
}
7+
}
8+
9+
module ModTU {
10+
header "mod_tu.h"
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "mod_common.h"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "mod_tu.h"
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// Check that when depending on a precompiled module, we depend on the
2+
// **top-level** module. Submodules don't have some information present (for
3+
// example the path to the modulemap file) and depending on them might cause
4+
// problems in the dependency scanner (e.g. generating empty `-fmodule-map-file=`
5+
// arguments).
6+
7+
// RUN: rm -rf %t && mkdir %t
8+
// RUN: cp %S/Inputs/modules-pch-common-submodule/* %t
9+
10+
// Scan dependencies of the PCH:
11+
//
12+
// RUN: sed "s|DIR|%/t|g" %S/Inputs/modules-pch-common-submodule/cdb_pch.json > %t/cdb.json
13+
// RUN: echo -%t > %t/result_pch.json
14+
// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-full \
15+
// RUN: -generate-modules-path-args -module-files-dir %t/build >> %t/result_pch.json
16+
// RUN: cat %t/result_pch.json | sed 's:\\\\\?:/:g' | FileCheck %s -check-prefix=CHECK-PCH
17+
//
18+
// CHECK-PCH: -[[PREFIX:.*]]
19+
// CHECK-PCH-NEXT: {
20+
// CHECK-PCH-NEXT: "modules": [
21+
// CHECK-PCH-NEXT: {
22+
// CHECK-PCH-NEXT: "clang-module-deps": [],
23+
// CHECK-PCH-NEXT: "clang-modulemap-file": "[[PREFIX]]/module.modulemap",
24+
// CHECK-PCH-NEXT: "command-line": [
25+
// CHECK-PCH-NEXT: "-cc1"
26+
// CHECK-PCH: "-emit-module"
27+
// CHECK-PCH: "-fmodules"
28+
// CHECK-PCH: "-fmodule-name=ModCommon"
29+
// CHECK-PCH: "-fno-implicit-modules"
30+
// CHECK-PCH: ],
31+
// CHECK-PCH-NEXT: "context-hash": "[[HASH_MOD_COMMON:.*]]",
32+
// CHECK-PCH-NEXT: "file-deps": [
33+
// CHECK-PCH-NEXT: "[[PREFIX]]/mod_common.h",
34+
// CHECK-PCH-NEXT: "[[PREFIX]]/mod_common_sub.h",
35+
// CHECK-PCH-NEXT: "[[PREFIX]]/module.modulemap"
36+
// CHECK-PCH-NEXT: ],
37+
// CHECK-PCH-NEXT: "name": "ModCommon"
38+
// CHECK-PCH-NEXT: }
39+
// CHECK-PCH-NEXT: ],
40+
// CHECK-PCH-NEXT: "translation-units": [
41+
// CHECK-PCH-NEXT: {
42+
// CHECK-PCH-NEXT: "clang-context-hash": "[[HASH_PCH:.*]]",
43+
// CHECK-PCH-NEXT: "clang-module-deps": [
44+
// CHECK-PCH-NEXT: {
45+
// CHECK-PCH-NEXT: "context-hash": "[[HASH_MOD_COMMON]]",
46+
// CHECK-PCH-NEXT: "module-name": "ModCommon"
47+
// CHECK-PCH-NEXT: }
48+
// CHECK-PCH-NEXT: ],
49+
// CHECK-PCH-NEXT: "command-line": [
50+
// CHECK-PCH-NEXT: "-fno-implicit-modules"
51+
// CHECK-PCH-NEXT: "-fno-implicit-module-maps"
52+
// CHECK-PCH-NEXT: "-fmodule-file=[[PREFIX]]/build/[[HASH_MOD_COMMON]]/ModCommon-{{.*}}.pcm"
53+
// CHECK-PCH-NEXT: "-fmodule-map-file=[[PREFIX]]/module.modulemap"
54+
// CHECK-PCH-NEXT: ],
55+
// CHECK-PCH-NEXT: "file-deps": [
56+
// CHECK-PCH-NEXT: "[[PREFIX]]/pch.h"
57+
// CHECK-PCH-NEXT: ],
58+
// CHECK-PCH-NEXT: "input-file": "[[PREFIX]]/pch.h"
59+
// CHECK-PCH-NEXT: }
60+
// CHECK-PCH-NEXT: ]
61+
// CHECK-PCH-NEXT: }
62+
63+
// Explicitly build the PCH:
64+
//
65+
// RUN: tail -n +2 %t/result_pch.json > %t/result_pch_stripped.json
66+
// RUN: %python %S/../../utils/module-deps-to-rsp.py %t/result_pch_stripped.json \
67+
// RUN: --module-name=ModCommon > %t/mod_common.cc1.rsp
68+
// RUN: %python %S/../../utils/module-deps-to-rsp.py %t/result_pch_stripped.json \
69+
// RUN: --tu-index=0 > %t/pch.rsp
70+
//
71+
// RUN: %clang @%t/mod_common.cc1.rsp
72+
// RUN: %clang -x c-header %t/pch.h -fmodules -gmodules -fimplicit-module-maps \
73+
// RUN: -fmodules-cache-path=%t/cache -o %t/pch.h.gch @%t/pch.rsp
74+
75+
// Scan dependencies of the TU:
76+
//
77+
// RUN: sed "s|DIR|%/t|g" %S/Inputs/modules-pch-common-submodule/cdb_tu.json > %t/cdb.json
78+
// RUN: echo -%t > %t/result_tu.json
79+
// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-full \
80+
// RUN: -generate-modules-path-args -module-files-dir %t/build >> %t/result_tu.json
81+
// RUN: cat %t/result_tu.json | sed 's:\\\\\?:/:g' | FileCheck %s -check-prefix=CHECK-TU
82+
//
83+
// CHECK-TU: -[[PREFIX:.*]]
84+
// CHECK-TU-NEXT: {
85+
// CHECK-TU-NEXT: "modules": [
86+
// CHECK-TU-NEXT: {
87+
// CHECK-TU-NEXT: "clang-module-deps": [],
88+
// CHECK-TU-NEXT: "clang-modulemap-file": "[[PREFIX]]/module.modulemap",
89+
// CHECK-TU-NEXT: "command-line": [
90+
// CHECK-TU-NEXT: "-cc1"
91+
// CHECK-TU: "-fmodule-map-file=[[PREFIX]]/module.modulemap"
92+
// CHECK-TU: "-emit-module"
93+
// CHECK-TU: "-fmodule-file=[[PREFIX]]/build/[[HASH_MOD_COMMON:.*]]/ModCommon-{{.*}}.pcm"
94+
// CHECK-TU: "-fmodules"
95+
// CHECK-TU: "-fmodule-name=ModTU"
96+
// CHECK-TU: "-fno-implicit-modules"
97+
// CHECK-TU: ],
98+
// CHECK-TU-NEXT: "context-hash": "[[HASH_MOD_TU:.*]]",
99+
// CHECK-TU-NEXT: "file-deps": [
100+
// CHECK-TU-NEXT: "[[PREFIX]]/mod_tu.h",
101+
// CHECK-TU-NEXT: "[[PREFIX]]/module.modulemap"
102+
// CHECK-TU-NEXT: ],
103+
// CHECK-TU-NEXT: "name": "ModTU"
104+
// CHECK-TU-NEXT: }
105+
// CHECK-TU-NEXT: ],
106+
// CHECK-TU-NEXT: "translation-units": [
107+
// CHECK-TU-NEXT: {
108+
// CHECK-TU-NEXT: "clang-context-hash": "[[HASH_TU:.*]]",
109+
// CHECK-TU-NEXT: "clang-module-deps": [
110+
// CHECK-TU-NEXT: {
111+
// CHECK-TU-NEXT: "context-hash": "[[HASH_MOD_TU]]"
112+
// CHECK-TU-NEXT: "module-name": "ModTU"
113+
// CHECK-TU-NEXT: }
114+
// CHECK-TU-NEXT: ],
115+
// CHECK-TU-NEXT: "command-line": [
116+
// CHECK-TU-NEXT: "-fno-implicit-modules",
117+
// CHECK-TU-NEXT: "-fno-implicit-module-maps",
118+
// CHECK-TU-NEXT: "-fmodule-file=[[PREFIX]]/build/[[HASH_MOD_TU:.*]]/ModTU-{{.*}}.pcm",
119+
// CHECK-TU-NEXT: "-fmodule-map-file=[[PREFIX]]/module.modulemap"
120+
// CHECK-TU-NEXT: ],
121+
// CHECK-TU-NEXT: "file-deps": [
122+
// CHECK-TU-NEXT: "[[PREFIX]]/tu.c",
123+
// CHECK-TU-NEXT: "[[PREFIX]]/pch.h.gch"
124+
// CHECK-TU-NEXT: ],
125+
// CHECK-TU-NEXT: "input-file": "[[PREFIX]]/tu.c"
126+
// CHECK-TU-NEXT: }
127+
// CHECK-TU-NEXT: ]
128+
// CHECK-TU-NEXT: }
129+
130+
// Explicitly build the TU:
131+
//
132+
// RUN: tail -n +2 %t/result_tu.json > %t/result_tu_stripped.json
133+
// RUN: %python %S/../../utils/module-deps-to-rsp.py %t/result_tu_stripped.json \
134+
// RUN: --module-name=ModTU > %t/mod_tu.cc1.rsp
135+
// RUN: %python %S/../../utils/module-deps-to-rsp.py %t/result_tu_stripped.json \
136+
// RUN: --tu-index=0 > %t/tu.rsp
137+
//
138+
// RUN: %clang @%t/mod_tu.cc1.rsp
139+
// RUN: %clang -fsyntax-only %t/tu.c -fmodules -gmodules -fimplicit-module-maps \
140+
// RUN: -fmodules-cache-path=%t/cache -include %t/pch.h -o %t/tu.o @%t/tu.rsp

0 commit comments

Comments
 (0)