Teach CFGBuilder that the 'default' branch of a switch statement is dead if all enum values in a switch conditioned are handled.

llvm-svn: 127727
diff --git a/clang/lib/Analysis/CFG.cpp b/clang/lib/Analysis/CFG.cpp
index 4772a69..a1afd60 100644
--- a/clang/lib/Analysis/CFG.cpp
+++ b/clang/lib/Analysis/CFG.cpp
@@ -2249,9 +2249,11 @@
   }
 
   // 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);
diff --git a/clang/test/SemaCXX/array-bounds.cpp b/clang/test/SemaCXX/array-bounds.cpp
index 62b4d52..3bd6c35 100644
--- a/clang/test/SemaCXX/array-bounds.cpp
+++ b/clang/test/SemaCXX/array-bounds.cpp
@@ -160,3 +160,16 @@
   }
 }
 
+// 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
+}