[PATCH] Fix algo constexpr tests in Debug mode
François Dumont
frs.dumont@gmail.com
Sat Sep 28 21:12:00 GMT 2019
Here is what I just commited.
I try to use the asm trick in the _GLIBCXX_DEBUG_VERIFY_COND_AT but
didn't notice any enhancement. So for now I kept my solution to just
have a non-constexpr call compiler error.
I fix my patch to use __builtin_is_constant_evaluated rather than
std::is_constant_evaluated in __valid_range.
   * include/bits/stl_algobase.h (__memmove): Return _Tp*.
   (__memmove): Loop as long as __n is not 0.
   (__copy_move<>::__copy_m): Likewise.
   (__copy_move_backward<>::__copy_move_b): Likewise.
   * testsuite/25_algorithms/copy/constexpr.cc: Add check on copied
values.
   * testsuite/25_algorithms/copy_backward/constexpr.cc: Likewise.
   * testsuite/25_algorithms/copy/constexpr_neg.cc: New.
   * testsuite/25_algorithms/copy_backward/constexpr.cc: New.
   * include/debug/forward_list
(_Sequence_traits<__debug::forward_list<>>::_S_size): Returns __dp_sign
   distance when not empty.
   * include/debug/list
   (_Sequence_traits<__debug::list<>>::_S_size): Likewise.
   * include/debug/helper_functions.h (__dp_sign_max_size): New
   _Distance_precision enum entry.
   * include/debug/safe_iterator.h
   (__copy_move_a(_II, _II, const _Safe_iterator<>&)): Check for output
   iterator _M_can_advance as soon as input range distance precision is
   strictly higher than __dp_size.
   (__copy_move_a(const _Safe_iterator<>&, const _Safe_iterator<>&,
   const _Safe_iterator<>&)): Likewise.
   (__copy_move_backward_a(_II, _II, const _Safe_iterator<>&)): Likewise.
   (__copy_move_backward_a(const _Safe_iterator<>&,
   const _Safe_iterator<>&, const _Safe_iterator<>&)): Likewise.
   (__equal_aux(_II, _II, const _Safe_iterator<>&)): Likewise.
   (__equal_aux(const _Safe_iterator<>&,
   const _Safe_iterator<>&, const _Safe_iterator<>&)): Likewise.
François
On 9/27/19 6:45 PM, Jonathan Wakely wrote:
> On 27/09/19 18:24 +0200, François Dumont wrote:
>> On 9/27/19 2:11 PM, Jonathan Wakely wrote:
>>> On 19/09/19 22:27 +0200, François Dumont wrote:
>>>> Hi
>>>>
>>>> Â Â Â I start working on making recently added constexpr tests to
>>>> work in Debug mode.
>>>
>>> The attached patch seems to be necessary for that, right?
>>>
>>>
>> On my side I had done this, almost the same.
>>
>> For the moment there is a FIXME in macros.h to find out how to
>> generate a nice compilation error when the condition is not meant.
>>
>> static_assert can't be called in this context, too bad.
>>
>> I also try to define a function with a
>> __attribute__((__error__("because"))) attribute. But when I make it
>> constexpr gcc complains about missing definition. When I provide a
>> definition gcc complains that this attribute must be on a
>> declaration. And when I split declaration and definition gcc does not
>> produce the expected compilation error.
>
> Yes, I've tried similar things without success.
>
>> Unless you have the solution I consider that we need help from the
>> front-end.
>>
>> For the moment if Debug mode finds a problem it will be reported as
>> _M_error function not being constexpr !
>
> A reasonable workaround is to do:
>
> #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
> Â if (__builtin_is_constant_evaluated())
> Â Â asm("Debug Mode assertion failed");
> Â else
> #endif
> Â if (!(Cond))
> Â Â __gnu_debug::_Error_formatter::...
>
> The builtin is available even for C++98, whereas
> std::is_constant_evaluated() is only available for C++20.
>
> This produces errors that include lines like:
>
> asm.cc:12:17:Â Â in âconstexprâ expansion of âf(-1)â
> asm.cc:4:7: error: inline assembly is not a constant expression
> Â Â 4 |Â Â Â Â Â Â asm("debug mode assertion failed");
> Â Â Â Â |Â Â Â Â Â Â ^~~
> asm.cc:8:3: note: in expansion of macro âCHECKâ
> Â Â 8 |Â Â _GLIBCXX_ASSERT(i > 0);
> Â Â Â Â |Â Â ^~~~~
> asm.cc:4:7: note: only unevaluated inline assembly is allowed in a
> âconstexprâ function in C++2a
> Â Â 4 |Â Â Â Â Â Â asm("debug mode assertion failed");
> Â Â Â Â |Â Â Â Â Â Â ^~~
> asm.cc:8:3: note: in expansion of macro âCHECKâ
> Â Â 8 |Â Â CHECK(i > 0);
> Â Â Â Â |Â Â ^~~~~
>
> It's not ideal, but it does show the failed condition and the text
> "debug mode assertion failed" (or whatever message you choose to use
> there).
>
>
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: debug_constexpr.patch
Type: text/x-patch
Size: 19721 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/libstdc++/attachments/20190928/73a0b063/attachment.bin>
More information about the Libstdc++
mailing list