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#ifndef _LIBCPP_ABI_MICROSOFT 11#error this header can only be used when targeting the MSVC ABI 12#endif 13 14#include <stdio.h> 15#include <stdlib.h> 16 17extern "C" { 18typedef void (__cdecl* terminate_handler)(); 19_LIBCPP_CRT_FUNC terminate_handler __cdecl set_terminate( 20 terminate_handler _NewTerminateHandler) throw(); 21_LIBCPP_CRT_FUNC terminate_handler __cdecl _get_terminate(); 22 23typedef void (__cdecl* unexpected_handler)(); 24unexpected_handler __cdecl set_unexpected( 25 unexpected_handler _NewUnexpectedHandler) throw(); 26unexpected_handler __cdecl _get_unexpected(); 27 28int __cdecl __uncaught_exceptions(); 29} 30 31namespace std { 32 33unexpected_handler 34set_unexpected(unexpected_handler func) noexcept { 35 return ::set_unexpected(func); 36} 37 38unexpected_handler get_unexpected() noexcept { 39 return ::_get_unexpected(); 40} 41 42_LIBCPP_NORETURN 43void unexpected() { 44 (*get_unexpected())(); 45 // unexpected handler should not return 46 terminate(); 47} 48 49terminate_handler set_terminate(terminate_handler func) noexcept { 50 return ::set_terminate(func); 51} 52 53terminate_handler get_terminate() noexcept { 54 return ::_get_terminate(); 55} 56 57_LIBCPP_NORETURN 58void terminate() noexcept 59{ 60#ifndef _LIBCPP_NO_EXCEPTIONS 61 try 62 { 63#endif // _LIBCPP_NO_EXCEPTIONS 64 (*get_terminate())(); 65 // handler should not return 66 fprintf(stderr, "terminate_handler unexpectedly returned\n"); 67 ::abort(); 68#ifndef _LIBCPP_NO_EXCEPTIONS 69 } 70 catch (...) 71 { 72 // handler should not throw exception 73 fprintf(stderr, "terminate_handler unexpectedly threw an exception\n"); 74 ::abort(); 75 } 76#endif // _LIBCPP_NO_EXCEPTIONS 77} 78 79bool uncaught_exception() noexcept { return uncaught_exceptions() > 0; } 80 81int uncaught_exceptions() noexcept { 82 return __uncaught_exceptions(); 83} 84 85#if !defined(_LIBCPP_ABI_VCRUNTIME) 86bad_cast::bad_cast() noexcept 87{ 88} 89 90bad_cast::~bad_cast() noexcept 91{ 92} 93 94const char * 95bad_cast::what() const noexcept 96{ 97 return "std::bad_cast"; 98} 99 100bad_typeid::bad_typeid() noexcept 101{ 102} 103 104bad_typeid::~bad_typeid() noexcept 105{ 106} 107 108const char * 109bad_typeid::what() const noexcept 110{ 111 return "std::bad_typeid"; 112} 113 114exception::~exception() noexcept 115{ 116} 117 118const char* exception::what() const noexcept 119{ 120 return "std::exception"; 121} 122 123 124bad_exception::~bad_exception() noexcept 125{ 126} 127 128const char* bad_exception::what() const noexcept 129{ 130 return "std::bad_exception"; 131} 132 133 134bad_alloc::bad_alloc() noexcept 135{ 136} 137 138bad_alloc::~bad_alloc() noexcept 139{ 140} 141 142const char* 143bad_alloc::what() const noexcept 144{ 145 return "std::bad_alloc"; 146} 147 148bad_array_new_length::bad_array_new_length() noexcept 149{ 150} 151 152bad_array_new_length::~bad_array_new_length() noexcept 153{ 154} 155 156const char* 157bad_array_new_length::what() const noexcept 158{ 159 return "bad_array_new_length"; 160} 161#endif // !_LIBCPP_ABI_VCRUNTIME 162 163} // namespace std 164