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 <bits/move.h> // for __addressof and addressof
37 #if __cplusplus >= 201103L
38 # include <bits/stl_function.h> // for less and greater_equal
39 # include <type_traits> // for is_lvalue_reference and __and_
41 #include <debug/formatter.h>
45 template<typename _Iterator
, typename _Sequence
>
48 template<typename _Sequence
>
49 struct _Insert_range_from_self_is_safe
50 { enum { __value
= 0 }; };
52 // An arbitrary iterator pointer is not singular.
54 __check_singular_aux(const void*) { return false; }
56 // We may have an iterator that derives from _Safe_iterator_base but isn't
58 template<typename _Iterator
>
60 __check_singular(_Iterator
& __x
)
61 { return __check_singular_aux(&__x
); }
63 /** Non-NULL pointers are nonsingular. */
64 template<typename _Tp
>
66 __check_singular(const _Tp
* __ptr
)
67 { return __ptr
== 0; }
69 /** Safe iterators know if they are singular. */
70 template<typename _Iterator
, typename _Sequence
>
72 __check_singular(const _Safe_iterator
<_Iterator
, _Sequence
>& __x
)
73 { return __x
._M_singular(); }
75 /** Assume that some arbitrary iterator is dereferenceable, because we
76 can't prove that it isn't. */
77 template<typename _Iterator
>
79 __check_dereferenceable(_Iterator
&)
82 /** Non-NULL pointers are dereferenceable. */
83 template<typename _Tp
>
85 __check_dereferenceable(const _Tp
* __ptr
)
88 /** Safe iterators know if they are singular. */
89 template<typename _Iterator
, typename _Sequence
>
91 __check_dereferenceable(const _Safe_iterator
<_Iterator
, _Sequence
>& __x
)
92 { return __x
._M_dereferenceable(); }
94 /** If the distance between two random access iterators is
95 * nonnegative, assume the range is valid.
97 template<typename _RandomAccessIterator
>
99 __valid_range_aux2(const _RandomAccessIterator
& __first
,
100 const _RandomAccessIterator
& __last
,
101 std::random_access_iterator_tag
)
102 { return __last
- __first
>= 0; }
104 /** Can't test for a valid range with input iterators, because
105 * iteration may be destructive. So we just assume that the range
108 template<typename _InputIterator
>
110 __valid_range_aux2(const _InputIterator
&, const _InputIterator
&,
111 std::input_iterator_tag
)
114 /** We say that integral types for a valid range, and defer to other
115 * routines to realize what to do with integral types instead of
118 template<typename _Integral
>
120 __valid_range_aux(const _Integral
&, const _Integral
&, std::__true_type
)
123 /** We have iterators, so figure out what kind of iterators that are
124 * to see if we can check the range ahead of time.
126 template<typename _InputIterator
>
128 __valid_range_aux(const _InputIterator
& __first
,
129 const _InputIterator
& __last
, std::__false_type
)
130 { return __valid_range_aux2(__first
, __last
,
131 std::__iterator_category(__first
)); }
133 /** Don't know what these iterators are, or if they are even
134 * iterators (we may get an integral type for InputIterator), so
135 * see if they are integral and pass them on to the next phase
138 template<typename _InputIterator
>
140 __valid_range(const _InputIterator
& __first
, const _InputIterator
& __last
)
142 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
143 return __valid_range_aux(__first
, __last
, _Integral());
146 /** Safe iterators know how to check if they form a valid range. */
147 template<typename _Iterator
, typename _Sequence
>
149 __valid_range(const _Safe_iterator
<_Iterator
, _Sequence
>& __first
,
150 const _Safe_iterator
<_Iterator
, _Sequence
>& __last
)
151 { return __first
._M_valid_range(__last
); }
153 /** Safe local iterators know how to check if they form a valid range. */
154 template<typename _Iterator
, typename _Sequence
>
156 __valid_range(const _Safe_local_iterator
<_Iterator
, _Sequence
>& __first
,
157 const _Safe_local_iterator
<_Iterator
, _Sequence
>& __last
)
158 { return __first
._M_valid_range(__last
); }
160 /* Checks that [first, last) is a valid range, and then returns
161 * __first. This routine is useful when we can't use a separate
162 * assertion statement because, e.g., we are in a constructor.
164 template<typename _InputIterator
>
165 inline _InputIterator
166 __check_valid_range(const _InputIterator
& __first
,
167 const _InputIterator
& __last
168 __attribute__((__unused__
)))
170 __glibcxx_check_valid_range(__first
, __last
);
174 #if __cplusplus >= 201103L
175 // Default implementation.
176 template<typename _Iterator
, typename _Sequence
>
178 __foreign_iterator_aux4(const _Safe_iterator
<_Iterator
, _Sequence
>& __it
,
179 typename
_Sequence::const_pointer __begin
,
180 typename
_Sequence::const_pointer __other
)
182 typedef typename
_Sequence::const_pointer _PointerType
;
183 constexpr std::less
<_PointerType
> __l
{};
185 return (__l(__other
, __begin
)
186 || __l(std::addressof(*(__it
._M_get_sequence()->_M_base().end()
190 // Fallback when address type cannot be implicitely casted to sequence
192 template<typename _Iterator
, typename _Sequence
,
193 typename _InputIterator
>
195 __foreign_iterator_aux4(const _Safe_iterator
<_Iterator
, _Sequence
>&,
199 template<typename _Iterator
, typename _Sequence
, typename _InputIterator
>
201 __foreign_iterator_aux3(const _Safe_iterator
<_Iterator
, _Sequence
>& __it
,
202 _InputIterator __other
,
205 // Only containers with all elements in contiguous memory can have their
206 // elements passed through pointers.
207 // Arithmetics is here just to make sure we are not dereferencing
208 // past-the-end iterator.
209 if (__it
._M_get_sequence()->_M_base().begin()
210 != __it
._M_get_sequence()->_M_base().end())
211 if (std::addressof(*(__it
._M_get_sequence()->_M_base().end() - 1))
212 - std::addressof(*(__it
._M_get_sequence()->_M_base().begin()))
213 == __it
._M_get_sequence()->size() - 1)
214 return (__foreign_iterator_aux4
216 std::addressof(*(__it
._M_get_sequence()->_M_base().begin())),
217 std::addressof(*__other
)));
221 /* Fallback overload for which we can't say, assume it is valid. */
222 template<typename _Iterator
, typename _Sequence
, typename _InputIterator
>
224 __foreign_iterator_aux3(const _Safe_iterator
<_Iterator
, _Sequence
>& __it
,
225 _InputIterator __other
,
230 /** Checks that iterators do not belong to the same sequence. */
231 template<typename _Iterator
, typename _Sequence
, typename _OtherIterator
>
233 __foreign_iterator_aux2(const _Safe_iterator
<_Iterator
, _Sequence
>& __it
,
234 const _Safe_iterator
<_OtherIterator
, _Sequence
>& __other
,
235 std::input_iterator_tag
)
236 { return __it
._M_get_sequence() != __other
._M_get_sequence(); }
238 #if __cplusplus >= 201103L
239 /* This overload detects when passing pointers to the contained elements
240 rather than using iterators.
242 template<typename _Iterator
, typename _Sequence
, typename _InputIterator
>
244 __foreign_iterator_aux2(const _Safe_iterator
<_Iterator
, _Sequence
>& __it
,
245 _InputIterator __other
,
246 std::random_access_iterator_tag
)
248 typedef typename
_Sequence::const_iterator _ItType
;
249 typedef typename
std::iterator_traits
<_ItType
>::reference _Ref
;
250 return __foreign_iterator_aux3(__it
, __other
,
251 std::is_lvalue_reference
<_Ref
>());
255 /* Fallback overload for which we can't say, assume it is valid. */
256 template<typename _Iterator
, typename _Sequence
, typename _InputIterator
>
258 __foreign_iterator_aux2(const _Safe_iterator
<_Iterator
, _Sequence
>&,
260 std::input_iterator_tag
)
263 template<typename _Iterator
, typename _Sequence
,
266 __foreign_iterator_aux(const _Safe_iterator
<_Iterator
, _Sequence
>& __it
,
271 template<typename _Iterator
, typename _Sequence
,
272 typename _InputIterator
>
274 __foreign_iterator_aux(const _Safe_iterator
<_Iterator
, _Sequence
>& __it
,
275 _InputIterator __other
,
278 return (_Insert_range_from_self_is_safe
<_Sequence
>::__value
279 || __foreign_iterator_aux2(__it
, __other
,
280 std::__iterator_category(__it
)));
283 template<typename _Iterator
, typename _Sequence
,
284 typename _InputIterator
>
286 __foreign_iterator(const _Safe_iterator
<_Iterator
, _Sequence
>& __it
,
287 _InputIterator __other
)
289 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
290 return __foreign_iterator_aux(__it
, __other
, _Integral());
293 /** Checks that __s is non-NULL or __n == 0, and then returns __s. */
294 template<typename _CharT
, typename _Integer
>
296 __check_string(const _CharT
* __s
,
297 const _Integer
& __n
__attribute__((__unused__
)))
299 #ifdef _GLIBCXX_DEBUG_PEDANTIC
300 __glibcxx_assert(__s
!= 0 || __n
== 0);
305 /** Checks that __s is non-NULL and then returns __s. */
306 template<typename _CharT
>
308 __check_string(const _CharT
* __s
)
310 #ifdef _GLIBCXX_DEBUG_PEDANTIC
311 __glibcxx_assert(__s
!= 0);
316 // Can't check if an input iterator sequence is sorted, because we
317 // can't step through the sequence.
318 template<typename _InputIterator
>
320 __check_sorted_aux(const _InputIterator
&, const _InputIterator
&,
321 std::input_iterator_tag
)
324 // Can verify if a forward iterator sequence is in fact sorted using
326 template<typename _ForwardIterator
>
328 __check_sorted_aux(_ForwardIterator __first
, _ForwardIterator __last
,
329 std::forward_iterator_tag
)
331 if (__first
== __last
)
334 _ForwardIterator __next
= __first
;
335 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
336 if (*__next
< *__first
)
342 // Can't check if an input iterator sequence is sorted, because we can't step
343 // through the sequence.
344 template<typename _InputIterator
, typename _Predicate
>
346 __check_sorted_aux(const _InputIterator
&, const _InputIterator
&,
347 _Predicate
, std::input_iterator_tag
)
350 // Can verify if a forward iterator sequence is in fact sorted using
352 template<typename _ForwardIterator
, typename _Predicate
>
354 __check_sorted_aux(_ForwardIterator __first
, _ForwardIterator __last
,
355 _Predicate __pred
, std::forward_iterator_tag
)
357 if (__first
== __last
)
360 _ForwardIterator __next
= __first
;
361 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
362 if (__pred(*__next
, *__first
))
368 // Determine if a sequence is sorted.
369 template<typename _InputIterator
>
371 __check_sorted(const _InputIterator
& __first
, const _InputIterator
& __last
)
373 // Verify that the < operator for elements in the sequence is a
374 // StrictWeakOrdering by checking that it is irreflexive.
375 __glibcxx_assert(__first
== __last
|| !(*__first
< *__first
));
377 return __check_sorted_aux(__first
, __last
,
378 std::__iterator_category(__first
));
381 template<typename _InputIterator
, typename _Predicate
>
383 __check_sorted(const _InputIterator
& __first
, const _InputIterator
& __last
,
386 // Verify that the predicate is StrictWeakOrdering by checking that it
388 __glibcxx_assert(__first
== __last
|| !__pred(*__first
, *__first
));
390 return __check_sorted_aux(__first
, __last
, __pred
,
391 std::__iterator_category(__first
));
394 template<typename _InputIterator
>
396 __check_sorted_set_aux(const _InputIterator
& __first
,
397 const _InputIterator
& __last
,
399 { return __check_sorted(__first
, __last
); }
401 template<typename _InputIterator
>
403 __check_sorted_set_aux(const _InputIterator
&,
404 const _InputIterator
&,
408 template<typename _InputIterator
, typename _Predicate
>
410 __check_sorted_set_aux(const _InputIterator
& __first
,
411 const _InputIterator
& __last
,
412 _Predicate __pred
, std::__true_type
)
413 { return __check_sorted(__first
, __last
, __pred
); }
415 template<typename _InputIterator
, typename _Predicate
>
417 __check_sorted_set_aux(const _InputIterator
&,
418 const _InputIterator
&, _Predicate
,
422 // ... special variant used in std::merge, std::includes, std::set_*.
423 template<typename _InputIterator1
, typename _InputIterator2
>
425 __check_sorted_set(const _InputIterator1
& __first
,
426 const _InputIterator1
& __last
,
427 const _InputIterator2
&)
429 typedef typename
std::iterator_traits
<_InputIterator1
>::value_type
431 typedef typename
std::iterator_traits
<_InputIterator2
>::value_type
434 typedef typename
std::__are_same
<_ValueType1
, _ValueType2
>::__type
436 return __check_sorted_set_aux(__first
, __last
, _SameType());
439 template<typename _InputIterator1
, typename _InputIterator2
,
442 __check_sorted_set(const _InputIterator1
& __first
,
443 const _InputIterator1
& __last
,
444 const _InputIterator2
&, _Predicate __pred
)
446 typedef typename
std::iterator_traits
<_InputIterator1
>::value_type
448 typedef typename
std::iterator_traits
<_InputIterator2
>::value_type
451 typedef typename
std::__are_same
<_ValueType1
, _ValueType2
>::__type
453 return __check_sorted_set_aux(__first
, __last
, __pred
, _SameType());
456 // _GLIBCXX_RESOLVE_LIB_DEFECTS
457 // 270. Binary search requirements overly strict
458 // Determine if a sequence is partitioned w.r.t. this element.
459 template<typename _ForwardIterator
, typename _Tp
>
461 __check_partitioned_lower(_ForwardIterator __first
,
462 _ForwardIterator __last
, const _Tp
& __value
)
464 while (__first
!= __last
&& *__first
< __value
)
466 if (__first
!= __last
)
469 while (__first
!= __last
&& !(*__first
< __value
))
472 return __first
== __last
;
475 template<typename _ForwardIterator
, typename _Tp
>
477 __check_partitioned_upper(_ForwardIterator __first
,
478 _ForwardIterator __last
, const _Tp
& __value
)
480 while (__first
!= __last
&& !(__value
< *__first
))
482 if (__first
!= __last
)
485 while (__first
!= __last
&& __value
< *__first
)
488 return __first
== __last
;
491 // Determine if a sequence is partitioned w.r.t. this element.
492 template<typename _ForwardIterator
, typename _Tp
, typename _Pred
>
494 __check_partitioned_lower(_ForwardIterator __first
,
495 _ForwardIterator __last
, const _Tp
& __value
,
498 while (__first
!= __last
&& bool(__pred(*__first
, __value
)))
500 if (__first
!= __last
)
503 while (__first
!= __last
&& !bool(__pred(*__first
, __value
)))
506 return __first
== __last
;
509 template<typename _ForwardIterator
, typename _Tp
, typename _Pred
>
511 __check_partitioned_upper(_ForwardIterator __first
,
512 _ForwardIterator __last
, const _Tp
& __value
,
515 while (__first
!= __last
&& !bool(__pred(__value
, *__first
)))
517 if (__first
!= __last
)
520 while (__first
!= __last
&& bool(__pred(__value
, *__first
)))
523 return __first
== __last
;
526 // Helper struct to detect random access safe iterators.
527 template<typename _Iterator
>
528 struct __is_safe_random_iterator
530 enum { __value
= 0 };
531 typedef std::__false_type __type
;
534 template<typename _Iterator
, typename _Sequence
>
535 struct __is_safe_random_iterator
<_Safe_iterator
<_Iterator
, _Sequence
> >
536 : std::__are_same
<std::random_access_iterator_tag
,
537 typename
std::iterator_traits
<_Iterator
>::
541 template<typename _Iterator
>
543 : std::_Iter_base
<_Iterator
, __is_safe_random_iterator
<_Iterator
>::__value
>
546 /** Helper function to extract base iterator of random access safe iterator
547 in order to reduce performance impact of debug mode. Limited to random
548 access iterator because it is the only category for which it is possible
549 to check for correct iterators order in the __valid_range function
550 thanks to the < operator.
552 template<typename _Iterator
>
553 inline typename _Siter_base
<_Iterator
>::iterator_type
554 __base(_Iterator __it
)
555 { return _Siter_base
<_Iterator
>::_S_base(__it
); }
556 } // namespace __gnu_debug