[libcxx-commits] [libcxx] [libc++] Fix std::make_exception_ptr interaction with ObjC (PR #135386)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Jul 2 06:33:18 PDT 2025


================
@@ -118,14 +113,42 @@ _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
     __cxxabiv1::__cxa_free_exception(__ex);
     return current_exception();
   }
-#    else
+}
+
+template <class _Ep>
+_LIBCPP_HIDE_FROM_ABI exception_ptr __make_exception_ptr_via_throw(_Ep& __e) _NOEXCEPT {
   try {
     throw __e;
   } catch (...) {
     return current_exception();
   }
+}
+
+template <class _Ep>
+_LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
+#  if _LIBCPP_HAS_EXCEPTIONS
+  // Objective-C exceptions are thrown via pointer. When throwing an Objective-C exception,
+  // Clang generates a call to `objc_exception_throw` instead of the usual `__cxa_throw`.
+  // That function creates an exception with a special Objective-C typeinfo instead of
+  // the usual C++ typeinfo, since that is needed to implement the behavior documented
+  // at [1]).
+  //
+  // Because of this special behavior, we can't create an exception via `__cxa_init_primary_exception`
+  // for Objective-C exceptions, otherwise we'd bypass `objc_exception_throw`. See https://llvm.org/PR135089.
+  //
+  // [1]:
+  // https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Exceptions/Articles/Exceptions64Bit.html
+  if constexpr (is_pointer<_Ep>::value) {
----------------
ldionne wrote:

Here, if there was a way to query Clang whether `_Ep` is an Objective-C pointer type, that would match exactly the condition they use in https://github.com/llvm/llvm-project/blob/5b384c3015100ad815f4d994d7ef35cc947db711/clang/lib/CodeGen/CGException.cpp#L456 to decide whether to generate an Objective-C `throw` or a normal C++ `throw`. Do you think that is something we should be able to query directly from Clang?

https://github.com/llvm/llvm-project/pull/135386


More information about the libcxx-commits mailing list