]> granicus.if.org Git - clang/commitdiff
Fix crash in CFG construction for 'break' statements appearing in statement expressions
authorTed Kremenek <kremenek@apple.com>
Fri, 21 May 2010 20:30:15 +0000 (20:30 +0000)
committerTed Kremenek <kremenek@apple.com>
Fri, 21 May 2010 20:30:15 +0000 (20:30 +0000)
within the increment code of a for loop.

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

lib/Analysis/CFG.cpp
test/Analysis/dead-stores.c

index c7eb53d36135937283f694902bc47357b2d14690..6f2cb41a6e40c846b813547bf29359b5cdc6e8dd 100644 (file)
@@ -985,6 +985,11 @@ CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
   } else
     LoopSuccessor = Succ;
 
+  // Save the current value for the break targets.
+  // All breaks should go to the code following the loop.
+  SaveAndRestore<CFGBlock*> save_break(BreakTargetBlock);
+  BreakTargetBlock = LoopSuccessor;
+
   // Because of short-circuit evaluation, the condition of the loop can span
   // multiple basic blocks.  Thus we need the "Entry" and "Exit" blocks that
   // evaluate the condition.
@@ -1032,10 +1037,9 @@ CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
   {
     assert(F->getBody());
 
-    // Save the current values for Block, Succ, and continue and break targets
-    SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
-      save_continue(ContinueTargetBlock),
-      save_break(BreakTargetBlock);
+   // Save the current values for Block, Succ, and continue targets.
+   SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
+      save_continue(ContinueTargetBlock);
 
     // Create a new block to contain the (bottom) of the loop body.
     Block = NULL;
@@ -1065,9 +1069,6 @@ CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
     // represent the 'loop target' for looping back to the start of the loop.
     ContinueTargetBlock->setLoopTarget(F);
 
-    // All breaks should go to the code following the loop.
-    BreakTargetBlock = LoopSuccessor;
-
     // Now populate the body block, and in the process create new blocks as we
     // walk the body of the loop.
     CFGBlock* BodyBlock = addStmt(F->getBody());
index 209ca6531cc2afe0c3bb32119fb659ab938127f2..1c600276ba43b34e84975fe76bcb086abea88bdc 100644 (file)
@@ -450,3 +450,15 @@ int f26_nestedblocks() {
   return y;
 }
 
+// The FOREACH macro in QT uses 'break' statements within statement expressions
+// placed within the increment code of for loops.
+void rdar8014335() {
+  for (int i = 0 ; i != 10 ; ({ break; })) {
+    for ( ; ; ({ ++i; break; })) ;
+    // Note that the next value stored to 'i' is never executed
+    // because the next statement to be executed is the 'break'
+    // in the increment code of the first loop.
+    i = i * 3; // expected-warning{{Value stored to 'i' is never read}}
+  }
+}
+