r276350 - [CodeGen] Fix a crash when constant folding switch statement

Erik Pilkington via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 21 15:31:40 PDT 2016


Author: epilk
Date: Thu Jul 21 17:31:40 2016
New Revision: 276350

URL: http://llvm.org/viewvc/llvm-project?rev=276350&view=rev
Log:
[CodeGen] Fix a crash when constant folding switch statement

Differential revision: https://reviews.llvm.org/D22542

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

Modified: cfe/trunk/lib/CodeGen/CGStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGStmt.cpp?rev=276350&r1=276349&r2=276350&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGStmt.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGStmt.cpp Thu Jul 21 17:31:40 2016
@@ -1264,6 +1264,14 @@ void CodeGenFunction::EmitCaseStmt(const
 }
 
 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?");

Modified: cfe/trunk/test/CodeGenCXX/switch-case-folding-2.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/switch-case-folding-2.cpp?rev=276350&r1=276349&r2=276350&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/switch-case-folding-2.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/switch-case-folding-2.cpp Thu Jul 21 17:31:40 2016
@@ -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




More information about the cfe-commits mailing list