1 //===------------------------ exception.cpp -------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
15 #if defined(_LIBCPP_ABI_MICROSOFT)
17 #include <corecrt_terminate.h>
18 #elif defined(__APPLE__) && !defined(LIBCXXRT) && \
19 !defined(_LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY)
22 using namespace __cxxabiv1;
23 #define HAVE_DEPENDENT_EH_ABI 1
24 #ifndef _LIBCPPABI_VERSION
25 using namespace __cxxabiapple;
26 // On Darwin, there are two STL shared libraries and a lower level ABI
27 // shared library. The globals holding the current terminate handler and
28 // current unexpected handler are in the ABI library.
29 #define __terminate_handler __cxxabiapple::__cxa_terminate_handler
30 #define __unexpected_handler __cxxabiapple::__cxa_unexpected_handler
31 #endif // _LIBCPPABI_VERSION
32 #elif defined(LIBCXXRT) || defined(LIBCXX_BUILDING_LIBCXXABI)
34 using namespace __cxxabiv1;
35 #if defined(LIBCXXRT) || defined(_LIBCPPABI_VERSION)
36 #define HAVE_DEPENDENT_EH_ABI 1
38 #elif !defined(__GLIBCXX__) // defined(LIBCXX_BUILDING_LIBCXXABI)
39 _LIBCPP_SAFE_STATIC static std::terminate_handler __terminate_handler;
40 _LIBCPP_SAFE_STATIC static std::unexpected_handler __unexpected_handler;
41 #endif // defined(LIBCXX_BUILDING_LIBCXXABI)
46 #if !defined(LIBCXXRT) && !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__)
48 // libcxxrt provides implementations of these functions itself.
50 set_unexpected(unexpected_handler func) _NOEXCEPT
52 #if defined(_LIBCPP_ABI_MICROSOFT)
53 return ::set_unexpected(func);
55 return __sync_lock_test_and_set(&__unexpected_handler, func);
60 get_unexpected() _NOEXCEPT
62 #if defined(_LIBCPP_ABI_MICROSOFT)
63 return ::_get_unexpected();
65 return __sync_fetch_and_add(&__unexpected_handler, (unexpected_handler)0);
73 (*get_unexpected())();
74 // unexpected handler should not return
79 set_terminate(terminate_handler func) _NOEXCEPT
81 #if defined(_LIBCPP_ABI_MICROSOFT)
82 return ::set_terminate(func);
84 return __sync_lock_test_and_set(&__terminate_handler, func);
89 get_terminate() _NOEXCEPT
91 #if defined(_LIBCPP_ABI_MICROSOFT)
92 return ::_get_terminate();
94 return __sync_fetch_and_add(&__terminate_handler, (terminate_handler)0);
98 #ifndef __EMSCRIPTEN__ // We provide this in JS
101 terminate() _NOEXCEPT
103 #ifndef _LIBCPP_NO_EXCEPTIONS
106 #endif // _LIBCPP_NO_EXCEPTIONS
107 (*get_terminate())();
108 // handler should not return
109 fprintf(stderr, "terminate_handler unexpectedly returned\n");
111 #ifndef _LIBCPP_NO_EXCEPTIONS
115 // handler should not throw exception
116 fprintf(stderr, "terminate_handler unexpectedly threw an exception\n");
119 #endif // _LIBCPP_NO_EXCEPTIONS
121 #endif // !__EMSCRIPTEN__
122 #endif // !defined(LIBCXXRT) && !defined(_LIBCPPABI_VERSION)
124 #if !defined(LIBCXXRT) && !defined(__GLIBCXX__) && !defined(__EMSCRIPTEN__)
126 bool uncaught_exception() _NOEXCEPT { return uncaught_exceptions() > 0; }
128 int uncaught_exceptions() _NOEXCEPT
130 #if !defined(_LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY) && \
131 (defined(__APPLE__) || defined(_LIBCPPABI_VERSION))
132 // on Darwin, there is a helper function so __cxa_get_globals is private
133 # if _LIBCPPABI_VERSION > 1101
134 return __cxa_uncaught_exceptions();
136 return __cxa_uncaught_exception() ? 1 : 0;
138 #elif defined(_LIBCPP_ABI_MICROSOFT)
139 return __uncaught_exceptions();
141 # if defined(_MSC_VER) && ! defined(__clang__)
142 _LIBCPP_WARNING("uncaught_exceptions not yet implemented")
144 # warning uncaught_exception not yet implemented
146 fprintf(stderr, "uncaught_exceptions not yet implemented\n");
152 #ifndef _LIBCPPABI_VERSION
154 exception::~exception() _NOEXCEPT
158 const char* exception::what() const _NOEXCEPT
160 return "std::exception";
163 #endif // _LIBCPPABI_VERSION
165 #if !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__)
167 bad_exception::~bad_exception() _NOEXCEPT
171 const char* bad_exception::what() const _NOEXCEPT
173 return "std::bad_exception";
178 #if defined(__GLIBCXX__)
180 // libsupc++ does not implement the dependent EH ABI and the functionality
181 // it uses to implement std::exception_ptr (which it declares as an alias of
182 // std::__exception_ptr::exception_ptr) is not directly exported to clients. So
183 // we have little choice but to hijack std::__exception_ptr::exception_ptr's
184 // (which fortunately has the same layout as our std::exception_ptr) copy
185 // constructor, assignment operator and destructor (which are part of its
186 // stable ABI), and its rethrow_exception(std::__exception_ptr::exception_ptr)
189 namespace __exception_ptr
196 exception_ptr(const exception_ptr&) _NOEXCEPT;
197 exception_ptr& operator=(const exception_ptr&) _NOEXCEPT;
198 ~exception_ptr() _NOEXCEPT;
203 _LIBCPP_NORETURN void rethrow_exception(__exception_ptr::exception_ptr);
207 exception_ptr::~exception_ptr() _NOEXCEPT
209 #if HAVE_DEPENDENT_EH_ABI
210 __cxa_decrement_exception_refcount(__ptr_);
211 #elif defined(__GLIBCXX__)
212 reinterpret_cast<__exception_ptr::exception_ptr*>(this)->~exception_ptr();
214 # if defined(_MSC_VER) && ! defined(__clang__)
215 _LIBCPP_WARNING("exception_ptr not yet implemented")
217 # warning exception_ptr not yet implemented
219 fprintf(stderr, "exception_ptr not yet implemented\n");
224 exception_ptr::exception_ptr(const exception_ptr& other) _NOEXCEPT
225 : __ptr_(other.__ptr_)
227 #if HAVE_DEPENDENT_EH_ABI
228 __cxa_increment_exception_refcount(__ptr_);
229 #elif defined(__GLIBCXX__)
230 new (reinterpret_cast<void*>(this)) __exception_ptr::exception_ptr(
231 reinterpret_cast<const __exception_ptr::exception_ptr&>(other));
233 # if defined(_MSC_VER) && ! defined(__clang__)
234 _LIBCPP_WARNING("exception_ptr not yet implemented")
236 # warning exception_ptr not yet implemented
238 fprintf(stderr, "exception_ptr not yet implemented\n");
243 exception_ptr& exception_ptr::operator=(const exception_ptr& other) _NOEXCEPT
245 #if HAVE_DEPENDENT_EH_ABI
246 if (__ptr_ != other.__ptr_)
248 __cxa_increment_exception_refcount(other.__ptr_);
249 __cxa_decrement_exception_refcount(__ptr_);
250 __ptr_ = other.__ptr_;
253 #elif defined(__GLIBCXX__)
254 *reinterpret_cast<__exception_ptr::exception_ptr*>(this) =
255 reinterpret_cast<const __exception_ptr::exception_ptr&>(other);
258 # if defined(_MSC_VER) && ! defined(__clang__)
259 _LIBCPP_WARNING("exception_ptr not yet implemented")
261 # warning exception_ptr not yet implemented
263 fprintf(stderr, "exception_ptr not yet implemented\n");
268 nested_exception::nested_exception() _NOEXCEPT
269 : __ptr_(current_exception())
273 #if !defined(__GLIBCXX__)
275 nested_exception::~nested_exception() _NOEXCEPT
283 nested_exception::rethrow_nested() const
285 if (__ptr_ == nullptr)
287 rethrow_exception(__ptr_);
290 #if !defined(__GLIBCXX__)
292 exception_ptr current_exception() _NOEXCEPT
294 #if HAVE_DEPENDENT_EH_ABI
295 // be nicer if there was a constructor that took a ptr, then
296 // this whole function would be just:
297 // return exception_ptr(__cxa_current_primary_exception());
299 ptr.__ptr_ = __cxa_current_primary_exception();
302 # if defined(_MSC_VER) && ! defined(__clang__)
303 _LIBCPP_WARNING( "exception_ptr not yet implemented" )
305 # warning exception_ptr not yet implemented
307 fprintf(stderr, "exception_ptr not yet implemented\n");
312 #endif // !__GLIBCXX__
315 void rethrow_exception(exception_ptr p)
317 #if HAVE_DEPENDENT_EH_ABI
318 __cxa_rethrow_primary_exception(p.__ptr_);
319 // if p.__ptr_ is NULL, above returns so we terminate
321 #elif defined(__GLIBCXX__)
322 rethrow_exception(reinterpret_cast<__exception_ptr::exception_ptr&>(p));
324 # if defined(_MSC_VER) && ! defined(__clang__)
325 _LIBCPP_WARNING("exception_ptr not yet implemented")
327 # warning exception_ptr not yet implemented
329 fprintf(stderr, "exception_ptr not yet implemented\n");