1 // Debugging support implementation -*- C++ -*-
2 
3 // Copyright (C) 2003, 2005, 2006
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 2, 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 // 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,
20 // USA.
21 
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.
30 
31 /** @file debug/macros.h
32  *  This file is a GNU debug extension to the Standard C++ Library.
33  */
34 
35 #ifndef _GLIBCXX_DEBUG_MACROS_H
36 #define _GLIBCXX_DEBUG_MACROS_H 1
37 
38 /**
39  * Macros used by the implementation to verify certain
40  * properties. These macros may only be used directly by the debug
41  * wrappers. Note that these are macros (instead of the more obviously
42  * "correct" choice of making them functions) because we need line and
43  * file information at the call site, to minimize the distance between
44  * the user error and where the error is reported.
45  *
46  */
47 #define _GLIBCXX_DEBUG_VERIFY(_Condition,_ErrorMessage)		        \
48   do 									\
49   {									\
50     if (! (_Condition))							\
51       __gnu_debug::_Error_formatter::_M_at(__FILE__, __LINE__)	        \
52 	  ._ErrorMessage._M_error();					\
53   } while (false)
54 
55 // Verify that [_First, _Last) forms a valid iterator range.
56 #define __glibcxx_check_valid_range(_First,_Last)			\
57 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__valid_range(_First, _Last),	\
58 		      _M_message(__gnu_debug::__msg_valid_range)	\
59 		      ._M_iterator(_First, #_First)			\
60 		      ._M_iterator(_Last, #_Last))
61 
62 /** Verify that we can insert into *this with the iterator _Position.
63  *  Insertion into a container at a specific position requires that
64  *  the iterator be nonsingular (i.e., either dereferenceable or
65  *  past-the-end) and that it reference the sequence we are inserting
66  *  into. Note that this macro is only valid when the container is a
67  *  _Safe_sequence and the iterator is a _Safe_iterator.
68 */
69 #define __glibcxx_check_insert(_Position)				\
70 _GLIBCXX_DEBUG_VERIFY(!_Position._M_singular(),				\
71 		      _M_message(__gnu_debug::__msg_insert_singular) \
72 		      ._M_sequence(*this, "this")			\
73 		      ._M_iterator(_Position, #_Position));		\
74 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this),			\
75 		      _M_message(__gnu_debug::__msg_insert_different) \
76 		      ._M_sequence(*this, "this")			\
77 		      ._M_iterator(_Position, #_Position))
78 
79 /** Verify that we can insert the values in the iterator range
80  *  [_First, _Last) into *this with the iterator _Position.  Insertion
81  *  into a container at a specific position requires that the iterator
82  *  be nonsingular (i.e., either dereferenceable or past-the-end),
83  *  that it reference the sequence we are inserting into, and that the
84  *  iterator range [_First, Last) is a valid (possibly empty)
85  *  range. Note that this macro is only valid when the container is a
86  *  _Safe_sequence and the iterator is a _Safe_iterator.
87  *
88  *  @tbd We would like to be able to check for noninterference of
89  *  _Position and the range [_First, _Last), but that can't (in
90  *  general) be done.
91 */
92 #define __glibcxx_check_insert_range(_Position,_First,_Last)		\
93 __glibcxx_check_valid_range(_First,_Last);				\
94 _GLIBCXX_DEBUG_VERIFY(!_Position._M_singular(),				\
95 		      _M_message(__gnu_debug::__msg_insert_singular)    \
96                       ._M_sequence(*this, "this")			\
97 		      ._M_iterator(_Position, #_Position));		\
98 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this),			\
99 		      _M_message(__gnu_debug::__msg_insert_different)   \
100 		      ._M_sequence(*this, "this")			\
101 		      ._M_iterator(_Position, #_Position))
102 
103 /** Verify that we can erase the element referenced by the iterator
104  * _Position. We can erase the element if the _Position iterator is
105  * dereferenceable and references this sequence.
106 */
107 #define __glibcxx_check_erase(_Position)				\
108 _GLIBCXX_DEBUG_VERIFY(_Position._M_dereferenceable(),			\
109 		      _M_message(__gnu_debug::__msg_erase_bad)	        \
110                       ._M_sequence(*this, "this")			\
111 		      ._M_iterator(_Position, #_Position));		\
112 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this),			\
113 		      _M_message(__gnu_debug::__msg_erase_different)    \
114 		      ._M_sequence(*this, "this")			\
115 		      ._M_iterator(_Position, #_Position))
116 
117 /** Verify that we can erase the elements in the iterator range
118  *  [_First, _Last). We can erase the elements if [_First, _Last) is a
119  *  valid iterator range within this sequence.
120 */
121 #define __glibcxx_check_erase_range(_First,_Last)			\
122 __glibcxx_check_valid_range(_First,_Last);				\
123 _GLIBCXX_DEBUG_VERIFY(_First._M_attached_to(this),			\
124 		      _M_message(__gnu_debug::__msg_erase_different)    \
125                       ._M_sequence(*this, "this")			\
126 		      ._M_iterator(_First, #_First)			\
127 		      ._M_iterator(_Last, #_Last))
128 
129 // Verify that the subscript _N is less than the container's size.
130 #define __glibcxx_check_subscript(_N)					\
131 _GLIBCXX_DEBUG_VERIFY(_N < this->size(),				\
132 		      _M_message(__gnu_debug::__msg_subscript_oob)      \
133                       ._M_sequence(*this, "this")			\
134 		      ._M_integer(_N, #_N)				\
135 		      ._M_integer(this->size(), "size"))
136 
137 // Verify that the container is nonempty
138 #define __glibcxx_check_nonempty()					\
139 _GLIBCXX_DEBUG_VERIFY(! this->empty(),					\
140 		      _M_message(__gnu_debug::__msg_empty)	        \
141                       ._M_sequence(*this, "this"))
142 
143 // Verify that the < operator for elements in the sequence is a
144 // StrictWeakOrdering by checking that it is irreflexive.
145 #define __glibcxx_check_strict_weak_ordering(_First,_Last)	\
146 _GLIBCXX_DEBUG_ASSERT(_First == _Last || !(*_First < *_First))
147 
148 // Verify that the predicate is StrictWeakOrdering by checking that it
149 // is irreflexive.
150 #define __glibcxx_check_strict_weak_ordering_pred(_First,_Last,_Pred)	\
151 _GLIBCXX_DEBUG_ASSERT(_First == _Last || !_Pred(*_First, *_First))
152 
153 
154 // Verify that the iterator range [_First, _Last) is sorted
155 #define __glibcxx_check_sorted(_First,_Last)				\
156 __glibcxx_check_valid_range(_First,_Last);				\
157 __glibcxx_check_strict_weak_ordering(_First,_Last);			\
158 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted(_First, _Last),	\
159 		      _M_message(__gnu_debug::__msg_unsorted)	        \
160                       ._M_iterator(_First, #_First)			\
161 		      ._M_iterator(_Last, #_Last))
162 
163 /** Verify that the iterator range [_First, _Last) is sorted by the
164     predicate _Pred. */
165 #define __glibcxx_check_sorted_pred(_First,_Last,_Pred)			\
166 __glibcxx_check_valid_range(_First,_Last);				\
167 __glibcxx_check_strict_weak_ordering_pred(_First,_Last,_Pred);	        \
168 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted(_First, _Last, _Pred), \
169 		      _M_message(__gnu_debug::__msg_unsorted_pred)      \
170                       ._M_iterator(_First, #_First)			\
171 		      ._M_iterator(_Last, #_Last)			\
172 		      ._M_string(#_Pred))
173 
174 /** Verify that the iterator range [_First, _Last) is partitioned
175     w.r.t. the value _Value. */
176 #define __glibcxx_check_partitioned(_First,_Last,_Value)		\
177 __glibcxx_check_valid_range(_First,_Last);				\
178 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned(_First, _Last,   \
179 							    _Value),	\
180 		      _M_message(__gnu_debug::__msg_unpartitioned)      \
181 		      ._M_iterator(_First, #_First)			\
182 		      ._M_iterator(_Last, #_Last)			\
183 		      ._M_string(#_Value))
184 
185 /** Verify that the iterator range [_First, _Last) is partitioned
186     w.r.t. the value _Value and predicate _Pred. */
187 #define __glibcxx_check_partitioned_pred(_First,_Last,_Value,_Pred)	\
188 __glibcxx_check_valid_range(_First,_Last);				\
189 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned(_First, _Last,   \
190 							 _Value, _Pred), \
191 		      _M_message(__gnu_debug::__msg_unpartitioned_pred) \
192 		      ._M_iterator(_First, #_First)			\
193 		      ._M_iterator(_Last, #_Last)			\
194 		      ._M_string(#_Pred)				\
195                       ._M_string(#_Value))
196 
197 // Verify that the iterator range [_First, _Last) is a heap
198 #define __glibcxx_check_heap(_First,_Last)				\
199 __glibcxx_check_valid_range(_First,_Last);				\
200 _GLIBCXX_DEBUG_VERIFY(std::__is_heap(_First, _Last),		        \
201 		      _M_message(__gnu_debug::__msg_not_heap)	        \
202 		      ._M_iterator(_First, #_First)			\
203 		      ._M_iterator(_Last, #_Last))
204 
205 /** Verify that the iterator range [_First, _Last) is a heap
206     w.r.t. the predicate _Pred. */
207 #define __glibcxx_check_heap_pred(_First,_Last,_Pred)			\
208 __glibcxx_check_valid_range(_First,_Last);				\
209 _GLIBCXX_DEBUG_VERIFY(std::__is_heap(_First, _Last, _Pred),		\
210 		      _M_message(__gnu_debug::__msg_not_heap_pred)      \
211                       ._M_iterator(_First, #_First)			\
212 		      ._M_iterator(_Last, #_Last)			\
213 		      ._M_string(#_Pred))
214 
215 #ifdef _GLIBCXX_DEBUG_PEDANTIC
216 #  define __glibcxx_check_string(_String) _GLIBCXX_DEBUG_ASSERT(_String != 0)
217 #  define __glibcxx_check_string_len(_String,_Len) \
218        _GLIBCXX_DEBUG_ASSERT(_String != 0 || _Len == 0)
219 #else
220 #  define __glibcxx_check_string(_String)
221 #  define __glibcxx_check_string_len(_String,_Len)
222 #endif
223 
224 #endif
225