Skip to content

Commit 2f3bb76

Browse files
authored
[clang][DependencyScanning] Reset options generated for named module compilations. (#161486)
The driver-generated -cc1 command-lines for C++ named module inputs introduce some command-line options which affect the canonical module build command (and therefore the context hash). This resets those options.
1 parent 2aff3c6 commit 2f3bb76

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

clang/lib/Tooling/DependencyScanning/DependencyScannerImpl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,8 @@ bool initializeScanCompilerInstance(
509509
ScanInstance.getFrontendOpts().DisableFree = false;
510510
ScanInstance.getFrontendOpts().GenerateGlobalModuleIndex = false;
511511
ScanInstance.getFrontendOpts().UseGlobalModuleIndex = false;
512+
ScanInstance.getFrontendOpts().GenReducedBMI = false;
513+
ScanInstance.getFrontendOpts().ModuleOutputPath.clear();
512514
// This will prevent us compiling individual modules asynchronously since
513515
// FileManager is not thread-safe, but it does improve performance for now.
514516
ScanInstance.getFrontendOpts().ModulesShareFileManager = true;

clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ makeCommonInvocationForModuleBuild(CompilerInvocation CI) {
263263
// units.
264264
CI.getFrontendOpts().Inputs.clear();
265265
CI.getFrontendOpts().OutputFile.clear();
266+
CI.getFrontendOpts().GenReducedBMI = false;
267+
CI.getFrontendOpts().ModuleOutputPath.clear();
268+
CI.getHeaderSearchOpts().ModulesSkipHeaderSearchPaths = false;
269+
CI.getHeaderSearchOpts().ModulesSkipDiagnosticOptions = false;
266270
// LLVM options are not going to affect the AST
267271
CI.getFrontendOpts().LLVMArgs.clear();
268272

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Checks that driver-generated options for C++ module inputs preserve the
2+
// canonical module build commands compared to an equivalent non-module input,
3+
// and that they do not produce additional internal scanning PCMs.
4+
5+
// RUN: rm -rf %t
6+
// RUN: split-file %s %t
7+
8+
//--- main.cpp
9+
#include "root.h"
10+
import A;
11+
import B;
12+
13+
auto main() -> int { return 1; }
14+
15+
//--- A.cppm
16+
module;
17+
#include "root.h"
18+
export module A;
19+
20+
//--- B.cppm
21+
module;
22+
#include "root.h"
23+
export module B;
24+
25+
//--- module.modulemap
26+
module root { header "root.h" }
27+
28+
//--- root.h
29+
// empty
30+
31+
// RUN: %clang -std=c++23 -fmodules \
32+
// RUN: -fmodules-cache-path=%t/modules-cache \
33+
// RUN: %t/main.cpp %t/A.cppm %t/B.cppm \
34+
// RUN: -fsyntax-only -fdriver-only -MJ %t/deps.json
35+
//
36+
// RUN: sed -e '1s/^/[/' -e '$s/,$/]/' -e 's:\\\\\?:/:g' %t/deps.json \
37+
// RUN: > %t/compile_commands.json
38+
//
39+
// RUN: clang-scan-deps \
40+
// RUN: -compilation-database=%t/compile_commands.json \
41+
// RUN: -format experimental-full \
42+
// RUN: | sed 's:\\\\\?:/:g' | FileCheck %s -DPREFIX=%/t
43+
44+
// CHECK: {
45+
// CHECK-NEXT: "modules": [
46+
// CHECK-NEXT: {
47+
// CHECK-NEXT: "clang-module-deps": [],
48+
// CHECK-NEXT: "clang-modulemap-file": "[[PREFIX]]/module.modulemap",
49+
// CHECK: "context-hash": "[[HASH_ROOT:.*]]",
50+
// CHECK-NEXT: "file-deps": [
51+
// CHECK-NEXT: "[[PREFIX]]/module.modulemap",
52+
// CHECK-NEXT: "[[PREFIX]]/root.h"
53+
// CHECK-NEXT: ],
54+
// CHECK-NEXT: "link-libraries": [],
55+
// CHECK-NEXT: "name": "root"
56+
// CHECK-NEXT: }
57+
// CHECK-NEXT: ],
58+
// CHECK-NEXT: "translation-units": [
59+
// CHECK-NEXT: {
60+
// CHECK-NEXT: "commands": [
61+
// CHECK-NEXT: {
62+
// CHECK-NEXT: "clang-context-hash": "{{.*}}",
63+
// CHECK-NEXT: "named-module-deps": [
64+
// CHECK-NEXT: "A",
65+
// CHECK-NEXT: "B"
66+
// CHECK-NEXT: ],
67+
// CHECK-NEXT: "clang-module-deps": [
68+
// CHECK-NEXT: {
69+
// CHECK-NEXT: "context-hash": "[[HASH_ROOT]]",
70+
// CHECK-NEXT: "module-name": "root"
71+
// CHECK-NEXT: }
72+
// CHECK-NEXT: ],
73+
// CHECK: "file-deps": [
74+
// CHECK-NEXT: "[[PREFIX]]/main.cpp"
75+
// CHECK-NEXT: ],
76+
// CHECK-NEXT: "input-file": "[[PREFIX]]/main.cpp"
77+
// CHECK-NEXT: }
78+
// CHECK-NEXT: ]
79+
// CHECK-NEXT: },
80+
// CHECK-NEXT: {
81+
// CHECK-NEXT: "commands": [
82+
// CHECK-NEXT: {
83+
// CHECK-NEXT: "clang-context-hash": "{{.*}}",
84+
// CHECK-NEXT: "named-module": "A",
85+
// CHECK-NEXT: "clang-module-deps": [
86+
// CHECK-NEXT: {
87+
// CHECK-NEXT: "context-hash": "[[HASH_ROOT]]",
88+
// CHECK-NEXT: "module-name": "root"
89+
// CHECK-NEXT: }
90+
// CHECK-NEXT: ],
91+
// CHECK: "file-deps": [
92+
// CHECK-NEXT: "[[PREFIX]]/A.cppm"
93+
// CHECK-NEXT: ],
94+
// CHECK-NEXT: "input-file": "[[PREFIX]]/A.cppm"
95+
// CHECK-NEXT: }
96+
// CHECK-NEXT: ]
97+
// CHECK-NEXT: },
98+
// CHECK-NEXT: {
99+
// CHECK-NEXT: "commands": [
100+
// CHECK-NEXT: {
101+
// CHECK-NEXT: "clang-context-hash": "{{.*}}",
102+
// CHECK-NEXT: "named-module": "B",
103+
// CHECK-NEXT: "clang-module-deps": [
104+
// CHECK-NEXT: {
105+
// CHECK-NEXT: "context-hash": "[[HASH_ROOT]]",
106+
// CHECK-NEXT: "module-name": "root"
107+
// CHECK-NEXT: }
108+
// CHECK-NEXT: ],
109+
// CHECK: "file-deps": [
110+
// CHECK-NEXT: "[[PREFIX]]/B.cppm"
111+
// CHECK-NEXT: ],
112+
// CHECK-NEXT: "input-file": "[[PREFIX]]/B.cppm"
113+
// CHECK-NEXT: }
114+
// CHECK-NEXT: ]
115+
// CHECK-NEXT: }
116+
// CHECK-NEXT: ]
117+
// CHECK-NEXT: }
118+
119+
// This tests that the scanner doesn't produce multiple internal scanning PCMs
120+
// for our single Clang module (root).
121+
// RUN: find %t/modules-cache -name "*.pcm" | wc -l | grep 1

0 commit comments

Comments
 (0)