From: Ted Kremenek Date: Thu, 30 Aug 2007 18:13:31 +0000 (+0000) Subject: Fixed bug where the CFG would fail to build when an 'if' statement had X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b6f7b72047b3fd3f96a5040e1e4d520a9dea01cd;p=clang Fixed bug where the CFG would fail to build when an 'if' statement had an empty then or else block (or contained only ';' statements). For example, we now handle the following: int empty_else() { if (0) { int a; } else ; } int empty_then() { if (0) ; else { int a; } } Thanks to Nico Weber for spotting this problem. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@41617 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/AST/CFG.cpp b/AST/CFG.cpp index 6e2e4dbe0e..014dc861de 100644 --- a/AST/CFG.cpp +++ b/AST/CFG.cpp @@ -34,7 +34,8 @@ template struct SaveAndRestore { SaveAndRestore(T& x) : X(x), old_value(x) {} ~SaveAndRestore() { X = old_value; } - + T get() { return old_value; } + T& X; T old_value; }; @@ -396,9 +397,12 @@ CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) { // NULL out Block so that the recursive call to Visit will // create a new basic block. Block = NULL; - ElseBlock = Visit(Else); - if (!ElseBlock) return NULL; - if (Block) FinishBlock(ElseBlock); + ElseBlock = Visit(Else); + + if (!ElseBlock) // Can occur when the Else body has all NullStmts. + ElseBlock = sv.get(); + else if (Block) + FinishBlock(ElseBlock); } // Process the true branch. NULL out Block so that the recursive @@ -410,9 +414,12 @@ CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) { assert (Then); SaveAndRestore sv(Succ); Block = NULL; - ThenBlock = Visit(Then); - if (!ThenBlock) return NULL; - if (Block) FinishBlock(ThenBlock); + ThenBlock = Visit(Then); + + if (!ThenBlock) // Can occur when the Then body has all NullStmts. + ThenBlock = sv.get(); + else if (Block) + FinishBlock(ThenBlock); } // Now create a new block containing the if statement.