[cfe-commits] r134456 - in /cfe/trunk: include/clang/Driver/CC1Options.td include/clang/Driver/ObjCRuntime.h include/clang/Frontend/CodeGenOptions.h lib/CodeGen/CGException.cpp lib/Driver/ToolChain.cpp lib/Driver/ToolChains.cpp lib/Driver/Tools.cpp lib/Frontend/CompilerInvocation.cpp test/CodeGenObjC/terminate.m

John McCall rjmccall at apple.com
Tue Jul 5 18:22:26 PDT 2011


Author: rjmccall
Date: Tue Jul  5 20:22:26 2011
New Revision: 134456

URL: http://llvm.org/viewvc/llvm-project?rev=134456&view=rev
Log:
Call objc_terminate() instead of abort() when a cleanup throws an
exception in Objective-C;  in Objective-C++ we still use std::terminate().
This is only available in very recent runtimes.


Added:
    cfe/trunk/test/CodeGenObjC/terminate.m
Modified:
    cfe/trunk/include/clang/Driver/CC1Options.td
    cfe/trunk/include/clang/Driver/ObjCRuntime.h
    cfe/trunk/include/clang/Frontend/CodeGenOptions.h
    cfe/trunk/lib/CodeGen/CGException.cpp
    cfe/trunk/lib/Driver/ToolChain.cpp
    cfe/trunk/lib/Driver/ToolChains.cpp
    cfe/trunk/lib/Driver/Tools.cpp
    cfe/trunk/lib/Frontend/CompilerInvocation.cpp

Modified: cfe/trunk/include/clang/Driver/CC1Options.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/CC1Options.td?rev=134456&r1=134455&r2=134456&view=diff
==============================================================================
--- cfe/trunk/include/clang/Driver/CC1Options.td (original)
+++ cfe/trunk/include/clang/Driver/CC1Options.td Tue Jul  5 20:22:26 2011
@@ -500,6 +500,8 @@
   HelpText<"The target Objective-C runtime provides ARC entrypoints">;
 def fobjc_runtime_has_weak : Flag<"-fobjc-runtime-has-weak">,
   HelpText<"The target Objective-C runtime supports ARC weak operations">;
+def fobjc_runtime_has_terminate : Flag<"-fobjc-runtime-has-terminate">,
+  HelpText<"The target Objective-C runtime provides an objc_terminate entrypoint">;
 def fobjc_gc : Flag<"-fobjc-gc">,
   HelpText<"Enable Objective-C garbage collection">;
 def fobjc_gc_only : Flag<"-fobjc-gc-only">,

Modified: cfe/trunk/include/clang/Driver/ObjCRuntime.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/ObjCRuntime.h?rev=134456&r1=134455&r2=134456&view=diff
==============================================================================
--- cfe/trunk/include/clang/Driver/ObjCRuntime.h (original)
+++ cfe/trunk/include/clang/Driver/ObjCRuntime.h Tue Jul  5 20:22:26 2011
@@ -30,7 +30,14 @@
   /// True if the runtime supports ARC zeroing __weak.
   unsigned HasWeak : 1;
 
-  ObjCRuntime() : RuntimeKind(NeXT), HasARC(false), HasWeak(false) {}
+  /// True if the runtime provides the following entrypoint:
+  ///   void objc_terminate(void);
+  /// If available, this will be called instead of abort() when an
+  /// exception is thrown out of an EH cleanup.
+  unsigned HasTerminate : 1;
+
+  ObjCRuntime() : RuntimeKind(NeXT), HasARC(false), HasWeak(false),
+    HasTerminate(false) {}
 };
 
 }

Modified: cfe/trunk/include/clang/Frontend/CodeGenOptions.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/CodeGenOptions.h?rev=134456&r1=134455&r2=134456&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/CodeGenOptions.h (original)
+++ cfe/trunk/include/clang/Frontend/CodeGenOptions.h Tue Jul  5 20:22:26 2011
@@ -77,6 +77,7 @@
   unsigned NoZeroInitializedInBSS : 1; /// -fno-zero-initialized-in-bss
   unsigned ObjCDispatchMethod : 2; /// Method of Objective-C dispatch to use.
   unsigned ObjCRuntimeHasARC : 1; /// The target runtime supports ARC natively
+  unsigned ObjCRuntimeHasTerminate : 1; /// The ObjC runtime has objc_terminate
   unsigned OmitLeafFramePointer : 1; /// Set when -momit-leaf-frame-pointer is
                                      /// enabled.
   unsigned OptimizationLevel : 3; /// The -O[0-4] option specified.
@@ -141,7 +142,6 @@
 public:
   CodeGenOptions() {
     AsmVerbose = 0;
-    ObjCAutoRefCountExceptions = 0;
     CXAAtExit = 1;
     CXXCtorDtorAliases = 0;
     DataSections = 0;
@@ -168,7 +168,10 @@
     NoNaNsFPMath = 0;
     NoZeroInitializedInBSS = 0;
     NumRegisterParameters = 0;
+    ObjCAutoRefCountExceptions = 0;
     ObjCDispatchMethod = Legacy;
+    ObjCRuntimeHasARC = 0;
+    ObjCRuntimeHasTerminate = 0;
     OmitLeafFramePointer = 0;
     OptimizationLevel = 0;
     OptimizeSize = 0;

Modified: cfe/trunk/lib/CodeGen/CGException.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGException.cpp?rev=134456&r1=134455&r2=134456&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGException.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGException.cpp Tue Jul  5 20:22:26 2011
@@ -137,8 +137,17 @@
     llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), 
                             /*IsVarArgs=*/false);
 
-  return CGF.CGM.CreateRuntimeFunction(FTy, 
-      CGF.CGM.getLangOptions().CPlusPlus ? "_ZSt9terminatev" : "abort");
+  llvm::StringRef name;
+
+  // In C++, use std::terminate().
+  if (CGF.getLangOptions().CPlusPlus)
+    name = "_ZSt9terminatev"; // FIXME: mangling!
+  else if (CGF.getLangOptions().ObjC1 &&
+           CGF.CGM.getCodeGenOpts().ObjCRuntimeHasTerminate)
+    name = "objc_terminate";
+  else
+    name = "abort";
+  return CGF.CGM.CreateRuntimeFunction(FTy, name);
 }
 
 static llvm::Constant *getCatchallRethrowFn(CodeGenFunction &CGF,

Modified: cfe/trunk/lib/Driver/ToolChain.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/ToolChain.cpp?rev=134456&r1=134455&r2=134456&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/ToolChain.cpp (original)
+++ cfe/trunk/lib/Driver/ToolChain.cpp Tue Jul  5 20:22:26 2011
@@ -55,12 +55,14 @@
     // Assume a minimal NeXT runtime.
     runtime.HasARC = false;
     runtime.HasWeak = false;
+    runtime.HasTerminate = false;
     return;
 
   case ObjCRuntime::GNU:
     // Assume a maximal GNU runtime.
     runtime.HasARC = true;
     runtime.HasWeak = true;
+    runtime.HasTerminate = false; // to be added
     return;
   }
   llvm_unreachable("invalid runtime kind!");

Modified: cfe/trunk/lib/Driver/ToolChains.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/ToolChains.cpp?rev=134456&r1=134455&r2=134456&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/ToolChains.cpp (original)
+++ cfe/trunk/lib/Driver/ToolChains.cpp Tue Jul  5 20:22:26 2011
@@ -99,6 +99,13 @@
     return ToolChain::configureObjCRuntime(runtime);
 
   runtime.HasARC = runtime.HasWeak = hasARCRuntime();
+
+  // So far, objc_terminate is only available in iOS 5.
+  // FIXME: do the simulator logic properly.
+  if (!ARCRuntimeForSimulator && isTargetIPhoneOS())
+    runtime.HasTerminate = !isIPhoneOSVersionLT(5);
+  else
+    runtime.HasTerminate = false;
 }
 
 // FIXME: Can we tablegen this?

Modified: cfe/trunk/lib/Driver/Tools.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/Tools.cpp?rev=134456&r1=134455&r2=134456&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/Tools.cpp (original)
+++ cfe/trunk/lib/Driver/Tools.cpp Tue Jul  5 20:22:26 2011
@@ -1781,6 +1781,8 @@
       CmdArgs.push_back("-fobjc-runtime-has-arc");
     if (objCRuntime.HasWeak)
       CmdArgs.push_back("-fobjc-runtime-has-weak");
+    if (objCRuntime.HasTerminate)
+      CmdArgs.push_back("-fobjc-runtime-has-terminate");
 
     // Compute the Objective-C ABI "version" to use. Version numbers are
     // slightly confusing for historical reasons:

Modified: cfe/trunk/lib/Frontend/CompilerInvocation.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInvocation.cpp?rev=134456&r1=134455&r2=134456&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInvocation.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInvocation.cpp Tue Jul  5 20:22:26 2011
@@ -125,6 +125,8 @@
   }
   if (Opts.ObjCRuntimeHasARC)
     Res.push_back("-fobjc-runtime-has-arc");
+  if (Opts.ObjCRuntimeHasTerminate)
+    Res.push_back("-fobjc-runtime-has-terminate");
   if (Opts.EmitGcovArcs)
     Res.push_back("-femit-coverage-data");
   if (Opts.EmitGcovNotes)
@@ -979,6 +981,7 @@
   Opts.AsmVerbose = Args.hasArg(OPT_masm_verbose);
   Opts.ObjCAutoRefCountExceptions = Args.hasArg(OPT_fobjc_arc_exceptions);
   Opts.ObjCRuntimeHasARC = Args.hasArg(OPT_fobjc_runtime_has_arc);
+  Opts.ObjCRuntimeHasTerminate = Args.hasArg(OPT_fobjc_runtime_has_terminate);
   Opts.CXAAtExit = !Args.hasArg(OPT_fno_use_cxa_atexit);
   Opts.CXXCtorDtorAliases = Args.hasArg(OPT_mconstructor_aliases);
   Opts.CodeModel = Args.getLastArgValue(OPT_mcode_model);

Added: cfe/trunk/test/CodeGenObjC/terminate.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenObjC/terminate.m?rev=134456&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenObjC/terminate.m (added)
+++ cfe/trunk/test/CodeGenObjC/terminate.m Tue Jul  5 20:22:26 2011
@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -fexceptions -fobjc-exceptions -fobjc-runtime-has-terminate -o - %s | FileCheck %s -check-prefix=CHECK-WITH
+// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -fexceptions -fobjc-exceptions -o - %s | FileCheck %s -check-prefix=CHECK-WITHOUT
+
+void destroy(void**);
+
+// rdar://problem/9519113
+void test0(void) {
+  void test0_helper(void);
+  void *ptr __attribute__((cleanup(destroy)));
+  test0_helper();
+
+  // CHECK-WITH:       define void @test0()
+  // CHECK-WITH:         [[PTR:%.*]] = alloca i8*,
+  // CHECK-WITH:         call void @destroy(i8** [[PTR]])
+  // CHECK-WITH-NEXT:    ret void
+  // CHECK-WITH:         invoke void @destroy(i8** [[PTR]])
+  // CHECK-WITH:         call i8* @llvm.eh.exception()
+  // CHECK-WITH-NEXT:    @llvm.eh.selector
+  // CHECK-WITH-NEXT:    call void @objc_terminate()
+
+  // CHECK-WITHOUT:    define void @test0()
+  // CHECK-WITHOUT:      [[PTR:%.*]] = alloca i8*,
+  // CHECK-WITHOUT:      call void @destroy(i8** [[PTR]])
+  // CHECK-WITHOUT-NEXT: ret void
+  // CHECK-WITHOUT:      invoke void @destroy(i8** [[PTR]])
+  // CHECK-WITHOUT:      call i8* @llvm.eh.exception()
+  // CHECK-WITHOUT-NEXT: @llvm.eh.selector
+  // CHECK-WITHOUT-NEXT: call void @abort()
+}





More information about the cfe-commits mailing list