* [PATCH 1/2] libstdc++: Enable more debug assertions during constant evaluation [PR111250]
@ 2024-06-27 8:45 Jonathan Wakely
2024-06-27 8:45 ` [PATCH 2/2] libstdc++: Add debug assertions to std::vector<bool> [PR103191] Jonathan Wakely
0 siblings, 1 reply; 2+ messages in thread
From: Jonathan Wakely @ 2024-06-27 8:45 UTC (permalink / raw)
To: libstdc++, gcc-patches
Tested x86_64-linux. Pushed to trunk.
Worth backporting, I think, but I'll wait a bit.
-- >8 --
Some of our debug assertions expand to nothing unless
_GLIBCXX_ASSERTIONS is defined, which means they are not checked during
constant evaluation. By making them unconditionally expand to a
__glibcxx_assert expression they will be checked during constant
evaluation. This allows us to diagnose more instances of undefined
behaviour at compile-time, such as accessing a vector past-the-end.
libstdc++-v3/ChangeLog:
PR libstdc++/111250
* include/debug/assertions.h (__glibcxx_requires_non_empty_range)
(__glibcxx_requires_nonempty, __glibcxx_requires_subscript):
Define to __glibcxx_assert expressions or to debug mode
__glibcxx_check_xxx expressions.
* testsuite/23_containers/array/element_access/constexpr_c++17.cc:
Add checks for out-of-bounds accesses in constant expressions.
* testsuite/23_containers/vector/element_access/constexpr.cc:
Likewise.
---
libstdc++-v3/include/debug/assertions.h | 14 +++---
.../array/element_access/constexpr_c++17.cc | 44 +++++++++++++++++++
.../vector/element_access/constexpr.cc | 24 ++++++++--
3 files changed, 72 insertions(+), 10 deletions(-)
diff --git a/libstdc++-v3/include/debug/assertions.h b/libstdc++-v3/include/debug/assertions.h
index fff1ae8def0..20441e33897 100644
--- a/libstdc++-v3/include/debug/assertions.h
+++ b/libstdc++-v3/include/debug/assertions.h
@@ -31,12 +31,7 @@
#include <bits/c++config.h>
-#ifndef _GLIBCXX_ASSERTIONS
-# define __glibcxx_requires_non_empty_range(_First,_Last)
-# define __glibcxx_requires_nonempty()
-# define __glibcxx_requires_subscript(_N)
-#else
-
+#ifndef _GLIBCXX_DEBUG
// Verify that [_First, _Last) forms a non-empty iterator range.
# define __glibcxx_requires_non_empty_range(_First,_Last) \
__glibcxx_assert(_First != _Last)
@@ -45,6 +40,13 @@
// Verify that the container is nonempty
# define __glibcxx_requires_nonempty() \
__glibcxx_assert(!this->empty())
+#else // Use the more verbose Debug Mode checks.
+# define __glibcxx_requires_non_empty_range(_First,_Last) \
+ __glibcxx_check_non_empty_range(_First,_Last)
+# define __glibcxx_requires_nonempty() \
+ __glibcxx_check_nonempty()
+# define __glibcxx_requires_subscript(_N) \
+ __glibcxx_check_subscript(_N)
#endif
#if defined _GLIBCXX_DEBUG && _GLIBCXX_HOSTED
diff --git a/libstdc++-v3/testsuite/23_containers/array/element_access/constexpr_c++17.cc b/libstdc++-v3/testsuite/23_containers/array/element_access/constexpr_c++17.cc
index a14ad487b42..19ab1cc1f8e 100644
--- a/libstdc++-v3/testsuite/23_containers/array/element_access/constexpr_c++17.cc
+++ b/libstdc++-v3/testsuite/23_containers/array/element_access/constexpr_c++17.cc
@@ -66,3 +66,47 @@ constexpr bool test_zero()
}
static_assert( test_zero() );
+
+#ifdef __cpp_concepts
+template<typename T = int>
+ constexpr std::false_type
+ access_empty() { return {}; }
+
+template<typename T = int>
+ requires (std::bool_constant<&std::array<T, 0>{}.at(0) != nullptr>::value)
+ constexpr std::true_type
+ access_empty() { return {}; }
+
+template<typename T = int>
+ requires (std::bool_constant<&std::array<T, 0>{}[0] != nullptr>::value)
+ constexpr std::true_type
+ access_empty() { return {}; }
+
+template<typename T = int>
+ requires (std::bool_constant<&std::array<T, 0>{}.front() != nullptr>::value)
+ constexpr std::true_type
+ access_empty() { return {}; }
+
+template<typename T = int>
+ requires (std::bool_constant<&std::array<T, 0>{}.back() != nullptr>::value)
+ constexpr std::true_type
+ access_empty() { return {}; }
+
+static_assert( ! access_empty() );
+
+template<typename T = int>
+ constexpr std::false_type
+ access_past_the_end() { return {}; }
+
+template<typename T = int>
+ requires (std::bool_constant<std::array<T, 1>{}.at(0) != nullptr>::value)
+ constexpr std::true_type
+ access_past_the_end() { return {}; }
+
+template<typename T = int>
+ requires (std::bool_constant<&std::array<T, 1>{}[1] != nullptr>::value)
+ constexpr std::true_type
+ access_past_the_end() { return {}; }
+
+static_assert( ! access_past_the_end() );
+#endif
diff --git a/libstdc++-v3/testsuite/23_containers/vector/element_access/constexpr.cc b/libstdc++-v3/testsuite/23_containers/vector/element_access/constexpr.cc
index 19c91d28cd6..358ded47ad9 100644
--- a/libstdc++-v3/testsuite/23_containers/vector/element_access/constexpr.cc
+++ b/libstdc++-v3/testsuite/23_containers/vector/element_access/constexpr.cc
@@ -85,23 +85,39 @@ template<typename T = int>
access_empty() { return {}; }
template<typename T = int>
- requires (std::bool_constant<(std::vector<T>().at(0), true)>::value)
+ requires (std::bool_constant<&std::vector<T>().at(0) != nullptr>::value)
constexpr std::true_type
access_empty() { return {}; }
template<typename T = int>
- requires (std::bool_constant<(std::vector<T>()[0], true)>::value)
+ requires (std::bool_constant<&std::vector<T>()[0] != nullptr>::value)
constexpr std::true_type
access_empty() { return {}; }
template<typename T = int>
- requires (std::bool_constant<(std::vector<T>().front(), true)>::value)
+ requires (std::bool_constant<&std::vector<T>().front() != nullptr>::value)
constexpr std::true_type
access_empty() { return {}; }
template<typename T = int>
- requires (std::bool_constant<(std::vector<T>().back(), true)>::value)
+ requires (std::bool_constant<&std::vector<T>().back() != nullptr>::value)
constexpr std::true_type
access_empty() { return {}; }
static_assert( ! access_empty() );
+
+template<typename T = int>
+ constexpr std::false_type
+ access_past_the_end() { return {}; }
+
+template<typename T = int>
+ requires (std::bool_constant<&std::vector<T>(3).at(3) != nullptr>::value)
+ constexpr std::true_type
+ access_past_the_end() { return {}; }
+
+template<typename T = int>
+ requires (std::bool_constant<&std::vector<T>(3)[3] != nullptr>::value)
+ constexpr std::true_type
+ access_past_the_end() { return {}; }
+
+static_assert( ! access_past_the_end() );
--
2.45.2
^ permalink raw reply [flat|nested] 2+ messages in thread
* [PATCH 2/2] libstdc++: Add debug assertions to std::vector<bool> [PR103191]
2024-06-27 8:45 [PATCH 1/2] libstdc++: Enable more debug assertions during constant evaluation [PR111250] Jonathan Wakely
@ 2024-06-27 8:45 ` Jonathan Wakely
0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2024-06-27 8:45 UTC (permalink / raw)
To: libstdc++, gcc-patches
The previous commit allows these to work for constexpr too, which is
nice.
Tested x86_64-linux. Pushed to trunk.
-- >8 --
This adds debug assertions for std::vector<bool> element access.
libstdc++-v3/ChangeLog:
PR libstdc++/103191
* include/bits/stl_bvector.h (vector<bool>::operator[])
(vector<bool>::front, vector<bool>::back): Add debug assertions.
* testsuite/23_containers/vector/bool/element_access/constexpr.cc:
Remove dg-error that no longer triggers.
---
libstdc++-v3/include/bits/stl_bvector.h | 30 +++++++++++++++----
.../vector/bool/element_access/constexpr.cc | 2 +-
2 files changed, 25 insertions(+), 7 deletions(-)
diff --git a/libstdc++-v3/include/bits/stl_bvector.h b/libstdc++-v3/include/bits/stl_bvector.h
index 52153cadf8f..8685cc64cc4 100644
--- a/libstdc++-v3/include/bits/stl_bvector.h
+++ b/libstdc++-v3/include/bits/stl_bvector.h
@@ -1084,12 +1084,18 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
reference
operator[](size_type __n)
- { return begin()[__n]; }
+ {
+ __glibcxx_requires_subscript(__n);
+ return begin()[__n];
+ }
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
const_reference
operator[](size_type __n) const
- { return begin()[__n]; }
+ {
+ __glibcxx_requires_subscript(__n);
+ return begin()[__n];
+ }
protected:
_GLIBCXX20_CONSTEXPR
@@ -1133,22 +1139,34 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
reference
front()
- { return *begin(); }
+ {
+ __glibcxx_requires_nonempty();
+ return *begin();
+ }
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
const_reference
front() const
- { return *begin(); }
+ {
+ __glibcxx_requires_nonempty();
+ return *begin();
+ }
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
reference
back()
- { return *(end() - 1); }
+ {
+ __glibcxx_requires_nonempty();
+ return *(end() - 1);
+ }
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
const_reference
back() const
- { return *(end() - 1); }
+ {
+ __glibcxx_requires_nonempty();
+ return *(end() - 1);
+ }
_GLIBCXX20_CONSTEXPR
void
diff --git a/libstdc++-v3/testsuite/23_containers/vector/bool/element_access/constexpr.cc b/libstdc++-v3/testsuite/23_containers/vector/bool/element_access/constexpr.cc
index bff9f7b4e0f..7c60e5db4d1 100644
--- a/libstdc++-v3/testsuite/23_containers/vector/bool/element_access/constexpr.cc
+++ b/libstdc++-v3/testsuite/23_containers/vector/bool/element_access/constexpr.cc
@@ -115,4 +115,4 @@ template<typename T = bool>
constexpr std::true_type
access_empty_front() { return {}; }
-static_assert( ! access_empty_front() ); // { dg-error "ambiguous" "PR 103191" { target { ! debug_mode } } }
+static_assert( ! access_empty_front() );
--
2.45.2
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-06-27 8:48 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-27 8:45 [PATCH 1/2] libstdc++: Enable more debug assertions during constant evaluation [PR111250] Jonathan Wakely
2024-06-27 8:45 ` [PATCH 2/2] libstdc++: Add debug assertions to std::vector<bool> [PR103191] Jonathan Wakely
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).