xref: /freebsd/contrib/llvm-project/libcxx/src/support/runtime/exception_fallback.ipp (revision 2f9966ff63d65bd474478888c9088eeae3f9c669)
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
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_LIBCPP_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_LIBCPP_NORETURN void terminate() noexcept {
37#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
38  try {
39#endif // _LIBCPP_HAS_NO_EXCEPTIONS
40    (*get_terminate())();
41    // handler should not return
42    fprintf(stderr, "terminate_handler unexpectedly returned\n");
43    ::abort();
44#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
45  } catch (...) {
46    // handler should not throw exception
47    fprintf(stderr, "terminate_handler unexpectedly threw an exception\n");
48    ::abort();
49  }
50#endif // _LIBCPP_HAS_NO_EXCEPTIONS
51}
52
53bool uncaught_exception() noexcept { return uncaught_exceptions() > 0; }
54
55int uncaught_exceptions() noexcept {
56#warning uncaught_exception not yet implemented
57  fprintf(stderr, "uncaught_exceptions not yet implemented\n");
58  ::abort();
59}
60
61exception::~exception() noexcept {}
62
63const char* exception::what() const noexcept { return "std::exception"; }
64
65bad_exception::~bad_exception() noexcept {}
66
67const char* bad_exception::what() const noexcept { return "std::bad_exception"; }
68
69bad_alloc::bad_alloc() noexcept {}
70
71bad_alloc::~bad_alloc() noexcept {}
72
73const char* bad_alloc::what() const noexcept { return "std::bad_alloc"; }
74
75bad_array_new_length::bad_array_new_length() noexcept {}
76
77bad_array_new_length::~bad_array_new_length() noexcept {}
78
79const char* bad_array_new_length::what() const noexcept { return "bad_array_new_length"; }
80
81bad_cast::bad_cast() noexcept {}
82
83bad_typeid::bad_typeid() noexcept {}
84
85bad_cast::~bad_cast() noexcept {}
86
87const char* bad_cast::what() const noexcept { return "std::bad_cast"; }
88
89bad_typeid::~bad_typeid() noexcept {}
90
91const char* bad_typeid::what() const noexcept { return "std::bad_typeid"; }
92
93} // namespace std
94