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