[PATCH] D121715: [Clang] Fix an unused-but-set-variable warning with volatile variable
Yonghong Song via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 15 19:22:57 PDT 2022
yonghong-song updated this revision to Diff 415665.
yonghong-song retitled this revision from "[Clang] Fix an unused-but-set-variable warning with compond assignment" to "[Clang] Fix an unused-but-set-variable warning with volatile variable".
yonghong-song edited the summary of this revision.
yonghong-song added a comment.
- only handle volatile variables with component assignment.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D121715/new/
https://reviews.llvm.org/D121715
Files:
clang/lib/Sema/SemaDecl.cpp
clang/lib/Sema/SemaExprCXX.cpp
clang/test/Sema/warn-unused-but-set-variables.c
Index: clang/test/Sema/warn-unused-but-set-variables.c
===================================================================
--- clang/test/Sema/warn-unused-but-set-variables.c
+++ clang/test/Sema/warn-unused-but-set-variables.c
@@ -23,10 +23,19 @@
int a;
w = (a = 0);
+ int j = 0; // expected-warning{{variable 'j' set but not used}}
+ for (int i = 0; i < 1000; i++)
+ j += 1;
+
// Following gcc, warn for a volatile variable.
volatile int b; // expected-warning{{variable 'b' set but not used}}
b = 0;
+ // volatile variable k is used, no warning.
+ volatile int k = 0;
+ for (int i = 0; i < 1000; i++)
+ k += 1;
+
int x;
x = 0;
return x;
Index: clang/lib/Sema/SemaExprCXX.cpp
===================================================================
--- clang/lib/Sema/SemaExprCXX.cpp
+++ clang/lib/Sema/SemaExprCXX.cpp
@@ -7922,6 +7922,7 @@
static void MaybeDecrementCount(
Expr *E, llvm::DenseMap<const VarDecl *, int> &RefsMinusAssignments) {
DeclRefExpr *LHS = nullptr;
+ bool IsComponentAssign = false;
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
if (BO->getLHS()->getType()->isDependentType() ||
BO->getRHS()->getType()->isDependentType()) {
@@ -7929,6 +7930,8 @@
return;
} else if (!BO->isAssignmentOp())
return;
+ else
+ IsComponentAssign = BO->isCompoundAssignmentOp();
LHS = dyn_cast<DeclRefExpr>(BO->getLHS());
} else if (CXXOperatorCallExpr *COCE = dyn_cast<CXXOperatorCallExpr>(E)) {
if (COCE->getOperator() != OO_Equal)
@@ -7940,6 +7943,10 @@
VarDecl *VD = dyn_cast<VarDecl>(LHS->getDecl());
if (!VD)
return;
+ // Don't decrement RefsMinusAssignments if volatile variable with component
+ // assignment (+=, ...) to avoid potential unused-but-set-variable warning.
+ if (IsComponentAssign && VD->getType().isVolatileQualified())
+ return;
auto iter = RefsMinusAssignments.find(VD);
if (iter == RefsMinusAssignments.end())
return;
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -2030,6 +2030,7 @@
"Found a negative number of references to a VarDecl");
if (iter->getSecond() != 0)
return;
+
unsigned DiagID = isa<ParmVarDecl>(VD) ? diag::warn_unused_but_set_parameter
: diag::warn_unused_but_set_variable;
Diag(VD->getLocation(), DiagID) << VD;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D121715.415665.patch
Type: text/x-patch
Size: 2489 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220316/ebb92e54/attachment-0001.bin>
More information about the cfe-commits
mailing list