From 89102f0fa96c1d3b53bf289802167fa78baaa83d Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Tue, 2 Jun 2015 15:33:38 +0000 Subject: Implement uncaught_exceptions() using the newly added hooks in libc++abi, when available llvm-svn: 238846 --- libcxx/include/exception | 4 +- libcxx/src/exception.cpp | 16 +++++--- .../uncaught/uncaught_exceptions.pass.cpp | 45 ++++++++++++++++++++++ libcxx/www/cxx1z_status.html | 2 +- 4 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 libcxx/test/std/language.support/support.exception/uncaught/uncaught_exceptions.pass.cpp diff --git a/libcxx/include/exception b/libcxx/include/exception index ef2b969b742..39d251ec499 100644 --- a/libcxx/include/exception +++ b/libcxx/include/exception @@ -48,7 +48,8 @@ terminate_handler set_terminate(terminate_handler f ) noexcept; terminate_handler get_terminate() noexcept; [[noreturn]] void terminate() noexcept; -bool uncaught_exception() noexcept; +bool uncaught_exception() noexcept; +int uncaught_exceptions() noexcept; // C++17 typedef unspecified exception_ptr; @@ -115,6 +116,7 @@ _LIBCPP_FUNC_VIS terminate_handler get_terminate() _NOEXCEPT; _LIBCPP_NORETURN _LIBCPP_FUNC_VIS void terminate() _NOEXCEPT; _LIBCPP_FUNC_VIS bool uncaught_exception() _NOEXCEPT; +_LIBCPP_FUNC_VIS int uncaught_exceptions() _NOEXCEPT; class _LIBCPP_TYPE_VIS exception_ptr; diff --git a/libcxx/src/exception.cpp b/libcxx/src/exception.cpp index 98b7b441b52..a13a0b9b064 100644 --- a/libcxx/src/exception.cpp +++ b/libcxx/src/exception.cpp @@ -105,19 +105,25 @@ terminate() _NOEXCEPT #endif // !__EMSCRIPTEN__ #endif // !defined(LIBCXXRT) && !defined(_LIBCPPABI_VERSION) +bool uncaught_exception() _NOEXCEPT { return uncaught_exceptions() > 0; } + #if !defined(LIBCXXRT) && !defined(__GLIBCXX__) && !defined(__EMSCRIPTEN__) -bool uncaught_exception() _NOEXCEPT +int uncaught_exceptions() _NOEXCEPT { #if defined(__APPLE__) || defined(_LIBCPPABI_VERSION) - // on Darwin, there is a helper function so __cxa_get_globals is private - return __cxa_uncaught_exception(); + // on Darwin, there is a helper function so __cxa_get_globals is private +# if _LIBCPPABI_VERSION > 1101 + return __cxa_uncaught_exceptions(); +# else + return __cxa_uncaught_exception() ? 1 : 0; +# endif #else // __APPLE__ # if defined(_MSC_VER) && ! defined(__clang__) - _LIBCPP_WARNING("uncaught_exception not yet implemented") + _LIBCPP_WARNING("uncaught_exceptions not yet implemented") # else # warning uncaught_exception not yet implemented # endif - fprintf(stderr, "uncaught_exception not yet implemented\n"); + fprintf(stderr, "uncaught_exceptions not yet implemented\n"); ::abort(); #endif // __APPLE__ } diff --git a/libcxx/test/std/language.support/support.exception/uncaught/uncaught_exceptions.pass.cpp b/libcxx/test/std/language.support/support.exception/uncaught/uncaught_exceptions.pass.cpp new file mode 100644 index 00000000000..1adc904fd5d --- /dev/null +++ b/libcxx/test/std/language.support/support.exception/uncaught/uncaught_exceptions.pass.cpp @@ -0,0 +1,45 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// test uncaught_exceptions + +#include +#include + +struct A +{ + ~A() + { + assert(std::uncaught_exceptions() > 0); + } +}; + +struct B +{ + B() + { + // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#475 + assert(std::uncaught_exceptions() == 0); + } +}; + +int main() +{ + try + { + A a; + assert(std::uncaught_exceptions() == 0); + throw B(); + } + catch (...) + { + assert(std::uncaught_exception() == 0); + } + assert(std::uncaught_exceptions() == 0); +} diff --git a/libcxx/www/cxx1z_status.html b/libcxx/www/cxx1z_status.html index 4ad892e2780..92268925be0 100644 --- a/libcxx/www/cxx1z_status.html +++ b/libcxx/www/cxx1z_status.html @@ -57,7 +57,7 @@ N4169LWGA proposal to add invoke function templateUrbana N4190LWGRemoving auto_ptr, random_shuffle(), And Old Stuff.Urbana N4258LWGCleaning-up noexcept in the Library.Urbana - N4259CWGWording for std::uncaught_exceptionsUrbana + N4259CWGWording for std::uncaught_exceptionsUrbanaComplete3.7 N4277LWGTriviallyCopyable reference_wrapper.UrbanaComplete3.2 N4279LWGImproved insertion interface for unique-key maps.Urbana N4280LWGNon-member size() and moreUrbanaComplete3.6 -- cgit v1.2.1