From 13d1a807ad9797f5e0ff8f2e8d3b88ff80f94467 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Fri, 6 Jun 2014 15:49:29 +0300 Subject: [PATCH] Add core::demangled_name. --- include/boost/core/demangled_name.hpp | 103 ++++++++++++++++++ include/boost/core/lightweight_test_trait.hpp | 3 +- test/Jamfile.v2 | 2 + test/demangled_name_test.cpp | 24 ++++ 4 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 include/boost/core/demangled_name.hpp create mode 100644 test/demangled_name_test.cpp diff --git a/include/boost/core/demangled_name.hpp b/include/boost/core/demangled_name.hpp new file mode 100644 index 0000000..c2de377 --- /dev/null +++ b/include/boost/core/demangled_name.hpp @@ -0,0 +1,103 @@ +#ifndef BOOST_CORE_DEMANGLED_NAME_HPP_INCLUDED +#define BOOST_CORE_DEMANGLED_NAME_HPP_INCLUDED + +// MS compatible compilers support #pragma once + +#if defined(_MSC_VER) && (_MSC_VER >= 1020) +# pragma once +#endif + +// core::demangled_name( BOOST_CORE_TYPEID(T) ) +// +// Copyright 2014 Peter Dimov +// +// 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 +#include +#include + +#if defined(__GLIBCXX__) || defined(__GLIBCPP__) +# include +# include +# include +# define BOOST_CORE_HAS_CXXABI_H +#endif + +namespace boost +{ + +namespace core +{ + +namespace detail +{ + +#if defined( BOOST_CORE_HAS_CXXABI_H ) + +// lifted from boost/exception/detail/type_info.hpp + +inline std::string demangle( char const * name ) +{ + struct auto_free + { + explicit auto_free( char * ptr ): p( ptr ) + { + } + + ~auto_free() + { + std::free( p ); + } + + char * p; + }; + + int status = 0; + std::size_t size = 0; + + auto_free demangled( abi::__cxa_demangle( name, NULL, &size, &status ) ); + + if( demangled.p ) + { + return demangled.p; + } + else + { + return name; + } +} + +#else + +inline std::string demangle( char const * name ) +{ + return name; +} + +#endif + +} // namespace detail + +inline std::string demangled_name( core::typeinfo const & ti ) +{ +#if defined( BOOST_NO_TYPEID ) + + return ti.name(); + +#else + + return core::detail::demangle( ti.name() ); + +#endif +} + +} // namespace core + +} // namespace boost + +#undef BOOST_CORE_HAS_CXXABI_H + +#endif // #ifndef BOOST_CORE_DEMANGLED_NAME_HPP_INCLUDED diff --git a/include/boost/core/lightweight_test_trait.hpp b/include/boost/core/lightweight_test_trait.hpp index 7eb4936..d8e756c 100644 --- a/include/boost/core/lightweight_test_trait.hpp +++ b/include/boost/core/lightweight_test_trait.hpp @@ -19,6 +19,7 @@ #include #include +#include namespace boost { @@ -36,7 +37,7 @@ template< class T > inline void test_trait_impl( char const * trait, void (*)( T { BOOST_LIGHTWEIGHT_TEST_OSTREAM << file << "(" << line << "): predicate '" << trait << "' [" - << BOOST_CORE_TYPEID(T).name() << "]" + << boost::core::demangled_name( BOOST_CORE_TYPEID(T) ) << "]" << " test failed in function '" << function << "' (should have been " << ( expected? "true": "false" ) << ")" << std::endl; diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 8b87ea0..a1fdd4a 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -68,3 +68,5 @@ run typeinfo_test.cpp : : : off : typeinfo_test_no_rtti ; run iterator_test.cpp ; run detail_iterator_test.cpp ; + +run demangled_name_test.cpp : : : always_show_run_output ; diff --git a/test/demangled_name_test.cpp b/test/demangled_name_test.cpp new file mode 100644 index 0000000..06da694 --- /dev/null +++ b/test/demangled_name_test.cpp @@ -0,0 +1,24 @@ +// +// Trivial test for core::demangled_name +// +// Copyright (c) 2014 Peter Dimov +// +// 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 +#include +#include + +template struct Y1 +{ +}; + +int main() +{ + typedef Y1 T; + std::cout << boost::core::demangled_name( BOOST_CORE_TYPEID( T ) ); + return 0; +}