]> granicus.if.org Git - clang/commitdiff
[CodeGen] Fix a crash when constant folding switch statement
authorErik Pilkington <erik.pilkington@gmail.com>
Thu, 21 Jul 2016 22:31:40 +0000 (22:31 +0000)
committerErik Pilkington <erik.pilkington@gmail.com>
Thu, 21 Jul 2016 22:31:40 +0000 (22:31 +0000)
Differential revision: https://reviews.llvm.org/D22542

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@276350 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGStmt.cpp
test/CodeGenCXX/switch-case-folding-2.cpp

index a7fa60f73ad1c5b308603d468a50199727163ca3..31f19829d9204a08bbb8209ab6769d749d6a521d 100644 (file)
@@ -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?");
index b0bbf3282ac3edffd7b31b29fa8ebfa74d240123..558ca3c87d9ba5b108de551487fb4e5621a764d1 100644 (file)
@@ -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