aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2018-06-15 17:47:55 +0100
committerJonathan Wakely <redi@gcc.gnu.org>2018-06-15 17:47:55 +0100
commit36eec25a6fa7c32e36d7f77f85c8acb7ff38a66b (patch)
tree5141ae4527a55652b497904a61bf3dc2f23ff62a
parent3438bf7b9f5ce1cfbb7e2419da25e72f109ea480 (diff)
downloadgcc-36eec25a6fa7c32e36d7f77f85c8acb7ff38a66b.zip
gcc-36eec25a6fa7c32e36d7f77f85c8acb7ff38a66b.tar.gz
gcc-36eec25a6fa7c32e36d7f77f85c8acb7ff38a66b.tar.bz2
Decorate string_view members with nonnull attribute
The C++ committee has confirmed that passing a null pointer to the unary basic_string_view constructor is undefined. This removes the check from our implementation, and adds the nonnull attribute to warn when the compiler can detect undefined input. * include/std/string_view (basic_string_view(const CharT*)): Remove check for null pointer and add nonnull attribute. (compare(const CharT*), compare(size_type, size_type, const CharT*)) (find(const CharT*, size_type), rfind(const CharT*, size_type)) (find_first_of(const CharT*, size_type)) (find_last_of(const CharT*, size_type)) (find_first_not_of(const CharT*, size_type)) (find_last_not_of(const CharT*, size_type)): Add nonnull attribute. * testsuite/21_strings/basic_string_view/cons/char/nonnull.cc: New. * testsuite/21_strings/basic_string_view/operations/compare/char/ nonnull.cc: New. * testsuite/21_strings/basic_string_view/operations/find/char/ nonnull.cc: New. * testsuite/21_strings/basic_string_view/operations/rfind/char/ nonnull.cc: New. From-SVN: r261638
-rw-r--r--libstdc++-v3/ChangeLog16
-rw-r--r--libstdc++-v3/include/std/string_view24
-rw-r--r--libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/nonnull.cc29
-rw-r--r--libstdc++-v3/testsuite/21_strings/basic_string_view/cons/wchar_t/nonnull.cc29
-rw-r--r--libstdc++-v3/testsuite/21_strings/basic_string_view/operations/compare/char/nonnull.cc29
-rw-r--r--libstdc++-v3/testsuite/21_strings/basic_string_view/operations/find/char/nonnull.cc33
-rw-r--r--libstdc++-v3/testsuite/21_strings/basic_string_view/operations/rfind/char/nonnull.cc29
7 files changed, 178 insertions, 11 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
index 482f64e..b7cef60 100644
--- a/libstdc++-v3/ChangeLog
+++ b/libstdc++-v3/ChangeLog
@@ -1,5 +1,21 @@
2018-06-15 Jonathan Wakely <jwakely@redhat.com>
+ * include/std/string_view (basic_string_view(const CharT*)): Remove
+ check for null pointer and add nonnull attribute.
+ (compare(const CharT*), compare(size_type, size_type, const CharT*))
+ (find(const CharT*, size_type), rfind(const CharT*, size_type))
+ (find_first_of(const CharT*, size_type))
+ (find_last_of(const CharT*, size_type))
+ (find_first_not_of(const CharT*, size_type))
+ (find_last_not_of(const CharT*, size_type)): Add nonnull attribute.
+ * testsuite/21_strings/basic_string_view/cons/char/nonnull.cc: New.
+ * testsuite/21_strings/basic_string_view/operations/compare/char/
+ nonnull.cc: New.
+ * testsuite/21_strings/basic_string_view/operations/find/char/
+ nonnull.cc: New.
+ * testsuite/21_strings/basic_string_view/operations/rfind/char/
+ nonnull.cc: New.
+
PR libstdc++/86168
* include/bits/random.h (random_device(const string&)): Remove
default argument.
diff --git a/libstdc++-v3/include/std/string_view b/libstdc++-v3/include/std/string_view
index 9f39df8..f84664c 100644
--- a/libstdc++-v3/include/std/string_view
+++ b/libstdc++-v3/include/std/string_view
@@ -96,8 +96,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
constexpr basic_string_view(const basic_string_view&) noexcept = default;
- constexpr basic_string_view(const _CharT* __str) noexcept
- : _M_len{__str == nullptr ? 0 : traits_type::length(__str)},
+ __attribute__((__nonnull__)) constexpr
+ basic_string_view(const _CharT* __str) noexcept
+ : _M_len{traits_type::length(__str)},
_M_str{__str}
{ }
@@ -270,11 +271,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
}
- constexpr int
+ __attribute__((__nonnull__)) constexpr int
compare(const _CharT* __str) const noexcept
{ return this->compare(basic_string_view{__str}); }
- constexpr int
+ __attribute__((__nonnull__)) constexpr int
compare(size_type __pos1, size_type __n1, const _CharT* __str) const
{ return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
@@ -296,7 +297,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
constexpr size_type
find(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
- constexpr size_type
+ __attribute__((__nonnull__)) constexpr size_type
find(const _CharT* __str, size_type __pos = 0) const noexcept
{ return this->find(__str, __pos, traits_type::length(__str)); }
@@ -310,7 +311,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
constexpr size_type
rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
- constexpr size_type
+ __attribute__((__nonnull__)) constexpr size_type
rfind(const _CharT* __str, size_type __pos = npos) const noexcept
{ return this->rfind(__str, __pos, traits_type::length(__str)); }
@@ -323,9 +324,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ return this->find(__c, __pos); }
constexpr size_type
- find_first_of(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
+ find_first_of(const _CharT* __str, size_type __pos,
+ size_type __n) const noexcept;
- constexpr size_type
+ __attribute__((__nonnull__)) constexpr size_type
find_first_of(const _CharT* __str, size_type __pos = 0) const noexcept
{ return this->find_first_of(__str, __pos, traits_type::length(__str)); }
@@ -342,7 +344,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
find_last_of(const _CharT* __str, size_type __pos,
size_type __n) const noexcept;
- constexpr size_type
+ __attribute__((__nonnull__)) constexpr size_type
find_last_of(const _CharT* __str, size_type __pos = npos) const noexcept
{ return this->find_last_of(__str, __pos, traits_type::length(__str)); }
@@ -358,7 +360,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
find_first_not_of(const _CharT* __str,
size_type __pos, size_type __n) const noexcept;
- constexpr size_type
+ __attribute__((__nonnull__)) constexpr size_type
find_first_not_of(const _CharT* __str, size_type __pos = 0) const noexcept
{
return this->find_first_not_of(__str, __pos,
@@ -377,7 +379,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
find_last_not_of(const _CharT* __str,
size_type __pos, size_type __n) const noexcept;
- constexpr size_type
+ __attribute__((__nonnull__)) constexpr size_type
find_last_not_of(const _CharT* __str,
size_type __pos = npos) const noexcept
{
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/nonnull.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/nonnull.cc
new file mode 100644
index 0000000..463ece7
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/nonnull.cc
@@ -0,0 +1,29 @@
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++17 -Wnonnull" }
+// { dg-do compile { target c++17 } }
+
+#include <string_view>
+
+void
+test01()
+{
+ std::string_view s((const char*)nullptr); // { dg-warning "null arg" }
+ std::string_view t((char*)nullptr); // { dg-warning "null arg" }
+ std::string_view u(nullptr); // { dg-warning "null arg" }
+}
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/wchar_t/nonnull.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/wchar_t/nonnull.cc
new file mode 100644
index 0000000..8df6142
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/wchar_t/nonnull.cc
@@ -0,0 +1,29 @@
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++17 -Wnonnull" }
+// { dg-do compile { target c++17 } }
+
+#include <string_view>
+
+void
+test01()
+{
+ std::wstring_view s((const wchar_t*)nullptr); // { dg-warning "null arg" }
+ std::wstring_view t((wchar_t*)nullptr); // { dg-warning "null arg" }
+ std::wstring_view u(nullptr); // { dg-warning "null arg" }
+}
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/operations/compare/char/nonnull.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/operations/compare/char/nonnull.cc
new file mode 100644
index 0000000..1d12b70
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/operations/compare/char/nonnull.cc
@@ -0,0 +1,29 @@
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++17 -Wnonnull" }
+// { dg-do compile { target c++17 } }
+
+#include <string_view>
+
+int
+test01()
+{
+ std::string_view s = "abcd";
+ return s.compare((const char*)nullptr); // { dg-warning "null arg" }
+ return s.compare(0, 2, (const char*)nullptr); // { dg-warning "null arg" }
+}
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/operations/find/char/nonnull.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/operations/find/char/nonnull.cc
new file mode 100644
index 0000000..69f1850
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/operations/find/char/nonnull.cc
@@ -0,0 +1,33 @@
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++17 -Wnonnull" }
+// { dg-do compile { target c++17 } }
+
+#include <string_view>
+
+int
+test01()
+{
+ std::string_view s = "abcd";
+ return s.find((const char*)nullptr); // { dg-warning "null arg" }
+ return s.find((const char*)nullptr, 1); // { dg-warning "null arg" }
+ return s.find_first_of((const char*)nullptr); // { dg-warning "null arg" }
+ return s.find_first_of((const char*)nullptr, 1); // { dg-warning "null arg" }
+ return s.find_first_not_of((const char*)nullptr); // { dg-warning "null arg" }
+ return s.find_first_not_of((const char*)nullptr, 1); // { dg-warning "null arg" }
+}
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/operations/rfind/char/nonnull.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/operations/rfind/char/nonnull.cc
new file mode 100644
index 0000000..ca8527a
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/operations/rfind/char/nonnull.cc
@@ -0,0 +1,29 @@
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++17 -Wnonnull" }
+// { dg-do compile { target c++17 } }
+
+#include <string_view>
+
+int
+test01()
+{
+ std::string_view s = "abcd";
+ return s.rfind((const char*)nullptr); // { dg-warning "null arg" }
+ return s.rfind((const char*)nullptr, 1); // { dg-warning "null arg" }
+}