PR5863: Don't erase unreachable BBs which have an associated cleanup size.

This works around a crash where malloc reused the memory of an erased BB for a
new BB leaving old cleanup information pointing at the new block.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@104472 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CGStmt.cpp b/lib/CodeGen/CGStmt.cpp
index 589273d..3e807bd 100644
--- a/lib/CodeGen/CGStmt.cpp
+++ b/lib/CodeGen/CGStmt.cpp
@@ -80,7 +80,7 @@
     // explicitly here. This handles the common case of a call to a noreturn
     // function.
     if (llvm::BasicBlock *CurBB = Builder.GetInsertBlock()) {
-      if (CurBB->empty() && CurBB->use_empty()) {
+      if (CurBB->empty() && CurBB->use_empty() && !BlockScopes.count(CurBB)) {
         CurBB->eraseFromParent();
         Builder.ClearInsertionPoint();
       }
diff --git a/test/CodeGenCXX/PR5863-unreachable-block.cpp b/test/CodeGenCXX/PR5863-unreachable-block.cpp
new file mode 100644
index 0000000..91221c9
--- /dev/null
+++ b/test/CodeGenCXX/PR5863-unreachable-block.cpp
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -emit-llvm-only %s
+
+// PR5836
+class E { };
+
+void P1() {
+ try {
+  int a=0, b=0;
+  if (a > b) // simply filling in 0 or 1 doesn't trigger the assertion
+    throw E(); // commenting out 'if' or 'throw' 'fixes' the assertion failure
+  try { } catch (...) { } // empty try/catch block needed for failure
+ } catch (...) { } // this try/catch block needed for failure
+}