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