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
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(_Condition,_ErrorMessage)             \
00043   do                                    \
00044   {                                 \
00045     if (! (_Condition))                         \
00046       __gnu_debug::_Error_formatter::_M_at(__FILE__, __LINE__)          \
00047       ._ErrorMessage._M_error();                    \
00048   } while (false)
00049 
00050 // Verify that [_First, _Last) forms a valid iterator range.
00051 #define __glibcxx_check_valid_range(_First,_Last)           \
00052 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__valid_range(_First, _Last),    \
00053               _M_message(__gnu_debug::__msg_valid_range)    \
00054               ._M_iterator(_First, #_First)         \
00055               ._M_iterator(_Last, #_Last))
00056 
00057 /** Verify that we can insert into *this with the iterator _Position.
00058  *  Insertion into a container at a specific position requires that
00059  *  the iterator be nonsingular, either dereferenceable or past-the-end,
00060  *  and that it reference the sequence we are inserting into. Note that
00061  *  this macro is only valid when the container is a_Safe_sequence and
00062  *  the iterator is a _Safe_iterator.
00063 */
00064 #define __glibcxx_check_insert(_Position)               \
00065 _GLIBCXX_DEBUG_VERIFY(!_Position._M_singular(),             \
00066               _M_message(__gnu_debug::__msg_insert_singular) \
00067               ._M_sequence(*this, "this")           \
00068               ._M_iterator(_Position, #_Position));     \
00069 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this),           \
00070               _M_message(__gnu_debug::__msg_insert_different) \
00071               ._M_sequence(*this, "this")           \
00072               ._M_iterator(_Position, #_Position))
00073 
00074 /** Verify that we can insert into *this after the iterator _Position.
00075  *  Insertion into a container after a specific position requires that
00076  *  the iterator be nonsingular, either dereferenceable or before-begin,
00077  *  and that it reference the sequence we are inserting into. Note that
00078  *  this macro is only valid when the container is a_Safe_sequence and
00079  *  the iterator is a _Safe_iterator.
00080 */
00081 #define __glibcxx_check_insert_after(_Position)             \
00082 __glibcxx_check_insert(_Position);                  \
00083 _GLIBCXX_DEBUG_VERIFY(!_Position._M_is_end(),               \
00084               _M_message(__gnu_debug::__msg_insert_after_end)   \
00085               ._M_sequence(*this, "this")           \
00086               ._M_iterator(_Position, #_Position))
00087 
00088 /** Verify that we can insert the values in the iterator range
00089  *  [_First, _Last) into *this with the iterator _Position.  Insertion
00090  *  into a container at a specific position requires that the iterator
00091  *  be nonsingular (i.e., either dereferenceable or past-the-end),
00092  *  that it reference the sequence we are inserting into, and that the
00093  *  iterator range [_First, Last) is a valid (possibly empty)
00094  *  range. Note that this macro is only valid when the container is a
00095  *  _Safe_sequence and the iterator is a _Safe_iterator.
00096  *
00097  *  @todo We would like to be able to check for noninterference of
00098  *  _Position and the range [_First, _Last), but that can't (in
00099  *  general) be done.
00100 */
00101 #define __glibcxx_check_insert_range(_Position,_First,_Last)        \
00102 __glibcxx_check_valid_range(_First,_Last);              \
00103 __glibcxx_check_insert(_Position)
00104 
00105 /** Verify that we can insert the values in the iterator range
00106  *  [_First, _Last) into *this after the iterator _Position.  Insertion
00107  *  into a container after a specific position requires that the iterator
00108  *  be nonsingular (i.e., either dereferenceable or past-the-end),
00109  *  that it reference the sequence we are inserting into, and that the
00110  *  iterator range [_First, Last) is a valid (possibly empty)
00111  *  range. Note that this macro is only valid when the container is a
00112  *  _Safe_sequence and the iterator is a _Safe_iterator.
00113  *
00114  *  @todo We would like to be able to check for noninterference of
00115  *  _Position and the range [_First, _Last), but that can't (in
00116  *  general) be done.
00117 */
00118 #define __glibcxx_check_insert_range_after(_Position,_First,_Last)  \
00119 __glibcxx_check_valid_range(_First,_Last);              \
00120 __glibcxx_check_insert_after(_Position)
00121 
00122 /** Verify that we can erase the element referenced by the iterator
00123  * _Position. We can erase the element if the _Position iterator is
00124  * dereferenceable and references this sequence.
00125 */
00126 #define __glibcxx_check_erase(_Position)                \
00127 _GLIBCXX_DEBUG_VERIFY(_Position._M_dereferenceable(),           \
00128               _M_message(__gnu_debug::__msg_erase_bad)          \
00129                       ._M_sequence(*this, "this")           \
00130               ._M_iterator(_Position, #_Position));     \
00131 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this),           \
00132               _M_message(__gnu_debug::__msg_erase_different)    \
00133               ._M_sequence(*this, "this")           \
00134               ._M_iterator(_Position, #_Position))
00135 
00136 /** Verify that we can erase the element after the iterator
00137  * _Position. We can erase the element if the _Position iterator is
00138  * before a dereferenceable one and references this sequence.
00139 */
00140 #define __glibcxx_check_erase_after(_Position)              \
00141 _GLIBCXX_DEBUG_VERIFY(_Position._M_before_dereferenceable(),        \
00142               _M_message(__gnu_debug::__msg_erase_after_bad)    \
00143               ._M_sequence(*this, "this")           \
00144               ._M_iterator(_Position, #_Position));     \
00145 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this),           \
00146               _M_message(__gnu_debug::__msg_erase_different)    \
00147               ._M_sequence(*this, "this")           \
00148               ._M_iterator(_Position, #_Position))
00149 
00150 /** Verify that we can erase the elements in the iterator range
00151  *  [_First, _Last). We can erase the elements if [_First, _Last) is a
00152  *  valid iterator range within this sequence.
00153 */
00154 #define __glibcxx_check_erase_range(_First,_Last)           \
00155 __glibcxx_check_valid_range(_First,_Last);              \
00156 _GLIBCXX_DEBUG_VERIFY(_First._M_attached_to(this),          \
00157               _M_message(__gnu_debug::__msg_erase_different)    \
00158                       ._M_sequence(*this, "this")           \
00159               ._M_iterator(_First, #_First)         \
00160               ._M_iterator(_Last, #_Last))
00161 
00162 /** Verify that we can erase the elements in the iterator range
00163  *  (_First, _Last). We can erase the elements if (_First, _Last) is a
00164  *  valid iterator range within this sequence.
00165 */
00166 #define __glibcxx_check_erase_range_after(_First,_Last)         \
00167 _GLIBCXX_DEBUG_VERIFY(_First._M_can_compare(_Last),         \
00168               _M_message(__gnu_debug::__msg_erase_different)    \
00169               ._M_sequence(*this, "this")           \
00170               ._M_iterator(_First, #_First)         \
00171               ._M_iterator(_Last, #_Last));         \
00172 _GLIBCXX_DEBUG_VERIFY(_First._M_attached_to(this),          \
00173               _M_message(__gnu_debug::__msg_erase_different)    \
00174               ._M_sequence(*this, "this")           \
00175               ._M_iterator(_First, #_First));           \
00176 _GLIBCXX_DEBUG_VERIFY(_First != _Last,                  \
00177               _M_message(__gnu_debug::__msg_valid_range2)   \
00178               ._M_sequence(*this, "this")           \
00179               ._M_iterator(_First, #_First)         \
00180               ._M_iterator(_Last, #_Last));         \
00181 _GLIBCXX_DEBUG_VERIFY(_First._M_incrementable(),            \
00182               _M_message(__gnu_debug::__msg_valid_range2)   \
00183               ._M_sequence(*this, "this")           \
00184               ._M_iterator(_First, #_First)         \
00185               ._M_iterator(_Last, #_Last));         \
00186 _GLIBCXX_DEBUG_VERIFY(!_Last._M_is_before_begin(),          \
00187               _M_message(__gnu_debug::__msg_valid_range2)   \
00188               ._M_sequence(*this, "this")           \
00189               ._M_iterator(_First, #_First)         \
00190               ._M_iterator(_Last, #_Last))          \
00191 
00192 // Verify that the subscript _N is less than the container's size.
00193 #define __glibcxx_check_subscript(_N)                   \
00194 _GLIBCXX_DEBUG_VERIFY(_N < this->size(),                \
00195               _M_message(__gnu_debug::__msg_subscript_oob)      \
00196                       ._M_sequence(*this, "this")           \
00197               ._M_integer(_N, #_N)              \
00198               ._M_integer(this->size(), "size"))
00199 
00200 // Verify that the container is nonempty
00201 #define __glibcxx_check_nonempty()                  \
00202 _GLIBCXX_DEBUG_VERIFY(! this->empty(),                  \
00203               _M_message(__gnu_debug::__msg_empty)          \
00204                       ._M_sequence(*this, "this"))
00205 
00206 // Verify that the iterator range [_First, _Last) is sorted
00207 #define __glibcxx_check_sorted(_First,_Last)                \
00208 __glibcxx_check_valid_range(_First,_Last);              \
00209 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted(_First, _Last),   \
00210               _M_message(__gnu_debug::__msg_unsorted)           \
00211                       ._M_iterator(_First, #_First)         \
00212               ._M_iterator(_Last, #_Last))
00213 
00214 /** Verify that the iterator range [_First, _Last) is sorted by the
00215     predicate _Pred. */
00216 #define __glibcxx_check_sorted_pred(_First,_Last,_Pred)         \
00217 __glibcxx_check_valid_range(_First,_Last);              \
00218 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted(_First, _Last, _Pred), \
00219               _M_message(__gnu_debug::__msg_unsorted_pred)      \
00220                       ._M_iterator(_First, #_First)         \
00221               ._M_iterator(_Last, #_Last)           \
00222               ._M_string(#_Pred))
00223 
00224 // Special variant for std::merge, std::includes, std::set_*
00225 #define __glibcxx_check_sorted_set(_First1,_Last1,_First2)      \
00226 __glibcxx_check_valid_range(_First1,_Last1);                \
00227 _GLIBCXX_DEBUG_VERIFY(                                                  \
00228   __gnu_debug::__check_sorted_set(_First1, _Last1, _First2),        \
00229   _M_message(__gnu_debug::__msg_unsorted)               \
00230   ._M_iterator(_First1, #_First1)                   \
00231   ._M_iterator(_Last1, #_Last1))
00232 
00233 // Likewise with a _Pred.
00234 #define __glibcxx_check_sorted_set_pred(_First1,_Last1,_First2,_Pred)   \
00235 __glibcxx_check_valid_range(_First1,_Last1);                    \
00236 _GLIBCXX_DEBUG_VERIFY(                          \
00237   __gnu_debug::__check_sorted_set(_First1, _Last1, _First2, _Pred), \
00238   _M_message(__gnu_debug::__msg_unsorted_pred)              \
00239   ._M_iterator(_First1, #_First1)                   \
00240   ._M_iterator(_Last1, #_Last1)                     \
00241   ._M_string(#_Pred))
00242 
00243 /** Verify that the iterator range [_First, _Last) is partitioned
00244     w.r.t. the value _Value. */
00245 #define __glibcxx_check_partitioned_lower(_First,_Last,_Value)      \
00246 __glibcxx_check_valid_range(_First,_Last);              \
00247 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_lower(_First, _Last, \
00248                                 _Value),    \
00249               _M_message(__gnu_debug::__msg_unpartitioned)      \
00250               ._M_iterator(_First, #_First)         \
00251               ._M_iterator(_Last, #_Last)           \
00252               ._M_string(#_Value))
00253 
00254 #define __glibcxx_check_partitioned_upper(_First,_Last,_Value)      \
00255 __glibcxx_check_valid_range(_First,_Last);              \
00256 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_upper(_First, _Last, \
00257                                 _Value),    \
00258               _M_message(__gnu_debug::__msg_unpartitioned)      \
00259               ._M_iterator(_First, #_First)         \
00260               ._M_iterator(_Last, #_Last)           \
00261               ._M_string(#_Value))
00262 
00263 /** Verify that the iterator range [_First, _Last) is partitioned
00264     w.r.t. the value _Value and predicate _Pred. */
00265 #define __glibcxx_check_partitioned_lower_pred(_First,_Last,_Value,_Pred) \
00266 __glibcxx_check_valid_range(_First,_Last);              \
00267 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_lower(_First, _Last, \
00268                              _Value, _Pred), \
00269               _M_message(__gnu_debug::__msg_unpartitioned_pred) \
00270               ._M_iterator(_First, #_First)         \
00271               ._M_iterator(_Last, #_Last)           \
00272               ._M_string(#_Pred)                \
00273                       ._M_string(#_Value))
00274 
00275 /** Verify that the iterator range [_First, _Last) is partitioned
00276     w.r.t. the value _Value and predicate _Pred. */
00277 #define __glibcxx_check_partitioned_upper_pred(_First,_Last,_Value,_Pred) \
00278 __glibcxx_check_valid_range(_First,_Last);              \
00279 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_upper(_First, _Last, \
00280                              _Value, _Pred), \
00281               _M_message(__gnu_debug::__msg_unpartitioned_pred) \
00282               ._M_iterator(_First, #_First)         \
00283               ._M_iterator(_Last, #_Last)           \
00284               ._M_string(#_Pred)                \
00285                       ._M_string(#_Value))
00286 
00287 // Verify that the iterator range [_First, _Last) is a heap
00288 #define __glibcxx_check_heap(_First,_Last)              \
00289 _GLIBCXX_DEBUG_VERIFY(std::__is_heap(_First, _Last),                \
00290               _M_message(__gnu_debug::__msg_not_heap)           \
00291               ._M_iterator(_First, #_First)         \
00292               ._M_iterator(_Last, #_Last))
00293 
00294 /** Verify that the iterator range [_First, _Last) is a heap
00295     w.r.t. the predicate _Pred. */
00296 #define __glibcxx_check_heap_pred(_First,_Last,_Pred)           \
00297 _GLIBCXX_DEBUG_VERIFY(std::__is_heap(_First, _Last, _Pred),     \
00298               _M_message(__gnu_debug::__msg_not_heap_pred)      \
00299                       ._M_iterator(_First, #_First)         \
00300               ._M_iterator(_Last, #_Last)           \
00301               ._M_string(#_Pred))
00302 
00303 #ifdef _GLIBCXX_DEBUG_PEDANTIC
00304 #  define __glibcxx_check_string(_String) _GLIBCXX_DEBUG_ASSERT(_String != 0)
00305 #  define __glibcxx_check_string_len(_String,_Len) \
00306        _GLIBCXX_DEBUG_ASSERT(_String != 0 || _Len == 0)
00307 #else
00308 #  define __glibcxx_check_string(_String)
00309 #  define __glibcxx_check_string_len(_String,_Len)
00310 #endif
00311 
00312 #endif