Initial checkin for debug mode (version 2)

llvm-svn: 139711
diff --git a/libcxx/include/iterator b/libcxx/include/iterator
index b232d09..ed2d7a7 100644
--- a/libcxx/include/iterator
+++ b/libcxx/include/iterator
@@ -1066,30 +1066,90 @@
     _LIBCPP_INLINE_VISIBILITY __wrap_iter() _NOEXCEPT {}
     template <class _Up> _LIBCPP_INLINE_VISIBILITY __wrap_iter(const __wrap_iter<_Up>& __u,
         typename enable_if<is_convertible<_Up, iterator_type>::value>::type* = 0) _NOEXCEPT
-        : __i(__u.base()) {}
-    _LIBCPP_INLINE_VISIBILITY reference operator*() const _NOEXCEPT {return *__i;}
+        : __i(__u.base())
+    {
+#ifdef _LIBCPP_DEBUG2
+        __get_db()->__iterator_copy(this, &__u);
+#endif
+    }
+#ifdef _LIBCPP_DEBUG2
+    _LIBCPP_INLINE_VISIBILITY
+    __wrap_iter(const __wrap_iter& __x)
+        : __i(__x.base())
+    {
+        __get_db()->__iterator_copy(this, &__x);
+    }
+    _LIBCPP_INLINE_VISIBILITY
+    __wrap_iter& operator=(const __wrap_iter& __x)
+    {
+        if (this != &__x)
+        {
+            __get_db()->__iterator_copy(this, &__x);
+            __i = __x.__i;
+        }
+        return *this;
+    }
+    _LIBCPP_INLINE_VISIBILITY
+    ~__wrap_iter()
+    {
+        __get_db()->__erase_i(this);
+    }
+#endif
+    _LIBCPP_INLINE_VISIBILITY reference operator*() const _NOEXCEPT
+    {
+        _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
+                       "Attempted to dereference a non-dereferenceable iterator");
+        return *__i;
+    }
     _LIBCPP_INLINE_VISIBILITY pointer  operator->() const _NOEXCEPT {return &(operator*());}
-    _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator++() _NOEXCEPT {++__i; return *this;}
+    _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator++() _NOEXCEPT
+    {
+        _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
+                       "Attempted to increment non-incrementable iterator");
+        ++__i;
+        return *this;
+    }
     _LIBCPP_INLINE_VISIBILITY __wrap_iter  operator++(int) _NOEXCEPT
-        {__wrap_iter __tmp(*this); ++__i; return __tmp;}
-    _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator--() _NOEXCEPT {--__i; return *this;}
+        {__wrap_iter __tmp(*this); ++(*this); return __tmp;}
+    _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator--() _NOEXCEPT
+    {
+        _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
+                       "Attempted to decrement non-decrementable iterator");
+        --__i;
+        return *this;
+    }
     _LIBCPP_INLINE_VISIBILITY __wrap_iter  operator--(int) _NOEXCEPT
-        {__wrap_iter __tmp(*this); --__i; return __tmp;}
+        {__wrap_iter __tmp(*this); --(*this); return __tmp;}
     _LIBCPP_INLINE_VISIBILITY __wrap_iter  operator+ (difference_type __n) const _NOEXCEPT
-        {return __wrap_iter(__i + __n);}
+        {__wrap_iter __w(*this); __w += __n; return __w;}
     _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator+=(difference_type __n) _NOEXCEPT
-        {__i += __n; return *this;}
+    {
+        _LIBCPP_ASSERT(__get_const_db()->__addable(this, __n),
+                   "Attempted to add/subtract iterator outside of valid range");
+        __i += __n;
+        return *this;
+    }
     _LIBCPP_INLINE_VISIBILITY __wrap_iter  operator- (difference_type __n) const _NOEXCEPT
-        {return __wrap_iter(__i - __n);}
+        {return *this + (-__n);}
     _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator-=(difference_type __n) _NOEXCEPT
-        {__i -= __n; return *this;}
+        {*this += -__n; return *this;}
     _LIBCPP_INLINE_VISIBILITY reference        operator[](difference_type __n) const _NOEXCEPT
-        {return __i[__n];}
+    {
+        _LIBCPP_ASSERT(__get_const_db()->__subscriptable(this, __n),
+                   "Attempted to subscript iterator outside of valid range");
+        return __i[__n];
+    }
 
     _LIBCPP_INLINE_VISIBILITY iterator_type base() const _NOEXCEPT {return __i;}
 
 private:
     _LIBCPP_INLINE_VISIBILITY __wrap_iter(iterator_type __x) _NOEXCEPT : __i(__x) {}
+#ifdef _LIBCPP_DEBUG2
+    _LIBCPP_INLINE_VISIBILITY __wrap_iter(const void* __p, iterator_type __x) : __i(__x)
+    {
+        __get_db()->__insert_ic(this, __p);
+    }
+#endif
 
     template <class _Up> friend class __wrap_iter;
     template <class _CharT, class _Traits, class _Alloc> friend class basic_string;
@@ -1155,6 +1215,8 @@
 bool
 operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
 {
+    _LIBCPP_ASSERT(__get_const_db()->__comparable(&__x, &__y),
+                   "Attempted to compare incomparable iterators");
     return __x.base() == __y.base();
 }
 
@@ -1163,6 +1225,8 @@
 bool
 operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
 {
+    _LIBCPP_ASSERT(__get_const_db()->__comparable(&__x, &__y),
+                   "Attempted to compare incomparable iterators");
     return __x.base() < __y.base();
 }
 
@@ -1171,7 +1235,7 @@
 bool
 operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
 {
-    return __x.base() != __y.base();
+    return !(__x == __y);
 }
 
 template <class _Iter1, class _Iter2>
@@ -1179,7 +1243,7 @@
 bool
 operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
 {
-    return __x.base() > __y.base();
+    return __y < __x;
 }
 
 template <class _Iter1, class _Iter2>
@@ -1187,7 +1251,7 @@
 bool
 operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
 {
-    return __x.base() >= __y.base();
+    return !(__x < __y);
 }
 
 template <class _Iter1, class _Iter2>
@@ -1195,7 +1259,7 @@
 bool
 operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
 {
-    return __x.base() <= __y.base();
+    return !(__y < __x);
 }
 
 template <class _Iter1, class _Iter2>
@@ -1203,6 +1267,8 @@
 typename __wrap_iter<_Iter1>::difference_type
 operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
 {
+    _LIBCPP_ASSERT(__get_const_db()->__comparable(&__x, &__y),
+                   "Attempted to subtract incompatible iterators");
     return __x.base() - __y.base();
 }
 
@@ -1210,9 +1276,10 @@
 inline _LIBCPP_INLINE_VISIBILITY
 __wrap_iter<_Iter>
 operator+(typename __wrap_iter<_Iter>::difference_type __n,
-          const __wrap_iter<_Iter>& __x) _NOEXCEPT
+          __wrap_iter<_Iter> __x) _NOEXCEPT
 {
-    return __wrap_iter<_Iter>(__x.base() + __n);
+    __x += __n;
+    return __x;
 }
 
 #ifdef _LIBCPP_DEBUG