]> granicus.if.org Git - clang/commitdiff
[analyzer] Tweak the UnreachableCode checker to not warning about unreachable default...
authorTed Kremenek <kremenek@apple.com>
Wed, 29 Feb 2012 06:05:28 +0000 (06:05 +0000)
committerTed Kremenek <kremenek@apple.com>
Wed, 29 Feb 2012 06:05:28 +0000 (06:05 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@151709 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 2b550df2b93f0339b086f812c825de2a068fc9dc..8ac785ce7df4c2ccae97a3ff97f0bdefcc7b11d8 100644 (file)
@@ -116,6 +116,14 @@ void UnreachableCodeChecker::checkEndAnalysis(ExplodedGraph &G,
     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.
index da14f4c010d6216077eb7a2322df212352110c5b..48a3462ca1e5e5cdf8057bea00d44c1c42d44913 100644 (file)
@@ -122,3 +122,20 @@ void test10() {
   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}}
+  }
+}