Skip to content

Commit 3ea5dff

Browse files
authored
[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.
1 parent 8e2060b commit 3ea5dff

File tree

7 files changed

+84
-36
lines changed

7 files changed

+84
-36
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3100,6 +3100,11 @@ defm modules_skip_header_search_paths : BoolFOption<"modules-skip-header-search-
31003100
HeaderSearchOpts<"ModulesSkipHeaderSearchPaths">, DefaultFalse,
31013101
PosFlag<SetTrue, [], [], "Disable writing header search paths">,
31023102
NegFlag<SetFalse>, BothFlags<[], [CC1Option]>>;
3103+
def fno_modules_prune_non_affecting_module_map_files :
3104+
Flag<["-"], "fno-modules-prune-non-affecting-module-map-files">,
3105+
Group<f_Group>, Flags<[]>, Visibility<[CC1Option]>,
3106+
MarshallingInfoNegativeFlag<HeaderSearchOpts<"ModulesPruneNonAffectingModuleMaps">>,
3107+
HelpText<"Do not prune non-affecting module map files when writing module files">;
31033108

31043109
def fincremental_extensions :
31053110
Flag<["-"], "fincremental-extensions">,

clang/include/clang/Lex/HeaderSearchOptions.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ class HeaderSearchOptions {
252252
LLVM_PREFERRED_TYPE(bool)
253253
unsigned ModulesSkipPragmaDiagnosticMappings : 1;
254254

255+
/// Whether to prune non-affecting module map files from PCM files.
256+
LLVM_PREFERRED_TYPE(bool)
257+
unsigned ModulesPruneNonAffectingModuleMaps : 1;
258+
255259
LLVM_PREFERRED_TYPE(bool)
256260
unsigned ModulesHashContent : 1;
257261

@@ -280,7 +284,8 @@ class HeaderSearchOptions {
280284
ModulesValidateDiagnosticOptions(true),
281285
ModulesSkipDiagnosticOptions(false),
282286
ModulesSkipHeaderSearchPaths(false),
283-
ModulesSkipPragmaDiagnosticMappings(false), ModulesHashContent(false),
287+
ModulesSkipPragmaDiagnosticMappings(false),
288+
ModulesPruneNonAffectingModuleMaps(true), ModulesHashContent(false),
284289
ModulesStrictContextHash(false), ModulesIncludeVFSUsage(false) {}
285290

286291
/// 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
@@ -166,9 +166,9 @@ namespace {
166166

167167
std::optional<std::set<const FileEntry *>>
168168
GetAffectingModuleMaps(const Preprocessor &PP, Module *RootModule) {
169-
// Without implicit module map search, there's no good reason to know about
170-
// any module maps that are not affecting.
171-
if (!PP.getHeaderSearchInfo().getHeaderSearchOpts().ImplicitModuleMaps)
169+
if (!PP.getHeaderSearchInfo()
170+
.getHeaderSearchOpts()
171+
.ModulesPruneNonAffectingModuleMaps)
172172
return std::nullopt;
173173

174174
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
@@ -179,6 +179,11 @@ makeCommonInvocationForModuleBuild(CompilerInvocation CI) {
179179
CI.resetNonModularOptions();
180180
CI.clearImplicitModuleBuildOptions();
181181

182+
// The scanner takes care to avoid passing non-affecting module maps to the
183+
// explicit compiles. No need to do extra work just to find out there are no
184+
// module map files to prune.
185+
CI.getHeaderSearchOpts().ModulesPruneNonAffectingModuleMaps = false;
186+
182187
// Remove options incompatible with explicit module build or are likely to
183188
// differ between identical modules discovered from different translation
184189
// 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)