]> granicus.if.org Git - clang/commitdiff
[StaticAnalyzer] Fix false positives for vardecls that are technically unreachable...
authorDaniel Marjamaki <daniel.marjamaki@evidente.se>
Wed, 28 Sep 2016 10:39:53 +0000 (10:39 +0000)
committerDaniel Marjamaki <daniel.marjamaki@evidente.se>
Wed, 28 Sep 2016 10:39:53 +0000 (10:39 +0000)
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

lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp
test/Analysis/unreachable-code-path.c

index 892e713d241f096a051b3dad5de15f444b2dae89..ff07a64d9b1f48fa823bd123aeb3433ab856db24 100644 (file)
@@ -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<CFGStmt> S = I->getAs<CFGStmt>())
-      return S->getStmt();
+    if (Optional<CFGStmt> S = I->getAs<CFGStmt>()) {
+      if (!isa<DeclStmt>(S->getStmt()))
+        return S->getStmt();
+    }
   }
   if (const Stmt *S = CB->getTerminator())
     return S;
index 08019d9cbe319e91ca1b6a605eac9756e7d5aee6..f547f553246b05bf00d3e9dfab7fe4230e120cf9 100644 (file)
@@ -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;
+  }
+}
+