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
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(_Condition,_ErrorMessage) \
43  do \
44  { \
45  if (! (_Condition)) \
46  __gnu_debug::_Error_formatter::_M_at(__FILE__, __LINE__) \
47  ._ErrorMessage._M_error(); \
48  } while (false)
49 
50 // Verify that [_First, _Last) forms a valid iterator range.
51 #define __glibcxx_check_valid_range(_First,_Last) \
52 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__valid_range(_First, _Last), \
53  _M_message(__gnu_debug::__msg_valid_range) \
54  ._M_iterator(_First, #_First) \
55  ._M_iterator(_Last, #_Last))
56 
57 /** Verify that we can insert into *this with the iterator _Position.
58  * Insertion into a container at a specific position requires that
59  * the iterator be nonsingular, either dereferenceable or past-the-end,
60  * and that it reference the sequence we are inserting into. Note that
61  * this macro is only valid when the container is a_Safe_sequence and
62  * the iterator is a _Safe_iterator.
63 */
64 #define __glibcxx_check_insert(_Position) \
65 _GLIBCXX_DEBUG_VERIFY(!_Position._M_singular(), \
66  _M_message(__gnu_debug::__msg_insert_singular) \
67  ._M_sequence(*this, "this") \
68  ._M_iterator(_Position, #_Position)); \
69 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this), \
70  _M_message(__gnu_debug::__msg_insert_different) \
71  ._M_sequence(*this, "this") \
72  ._M_iterator(_Position, #_Position))
73 
74 /** Verify that we can insert into *this after the iterator _Position.
75  * Insertion into a container after a specific position requires that
76  * the iterator be nonsingular, either dereferenceable or before-begin,
77  * and that it reference the sequence we are inserting into. Note that
78  * this macro is only valid when the container is a_Safe_sequence and
79  * the iterator is a _Safe_iterator.
80 */
81 #define __glibcxx_check_insert_after(_Position) \
82 __glibcxx_check_insert(_Position); \
83 _GLIBCXX_DEBUG_VERIFY(!_Position._M_is_end(), \
84  _M_message(__gnu_debug::__msg_insert_after_end) \
85  ._M_sequence(*this, "this") \
86  ._M_iterator(_Position, #_Position))
87 
88 /** Verify that we can insert the values in the iterator range
89  * [_First, _Last) into *this with the iterator _Position. Insertion
90  * into a container at a specific position requires that the iterator
91  * be nonsingular (i.e., either dereferenceable or past-the-end),
92  * that it reference the sequence we are inserting into, and that the
93  * iterator range [_First, Last) is a valid (possibly empty)
94  * range. Note that this macro is only valid when the container is a
95  * _Safe_sequence and the iterator is a _Safe_iterator.
96  *
97  * @todo We would like to be able to check for noninterference of
98  * _Position and the range [_First, _Last), but that can't (in
99  * general) be done.
100 */
101 #define __glibcxx_check_insert_range(_Position,_First,_Last) \
102 __glibcxx_check_valid_range(_First,_Last); \
103 __glibcxx_check_insert(_Position)
104 
105 /** Verify that we can insert the values in the iterator range
106  * [_First, _Last) into *this after the iterator _Position. Insertion
107  * into a container after a specific position requires that the iterator
108  * be nonsingular (i.e., either dereferenceable or past-the-end),
109  * that it reference the sequence we are inserting into, and that the
110  * iterator range [_First, Last) is a valid (possibly empty)
111  * range. Note that this macro is only valid when the container is a
112  * _Safe_sequence and the iterator is a _Safe_iterator.
113  *
114  * @todo We would like to be able to check for noninterference of
115  * _Position and the range [_First, _Last), but that can't (in
116  * general) be done.
117 */
118 #define __glibcxx_check_insert_range_after(_Position,_First,_Last) \
119 __glibcxx_check_valid_range(_First,_Last); \
120 __glibcxx_check_insert_after(_Position)
121 
122 /** Verify that we can erase the element referenced by the iterator
123  * _Position. We can erase the element if the _Position iterator is
124  * dereferenceable and references this sequence.
125 */
126 #define __glibcxx_check_erase(_Position) \
127 _GLIBCXX_DEBUG_VERIFY(_Position._M_dereferenceable(), \
128  _M_message(__gnu_debug::__msg_erase_bad) \
129  ._M_sequence(*this, "this") \
130  ._M_iterator(_Position, #_Position)); \
131 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this), \
132  _M_message(__gnu_debug::__msg_erase_different) \
133  ._M_sequence(*this, "this") \
134  ._M_iterator(_Position, #_Position))
135 
136 /** Verify that we can erase the element after the iterator
137  * _Position. We can erase the element if the _Position iterator is
138  * before a dereferenceable one and references this sequence.
139 */
140 #define __glibcxx_check_erase_after(_Position) \
141 _GLIBCXX_DEBUG_VERIFY(_Position._M_before_dereferenceable(), \
142  _M_message(__gnu_debug::__msg_erase_after_bad) \
143  ._M_sequence(*this, "this") \
144  ._M_iterator(_Position, #_Position)); \
145 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this), \
146  _M_message(__gnu_debug::__msg_erase_different) \
147  ._M_sequence(*this, "this") \
148  ._M_iterator(_Position, #_Position))
149 
150 /** Verify that we can erase the elements in the iterator range
151  * [_First, _Last). We can erase the elements if [_First, _Last) is a
152  * valid iterator range within this sequence.
153 */
154 #define __glibcxx_check_erase_range(_First,_Last) \
155 __glibcxx_check_valid_range(_First,_Last); \
156 _GLIBCXX_DEBUG_VERIFY(_First._M_attached_to(this), \
157  _M_message(__gnu_debug::__msg_erase_different) \
158  ._M_sequence(*this, "this") \
159  ._M_iterator(_First, #_First) \
160  ._M_iterator(_Last, #_Last))
161 
162 /** Verify that we can erase the elements in the iterator range
163  * (_First, _Last). We can erase the elements if (_First, _Last) is a
164  * valid iterator range within this sequence.
165 */
166 #define __glibcxx_check_erase_range_after(_First,_Last) \
167 _GLIBCXX_DEBUG_VERIFY(_First._M_can_compare(_Last), \
168  _M_message(__gnu_debug::__msg_erase_different) \
169  ._M_sequence(*this, "this") \
170  ._M_iterator(_First, #_First) \
171  ._M_iterator(_Last, #_Last)); \
172 _GLIBCXX_DEBUG_VERIFY(_First._M_attached_to(this), \
173  _M_message(__gnu_debug::__msg_erase_different) \
174  ._M_sequence(*this, "this") \
175  ._M_iterator(_First, #_First)); \
176 _GLIBCXX_DEBUG_VERIFY(_First != _Last, \
177  _M_message(__gnu_debug::__msg_valid_range2) \
178  ._M_sequence(*this, "this") \
179  ._M_iterator(_First, #_First) \
180  ._M_iterator(_Last, #_Last)); \
181 _GLIBCXX_DEBUG_VERIFY(_First._M_incrementable(), \
182  _M_message(__gnu_debug::__msg_valid_range2) \
183  ._M_sequence(*this, "this") \
184  ._M_iterator(_First, #_First) \
185  ._M_iterator(_Last, #_Last)); \
186 _GLIBCXX_DEBUG_VERIFY(!_Last._M_is_before_begin(), \
187  _M_message(__gnu_debug::__msg_valid_range2) \
188  ._M_sequence(*this, "this") \
189  ._M_iterator(_First, #_First) \
190  ._M_iterator(_Last, #_Last)) \
191 
192 // Verify that the subscript _N is less than the container's size.
193 #define __glibcxx_check_subscript(_N) \
194 _GLIBCXX_DEBUG_VERIFY(_N < this->size(), \
195  _M_message(__gnu_debug::__msg_subscript_oob) \
196  ._M_sequence(*this, "this") \
197  ._M_integer(_N, #_N) \
198  ._M_integer(this->size(), "size"))
199 
200 // Verify that the container is nonempty
201 #define __glibcxx_check_nonempty() \
202 _GLIBCXX_DEBUG_VERIFY(! this->empty(), \
203  _M_message(__gnu_debug::__msg_empty) \
204  ._M_sequence(*this, "this"))
205 
206 // Verify that the iterator range [_First, _Last) is sorted
207 #define __glibcxx_check_sorted(_First,_Last) \
208 __glibcxx_check_valid_range(_First,_Last); \
209 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted(_First, _Last), \
210  _M_message(__gnu_debug::__msg_unsorted) \
211  ._M_iterator(_First, #_First) \
212  ._M_iterator(_Last, #_Last))
213 
214 /** Verify that the iterator range [_First, _Last) is sorted by the
215  predicate _Pred. */
216 #define __glibcxx_check_sorted_pred(_First,_Last,_Pred) \
217 __glibcxx_check_valid_range(_First,_Last); \
218 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted(_First, _Last, _Pred), \
219  _M_message(__gnu_debug::__msg_unsorted_pred) \
220  ._M_iterator(_First, #_First) \
221  ._M_iterator(_Last, #_Last) \
222  ._M_string(#_Pred))
223 
224 // Special variant for std::merge, std::includes, std::set_*
225 #define __glibcxx_check_sorted_set(_First1,_Last1,_First2) \
226 __glibcxx_check_valid_range(_First1,_Last1); \
227 _GLIBCXX_DEBUG_VERIFY( \
228  __gnu_debug::__check_sorted_set(_First1, _Last1, _First2), \
229  _M_message(__gnu_debug::__msg_unsorted) \
230  ._M_iterator(_First1, #_First1) \
231  ._M_iterator(_Last1, #_Last1))
232 
233 // Likewise with a _Pred.
234 #define __glibcxx_check_sorted_set_pred(_First1,_Last1,_First2,_Pred) \
235 __glibcxx_check_valid_range(_First1,_Last1); \
236 _GLIBCXX_DEBUG_VERIFY( \
237  __gnu_debug::__check_sorted_set(_First1, _Last1, _First2, _Pred), \
238  _M_message(__gnu_debug::__msg_unsorted_pred) \
239  ._M_iterator(_First1, #_First1) \
240  ._M_iterator(_Last1, #_Last1) \
241  ._M_string(#_Pred))
242 
243 /** Verify that the iterator range [_First, _Last) is partitioned
244  w.r.t. the value _Value. */
245 #define __glibcxx_check_partitioned_lower(_First,_Last,_Value) \
246 __glibcxx_check_valid_range(_First,_Last); \
247 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_lower(_First, _Last, \
248  _Value), \
249  _M_message(__gnu_debug::__msg_unpartitioned) \
250  ._M_iterator(_First, #_First) \
251  ._M_iterator(_Last, #_Last) \
252  ._M_string(#_Value))
253 
254 #define __glibcxx_check_partitioned_upper(_First,_Last,_Value) \
255 __glibcxx_check_valid_range(_First,_Last); \
256 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_upper(_First, _Last, \
257  _Value), \
258  _M_message(__gnu_debug::__msg_unpartitioned) \
259  ._M_iterator(_First, #_First) \
260  ._M_iterator(_Last, #_Last) \
261  ._M_string(#_Value))
262 
263 /** Verify that the iterator range [_First, _Last) is partitioned
264  w.r.t. the value _Value and predicate _Pred. */
265 #define __glibcxx_check_partitioned_lower_pred(_First,_Last,_Value,_Pred) \
266 __glibcxx_check_valid_range(_First,_Last); \
267 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_lower(_First, _Last, \
268  _Value, _Pred), \
269  _M_message(__gnu_debug::__msg_unpartitioned_pred) \
270  ._M_iterator(_First, #_First) \
271  ._M_iterator(_Last, #_Last) \
272  ._M_string(#_Pred) \
273  ._M_string(#_Value))
274 
275 /** Verify that the iterator range [_First, _Last) is partitioned
276  w.r.t. the value _Value and predicate _Pred. */
277 #define __glibcxx_check_partitioned_upper_pred(_First,_Last,_Value,_Pred) \
278 __glibcxx_check_valid_range(_First,_Last); \
279 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_upper(_First, _Last, \
280  _Value, _Pred), \
281  _M_message(__gnu_debug::__msg_unpartitioned_pred) \
282  ._M_iterator(_First, #_First) \
283  ._M_iterator(_Last, #_Last) \
284  ._M_string(#_Pred) \
285  ._M_string(#_Value))
286 
287 // Verify that the iterator range [_First, _Last) is a heap
288 #define __glibcxx_check_heap(_First,_Last) \
289 _GLIBCXX_DEBUG_VERIFY(std::__is_heap(_First, _Last), \
290  _M_message(__gnu_debug::__msg_not_heap) \
291  ._M_iterator(_First, #_First) \
292  ._M_iterator(_Last, #_Last))
293 
294 /** Verify that the iterator range [_First, _Last) is a heap
295  w.r.t. the predicate _Pred. */
296 #define __glibcxx_check_heap_pred(_First,_Last,_Pred) \
297 _GLIBCXX_DEBUG_VERIFY(std::__is_heap(_First, _Last, _Pred), \
298  _M_message(__gnu_debug::__msg_not_heap_pred) \
299  ._M_iterator(_First, #_First) \
300  ._M_iterator(_Last, #_Last) \
301  ._M_string(#_Pred))
302 
303 #ifdef _GLIBCXX_DEBUG_PEDANTIC
304 # define __glibcxx_check_string(_String) _GLIBCXX_DEBUG_ASSERT(_String != 0)
305 # define __glibcxx_check_string_len(_String,_Len) \
306  _GLIBCXX_DEBUG_ASSERT(_String != 0 || _Len == 0)
307 #else
308 # define __glibcxx_check_string(_String)
309 # define __glibcxx_check_string_len(_String,_Len)
310 #endif
311 
312 #endif