From: Daniel Marjamaki Date: Mon, 3 Oct 2016 08:28:51 +0000 (+0000) Subject: [StaticAnalyzer] Fix UnreachableCode false positives. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bcd3bb368fa016e437972db76aa312a2fb6a8862;p=clang [StaticAnalyzer] Fix UnreachableCode false positives. 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 --- diff --git a/lib/Analysis/CFG.cpp b/lib/Analysis/CFG.cpp index a67f0910e1..3e5e54868d 100644 --- a/lib/Analysis/CFG.cpp +++ b/lib/Analysis/CFG.cpp @@ -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); } diff --git a/test/Analysis/unreachable-code-path.c b/test/Analysis/unreachable-code-path.c index f547f55324..7879240d42 100644 --- a/test/Analysis/unreachable-code-path.c +++ b/test/Analysis/unreachable-code-path.c @@ -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; + } +}