]> granicus.if.org Git - clang/commitdiff
[StaticAnalyzer] Fix UnreachableCode false positives.
authorDaniel Marjamaki <daniel.marjamaki@evidente.se>
Mon, 3 Oct 2016 08:28:51 +0000 (08:28 +0000)
committerDaniel Marjamaki <daniel.marjamaki@evidente.se>
Mon, 3 Oct 2016 08:28:51 +0000 (08:28 +0000)
When there is 'do { } while (0);' in the code the ExplodedGraph and UnoptimizedCFG did not match.

Differential Revision: https://reviews.llvm.org/D24759

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

lib/Analysis/CFG.cpp
test/Analysis/unreachable-code-path.c

index a67f0910e15a7581bef8c5f0cb48688094fce67e..3e5e54868d2c3f7f7e6a12dde7ed5f7e2875d681 100644 (file)
@@ -2983,20 +2983,19 @@ CFGBlock *CFGBuilder::VisitDoStmt(DoStmt *D) {
         return nullptr;
     }
 
-    if (!KnownVal.isFalse()) {
-      // Add an intermediate block between the BodyBlock and the
-      // ExitConditionBlock to represent the "loop back" transition.  Create an
-      // empty block to represent the transition block for looping back to the
-      // head of the loop.
-      // FIXME: Can we do this more efficiently without adding another block?
-      Block = nullptr;
-      Succ = BodyBlock;
-      CFGBlock *LoopBackBlock = createBlock();
-      LoopBackBlock->setLoopTarget(D);
+    // Add an intermediate block between the BodyBlock and the
+    // ExitConditionBlock to represent the "loop back" transition.  Create an
+    // empty block to represent the transition block for looping back to the
+    // head of the loop.
+    // FIXME: Can we do this more efficiently without adding another block?
+    Block = nullptr;
+    Succ = BodyBlock;
+    CFGBlock *LoopBackBlock = createBlock();
+    LoopBackBlock->setLoopTarget(D);
 
+    if (!KnownVal.isFalse())
       // Add the loop body entry as a successor to the condition.
       addSuccessor(ExitConditionBlock, LoopBackBlock);
-    }
     else
       addSuccessor(ExitConditionBlock, nullptr);
   }
index f547f553246b05bf00d3e9dfab7fe4230e120cf9..7879240d4292b6c9ee0831066bd33b3e1e19dc06 100644 (file)
@@ -173,3 +173,13 @@ void varDecl(int X) {
   }
 }
 
+// Ensure that ExplodedGraph and unoptimized CFG match.
+void test12(int x) {
+  switch (x) {
+  case 1:
+    break; // not unreachable
+  case 2:
+    do { } while (0);
+    break;
+  }
+}