This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Make istreambuf_iterator::_M_sbuf immutable and add debug checks
- From: François Dumont <frs dot dumont at gmail dot com>
- To: "libstdc++ at gcc dot gnu dot org" <libstdc++ at gcc dot gnu dot org>, gcc-patches <gcc-patches at gcc dot gnu dot org>
- Date: Fri, 13 Oct 2017 19:14:38 +0200
- Subject: Make istreambuf_iterator::_M_sbuf immutable and add debug checks
- Authentication-results: sourceware.org; auth=none
Hi
Here is the last patch I will propose for istreambuf_iterator.
This is mostly to remove the mutable keyword on _M_sbuf.
To do so I had to reset _M_sbuf in valid places that is to say
constructors and increment operators. Despite that we might still have
eof iterators with _M_sbuf not null when you have for instance several
iterator instance but only increment one. It seems fine to me because
even in this case iterator will still be considered as eof and using
several istreambuf_iterator to go through a given streambuf is not usual.
As _M_sbuf is immutable I have been able to restore the simple
call to _M_at_eof() in the increment operators debug check.
Ok to commit after successful tests ?
François
diff --git a/libstdc++-v3/include/bits/streambuf_iterator.h b/libstdc++-v3/include/bits/streambuf_iterator.h
index 081afe5..ef22aa9 100644
--- a/libstdc++-v3/include/bits/streambuf_iterator.h
+++ b/libstdc++-v3/include/bits/streambuf_iterator.h
@@ -94,8 +94,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// the "end of stream" iterator value.
// NB: This implementation assumes the "end of stream" value
// is EOF, or -1.
- mutable streambuf_type* _M_sbuf;
- int_type _M_c;
+ streambuf_type* _M_sbuf;
+ int_type _M_c;
public:
/// Construct end of input stream iterator.
@@ -110,11 +110,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
/// Construct start of input stream iterator.
istreambuf_iterator(istream_type& __s) _GLIBCXX_USE_NOEXCEPT
- : _M_sbuf(__s.rdbuf()), _M_c(traits_type::eof()) { }
+ : _M_sbuf(__s.rdbuf()), _M_c(traits_type::eof())
+ { _M_init(); }
/// Construct start of streambuf iterator.
istreambuf_iterator(streambuf_type* __s) _GLIBCXX_USE_NOEXCEPT
- : _M_sbuf(__s), _M_c(traits_type::eof()) { }
+ : _M_sbuf(__s), _M_c(traits_type::eof())
+ { _M_init(); }
/// Return the current character pointed to by iterator. This returns
/// streambuf.sgetc(). It cannot be assigned. NB: The result of
@@ -138,13 +140,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
istreambuf_iterator&
operator++()
{
- __glibcxx_requires_cond(_M_sbuf &&
- (!_S_is_eof(_M_c) || !_S_is_eof(_M_sbuf->sgetc())),
+ __glibcxx_requires_cond(!_M_at_eof(),
_M_message(__gnu_debug::__msg_inc_istreambuf)
._M_iterator(*this));
_M_sbuf->sbumpc();
_M_c = traits_type::eof();
+
+ if (_S_is_eof(_M_sbuf->sgetc()))
+ _M_sbuf = 0;
+
return *this;
}
@@ -152,14 +157,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
istreambuf_iterator
operator++(int)
{
- __glibcxx_requires_cond(_M_sbuf &&
- (!_S_is_eof(_M_c) || !_S_is_eof(_M_sbuf->sgetc())),
+ __glibcxx_requires_cond(!_M_at_eof(),
_M_message(__gnu_debug::__msg_inc_istreambuf)
._M_iterator(*this));
istreambuf_iterator __old = *this;
__old._M_c = _M_sbuf->sbumpc();
_M_c = traits_type::eof();
+
+ if (_S_is_eof(_M_sbuf->sgetc()))
+ _M_sbuf = __old._M_sbuf = 0;
+
return __old;
}
@@ -172,12 +180,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ return _M_at_eof() == __b._M_at_eof(); }
private:
+ void
+ _M_init()
+ {
+ if (_M_sbuf && _S_is_eof(_M_sbuf->sgetc()))
+ _M_sbuf = 0;
+ }
+
int_type
_M_get() const
{
int_type __ret = _M_c;
- if (_M_sbuf && _S_is_eof(__ret) && _S_is_eof(__ret = _M_sbuf->sgetc()))
- _M_sbuf = 0;
+ if (_M_sbuf && __builtin_expect(_S_is_eof(__ret), true))
+ __ret = _M_sbuf->sgetc();
+
return __ret;
}
@@ -391,10 +407,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__c = __sb->snextc();
}
- __first._M_c = __eof;
+ if (!traits_type::eq_int_type(__c, __eof))
+ {
+ __first._M_c = __eof;
+ return __first;
+ }
}
- return __first;
+ return __last;
}
// @} group iterators
diff --git a/libstdc++-v3/testsuite/24_iterators/istreambuf_iterator/debug/1_neg.cc b/libstdc++-v3/testsuite/24_iterators/istreambuf_iterator/debug/1_neg.cc
new file mode 100644
index 0000000..241fc58
--- /dev/null
+++ b/libstdc++-v3/testsuite/24_iterators/istreambuf_iterator/debug/1_neg.cc
@@ -0,0 +1,35 @@
+// 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-do run { xfail *-*-* } }
+// { dg-require-debug-mode "" }
+
+#include <iterator>
+
+void test01()
+{
+ typedef std::istreambuf_iterator<char> cistreambuf_iter;
+
+ cistreambuf_iter eof;
+ ++eof; // Invalid.
+}
+
+int main()
+{
+ test01();
+ return 0;
+}
diff --git a/libstdc++-v3/testsuite/24_iterators/istreambuf_iterator/debug/2_neg.cc b/libstdc++-v3/testsuite/24_iterators/istreambuf_iterator/debug/2_neg.cc
new file mode 100644
index 0000000..407f00b
--- /dev/null
+++ b/libstdc++-v3/testsuite/24_iterators/istreambuf_iterator/debug/2_neg.cc
@@ -0,0 +1,35 @@
+// 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-do run { xfail *-*-* } }
+// { dg-require-debug-mode "" }
+
+#include <iterator>
+
+void test01()
+{
+ typedef std::istreambuf_iterator<char> cistreambuf_iter;
+
+ cistreambuf_iter eof;
+ eof++; // Invalid.
+}
+
+int main()
+{
+ test01();
+ return 0;
+}