xref: /openbsd-src/gnu/llvm/libcxx/src/support/runtime/exception_fallback.ipp (revision 4bdff4bed0e3d54e55670334c7d0077db4170f86)
146035553Spatrick// -*- C++ -*-
246035553Spatrick//===----------------------------------------------------------------------===//
346035553Spatrick//
446035553Spatrick// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
546035553Spatrick// See https://llvm.org/LICENSE.txt for license information.
646035553Spatrick// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
746035553Spatrick//
846035553Spatrick//===----------------------------------------------------------------------===//
946035553Spatrick
1046035553Spatrick#include <cstdio>
1146035553Spatrick
1246035553Spatricknamespace std {
1346035553Spatrick
14*4bdff4beSrobertstatic constinit std::terminate_handler  __terminate_handler = nullptr;
15*4bdff4beSrobertstatic constinit std::unexpected_handler __unexpected_handler = nullptr;
1646035553Spatrick
1746035553Spatrick// libcxxrt provides implementations of these functions itself.
1846035553Spatrickunexpected_handler
1976d0caaeSpatrickset_unexpected(unexpected_handler func) noexcept
2046035553Spatrick{
2146035553Spatrick  return __libcpp_atomic_exchange(&__unexpected_handler, func);
2246035553Spatrick}
2346035553Spatrick
2446035553Spatrickunexpected_handler
2576d0caaeSpatrickget_unexpected() noexcept
2646035553Spatrick{
2746035553Spatrick  return __libcpp_atomic_load(&__unexpected_handler);
2846035553Spatrick}
2946035553Spatrick
3046035553Spatrick_LIBCPP_NORETURN
3146035553Spatrickvoid unexpected()
3246035553Spatrick{
3346035553Spatrick    (*get_unexpected())();
3446035553Spatrick    // unexpected handler should not return
3546035553Spatrick    terminate();
3646035553Spatrick}
3746035553Spatrick
3846035553Spatrickterminate_handler
3976d0caaeSpatrickset_terminate(terminate_handler func) noexcept
4046035553Spatrick{
4146035553Spatrick  return __libcpp_atomic_exchange(&__terminate_handler, func);
4246035553Spatrick}
4346035553Spatrick
4446035553Spatrickterminate_handler
4576d0caaeSpatrickget_terminate() noexcept
4646035553Spatrick{
4746035553Spatrick  return __libcpp_atomic_load(&__terminate_handler);
4846035553Spatrick}
4946035553Spatrick
5046035553Spatrick_LIBCPP_NORETURN
5146035553Spatrickvoid
5276d0caaeSpatrickterminate() noexcept
5346035553Spatrick{
5446035553Spatrick#ifndef _LIBCPP_NO_EXCEPTIONS
5546035553Spatrick    try
5646035553Spatrick    {
5746035553Spatrick#endif // _LIBCPP_NO_EXCEPTIONS
5846035553Spatrick        (*get_terminate())();
5946035553Spatrick        // handler should not return
6046035553Spatrick        fprintf(stderr, "terminate_handler unexpectedly returned\n");
6146035553Spatrick        ::abort();
6246035553Spatrick#ifndef _LIBCPP_NO_EXCEPTIONS
6346035553Spatrick    }
6446035553Spatrick    catch (...)
6546035553Spatrick    {
6646035553Spatrick        // handler should not throw exception
6746035553Spatrick        fprintf(stderr, "terminate_handler unexpectedly threw an exception\n");
6846035553Spatrick        ::abort();
6946035553Spatrick    }
7046035553Spatrick#endif // _LIBCPP_NO_EXCEPTIONS
7146035553Spatrick}
7246035553Spatrick
7376d0caaeSpatrickbool uncaught_exception() noexcept { return uncaught_exceptions() > 0; }
7446035553Spatrick
7576d0caaeSpatrickint uncaught_exceptions() noexcept
7646035553Spatrick{
7746035553Spatrick#warning uncaught_exception not yet implemented
7846035553Spatrick  fprintf(stderr, "uncaught_exceptions not yet implemented\n");
7946035553Spatrick  ::abort();
8046035553Spatrick}
8146035553Spatrick
8246035553Spatrick
8376d0caaeSpatrickexception::~exception() noexcept
8446035553Spatrick{
8546035553Spatrick}
8646035553Spatrick
8776d0caaeSpatrickconst char* exception::what() const noexcept
8846035553Spatrick{
8946035553Spatrick  return "std::exception";
9046035553Spatrick}
9146035553Spatrick
9276d0caaeSpatrickbad_exception::~bad_exception() noexcept
9346035553Spatrick{
9446035553Spatrick}
9546035553Spatrick
9676d0caaeSpatrickconst char* bad_exception::what() const noexcept
9746035553Spatrick{
9846035553Spatrick  return "std::bad_exception";
9946035553Spatrick}
10046035553Spatrick
10146035553Spatrick
10276d0caaeSpatrickbad_alloc::bad_alloc() noexcept
10346035553Spatrick{
10446035553Spatrick}
10546035553Spatrick
10676d0caaeSpatrickbad_alloc::~bad_alloc() noexcept
10746035553Spatrick{
10846035553Spatrick}
10946035553Spatrick
11046035553Spatrickconst char*
11176d0caaeSpatrickbad_alloc::what() const noexcept
11246035553Spatrick{
11346035553Spatrick    return "std::bad_alloc";
11446035553Spatrick}
11546035553Spatrick
11676d0caaeSpatrickbad_array_new_length::bad_array_new_length() noexcept
11746035553Spatrick{
11846035553Spatrick}
11946035553Spatrick
12076d0caaeSpatrickbad_array_new_length::~bad_array_new_length() noexcept
12146035553Spatrick{
12246035553Spatrick}
12346035553Spatrick
12446035553Spatrickconst char*
12576d0caaeSpatrickbad_array_new_length::what() const noexcept
12646035553Spatrick{
12746035553Spatrick    return "bad_array_new_length";
12846035553Spatrick}
12946035553Spatrick
13076d0caaeSpatrickbad_cast::bad_cast() noexcept
13146035553Spatrick{
13246035553Spatrick}
13346035553Spatrick
13476d0caaeSpatrickbad_typeid::bad_typeid() noexcept
13546035553Spatrick{
13646035553Spatrick}
13746035553Spatrick
13876d0caaeSpatrickbad_cast::~bad_cast() noexcept
13946035553Spatrick{
14046035553Spatrick}
14146035553Spatrick
14246035553Spatrickconst char*
14376d0caaeSpatrickbad_cast::what() const noexcept
14446035553Spatrick{
14546035553Spatrick  return "std::bad_cast";
14646035553Spatrick}
14746035553Spatrick
14876d0caaeSpatrickbad_typeid::~bad_typeid() noexcept
14946035553Spatrick{
15046035553Spatrick}
15146035553Spatrick
15246035553Spatrickconst char*
15376d0caaeSpatrickbad_typeid::what() const noexcept
15446035553Spatrick{
15546035553Spatrick  return "std::bad_typeid";
15646035553Spatrick}
15746035553Spatrick
15846035553Spatrick} // namespace std
159