blob: 2c16060a75835b0a8a4af5dcf49f230330122a5b [file] [log] [blame]
Nick Kledzik804b6e72010-05-14 20:19:37 +00001//===------------------------ exception.cpp -------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Nick Kledzik804b6e72010-05-14 20:19:37 +00007//
8//===----------------------------------------------------------------------===//
9#include <stdlib.h>
Howard Hinnanted14a762013-07-23 16:05:56 +000010#include <stdio.h>
Nick Kledzik804b6e72010-05-14 20:19:37 +000011
12#include "exception"
Peter Collingbourneece95912013-10-06 22:13:21 +000013#include "new"
Nick Kledzik804b6e72010-05-14 20:19:37 +000014
Dan Albert1d4a1ed2016-05-25 22:36:09 -070015#ifndef __has_include
16#define __has_include(inc) 0
17#endif
18
Eric Fiselier3be7f192014-11-01 00:41:42 +000019#if defined(__APPLE__) && !defined(LIBCXXRT)
Nick Kledzik804b6e72010-05-14 20:19:37 +000020 #include <cxxabi.h>
Howard Hinnantdea7f392012-02-02 20:48:35 +000021
Nick Kledzik804b6e72010-05-14 20:19:37 +000022 using namespace __cxxabiv1;
David Chisnallc512df12011-09-21 08:39:44 +000023 #define HAVE_DEPENDENT_EH_ABI 1
Howard Hinnantdea7f392012-02-02 20:48:35 +000024 #ifndef _LIBCPPABI_VERSION
25 using namespace __cxxabiapple;
26 // On Darwin, there are two STL shared libraries and a lower level ABI
Marshall Clowa46a0ad2013-11-11 23:27:19 +000027 // shared library. The globals holding the current terminate handler and
Howard Hinnantdea7f392012-02-02 20:48:35 +000028 // 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
Dan Albert1d4a1ed2016-05-25 22:36:09 -070032#elif defined(LIBCXXRT) || defined(LIBCXX_BUILDING_LIBCXXABI) || __has_include(<cxxabi.h>)
David Chisnallc512df12011-09-21 08:39:44 +000033 #include <cxxabi.h>
34 using namespace __cxxabiv1;
Richard Smith591e32d2012-07-11 09:35:47 +000035 #if defined(LIBCXXRT) || defined(_LIBCPPABI_VERSION)
36 #define HAVE_DEPENDENT_EH_ABI 1
37 #endif
Dan Albert1d4a1ed2016-05-25 22:36:09 -070038#elif !defined(__GLIBCXX__) // __has_include(<cxxabi.h>)
Nick Kledzik804b6e72010-05-14 20:19:37 +000039 static std::terminate_handler __terminate_handler;
40 static std::unexpected_handler __unexpected_handler;
Dan Albert1d4a1ed2016-05-25 22:36:09 -070041#endif // __has_include(<cxxabi.h>)
Nick Kledzik804b6e72010-05-14 20:19:37 +000042
David Chisnall1e8b3f92012-02-29 12:59:17 +000043namespace std
44{
45
Michael J. Spencera358fbe2012-11-30 21:02:29 +000046#if !defined(LIBCXXRT) && !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__)
Howard Hinnantdea7f392012-02-02 20:48:35 +000047
David Chisnallc512df12011-09-21 08:39:44 +000048// libcxxrt provides implementations of these functions itself.
David Chisnall1e8b3f92012-02-29 12:59:17 +000049unexpected_handler
50set_unexpected(unexpected_handler func) _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +000051{
Howard Hinnanta4451512010-12-02 16:45:21 +000052 return __sync_lock_test_and_set(&__unexpected_handler, func);
53}
54
David Chisnall1e8b3f92012-02-29 12:59:17 +000055unexpected_handler
56get_unexpected() _NOEXCEPT
Howard Hinnanta4451512010-12-02 16:45:21 +000057{
David Chisnall1e8b3f92012-02-29 12:59:17 +000058 return __sync_fetch_and_add(&__unexpected_handler, (unexpected_handler)0);
Nick Kledzik804b6e72010-05-14 20:19:37 +000059}
60
Richard Smith0405cc42012-07-26 02:04:22 +000061_LIBCPP_NORETURN
Nick Kledzik804b6e72010-05-14 20:19:37 +000062void
David Chisnall1e8b3f92012-02-29 12:59:17 +000063unexpected()
Nick Kledzik804b6e72010-05-14 20:19:37 +000064{
David Chisnall1e8b3f92012-02-29 12:59:17 +000065 (*get_unexpected())();
Howard Hinnantd5109772010-08-22 13:53:14 +000066 // unexpected handler should not return
David Chisnall1e8b3f92012-02-29 12:59:17 +000067 terminate();
Nick Kledzik804b6e72010-05-14 20:19:37 +000068}
69
David Chisnall1e8b3f92012-02-29 12:59:17 +000070terminate_handler
71set_terminate(terminate_handler func) _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +000072{
Howard Hinnanta4451512010-12-02 16:45:21 +000073 return __sync_lock_test_and_set(&__terminate_handler, func);
74}
75
David Chisnall1e8b3f92012-02-29 12:59:17 +000076terminate_handler
77get_terminate() _NOEXCEPT
Howard Hinnanta4451512010-12-02 16:45:21 +000078{
David Chisnall1e8b3f92012-02-29 12:59:17 +000079 return __sync_fetch_and_add(&__terminate_handler, (terminate_handler)0);
Nick Kledzik804b6e72010-05-14 20:19:37 +000080}
81
Marshall Clow2ccffef2013-11-19 18:05:03 +000082#ifndef __EMSCRIPTEN__ // We provide this in JS
Richard Smith0405cc42012-07-26 02:04:22 +000083_LIBCPP_NORETURN
Nick Kledzik804b6e72010-05-14 20:19:37 +000084void
David Chisnall1e8b3f92012-02-29 12:59:17 +000085terminate() _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +000086{
Howard Hinnantd4444702010-08-11 17:04:31 +000087#ifndef _LIBCPP_NO_EXCEPTIONS
88 try
89 {
Howard Hinnant16e6e1d2010-08-22 00:03:27 +000090#endif // _LIBCPP_NO_EXCEPTIONS
David Chisnall1e8b3f92012-02-29 12:59:17 +000091 (*get_terminate())();
Nick Kledzik804b6e72010-05-14 20:19:37 +000092 // handler should not return
Ed Schoutene5a356a2015-03-10 07:57:43 +000093 fprintf(stderr, "terminate_handler unexpectedly returned\n");
Peter Collingbourne4a0555a2013-10-06 22:13:24 +000094 ::abort();
Howard Hinnantd4444702010-08-11 17:04:31 +000095#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant16e6e1d2010-08-22 00:03:27 +000096 }
Howard Hinnantd4444702010-08-11 17:04:31 +000097 catch (...)
98 {
Nick Kledzik804b6e72010-05-14 20:19:37 +000099 // handler should not throw exception
Ed Schoutene5a356a2015-03-10 07:57:43 +0000100 fprintf(stderr, "terminate_handler unexpectedly threw an exception\n");
Peter Collingbourne4a0555a2013-10-06 22:13:24 +0000101 ::abort();
Nick Kledzik804b6e72010-05-14 20:19:37 +0000102 }
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000103#endif // _LIBCPP_NO_EXCEPTIONS
Nick Kledzik804b6e72010-05-14 20:19:37 +0000104}
Marshall Clow2ccffef2013-11-19 18:05:03 +0000105#endif // !__EMSCRIPTEN__
Howard Hinnantdea7f392012-02-02 20:48:35 +0000106#endif // !defined(LIBCXXRT) && !defined(_LIBCPPABI_VERSION)
Nick Kledzik804b6e72010-05-14 20:19:37 +0000107
Marshall Clow22f6c052015-06-02 22:25:23 +0000108#if !defined(LIBCXXRT) && !defined(__GLIBCXX__) && !defined(__EMSCRIPTEN__)
Marshall Clow8731c5d2015-06-02 15:33:38 +0000109bool uncaught_exception() _NOEXCEPT { return uncaught_exceptions() > 0; }
110
Marshall Clow8731c5d2015-06-02 15:33:38 +0000111int uncaught_exceptions() _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +0000112{
Marshall Clowdece7fe2013-03-18 17:45:34 +0000113#if defined(__APPLE__) || defined(_LIBCPPABI_VERSION)
Marshall Clow8731c5d2015-06-02 15:33:38 +0000114 // on Darwin, there is a helper function so __cxa_get_globals is private
115# if _LIBCPPABI_VERSION > 1101
116 return __cxa_uncaught_exceptions();
117# else
118 return __cxa_uncaught_exception() ? 1 : 0;
119# endif
Howard Hinnantd0ed21e2012-02-29 15:37:30 +0000120#else // __APPLE__
Howard Hinnantf7555062013-10-04 21:14:44 +0000121# if defined(_MSC_VER) && ! defined(__clang__)
Marshall Clow8731c5d2015-06-02 15:33:38 +0000122 _LIBCPP_WARNING("uncaught_exceptions not yet implemented")
Howard Hinnantf7555062013-10-04 21:14:44 +0000123# else
124# warning uncaught_exception not yet implemented
125# endif
Marshall Clow8731c5d2015-06-02 15:33:38 +0000126 fprintf(stderr, "uncaught_exceptions not yet implemented\n");
Peter Collingbourne4a0555a2013-10-06 22:13:24 +0000127 ::abort();
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000128#endif // __APPLE__
Nick Kledzik804b6e72010-05-14 20:19:37 +0000129}
130
Howard Hinnantf7555062013-10-04 21:14:44 +0000131
Howard Hinnantdea7f392012-02-02 20:48:35 +0000132#ifndef _LIBCPPABI_VERSION
133
Howard Hinnanted569212011-05-26 18:23:59 +0000134exception::~exception() _NOEXCEPT
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000135{
Nick Kledzik804b6e72010-05-14 20:19:37 +0000136}
137
Howard Hinnanted569212011-05-26 18:23:59 +0000138const char* exception::what() const _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +0000139{
140 return "std::exception";
141}
142
David Chisnall21a84cf2012-03-14 14:11:13 +0000143#endif // _LIBCPPABI_VERSION
144#endif //LIBCXXRT
Michael J. Spencera358fbe2012-11-30 21:02:29 +0000145#if !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__)
David Chisnall21a84cf2012-03-14 14:11:13 +0000146
147bad_exception::~bad_exception() _NOEXCEPT
148{
149}
150
Howard Hinnanted569212011-05-26 18:23:59 +0000151const char* bad_exception::what() const _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +0000152{
153 return "std::bad_exception";
154}
155
David Chisnall21a84cf2012-03-14 14:11:13 +0000156#endif
157
Peter Collingbourneece95912013-10-06 22:13:21 +0000158#if defined(__GLIBCXX__)
159
160// libsupc++ does not implement the dependent EH ABI and the functionality
161// it uses to implement std::exception_ptr (which it declares as an alias of
162// std::__exception_ptr::exception_ptr) is not directly exported to clients. So
163// we have little choice but to hijack std::__exception_ptr::exception_ptr's
164// (which fortunately has the same layout as our std::exception_ptr) copy
165// constructor, assignment operator and destructor (which are part of its
166// stable ABI), and its rethrow_exception(std::__exception_ptr::exception_ptr)
167// function.
168
169namespace __exception_ptr
170{
171
172struct exception_ptr
173{
174 void* __ptr_;
175
176 exception_ptr(const exception_ptr&) _NOEXCEPT;
177 exception_ptr& operator=(const exception_ptr&) _NOEXCEPT;
178 ~exception_ptr() _NOEXCEPT;
179};
180
181}
182
183_LIBCPP_NORETURN void rethrow_exception(__exception_ptr::exception_ptr);
184
185#endif
Howard Hinnantdea7f392012-02-02 20:48:35 +0000186
Howard Hinnanted569212011-05-26 18:23:59 +0000187exception_ptr::~exception_ptr() _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +0000188{
David Chisnallc512df12011-09-21 08:39:44 +0000189#if HAVE_DEPENDENT_EH_ABI
190 __cxa_decrement_exception_refcount(__ptr_);
Peter Collingbourneece95912013-10-06 22:13:21 +0000191#elif defined(__GLIBCXX__)
192 reinterpret_cast<__exception_ptr::exception_ptr*>(this)->~exception_ptr();
Nick Kledzik804b6e72010-05-14 20:19:37 +0000193#else
Howard Hinnantf7555062013-10-04 21:14:44 +0000194# if defined(_MSC_VER) && ! defined(__clang__)
195 _LIBCPP_WARNING("exception_ptr not yet implemented")
196# else
197# warning exception_ptr not yet implemented
198# endif
Ed Schoutene5a356a2015-03-10 07:57:43 +0000199 fprintf(stderr, "exception_ptr not yet implemented\n");
Peter Collingbourne4a0555a2013-10-06 22:13:24 +0000200 ::abort();
Peter Collingbourneece95912013-10-06 22:13:21 +0000201#endif
Nick Kledzik804b6e72010-05-14 20:19:37 +0000202}
203
Howard Hinnanted569212011-05-26 18:23:59 +0000204exception_ptr::exception_ptr(const exception_ptr& other) _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +0000205 : __ptr_(other.__ptr_)
206{
David Chisnallc512df12011-09-21 08:39:44 +0000207#if HAVE_DEPENDENT_EH_ABI
208 __cxa_increment_exception_refcount(__ptr_);
Peter Collingbourneece95912013-10-06 22:13:21 +0000209#elif defined(__GLIBCXX__)
210 new (reinterpret_cast<void*>(this)) __exception_ptr::exception_ptr(
211 reinterpret_cast<const __exception_ptr::exception_ptr&>(other));
Nick Kledzik804b6e72010-05-14 20:19:37 +0000212#else
Howard Hinnantf7555062013-10-04 21:14:44 +0000213# if defined(_MSC_VER) && ! defined(__clang__)
214 _LIBCPP_WARNING("exception_ptr not yet implemented")
215# else
216# warning exception_ptr not yet implemented
217# endif
Ed Schoutene5a356a2015-03-10 07:57:43 +0000218 fprintf(stderr, "exception_ptr not yet implemented\n");
Peter Collingbourne4a0555a2013-10-06 22:13:24 +0000219 ::abort();
Peter Collingbourneece95912013-10-06 22:13:21 +0000220#endif
Nick Kledzik804b6e72010-05-14 20:19:37 +0000221}
222
Howard Hinnanted569212011-05-26 18:23:59 +0000223exception_ptr& exception_ptr::operator=(const exception_ptr& other) _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +0000224{
David Chisnallc512df12011-09-21 08:39:44 +0000225#if HAVE_DEPENDENT_EH_ABI
Nick Kledzik804b6e72010-05-14 20:19:37 +0000226 if (__ptr_ != other.__ptr_)
227 {
David Chisnallc512df12011-09-21 08:39:44 +0000228 __cxa_increment_exception_refcount(other.__ptr_);
229 __cxa_decrement_exception_refcount(__ptr_);
Howard Hinnantd5109772010-08-22 13:53:14 +0000230 __ptr_ = other.__ptr_;
231 }
Nick Kledzik804b6e72010-05-14 20:19:37 +0000232 return *this;
Peter Collingbourneece95912013-10-06 22:13:21 +0000233#elif defined(__GLIBCXX__)
234 *reinterpret_cast<__exception_ptr::exception_ptr*>(this) =
235 reinterpret_cast<const __exception_ptr::exception_ptr&>(other);
236 return *this;
237#else
Howard Hinnantf7555062013-10-04 21:14:44 +0000238# if defined(_MSC_VER) && ! defined(__clang__)
239 _LIBCPP_WARNING("exception_ptr not yet implemented")
240# else
241# warning exception_ptr not yet implemented
242# endif
Ed Schoutene5a356a2015-03-10 07:57:43 +0000243 fprintf(stderr, "exception_ptr not yet implemented\n");
Peter Collingbourne4a0555a2013-10-06 22:13:24 +0000244 ::abort();
Peter Collingbourneece95912013-10-06 22:13:21 +0000245#endif
Nick Kledzik804b6e72010-05-14 20:19:37 +0000246}
247
Howard Hinnanted569212011-05-26 18:23:59 +0000248nested_exception::nested_exception() _NOEXCEPT
Howard Hinnanted2c2912010-05-27 17:06:52 +0000249 : __ptr_(current_exception())
250{
251}
252
Peter Collingbourne40455c62013-10-06 22:13:16 +0000253#if !defined(__GLIBCXX__)
254
Howard Hinnanted569212011-05-26 18:23:59 +0000255nested_exception::~nested_exception() _NOEXCEPT
Howard Hinnanted2c2912010-05-27 17:06:52 +0000256{
257}
258
Peter Collingbourne40455c62013-10-06 22:13:16 +0000259#endif
260
Richard Smith0405cc42012-07-26 02:04:22 +0000261_LIBCPP_NORETURN
Howard Hinnanted2c2912010-05-27 17:06:52 +0000262void
Howard Hinnant4b7a43d2011-05-26 17:07:32 +0000263nested_exception::rethrow_nested() const
Howard Hinnanted2c2912010-05-27 17:06:52 +0000264{
265 if (__ptr_ == nullptr)
266 terminate();
267 rethrow_exception(__ptr_);
268}
269
Peter Collingbourneece95912013-10-06 22:13:21 +0000270#if !defined(__GLIBCXX__)
Nick Kledzik804b6e72010-05-14 20:19:37 +0000271
David Chisnall1e8b3f92012-02-29 12:59:17 +0000272exception_ptr current_exception() _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +0000273{
David Chisnallc512df12011-09-21 08:39:44 +0000274#if HAVE_DEPENDENT_EH_ABI
Howard Hinnantd5109772010-08-22 13:53:14 +0000275 // be nicer if there was a constructor that took a ptr, then
276 // this whole function would be just:
277 // return exception_ptr(__cxa_current_primary_exception());
David Chisnall1e8b3f92012-02-29 12:59:17 +0000278 exception_ptr ptr;
David Chisnallc512df12011-09-21 08:39:44 +0000279 ptr.__ptr_ = __cxa_current_primary_exception();
Howard Hinnantd5109772010-08-22 13:53:14 +0000280 return ptr;
Peter Collingbourneece95912013-10-06 22:13:21 +0000281#else
Howard Hinnantf7555062013-10-04 21:14:44 +0000282# if defined(_MSC_VER) && ! defined(__clang__)
283 _LIBCPP_WARNING( "exception_ptr not yet implemented" )
284# else
285# warning exception_ptr not yet implemented
286# endif
Ed Schoutene5a356a2015-03-10 07:57:43 +0000287 fprintf(stderr, "exception_ptr not yet implemented\n");
Peter Collingbourne4a0555a2013-10-06 22:13:24 +0000288 ::abort();
Peter Collingbourneece95912013-10-06 22:13:21 +0000289#endif
Nick Kledzik804b6e72010-05-14 20:19:37 +0000290}
291
Peter Collingbourneece95912013-10-06 22:13:21 +0000292#endif // !__GLIBCXX__
293
Richard Smith0405cc42012-07-26 02:04:22 +0000294_LIBCPP_NORETURN
David Chisnall1e8b3f92012-02-29 12:59:17 +0000295void rethrow_exception(exception_ptr p)
Nick Kledzik804b6e72010-05-14 20:19:37 +0000296{
David Chisnallc512df12011-09-21 08:39:44 +0000297#if HAVE_DEPENDENT_EH_ABI
298 __cxa_rethrow_primary_exception(p.__ptr_);
Howard Hinnantd5109772010-08-22 13:53:14 +0000299 // if p.__ptr_ is NULL, above returns so we terminate
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000300 terminate();
Peter Collingbourneece95912013-10-06 22:13:21 +0000301#elif defined(__GLIBCXX__)
302 rethrow_exception(reinterpret_cast<__exception_ptr::exception_ptr&>(p));
303#else
Howard Hinnantf7555062013-10-04 21:14:44 +0000304# if defined(_MSC_VER) && ! defined(__clang__)
305 _LIBCPP_WARNING("exception_ptr not yet implemented")
306# else
307# warning exception_ptr not yet implemented
308# endif
Ed Schoutene5a356a2015-03-10 07:57:43 +0000309 fprintf(stderr, "exception_ptr not yet implemented\n");
Peter Collingbourne4a0555a2013-10-06 22:13:24 +0000310 ::abort();
Peter Collingbourneece95912013-10-06 22:13:21 +0000311#endif
Nick Kledzik804b6e72010-05-14 20:19:37 +0000312}
David Chisnall1e8b3f92012-02-29 12:59:17 +0000313} // std