macros.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // Debugging support implementation -*- C++ -*-
  2. // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
  3. // Free Software Foundation, Inc.
  4. //
  5. // This file is part of the GNU ISO C++ Library. This library is free
  6. // software; you can redistribute it and/or modify it under the
  7. // terms of the GNU General Public License as published by the
  8. // Free Software Foundation; either version 3, or (at your option)
  9. // any later version.
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. // Under Section 7 of GPL version 3, you are granted additional
  15. // permissions described in the GCC Runtime Library Exception, version
  16. // 3.1, as published by the Free Software Foundation.
  17. // You should have received a copy of the GNU General Public License and
  18. // a copy of the GCC Runtime Library Exception along with this program;
  19. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. // <http://www.gnu.org/licenses/>.
  21. /** @file debug/macros.h
  22. * This file is a GNU debug extension to the Standard C++ Library.
  23. */
  24. #ifndef _GLIBCXX_DEBUG_MACROS_H
  25. #define _GLIBCXX_DEBUG_MACROS_H 1
  26. /**
  27. * Macros used by the implementation to verify certain
  28. * properties. These macros may only be used directly by the debug
  29. * wrappers. Note that these are macros (instead of the more obviously
  30. * "correct" choice of making them functions) because we need line and
  31. * file information at the call site, to minimize the distance between
  32. * the user error and where the error is reported.
  33. *
  34. */
  35. #define _GLIBCXX_DEBUG_VERIFY(_Condition,_ErrorMessage) \
  36. do \
  37. { \
  38. if (! (_Condition)) \
  39. __gnu_debug::_Error_formatter::_M_at(__FILE__, __LINE__) \
  40. ._ErrorMessage._M_error(); \
  41. } while (false)
  42. // Verify that [_First, _Last) forms a valid iterator range.
  43. #define __glibcxx_check_valid_range(_First,_Last) \
  44. _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__valid_range(_First, _Last), \
  45. _M_message(__gnu_debug::__msg_valid_range) \
  46. ._M_iterator(_First, #_First) \
  47. ._M_iterator(_Last, #_Last))
  48. /** Verify that we can insert into *this with the iterator _Position.
  49. * Insertion into a container at a specific position requires that
  50. * the iterator be nonsingular (i.e., either dereferenceable or
  51. * past-the-end) and that it reference the sequence we are inserting
  52. * into. Note that this macro is only valid when the container is a
  53. * _Safe_sequence and the iterator is a _Safe_iterator.
  54. */
  55. #define __glibcxx_check_insert(_Position) \
  56. _GLIBCXX_DEBUG_VERIFY(!_Position._M_singular(), \
  57. _M_message(__gnu_debug::__msg_insert_singular) \
  58. ._M_sequence(*this, "this") \
  59. ._M_iterator(_Position, #_Position)); \
  60. _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this), \
  61. _M_message(__gnu_debug::__msg_insert_different) \
  62. ._M_sequence(*this, "this") \
  63. ._M_iterator(_Position, #_Position))
  64. /** Verify that we can insert the values in the iterator range
  65. * [_First, _Last) into *this with the iterator _Position. Insertion
  66. * into a container at a specific position requires that the iterator
  67. * be nonsingular (i.e., either dereferenceable or past-the-end),
  68. * that it reference the sequence we are inserting into, and that the
  69. * iterator range [_First, Last) is a valid (possibly empty)
  70. * range. Note that this macro is only valid when the container is a
  71. * _Safe_sequence and the iterator is a _Safe_iterator.
  72. *
  73. * @tbd We would like to be able to check for noninterference of
  74. * _Position and the range [_First, _Last), but that can't (in
  75. * general) be done.
  76. */
  77. #define __glibcxx_check_insert_range(_Position,_First,_Last) \
  78. __glibcxx_check_valid_range(_First,_Last); \
  79. _GLIBCXX_DEBUG_VERIFY(!_Position._M_singular(), \
  80. _M_message(__gnu_debug::__msg_insert_singular) \
  81. ._M_sequence(*this, "this") \
  82. ._M_iterator(_Position, #_Position)); \
  83. _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this), \
  84. _M_message(__gnu_debug::__msg_insert_different) \
  85. ._M_sequence(*this, "this") \
  86. ._M_iterator(_Position, #_Position))
  87. /** Verify that we can erase the element referenced by the iterator
  88. * _Position. We can erase the element if the _Position iterator is
  89. * dereferenceable and references this sequence.
  90. */
  91. #define __glibcxx_check_erase(_Position) \
  92. _GLIBCXX_DEBUG_VERIFY(_Position._M_dereferenceable(), \
  93. _M_message(__gnu_debug::__msg_erase_bad) \
  94. ._M_sequence(*this, "this") \
  95. ._M_iterator(_Position, #_Position)); \
  96. _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this), \
  97. _M_message(__gnu_debug::__msg_erase_different) \
  98. ._M_sequence(*this, "this") \
  99. ._M_iterator(_Position, #_Position))
  100. /** Verify that we can erase the elements in the iterator range
  101. * [_First, _Last). We can erase the elements if [_First, _Last) is a
  102. * valid iterator range within this sequence.
  103. */
  104. #define __glibcxx_check_erase_range(_First,_Last) \
  105. __glibcxx_check_valid_range(_First,_Last); \
  106. _GLIBCXX_DEBUG_VERIFY(_First._M_attached_to(this), \
  107. _M_message(__gnu_debug::__msg_erase_different) \
  108. ._M_sequence(*this, "this") \
  109. ._M_iterator(_First, #_First) \
  110. ._M_iterator(_Last, #_Last))
  111. // Verify that the subscript _N is less than the container's size.
  112. #define __glibcxx_check_subscript(_N) \
  113. _GLIBCXX_DEBUG_VERIFY(_N < this->size(), \
  114. _M_message(__gnu_debug::__msg_subscript_oob) \
  115. ._M_sequence(*this, "this") \
  116. ._M_integer(_N, #_N) \
  117. ._M_integer(this->size(), "size"))
  118. // Verify that the container is nonempty
  119. #define __glibcxx_check_nonempty() \
  120. _GLIBCXX_DEBUG_VERIFY(! this->empty(), \
  121. _M_message(__gnu_debug::__msg_empty) \
  122. ._M_sequence(*this, "this"))
  123. // Verify that the iterator range [_First, _Last) is sorted
  124. #define __glibcxx_check_sorted(_First,_Last) \
  125. __glibcxx_check_valid_range(_First,_Last); \
  126. _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted(_First, _Last), \
  127. _M_message(__gnu_debug::__msg_unsorted) \
  128. ._M_iterator(_First, #_First) \
  129. ._M_iterator(_Last, #_Last))
  130. /** Verify that the iterator range [_First, _Last) is sorted by the
  131. predicate _Pred. */
  132. #define __glibcxx_check_sorted_pred(_First,_Last,_Pred) \
  133. __glibcxx_check_valid_range(_First,_Last); \
  134. _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted(_First, _Last, _Pred), \
  135. _M_message(__gnu_debug::__msg_unsorted_pred) \
  136. ._M_iterator(_First, #_First) \
  137. ._M_iterator(_Last, #_Last) \
  138. ._M_string(#_Pred))
  139. // Special variant for std::merge, std::includes, std::set_*
  140. #define __glibcxx_check_sorted_set(_First1,_Last1,_First2) \
  141. __glibcxx_check_valid_range(_First1,_Last1); \
  142. _GLIBCXX_DEBUG_VERIFY( \
  143. __gnu_debug::__check_sorted_set(_First1, _Last1, _First2), \
  144. _M_message(__gnu_debug::__msg_unsorted) \
  145. ._M_iterator(_First1, #_First1) \
  146. ._M_iterator(_Last1, #_Last1))
  147. // Likewise with a _Pred.
  148. #define __glibcxx_check_sorted_set_pred(_First1,_Last1,_First2,_Pred) \
  149. __glibcxx_check_valid_range(_First1,_Last1); \
  150. _GLIBCXX_DEBUG_VERIFY( \
  151. __gnu_debug::__check_sorted_set(_First1, _Last1, _First2, _Pred), \
  152. _M_message(__gnu_debug::__msg_unsorted_pred) \
  153. ._M_iterator(_First1, #_First1) \
  154. ._M_iterator(_Last1, #_Last1) \
  155. ._M_string(#_Pred))
  156. /** Verify that the iterator range [_First, _Last) is partitioned
  157. w.r.t. the value _Value. */
  158. #define __glibcxx_check_partitioned_lower(_First,_Last,_Value) \
  159. __glibcxx_check_valid_range(_First,_Last); \
  160. _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_lower(_First, _Last, \
  161. _Value), \
  162. _M_message(__gnu_debug::__msg_unpartitioned) \
  163. ._M_iterator(_First, #_First) \
  164. ._M_iterator(_Last, #_Last) \
  165. ._M_string(#_Value))
  166. #define __glibcxx_check_partitioned_upper(_First,_Last,_Value) \
  167. __glibcxx_check_valid_range(_First,_Last); \
  168. _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_upper(_First, _Last, \
  169. _Value), \
  170. _M_message(__gnu_debug::__msg_unpartitioned) \
  171. ._M_iterator(_First, #_First) \
  172. ._M_iterator(_Last, #_Last) \
  173. ._M_string(#_Value))
  174. /** Verify that the iterator range [_First, _Last) is partitioned
  175. w.r.t. the value _Value and predicate _Pred. */
  176. #define __glibcxx_check_partitioned_lower_pred(_First,_Last,_Value,_Pred) \
  177. __glibcxx_check_valid_range(_First,_Last); \
  178. _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_lower(_First, _Last, \
  179. _Value, _Pred), \
  180. _M_message(__gnu_debug::__msg_unpartitioned_pred) \
  181. ._M_iterator(_First, #_First) \
  182. ._M_iterator(_Last, #_Last) \
  183. ._M_string(#_Pred) \
  184. ._M_string(#_Value))
  185. /** Verify that the iterator range [_First, _Last) is partitioned
  186. w.r.t. the value _Value and predicate _Pred. */
  187. #define __glibcxx_check_partitioned_upper_pred(_First,_Last,_Value,_Pred) \
  188. __glibcxx_check_valid_range(_First,_Last); \
  189. _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_upper(_First, _Last, \
  190. _Value, _Pred), \
  191. _M_message(__gnu_debug::__msg_unpartitioned_pred) \
  192. ._M_iterator(_First, #_First) \
  193. ._M_iterator(_Last, #_Last) \
  194. ._M_string(#_Pred) \
  195. ._M_string(#_Value))
  196. // Verify that the iterator range [_First, _Last) is a heap
  197. #define __glibcxx_check_heap(_First,_Last) \
  198. __glibcxx_check_valid_range(_First,_Last); \
  199. _GLIBCXX_DEBUG_VERIFY(std::__is_heap(_First, _Last), \
  200. _M_message(__gnu_debug::__msg_not_heap) \
  201. ._M_iterator(_First, #_First) \
  202. ._M_iterator(_Last, #_Last))
  203. /** Verify that the iterator range [_First, _Last) is a heap
  204. w.r.t. the predicate _Pred. */
  205. #define __glibcxx_check_heap_pred(_First,_Last,_Pred) \
  206. __glibcxx_check_valid_range(_First,_Last); \
  207. _GLIBCXX_DEBUG_VERIFY(std::__is_heap(_First, _Last, _Pred), \
  208. _M_message(__gnu_debug::__msg_not_heap_pred) \
  209. ._M_iterator(_First, #_First) \
  210. ._M_iterator(_Last, #_Last) \
  211. ._M_string(#_Pred))
  212. #ifdef _GLIBCXX_DEBUG_PEDANTIC
  213. # define __glibcxx_check_string(_String) _GLIBCXX_DEBUG_ASSERT(_String != 0)
  214. # define __glibcxx_check_string_len(_String,_Len) \
  215. _GLIBCXX_DEBUG_ASSERT(_String != 0 || _Len == 0)
  216. #else
  217. # define __glibcxx_check_string(_String)
  218. # define __glibcxx_check_string_len(_String,_Len)
  219. #endif
  220. #endif