uncaught_exceptions.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright Andrey Semashev 2018.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * https://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file uncaught_exceptions.hpp
  9. * \author Andrey Semashev
  10. * \date 2018-11-10
  11. *
  12. * \brief This header provides an `uncaught_exceptions` function implementation, which was introduced in C++17.
  13. *
  14. * The code in this file is based on the implementation by Evgeny Panasyuk:
  15. *
  16. * https://github.com/panaseleus/stack_unwinding/blob/master/boost/exception/uncaught_exception_count.hpp
  17. */
  18. #ifndef BOOST_CORE_UNCAUGHT_EXCEPTIONS_HPP_INCLUDED_
  19. #define BOOST_CORE_UNCAUGHT_EXCEPTIONS_HPP_INCLUDED_
  20. #include <exception>
  21. #include <boost/config.hpp>
  22. #if defined(BOOST_HAS_PRAGMA_ONCE)
  23. #pragma once
  24. #endif
  25. // Visual Studio 14 supports N4152 std::uncaught_exceptions()
  26. #if (defined(__cpp_lib_uncaught_exceptions) && __cpp_lib_uncaught_exceptions >= 201411) || \
  27. (defined(_MSC_VER) && _MSC_VER >= 1900)
  28. #define BOOST_CORE_HAS_UNCAUGHT_EXCEPTIONS
  29. #endif
  30. #if !defined(BOOST_CORE_HAS_UNCAUGHT_EXCEPTIONS)
  31. // cxxabi.h availability macro
  32. #if defined(__has_include) && (!defined(BOOST_GCC) || (__GNUC__ >= 5))
  33. # if __has_include(<cxxabi.h>)
  34. # define BOOST_CORE_HAS_CXXABI_H
  35. # endif
  36. #elif defined(__GLIBCXX__) || defined(__GLIBCPP__)
  37. # define BOOST_CORE_HAS_CXXABI_H
  38. #endif
  39. #if defined(BOOST_CORE_HAS_CXXABI_H)
  40. // 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.
  41. // Just disable it for now and fall back to std::uncaught_exception().
  42. #if !(defined(__MINGW32__) && (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) < 405))
  43. #include <cxxabi.h>
  44. #include <cstring>
  45. #define BOOST_CORE_HAS_CXA_GET_GLOBALS
  46. // 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.
  47. // On OpenBSD, it seems, the declaration is also missing.
  48. // 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.
  49. // 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.
  50. // The function only appeared in version _LIBCPPABI_VERSION >= 1002 of the library. Unfortunately, there are linking errors about undefined reference to __cxa_uncaught_exceptions
  51. // on Ubuntu Trusty and OS X, so we avoid using it and forward-declare __cxa_get_globals instead.
  52. // 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
  53. // 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
  54. // 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.
  55. // See https://github.com/boostorg/core/issues/59.
  56. #if !defined(__FreeBSD__) && \
  57. ( \
  58. (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) < 407) || \
  59. defined(__OpenBSD__) || \
  60. (defined(__QNXNTO__) && !defined(__GLIBCXX__) && !defined(__GLIBCPP__)) || \
  61. defined(_LIBCPPABI_VERSION) \
  62. )
  63. namespace __cxxabiv1 {
  64. struct __cxa_eh_globals;
  65. #if defined(__OpenBSD__)
  66. extern "C" __cxa_eh_globals* __cxa_get_globals();
  67. #else
  68. extern "C" __cxa_eh_globals* __cxa_get_globals() BOOST_NOEXCEPT_OR_NOTHROW __attribute__((__const__));
  69. #endif
  70. } // namespace __cxxabiv1
  71. #endif
  72. #endif // !(defined(__MINGW32__) && (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) < 405))
  73. #endif // defined(BOOST_CORE_HAS_CXXABI_H)
  74. #if defined(_MSC_VER) && _MSC_VER >= 1400
  75. #include <cstring>
  76. #define BOOST_CORE_HAS_GETPTD
  77. namespace boost {
  78. namespace core {
  79. namespace detail {
  80. extern "C" void* _getptd();
  81. } // namespace detail
  82. } // namespace core
  83. } // namespace boost
  84. #endif // defined(_MSC_VER) && _MSC_VER >= 1400
  85. #endif // !defined(BOOST_CORE_HAS_UNCAUGHT_EXCEPTIONS)
  86. #if !defined(BOOST_CORE_HAS_UNCAUGHT_EXCEPTIONS) && !defined(BOOST_CORE_HAS_CXA_GET_GLOBALS) && !defined(BOOST_CORE_HAS_GETPTD)
  87. //! This macro is defined when `uncaught_exceptions` is not guaranteed to return values greater than 1 if multiple exceptions are pending
  88. #define BOOST_CORE_UNCAUGHT_EXCEPTIONS_EMULATED
  89. #endif
  90. namespace boost {
  91. namespace core {
  92. //! Returns the number of currently pending exceptions
  93. inline unsigned int uncaught_exceptions() BOOST_NOEXCEPT
  94. {
  95. #if defined(BOOST_CORE_HAS_UNCAUGHT_EXCEPTIONS)
  96. // C++17 implementation
  97. return static_cast< unsigned int >(std::uncaught_exceptions());
  98. #elif defined(BOOST_CORE_HAS_CXA_GET_GLOBALS)
  99. // Tested on {clang 3.2,GCC 3.5.6,GCC 4.1.2,GCC 4.4.6,GCC 4.4.7}x{x32,x64}
  100. unsigned int count;
  101. 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
  102. return count;
  103. #elif defined(BOOST_CORE_HAS_GETPTD)
  104. // MSVC specific. Tested on {MSVC2005SP1,MSVC2008SP1,MSVC2010SP1,MSVC2012}x{x32,x64}.
  105. unsigned int count;
  106. 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
  107. return count;
  108. #else
  109. // Portable C++03 implementation. Does not allow to detect multiple nested exceptions.
  110. return static_cast< unsigned int >(std::uncaught_exception());
  111. #endif
  112. }
  113. } // namespace core
  114. } // namespace boost
  115. #undef BOOST_CORE_HAS_CXXABI_H
  116. #undef BOOST_CORE_HAS_CXA_GET_GLOBALS
  117. #undef BOOST_CORE_HAS_UNCAUGHT_EXCEPTIONS
  118. #undef BOOST_CORE_HAS_GETPTD
  119. #endif // BOOST_CORE_UNCAUGHT_EXCEPTIONS_HPP_INCLUDED_