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