1 // Debugging support implementation -*- C++ -*-
3 // Copyright (C) 2003, 2005
4 // Free Software Foundation, Inc.
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 2, or (at your option)
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.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
31 #ifndef _GLIBCXX_DEBUG_MACROS_H
32 #define _GLIBCXX_DEBUG_MACROS_H 1
35 * Macros used by the implementation to verify certain
36 * properties. These macros may only be used directly by the debug
37 * wrappers. Note that these are macros (instead of the more obviously
38 * "correct" choice of making them functions) because we need line and
39 * file information at the call site, to minimize the distance between
40 * the user error and where the error is reported.
44 { void __fancy_abort(const char*, int, const char*, const char*); }
45 #define _GLIBCXX_DEBUG_ABORT(_Condition) \
48 ::__gnu_debug::__fancy_abort(__FILE__, __LINE__, \
49 __PRETTY_FUNCTION__, \
53 #define _GLIBCXX_DEBUG_VERIFY(_Condition,_ErrorMessage) \
56 ::__gnu_debug::_Error_formatter::_M_at(__FILE__, __LINE__) \
57 ._ErrorMessage._M_error(); \
60 // Verify that [_First, _Last) forms a valid iterator range.
61 #define __glibcxx_check_valid_range(_First,_Last) \
62 _GLIBCXX_DEBUG_VERIFY(::__gnu_debug::__valid_range(_First, _Last), \
63 _M_message(::__gnu_debug::__msg_valid_range) \
64 ._M_iterator(_First, #_First) \
65 ._M_iterator(_Last, #_Last))
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 (i.e., either dereferenceable or
70 * past-the-end) and that it reference the sequence we are inserting
71 * into. Note that this macro is only valid when the container is a
72 * _Safe_sequence and the iterator is a _Safe_iterator.
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))
84 /** Verify that we can insert the values in the iterator range
85 * [_First, _Last) into *this with the iterator _Position. Insertion
86 * into a container at a specific position requires that the iterator
87 * be nonsingular (i.e., either dereferenceable or past-the-end),
88 * that it reference the sequence we are inserting into, and that the
89 * iterator range [_First, Last) is a valid (possibly empty)
90 * range. Note that this macro is only valid when the container is a
91 * _Safe_sequence and the iterator is a _Safe_iterator.
93 * @tbd We would like to be able to check for noninterference of
94 * _Position and the range [_First, _Last), but that can't (in
97 #define __glibcxx_check_insert_range(_Position,_First,_Last) \
98 __glibcxx_check_valid_range(_First,_Last); \
99 _GLIBCXX_DEBUG_VERIFY(!_Position._M_singular(), \
100 _M_message(::__gnu_debug::__msg_insert_singular) \
101 ._M_sequence(*this, "this") \
102 ._M_iterator(_Position, #_Position)); \
103 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this), \
104 _M_message(::__gnu_debug::__msg_insert_different) \
105 ._M_sequence(*this, "this") \
106 ._M_iterator(_Position, #_Position))
108 /** Verify that we can erase the element referenced by the iterator
109 * _Position. We can erase the element if the _Position iterator is
110 * dereferenceable and references this sequence.
112 #define __glibcxx_check_erase(_Position) \
113 _GLIBCXX_DEBUG_VERIFY(_Position._M_dereferenceable(), \
114 _M_message(::__gnu_debug::__msg_erase_bad) \
115 ._M_sequence(*this, "this") \
116 ._M_iterator(_Position, #_Position)); \
117 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this), \
118 _M_message(::__gnu_debug::__msg_erase_different) \
119 ._M_sequence(*this, "this") \
120 ._M_iterator(_Position, #_Position))
122 /** Verify that we can erase the elements in the iterator range
123 * [_First, _Last). We can erase the elements if [_First, _Last) is a
124 * valid iterator range within this sequence.
126 #define __glibcxx_check_erase_range(_First,_Last) \
127 __glibcxx_check_valid_range(_First,_Last); \
128 _GLIBCXX_DEBUG_VERIFY(_First._M_attached_to(this), \
129 _M_message(::__gnu_debug::__msg_erase_different) \
130 ._M_sequence(*this, "this") \
131 ._M_iterator(_First, #_First) \
132 ._M_iterator(_Last, #_Last))
134 // Verify that the subscript _N is less than the container's size.
135 #define __glibcxx_check_subscript(_N) \
136 _GLIBCXX_DEBUG_VERIFY(_N < this->size(), \
137 _M_message(::__gnu_debug::__msg_subscript_oob) \
138 ._M_sequence(*this, "this") \
139 ._M_integer(_N, #_N) \
140 ._M_integer(this->size(), "size"))
142 // Verify that the container is nonempty
143 #define __glibcxx_check_nonempty() \
144 _GLIBCXX_DEBUG_VERIFY(! this->empty(), \
145 _M_message(::__gnu_debug::__msg_empty) \
146 ._M_sequence(*this, "this"))
148 // Verify that the < operator for elements in the sequence is a
149 // StrictWeakOrdering by checking that it is irreflexive.
150 #define __glibcxx_check_strict_weak_ordering(_First,_Last) \
151 _GLIBCXX_DEBUG_ASSERT(_First == _Last || !(*_First < *_First))
153 // Verify that the predicate is StrictWeakOrdering by checking that it
155 #define __glibcxx_check_strict_weak_ordering_pred(_First,_Last,_Pred) \
156 _GLIBCXX_DEBUG_ASSERT(_First == _Last || !_Pred(*_First, *_First))
159 // Verify that the iterator range [_First, _Last) is sorted
160 #define __glibcxx_check_sorted(_First,_Last) \
161 __glibcxx_check_valid_range(_First,_Last); \
162 __glibcxx_check_strict_weak_ordering(_First,_Last); \
163 _GLIBCXX_DEBUG_VERIFY(::__gnu_debug::__check_sorted(_First, _Last), \
164 _M_message(::__gnu_debug::__msg_unsorted) \
165 ._M_iterator(_First, #_First) \
166 ._M_iterator(_Last, #_Last))
168 /** Verify that the iterator range [_First, _Last) is sorted by the
170 #define __glibcxx_check_sorted_pred(_First,_Last,_Pred) \
171 __glibcxx_check_valid_range(_First,_Last); \
172 __glibcxx_check_strict_weak_ordering_pred(_First,_Last,_Pred); \
173 _GLIBCXX_DEBUG_VERIFY(::__gnu_debug::__check_sorted(_First, _Last, _Pred), \
174 _M_message(::__gnu_debug::__msg_unsorted_pred) \
175 ._M_iterator(_First, #_First) \
176 ._M_iterator(_Last, #_Last) \
179 /** Verify that the iterator range [_First, _Last) is partitioned
180 w.r.t. the value _Value. */
181 #define __glibcxx_check_partitioned(_First,_Last,_Value) \
182 __glibcxx_check_valid_range(_First,_Last); \
183 _GLIBCXX_DEBUG_VERIFY(::__gnu_debug::__check_partitioned(_First, _Last, \
185 _M_message(::__gnu_debug::__msg_unpartitioned) \
186 ._M_iterator(_First, #_First) \
187 ._M_iterator(_Last, #_Last) \
190 /** Verify that the iterator range [_First, _Last) is partitioned
191 w.r.t. the value _Value and predicate _Pred. */
192 #define __glibcxx_check_partitioned_pred(_First,_Last,_Value,_Pred) \
193 __glibcxx_check_valid_range(_First,_Last); \
194 _GLIBCXX_DEBUG_VERIFY(::__gnu_debug::__check_partitioned(_First, _Last, \
196 _M_message(::__gnu_debug::__msg_unpartitioned_pred) \
197 ._M_iterator(_First, #_First) \
198 ._M_iterator(_Last, #_Last) \
202 // Verify that the iterator range [_First, _Last) is a heap
203 #define __glibcxx_check_heap(_First,_Last) \
204 __glibcxx_check_valid_range(_First,_Last); \
205 _GLIBCXX_DEBUG_VERIFY(::std::__is_heap(_First, _Last), \
206 _M_message(::__gnu_debug::__msg_not_heap) \
207 ._M_iterator(_First, #_First) \
208 ._M_iterator(_Last, #_Last))
210 /** Verify that the iterator range [_First, _Last) is a heap
211 w.r.t. the predicate _Pred. */
212 #define __glibcxx_check_heap_pred(_First,_Last,_Pred) \
213 __glibcxx_check_valid_range(_First,_Last); \
214 _GLIBCXX_DEBUG_VERIFY(::std::__is_heap(_First, _Last, _Pred), \
215 _M_message(::__gnu_debug::__msg_not_heap_pred) \
216 ._M_iterator(_First, #_First) \
217 ._M_iterator(_Last, #_Last) \
220 #ifdef _GLIBCXX_DEBUG_PEDANTIC
221 # define __glibcxx_check_string(_String) _GLIBCXX_DEBUG_ASSERT(_String != 0)
222 # define __glibcxx_check_string_len(_String,_Len) \
223 _GLIBCXX_DEBUG_ASSERT(_String != 0 || _Len == 0)
225 # define __glibcxx_check_string(_String)
226 # define __glibcxx_check_string_len(_String,_Len)