Re: [committed 2/2] libstdc++: Add [[nodiscard]] to sequence containers
On 04/08/21 13:00 +0100, Jonathan Wakely wrote: On 04/08/21 12:56 +0100, Jonathan Wakely wrote: ... and container adaptors. This adds the [[nodiscard]] attribute to functions with no side-effects for the sequence containers and their iterators, and the debug versions of those containers, and the container adaptors, I don't plan to add any more [[nodiscard]] attributes for now, but these two commits should demonstrate how to do it for anybody who wants to contribute similar patches. OK, one more change in this vein, adding [[nodiscard]] to . Tested powerpc64le-linux, committed to trunk. commit 8dec72aeb54e98643c0fb3d53768cdb96cf1342a Author: Jonathan Wakely Date: Thu Aug 5 14:01:31 2021 libstdc++: Add [[nodiscard]] to This adds the [[nodiscard]] attribute to all conversion operators, comparison operators, call operators and non-member functions in . Nothing in this header except constructors has side effects. Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: * libsupc++/compare (partial_ordering, weak_ordering) (strong_ordering, is_eq, is_neq, is_lt, is_lteq, is_gt, is_gteq) (compare_three_way, strong_order, weak_order, partial_order) (compare_strong_order_fallback, compare_weak_order_fallback) (compare_partial_order_fallback, __detail::__synth3way): Add nodiscard attribute. * testsuite/18_support/comparisons/categories/zero_neg.cc: Add -Wno-unused-result to options. diff --git a/libstdc++-v3/libsupc++/compare b/libstdc++-v3/libsupc++/compare index dd0ec5fa36d..faeff641437 100644 --- a/libstdc++-v3/libsupc++/compare +++ b/libstdc++-v3/libsupc++/compare @@ -86,49 +86,61 @@ namespace std static const partial_ordering unordered; // comparisons +[[nodiscard]] friend constexpr bool operator==(partial_ordering __v, __cmp_cat::__unspec) noexcept { return __v._M_value == 0; } +[[nodiscard]] friend constexpr bool operator==(partial_ordering, partial_ordering) noexcept = default; +[[nodiscard]] friend constexpr bool operator< (partial_ordering __v, __cmp_cat::__unspec) noexcept { return __v._M_value == -1; } +[[nodiscard]] friend constexpr bool operator> (partial_ordering __v, __cmp_cat::__unspec) noexcept { return __v._M_value == 1; } +[[nodiscard]] friend constexpr bool operator<=(partial_ordering __v, __cmp_cat::__unspec) noexcept { return __v._M_value <= 0; } +[[nodiscard]] friend constexpr bool operator>=(partial_ordering __v, __cmp_cat::__unspec) noexcept { return __cmp_cat::type(__v._M_value & 1) == __v._M_value; } +[[nodiscard]] friend constexpr bool operator< (__cmp_cat::__unspec, partial_ordering __v) noexcept { return __v._M_value == 1; } +[[nodiscard]] friend constexpr bool operator> (__cmp_cat::__unspec, partial_ordering __v) noexcept { return __v._M_value == -1; } +[[nodiscard]] friend constexpr bool operator<=(__cmp_cat::__unspec, partial_ordering __v) noexcept { return __cmp_cat::type(__v._M_value & 1) == __v._M_value; } +[[nodiscard]] friend constexpr bool operator>=(__cmp_cat::__unspec, partial_ordering __v) noexcept { return 0 >= __v._M_value; } +[[nodiscard]] friend constexpr partial_ordering operator<=>(partial_ordering __v, __cmp_cat::__unspec) noexcept { return __v; } +[[nodiscard]] friend constexpr partial_ordering operator<=>(__cmp_cat::__unspec, partial_ordering __v) noexcept { @@ -168,53 +180,66 @@ namespace std static const weak_ordering equivalent; static const weak_ordering greater; +[[nodiscard]] constexpr operator partial_ordering() const noexcept { return partial_ordering(__cmp_cat::_Ord(_M_value)); } // comparisons +[[nodiscard]] friend constexpr bool operator==(weak_ordering __v, __cmp_cat::__unspec) noexcept { return __v._M_value == 0; } +[[nodiscard]] friend constexpr bool operator==(weak_ordering, weak_ordering) noexcept = default; +[[nodiscard]] friend constexpr bool operator< (weak_ordering __v, __cmp_cat::__unspec) noexcept { return __v._M_value < 0; } +[[nodiscard]] friend constexpr bool operator> (weak_ordering __v, __cmp_cat::__unspec) noexcept { return __v._M_value > 0; } +[[nodiscard]] friend constexpr bool operator<=(weak_ordering __v, __cmp_cat::__unspec) noexcept { return __v._M_value <= 0; } +[[nodiscard]] friend constexpr bool operator>=(weak_ordering __v, __cmp_cat::__unspec) noexcept { return __v._M_value >= 0; } +[[nodiscard]] friend constexpr bool operator< (__cmp_cat::__unspec, weak_ordering __v) noexcept { return 0 < __v._M_value; } +[[nodiscard]] friend constexpr
Re: [committed 2/2] libstdc++: Add [[nodiscard]] to sequence containers
On Thu, 5 Aug 2021 at 13:14, Ville Voutilainen wrote: > > On Thu, 5 Aug 2021 at 15:11, Christophe Lyon via Libstdc++ > wrote: > > > > Hi Jonathan, > > > > On Wed, Aug 4, 2021 at 2:04 PM Jonathan Wakely via Gcc-patches < > > gcc-patches@gcc.gnu.org> wrote: > > > > > On 04/08/21 12:56 +0100, Jonathan Wakely wrote: > > > >... and container adaptors. > > > > > > > >This adds the [[nodiscard]] attribute to functions with no side-effects > > > >for the sequence containers and their iterators, and the debug versions > > > >of those containers, and the container adaptors, > > > > > > I don't plan to add any more [[nodiscard]] attributes for now, but > > > these two commits should demonstrate how to do it for anybody who > > > wants to contribute similar patches. > > > > > > I didn't add tests that verify we do actually warn on each of those > > > functions, because there are hundreds of them, and I know they're > > > working because I had to alter existing tests to not warn. > > > > > > > > I've noticed a regression on aarch64/arm: > > FAIL: g++.old-deja/g++.other/inline7.C -std=gnu++17 (test for excess > > errors) > > Excess errors: > > /gcc/testsuite/g++.old-deja/g++.other/inline7.C:11:11: warning: ignoring > > return value of 'std::__cxx11::list<_Tp, _Alloc>::size_type > > std::__cxx11::list<_Tp, _Alloc>::size() const [with _Tp = int*; _Alloc = > > std::allocator; std::__cxx11::list<_Tp, _Alloc>::size_type = long > > unsigned int]', declared with attribute 'nodiscard' [-Wunused-result] > > > > FAIL: g++.old-deja/g++.other/inline7.C -std=gnu++2a (test for excess > > errors) > > Excess errors: > > /gcc/testsuite/g++.old-deja/g++.other/inline7.C:11:11: warning: ignoring > > return value of 'std::__cxx11::list<_Tp, _Alloc>::size_type > > std::__cxx11::list<_Tp, _Alloc>::size() const [with _Tp = int*; _Alloc = > > std::allocator; std::__cxx11::list<_Tp, _Alloc>::size_type = long > > unsigned int]', declared with attribute 'nodiscard' [-Wunused-result] > > > > Not sure why you didn't see it? > > That can easily happen when running just the library tests, rather > than all of them. :P Right, I didn't run all the compiler tests. Fixed with this patch, tested x86_64-linux, pushed to trunk. commit 03d47da7e1e91adddbde261ffefd2760df59a564 Author: Jonathan Wakely Date: Thu Aug 5 14:00:35 2021 testsuite: Fix warning introduced by nodiscard in libstdc++ Signed-off-by: Jonathan Wakely gcc/testsuite/ChangeLog: * g++.old-deja/g++.other/inline7.C: Cast nodiscard call to void. diff --git a/gcc/testsuite/g++.old-deja/g++.other/inline7.C b/gcc/testsuite/g++.old-deja/g++.other/inline7.C index a3723cfba1e..62639c5 100644 --- a/gcc/testsuite/g++.old-deja/g++.other/inline7.C +++ b/gcc/testsuite/g++.old-deja/g++.other/inline7.C @@ -8,7 +8,7 @@ std::list li; void f () { - li.size (); + (void) li.size (); } int main ()
Re: [committed 2/2] libstdc++: Add [[nodiscard]] to sequence containers
On Thu, 5 Aug 2021 at 15:11, Christophe Lyon via Libstdc++ wrote: > > Hi Jonathan, > > On Wed, Aug 4, 2021 at 2:04 PM Jonathan Wakely via Gcc-patches < > gcc-patches@gcc.gnu.org> wrote: > > > On 04/08/21 12:56 +0100, Jonathan Wakely wrote: > > >... and container adaptors. > > > > > >This adds the [[nodiscard]] attribute to functions with no side-effects > > >for the sequence containers and their iterators, and the debug versions > > >of those containers, and the container adaptors, > > > > I don't plan to add any more [[nodiscard]] attributes for now, but > > these two commits should demonstrate how to do it for anybody who > > wants to contribute similar patches. > > > > I didn't add tests that verify we do actually warn on each of those > > functions, because there are hundreds of them, and I know they're > > working because I had to alter existing tests to not warn. > > > > > I've noticed a regression on aarch64/arm: > FAIL: g++.old-deja/g++.other/inline7.C -std=gnu++17 (test for excess > errors) > Excess errors: > /gcc/testsuite/g++.old-deja/g++.other/inline7.C:11:11: warning: ignoring > return value of 'std::__cxx11::list<_Tp, _Alloc>::size_type > std::__cxx11::list<_Tp, _Alloc>::size() const [with _Tp = int*; _Alloc = > std::allocator; std::__cxx11::list<_Tp, _Alloc>::size_type = long > unsigned int]', declared with attribute 'nodiscard' [-Wunused-result] > > FAIL: g++.old-deja/g++.other/inline7.C -std=gnu++2a (test for excess > errors) > Excess errors: > /gcc/testsuite/g++.old-deja/g++.other/inline7.C:11:11: warning: ignoring > return value of 'std::__cxx11::list<_Tp, _Alloc>::size_type > std::__cxx11::list<_Tp, _Alloc>::size() const [with _Tp = int*; _Alloc = > std::allocator; std::__cxx11::list<_Tp, _Alloc>::size_type = long > unsigned int]', declared with attribute 'nodiscard' [-Wunused-result] > > Not sure why you didn't see it? That can easily happen when running just the library tests, rather than all of them. :P
Re: [committed 2/2] libstdc++: Add [[nodiscard]] to sequence containers
Hi Jonathan, On Wed, Aug 4, 2021 at 2:04 PM Jonathan Wakely via Gcc-patches < gcc-patches@gcc.gnu.org> wrote: > On 04/08/21 12:56 +0100, Jonathan Wakely wrote: > >... and container adaptors. > > > >This adds the [[nodiscard]] attribute to functions with no side-effects > >for the sequence containers and their iterators, and the debug versions > >of those containers, and the container adaptors, > > I don't plan to add any more [[nodiscard]] attributes for now, but > these two commits should demonstrate how to do it for anybody who > wants to contribute similar patches. > > I didn't add tests that verify we do actually warn on each of those > functions, because there are hundreds of them, and I know they're > working because I had to alter existing tests to not warn. > > I've noticed a regression on aarch64/arm: FAIL: g++.old-deja/g++.other/inline7.C -std=gnu++17 (test for excess errors) Excess errors: /gcc/testsuite/g++.old-deja/g++.other/inline7.C:11:11: warning: ignoring return value of 'std::__cxx11::list<_Tp, _Alloc>::size_type std::__cxx11::list<_Tp, _Alloc>::size() const [with _Tp = int*; _Alloc = std::allocator; std::__cxx11::list<_Tp, _Alloc>::size_type = long unsigned int]', declared with attribute 'nodiscard' [-Wunused-result] FAIL: g++.old-deja/g++.other/inline7.C -std=gnu++2a (test for excess errors) Excess errors: /gcc/testsuite/g++.old-deja/g++.other/inline7.C:11:11: warning: ignoring return value of 'std::__cxx11::list<_Tp, _Alloc>::size_type std::__cxx11::list<_Tp, _Alloc>::size() const [with _Tp = int*; _Alloc = std::allocator; std::__cxx11::list<_Tp, _Alloc>::size_type = long unsigned int]', declared with attribute 'nodiscard' [-Wunused-result] Not sure why you didn't see it? Christophe
Re: [committed 2/2] libstdc++: Add [[nodiscard]] to sequence containers
On 04/08/21 12:56 +0100, Jonathan Wakely wrote: ... and container adaptors. This adds the [[nodiscard]] attribute to functions with no side-effects for the sequence containers and their iterators, and the debug versions of those containers, and the container adaptors, I don't plan to add any more [[nodiscard]] attributes for now, but these two commits should demonstrate how to do it for anybody who wants to contribute similar patches. I didn't add tests that verify we do actually warn on each of those functions, because there are hundreds of them, and I know they're working because I had to alter existing tests to not warn.
[committed 2/2] libstdc++: Add [[nodiscard]] to sequence containers
... and container adaptors. This adds the [[nodiscard]] attribute to functions with no side-effects for the sequence containers and their iterators, and the debug versions of those containers, and the container adaptors, Tested powerpc64le-linux, committed to trunk. commit 0d04fe49239d91787850036599164788f1c87785 Author: Jonathan Wakely Date: Tue Aug 3 20:50:52 2021 libstdc++: Add [[nodiscard]] to sequence containers ... and container adaptors. This adds the [[nodiscard]] attribute to functions with no side-effects for the sequence containers and their iterators, and the debug versions of those containers, and the container adaptors, Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: * include/bits/forward_list.h: Add [[nodiscard]] to functions with no side-effects. * include/bits/stl_bvector.h: Likewise. * include/bits/stl_deque.h: Likewise. * include/bits/stl_list.h: Likewise. * include/bits/stl_queue.h: Likewise. * include/bits/stl_stack.h: Likewise. * include/bits/stl_vector.h: Likewise. * include/debug/deque: Likewise. * include/debug/forward_list: Likewise. * include/debug/list: Likewise. * include/debug/safe_iterator.h: Likewise. * include/debug/vector: Likewise. * include/std/array: Likewise. * testsuite/23_containers/array/creation/3_neg.cc: Use -Wno-unused-result. * testsuite/23_containers/array/debug/back1_neg.cc: Cast result to void. * testsuite/23_containers/array/debug/back2_neg.cc: Likewise. * testsuite/23_containers/array/debug/front1_neg.cc: Likewise. * testsuite/23_containers/array/debug/front2_neg.cc: Likewise. * testsuite/23_containers/array/debug/square_brackets_operator1_neg.cc: Likewise. * testsuite/23_containers/array/debug/square_brackets_operator2_neg.cc: Likewise. * testsuite/23_containers/array/tuple_interface/get_neg.cc: Adjust dg-error line numbers. * testsuite/23_containers/deque/cons/clear_allocator.cc: Cast result to void. * testsuite/23_containers/deque/debug/invalidation/4.cc: Likewise. * testsuite/23_containers/deque/types/1.cc: Use -Wno-unused-result. * testsuite/23_containers/list/types/1.cc: Cast result to void. * testsuite/23_containers/priority_queue/members/7161.cc: Likewise. * testsuite/23_containers/queue/members/7157.cc: Likewise. * testsuite/23_containers/vector/59829.cc: Likewise. * testsuite/23_containers/vector/ext_pointer/types/1.cc: Likewise. * testsuite/23_containers/vector/ext_pointer/types/2.cc: Likewise. * testsuite/23_containers/vector/types/1.cc: Use -Wno-unused-result. diff --git a/libstdc++-v3/include/bits/forward_list.h b/libstdc++-v3/include/bits/forward_list.h index e61746848f6..ab6d9389194 100644 --- a/libstdc++-v3/include/bits/forward_list.h +++ b/libstdc++-v3/include/bits/forward_list.h @@ -150,10 +150,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER _Fwd_list_iterator(_Fwd_list_node_base* __n) noexcept : _M_node(__n) { } + [[__nodiscard__]] reference operator*() const noexcept { return *static_cast<_Node*>(this->_M_node)->_M_valptr(); } + [[__nodiscard__]] pointer operator->() const noexcept { return static_cast<_Node*>(this->_M_node)->_M_valptr(); } @@ -176,6 +178,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER /** * @brief Forward list iterator equality comparison. */ + [[__nodiscard__]] friend bool operator==(const _Self& __x, const _Self& __y) noexcept { return __x._M_node == __y._M_node; } @@ -184,6 +187,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER /** * @brief Forward list iterator inequality comparison. */ + [[__nodiscard__]] friend bool operator!=(const _Self& __x, const _Self& __y) noexcept { return __x._M_node != __y._M_node; } @@ -229,10 +233,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER _Fwd_list_const_iterator(const iterator& __iter) noexcept : _M_node(__iter._M_node) { } + [[__nodiscard__]] reference operator*() const noexcept { return *static_cast<_Node*>(this->_M_node)->_M_valptr(); } + [[__nodiscard__]] pointer operator->() const noexcept { return static_cast<_Node*>(this->_M_node)->_M_valptr(); } @@ -255,6 +261,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER /** * @brief Forward list const_iterator equality comparison. */ + [[__nodiscard__]] friend bool operator==(const _Self& __x, const