[PATCH] D22542: [CodeGen] Fix a crash on valid when constant folding 'default:' statement in switch

Erik Pilkington via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 19 15:15:48 PDT 2016


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?");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D22542.64569.patch
Type: text/x-patch
Size: 1065 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160719/f3acceae/attachment.bin>


More information about the cfe-commits mailing list