r180579 - [analyzer] Teach DeadStoreChecker to look though BO_Comma and disregard the LHS.
Anna Zaks
ganna at apple.com
Thu Apr 25 14:52:36 PDT 2013
Author: zaks
Date: Thu Apr 25 16:52:35 2013
New Revision: 180579
URL: http://llvm.org/viewvc/llvm-project?rev=180579&view=rev
Log:
[analyzer] Teach DeadStoreChecker to look though BO_Comma and disregard the LHS.
Modified:
cfe/trunk/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp
cfe/trunk/test/Analysis/dead-stores.c
Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp?rev=180579&r1=180578&r2=180579&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp Thu Apr 25 16:52:35 2013
@@ -101,7 +101,8 @@ void ReachableCode::computeReachableBloc
}
}
-static const Expr *LookThroughTransitiveAssignments(const Expr *Ex) {
+static const Expr *
+LookThroughTransitiveAssignmentsAndCommaOperators(const Expr *Ex) {
while (Ex) {
const BinaryOperator *BO =
dyn_cast<BinaryOperator>(Ex->IgnoreParenCasts());
@@ -111,6 +112,10 @@ static const Expr *LookThroughTransitive
Ex = BO->getRHS();
continue;
}
+ if (BO->getOpcode() == BO_Comma) {
+ Ex = BO->getRHS();
+ continue;
+ }
break;
}
return Ex;
@@ -266,7 +271,9 @@ public:
if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
// Special case: check for assigning null to a pointer.
// This is a common form of defensive programming.
- const Expr *RHS = LookThroughTransitiveAssignments(B->getRHS());
+ const Expr *RHS =
+ LookThroughTransitiveAssignmentsAndCommaOperators(B->getRHS());
+ RHS = RHS->IgnoreParenCasts();
QualType T = VD->getType();
if (T->isPointerType() || T->isObjCObjectPointerType()) {
@@ -274,7 +281,6 @@ public:
return;
}
- RHS = RHS->IgnoreParenCasts();
// Special case: self-assignments. These are often used to shut up
// "unused variable" compiler warnings.
if (const DeclRefExpr *RhsDR = dyn_cast<DeclRefExpr>(RHS))
@@ -326,7 +332,7 @@ public:
// Look through transitive assignments, e.g.:
// int x = y = 0;
- E = LookThroughTransitiveAssignments(E);
+ E = LookThroughTransitiveAssignmentsAndCommaOperators(E);
// Don't warn on C++ objects (yet) until we can show that their
// constructors/destructors don't have side effects.
Modified: cfe/trunk/test/Analysis/dead-stores.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/dead-stores.c?rev=180579&r1=180578&r2=180579&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/dead-stores.c (original)
+++ cfe/trunk/test/Analysis/dead-stores.c Thu Apr 25 16:52:35 2013
@@ -547,3 +547,25 @@ int *radar11185138_baz() {
return y;
}
+int getInt();
+int *getPtr();
+void testBOComma() {
+ int x0 = (getInt(), 0); // expected-warning{{unused variable 'x0'}}
+ int x1 = (getInt(), getInt()); // expected-warning {{Value stored to 'x1' during its initialization is never read}} // expected-warning{{unused variable 'x1'}}
+ int x2 = (getInt(), getInt(), getInt()); //expected-warning{{Value stored to 'x2' during its initialization is never read}} // expected-warning{{unused variable 'x2'}}
+ int x3;
+ x3 = (getInt(), getInt(), 0); // expected-warning{{Value stored to 'x3' is never read}}
+ int x4 = (getInt(), (getInt(), 0)); // expected-warning{{unused variable 'x4'}}
+ int y;
+ int x5 = (getInt(), (y = 0)); // expected-warning{{unused variable 'x5'}}
+ int x6 = (getInt(), (y = getInt())); //expected-warning {{Value stored to 'x6' during its initialization is never read}} // expected-warning{{unused variable 'x6'}}
+ int x7 = 0, x8 = getInt(); //expected-warning {{Value stored to 'x8' during its initialization is never read}} // expected-warning{{unused variable 'x8'}} // expected-warning{{unused variable 'x7'}}
+ int x9 = getInt(), x10 = 0; //expected-warning {{Value stored to 'x9' during its initialization is never read}} // expected-warning{{unused variable 'x9'}} // expected-warning{{unused variable 'x10'}}
+ int m = getInt(), mm, mmm; //expected-warning {{Value stored to 'm' during its initialization is never read}} // expected-warning{{unused variable 'm'}} // expected-warning{{unused variable 'mm'}} // expected-warning{{unused variable 'mmm'}}
+ int n, nn = getInt(); //expected-warning {{Value stored to 'nn' during its initialization is never read}} // expected-warning{{unused variable 'n'}} // expected-warning{{unused variable 'nn'}}
+
+ int *p;
+ p = (getPtr(), (int *)0); // no warning
+
+}
+
More information about the cfe-commits
mailing list