macros.h revision 169692
1184610Salfred// Debugging support implementation -*- C++ -*- 2184610Salfred 3188412Sthompsa// Copyright (C) 2003, 2005, 2006 4184610Salfred// Free Software Foundation, Inc. 5184610Salfred// 6184610Salfred// This file is part of the GNU ISO C++ Library. This library is free 7184610Salfred// software; you can redistribute it and/or modify it under the 8184610Salfred// terms of the GNU General Public License as published by the 9184610Salfred// Free Software Foundation; either version 2, or (at your option) 10184610Salfred// any later version. 11184610Salfred 12184610Salfred// This library is distributed in the hope that it will be useful, 13184610Salfred// but WITHOUT ANY WARRANTY; without even the implied warranty of 14184610Salfred// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15184610Salfred// GNU General Public License for more details. 16184610Salfred 17184610Salfred// You should have received a copy of the GNU General Public License along 18184610Salfred// with this library; see the file COPYING. If not, write to the Free 19184610Salfred// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 20184610Salfred// USA. 21184610Salfred 22184610Salfred// As a special exception, you may use this file as part of a free software 23184610Salfred// library without restriction. Specifically, if other files instantiate 24184610Salfred// templates or use macros or inline functions from this file, or you compile 25184610Salfred// this file and link it with other files to produce an executable, this 26184610Salfred// file does not by itself cause the resulting executable to be covered by 27194677Sthompsa// the GNU General Public License. This exception does not however 28194677Sthompsa// invalidate any other reasons why the executable file might be covered by 29194677Sthompsa// the GNU General Public License. 30194677Sthompsa 31194677Sthompsa/** @file debug/macros.h 32194677Sthompsa * This file is a GNU debug extension to the Standard C++ Library. 33194677Sthompsa */ 34194677Sthompsa 35194677Sthompsa#ifndef _GLIBCXX_DEBUG_MACROS_H 36194677Sthompsa#define _GLIBCXX_DEBUG_MACROS_H 1 37194677Sthompsa 38194677Sthompsa/** 39194677Sthompsa * Macros used by the implementation to verify certain 40194677Sthompsa * properties. These macros may only be used directly by the debug 41194677Sthompsa * wrappers. Note that these are macros (instead of the more obviously 42194677Sthompsa * "correct" choice of making them functions) because we need line and 43194677Sthompsa * file information at the call site, to minimize the distance between 44194677Sthompsa * the user error and where the error is reported. 45194677Sthompsa * 46194677Sthompsa */ 47188942Sthompsa#define _GLIBCXX_DEBUG_VERIFY(_Condition,_ErrorMessage) \ 48194677Sthompsa do \ 49188412Sthompsa { \ 50188942Sthompsa if (! (_Condition)) \ 51188942Sthompsa __gnu_debug::_Error_formatter::_M_at(__FILE__, __LINE__) \ 52184610Salfred ._ErrorMessage._M_error(); \ 53188412Sthompsa } while (false) 54184610Salfred 55188412Sthompsa// Verify that [_First, _Last) forms a valid iterator range. 56188412Sthompsa#define __glibcxx_check_valid_range(_First,_Last) \ 57188412Sthompsa_GLIBCXX_DEBUG_VERIFY(__gnu_debug::__valid_range(_First, _Last), \ 58188412Sthompsa _M_message(__gnu_debug::__msg_valid_range) \ 59188942Sthompsa ._M_iterator(_First, #_First) \ 60188942Sthompsa ._M_iterator(_Last, #_Last)) 61188552Sthompsa 62188412Sthompsa/** Verify that we can insert into *this with the iterator _Position. 63188412Sthompsa * Insertion into a container at a specific position requires that 64193045Sthompsa * the iterator be nonsingular (i.e., either dereferenceable or 65193045Sthompsa * past-the-end) and that it reference the sequence we are inserting 66193045Sthompsa * into. Note that this macro is only valid when the container is a 67193045Sthompsa * _Safe_sequence and the iterator is a _Safe_iterator. 68193045Sthompsa*/ 69193045Sthompsa#define __glibcxx_check_insert(_Position) \ 70193045Sthompsa_GLIBCXX_DEBUG_VERIFY(!_Position._M_singular(), \ 71188412Sthompsa _M_message(__gnu_debug::__msg_insert_singular) \ 72188412Sthompsa ._M_sequence(*this, "this") \ 73188412Sthompsa ._M_iterator(_Position, #_Position)); \ 74188412Sthompsa_GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this), \ 75188412Sthompsa _M_message(__gnu_debug::__msg_insert_different) \ 76188412Sthompsa ._M_sequence(*this, "this") \ 77188412Sthompsa ._M_iterator(_Position, #_Position)) 78188412Sthompsa 79188412Sthompsa/** Verify that we can insert the values in the iterator range 80188412Sthompsa * [_First, _Last) into *this with the iterator _Position. Insertion 81188412Sthompsa * into a container at a specific position requires that the iterator 82188412Sthompsa * be nonsingular (i.e., either dereferenceable or past-the-end), 83194228Sthompsa * that it reference the sequence we are inserting into, and that the 84184610Salfred * iterator range [_First, Last) is a valid (possibly empty) 85194228Sthompsa * range. Note that this macro is only valid when the container is a 86188412Sthompsa * _Safe_sequence and the iterator is a _Safe_iterator. 87188412Sthompsa * 88188412Sthompsa * @tbd We would like to be able to check for noninterference of 89194228Sthompsa * _Position and the range [_First, _Last), but that can't (in 90188412Sthompsa * general) be done. 91188412Sthompsa*/ 92184610Salfred#define __glibcxx_check_insert_range(_Position,_First,_Last) \ 93188412Sthompsa__glibcxx_check_valid_range(_First,_Last); \ 94192984Sthompsa_GLIBCXX_DEBUG_VERIFY(!_Position._M_singular(), \ 95193045Sthompsa _M_message(__gnu_debug::__msg_insert_singular) \ 96192984Sthompsa ._M_sequence(*this, "this") \ 97188412Sthompsa ._M_iterator(_Position, #_Position)); \ 98192984Sthompsa_GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this), \ 99188412Sthompsa _M_message(__gnu_debug::__msg_insert_different) \ 100188412Sthompsa ._M_sequence(*this, "this") \ 101188412Sthompsa ._M_iterator(_Position, #_Position)) 102194228Sthompsa 103188412Sthompsa/** Verify that we can erase the element referenced by the iterator 104184610Salfred * _Position. We can erase the element if the _Position iterator is 105188412Sthompsa * dereferenceable and references this sequence. 106188412Sthompsa*/ 107188412Sthompsa#define __glibcxx_check_erase(_Position) \ 108188412Sthompsa_GLIBCXX_DEBUG_VERIFY(_Position._M_dereferenceable(), \ 109188412Sthompsa _M_message(__gnu_debug::__msg_erase_bad) \ 110192984Sthompsa ._M_sequence(*this, "this") \ 111194228Sthompsa ._M_iterator(_Position, #_Position)); \ 112188412Sthompsa_GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this), \ 113188412Sthompsa _M_message(__gnu_debug::__msg_erase_different) \ 114188412Sthompsa ._M_sequence(*this, "this") \ 115188412Sthompsa ._M_iterator(_Position, #_Position)) 116188412Sthompsa 117188412Sthompsa/** Verify that we can erase the elements in the iterator range 118188412Sthompsa * [_First, _Last). We can erase the elements if [_First, _Last) is a 119188412Sthompsa * valid iterator range within this sequence. 120188412Sthompsa*/ 121194228Sthompsa#define __glibcxx_check_erase_range(_First,_Last) \ 122184610Salfred__glibcxx_check_valid_range(_First,_Last); \ 123184610Salfred_GLIBCXX_DEBUG_VERIFY(_First._M_attached_to(this), \ 124188412Sthompsa _M_message(__gnu_debug::__msg_erase_different) \ 125194228Sthompsa ._M_sequence(*this, "this") \ 126184610Salfred ._M_iterator(_First, #_First) \ 127188412Sthompsa ._M_iterator(_Last, #_Last)) 128188412Sthompsa 129184610Salfred// Verify that the subscript _N is less than the container's size. 130188412Sthompsa#define __glibcxx_check_subscript(_N) \ 131194228Sthompsa_GLIBCXX_DEBUG_VERIFY(_N < this->size(), \ 132188412Sthompsa _M_message(__gnu_debug::__msg_subscript_oob) \ 133188412Sthompsa ._M_sequence(*this, "this") \ 134188412Sthompsa ._M_integer(_N, #_N) \ 135188412Sthompsa ._M_integer(this->size(), "size")) 136188412Sthompsa 137194228Sthompsa// Verify that the container is nonempty 138188412Sthompsa#define __glibcxx_check_nonempty() \ 139188412Sthompsa_GLIBCXX_DEBUG_VERIFY(! this->empty(), \ 140188412Sthompsa _M_message(__gnu_debug::__msg_empty) \ 141188412Sthompsa ._M_sequence(*this, "this")) 142188412Sthompsa 143188412Sthompsa// Verify that the < operator for elements in the sequence is a 144188412Sthompsa// StrictWeakOrdering by checking that it is irreflexive. 145192984Sthompsa#define __glibcxx_check_strict_weak_ordering(_First,_Last) \ 146188412Sthompsa_GLIBCXX_DEBUG_ASSERT(_First == _Last || !(*_First < *_First)) 147188412Sthompsa 148188412Sthompsa// Verify that the predicate is StrictWeakOrdering by checking that it 149188412Sthompsa// is irreflexive. 150188412Sthompsa#define __glibcxx_check_strict_weak_ordering_pred(_First,_Last,_Pred) \ 151188412Sthompsa_GLIBCXX_DEBUG_ASSERT(_First == _Last || !_Pred(*_First, *_First)) 152188412Sthompsa 153194228Sthompsa 154188412Sthompsa// Verify that the iterator range [_First, _Last) is sorted 155188412Sthompsa#define __glibcxx_check_sorted(_First,_Last) \ 156188412Sthompsa__glibcxx_check_valid_range(_First,_Last); \ 157188412Sthompsa__glibcxx_check_strict_weak_ordering(_First,_Last); \ 158188412Sthompsa_GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted(_First, _Last), \ 159188412Sthompsa _M_message(__gnu_debug::__msg_unsorted) \ 160188412Sthompsa ._M_iterator(_First, #_First) \ 161188412Sthompsa ._M_iterator(_Last, #_Last)) 162188412Sthompsa 163188412Sthompsa/** Verify that the iterator range [_First, _Last) is sorted by the 164194228Sthompsa predicate _Pred. */ 165188412Sthompsa#define __glibcxx_check_sorted_pred(_First,_Last,_Pred) \ 166188412Sthompsa__glibcxx_check_valid_range(_First,_Last); \ 167188412Sthompsa__glibcxx_check_strict_weak_ordering_pred(_First,_Last,_Pred); \ 168188412Sthompsa_GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted(_First, _Last, _Pred), \ 169188412Sthompsa _M_message(__gnu_debug::__msg_unsorted_pred) \ 170188412Sthompsa ._M_iterator(_First, #_First) \ 171188412Sthompsa ._M_iterator(_Last, #_Last) \ 172188412Sthompsa ._M_string(#_Pred)) 173188412Sthompsa 174188412Sthompsa/** Verify that the iterator range [_First, _Last) is partitioned 175188412Sthompsa w.r.t. the value _Value. */ 176188412Sthompsa#define __glibcxx_check_partitioned(_First,_Last,_Value) \ 177188412Sthompsa__glibcxx_check_valid_range(_First,_Last); \ 178188412Sthompsa_GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned(_First, _Last, \ 179188412Sthompsa _Value), \ 180188412Sthompsa _M_message(__gnu_debug::__msg_unpartitioned) \ 181188412Sthompsa ._M_iterator(_First, #_First) \ 182188412Sthompsa ._M_iterator(_Last, #_Last) \ 183192984Sthompsa ._M_string(#_Value)) 184188412Sthompsa 185192984Sthompsa/** Verify that the iterator range [_First, _Last) is partitioned 186192984Sthompsa w.r.t. the value _Value and predicate _Pred. */ 187192984Sthompsa#define __glibcxx_check_partitioned_pred(_First,_Last,_Value,_Pred) \ 188188412Sthompsa__glibcxx_check_valid_range(_First,_Last); \ 189188412Sthompsa_GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned(_First, _Last, \ 190188412Sthompsa _Value, _Pred), \ 191188412Sthompsa _M_message(__gnu_debug::__msg_unpartitioned_pred) \ 192188412Sthompsa ._M_iterator(_First, #_First) \ 193188412Sthompsa ._M_iterator(_Last, #_Last) \ 194188412Sthompsa ._M_string(#_Pred) \ 195188412Sthompsa ._M_string(#_Value)) 196188412Sthompsa 197188412Sthompsa// Verify that the iterator range [_First, _Last) is a heap 198194228Sthompsa#define __glibcxx_check_heap(_First,_Last) \ 199188412Sthompsa__glibcxx_check_valid_range(_First,_Last); \ 200188412Sthompsa_GLIBCXX_DEBUG_VERIFY(std::__is_heap(_First, _Last), \ 201188412Sthompsa _M_message(__gnu_debug::__msg_not_heap) \ 202184610Salfred ._M_iterator(_First, #_First) \ 203188412Sthompsa ._M_iterator(_Last, #_Last)) 204188412Sthompsa 205184610Salfred/** Verify that the iterator range [_First, _Last) is a heap 206184610Salfred w.r.t. the predicate _Pred. */ 207188412Sthompsa#define __glibcxx_check_heap_pred(_First,_Last,_Pred) \ 208188412Sthompsa__glibcxx_check_valid_range(_First,_Last); \ 209188412Sthompsa_GLIBCXX_DEBUG_VERIFY(std::__is_heap(_First, _Last, _Pred), \ 210188412Sthompsa _M_message(__gnu_debug::__msg_not_heap_pred) \ 211188412Sthompsa ._M_iterator(_First, #_First) \ 212188412Sthompsa ._M_iterator(_Last, #_Last) \ 213188412Sthompsa ._M_string(#_Pred)) 214194228Sthompsa 215188412Sthompsa#ifdef _GLIBCXX_DEBUG_PEDANTIC 216188412Sthompsa# define __glibcxx_check_string(_String) _GLIBCXX_DEBUG_ASSERT(_String != 0) 217207554Ssobomax# define __glibcxx_check_string_len(_String,_Len) \ 218207554Ssobomax _GLIBCXX_DEBUG_ASSERT(_String != 0 || _Len == 0) 219188412Sthompsa#else 220188412Sthompsa# define __glibcxx_check_string(_String) 221184610Salfred# define __glibcxx_check_string_len(_String,_Len) 222188412Sthompsa#endif 223188412Sthompsa 224196403Sjhb#endif 225188412Sthompsa