Skip to content

Commit

Permalink
Moved parse_name inside leaf_detail for more consistent results
Browse files Browse the repository at this point in the history
  • Loading branch information
zajo committed Sep 2, 2024
1 parent 4795aac commit 68bf3a8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 58 deletions.
114 changes: 57 additions & 57 deletions include/boost/leaf/detail/demangle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,69 +109,69 @@ namespace leaf_detail
{
return cpp11_suffix<S1, S2, S1 - 2, S2 - 2>::check(str, suffix) ? S1 - S2 : 0;
}
}

struct parsed_name
{
char const * name;
int len;
parsed_name(char const * name, int len) noexcept:
name(name),
len(len)
{
}
template <int S>
parsed_name(char const(&name)[S]) noexcept:
name(name),
len(S-1)
{
}
bool parse_success() const noexcept
struct parsed_name
{
return name[len] != 0;
}
template <class CharT, class Traits>
friend std::ostream & operator<<(std::basic_ostream<CharT, Traits> & os, parsed_name const & pn)
{
return os.write(pn.name, pn.len);
}
};
char const * name;
int len;
parsed_name(char const * name, int len) noexcept:
name(name),
len(len)
{
}
template <int S>
parsed_name(char const(&name)[S]) noexcept:
name(name),
len(S-1)
{
}
bool parse_success() const noexcept
{
return name[len] != 0;
}
template <class CharT, class Traits>
friend std::ostream & operator<<(std::basic_ostream<CharT, Traits> & os, parsed_name const & pn)
{
return os.write(pn.name, pn.len);
}
};

template <class Name>
parsed_name parse_name()
{
// Workaround for older gcc compilers where __PRETTY_FUNCTION__ is not constexpr.
// Instead of evaluating constexpr int x = f(__PRETTY_FUNCTION__), which fails,
// we evaluate int const x = f(__PRETTY_FUNCTION__). Then we enforce compile-time
// execution by evaluating sizeof(char[1 + x]) -1.
template <class Name>
parsed_name parse_name()
{
// Workaround for older gcc compilers where __PRETTY_FUNCTION__ is not constexpr.
// Instead of evaluating constexpr int x = f(__PRETTY_FUNCTION__), which fails,
// we evaluate int const x = f(__PRETTY_FUNCTION__). Then we enforce compile-time
// execution by evaluating sizeof(char[1 + x]) -1.
#define BOOST_LEAF_PARSE_PF(prefix, suffix) \
{ \
if( int const s = leaf_detail::check_suffix(BOOST_LEAF_PRETTY_FUNCTION, suffix) ) \
if( int const p = leaf_detail::check_prefix(BOOST_LEAF_PRETTY_FUNCTION, prefix) ) \
return parsed_name(BOOST_LEAF_PRETTY_FUNCTION + sizeof(char[1 + p]) - 1, sizeof(char[1 + s - p]) - 1); \
}
// clang style:
BOOST_LEAF_PARSE_PF( "parsed_name boost::leaf::parse_name() [Name = ", "]");
// old clang style:
BOOST_LEAF_PARSE_PF( "boost::leaf::parsed_name boost::leaf::parse_name() [Name = ", "]");
// gcc style:
BOOST_LEAF_PARSE_PF( "boost::leaf::parsed_name boost::leaf::parse_name() [with Name = ", "]");
// msvc style, __cdecl, struct/class/enum:
BOOST_LEAF_PARSE_PF( "struct boost::leaf::parsed_name __cdecl boost::leaf::parse_name<struct ", ">(void)");
BOOST_LEAF_PARSE_PF( "struct boost::leaf::parsed_name __cdecl boost::leaf::parse_name<class ", ">(void)");
BOOST_LEAF_PARSE_PF( "struct boost::leaf::parsed_name __cdecl boost::leaf::parse_name<enum ", ">(void)");
// msvc style, __stdcall, struct/class/enum:
BOOST_LEAF_PARSE_PF( "struct boost::leaf::parsed_name __stdcall boost::leaf::parse_name<struct ", ">(void)");
BOOST_LEAF_PARSE_PF( "struct boost::leaf::parsed_name __stdcall boost::leaf::parse_name<class ", ">(void)");
BOOST_LEAF_PARSE_PF( "struct boost::leaf::parsed_name __stdcall boost::leaf::parse_name<enum ", ">(void)");
// msvc style, __fastcall, struct/class/enum:
BOOST_LEAF_PARSE_PF( "struct boost::leaf::parsed_name __fastcall boost::leaf::parse_name<struct ", ">(void)");
BOOST_LEAF_PARSE_PF( "struct boost::leaf::parsed_name __fastcall boost::leaf::parse_name<class ", ">(void)");
BOOST_LEAF_PARSE_PF( "struct boost::leaf::parsed_name __fastcall boost::leaf::parse_name<enum ", ">(void)");
{ \
if( int const s = leaf_detail::check_suffix(BOOST_LEAF_PRETTY_FUNCTION, suffix) ) \
if( int const p = leaf_detail::check_prefix(BOOST_LEAF_PRETTY_FUNCTION, prefix) ) \
return parsed_name(BOOST_LEAF_PRETTY_FUNCTION + sizeof(char[1 + p]) - 1, sizeof(char[1 + s - p]) - 1); \
}
// clang style:
BOOST_LEAF_PARSE_PF( "parsed_name boost::leaf::leaf_detail::parse_name() [Name = ", "]");
// old clang style:
BOOST_LEAF_PARSE_PF( "boost::leaf::parsed_name boost::leaf::leaf_detail::parse_name() [Name = ", "]");
// gcc style:
BOOST_LEAF_PARSE_PF( "boost::leaf::parsed_name boost::leaf::leaf_detail::parse_name() [with Name = ", "]");
// msvc style, __cdecl, struct/class/enum:
BOOST_LEAF_PARSE_PF( "struct boost::leaf::parsed_name __cdecl boost::leaf::leaf_detail::parse_name<struct ", ">(void)");
BOOST_LEAF_PARSE_PF( "struct boost::leaf::parsed_name __cdecl boost::leaf::leaf_detail::parse_name<class ", ">(void)");
BOOST_LEAF_PARSE_PF( "struct boost::leaf::parsed_name __cdecl boost::leaf::leaf_detail::parse_name<enum ", ">(void)");
// msvc style, __stdcall, struct/class/enum:
BOOST_LEAF_PARSE_PF( "struct boost::leaf::parsed_name __stdcall boost::leaf::leaf_detail::parse_name<struct ", ">(void)");
BOOST_LEAF_PARSE_PF( "struct boost::leaf::parsed_name __stdcall boost::leaf::leaf_detail::parse_name<class ", ">(void)");
BOOST_LEAF_PARSE_PF( "struct boost::leaf::parsed_name __stdcall boost::leaf::leaf_detail::parse_name<enum ", ">(void)");
// msvc style, __fastcall, struct/class/enum:
BOOST_LEAF_PARSE_PF( "struct boost::leaf::parsed_name __fastcall boost::leaf::leaf_detail::parse_name<struct ", ">(void)");
BOOST_LEAF_PARSE_PF( "struct boost::leaf::parsed_name __fastcall boost::leaf::leaf_detail::parse_name<class ", ">(void)");
BOOST_LEAF_PARSE_PF( "struct boost::leaf::parsed_name __fastcall boost::leaf::leaf_detail::parse_name<enum ", ">(void)");
#undef BOOST_LEAF_PARSE_PF

// Unrecognized __PRETTY_FUNCTION__/__FUNSIG__ format, return as-is. Note, parsing is done at compile-time.
return parsed_name(BOOST_LEAF_PRETTY_FUNCTION);
// Unrecognized __PRETTY_FUNCTION__/__FUNSIG__ format, return as-is. Note, parsing is done at compile-time.
return parsed_name(BOOST_LEAF_PRETTY_FUNCTION);
}
}

} }
Expand Down
2 changes: 1 addition & 1 deletion test/parse_name_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace
template <class Name>
void test_name(std::string const & correct)
{
leaf::parsed_name pn = leaf::parse_name<Name>();
auto pn = leaf::leaf_detail::parse_name<Name>();
BOOST_TEST(pn.parse_success());
BOOST_TEST_EQ(std::string(pn.name, pn.len), correct);
}
Expand Down

0 comments on commit 68bf3a8

Please sign in to comment.