Skip to content

Commit

Permalink
[CodeGen] Fix a crash when constant folding switch statement
Browse files Browse the repository at this point in the history
Differential revision: https://reviews.llvm.org/D22542

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@276350 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
epilk committed Jul 21, 2016
1 parent 5210a74 commit 540a73b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/CodeGen/CGStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,14 @@ void CodeGenFunction::EmitCaseStmt(const CaseStmt &S) {
}

void CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) {
// If there is no enclosing switch instance that we're aware of, then this
// default statement can be elided. This situation only happens when we've
// constant-folded the switch.
if (!SwitchInsn) {
EmitStmt(S.getSubStmt());
return;
}

llvm::BasicBlock *DefaultBlock = SwitchInsn->getDefaultDest();
assert(DefaultBlock->empty() &&
"EmitDefaultStmt: Default block already defined?");
Expand Down
9 changes: 9 additions & 0 deletions test/CodeGenCXX/switch-case-folding-2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,13 @@ int main(void) {
return test(5);
}

void other_test() {
switch(0) {
case 0:
do {
default:;
} while(0);
}
}

// CHECK: call i32 (i8*, ...) @_Z6printfPKcz

0 comments on commit 540a73b

Please sign in to comment.