libstdc++
macros.h
Go to the documentation of this file.
1 // Debugging support implementation -*- C++ -*-
2 
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11 
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20 
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
25 
26 /** @file debug/macros.h
27  * This file is a GNU debug extension to the Standard C++ Library.
28  */
29 
30 #ifndef _GLIBCXX_DEBUG_MACROS_H
31 #define _GLIBCXX_DEBUG_MACROS_H 1
32 
33 /**
34  * Macros used by the implementation to verify certain
35  * properties. These macros may only be used directly by the debug
36  * wrappers. Note that these are macros (instead of the more obviously
37  * @a correct choice of making them functions) because we need line and
38  * file information at the call site, to minimize the distance between
39  * the user error and where the error is reported.
40  *
41  */
42 #define _GLIBCXX_DEBUG_VERIFY_AT(_Condition,_ErrorMessage,_File,_Line) \
43  do \
44  { \
45  if (! (_Condition)) \
46  __gnu_debug::_Error_formatter::_M_at(_File, _Line) \
47  ._ErrorMessage._M_error(); \
48  } while (false)
49 
50 #define _GLIBCXX_DEBUG_VERIFY(_Condition,_ErrorMessage) \
51  _GLIBCXX_DEBUG_VERIFY_AT(_Condition,_ErrorMessage,__FILE__,__LINE__)
52 
53 // Verify that [_First, _Last) forms a valid iterator range.
54 #define __glibcxx_check_valid_range(_First,_Last) \
55 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__valid_range(_First, _Last), \
56  _M_message(__gnu_debug::__msg_valid_range) \
57  ._M_iterator(_First, #_First) \
58  ._M_iterator(_Last, #_Last))
59 
60 // Verify that [_First, _Last) forms a non-empty iterator range.
61 #define __glibcxx_check_non_empty_range(_First,_Last) \
62 _GLIBCXX_DEBUG_VERIFY(_First != _Last, \
63  _M_message(__gnu_debug::__msg_non_empty_range) \
64  ._M_iterator(_First, #_First) \
65  ._M_iterator(_Last, #_Last))
66 
67 /** Verify that we can insert into *this with the iterator _Position.
68  * Insertion into a container at a specific position requires that
69  * the iterator be nonsingular, either dereferenceable or past-the-end,
70  * and that it reference the sequence we are inserting into. Note that
71  * this macro is only valid when the container is a_Safe_sequence and
72  * the iterator is a _Safe_iterator.
73 */
74 #define __glibcxx_check_insert(_Position) \
75 _GLIBCXX_DEBUG_VERIFY(!_Position._M_singular(), \
76  _M_message(__gnu_debug::__msg_insert_singular) \
77  ._M_sequence(*this, "this") \
78  ._M_iterator(_Position, #_Position)); \
79 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this), \
80  _M_message(__gnu_debug::__msg_insert_different) \
81  ._M_sequence(*this, "this") \
82  ._M_iterator(_Position, #_Position))
83 
84 /** Verify that we can insert into *this after the iterator _Position.
85  * Insertion into a container after a specific position requires that
86  * the iterator be nonsingular, either dereferenceable or before-begin,
87  * and that it reference the sequence we are inserting into. Note that
88  * this macro is only valid when the container is a_Safe_sequence and
89  * the iterator is a _Safe_iterator.
90 */
91 #define __glibcxx_check_insert_after(_Position) \
92 __glibcxx_check_insert(_Position); \
93 _GLIBCXX_DEBUG_VERIFY(!_Position._M_is_end(), \
94  _M_message(__gnu_debug::__msg_insert_after_end) \
95  ._M_sequence(*this, "this") \
96  ._M_iterator(_Position, #_Position))
97 
98 /** Verify that we can insert the values in the iterator range
99  * [_First, _Last) into *this with the iterator _Position. Insertion
100  * into a container at a specific position requires that the iterator
101  * be nonsingular (i.e., either dereferenceable or past-the-end),
102  * that it reference the sequence we are inserting into, and that the
103  * iterator range [_First, Last) is a valid (possibly empty)
104  * range. Note that this macro is only valid when the container is a
105  * _Safe_sequence and the iterator is a _Safe_iterator.
106  *
107  * @todo We would like to be able to check for noninterference of
108  * _Position and the range [_First, _Last), but that can't (in
109  * general) be done.
110 */
111 #define __glibcxx_check_insert_range(_Position,_First,_Last) \
112 __glibcxx_check_valid_range(_First,_Last); \
113 __glibcxx_check_insert(_Position)
114 
115 /** Verify that we can insert the values in the iterator range
116  * [_First, _Last) into *this after the iterator _Position. Insertion
117  * into a container after a specific position requires that the iterator
118  * be nonsingular (i.e., either dereferenceable or past-the-end),
119  * that it reference the sequence we are inserting into, and that the
120  * iterator range [_First, Last) is a valid (possibly empty)
121  * range. Note that this macro is only valid when the container is a
122  * _Safe_sequence and the iterator is a _Safe_iterator.
123  *
124  * @todo We would like to be able to check for noninterference of
125  * _Position and the range [_First, _Last), but that can't (in
126  * general) be done.
127 */
128 #define __glibcxx_check_insert_range_after(_Position,_First,_Last) \
129 __glibcxx_check_valid_range(_First,_Last); \
130 __glibcxx_check_insert_after(_Position)
131 
132 /** Verify that we can erase the element referenced by the iterator
133  * _Position. We can erase the element if the _Position iterator is
134  * dereferenceable and references this sequence.
135 */
136 #define __glibcxx_check_erase(_Position) \
137 _GLIBCXX_DEBUG_VERIFY(_Position._M_dereferenceable(), \
138  _M_message(__gnu_debug::__msg_erase_bad) \
139  ._M_sequence(*this, "this") \
140  ._M_iterator(_Position, #_Position)); \
141 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this), \
142  _M_message(__gnu_debug::__msg_erase_different) \
143  ._M_sequence(*this, "this") \
144  ._M_iterator(_Position, #_Position))
145 
146 /** Verify that we can erase the element after the iterator
147  * _Position. We can erase the element if the _Position iterator is
148  * before a dereferenceable one and references this sequence.
149 */
150 #define __glibcxx_check_erase_after(_Position) \
151 _GLIBCXX_DEBUG_VERIFY(_Position._M_before_dereferenceable(), \
152  _M_message(__gnu_debug::__msg_erase_after_bad) \
153  ._M_sequence(*this, "this") \
154  ._M_iterator(_Position, #_Position)); \
155 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this), \
156  _M_message(__gnu_debug::__msg_erase_different) \
157  ._M_sequence(*this, "this") \
158  ._M_iterator(_Position, #_Position))
159 
160 /** Verify that we can erase the elements in the iterator range
161  * [_First, _Last). We can erase the elements if [_First, _Last) is a
162  * valid iterator range within this sequence.
163 */
164 #define __glibcxx_check_erase_range(_First,_Last) \
165 __glibcxx_check_valid_range(_First,_Last); \
166 _GLIBCXX_DEBUG_VERIFY(_First._M_attached_to(this), \
167  _M_message(__gnu_debug::__msg_erase_different) \
168  ._M_sequence(*this, "this") \
169  ._M_iterator(_First, #_First) \
170  ._M_iterator(_Last, #_Last))
171 
172 /** Verify that we can erase the elements in the iterator range
173  * (_First, _Last). We can erase the elements if (_First, _Last) is a
174  * valid iterator range within this sequence.
175 */
176 #define __glibcxx_check_erase_range_after(_First,_Last) \
177 _GLIBCXX_DEBUG_VERIFY(_First._M_can_compare(_Last), \
178  _M_message(__gnu_debug::__msg_erase_different) \
179  ._M_sequence(*this, "this") \
180  ._M_iterator(_First, #_First) \
181  ._M_iterator(_Last, #_Last)); \
182 _GLIBCXX_DEBUG_VERIFY(_First._M_attached_to(this), \
183  _M_message(__gnu_debug::__msg_erase_different) \
184  ._M_sequence(*this, "this") \
185  ._M_iterator(_First, #_First)); \
186 _GLIBCXX_DEBUG_VERIFY(_First != _Last, \
187  _M_message(__gnu_debug::__msg_valid_range2) \
188  ._M_sequence(*this, "this") \
189  ._M_iterator(_First, #_First) \
190  ._M_iterator(_Last, #_Last)); \
191 _GLIBCXX_DEBUG_VERIFY(_First._M_incrementable(), \
192  _M_message(__gnu_debug::__msg_valid_range2) \
193  ._M_sequence(*this, "this") \
194  ._M_iterator(_First, #_First) \
195  ._M_iterator(_Last, #_Last)); \
196 _GLIBCXX_DEBUG_VERIFY(!_Last._M_is_before_begin(), \
197  _M_message(__gnu_debug::__msg_valid_range2) \
198  ._M_sequence(*this, "this") \
199  ._M_iterator(_First, #_First) \
200  ._M_iterator(_Last, #_Last)) \
201 
202 // Verify that the subscript _N is less than the container's size.
203 #define __glibcxx_check_subscript(_N) \
204 _GLIBCXX_DEBUG_VERIFY(_N < this->size(), \
205  _M_message(__gnu_debug::__msg_subscript_oob) \
206  ._M_sequence(*this, "this") \
207  ._M_integer(_N, #_N) \
208  ._M_integer(this->size(), "size"))
209 
210 // Verify that the container is nonempty
211 #define __glibcxx_check_nonempty() \
212 _GLIBCXX_DEBUG_VERIFY(! this->empty(), \
213  _M_message(__gnu_debug::__msg_empty) \
214  ._M_sequence(*this, "this"))
215 
216 // Verify that the iterator range [_First, _Last) is sorted
217 #define __glibcxx_check_sorted(_First,_Last) \
218 __glibcxx_check_valid_range(_First,_Last); \
219 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted(_First, _Last), \
220  _M_message(__gnu_debug::__msg_unsorted) \
221  ._M_iterator(_First, #_First) \
222  ._M_iterator(_Last, #_Last))
223 
224 /** Verify that the iterator range [_First, _Last) is sorted by the
225  predicate _Pred. */
226 #define __glibcxx_check_sorted_pred(_First,_Last,_Pred) \
227 __glibcxx_check_valid_range(_First,_Last); \
228 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted(_First, _Last, _Pred), \
229  _M_message(__gnu_debug::__msg_unsorted_pred) \
230  ._M_iterator(_First, #_First) \
231  ._M_iterator(_Last, #_Last) \
232  ._M_string(#_Pred))
233 
234 // Special variant for std::merge, std::includes, std::set_*
235 #define __glibcxx_check_sorted_set(_First1,_Last1,_First2) \
236 __glibcxx_check_valid_range(_First1,_Last1); \
237 _GLIBCXX_DEBUG_VERIFY( \
238  __gnu_debug::__check_sorted_set(_First1, _Last1, _First2), \
239  _M_message(__gnu_debug::__msg_unsorted) \
240  ._M_iterator(_First1, #_First1) \
241  ._M_iterator(_Last1, #_Last1))
242 
243 // Likewise with a _Pred.
244 #define __glibcxx_check_sorted_set_pred(_First1,_Last1,_First2,_Pred) \
245 __glibcxx_check_valid_range(_First1,_Last1); \
246 _GLIBCXX_DEBUG_VERIFY( \
247  __gnu_debug::__check_sorted_set(_First1, _Last1, _First2, _Pred), \
248  _M_message(__gnu_debug::__msg_unsorted_pred) \
249  ._M_iterator(_First1, #_First1) \
250  ._M_iterator(_Last1, #_Last1) \
251  ._M_string(#_Pred))
252 
253 /** Verify that the iterator range [_First, _Last) is partitioned
254  w.r.t. the value _Value. */
255 #define __glibcxx_check_partitioned_lower(_First,_Last,_Value) \
256 __glibcxx_check_valid_range(_First,_Last); \
257 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_lower(_First, _Last, \
258  _Value), \
259  _M_message(__gnu_debug::__msg_unpartitioned) \
260  ._M_iterator(_First, #_First) \
261  ._M_iterator(_Last, #_Last) \
262  ._M_string(#_Value))
263 
264 #define __glibcxx_check_partitioned_upper(_First,_Last,_Value) \
265 __glibcxx_check_valid_range(_First,_Last); \
266 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_upper(_First, _Last, \
267  _Value), \
268  _M_message(__gnu_debug::__msg_unpartitioned) \
269  ._M_iterator(_First, #_First) \
270  ._M_iterator(_Last, #_Last) \
271  ._M_string(#_Value))
272 
273 /** Verify that the iterator range [_First, _Last) is partitioned
274  w.r.t. the value _Value and predicate _Pred. */
275 #define __glibcxx_check_partitioned_lower_pred(_First,_Last,_Value,_Pred) \
276 __glibcxx_check_valid_range(_First,_Last); \
277 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_lower(_First, _Last, \
278  _Value, _Pred), \
279  _M_message(__gnu_debug::__msg_unpartitioned_pred) \
280  ._M_iterator(_First, #_First) \
281  ._M_iterator(_Last, #_Last) \
282  ._M_string(#_Pred) \
283  ._M_string(#_Value))
284 
285 /** Verify that the iterator range [_First, _Last) is partitioned
286  w.r.t. the value _Value and predicate _Pred. */
287 #define __glibcxx_check_partitioned_upper_pred(_First,_Last,_Value,_Pred) \
288 __glibcxx_check_valid_range(_First,_Last); \
289 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_upper(_First, _Last, \
290  _Value, _Pred), \
291  _M_message(__gnu_debug::__msg_unpartitioned_pred) \
292  ._M_iterator(_First, #_First) \
293  ._M_iterator(_Last, #_Last) \
294  ._M_string(#_Pred) \
295  ._M_string(#_Value))
296 
297 // Verify that the iterator range [_First, _Last) is a heap
298 #define __glibcxx_check_heap(_First,_Last) \
299 _GLIBCXX_DEBUG_VERIFY(std::__is_heap(_First, _Last), \
300  _M_message(__gnu_debug::__msg_not_heap) \
301  ._M_iterator(_First, #_First) \
302  ._M_iterator(_Last, #_Last))
303 
304 /** Verify that the iterator range [_First, _Last) is a heap
305  w.r.t. the predicate _Pred. */
306 #define __glibcxx_check_heap_pred(_First,_Last,_Pred) \
307 _GLIBCXX_DEBUG_VERIFY(std::__is_heap(_First, _Last, _Pred), \
308  _M_message(__gnu_debug::__msg_not_heap_pred) \
309  ._M_iterator(_First, #_First) \
310  ._M_iterator(_Last, #_Last) \
311  ._M_string(#_Pred))
312 
313 #ifdef _GLIBCXX_DEBUG_PEDANTIC
314 # define __glibcxx_check_string(_String) _GLIBCXX_DEBUG_ASSERT(_String != 0)
315 # define __glibcxx_check_string_len(_String,_Len) \
316  _GLIBCXX_DEBUG_ASSERT(_String != 0 || _Len == 0)
317 #else
318 # define __glibcxx_check_string(_String)
319 # define __glibcxx_check_string_len(_String,_Len)
320 #endif
321 
322 #endif