commit c1a5d9ebe73b6a7512757c5cb247d20689fa6cb6 Author: Jonathan Wakely Date: Fri Aug 26 09:43:32 2022 libstdc++: Add nonnull to starts_with/ends_with/contains string members Ideally this wouldn't be needed, because eventually these pointers all get passed to either the basic_string_view(const CharT*) constructor, or to basic_string_view::find(const CharT*), both of which already have the attribute. But for that to work requires optimization, so that the null value gets propagated through the call chain. Adding it explicitly to each member that requires a non-null pointer makes the diagnostics more reliable even without optimization. It's better to give a diagnostic earlier anyway, at the actual problematic call in the user's code. libstdc++-v3/ChangeLog: * include/bits/basic_string.h (starts_with, ends_with, contains): Add nonnull attribute. * include/bits/cow_string.h (starts_with, ends_with, contains): Likewise. * include/std/string_view (starts_with, ends_with, contains): Likewise. diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index b04fba95678..9d8b415302b 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -3400,6 +3400,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 starts_with(_CharT __x) const noexcept { return __sv_type(this->data(), this->size()).starts_with(__x); } + [[__gnu__::__nonnull__]] constexpr bool starts_with(const _CharT* __x) const noexcept { return __sv_type(this->data(), this->size()).starts_with(__x); } @@ -3412,6 +3413,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 ends_with(_CharT __x) const noexcept { return __sv_type(this->data(), this->size()).ends_with(__x); } + [[__gnu__::__nonnull__]] constexpr bool ends_with(const _CharT* __x) const noexcept { return __sv_type(this->data(), this->size()).ends_with(__x); } @@ -3426,6 +3428,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 contains(_CharT __x) const noexcept { return __sv_type(this->data(), this->size()).contains(__x); } + [[__gnu__::__nonnull__]] constexpr bool contains(const _CharT* __x) const noexcept { return __sv_type(this->data(), this->size()).contains(__x); } diff --git a/libstdc++-v3/include/bits/cow_string.h b/libstdc++-v3/include/bits/cow_string.h index f16e33ac1ef..f5f03338eec 100644 --- a/libstdc++-v3/include/bits/cow_string.h +++ b/libstdc++-v3/include/bits/cow_string.h @@ -3012,6 +3012,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION starts_with(_CharT __x) const noexcept { return __sv_type(this->data(), this->size()).starts_with(__x); } + [[__gnu__::__nonnull__]] bool starts_with(const _CharT* __x) const noexcept { return __sv_type(this->data(), this->size()).starts_with(__x); } @@ -3024,6 +3025,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ends_with(_CharT __x) const noexcept { return __sv_type(this->data(), this->size()).ends_with(__x); } + [[__gnu__::__nonnull__]] bool ends_with(const _CharT* __x) const noexcept { return __sv_type(this->data(), this->size()).ends_with(__x); } @@ -3038,6 +3040,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION contains(_CharT __x) const noexcept { return __sv_type(this->data(), this->size()).contains(__x); } + [[__gnu__::__nonnull__]] bool contains(const _CharT* __x) const noexcept { return __sv_type(this->data(), this->size()).contains(__x); } diff --git a/libstdc++-v3/include/std/string_view b/libstdc++-v3/include/std/string_view index 30ff136b1cb..5b96ffeecd6 100644 --- a/libstdc++-v3/include/std/string_view +++ b/libstdc++-v3/include/std/string_view @@ -360,6 +360,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION starts_with(_CharT __x) const noexcept { return !this->empty() && traits_type::eq(this->front(), __x); } + [[__gnu__::__nonnull__]] constexpr bool starts_with(const _CharT* __x) const noexcept { return this->starts_with(basic_string_view(__x)); } @@ -377,6 +378,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ends_with(_CharT __x) const noexcept { return !this->empty() && traits_type::eq(this->back(), __x); } + [[__gnu__::__nonnull__]] constexpr bool ends_with(const _CharT* __x) const noexcept { return this->ends_with(basic_string_view(__x)); } @@ -392,6 +394,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION contains(_CharT __x) const noexcept { return this->find(__x) != npos; } + [[__gnu__::__nonnull__]] constexpr bool contains(const _CharT* __x) const noexcept { return this->find(__x) != npos; }