xref: /aosp_15_r20/external/libcxx/src/support/runtime/exception_fallback.ipp (revision 58b9f456b02922dfdb1fad8a988d5fd8765ecb80)
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
13namespace 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.
20unexpected_handler
21set_unexpected(unexpected_handler func) _NOEXCEPT
22{
23  return __libcpp_atomic_exchange(&__unexpected_handler, func);
24}
25
26unexpected_handler
27get_unexpected() _NOEXCEPT
28{
29  return __libcpp_atomic_load(&__unexpected_handler);
30
31}
32
33_LIBCPP_NORETURN
34void unexpected()
35{
36    (*get_unexpected())();
37    // unexpected handler should not return
38    terminate();
39}
40
41terminate_handler
42set_terminate(terminate_handler func) _NOEXCEPT
43{
44  return __libcpp_atomic_exchange(&__terminate_handler, func);
45}
46
47terminate_handler
48get_terminate() _NOEXCEPT
49{
50  return __libcpp_atomic_load(&__terminate_handler);
51}
52
53#ifndef __EMSCRIPTEN__ // We provide this in JS
54_LIBCPP_NORETURN
55void
56terminate() _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__)
79bool uncaught_exception() _NOEXCEPT { return uncaught_exceptions() > 0; }
80
81int 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
90exception::~exception() _NOEXCEPT
91{
92}
93
94const char* exception::what() const _NOEXCEPT
95{
96  return "std::exception";
97}
98
99bad_exception::~bad_exception() _NOEXCEPT
100{
101}
102
103const char* bad_exception::what() const _NOEXCEPT
104{
105  return "std::bad_exception";
106}
107
108
109bad_alloc::bad_alloc() _NOEXCEPT
110{
111}
112
113bad_alloc::~bad_alloc() _NOEXCEPT
114{
115}
116
117const char*
118bad_alloc::what() const _NOEXCEPT
119{
120    return "std::bad_alloc";
121}
122
123bad_array_new_length::bad_array_new_length() _NOEXCEPT
124{
125}
126
127bad_array_new_length::~bad_array_new_length() _NOEXCEPT
128{
129}
130
131const char*
132bad_array_new_length::what() const _NOEXCEPT
133{
134    return "bad_array_new_length";
135}
136
137bad_cast::bad_cast() _NOEXCEPT
138{
139}
140
141bad_typeid::bad_typeid() _NOEXCEPT
142{
143}
144
145bad_cast::~bad_cast() _NOEXCEPT
146{
147}
148
149const char*
150bad_cast::what() const _NOEXCEPT
151{
152  return "std::bad_cast";
153}
154
155bad_typeid::~bad_typeid() _NOEXCEPT
156{
157}
158
159const char*
160bad_typeid::what() const _NOEXCEPT
161{
162  return "std::bad_typeid";
163}
164
165} // namespace std
166