1 // Debugging support implementation -*- C++ -*-
3 // Copyright (C) 2003-2021 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/helper_functions.h
26 * This file is a GNU debug extension to the Standard C++ Library.
29 #ifndef _GLIBCXX_DEBUG_HELPER_FUNCTIONS_H
30 #define _GLIBCXX_DEBUG_HELPER_FUNCTIONS_H 1
32 #include <bits/move.h> // for __addressof
33 #include <bits/stl_iterator_base_types.h> // for iterator_traits,
34 // categories and _Iter_base
35 #include <bits/cpp_type_traits.h> // for __is_integer
37 #include <bits/stl_pair.h> // for pair
41 template<typename _Iterator
, typename _Sequence
, typename _Category
>
44 #if __cplusplus >= 201103L
45 template<typename _Iterator
, typename _Sequence
>
46 class _Safe_local_iterator
;
49 /** The precision to which we can calculate the distance between
52 enum _Distance_precision
54 __dp_none
, // Not even an iterator type
55 __dp_equality
, //< Can compare iterator equality, only
56 __dp_sign
, //< Can determine equality and ordering
57 __dp_sign_max_size
, //< __dp_sign and gives max range size
58 __dp_exact
//< Can determine distance precisely
61 template<typename _Iterator
,
62 typename
= typename
std::__is_integer
<_Iterator
>::__type
>
63 struct _Distance_traits
67 typename
std::iterator_traits
<_Iterator
>::difference_type _ItDiffType
;
69 template<typename _DiffType
,
70 typename
= typename
std::__is_void
<_DiffType
>::__type
>
72 { typedef _DiffType __type
; };
74 template<typename _DiffType
>
75 struct _DiffTraits
<_DiffType
, std::__true_type
>
76 { typedef std::ptrdiff_t __type
; };
78 typedef typename _DiffTraits
<_ItDiffType
>::__type _DiffType
;
81 typedef std::pair
<_DiffType
, _Distance_precision
> __type
;
84 template<typename _Integral
>
85 struct _Distance_traits
<_Integral
, std::__true_type
>
86 { typedef std::pair
<std::ptrdiff_t, _Distance_precision
> __type
; };
88 /** Determine the distance between two iterators with some known
91 template<typename _Iterator
>
93 inline typename _Distance_traits
<_Iterator
>::__type
94 __get_distance(_Iterator __lhs
, _Iterator __rhs
,
95 std::random_access_iterator_tag
)
96 { return std::make_pair(__rhs
- __lhs
, __dp_exact
); }
98 template<typename _Iterator
>
100 inline typename _Distance_traits
<_Iterator
>::__type
101 __get_distance(_Iterator __lhs
, _Iterator __rhs
,
102 std::input_iterator_tag
)
105 return std::make_pair(0, __dp_exact
);
107 return std::make_pair(1, __dp_equality
);
110 template<typename _Iterator
>
112 inline typename _Distance_traits
<_Iterator
>::__type
113 __get_distance(_Iterator __lhs
, _Iterator __rhs
)
114 { return __get_distance(__lhs
, __rhs
, std::__iterator_category(__lhs
)); }
116 // An arbitrary iterator pointer is not singular.
118 __check_singular_aux(const void*) { return false; }
120 // We may have an iterator that derives from _Safe_iterator_base but isn't
122 template<typename _Iterator
>
125 __check_singular(_Iterator
const& __x
)
128 #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
129 __builtin_is_constant_evaluated() ? false :
131 __check_singular_aux(std::__addressof(__x
));
134 /** Non-NULL pointers are nonsingular. */
135 template<typename _Tp
>
138 __check_singular(_Tp
* const& __ptr
)
141 #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
142 __builtin_is_constant_evaluated() ? false :
147 /** We say that integral types for a valid range, and defer to other
148 * routines to realize what to do with integral types instead of
151 template<typename _Integral
>
154 __valid_range_aux(_Integral
, _Integral
, std::__true_type
)
157 template<typename _Integral
>
160 __valid_range_aux(_Integral
, _Integral
,
161 typename _Distance_traits
<_Integral
>::__type
& __dist
,
164 __dist
= std::make_pair(0, __dp_none
);
168 template<typename _InputIterator
>
171 __valid_range_aux(_InputIterator __first
, _InputIterator __last
,
172 std::input_iterator_tag
)
174 return __first
== __last
175 || (!__check_singular(__first
) && !__check_singular(__last
));
178 template<typename _InputIterator
>
181 __valid_range_aux(_InputIterator __first
, _InputIterator __last
,
182 std::random_access_iterator_tag
)
185 __valid_range_aux(__first
, __last
, std::input_iterator_tag())
186 && __first
<= __last
;
189 /** We have iterators, so figure out what kind of iterators they are
190 * to see if we can check the range ahead of time.
192 template<typename _InputIterator
>
195 __valid_range_aux(_InputIterator __first
, _InputIterator __last
,
198 return __valid_range_aux(__first
, __last
,
199 std::__iterator_category(__first
));
202 template<typename _InputIterator
>
205 __valid_range_aux(_InputIterator __first
, _InputIterator __last
,
206 typename _Distance_traits
<_InputIterator
>::__type
& __dist
,
209 if (!__valid_range_aux(__first
, __last
, std::input_iterator_tag()))
212 __dist
= __get_distance(__first
, __last
);
213 switch (__dist
.second
)
218 if (__dist
.first
== 0)
222 case __dp_sign_max_size
:
224 return __dist
.first
>= 0;
227 // Can't tell so assume it is fine.
231 /** Don't know what these iterators are, or if they are even
232 * iterators (we may get an integral type for InputIterator), so
233 * see if they are integral and pass them on to the next phase
236 template<typename _InputIterator
>
239 __valid_range(_InputIterator __first
, _InputIterator __last
,
240 typename _Distance_traits
<_InputIterator
>::__type
& __dist
)
242 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
243 return __valid_range_aux(__first
, __last
, __dist
, _Integral());
246 template<typename _Iterator
, typename _Sequence
, typename _Category
>
248 __valid_range(const _Safe_iterator
<_Iterator
, _Sequence
, _Category
>&,
249 const _Safe_iterator
<_Iterator
, _Sequence
, _Category
>&,
250 typename _Distance_traits
<_Iterator
>::__type
&);
252 #if __cplusplus >= 201103L
253 template<typename _Iterator
,typename _Sequence
>
255 __valid_range(const _Safe_local_iterator
<_Iterator
, _Sequence
>&,
256 const _Safe_local_iterator
<_Iterator
, _Sequence
>&,
257 typename _Distance_traits
<_Iterator
>::__type
&);
260 template<typename _InputIterator
>
263 __valid_range(_InputIterator __first
, _InputIterator __last
)
265 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
266 return __valid_range_aux(__first
, __last
, _Integral());
269 template<typename _Iterator
, typename _Sequence
, typename _Category
>
271 __valid_range(const _Safe_iterator
<_Iterator
, _Sequence
, _Category
>&,
272 const _Safe_iterator
<_Iterator
, _Sequence
, _Category
>&);
274 #if __cplusplus >= 201103L
275 template<typename _Iterator
, typename _Sequence
>
277 __valid_range(const _Safe_local_iterator
<_Iterator
, _Sequence
>&,
278 const _Safe_local_iterator
<_Iterator
, _Sequence
>&);
281 // Fallback method, always ok.
282 template<typename _InputIterator
, typename _Size
>
285 __can_advance(_InputIterator
, _Size
)
288 template<typename _Iterator
, typename _Sequence
, typename _Category
,
291 __can_advance(const _Safe_iterator
<_Iterator
, _Sequence
, _Category
>&,
294 /** Helper function to extract base iterator of random access safe iterator
295 * in order to reduce performance impact of debug mode. Limited to random
296 * access iterator because it is the only category for which it is possible
297 * to check for correct iterators order in the __valid_range function
298 * thanks to the < operator.
300 template<typename _Iterator
>
303 __base(_Iterator __it
)
306 #if __cplusplus < 201103L
307 template<typename _Iterator
>
309 { typedef _Iterator _Type
; };
312 /* Remove debug mode safe iterator layer, if any. */
313 template<typename _Iterator
>
315 __unsafe(_Iterator __it
)