]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libc++/src/support/runtime/exception_fallback.ipp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / libc++ / src / support / runtime / exception_fallback.ipp
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #include <cstdio>
12
13 namespace std {
14
15 _LIBCPP_SAFE_STATIC static std::terminate_handler  __terminate_handler;
16 _LIBCPP_SAFE_STATIC static std::unexpected_handler __unexpected_handler;
17
18
19 // libcxxrt provides implementations of these functions itself.
20 unexpected_handler
21 set_unexpected(unexpected_handler func) _NOEXCEPT
22 {
23   return __libcpp_atomic_exchange(&__unexpected_handler, func);
24 }
25
26 unexpected_handler
27 get_unexpected() _NOEXCEPT
28 {
29   return __libcpp_atomic_load(&__unexpected_handler);
30
31 }
32
33 _LIBCPP_NORETURN
34 void unexpected()
35 {
36     (*get_unexpected())();
37     // unexpected handler should not return
38     terminate();
39 }
40
41 terminate_handler
42 set_terminate(terminate_handler func) _NOEXCEPT
43 {
44   return __libcpp_atomic_exchange(&__terminate_handler, func);
45 }
46
47 terminate_handler
48 get_terminate() _NOEXCEPT
49 {
50   return __libcpp_atomic_load(&__terminate_handler);
51 }
52
53 #ifndef __EMSCRIPTEN__ // We provide this in JS
54 _LIBCPP_NORETURN
55 void
56 terminate() _NOEXCEPT
57 {
58 #ifndef _LIBCPP_NO_EXCEPTIONS
59     try
60     {
61 #endif  // _LIBCPP_NO_EXCEPTIONS
62         (*get_terminate())();
63         // handler should not return
64         fprintf(stderr, "terminate_handler unexpectedly returned\n");
65         ::abort();
66 #ifndef _LIBCPP_NO_EXCEPTIONS
67     }
68     catch (...)
69     {
70         // handler should not throw exception
71         fprintf(stderr, "terminate_handler unexpectedly threw an exception\n");
72         ::abort();
73     }
74 #endif  // _LIBCPP_NO_EXCEPTIONS
75 }
76 #endif // !__EMSCRIPTEN__
77
78 #if !defined(__EMSCRIPTEN__)
79 bool uncaught_exception() _NOEXCEPT { return uncaught_exceptions() > 0; }
80
81 int uncaught_exceptions() _NOEXCEPT
82 {
83 #warning uncaught_exception not yet implemented
84   fprintf(stderr, "uncaught_exceptions not yet implemented\n");
85   ::abort();
86 }
87 #endif // !__EMSCRIPTEN__
88
89
90 exception::~exception() _NOEXCEPT
91 {
92 }
93
94 const char* exception::what() const _NOEXCEPT
95 {
96   return "std::exception";
97 }
98
99 bad_exception::~bad_exception() _NOEXCEPT
100 {
101 }
102
103 const char* bad_exception::what() const _NOEXCEPT
104 {
105   return "std::bad_exception";
106 }
107
108
109 bad_alloc::bad_alloc() _NOEXCEPT
110 {
111 }
112
113 bad_alloc::~bad_alloc() _NOEXCEPT
114 {
115 }
116
117 const char*
118 bad_alloc::what() const _NOEXCEPT
119 {
120     return "std::bad_alloc";
121 }
122
123 bad_array_new_length::bad_array_new_length() _NOEXCEPT
124 {
125 }
126
127 bad_array_new_length::~bad_array_new_length() _NOEXCEPT
128 {
129 }
130
131 const char*
132 bad_array_new_length::what() const _NOEXCEPT
133 {
134     return "bad_array_new_length";
135 }
136
137 bad_cast::bad_cast() _NOEXCEPT
138 {
139 }
140
141 bad_typeid::bad_typeid() _NOEXCEPT
142 {
143 }
144
145 bad_cast::~bad_cast() _NOEXCEPT
146 {
147 }
148
149 const char*
150 bad_cast::what() const _NOEXCEPT
151 {
152   return "std::bad_cast";
153 }
154
155 bad_typeid::~bad_typeid() _NOEXCEPT
156 {
157 }
158
159 const char*
160 bad_typeid::what() const _NOEXCEPT
161 {
162   return "std::bad_typeid";
163 }
164
165 } // namespace std