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