xref: /freebsd/contrib/llvm-project/libcxx/src/support/runtime/exception_fallback.ipp (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
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
12namespace 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.
19unexpected_handler
20set_unexpected(unexpected_handler func) noexcept
21{
22  return __libcpp_atomic_exchange(&__unexpected_handler, func);
23}
24
25unexpected_handler
26get_unexpected() noexcept
27{
28  return __libcpp_atomic_load(&__unexpected_handler);
29
30}
31
32_LIBCPP_NORETURN
33void unexpected()
34{
35    (*get_unexpected())();
36    // unexpected handler should not return
37    terminate();
38}
39
40terminate_handler
41set_terminate(terminate_handler func) noexcept
42{
43  return __libcpp_atomic_exchange(&__terminate_handler, func);
44}
45
46terminate_handler
47get_terminate() noexcept
48{
49  return __libcpp_atomic_load(&__terminate_handler);
50}
51
52_LIBCPP_NORETURN
53void
54terminate() noexcept
55{
56#ifndef _LIBCPP_NO_EXCEPTIONS
57    try
58    {
59#endif // _LIBCPP_NO_EXCEPTIONS
60        (*get_terminate())();
61        // handler should not return
62        fprintf(stderr, "terminate_handler unexpectedly returned\n");
63        ::abort();
64#ifndef _LIBCPP_NO_EXCEPTIONS
65    }
66    catch (...)
67    {
68        // handler should not throw exception
69        fprintf(stderr, "terminate_handler unexpectedly threw an exception\n");
70        ::abort();
71    }
72#endif // _LIBCPP_NO_EXCEPTIONS
73}
74
75bool uncaught_exception() noexcept { return uncaught_exceptions() > 0; }
76
77int uncaught_exceptions() noexcept
78{
79#warning uncaught_exception not yet implemented
80  fprintf(stderr, "uncaught_exceptions not yet implemented\n");
81  ::abort();
82}
83
84
85exception::~exception() noexcept
86{
87}
88
89const char* exception::what() const noexcept
90{
91  return "std::exception";
92}
93
94bad_exception::~bad_exception() noexcept
95{
96}
97
98const char* bad_exception::what() const noexcept
99{
100  return "std::bad_exception";
101}
102
103
104bad_alloc::bad_alloc() noexcept
105{
106}
107
108bad_alloc::~bad_alloc() noexcept
109{
110}
111
112const char*
113bad_alloc::what() const noexcept
114{
115    return "std::bad_alloc";
116}
117
118bad_array_new_length::bad_array_new_length() noexcept
119{
120}
121
122bad_array_new_length::~bad_array_new_length() noexcept
123{
124}
125
126const char*
127bad_array_new_length::what() const noexcept
128{
129    return "bad_array_new_length";
130}
131
132bad_cast::bad_cast() noexcept
133{
134}
135
136bad_typeid::bad_typeid() noexcept
137{
138}
139
140bad_cast::~bad_cast() noexcept
141{
142}
143
144const char*
145bad_cast::what() const noexcept
146{
147  return "std::bad_cast";
148}
149
150bad_typeid::~bad_typeid() noexcept
151{
152}
153
154const char*
155bad_typeid::what() const noexcept
156{
157  return "std::bad_typeid";
158}
159
160} // namespace std
161