Skip to content

Commit 83c633e

Browse files
committed
[clang][deps] Collect precompiled deps from submodules too
In this patch, the dependency scanner starts collecting precompiled dependencies from all encountered submodules, not only from top-level modules. Reviewed By: dexonsmith Differential Revision: https://reviews.llvm.org/D108540
1 parent 023f18b commit 83c633e

File tree

11 files changed

+186
-5
lines changed

11 files changed

+186
-5
lines changed

clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,11 @@ class ModuleDepCollectorPP final : public PPCallbacks {
172172

173173
/// Adds direct modular dependencies that have already been built to the
174174
/// ModuleDeps instance.
175-
void addDirectPrebuiltModuleDeps(const Module *M, ModuleDeps &MD);
175+
void
176+
addAllSubmodulePrebuiltDeps(const Module *M, ModuleDeps &MD,
177+
llvm::DenseSet<const Module *> &SeenSubmodules);
178+
void addModulePrebuiltDeps(const Module *M, ModuleDeps &MD,
179+
llvm::DenseSet<const Module *> &SeenSubmodules);
176180

177181
/// Traverses the previously collected direct modular dependencies to discover
178182
/// transitive modular dependencies and fills the parent \c ModuleDepCollector

clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ ModuleID ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
231231
// Add direct prebuilt module dependencies now, so that we can use them when
232232
// creating a CompilerInvocation and computing context hash for this
233233
// ModuleDeps instance.
234-
addDirectPrebuiltModuleDeps(M, MD);
234+
llvm::DenseSet<const Module *> SeenModules;
235+
addAllSubmodulePrebuiltDeps(M, MD, SeenModules);
235236

236237
MD.Invocation = MDC.makeInvocationForModuleBuildWithoutPaths(MD);
237238
MD.ID.ContextHash = MD.Invocation.getModuleHash();
@@ -242,12 +243,23 @@ ModuleID ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
242243
return MD.ID;
243244
}
244245

245-
void ModuleDepCollectorPP::addDirectPrebuiltModuleDeps(const Module *M,
246-
ModuleDeps &MD) {
246+
void ModuleDepCollectorPP::addAllSubmodulePrebuiltDeps(
247+
const Module *M, ModuleDeps &MD,
248+
llvm::DenseSet<const Module *> &SeenSubmodules) {
249+
addModulePrebuiltDeps(M, MD, SeenSubmodules);
250+
251+
for (const Module *SubM : M->submodules())
252+
addAllSubmodulePrebuiltDeps(SubM, MD, SeenSubmodules);
253+
}
254+
255+
void ModuleDepCollectorPP::addModulePrebuiltDeps(
256+
const Module *M, ModuleDeps &MD,
257+
llvm::DenseSet<const Module *> &SeenSubmodules) {
247258
for (const Module *Import : M->Imports)
248259
if (Import->getTopLevelModule() != M->getTopLevelModule())
249260
if (MDC.isPrebuiltModule(Import))
250-
MD.PrebuiltModuleDeps.emplace_back(Import);
261+
if (SeenSubmodules.insert(Import).second)
262+
MD.PrebuiltModuleDeps.emplace_back(Import);
251263
}
252264

253265
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-via-submodule/mod_common.h

Whitespace-only changes.

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

0 commit comments

Comments
 (0)