libstdc++
macros.h
Go to the documentation of this file.
00001 // Debugging support implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 3, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // Under Section 7 of GPL version 3, you are granted additional
00018 // permissions described in the GCC Runtime Library Exception, version
00019 // 3.1, as published by the Free Software Foundation.
00020 
00021 // You should have received a copy of the GNU General Public License and
00022 // a copy of the GCC Runtime Library Exception along with this program;
00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00024 // <http://www.gnu.org/licenses/>.
00025 
00026 /** @file debug/macros.h
00027  *  This file is a GNU debug extension to the Standard C++ Library.
00028  */
00029 
00030 #ifndef _GLIBCXX_DEBUG_MACROS_H
00031 #define _GLIBCXX_DEBUG_MACROS_H 1
00032 
00033 /**
00034  * Macros used by the implementation to verify certain
00035  * properties. These macros may only be used directly by the debug
00036  * wrappers. Note that these are macros (instead of the more obviously
00037  * @a correct choice of making them functions) because we need line and
00038  * file information at the call site, to minimize the distance between
00039  * the user error and where the error is reported.
00040  *
00041  */
00042 #define _GLIBCXX_DEBUG_VERIFY_AT(_Condition,_ErrorMessage,_File,_Line)  \
00043   do                                    \
00044   {                                 \
00045     if (! (_Condition))                         \
00046       __gnu_debug::_Error_formatter::_M_at(_File, _Line)        \
00047       ._ErrorMessage._M_error();                    \
00048   } while (false)
00049 
00050 #define _GLIBCXX_DEBUG_VERIFY(_Condition,_ErrorMessage)         \
00051   _GLIBCXX_DEBUG_VERIFY_AT(_Condition,_ErrorMessage,__FILE__,__LINE__)
00052 
00053 // Verify that [_First, _Last) forms a valid iterator range.
00054 #define __glibcxx_check_valid_range(_First,_Last)           \
00055 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__valid_range(_First, _Last),    \
00056               _M_message(__gnu_debug::__msg_valid_range)    \
00057               ._M_iterator(_First, #_First)         \
00058               ._M_iterator(_Last, #_Last))
00059 
00060 // Verify that [_First, _Last) forms a non-empty iterator range.
00061 #define __glibcxx_check_non_empty_range(_First,_Last)           \
00062 _GLIBCXX_DEBUG_VERIFY(_First != _Last,                  \
00063               _M_message(__gnu_debug::__msg_non_empty_range)    \
00064               ._M_iterator(_First, #_First)         \
00065               ._M_iterator(_Last, #_Last))
00066 
00067 /** Verify that we can insert into *this with the iterator _Position.
00068  *  Insertion into a container at a specific position requires that
00069  *  the iterator be nonsingular, either dereferenceable or past-the-end,
00070  *  and that it reference the sequence we are inserting into. Note that
00071  *  this macro is only valid when the container is a_Safe_sequence and
00072  *  the iterator is a _Safe_iterator.
00073 */
00074 #define __glibcxx_check_insert(_Position)               \
00075 _GLIBCXX_DEBUG_VERIFY(!_Position._M_singular(),             \
00076               _M_message(__gnu_debug::__msg_insert_singular) \
00077               ._M_sequence(*this, "this")           \
00078               ._M_iterator(_Position, #_Position));     \
00079 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this),           \
00080               _M_message(__gnu_debug::__msg_insert_different) \
00081               ._M_sequence(*this, "this")           \
00082               ._M_iterator(_Position, #_Position))
00083 
00084 /** Verify that we can insert into *this after the iterator _Position.
00085  *  Insertion into a container after a specific position requires that
00086  *  the iterator be nonsingular, either dereferenceable or before-begin,
00087  *  and that it reference the sequence we are inserting into. Note that
00088  *  this macro is only valid when the container is a_Safe_sequence and
00089  *  the iterator is a _Safe_iterator.
00090 */
00091 #define __glibcxx_check_insert_after(_Position)             \
00092 __glibcxx_check_insert(_Position);                  \
00093 _GLIBCXX_DEBUG_VERIFY(!_Position._M_is_end(),               \
00094               _M_message(__gnu_debug::__msg_insert_after_end)   \
00095               ._M_sequence(*this, "this")           \
00096               ._M_iterator(_Position, #_Position))
00097 
00098 /** Verify that we can insert the values in the iterator range
00099  *  [_First, _Last) into *this with the iterator _Position.  Insertion
00100  *  into a container at a specific position requires that the iterator
00101  *  be nonsingular (i.e., either dereferenceable or past-the-end),
00102  *  that it reference the sequence we are inserting into, and that the
00103  *  iterator range [_First, Last) is a valid (possibly empty)
00104  *  range. Note that this macro is only valid when the container is a
00105  *  _Safe_sequence and the iterator is a _Safe_iterator.
00106  *
00107  *  @todo We would like to be able to check for noninterference of
00108  *  _Position and the range [_First, _Last), but that can't (in
00109  *  general) be done.
00110 */
00111 #define __glibcxx_check_insert_range(_Position,_First,_Last)        \
00112 __glibcxx_check_valid_range(_First,_Last);              \
00113 __glibcxx_check_insert(_Position)
00114 
00115 /** Verify that we can insert the values in the iterator range
00116  *  [_First, _Last) into *this after the iterator _Position.  Insertion
00117  *  into a container after a specific position requires that the iterator
00118  *  be nonsingular (i.e., either dereferenceable or past-the-end),
00119  *  that it reference the sequence we are inserting into, and that the
00120  *  iterator range [_First, Last) is a valid (possibly empty)
00121  *  range. Note that this macro is only valid when the container is a
00122  *  _Safe_sequence and the iterator is a _Safe_iterator.
00123  *
00124  *  @todo We would like to be able to check for noninterference of
00125  *  _Position and the range [_First, _Last), but that can't (in
00126  *  general) be done.
00127 */
00128 #define __glibcxx_check_insert_range_after(_Position,_First,_Last)  \
00129 __glibcxx_check_valid_range(_First,_Last);              \
00130 __glibcxx_check_insert_after(_Position)
00131 
00132 /** Verify that we can erase the element referenced by the iterator
00133  * _Position. We can erase the element if the _Position iterator is
00134  * dereferenceable and references this sequence.
00135 */
00136 #define __glibcxx_check_erase(_Position)                \
00137 _GLIBCXX_DEBUG_VERIFY(_Position._M_dereferenceable(),           \
00138               _M_message(__gnu_debug::__msg_erase_bad)          \
00139                       ._M_sequence(*this, "this")           \
00140               ._M_iterator(_Position, #_Position));     \
00141 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this),           \
00142               _M_message(__gnu_debug::__msg_erase_different)    \
00143               ._M_sequence(*this, "this")           \
00144               ._M_iterator(_Position, #_Position))
00145 
00146 /** Verify that we can erase the element after the iterator
00147  * _Position. We can erase the element if the _Position iterator is
00148  * before a dereferenceable one and references this sequence.
00149 */
00150 #define __glibcxx_check_erase_after(_Position)              \
00151 _GLIBCXX_DEBUG_VERIFY(_Position._M_before_dereferenceable(),        \
00152               _M_message(__gnu_debug::__msg_erase_after_bad)    \
00153               ._M_sequence(*this, "this")           \
00154               ._M_iterator(_Position, #_Position));     \
00155 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this),           \
00156               _M_message(__gnu_debug::__msg_erase_different)    \
00157               ._M_sequence(*this, "this")           \
00158               ._M_iterator(_Position, #_Position))
00159 
00160 /** Verify that we can erase the elements in the iterator range
00161  *  [_First, _Last). We can erase the elements if [_First, _Last) is a
00162  *  valid iterator range within this sequence.
00163 */
00164 #define __glibcxx_check_erase_range(_First,_Last)           \
00165 __glibcxx_check_valid_range(_First,_Last);              \
00166 _GLIBCXX_DEBUG_VERIFY(_First._M_attached_to(this),          \
00167               _M_message(__gnu_debug::__msg_erase_different)    \
00168                       ._M_sequence(*this, "this")           \
00169               ._M_iterator(_First, #_First)         \
00170               ._M_iterator(_Last, #_Last))
00171 
00172 /** Verify that we can erase the elements in the iterator range
00173  *  (_First, _Last). We can erase the elements if (_First, _Last) is a
00174  *  valid iterator range within this sequence.
00175 */
00176 #define __glibcxx_check_erase_range_after(_First,_Last)         \
00177 _GLIBCXX_DEBUG_VERIFY(_First._M_can_compare(_Last),         \
00178               _M_message(__gnu_debug::__msg_erase_different)    \
00179               ._M_sequence(*this, "this")           \
00180               ._M_iterator(_First, #_First)         \
00181               ._M_iterator(_Last, #_Last));         \
00182 _GLIBCXX_DEBUG_VERIFY(_First._M_attached_to(this),          \
00183               _M_message(__gnu_debug::__msg_erase_different)    \
00184               ._M_sequence(*this, "this")           \
00185               ._M_iterator(_First, #_First));           \
00186 _GLIBCXX_DEBUG_VERIFY(_First != _Last,                  \
00187               _M_message(__gnu_debug::__msg_valid_range2)   \
00188               ._M_sequence(*this, "this")           \
00189               ._M_iterator(_First, #_First)         \
00190               ._M_iterator(_Last, #_Last));         \
00191 _GLIBCXX_DEBUG_VERIFY(_First._M_incrementable(),            \
00192               _M_message(__gnu_debug::__msg_valid_range2)   \
00193               ._M_sequence(*this, "this")           \
00194               ._M_iterator(_First, #_First)         \
00195               ._M_iterator(_Last, #_Last));         \
00196 _GLIBCXX_DEBUG_VERIFY(!_Last._M_is_before_begin(),          \
00197               _M_message(__gnu_debug::__msg_valid_range2)   \
00198               ._M_sequence(*this, "this")           \
00199               ._M_iterator(_First, #_First)         \
00200               ._M_iterator(_Last, #_Last))          \
00201 
00202 // Verify that the subscript _N is less than the container's size.
00203 #define __glibcxx_check_subscript(_N)                   \
00204 _GLIBCXX_DEBUG_VERIFY(_N < this->size(),                \
00205               _M_message(__gnu_debug::__msg_subscript_oob)      \
00206                       ._M_sequence(*this, "this")           \
00207               ._M_integer(_N, #_N)              \
00208               ._M_integer(this->size(), "size"))
00209 
00210 // Verify that the container is nonempty
00211 #define __glibcxx_check_nonempty()                  \
00212 _GLIBCXX_DEBUG_VERIFY(! this->empty(),                  \
00213               _M_message(__gnu_debug::__msg_empty)          \
00214                       ._M_sequence(*this, "this"))
00215 
00216 // Verify that the iterator range [_First, _Last) is sorted
00217 #define __glibcxx_check_sorted(_First,_Last)                \
00218 __glibcxx_check_valid_range(_First,_Last);              \
00219 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted(_First, _Last),   \
00220               _M_message(__gnu_debug::__msg_unsorted)           \
00221                       ._M_iterator(_First, #_First)         \
00222               ._M_iterator(_Last, #_Last))
00223 
00224 /** Verify that the iterator range [_First, _Last) is sorted by the
00225     predicate _Pred. */
00226 #define __glibcxx_check_sorted_pred(_First,_Last,_Pred)         \
00227 __glibcxx_check_valid_range(_First,_Last);              \
00228 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted(_First, _Last, _Pred), \
00229               _M_message(__gnu_debug::__msg_unsorted_pred)      \
00230                       ._M_iterator(_First, #_First)         \
00231               ._M_iterator(_Last, #_Last)           \
00232               ._M_string(#_Pred))
00233 
00234 // Special variant for std::merge, std::includes, std::set_*
00235 #define __glibcxx_check_sorted_set(_First1,_Last1,_First2)      \
00236 __glibcxx_check_valid_range(_First1,_Last1);                \
00237 _GLIBCXX_DEBUG_VERIFY(                                                  \
00238   __gnu_debug::__check_sorted_set(_First1, _Last1, _First2),        \
00239   _M_message(__gnu_debug::__msg_unsorted)               \
00240   ._M_iterator(_First1, #_First1)                   \
00241   ._M_iterator(_Last1, #_Last1))
00242 
00243 // Likewise with a _Pred.
00244 #define __glibcxx_check_sorted_set_pred(_First1,_Last1,_First2,_Pred)   \
00245 __glibcxx_check_valid_range(_First1,_Last1);                    \
00246 _GLIBCXX_DEBUG_VERIFY(                          \
00247   __gnu_debug::__check_sorted_set(_First1, _Last1, _First2, _Pred), \
00248   _M_message(__gnu_debug::__msg_unsorted_pred)              \
00249   ._M_iterator(_First1, #_First1)                   \
00250   ._M_iterator(_Last1, #_Last1)                     \
00251   ._M_string(#_Pred))
00252 
00253 /** Verify that the iterator range [_First, _Last) is partitioned
00254     w.r.t. the value _Value. */
00255 #define __glibcxx_check_partitioned_lower(_First,_Last,_Value)      \
00256 __glibcxx_check_valid_range(_First,_Last);              \
00257 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_lower(_First, _Last, \
00258                                 _Value),    \
00259               _M_message(__gnu_debug::__msg_unpartitioned)      \
00260               ._M_iterator(_First, #_First)         \
00261               ._M_iterator(_Last, #_Last)           \
00262               ._M_string(#_Value))
00263 
00264 #define __glibcxx_check_partitioned_upper(_First,_Last,_Value)      \
00265 __glibcxx_check_valid_range(_First,_Last);              \
00266 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_upper(_First, _Last, \
00267                                 _Value),    \
00268               _M_message(__gnu_debug::__msg_unpartitioned)      \
00269               ._M_iterator(_First, #_First)         \
00270               ._M_iterator(_Last, #_Last)           \
00271               ._M_string(#_Value))
00272 
00273 /** Verify that the iterator range [_First, _Last) is partitioned
00274     w.r.t. the value _Value and predicate _Pred. */
00275 #define __glibcxx_check_partitioned_lower_pred(_First,_Last,_Value,_Pred) \
00276 __glibcxx_check_valid_range(_First,_Last);              \
00277 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_lower(_First, _Last, \
00278                              _Value, _Pred), \
00279               _M_message(__gnu_debug::__msg_unpartitioned_pred) \
00280               ._M_iterator(_First, #_First)         \
00281               ._M_iterator(_Last, #_Last)           \
00282               ._M_string(#_Pred)                \
00283                       ._M_string(#_Value))
00284 
00285 /** Verify that the iterator range [_First, _Last) is partitioned
00286     w.r.t. the value _Value and predicate _Pred. */
00287 #define __glibcxx_check_partitioned_upper_pred(_First,_Last,_Value,_Pred) \
00288 __glibcxx_check_valid_range(_First,_Last);              \
00289 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_upper(_First, _Last, \
00290                              _Value, _Pred), \
00291               _M_message(__gnu_debug::__msg_unpartitioned_pred) \
00292               ._M_iterator(_First, #_First)         \
00293               ._M_iterator(_Last, #_Last)           \
00294               ._M_string(#_Pred)                \
00295                       ._M_string(#_Value))
00296 
00297 // Verify that the iterator range [_First, _Last) is a heap
00298 #define __glibcxx_check_heap(_First,_Last)              \
00299 _GLIBCXX_DEBUG_VERIFY(std::__is_heap(_First, _Last),                \
00300               _M_message(__gnu_debug::__msg_not_heap)           \
00301               ._M_iterator(_First, #_First)         \
00302               ._M_iterator(_Last, #_Last))
00303 
00304 /** Verify that the iterator range [_First, _Last) is a heap
00305     w.r.t. the predicate _Pred. */
00306 #define __glibcxx_check_heap_pred(_First,_Last,_Pred)           \
00307 _GLIBCXX_DEBUG_VERIFY(std::__is_heap(_First, _Last, _Pred),     \
00308               _M_message(__gnu_debug::__msg_not_heap_pred)      \
00309                       ._M_iterator(_First, #_First)         \
00310               ._M_iterator(_Last, #_Last)           \
00311               ._M_string(#_Pred))
00312 
00313 #ifdef _GLIBCXX_DEBUG_PEDANTIC
00314 #  define __glibcxx_check_string(_String) _GLIBCXX_DEBUG_ASSERT(_String != 0)
00315 #  define __glibcxx_check_string_len(_String,_Len) \
00316        _GLIBCXX_DEBUG_ASSERT(_String != 0 || _Len == 0)
00317 #else
00318 #  define __glibcxx_check_string(_String)
00319 #  define __glibcxx_check_string_len(_String,_Len)
00320 #endif
00321 
00322 #endif