diff --git a/include/__config b/include/__config index 27516199..3f6bfd45 100644 --- a/include/__config +++ b/include/__config @@ -309,6 +309,16 @@ template struct __static_assert_check {}; #define _LIBCPP_WCTYPE_IS_MASK #endif +#ifdef _LIBCPP_DEBUG2 +# if _LIBCPP_DEBUG2 == 0 +# define _LIBCPP_DEBUG_LEVEL 1 +# elif _LIBCPP_DEBUG2 == 1 +# define _LIBCPP_DEBUG_LEVEL 2 +# else +# error Supported values for _LIBCPP_DEBUG2 are 0 and 1 +# endif +#endif + #ifdef _LIBCPP_DEBUG2 # include <__debug> #else diff --git a/include/__debug b/include/__debug index e7d027ad..4e80c6f8 100644 --- a/include/__debug +++ b/include/__debug @@ -11,11 +11,17 @@ #ifndef _LIBCPP_DEBUG_H #define _LIBCPP_DEBUG_H +#if _LIBCPP_DEBUG_LEVEL >= 1 + # include # include # include # define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : (_VSTD::printf("%s\n", m), _VSTD::abort())) +#endif + +#if _LIBCPP_DEBUG_LEVEL >= 2 + _LIBCPP_BEGIN_NAMESPACE_STD struct _LIBCPP_VISIBLE __c_node; @@ -177,5 +183,7 @@ _LIBCPP_VISIBLE const __libcpp_db* __get_const_db(); _LIBCPP_END_NAMESPACE_STD +#endif + #endif // _LIBCPP_DEBUG_H diff --git a/include/iterator b/include/iterator index ed2d7a71..d5f1eb7d 100644 --- a/include/iterator +++ b/include/iterator @@ -1068,11 +1068,11 @@ public: typename enable_if::value>::type* = 0) _NOEXCEPT : __i(__u.base()) { -#ifdef _LIBCPP_DEBUG2 +#if _LIBCPP_DEBUG_LEVEL >= 2 __get_db()->__iterator_copy(this, &__u); #endif } -#ifdef _LIBCPP_DEBUG2 +#if _LIBCPP_DEBUG_LEVEL >= 2 _LIBCPP_INLINE_VISIBILITY __wrap_iter(const __wrap_iter& __x) : __i(__x.base()) @@ -1097,15 +1097,19 @@ public: #endif _LIBCPP_INLINE_VISIBILITY reference operator*() const _NOEXCEPT { +#if _LIBCPP_DEBUG_LEVEL >= 2 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this), "Attempted to dereference a non-dereferenceable iterator"); +#endif return *__i; } _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return &(operator*());} _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator++() _NOEXCEPT { +#if _LIBCPP_DEBUG_LEVEL >= 2 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this), "Attempted to increment non-incrementable iterator"); +#endif ++__i; return *this; } @@ -1113,8 +1117,10 @@ public: {__wrap_iter __tmp(*this); ++(*this); return __tmp;} _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator--() _NOEXCEPT { +#if _LIBCPP_DEBUG_LEVEL >= 2 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this), "Attempted to decrement non-decrementable iterator"); +#endif --__i; return *this; } @@ -1124,8 +1130,10 @@ public: {__wrap_iter __w(*this); __w += __n; return __w;} _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator+=(difference_type __n) _NOEXCEPT { +#if _LIBCPP_DEBUG_LEVEL >= 2 _LIBCPP_ASSERT(__get_const_db()->__addable(this, __n), "Attempted to add/subtract iterator outside of valid range"); +#endif __i += __n; return *this; } @@ -1135,8 +1143,10 @@ public: {*this += -__n; return *this;} _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const _NOEXCEPT { +#if _LIBCPP_DEBUG_LEVEL >= 2 _LIBCPP_ASSERT(__get_const_db()->__subscriptable(this, __n), "Attempted to subscript iterator outside of valid range"); +#endif return __i[__n]; } @@ -1144,7 +1154,7 @@ public: private: _LIBCPP_INLINE_VISIBILITY __wrap_iter(iterator_type __x) _NOEXCEPT : __i(__x) {} -#ifdef _LIBCPP_DEBUG2 +#if _LIBCPP_DEBUG_LEVEL >= 2 _LIBCPP_INLINE_VISIBILITY __wrap_iter(const void* __p, iterator_type __x) : __i(__x) { __get_db()->__insert_ic(this, __p); @@ -1215,8 +1225,10 @@ inline _LIBCPP_INLINE_VISIBILITY bool operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT { +#if _LIBCPP_DEBUG_LEVEL >= 2 _LIBCPP_ASSERT(__get_const_db()->__comparable(&__x, &__y), "Attempted to compare incomparable iterators"); +#endif return __x.base() == __y.base(); } @@ -1225,8 +1237,10 @@ inline _LIBCPP_INLINE_VISIBILITY bool operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT { +#if _LIBCPP_DEBUG_LEVEL >= 2 _LIBCPP_ASSERT(__get_const_db()->__comparable(&__x, &__y), "Attempted to compare incomparable iterators"); +#endif return __x.base() < __y.base(); } @@ -1267,8 +1281,10 @@ inline _LIBCPP_INLINE_VISIBILITY typename __wrap_iter<_Iter1>::difference_type operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT { +#if _LIBCPP_DEBUG_LEVEL >= 2 _LIBCPP_ASSERT(__get_const_db()->__comparable(&__x, &__y), "Attempted to subtract incompatible iterators"); +#endif return __x.base() - __y.base(); } diff --git a/include/vector b/include/vector index d1a67ce2..5c3a6978 100644 --- a/include/vector +++ b/include/vector @@ -269,9 +269,6 @@ void swap(vector& x, vector& y) #include #include <__split_buffer> #include <__functional_base> -#if defined(_LIBCPP_DEBUG) || defined(_LIBCPP_NO_EXCEPTIONS) - #include -#endif #pragma GCC system_header @@ -489,24 +486,8 @@ public: typedef typename __base::difference_type difference_type; typedef typename __base::pointer pointer; typedef typename __base::const_pointer const_pointer; -#ifdef _LIBCPP_DEBUG - typedef __debug_iter iterator; - typedef __debug_iter const_iterator; - - friend class __debug_iter; - friend class __debug_iter; - - pair __iterator_list_; - - _LIBCPP_INLINE_VISIBILITY iterator*& __get_iterator_list(iterator*) {return __iterator_list_.first;} - _LIBCPP_INLINE_VISIBILITY const_iterator*& __get_iterator_list(const_iterator*) {return __iterator_list_.second;} -#elif defined(_LIBCPP_RAW_ITERATORS) - typedef pointer iterator; - typedef const_pointer const_iterator; -#else // defined(_LIBCPP_RAW_ITERATORS) typedef __wrap_iter iterator; typedef __wrap_iter const_iterator; -#endif // defined(_LIBCPP_RAW_ITERATORS) typedef _VSTD::reverse_iterator reverse_iterator; typedef _VSTD::reverse_iterator const_reverse_iterator; @@ -514,14 +495,14 @@ public: vector() _NOEXCEPT_(is_nothrow_default_constructible::value) { -#ifdef _LIBCPP_DEBUG2 +#if _LIBCPP_DEBUG_LEVEL >= 2 __get_db()->__insert_c(this); #endif } _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a) : __base(__a) { -#ifdef _LIBCPP_DEBUG2 +#if _LIBCPP_DEBUG_LEVEL >= 2 __get_db()->__insert_c(this); #endif } @@ -548,7 +529,7 @@ public: _LIBCPP_INLINE_VISIBILITY vector(initializer_list __il, const allocator_type& __a); #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS -#ifdef _LIBCPP_DEBUG2 +#if _LIBCPP_DEBUG_LEVEL >= 2 _LIBCPP_INLINE_VISIBILITY ~vector() { @@ -741,14 +722,14 @@ public: bool __invariants() const; -#ifdef _LIBCPP_DEBUG2 +#if _LIBCPP_DEBUG_LEVEL >= 2 bool __dereferenceable(const const_iterator* __i) const; bool __decrementable(const const_iterator* __i) const; bool __addable(const const_iterator* __i, ptrdiff_t __n) const; bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const; -#endif // _LIBCPP_DEBUG2 +#endif // _LIBCPP_DEBUG_LEVEL >= 2 private: _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators(); @@ -780,7 +761,7 @@ private: _LIBCPP_INLINE_VISIBILITY void __destruct_at_end(const_pointer __new_last) _NOEXCEPT { -#ifdef _LIBCPP_DEBUG2 +#if _LIBCPP_DEBUG_LEVEL >= 2 __c_node* __c = __get_db()->__find_c_and_lock(this); for (__i_node** __p = __c->end_; __p != __c->beg_; ) { @@ -994,7 +975,7 @@ vector<_Tp, _Allocator>::vector(size_type __n) allocate(__n); __construct_at_end(__n); } -#ifdef _LIBCPP_DEBUG2 +#if _LIBCPP_DEBUG_LEVEL >= 2 __get_db()->__insert_c(this); #endif } @@ -1007,7 +988,7 @@ vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x) allocate(__n); __construct_at_end(__n, __x); } -#ifdef _LIBCPP_DEBUG2 +#if _LIBCPP_DEBUG_LEVEL >= 2 __get_db()->__insert_c(this); #endif } @@ -1021,7 +1002,7 @@ vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const alloca allocate(__n); __construct_at_end(__n, __x); } -#ifdef _LIBCPP_DEBUG2 +#if _LIBCPP_DEBUG_LEVEL >= 2 __get_db()->__insert_c(this); #endif } @@ -1034,7 +1015,7 @@ vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, { for (; __first != __last; ++__first) push_back(*__first); -#ifdef _LIBCPP_DEBUG2 +#if _LIBCPP_DEBUG_LEVEL >= 2 __get_db()->__insert_c(this); #endif } @@ -1048,7 +1029,7 @@ vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, c { for (; __first != __last; ++__first) push_back(*__first); -#ifdef _LIBCPP_DEBUG2 +#if _LIBCPP_DEBUG_LEVEL >= 2 __get_db()->__insert_c(this); #endif } @@ -1064,7 +1045,7 @@ vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __las allocate(__n); __construct_at_end(__first, __last); } -#ifdef _LIBCPP_DEBUG2 +#if _LIBCPP_DEBUG_LEVEL >= 2 __get_db()->__insert_c(this); #endif } @@ -1081,7 +1062,7 @@ vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __las allocate(__n); __construct_at_end(__first, __last); } -#ifdef _LIBCPP_DEBUG2 +#if _LIBCPP_DEBUG_LEVEL >= 2 __get_db()->__insert_c(this); #endif } @@ -1096,7 +1077,7 @@ vector<_Tp, _Allocator>::vector(const vector& __x) allocate(__n); __construct_at_end(__x.__begin_, __x.__end_); } -#ifdef _LIBCPP_DEBUG2 +#if _LIBCPP_DEBUG_LEVEL >= 2 __get_db()->__insert_c(this); #endif } @@ -1111,7 +1092,7 @@ vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a) allocate(__n); __construct_at_end(__x.__begin_, __x.__end_); } -#ifdef _LIBCPP_DEBUG2 +#if _LIBCPP_DEBUG_LEVEL >= 2 __get_db()->__insert_c(this); #endif } @@ -1128,7 +1109,7 @@ vector<_Tp, _Allocator>::vector(vector&& __x) this->__end_ = __x.__end_; this->__end_cap() = __x.__end_cap(); __x.__begin_ = __x.__end_ = __x.__end_cap() = 0; -#ifdef _LIBCPP_DEBUG2 +#if _LIBCPP_DEBUG_LEVEL >= 2 __x.__invalidate_all_iterators(); __get_db()->__insert_c(this); #endif @@ -1152,7 +1133,7 @@ vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a) typedef move_iterator _I; assign(_I(__x.begin()), _I(__x.end())); } -#ifdef _LIBCPP_DEBUG2 +#if _LIBCPP_DEBUG_LEVEL >= 2 __get_db()->__insert_c(this); #endif } @@ -1168,7 +1149,7 @@ vector<_Tp, _Allocator>::vector(initializer_list __il) allocate(__il.size()); __construct_at_end(__il.begin(), __il.end()); } -#ifdef _LIBCPP_DEBUG2 +#if _LIBCPP_DEBUG_LEVEL >= 2 __get_db()->__insert_c(this); #endif } @@ -1183,7 +1164,7 @@ vector<_Tp, _Allocator>::vector(initializer_list __il, const allocat allocate(__il.size()); __construct_at_end(__il.begin(), __il.end()); } -#ifdef _LIBCPP_DEBUG2 +#if _LIBCPP_DEBUG_LEVEL >= 2 __get_db()->__insert_c(this); #endif } @@ -1319,7 +1300,7 @@ _LIBCPP_INLINE_VISIBILITY inline typename vector<_Tp, _Allocator>::iterator vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT { -#ifdef _LIBCPP_DEBUG2 +#if _LIBCPP_DEBUG_LEVEL >= 2 return iterator(this, __p); #else return iterator(__p); @@ -1331,7 +1312,7 @@ _LIBCPP_INLINE_VISIBILITY inline typename vector<_Tp, _Allocator>::const_iterator vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT { -#ifdef _LIBCPP_DEBUG2 +#if _LIBCPP_DEBUG_LEVEL >= 2 return const_iterator(this, __p); #else return const_iterator(__p); @@ -1521,9 +1502,11 @@ _LIBCPP_INLINE_VISIBILITY inline typename vector<_Tp, _Allocator>::iterator vector<_Tp, _Allocator>::erase(const_iterator __position) { +#if _LIBCPP_DEBUG_LEVEL >= 2 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this, "vector::erase(iterator) called with an iterator not" " referring to this vector"); +#endif pointer __p = const_cast(&*__position); iterator __r = __make_iter(__p); this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p)); @@ -1534,9 +1517,11 @@ template typename vector<_Tp, _Allocator>::iterator vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last) { +#if _LIBCPP_DEBUG_LEVEL >= 2 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this, "vector::erase(iterator, iterator) called with an iterator not" " referring to this vector"); +#endif _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range"); pointer __p = this->__begin_ + (__first - begin()); iterator __r = __make_iter(__p); @@ -1561,9 +1546,11 @@ template typename vector<_Tp, _Allocator>::iterator vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x) { +#if _LIBCPP_DEBUG_LEVEL >= 2 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this, "vector::insert(iterator, x) called with an iterator not" " referring to this vector"); +#endif pointer __p = this->__begin_ + (__position - begin()); if (this->__end_ < this->__end_cap()) { @@ -1598,9 +1585,11 @@ template typename vector<_Tp, _Allocator>::iterator vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x) { +#if _LIBCPP_DEBUG_LEVEL >= 2 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this, "vector::insert(iterator, x) called with an iterator not" " referring to this vector"); +#endif pointer __p = this->__begin_ + (__position - begin()); if (this->__end_ < this->__end_cap()) { @@ -1634,9 +1623,11 @@ template typename vector<_Tp, _Allocator>::iterator vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args) { +#if _LIBCPP_DEBUG_LEVEL >= 2 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this, "vector::emplace(iterator, x) called with an iterator not" " referring to this vector"); +#endif pointer __p = this->__begin_ + (__position - begin()); if (this->__end_ < this->__end_cap()) { @@ -1670,9 +1661,11 @@ template typename vector<_Tp, _Allocator>::iterator vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x) { +#if _LIBCPP_DEBUG_LEVEL >= 2 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this, "vector::insert(iterator, n, x) called with an iterator not" " referring to this vector"); +#endif pointer __p = this->__begin_ + (__position - begin()); if (__n > 0) { @@ -1716,9 +1709,11 @@ typename enable_if >::type vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last) { +#if _LIBCPP_DEBUG_LEVEL >= 2 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this, "vector::insert(iterator, range) called with an iterator not" " referring to this vector"); +#endif difference_type __off = __position - begin(); pointer __p = this->__begin_ + __off; allocator_type& __a = this->__alloc(); @@ -1766,9 +1761,11 @@ typename enable_if >::type vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last) { +#if _LIBCPP_DEBUG_LEVEL >= 2 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this, "vector::insert(iterator, range) called with an iterator not" " referring to this vector"); +#endif pointer __p = this->__begin_ + (__position - begin()); difference_type __n = _VSTD::distance(__first, __last); if (__n > 0) @@ -1839,9 +1836,9 @@ vector<_Tp, _Allocator>::swap(vector& __x) _VSTD::swap(this->__end_, __x.__end_); _VSTD::swap(this->__end_cap(), __x.__end_cap()); __base::__swap_alloc(this->__alloc(), __x.__alloc()); -#ifdef _LIBCPP_DEBUG2 +#if _LIBCPP_DEBUG_LEVEL >= 2 __get_db()->swap(this, &__x); -#endif // _LIBCPP_DEBUG +#endif // _LIBCPP_DEBUG_LEVEL >= 2 } template @@ -1865,7 +1862,7 @@ vector<_Tp, _Allocator>::__invariants() const return true; } -#ifdef _LIBCPP_DEBUG2 +#if _LIBCPP_DEBUG_LEVEL >= 2 template bool @@ -1897,18 +1894,16 @@ vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __ return this->__begin_ <= __p && __p < this->__end_; } -#endif // LIBCPP_DEBUG2 +#endif // _LIBCPP_DEBUG_LEVEL >= 2 template _LIBCPP_INLINE_VISIBILITY inline void vector<_Tp, _Allocator>::__invalidate_all_iterators() { -#ifdef _LIBCPP_DEBUG2 +#if _LIBCPP_DEBUG_LEVEL >= 2 __get_db()->__invalidate_all(this); -// iterator::__remove_all(this); -// const_iterator::__remove_all(this); -#endif // _LIBCPP_DEBUG +#endif // _LIBCPP_DEBUG_LEVEL >= 2 } // vector diff --git a/src/debug.cpp b/src/debug.cpp index 42e5edb3..04d50634 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -7,7 +7,8 @@ // //===----------------------------------------------------------------------===// -#define _LIBCPP_DEBUG2 +#define _LIBCPP_DEBUG2 1 +#include "__config" #include "__debug" #include "functional" #include "algorithm" @@ -35,11 +36,6 @@ namespace { typedef mutex mutex_type; -// struct mutex_type -// { -// void lock() {} -// void unlock() {}; -// }; typedef lock_guard WLock; typedef lock_guard RLock;