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

Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 21 15:39:18 PDT 2016


This revision was automatically updated to reflect the committed changes.
Closed by commit rL276350: [CodeGen] Fix a crash when constant folding switch statement (authored by epilk).

Changed prior to commit:
  https://reviews.llvm.org/D22542?vs=64569&id=64981#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D22542

Files:
  cfe/trunk/lib/CodeGen/CGStmt.cpp
  cfe/trunk/test/CodeGenCXX/switch-case-folding-2.cpp

Index: cfe/trunk/test/CodeGenCXX/switch-case-folding-2.cpp
===================================================================
--- cfe/trunk/test/CodeGenCXX/switch-case-folding-2.cpp
+++ cfe/trunk/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: cfe/trunk/lib/CodeGen/CGStmt.cpp
===================================================================
--- cfe/trunk/lib/CodeGen/CGStmt.cpp
+++ cfe/trunk/lib/CodeGen/CGStmt.cpp
@@ -1264,6 +1264,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.64981.patch
Type: text/x-patch
Size: 1125 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160721/bbd84503/attachment.bin>


More information about the cfe-commits mailing list