]> granicus.if.org Git - clang/commitdiff
Teach CFGBuilder that the 'default' branch of a switch statement is dead if all enum...
authorTed Kremenek <kremenek@apple.com>
Wed, 16 Mar 2011 04:32:01 +0000 (04:32 +0000)
committerTed Kremenek <kremenek@apple.com>
Wed, 16 Mar 2011 04:32:01 +0000 (04:32 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@127727 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/CFG.cpp
test/SemaCXX/array-bounds.cpp

index 4772a69b4ae05579f84b7c64bb4f2938a2583ee6..a1afd60fe7ad5914dc86a6db1b881dd1bf18994a 100644 (file)
@@ -2249,9 +2249,11 @@ CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
   }
 
   // If we have no "default:" case, the default transition is to the code
-  // following the switch body.
+  // following the switch body.  Moreover, take into account if all the
+  // cases of a switch are covered (e.g., switching on an enum value).
   addSuccessor(SwitchTerminatedBlock,
-               switchExclusivelyCovered ? 0 : DefaultCaseBlock);
+               switchExclusivelyCovered || Terminator->isAllEnumCasesCovered()
+               ? 0 : DefaultCaseBlock);
 
   // Add the terminator and condition in the switch block.
   SwitchTerminatedBlock->setTerminator(Terminator);
index 62b4d520cc7f9cfd52742d365a9155cc4e430580..3bd6c35420d7b578b733f6343d2a9586e3d76226 100644 (file)
@@ -160,3 +160,16 @@ void test_nested_switch()
   }
 }
 
+// Test that if all the values of an enum covered, that the 'default' branch
+// is unreachable.
+enum Values { A, B, C, D };
+void test_all_enums_covered(enum Values v) {
+  int x[2];
+  switch (v) {
+  case A: return;
+  case B: return;
+  case C: return;
+  case D: return;
+  }
+  x[2] = 0; // no-warning
+}