Skip to content

Commit ce58f69

Browse files
committed
[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. (cherry picked from commit 731152e)
1 parent d9f3929 commit ce58f69

File tree

4 files changed

+156
-2
lines changed

4 files changed

+156
-2
lines changed

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

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

70-
LLVM_MARK_AS_BITMASK_ENUM(HeaderSearch),
71-
All = HeaderSearch,
70+
/// Remove warnings from system modules.
71+
SystemWarnings = 2,
72+
73+
LLVM_MARK_AS_BITMASK_ENUM(SystemWarnings),
74+
All = HeaderSearch | SystemWarnings,
7275
Default = All
7376
};
7477

clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,29 @@ static void optimizeHeaderSearchOpts(HeaderSearchOptions &Opts,
5757
Opts.UserEntries.push_back(Entries[Idx]);
5858
}
5959

60+
static void optimizeDiagnosticOpts(DiagnosticOptions &Opts,
61+
bool IsSystemModule) {
62+
// If this is not a system module or -Wsystem-headers was passed, don't
63+
// optimize.
64+
if (!IsSystemModule)
65+
return;
66+
bool Wsystem_headers = false;
67+
for (StringRef Opt : Opts.Warnings) {
68+
bool isPositive = !Opt.consume_front("no-");
69+
if (Opt == "system-headers")
70+
Wsystem_headers = isPositive;
71+
}
72+
if (Wsystem_headers)
73+
return;
74+
75+
// Remove all warning flags. System modules suppress most, but not all,
76+
// warnings.
77+
Opts.Warnings.clear();
78+
Opts.UndefPrefixes.clear();
79+
// FIXME: CAS currently depends on remarks. rdar://107619111
80+
// Opts.Remarks.clear();
81+
}
82+
6083
static std::vector<std::string> splitString(std::string S, char Separator) {
6184
SmallVector<StringRef> Segments;
6285
StringRef(S).split(Segments, Separator, /*MaxSplit=*/-1, /*KeepEmpty=*/false);
@@ -617,6 +640,10 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
617640
if (any(MDC.OptimizeArgs & ScanningOptimizations::HeaderSearch))
618641
optimizeHeaderSearchOpts(BuildInvocation.getMutHeaderSearchOpts(),
619642
*MDC.ScanInstance.getASTReader(), *MF);
643+
if (any(MDC.OptimizeArgs & ScanningOptimizations::SystemWarnings))
644+
optimizeDiagnosticOpts(
645+
BuildInvocation.getMutDiagnosticOpts(),
646+
BuildInvocation.getFrontendOpts().IsSystemModule);
620647
});
621648

622649
auto &Diags = MDC.ScanInstance.getDiagnostics();
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
@@ -176,6 +176,7 @@ static void ParseArgs(int argc, char **argv) {
176176
llvm::StringSwitch<std::optional<ScanningOptimizations>>(Arg)
177177
.Case("none", ScanningOptimizations::None)
178178
.Case("header-search", ScanningOptimizations::HeaderSearch)
179+
.Case("system-warnings", ScanningOptimizations::SystemWarnings)
179180
.Case("all", ScanningOptimizations::All)
180181
.Default(std::nullopt);
181182
if (!Optimization) {

0 commit comments

Comments
 (0)