public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Patrick Palka <ppalka@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: libstdc++@gcc.gnu.org, jwakely@redhat.com,
	Patrick Palka <ppalka@redhat.com>
Subject: [PATCH] libstdc++: Add a test that takes the split_view of a non-forward range
Date: Wed, 11 Mar 2020 11:46:54 -0400	[thread overview]
Message-ID: <20200311154654.837650-1-ppalka@redhat.com> (raw)

This adds a tests that verifies taking the split_view of a non-forward range
works correctly.  Doing so revealed a typo in one of _OuterIter's constructors.

It also revealed that the default constructor of
__gnu_test::test_range::iterator misbehaves, because by delegating to
Iter<T>(nullptr, nullptr) we perform a null-pointer deref at runtime in
input_iterator_wrapper's constructor due to the ITERATOR_VERIFY check therein.
Instead of delegating to this constructor it seems we can just inherit the
protected default constructor, which does not contain this ITERATOR_VERIFY
check.

libstdc++-v3/ChangeLog:

	* include/std/ranges (split_view::_OuterIter::_OuterIter): Typo fix,
	'address' -> 'std::__addressof'.
	* testsuite/std/ranges/adaptors/split.cc: Test taking the split_view of
	a non-forward input_range.
	* testsuite/util/testsuite_iterators.h (output_iterator_wrapper): Make
	default constructor protected instead of deleted, like with
	input_iterator_wrapper.
	(test_range::iterator): Add comment explaining that this type is used
	only when the underlying wrapper is input_iterator_wrapper or
	output_iterator_wrapper.  Remove delegating defaulted constructor so
	that the inherited default constructor is used instead.
---
 libstdc++-v3/include/std/ranges               |  2 +-
 .../testsuite/std/ranges/adaptors/split.cc    | 20 +++++++++++++++++++
 .../testsuite/util/testsuite_iterators.h      | 14 ++++++++-----
 3 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index 23396947ac3..f4faec463e8 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -2786,7 +2786,7 @@ namespace views
 
 	  constexpr explicit
 	  _OuterIter(_Parent& __parent) requires (!forward_range<_Base>)
-	    : _M_parent(address(__parent))
+	    : _M_parent(std::__addressof(__parent))
 	  { }
 
 	  constexpr
diff --git a/libstdc++-v3/testsuite/std/ranges/adaptors/split.cc b/libstdc++-v3/testsuite/std/ranges/adaptors/split.cc
index abdbfb41d8b..fe895827fc5 100644
--- a/libstdc++-v3/testsuite/std/ranges/adaptors/split.cc
+++ b/libstdc++-v3/testsuite/std/ranges/adaptors/split.cc
@@ -27,6 +27,7 @@
 
 using __gnu_test::test_range;
 using __gnu_test::forward_iterator_wrapper;
+using __gnu_test::input_iterator_wrapper;
 
 namespace ranges = std::ranges;
 namespace views = std::ranges::views;
@@ -133,6 +134,24 @@ test07()
   static_assert( noexcept(iter_swap(b, b2)) );
 }
 
+void
+test08()
+{
+  char x[] = "the quick brown fox";
+  test_range<char, input_iterator_wrapper> rx(x, x+sizeof(x)-1);
+  auto v = rx | views::split(' ');
+  auto i = v.begin();
+  VERIFY( ranges::equal(*i, "the"sv) );
+  ++i;
+  VERIFY( ranges::equal(*i, "quick"sv) );
+  ++i;
+  VERIFY( ranges::equal(*i, "brown"sv) );
+  ++i;
+  VERIFY( ranges::equal(*i, "fox"sv) );
+  ++i;
+  VERIFY( i == v.end() );
+}
+
 int
 main()
 {
@@ -143,4 +162,5 @@ main()
   test05();
   test06();
   test07();
+  test08();
 }
diff --git a/libstdc++-v3/testsuite/util/testsuite_iterators.h b/libstdc++-v3/testsuite/util/testsuite_iterators.h
index a915c02248b..5be47f47915 100644
--- a/libstdc++-v3/testsuite/util/testsuite_iterators.h
+++ b/libstdc++-v3/testsuite/util/testsuite_iterators.h
@@ -124,6 +124,11 @@ namespace __gnu_test
   struct output_iterator_wrapper
   : public std::iterator<std::output_iterator_tag, T, std::ptrdiff_t, T*, T&>
   {
+  protected:
+    output_iterator_wrapper() : ptr(0), SharedInfo(0)
+    { }
+
+  public:
     typedef OutputContainer<T> ContainerType;
     T* ptr;
     ContainerType* SharedInfo;
@@ -135,8 +140,6 @@ namespace __gnu_test
     }
 
 #if __cplusplus >= 201103L
-    output_iterator_wrapper() = delete;
-
     output_iterator_wrapper(const output_iterator_wrapper&) = default;
 
     output_iterator_wrapper&
@@ -706,13 +709,14 @@ namespace __gnu_test
   template<typename T, template<typename> class Iter>
     class test_range
     {
-      // Adds default constructor to Iter<T> if needed
+      // Exposes the protected default constructor of Iter<T> if needed.  This
+      // is needed only when Iter is input_iterator_wrapper or
+      // output_iterator_wrapper, because legacy forward iterators and beyond
+      // are already default constructible.
       struct iterator : Iter<T>
       {
 	using Iter<T>::Iter;
 
-	iterator() : Iter<T>(nullptr, nullptr) { }
-
 	using Iter<T>::operator++;
 
 	iterator& operator++() { Iter<T>::operator++(); return *this; }
-- 
2.26.0.rc1


             reply	other threads:[~2020-03-11 15:47 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-11 15:46 Patrick Palka [this message]
2020-03-11 15:57 ` Jonathan Wakely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200311154654.837650-1-ppalka@redhat.com \
    --to=ppalka@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jwakely@redhat.com \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).