Skip to content

Commit

Permalink
Backport demangle() from main
Browse files Browse the repository at this point in the history
  • Loading branch information
d-frey committed Feb 5, 2022
1 parent 558757e commit 442d518
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 81 deletions.
10 changes: 8 additions & 2 deletions doc/Changelog.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
# Changelog

## 3.2.5

Released 2022-02-05

* Added missing include for fallback `demangle()` implementations.

## 3.2.4

Released: 2022-02-03
Released 2022-02-03

* Fixed `version.hpp`.

## 3.2.3

Released: 2022-02-03
Released 2022-02-03

* Fixed `static_assert` in `demangle()` with recent MSVC.

Expand Down
187 changes: 110 additions & 77 deletions include/tao/pegtl/demangle.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2014-2022 Dr. Colin Hirsch and Daniel Frey
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/

#ifndef TAO_PEGTL_DEMANGLE_HPP
#define TAO_PEGTL_DEMANGLE_HPP
Expand All @@ -9,28 +9,43 @@

#include "config.hpp"

#include "internal/dependent_true.hpp"

// ensure a consistent interface
namespace TAO_PEGTL_NAMESPACE
{
#if !defined( __clang__ ) && defined( __GNUC__ ) && ( __GNUC__ == 7 )

template< typename T >
[[nodiscard]] std::string_view demangle() noexcept;

#else

template< typename T >
[[nodiscard]] constexpr std::string_view demangle() noexcept;

#endif

} // namespace TAO_PEGTL_NAMESPACE

#if defined( __clang__ )

#if defined( _LIBCPP_VERSION )

template< typename T >
[[nodiscard]] constexpr std::string_view demangle() noexcept
{
constexpr std::string_view sv = __PRETTY_FUNCTION__;
constexpr auto begin = sv.find( '=' );
static_assert( internal::dependent_true< T > && ( begin != std::string_view::npos ) );
return sv.substr( begin + 2, sv.size() - begin - 3 );
}
template< typename T >
[[nodiscard]] constexpr std::string_view TAO_PEGTL_NAMESPACE::demangle() noexcept
{
constexpr std::string_view sv = __PRETTY_FUNCTION__;
constexpr auto begin = sv.find( '=' );
static_assert( begin != std::string_view::npos );
return sv.substr( begin + 2, sv.size() - begin - 3 );
}

#else

namespace TAO_PEGTL_NAMESPACE::internal
{
// When using libstdc++ with clang, std::string_view::find is not constexpr :(
template< char C >
constexpr const char* find( const char* p, std::size_t n ) noexcept
constexpr const char* string_view_find( const char* p, std::size_t n ) noexcept
{
while( n ) {
if( *p == C ) {
Expand All @@ -42,101 +57,119 @@ namespace TAO_PEGTL_NAMESPACE
return nullptr;
}

template< typename T >
[[nodiscard]] constexpr std::string_view demangle() noexcept
{
constexpr std::string_view sv = __PRETTY_FUNCTION__;
constexpr auto begin = find< '=' >( sv.data(), sv.size() );
static_assert( internal::dependent_true< T > && ( begin != nullptr ) );
return { begin + 2, sv.data() + sv.size() - begin - 3 };
}
} // namespace TAO_PEGTL_NAMESPACE::internal

template< typename T >
[[nodiscard]] constexpr std::string_view TAO_PEGTL_NAMESPACE::demangle() noexcept
{
constexpr std::string_view sv = __PRETTY_FUNCTION__;
constexpr auto begin = internal::string_view_find< '=' >( sv.data(), sv.size() );
static_assert( begin != nullptr );
return { begin + 2, sv.data() + sv.size() - begin - 3 };
}

#endif

#elif defined( __GNUC__ )

#if( __GNUC__ == 7 )

// GCC 7 wrongly sometimes disallows __PRETTY_FUNCTION__ in constexpr functions,
// therefore we drop the 'constexpr' and hope for the best.
template< typename T >
[[nodiscard]] std::string_view demangle() noexcept
{
const std::string_view sv = __PRETTY_FUNCTION__;
const auto begin = sv.find( '=' );
const auto tmp = sv.substr( begin + 2 );
const auto end = tmp.rfind( ';' );
return tmp.substr( 0, end );
}
// GCC 7 wrongly sometimes disallows __PRETTY_FUNCTION__ in constexpr functions,
// therefore we drop the 'constexpr' and hope for the best.
template< typename T >
[[nodiscard]] std::string_view TAO_PEGTL_NAMESPACE::demangle() noexcept
{
const std::string_view sv = __PRETTY_FUNCTION__;
const auto begin = sv.find( '=' );
const auto tmp = sv.substr( begin + 2 );
const auto end = tmp.rfind( ';' );
return tmp.substr( 0, end );
}

#elif( __GNUC__ == 9 ) && ( __GNUC_MINOR__ < 3 )

// GCC 9.1 and 9.2 have a bug that leads to truncated __PRETTY_FUNCTION__ names,
// see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91155
template< typename T >
[[nodiscard]] constexpr std::string_view demangle() noexcept
{
// fallback: requires RTTI, no demangling
return typeid( T ).name();
}
#if !defined( __cpp_rtti )
#error "RTTI support required for GCC 9.1/9.2"
#else

#include <typeinfo>

// GCC 9.1 and 9.2 have a bug that leads to truncated __PRETTY_FUNCTION__ names,
// see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91155
template< typename T >
[[nodiscard]] constexpr std::string_view TAO_PEGTL_NAMESPACE::demangle() noexcept
{
// fallback: requires RTTI, no demangling
return typeid( T ).name();
}

#endif

#else

template< typename T >
[[nodiscard]] constexpr std::string_view demangle() noexcept
{
constexpr std::string_view sv = __PRETTY_FUNCTION__;
constexpr auto begin = sv.find( '=' );
static_assert( internal::dependent_true< T > && ( begin != std::string_view::npos ) );
constexpr auto tmp = sv.substr( begin + 2 );
constexpr auto end = tmp.rfind( ';' );
static_assert( internal::dependent_true< T > && ( end != std::string_view::npos ) );
return tmp.substr( 0, end );
}
template< typename T >
[[nodiscard]] constexpr std::string_view TAO_PEGTL_NAMESPACE::demangle() noexcept
{
constexpr std::string_view sv = __PRETTY_FUNCTION__;
constexpr auto begin = sv.find( '=' );
static_assert( begin != std::string_view::npos );
constexpr auto tmp = sv.substr( begin + 2 );
constexpr auto end = tmp.rfind( ';' );
static_assert( end != std::string_view::npos );
return tmp.substr( 0, end );
}

#endif

#elif defined( _MSC_VER )

#if( _MSC_VER < 1920 )

template< typename T >
[[nodiscard]] constexpr std::string_view demangle() noexcept
{
const std::string_view sv = __FUNCSIG__;
const auto begin = sv.find( "demangle<" );
const auto tmp = sv.substr( begin + 9 );
const auto end = tmp.rfind( '>' );
return tmp.substr( 0, end );
}
template< typename T >
[[nodiscard]] constexpr std::string_view TAO_PEGTL_NAMESPACE::demangle() noexcept
{
const std::string_view sv = __FUNCSIG__;
const auto begin = sv.find( "demangle<" );
const auto tmp = sv.substr( begin + 9 );
const auto end = tmp.rfind( '>' );
return tmp.substr( 0, end );
}

#else

template< typename T >
[[nodiscard]] constexpr std::string_view demangle() noexcept
{
constexpr std::string_view sv = __FUNCSIG__;
constexpr auto begin = sv.find( "demangle<" );
static_assert( internal::dependent_true< T > && ( begin != std::string_view::npos ) );
constexpr auto tmp = sv.substr( begin + 9 );
constexpr auto end = tmp.rfind( '>' );
static_assert( internal::dependent_true< T > && ( end != std::string_view::npos ) );
return tmp.substr( 0, end );
}
#include "internal/dependent_true.hpp"

template< typename T >
[[nodiscard]] constexpr std::string_view TAO_PEGTL_NAMESPACE::demangle() noexcept
{
constexpr std::string_view sv = __FUNCSIG__;
constexpr auto begin = sv.find( "demangle<" );
static_assert( internal::dependent_true< T > && ( begin != std::string_view::npos ) );
constexpr auto tmp = sv.substr( begin + 9 );
constexpr auto end = tmp.rfind( '>' );
static_assert( internal::dependent_true< T > && ( end != std::string_view::npos ) );
return tmp.substr( 0, end );
}

#endif

#else

template< typename T >
[[nodiscard]] constexpr std::string_view demangle() noexcept
{
// fallback: requires RTTI, no demangling
return typeid( T ).name();
}
#if !defined( __cpp_rtti )
#error "RTTI support required for unknown compilers"
#else

#include <typeinfo>

template< typename T >
[[nodiscard]] constexpr std::string_view TAO_PEGTL_NAMESPACE::demangle() noexcept
{
// fallback: requires RTTI, no demangling
return typeid( T ).name();
}

#endif

} // namespace TAO_PEGTL_NAMESPACE
#endif

#endif
4 changes: 2 additions & 2 deletions include/tao/pegtl/version.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
#ifndef TAO_PEGTL_VERSION_HPP
#define TAO_PEGTL_VERSION_HPP

#define TAO_PEGTL_VERSION "3.2.4"
#define TAO_PEGTL_VERSION "3.2.5"

#define TAO_PEGTL_VERSION_MAJOR 3
#define TAO_PEGTL_VERSION_MINOR 2
#define TAO_PEGTL_VERSION_PATCH 4
#define TAO_PEGTL_VERSION_PATCH 5

#endif

0 comments on commit 442d518

Please sign in to comment.