Skip to content

Commit 57e354d

Browse files
committed
Prettier verbose_diagnostic_info printing
1 parent 04c534b commit 57e354d

File tree

5 files changed

+71
-29
lines changed

5 files changed

+71
-29
lines changed

include/boost/leaf/common.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ struct BOOST_LEAF_SYMBOL_VISIBLE e_errno
5757
template <class CharT, class Traits>
5858
friend std::ostream & operator<<(std::basic_ostream<CharT, Traits> & os, e_errno const & err)
5959
{
60-
return os << type<e_errno>() << ": " << err.value << ", \"" << std::strerror(err.value) << '"';
60+
return print_type_str<e_errno>(os) << err.value << ", \"" << std::strerror(err.value) << '"';
6161
}
6262
};
6363

@@ -101,7 +101,7 @@ namespace windows
101101
*--z = 0;
102102
if( z[-1] == '\r' )
103103
*--z = 0;
104-
return os << type<e_LastError>() << ": " << err.value << ", \"" << (LPCSTR)mb.p << '"';
104+
return print_type_str<e_LastError>(os) << err.value << ", \"" << (LPCSTR)mb.p << '"';
105105
}
106106
return os;
107107
}

include/boost/leaf/detail/demangle.hpp

+62-21
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,85 @@
1717

1818
#include <boost/leaf/config.hpp>
1919

20+
#include <iosfwd>
2021
#include <cstring>
2122

2223
namespace boost { namespace leaf {
2324

2425
namespace leaf_detail
2526
{
2627
template <int N>
27-
BOOST_LEAF_CONSTEXPR inline char const * check_prefix( char const * t, char const (&prefix)[N] )
28+
BOOST_LEAF_CONSTEXPR inline int check_prefix(char const * t, char const (&prefix)[N]) noexcept
2829
{
29-
return std::strncmp(t,prefix,sizeof(prefix)-1)==0 ? t+sizeof(prefix)-1 : t;
30+
return std::strncmp(t, prefix, sizeof(prefix)-1) == 0 ? sizeof(prefix) - 1 : 0;
3031
}
3132
}
3233

3334
template <class Name>
34-
inline char const * type()
35+
inline char const * type_str()
3536
{
36-
using leaf_detail::check_prefix;
37-
char const * t =
38-
#ifdef __FUNCSIG__
39-
__FUNCSIG__;
37+
#if defined(_MSC_VER)
38+
if( char const * p = std::strstr(__FUNCSIG__, "boost::leaf::type_str<") )
39+
return p + 22;
40+
else
41+
return __FUNCSIG__;
4042
#else
41-
__PRETTY_FUNCTION__;
43+
# if defined(__clang__)
44+
BOOST_LEAF_ASSERT(leaf_detail::check_prefix(__PRETTY_FUNCTION__, "const char *boost::leaf::type_str() [Name = ") == 44);
45+
return __PRETTY_FUNCTION__ + 44;
46+
# elif defined(__GNUC__)
47+
BOOST_LEAF_ASSERT(leaf_detail::check_prefix(__PRETTY_FUNCTION__, "const char* boost::leaf::type_str() [with Name = ") == 49);
48+
return __PRETTY_FUNCTION__ + 49;
49+
# else
50+
if( int clang_style = leaf_detail::check_prefix(__PRETTY_FUNCTION__, "const char *boost::leaf::type_str() [Name = ") )
51+
return __PRETTY_FUNCTION__ + clang_style;
52+
else if( int gcc_style = leaf_detail::check_prefix(__PRETTY_FUNCTION__, "const char* boost::leaf::type_str() [with Name = ") )
53+
return __PRETTY_FUNCTION__ + gcc_style;
54+
else
55+
return __PRETTY_FUNCTION__;
56+
return nullptr;
57+
# endif
4258
#endif
43-
#if defined(__clang__)
44-
BOOST_LEAF_ASSERT(check_prefix(t,"const char *boost::leaf::type() ")==t+32);
45-
return t+32;
46-
#elif defined(__GNUC__)
47-
BOOST_LEAF_ASSERT(check_prefix(t,"const char* boost::leaf::type() ")==t+32);
48-
return t+32;
59+
}
60+
61+
template <class Name, class CharT, class Traits>
62+
inline std::ostream & print_type_str(std::basic_ostream<CharT, Traits> & os)
63+
{
64+
if( char const * t = type_str<Name>() )
65+
{
66+
char const * end = std::strchr(t, 0);
67+
#ifdef _MSC_VER
68+
BOOST_LEAF_ASSERT(end - t > 7);
69+
BOOST_LEAF_ASSERT(std::memcmp(end - 7, ">(void)", 7) == 0);
70+
if( *t == 's' )
71+
{
72+
BOOST_LEAF_ASSERT(std::memcmp(t + 1, "truct ", 6) == 0);
73+
t += 7;
74+
}
75+
else if( *t == 'c' )
76+
{
77+
BOOST_LEAF_ASSERT(std::memcmp(t + 1, "lass ", 5) == 0);
78+
t += 6;
79+
}
80+
else if( *t == 'e' )
81+
{
82+
BOOST_LEAF_ASSERT(std::memcmp(t + 1, "num ", 4) == 0);
83+
t += 5;
84+
}
85+
return os.write(t, end - t - 7) << ": ";
86+
#elif defined(__clang__) || defined(__GNUC__)
87+
BOOST_LEAF_ASSERT(end != t);
88+
BOOST_LEAF_ASSERT(end[-1] == ']');
89+
return os.write(t, end - t - 1) << ": ";
4990
#else
50-
char const * clang_style = check_prefix(t,"const char *boost::leaf::type() ");
51-
if( clang_style!=t )
52-
return clang_style;
53-
char const * gcc_style = check_prefix(t,"const char* boost::leaf::type() ");
54-
if( gcc_style!=t )
55-
return gcc_style;
91+
if( end != t && end[-1] == ']')
92+
return os.write(t, end - t - 1) << ": ";
93+
else
94+
return os << t << ": ";
5695
#endif
57-
return t;
96+
}
97+
else
98+
return os << "no name: ";
5899
}
59100

60101
} }

include/boost/leaf/detail/print.hpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ namespace leaf_detail
7070
template <class CharT, class Traits>
7171
static void print( std::basic_ostream<CharT, Traits> & os, Wrapper const & x )
7272
{
73-
os << type<Wrapper>() << ": " << x.value;
73+
print_type_str<Wrapper>(os) << x.value;
7474
}
7575
};
7676

@@ -82,7 +82,7 @@ namespace leaf_detail
8282
template <class CharT, class Traits>
8383
static void print( std::basic_ostream<CharT, Traits> & os, Wrapper const & ex )
8484
{
85-
os << type<Wrapper>() << ": std::exception::what(): " << ex.what();
85+
print_type_str<Wrapper>(os) << "std::exception::what(): " << ex.what();
8686
}
8787
};
8888

@@ -94,7 +94,7 @@ namespace leaf_detail
9494
template <class CharT, class Traits>
9595
static void print( std::basic_ostream<CharT, Traits> & os, Wrapper const & )
9696
{
97-
os << type<Wrapper>() << ": {not printable}";
97+
print_type_str<Wrapper>(os) << "{not printable}";
9898
}
9999
};
100100

@@ -106,7 +106,7 @@ namespace leaf_detail
106106
template <class CharT, class Traits>
107107
static void print( std::basic_ostream<CharT, Traits> & os, Wrapper const & w )
108108
{
109-
os << type<Wrapper>() << ": " << static_cast<typename std::underlying_type<Wrapper>::type>(w);
109+
print_type_str<Wrapper>(os) << static_cast<typename std::underlying_type<Wrapper>::type>(w);
110110
}
111111
};
112112

include/boost/leaf/error.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ struct BOOST_LEAF_SYMBOL_VISIBLE e_source_location
7777
template <class CharT, class Traits>
7878
friend std::ostream & operator<<( std::basic_ostream<CharT, Traits> & os, e_source_location const & x )
7979
{
80-
return os << leaf::type<e_source_location>() << ": " << x.file << '(' << x.line << ") in function " << x.function;
80+
return leaf::print_type_str<e_source_location>(os) << x.file << '(' << x.line << ") in function " << x.function;
8181
}
8282
};
8383

test/diagnostic_info_test1.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ struct printable_info_non_printable_payload
7373
}
7474
};
7575

76-
struct non_printable_info_printable_payload
76+
class non_printable_info_printable_payload
7777
{
78+
public:
7879
printable_payload value;
7980
};
8081

0 commit comments

Comments
 (0)