[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
Mon Jul 7 13:53:06 PDT 2025
https://github.com/ldionne updated https://github.com/llvm/llvm-project/pull/135386
>From 9f88c67d226864bd7df99a3e55d4a4790a8d69fa Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Fri, 11 Apr 2025 18:23:30 +0300
Subject: [PATCH 01/14] fix std::make_exception_ptr interaction with ObjC
---
libcxx/include/__exception/exception_ptr.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index dac5b00b57fe3..8b90a6569d136 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -92,7 +92,7 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
template <class _Ep>
_LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
# if _LIBCPP_HAS_EXCEPTIONS
-# if _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && __cplusplus >= 201103L
+# if !defined(__OBJC__) && _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && __cplusplus >= 201103L
using _Ep2 = __decay_t<_Ep>;
void* __ex = __cxxabiv1::__cxa_allocate_exception(sizeof(_Ep));
>From 764caba12345ab1bc626758d5052c4597cbade62 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Mon, 14 Apr 2025 17:17:37 +0300
Subject: [PATCH 02/14] add a comment about __OBJC__ define
---
libcxx/include/__exception/exception_ptr.h | 1 +
1 file changed, 1 insertion(+)
diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index 8b90a6569d136..167aa4d9b367a 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -92,6 +92,7 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
template <class _Ep>
_LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
# if _LIBCPP_HAS_EXCEPTIONS
+ // Clang treats throwing ObjC types differently, and we have to preserve original throw-ing behavior
# if !defined(__OBJC__) && _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && __cplusplus >= 201103L
using _Ep2 = __decay_t<_Ep>;
>From 6e108eb698cccec4202d711ae9795fffb53d5ec8 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Wed, 14 May 2025 01:11:01 +0300
Subject: [PATCH 03/14] remove the __OBJC__ usage in favor of checking whether
the type thrown is a pointer
---
libcxx/include/__exception/exception_ptr.h | 52 +++++++++++++---------
1 file changed, 32 insertions(+), 20 deletions(-)
diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index 167aa4d9b367a..e0c6fc48b1333 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -15,6 +15,7 @@
#include <__memory/addressof.h>
#include <__memory/construct_at.h>
#include <__type_traits/decay.h>
+#include <__type_traits/is_pointer.h>
#include <cstdlib>
#include <typeinfo>
@@ -92,39 +93,50 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
template <class _Ep>
_LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
# if _LIBCPP_HAS_EXCEPTIONS
+# if _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && __cplusplus >= 201703L
// Clang treats throwing ObjC types differently, and we have to preserve original throw-ing behavior
-# if !defined(__OBJC__) && _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && __cplusplus >= 201103L
- using _Ep2 = __decay_t<_Ep>;
-
- void* __ex = __cxxabiv1::__cxa_allocate_exception(sizeof(_Ep));
+ // to not break some ObjC invariants. ObjC types are thrown by a pointer, hence the condition;
+ // although it does also trigger for some valid c++ usages, this should be a case rare enough to
+ // not complicate the condition any further
+ if constexpr (std::is_pointer_v<_Ep>) {
+ try {
+ throw __e;
+ } catch (...) {
+ return current_exception();
+ }
+ } else {
+ using _Ep2 = __decay_t<_Ep>;
+
+ void* __ex = __cxxabiv1::__cxa_allocate_exception(sizeof(_Ep));
# ifdef __wasm__
- // In Wasm, a destructor returns its argument
- (void)__cxxabiv1::__cxa_init_primary_exception(
- __ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) -> void* {
+ // In Wasm, a destructor returns its argument
+ (void)__cxxabiv1::__cxa_init_primary_exception(
+ __ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) -> void* {
# else
- (void)__cxxabiv1::__cxa_init_primary_exception(__ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) {
+ (void)__cxxabiv1::__cxa_init_primary_exception(__ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) {
# endif
- std::__destroy_at(static_cast<_Ep2*>(__p));
+ std::__destroy_at(static_cast<_Ep2*>(__p));
# ifdef __wasm__
- return __p;
+ return __p;
# endif
- });
-
- try {
- ::new (__ex) _Ep2(__e);
- return exception_ptr::__from_native_exception_pointer(__ex);
- } catch (...) {
- __cxxabiv1::__cxa_free_exception(__ex);
- return current_exception();
+ });
+
+ try {
+ ::new (__ex) _Ep2(__e);
+ return exception_ptr::__from_native_exception_pointer(__ex);
+ } catch (...) {
+ __cxxabiv1::__cxa_free_exception(__ex);
+ return current_exception();
+ }
}
-# else
+# else // !(_LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && __cplusplus >= 201703L)
try {
throw __e;
} catch (...) {
return current_exception();
}
# endif
-# else
+# else // !LIBCPP_HAS_EXCEPTIONS
((void)__e);
std::abort();
# endif
>From a1e858653db7c21dc3ff5ff5ba887d72c6dc1942 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Tue, 20 May 2025 14:23:10 +0300
Subject: [PATCH 04/14] relax the c++-standard requirement
---
libcxx/include/__exception/exception_ptr.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index eb791d739e4b6..d38bec7720a5f 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -96,7 +96,7 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
template <class _Ep>
_LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
# if _LIBCPP_HAS_EXCEPTIONS
-# if _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && __cplusplus >= 201703L
+# if _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && __cplusplus >= 201103L
// Clang treats throwing ObjC types differently, and we have to preserve original throw-ing behavior
// to not break some ObjC invariants. ObjC types are thrown by a pointer, hence the condition;
// although it does also trigger for some valid c++ usages, this should be a case rare enough to
>From ec0735c8b312bfc89e2bd4c37ba089548619d1be Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Tue, 10 Jun 2025 16:18:38 +0300
Subject: [PATCH 05/14] fix the closing #else comment typo
---
libcxx/include/__exception/exception_ptr.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index 1142866548e52..485f3a80c13fc 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -131,7 +131,7 @@ _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
return current_exception();
}
}
-# else // !(_LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && __cplusplus >= 201703L)
+# else // !(_LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && __cplusplus >= 201103L)
try {
throw __e;
} catch (...) {
>From 985f7a602695f4dda168ae011cd0274f59c2a356 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Tue, 1 Jul 2025 13:58:52 -0400
Subject: [PATCH 06/14] Add test
---
.../function.objects/exceptions.pass.mm | 42 +++++++++++++++++++
1 file changed, 42 insertions(+)
create mode 100644 libcxx/test/libcxx/utilities/function.objects/exceptions.pass.mm
diff --git a/libcxx/test/libcxx/utilities/function.objects/exceptions.pass.mm b/libcxx/test/libcxx/utilities/function.objects/exceptions.pass.mm
new file mode 100644
index 0000000000000..6a737525bb8ce
--- /dev/null
+++ b/libcxx/test/libcxx/utilities/function.objects/exceptions.pass.mm
@@ -0,0 +1,42 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// This test ensures that we can catch an Objective-C++ exception by type when
+// throwing via `std::rethrow_exception`. See http://llvm.org/PR135089.
+
+// UNSUPPORTED: c++03
+
+// This test requires the Objective-C ARC, which is (only?) available on Darwin
+// out-of-the-box.
+// REQUIRES: has-fobjc-arc && darwin
+
+// ADDITIONAL_COMPILE_FLAGS: -fobjc-arc
+
+#include <cassert>
+#include <exception>
+
+#import <Foundation/Foundation.h>
+
+NSError* RecoverException(const std::exception_ptr& exc) {
+ try {
+ std::rethrow_exception(exc);
+ } catch (NSError* error) {
+ return error;
+ } catch (...) {
+ }
+ return nullptr;
+}
+
+int main(int, char**) {
+ NSError* error = [NSError errorWithDomain:NSPOSIXErrorDomain code:EPERM userInfo:nil];
+ std::exception_ptr exc = std::make_exception_ptr(error);
+ NSError* recov = RecoverException(exc);
+ assert(recov != nullptr);
+
+ return 0;
+}
>From e647f47ba7ce400c902841ac107185e111d26e56 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Tue, 1 Jul 2025 14:29:16 -0400
Subject: [PATCH 07/14] Move test to proper location
---
.../propagation/make_exception_ptr.objc.pass.mm} | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
rename libcxx/test/{libcxx/utilities/function.objects/exceptions.pass.mm => std/language.support/support.exception/propagation/make_exception_ptr.objc.pass.mm} (92%)
diff --git a/libcxx/test/libcxx/utilities/function.objects/exceptions.pass.mm b/libcxx/test/std/language.support/support.exception/propagation/make_exception_ptr.objc.pass.mm
similarity index 92%
rename from libcxx/test/libcxx/utilities/function.objects/exceptions.pass.mm
rename to libcxx/test/std/language.support/support.exception/propagation/make_exception_ptr.objc.pass.mm
index 6a737525bb8ce..3752f1aa7ac83 100644
--- a/libcxx/test/libcxx/utilities/function.objects/exceptions.pass.mm
+++ b/libcxx/test/std/language.support/support.exception/propagation/make_exception_ptr.objc.pass.mm
@@ -7,7 +7,8 @@
//===----------------------------------------------------------------------===//
// This test ensures that we can catch an Objective-C++ exception by type when
-// throwing via `std::rethrow_exception`. See http://llvm.org/PR135089.
+// throwing an exception created via `std::make_exception_ptr`.
+// See http://llvm.org/PR135089.
// UNSUPPORTED: c++03
>From ca8f571a1e93642ac638c3836d817ebdec7c2133 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Wed, 2 Jul 2025 08:38:38 -0400
Subject: [PATCH 08/14] Use is_pointer instead of is_pointer_v
---
libcxx/include/__exception/exception_ptr.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index 485f3a80c13fc..3da194af5b4db 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -100,7 +100,7 @@ _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
// to not break some ObjC invariants. ObjC types are thrown by a pointer, hence the condition;
// although it does also trigger for some valid c++ usages, this should be a case rare enough to
// not complicate the condition any further
- if constexpr (std::is_pointer_v<_Ep>) {
+ if constexpr (is_pointer<_Ep>::value) {
try {
throw __e;
} catch (...) {
>From ddbbe9795fe7a516099908881d14ebe36d8fb429 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Wed, 2 Jul 2025 09:21:08 -0400
Subject: [PATCH 09/14] Refactor the function to be easier to understand
---
libcxx/include/__exception/exception_ptr.h | 86 ++++++++++++----------
1 file changed, 48 insertions(+), 38 deletions(-)
diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index 3da194af5b4db..e076fdf4ddb54 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -93,51 +93,61 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
};
template <class _Ep>
-_LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
-# if _LIBCPP_HAS_EXCEPTIONS
-# if _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && __cplusplus >= 201103L
- // Clang treats throwing ObjC types differently, and we have to preserve original throw-ing behavior
- // to not break some ObjC invariants. ObjC types are thrown by a pointer, hence the condition;
- // although it does also trigger for some valid c++ usages, this should be a case rare enough to
- // not complicate the condition any further
- if constexpr (is_pointer<_Ep>::value) {
- try {
- throw __e;
- } catch (...) {
- return current_exception();
- }
- } else {
- using _Ep2 = __decay_t<_Ep>;
-
- void* __ex = __cxxabiv1::__cxa_allocate_exception(sizeof(_Ep));
-# ifdef __wasm__
- // In Wasm, a destructor returns its argument
- (void)__cxxabiv1::__cxa_init_primary_exception(
- __ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) -> void* {
-# else
- (void)__cxxabiv1::__cxa_init_primary_exception(__ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) {
-# endif
- std::__destroy_at(static_cast<_Ep2*>(__p));
-# ifdef __wasm__
- return __p;
-# endif
- });
-
- try {
- ::new (__ex) _Ep2(__e);
- return exception_ptr::__from_native_exception_pointer(__ex);
- } catch (...) {
- __cxxabiv1::__cxa_free_exception(__ex);
- return current_exception();
- }
+_LIBCPP_HIDE_FROM_ABI exception_ptr __make_exception_ptr_explicit(_Ep& __e) _NOEXCEPT {
+ using _Ep2 = __decay_t<_Ep>;
+ void* __ex = __cxxabiv1::__cxa_allocate_exception(sizeof(_Ep));
+# ifdef __wasm__
+ auto __cleanup = [](void* __p) -> void* {
+ std::__destroy_at(static_cast<_Ep2*>(__p));
+ return __p;
+ };
+# else
+ auto __cleanup = [](void* __p) { std::__destroy_at(static_cast<_Ep2*>(__p)); };
+# endif
+ (void)__cxxabiv1::__cxa_init_primary_exception(__ex, const_cast<std::type_info*>(&typeid(_Ep)), __cleanup);
+
+ try {
+ ::new (__ex) _Ep2(__e);
+ return exception_ptr::__from_native_exception_pointer(__ex);
+ } catch (...) {
+ __cxxabiv1::__cxa_free_exception(__ex);
+ return current_exception();
}
-# else // !(_LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && __cplusplus >= 201103L)
+}
+
+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) {
+ return std::__make_exception_ptr_via_throw(__e);
+ }
+
+# if _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && __cplusplus >= 201103L
+ return std::__make_exception_ptr_explicit(__e);
+# else
+ return std::__make_exception_ptr_via_throw(__e);
# endif
+
# else // !LIBCPP_HAS_EXCEPTIONS
((void)__e);
std::abort();
>From 3808d880070affebdb2b03fbdf8dc49289fb99e0 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Wed, 2 Jul 2025 09:23:08 -0400
Subject: [PATCH 10/14] Fix broken friend
---
libcxx/include/__exception/exception_ptr.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index e076fdf4ddb54..f287c05b33dc7 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -63,7 +63,7 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
static exception_ptr __from_native_exception_pointer(void*) _NOEXCEPT;
template <class _Ep>
- friend _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep) _NOEXCEPT;
+ friend _LIBCPP_HIDE_FROM_ABI exception_ptr __make_exception_ptr_explicit(_Ep&) _NOEXCEPT;
public:
// exception_ptr is basically a COW string so it is trivially relocatable.
>From f587c7191b0c14a1ca8ab5b572f40a1af4abb397 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Wed, 2 Jul 2025 09:35:54 -0400
Subject: [PATCH 11/14] Fix C++03
---
libcxx/include/__exception/exception_ptr.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index f287c05b33dc7..ddf1271a734b2 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -138,7 +138,7 @@ _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
//
// [1]:
// https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Exceptions/Articles/Exceptions64Bit.html
- if constexpr (is_pointer<_Ep>::value) {
+ if _LIBCPP_CONSTEXPR (is_pointer<_Ep>::value) {
return std::__make_exception_ptr_via_throw(__e);
}
>From 860bec26c216299e74ca7d37b585e43d2f0b7d80 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Wed, 2 Jul 2025 09:38:24 -0400
Subject: [PATCH 12/14] More C++03
---
libcxx/include/__exception/exception_ptr.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index ddf1271a734b2..25cda11af4747 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -142,7 +142,7 @@ _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
return std::__make_exception_ptr_via_throw(__e);
}
-# if _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && __cplusplus >= 201103L
+# if _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && !defined(_LIBCPP_CXX03_LANG)
return std::__make_exception_ptr_explicit(__e);
# else
return std::__make_exception_ptr_via_throw(__e);
>From 455abac62421992aa083aca5e64dea57bc7933e8 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Wed, 2 Jul 2025 12:22:03 -0400
Subject: [PATCH 13/14] Fix issue with -fno-exceptions
---
libcxx/include/__exception/exception_ptr.h | 17 +++++++++--------
.../propagation/make_exception_ptr.objc.pass.mm | 1 +
2 files changed, 10 insertions(+), 8 deletions(-)
diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index 25cda11af4747..1256a2de2395b 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -92,18 +92,19 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
friend _LIBCPP_EXPORTED_FROM_ABI void rethrow_exception(exception_ptr);
};
+# if _LIBCPP_HAS_EXCEPTIONS
template <class _Ep>
_LIBCPP_HIDE_FROM_ABI exception_ptr __make_exception_ptr_explicit(_Ep& __e) _NOEXCEPT {
using _Ep2 = __decay_t<_Ep>;
void* __ex = __cxxabiv1::__cxa_allocate_exception(sizeof(_Ep));
-# ifdef __wasm__
+# ifdef __wasm__
auto __cleanup = [](void* __p) -> void* {
std::__destroy_at(static_cast<_Ep2*>(__p));
return __p;
};
-# else
+# else
auto __cleanup = [](void* __p) { std::__destroy_at(static_cast<_Ep2*>(__p)); };
-# endif
+# endif
(void)__cxxabiv1::__cxa_init_primary_exception(__ex, const_cast<std::type_info*>(&typeid(_Ep)), __cleanup);
try {
@@ -126,7 +127,6 @@ _LIBCPP_HIDE_FROM_ABI exception_ptr __make_exception_ptr_via_throw(_Ep& __e) _NO
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
@@ -147,12 +147,13 @@ _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
# else
return std::__make_exception_ptr_via_throw(__e);
# endif
-
-# else // !LIBCPP_HAS_EXCEPTIONS
- ((void)__e);
+}
+# else // !_LIBCPP_HAS_EXCEPTIONS
+template <class _Ep>
+_LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep) _NOEXCEPT {
std::abort();
-# endif
}
+# endif // _LIBCPP_HAS_EXCEPTIONS
#else // _LIBCPP_ABI_MICROSOFT
diff --git a/libcxx/test/std/language.support/support.exception/propagation/make_exception_ptr.objc.pass.mm b/libcxx/test/std/language.support/support.exception/propagation/make_exception_ptr.objc.pass.mm
index 3752f1aa7ac83..05a6698ea1a59 100644
--- a/libcxx/test/std/language.support/support.exception/propagation/make_exception_ptr.objc.pass.mm
+++ b/libcxx/test/std/language.support/support.exception/propagation/make_exception_ptr.objc.pass.mm
@@ -10,6 +10,7 @@
// throwing an exception created via `std::make_exception_ptr`.
// See http://llvm.org/PR135089.
+// UNSUPPORTED: no-exceptions
// UNSUPPORTED: c++03
// This test requires the Objective-C ARC, which is (only?) available on Darwin
>From d8d66b9437ba901dcdaf43b877e34da38c696c6a Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Mon, 7 Jul 2025 16:52:30 -0400
Subject: [PATCH 14/14] Fix forgotten conditional for back-deployment
---
libcxx/include/__exception/exception_ptr.h | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index 1256a2de2395b..796fa924be121 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -93,18 +93,19 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
};
# if _LIBCPP_HAS_EXCEPTIONS
+# if _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION
template <class _Ep>
_LIBCPP_HIDE_FROM_ABI exception_ptr __make_exception_ptr_explicit(_Ep& __e) _NOEXCEPT {
using _Ep2 = __decay_t<_Ep>;
void* __ex = __cxxabiv1::__cxa_allocate_exception(sizeof(_Ep));
-# ifdef __wasm__
+# ifdef __wasm__
auto __cleanup = [](void* __p) -> void* {
std::__destroy_at(static_cast<_Ep2*>(__p));
return __p;
};
-# else
+# else
auto __cleanup = [](void* __p) { std::__destroy_at(static_cast<_Ep2*>(__p)); };
-# endif
+# endif
(void)__cxxabiv1::__cxa_init_primary_exception(__ex, const_cast<std::type_info*>(&typeid(_Ep)), __cleanup);
try {
@@ -115,6 +116,7 @@ _LIBCPP_HIDE_FROM_ABI exception_ptr __make_exception_ptr_explicit(_Ep& __e) _NOE
return current_exception();
}
}
+# endif
template <class _Ep>
_LIBCPP_HIDE_FROM_ABI exception_ptr __make_exception_ptr_via_throw(_Ep& __e) _NOEXCEPT {
More information about the libcxx-commits
mailing list