xref: /openbsd-src/gnu/gcc/libstdc++-v3/include/debug/functions.h (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert // Debugging support implementation -*- C++ -*-
2*404b540aSrobert 
3*404b540aSrobert // Copyright (C) 2003, 2005, 2006
4*404b540aSrobert // Free Software Foundation, Inc.
5*404b540aSrobert //
6*404b540aSrobert // This file is part of the GNU ISO C++ Library.  This library is free
7*404b540aSrobert // software; you can redistribute it and/or modify it under the
8*404b540aSrobert // terms of the GNU General Public License as published by the
9*404b540aSrobert // Free Software Foundation; either version 2, or (at your option)
10*404b540aSrobert // any later version.
11*404b540aSrobert 
12*404b540aSrobert // This library is distributed in the hope that it will be useful,
13*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
14*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*404b540aSrobert // GNU General Public License for more details.
16*404b540aSrobert 
17*404b540aSrobert // You should have received a copy of the GNU General Public License along
18*404b540aSrobert // with this library; see the file COPYING.  If not, write to the Free
19*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20*404b540aSrobert // USA.
21*404b540aSrobert 
22*404b540aSrobert // As a special exception, you may use this file as part of a free software
23*404b540aSrobert // library without restriction.  Specifically, if other files instantiate
24*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
25*404b540aSrobert // this file and link it with other files to produce an executable, this
26*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
27*404b540aSrobert // the GNU General Public License.  This exception does not however
28*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
29*404b540aSrobert // the GNU General Public License.
30*404b540aSrobert 
31*404b540aSrobert /** @file debug/functions.h
32*404b540aSrobert  *  This file is a GNU debug extension to the Standard C++ Library.
33*404b540aSrobert  */
34*404b540aSrobert 
35*404b540aSrobert #ifndef _GLIBCXX_DEBUG_FUNCTIONS_H
36*404b540aSrobert #define _GLIBCXX_DEBUG_FUNCTIONS_H 1
37*404b540aSrobert 
38*404b540aSrobert #include <bits/c++config.h>
39*404b540aSrobert #include <stddef.h>                       // for ptrdiff_t
40*404b540aSrobert #include <bits/stl_iterator_base_types.h> // for iterator_traits, categories
41*404b540aSrobert #include <bits/cpp_type_traits.h>         // for __is_integer
42*404b540aSrobert 
43*404b540aSrobert namespace __gnu_debug
44*404b540aSrobert {
45*404b540aSrobert   template<typename _Iterator, typename _Sequence>
46*404b540aSrobert     class _Safe_iterator;
47*404b540aSrobert 
48*404b540aSrobert   // An arbitrary iterator pointer is not singular.
49*404b540aSrobert   inline bool
__check_singular_aux(const void *)50*404b540aSrobert   __check_singular_aux(const void*) { return false; }
51*404b540aSrobert 
52*404b540aSrobert   // We may have an iterator that derives from _Safe_iterator_base but isn't
53*404b540aSrobert   // a _Safe_iterator.
54*404b540aSrobert   template<typename _Iterator>
55*404b540aSrobert     inline bool
__check_singular(_Iterator & __x)56*404b540aSrobert     __check_singular(_Iterator& __x)
57*404b540aSrobert     { return __check_singular_aux(&__x); }
58*404b540aSrobert 
59*404b540aSrobert   /** Non-NULL pointers are nonsingular. */
60*404b540aSrobert   template<typename _Tp>
61*404b540aSrobert     inline bool
__check_singular(const _Tp * __ptr)62*404b540aSrobert     __check_singular(const _Tp* __ptr)
63*404b540aSrobert     { return __ptr == 0; }
64*404b540aSrobert 
65*404b540aSrobert   /** Safe iterators know if they are singular. */
66*404b540aSrobert   template<typename _Iterator, typename _Sequence>
67*404b540aSrobert     inline bool
__check_singular(const _Safe_iterator<_Iterator,_Sequence> & __x)68*404b540aSrobert     __check_singular(const _Safe_iterator<_Iterator, _Sequence>& __x)
69*404b540aSrobert     { return __x._M_singular(); }
70*404b540aSrobert 
71*404b540aSrobert   /** Assume that some arbitrary iterator is dereferenceable, because we
72*404b540aSrobert       can't prove that it isn't. */
73*404b540aSrobert   template<typename _Iterator>
74*404b540aSrobert     inline bool
__check_dereferenceable(_Iterator &)75*404b540aSrobert     __check_dereferenceable(_Iterator&)
76*404b540aSrobert     { return true; }
77*404b540aSrobert 
78*404b540aSrobert   /** Non-NULL pointers are dereferenceable. */
79*404b540aSrobert   template<typename _Tp>
80*404b540aSrobert     inline bool
__check_dereferenceable(const _Tp * __ptr)81*404b540aSrobert     __check_dereferenceable(const _Tp* __ptr)
82*404b540aSrobert     { return __ptr; }
83*404b540aSrobert 
84*404b540aSrobert   /** Safe iterators know if they are singular. */
85*404b540aSrobert   template<typename _Iterator, typename _Sequence>
86*404b540aSrobert     inline bool
__check_dereferenceable(const _Safe_iterator<_Iterator,_Sequence> & __x)87*404b540aSrobert     __check_dereferenceable(const _Safe_iterator<_Iterator, _Sequence>& __x)
88*404b540aSrobert     { return __x._M_dereferenceable(); }
89*404b540aSrobert 
90*404b540aSrobert   /** If the distance between two random access iterators is
91*404b540aSrobert    *  nonnegative, assume the range is valid.
92*404b540aSrobert   */
93*404b540aSrobert   template<typename _RandomAccessIterator>
94*404b540aSrobert     inline bool
__valid_range_aux2(const _RandomAccessIterator & __first,const _RandomAccessIterator & __last,std::random_access_iterator_tag)95*404b540aSrobert     __valid_range_aux2(const _RandomAccessIterator& __first,
96*404b540aSrobert 		       const _RandomAccessIterator& __last,
97*404b540aSrobert 		       std::random_access_iterator_tag)
98*404b540aSrobert     { return __last - __first >= 0; }
99*404b540aSrobert 
100*404b540aSrobert   /** Can't test for a valid range with input iterators, because
101*404b540aSrobert    *  iteration may be destructive. So we just assume that the range
102*404b540aSrobert    *  is valid.
103*404b540aSrobert   */
104*404b540aSrobert   template<typename _InputIterator>
105*404b540aSrobert     inline bool
__valid_range_aux2(const _InputIterator &,const _InputIterator &,std::input_iterator_tag)106*404b540aSrobert     __valid_range_aux2(const _InputIterator&, const _InputIterator&,
107*404b540aSrobert 		       std::input_iterator_tag)
108*404b540aSrobert     { return true; }
109*404b540aSrobert 
110*404b540aSrobert   /** We say that integral types for a valid range, and defer to other
111*404b540aSrobert    *  routines to realize what to do with integral types instead of
112*404b540aSrobert    *  iterators.
113*404b540aSrobert   */
114*404b540aSrobert   template<typename _Integral>
115*404b540aSrobert     inline bool
__valid_range_aux(const _Integral &,const _Integral &,std::__true_type)116*404b540aSrobert     __valid_range_aux(const _Integral&, const _Integral&, std::__true_type)
117*404b540aSrobert     { return true; }
118*404b540aSrobert 
119*404b540aSrobert   /** We have iterators, so figure out what kind of iterators that are
120*404b540aSrobert    *  to see if we can check the range ahead of time.
121*404b540aSrobert   */
122*404b540aSrobert   template<typename _InputIterator>
123*404b540aSrobert     inline bool
__valid_range_aux(const _InputIterator & __first,const _InputIterator & __last,std::__false_type)124*404b540aSrobert     __valid_range_aux(const _InputIterator& __first,
125*404b540aSrobert 		      const _InputIterator& __last, std::__false_type)
126*404b540aSrobert   {
127*404b540aSrobert     typedef typename std::iterator_traits<_InputIterator>::iterator_category
128*404b540aSrobert       _Category;
129*404b540aSrobert     return __valid_range_aux2(__first, __last, _Category());
130*404b540aSrobert   }
131*404b540aSrobert 
132*404b540aSrobert   /** Don't know what these iterators are, or if they are even
133*404b540aSrobert    *  iterators (we may get an integral type for InputIterator), so
134*404b540aSrobert    *  see if they are integral and pass them on to the next phase
135*404b540aSrobert    *  otherwise.
136*404b540aSrobert   */
137*404b540aSrobert   template<typename _InputIterator>
138*404b540aSrobert     inline bool
__valid_range(const _InputIterator & __first,const _InputIterator & __last)139*404b540aSrobert     __valid_range(const _InputIterator& __first, const _InputIterator& __last)
140*404b540aSrobert     {
141*404b540aSrobert       typedef typename std::__is_integer<_InputIterator>::__type _Integral;
142*404b540aSrobert       return __valid_range_aux(__first, __last, _Integral());
143*404b540aSrobert     }
144*404b540aSrobert 
145*404b540aSrobert   /** Safe iterators know how to check if they form a valid range. */
146*404b540aSrobert   template<typename _Iterator, typename _Sequence>
147*404b540aSrobert     inline bool
__valid_range(const _Safe_iterator<_Iterator,_Sequence> & __first,const _Safe_iterator<_Iterator,_Sequence> & __last)148*404b540aSrobert     __valid_range(const _Safe_iterator<_Iterator, _Sequence>& __first,
149*404b540aSrobert 		  const _Safe_iterator<_Iterator, _Sequence>& __last)
150*404b540aSrobert     { return __first._M_valid_range(__last); }
151*404b540aSrobert 
152*404b540aSrobert   /* Checks that [first, last) is a valid range, and then returns
153*404b540aSrobert    * __first. This routine is useful when we can't use a separate
154*404b540aSrobert    * assertion statement because, e.g., we are in a constructor.
155*404b540aSrobert   */
156*404b540aSrobert   template<typename _InputIterator>
157*404b540aSrobert     inline _InputIterator
__check_valid_range(const _InputIterator & __first,const _InputIterator & __last)158*404b540aSrobert     __check_valid_range(const _InputIterator& __first,
159*404b540aSrobert 			const _InputIterator& __last
160*404b540aSrobert 			__attribute__((__unused__)))
161*404b540aSrobert     {
162*404b540aSrobert       _GLIBCXX_DEBUG_ASSERT(__valid_range(__first, __last));
163*404b540aSrobert       return __first;
164*404b540aSrobert     }
165*404b540aSrobert 
166*404b540aSrobert   /** Checks that __s is non-NULL or __n == 0, and then returns __s. */
167*404b540aSrobert   template<typename _CharT, typename _Integer>
168*404b540aSrobert     inline const _CharT*
__check_string(const _CharT * __s,const _Integer & __n)169*404b540aSrobert     __check_string(const _CharT* __s,
170*404b540aSrobert 		   const _Integer& __n __attribute__((__unused__)))
171*404b540aSrobert     {
172*404b540aSrobert #ifdef _GLIBCXX_DEBUG_PEDANTIC
173*404b540aSrobert       _GLIBCXX_DEBUG_ASSERT(__s != 0 || __n == 0);
174*404b540aSrobert #endif
175*404b540aSrobert       return __s;
176*404b540aSrobert     }
177*404b540aSrobert 
178*404b540aSrobert   /** Checks that __s is non-NULL and then returns __s. */
179*404b540aSrobert   template<typename _CharT>
180*404b540aSrobert     inline const _CharT*
__check_string(const _CharT * __s)181*404b540aSrobert     __check_string(const _CharT* __s)
182*404b540aSrobert     {
183*404b540aSrobert #ifdef _GLIBCXX_DEBUG_PEDANTIC
184*404b540aSrobert       _GLIBCXX_DEBUG_ASSERT(__s != 0);
185*404b540aSrobert #endif
186*404b540aSrobert       return __s;
187*404b540aSrobert     }
188*404b540aSrobert 
189*404b540aSrobert   // Can't check if an input iterator sequence is sorted, because we
190*404b540aSrobert   // can't step through the sequence.
191*404b540aSrobert   template<typename _InputIterator>
192*404b540aSrobert     inline bool
__check_sorted_aux(const _InputIterator &,const _InputIterator &,std::input_iterator_tag)193*404b540aSrobert     __check_sorted_aux(const _InputIterator&, const _InputIterator&,
194*404b540aSrobert                        std::input_iterator_tag)
195*404b540aSrobert     { return true; }
196*404b540aSrobert 
197*404b540aSrobert   // Can verify if a forward iterator sequence is in fact sorted using
198*404b540aSrobert   // std::__is_sorted
199*404b540aSrobert   template<typename _ForwardIterator>
200*404b540aSrobert     inline bool
__check_sorted_aux(_ForwardIterator __first,_ForwardIterator __last,std::forward_iterator_tag)201*404b540aSrobert     __check_sorted_aux(_ForwardIterator __first, _ForwardIterator __last,
202*404b540aSrobert                        std::forward_iterator_tag)
203*404b540aSrobert     {
204*404b540aSrobert       if (__first == __last)
205*404b540aSrobert         return true;
206*404b540aSrobert 
207*404b540aSrobert       _ForwardIterator __next = __first;
208*404b540aSrobert       for (++__next; __next != __last; __first = __next, ++__next) {
209*404b540aSrobert         if (*__next < *__first)
210*404b540aSrobert           return false;
211*404b540aSrobert       }
212*404b540aSrobert 
213*404b540aSrobert       return true;
214*404b540aSrobert     }
215*404b540aSrobert 
216*404b540aSrobert   // Can't check if an input iterator sequence is sorted, because we can't step
217*404b540aSrobert   // through the sequence.
218*404b540aSrobert   template<typename _InputIterator, typename _Predicate>
219*404b540aSrobert     inline bool
__check_sorted_aux(const _InputIterator &,const _InputIterator &,_Predicate,std::input_iterator_tag)220*404b540aSrobert     __check_sorted_aux(const _InputIterator&, const _InputIterator&,
221*404b540aSrobert                        _Predicate, std::input_iterator_tag)
222*404b540aSrobert     { return true; }
223*404b540aSrobert 
224*404b540aSrobert   // Can verify if a forward iterator sequence is in fact sorted using
225*404b540aSrobert   // std::__is_sorted
226*404b540aSrobert   template<typename _ForwardIterator, typename _Predicate>
227*404b540aSrobert     inline bool
__check_sorted_aux(_ForwardIterator __first,_ForwardIterator __last,_Predicate __pred,std::forward_iterator_tag)228*404b540aSrobert     __check_sorted_aux(_ForwardIterator __first, _ForwardIterator __last,
229*404b540aSrobert                        _Predicate __pred, std::forward_iterator_tag)
230*404b540aSrobert     {
231*404b540aSrobert       if (__first == __last)
232*404b540aSrobert         return true;
233*404b540aSrobert 
234*404b540aSrobert       _ForwardIterator __next = __first;
235*404b540aSrobert       for (++__next; __next != __last; __first = __next, ++__next) {
236*404b540aSrobert         if (__pred(*__next, *__first))
237*404b540aSrobert           return false;
238*404b540aSrobert       }
239*404b540aSrobert 
240*404b540aSrobert       return true;
241*404b540aSrobert     }
242*404b540aSrobert 
243*404b540aSrobert   // Determine if a sequence is sorted.
244*404b540aSrobert   template<typename _InputIterator>
245*404b540aSrobert     inline bool
__check_sorted(const _InputIterator & __first,const _InputIterator & __last)246*404b540aSrobert     __check_sorted(const _InputIterator& __first, const _InputIterator& __last)
247*404b540aSrobert     {
248*404b540aSrobert       typedef typename std::iterator_traits<_InputIterator>::iterator_category
249*404b540aSrobert         _Category;
250*404b540aSrobert       return __check_sorted_aux(__first, __last, _Category());
251*404b540aSrobert     }
252*404b540aSrobert 
253*404b540aSrobert   template<typename _InputIterator, typename _Predicate>
254*404b540aSrobert     inline bool
__check_sorted(const _InputIterator & __first,const _InputIterator & __last,_Predicate __pred)255*404b540aSrobert     __check_sorted(const _InputIterator& __first, const _InputIterator& __last,
256*404b540aSrobert                    _Predicate __pred)
257*404b540aSrobert     {
258*404b540aSrobert       typedef typename std::iterator_traits<_InputIterator>::iterator_category
259*404b540aSrobert         _Category;
260*404b540aSrobert       return __check_sorted_aux(__first, __last, __pred,
261*404b540aSrobert 					     _Category());
262*404b540aSrobert     }
263*404b540aSrobert 
264*404b540aSrobert   // _GLIBCXX_RESOLVE_LIB_DEFECTS
265*404b540aSrobert   // 270. Binary search requirements overly strict
266*404b540aSrobert   // Determine if a sequence is partitioned w.r.t. this element.
267*404b540aSrobert   template<typename _ForwardIterator, typename _Tp>
268*404b540aSrobert     inline bool
__check_partitioned(_ForwardIterator __first,_ForwardIterator __last,const _Tp & __value)269*404b540aSrobert     __check_partitioned(_ForwardIterator __first, _ForwardIterator __last,
270*404b540aSrobert 			const _Tp& __value)
271*404b540aSrobert     {
272*404b540aSrobert       while (__first != __last && *__first < __value)
273*404b540aSrobert 	++__first;
274*404b540aSrobert       while (__first != __last && !(*__first < __value))
275*404b540aSrobert 	++__first;
276*404b540aSrobert       return __first == __last;
277*404b540aSrobert     }
278*404b540aSrobert 
279*404b540aSrobert   // Determine if a sequence is partitioned w.r.t. this element.
280*404b540aSrobert   template<typename _ForwardIterator, typename _Tp, typename _Pred>
281*404b540aSrobert     inline bool
__check_partitioned(_ForwardIterator __first,_ForwardIterator __last,const _Tp & __value,_Pred __pred)282*404b540aSrobert     __check_partitioned(_ForwardIterator __first, _ForwardIterator __last,
283*404b540aSrobert 			const _Tp& __value, _Pred __pred)
284*404b540aSrobert     {
285*404b540aSrobert       while (__first != __last && __pred(*__first, __value))
286*404b540aSrobert 	++__first;
287*404b540aSrobert       while (__first != __last && !__pred(*__first, __value))
288*404b540aSrobert 	++__first;
289*404b540aSrobert       return __first == __last;
290*404b540aSrobert     }
291*404b540aSrobert } // namespace __gnu_debug
292*404b540aSrobert 
293*404b540aSrobert #endif
294