[cfe-commits] r127727 - in /cfe/trunk: lib/Analysis/CFG.cpp test/SemaCXX/array-bounds.cpp
Ted Kremenek
kremenek at apple.com
Tue Mar 15 21:32:01 PDT 2011
Author: kremenek
Date: Tue Mar 15 23:32:01 2011
New Revision: 127727
URL: http://llvm.org/viewvc/llvm-project?rev=127727&view=rev
Log:
Teach CFGBuilder that the 'default' branch of a switch statement is dead if all enum values in a switch conditioned are handled.
Modified:
cfe/trunk/lib/Analysis/CFG.cpp
cfe/trunk/test/SemaCXX/array-bounds.cpp
Modified: cfe/trunk/lib/Analysis/CFG.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/CFG.cpp?rev=127727&r1=127726&r2=127727&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/CFG.cpp (original)
+++ cfe/trunk/lib/Analysis/CFG.cpp Tue Mar 15 23:32:01 2011
@@ -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);
Modified: cfe/trunk/test/SemaCXX/array-bounds.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/array-bounds.cpp?rev=127727&r1=127726&r2=127727&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/array-bounds.cpp (original)
+++ cfe/trunk/test/SemaCXX/array-bounds.cpp Tue Mar 15 23:32:01 2011
@@ -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
+}
More information about the cfe-commits
mailing list