diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2022-08-26 09:43:32 +0100 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2022-08-26 15:29:03 +0100 |
commit | 1b0b969df794d0401ccb9279d44302d1e4729f15 (patch) | |
tree | cabdb1b04d535e5768e7531544ba3764c312f1a8 /libstdc++-v3 | |
parent | dad2d3e003f1a9885cb1fa0f67baf50f62d57b06 (diff) | |
download | gcc-1b0b969df794d0401ccb9279d44302d1e4729f15.zip gcc-1b0b969df794d0401ccb9279d44302d1e4729f15.tar.gz gcc-1b0b969df794d0401ccb9279d44302d1e4729f15.tar.bz2 |
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.
* testsuite/21_strings/basic_string/operations/contains/nonnull.cc
* testsuite/21_strings/basic_string/operations/ends_with/nonnull.cc
* testsuite/21_strings/basic_string/operations/starts_with/nonnull.cc
* testsuite/21_strings/basic_string_view/operations/contains/nonnull.cc
* testsuite/21_strings/basic_string_view/operations/ends_with/nonnull.cc
* testsuite/21_strings/basic_string_view/operations/starts_with/nonnull.cc
Diffstat (limited to 'libstdc++-v3')
9 files changed, 81 insertions, 0 deletions
diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index b04fba9..9d8b415 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 f16e33a..f5f0333 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 30ff136..5b96ffe 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; } diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/operations/contains/nonnull.cc b/libstdc++-v3/testsuite/21_strings/basic_string/operations/contains/nonnull.cc new file mode 100644 index 0000000..e987cb7 --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string/operations/contains/nonnull.cc @@ -0,0 +1,12 @@ +// { dg-options "-std=gnu++23 -Wnonnull -O0" } +// { dg-do compile { target c++23 } } + +#include <string> + +void +test01(const std::string& s) +{ + s.contains((const char*)nullptr); // { dg-warning "\\\[-Wnonnull" } + s.contains((char*)nullptr); // { dg-warning "\\\[-Wnonnull" } + s.contains(nullptr); // { dg-warning "\\\[-Wnonnull" } +} diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/operations/ends_with/nonnull.cc b/libstdc++-v3/testsuite/21_strings/basic_string/operations/ends_with/nonnull.cc new file mode 100644 index 0000000..1f2a156 --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string/operations/ends_with/nonnull.cc @@ -0,0 +1,12 @@ +// { dg-options "-std=gnu++20 -Wnonnull -O0" } +// { dg-do compile { target c++20 } } + +#include <string> + +void +test01(const std::string& s) +{ + s.ends_with((const char*)nullptr); // { dg-warning "\\\[-Wnonnull" } + s.ends_with((char*)nullptr); // { dg-warning "\\\[-Wnonnull" } + s.ends_with(nullptr); // { dg-warning "\\\[-Wnonnull" } +} diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/operations/starts_with/nonnull.cc b/libstdc++-v3/testsuite/21_strings/basic_string/operations/starts_with/nonnull.cc new file mode 100644 index 0000000..8514359 --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string/operations/starts_with/nonnull.cc @@ -0,0 +1,12 @@ +// { dg-options "-std=gnu++20 -Wnonnull -O0" } +// { dg-do compile { target c++20 } } + +#include <string> + +void +test01(const std::string& s) +{ + s.starts_with((const char*)nullptr); // { dg-warning "\\\[-Wnonnull" } + s.starts_with((char*)nullptr); // { dg-warning "\\\[-Wnonnull" } + s.starts_with(nullptr); // { dg-warning "\\\[-Wnonnull" } +} diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/operations/contains/nonnull.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/operations/contains/nonnull.cc new file mode 100644 index 0000000..c0d274f --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/operations/contains/nonnull.cc @@ -0,0 +1,12 @@ +// { dg-options "-std=gnu++23 -Wnonnull -O0" } +// { dg-do compile { target c++23 } } + +#include <string_view> + +void +test01(std::string_view s) +{ + s.contains((const char*)nullptr); // { dg-warning "\\\[-Wnonnull" } + s.contains((char*)nullptr); // { dg-warning "\\\[-Wnonnull" } + s.contains(nullptr); // { dg-warning "\\\[-Wnonnull" } +} diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/operations/ends_with/nonnull.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/operations/ends_with/nonnull.cc new file mode 100644 index 0000000..09a9997 --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/operations/ends_with/nonnull.cc @@ -0,0 +1,12 @@ +// { dg-options "-std=gnu++20 -Wnonnull -O0" } +// { dg-do compile { target c++20 } } + +#include <string_view> + +void +test01(std::string_view s) +{ + s.ends_with((const char*)nullptr); // { dg-warning "\\\[-Wnonnull" } + s.ends_with((char*)nullptr); // { dg-warning "\\\[-Wnonnull" } + s.ends_with(nullptr); // { dg-warning "\\\[-Wnonnull" } +} diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/operations/starts_with/nonnull.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/operations/starts_with/nonnull.cc new file mode 100644 index 0000000..c47f1da --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/operations/starts_with/nonnull.cc @@ -0,0 +1,12 @@ +// { dg-options "-std=gnu++20 -Wnonnull -O0" } +// { dg-do compile { target c++20 } } + +#include <string_view> + +void +test01(std::string_view s) +{ + s.starts_with((const char*)nullptr); // { dg-warning "\\\[-Wnonnull" } + s.starts_with((char*)nullptr); // { dg-warning "\\\[-Wnonnull" } + s.starts_with(nullptr); // { dg-warning "\\\[-Wnonnull" } +} |