Skip to content

Commit 731152e

Browse files
authored
[clang][DependencyScanner] Remove all warning flags when suppressing warnings (llvm#71612)
Since system modules don't emit most warnings, remove the warning flags to increase module reuse.
1 parent c3148e9 commit 731152e

File tree

4 files changed

+155
-2
lines changed

4 files changed

+155
-2
lines changed

clang/include/clang/Tooling/DependencyScanning/DependencyScanningService.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ enum class ScanningOptimizations {
5151
/// Remove unused header search paths including header maps.
5252
HeaderSearch = 1,
5353

54-
LLVM_MARK_AS_BITMASK_ENUM(HeaderSearch),
55-
All = HeaderSearch,
54+
/// Remove warnings from system modules.
55+
SystemWarnings = 2,
56+
57+
LLVM_MARK_AS_BITMASK_ENUM(SystemWarnings),
58+
All = HeaderSearch | SystemWarnings,
5659
Default = All
5760
};
5861

clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,28 @@ static void optimizeHeaderSearchOpts(HeaderSearchOptions &Opts,
5252
Opts.UserEntries.push_back(Entries[Idx]);
5353
}
5454

55+
static void optimizeDiagnosticOpts(DiagnosticOptions &Opts,
56+
bool IsSystemModule) {
57+
// If this is not a system module or -Wsystem-headers was passed, don't
58+
// optimize.
59+
if (!IsSystemModule)
60+
return;
61+
bool Wsystem_headers = false;
62+
for (StringRef Opt : Opts.Warnings) {
63+
bool isPositive = !Opt.consume_front("no-");
64+
if (Opt == "system-headers")
65+
Wsystem_headers = isPositive;
66+
}
67+
if (Wsystem_headers)
68+
return;
69+
70+
// Remove all warning flags. System modules suppress most, but not all,
71+
// warnings.
72+
Opts.Warnings.clear();
73+
Opts.UndefPrefixes.clear();
74+
Opts.Remarks.clear();
75+
}
76+
5577
static std::vector<std::string> splitString(std::string S, char Separator) {
5678
SmallVector<StringRef> Segments;
5779
StringRef(S).split(Segments, Separator, /*MaxSplit=*/-1, /*KeepEmpty=*/false);
@@ -532,6 +554,10 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
532554
if (any(MDC.OptimizeArgs & ScanningOptimizations::HeaderSearch))
533555
optimizeHeaderSearchOpts(BuildInvocation.getMutHeaderSearchOpts(),
534556
*MDC.ScanInstance.getASTReader(), *MF);
557+
if (any(MDC.OptimizeArgs & ScanningOptimizations::SystemWarnings))
558+
optimizeDiagnosticOpts(
559+
BuildInvocation.getMutDiagnosticOpts(),
560+
BuildInvocation.getFrontendOpts().IsSystemModule);
535561
});
536562

537563
MDC.associateWithContextHash(CI, MD);
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// This test verifies that system module variants are mergable despite having
2+
// different warning flags, as most warnings are disabled in system modules.
3+
// This checks for system modules marked as such both via `-isystem` and
4+
// `[system]`.
5+
6+
// RUN: rm -rf %t
7+
// RUN: split-file %s %t
8+
// RUN: sed -e "s|DIR|%/t|g" %t/build/compile-commands.json.in > %t/build/compile-commands.json
9+
// RUN: clang-scan-deps -compilation-database %t/build/compile-commands.json \
10+
// RUN: -j 1 -format experimental-full -optimize-args=system-warnings > %t/deps.db
11+
// RUN: cat %t/deps.db | sed 's:\\\\\?:/:g' | FileCheck %s -DPREFIX=%/t
12+
13+
// CHECK: {
14+
// CHECK-NEXT: "modules": [
15+
// CHECK-NEXT: {
16+
// CHECK-NEXT: "clang-module-deps": [],
17+
// CHECK-NEXT: "clang-modulemap-file":
18+
// CHECK-NEXT: "command-line": [
19+
// CHECK-NOT: "-W
20+
// CHECK: ],
21+
// CHECK-NEXT: "context-hash": "{{.*}}",
22+
// CHECK-NEXT: "file-deps": [
23+
// CHECK: ],
24+
// CHECK-NEXT: "name": "A"
25+
// CHECK-NEXT: },
26+
// CHECK-NEXT: {
27+
// CHECK-NEXT: "clang-module-deps": [],
28+
// CHECK-NEXT: "clang-modulemap-file":
29+
// CHECK-NEXT: "command-line": [
30+
// CHECK-NOT: "-W
31+
// CHECK: ],
32+
// CHECK-NEXT: "context-hash": "{{.*}}",
33+
// CHECK-NEXT: "file-deps": [
34+
// CHECK: ],
35+
// CHECK-NEXT: "name": "B"
36+
// CHECK-NEXT: },
37+
// CHECK-NEXT: {
38+
// CHECK-NEXT: "clang-module-deps": [],
39+
// CHECK-NEXT: "clang-modulemap-file":
40+
// CHECK-NEXT: "command-line": [
41+
// CHECK: "-Wmaybe-unused
42+
// CHECK: ],
43+
// CHECK-NEXT: "context-hash": "{{.*}}",
44+
// CHECK-NEXT: "file-deps": [
45+
// CHECK: ],
46+
// CHECK-NEXT: "name": "C"
47+
// CHECK-NEXT: }
48+
// CHECK-NEXT: ],
49+
// CHECK-NEXT: "translation-units": [
50+
// CHECK: ]
51+
// CHECK: }
52+
53+
// A.m and B.m verify that system modules with different warning flags get
54+
// merged. C.m verifies that -Wsystem-headers disables the optimization.
55+
//--- build/compile-commands.json.in
56+
57+
[
58+
{
59+
"directory": "DIR",
60+
"command": "clang -c DIR/A.m -isystem modules/A -I modules/B -fmodules -fmodules-cache-path=DIR/module-cache -fimplicit-module-maps",
61+
"file": "DIR/A.m"
62+
},
63+
{
64+
"directory": "DIR",
65+
"command": "clang -c DIR/B.m -isystem modules/A -I modules/B -fmodules -fmodules-cache-path=DIR/module-cache -fimplicit-module-maps -Wmaybe-unused",
66+
"file": "DIR/B.m"
67+
},
68+
{
69+
"directory": "DIR",
70+
"command": "clang -c DIR/C.m -isystem modules/C -fmodules -fmodules-cache-path=DIR/module-cache -fimplicit-module-maps -Wmaybe-unused -Wsystem-headers",
71+
"file": "DIR/C.m"
72+
}
73+
]
74+
75+
//--- modules/A/module.modulemap
76+
77+
module A {
78+
umbrella header "A.h"
79+
}
80+
81+
//--- modules/A/A.h
82+
83+
typedef int A_t;
84+
85+
//--- modules/B/module.modulemap
86+
87+
module B [system] {
88+
umbrella header "B.h"
89+
}
90+
91+
//--- modules/B/B.h
92+
93+
typedef int B_t;
94+
95+
//--- modules/C/module.modulemap
96+
97+
module C [system] {
98+
umbrella header "C.h"
99+
}
100+
101+
//--- modules/C/C.h
102+
103+
typedef int C_t;
104+
105+
//--- A.m
106+
107+
#include <A.h>
108+
#include <B.h>
109+
110+
A_t a = 0;
111+
112+
//--- B.m
113+
114+
#include <A.h>
115+
#include <B.h>
116+
117+
A_t b = 0;
118+
119+
//--- C.m
120+
121+
#include <C.h>
122+
123+
C_t c = 0;

clang/tools/clang-scan-deps/ClangScanDeps.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ static void ParseArgs(int argc, char **argv) {
156156
llvm::StringSwitch<std::optional<ScanningOptimizations>>(Arg)
157157
.Case("none", ScanningOptimizations::None)
158158
.Case("header-search", ScanningOptimizations::HeaderSearch)
159+
.Case("system-warnings", ScanningOptimizations::SystemWarnings)
159160
.Case("all", ScanningOptimizations::All)
160161
.Default(std::nullopt);
161162
if (!Optimization) {

0 commit comments

Comments
 (0)