]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/debug/macros.h
macros.h (__glibcxx_check_can_increment_range): New.
[thirdparty/gcc.git] / libstdc++-v3 / include / debug / macros.h
1 // Debugging support implementation -*- C++ -*-
2
3 // Copyright (C) 2003-2018 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10
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.
15
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.
19
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/>.
24
25 /** @file debug/macros.h
26 * This file is a GNU debug extension to the Standard C++ Library.
27 */
28
29 #ifndef _GLIBCXX_DEBUG_MACROS_H
30 #define _GLIBCXX_DEBUG_MACROS_H 1
31
32 /**
33 * Macros used by the implementation to verify certain
34 * properties. These macros may only be used directly by the debug
35 * wrappers. Note that these are macros (instead of the more obviously
36 * @a correct choice of making them functions) because we need line and
37 * file information at the call site, to minimize the distance between
38 * the user error and where the error is reported.
39 *
40 */
41 #define _GLIBCXX_DEBUG_VERIFY_COND_AT(_Cond,_ErrMsg,_File,_Line,_Func) \
42 if (! (_Cond)) \
43 __gnu_debug::_Error_formatter::_S_at(_File, _Line, _Func) \
44 ._ErrMsg._M_error()
45
46 #define _GLIBCXX_DEBUG_VERIFY_AT_F(_Cond,_ErrMsg,_File,_Line,_Func) \
47 do \
48 { \
49 _GLIBCXX_DEBUG_VERIFY_COND_AT(_Cond,_ErrMsg,_File,_Line,_Func); \
50 } while (false)
51
52 #define _GLIBCXX_DEBUG_VERIFY_AT(_Cond,_ErrMsg,_File,_Line) \
53 _GLIBCXX_DEBUG_VERIFY_AT_F(_Cond,_ErrMsg,_File,_Line,__PRETTY_FUNCTION__)
54
55 #define _GLIBCXX_DEBUG_VERIFY(_Cond,_ErrMsg) \
56 _GLIBCXX_DEBUG_VERIFY_AT_F(_Cond, _ErrMsg, __FILE__, __LINE__, \
57 __PRETTY_FUNCTION__)
58
59 // Verify that [_First, _Last) forms a valid iterator range.
60 #define __glibcxx_check_valid_range(_First,_Last) \
61 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__valid_range(_First, _Last), \
62 _M_message(__gnu_debug::__msg_valid_range) \
63 ._M_iterator(_First, #_First) \
64 ._M_iterator(_Last, #_Last))
65
66 #define __glibcxx_check_valid_range_at(_First,_Last,_File,_Line,_Func) \
67 _GLIBCXX_DEBUG_VERIFY_AT_F(__gnu_debug::__valid_range(_First, _Last), \
68 _M_message(__gnu_debug::__msg_valid_range) \
69 ._M_iterator(_First, #_First) \
70 ._M_iterator(_Last, #_Last), \
71 _File,_Line,_Func)
72
73 #define __glibcxx_check_valid_range2(_First,_Last,_Dist) \
74 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__valid_range(_First, _Last, _Dist), \
75 _M_message(__gnu_debug::__msg_valid_range) \
76 ._M_iterator(_First, #_First) \
77 ._M_iterator(_Last, #_Last))
78
79 #define __glibcxx_check_valid_constructor_range(_First,_Last) \
80 __gnu_debug::__check_valid_range(_First, _Last, \
81 __FILE__, __LINE__, __PRETTY_FUNCTION__)
82
83 // Verify that [_First, _Last) forms a non-empty iterator range.
84 #define __glibcxx_check_non_empty_range(_First,_Last) \
85 _GLIBCXX_DEBUG_VERIFY(_First != _Last, \
86 _M_message(__gnu_debug::__msg_non_empty_range) \
87 ._M_iterator(_First, #_First) \
88 ._M_iterator(_Last, #_Last))
89
90 #define __glibcxx_check_can_increment_range(_First1,_Last1,_First2) \
91 do \
92 { \
93 typename __gnu_debug::_Distance_traits<__decltype(_First1)>::__type __dist;\
94 _GLIBCXX_DEBUG_VERIFY_COND_AT( \
95 __gnu_debug::__valid_range(_First1, _Last1, __dist),\
96 _M_message(__gnu_debug::__msg_valid_range) \
97 ._M_iterator(_First1, #_First1) \
98 ._M_iterator(_Last1, #_Last1), \
99 __FILE__,__LINE__,__PRETTY_FUNCTION__); \
100 _GLIBCXX_DEBUG_VERIFY_COND_AT( \
101 __gnu_debug::__can_advance(_First2, __dist.first),\
102 _M_message(__gnu_debug::__msg_iter_subscript_oob)\
103 ._M_iterator(_First2, #_First2) \
104 ._M_integer(__dist.first), \
105 __FILE__,__LINE__,__PRETTY_FUNCTION__); \
106 } while(false)
107
108 #define __glibcxx_check_can_decrement_range(_First1,_Last1,_First2) \
109 do \
110 { \
111 typename __gnu_debug::_Distance_traits<__decltype(_First1)>::__type __dist;\
112 _GLIBCXX_DEBUG_VERIFY_COND_AT( \
113 __gnu_debug::__valid_range(_First1, _Last1, __dist),\
114 _M_message(__gnu_debug::__msg_valid_range) \
115 ._M_iterator(_First1, #_First1) \
116 ._M_iterator(_Last1, #_Last1), \
117 __FILE__,__LINE__,__PRETTY_FUNCTION__); \
118 _GLIBCXX_DEBUG_VERIFY_COND_AT( \
119 __gnu_debug::__can_advance(_First2, -__dist.first),\
120 _M_message(__gnu_debug::__msg_iter_subscript_oob)\
121 ._M_iterator(_First2, #_First2) \
122 ._M_integer(-__dist.first), \
123 __FILE__,__LINE__,__PRETTY_FUNCTION__); \
124 } while(false)
125
126 /** Verify that we can insert into *this with the iterator _Position.
127 * Insertion into a container at a specific position requires that
128 * the iterator be nonsingular, either dereferenceable or past-the-end,
129 * and that it reference the sequence we are inserting into. Note that
130 * this macro is only valid when the container is a_Safe_sequence and
131 * the iterator is a _Safe_iterator.
132 */
133 #define __glibcxx_check_insert(_Position) \
134 _GLIBCXX_DEBUG_VERIFY(!_Position._M_singular(), \
135 _M_message(__gnu_debug::__msg_insert_singular) \
136 ._M_sequence(*this, "this") \
137 ._M_iterator(_Position, #_Position)); \
138 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this), \
139 _M_message(__gnu_debug::__msg_insert_different) \
140 ._M_sequence(*this, "this") \
141 ._M_iterator(_Position, #_Position))
142
143 /** Verify that we can insert into *this after the iterator _Position.
144 * Insertion into a container after a specific position requires that
145 * the iterator be nonsingular, either dereferenceable or before-begin,
146 * and that it reference the sequence we are inserting into. Note that
147 * this macro is only valid when the container is a_Safe_sequence and
148 * the iterator is a _Safe_iterator.
149 */
150 #define __glibcxx_check_insert_after(_Position) \
151 __glibcxx_check_insert(_Position); \
152 _GLIBCXX_DEBUG_VERIFY(!_Position._M_is_end(), \
153 _M_message(__gnu_debug::__msg_insert_after_end) \
154 ._M_sequence(*this, "this") \
155 ._M_iterator(_Position, #_Position))
156
157 /** Verify that we can insert the values in the iterator range
158 * [_First, _Last) into *this with the iterator _Position. Insertion
159 * into a container at a specific position requires that the iterator
160 * be nonsingular (i.e., either dereferenceable or past-the-end),
161 * that it reference the sequence we are inserting into, and that the
162 * iterator range [_First, _Last) is a valid (possibly empty)
163 * range which does not reference the sequence we are inserting into.
164 * Note that this macro is only valid when the container is a
165 * _Safe_sequence and the _Position iterator is a _Safe_iterator.
166 */
167 #define __glibcxx_check_insert_range(_Position,_First,_Last,_Dist) \
168 __glibcxx_check_valid_range2(_First,_Last,_Dist); \
169 __glibcxx_check_insert(_Position); \
170 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__foreign_iterator(_Position,_First,_Last),\
171 _M_message(__gnu_debug::__msg_insert_range_from_self)\
172 ._M_iterator(_First, #_First) \
173 ._M_iterator(_Last, #_Last) \
174 ._M_sequence(*this, "this"))
175
176 /** Verify that we can insert the values in the iterator range
177 * [_First, _Last) into *this after the iterator _Position. Insertion
178 * into a container after a specific position requires that the iterator
179 * be nonsingular (i.e., either dereferenceable or past-the-end),
180 * that it reference the sequence we are inserting into, and that the
181 * iterator range [_First, _Last) is a valid (possibly empty)
182 * range which does not reference the sequence we are inserting into.
183 * Note that this macro is only valid when the container is a
184 * _Safe_sequence and the _Position iterator is a _Safe_iterator.
185 */
186 #define __glibcxx_check_insert_range_after(_Position,_First,_Last,_Dist)\
187 __glibcxx_check_valid_range2(_First,_Last,_Dist); \
188 __glibcxx_check_insert_after(_Position); \
189 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__foreign_iterator(_Position,_First,_Last),\
190 _M_message(__gnu_debug::__msg_insert_range_from_self)\
191 ._M_iterator(_First, #_First) \
192 ._M_iterator(_Last, #_Last) \
193 ._M_sequence(*this, "this"))
194
195 /** Verify that we can erase the element referenced by the iterator
196 * _Position. We can erase the element if the _Position iterator is
197 * dereferenceable and references this sequence.
198 */
199 #define __glibcxx_check_erase(_Position) \
200 _GLIBCXX_DEBUG_VERIFY(_Position._M_dereferenceable(), \
201 _M_message(__gnu_debug::__msg_erase_bad) \
202 ._M_sequence(*this, "this") \
203 ._M_iterator(_Position, #_Position)); \
204 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this), \
205 _M_message(__gnu_debug::__msg_erase_different) \
206 ._M_sequence(*this, "this") \
207 ._M_iterator(_Position, #_Position))
208
209 /** Verify that we can erase the element after the iterator
210 * _Position. We can erase the element if the _Position iterator is
211 * before a dereferenceable one and references this sequence.
212 */
213 #define __glibcxx_check_erase_after(_Position) \
214 _GLIBCXX_DEBUG_VERIFY(_Position._M_before_dereferenceable(), \
215 _M_message(__gnu_debug::__msg_erase_after_bad) \
216 ._M_sequence(*this, "this") \
217 ._M_iterator(_Position, #_Position)); \
218 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this), \
219 _M_message(__gnu_debug::__msg_erase_different) \
220 ._M_sequence(*this, "this") \
221 ._M_iterator(_Position, #_Position))
222
223 /** Verify that we can erase the elements in the iterator range
224 * [_First, _Last). We can erase the elements if [_First, _Last) is a
225 * valid iterator range within this sequence.
226 */
227 #define __glibcxx_check_erase_range(_First,_Last) \
228 __glibcxx_check_valid_range(_First,_Last); \
229 _GLIBCXX_DEBUG_VERIFY(_First._M_attached_to(this), \
230 _M_message(__gnu_debug::__msg_erase_different) \
231 ._M_sequence(*this, "this") \
232 ._M_iterator(_First, #_First) \
233 ._M_iterator(_Last, #_Last))
234
235 /** Verify that we can erase the elements in the iterator range
236 * (_First, _Last). We can erase the elements if (_First, _Last) is a
237 * valid iterator range within this sequence.
238 */
239 #define __glibcxx_check_erase_range_after(_First,_Last) \
240 _GLIBCXX_DEBUG_VERIFY(_First._M_can_compare(_Last), \
241 _M_message(__gnu_debug::__msg_erase_different) \
242 ._M_sequence(*this, "this") \
243 ._M_iterator(_First, #_First) \
244 ._M_iterator(_Last, #_Last)); \
245 _GLIBCXX_DEBUG_VERIFY(_First._M_attached_to(this), \
246 _M_message(__gnu_debug::__msg_erase_different) \
247 ._M_sequence(*this, "this") \
248 ._M_iterator(_First, #_First)); \
249 _GLIBCXX_DEBUG_VERIFY(_First != _Last, \
250 _M_message(__gnu_debug::__msg_valid_range2) \
251 ._M_sequence(*this, "this") \
252 ._M_iterator(_First, #_First) \
253 ._M_iterator(_Last, #_Last)); \
254 _GLIBCXX_DEBUG_VERIFY(_First._M_incrementable(), \
255 _M_message(__gnu_debug::__msg_valid_range2) \
256 ._M_sequence(*this, "this") \
257 ._M_iterator(_First, #_First) \
258 ._M_iterator(_Last, #_Last)); \
259 _GLIBCXX_DEBUG_VERIFY(!_Last._M_is_before_begin(), \
260 _M_message(__gnu_debug::__msg_valid_range2) \
261 ._M_sequence(*this, "this") \
262 ._M_iterator(_First, #_First) \
263 ._M_iterator(_Last, #_Last)) \
264
265 // Verify that the subscript _N is less than the container's size.
266 #define __glibcxx_check_subscript(_N) \
267 _GLIBCXX_DEBUG_VERIFY(_N < this->size(), \
268 _M_message(__gnu_debug::__msg_subscript_oob) \
269 ._M_sequence(*this, "this") \
270 ._M_integer(_N, #_N) \
271 ._M_integer(this->size(), "size"))
272
273 // Verify that the bucket _N is less than the container's buckets count.
274 #define __glibcxx_check_bucket_index(_N) \
275 _GLIBCXX_DEBUG_VERIFY(_N < this->bucket_count(), \
276 _M_message(__gnu_debug::__msg_bucket_index_oob) \
277 ._M_sequence(*this, "this") \
278 ._M_integer(_N, #_N) \
279 ._M_integer(this->bucket_count(), "size"))
280
281 // Verify that the container is nonempty
282 #define __glibcxx_check_nonempty() \
283 _GLIBCXX_DEBUG_VERIFY(! this->empty(), \
284 _M_message(__gnu_debug::__msg_empty) \
285 ._M_sequence(*this, "this"))
286
287 // Verify that the iterator range [_First, _Last) is sorted
288 #define __glibcxx_check_sorted(_First,_Last) \
289 __glibcxx_check_valid_range(_First,_Last); \
290 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted( \
291 __gnu_debug::__base(_First), \
292 __gnu_debug::__base(_Last)), \
293 _M_message(__gnu_debug::__msg_unsorted) \
294 ._M_iterator(_First, #_First) \
295 ._M_iterator(_Last, #_Last))
296
297 /** Verify that the iterator range [_First, _Last) is sorted by the
298 predicate _Pred. */
299 #define __glibcxx_check_sorted_pred(_First,_Last,_Pred) \
300 __glibcxx_check_valid_range(_First,_Last); \
301 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted( \
302 __gnu_debug::__base(_First), \
303 __gnu_debug::__base(_Last), _Pred), \
304 _M_message(__gnu_debug::__msg_unsorted_pred) \
305 ._M_iterator(_First, #_First) \
306 ._M_iterator(_Last, #_Last) \
307 ._M_string(#_Pred))
308
309 // Special variant for std::merge, std::includes, std::set_*
310 #define __glibcxx_check_sorted_set(_First1,_Last1,_First2) \
311 __glibcxx_check_valid_range(_First1,_Last1); \
312 _GLIBCXX_DEBUG_VERIFY( \
313 __gnu_debug::__check_sorted_set(__gnu_debug::__base(_First1), \
314 __gnu_debug::__base(_Last1), _First2),\
315 _M_message(__gnu_debug::__msg_unsorted) \
316 ._M_iterator(_First1, #_First1) \
317 ._M_iterator(_Last1, #_Last1))
318
319 // Likewise with a _Pred.
320 #define __glibcxx_check_sorted_set_pred(_First1,_Last1,_First2,_Pred) \
321 __glibcxx_check_valid_range(_First1,_Last1); \
322 _GLIBCXX_DEBUG_VERIFY( \
323 __gnu_debug::__check_sorted_set(__gnu_debug::__base(_First1), \
324 __gnu_debug::__base(_Last1), \
325 _First2, _Pred), \
326 _M_message(__gnu_debug::__msg_unsorted_pred) \
327 ._M_iterator(_First1, #_First1) \
328 ._M_iterator(_Last1, #_Last1) \
329 ._M_string(#_Pred))
330
331 /** Verify that the iterator range [_First, _Last) is partitioned
332 w.r.t. the value _Value. */
333 #define __glibcxx_check_partitioned_lower(_First,_Last,_Value) \
334 __glibcxx_check_valid_range(_First,_Last); \
335 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_lower( \
336 __gnu_debug::__base(_First), \
337 __gnu_debug::__base(_Last), _Value), \
338 _M_message(__gnu_debug::__msg_unpartitioned) \
339 ._M_iterator(_First, #_First) \
340 ._M_iterator(_Last, #_Last) \
341 ._M_string(#_Value))
342
343 #define __glibcxx_check_partitioned_upper(_First,_Last,_Value) \
344 __glibcxx_check_valid_range(_First,_Last); \
345 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_upper( \
346 __gnu_debug::__base(_First), \
347 __gnu_debug::__base(_Last), _Value), \
348 _M_message(__gnu_debug::__msg_unpartitioned) \
349 ._M_iterator(_First, #_First) \
350 ._M_iterator(_Last, #_Last) \
351 ._M_string(#_Value))
352
353 /** Verify that the iterator range [_First, _Last) is partitioned
354 w.r.t. the value _Value and predicate _Pred. */
355 #define __glibcxx_check_partitioned_lower_pred(_First,_Last,_Value,_Pred) \
356 __glibcxx_check_valid_range(_First,_Last); \
357 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_lower( \
358 __gnu_debug::__base(_First), \
359 __gnu_debug::__base(_Last), _Value, _Pred), \
360 _M_message(__gnu_debug::__msg_unpartitioned_pred) \
361 ._M_iterator(_First, #_First) \
362 ._M_iterator(_Last, #_Last) \
363 ._M_string(#_Pred) \
364 ._M_string(#_Value))
365
366 /** Verify that the iterator range [_First, _Last) is partitioned
367 w.r.t. the value _Value and predicate _Pred. */
368 #define __glibcxx_check_partitioned_upper_pred(_First,_Last,_Value,_Pred) \
369 __glibcxx_check_valid_range(_First,_Last); \
370 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_upper( \
371 __gnu_debug::__base(_First), \
372 __gnu_debug::__base(_Last), _Value, _Pred), \
373 _M_message(__gnu_debug::__msg_unpartitioned_pred) \
374 ._M_iterator(_First, #_First) \
375 ._M_iterator(_Last, #_Last) \
376 ._M_string(#_Pred) \
377 ._M_string(#_Value))
378
379 // Verify that the iterator range [_First, _Last) is a heap
380 #define __glibcxx_check_heap(_First,_Last) \
381 _GLIBCXX_DEBUG_VERIFY(std::__is_heap(__gnu_debug::__base(_First), \
382 __gnu_debug::__base(_Last)), \
383 _M_message(__gnu_debug::__msg_not_heap) \
384 ._M_iterator(_First, #_First) \
385 ._M_iterator(_Last, #_Last))
386
387 /** Verify that the iterator range [_First, _Last) is a heap
388 w.r.t. the predicate _Pred. */
389 #define __glibcxx_check_heap_pred(_First,_Last,_Pred) \
390 _GLIBCXX_DEBUG_VERIFY(std::__is_heap(__gnu_debug::__base(_First), \
391 __gnu_debug::__base(_Last), \
392 _Pred), \
393 _M_message(__gnu_debug::__msg_not_heap_pred) \
394 ._M_iterator(_First, #_First) \
395 ._M_iterator(_Last, #_Last) \
396 ._M_string(#_Pred))
397
398 // Verify that the container is not self move assigned
399 #define __glibcxx_check_self_move_assign(_Other) \
400 _GLIBCXX_DEBUG_VERIFY(this != &_Other, \
401 _M_message(__gnu_debug::__msg_self_move_assign) \
402 ._M_sequence(*this, "this"))
403
404 // Verify that load factor is positive
405 #define __glibcxx_check_max_load_factor(_F) \
406 _GLIBCXX_DEBUG_VERIFY(_F > 0.0f, \
407 _M_message(__gnu_debug::__msg_valid_load_factor) \
408 ._M_sequence(*this, "this"))
409
410 #define __glibcxx_check_equal_allocs(_This, _Other) \
411 _GLIBCXX_DEBUG_VERIFY(_This.get_allocator() == _Other.get_allocator(), \
412 _M_message(__gnu_debug::__msg_equal_allocs) \
413 ._M_sequence(_This, "this"))
414
415 #define __glibcxx_check_string(_String) _GLIBCXX_DEBUG_PEDASSERT(_String != 0)
416 #define __glibcxx_check_string_len(_String,_Len) \
417 _GLIBCXX_DEBUG_PEDASSERT(_String != 0 || _Len == 0)
418
419 // Verify that a predicate is irreflexive
420 #define __glibcxx_check_irreflexive(_First,_Last) \
421 _GLIBCXX_DEBUG_VERIFY(_First == _Last || !(*_First < *_First), \
422 _M_message(__gnu_debug::__msg_irreflexive_ordering) \
423 ._M_iterator_value_type(_First, "< operator type"))
424
425 #if __cplusplus >= 201103L
426 # define __glibcxx_check_irreflexive2(_First,_Last) \
427 _GLIBCXX_DEBUG_VERIFY(_First == _Last \
428 || __gnu_debug::__is_irreflexive(_First), \
429 _M_message(__gnu_debug::__msg_irreflexive_ordering) \
430 ._M_iterator_value_type(_First, "< operator type"))
431 #else
432 # define __glibcxx_check_irreflexive2(_First,_Last)
433 #endif
434
435 #define __glibcxx_check_irreflexive_pred(_First,_Last,_Pred) \
436 _GLIBCXX_DEBUG_VERIFY(_First == _Last || !_Pred(*_First, *_First), \
437 _M_message(__gnu_debug::__msg_irreflexive_ordering) \
438 ._M_instance(_Pred, "functor") \
439 ._M_iterator_value_type(_First, "ordered type"))
440
441 #if __cplusplus >= 201103L
442 # define __glibcxx_check_irreflexive_pred2(_First,_Last,_Pred) \
443 _GLIBCXX_DEBUG_VERIFY(_First == _Last \
444 ||__gnu_debug::__is_irreflexive_pred(_First, _Pred), \
445 _M_message(__gnu_debug::__msg_irreflexive_ordering) \
446 ._M_instance(_Pred, "functor") \
447 ._M_iterator_value_type(_First, "ordered type"))
448 #else
449 # define __glibcxx_check_irreflexive_pred2(_First,_Last,_Pred)
450 #endif
451
452 #endif