libstdc++
macros.h
Go to the documentation of this file.
00001 // Debugging support implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2003-2015 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 which does not reference the sequence we are inserting into.
00104  *  Note that this macro is only valid when the container is a
00105  *  _Safe_sequence and the _Position iterator is a _Safe_iterator.
00106 */
00107 #define __glibcxx_check_insert_range(_Position,_First,_Last)            \
00108 __glibcxx_check_valid_range(_First,_Last);                              \
00109 __glibcxx_check_insert(_Position);                                      \
00110 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__foreign_iterator(_Position,_First,_Last),\
00111                       _M_message(__gnu_debug::__msg_insert_range_from_self)\
00112                       ._M_iterator(_First, #_First)                     \
00113                       ._M_iterator(_Last, #_Last)                       \
00114                       ._M_sequence(*this, "this"))
00115 
00116 /** Verify that we can insert the values in the iterator range
00117  *  [_First, _Last) into *this after the iterator _Position.  Insertion
00118  *  into a container after a specific position requires that the iterator
00119  *  be nonsingular (i.e., either dereferenceable or past-the-end),
00120  *  that it reference the sequence we are inserting into, and that the
00121  *  iterator range [_First, _Last) is a valid (possibly empty)
00122  *  range which does not reference the sequence we are inserting into.
00123  *  Note that this macro is only valid when the container is a
00124  *  _Safe_sequence and the _Position iterator is a _Safe_iterator.
00125 */
00126 #define __glibcxx_check_insert_range_after(_Position,_First,_Last)      \
00127 __glibcxx_check_valid_range(_First,_Last);                              \
00128 __glibcxx_check_insert_after(_Position);                                \
00129 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__foreign_iterator(_Position,_First,_Last),\
00130                       _M_message(__gnu_debug::__msg_insert_range_from_self)\
00131                       ._M_iterator(_First, #_First)                     \
00132                       ._M_iterator(_Last, #_Last)                       \
00133                       ._M_sequence(*this, "this"))
00134 
00135 /** Verify that we can erase the element referenced by the iterator
00136  * _Position. We can erase the element if the _Position iterator is
00137  * dereferenceable and references this sequence.
00138 */
00139 #define __glibcxx_check_erase(_Position)                                \
00140 _GLIBCXX_DEBUG_VERIFY(_Position._M_dereferenceable(),                   \
00141                       _M_message(__gnu_debug::__msg_erase_bad)          \
00142                       ._M_sequence(*this, "this")                       \
00143                       ._M_iterator(_Position, #_Position));             \
00144 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this),                   \
00145                       _M_message(__gnu_debug::__msg_erase_different)    \
00146                       ._M_sequence(*this, "this")                       \
00147                       ._M_iterator(_Position, #_Position))
00148 
00149 /** Verify that we can erase the element after the iterator
00150  * _Position. We can erase the element if the _Position iterator is
00151  * before a dereferenceable one and references this sequence.
00152 */
00153 #define __glibcxx_check_erase_after(_Position)                          \
00154 _GLIBCXX_DEBUG_VERIFY(_Position._M_before_dereferenceable(),            \
00155                       _M_message(__gnu_debug::__msg_erase_after_bad)    \
00156                       ._M_sequence(*this, "this")                       \
00157                       ._M_iterator(_Position, #_Position));             \
00158 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this),                   \
00159                       _M_message(__gnu_debug::__msg_erase_different)    \
00160                       ._M_sequence(*this, "this")                       \
00161                       ._M_iterator(_Position, #_Position))
00162 
00163 /** Verify that we can erase the elements in the iterator range
00164  *  [_First, _Last). We can erase the elements if [_First, _Last) is a
00165  *  valid iterator range within this sequence.
00166 */
00167 #define __glibcxx_check_erase_range(_First,_Last)                       \
00168 __glibcxx_check_valid_range(_First,_Last);                              \
00169 _GLIBCXX_DEBUG_VERIFY(_First._M_attached_to(this),                      \
00170                       _M_message(__gnu_debug::__msg_erase_different)    \
00171                       ._M_sequence(*this, "this")                       \
00172                       ._M_iterator(_First, #_First)                     \
00173                       ._M_iterator(_Last, #_Last))
00174 
00175 /** Verify that we can erase the elements in the iterator range
00176  *  (_First, _Last). We can erase the elements if (_First, _Last) is a
00177  *  valid iterator range within this sequence.
00178 */
00179 #define __glibcxx_check_erase_range_after(_First,_Last)                 \
00180 _GLIBCXX_DEBUG_VERIFY(_First._M_can_compare(_Last),                     \
00181                       _M_message(__gnu_debug::__msg_erase_different)    \
00182                       ._M_sequence(*this, "this")                       \
00183                       ._M_iterator(_First, #_First)                     \
00184                       ._M_iterator(_Last, #_Last));                     \
00185 _GLIBCXX_DEBUG_VERIFY(_First._M_attached_to(this),                      \
00186                       _M_message(__gnu_debug::__msg_erase_different)    \
00187                       ._M_sequence(*this, "this")                       \
00188                       ._M_iterator(_First, #_First));                   \
00189 _GLIBCXX_DEBUG_VERIFY(_First != _Last,                                  \
00190                       _M_message(__gnu_debug::__msg_valid_range2)       \
00191                       ._M_sequence(*this, "this")                       \
00192                       ._M_iterator(_First, #_First)                     \
00193                       ._M_iterator(_Last, #_Last));                     \
00194 _GLIBCXX_DEBUG_VERIFY(_First._M_incrementable(),                        \
00195                       _M_message(__gnu_debug::__msg_valid_range2)       \
00196                       ._M_sequence(*this, "this")                       \
00197                       ._M_iterator(_First, #_First)                     \
00198                       ._M_iterator(_Last, #_Last));                     \
00199 _GLIBCXX_DEBUG_VERIFY(!_Last._M_is_before_begin(),                      \
00200                       _M_message(__gnu_debug::__msg_valid_range2)       \
00201                       ._M_sequence(*this, "this")                       \
00202                       ._M_iterator(_First, #_First)                     \
00203                       ._M_iterator(_Last, #_Last))                      \
00204 
00205 // Verify that the subscript _N is less than the container's size.
00206 #define __glibcxx_check_subscript(_N)                                   \
00207 _GLIBCXX_DEBUG_VERIFY(_N < this->size(),                                \
00208                       _M_message(__gnu_debug::__msg_subscript_oob)      \
00209                       ._M_sequence(*this, "this")                       \
00210                       ._M_integer(_N, #_N)                              \
00211                       ._M_integer(this->size(), "size"))
00212 
00213 // Verify that the bucket _N is less than the container's buckets count.
00214 #define __glibcxx_check_bucket_index(_N)                                \
00215 _GLIBCXX_DEBUG_VERIFY(_N < this->bucket_count(),                        \
00216                       _M_message(__gnu_debug::__msg_bucket_index_oob)   \
00217                       ._M_sequence(*this, "this")                       \
00218                       ._M_integer(_N, #_N)                              \
00219                       ._M_integer(this->bucket_count(), "size"))
00220 
00221 // Verify that the container is nonempty
00222 #define __glibcxx_check_nonempty()                                      \
00223 _GLIBCXX_DEBUG_VERIFY(! this->empty(),                                  \
00224                       _M_message(__gnu_debug::__msg_empty)              \
00225                       ._M_sequence(*this, "this"))
00226 
00227 // Verify that the iterator range [_First, _Last) is sorted
00228 #define __glibcxx_check_sorted(_First,_Last)                            \
00229 __glibcxx_check_valid_range(_First,_Last);                              \
00230  _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted(                     \
00231                         __gnu_debug::__base(_First),                    \
00232                         __gnu_debug::__base(_Last)),                    \
00233                       _M_message(__gnu_debug::__msg_unsorted)           \
00234                       ._M_iterator(_First, #_First)                     \
00235                       ._M_iterator(_Last, #_Last))
00236 
00237 /** Verify that the iterator range [_First, _Last) is sorted by the
00238     predicate _Pred. */
00239 #define __glibcxx_check_sorted_pred(_First,_Last,_Pred)                 \
00240 __glibcxx_check_valid_range(_First,_Last);                              \
00241 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted(                      \
00242                         __gnu_debug::__base(_First),                    \
00243                         __gnu_debug::__base(_Last), _Pred),             \
00244                       _M_message(__gnu_debug::__msg_unsorted_pred)      \
00245                       ._M_iterator(_First, #_First)                     \
00246                       ._M_iterator(_Last, #_Last)                       \
00247                       ._M_string(#_Pred))
00248 
00249 // Special variant for std::merge, std::includes, std::set_*
00250 #define __glibcxx_check_sorted_set(_First1,_Last1,_First2)              \
00251 __glibcxx_check_valid_range(_First1,_Last1);                            \
00252 _GLIBCXX_DEBUG_VERIFY(                                                  \
00253   __gnu_debug::__check_sorted_set(__gnu_debug::__base(_First1),         \
00254                                   __gnu_debug::__base(_Last1), _First2),\
00255   _M_message(__gnu_debug::__msg_unsorted)                               \
00256   ._M_iterator(_First1, #_First1)                                       \
00257   ._M_iterator(_Last1, #_Last1))
00258 
00259 // Likewise with a _Pred.
00260 #define __glibcxx_check_sorted_set_pred(_First1,_Last1,_First2,_Pred)   \
00261 __glibcxx_check_valid_range(_First1,_Last1);                            \
00262 _GLIBCXX_DEBUG_VERIFY(                                                  \
00263   __gnu_debug::__check_sorted_set(__gnu_debug::__base(_First1),         \
00264                                   __gnu_debug::__base(_Last1),          \
00265                                   _First2, _Pred),                      \
00266   _M_message(__gnu_debug::__msg_unsorted_pred)                          \
00267   ._M_iterator(_First1, #_First1)                                       \
00268   ._M_iterator(_Last1, #_Last1)                                         \
00269   ._M_string(#_Pred))
00270 
00271 /** Verify that the iterator range [_First, _Last) is partitioned
00272     w.r.t. the value _Value. */
00273 #define __glibcxx_check_partitioned_lower(_First,_Last,_Value)          \
00274 __glibcxx_check_valid_range(_First,_Last);                              \
00275 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_lower(           \
00276                         __gnu_debug::__base(_First),                    \
00277                         __gnu_debug::__base(_Last), _Value),            \
00278                       _M_message(__gnu_debug::__msg_unpartitioned)      \
00279                       ._M_iterator(_First, #_First)                     \
00280                       ._M_iterator(_Last, #_Last)                       \
00281                       ._M_string(#_Value))
00282 
00283 #define __glibcxx_check_partitioned_upper(_First,_Last,_Value)          \
00284 __glibcxx_check_valid_range(_First,_Last);                              \
00285 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_upper(           \
00286                         __gnu_debug::__base(_First),                    \
00287                         __gnu_debug::__base(_Last), _Value),            \
00288                       _M_message(__gnu_debug::__msg_unpartitioned)      \
00289                       ._M_iterator(_First, #_First)                     \
00290                       ._M_iterator(_Last, #_Last)                       \
00291                       ._M_string(#_Value))
00292 
00293 /** Verify that the iterator range [_First, _Last) is partitioned
00294     w.r.t. the value _Value and predicate _Pred. */
00295 #define __glibcxx_check_partitioned_lower_pred(_First,_Last,_Value,_Pred) \
00296 __glibcxx_check_valid_range(_First,_Last);                              \
00297 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_lower(           \
00298                         __gnu_debug::__base(_First),                    \
00299                         __gnu_debug::__base(_Last), _Value, _Pred),     \
00300                       _M_message(__gnu_debug::__msg_unpartitioned_pred) \
00301                       ._M_iterator(_First, #_First)                     \
00302                       ._M_iterator(_Last, #_Last)                       \
00303                       ._M_string(#_Pred)                                \
00304                       ._M_string(#_Value))
00305 
00306 /** Verify that the iterator range [_First, _Last) is partitioned
00307     w.r.t. the value _Value and predicate _Pred. */
00308 #define __glibcxx_check_partitioned_upper_pred(_First,_Last,_Value,_Pred) \
00309 __glibcxx_check_valid_range(_First,_Last);                              \
00310 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_upper(           \
00311                         __gnu_debug::__base(_First),                    \
00312                         __gnu_debug::__base(_Last), _Value, _Pred),     \
00313                       _M_message(__gnu_debug::__msg_unpartitioned_pred) \
00314                       ._M_iterator(_First, #_First)                     \
00315                       ._M_iterator(_Last, #_Last)                       \
00316                       ._M_string(#_Pred)                                \
00317                       ._M_string(#_Value))
00318 
00319 // Verify that the iterator range [_First, _Last) is a heap
00320 #define __glibcxx_check_heap(_First,_Last)                              \
00321   _GLIBCXX_DEBUG_VERIFY(std::__is_heap(__gnu_debug::__base(_First),     \
00322                                        __gnu_debug::__base(_Last)),     \
00323                       _M_message(__gnu_debug::__msg_not_heap)           \
00324                       ._M_iterator(_First, #_First)                     \
00325                       ._M_iterator(_Last, #_Last))
00326 
00327 /** Verify that the iterator range [_First, _Last) is a heap
00328     w.r.t. the predicate _Pred. */
00329 #define __glibcxx_check_heap_pred(_First,_Last,_Pred)                   \
00330   _GLIBCXX_DEBUG_VERIFY(std::__is_heap(__gnu_debug::__base(_First),     \
00331                                        __gnu_debug::__base(_Last),      \
00332                                        _Pred),                          \
00333                       _M_message(__gnu_debug::__msg_not_heap_pred)      \
00334                       ._M_iterator(_First, #_First)                     \
00335                       ._M_iterator(_Last, #_Last)                       \
00336                       ._M_string(#_Pred))
00337 
00338 // Verify that the container is not self move assigned
00339 #define __glibcxx_check_self_move_assign(_Other)                        \
00340 _GLIBCXX_DEBUG_VERIFY(this != &_Other,                                  \
00341                       _M_message(__gnu_debug::__msg_self_move_assign)   \
00342                       ._M_sequence(*this, "this"))
00343 
00344 // Verify that load factor is positive
00345 #define __glibcxx_check_max_load_factor(_F)                             \
00346 _GLIBCXX_DEBUG_VERIFY(_F > 0.0f,                                        \
00347                       _M_message(__gnu_debug::__msg_valid_load_factor)  \
00348                       ._M_sequence(*this, "this"))
00349 
00350 #define __glibcxx_check_equal_allocs(_This, _Other)                     \
00351 _GLIBCXX_DEBUG_VERIFY(_This.get_allocator() == _Other.get_allocator(),  \
00352                       _M_message(__gnu_debug::__msg_equal_allocs)       \
00353                       ._M_sequence(_This, "this"))
00354 
00355 #ifdef _GLIBCXX_DEBUG_PEDANTIC
00356 #  define __glibcxx_check_string(_String) _GLIBCXX_DEBUG_ASSERT(_String != 0)
00357 #  define __glibcxx_check_string_len(_String,_Len) \
00358        _GLIBCXX_DEBUG_ASSERT(_String != 0 || _Len == 0)
00359 #else
00360 #  define __glibcxx_check_string(_String)
00361 #  define __glibcxx_check_string_len(_String,_Len)
00362 #endif
00363 
00364 #endif