diff mbox series

Add [[nodiscard]] attribute to C++17 components

Message ID 20171123224048.GA24885@redhat.com
State New
Headers show
Series Add [[nodiscard]] attribute to C++17 components | expand

Commit Message

Jonathan Wakely Nov. 23, 2017, 10:40 p.m. UTC
C++17 added the [[nodiscard]] attribute, similar to GCC's
warn_unused_result. The C++2a draft requires it to be used in several
places. This patch adds it where required on components which are new
to C++17, which are still experimental and so I'm changing them in
stage 3.

	* include/bits/fs_path.h (path::empty): Add nodiscard attribute.
	* include/bits/range_access.h (empty): Likewise.
	* include/std/string_view (basic_string_view::empty): Likewise.
	* testsuite/21_strings/basic_string_view/capacity/empty_neg.cc: New
	test.
	* testsuite/24_iterators/range_access_cpp17_neg.cc: New test.
	* testsuite/27_io/filesystem/path/query/empty_neg.cc: New test.

Tested powerpc64le-linux, committed to trunk.
commit b5d8dfbb291b02c071173612c63fd806e844e191
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Nov 23 21:39:30 2017 +0000

    Add [[nodiscard]] attribute to C++17 components
    
            * include/bits/fs_path.h (path::empty): Add nodiscard attribute.
            * include/bits/range_access.h (empty): Likewise.
            * include/std/string_view (basic_string_view::empty): Likewise.
            * testsuite/21_strings/basic_string_view/capacity/empty_neg.cc: New
            test.
            * testsuite/24_iterators/range_access_cpp17_neg.cc: New test.
            * testsuite/27_io/filesystem/path/query/empty_neg.cc: New test.

Comments

Jonathan Wakely Nov. 23, 2017, 10:51 p.m. UTC | #1
On 23/11/17 22:40 +0000, Jonathan Wakely wrote:
>C++17 added the [[nodiscard]] attribute, similar to GCC's
>warn_unused_result. The C++2a draft requires it to be used in several
>places. This patch adds it where required on components which are new
>to C++17, which are still experimental and so I'm changing them in
>stage 3.

This adds the other [[nodiscard]] attributes required by C++2a, but
using [[__nodiscard__]] so it can be enabled in C++11 and C++14, where
nodiscard is not a reserved name.

I don't plan to commit this until stage 1 (and don't have tests for it
yet anyway).
commit 9159c87e19510662eecfeadf8facbe68b40ac2c7
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Nov 23 22:42:45 2017 +0000

    Add more nodiscard attributes

diff --git a/libstdc++-v3/include/bits/alloc_traits.h b/libstdc++-v3/include/bits/alloc_traits.h
index 86a48594ea1..ba75f33726d 100644
--- a/libstdc++-v3/include/bits/alloc_traits.h
+++ b/libstdc++-v3/include/bits/alloc_traits.h
@@ -296,7 +296,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *
        *  Calls @c a.allocate(n)
       */
-      static pointer
+      [[__nodiscard__]] static pointer
       allocate(_Alloc& __a, size_type __n)
       { return __a.allocate(__n); }
 
@@ -311,7 +311,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  Returns <tt> a.allocate(n, hint) </tt> if that expression is
        *  well-formed, otherwise returns @c a.allocate(n)
       */
-      static pointer
+      [[__nodiscard__]] static pointer
       allocate(_Alloc& __a, size_type __n, const_void_pointer __hint)
       { return _S_allocate(__a, __n, __hint, 0); }
 
diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h
index a4b81137571..bec6e902076 100644
--- a/libstdc++-v3/include/bits/basic_string.h
+++ b/libstdc++-v3/include/bits/basic_string.h
@@ -1008,7 +1008,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
        *  Returns true if the %string is empty.  Equivalent to 
        *  <code>*this == ""</code>.
        */
-      bool
+      _GLIBCXX_NODISCARD bool
       empty() const _GLIBCXX_NOEXCEPT
       { return this->size() == 0; }
 
diff --git a/libstdc++-v3/include/bits/c++config b/libstdc++-v3/include/bits/c++config
index 21e3fbb2741..cfa2534e3f5 100644
--- a/libstdc++-v3/include/bits/c++config
+++ b/libstdc++-v3/include/bits/c++config
@@ -99,6 +99,11 @@
 # define _GLIBCXX_ABI_TAG_CXX11 __attribute ((__abi_tag__ ("cxx11")))
 #endif
 
+#if __cplusplus >= 201103L
+# define _GLIBCXX_NODISCARD [[__nodiscard__]]
+#else
+# define _GLIBCXX_NODISCARD
+#endif
 
 #if __cplusplus
 
diff --git a/libstdc++-v3/include/bits/forward_list.h b/libstdc++-v3/include/bits/forward_list.h
index 96494cc20de..b8eb9f3d6fe 100644
--- a/libstdc++-v3/include/bits/forward_list.h
+++ b/libstdc++-v3/include/bits/forward_list.h
@@ -756,7 +756,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
        *  Returns true if the %forward_list is empty.  (Thus begin() would
        *  equal end().)
        */
-      bool
+      [[__nodiscard__]] bool
       empty() const noexcept
       { return this->_M_impl._M_head._M_next == 0; }
 
diff --git a/libstdc++-v3/include/bits/regex.h b/libstdc++-v3/include/bits/regex.h
index 32e7159eec9..4866cb42b00 100644
--- a/libstdc++-v3/include/bits/regex.h
+++ b/libstdc++-v3/include/bits/regex.h
@@ -1631,7 +1631,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
        * @retval true The %match_results object is empty.
        * @retval false The %match_results object is not empty.
        */
-      bool
+      [[__nodiscard__]] bool
       empty() const
       { return size() == 0; }
 
diff --git a/libstdc++-v3/include/bits/stl_bvector.h b/libstdc++-v3/include/bits/stl_bvector.h
index ac548846b0e..80a54e78ff3 100644
--- a/libstdc++-v3/include/bits/stl_bvector.h
+++ b/libstdc++-v3/include/bits/stl_bvector.h
@@ -873,7 +873,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       { return size_type(const_iterator(this->_M_impl._M_end_addr(), 0)
 			 - begin()); }
 
-      bool
+      _GLIBCXX_NODISCARD bool
       empty() const _GLIBCXX_NOEXCEPT
       { return begin() == end(); }
 
diff --git a/libstdc++-v3/include/bits/stl_deque.h b/libstdc++-v3/include/bits/stl_deque.h
index 93c82ca8909..ae170623247 100644
--- a/libstdc++-v3/include/bits/stl_deque.h
+++ b/libstdc++-v3/include/bits/stl_deque.h
@@ -1363,7 +1363,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
        *  Returns true if the %deque is empty.  (Thus begin() would
        *  equal end().)
        */
-      bool
+      _GLIBCXX_NODISCARD bool
       empty() const _GLIBCXX_NOEXCEPT
       { return this->_M_impl._M_finish == this->_M_impl._M_start; }
 
diff --git a/libstdc++-v3/include/bits/stl_list.h b/libstdc++-v3/include/bits/stl_list.h
index 8ed97f7c8b0..93378139871 100644
--- a/libstdc++-v3/include/bits/stl_list.h
+++ b/libstdc++-v3/include/bits/stl_list.h
@@ -1058,7 +1058,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
        *  Returns true if the %list is empty.  (Thus begin() would equal
        *  end().)
        */
-      bool
+      _GLIBCXX_NODISCARD bool
       empty() const _GLIBCXX_NOEXCEPT
       { return this->_M_impl._M_node._M_next == &this->_M_impl._M_node; }
 
diff --git a/libstdc++-v3/include/bits/stl_map.h b/libstdc++-v3/include/bits/stl_map.h
index bec6c47c086..fec8d81c183 100644
--- a/libstdc++-v3/include/bits/stl_map.h
+++ b/libstdc++-v3/include/bits/stl_map.h
@@ -459,7 +459,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       /** Returns true if the %map is empty.  (Thus begin() would equal
        *  end().)
       */
-      bool
+      _GLIBCXX_NODISCARD bool
       empty() const _GLIBCXX_NOEXCEPT
       { return _M_t.empty(); }
 
diff --git a/libstdc++-v3/include/bits/stl_multimap.h b/libstdc++-v3/include/bits/stl_multimap.h
index 24ac4a3c186..bce79c2d587 100644
--- a/libstdc++-v3/include/bits/stl_multimap.h
+++ b/libstdc++-v3/include/bits/stl_multimap.h
@@ -452,7 +452,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 
       // capacity
       /** Returns true if the %multimap is empty.  */
-      bool
+      _GLIBCXX_NODISCARD bool
       empty() const _GLIBCXX_NOEXCEPT
       { return _M_t.empty(); }
 
diff --git a/libstdc++-v3/include/bits/stl_multiset.h b/libstdc++-v3/include/bits/stl_multiset.h
index ff7d3dca586..aa147467c36 100644
--- a/libstdc++-v3/include/bits/stl_multiset.h
+++ b/libstdc++-v3/include/bits/stl_multiset.h
@@ -406,7 +406,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 #endif
 
       ///  Returns true if the %set is empty.
-      bool
+      _GLIBCXX_NODISCARD bool
       empty() const _GLIBCXX_NOEXCEPT
       { return _M_t.empty(); }
 
diff --git a/libstdc++-v3/include/bits/stl_queue.h b/libstdc++-v3/include/bits/stl_queue.h
index c49f371fbc8..60a2f0e281e 100644
--- a/libstdc++-v3/include/bits/stl_queue.h
+++ b/libstdc++-v3/include/bits/stl_queue.h
@@ -185,7 +185,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       /**
        *  Returns true if the %queue is empty.
        */
-      bool
+      _GLIBCXX_NODISCARD bool
       empty() const
       { return c.empty(); }
 
@@ -563,7 +563,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       /**
        *  Returns true if the %queue is empty.
        */
-      bool
+      _GLIBCXX_NODISCARD bool
       empty() const
       { return c.empty(); }
 
diff --git a/libstdc++-v3/include/bits/stl_set.h b/libstdc++-v3/include/bits/stl_set.h
index e07d78124eb..9f566a2a6eb 100644
--- a/libstdc++-v3/include/bits/stl_set.h
+++ b/libstdc++-v3/include/bits/stl_set.h
@@ -410,7 +410,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 #endif
 
       ///  Returns true if the %set is empty.
-      bool
+      _GLIBCXX_NODISCARD bool
       empty() const _GLIBCXX_NOEXCEPT
       { return _M_t.empty(); }
 
diff --git a/libstdc++-v3/include/bits/stl_stack.h b/libstdc++-v3/include/bits/stl_stack.h
index 5f2b4ab4486..d115134d0fe 100644
--- a/libstdc++-v3/include/bits/stl_stack.h
+++ b/libstdc++-v3/include/bits/stl_stack.h
@@ -181,7 +181,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       /**
        *  Returns true if the %stack is empty.
        */
-      bool
+      _GLIBCXX_NODISCARD bool
       empty() const
       { return c.empty(); }
 
diff --git a/libstdc++-v3/include/bits/stl_vector.h b/libstdc++-v3/include/bits/stl_vector.h
index aeeba820020..75f58e525ae 100644
--- a/libstdc++-v3/include/bits/stl_vector.h
+++ b/libstdc++-v3/include/bits/stl_vector.h
@@ -890,7 +890,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
        *  Returns true if the %vector is empty.  (Thus begin() would
        *  equal end().)
        */
-      bool
+      _GLIBCXX_NODISCARD bool
       empty() const _GLIBCXX_NOEXCEPT
       { return begin() == end(); }
 
diff --git a/libstdc++-v3/include/bits/unordered_map.h b/libstdc++-v3/include/bits/unordered_map.h
index cb5bcb89a16..3400aed47e9 100644
--- a/libstdc++-v3/include/bits/unordered_map.h
+++ b/libstdc++-v3/include/bits/unordered_map.h
@@ -300,7 +300,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       // size and capacity:
 
       ///  Returns true if the %unordered_map is empty.
-      bool
+      [[__nodiscard__]] bool
       empty() const noexcept
       { return _M_h.empty(); }
 
@@ -1433,7 +1433,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       // size and capacity:
 
       ///  Returns true if the %unordered_multimap is empty.
-      bool
+      [[__nodiscard__]] bool
       empty() const noexcept
       { return _M_h.empty(); }
 
diff --git a/libstdc++-v3/include/bits/unordered_set.h b/libstdc++-v3/include/bits/unordered_set.h
index 550548c42db..279b61ce3d6 100644
--- a/libstdc++-v3/include/bits/unordered_set.h
+++ b/libstdc++-v3/include/bits/unordered_set.h
@@ -294,7 +294,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       // size and capacity:
 
       ///  Returns true if the %unordered_set is empty.
-      bool
+      [[__nodiscard__]] bool
       empty() const noexcept
       { return _M_h.empty(); }
 
@@ -1091,7 +1091,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       // size and capacity:
 
       ///  Returns true if the %unordered_multiset is empty.
-      bool
+      [[__nodiscard__]] bool
       empty() const noexcept
       { return _M_h.empty(); }
 
diff --git a/libstdc++-v3/include/experimental/bits/fs_path.h b/libstdc++-v3/include/experimental/bits/fs_path.h
index 3e9bc6357af..81c6ac44dd1 100644
--- a/libstdc++-v3/include/experimental/bits/fs_path.h
+++ b/libstdc++-v3/include/experimental/bits/fs_path.h
@@ -359,7 +359,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
 
     // query
 
-    bool empty() const noexcept { return _M_pathname.empty(); }
+    [[__nodiscard__]] bool
+    empty() const noexcept { return _M_pathname.empty(); }
+
     bool has_root_name() const;
     bool has_root_directory() const;
     bool has_root_path() const;
diff --git a/libstdc++-v3/include/experimental/string_view b/libstdc++-v3/include/experimental/string_view
index 8eaf9ec3d96..bbe7a46791f 100644
--- a/libstdc++-v3/include/experimental/string_view
+++ b/libstdc++-v3/include/experimental/string_view
@@ -169,7 +169,7 @@ inline namespace fundamentals_v1
 		/ sizeof(value_type) / 4;
       }
 
-      constexpr bool
+      [[__nodiscard__]] constexpr bool
       empty() const noexcept
       { return this->_M_len == 0; }
 
diff --git a/libstdc++-v3/include/ext/new_allocator.h b/libstdc++-v3/include/ext/new_allocator.h
index e1e152c4bf0..de18cc7a4f8 100644
--- a/libstdc++-v3/include/ext/new_allocator.h
+++ b/libstdc++-v3/include/ext/new_allocator.h
@@ -95,7 +95,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
       // NB: __n is permitted to be 0.  The C++ standard says nothing
       // about what the return value is when __n == 0.
-      pointer
+      _GLIBCXX_NODISCARD pointer
       allocate(size_type __n, const void* = static_cast<const void*>(0))
       {
 	if (__n > this->max_size())
diff --git a/libstdc++-v3/include/std/array b/libstdc++-v3/include/std/array
index 01f7100bae0..329d3b33a68 100644
--- a/libstdc++-v3/include/std/array
+++ b/libstdc++-v3/include/std/array
@@ -177,7 +177,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       constexpr size_type
       max_size() const noexcept { return _Nm; }
 
-      constexpr bool
+      [[__nodiscard__]] constexpr bool
       empty() const noexcept { return size() == 0; }
 
       // Element access.
diff --git a/libstdc++-v3/include/std/future b/libstdc++-v3/include/std/future
index d9d446bc2f6..6b51499526a 100644
--- a/libstdc++-v3/include/std/future
+++ b/libstdc++-v3/include/std/future
@@ -1708,7 +1708,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
   /// async
   template<typename _Fn, typename... _Args>
-    future<__async_result_of<_Fn, _Args...>>
+    [[__nodiscard__]] future<__async_result_of<_Fn, _Args...>>
     async(launch __policy, _Fn&& __fn, _Args&&... __args)
     {
       std::shared_ptr<__future_base::_State_base> __state;
@@ -1741,7 +1741,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
   /// async, potential overload
   template<typename _Fn, typename... _Args>
-    inline future<__async_result_of<_Fn, _Args...>>
+    [[__nodiscard__]] inline future<__async_result_of<_Fn, _Args...>>
     async(_Fn&& __fn, _Args&&... __args)
     {
       return std::async(launch::async|launch::deferred,
diff --git a/libstdc++-v3/include/std/scoped_allocator b/libstdc++-v3/include/std/scoped_allocator
index 725f5471444..ee955e9fa90 100644
--- a/libstdc++-v3/include/std/scoped_allocator
+++ b/libstdc++-v3/include/std/scoped_allocator
@@ -335,10 +335,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       const outer_allocator_type& outer_allocator() const noexcept
       { return static_cast<const _OuterAlloc&>(*this); }
 
-      pointer allocate(size_type __n)
+      [[__nodiscard__]] pointer
+      allocate(size_type __n)
       { return __traits::allocate(outer_allocator(), __n); }
 
-      pointer allocate(size_type __n, const_void_pointer __hint)
+      [[__nodiscard__]] pointer
+      allocate(size_type __n, const_void_pointer __hint)
       { return __traits::allocate(outer_allocator(), __n, __hint); }
 
       void deallocate(pointer __p, size_type __n)
diff --git a/libstdc++-v3/libsupc++/new b/libstdc++-v3/libsupc++/new
index 795b33c4e6a..cc4cb294be7 100644
--- a/libstdc++-v3/libsupc++/new
+++ b/libstdc++-v3/libsupc++/new
@@ -117,8 +117,10 @@ namespace std
  *  Placement new and delete signatures (take a memory address argument,
  *  does nothing) may not be replaced by a user's program.
 */
+_GLIBCXX_NODISCARD
 void* operator new(std::size_t) _GLIBCXX_THROW (std::bad_alloc)
   __attribute__((__externally_visible__));
+_GLIBCXX_NODISCARD
 void* operator new[](std::size_t) _GLIBCXX_THROW (std::bad_alloc)
   __attribute__((__externally_visible__));
 void operator delete(void*) _GLIBCXX_USE_NOEXCEPT
@@ -131,8 +133,10 @@ void operator delete(void*, std::size_t) _GLIBCXX_USE_NOEXCEPT
 void operator delete[](void*, std::size_t) _GLIBCXX_USE_NOEXCEPT
   __attribute__((__externally_visible__));
 #endif
+_GLIBCXX_NODISCARD
 void* operator new(std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
   __attribute__((__externally_visible__));
+_GLIBCXX_NODISCARD
 void* operator new[](std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
   __attribute__((__externally_visible__));
 void operator delete(void*, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
@@ -140,16 +144,20 @@ void operator delete(void*, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
 void operator delete[](void*, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
   __attribute__((__externally_visible__));
 #if __cpp_aligned_new
+_GLIBCXX_NODISCARD
 void* operator new(std::size_t, std::align_val_t)
   __attribute__((__externally_visible__));
+_GLIBCXX_NODISCARD
 void* operator new(std::size_t, std::align_val_t, const std::nothrow_t&)
   _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
 void operator delete(void*, std::align_val_t)
   _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
 void operator delete(void*, std::align_val_t, const std::nothrow_t&)
   _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
+_GLIBCXX_NODISCARD
 void* operator new[](std::size_t, std::align_val_t)
   __attribute__((__externally_visible__));
+_GLIBCXX_NODISCARD
 void* operator new[](std::size_t, std::align_val_t, const std::nothrow_t&)
   _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
 void operator delete[](void*, std::align_val_t)
@@ -165,8 +173,10 @@ void operator delete[](void*, std::size_t, std::align_val_t)
 #endif // __cpp_aligned_new
 
 // Default placement versions of operator new.
+_GLIBCXX_NODISCARD
 inline void* operator new(std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT
 { return __p; }
+_GLIBCXX_NODISCARD
 inline void* operator new[](std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT
 { return __p; }
 
diff --git a/libstdc++-v3/testsuite/17_intro/headers/c++1998/all_attributes.cc b/libstdc++-v3/testsuite/17_intro/headers/c++1998/all_attributes.cc
index 6d6d7980396..8090cce8b9c 100644
--- a/libstdc++-v3/testsuite/17_intro/headers/c++1998/all_attributes.cc
+++ b/libstdc++-v3/testsuite/17_intro/headers/c++1998/all_attributes.cc
@@ -22,6 +22,7 @@
 // Don't test 'const' because it is reserved anyway.
 #define abi_tag 1
 #define always_inline 1
+#define nodiscard 1
 #ifndef __APPLE__
 // darwin headers use these, see PR 64883
 # define deprecated 1
diff --git a/libstdc++-v3/testsuite/17_intro/headers/c++2011/all_attributes.cc b/libstdc++-v3/testsuite/17_intro/headers/c++2011/all_attributes.cc
index 415ece0296f..809f001a384 100644
--- a/libstdc++-v3/testsuite/17_intro/headers/c++2011/all_attributes.cc
+++ b/libstdc++-v3/testsuite/17_intro/headers/c++2011/all_attributes.cc
@@ -22,6 +22,7 @@
 // Don't test 'const' and 'noreturn' because they are reserved anyway.
 #define abi_tag 1
 #define always_inline 1
+#define nodiscard 1
 #ifndef __APPLE__
 // darwin headers use these, see PR 64883
 # define visibility 1
diff --git a/libstdc++-v3/testsuite/17_intro/headers/c++2014/all_attributes.cc b/libstdc++-v3/testsuite/17_intro/headers/c++2014/all_attributes.cc
index d4ef52412a3..70533610a1f 100644
--- a/libstdc++-v3/testsuite/17_intro/headers/c++2014/all_attributes.cc
+++ b/libstdc++-v3/testsuite/17_intro/headers/c++2014/all_attributes.cc
@@ -22,6 +22,7 @@
 // Don't test 'const' and 'noreturn' because they are reserved anyway.
 #define abi_tag 1
 #define always_inline 1
+#define nodiscard 1
 #ifndef __APPLE__
 // darwin headers use these, see PR 64883
 # define deprecated 1
Jonathan Wakely Nov. 24, 2017, 12:01 a.m. UTC | #2
On 23/11/17 22:40 +0000, Jonathan Wakely wrote:
>C++17 added the [[nodiscard]] attribute, similar to GCC's
>warn_unused_result. The C++2a draft requires it to be used in several
>places. This patch adds it where required on components which are new
>to C++17, which are still experimental and so I'm changing them in
>stage 3.

I missed one of the C++17 pieces that should have [[nodiscard]].

Tested powerpc64le-linux, committed to trunk.
commit 65c20af6ec193916e390462ccfbd8e8e6b54ba8c
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Nov 23 22:51:31 2017 +0000

    Add [[nodiscard]] attribute to std::launder
    
            * libsupc++/new (launder): Add nodiscard attribute.
            * testsuite/18_support/launder/nodiscard.cc: New test.

diff --git a/libstdc++-v3/libsupc++/new b/libstdc++-v3/libsupc++/new
index 795b33c4e6a..0e408b1019c 100644
--- a/libstdc++-v3/libsupc++/new
+++ b/libstdc++-v3/libsupc++/new
@@ -176,7 +176,7 @@ inline void operator delete[](void*, void*) _GLIBCXX_USE_NOEXCEPT { }
 //@}
 } // extern "C++"
 
-#if __cplusplus > 201402L
+#if __cplusplus >= 201703L
 #if __GNUC__ >= 7
 #  define _GLIBCXX_HAVE_BUILTIN_LAUNDER 1
 #elif defined __has_builtin
@@ -192,7 +192,7 @@ namespace std
 #define __cpp_lib_launder 201606
   /// Pointer optimization barrier [ptr.launder]
   template<typename _Tp>
-    constexpr _Tp*
+    [[nodiscard]] constexpr _Tp*
     launder(_Tp* __p) noexcept
     { return __builtin_launder(__p); }
 
diff --git a/libstdc++-v3/testsuite/18_support/launder/nodiscard.cc b/libstdc++-v3/testsuite/18_support/launder/nodiscard.cc
new file mode 100644
index 00000000000..1741465abed
--- /dev/null
+++ b/libstdc++-v3/testsuite/18_support/launder/nodiscard.cc
@@ -0,0 +1,29 @@
+// Copyright (C) 2017 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" }
+// { dg-do compile { target c++17 } }
+
+#include <new>
+
+struct A { const int i; };
+
+void
+test01(A* a)
+{
+  std::launder(a); // { dg-warning "ignoring return value" }
+}
diff mbox series

Patch

diff --git a/libstdc++-v3/include/bits/fs_path.h b/libstdc++-v3/include/bits/fs_path.h
index 7d97cdfbb81..99740c9b383 100644
--- a/libstdc++-v3/include/bits/fs_path.h
+++ b/libstdc++-v3/include/bits/fs_path.h
@@ -370,7 +370,7 @@  _GLIBCXX_BEGIN_NAMESPACE_CXX11
 
     // query
 
-    bool empty() const noexcept { return _M_pathname.empty(); }
+    [[nodiscard]] bool empty() const noexcept { return _M_pathname.empty(); }
     bool has_root_name() const;
     bool has_root_directory() const;
     bool has_root_path() const;
diff --git a/libstdc++-v3/include/bits/node_handle.h b/libstdc++-v3/include/bits/node_handle.h
index 4a830630c89..0d8dbeb4110 100644
--- a/libstdc++-v3/include/bits/node_handle.h
+++ b/libstdc++-v3/include/bits/node_handle.h
@@ -62,7 +62,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
       explicit operator bool() const noexcept { return _M_ptr != nullptr; }
 
-      bool empty() const noexcept { return _M_ptr == nullptr; }
+      [[nodiscard]] bool empty() const noexcept { return _M_ptr == nullptr; }
 
     protected:
       constexpr _Node_handle_common() noexcept : _M_ptr(), _M_alloc() {}
diff --git a/libstdc++-v3/include/bits/range_access.h b/libstdc++-v3/include/bits/range_access.h
index 2a037ad8082..a5044f11976 100644
--- a/libstdc++-v3/include/bits/range_access.h
+++ b/libstdc++-v3/include/bits/range_access.h
@@ -257,7 +257,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
    *  @param  __cont  Container.
    */
   template <typename _Container>
-    constexpr auto
+    [[nodiscard]] constexpr auto
     empty(const _Container& __cont) noexcept(noexcept(__cont.empty()))
     -> decltype(__cont.empty())
     { return __cont.empty(); }
@@ -267,7 +267,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
    *  @param  __array  Container.
    */
   template <typename _Tp, size_t _Nm>
-    constexpr bool
+    [[nodiscard]] constexpr bool
     empty(const _Tp (&/*__array*/)[_Nm]) noexcept
     { return false; }
 
@@ -276,7 +276,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
    *  @param  __il  Initializer list.
    */
   template <typename _Tp>
-    constexpr bool
+    [[nodiscard]] constexpr bool
     empty(initializer_list<_Tp> __il) noexcept
     { return __il.size() == 0;}
 
diff --git a/libstdc++-v3/include/std/string_view b/libstdc++-v3/include/std/string_view
index 1900b867841..fa834002726 100644
--- a/libstdc++-v3/include/std/string_view
+++ b/libstdc++-v3/include/std/string_view
@@ -160,7 +160,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 		/ sizeof(value_type) / 4;
       }
 
-      constexpr bool
+      [[nodiscard]] constexpr bool
       empty() const noexcept
       { return this->_M_len == 0; }
 
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/capacity/empty_neg.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/capacity/empty_neg.cc
new file mode 100644
index 00000000000..59c87d007d8
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/capacity/empty_neg.cc
@@ -0,0 +1,28 @@ 
+// Copyright (C) 2017 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" }
+// { dg-do compile { target c++17 } }
+
+#include <string_view>
+
+void
+test01()
+{
+  std::string_view s;
+  s.empty();  // { dg-warning "ignoring return value" }
+}
diff --git a/libstdc++-v3/testsuite/24_iterators/range_access_cpp17_neg.cc b/libstdc++-v3/testsuite/24_iterators/range_access_cpp17_neg.cc
new file mode 100644
index 00000000000..12de34eb6e4
--- /dev/null
+++ b/libstdc++-v3/testsuite/24_iterators/range_access_cpp17_neg.cc
@@ -0,0 +1,44 @@ 
+// Copyright (C) 2017 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" }
+// { dg-do compile { target c++17 } }
+
+#include <iterator>
+#include <initializer_list>
+
+void
+test01()
+{
+  struct A { bool empty() const { return true; } };
+  A a;
+  std::empty(a);  // { dg-warning "ignoring return value" }
+}
+
+void
+test02()
+{
+  int a[2];
+  std::empty(a);  // { dg-warning "ignoring return value" }
+}
+
+void
+test03()
+{
+  std::initializer_list<int> a{};
+  std::empty(a);  // { dg-warning "ignoring return value" }
+}
diff --git a/libstdc++-v3/testsuite/27_io/filesystem/path/query/empty_neg.cc b/libstdc++-v3/testsuite/27_io/filesystem/path/query/empty_neg.cc
new file mode 100644
index 00000000000..7d38b494e9e
--- /dev/null
+++ b/libstdc++-v3/testsuite/27_io/filesystem/path/query/empty_neg.cc
@@ -0,0 +1,28 @@ 
+// Copyright (C) 2017 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" }
+// { dg-do compile { target c++17 } }
+
+#include <filesystem>
+
+void
+test01()
+{
+  std::filesystem::path p;
+  p.empty();  // { dg-warning "ignoring return value" }
+}