macros.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // Debugging support implementation -*- C++ -*-
  2. // Copyright (C) 2003-2015 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file debug/macros.h
  21. * This file is a GNU debug extension to the Standard C++ Library.
  22. */
  23. #ifndef _GLIBCXX_DEBUG_MACROS_H
  24. #define _GLIBCXX_DEBUG_MACROS_H 1
  25. /**
  26. * Macros used by the implementation to verify certain
  27. * properties. These macros may only be used directly by the debug
  28. * wrappers. Note that these are macros (instead of the more obviously
  29. * @a correct choice of making them functions) because we need line and
  30. * file information at the call site, to minimize the distance between
  31. * the user error and where the error is reported.
  32. *
  33. */
  34. #define _GLIBCXX_DEBUG_VERIFY_AT(_Condition,_ErrorMessage,_File,_Line) \
  35. do \
  36. { \
  37. if (! (_Condition)) \
  38. __gnu_debug::_Error_formatter::_M_at(_File, _Line) \
  39. ._ErrorMessage._M_error(); \
  40. } while (false)
  41. #define _GLIBCXX_DEBUG_VERIFY(_Condition,_ErrorMessage) \
  42. _GLIBCXX_DEBUG_VERIFY_AT(_Condition,_ErrorMessage,__FILE__,__LINE__)
  43. // Verify that [_First, _Last) forms a valid iterator range.
  44. #define __glibcxx_check_valid_range(_First,_Last) \
  45. _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__valid_range(_First, _Last), \
  46. _M_message(__gnu_debug::__msg_valid_range) \
  47. ._M_iterator(_First, #_First) \
  48. ._M_iterator(_Last, #_Last))
  49. // Verify that [_First, _Last) forms a non-empty iterator range.
  50. #define __glibcxx_check_non_empty_range(_First,_Last) \
  51. _GLIBCXX_DEBUG_VERIFY(_First != _Last, \
  52. _M_message(__gnu_debug::__msg_non_empty_range) \
  53. ._M_iterator(_First, #_First) \
  54. ._M_iterator(_Last, #_Last))
  55. /** Verify that we can insert into *this with the iterator _Position.
  56. * Insertion into a container at a specific position requires that
  57. * the iterator be nonsingular, either dereferenceable or past-the-end,
  58. * and that it reference the sequence we are inserting into. Note that
  59. * this macro is only valid when the container is a_Safe_sequence and
  60. * the iterator is a _Safe_iterator.
  61. */
  62. #define __glibcxx_check_insert(_Position) \
  63. _GLIBCXX_DEBUG_VERIFY(!_Position._M_singular(), \
  64. _M_message(__gnu_debug::__msg_insert_singular) \
  65. ._M_sequence(*this, "this") \
  66. ._M_iterator(_Position, #_Position)); \
  67. _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this), \
  68. _M_message(__gnu_debug::__msg_insert_different) \
  69. ._M_sequence(*this, "this") \
  70. ._M_iterator(_Position, #_Position))
  71. /** Verify that we can insert into *this after the iterator _Position.
  72. * Insertion into a container after a specific position requires that
  73. * the iterator be nonsingular, either dereferenceable or before-begin,
  74. * and that it reference the sequence we are inserting into. Note that
  75. * this macro is only valid when the container is a_Safe_sequence and
  76. * the iterator is a _Safe_iterator.
  77. */
  78. #define __glibcxx_check_insert_after(_Position) \
  79. __glibcxx_check_insert(_Position); \
  80. _GLIBCXX_DEBUG_VERIFY(!_Position._M_is_end(), \
  81. _M_message(__gnu_debug::__msg_insert_after_end) \
  82. ._M_sequence(*this, "this") \
  83. ._M_iterator(_Position, #_Position))
  84. /** Verify that we can insert the values in the iterator range
  85. * [_First, _Last) into *this with the iterator _Position. Insertion
  86. * into a container at a specific position requires that the iterator
  87. * be nonsingular (i.e., either dereferenceable or past-the-end),
  88. * that it reference the sequence we are inserting into, and that the
  89. * iterator range [_First, _Last) is a valid (possibly empty)
  90. * range which does not reference the sequence we are inserting into.
  91. * Note that this macro is only valid when the container is a
  92. * _Safe_sequence and the _Position iterator is a _Safe_iterator.
  93. */
  94. #define __glibcxx_check_insert_range(_Position,_First,_Last) \
  95. __glibcxx_check_valid_range(_First,_Last); \
  96. __glibcxx_check_insert(_Position); \
  97. _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__foreign_iterator(_Position,_First,_Last),\
  98. _M_message(__gnu_debug::__msg_insert_range_from_self)\
  99. ._M_iterator(_First, #_First) \
  100. ._M_iterator(_Last, #_Last) \
  101. ._M_sequence(*this, "this"))
  102. /** Verify that we can insert the values in the iterator range
  103. * [_First, _Last) into *this after the iterator _Position. Insertion
  104. * into a container after a specific position requires that the iterator
  105. * be nonsingular (i.e., either dereferenceable or past-the-end),
  106. * that it reference the sequence we are inserting into, and that the
  107. * iterator range [_First, _Last) is a valid (possibly empty)
  108. * range which does not reference the sequence we are inserting into.
  109. * Note that this macro is only valid when the container is a
  110. * _Safe_sequence and the _Position iterator is a _Safe_iterator.
  111. */
  112. #define __glibcxx_check_insert_range_after(_Position,_First,_Last) \
  113. __glibcxx_check_valid_range(_First,_Last); \
  114. __glibcxx_check_insert_after(_Position); \
  115. _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__foreign_iterator(_Position,_First,_Last),\
  116. _M_message(__gnu_debug::__msg_insert_range_from_self)\
  117. ._M_iterator(_First, #_First) \
  118. ._M_iterator(_Last, #_Last) \
  119. ._M_sequence(*this, "this"))
  120. /** Verify that we can erase the element referenced by the iterator
  121. * _Position. We can erase the element if the _Position iterator is
  122. * dereferenceable and references this sequence.
  123. */
  124. #define __glibcxx_check_erase(_Position) \
  125. _GLIBCXX_DEBUG_VERIFY(_Position._M_dereferenceable(), \
  126. _M_message(__gnu_debug::__msg_erase_bad) \
  127. ._M_sequence(*this, "this") \
  128. ._M_iterator(_Position, #_Position)); \
  129. _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this), \
  130. _M_message(__gnu_debug::__msg_erase_different) \
  131. ._M_sequence(*this, "this") \
  132. ._M_iterator(_Position, #_Position))
  133. /** Verify that we can erase the element after the iterator
  134. * _Position. We can erase the element if the _Position iterator is
  135. * before a dereferenceable one and references this sequence.
  136. */
  137. #define __glibcxx_check_erase_after(_Position) \
  138. _GLIBCXX_DEBUG_VERIFY(_Position._M_before_dereferenceable(), \
  139. _M_message(__gnu_debug::__msg_erase_after_bad) \
  140. ._M_sequence(*this, "this") \
  141. ._M_iterator(_Position, #_Position)); \
  142. _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this), \
  143. _M_message(__gnu_debug::__msg_erase_different) \
  144. ._M_sequence(*this, "this") \
  145. ._M_iterator(_Position, #_Position))
  146. /** Verify that we can erase the elements in the iterator range
  147. * [_First, _Last). We can erase the elements if [_First, _Last) is a
  148. * valid iterator range within this sequence.
  149. */
  150. #define __glibcxx_check_erase_range(_First,_Last) \
  151. __glibcxx_check_valid_range(_First,_Last); \
  152. _GLIBCXX_DEBUG_VERIFY(_First._M_attached_to(this), \
  153. _M_message(__gnu_debug::__msg_erase_different) \
  154. ._M_sequence(*this, "this") \
  155. ._M_iterator(_First, #_First) \
  156. ._M_iterator(_Last, #_Last))
  157. /** Verify that we can erase the elements in the iterator range
  158. * (_First, _Last). We can erase the elements if (_First, _Last) is a
  159. * valid iterator range within this sequence.
  160. */
  161. #define __glibcxx_check_erase_range_after(_First,_Last) \
  162. _GLIBCXX_DEBUG_VERIFY(_First._M_can_compare(_Last), \
  163. _M_message(__gnu_debug::__msg_erase_different) \
  164. ._M_sequence(*this, "this") \
  165. ._M_iterator(_First, #_First) \
  166. ._M_iterator(_Last, #_Last)); \
  167. _GLIBCXX_DEBUG_VERIFY(_First._M_attached_to(this), \
  168. _M_message(__gnu_debug::__msg_erase_different) \
  169. ._M_sequence(*this, "this") \
  170. ._M_iterator(_First, #_First)); \
  171. _GLIBCXX_DEBUG_VERIFY(_First != _Last, \
  172. _M_message(__gnu_debug::__msg_valid_range2) \
  173. ._M_sequence(*this, "this") \
  174. ._M_iterator(_First, #_First) \
  175. ._M_iterator(_Last, #_Last)); \
  176. _GLIBCXX_DEBUG_VERIFY(_First._M_incrementable(), \
  177. _M_message(__gnu_debug::__msg_valid_range2) \
  178. ._M_sequence(*this, "this") \
  179. ._M_iterator(_First, #_First) \
  180. ._M_iterator(_Last, #_Last)); \
  181. _GLIBCXX_DEBUG_VERIFY(!_Last._M_is_before_begin(), \
  182. _M_message(__gnu_debug::__msg_valid_range2) \
  183. ._M_sequence(*this, "this") \
  184. ._M_iterator(_First, #_First) \
  185. ._M_iterator(_Last, #_Last)) \
  186. // Verify that the subscript _N is less than the container's size.
  187. #define __glibcxx_check_subscript(_N) \
  188. _GLIBCXX_DEBUG_VERIFY(_N < this->size(), \
  189. _M_message(__gnu_debug::__msg_subscript_oob) \
  190. ._M_sequence(*this, "this") \
  191. ._M_integer(_N, #_N) \
  192. ._M_integer(this->size(), "size"))
  193. // Verify that the bucket _N is less than the container's buckets count.
  194. #define __glibcxx_check_bucket_index(_N) \
  195. _GLIBCXX_DEBUG_VERIFY(_N < this->bucket_count(), \
  196. _M_message(__gnu_debug::__msg_bucket_index_oob) \
  197. ._M_sequence(*this, "this") \
  198. ._M_integer(_N, #_N) \
  199. ._M_integer(this->bucket_count(), "size"))
  200. // Verify that the container is nonempty
  201. #define __glibcxx_check_nonempty() \
  202. _GLIBCXX_DEBUG_VERIFY(! this->empty(), \
  203. _M_message(__gnu_debug::__msg_empty) \
  204. ._M_sequence(*this, "this"))
  205. // Verify that the iterator range [_First, _Last) is sorted
  206. #define __glibcxx_check_sorted(_First,_Last) \
  207. __glibcxx_check_valid_range(_First,_Last); \
  208. _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted( \
  209. __gnu_debug::__base(_First), \
  210. __gnu_debug::__base(_Last)), \
  211. _M_message(__gnu_debug::__msg_unsorted) \
  212. ._M_iterator(_First, #_First) \
  213. ._M_iterator(_Last, #_Last))
  214. /** Verify that the iterator range [_First, _Last) is sorted by the
  215. predicate _Pred. */
  216. #define __glibcxx_check_sorted_pred(_First,_Last,_Pred) \
  217. __glibcxx_check_valid_range(_First,_Last); \
  218. _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted( \
  219. __gnu_debug::__base(_First), \
  220. __gnu_debug::__base(_Last), _Pred), \
  221. _M_message(__gnu_debug::__msg_unsorted_pred) \
  222. ._M_iterator(_First, #_First) \
  223. ._M_iterator(_Last, #_Last) \
  224. ._M_string(#_Pred))
  225. // Special variant for std::merge, std::includes, std::set_*
  226. #define __glibcxx_check_sorted_set(_First1,_Last1,_First2) \
  227. __glibcxx_check_valid_range(_First1,_Last1); \
  228. _GLIBCXX_DEBUG_VERIFY( \
  229. __gnu_debug::__check_sorted_set(__gnu_debug::__base(_First1), \
  230. __gnu_debug::__base(_Last1), _First2),\
  231. _M_message(__gnu_debug::__msg_unsorted) \
  232. ._M_iterator(_First1, #_First1) \
  233. ._M_iterator(_Last1, #_Last1))
  234. // Likewise with a _Pred.
  235. #define __glibcxx_check_sorted_set_pred(_First1,_Last1,_First2,_Pred) \
  236. __glibcxx_check_valid_range(_First1,_Last1); \
  237. _GLIBCXX_DEBUG_VERIFY( \
  238. __gnu_debug::__check_sorted_set(__gnu_debug::__base(_First1), \
  239. __gnu_debug::__base(_Last1), \
  240. _First2, _Pred), \
  241. _M_message(__gnu_debug::__msg_unsorted_pred) \
  242. ._M_iterator(_First1, #_First1) \
  243. ._M_iterator(_Last1, #_Last1) \
  244. ._M_string(#_Pred))
  245. /** Verify that the iterator range [_First, _Last) is partitioned
  246. w.r.t. the value _Value. */
  247. #define __glibcxx_check_partitioned_lower(_First,_Last,_Value) \
  248. __glibcxx_check_valid_range(_First,_Last); \
  249. _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_lower( \
  250. __gnu_debug::__base(_First), \
  251. __gnu_debug::__base(_Last), _Value), \
  252. _M_message(__gnu_debug::__msg_unpartitioned) \
  253. ._M_iterator(_First, #_First) \
  254. ._M_iterator(_Last, #_Last) \
  255. ._M_string(#_Value))
  256. #define __glibcxx_check_partitioned_upper(_First,_Last,_Value) \
  257. __glibcxx_check_valid_range(_First,_Last); \
  258. _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_upper( \
  259. __gnu_debug::__base(_First), \
  260. __gnu_debug::__base(_Last), _Value), \
  261. _M_message(__gnu_debug::__msg_unpartitioned) \
  262. ._M_iterator(_First, #_First) \
  263. ._M_iterator(_Last, #_Last) \
  264. ._M_string(#_Value))
  265. /** Verify that the iterator range [_First, _Last) is partitioned
  266. w.r.t. the value _Value and predicate _Pred. */
  267. #define __glibcxx_check_partitioned_lower_pred(_First,_Last,_Value,_Pred) \
  268. __glibcxx_check_valid_range(_First,_Last); \
  269. _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_lower( \
  270. __gnu_debug::__base(_First), \
  271. __gnu_debug::__base(_Last), _Value, _Pred), \
  272. _M_message(__gnu_debug::__msg_unpartitioned_pred) \
  273. ._M_iterator(_First, #_First) \
  274. ._M_iterator(_Last, #_Last) \
  275. ._M_string(#_Pred) \
  276. ._M_string(#_Value))
  277. /** Verify that the iterator range [_First, _Last) is partitioned
  278. w.r.t. the value _Value and predicate _Pred. */
  279. #define __glibcxx_check_partitioned_upper_pred(_First,_Last,_Value,_Pred) \
  280. __glibcxx_check_valid_range(_First,_Last); \
  281. _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_upper( \
  282. __gnu_debug::__base(_First), \
  283. __gnu_debug::__base(_Last), _Value, _Pred), \
  284. _M_message(__gnu_debug::__msg_unpartitioned_pred) \
  285. ._M_iterator(_First, #_First) \
  286. ._M_iterator(_Last, #_Last) \
  287. ._M_string(#_Pred) \
  288. ._M_string(#_Value))
  289. // Verify that the iterator range [_First, _Last) is a heap
  290. #define __glibcxx_check_heap(_First,_Last) \
  291. _GLIBCXX_DEBUG_VERIFY(std::__is_heap(__gnu_debug::__base(_First), \
  292. __gnu_debug::__base(_Last)), \
  293. _M_message(__gnu_debug::__msg_not_heap) \
  294. ._M_iterator(_First, #_First) \
  295. ._M_iterator(_Last, #_Last))
  296. /** Verify that the iterator range [_First, _Last) is a heap
  297. w.r.t. the predicate _Pred. */
  298. #define __glibcxx_check_heap_pred(_First,_Last,_Pred) \
  299. _GLIBCXX_DEBUG_VERIFY(std::__is_heap(__gnu_debug::__base(_First), \
  300. __gnu_debug::__base(_Last), \
  301. _Pred), \
  302. _M_message(__gnu_debug::__msg_not_heap_pred) \
  303. ._M_iterator(_First, #_First) \
  304. ._M_iterator(_Last, #_Last) \
  305. ._M_string(#_Pred))
  306. // Verify that the container is not self move assigned
  307. #define __glibcxx_check_self_move_assign(_Other) \
  308. _GLIBCXX_DEBUG_VERIFY(this != &_Other, \
  309. _M_message(__gnu_debug::__msg_self_move_assign) \
  310. ._M_sequence(*this, "this"))
  311. // Verify that load factor is positive
  312. #define __glibcxx_check_max_load_factor(_F) \
  313. _GLIBCXX_DEBUG_VERIFY(_F > 0.0f, \
  314. _M_message(__gnu_debug::__msg_valid_load_factor) \
  315. ._M_sequence(*this, "this"))
  316. #define __glibcxx_check_equal_allocs(_This, _Other) \
  317. _GLIBCXX_DEBUG_VERIFY(_This.get_allocator() == _Other.get_allocator(), \
  318. _M_message(__gnu_debug::__msg_equal_allocs) \
  319. ._M_sequence(_This, "this"))
  320. #ifdef _GLIBCXX_DEBUG_PEDANTIC
  321. # define __glibcxx_check_string(_String) _GLIBCXX_DEBUG_ASSERT(_String != 0)
  322. # define __glibcxx_check_string_len(_String,_Len) \
  323. _GLIBCXX_DEBUG_ASSERT(_String != 0 || _Len == 0)
  324. #else
  325. # define __glibcxx_check_string(_String)
  326. # define __glibcxx_check_string_len(_String,_Len)
  327. #endif
  328. #endif