]> git.ipfire.org Git - thirdparty/gcc.git/commit - libstdc++-v3/include/parallel/base.h
libstdc++: Add compile-time checks to__glibcxx_assert [PR 71960]
authorJonathan Wakely <jwakely@redhat.com>
Tue, 1 Sep 2020 19:52:26 +0000 (20:52 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 1 Sep 2020 19:52:26 +0000 (20:52 +0100)
commit10f51543bb81cc953792270b40a9c812049e8b4c
tree871b9555550a2aa022e056d73237b2cf377de12a
parent8bc0f24d7a20d89383859907b875a26ce59dc6c8
libstdc++: Add compile-time checks to__glibcxx_assert [PR 71960]

This change evaluates __glibcxx_assert checks unconditionally when a
function is being constant evaluated (when std::is_constant_evaluated()
is true). If the check fails, compilation will fail with an error.

If the function isn't being constant evaluated, the normal runtime check
will be done if enabled by _GLIBCXX_ASSERTIONS or _GLIBCXX_DEBUG, the
same as before.

Tangentially, the __glibcxx_assert and _GLIBCXX_PARALLEL_ASSERT macros
are changed to expand to 'do { } while (false)' when assertions are
disabled, instead of expanding to nothing. This avoids -Wempty-body
warnings when a disabled assertion is used in an 'if' or 'else'
statement e.g.

  if constexpr (/* precondition is testable */)
    __glibcxx_assert(precondition);

a.C:9:27: warning: suggest braces around empty body in an ‘if’ statement [-Wempty-body]
    9 |     __glibcxx_assert(precondition);
      |                                  ^

libstdc++-v3/ChangeLog:

PR libstdc++/71960
* include/bits/c++config (__glibcxx_assert_impl): Remove
do-while so that uses of the macro need to add it.
(__glibcxx_assert): Rename macro for runtime assertions
to __glibcxx_assert_2.
(__glibcxx_assert_1): Define macro for constexpr assertions.
(__glibcxx_assert): Define macro for constexpr and runtime
assertions.
* include/bits/range_access.h (ranges::advance): Remove
redundant precondition checks during constant evaluation.
* include/parallel/base.h (_GLIBCXX_PARALLEL_ASSERT): Always
use do-while in macro expansion.
* include/std/ranges (iota_view::iota_view(W, B)): Remove
redundant braces.
libstdc++-v3/include/bits/c++config
libstdc++-v3/include/bits/range_access.h
libstdc++-v3/include/parallel/base.h
libstdc++-v3/include/std/ranges