r282574 - [StaticAnalyzer] Fix false positives for vardecls that are technically unreachable but they are needed.

Daniel Marjamaki via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 28 03:39:53 PDT 2016


Author: danielmarjamaki
Date: Wed Sep 28 05:39:53 2016
New Revision: 282574

URL: http://llvm.org/viewvc/llvm-project?rev=282574&view=rev
Log:
[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


Modified:
    cfe/trunk/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp
    cfe/trunk/test/Analysis/unreachable-code-path.c

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp?rev=282574&r1=282573&r2=282574&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp Wed Sep 28 05:39:53 2016
@@ -191,8 +191,10 @@ void UnreachableCodeChecker::FindUnreach
 // 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;

Modified: cfe/trunk/test/Analysis/unreachable-code-path.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/unreachable-code-path.c?rev=282574&r1=282573&r2=282574&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/unreachable-code-path.c (original)
+++ cfe/trunk/test/Analysis/unreachable-code-path.c Wed Sep 28 05:39:53 2016
@@ -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;
+  }
+}
+




More information about the cfe-commits mailing list