Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:39:11

0001 #ifndef BOOST_LEAF_DETAIL_DEMANGLE_HPP_INCLUDED
0002 #define BOOST_LEAF_DETAIL_DEMANGLE_HPP_INCLUDED
0003 
0004 // Copyright 2018-2023 Emil Dotchevski and Reverge Studios, Inc.
0005 
0006 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0007 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0008 
0009 // This file is based on boost::core::demangle
0010 //
0011 // Copyright 2014 Peter Dimov
0012 // Copyright 2014 Andrey Semashev
0013 //
0014 // Distributed under the Boost Software License, Version 1.0.
0015 // See accompanying file LICENSE_1_0.txt or copy at
0016 // http://www.boost.org/LICENSE_1_0.txt
0017 
0018 #include <boost/leaf/config.hpp>
0019 
0020 #include <cstring>
0021 
0022 namespace boost { namespace leaf {
0023 
0024 namespace leaf_detail
0025 {
0026     template <int N>
0027     BOOST_LEAF_CONSTEXPR inline char const * check_prefix( char const * t, char const (&prefix)[N] )
0028     {
0029         return std::strncmp(t,prefix,sizeof(prefix)-1)==0 ? t+sizeof(prefix)-1 : t;
0030     }
0031 }
0032 
0033 template <class Name>
0034 inline char const * type()
0035 {
0036     using leaf_detail::check_prefix;
0037 char const * t =
0038 #ifdef __FUNCSIG__
0039     __FUNCSIG__;
0040 #else
0041     __PRETTY_FUNCTION__;
0042 #endif
0043 #if defined(__clang__)
0044     BOOST_LEAF_ASSERT(check_prefix(t,"const char *boost::leaf::type() ")==t+32);
0045     return t+32;
0046 #elif defined(__GNUC__)
0047     BOOST_LEAF_ASSERT(check_prefix(t,"const char* boost::leaf::type() ")==t+32);
0048     return t+32;
0049 #else
0050     char const * clang_style = check_prefix(t,"const char *boost::leaf::type() ");
0051     if( clang_style!=t )
0052         return clang_style;
0053     char const * gcc_style = check_prefix(t,"const char* boost::leaf::type() ");
0054     if( gcc_style!=t )
0055         return gcc_style;
0056 #endif
0057     return t;
0058 }
0059 
0060 } }
0061 
0062 ////////////////////////////////////////
0063 
0064 // __has_include is currently supported by GCC and Clang. However GCC 4.9 may have issues and
0065 // returns 1 for 'defined( __has_include )', while '__has_include' is actually not supported:
0066 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63662
0067 #if defined(__has_include) && (!defined(__GNUC__) || defined(__clang__) || (__GNUC__ + 0) >= 5)
0068 #   if __has_include(<cxxabi.h>)
0069 #       define BOOST_LEAF_HAS_CXXABI_H
0070 #   endif
0071 #elif defined( __GLIBCXX__ ) || defined( __GLIBCPP__ )
0072 #   define BOOST_LEAF_HAS_CXXABI_H
0073 #endif
0074 
0075 #if defined( BOOST_LEAF_HAS_CXXABI_H )
0076 #   include <cxxabi.h>
0077 //  For some archtectures (mips, mips64, x86, x86_64) cxxabi.h in Android NDK is implemented by gabi++ library
0078 //  (https://android.googlesource.com/platform/ndk/+/master/sources/cxx-stl/gabi++/), which does not implement
0079 //  abi::__cxa_demangle(). We detect this implementation by checking the include guard here.
0080 #   if defined( __GABIXX_CXXABI_H__ )
0081 #       undef BOOST_LEAF_HAS_CXXABI_H
0082 #   else
0083 #       include <cstdlib>
0084 #       include <cstddef>
0085 #   endif
0086 #endif
0087 
0088 #if BOOST_LEAF_CFG_STD_STRING
0089 
0090 #include <string>
0091 
0092 namespace boost { namespace leaf {
0093 
0094 namespace leaf_detail
0095 {
0096     inline char const * demangle_alloc( char const * name ) noexcept;
0097     inline void demangle_free( char const * name ) noexcept;
0098 
0099     class scoped_demangled_name
0100     {
0101     private:
0102 
0103         char const * m_p;
0104 
0105     public:
0106 
0107         explicit scoped_demangled_name( char const * name ) noexcept :
0108             m_p( demangle_alloc( name ) )
0109         {
0110         }
0111 
0112         ~scoped_demangled_name() noexcept
0113         {
0114             demangle_free( m_p );
0115         }
0116 
0117         char const * get() const noexcept
0118         {
0119             return m_p;
0120         }
0121 
0122         scoped_demangled_name( scoped_demangled_name const& ) = delete;
0123         scoped_demangled_name& operator= ( scoped_demangled_name const& ) = delete;
0124     };
0125 
0126 #ifdef BOOST_LEAF_HAS_CXXABI_H
0127 
0128     inline char const * demangle_alloc( char const * name ) noexcept
0129     {
0130         int status = 0;
0131         std::size_t size = 0;
0132         return abi::__cxa_demangle( name, NULL, &size, &status );
0133     }
0134 
0135     inline void demangle_free( char const * name ) noexcept
0136     {
0137         std::free( const_cast< char* >( name ) );
0138     }
0139 
0140     inline std::string demangle( char const * name )
0141     {
0142         scoped_demangled_name demangled_name( name );
0143         char const * p = demangled_name.get();
0144         if( !p )
0145             p = name;
0146         return p;
0147     }
0148 
0149 #else
0150 
0151     inline char const * demangle_alloc( char const * name ) noexcept
0152     {
0153         return name;
0154     }
0155 
0156     inline void demangle_free( char const * ) noexcept
0157     {
0158     }
0159 
0160     inline char const * demangle( char const * name )
0161     {
0162         return name;
0163     }
0164 
0165 #endif
0166 }
0167 
0168 } }
0169 
0170 #else
0171 
0172 namespace boost { namespace leaf {
0173 
0174 namespace leaf_detail
0175 {
0176     inline char const * demangle( char const * name )
0177     {
0178         return name;
0179     }
0180 }
0181 
0182 } }
0183 
0184 #endif
0185 
0186 #ifdef BOOST_LEAF_HAS_CXXABI_H
0187 #   undef BOOST_LEAF_HAS_CXXABI_H
0188 #endif
0189 
0190 #endif