[libcxx-commits] [libcxx] [libc++] [libc++abi] Initialize exception directly in make_exception_ptr if __cxa_init_primary_exception is available in ABI-library (PR #65534)

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Oct 25 15:57:39 PDT 2023


================
@@ -50,15 +67,38 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
 
 template <class _Ep>
 _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
-#  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+#  if defined(_LIBCPP_EXCEPTION_PTR_DIRECT_INIT)
+  using _Ep2 = __decay_t<_Ep>;
+  void* __ex = exception_ptr::__init_native_exception(
+      sizeof(_Ep), const_cast<std::type_info*>(&typeid(_Ep)), exception_ptr::__dest_thunk<_Ep2>);
+  // This could happen with older versions of libcxxrt/libcxxabi,
+  // which don't yet support direct exception initialization.
+  if (__ex == nullptr) {
+    try {
+      throw __e;
+    } catch (...) {
+      return current_exception();
+    }
+  }
+
   try {
-    throw __e;
+    ::new (__ex) _Ep2(__e);
+    return exception_ptr::__from_native_exception_pointer(__ex);
   } catch (...) {
+    exception_ptr::__free_native_exception(__ex);
----------------
EricWF wrote:

How do we know that we don't also have to destroy the constructed exception?

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


More information about the libcxx-commits mailing list