[cfe-commits] r103803 - in /cfe/trunk: lib/CodeGen/CGExprCXX.cpp test/CodeGenCXX/dynamic-cast.cpp test/CodeGenCXX/dyncast.cpp
Douglas Gregor
dgregor at apple.com
Fri May 14 14:14:41 PDT 2010
Author: dgregor
Date: Fri May 14 16:14:41 2010
New Revision: 103803
URL: http://llvm.org/viewvc/llvm-project?rev=103803&view=rev
Log:
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.
Modified:
cfe/trunk/lib/CodeGen/CGExprCXX.cpp
cfe/trunk/test/CodeGenCXX/dynamic-cast.cpp
cfe/trunk/test/CodeGenCXX/dyncast.cpp
Modified: cfe/trunk/lib/CodeGen/CGExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprCXX.cpp?rev=103803&r1=103802&r2=103803&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprCXX.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprCXX.cpp Fri May 14 16:14:41 2010
@@ -867,6 +867,8 @@
ToVoid = true;
} else {
LTy = LTy->getPointerTo();
+
+ // FIXME: What if exceptions are disabled?
ThrowOnBad = true;
}
@@ -933,15 +935,21 @@
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();
}
}
Modified: cfe/trunk/test/CodeGenCXX/dynamic-cast.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/dynamic-cast.cpp?rev=103803&r1=103802&r2=103803&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/dynamic-cast.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/dynamic-cast.cpp Fri May 14 16:14:41 2010
@@ -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;
}
Modified: cfe/trunk/test/CodeGenCXX/dyncast.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/dyncast.cpp?rev=103803&r1=103802&r2=103803&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/dyncast.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/dyncast.cpp Fri May 14 16:14:41 2010
@@ -20,8 +20,6 @@
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);
More information about the cfe-commits
mailing list