1169691Skan// Debugging support implementation -*- C++ -*-
2169691Skan
3169691Skan// Copyright (C) 2003, 2005, 2006
4169691Skan// Free Software Foundation, Inc.
5169691Skan//
6169691Skan// This file is part of the GNU ISO C++ Library.  This library is free
7169691Skan// software; you can redistribute it and/or modify it under the
8169691Skan// terms of the GNU General Public License as published by the
9169691Skan// Free Software Foundation; either version 2, or (at your option)
10169691Skan// any later version.
11169691Skan
12169691Skan// This library is distributed in the hope that it will be useful,
13169691Skan// but WITHOUT ANY WARRANTY; without even the implied warranty of
14169691Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15169691Skan// GNU General Public License for more details.
16169691Skan
17169691Skan// You should have received a copy of the GNU General Public License along
18169691Skan// with this library; see the file COPYING.  If not, write to the Free
19169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20169691Skan// USA.
21169691Skan
22169691Skan// As a special exception, you may use this file as part of a free software
23169691Skan// library without restriction.  Specifically, if other files instantiate
24169691Skan// templates or use macros or inline functions from this file, or you compile
25169691Skan// this file and link it with other files to produce an executable, this
26169691Skan// file does not by itself cause the resulting executable to be covered by
27169691Skan// the GNU General Public License.  This exception does not however
28169691Skan// invalidate any other reasons why the executable file might be covered by
29169691Skan// the GNU General Public License.
30169691Skan
31169691Skan/** @file debug/macros.h
32169691Skan *  This file is a GNU debug extension to the Standard C++ Library.
33169691Skan */
34169691Skan
35169691Skan#ifndef _GLIBCXX_DEBUG_MACROS_H
36169691Skan#define _GLIBCXX_DEBUG_MACROS_H 1
37169691Skan
38169691Skan/**
39169691Skan * Macros used by the implementation to verify certain
40169691Skan * properties. These macros may only be used directly by the debug
41169691Skan * wrappers. Note that these are macros (instead of the more obviously
42169691Skan * "correct" choice of making them functions) because we need line and
43169691Skan * file information at the call site, to minimize the distance between
44169691Skan * the user error and where the error is reported.
45169691Skan *
46169691Skan */
47169691Skan#define _GLIBCXX_DEBUG_VERIFY(_Condition,_ErrorMessage)		        \
48169691Skan  do 									\
49169691Skan  {									\
50169691Skan    if (! (_Condition))							\
51169691Skan      __gnu_debug::_Error_formatter::_M_at(__FILE__, __LINE__)	        \
52169691Skan	  ._ErrorMessage._M_error();					\
53169691Skan  } while (false)
54169691Skan
55169691Skan// Verify that [_First, _Last) forms a valid iterator range.
56169691Skan#define __glibcxx_check_valid_range(_First,_Last)			\
57169691Skan_GLIBCXX_DEBUG_VERIFY(__gnu_debug::__valid_range(_First, _Last),	\
58169691Skan		      _M_message(__gnu_debug::__msg_valid_range)	\
59169691Skan		      ._M_iterator(_First, #_First)			\
60169691Skan		      ._M_iterator(_Last, #_Last))
61169691Skan
62169691Skan/** Verify that we can insert into *this with the iterator _Position.
63169691Skan *  Insertion into a container at a specific position requires that
64169691Skan *  the iterator be nonsingular (i.e., either dereferenceable or
65169691Skan *  past-the-end) and that it reference the sequence we are inserting
66169691Skan *  into. Note that this macro is only valid when the container is a
67169691Skan *  _Safe_sequence and the iterator is a _Safe_iterator.
68169691Skan*/
69169691Skan#define __glibcxx_check_insert(_Position)				\
70169691Skan_GLIBCXX_DEBUG_VERIFY(!_Position._M_singular(),				\
71169691Skan		      _M_message(__gnu_debug::__msg_insert_singular) \
72169691Skan		      ._M_sequence(*this, "this")			\
73169691Skan		      ._M_iterator(_Position, #_Position));		\
74169691Skan_GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this),			\
75169691Skan		      _M_message(__gnu_debug::__msg_insert_different) \
76169691Skan		      ._M_sequence(*this, "this")			\
77169691Skan		      ._M_iterator(_Position, #_Position))
78169691Skan
79169691Skan/** Verify that we can insert the values in the iterator range
80169691Skan *  [_First, _Last) into *this with the iterator _Position.  Insertion
81169691Skan *  into a container at a specific position requires that the iterator
82169691Skan *  be nonsingular (i.e., either dereferenceable or past-the-end),
83169691Skan *  that it reference the sequence we are inserting into, and that the
84169691Skan *  iterator range [_First, Last) is a valid (possibly empty)
85169691Skan *  range. Note that this macro is only valid when the container is a
86169691Skan *  _Safe_sequence and the iterator is a _Safe_iterator.
87169691Skan *
88169691Skan *  @tbd We would like to be able to check for noninterference of
89169691Skan *  _Position and the range [_First, _Last), but that can't (in
90169691Skan *  general) be done.
91169691Skan*/
92169691Skan#define __glibcxx_check_insert_range(_Position,_First,_Last)		\
93169691Skan__glibcxx_check_valid_range(_First,_Last);				\
94169691Skan_GLIBCXX_DEBUG_VERIFY(!_Position._M_singular(),				\
95169691Skan		      _M_message(__gnu_debug::__msg_insert_singular)    \
96169691Skan                      ._M_sequence(*this, "this")			\
97169691Skan		      ._M_iterator(_Position, #_Position));		\
98169691Skan_GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this),			\
99169691Skan		      _M_message(__gnu_debug::__msg_insert_different)   \
100169691Skan		      ._M_sequence(*this, "this")			\
101169691Skan		      ._M_iterator(_Position, #_Position))
102169691Skan
103169691Skan/** Verify that we can erase the element referenced by the iterator
104169691Skan * _Position. We can erase the element if the _Position iterator is
105169691Skan * dereferenceable and references this sequence.
106169691Skan*/
107169691Skan#define __glibcxx_check_erase(_Position)				\
108169691Skan_GLIBCXX_DEBUG_VERIFY(_Position._M_dereferenceable(),			\
109169691Skan		      _M_message(__gnu_debug::__msg_erase_bad)	        \
110169691Skan                      ._M_sequence(*this, "this")			\
111169691Skan		      ._M_iterator(_Position, #_Position));		\
112169691Skan_GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this),			\
113169691Skan		      _M_message(__gnu_debug::__msg_erase_different)    \
114169691Skan		      ._M_sequence(*this, "this")			\
115169691Skan		      ._M_iterator(_Position, #_Position))
116169691Skan
117169691Skan/** Verify that we can erase the elements in the iterator range
118169691Skan *  [_First, _Last). We can erase the elements if [_First, _Last) is a
119169691Skan *  valid iterator range within this sequence.
120169691Skan*/
121169691Skan#define __glibcxx_check_erase_range(_First,_Last)			\
122169691Skan__glibcxx_check_valid_range(_First,_Last);				\
123169691Skan_GLIBCXX_DEBUG_VERIFY(_First._M_attached_to(this),			\
124169691Skan		      _M_message(__gnu_debug::__msg_erase_different)    \
125169691Skan                      ._M_sequence(*this, "this")			\
126169691Skan		      ._M_iterator(_First, #_First)			\
127169691Skan		      ._M_iterator(_Last, #_Last))
128169691Skan
129169691Skan// Verify that the subscript _N is less than the container's size.
130169691Skan#define __glibcxx_check_subscript(_N)					\
131169691Skan_GLIBCXX_DEBUG_VERIFY(_N < this->size(),				\
132169691Skan		      _M_message(__gnu_debug::__msg_subscript_oob)      \
133169691Skan                      ._M_sequence(*this, "this")			\
134169691Skan		      ._M_integer(_N, #_N)				\
135169691Skan		      ._M_integer(this->size(), "size"))
136169691Skan
137169691Skan// Verify that the container is nonempty
138169691Skan#define __glibcxx_check_nonempty()					\
139169691Skan_GLIBCXX_DEBUG_VERIFY(! this->empty(),					\
140169691Skan		      _M_message(__gnu_debug::__msg_empty)	        \
141169691Skan                      ._M_sequence(*this, "this"))
142169691Skan
143169691Skan// Verify that the < operator for elements in the sequence is a
144169691Skan// StrictWeakOrdering by checking that it is irreflexive.
145169691Skan#define __glibcxx_check_strict_weak_ordering(_First,_Last)	\
146169691Skan_GLIBCXX_DEBUG_ASSERT(_First == _Last || !(*_First < *_First))
147169691Skan
148169691Skan// Verify that the predicate is StrictWeakOrdering by checking that it
149169691Skan// is irreflexive.
150169691Skan#define __glibcxx_check_strict_weak_ordering_pred(_First,_Last,_Pred)	\
151169691Skan_GLIBCXX_DEBUG_ASSERT(_First == _Last || !_Pred(*_First, *_First))
152169691Skan
153169691Skan
154169691Skan// Verify that the iterator range [_First, _Last) is sorted
155169691Skan#define __glibcxx_check_sorted(_First,_Last)				\
156169691Skan__glibcxx_check_valid_range(_First,_Last);				\
157169691Skan__glibcxx_check_strict_weak_ordering(_First,_Last);			\
158169691Skan_GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted(_First, _Last),	\
159169691Skan		      _M_message(__gnu_debug::__msg_unsorted)	        \
160169691Skan                      ._M_iterator(_First, #_First)			\
161169691Skan		      ._M_iterator(_Last, #_Last))
162169691Skan
163169691Skan/** Verify that the iterator range [_First, _Last) is sorted by the
164169691Skan    predicate _Pred. */
165169691Skan#define __glibcxx_check_sorted_pred(_First,_Last,_Pred)			\
166169691Skan__glibcxx_check_valid_range(_First,_Last);				\
167169691Skan__glibcxx_check_strict_weak_ordering_pred(_First,_Last,_Pred);	        \
168169691Skan_GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted(_First, _Last, _Pred), \
169169691Skan		      _M_message(__gnu_debug::__msg_unsorted_pred)      \
170169691Skan                      ._M_iterator(_First, #_First)			\
171169691Skan		      ._M_iterator(_Last, #_Last)			\
172169691Skan		      ._M_string(#_Pred))
173169691Skan
174169691Skan/** Verify that the iterator range [_First, _Last) is partitioned
175169691Skan    w.r.t. the value _Value. */
176169691Skan#define __glibcxx_check_partitioned(_First,_Last,_Value)		\
177169691Skan__glibcxx_check_valid_range(_First,_Last);				\
178169691Skan_GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned(_First, _Last,   \
179169691Skan							    _Value),	\
180169691Skan		      _M_message(__gnu_debug::__msg_unpartitioned)      \
181169691Skan		      ._M_iterator(_First, #_First)			\
182169691Skan		      ._M_iterator(_Last, #_Last)			\
183169691Skan		      ._M_string(#_Value))
184169691Skan
185169691Skan/** Verify that the iterator range [_First, _Last) is partitioned
186169691Skan    w.r.t. the value _Value and predicate _Pred. */
187169691Skan#define __glibcxx_check_partitioned_pred(_First,_Last,_Value,_Pred)	\
188169691Skan__glibcxx_check_valid_range(_First,_Last);				\
189169691Skan_GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned(_First, _Last,   \
190169691Skan							 _Value, _Pred), \
191169691Skan		      _M_message(__gnu_debug::__msg_unpartitioned_pred) \
192169691Skan		      ._M_iterator(_First, #_First)			\
193169691Skan		      ._M_iterator(_Last, #_Last)			\
194169691Skan		      ._M_string(#_Pred)				\
195169691Skan                      ._M_string(#_Value))
196169691Skan
197169691Skan// Verify that the iterator range [_First, _Last) is a heap
198169691Skan#define __glibcxx_check_heap(_First,_Last)				\
199169691Skan__glibcxx_check_valid_range(_First,_Last);				\
200169691Skan_GLIBCXX_DEBUG_VERIFY(std::__is_heap(_First, _Last),		        \
201169691Skan		      _M_message(__gnu_debug::__msg_not_heap)	        \
202169691Skan		      ._M_iterator(_First, #_First)			\
203169691Skan		      ._M_iterator(_Last, #_Last))
204169691Skan
205169691Skan/** Verify that the iterator range [_First, _Last) is a heap
206169691Skan    w.r.t. the predicate _Pred. */
207169691Skan#define __glibcxx_check_heap_pred(_First,_Last,_Pred)			\
208169691Skan__glibcxx_check_valid_range(_First,_Last);				\
209169691Skan_GLIBCXX_DEBUG_VERIFY(std::__is_heap(_First, _Last, _Pred),		\
210169691Skan		      _M_message(__gnu_debug::__msg_not_heap_pred)      \
211169691Skan                      ._M_iterator(_First, #_First)			\
212169691Skan		      ._M_iterator(_Last, #_Last)			\
213169691Skan		      ._M_string(#_Pred))
214169691Skan
215169691Skan#ifdef _GLIBCXX_DEBUG_PEDANTIC
216169691Skan#  define __glibcxx_check_string(_String) _GLIBCXX_DEBUG_ASSERT(_String != 0)
217169691Skan#  define __glibcxx_check_string_len(_String,_Len) \
218169691Skan       _GLIBCXX_DEBUG_ASSERT(_String != 0 || _Len == 0)
219169691Skan#else
220169691Skan#  define __glibcxx_check_string(_String)
221169691Skan#  define __glibcxx_check_string_len(_String,_Len)
222169691Skan#endif
223169691Skan
224169691Skan#endif
225