xref: /openbsd-src/gnu/llvm/libcxx/src/support/runtime/exception_msvc.ipp (revision 76d0caaeb19ae0808d90af1d0b3b7b50b3e5383f)
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#ifndef _LIBCPP_ABI_MICROSOFT
1146035553Spatrick#error this header can only be used when targeting the MSVC ABI
1246035553Spatrick#endif
1346035553Spatrick
1446035553Spatrick#include <stdio.h>
1546035553Spatrick#include <stdlib.h>
1646035553Spatrick
1746035553Spatrickextern "C" {
1846035553Spatricktypedef void (__cdecl* terminate_handler)();
1946035553Spatrick_LIBCPP_CRT_FUNC terminate_handler __cdecl set_terminate(
2046035553Spatrick    terminate_handler _NewTerminateHandler) throw();
2146035553Spatrick_LIBCPP_CRT_FUNC terminate_handler __cdecl _get_terminate();
2246035553Spatrick
2346035553Spatricktypedef void (__cdecl* unexpected_handler)();
2446035553Spatrickunexpected_handler __cdecl set_unexpected(
2546035553Spatrick    unexpected_handler _NewUnexpectedHandler) throw();
2646035553Spatrickunexpected_handler __cdecl _get_unexpected();
2746035553Spatrick
2846035553Spatrickint __cdecl __uncaught_exceptions();
2946035553Spatrick}
3046035553Spatrick
3146035553Spatricknamespace std {
3246035553Spatrick
3346035553Spatrickunexpected_handler
34*76d0caaeSpatrickset_unexpected(unexpected_handler func) noexcept {
3546035553Spatrick  return ::set_unexpected(func);
3646035553Spatrick}
3746035553Spatrick
38*76d0caaeSpatrickunexpected_handler get_unexpected() noexcept {
3946035553Spatrick  return ::_get_unexpected();
4046035553Spatrick}
4146035553Spatrick
4246035553Spatrick_LIBCPP_NORETURN
4346035553Spatrickvoid unexpected() {
4446035553Spatrick    (*get_unexpected())();
4546035553Spatrick    // unexpected handler should not return
4646035553Spatrick    terminate();
4746035553Spatrick}
4846035553Spatrick
49*76d0caaeSpatrickterminate_handler set_terminate(terminate_handler func) noexcept {
5046035553Spatrick  return ::set_terminate(func);
5146035553Spatrick}
5246035553Spatrick
53*76d0caaeSpatrickterminate_handler get_terminate() noexcept {
5446035553Spatrick  return ::_get_terminate();
5546035553Spatrick}
5646035553Spatrick
5746035553Spatrick_LIBCPP_NORETURN
58*76d0caaeSpatrickvoid terminate() noexcept
5946035553Spatrick{
6046035553Spatrick#ifndef _LIBCPP_NO_EXCEPTIONS
6146035553Spatrick    try
6246035553Spatrick    {
6346035553Spatrick#endif // _LIBCPP_NO_EXCEPTIONS
6446035553Spatrick        (*get_terminate())();
6546035553Spatrick        // handler should not return
6646035553Spatrick        fprintf(stderr, "terminate_handler unexpectedly returned\n");
6746035553Spatrick        ::abort();
6846035553Spatrick#ifndef _LIBCPP_NO_EXCEPTIONS
6946035553Spatrick    }
7046035553Spatrick    catch (...)
7146035553Spatrick    {
7246035553Spatrick        // handler should not throw exception
7346035553Spatrick        fprintf(stderr, "terminate_handler unexpectedly threw an exception\n");
7446035553Spatrick        ::abort();
7546035553Spatrick    }
7646035553Spatrick#endif // _LIBCPP_NO_EXCEPTIONS
7746035553Spatrick}
7846035553Spatrick
79*76d0caaeSpatrickbool uncaught_exception() noexcept { return uncaught_exceptions() > 0; }
8046035553Spatrick
81*76d0caaeSpatrickint uncaught_exceptions() noexcept {
8246035553Spatrick    return __uncaught_exceptions();
8346035553Spatrick}
8446035553Spatrick
8546035553Spatrick#if !defined(_LIBCPP_ABI_VCRUNTIME)
86*76d0caaeSpatrickbad_cast::bad_cast() noexcept
8746035553Spatrick{
8846035553Spatrick}
8946035553Spatrick
90*76d0caaeSpatrickbad_cast::~bad_cast() noexcept
9146035553Spatrick{
9246035553Spatrick}
9346035553Spatrick
9446035553Spatrickconst char *
95*76d0caaeSpatrickbad_cast::what() const noexcept
9646035553Spatrick{
9746035553Spatrick  return "std::bad_cast";
9846035553Spatrick}
9946035553Spatrick
100*76d0caaeSpatrickbad_typeid::bad_typeid() noexcept
10146035553Spatrick{
10246035553Spatrick}
10346035553Spatrick
104*76d0caaeSpatrickbad_typeid::~bad_typeid() noexcept
10546035553Spatrick{
10646035553Spatrick}
10746035553Spatrick
10846035553Spatrickconst char *
109*76d0caaeSpatrickbad_typeid::what() const noexcept
11046035553Spatrick{
11146035553Spatrick  return "std::bad_typeid";
11246035553Spatrick}
11346035553Spatrick
114*76d0caaeSpatrickexception::~exception() noexcept
11546035553Spatrick{
11646035553Spatrick}
11746035553Spatrick
118*76d0caaeSpatrickconst char* exception::what() const noexcept
11946035553Spatrick{
12046035553Spatrick  return "std::exception";
12146035553Spatrick}
12246035553Spatrick
12346035553Spatrick
124*76d0caaeSpatrickbad_exception::~bad_exception() noexcept
12546035553Spatrick{
12646035553Spatrick}
12746035553Spatrick
128*76d0caaeSpatrickconst char* bad_exception::what() const noexcept
12946035553Spatrick{
13046035553Spatrick  return "std::bad_exception";
13146035553Spatrick}
13246035553Spatrick
13346035553Spatrick
134*76d0caaeSpatrickbad_alloc::bad_alloc() noexcept
13546035553Spatrick{
13646035553Spatrick}
13746035553Spatrick
138*76d0caaeSpatrickbad_alloc::~bad_alloc() noexcept
13946035553Spatrick{
14046035553Spatrick}
14146035553Spatrick
14246035553Spatrickconst char*
143*76d0caaeSpatrickbad_alloc::what() const noexcept
14446035553Spatrick{
14546035553Spatrick    return "std::bad_alloc";
14646035553Spatrick}
14746035553Spatrick
148*76d0caaeSpatrickbad_array_new_length::bad_array_new_length() noexcept
14946035553Spatrick{
15046035553Spatrick}
15146035553Spatrick
152*76d0caaeSpatrickbad_array_new_length::~bad_array_new_length() noexcept
15346035553Spatrick{
15446035553Spatrick}
15546035553Spatrick
15646035553Spatrickconst char*
157*76d0caaeSpatrickbad_array_new_length::what() const noexcept
15846035553Spatrick{
15946035553Spatrick    return "bad_array_new_length";
16046035553Spatrick}
16146035553Spatrick#endif // !_LIBCPP_ABI_VCRUNTIME
16246035553Spatrick
16346035553Spatrick} // namespace std
164