libstdc++
macros.h
Go to the documentation of this file.
1 // Debugging support implementation -*- C++ -*-
2 
3 // Copyright (C) 2003-2017 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_AT(_Condition,_ErrorMessage,_File,_Line) \
42  do \
43  { \
44  if (! (_Condition)) \
45  __gnu_debug::_Error_formatter::_M_at(_File, _Line) \
46  ._ErrorMessage._M_error(); \
47  } while (false)
48 
49 #define _GLIBCXX_DEBUG_VERIFY(_Condition,_ErrorMessage) \
50  _GLIBCXX_DEBUG_VERIFY_AT(_Condition,_ErrorMessage,__FILE__,__LINE__)
51 
52 // Verify that [_First, _Last) forms a valid iterator range.
53 #define __glibcxx_check_valid_range(_First,_Last) \
54 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__valid_range(_First, _Last), \
55  _M_message(__gnu_debug::__msg_valid_range) \
56  ._M_iterator(_First, #_First) \
57  ._M_iterator(_Last, #_Last))
58 
59 #define __glibcxx_check_valid_range2(_First,_Last,_Dist) \
60 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__valid_range(_First, _Last, _Dist), \
61  _M_message(__gnu_debug::__msg_valid_range) \
62  ._M_iterator(_First, #_First) \
63  ._M_iterator(_Last, #_Last))
64 
65 // Verify that [_First, _Last) forms a non-empty iterator range.
66 #define __glibcxx_check_non_empty_range(_First,_Last) \
67 _GLIBCXX_DEBUG_VERIFY(_First != _Last, \
68  _M_message(__gnu_debug::__msg_non_empty_range) \
69  ._M_iterator(_First, #_First) \
70  ._M_iterator(_Last, #_Last))
71 
72 /** Verify that we can insert into *this with the iterator _Position.
73  * Insertion into a container at a specific position requires that
74  * the iterator be nonsingular, either dereferenceable or past-the-end,
75  * and that it reference the sequence we are inserting into. Note that
76  * this macro is only valid when the container is a_Safe_sequence and
77  * the iterator is a _Safe_iterator.
78 */
79 #define __glibcxx_check_insert(_Position) \
80 _GLIBCXX_DEBUG_VERIFY(!_Position._M_singular(), \
81  _M_message(__gnu_debug::__msg_insert_singular) \
82  ._M_sequence(*this, "this") \
83  ._M_iterator(_Position, #_Position)); \
84 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this), \
85  _M_message(__gnu_debug::__msg_insert_different) \
86  ._M_sequence(*this, "this") \
87  ._M_iterator(_Position, #_Position))
88 
89 /** Verify that we can insert into *this after the iterator _Position.
90  * Insertion into a container after a specific position requires that
91  * the iterator be nonsingular, either dereferenceable or before-begin,
92  * and that it reference the sequence we are inserting into. Note that
93  * this macro is only valid when the container is a_Safe_sequence and
94  * the iterator is a _Safe_iterator.
95 */
96 #define __glibcxx_check_insert_after(_Position) \
97 __glibcxx_check_insert(_Position); \
98 _GLIBCXX_DEBUG_VERIFY(!_Position._M_is_end(), \
99  _M_message(__gnu_debug::__msg_insert_after_end) \
100  ._M_sequence(*this, "this") \
101  ._M_iterator(_Position, #_Position))
102 
103 /** Verify that we can insert the values in the iterator range
104  * [_First, _Last) into *this with the iterator _Position. Insertion
105  * into a container at a specific position requires that the iterator
106  * be nonsingular (i.e., either dereferenceable or past-the-end),
107  * that it reference the sequence we are inserting into, and that the
108  * iterator range [_First, _Last) is a valid (possibly empty)
109  * range which does not reference the sequence we are inserting into.
110  * Note that this macro is only valid when the container is a
111  * _Safe_sequence and the _Position iterator is a _Safe_iterator.
112 */
113 #define __glibcxx_check_insert_range(_Position,_First,_Last,_Dist) \
114 __glibcxx_check_valid_range2(_First,_Last,_Dist); \
115 __glibcxx_check_insert(_Position); \
116 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__foreign_iterator(_Position,_First,_Last),\
117  _M_message(__gnu_debug::__msg_insert_range_from_self)\
118  ._M_iterator(_First, #_First) \
119  ._M_iterator(_Last, #_Last) \
120  ._M_sequence(*this, "this"))
121 
122 /** Verify that we can insert the values in the iterator range
123  * [_First, _Last) into *this after the iterator _Position. Insertion
124  * into a container after a specific position requires that the iterator
125  * be nonsingular (i.e., either dereferenceable or past-the-end),
126  * that it reference the sequence we are inserting into, and that the
127  * iterator range [_First, _Last) is a valid (possibly empty)
128  * range which does not reference the sequence we are inserting into.
129  * Note that this macro is only valid when the container is a
130  * _Safe_sequence and the _Position iterator is a _Safe_iterator.
131 */
132 #define __glibcxx_check_insert_range_after(_Position,_First,_Last,_Dist)\
133  __glibcxx_check_valid_range2(_First,_Last,_Dist); \
134 __glibcxx_check_insert_after(_Position); \
135 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__foreign_iterator(_Position,_First,_Last),\
136  _M_message(__gnu_debug::__msg_insert_range_from_self)\
137  ._M_iterator(_First, #_First) \
138  ._M_iterator(_Last, #_Last) \
139  ._M_sequence(*this, "this"))
140 
141 /** Verify that we can erase the element referenced by the iterator
142  * _Position. We can erase the element if the _Position iterator is
143  * dereferenceable and references this sequence.
144 */
145 #define __glibcxx_check_erase(_Position) \
146 _GLIBCXX_DEBUG_VERIFY(_Position._M_dereferenceable(), \
147  _M_message(__gnu_debug::__msg_erase_bad) \
148  ._M_sequence(*this, "this") \
149  ._M_iterator(_Position, #_Position)); \
150 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this), \
151  _M_message(__gnu_debug::__msg_erase_different) \
152  ._M_sequence(*this, "this") \
153  ._M_iterator(_Position, #_Position))
154 
155 /** Verify that we can erase the element after the iterator
156  * _Position. We can erase the element if the _Position iterator is
157  * before a dereferenceable one and references this sequence.
158 */
159 #define __glibcxx_check_erase_after(_Position) \
160 _GLIBCXX_DEBUG_VERIFY(_Position._M_before_dereferenceable(), \
161  _M_message(__gnu_debug::__msg_erase_after_bad) \
162  ._M_sequence(*this, "this") \
163  ._M_iterator(_Position, #_Position)); \
164 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this), \
165  _M_message(__gnu_debug::__msg_erase_different) \
166  ._M_sequence(*this, "this") \
167  ._M_iterator(_Position, #_Position))
168 
169 /** Verify that we can erase the elements in the iterator range
170  * [_First, _Last). We can erase the elements if [_First, _Last) is a
171  * valid iterator range within this sequence.
172 */
173 #define __glibcxx_check_erase_range(_First,_Last) \
174 __glibcxx_check_valid_range(_First,_Last); \
175 _GLIBCXX_DEBUG_VERIFY(_First._M_attached_to(this), \
176  _M_message(__gnu_debug::__msg_erase_different) \
177  ._M_sequence(*this, "this") \
178  ._M_iterator(_First, #_First) \
179  ._M_iterator(_Last, #_Last))
180 
181 /** Verify that we can erase the elements in the iterator range
182  * (_First, _Last). We can erase the elements if (_First, _Last) is a
183  * valid iterator range within this sequence.
184 */
185 #define __glibcxx_check_erase_range_after(_First,_Last) \
186 _GLIBCXX_DEBUG_VERIFY(_First._M_can_compare(_Last), \
187  _M_message(__gnu_debug::__msg_erase_different) \
188  ._M_sequence(*this, "this") \
189  ._M_iterator(_First, #_First) \
190  ._M_iterator(_Last, #_Last)); \
191 _GLIBCXX_DEBUG_VERIFY(_First._M_attached_to(this), \
192  _M_message(__gnu_debug::__msg_erase_different) \
193  ._M_sequence(*this, "this") \
194  ._M_iterator(_First, #_First)); \
195 _GLIBCXX_DEBUG_VERIFY(_First != _Last, \
196  _M_message(__gnu_debug::__msg_valid_range2) \
197  ._M_sequence(*this, "this") \
198  ._M_iterator(_First, #_First) \
199  ._M_iterator(_Last, #_Last)); \
200 _GLIBCXX_DEBUG_VERIFY(_First._M_incrementable(), \
201  _M_message(__gnu_debug::__msg_valid_range2) \
202  ._M_sequence(*this, "this") \
203  ._M_iterator(_First, #_First) \
204  ._M_iterator(_Last, #_Last)); \
205 _GLIBCXX_DEBUG_VERIFY(!_Last._M_is_before_begin(), \
206  _M_message(__gnu_debug::__msg_valid_range2) \
207  ._M_sequence(*this, "this") \
208  ._M_iterator(_First, #_First) \
209  ._M_iterator(_Last, #_Last)) \
210 
211 // Verify that the subscript _N is less than the container's size.
212 #define __glibcxx_check_subscript(_N) \
213 _GLIBCXX_DEBUG_VERIFY(_N < this->size(), \
214  _M_message(__gnu_debug::__msg_subscript_oob) \
215  ._M_sequence(*this, "this") \
216  ._M_integer(_N, #_N) \
217  ._M_integer(this->size(), "size"))
218 
219 // Verify that the bucket _N is less than the container's buckets count.
220 #define __glibcxx_check_bucket_index(_N) \
221 _GLIBCXX_DEBUG_VERIFY(_N < this->bucket_count(), \
222  _M_message(__gnu_debug::__msg_bucket_index_oob) \
223  ._M_sequence(*this, "this") \
224  ._M_integer(_N, #_N) \
225  ._M_integer(this->bucket_count(), "size"))
226 
227 // Verify that the container is nonempty
228 #define __glibcxx_check_nonempty() \
229 _GLIBCXX_DEBUG_VERIFY(! this->empty(), \
230  _M_message(__gnu_debug::__msg_empty) \
231  ._M_sequence(*this, "this"))
232 
233 // Verify that the iterator range [_First, _Last) is sorted
234 #define __glibcxx_check_sorted(_First,_Last) \
235 __glibcxx_check_valid_range(_First,_Last); \
236  _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted( \
237  __gnu_debug::__base(_First), \
238  __gnu_debug::__base(_Last)), \
239  _M_message(__gnu_debug::__msg_unsorted) \
240  ._M_iterator(_First, #_First) \
241  ._M_iterator(_Last, #_Last))
242 
243 /** Verify that the iterator range [_First, _Last) is sorted by the
244  predicate _Pred. */
245 #define __glibcxx_check_sorted_pred(_First,_Last,_Pred) \
246 __glibcxx_check_valid_range(_First,_Last); \
247 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted( \
248  __gnu_debug::__base(_First), \
249  __gnu_debug::__base(_Last), _Pred), \
250  _M_message(__gnu_debug::__msg_unsorted_pred) \
251  ._M_iterator(_First, #_First) \
252  ._M_iterator(_Last, #_Last) \
253  ._M_string(#_Pred))
254 
255 // Special variant for std::merge, std::includes, std::set_*
256 #define __glibcxx_check_sorted_set(_First1,_Last1,_First2) \
257 __glibcxx_check_valid_range(_First1,_Last1); \
258 _GLIBCXX_DEBUG_VERIFY( \
259  __gnu_debug::__check_sorted_set(__gnu_debug::__base(_First1), \
260  __gnu_debug::__base(_Last1), _First2),\
261  _M_message(__gnu_debug::__msg_unsorted) \
262  ._M_iterator(_First1, #_First1) \
263  ._M_iterator(_Last1, #_Last1))
264 
265 // Likewise with a _Pred.
266 #define __glibcxx_check_sorted_set_pred(_First1,_Last1,_First2,_Pred) \
267 __glibcxx_check_valid_range(_First1,_Last1); \
268 _GLIBCXX_DEBUG_VERIFY( \
269  __gnu_debug::__check_sorted_set(__gnu_debug::__base(_First1), \
270  __gnu_debug::__base(_Last1), \
271  _First2, _Pred), \
272  _M_message(__gnu_debug::__msg_unsorted_pred) \
273  ._M_iterator(_First1, #_First1) \
274  ._M_iterator(_Last1, #_Last1) \
275  ._M_string(#_Pred))
276 
277 /** Verify that the iterator range [_First, _Last) is partitioned
278  w.r.t. the value _Value. */
279 #define __glibcxx_check_partitioned_lower(_First,_Last,_Value) \
280 __glibcxx_check_valid_range(_First,_Last); \
281 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_lower( \
282  __gnu_debug::__base(_First), \
283  __gnu_debug::__base(_Last), _Value), \
284  _M_message(__gnu_debug::__msg_unpartitioned) \
285  ._M_iterator(_First, #_First) \
286  ._M_iterator(_Last, #_Last) \
287  ._M_string(#_Value))
288 
289 #define __glibcxx_check_partitioned_upper(_First,_Last,_Value) \
290 __glibcxx_check_valid_range(_First,_Last); \
291 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_upper( \
292  __gnu_debug::__base(_First), \
293  __gnu_debug::__base(_Last), _Value), \
294  _M_message(__gnu_debug::__msg_unpartitioned) \
295  ._M_iterator(_First, #_First) \
296  ._M_iterator(_Last, #_Last) \
297  ._M_string(#_Value))
298 
299 /** Verify that the iterator range [_First, _Last) is partitioned
300  w.r.t. the value _Value and predicate _Pred. */
301 #define __glibcxx_check_partitioned_lower_pred(_First,_Last,_Value,_Pred) \
302 __glibcxx_check_valid_range(_First,_Last); \
303 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_lower( \
304  __gnu_debug::__base(_First), \
305  __gnu_debug::__base(_Last), _Value, _Pred), \
306  _M_message(__gnu_debug::__msg_unpartitioned_pred) \
307  ._M_iterator(_First, #_First) \
308  ._M_iterator(_Last, #_Last) \
309  ._M_string(#_Pred) \
310  ._M_string(#_Value))
311 
312 /** Verify that the iterator range [_First, _Last) is partitioned
313  w.r.t. the value _Value and predicate _Pred. */
314 #define __glibcxx_check_partitioned_upper_pred(_First,_Last,_Value,_Pred) \
315 __glibcxx_check_valid_range(_First,_Last); \
316 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_upper( \
317  __gnu_debug::__base(_First), \
318  __gnu_debug::__base(_Last), _Value, _Pred), \
319  _M_message(__gnu_debug::__msg_unpartitioned_pred) \
320  ._M_iterator(_First, #_First) \
321  ._M_iterator(_Last, #_Last) \
322  ._M_string(#_Pred) \
323  ._M_string(#_Value))
324 
325 // Verify that the iterator range [_First, _Last) is a heap
326 #define __glibcxx_check_heap(_First,_Last) \
327  _GLIBCXX_DEBUG_VERIFY(std::__is_heap(__gnu_debug::__base(_First), \
328  __gnu_debug::__base(_Last)), \
329  _M_message(__gnu_debug::__msg_not_heap) \
330  ._M_iterator(_First, #_First) \
331  ._M_iterator(_Last, #_Last))
332 
333 /** Verify that the iterator range [_First, _Last) is a heap
334  w.r.t. the predicate _Pred. */
335 #define __glibcxx_check_heap_pred(_First,_Last,_Pred) \
336  _GLIBCXX_DEBUG_VERIFY(std::__is_heap(__gnu_debug::__base(_First), \
337  __gnu_debug::__base(_Last), \
338  _Pred), \
339  _M_message(__gnu_debug::__msg_not_heap_pred) \
340  ._M_iterator(_First, #_First) \
341  ._M_iterator(_Last, #_Last) \
342  ._M_string(#_Pred))
343 
344 // Verify that the container is not self move assigned
345 #define __glibcxx_check_self_move_assign(_Other) \
346 _GLIBCXX_DEBUG_VERIFY(this != &_Other, \
347  _M_message(__gnu_debug::__msg_self_move_assign) \
348  ._M_sequence(*this, "this"))
349 
350 // Verify that load factor is positive
351 #define __glibcxx_check_max_load_factor(_F) \
352 _GLIBCXX_DEBUG_VERIFY(_F > 0.0f, \
353  _M_message(__gnu_debug::__msg_valid_load_factor) \
354  ._M_sequence(*this, "this"))
355 
356 #define __glibcxx_check_equal_allocs(_This, _Other) \
357 _GLIBCXX_DEBUG_VERIFY(_This.get_allocator() == _Other.get_allocator(), \
358  _M_message(__gnu_debug::__msg_equal_allocs) \
359  ._M_sequence(_This, "this"))
360 
361 #define __glibcxx_check_string(_String) _GLIBCXX_DEBUG_PEDASSERT(_String != 0)
362 #define __glibcxx_check_string_len(_String,_Len) \
363  _GLIBCXX_DEBUG_PEDASSERT(_String != 0 || _Len == 0)
364 
365 // Verify that a predicate is irreflexive
366 #define __glibcxx_check_irreflexive(_First,_Last) \
367  _GLIBCXX_DEBUG_VERIFY(_First == _Last || !(*_First < *_First), \
368  _M_message(__gnu_debug::__msg_irreflexive_ordering) \
369  ._M_iterator_value_type(_First, "< operator type"))
370 
371 #if __cplusplus >= 201103L
372 # define __glibcxx_check_irreflexive2(_First,_Last) \
373  _GLIBCXX_DEBUG_VERIFY(_First == _Last \
374  || __gnu_debug::__is_irreflexive(_First), \
375  _M_message(__gnu_debug::__msg_irreflexive_ordering) \
376  ._M_iterator_value_type(_First, "< operator type"))
377 #else
378 # define __glibcxx_check_irreflexive2(_First,_Last)
379 #endif
380 
381 #define __glibcxx_check_irreflexive_pred(_First,_Last,_Pred) \
382  _GLIBCXX_DEBUG_VERIFY(_First == _Last || !_Pred(*_First, *_First), \
383  _M_message(__gnu_debug::__msg_irreflexive_ordering) \
384  ._M_instance(_Pred, "functor") \
385  ._M_iterator_value_type(_First, "ordered type"))
386 
387 #if __cplusplus >= 201103L
388 # define __glibcxx_check_irreflexive_pred2(_First,_Last,_Pred) \
389  _GLIBCXX_DEBUG_VERIFY(_First == _Last \
390  ||__gnu_debug::__is_irreflexive_pred(_First, _Pred), \
391  _M_message(__gnu_debug::__msg_irreflexive_ordering) \
392  ._M_instance(_Pred, "functor") \
393  ._M_iterator_value_type(_First, "ordered type"))
394 #else
395 # define __glibcxx_check_irreflexive_pred2(_First,_Last,_Pred)
396 #endif
397 
398 #endif