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