1#ifndef BOOST_CORE_DEMANGLE_HPP_INCLUDED
2#define BOOST_CORE_DEMANGLE_HPP_INCLUDED
3
4// core::demangle
5//
6// Copyright 2014 Peter Dimov
7// Copyright 2014 Andrey Semashev
8//
9// Distributed under the Boost Software License, Version 1.0.
10// See accompanying file LICENSE_1_0.txt or copy at
11// http://www.boost.org/LICENSE_1_0.txt
12
13#include <boost/config.hpp>
14#include <string>
15
16#if defined(BOOST_HAS_PRAGMA_ONCE)
17# pragma once
18#endif
19
20#if defined( __clang__ ) && defined( __has_include )
21# if __has_include(<cxxabi.h>)
22# define BOOST_CORE_HAS_CXXABI_H
23# endif
24#elif defined( __GLIBCXX__ ) || defined( __GLIBCPP__ )
25# define BOOST_CORE_HAS_CXXABI_H
26#endif
27
28#if defined( BOOST_CORE_HAS_CXXABI_H )
29# include <cxxabi.h>
30// For some archtectures (mips, mips64, x86, x86_64) cxxabi.h in Android NDK is implemented by gabi++ library
31// (https://android.googlesource.com/platform/ndk/+/master/sources/cxx-stl/gabi++/), which does not implement
32// abi::__cxa_demangle(). We detect this implementation by checking the include guard here.
33# if defined( __GABIXX_CXXABI_H__ )
34# undef BOOST_CORE_HAS_CXXABI_H
35# else
36# include <cstdlib>
37# include <cstddef>
38# endif
39#endif
40
41namespace boost
42{
43
44namespace core
45{
46
47inline char const * demangle_alloc( char const * name ) BOOST_NOEXCEPT;
48inline void demangle_free( char const * name ) BOOST_NOEXCEPT;
49
50class scoped_demangled_name
51{
52private:
53 char const * m_p;
54
55public:
56 explicit scoped_demangled_name( char const * name ) BOOST_NOEXCEPT :
57 m_p( demangle_alloc( name ) )
58 {
59 }
60
61 ~scoped_demangled_name() BOOST_NOEXCEPT
62 {
63 demangle_free( name: m_p );
64 }
65
66 char const * get() const BOOST_NOEXCEPT
67 {
68 return m_p;
69 }
70
71 BOOST_DELETED_FUNCTION(scoped_demangled_name( scoped_demangled_name const& ))
72 BOOST_DELETED_FUNCTION(scoped_demangled_name& operator= ( scoped_demangled_name const& ))
73};
74
75
76#if defined( BOOST_CORE_HAS_CXXABI_H )
77
78inline char const * demangle_alloc( char const * name ) BOOST_NOEXCEPT
79{
80 int status = 0;
81 std::size_t size = 0;
82 return abi::__cxa_demangle( mangled_name: name, NULL, length: &size, status: &status );
83}
84
85inline void demangle_free( char const * name ) BOOST_NOEXCEPT
86{
87 std::free( ptr: const_cast< char* >( name ) );
88}
89
90inline std::string demangle( char const * name )
91{
92 scoped_demangled_name demangled_name( name );
93 char const * const p = demangled_name.get();
94 if( p )
95 {
96 return p;
97 }
98 else
99 {
100 return name;
101 }
102}
103
104#else
105
106inline char const * demangle_alloc( char const * name ) BOOST_NOEXCEPT
107{
108 return name;
109}
110
111inline void demangle_free( char const * ) BOOST_NOEXCEPT
112{
113}
114
115inline std::string demangle( char const * name )
116{
117 return name;
118}
119
120#endif
121
122} // namespace core
123
124} // namespace boost
125
126#undef BOOST_CORE_HAS_CXXABI_H
127
128#endif // #ifndef BOOST_CORE_DEMANGLE_HPP_INCLUDED
129

source code of boost/boost/core/demangle.hpp