return 0;
}
-/* { dg-final { scan-tree-dump-not "if" "cddce2"} } */
+/* Adding __builtin_unreachable to std::string::size() prevents cddce2 from
+ eliminating the loop early, see PR117764. */
+/* { dg-final { scan-tree-dump-not "if" "cddce2" { xfail *-*-* } } } */
+/* { dg-final { scan-tree-dump-not "if" "cddce3"} } */
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
size_type
size() const _GLIBCXX_NOEXCEPT
- { return _M_string_length; }
+ {
+ size_type __sz = _M_string_length;
+ if (__sz > max_size ())
+ __builtin_unreachable ();
+ return __sz;
+ }
/// Returns the number of characters in the string, not including any
/// null-termination.
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
size_type
length() const _GLIBCXX_NOEXCEPT
- { return _M_string_length; }
+ { return size(); }
/// Returns the size() of the largest possible %string.
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
size_type
max_size() const _GLIBCXX_NOEXCEPT
- { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; }
+ {
+ const size_t __diffmax
+ = __gnu_cxx::__numeric_traits<ptrdiff_t>::__max / sizeof(_CharT);
+ const size_t __allocmax = _Alloc_traits::max_size(_M_get_allocator());
+ return (std::min)(__diffmax, __allocmax) - 1;
+ }
/**
* @brief Resizes the %string to the specified number of characters.
size_type
capacity() const _GLIBCXX_NOEXCEPT
{
- return _M_is_local() ? size_type(_S_local_capacity)
- : _M_allocated_capacity;
+ size_t __sz = _M_is_local() ? size_type(_S_local_capacity)
+ : _M_allocated_capacity;
+ if (__sz < _S_local_capacity || __sz > max_size ())
+ __builtin_unreachable ();
+ return __sz;
}
/**
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
bool
empty() const _GLIBCXX_NOEXCEPT
- { return this->size() == 0; }
+ { return _M_string_length == 0; }
// Element access:
/**