]>
Commit | Line | Data |
---|---|---|
20effc67 TL |
1 | #ifndef BOOST_LEAF_DETAIL_DEMANGLE_HPP_INCLUDED |
2 | #define BOOST_LEAF_DETAIL_DEMANGLE_HPP_INCLUDED | |
3 | ||
1e59de90 | 4 | // Copyright 2018-2022 Emil Dotchevski and Reverge Studios, Inc. |
20effc67 TL |
5 | |
6 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
7 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
8 | ||
1e59de90 | 9 | // This file is based on boost::core::demangle |
20effc67 TL |
10 | // |
11 | // Copyright 2014 Peter Dimov | |
12 | // Copyright 2014 Andrey Semashev | |
13 | // | |
14 | // Distributed under the Boost Software License, Version 1.0. | |
15 | // See accompanying file LICENSE_1_0.txt or copy at | |
16 | // http://www.boost.org/LICENSE_1_0.txt | |
17 | ||
1e59de90 TL |
18 | #include <boost/leaf/config.hpp> |
19 | ||
20 | #include <cstring> | |
21 | ||
22 | namespace boost { namespace leaf { | |
23 | ||
24 | namespace leaf_detail | |
25 | { | |
26 | template <int N> | |
27 | BOOST_LEAF_CONSTEXPR inline char const * check_prefix( char const * t, char const (&prefix)[N] ) | |
28 | { | |
29 | return std::strncmp(t,prefix,sizeof(prefix)-1)==0 ? t+sizeof(prefix)-1 : t; | |
30 | } | |
31 | } | |
32 | ||
33 | template <class Name> | |
34 | inline char const * type() | |
35 | { | |
36 | using leaf_detail::check_prefix; | |
37 | char const * t = | |
38 | #ifdef __FUNCSIG__ | |
39 | __FUNCSIG__; | |
40 | #else | |
41 | __PRETTY_FUNCTION__; | |
42 | #endif | |
43 | #if defined(__clang__) | |
44 | BOOST_LEAF_ASSERT(check_prefix(t,"const char *boost::leaf::type() ")==t+32); | |
45 | return t+32; | |
46 | #elif defined(__GNUC__) | |
47 | BOOST_LEAF_ASSERT(check_prefix(t,"const char* boost::leaf::type() ")==t+32); | |
48 | return t+32; | |
49 | #else | |
50 | char const * clang_style = check_prefix(t,"const char *boost::leaf::type() "); | |
51 | if( clang_style!=t ) | |
52 | return clang_style; | |
53 | char const * gcc_style = check_prefix(t,"const char* boost::leaf::type() "); | |
54 | if( gcc_style!=t ) | |
55 | return gcc_style; | |
20effc67 | 56 | #endif |
1e59de90 TL |
57 | return t; |
58 | } | |
20effc67 | 59 | |
1e59de90 TL |
60 | } } |
61 | ||
62 | //////////////////////////////////////// | |
63 | ||
64 | // __has_include is currently supported by GCC and Clang. However GCC 4.9 may have issues and | |
65 | // returns 1 for 'defined( __has_include )', while '__has_include' is actually not supported: | |
66 | // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63662 | |
67 | #if defined(__has_include) && (!defined(__GNUC__) || defined(__clang__) || (__GNUC__ + 0) >= 5) | |
68 | # if __has_include(<cxxabi.h>) | |
20effc67 TL |
69 | # define BOOST_LEAF_HAS_CXXABI_H |
70 | # endif | |
1e59de90 TL |
71 | #elif defined( __GLIBCXX__ ) || defined( __GLIBCPP__ ) |
72 | # define BOOST_LEAF_HAS_CXXABI_H | |
20effc67 TL |
73 | #endif |
74 | ||
75 | #if defined( BOOST_LEAF_HAS_CXXABI_H ) | |
76 | # include <cxxabi.h> | |
1e59de90 TL |
77 | // For some archtectures (mips, mips64, x86, x86_64) cxxabi.h in Android NDK is implemented by gabi++ library |
78 | // (https://android.googlesource.com/platform/ndk/+/master/sources/cxx-stl/gabi++/), which does not implement | |
79 | // abi::__cxa_demangle(). We detect this implementation by checking the include guard here. | |
20effc67 TL |
80 | # if defined( __GABIXX_CXXABI_H__ ) |
81 | # undef BOOST_LEAF_HAS_CXXABI_H | |
82 | # else | |
1e59de90 | 83 | # include <cstdlib> |
20effc67 TL |
84 | # include <cstddef> |
85 | # endif | |
86 | #endif | |
87 | ||
1e59de90 | 88 | #if BOOST_LEAF_CFG_STD_STRING |
20effc67 | 89 | |
1e59de90 | 90 | #include <string> |
20effc67 | 91 | |
1e59de90 | 92 | namespace boost { namespace leaf { |
20effc67 | 93 | |
1e59de90 TL |
94 | namespace leaf_detail |
95 | { | |
96 | inline char const * demangle_alloc( char const * name ) noexcept; | |
97 | inline void demangle_free( char const * name ) noexcept; | |
20effc67 | 98 | |
1e59de90 TL |
99 | class scoped_demangled_name |
100 | { | |
101 | private: | |
20effc67 | 102 | |
1e59de90 | 103 | char const * m_p; |
20effc67 | 104 | |
1e59de90 | 105 | public: |
20effc67 | 106 | |
1e59de90 TL |
107 | explicit scoped_demangled_name( char const * name ) noexcept : |
108 | m_p( demangle_alloc( name ) ) | |
20effc67 | 109 | { |
20effc67 TL |
110 | } |
111 | ||
1e59de90 | 112 | ~scoped_demangled_name() noexcept |
20effc67 | 113 | { |
1e59de90 | 114 | demangle_free( m_p ); |
20effc67 TL |
115 | } |
116 | ||
1e59de90 | 117 | char const * get() const noexcept |
20effc67 | 118 | { |
1e59de90 | 119 | return m_p; |
20effc67 TL |
120 | } |
121 | ||
1e59de90 TL |
122 | scoped_demangled_name( scoped_demangled_name const& ) = delete; |
123 | scoped_demangled_name& operator= ( scoped_demangled_name const& ) = delete; | |
124 | }; | |
125 | ||
126 | #ifdef BOOST_LEAF_HAS_CXXABI_H | |
127 | ||
128 | inline char const * demangle_alloc( char const * name ) noexcept | |
129 | { | |
130 | int status = 0; | |
131 | std::size_t size = 0; | |
132 | return abi::__cxa_demangle( name, NULL, &size, &status ); | |
133 | } | |
134 | ||
135 | inline void demangle_free( char const * name ) noexcept | |
136 | { | |
137 | std::free( const_cast< char* >( name ) ); | |
138 | } | |
139 | ||
140 | inline std::string demangle( char const * name ) | |
141 | { | |
142 | scoped_demangled_name demangled_name( name ); | |
143 | char const * p = demangled_name.get(); | |
144 | if( !p ) | |
145 | p = name; | |
146 | return p; | |
147 | } | |
148 | ||
20effc67 TL |
149 | #else |
150 | ||
1e59de90 TL |
151 | inline char const * demangle_alloc( char const * name ) noexcept |
152 | { | |
153 | return name; | |
154 | } | |
20effc67 | 155 | |
1e59de90 TL |
156 | inline void demangle_free( char const * ) noexcept |
157 | { | |
158 | } | |
20effc67 | 159 | |
1e59de90 TL |
160 | inline char const * demangle( char const * name ) |
161 | { | |
162 | return name; | |
163 | } | |
20effc67 TL |
164 | |
165 | #endif | |
1e59de90 TL |
166 | } |
167 | ||
168 | } } | |
169 | ||
170 | #else | |
171 | ||
172 | namespace boost { namespace leaf { | |
173 | ||
174 | namespace leaf_detail | |
175 | { | |
176 | inline char const * demangle( char const * name ) | |
177 | { | |
178 | return name; | |
20effc67 | 179 | } |
1e59de90 | 180 | } |
20effc67 TL |
181 | |
182 | } } | |
183 | ||
1e59de90 TL |
184 | #endif |
185 | ||
20effc67 TL |
186 | #ifdef BOOST_LEAF_HAS_CXXABI_H |
187 | # undef BOOST_LEAF_HAS_CXXABI_H | |
188 | #endif | |
189 | ||
190 | #endif |