[libcxx] r217902 - Create a 'comma_iterator' class that overloads operator, and asserts when it's called. Add tests to mismatch to make sure it can't be blindsided by such an evil iterator. More tests for other algorithms forthcoming. Thanks to STL for pointing this out at CppCon and Yakov Galka for opening LWG issue #2133

Marshall Clow mclow.lists at gmail.com
Tue Sep 16 13:38:11 PDT 2014


Author: marshall
Date: Tue Sep 16 15:38:11 2014
New Revision: 217902

URL: http://llvm.org/viewvc/llvm-project?rev=217902&view=rev
Log:
Create a 'comma_iterator' class that overloads operator, and asserts when it's called. Add tests to mismatch to make sure it can't be blindsided by such an evil iterator. More tests for other algorithms forthcoming. Thanks to STL for pointing this out at CppCon and Yakov Galka for opening LWG issue #2133

Modified:
    libcxx/trunk/test/algorithms/alg.nonmodifying/mismatch/mismatch.pass.cpp
    libcxx/trunk/test/algorithms/alg.nonmodifying/mismatch/mismatch_pred.pass.cpp
    libcxx/trunk/test/support/test_iterators.h

Modified: libcxx/trunk/test/algorithms/alg.nonmodifying/mismatch/mismatch.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/algorithms/alg.nonmodifying/mismatch/mismatch.pass.cpp?rev=217902&r1=217901&r2=217902&view=diff
==============================================================================
--- libcxx/trunk/test/algorithms/alg.nonmodifying/mismatch/mismatch.pass.cpp (original)
+++ libcxx/trunk/test/algorithms/alg.nonmodifying/mismatch/mismatch.pass.cpp Tue Sep 16 15:38:11 2014
@@ -36,6 +36,14 @@ int main()
                             input_iterator<const int*>(ia+3),
                             input_iterator<const int*>(ib+3))));
 
+    assert(std::mismatch(comma_iterator<const int*>(ia),
+                         comma_iterator<const int*>(ia + sa),
+                         comma_iterator<const int*>(ib)) ==
+                         (std::pair<comma_iterator<const int*>,
+                                    comma_iterator<const int*> >(
+                            comma_iterator<const int*>(ia+3),
+                            comma_iterator<const int*>(ib+3))));
+
 #ifdef HAS_FOUR_ITERATOR_VERSION
     assert(std::mismatch(input_iterator<const int*>(ia),
                          input_iterator<const int*>(ia + sa),

Modified: libcxx/trunk/test/algorithms/alg.nonmodifying/mismatch/mismatch_pred.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/algorithms/alg.nonmodifying/mismatch/mismatch_pred.pass.cpp?rev=217902&r1=217901&r2=217902&view=diff
==============================================================================
--- libcxx/trunk/test/algorithms/alg.nonmodifying/mismatch/mismatch_pred.pass.cpp (original)
+++ libcxx/trunk/test/algorithms/alg.nonmodifying/mismatch/mismatch_pred.pass.cpp Tue Sep 16 15:38:11 2014
@@ -39,6 +39,14 @@ int main()
                                     input_iterator<const int*> >(
                             input_iterator<const int*>(ia+3),
                             input_iterator<const int*>(ib+3))));
+    assert(std::mismatch(comma_iterator<const int*>(ia),
+                         comma_iterator<const int*>(ia + sa),
+                         comma_iterator<const int*>(ib),
+                         std::equal_to<int>()) ==
+                         (std::pair<comma_iterator<const int*>,
+                                    comma_iterator<const int*> >(
+                            comma_iterator<const int*>(ia+3),
+                            comma_iterator<const int*>(ib+3))));
 #ifdef HAS_FOUR_ITERATOR_VERSION
     assert(std::mismatch(input_iterator<const int*>(ia),
                          input_iterator<const int*>(ia + sa),

Modified: libcxx/trunk/test/support/test_iterators.h
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/support/test_iterators.h?rev=217902&r1=217901&r2=217902&view=diff
==============================================================================
--- libcxx/trunk/test/support/test_iterators.h (original)
+++ libcxx/trunk/test/support/test_iterators.h Tue Sep 16 15:38:11 2014
@@ -284,6 +284,108 @@ operator-(const random_access_iterator<T
     return x.base() - y.base();
 }
 
+template <class It>
+class comma_iterator
+{
+    It it_;
+
+    template <class U> friend class comma_iterator;
+public:
+    typedef          std::random_access_iterator_tag           iterator_category;
+    typedef typename std::iterator_traits<It>::value_type      value_type;
+    typedef typename std::iterator_traits<It>::difference_type difference_type;
+    typedef It                                                 pointer;
+    typedef typename std::iterator_traits<It>::reference       reference;
+
+    It base() const {return it_;}
+
+    comma_iterator() : it_() {}
+    explicit comma_iterator(It it) : it_(it) {}
+   template <class U>
+        comma_iterator(const comma_iterator<U>& u) :it_(u.it_) {}
+
+    reference operator*() const {return *it_;}
+    pointer operator->() const {return it_;}
+
+    comma_iterator& operator++() {++it_; return *this;}
+    comma_iterator operator++(int)
+        {comma_iterator tmp(*this); ++(*this); return tmp;}
+
+    comma_iterator& operator--() {--it_; return *this;}
+    comma_iterator operator--(int)
+        {comma_iterator tmp(*this); --(*this); return tmp;}
+
+    comma_iterator& operator+=(difference_type n) {it_ += n; return *this;}
+    comma_iterator operator+(difference_type n) const
+        {comma_iterator tmp(*this); tmp += n; return tmp;}
+    friend comma_iterator operator+(difference_type n, comma_iterator x)
+        {x += n; return x;}
+    comma_iterator& operator-=(difference_type n) {return *this += -n;}
+    comma_iterator operator-(difference_type n) const
+        {comma_iterator tmp(*this); tmp -= n; return tmp;}
+
+    reference operator[](difference_type n) const {return it_[n];}
+    template <typename T>
+    void operator,(const T &) { assert(false); }
+};
+
+template <class T, class U>
+inline
+bool
+operator==(const comma_iterator<T>& x, const comma_iterator<U>& y)
+{
+    return x.base() == y.base();
+}
+
+template <class T, class U>
+inline
+bool
+operator!=(const comma_iterator<T>& x, const comma_iterator<U>& y)
+{
+    return !(x == y);
+}
+
+template <class T, class U>
+inline
+bool
+operator<(const comma_iterator<T>& x, const comma_iterator<U>& y)
+{
+    return x.base() < y.base();
+}
+
+template <class T, class U>
+inline
+bool
+operator<=(const comma_iterator<T>& x, const comma_iterator<U>& y)
+{
+    return !(y < x);
+}
+
+template <class T, class U>
+inline
+bool
+operator>(const comma_iterator<T>& x, const comma_iterator<U>& y)
+{
+    return y < x;
+}
+
+template <class T, class U>
+inline
+bool
+operator>=(const comma_iterator<T>& x, const comma_iterator<U>& y)
+{
+    return !(x < y);
+}
+
+template <class T, class U>
+inline
+typename std::iterator_traits<T>::difference_type
+operator-(const comma_iterator<T>& x, const comma_iterator<U>& y)
+{
+    return x.base() - y.base();
+}
+
+
 template <class Iter>
 inline Iter base(output_iterator<Iter> i) { return i.base(); }
 
@@ -299,6 +401,9 @@ inline Iter base(bidirectional_iterator<
 template <class Iter>
 inline Iter base(random_access_iterator<Iter> i) { return i.base(); }
 
+template <class Iter>
+inline Iter base(comma_iterator<Iter> i) { return i.base(); }
+
 template <class Iter>    // everything else
 inline Iter base(Iter i) { return i; }
 





More information about the cfe-commits mailing list