Skip to content

Commit a1435eb

Browse files
committed
[clang][modules] Only avoid pruning module maps when asked to (llvm#89428)
Pruning non-affecting module maps is useful even when passing module maps explicitly via `-fmodule-map-file=<path>`. For this situation, this patch reinstates the behavior we had prior to llvm#87849. For the situation where the explicit module map file arguments were generated by the dependency scanner (which already pruned the non-affecting ones), this patch introduces new `-cc1` flag `-fno-modules-prune-non-affecting-module-map-files` that avoids the extra work. (cherry picked from commit 3ea5dff)
1 parent 0f54a18 commit a1435eb

File tree

7 files changed

+83
-36
lines changed

7 files changed

+83
-36
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2579,6 +2579,11 @@ defm modules_skip_header_search_paths : BoolFOption<"modules-skip-header-search-
25792579
HeaderSearchOpts<"ModulesSkipHeaderSearchPaths">, DefaultFalse,
25802580
PosFlag<SetTrue, [], "Disable writing header search paths">,
25812581
NegFlag<SetFalse>, BothFlags<[CC1Option]>>;
2582+
def fno_modules_prune_non_affecting_module_map_files :
2583+
Flag<["-"], "fno-modules-prune-non-affecting-module-map-files">,
2584+
Group<f_Group>, Flags<[CC1Option]>,
2585+
MarshallingInfoNegativeFlag<HeaderSearchOpts<"ModulesPruneNonAffectingModuleMaps">>,
2586+
HelpText<"Do not prune non-affecting module map files when writing module files">;
25822587

25832588
def fincremental_extensions :
25842589
Flag<["-"], "fincremental-extensions">,

clang/include/clang/Lex/HeaderSearchOptions.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ class HeaderSearchOptions {
228228
/// Primarily used to speed up deserialization during dependency scanning.
229229
unsigned ModulesSkipPragmaDiagnosticMappings : 1;
230230

231+
/// Whether to prune non-affecting module map files from PCM files.
232+
unsigned ModulesPruneNonAffectingModuleMaps : 1;
233+
231234
unsigned ModulesHashContent : 1;
232235

233236
/// Whether we should include all things that could impact the module in the
@@ -252,7 +255,8 @@ class HeaderSearchOptions {
252255
ModulesValidateDiagnosticOptions(true),
253256
ModulesSkipDiagnosticOptions(false),
254257
ModulesSkipHeaderSearchPaths(false),
255-
ModulesSkipPragmaDiagnosticMappings(false), ModulesHashContent(false),
258+
ModulesSkipPragmaDiagnosticMappings(false),
259+
ModulesPruneNonAffectingModuleMaps(true), ModulesHashContent(false),
256260
ModulesStrictContextHash(false), ModulesIncludeVFSUsage(false) {}
257261

258262
/// AddPath - Add the \p Path path to the specified \p Group list.

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ namespace {
163163

164164
std::optional<std::set<const FileEntry *>>
165165
GetAffectingModuleMaps(const Preprocessor &PP, Module *RootModule) {
166-
// Without implicit module map search, there's no good reason to know about
167-
// any module maps that are not affecting.
168-
if (!PP.getHeaderSearchInfo().getHeaderSearchOpts().ImplicitModuleMaps)
166+
if (!PP.getHeaderSearchInfo()
167+
.getHeaderSearchOpts()
168+
.ModulesPruneNonAffectingModuleMaps)
169169
return std::nullopt;
170170

171171
SmallVector<const Module *> ModulesToProcess{RootModule};

clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ makeCommonInvocationForModuleBuild(CompilerInvocation CI) {
185185
CI.resetNonModularOptions();
186186
CI.clearImplicitModuleBuildOptions();
187187

188+
// The scanner takes care to avoid passing non-affecting module maps to the
189+
// explicit compiles. No need to do extra work just to find out there are no
190+
// module map files to prune.
191+
CI.getHeaderSearchOpts().ModulesPruneNonAffectingModuleMaps = false;
192+
188193
// Remove options incompatible with explicit module build or are likely to
189194
// differ between identical modules discovered from different translation
190195
// units.

clang/test/ClangScanDeps/modules-full.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
// CHECK-NEXT: "command-line": [
3434
// CHECK-NEXT: "-cc1"
3535
// CHECK: "-emit-module"
36+
// CHECK: "-fno-modules-prune-non-affecting-module-map-files"
3637
// CHECK: "-fmodule-file={{.*}}[[PREFIX]]/module-cache{{(_clangcl)?}}/[[HASH_H2_DINCLUDE]]/header2-{{[A-Z0-9]+}}.pcm"
3738
// CHECK-NOT: "-fimplicit-module-maps"
3839
// CHECK: "-fmodule-name=header1"
@@ -51,6 +52,7 @@
5152
// CHECK-NEXT: "command-line": [
5253
// CHECK-NEXT: "-cc1",
5354
// CHECK: "-emit-module",
55+
// CHECK: "-fno-modules-prune-non-affecting-module-map-files"
5456
// CHECK-NOT: "-fimplicit-module-maps",
5557
// CHECK: "-fmodule-name=header1",
5658
// CHECK: "-fno-implicit-modules",
@@ -68,6 +70,7 @@
6870
// CHECK-NEXT: "command-line": [
6971
// CHECK-NEXT: "-cc1",
7072
// CHECK: "-emit-module",
73+
// CHECK: "-fno-modules-prune-non-affecting-module-map-files"
7174
// CHECK: "-fmodule-name=header2",
7275
// CHECK-NOT: "-fimplicit-module-maps",
7376
// CHECK: "-fno-implicit-modules",

clang/test/Modules/add-remove-irrelevant-module-map.m

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Check that the presence of non-affecting module map files does not affect the
2+
// contents of PCM files.
3+
4+
// RUN: rm -rf %t && mkdir %t
5+
// RUN: split-file %s %t
6+
7+
//--- a/module.modulemap
8+
module a {}
9+
10+
//--- b/module.modulemap
11+
module b {}
12+
13+
//--- c/module.modulemap
14+
module c { header "c.h" }
15+
//--- c/c.h
16+
@import b;
17+
18+
//--- tu.m
19+
@import c;
20+
21+
//--- explicit-mms-common-args.rsp
22+
-fmodule-map-file=b/module.modulemap -fmodule-map-file=c/module.modulemap -fmodules -fmodules-cache-path=cache -fdisable-module-hash -fsyntax-only tu.m
23+
//--- implicit-search-args.rsp
24+
-I a -I b -I c -fimplicit-module-maps -fmodules -fmodules-cache-path=cache -fdisable-module-hash -fsyntax-only tu.m
25+
//--- implicit-search-args.rsp-end
26+
27+
// Test with explicit module map files.
28+
//
29+
// RUN: %clang_cc1 -working-directory %t @%t/explicit-mms-common-args.rsp
30+
// RUN: mv %t/cache %t/cache-explicit-no-a-prune
31+
// RUN: %clang_cc1 -working-directory %t @%t/explicit-mms-common-args.rsp -fno-modules-prune-non-affecting-module-map-files
32+
// RUN: mv %t/cache %t/cache-explicit-no-a-keep
33+
//
34+
// RUN: %clang_cc1 -working-directory %t -fmodule-map-file=a/module.modulemap @%t/explicit-mms-common-args.rsp
35+
// RUN: mv %t/cache %t/cache-explicit-a-prune
36+
// RUN: %clang_cc1 -working-directory %t -fmodule-map-file=a/module.modulemap @%t/explicit-mms-common-args.rsp -fno-modules-prune-non-affecting-module-map-files
37+
// RUN: mv %t/cache %t/cache-explicit-a-keep
38+
//
39+
// RUN: diff %t/cache-explicit-no-a-prune/c.pcm %t/cache-explicit-a-prune/c.pcm
40+
// RUN: not diff %t/cache-explicit-no-a-keep/c.pcm %t/cache-explicit-a-keep/c.pcm
41+
42+
// Test with implicit module map search.
43+
//
44+
// RUN: %clang_cc1 -working-directory %t @%t/implicit-search-args.rsp
45+
// RUN: mv %t/cache %t/cache-implicit-no-a-prune
46+
// RUN: %clang_cc1 -working-directory %t @%t/implicit-search-args.rsp -fno-modules-prune-non-affecting-module-map-files
47+
// RUN: mv %t/cache %t/cache-implicit-no-a-keep
48+
//
49+
// FIXME: Instead of removing "a/module.modulemap" from the file system, we
50+
// could drop the "-I a" search path argument in combination with the
51+
// "-fmodules-skip-header-search-paths" flag. Unfortunately, that flag
52+
// does not prevent serialization of the search path usage bit vector,
53+
// making the files differ anyways.
54+
// RUN: rm %t/a/module.modulemap
55+
//
56+
// RUN: %clang_cc1 -working-directory %t @%t/implicit-search-args.rsp
57+
// RUN: mv %t/cache %t/cache-implicit-a-prune
58+
// RUN: %clang_cc1 -working-directory %t @%t/implicit-search-args.rsp -fno-modules-prune-non-affecting-module-map-files
59+
// RUN: mv %t/cache %t/cache-implicit-a-keep
60+
//
61+
// RUN: diff %t/cache-implicit-no-a-prune/c.pcm %t/cache-implicit-a-prune/c.pcm
62+
// RUN: not diff %t/cache-implicit-no-a-keep/c.pcm %t/cache-implicit-a-keep/c.pcm

0 commit comments

Comments
 (0)