demangle.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #ifndef BOOST_LEAF_DETAIL_DEMANGLE_HPP_INCLUDED
  2. #define BOOST_LEAF_DETAIL_DEMANGLE_HPP_INCLUDED
  3. /// Copyright (c) 2018-2021 Emil Dotchevski and Reverge Studios, Inc.
  4. /// Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. // core::demangle
  7. //
  8. // Copyright 2014 Peter Dimov
  9. // Copyright 2014 Andrey Semashev
  10. //
  11. // Distributed under the Boost Software License, Version 1.0.
  12. // See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt
  14. #include <string>
  15. #ifndef BOOST_LEAF_ENABLE_WARNINGS ///
  16. # if defined(_MSC_VER) ///
  17. # pragma warning(push,1) ///
  18. # elif defined(__clang__) ///
  19. # pragma clang system_header ///
  20. # elif (__GNUC__*100+__GNUC_MINOR__>301) ///
  21. # pragma GCC system_header ///
  22. # endif ///
  23. #endif ///
  24. #if !defined(_MSC_VER)
  25. # if defined(__has_include) && __has_include(<cxxabi.h>)
  26. # define BOOST_LEAF_HAS_CXXABI_H
  27. # endif
  28. #endif
  29. #if defined( BOOST_LEAF_HAS_CXXABI_H )
  30. # include <cxxabi.h>
  31. // For some architectures (mips, mips64, x86, x86_64) cxxabi.h in Android NDK is implemented by gabi++ library
  32. // (https://android.googlesource.com/platform/ndk/+/master/sources/cxx-stl/gabi++/), which does not implement
  33. // abi::__cxa_demangle(). We detect this implementation by checking the include guard here.
  34. # if defined( __GABIXX_CXXABI_H__ )
  35. # undef BOOST_LEAF_HAS_CXXABI_H
  36. # else
  37. # include <cstddef>
  38. # endif
  39. #endif
  40. namespace boost { namespace leaf {
  41. namespace leaf_detail
  42. {
  43. inline char const * demangle_alloc( char const * name ) noexcept;
  44. inline void demangle_free( char const * name ) noexcept;
  45. class scoped_demangled_name
  46. {
  47. private:
  48. char const * m_p;
  49. public:
  50. explicit scoped_demangled_name( char const * name ) noexcept :
  51. m_p( demangle_alloc( name ) )
  52. {
  53. }
  54. ~scoped_demangled_name() noexcept
  55. {
  56. demangle_free( m_p );
  57. }
  58. char const * get() const noexcept
  59. {
  60. return m_p;
  61. }
  62. scoped_demangled_name( scoped_demangled_name const& ) = delete;
  63. scoped_demangled_name& operator= ( scoped_demangled_name const& ) = delete;
  64. };
  65. #if defined( BOOST_LEAF_HAS_CXXABI_H )
  66. inline char const * demangle_alloc( char const * name ) noexcept
  67. {
  68. int status = 0;
  69. std::size_t size = 0;
  70. return abi::__cxa_demangle( name, NULL, &size, &status );
  71. }
  72. inline void demangle_free( char const * name ) noexcept
  73. {
  74. std::free( const_cast< char* >( name ) );
  75. }
  76. inline std::string demangle( char const * name )
  77. {
  78. scoped_demangled_name demangled_name( name );
  79. char const * p = demangled_name.get();
  80. if( !p )
  81. p = name;
  82. return p;
  83. }
  84. #else
  85. inline char const * demangle_alloc( char const * name ) noexcept
  86. {
  87. return name;
  88. }
  89. inline void demangle_free( char const * ) noexcept
  90. {
  91. }
  92. inline char const * demangle( char const * name )
  93. {
  94. return name;
  95. }
  96. #endif
  97. }
  98. } }
  99. #ifdef BOOST_LEAF_HAS_CXXABI_H
  100. # undef BOOST_LEAF_HAS_CXXABI_H
  101. #endif
  102. #if defined(_MSC_VER) && !defined(BOOST_LEAF_ENABLE_WARNINGS) ///
  103. #pragma warning(pop) ///
  104. #endif ///
  105. #endif