uncaught_exceptions.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_exception` 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. #if !defined(__FreeBSD__) && \
  53. ( \
  54. (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) < 407) || \
  55. defined(__OpenBSD__) || \
  56. defined(_LIBCPPABI_VERSION) \
  57. )
  58. namespace __cxxabiv1 {
  59. struct __cxa_eh_globals;
  60. #if defined(__OpenBSD__)
  61. extern "C" __cxa_eh_globals* __cxa_get_globals();
  62. #else
  63. extern "C" __cxa_eh_globals* __cxa_get_globals() BOOST_NOEXCEPT_OR_NOTHROW __attribute__((__const__));
  64. #endif
  65. } // namespace __cxxabiv1
  66. #endif
  67. #endif // !(defined(__MINGW32__) && (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) < 405))
  68. #endif // defined(BOOST_CORE_HAS_CXXABI_H)
  69. #if defined(_MSC_VER) && _MSC_VER >= 1400
  70. #include <cstring>
  71. #define BOOST_CORE_HAS_GETPTD
  72. namespace boost {
  73. namespace core {
  74. namespace detail {
  75. extern "C" void* _getptd();
  76. } // namespace detail
  77. } // namespace core
  78. } // namespace boost
  79. #endif // defined(_MSC_VER) && _MSC_VER >= 1400
  80. #endif // !defined(BOOST_CORE_HAS_UNCAUGHT_EXCEPTIONS)
  81. #if !defined(BOOST_CORE_HAS_UNCAUGHT_EXCEPTIONS) && !defined(BOOST_CORE_HAS_CXA_GET_GLOBALS) && !defined(BOOST_CORE_HAS_GETPTD)
  82. //! This macro is defined when `uncaught_exceptions` is not guaranteed to return values greater than 1 if multiple exceptions are pending
  83. #define BOOST_CORE_UNCAUGHT_EXCEPTIONS_EMULATED
  84. #endif
  85. namespace boost {
  86. namespace core {
  87. //! Returns the number of currently pending exceptions
  88. inline unsigned int uncaught_exceptions() BOOST_NOEXCEPT
  89. {
  90. #if defined(BOOST_CORE_HAS_UNCAUGHT_EXCEPTIONS)
  91. // C++17 implementation
  92. return static_cast< unsigned int >(std::uncaught_exceptions());
  93. #elif defined(BOOST_CORE_HAS_CXA_GET_GLOBALS)
  94. // Tested on {clang 3.2,GCC 3.5.6,GCC 4.1.2,GCC 4.4.6,GCC 4.4.7}x{x32,x64}
  95. unsigned int count;
  96. 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
  97. return count;
  98. #elif defined(BOOST_CORE_HAS_GETPTD)
  99. // MSVC specific. Tested on {MSVC2005SP1,MSVC2008SP1,MSVC2010SP1,MSVC2012}x{x32,x64}.
  100. unsigned int count;
  101. 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
  102. return count;
  103. #else
  104. // Portable C++03 implementation. Does not allow to detect multiple nested exceptions.
  105. return static_cast< unsigned int >(std::uncaught_exception());
  106. #endif
  107. }
  108. } // namespace core
  109. } // namespace boost
  110. #undef BOOST_CORE_HAS_CXXABI_H
  111. #undef BOOST_CORE_HAS_CXA_GET_GLOBALS
  112. #undef BOOST_CORE_HAS_UNCAUGHT_EXCEPTIONS
  113. #undef BOOST_CORE_HAS_GETPTD
  114. #endif // BOOST_CORE_UNCAUGHT_EXCEPTIONS_HPP_INCLUDED_