1 // Debugging support implementation -*- C++ -*-
3 // Copyright (C) 2003-2013 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file debug/functions.h
26 * This file is a GNU debug extension to the Standard C++ Library.
29 #ifndef _GLIBCXX_DEBUG_FUNCTIONS_H
30 #define _GLIBCXX_DEBUG_FUNCTIONS_H 1
32 #include <bits/c++config.h>
33 #include <bits/stl_iterator_base_types.h> // for iterator_traits, categories and
35 #include <bits/cpp_type_traits.h> // for __is_integer
36 #include <debug/formatter.h>
40 template<typename _Iterator, typename _Sequence>
43 // An arbitrary iterator pointer is not singular.
45 __check_singular_aux(const void*) { return false; }
47 // We may have an iterator that derives from _Safe_iterator_base but isn't
49 template<typename _Iterator>
51 __check_singular(_Iterator& __x)
52 { return __check_singular_aux(&__x); }
54 /** Non-NULL pointers are nonsingular. */
55 template<typename _Tp>
57 __check_singular(const _Tp* __ptr)
58 { return __ptr == 0; }
60 /** Safe iterators know if they are singular. */
61 template<typename _Iterator, typename _Sequence>
63 __check_singular(const _Safe_iterator<_Iterator, _Sequence>& __x)
64 { return __x._M_singular(); }
66 /** Assume that some arbitrary iterator is dereferenceable, because we
67 can't prove that it isn't. */
68 template<typename _Iterator>
70 __check_dereferenceable(_Iterator&)
73 /** Non-NULL pointers are dereferenceable. */
74 template<typename _Tp>
76 __check_dereferenceable(const _Tp* __ptr)
79 /** Safe iterators know if they are singular. */
80 template<typename _Iterator, typename _Sequence>
82 __check_dereferenceable(const _Safe_iterator<_Iterator, _Sequence>& __x)
83 { return __x._M_dereferenceable(); }
85 /** If the distance between two random access iterators is
86 * nonnegative, assume the range is valid.
88 template<typename _RandomAccessIterator>
90 __valid_range_aux2(const _RandomAccessIterator& __first,
91 const _RandomAccessIterator& __last,
92 std::random_access_iterator_tag)
93 { return __last - __first >= 0; }
95 /** Can't test for a valid range with input iterators, because
96 * iteration may be destructive. So we just assume that the range
99 template<typename _InputIterator>
101 __valid_range_aux2(const _InputIterator&, const _InputIterator&,
102 std::input_iterator_tag)
105 /** We say that integral types for a valid range, and defer to other
106 * routines to realize what to do with integral types instead of
109 template<typename _Integral>
111 __valid_range_aux(const _Integral&, const _Integral&, std::__true_type)
114 /** We have iterators, so figure out what kind of iterators that are
115 * to see if we can check the range ahead of time.
117 template<typename _InputIterator>
119 __valid_range_aux(const _InputIterator& __first,
120 const _InputIterator& __last, std::__false_type)
121 { return __valid_range_aux2(__first, __last,
122 std::__iterator_category(__first)); }
124 /** Don't know what these iterators are, or if they are even
125 * iterators (we may get an integral type for InputIterator), so
126 * see if they are integral and pass them on to the next phase
129 template<typename _InputIterator>
131 __valid_range(const _InputIterator& __first, const _InputIterator& __last)
133 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
134 return __valid_range_aux(__first, __last, _Integral());
137 /** Safe iterators know how to check if they form a valid range. */
138 template<typename _Iterator, typename _Sequence>
140 __valid_range(const _Safe_iterator<_Iterator, _Sequence>& __first,
141 const _Safe_iterator<_Iterator, _Sequence>& __last)
142 { return __first._M_valid_range(__last); }
144 /** Safe local iterators know how to check if they form a valid range. */
145 template<typename _Iterator, typename _Sequence>
147 __valid_range(const _Safe_local_iterator<_Iterator, _Sequence>& __first,
148 const _Safe_local_iterator<_Iterator, _Sequence>& __last)
149 { return __first._M_valid_range(__last); }
151 /* Checks that [first, last) is a valid range, and then returns
152 * __first. This routine is useful when we can't use a separate
153 * assertion statement because, e.g., we are in a constructor.
155 template<typename _InputIterator>
156 inline _InputIterator
157 __check_valid_range(const _InputIterator& __first,
158 const _InputIterator& __last
159 __attribute__((__unused__)))
161 __glibcxx_check_valid_range(__first, __last);
165 /** Checks that __s is non-NULL or __n == 0, and then returns __s. */
166 template<typename _CharT, typename _Integer>
168 __check_string(const _CharT* __s,
169 const _Integer& __n __attribute__((__unused__)))
171 #ifdef _GLIBCXX_DEBUG_PEDANTIC
172 __glibcxx_assert(__s != 0 || __n == 0);
177 /** Checks that __s is non-NULL and then returns __s. */
178 template<typename _CharT>
180 __check_string(const _CharT* __s)
182 #ifdef _GLIBCXX_DEBUG_PEDANTIC
183 __glibcxx_assert(__s != 0);
188 // Can't check if an input iterator sequence is sorted, because we
189 // can't step through the sequence.
190 template<typename _InputIterator>
192 __check_sorted_aux(const _InputIterator&, const _InputIterator&,
193 std::input_iterator_tag)
196 // Can verify if a forward iterator sequence is in fact sorted using
198 template<typename _ForwardIterator>
200 __check_sorted_aux(_ForwardIterator __first, _ForwardIterator __last,
201 std::forward_iterator_tag)
203 if (__first == __last)
206 _ForwardIterator __next = __first;
207 for (++__next; __next != __last; __first = __next, ++__next)
208 if (*__next < *__first)
214 // For performance reason, as the iterator range has been validated, check on
215 // random access safe iterators is done using the base iterator.
216 template<typename _Iterator, typename _Sequence>
218 __check_sorted_aux(const _Safe_iterator<_Iterator, _Sequence>& __first,
219 const _Safe_iterator<_Iterator, _Sequence>& __last,
220 std::random_access_iterator_tag __tag)
221 { return __check_sorted_aux(__first.base(), __last.base(), __tag); }
223 // Can't check if an input iterator sequence is sorted, because we can't step
224 // through the sequence.
225 template<typename _InputIterator, typename _Predicate>
227 __check_sorted_aux(const _InputIterator&, const _InputIterator&,
228 _Predicate, std::input_iterator_tag)
231 // Can verify if a forward iterator sequence is in fact sorted using
233 template<typename _ForwardIterator, typename _Predicate>
235 __check_sorted_aux(_ForwardIterator __first, _ForwardIterator __last,
236 _Predicate __pred, std::forward_iterator_tag)
238 if (__first == __last)
241 _ForwardIterator __next = __first;
242 for (++__next; __next != __last; __first = __next, ++__next)
243 if (__pred(*__next, *__first))
249 // For performance reason, as the iterator range has been validated, check on
250 // random access safe iterators is done using the base iterator.
251 template<typename _Iterator, typename _Sequence,
254 __check_sorted_aux(const _Safe_iterator<_Iterator, _Sequence>& __first,
255 const _Safe_iterator<_Iterator, _Sequence>& __last,
257 std::random_access_iterator_tag __tag)
258 { return __check_sorted_aux(__first.base(), __last.base(), __pred, __tag); }
260 // Determine if a sequence is sorted.
261 template<typename _InputIterator>
263 __check_sorted(const _InputIterator& __first, const _InputIterator& __last)
265 // Verify that the < operator for elements in the sequence is a
266 // StrictWeakOrdering by checking that it is irreflexive.
267 __glibcxx_assert(__first == __last || !(*__first < *__first));
269 return __check_sorted_aux(__first, __last,
270 std::__iterator_category(__first));
273 template<typename _InputIterator, typename _Predicate>
275 __check_sorted(const _InputIterator& __first, const _InputIterator& __last,
278 // Verify that the predicate is StrictWeakOrdering by checking that it
280 __glibcxx_assert(__first == __last || !__pred(*__first, *__first));
282 return __check_sorted_aux(__first, __last, __pred,
283 std::__iterator_category(__first));
286 template<typename _InputIterator>
288 __check_sorted_set_aux(const _InputIterator& __first,
289 const _InputIterator& __last,
291 { return __check_sorted(__first, __last); }
293 template<typename _InputIterator>
295 __check_sorted_set_aux(const _InputIterator&,
296 const _InputIterator&,
300 template<typename _InputIterator, typename _Predicate>
302 __check_sorted_set_aux(const _InputIterator& __first,
303 const _InputIterator& __last,
304 _Predicate __pred, std::__true_type)
305 { return __check_sorted(__first, __last, __pred); }
307 template<typename _InputIterator, typename _Predicate>
309 __check_sorted_set_aux(const _InputIterator&,
310 const _InputIterator&, _Predicate,
314 // ... special variant used in std::merge, std::includes, std::set_*.
315 template<typename _InputIterator1, typename _InputIterator2>
317 __check_sorted_set(const _InputIterator1& __first,
318 const _InputIterator1& __last,
319 const _InputIterator2&)
321 typedef typename std::iterator_traits<_InputIterator1>::value_type
323 typedef typename std::iterator_traits<_InputIterator2>::value_type
326 typedef typename std::__are_same<_ValueType1, _ValueType2>::__type
328 return __check_sorted_set_aux(__first, __last, _SameType());
331 template<typename _InputIterator1, typename _InputIterator2,
334 __check_sorted_set(const _InputIterator1& __first,
335 const _InputIterator1& __last,
336 const _InputIterator2&, _Predicate __pred)
338 typedef typename std::iterator_traits<_InputIterator1>::value_type
340 typedef typename std::iterator_traits<_InputIterator2>::value_type
343 typedef typename std::__are_same<_ValueType1, _ValueType2>::__type
345 return __check_sorted_set_aux(__first, __last, __pred, _SameType());
348 template<typename _ForwardIterator, typename _Tp>
350 __check_partitioned_lower_aux(_ForwardIterator __first,
351 _ForwardIterator __last, const _Tp& __value,
352 std::forward_iterator_tag)
354 while (__first != __last && *__first < __value)
356 if (__first != __last)
359 while (__first != __last && !(*__first < __value))
362 return __first == __last;
365 // For performance reason, as the iterator range has been validated, check on
366 // random access safe iterators is done using the base iterator.
367 template<typename _Iterator, typename _Sequence, typename _Tp>
369 __check_partitioned_lower_aux(
370 const _Safe_iterator<_Iterator, _Sequence>& __first,
371 const _Safe_iterator<_Iterator, _Sequence>& __last,
373 std::random_access_iterator_tag __tag)
375 return __check_partitioned_lower_aux(__first.base(), __last.base(),
379 // _GLIBCXX_RESOLVE_LIB_DEFECTS
380 // 270. Binary search requirements overly strict
381 // Determine if a sequence is partitioned w.r.t. this element.
382 template<typename _ForwardIterator, typename _Tp>
384 __check_partitioned_lower(_ForwardIterator __first,
385 _ForwardIterator __last, const _Tp& __value)
387 return __check_partitioned_lower_aux(__first, __last, __value,
388 std::__iterator_category(__first));
391 template<typename _ForwardIterator, typename _Tp>
393 __check_partitioned_upper_aux(_ForwardIterator __first,
394 _ForwardIterator __last, const _Tp& __value,
395 std::forward_iterator_tag)
397 while (__first != __last && !(__value < *__first))
399 if (__first != __last)
402 while (__first != __last && __value < *__first)
405 return __first == __last;
408 // For performance reason, as the iterator range has been validated, check on
409 // random access safe iterators is done using the base iterator.
410 template<typename _Iterator, typename _Sequence, typename _Tp>
412 __check_partitioned_upper_aux(
413 const _Safe_iterator<_Iterator, _Sequence>& __first,
414 const _Safe_iterator<_Iterator, _Sequence>& __last,
416 std::random_access_iterator_tag __tag)
418 return __check_partitioned_upper_aux(__first.base(), __last.base(),
422 template<typename _ForwardIterator, typename _Tp>
424 __check_partitioned_upper(_ForwardIterator __first,
425 _ForwardIterator __last, const _Tp& __value)
427 return __check_partitioned_upper_aux(__first, __last, __value,
428 std::__iterator_category(__first));
431 template<typename _ForwardIterator, typename _Tp, typename _Pred>
433 __check_partitioned_lower_aux(_ForwardIterator __first,
434 _ForwardIterator __last, const _Tp& __value,
436 std::forward_iterator_tag)
438 while (__first != __last && bool(__pred(*__first, __value)))
440 if (__first != __last)
443 while (__first != __last && !bool(__pred(*__first, __value)))
446 return __first == __last;
449 // For performance reason, as the iterator range has been validated, check on
450 // random access safe iterators is done using the base iterator.
451 template<typename _Iterator, typename _Sequence,
452 typename _Tp, typename _Pred>
454 __check_partitioned_lower_aux(
455 const _Safe_iterator<_Iterator, _Sequence>& __first,
456 const _Safe_iterator<_Iterator, _Sequence>& __last,
457 const _Tp& __value, _Pred __pred,
458 std::random_access_iterator_tag __tag)
460 return __check_partitioned_lower_aux(__first.base(), __last.base(),
461 __value, __pred, __tag);
464 // Determine if a sequence is partitioned w.r.t. this element.
465 template<typename _ForwardIterator, typename _Tp, typename _Pred>
467 __check_partitioned_lower(_ForwardIterator __first,
468 _ForwardIterator __last, const _Tp& __value,
471 return __check_partitioned_lower_aux(__first, __last, __value, __pred,
472 std::__iterator_category(__first));
475 template<typename _ForwardIterator, typename _Tp, typename _Pred>
477 __check_partitioned_upper_aux(_ForwardIterator __first,
478 _ForwardIterator __last, const _Tp& __value,
480 std::forward_iterator_tag)
482 while (__first != __last && !bool(__pred(__value, *__first)))
484 if (__first != __last)
487 while (__first != __last && bool(__pred(__value, *__first)))
490 return __first == __last;
493 // For performance reason, as the iterator range has been validated, check on
494 // random access safe iterators is done using the base iterator.
495 template<typename _Iterator, typename _Sequence,
496 typename _Tp, typename _Pred>
498 __check_partitioned_upper_aux(
499 const _Safe_iterator<_Iterator, _Sequence>& __first,
500 const _Safe_iterator<_Iterator, _Sequence>& __last,
501 const _Tp& __value, _Pred __pred,
502 std::random_access_iterator_tag __tag)
504 return __check_partitioned_upper_aux(__first.base(), __last.base(),
505 __value, __pred, __tag);
508 template<typename _ForwardIterator, typename _Tp, typename _Pred>
510 __check_partitioned_upper(_ForwardIterator __first,
511 _ForwardIterator __last, const _Tp& __value,
514 return __check_partitioned_upper_aux(__first, __last, __value, __pred,
515 std::__iterator_category(__first));
518 // Helper struct to detect random access safe iterators.
519 template<typename _Iterator>
520 struct __is_safe_random_iterator
522 enum { __value = 0 };
523 typedef std::__false_type __type;
526 template<typename _Iterator, typename _Sequence>
527 struct __is_safe_random_iterator<_Safe_iterator<_Iterator, _Sequence> >
528 : std::__are_same<std::random_access_iterator_tag,
529 typename std::iterator_traits<_Iterator>::
533 template<typename _Iterator>
535 : std::_Iter_base<_Iterator, __is_safe_random_iterator<_Iterator>::__value>
538 /** Helper function to extract base iterator of random access safe iterator
539 in order to reduce performance impact of debug mode. Limited to random
540 access iterator because it is the only category for which it is possible
541 to check for correct iterators order in the __valid_range function
542 thanks to the < operator.
544 template<typename _Iterator>
545 inline typename _Siter_base<_Iterator>::iterator_type
546 __base(_Iterator __it)
547 { return _Siter_base<_Iterator>::_S_base(__it); }
548 } // namespace __gnu_debug