1// Debugging support implementation -*- C++ -*-
2
3// Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 * "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 (i.e., either dereferenceable or
60 *  past-the-end) and that it reference the sequence we are inserting
61 *  into. Note that this macro is only valid when the container is a
62 *  _Safe_sequence and 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 the values in the iterator range
75 *  [_First, _Last) into *this with the iterator _Position.  Insertion
76 *  into a container at a specific position requires that the iterator
77 *  be nonsingular (i.e., either dereferenceable or past-the-end),
78 *  that it reference the sequence we are inserting into, and that the
79 *  iterator range [_First, Last) is a valid (possibly empty)
80 *  range. Note that this macro is only valid when the container is a
81 *  _Safe_sequence and the iterator is a _Safe_iterator.
82 *
83 *  @tbd We would like to be able to check for noninterference of
84 *  _Position and the range [_First, _Last), but that can't (in
85 *  general) be done.
86*/
87#define __glibcxx_check_insert_range(_Position,_First,_Last)		\
88__glibcxx_check_valid_range(_First,_Last);				\
89_GLIBCXX_DEBUG_VERIFY(!_Position._M_singular(),				\
90		      _M_message(__gnu_debug::__msg_insert_singular)    \
91                      ._M_sequence(*this, "this")			\
92		      ._M_iterator(_Position, #_Position));		\
93_GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this),			\
94		      _M_message(__gnu_debug::__msg_insert_different)   \
95		      ._M_sequence(*this, "this")			\
96		      ._M_iterator(_Position, #_Position))
97
98/** Verify that we can erase the element referenced by the iterator
99 * _Position. We can erase the element if the _Position iterator is
100 * dereferenceable and references this sequence.
101*/
102#define __glibcxx_check_erase(_Position)				\
103_GLIBCXX_DEBUG_VERIFY(_Position._M_dereferenceable(),			\
104		      _M_message(__gnu_debug::__msg_erase_bad)	        \
105                      ._M_sequence(*this, "this")			\
106		      ._M_iterator(_Position, #_Position));		\
107_GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this),			\
108		      _M_message(__gnu_debug::__msg_erase_different)    \
109		      ._M_sequence(*this, "this")			\
110		      ._M_iterator(_Position, #_Position))
111
112/** Verify that we can erase the elements in the iterator range
113 *  [_First, _Last). We can erase the elements if [_First, _Last) is a
114 *  valid iterator range within this sequence.
115*/
116#define __glibcxx_check_erase_range(_First,_Last)			\
117__glibcxx_check_valid_range(_First,_Last);				\
118_GLIBCXX_DEBUG_VERIFY(_First._M_attached_to(this),			\
119		      _M_message(__gnu_debug::__msg_erase_different)    \
120                      ._M_sequence(*this, "this")			\
121		      ._M_iterator(_First, #_First)			\
122		      ._M_iterator(_Last, #_Last))
123
124// Verify that the subscript _N is less than the container's size.
125#define __glibcxx_check_subscript(_N)					\
126_GLIBCXX_DEBUG_VERIFY(_N < this->size(),				\
127		      _M_message(__gnu_debug::__msg_subscript_oob)      \
128                      ._M_sequence(*this, "this")			\
129		      ._M_integer(_N, #_N)				\
130		      ._M_integer(this->size(), "size"))
131
132// Verify that the container is nonempty
133#define __glibcxx_check_nonempty()					\
134_GLIBCXX_DEBUG_VERIFY(! this->empty(),					\
135		      _M_message(__gnu_debug::__msg_empty)	        \
136                      ._M_sequence(*this, "this"))
137
138// Verify that the iterator range [_First, _Last) is sorted
139#define __glibcxx_check_sorted(_First,_Last)				\
140__glibcxx_check_valid_range(_First,_Last);				\
141_GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted(_First, _Last),	\
142		      _M_message(__gnu_debug::__msg_unsorted)	        \
143                      ._M_iterator(_First, #_First)			\
144		      ._M_iterator(_Last, #_Last))
145
146/** Verify that the iterator range [_First, _Last) is sorted by the
147    predicate _Pred. */
148#define __glibcxx_check_sorted_pred(_First,_Last,_Pred)			\
149__glibcxx_check_valid_range(_First,_Last);				\
150_GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted(_First, _Last, _Pred), \
151		      _M_message(__gnu_debug::__msg_unsorted_pred)      \
152                      ._M_iterator(_First, #_First)			\
153		      ._M_iterator(_Last, #_Last)			\
154		      ._M_string(#_Pred))
155
156// Special variant for std::merge, std::includes, std::set_*
157#define __glibcxx_check_sorted_set(_First1,_Last1,_First2)		\
158__glibcxx_check_valid_range(_First1,_Last1);				\
159_GLIBCXX_DEBUG_VERIFY(                                                  \
160  __gnu_debug::__check_sorted_set(_First1, _Last1, _First2),		\
161  _M_message(__gnu_debug::__msg_unsorted)				\
162  ._M_iterator(_First1, #_First1)					\
163  ._M_iterator(_Last1, #_Last1))
164
165// Likewise with a _Pred.
166#define __glibcxx_check_sorted_set_pred(_First1,_Last1,_First2,_Pred)	\
167__glibcxx_check_valid_range(_First1,_Last1);        			\
168_GLIBCXX_DEBUG_VERIFY(							\
169  __gnu_debug::__check_sorted_set(_First1, _Last1, _First2, _Pred),	\
170  _M_message(__gnu_debug::__msg_unsorted_pred)				\
171  ._M_iterator(_First1, #_First1)					\
172  ._M_iterator(_Last1, #_Last1)						\
173  ._M_string(#_Pred))
174
175/** Verify that the iterator range [_First, _Last) is partitioned
176    w.r.t. the value _Value. */
177#define __glibcxx_check_partitioned_lower(_First,_Last,_Value)		\
178__glibcxx_check_valid_range(_First,_Last);				\
179_GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_lower(_First, _Last, \
180							    _Value),	\
181		      _M_message(__gnu_debug::__msg_unpartitioned)      \
182		      ._M_iterator(_First, #_First)			\
183		      ._M_iterator(_Last, #_Last)			\
184		      ._M_string(#_Value))
185
186#define __glibcxx_check_partitioned_upper(_First,_Last,_Value)		\
187__glibcxx_check_valid_range(_First,_Last);				\
188_GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_upper(_First, _Last, \
189							    _Value),	\
190		      _M_message(__gnu_debug::__msg_unpartitioned)      \
191		      ._M_iterator(_First, #_First)			\
192		      ._M_iterator(_Last, #_Last)			\
193		      ._M_string(#_Value))
194
195/** Verify that the iterator range [_First, _Last) is partitioned
196    w.r.t. the value _Value and predicate _Pred. */
197#define __glibcxx_check_partitioned_lower_pred(_First,_Last,_Value,_Pred) \
198__glibcxx_check_valid_range(_First,_Last);				\
199_GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_lower(_First, _Last, \
200							 _Value, _Pred), \
201		      _M_message(__gnu_debug::__msg_unpartitioned_pred) \
202		      ._M_iterator(_First, #_First)			\
203		      ._M_iterator(_Last, #_Last)			\
204		      ._M_string(#_Pred)				\
205                      ._M_string(#_Value))
206
207/** Verify that the iterator range [_First, _Last) is partitioned
208    w.r.t. the value _Value and predicate _Pred. */
209#define __glibcxx_check_partitioned_upper_pred(_First,_Last,_Value,_Pred) \
210__glibcxx_check_valid_range(_First,_Last);				\
211_GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_upper(_First, _Last, \
212							 _Value, _Pred), \
213		      _M_message(__gnu_debug::__msg_unpartitioned_pred) \
214		      ._M_iterator(_First, #_First)			\
215		      ._M_iterator(_Last, #_Last)			\
216		      ._M_string(#_Pred)				\
217                      ._M_string(#_Value))
218
219// Verify that the iterator range [_First, _Last) is a heap
220#define __glibcxx_check_heap(_First,_Last)				\
221__glibcxx_check_valid_range(_First,_Last);				\
222_GLIBCXX_DEBUG_VERIFY(std::__is_heap(_First, _Last),		        \
223		      _M_message(__gnu_debug::__msg_not_heap)	        \
224		      ._M_iterator(_First, #_First)			\
225		      ._M_iterator(_Last, #_Last))
226
227/** Verify that the iterator range [_First, _Last) is a heap
228    w.r.t. the predicate _Pred. */
229#define __glibcxx_check_heap_pred(_First,_Last,_Pred)			\
230__glibcxx_check_valid_range(_First,_Last);				\
231_GLIBCXX_DEBUG_VERIFY(std::__is_heap(_First, _Last, _Pred),		\
232		      _M_message(__gnu_debug::__msg_not_heap_pred)      \
233                      ._M_iterator(_First, #_First)			\
234		      ._M_iterator(_Last, #_Last)			\
235		      ._M_string(#_Pred))
236
237#ifdef _GLIBCXX_DEBUG_PEDANTIC
238#  define __glibcxx_check_string(_String) _GLIBCXX_DEBUG_ASSERT(_String != 0)
239#  define __glibcxx_check_string_len(_String,_Len) \
240       _GLIBCXX_DEBUG_ASSERT(_String != 0 || _Len == 0)
241#else
242#  define __glibcxx_check_string(_String)
243#  define __glibcxx_check_string_len(_String,_Len)
244#endif
245
246#endif
247