From b2987d159a88ab0ee2e40c884eb4d77b42ab89b6 Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Wed, 10 Feb 2010 18:49:11 +0000 Subject: [PATCH] Switch to using -fsjlj-exceptions instead of hard-coding it. Notably, this fixes calls to the UnwindResumeOrRethrow function for C++/Obj-C exception handling, for Darwin ARM. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@95787 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Driver/ToolChain.h | 3 +++ lib/Basic/Targets.cpp | 3 --- lib/CodeGen/CGException.cpp | 6 +----- lib/CodeGen/CGObjCMac.cpp | 3 ++- lib/Driver/ToolChains.cpp | 6 ++++++ lib/Driver/ToolChains.h | 2 ++ lib/Driver/Tools.cpp | 3 +++ lib/Frontend/InitPreprocessor.cpp | 2 ++ test/CodeGenObjC/unwind-fn.m | 14 ++++++++++++++ 9 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 test/CodeGenObjC/unwind-fn.m diff --git a/include/clang/Driver/ToolChain.h b/include/clang/Driver/ToolChain.h index 669d64263d..42b3d47c06 100644 --- a/include/clang/Driver/ToolChain.h +++ b/include/clang/Driver/ToolChain.h @@ -123,6 +123,9 @@ public: /// UseDwarfDebugFlags - Embed the compile options to clang into the Dwarf /// compile unit information. virtual bool UseDwarfDebugFlags() const { return false; } + + /// UseSjLjExceptions - Does this tool chain use SjLj exceptions. + virtual bool UseSjLjExceptions() const { return false; }; }; } // end namespace driver diff --git a/lib/Basic/Targets.cpp b/lib/Basic/Targets.cpp index cf2edb2b84..b96e17d892 100644 --- a/lib/Basic/Targets.cpp +++ b/lib/Basic/Targets.cpp @@ -1385,9 +1385,6 @@ public: // when Neon instructions are actually available. if (FPU == NeonFPU && !SoftFloat && IsThumb2) Builder.defineMacro("__ARM_NEON__"); - - if (getTriple().getOS() == llvm::Triple::Darwin) - Builder.defineMacro("__USING_SJLJ_EXCEPTIONS__"); } virtual void getTargetBuiltins(const Builtin::Info *&Records, unsigned &NumRecords) const { diff --git a/lib/CodeGen/CGException.cpp b/lib/CodeGen/CGException.cpp index 60a1016cc0..d956c1c3cd 100644 --- a/lib/CodeGen/CGException.cpp +++ b/lib/CodeGen/CGException.cpp @@ -100,10 +100,6 @@ static llvm::Constant *getUnexpectedFn(CodeGenFunction &CGF) { return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_call_unexpected"); } -// FIXME: Eventually this will all go into the backend. Set from the target for -// now. -static int using_sjlj_exceptions = 0; - static llvm::Constant *getUnwindResumeOrRethrowFn(CodeGenFunction &CGF) { const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); std::vector Args(1, Int8PtrTy); @@ -112,7 +108,7 @@ static llvm::Constant *getUnwindResumeOrRethrowFn(CodeGenFunction &CGF) { llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), Args, false); - if (using_sjlj_exceptions) + if (CGF.CGM.getLangOptions().SjLjExceptions) return CGF.CGM.CreateRuntimeFunction(FTy, "_Unwind_SjLj_Resume"); return CGF.CGM.CreateRuntimeFunction(FTy, "_Unwind_Resume_or_Rethrow"); } diff --git a/lib/CodeGen/CGObjCMac.cpp b/lib/CodeGen/CGObjCMac.cpp index 2f931bd637..b0b1a9fe7c 100644 --- a/lib/CodeGen/CGObjCMac.cpp +++ b/lib/CodeGen/CGObjCMac.cpp @@ -738,7 +738,8 @@ public: return CGM.CreateRuntimeFunction( llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false), - "_Unwind_Resume_or_Rethrow"); + (CGM.getLangOptions().SjLjExceptions ? "_Unwind_SjLj_Resume" : + "_Unwind_Resume_or_Rethrow")); } llvm::Constant *getObjCEndCatchFn() { diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp index a00d8d9597..a7cd711df1 100644 --- a/lib/Driver/ToolChains.cpp +++ b/lib/Driver/ToolChains.cpp @@ -640,6 +640,12 @@ bool Darwin::UseDwarfDebugFlags() const { return false; } +bool Darwin::UseSjLjExceptions() const { + // Darwin uses SjLj exceptions on ARM. + return (getTriple().getArch() == llvm::Triple::arm || + getTriple().getArch() == llvm::Triple::thumb); +} + const char *Darwin::GetDefaultRelocationModel() const { return "pic"; } diff --git a/lib/Driver/ToolChains.h b/lib/Driver/ToolChains.h index fbb1136199..fda08758c9 100644 --- a/lib/Driver/ToolChains.h +++ b/lib/Driver/ToolChains.h @@ -176,6 +176,8 @@ public: virtual bool UseDwarfDebugFlags() const; + virtual bool UseSjLjExceptions() const; + /// } }; diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp index 2aa71163be..7bc7875b11 100644 --- a/lib/Driver/Tools.cpp +++ b/lib/Driver/Tools.cpp @@ -1004,6 +1004,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, if (needsExceptions(Args, InputType, getToolChain().getTriple())) CmdArgs.push_back("-fexceptions"); + if (getToolChain().UseSjLjExceptions()) + CmdArgs.push_back("-fsjlj-exceptions"); + // -frtti is default. if (!Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti)) CmdArgs.push_back("-fno-rtti"); diff --git a/lib/Frontend/InitPreprocessor.cpp b/lib/Frontend/InitPreprocessor.cpp index 9aaf132034..b7ab3d8cd4 100644 --- a/lib/Frontend/InitPreprocessor.cpp +++ b/lib/Frontend/InitPreprocessor.cpp @@ -279,6 +279,8 @@ static void InitializePredefinedMacros(const TargetInfo &TI, if (LangOpts.Exceptions) Builder.defineMacro("__EXCEPTIONS"); + if (LangOpts.SjLjExceptions) + Builder.defineMacro("__USING_SJLJ_EXCEPTIONS__"); if (LangOpts.CPlusPlus) { Builder.defineMacro("__DEPRECATED"); diff --git a/test/CodeGenObjC/unwind-fn.m b/test/CodeGenObjC/unwind-fn.m new file mode 100644 index 0000000000..0aa8cde024 --- /dev/null +++ b/test/CodeGenObjC/unwind-fn.m @@ -0,0 +1,14 @@ +// RUN: %clang_cc1 -fobjc-nonfragile-abi -emit-llvm -o - %s | FileCheck --check-prefix=DEFAULT_EH %s +// RUN: %clang_cc1 -fsjlj-exceptions -fobjc-nonfragile-abi -emit-llvm -o - %s | FileCheck --check-prefix=SJLJ_EH %s + +// DEFAULT_EH: declare void @_Unwind_Resume_or_Rethrow(i8*) +// SJLJ_EH: declare void @_Unwind_SjLj_Resume(i8*) + +void f1(), f2(); +void f0() { + @try { + f1(); + } @catch (...) { + f2(); + } +}