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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@127727 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Ted Kremenek 2011-03-16 04:32:01 +00:00
Родитель a5bcb8fec4
Коммит 432c478fe0
2 изменённых файлов: 17 добавлений и 2 удалений

Просмотреть файл

@ -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);

Просмотреть файл

@ -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
}