]> granicus.if.org Git - clang/commitdiff
[analyzer] Fix a bug in IdenticalExprChecker concerning while loops.
authorJordan Rose <jordan_rose@apple.com>
Fri, 21 Feb 2014 00:18:31 +0000 (00:18 +0000)
committerJordan Rose <jordan_rose@apple.com>
Fri, 21 Feb 2014 00:18:31 +0000 (00:18 +0000)
Somehow both Daniel and I missed the fact that while loops are only identical
if they have identical bodies.

Patch by Daniel Fahlgren!

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@201829 91177308-0d34-0410-b5e6-96231b3b80d8

lib/StaticAnalyzer/Checkers/IdenticalExprChecker.cpp
test/Analysis/identical-expressions.cpp

index a76dff120bebc7697ef928e293f4ffdd2f228e39..6abfc14877e1fa78b89e718641d524e101c11e65 100644 (file)
@@ -361,8 +361,13 @@ static bool isIdenticalStmt(const ASTContext &Ctx, const Stmt *Stmt1,
     const WhileStmt *WStmt1 = cast<WhileStmt>(Stmt1);
     const WhileStmt *WStmt2 = cast<WhileStmt>(Stmt2);
 
-    return isIdenticalStmt(Ctx, WStmt1->getCond(), WStmt2->getCond(),
-                           IgnoreSideEffects);
+    if (!isIdenticalStmt(Ctx, WStmt1->getCond(), WStmt2->getCond(),
+                         IgnoreSideEffects))
+      return false;
+    if (!isIdenticalStmt(Ctx, WStmt1->getBody(), WStmt2->getBody(),
+                         IgnoreSideEffects))
+      return false;
+    return true;
   }
   case Stmt::IfStmtClass: {
     const IfStmt *IStmt1 = cast<IfStmt>(Stmt1);
index 950cdd93ff4d99f6b7c7e30d62cfc1d4cd071f47..e1f51af7c6f1680a45381175635c4b9ed0b0db37 100644 (file)
@@ -1287,6 +1287,17 @@ void test_identical_branches_while(bool b) {
   }
 }
 
+void test_identical_branches_while_2(bool b) {
+  int i = 10;
+  if (b) { // no-warning
+    while (func())
+      i--;
+  } else {
+    while (func())
+      i++;
+  }
+}
+
 void test_identical_branches_do_while(bool b) {
   int i = 10;
   if (b) { // expected-warning {{true and false branches are identical}}