Skip to content

Commit 6e108eb

Browse files
committed
remove the __OBJC__ usage in favor of checking whether the type thrown is a pointer
1 parent 764caba commit 6e108eb

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed

libcxx/include/__exception/exception_ptr.h

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <__memory/addressof.h>
1616
#include <__memory/construct_at.h>
1717
#include <__type_traits/decay.h>
18+
#include <__type_traits/is_pointer.h>
1819
#include <cstdlib>
1920
#include <typeinfo>
2021

@@ -92,39 +93,50 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
9293
template <class _Ep>
9394
_LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
9495
# if _LIBCPP_HAS_EXCEPTIONS
96+
# if _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && __cplusplus >= 201703L
9597
// Clang treats throwing ObjC types differently, and we have to preserve original throw-ing behavior
96-
# if !defined(__OBJC__) && _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && __cplusplus >= 201103L
97-
using _Ep2 = __decay_t<_Ep>;
98-
99-
void* __ex = __cxxabiv1::__cxa_allocate_exception(sizeof(_Ep));
98+
// to not break some ObjC invariants. ObjC types are thrown by a pointer, hence the condition;
99+
// although it does also trigger for some valid c++ usages, this should be a case rare enough to
100+
// not complicate the condition any further
101+
if constexpr (std::is_pointer_v<_Ep>) {
102+
try {
103+
throw __e;
104+
} catch (...) {
105+
return current_exception();
106+
}
107+
} else {
108+
using _Ep2 = __decay_t<_Ep>;
109+
110+
void* __ex = __cxxabiv1::__cxa_allocate_exception(sizeof(_Ep));
100111
# ifdef __wasm__
101-
// In Wasm, a destructor returns its argument
102-
(void)__cxxabiv1::__cxa_init_primary_exception(
103-
__ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) -> void* {
112+
// In Wasm, a destructor returns its argument
113+
(void)__cxxabiv1::__cxa_init_primary_exception(
114+
__ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) -> void* {
104115
# else
105-
(void)__cxxabiv1::__cxa_init_primary_exception(__ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) {
116+
(void)__cxxabiv1::__cxa_init_primary_exception(__ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) {
106117
# endif
107-
std::__destroy_at(static_cast<_Ep2*>(__p));
118+
std::__destroy_at(static_cast<_Ep2*>(__p));
108119
# ifdef __wasm__
109-
return __p;
120+
return __p;
110121
# endif
111-
});
112-
113-
try {
114-
::new (__ex) _Ep2(__e);
115-
return exception_ptr::__from_native_exception_pointer(__ex);
116-
} catch (...) {
117-
__cxxabiv1::__cxa_free_exception(__ex);
118-
return current_exception();
122+
});
123+
124+
try {
125+
::new (__ex) _Ep2(__e);
126+
return exception_ptr::__from_native_exception_pointer(__ex);
127+
} catch (...) {
128+
__cxxabiv1::__cxa_free_exception(__ex);
129+
return current_exception();
130+
}
119131
}
120-
# else
132+
# else // !(_LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && __cplusplus >= 201703L)
121133
try {
122134
throw __e;
123135
} catch (...) {
124136
return current_exception();
125137
}
126138
# endif
127-
# else
139+
# else // !LIBCPP_HAS_EXCEPTIONS
128140
((void)__e);
129141
std::abort();
130142
# endif

0 commit comments

Comments
 (0)