[PATCH] D122271: [Clang] -Wunused-but-set-variable warning - handle also pre/post unary operators
Dávid Bolvanský via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 22 16:27:28 PDT 2022
xbolva00 created this revision.
xbolva00 added reviewers: efriedma, yonghong-song.
Herald added a project: All.
xbolva00 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Clang fails to diagnose:
void test() {
int j = 0;
for (int i = 0; i < 1000; i++)
j++;
return;
}
Reason: Missing support for UnaryOperator.
We should not warn with volatile variables... so add check for it.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D122271
Files:
clang/lib/Sema/SemaExprCXX.cpp
clang/test/Sema/warn-unused-but-set-variables.c
clang/test/SemaCXX/warn-unused-but-set-variables-cpp.cpp
Index: clang/test/SemaCXX/warn-unused-but-set-variables-cpp.cpp
===================================================================
--- clang/test/SemaCXX/warn-unused-but-set-variables-cpp.cpp
+++ clang/test/SemaCXX/warn-unused-but-set-variables-cpp.cpp
@@ -7,6 +7,7 @@
struct __attribute__((warn_unused)) SWarnUnused {
int j;
void operator +=(int);
+ void operator ++();
};
int f0() {
@@ -62,3 +63,9 @@
SWarnUnused swu;
swu += n;
}
+
+template <typename T> void f5() {
+ // Don't warn for overloaded pre/post operators in template code.
+ SWarnUnused swu;
+ ++swu;
+}
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
@@ -73,3 +73,20 @@
__attribute__((__cleanup__(for_cleanup))) int x;
x = 5;
}
+
+void f4(void) {
+ int x1 = 0; // expected-warning{{variable 'x1' set but not used}}
+ x1++;
+ int x2 = 0; // expected-warning{{variable 'x2' set but not used}}
+ x2--;
+ int x3 = 0; // expected-warning{{variable 'x3' set but not used}}
+ ++x3;
+ int x4 = 0; // expected-warning{{variable 'x4' set but not used}}
+ --x4;
+
+ volatile int v1 = 0;
+ ++v1;
+ typedef volatile int volint;
+ volint v2 = 0;
+ v2++;
+}
Index: clang/lib/Sema/SemaExprCXX.cpp
===================================================================
--- clang/lib/Sema/SemaExprCXX.cpp
+++ clang/lib/Sema/SemaExprCXX.cpp
@@ -7926,6 +7926,7 @@
Expr *E, llvm::DenseMap<const VarDecl *, int> &RefsMinusAssignments) {
DeclRefExpr *LHS = nullptr;
bool IsCompoundAssign = false;
+ bool isIncrementDecrementUnaryOp = false;
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
if (BO->getLHS()->getType()->isDependentType() ||
BO->getRHS()->getType()->isDependentType()) {
@@ -7940,6 +7941,11 @@
if (COCE->getOperator() != OO_Equal)
return;
LHS = dyn_cast<DeclRefExpr>(COCE->getArg(0));
+ } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
+ if (!UO->isIncrementDecrementOp())
+ return;
+ isIncrementDecrementUnaryOp = true;
+ LHS = dyn_cast<DeclRefExpr>(UO->getSubExpr());
}
if (!LHS)
return;
@@ -7947,8 +7953,10 @@
if (!VD)
return;
// Don't decrement RefsMinusAssignments if volatile variable with compound
- // assignment (+=, ...) to avoid potential unused-but-set-variable warning.
- if (IsCompoundAssign && VD->getType().isVolatileQualified())
+ // assignment (+=, ...) or increment/decrement unary operator to avoid
+ // potential unused-but-set-variable warning.
+ if ((IsCompoundAssign || isIncrementDecrementUnaryOp) &&
+ VD->getType().isVolatileQualified())
return;
auto iter = RefsMinusAssignments.find(VD);
if (iter == RefsMinusAssignments.end())
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D122271.417428.patch
Type: text/x-patch
Size: 2855 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220322/50754a56/attachment.bin>
More information about the cfe-commits
mailing list