diff mbox series

[committed] libstdc++: Add [[nodiscard]] to iostream members

Message ID 20240926203533.2068507-1-jwakely@redhat.com
State New
Headers show
Series [committed] libstdc++: Add [[nodiscard]] to iostream members | expand

Commit Message

Jonathan Wakely Sept. 26, 2024, 8:35 p.m. UTC
Tested x86_64-linux. Pushed to trunk.

-- >8 --

These are all pure functions and MSVC also marks all of these as
nodiscard except for std::basic_ios::tie() const, but that's been
confirmed as an accidental omission.

libstdc++-v3/ChangeLog:

	* include/bits/basic_ios.h (basic_ios::operator bool()):
	Add [[nodiscard]] attribute.
	(basic_ios::operator!(), basic_ios::rdstate())
	(basic_ios::good(), basic_ios::eof(), basic_ios::fail())
	(basic_ios::bad(), basic_ios::exceptions(), basic_ios::tie())
	(basic_ios::rdbuf(), basic_ios::fill()): Likewise.
	* include/bits/ios_base.h (ios_base::flags()): Likewise.
	(ios_base::precision(), ios_base::width(), ios_base::getloc()):
	Likewise.
	* include/std/fstream (basic_filebuf::is_open)
	(basic_ifstream::rdbuf(), basic_ifstream::is_open)
	(basic_ofstream::rdbuf(), basic_ofstream::is_open)
	(basic_fstream::rdbuf(), basic_fstream::is_open): Likewise.
	* include/std/spanstream (basic_spanbuf::span())
	(basic_ispanstream::span(), basic_ispanstream::rdbuf())
	(basic_ospanstream::span(), basic_ospanstream::rdbuf())
	(basic_spanstream::span(), basic_spanstream::rdbuf()):
	Likewise.
	* include/std/sstream (basic_stringbuf::str())
	(basic_istringstream::rdbuf(), basic_istringstream::str())
	(basic_ostringstream::rdbuf(), basic_ostringstream::str())
	(basic_stringstream::rdbuf(), basic_stringstream::str()):
	Likewise.
	* testsuite/27_io/basic_istream/extractors_arithmetic/char/01.cc:
	Suppress -Wunused-result warnings.
	* testsuite/27_io/basic_istream/extractors_arithmetic/wchar_t/01.cc:
	Likewise.
---
 libstdc++-v3/include/bits/basic_ios.h             | 11 +++++++++++
 libstdc++-v3/include/bits/ios_base.h              |  4 ++++
 libstdc++-v3/include/std/fstream                  | 10 ++++++++++
 libstdc++-v3/include/std/spanstream               |  7 +++++++
 libstdc++-v3/include/std/sstream                  | 15 +++++++++++++++
 .../extractors_arithmetic/char/01.cc              |  4 ++--
 .../extractors_arithmetic/wchar_t/01.cc           |  4 ++--
 7 files changed, 51 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/libstdc++-v3/include/bits/basic_ios.h b/libstdc++-v3/include/bits/basic_ios.h
index a2d8060edd2..8954ad16d63 100644
--- a/libstdc++-v3/include/bits/basic_ios.h
+++ b/libstdc++-v3/include/bits/basic_ios.h
@@ -120,6 +120,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  <code>if (!a_stream) ...</code> and <code>while (a_stream) ...</code>
       */
 #if __cplusplus >= 201103L
+      _GLIBCXX_NODISCARD
       explicit operator bool() const
       { return !this->fail(); }
 #else
@@ -127,6 +128,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       { return this->fail() ? 0 : const_cast<basic_ios*>(this); }
 #endif
 
+      _GLIBCXX_NODISCARD
       bool
       operator!() const
       { return this->fail(); }
@@ -139,6 +141,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  See std::ios_base::iostate for the possible bit values.  Most
        *  users will call one of the interpreting wrappers, e.g., good().
       */
+      _GLIBCXX_NODISCARD
       iostate
       rdstate() const
       { return _M_streambuf_state; }
@@ -182,6 +185,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *
        *  A wrapper around rdstate.
       */
+      _GLIBCXX_NODISCARD
       bool
       good() const
       { return this->rdstate() == 0; }
@@ -192,6 +196,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *
        *  Note that other iostate flags may also be set.
       */
+      _GLIBCXX_NODISCARD
       bool
       eof() const
       { return (this->rdstate() & eofbit) != 0; }
@@ -203,6 +208,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  Checking the badbit in fail() is historical practice.
        *  Note that other iostate flags may also be set.
       */
+      _GLIBCXX_NODISCARD
       bool
       fail() const
       { return (this->rdstate() & (badbit | failbit)) != 0; }
@@ -213,6 +219,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *
        *  Note that other iostate flags may also be set.
       */
+      _GLIBCXX_NODISCARD
       bool
       bad() const
       { return (this->rdstate() & badbit) != 0; }
@@ -224,6 +231,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  This changes nothing in the stream.  See the one-argument version
        *  of exceptions(iostate) for the meaning of the return value.
       */
+      _GLIBCXX_NODISCARD
       iostate
       exceptions() const
       { return _M_exception; }
@@ -297,6 +305,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  stream.  When this stream performs any I/O, the tied stream is
        *  first flushed.  For example, @c std::cin is tied to @c std::cout.
       */
+      _GLIBCXX_NODISCARD
       basic_ostream<_CharT, _Traits>*
       tie() const
       { return _M_tie; }
@@ -323,6 +332,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *
        *  This does not change the state of the stream.
       */
+      _GLIBCXX_NODISCARD
       basic_streambuf<_CharT, _Traits>*
       rdbuf() const
       { return _M_streambuf; }
@@ -372,6 +382,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *
        *  It defaults to a space (' ') in the current locale.
       */
+      _GLIBCXX_NODISCARD
       char_type
       fill() const
       {
diff --git a/libstdc++-v3/include/bits/ios_base.h b/libstdc++-v3/include/bits/ios_base.h
index a649448e298..1418b18830a 100644
--- a/libstdc++-v3/include/bits/ios_base.h
+++ b/libstdc++-v3/include/bits/ios_base.h
@@ -689,6 +689,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
      *  @brief  Access to format flags.
      *  @return  The format control flags for both input and output.
     */
+    _GLIBCXX_NODISCARD
     fmtflags
     flags() const
     { return _M_flags; }
@@ -759,6 +760,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
      *  Be careful if you try to give a definition of @a precision here; see
      *  DR 189.
     */
+    _GLIBCXX_NODISCARD
     streamsize
     precision() const
     { return _M_precision; }
@@ -782,6 +784,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
      *
      *  <em>Minimum field width</em> refers to the number of characters.
     */
+    _GLIBCXX_NODISCARD
     streamsize
     width() const
     { return _M_width; }
@@ -833,6 +836,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
      *  returns @c loc.  Otherwise, it returns a copy of @c std::locale(),
      *  the global C++ locale.
     */
+    _GLIBCXX_NODISCARD
     locale
     getloc() const
     { return _M_ios_locale; }
diff --git a/libstdc++-v3/include/std/fstream b/libstdc++-v3/include/std/fstream
index 665a17afee6..8f359404a48 100644
--- a/libstdc++-v3/include/std/fstream
+++ b/libstdc++-v3/include/std/fstream
@@ -273,6 +273,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       /**
        *  @brief  Returns true if the external file is open.
        */
+      _GLIBCXX_NODISCARD
       bool
       is_open() const throw()
       { return _M_file.is_open(); }
@@ -670,6 +671,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *
        *  This hides both signatures of std::basic_ios::rdbuf().
        */
+      _GLIBCXX_NODISCARD
       __filebuf_type*
       rdbuf() const
       { return const_cast<__filebuf_type*>(&_M_filebuf); }
@@ -678,12 +680,14 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  @brief  Wrapper to test for an open file.
        *  @return  @c rdbuf()->is_open()
        */
+      _GLIBCXX_NODISCARD
       bool
       is_open()
       { return _M_filebuf.is_open(); }
 
       // _GLIBCXX_RESOLVE_LIB_DEFECTS
       // 365. Lack of const-qualification in clause 27
+      _GLIBCXX_NODISCARD
       bool
       is_open() const
       { return _M_filebuf.is_open(); }
@@ -942,6 +946,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *
        *  This hides both signatures of std::basic_ios::rdbuf().
        */
+      _GLIBCXX_NODISCARD
       __filebuf_type*
       rdbuf() const
       { return const_cast<__filebuf_type*>(&_M_filebuf); }
@@ -950,12 +955,14 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  @brief  Wrapper to test for an open file.
        *  @return  @c rdbuf()->is_open()
        */
+      _GLIBCXX_NODISCARD
       bool
       is_open()
       { return _M_filebuf.is_open(); }
 
       // _GLIBCXX_RESOLVE_LIB_DEFECTS
       // 365. Lack of const-qualification in clause 27
+      _GLIBCXX_NODISCARD
       bool
       is_open() const
       { return _M_filebuf.is_open(); }
@@ -1207,6 +1214,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *
        *  This hides both signatures of std::basic_ios::rdbuf().
        */
+      _GLIBCXX_NODISCARD
       __filebuf_type*
       rdbuf() const
       { return const_cast<__filebuf_type*>(&_M_filebuf); }
@@ -1215,12 +1223,14 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  @brief  Wrapper to test for an open file.
        *  @return  @c rdbuf()->is_open()
        */
+      _GLIBCXX_NODISCARD
       bool
       is_open()
       { return _M_filebuf.is_open(); }
 
       // _GLIBCXX_RESOLVE_LIB_DEFECTS
       // 365. Lack of const-qualification in clause 27
+      _GLIBCXX_NODISCARD
       bool
       is_open() const
       { return _M_filebuf.is_open(); }
diff --git a/libstdc++-v3/include/std/spanstream b/libstdc++-v3/include/std/spanstream
index b86bd613489..98ad3fa856a 100644
--- a/libstdc++-v3/include/std/spanstream
+++ b/libstdc++-v3/include/std/spanstream
@@ -110,6 +110,7 @@  template<typename _CharT, typename _Traits>
     }
 
     // [spanbuf.members], member functions
+    [[nodiscard]]
     std::span<_CharT>
     span() const noexcept
     {
@@ -277,12 +278,14 @@  template<typename _CharT, typename _Traits>
     }
 
     // [ispanstream.members], member functions
+    [[nodiscard]]
     basic_spanbuf<_CharT, _Traits>*
     rdbuf() const noexcept
     {
       return const_cast<basic_spanbuf<_CharT, _Traits>*>(std::__addressof(_M_sb));
     }
 
+    [[nodiscard]]
     std::span<const _CharT>
     span() const noexcept
     { return _M_sb.span(); }
@@ -356,12 +359,14 @@  template<typename _CharT, typename _Traits>
     }
 
     // [ospanstream.members], member functions
+    [[nodiscard]]
     basic_spanbuf<_CharT, _Traits>*
     rdbuf() const noexcept
     {
       return const_cast<basic_spanbuf<_CharT, _Traits>*>(std::__addressof(_M_sb));
     }
 
+    [[nodiscard]]
     std::span<_CharT>
     span() const noexcept
     { return _M_sb.span(); }
@@ -424,12 +429,14 @@  template<typename _CharT, typename _Traits>
     }
 
     // [spanstream.members], members
+    [[nodiscard]]
     basic_spanbuf<_CharT, _Traits>*
     rdbuf() const noexcept
     {
       return const_cast<basic_spanbuf<_CharT, _Traits>*>(std::__addressof(_M_sb));
     }
 
+    [[nodiscard]]
     std::span<_CharT>
     span() const noexcept
     { return _M_sb.span(); }
diff --git a/libstdc++-v3/include/std/sstream b/libstdc++-v3/include/std/sstream
index fd24f655218..aaa89b2bed8 100644
--- a/libstdc++-v3/include/std/sstream
+++ b/libstdc++-v3/include/std/sstream
@@ -247,6 +247,7 @@  _GLIBCXX_BEGIN_NAMESPACE_CXX11
        *  character sequence is equal to the input sequence; otherwise, it
        *  is equal to the output sequence.</em> [27.7.1.2]/1
       */
+      _GLIBCXX_NODISCARD
       __string_type
       str() const _GLIBCXX_LVAL_REF_QUAL
       {
@@ -262,6 +263,7 @@  _GLIBCXX_BEGIN_NAMESPACE_CXX11
 #if _GLIBCXX_USE_CXX11_ABI
 #if __cpp_concepts
       template<__allocator_like _SAlloc>
+	_GLIBCXX_NODISCARD
 	basic_string<_CharT, _Traits, _SAlloc>
 	str(const _SAlloc& __sa) const
 	{
@@ -270,6 +272,7 @@  _GLIBCXX_BEGIN_NAMESPACE_CXX11
 	}
 #endif
 
+      _GLIBCXX_NODISCARD
       __string_type
       str() &&
       {
@@ -696,6 +699,7 @@  _GLIBCXX_BEGIN_NAMESPACE_CXX11
        *
        *  This hides both signatures of std::basic_ios::rdbuf().
       */
+      _GLIBCXX_NODISCARD
       __stringbuf_type*
       rdbuf() const
       { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
@@ -704,6 +708,7 @@  _GLIBCXX_BEGIN_NAMESPACE_CXX11
        *  @brief  Copying out the string buffer.
        *  @return  @c rdbuf()->str()
       */
+      _GLIBCXX_NODISCARD
       __string_type
       str() const _GLIBCXX_LVAL_REF_QUAL
       { return _M_stringbuf.str(); }
@@ -712,11 +717,13 @@  _GLIBCXX_BEGIN_NAMESPACE_CXX11
 #if _GLIBCXX_USE_CXX11_ABI
 #if __cpp_concepts
       template<__allocator_like _SAlloc>
+	_GLIBCXX_NODISCARD
 	basic_string<_CharT, _Traits, _SAlloc>
 	str(const _SAlloc& __sa) const
 	{ return _M_stringbuf.str(__sa); }
 #endif
 
+      _GLIBCXX_NODISCARD
       __string_type
       str() &&
       { return std::move(_M_stringbuf).str(); }
@@ -920,6 +927,7 @@  _GLIBCXX_BEGIN_NAMESPACE_CXX11
        *
        *  This hides both signatures of std::basic_ios::rdbuf().
       */
+      _GLIBCXX_NODISCARD
       __stringbuf_type*
       rdbuf() const
       { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
@@ -928,6 +936,7 @@  _GLIBCXX_BEGIN_NAMESPACE_CXX11
        *  @brief  Copying out the string buffer.
        *  @return  @c rdbuf()->str()
       */
+      _GLIBCXX_NODISCARD
       __string_type
       str() const _GLIBCXX_LVAL_REF_QUAL
       { return _M_stringbuf.str(); }
@@ -936,11 +945,13 @@  _GLIBCXX_BEGIN_NAMESPACE_CXX11
 #if _GLIBCXX_USE_CXX11_ABI
 #if __cpp_concepts
       template<__allocator_like _SAlloc>
+	_GLIBCXX_NODISCARD
 	basic_string<_CharT, _Traits, _SAlloc>
 	str(const _SAlloc& __sa) const
 	{ return _M_stringbuf.str(__sa); }
 #endif
 
+      _GLIBCXX_NODISCARD
       __string_type
       str() &&
       { return std::move(_M_stringbuf).str(); }
@@ -1142,6 +1153,7 @@  _GLIBCXX_BEGIN_NAMESPACE_CXX11
        *
        *  This hides both signatures of std::basic_ios::rdbuf().
       */
+      _GLIBCXX_NODISCARD
       __stringbuf_type*
       rdbuf() const
       { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
@@ -1150,6 +1162,7 @@  _GLIBCXX_BEGIN_NAMESPACE_CXX11
        *  @brief  Copying out the string buffer.
        *  @return  @c rdbuf()->str()
       */
+      _GLIBCXX_NODISCARD
       __string_type
       str() const _GLIBCXX_LVAL_REF_QUAL
       { return _M_stringbuf.str(); }
@@ -1158,11 +1171,13 @@  _GLIBCXX_BEGIN_NAMESPACE_CXX11
 #if _GLIBCXX_USE_CXX11_ABI
 #if __cpp_concepts
       template<__allocator_like _SAlloc>
+	_GLIBCXX_NODISCARD
 	basic_string<_CharT, _Traits, _SAlloc>
 	str(const _SAlloc& __sa) const
 	{ return _M_stringbuf.str(__sa); }
 #endif
 
+      _GLIBCXX_NODISCARD
       __string_type
       str() &&
       { return std::move(_M_stringbuf).str(); }
diff --git a/libstdc++-v3/testsuite/27_io/basic_istream/extractors_arithmetic/char/01.cc b/libstdc++-v3/testsuite/27_io/basic_istream/extractors_arithmetic/char/01.cc
index 08cbd5fc902..1ebadd1913f 100644
--- a/libstdc++-v3/testsuite/27_io/basic_istream/extractors_arithmetic/char/01.cc
+++ b/libstdc++-v3/testsuite/27_io/basic_istream/extractors_arithmetic/char/01.cc
@@ -60,7 +60,7 @@  void test01() {
 
   // process alphanumeric versions of bool values
   is_02.setf(std::ios_base::boolalpha);
-  is_02.flags();
+  (void) is_02.flags();
   is_02 >> b1;
   VERIFY( b1 == 1 );
   is_02 >> b1;
@@ -68,7 +68,7 @@  void test01() {
 
   // process numeric versions of of bool values
   is_02.unsetf(std::ios_base::boolalpha);
-  is_02.flags();
+  (void) is_02.flags();
   is_02 >> b1;
   VERIFY( b1 == 0 );
   is_02 >> b1;
diff --git a/libstdc++-v3/testsuite/27_io/basic_istream/extractors_arithmetic/wchar_t/01.cc b/libstdc++-v3/testsuite/27_io/basic_istream/extractors_arithmetic/wchar_t/01.cc
index 2b70390fe2b..26db8c549f2 100644
--- a/libstdc++-v3/testsuite/27_io/basic_istream/extractors_arithmetic/wchar_t/01.cc
+++ b/libstdc++-v3/testsuite/27_io/basic_istream/extractors_arithmetic/wchar_t/01.cc
@@ -58,7 +58,7 @@  void test01() {
 
   // process alphanumeric versions of bool values
   is_02.setf(std::ios_base::boolalpha);
-  is_02.flags();
+  (void) is_02.flags();
   is_02 >> b1;
   VERIFY( b1 == 1 );
   is_02 >> b1;
@@ -66,7 +66,7 @@  void test01() {
 
   // process numeric versions of of bool values
   is_02.unsetf(std::ios_base::boolalpha);
-  is_02.flags();
+  (void) is_02.flags();
   is_02 >> b1;
   VERIFY( b1 == 0 );
   is_02 >> b1;