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