When a failed dynamic_cast<T&> (which is an lvalue) results in a

throw, it should use invoke when needed. The fixes the
Boost.Statechrt failures that motivated PR7132, but there are a few
side issues to tackle as well.

llvm-svn: 103803
This commit is contained in:
Douglas Gregor
2010-05-14 21:14:41 +00:00
parent 95f6ebcb37
commit fa8b4955bb
3 changed files with 26 additions and 8 deletions

View File

@@ -867,6 +867,8 @@ llvm::Value *CodeGenFunction::EmitDynamicCast(llvm::Value *V,
ToVoid = true;
} else {
LTy = LTy->getPointerTo();
// FIXME: What if exceptions are disabled?
ThrowOnBad = true;
}
@@ -933,15 +935,21 @@ llvm::Value *CodeGenFunction::EmitDynamicCast(llvm::Value *V,
if (ThrowOnBad) {
BadCastBlock = createBasicBlock();
Builder.CreateCondBr(Builder.CreateIsNotNull(V), ContBlock, BadCastBlock);
EmitBlock(BadCastBlock);
/// Call __cxa_bad_cast
/// Invoke __cxa_bad_cast
ResultType = llvm::Type::getVoidTy(VMContext);
const llvm::FunctionType *FBadTy;
FBadTy = llvm::FunctionType::get(ResultType, false);
llvm::Value *F = CGM.CreateRuntimeFunction(FBadTy, "__cxa_bad_cast");
Builder.CreateCall(F)->setDoesNotReturn();
if (llvm::BasicBlock *InvokeDest = getInvokeDest()) {
llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Builder.CreateInvoke(F, Cont, InvokeDest)->setDoesNotReturn();
EmitBlock(Cont);
} else {
// FIXME: Does this ever make sense?
Builder.CreateCall(F)->setDoesNotReturn();
}
Builder.CreateUnreachable();
}
}

View File

@@ -1,8 +1,20 @@
// RUN: %clang_cc1 %s -emit-llvm-only
// RUN: %clang_cc1 %s -triple x86_64-apple-darwin10 -emit-llvm -fexceptions -o - | FileCheck %s
#include <typeinfo>
struct A { virtual void f(); };
struct B : A { };
// CHECK: {{define.*@_Z1fP1A}}
B fail;
const B& f(A *a) {
return dynamic_cast<const B&>(*a);
try {
// CHECK: call i8* @__dynamic_cast
// CHECK: br i1
// CHECK: invoke void @__cxa_bad_cast() noreturn
return dynamic_cast<const B&>(*a);
} catch (std::bad_cast&) {
// CHECK: call i8* @llvm.eh.exception
// CHECK: {{call.*llvm.eh.selector.*_ZTISt8bad_cast}}
// CHECK: {{call i32 @llvm.eh.typeid.for.*@_ZTISt8bad_cast}}
}
return fail;
}

View File

@@ -20,8 +20,6 @@ extern "C" int printf(const char *str...);
void test1() {
test1_B* bp = (test1_B*)&test1_d;
test1_A* ap = &test1_d;
// This throws
// test1_D& dr = dynamic_cast<D&>(*bp);
test1_D* dp = dynamic_cast<test1_D*>(bp);
S(dp == 0, 1);
ap = dynamic_cast<test1_A*>(bp);