]> granicus.if.org Git - clang/commitdiff
CFG tweak: in a WhileStmt, the condition variable initializer is evaluated every...
authorTed Kremenek <kremenek@apple.com>
Thu, 24 Dec 2009 01:34:10 +0000 (01:34 +0000)
committerTed Kremenek <kremenek@apple.com>
Thu, 24 Dec 2009 01:34:10 +0000 (01:34 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@92111 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/CFG.cpp
test/Analysis/misc-ps-region-store.cpp

index fcdc95ec53d1e6d5e60a68b2ff92b5fcdcef5fa6..a317e0452cf48d4936a45eb4c17e3df6a43d5218 100644 (file)
@@ -1130,6 +1130,18 @@ CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
     Block = ExitConditionBlock;
     EntryConditionBlock = addStmt(C);
     assert(Block == EntryConditionBlock);
+    
+    // If this block contains a condition variable, add both the condition
+    // variable and initializer to the CFG.
+    if (VarDecl *VD = W->getConditionVariable()) {
+      if (Expr *Init = VD->getInit()) {
+        autoCreateBlock();
+        AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
+        EntryConditionBlock = addStmt(Init);
+        assert(Block == EntryConditionBlock);
+      }
+    }
+
     if (Block) {
       if (!FinishBlock(EntryConditionBlock))
         return 0;
@@ -1188,21 +1200,8 @@ CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
   // to this block.  NULL out Block to force lazy creation of another block.
   Block = NULL;
 
-  // Set Succ to be the condition block, which is the dominating block
-  // for the loop.
+  // Return the condition block, which is the dominating block for the loop.
   Succ = EntryConditionBlock;
-  
-  // Finally, if the WhileStmt contains a condition variable, add both the 
-  // WhileStmt and the condition variable initialization to the CFG.
-  if (VarDecl *VD = W->getConditionVariable()) {
-    if (Expr *Init = VD->getInit()) {
-      autoCreateBlock();
-      AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
-      Succ = addStmt(Init);
-      return Succ;
-    }
-  }
-  
   return EntryConditionBlock;
 }
 
index fcef0516ec0ef431cc47067284bfcc79e9765b8f..f71bb0ea3853b4e78d628062c712682d2a8cdc86 100644 (file)
@@ -61,16 +61,17 @@ int test_init_in_condition_switch() {
 }
 
 int test_init_in_condition_while() {
-  int y = 1;
-  while (int x = test_init_in_condition_aux()) { // no-warning
-    if (!x) {
-      y = 0;
+  int z = 0;
+  while (int x = ++z) { // no-warning
+    if (x == 2)
       break;
-    }
-  }  
-  if (!y) {
-    int *p = 0;
-    *p = 0xDEADBEEF; // no-warning
   }
+  
+  if (z == 2)
+    return 0;
+  
+  int *p = 0;
+  *p = 0xDEADBEEF; // no-warning
   return 0;
 }
+