Correctly handle nested switch statements in CFGBuilder when on switch statement has a condition that evaluates to a constant.

llvm-svn: 126977
This commit is contained in:
Ted Kremenek 2011-03-04 01:03:41 +00:00
parent c332e727b4
commit be52871b1a
2 changed files with 25 additions and 11 deletions

View File

@ -279,14 +279,14 @@ class CFGBuilder {
// State to track for building switch statements.
bool switchExclusivelyCovered;
Expr::EvalResult switchCond;
Expr::EvalResult *switchCond;
public:
explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
Block(NULL), Succ(NULL),
SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
TryTerminatedBlock(NULL), badCFG(false),
switchExclusivelyCovered(false) {}
switchExclusivelyCovered(false), switchCond(0) {}
// buildCFG - Used by external clients to construct the CFG.
CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C,
@ -2197,12 +2197,12 @@ CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
// for tracking the condition value.
SaveAndRestore<bool> save_switchExclusivelyCovered(switchExclusivelyCovered,
false);
SaveAndRestore<Expr::EvalResult> save_switchCond(switchCond);
// Determine if the switch condition can be explicitly evaluated.
assert(Terminator->getCond() && "switch condition must be non-NULL");
tryEvaluate(Terminator->getCond(), switchCond);
Expr::EvalResult result;
tryEvaluate(Terminator->getCond(), result);
SaveAndRestore<Expr::EvalResult*> save_switchCond(switchCond, &result);
// If body is not a compound statement create implicit scope
// and add destructors.
@ -2280,6 +2280,7 @@ CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
// CaseStmts are essentially labels, so they are the first statement in a
// block.
CFGBlock *TopBlock = 0, *LastBlock = 0;
assert(switchCond);
if (Stmt *Sub = CS->getSubStmt()) {
// For deeply nested chains of CaseStmts, instead of doing a recursion
@ -2295,7 +2296,7 @@ CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
TopBlock = currentBlock;
addSuccessor(SwitchTerminatedBlock,
shouldAddCase(switchExclusivelyCovered, switchCond,
shouldAddCase(switchExclusivelyCovered, *switchCond,
CS, *Context)
? currentBlock : 0);
@ -2322,7 +2323,7 @@ CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
// statement.
assert(SwitchTerminatedBlock);
addSuccessor(SwitchTerminatedBlock,
shouldAddCase(switchExclusivelyCovered, switchCond,
shouldAddCase(switchExclusivelyCovered, *switchCond,
CS, *Context)
? CaseBlock : 0);

View File

@ -147,3 +147,16 @@ void test_switch() {
}
}
// Test nested switch statements.
enum enumA { enumA_A, enumA_B, enumA_C, enumA_D, enumA_E };
enum enumB { enumB_X, enumB_Y, enumB_Z };
static enum enumB myVal = enumB_X;
void test_nested_switch()
{
switch (enumA_E) { // expected-warning {{no case matching constant}}
switch (myVal) { // expected-warning {{enumeration values 'enumB_X' and 'enumB_Z' not handled in switch}}
case enumB_Y: ;
}
}
}