From e8b5e6d529e6aa52fa826a4682e8f27429d4f70a Mon Sep 17 00:00:00 2001 From: Daniel Marjamaki Date: Wed, 28 Sep 2016 10:39:53 +0000 Subject: [PATCH] [StaticAnalyzer] Fix false positives for vardecls that are technically unreachable but they are needed. Example: switch (x) { int a; // <- This is unreachable but needed case 1: a = ... Differential Revision: https://reviews.llvm.org/D24905 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@282574 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Checkers/UnreachableCodeChecker.cpp | 6 ++++-- test/Analysis/unreachable-code-path.c | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp b/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp index 892e713d24..ff07a64d9b 100644 --- a/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp +++ b/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp @@ -191,8 +191,10 @@ void UnreachableCodeChecker::FindUnreachableEntryPoints(const CFGBlock *CB, // Find the Stmt* in a CFGBlock for reporting a warning const Stmt *UnreachableCodeChecker::getUnreachableStmt(const CFGBlock *CB) { for (CFGBlock::const_iterator I = CB->begin(), E = CB->end(); I != E; ++I) { - if (Optional S = I->getAs()) - return S->getStmt(); + if (Optional S = I->getAs()) { + if (!isa(S->getStmt())) + return S->getStmt(); + } } if (const Stmt *S = CB->getTerminator()) return S; diff --git a/test/Analysis/unreachable-code-path.c b/test/Analysis/unreachable-code-path.c index 08019d9cbe..f547f55324 100644 --- a/test/Analysis/unreachable-code-path.c +++ b/test/Analysis/unreachable-code-path.c @@ -158,3 +158,18 @@ void testInlined() { } } } + +// Don't warn about unreachable VarDecl. +void dostuff(int*A); +void varDecl(int X) { + switch (X) { + int A; // No warning here. + case 1: + dostuff(&A); + break; + case 2: + dostuff(&A); + break; + } +} + -- 2.40.0