Skip to content

Commit 67c415d

Browse files
committed
--- Merging r127563 into '.':
U test/Analysis/misc-ps.m U lib/Analysis/CFG.cpp llvm-svn: 127613
1 parent deab766 commit 67c415d

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

clang/lib/Analysis/CFG.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,8 +2201,9 @@ CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
22012201
// Determine if the switch condition can be explicitly evaluated.
22022202
assert(Terminator->getCond() && "switch condition must be non-NULL");
22032203
Expr::EvalResult result;
2204-
tryEvaluate(Terminator->getCond(), result);
2205-
SaveAndRestore<Expr::EvalResult*> save_switchCond(switchCond, &result);
2204+
bool b = tryEvaluate(Terminator->getCond(), result);
2205+
SaveAndRestore<Expr::EvalResult*> save_switchCond(switchCond,
2206+
b ? &result : 0);
22062207

22072208
// If body is not a compound statement create implicit scope
22082209
// and add destructors.
@@ -2239,18 +2240,21 @@ CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
22392240
}
22402241

22412242
static bool shouldAddCase(bool &switchExclusivelyCovered,
2242-
const Expr::EvalResult &switchCond,
2243+
const Expr::EvalResult *switchCond,
22432244
const CaseStmt *CS,
22442245
ASTContext &Ctx) {
2246+
if (!switchCond)
2247+
return true;
2248+
22452249
bool addCase = false;
22462250

22472251
if (!switchExclusivelyCovered) {
2248-
if (switchCond.Val.isInt()) {
2252+
if (switchCond->Val.isInt()) {
22492253
// Evaluate the LHS of the case value.
22502254
Expr::EvalResult V1;
22512255
CS->getLHS()->Evaluate(V1, Ctx);
22522256
assert(V1.Val.isInt());
2253-
const llvm::APSInt &condInt = switchCond.Val.getInt();
2257+
const llvm::APSInt &condInt = switchCond->Val.getInt();
22542258
const llvm::APSInt &lhsInt = V1.Val.getInt();
22552259

22562260
if (condInt == lhsInt) {
@@ -2280,7 +2284,6 @@ CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
22802284
// CaseStmts are essentially labels, so they are the first statement in a
22812285
// block.
22822286
CFGBlock *TopBlock = 0, *LastBlock = 0;
2283-
assert(switchCond);
22842287

22852288
if (Stmt *Sub = CS->getSubStmt()) {
22862289
// For deeply nested chains of CaseStmts, instead of doing a recursion
@@ -2296,7 +2299,7 @@ CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
22962299
TopBlock = currentBlock;
22972300

22982301
addSuccessor(SwitchTerminatedBlock,
2299-
shouldAddCase(switchExclusivelyCovered, *switchCond,
2302+
shouldAddCase(switchExclusivelyCovered, switchCond,
23002303
CS, *Context)
23012304
? currentBlock : 0);
23022305

@@ -2323,7 +2326,7 @@ CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
23232326
// statement.
23242327
assert(SwitchTerminatedBlock);
23252328
addSuccessor(SwitchTerminatedBlock,
2326-
shouldAddCase(switchExclusivelyCovered, *switchCond,
2329+
shouldAddCase(switchExclusivelyCovered, switchCond,
23272330
CS, *Context)
23282331
? CaseBlock : 0);
23292332

clang/test/Analysis/misc-ps.m

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1287,4 +1287,17 @@ void test_switch() {
12871287
break;
12881288
}
12891289
}
1290-
}
1290+
}
1291+
1292+
// PR 9467. Tests various CFG optimizations. This previously crashed.
1293+
static void test(unsigned int bit_mask)
1294+
{
1295+
unsigned int bit_index;
1296+
for (bit_index = 0;
1297+
bit_index < 24;
1298+
bit_index++) {
1299+
switch ((0x01 << bit_index) & bit_mask) {
1300+
case 0x100000: ;
1301+
}
1302+
}
1303+
}

0 commit comments

Comments
 (0)