erik.pilkington created this revision. erik.pilkington added reviewers: rsmith, manmanren. erik.pilkington added a subscriber: cfe-commits.
The following valid code crashes Clang in CodeGen: ``` void other_test() { switch(0) { case 0: do { default:; } while(0); } } ``` The problem is that when we constant fold the switch into the `case 0:` case, the `default:` attempts to find the enclosing switch it is attached to, which does not exist. The solution is to ignore the `default` and emit it's child statement. (Which is the exact same solution as in the `case` statement, in `EmitCaseStmt` just above this) Fixes PR28609. https://reviews.llvm.org/D22542 Files: lib/CodeGen/CGStmt.cpp test/CodeGenCXX/switch-case-folding-2.cpp Index: test/CodeGenCXX/switch-case-folding-2.cpp =================================================================== --- test/CodeGenCXX/switch-case-folding-2.cpp +++ test/CodeGenCXX/switch-case-folding-2.cpp @@ -18,4 +18,13 @@ return test(5); } +void other_test() { + switch(0) { + case 0: + do { + default:; + } while(0); + } +} + // CHECK: call i32 (i8*, ...) @_Z6printfPKcz Index: lib/CodeGen/CGStmt.cpp =================================================================== --- lib/CodeGen/CGStmt.cpp +++ lib/CodeGen/CGStmt.cpp @@ -1261,6 +1261,14 @@ } 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?");
Index: test/CodeGenCXX/switch-case-folding-2.cpp =================================================================== --- test/CodeGenCXX/switch-case-folding-2.cpp +++ test/CodeGenCXX/switch-case-folding-2.cpp @@ -18,4 +18,13 @@ return test(5); } +void other_test() { + switch(0) { + case 0: + do { + default:; + } while(0); + } +} + // CHECK: call i32 (i8*, ...) @_Z6printfPKcz Index: lib/CodeGen/CGStmt.cpp =================================================================== --- lib/CodeGen/CGStmt.cpp +++ lib/CodeGen/CGStmt.cpp @@ -1261,6 +1261,14 @@ } 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?");
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits