xref: /llvm-project/libcxx/src/support/runtime/exception_fallback.ipp (revision ba87515fea90b5d55836a8e3be63a7e683ce299d)
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 <__verbose_abort>
11
12namespace std {
13
14static constinit std::terminate_handler __terminate_handler   = nullptr;
15static constinit std::unexpected_handler __unexpected_handler = nullptr;
16
17// libcxxrt provides implementations of these functions itself.
18unexpected_handler set_unexpected(unexpected_handler func) noexcept {
19  return __libcpp_atomic_exchange(&__unexpected_handler, func);
20}
21
22unexpected_handler get_unexpected() noexcept { return __libcpp_atomic_load(&__unexpected_handler); }
23
24[[noreturn]] void unexpected() {
25  (*get_unexpected())();
26  // unexpected handler should not return
27  terminate();
28}
29
30terminate_handler set_terminate(terminate_handler func) noexcept {
31  return __libcpp_atomic_exchange(&__terminate_handler, func);
32}
33
34terminate_handler get_terminate() noexcept { return __libcpp_atomic_load(&__terminate_handler); }
35
36[[noreturn]] void terminate() noexcept {
37#if _LIBCPP_HAS_EXCEPTIONS
38  try {
39#endif // _LIBCPP_HAS_EXCEPTIONS
40    (*get_terminate())();
41    // handler should not return
42    __libcpp_verbose_abort("terminate_handler unexpectedly returned\n");
43#if _LIBCPP_HAS_EXCEPTIONS
44  } catch (...) {
45    // handler should not throw exception
46    __libcpp_verbose_abort("terminate_handler unexpectedly threw an exception\n");
47  }
48#endif // _LIBCPP_HAS_EXCEPTIONS
49}
50
51bool uncaught_exception() noexcept { return uncaught_exceptions() > 0; }
52
53int uncaught_exceptions() noexcept {
54#warning uncaught_exception not yet implemented
55  __libcpp_verbose_abort("uncaught_exceptions not yet implemented\n");
56}
57
58exception::~exception() noexcept {}
59
60const char* exception::what() const noexcept { return "std::exception"; }
61
62bad_exception::~bad_exception() noexcept {}
63
64const char* bad_exception::what() const noexcept { return "std::bad_exception"; }
65
66bad_alloc::bad_alloc() noexcept {}
67
68bad_alloc::~bad_alloc() noexcept {}
69
70const char* bad_alloc::what() const noexcept { return "std::bad_alloc"; }
71
72bad_array_new_length::bad_array_new_length() noexcept {}
73
74bad_array_new_length::~bad_array_new_length() noexcept {}
75
76const char* bad_array_new_length::what() const noexcept { return "bad_array_new_length"; }
77
78bad_cast::bad_cast() noexcept {}
79
80bad_typeid::bad_typeid() noexcept {}
81
82bad_cast::~bad_cast() noexcept {}
83
84const char* bad_cast::what() const noexcept { return "std::bad_cast"; }
85
86bad_typeid::~bad_typeid() noexcept {}
87
88const char* bad_typeid::what() const noexcept { return "std::bad_typeid"; }
89
90} // namespace std
91