Debian Codegraph @

libcxx/src/support/runtime/exception_fallback.ipp

raw
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 static constinit std::terminate_handler __terminate_handler = nullptr;
15 static constinit std::unexpected_handler __unexpected_handler = nullptr;
16
17 // libcxxrt provides implementations of these functions itself.
18 unexpected_handler set_unexpected(unexpected_handler func) noexcept {
19 return __libcpp_atomic_exchange(&__unexpected_handler, func);
20 }
21
22 unexpected_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
30 terminate_handler set_terminate(terminate_handler func) noexcept {
31 return __libcpp_atomic_exchange(&__terminate_handler, func);
32 }
33
34 terminate_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
53 bool uncaught_exception() noexcept { return uncaught_exceptions() > 0; }
54
55 int uncaught_exceptions() noexcept {
56 #warning uncaught_exception not yet implemented
57 fprintf(stderr, "uncaught_exceptions not yet implemented\n");
58 ::abort();
59 }
60
61 exception::~exception() noexcept {}
62
63 const char* exception::what() const noexcept { return "std::exception"; }
64
65 bad_exception::~bad_exception() noexcept {}
66
67 const char* bad_exception::what() const noexcept { return "std::bad_exception"; }
68
69 bad_alloc::bad_alloc() noexcept {}
70
71 bad_alloc::~bad_alloc() noexcept {}
72
73 const char* bad_alloc::what() const noexcept { return "std::bad_alloc"; }
74
75 bad_array_new_length::bad_array_new_length() noexcept {}
76
77 bad_array_new_length::~bad_array_new_length() noexcept {}
78
79 const char* bad_array_new_length::what() const noexcept { return "bad_array_new_length"; }
80
81 bad_cast::bad_cast() noexcept {}
82
83 bad_typeid::bad_typeid() noexcept {}
84
85 bad_cast::~bad_cast() noexcept {}
86
87 const char* bad_cast::what() const noexcept { return "std::bad_cast"; }
88
89 bad_typeid::~bad_typeid() noexcept {}
90
91 const char* bad_typeid::what() const noexcept { return "std::bad_typeid"; }
92
93 } // namespace std