[cfe-commits] r151709 - in /cfe/trunk: lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp test/Analysis/unreachable-code-path.c

Ted Kremenek kremenek at apple.com
Tue Feb 28 22:05:29 PST 2012


Author: kremenek
Date: Wed Feb 29 00:05:28 2012
New Revision: 151709

URL: http://llvm.org/viewvc/llvm-project?rev=151709&view=rev
Log:
[analyzer] Tweak the UnreachableCode checker to not warning about unreachable default blocks.  Patch by Cyril Roelandt!

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=151709&r1=151708&r2=151709&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp Wed Feb 29 00:05:28 2012
@@ -116,6 +116,14 @@
     if (CB->size() > 0 && isInvalidPath(CB, *PM))
       continue;
 
+    // It is good practice to always have a "default" label in a "switch", even
+    // if we should never get there. It can be used to detect errors, for
+    // instance. Unreachable code directly under a "default" label is therefore
+    // likely to be a false positive.
+    if (const Stmt *label = CB->getLabel())
+      if (label->getStmtClass() == Stmt::DefaultStmtClass)
+        continue;
+
     // Special case for __builtin_unreachable.
     // FIXME: This should be extended to include other unreachable markers,
     // such as llvm_unreachable.

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=151709&r1=151708&r2=151709&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/unreachable-code-path.c (original)
+++ cfe/trunk/test/Analysis/unreachable-code-path.c Wed Feb 29 00:05:28 2012
@@ -122,3 +122,20 @@
   goto d;
   f: ;
 }
+
+// test11: we can actually end up in the default case, even if it is not
+// obvious: there might be something wrong with the given argument.
+enum foobar { FOO, BAR };
+extern void error();
+void test11(enum foobar fb) {
+  switch (fb) {
+    case FOO:
+      break;
+    case BAR:
+      break;
+    default:
+      error(); // no-warning
+      return;
+      error(); // expected-warning {{never executed}}
+  }
+}





More information about the cfe-commits mailing list