Skip to content

Commit 3e01145

Browse files
authored
[clang] Add -Wunused-but-set-global (#188291)
Commit fd11cf4 extended `-Wunused-but-set-variable` to static globals. To make it easier for downstream projects to integrate this new functionality, this commit introduces `-Wunused-but-set-global` so it can be easily disabled as projects investigate and fix new findings.
1 parent 499afa6 commit 3e01145

File tree

9 files changed

+27
-9
lines changed

9 files changed

+27
-9
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,10 @@ Attribute Changes in Clang
231231
Improvements to Clang's diagnostics
232232
-----------------------------------
233233
- ``-Wunused-but-set-variable`` now diagnoses file-scope variables with
234-
internal linkage (``static`` storage class) that are assigned but never used. (#GH148361)
234+
internal linkage (``static`` storage class) that are assigned but never used.
235+
This new coverage is added under the subgroup ``-Wunused-but-set-global``,
236+
allowing it to be disabled independently with ``-Wno-unused-but-set-global``.
237+
(#GH148361)
235238

236239
- Added ``-Wlifetime-safety`` to enable lifetime safety analysis,
237240
a CFG-based intra-procedural analysis that detects use-after-free and related

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,9 @@ def UnusedValue : DiagGroup<"unused-value", [UnusedComparison, UnusedResult,
11581158
def UnusedConstVariable : DiagGroup<"unused-const-variable">;
11591159
def UnusedVariable : DiagGroup<"unused-variable",
11601160
[UnusedConstVariable]>;
1161-
def UnusedButSetVariable : DiagGroup<"unused-but-set-variable">;
1161+
def UnusedButSetGlobal : DiagGroup<"unused-but-set-global">;
1162+
def UnusedButSetVariable
1163+
: DiagGroup<"unused-but-set-variable", [UnusedButSetGlobal]>;
11621164
def UnusedLocalTypedef : DiagGroup<"unused-local-typedef">;
11631165
def UnusedPropertyIvar : DiagGroup<"unused-property-ivar">;
11641166
def UnusedGetterReturnValue : DiagGroup<"unused-getter-return-value">;

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,9 @@ def warn_unused_variable : Warning<"unused variable %0">,
433433
InGroup<UnusedVariable>, DefaultIgnore;
434434
def warn_unused_but_set_variable : Warning<"variable %0 set but not used">,
435435
InGroup<UnusedButSetVariable>, DefaultIgnore;
436+
def warn_unused_but_set_global : Warning<"variable %0 set but not used">,
437+
InGroup<UnusedButSetGlobal>,
438+
DefaultIgnore;
436439
def warn_unused_local_typedef : Warning<
437440
"unused %select{typedef|type alias}0 %1">,
438441
InGroup<UnusedLocalTypedef>, DefaultIgnore;

clang/lib/Sema/Sema.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1610,7 +1610,7 @@ void Sema::ActOnEndOfTranslationUnit() {
16101610
emitAndClearUnusedLocalTypedefWarnings();
16111611
}
16121612

1613-
if (!Diags.isIgnored(diag::warn_unused_but_set_variable, SourceLocation())) {
1613+
if (!Diags.isIgnored(diag::warn_unused_but_set_global, SourceLocation())) {
16141614
// Diagnose unused-but-set static globals in a deterministic order.
16151615
// Not tracking shadowing info for static globals; there's nothing to
16161616
// shadow.

clang/lib/Sema/SemaDecl.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2245,8 +2245,13 @@ void Sema::DiagnoseUnusedButSetDecl(const VarDecl *VD,
22452245
return;
22462246
}
22472247

2248-
unsigned DiagID = isa<ParmVarDecl>(VD) ? diag::warn_unused_but_set_parameter
2249-
: diag::warn_unused_but_set_variable;
2248+
unsigned DiagID;
2249+
if (isa<ParmVarDecl>(VD))
2250+
DiagID = diag::warn_unused_but_set_parameter;
2251+
else if (VD->isFileVarDecl())
2252+
DiagID = diag::warn_unused_but_set_global;
2253+
else
2254+
DiagID = diag::warn_unused_but_set_variable;
22502255
DiagReceiver(VD->getLocation(), PDiag(DiagID) << VD);
22512256
}
22522257

clang/test/C/C2y/n3622.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// RUN: %clang_cc1 -verify=good -pedantic -Wall -Wno-unused-but-set-variable -std=c2y %s
2-
// RUN: %clang_cc1 -verify=compat,expected -pedantic -Wall -Wno-unused-but-set-variable -Wpre-c2y-compat -std=c2y %s
3-
// RUN: %clang_cc1 -verify=ped,expected -pedantic -Wall -Wno-unused-but-set-variable -std=c23 %s
4-
// RUN: %clang_cc1 -verify=ped,expected -pedantic -Wall -Wno-unused-but-set-variable -std=c17 %s
1+
// RUN: %clang_cc1 -verify=good -pedantic -Wall -Wno-unused-but-set-global -std=c2y %s
2+
// RUN: %clang_cc1 -verify=compat,expected -pedantic -Wall -Wno-unused-but-set-global -Wpre-c2y-compat -std=c2y %s
3+
// RUN: %clang_cc1 -verify=ped,expected -pedantic -Wall -Wno-unused-but-set-global -std=c23 %s
4+
// RUN: %clang_cc1 -verify=ped,expected -pedantic -Wall -Wno-unused-but-set-global -std=c17 %s
55
// good-no-diagnostics
66

77
/* WG14 N3622: Clang 22

clang/test/Misc/warning-wall.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ CHECK-NEXT: -Wpotentially-evaluated-expression
8484
CHECK-NEXT: -Wunused-variable
8585
CHECK-NEXT: -Wunused-const-variable
8686
CHECK-NEXT: -Wunused-but-set-variable
87+
CHECK-NEXT: -Wunused-but-set-global
8788
CHECK-NEXT: -Wunused-property-ivar
8889
CHECK-NEXT: -Wvolatile-register-var
8990
CHECK-NEXT: -Wobjc-missing-super-calls

clang/test/Sema/warn-unused-but-set-static-global.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// RUN: %clang_cc1 -fsyntax-only -Wunused-but-set-variable -I %S/Inputs -verify %s
2+
// RUN: %clang_cc1 -fsyntax-only -Wunused-but-set-variable -Wno-unused-but-set-global -I %S/Inputs -verify=no-global %s
3+
// no-global-no-diagnostics
24

35
// Test that header-defined static globals don't warn.
46
#include "warn-unused-but-set-static-global-header.h"

clang/test/Sema/warn-unused-but-set-static-global.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// RUN: %clang_cc1 -fsyntax-only -Wunused-but-set-variable -verify -std=c++17 %s
2+
// RUN: %clang_cc1 -fsyntax-only -Wunused-but-set-variable -Wno-unused-but-set-global -verify=no-global -std=c++17 %s
3+
// no-global-no-diagnostics
24

35
static thread_local int tl_set_unused; // expected-warning {{variable 'tl_set_unused' set but not used}}
46
static thread_local int tl_set_and_used;

0 commit comments

Comments
 (0)