Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:35:15

0001 /*
0002  *          Copyright Andrey Semashev 2018 - 2020.
0003  * Distributed under the Boost Software License, Version 1.0.
0004  *    (See accompanying file LICENSE_1_0.txt or copy at
0005  *          https://www.boost.org/LICENSE_1_0.txt)
0006  */
0007 /*!
0008  * \file   uncaught_exceptions.hpp
0009  * \author Andrey Semashev
0010  * \date   2018-11-10
0011  *
0012  * \brief  This header provides an `uncaught_exceptions` function implementation, which was introduced in C++17.
0013  *
0014  * The code in this file is based on the implementation by Evgeny Panasyuk:
0015  *
0016  * https://github.com/panaseleus/stack_unwinding/blob/master/boost/exception/uncaught_exception_count.hpp
0017  */
0018 
0019 #ifndef BOOST_CORE_UNCAUGHT_EXCEPTIONS_HPP_INCLUDED_
0020 #define BOOST_CORE_UNCAUGHT_EXCEPTIONS_HPP_INCLUDED_
0021 
0022 #include <exception>
0023 #include <boost/config.hpp>
0024 
0025 #if defined(BOOST_HAS_PRAGMA_ONCE)
0026 #pragma once
0027 #endif
0028 
0029 #if (defined(__cpp_lib_uncaught_exceptions) && __cpp_lib_uncaught_exceptions >= 201411)
0030 #if defined(__APPLE__)
0031 #include <Availability.h>
0032 // Apple systems only support std::uncaught_exceptions starting with specific versions:
0033 // - Mac OS >= 10.12
0034 // - iOS >= 10.0
0035 // - tvOS >= 10.0
0036 // - watchOS >= 3.0
0037 // https://github.com/boostorg/core/issues/80
0038 #if (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200) || \
0039     (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 100000)
0040 #define BOOST_CORE_HAS_UNCAUGHT_EXCEPTIONS
0041 #endif
0042 #else
0043 #define BOOST_CORE_HAS_UNCAUGHT_EXCEPTIONS
0044 #endif // defined(__APPLE__)
0045 // Visual Studio 14.0 supports N4152 std::uncaught_exceptions() but doesn't define __cpp_lib_uncaught_exceptions
0046 #elif (defined(_MSC_VER) && _MSC_VER >= 1900)
0047 #define BOOST_CORE_HAS_UNCAUGHT_EXCEPTIONS
0048 #endif
0049 
0050 #if !defined(BOOST_CORE_HAS_UNCAUGHT_EXCEPTIONS)
0051 
0052 // cxxabi.h availability macro
0053 #if defined(__has_include) && (!defined(BOOST_GCC) || (__GNUC__ >= 5))
0054 #   if __has_include(<cxxabi.h>)
0055 #       define BOOST_CORE_HAS_CXXABI_H
0056 #   endif
0057 #elif defined(__GLIBCXX__) || defined(__GLIBCPP__)
0058 #   define BOOST_CORE_HAS_CXXABI_H
0059 #endif
0060 
0061 #if defined(BOOST_CORE_HAS_CXXABI_H)
0062 // MinGW GCC 4.4 seem to not work the same way the newer GCC versions do. As a result, __cxa_get_globals based implementation will always return 0.
0063 // Just disable it for now and fall back to std::uncaught_exception().
0064 // On AIX, xlclang++ does have cxxabi.h but doesn't have __cxa_get_globals (https://github.com/boostorg/core/issues/78).
0065 #if !( \
0066         (defined(__MINGW32__) && (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) < 405)) || \
0067         defined(__ibmxl__) \
0068     )
0069 #include <cxxabi.h>
0070 #include <cstring>
0071 #define BOOST_CORE_HAS_CXA_GET_GLOBALS
0072 // At least on MinGW and Linux, only GCC since 4.7 declares __cxa_get_globals() in cxxabi.h. Older versions of GCC do not expose this function but it's there.
0073 // On OpenBSD, it seems, the declaration is also missing.
0074 // Note that at least on FreeBSD 11, cxxabi.h declares __cxa_get_globals with a different exception specification, so we can't declare the function unconditionally.
0075 // On Linux with clang and libc++ and on OS X, there is a version of cxxabi.h from libc++abi that doesn't declare __cxa_get_globals, but provides __cxa_uncaught_exceptions.
0076 // The function only appeared in version _LIBCPPABI_VERSION >= 1002 of the library. Unfortunately, there are linking errors about undefined reference to __cxa_uncaught_exceptions
0077 // on Ubuntu Trusty and OS X, so we avoid using it and forward-declare __cxa_get_globals instead.
0078 // On QNX SDP 7.0 (QCC 5.4.0), there are multiple cxxabi.h, one from glibcxx from gcc and another from libc++abi from LLVM. Which one is included will be determined by the qcc
0079 // command line arguments (-V and/or -Y; http://www.qnx.com/developers/docs/7.0.0/#com.qnx.doc.neutrino.utilities/topic/q/qcc.html). The LLVM libc++abi is missing the declaration
0080 // of __cxa_get_globals but it is also patched by QNX developers to not define _LIBCPPABI_VERSION. Older QNX SDP versions, up to and including 6.6, don't provide LLVM and libc++abi.
0081 // See https://github.com/boostorg/core/issues/59.
0082 #if !defined(__FreeBSD__) && \
0083     ( \
0084         (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) < 407) || \
0085         defined(__OpenBSD__) || \
0086         (defined(__QNXNTO__) && !defined(__GLIBCXX__) && !defined(__GLIBCPP__)) || \
0087         defined(_LIBCPPABI_VERSION) \
0088     )
0089 namespace __cxxabiv1 {
0090 struct __cxa_eh_globals;
0091 #if defined(__OpenBSD__)
0092 extern "C" __cxa_eh_globals* __cxa_get_globals();
0093 #else
0094 extern "C" __cxa_eh_globals* __cxa_get_globals() BOOST_NOEXCEPT_OR_NOTHROW __attribute__((__const__));
0095 #endif
0096 } // namespace __cxxabiv1
0097 #endif
0098 #endif
0099 #endif // defined(BOOST_CORE_HAS_CXXABI_H)
0100 
0101 #if defined(_MSC_VER) && _MSC_VER >= 1400
0102 #include <cstring>
0103 #define BOOST_CORE_HAS_GETPTD
0104 namespace boost {
0105 namespace core {
0106 namespace detail {
0107 extern "C" void* _getptd();
0108 } // namespace detail
0109 } // namespace core
0110 } // namespace boost
0111 #endif // defined(_MSC_VER) && _MSC_VER >= 1400
0112 
0113 #endif // !defined(BOOST_CORE_HAS_UNCAUGHT_EXCEPTIONS)
0114 
0115 #if !defined(BOOST_CORE_HAS_UNCAUGHT_EXCEPTIONS) && !defined(BOOST_CORE_HAS_CXA_GET_GLOBALS) && !defined(BOOST_CORE_HAS_GETPTD)
0116 //! This macro is defined when `uncaught_exceptions` is not guaranteed to return values greater than 1 if multiple exceptions are pending
0117 #define BOOST_CORE_UNCAUGHT_EXCEPTIONS_EMULATED
0118 #endif
0119 
0120 namespace boost {
0121 
0122 namespace core {
0123 
0124 //! Returns the number of currently pending exceptions
0125 inline unsigned int uncaught_exceptions() BOOST_NOEXCEPT
0126 {
0127 #if defined(BOOST_CORE_HAS_UNCAUGHT_EXCEPTIONS)
0128     // C++17 implementation
0129     return static_cast< unsigned int >(std::uncaught_exceptions());
0130 #elif defined(BOOST_CORE_HAS_CXA_GET_GLOBALS)
0131     // Tested on {clang 3.2,GCC 3.5.6,GCC 4.1.2,GCC 4.4.6,GCC 4.4.7}x{x32,x64}
0132     unsigned int count;
0133     std::memcpy(&count, reinterpret_cast< const unsigned char* >(::abi::__cxa_get_globals()) + sizeof(void*), sizeof(count)); // __cxa_eh_globals::uncaughtExceptions, x32 offset - 0x4, x64 - 0x8
0134     return count;
0135 #elif defined(BOOST_CORE_HAS_GETPTD)
0136     // MSVC specific. Tested on {MSVC2005SP1,MSVC2008SP1,MSVC2010SP1,MSVC2012}x{x32,x64}.
0137     unsigned int count;
0138     std::memcpy(&count, static_cast< const unsigned char* >(boost::core::detail::_getptd()) + (sizeof(void*) == 8u ? 0x100 : 0x90), sizeof(count)); // _tiddata::_ProcessingThrow, x32 offset - 0x90, x64 - 0x100
0139     return count;
0140 #else
0141     // Portable C++03 implementation. Does not allow to detect multiple nested exceptions.
0142     return static_cast< unsigned int >(std::uncaught_exception());
0143 #endif
0144 }
0145 
0146 } // namespace core
0147 
0148 } // namespace boost
0149 
0150 #undef BOOST_CORE_HAS_CXXABI_H
0151 #undef BOOST_CORE_HAS_CXA_GET_GLOBALS
0152 #undef BOOST_CORE_HAS_UNCAUGHT_EXCEPTIONS
0153 #undef BOOST_CORE_HAS_GETPTD
0154 
0155 #endif // BOOST_CORE_UNCAUGHT_EXCEPTIONS_HPP_INCLUDED_