//===----------------------------------------------------------------------===//
#include "CGCXXABI.h"
+#include "CGCleanup.h"
using namespace clang;
using namespace CodeGen;
// Just call std::terminate and ignore the violating exception.
return CGF.EmitNounwindRuntimeCall(CGF.CGM.getTerminateFn());
}
+
+CatchTypeInfo CGCXXABI::getCatchAllTypeInfo() {
+ return CatchTypeInfo{nullptr, 0};
+}
namespace CodeGen {
class CodeGenFunction;
class CodeGenModule;
+struct CatchTypeInfo;
/// \brief Implements C++ ABI-specific code generation functions.
class CGCXXABI {
llvm::Value *Exn);
virtual llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) = 0;
- virtual llvm::Constant *
+ virtual CatchTypeInfo
getAddrOfCXXCatchHandlerType(QualType Ty, QualType CatchHandlerType) = 0;
+ virtual CatchTypeInfo getCatchAllTypeInfo();
virtual bool shouldTypeidBeNullChecked(bool IsDeref,
QualType SrcRecordTy) = 0;
class CodeGenModule;
class CodeGenFunction;
+/// The MS C++ ABI needs a pointer to RTTI data plus some flags to describe the
+/// type of a catch handler, so we use this wrapper.
+struct CatchTypeInfo {
+ llvm::Constant *RTTI;
+ unsigned Flags;
+};
+
/// A protected scope for zero-cost EH handling.
class EHScope {
llvm::BasicBlock *CachedLandingPad;
struct Handler {
/// A type info value, or null (C++ null, not an LLVM null pointer)
/// for a catch-all.
- llvm::Constant *Type;
+ CatchTypeInfo Type;
/// The catch handler for this type.
llvm::BasicBlock *Block;
- bool isCatchAll() const { return Type == nullptr; }
+ bool isCatchAll() const { return Type.RTTI == nullptr; }
};
private:
}
void setCatchAllHandler(unsigned I, llvm::BasicBlock *Block) {
- setHandler(I, /*catchall*/ nullptr, Block);
+ setHandler(I, CatchTypeInfo{nullptr, 0}, Block);
}
void setHandler(unsigned I, llvm::Constant *Type, llvm::BasicBlock *Block) {
+ assert(I < getNumHandlers());
+ getHandlers()[I].Type = CatchTypeInfo{Type, 0};
+ getHandlers()[I].Block = Block;
+ }
+
+ void setHandler(unsigned I, CatchTypeInfo Type, llvm::BasicBlock *Block) {
assert(I < getNumHandlers());
getHandlers()[I].Type = Type;
getHandlers()[I].Block = Block;
QualType CaughtType = CGM.getContext().getUnqualifiedArrayType(
C->getCaughtType().getNonReferenceType(), CaughtTypeQuals);
- llvm::Constant *TypeInfo = nullptr;
+ CatchTypeInfo TypeInfo{nullptr, 0};
if (CaughtType->isObjCObjectPointerType())
- TypeInfo = CGM.getObjCRuntime().GetEHType(CaughtType);
+ TypeInfo.RTTI = CGM.getObjCRuntime().GetEHType(CaughtType);
else
- TypeInfo =
- CGM.getAddrOfCXXCatchHandlerType(CaughtType, C->getCaughtType());
+ TypeInfo = CGM.getCXXABI().getAddrOfCXXCatchHandlerType(
+ CaughtType, C->getCaughtType());
CatchScope->setHandler(I, TypeInfo, Handler);
} else {
// No exception decl indicates '...', a catch-all.
- CatchScope->setCatchAllHandler(I, Handler);
+ CatchScope->setHandler(I, CGM.getCXXABI().getCatchAllTypeInfo(), Handler);
}
}
}
EHCatchScope &catchScope = cast<EHCatchScope>(*I);
for (unsigned hi = 0, he = catchScope.getNumHandlers(); hi != he; ++hi) {
EHCatchScope::Handler handler = catchScope.getHandler(hi);
+ assert(handler.Type.Flags == 0 &&
+ "landingpads do not support catch handler flags");
// If this is a catch-all, register that and abort.
- if (!handler.Type) {
+ if (!handler.Type.RTTI) {
assert(!hasCatchAll);
hasCatchAll = true;
goto done;
}
// Check whether we already have a handler for this type.
- if (catchTypes.insert(handler.Type).second)
+ if (catchTypes.insert(handler.Type.RTTI).second)
// If not, add it directly to the landingpad.
- LPadInst->addClause(handler.Type);
+ LPadInst->addClause(handler.Type.RTTI);
}
}
for (unsigned I = 0, E = CatchScope.getNumHandlers(); I < E; ++I) {
const EHCatchScope::Handler &Handler = CatchScope.getHandler(I);
- llvm::Value *TypeValue = Handler.Type;
- assert(TypeValue != nullptr || Handler.isCatchAll());
- if (!TypeValue)
- TypeValue = llvm::Constant::getNullValue(CGF.VoidPtrTy);
+ CatchTypeInfo TypeInfo = Handler.Type;
+ if (!TypeInfo.RTTI)
+ TypeInfo.RTTI = llvm::Constant::getNullValue(CGF.VoidPtrTy);
// If this is the last handler, we're at the end, and the next
// block is the block for the enclosing EH scope.
}
if (EHPersonality::get(CGF).isMSVCXXPersonality()) {
- CGF.Builder.CreateCatchPad(
- Handler.Block, NextBlock,
- {TypeValue, llvm::Constant::getNullValue(CGF.VoidPtrTy)});
+ CGF.Builder.CreateCatchPad(Handler.Block, NextBlock,
+ {TypeInfo.RTTI,
+ CGF.Builder.getInt32(TypeInfo.Flags),
+ llvm::Constant::getNullValue(CGF.VoidPtrTy)});
} else {
- CGF.Builder.CreateCatchPad(Handler.Block, NextBlock, {TypeValue});
+ CGF.Builder.CreateCatchPad(Handler.Block, NextBlock, {TypeInfo.RTTI});
}
// Otherwise we need to emit and continue at that block.
assert(i < e && "ran off end of handlers!");
const EHCatchScope::Handler &handler = catchScope.getHandler(i);
- llvm::Value *typeValue = handler.Type;
+ llvm::Value *typeValue = handler.Type.RTTI;
+ assert(handler.Type.Flags == 0 &&
+ "landingpads do not support catch handler flags");
assert(typeValue && "fell into catch-all case!");
typeValue = CGF.Builder.CreateBitCast(typeValue, CGF.Int8PtrTy);
HelperCGF.GenerateSEHFilterFunction(*this, *Except);
llvm::Constant *OpaqueFunc =
llvm::ConstantExpr::getBitCast(FilterFunc, Int8PtrTy);
- CatchScope->setHandler(0, OpaqueFunc, createBasicBlock("__except.ret"));
+ CatchScope->setHandler(0, CatchTypeInfo{OpaqueFunc},
+ createBasicBlock("__except.ret"));
}
void CodeGenFunction::ExitSEHTryStmt(const SEHTryStmt &S) {
return llvm::ConstantStruct::getAnon(Fields);
}
-llvm::Constant *
-CodeGenModule::getAddrOfCXXCatchHandlerType(QualType Ty,
- QualType CatchHandlerType) {
- return getCXXABI().getAddrOfCXXCatchHandlerType(Ty, CatchHandlerType);
-}
-
llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
bool ForEH) {
// Return a bogus pointer if RTTI is disabled, unless it's for EH.
/// Get the address of the RTTI descriptor for the given type.
llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false);
- llvm::Constant *getAddrOfCXXCatchHandlerType(QualType Ty,
- QualType CatchHandlerType);
-
/// Get the address of a uuid descriptor .
ConstantAddress GetAddrOfUuidDescriptor(const CXXUuidofExpr* E);
void EmitFundamentalRTTIDescriptor(QualType Type);
void EmitFundamentalRTTIDescriptors();
llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) override;
- llvm::Constant *
+ CatchTypeInfo
getAddrOfCXXCatchHandlerType(QualType Ty,
QualType CatchHandlerType) override {
- return getAddrOfRTTIDescriptor(Ty);
+ return CatchTypeInfo{getAddrOfRTTIDescriptor(Ty), 0};
}
bool shouldTypeidBeNullChecked(bool IsDeref, QualType SrcRecordTy) override;
//===----------------------------------------------------------------------===//
#include "CGCXXABI.h"
+#include "CGCleanup.h"
#include "CGVTables.h"
#include "CodeGenModule.h"
#include "CodeGenTypes.h"
const VPtrInfo *Info);
llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) override;
- llvm::Constant *
+ CatchTypeInfo
getAddrOfCXXCatchHandlerType(QualType Ty, QualType CatchHandlerType) override;
+ /// MSVC needs an extra flag to indicate a catchall.
+ CatchTypeInfo getCatchAllTypeInfo() override {
+ return CatchTypeInfo{nullptr, 0x40};
+ }
+
bool shouldTypeidBeNullChecked(bool IsDeref, QualType SrcRecordTy) override;
void EmitBadTypeidCall(CodeGenFunction &CGF) override;
llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy,
llvm::Value *Args[2] = {Exn, ParamAddr.getPointer()};
CGF.EmitNounwindRuntimeCall(BeginCatch, Args);
} else {
- CPI->setArgOperand(1, var.getObjectAddress(CGF).getPointer());
+ CPI->setArgOperand(2, var.getObjectAddress(CGF).getPointer());
}
CGF.EHStack.pushCleanup<CallEndCatchMSVC>(NormalCleanup, CPI);
CGF.EmitAutoVarCleanups(var);
return T;
}
-llvm::Constant *
+CatchTypeInfo
MicrosoftCXXABI::getAddrOfCXXCatchHandlerType(QualType Type,
QualType CatchHandlerType) {
// TypeDescriptors for exceptions never have qualified pointer types,
if (IsReference)
Flags |= 8;
- SmallString<256> MangledName;
- {
- llvm::raw_svector_ostream Out(MangledName);
- getMangleContext().mangleCXXCatchHandlerType(Type, Flags, Out);
- }
-
- if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(MangledName))
- return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
-
- llvm::Constant *Fields[] = {
- llvm::ConstantInt::get(CGM.IntTy, Flags), // Flags
- getAddrOfRTTIDescriptor(Type), // TypeDescriptor
- };
- llvm::StructType *CatchHandlerTypeType = getCatchHandlerTypeType();
- auto *Var = new llvm::GlobalVariable(
- CGM.getModule(), CatchHandlerTypeType, /*Constant=*/true,
- llvm::GlobalValue::PrivateLinkage,
- llvm::ConstantStruct::get(CatchHandlerTypeType, Fields),
- StringRef(MangledName));
- Var->setUnnamedAddr(true);
- Var->setSection("llvm.metadata");
- return Var;
+ return CatchTypeInfo{getAddrOfRTTIDescriptor(Type)->stripPointerCasts(),
+ Flags};
}
/// \brief Gets a TypeDescriptor. Returns a llvm::Constant * rather than a
// CHECK: to label %[[NORMAL:.*]] unwind label %[[CATCH_INT:.*]]
// CHECK: [[CATCH_INT]]
-// CHECK: %[[CATCHPAD_INT:.*]] = catchpad [{{.*}} @llvm.eh.handlertype.H.0, i8* null]
+// CHECK: %[[CATCHPAD_INT:.*]] = catchpad [%rtti.TypeDescriptor2* @"\01??_R0H@8", i32 0, i8* null]
// CHECK: to label %[[CATCH_INT_HANDLER:.*]] unwind label %[[CATCH_DOUBLE:.*]]
// CHECK: [[CATCH_INT_HANDLER]]
// CHECK: ret void
// CHECK: [[CATCH_DOUBLE]]
-// CHECK: %[[CATCHPAD_DOUBLE:.*]] = catchpad [{{.*}} @llvm.eh.handlertype.N.0, i8* null]
+// CHECK: %[[CATCHPAD_DOUBLE:.*]] = catchpad [%rtti.TypeDescriptor2* @"\01??_R0N@8", i32 0, i8* null]
// CHECK: to label %[[CATCH_DOUBLE_HANDLER:.*]] unwind label %[[CATCHENDPAD]]
// CHECK: [[CATCH_DOUBLE_HANDLER]]
-// RUN: %clang_cc1 -std=c++11 -emit-llvm %s -o - -triple=x86_64-pc-windows-msvc -mconstructor-aliases -fexceptions -fcxx-exceptions | FileCheck -check-prefix WIN64 %s
+// RUN: %clang_cc1 -std=c++11 -emit-llvm %s -o - -triple=x86_64-pc-windows-msvc \
+// RUN: -mconstructor-aliases -fexceptions -fcxx-exceptions -fnew-ms-eh \
+// RUN: | FileCheck -check-prefix WIN64 %s
extern "C" void might_throw();
// WIN64: invoke void @might_throw()
// WIN64-NEXT: to label %[[cont:[^ ]*]] unwind label %[[lpad:[^ ]*]]
//
-// WIN64: [[cont]]
-// WIN64: br label %[[ret:[^ ]*]]
-//
// WIN64: [[lpad]]
-// WIN64: landingpad { i8*, i32 }
-// WIN64-NEXT: catch i8* null
-// WIN64: call void @llvm.eh.begincatch(i8* %{{[^,]*}}, i8* null)
+// WIN64: catchpad [i8* null, i32 64, i8* null]
+// WIN64: to label %[[catchit:[^ ]*]] unwind label %{{.*}}
+//
+// WIN64: [[catchit]]
// WIN64: call void @recover()
-// WIN64: call void @llvm.eh.endcatch()
-// WIN64: br label %[[ret]]
+// WIN64: catchret %{{.*}} to label %[[catchret:[^ ]*]]
+//
+// WIN64: [[catchret]]
+// WIN64-NEXT: br label %[[ret:[^ ]*]]
//
// WIN64: [[ret]]
// WIN64: ret void
+//
+// WIN64: [[cont]]
+// WIN64: br label %[[ret]]
extern "C" void catch_int() {
try {
}
// WIN64-LABEL: define void @catch_int()
-// WIN64: landingpad { i8*, i32 }
-// WIN64: %[[e_i8:[^ ]*]] = bitcast i32* %[[e_addr:[^ ]*]] to i8*
-// WIN64: call void @llvm.eh.begincatch(i8* %{{.*}}, i8* %[[e_i8]])
+// WIN64: catchpad [%rtti.TypeDescriptor2* @"\01??_R0H@8", i32 0, i32* %[[e_addr:[^\]]*]]]
// WIN64: %[[e_i8:[^ ]*]] = bitcast i32* %[[e_addr]] to i8*
// WIN64: call void @handle_exception(i8* %[[e_i8]])
-// WIN64: call void @llvm.eh.endcatch()
+// WIN64: catchret
extern "C" void catch_int_unnamed() {
try {
}
// WIN64-LABEL: define void @catch_int_unnamed()
-// WIN64: landingpad { i8*, i32 }
-// WIN64: call void @llvm.eh.begincatch(i8* %{{.*}}, i8* null)
-// WIN64: call void @llvm.eh.endcatch()
+// WIN64: catchpad [%rtti.TypeDescriptor2* @"\01??_R0H@8", i32 0, i8* null]
+// WIN64: catchret
struct A {
A();
// WIN64-LABEL: define void @catch_a_byval()
// WIN64: %[[e_addr:[^ ]*]] = alloca %struct.A
-// WIN64: landingpad { i8*, i32 }
-// WIN64: %[[e_i8:[^ ]*]] = bitcast %struct.A* %[[e_addr]] to i8*
-// WIN64: call void @llvm.eh.begincatch(i8* %{{.*}}, i8* %[[e_i8]])
+// WIN64: catchpad [%rtti.TypeDescriptor7* @"\01??_R0?AUA@@@8", i32 0, %struct.A* %[[e_addr]]]
// WIN64: %[[e_i8:[^ ]*]] = bitcast %struct.A* %[[e_addr]] to i8*
// WIN64: call void @handle_exception(i8* %[[e_i8]])
-// WIN64: call void @llvm.eh.endcatch()
+// WIN64: catchret
extern "C" void catch_a_ref() {
try {
// WIN64-LABEL: define void @catch_a_ref()
// WIN64: %[[e_addr:[^ ]*]] = alloca %struct.A*
-// WIN64: landingpad { i8*, i32 }
-// WIN64: %[[e_i8:[^ ]*]] = bitcast %struct.A** %[[e_addr]] to i8*
-// WIN64: call void @llvm.eh.begincatch(i8* %{{.*}}, i8* %[[e_i8]])
+// WIN64: catchpad [%rtti.TypeDescriptor7* @"\01??_R0?AUA@@@8", i32 8, %struct.A** %[[e_addr]]]
// WIN64: %[[eptr:[^ ]*]] = load %struct.A*, %struct.A** %[[e_addr]]
// WIN64: %[[eptr_i8:[^ ]*]] = bitcast %struct.A* %[[eptr]] to i8*
// WIN64: call void @handle_exception(i8* %[[eptr_i8]])
-// WIN64: call void @llvm.eh.endcatch()
+// WIN64: catchret
extern "C" void fn_with_exc_spec() throw(int) {
might_throw();
// WIN64-LABEL: define void @catch_nested()
// WIN64: invoke void @might_throw()
-// WIN64-NEXT: to label %[[cont1:[^ ]*]] unwind label %[[lp1:[^ ]*]]
-// WIN64: [[cont1]]
+// WIN64-NEXT: to label %{{.*}} unwind label %[[lp1:[^ ]*]]
//
// WIN64: [[lp1]]
-// WIN64: landingpad { i8*, i32 }
-// WIN64: call void @llvm.eh.begincatch(i8* %{{.*}}, i8* null)
+// WIN64: catchpad [%rtti.TypeDescriptor2* @"\01??_R0H@8", i32 0, i8* null]
+// WIN64: to label %[[catchit:[^ ]*]] unwind label %{{.*}}
+//
+// WIN64: [[catchit]]
// WIN64: invoke void @might_throw()
// WIN64-NEXT: to label %[[cont2:[^ ]*]] unwind label %[[lp2:[^ ]*]]
//
-// WIN64: [[cont2]]
-// WIN64-NEXT: br label %[[trycont:[^ ]*]]
-//
// WIN64: [[lp2]]
-// WIN64: landingpad { i8*, i32 }
-// WIN64: call void @llvm.eh.begincatch(i8* %{{.*}}, i8* null)
-// WIN64-NEXT: call void @might_throw()
-// WIN64-NEXT: call void @llvm.eh.endcatch()
-// WIN64-NEXT: br label %[[trycont]]
+// WIN64: catchpad [%rtti.TypeDescriptor2* @"\01??_R0H@8", i32 0, i8* null]
+// WIN64: to label %[[catchit2:[^ ]*]] unwind label %[[endcatch2:[^ ]*]]
+//
+// WIN64: [[catchit2]]
+// WIN64-NEXT: invoke void @might_throw()
+// WIN64-NEXT: to label %[[catchret1:[^ ]*]] unwind label %[[endcatch2]]
+//
+// WIN64: [[catchret1]]
+// WIN64: catchret {{.*}} to label %[[catchret2:[^ ]*]]
+//
+// WIN64: [[catchret2]]
+// WIN64: catchret {{.*}} to label %[[mainret:[^ ]*]]
+//
+// WIN64: [[mainret]]
+// WIN64: ret void
//
-// WIN64: [[trycont]]
-// WIN64: call void @llvm.eh.endcatch()
+// WIN64: [[endcatch2]]
+// WIN64: catchendpad unwind label %{{.*}}
-// RUN: %clang_cc1 -emit-llvm %s -o - -triple=i386-pc-win32 -mconstructor-aliases -fcxx-exceptions -fexceptions -fno-rtti -DTRY | FileCheck %s -check-prefix=TRY
+// RUN: %clang_cc1 -emit-llvm %s -o - -triple=i386-pc-win32 -mconstructor-aliases -fcxx-exceptions -fexceptions -fno-rtti -DTRY -fnew-ms-eh | FileCheck %s -check-prefix=TRY
// RUN: %clang_cc1 -emit-llvm %s -o - -triple=i386-pc-win32 -mconstructor-aliases -fcxx-exceptions -fexceptions -fno-rtti -DTHROW | FileCheck %s -check-prefix=THROW
// THROW-DAG: @"\01??_R0H@8" = linkonce_odr global %rtti.TypeDescriptor2 { i8** @"\01??_7type_info@@6B@", i8* null, [3 x i8] c".H\00" }, comdat
// THROW-DAG: @_CTA1H = linkonce_odr unnamed_addr constant %eh.CatchableTypeArray.1 { i32 1, [1 x %eh.CatchableType*] [%eh.CatchableType* @"_CT??_R0H@84"] }, section ".xdata", comdat
// THROW-DAG: @_TI1H = linkonce_odr unnamed_addr constant %eh.ThrowInfo { i32 0, i8* null, i8* null, i8* bitcast (%eh.CatchableTypeArray.1* @_CTA1H to i8*) }, section ".xdata", comdat
-// TRY-DAG: @llvm.eh.handlertype.PAH.1 = private unnamed_addr constant %eh.CatchHandlerType { i32 1, i8* bitcast (%rtti.TypeDescriptor4* @"\01??_R0PAH@8" to i8*) }, section "llvm.metadata"
-
void external();
inline void not_emitted() {
external(); // TRY: invoke void @"\01?external@@YAXXZ"
} catch (int) {
rv = 1;
- // TRY: call void @llvm.eh.begincatch(i8* %{{.*}}, i8* null)
- // TRY: call void @llvm.eh.endcatch()
+ // TRY: catchpad [%rtti.TypeDescriptor2* @"\01??_R0H@8", i32 0, i8* null]
+ // TRY: catchret
}
#endif
#ifdef THROW
external();
} catch (const int *) {
}
- // TRY: catch %eh.CatchHandlerType* @llvm.eh.handlertype.PAH.1
- // TRY: call i32 @llvm.eh.typeid.for(i8* bitcast (%eh.CatchHandlerType* @llvm.eh.handlertype.PAH.1 to i8*))
+ // TRY: catchpad [%rtti.TypeDescriptor4* @"\01??_R0PAH@8", i32 1, i8* null]
+ // TRY: catchret
}
#endif