Skip to content

Commit e1c0b0c

Browse files
committed
n:p c++11 constexpr compliant
1 parent d4cf394 commit e1c0b0c

File tree

1 file changed

+68
-67
lines changed

1 file changed

+68
-67
lines changed

include/boost/leaf/detail/demangle.hpp

Lines changed: 68 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,35 @@ namespace detail
112112
{
113113
return cpp11_suffix<S1, S2, S1 - 2, S2 - 2>::check(str, suffix) ? S1 - S2 : 0;
114114
}
115+
116+
////////////////////////////////////////
117+
118+
template <
119+
int P01, int P02, int P03, int P04, int P05, int P06, int P07, int P08,
120+
int P09, int P10, int P11, int P12, int P13, int P14, int P15, int P16,
121+
int P17, int P18, int P19, int P20, int P21, int P22, int P23, int P24,
122+
int S01, int S02
123+
>
124+
class offset_length
125+
{
126+
static_assert(
127+
(S01 && (1 == (!!P01 + !!P02 + !!P03 + !!P04 + !!P05 + !!P06 + !!P07 + !!P08 + !!P09 + !!P10 + !!P11 + !!P12)))
128+
||
129+
(S02 && (1 == (!!P13 + !!P14 + !!P15 + !!P16 + !!P17 + !!P18 + !!P19 + !!P20 + !!P21)))
130+
||
131+
(S02 && (1 == (!!P22 + !!P23 + !!P24))),
132+
"Unrecognized __PRETTY_FUNCTION__/__FUNCSIG__ format, please file a github issue"
133+
);
134+
135+
static constexpr int off1 = !!S01 * (P01 + P02 + P03 + P04 + P05 + P06 + P07 + P08 + P09 + P10 + P11 + P12);
136+
static constexpr int off2 = !!S02 * (P13 + P14 + P15 + P16 + P17 + P18 + P19 + P20 + P21);
137+
static constexpr int off3 = !!S02 * (P22 + P23 + P24);
138+
139+
public:
140+
141+
static constexpr int offset = off1 ? off1 : (off2 ? off2 : off3);
142+
static constexpr int length = off1 ? (S01 - off1) : (off2 ? (S02 - off2) : (S02 - off3));
143+
};
115144
} // namespace detail
116145

117146
namespace n
@@ -134,76 +163,48 @@ namespace n
134163
template <class T>
135164
constexpr BOOST_LEAF_ALWAYS_INLINE r p()
136165
{
137-
// C++11 compile-time parsing of __PRETTY_FUNCTION__/__FUNCSIG__. The sizeof hacks are a
138-
// workaround for older GCC versions, where __PRETTY_FUNCTION__ is not constexpr, which triggers
139-
// compile errors when used in constexpr expressinos, yet evaluating a sizeof exrpession works.
140-
141-
// We don't try to recognize the compiler based on compiler-specific macros. Any compiler/version
142-
// is supported as long as it uses one of the formats we recognize.
143-
144-
// Unrecognized __PRETTY_FUNCTION__/__FUNCSIG__ formats will result in compiler diagnostics.
145-
// In that case, please file an issue on https://github.com/boostorg/leaf.
146-
147166
#define BOOST_LEAF_P(P) (sizeof(char[1 + detail::check_prefix(BOOST_LEAF_PRETTY_FUNCTION, P)]) - 1)
148-
// clang style:
149-
int const p01 = BOOST_LEAF_P("r boost::leaf::n::p() [T = ");
150-
int const p02 = BOOST_LEAF_P("r __cdecl boost::leaf::n::p(void) [T = ");
151-
int const p03 = BOOST_LEAF_P("r __stdcall boost::leaf::n::p(void) [T = ");
152-
int const p04 = BOOST_LEAF_P("r __fastcall boost::leaf::n::p(void) [T = ");
153-
// old clang style:
154-
int const p05 = BOOST_LEAF_P("boost::leaf::n::r boost::leaf::n::p() [T = ");
155-
int const p06 = BOOST_LEAF_P("boost::leaf::n::r __cdecl boost::leaf::n::p(void) [T = ");
156-
int const p07 = BOOST_LEAF_P("boost::leaf::n::r __stdcall boost::leaf::n::p(void) [T = ");
157-
int const p08 = BOOST_LEAF_P("boost::leaf::n::r __fastcall boost::leaf::n::p(void) [T = ");
158-
// gcc style:
159-
int const p09 = BOOST_LEAF_P("boost::leaf::n::r boost::leaf::n::p() [with T = ");
160-
int const p10 = BOOST_LEAF_P("boost::leaf::n::r __cdecl boost::leaf::n::p() [with T = ");
161-
int const p11 = BOOST_LEAF_P("boost::leaf::n::r __stdcall boost::leaf::n::p() [with T = ");
162-
int const p12 = BOOST_LEAF_P("boost::leaf::n::r __fastcall boost::leaf::n::p() [with T = ");
163-
// msvc style, struct:
164-
int const p13 = BOOST_LEAF_P("struct boost::leaf::n::r __cdecl boost::leaf::n::p<struct ");
165-
int const p14 = BOOST_LEAF_P("struct boost::leaf::n::r __stdcall boost::leaf::n::p<struct ");
166-
int const p15 = BOOST_LEAF_P("struct boost::leaf::n::r __fastcall boost::leaf::n::p<struct ");
167-
// msvc style, class:
168-
int const p16 = BOOST_LEAF_P("struct boost::leaf::n::r __cdecl boost::leaf::n::p<class ");
169-
int const p17 = BOOST_LEAF_P("struct boost::leaf::n::r __stdcall boost::leaf::n::p<class ");
170-
int const p18 = BOOST_LEAF_P("struct boost::leaf::n::r __fastcall boost::leaf::n::p<class ");
171-
// msvc style, enum:
172-
int const p19 = BOOST_LEAF_P("struct boost::leaf::n::r __cdecl boost::leaf::n::p<enum ");
173-
int const p20 = BOOST_LEAF_P("struct boost::leaf::n::r __stdcall boost::leaf::n::p<enum ");
174-
int const p21 = BOOST_LEAF_P("struct boost::leaf::n::r __fastcall boost::leaf::n::p<enum ");
175-
// msvc style, built-in type:
176-
int const p22 = BOOST_LEAF_P("struct boost::leaf::n::r __cdecl boost::leaf::n::p<");
177-
int const p23 = BOOST_LEAF_P("struct boost::leaf::n::r __stdcall boost::leaf::n::p<");
178-
int const p24 = BOOST_LEAF_P("struct boost::leaf::n::r __fastcall boost::leaf::n::p<");
179-
#undef BOOST_LEAF_P
180-
181167
#define BOOST_LEAF_S(S) (sizeof(char[1 + detail::check_suffix(BOOST_LEAF_PRETTY_FUNCTION, S)]) - 1)
182-
// clang/gcc style:
183-
int const s01 = BOOST_LEAF_S("]");
184-
// msvc style:
185-
int const s02 = BOOST_LEAF_S(">(void)");
168+
using offset_length = detail::offset_length<
169+
// prefix: clang style:
170+
BOOST_LEAF_P("r boost::leaf::n::p() [T = "),
171+
BOOST_LEAF_P("r __cdecl boost::leaf::n::p(void) [T = "),
172+
BOOST_LEAF_P("r __stdcall boost::leaf::n::p(void) [T = "),
173+
BOOST_LEAF_P("r __fastcall boost::leaf::n::p(void) [T = "),
174+
// prefix: old clang style:
175+
BOOST_LEAF_P("boost::leaf::n::r boost::leaf::n::p() [T = "),
176+
BOOST_LEAF_P("boost::leaf::n::r __cdecl boost::leaf::n::p(void) [T = "),
177+
BOOST_LEAF_P("boost::leaf::n::r __stdcall boost::leaf::n::p(void) [T = "),
178+
BOOST_LEAF_P("boost::leaf::n::r __fastcall boost::leaf::n::p(void) [T = "),
179+
// prefix: gcc style:
180+
BOOST_LEAF_P("boost::leaf::n::r boost::leaf::n::p() [with T = "),
181+
BOOST_LEAF_P("boost::leaf::n::r __cdecl boost::leaf::n::p() [with T = "),
182+
BOOST_LEAF_P("boost::leaf::n::r __stdcall boost::leaf::n::p() [with T = "),
183+
BOOST_LEAF_P("boost::leaf::n::r __fastcall boost::leaf::n::p() [with T = "),
184+
// prefix: msvc style, struct:
185+
BOOST_LEAF_P("struct boost::leaf::n::r __cdecl boost::leaf::n::p<struct "),
186+
BOOST_LEAF_P("struct boost::leaf::n::r __stdcall boost::leaf::n::p<struct "),
187+
BOOST_LEAF_P("struct boost::leaf::n::r __fastcall boost::leaf::n::p<struct "),
188+
// prefix: msvc style, class:
189+
BOOST_LEAF_P("struct boost::leaf::n::r __cdecl boost::leaf::n::p<class "),
190+
BOOST_LEAF_P("struct boost::leaf::n::r __stdcall boost::leaf::n::p<class "),
191+
BOOST_LEAF_P("struct boost::leaf::n::r __fastcall boost::leaf::n::p<class "),
192+
// prefix: msvc style, enum:
193+
BOOST_LEAF_P("struct boost::leaf::n::r __cdecl boost::leaf::n::p<enum "),
194+
BOOST_LEAF_P("struct boost::leaf::n::r __stdcall boost::leaf::n::p<enum "),
195+
BOOST_LEAF_P("struct boost::leaf::n::r __fastcall boost::leaf::n::p<enum "),
196+
// prefix: msvc style, built-in type:
197+
BOOST_LEAF_P("struct boost::leaf::n::r __cdecl boost::leaf::n::p<"),
198+
BOOST_LEAF_P("struct boost::leaf::n::r __stdcall boost::leaf::n::p<"),
199+
BOOST_LEAF_P("struct boost::leaf::n::r __fastcall boost::leaf::n::p<"),
200+
// suffix: clang/gcc style:
201+
BOOST_LEAF_S("]"),
202+
// suffix: msvc style:
203+
BOOST_LEAF_S(">(void)")
204+
>;
205+
return { BOOST_LEAF_PRETTY_FUNCTION + offset_length::offset, offset_length::length };
186206
#undef BOOST_LEAF_S
187-
188-
char static_assert_unrecognized_pretty_function_format_please_file_github_issue[sizeof(
189-
char[
190-
(s01 && (1 == (!!p01 + !!p02 + !!p03 + !!p04 + !!p05 + !!p06 + !!p07 + !!p08 + !!p09 + !!p10 + !!p11 + !!p12)))
191-
||
192-
(s02 && (1 == (!!p13 + !!p14 + !!p15 + !!p16 + !!p17 + !!p18 + !!p19 + !!p20 + !!p21)))
193-
||
194-
(s02 && (1 == (!!p22 + !!p23 + !!p24)))
195-
]
196-
) * 2 - 1] = {};
197-
(void) static_assert_unrecognized_pretty_function_format_please_file_github_issue;
198-
199-
if( int const p = sizeof(char[1 + !!s01 * (p01 + p02 + p03 + p04 + p05 + p06 + p07 + p08 + p09 + p10 + p11 + p12)]) - 1 )
200-
return { BOOST_LEAF_PRETTY_FUNCTION + p, s01 - p };
201-
202-
if( int const p = sizeof(char[1 + !!s02 * (p13 + p14 + p15 + p16 + p17 + p18 + p19 + p20 + p21)]) - 1 )
203-
return { BOOST_LEAF_PRETTY_FUNCTION + p, s02 - p };
204-
205-
int const p = sizeof(char[1 + !!s02 * (p22 + p23 + p24)]) - 1; // p is not zero, we've static asserted the hell out of it
206-
return { BOOST_LEAF_PRETTY_FUNCTION + p, s02 - p };
207+
#undef BOOST_LEAF_P
207208
}
208209
} // namespace n
209210

0 commit comments

Comments
 (0)