Skip to content

Commit 24d8122

Browse files
committed
Classify a few more assertions (mostly in <regex>`).
1 parent 904bb3e commit 24d8122

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

libcxx/include/__config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,8 @@ _LIBCPP_HARDENING_MODE_DEBUG
343343
# define _LIBCPP_ASSERT_NON_OVERLAPPING_RANGES(expression, message) _LIBCPP_ASSERT(expression, message)
344344
# define _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(expression, message) _LIBCPP_ASSERT(expression, message)
345345
# define _LIBCPP_ASSERT_UNCATEGORIZED(expression, message) _LIBCPP_ASSERT(expression, message)
346+
# define _LIBCPP_ASSERT_PEDANTIC(expression, message) _LIBCPP_ASSERT(expression, message)
346347
// Disabled checks.
347-
# define _LIBCPP_ASSERT_PEDANTIC(expression, message) _LIBCPP_ASSUME(expression)
348348
# define _LIBCPP_ASSERT_INTERNAL(expression, message) _LIBCPP_ASSUME(expression)
349349

350350
// Debug hardening mode checks.

libcxx/include/__filesystem/directory_iterator.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ class directory_iterator {
7373
_LIBCPP_HIDE_FROM_ABI ~directory_iterator() = default;
7474

7575
_LIBCPP_HIDE_FROM_ABI const directory_entry& operator*() const {
76-
_LIBCPP_ASSERT_UNCATEGORIZED(__imp_, "The end iterator cannot be dereferenced");
76+
// Note: this check duplicates a check in `__dereference()`.
77+
_LIBCPP_ASSERT_NON_NULL(__imp_, "The end iterator cannot be dereferenced");
7778
return __dereference();
7879
}
7980

libcxx/include/regex

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4587,28 +4587,36 @@ public:
45874587

45884588
// element access:
45894589
_LIBCPP_HIDE_FROM_ABI difference_type length(size_type __sub = 0) const {
4590-
_LIBCPP_ASSERT_UNCATEGORIZED(ready(), "match_results::length() called when not ready");
4590+
// If the match results are not ready, this will return `0`.
4591+
_LIBCPP_ASSERT_PEDANTIC(ready(), "match_results::length() called when not ready");
45914592
return (*this)[__sub].length();
45924593
}
45934594
_LIBCPP_HIDE_FROM_ABI difference_type position(size_type __sub = 0) const {
4594-
_LIBCPP_ASSERT_UNCATEGORIZED(ready(), "match_results::position() called when not ready");
4595+
// If the match results are not ready, this will return the result of subtracting two default-constructed iterators
4596+
// (which is typically a well-defined operation).
4597+
_LIBCPP_ASSERT_PEDANTIC(ready(), "match_results::position() called when not ready");
45954598
return std::distance(__position_start_, (*this)[__sub].first);
45964599
}
45974600
_LIBCPP_HIDE_FROM_ABI string_type str(size_type __sub = 0) const {
4598-
_LIBCPP_ASSERT_UNCATEGORIZED(ready(), "match_results::str() called when not ready");
4601+
// If the match results are not ready, this will return an empty string.
4602+
_LIBCPP_ASSERT_PEDANTIC(ready(), "match_results::str() called when not ready");
45994603
return (*this)[__sub].str();
46004604
}
46014605
_LIBCPP_HIDE_FROM_ABI const_reference operator[](size_type __n) const {
4602-
_LIBCPP_ASSERT_UNCATEGORIZED(ready(), "match_results::operator[]() called when not ready");
4606+
// If the match results are not ready, this call will be equivalent to calling this function with `__n >= size()`,
4607+
// returning an empty subrange.
4608+
_LIBCPP_ASSERT_PEDANTIC(ready(), "match_results::operator[]() called when not ready");
46034609
return __n < __matches_.size() ? __matches_[__n] : __unmatched_;
46044610
}
46054611

46064612
_LIBCPP_HIDE_FROM_ABI const_reference prefix() const {
4607-
_LIBCPP_ASSERT_UNCATEGORIZED(ready(), "match_results::prefix() called when not ready");
4613+
// If the match results are not ready, this will return a default-constructed empty `__suffix_`.
4614+
_LIBCPP_ASSERT_PEDANTIC(ready(), "match_results::prefix() called when not ready");
46084615
return __prefix_;
46094616
}
46104617
_LIBCPP_HIDE_FROM_ABI const_reference suffix() const {
4611-
_LIBCPP_ASSERT_UNCATEGORIZED(ready(), "match_results::suffix() called when not ready");
4618+
// If the match results are not ready, this will return a default-constructed empty `__suffix_`.
4619+
_LIBCPP_ASSERT_PEDANTIC(ready(), "match_results::suffix() called when not ready");
46124620
return __suffix_;
46134621
}
46144622

@@ -4722,7 +4730,8 @@ _OutputIter match_results<_BidirectionalIterator, _Allocator>::format(
47224730
const char_type* __fmt_first,
47234731
const char_type* __fmt_last,
47244732
regex_constants::match_flag_type __flags) const {
4725-
_LIBCPP_ASSERT_UNCATEGORIZED(ready(), "match_results::format() called when not ready");
4733+
// Note: this duplicates a check in `vector::operator[]` but provides a better error message.
4734+
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(ready(), "match_results::format() called when not ready");
47264735
if (__flags & regex_constants::format_sed) {
47274736
for (; __fmt_first != __fmt_last; ++__fmt_first) {
47284737
if (*__fmt_first == '&')

0 commit comments

Comments
 (0)