macros.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. // Debugging support implementation -*- C++ -*-
  2. // Copyright (C) 2003-2018 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. #define __glibcxx_check_valid_range2(_First,_Last,_Dist) \
  50. _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__valid_range(_First, _Last, _Dist), \
  51. _M_message(__gnu_debug::__msg_valid_range) \
  52. ._M_iterator(_First, #_First) \
  53. ._M_iterator(_Last, #_Last))
  54. // Verify that [_First, _Last) forms a non-empty iterator range.
  55. #define __glibcxx_check_non_empty_range(_First,_Last) \
  56. _GLIBCXX_DEBUG_VERIFY(_First != _Last, \
  57. _M_message(__gnu_debug::__msg_non_empty_range) \
  58. ._M_iterator(_First, #_First) \
  59. ._M_iterator(_Last, #_Last))
  60. /** Verify that we can insert into *this with the iterator _Position.
  61. * Insertion into a container at a specific position requires that
  62. * the iterator be nonsingular, either dereferenceable or past-the-end,
  63. * and that it reference the sequence we are inserting into. Note that
  64. * this macro is only valid when the container is a_Safe_sequence and
  65. * the iterator is a _Safe_iterator.
  66. */
  67. #define __glibcxx_check_insert(_Position) \
  68. _GLIBCXX_DEBUG_VERIFY(!_Position._M_singular(), \
  69. _M_message(__gnu_debug::__msg_insert_singular) \
  70. ._M_sequence(*this, "this") \
  71. ._M_iterator(_Position, #_Position)); \
  72. _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this), \
  73. _M_message(__gnu_debug::__msg_insert_different) \
  74. ._M_sequence(*this, "this") \
  75. ._M_iterator(_Position, #_Position))
  76. /** Verify that we can insert into *this after the iterator _Position.
  77. * Insertion into a container after a specific position requires that
  78. * the iterator be nonsingular, either dereferenceable or before-begin,
  79. * and that it reference the sequence we are inserting into. Note that
  80. * this macro is only valid when the container is a_Safe_sequence and
  81. * the iterator is a _Safe_iterator.
  82. */
  83. #define __glibcxx_check_insert_after(_Position) \
  84. __glibcxx_check_insert(_Position); \
  85. _GLIBCXX_DEBUG_VERIFY(!_Position._M_is_end(), \
  86. _M_message(__gnu_debug::__msg_insert_after_end) \
  87. ._M_sequence(*this, "this") \
  88. ._M_iterator(_Position, #_Position))
  89. /** Verify that we can insert the values in the iterator range
  90. * [_First, _Last) into *this with the iterator _Position. Insertion
  91. * into a container at a specific position requires that the iterator
  92. * be nonsingular (i.e., either dereferenceable or past-the-end),
  93. * that it reference the sequence we are inserting into, and that the
  94. * iterator range [_First, _Last) is a valid (possibly empty)
  95. * range which does not reference the sequence we are inserting into.
  96. * Note that this macro is only valid when the container is a
  97. * _Safe_sequence and the _Position iterator is a _Safe_iterator.
  98. */
  99. #define __glibcxx_check_insert_range(_Position,_First,_Last,_Dist) \
  100. __glibcxx_check_valid_range2(_First,_Last,_Dist); \
  101. __glibcxx_check_insert(_Position); \
  102. _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__foreign_iterator(_Position,_First,_Last),\
  103. _M_message(__gnu_debug::__msg_insert_range_from_self)\
  104. ._M_iterator(_First, #_First) \
  105. ._M_iterator(_Last, #_Last) \
  106. ._M_sequence(*this, "this"))
  107. /** Verify that we can insert the values in the iterator range
  108. * [_First, _Last) into *this after the iterator _Position. Insertion
  109. * into a container after a specific position requires that the iterator
  110. * be nonsingular (i.e., either dereferenceable or past-the-end),
  111. * that it reference the sequence we are inserting into, and that the
  112. * iterator range [_First, _Last) is a valid (possibly empty)
  113. * range which does not reference the sequence we are inserting into.
  114. * Note that this macro is only valid when the container is a
  115. * _Safe_sequence and the _Position iterator is a _Safe_iterator.
  116. */
  117. #define __glibcxx_check_insert_range_after(_Position,_First,_Last,_Dist)\
  118. __glibcxx_check_valid_range2(_First,_Last,_Dist); \
  119. __glibcxx_check_insert_after(_Position); \
  120. _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__foreign_iterator(_Position,_First,_Last),\
  121. _M_message(__gnu_debug::__msg_insert_range_from_self)\
  122. ._M_iterator(_First, #_First) \
  123. ._M_iterator(_Last, #_Last) \
  124. ._M_sequence(*this, "this"))
  125. /** Verify that we can erase the element referenced by the iterator
  126. * _Position. We can erase the element if the _Position iterator is
  127. * dereferenceable and references this sequence.
  128. */
  129. #define __glibcxx_check_erase(_Position) \
  130. _GLIBCXX_DEBUG_VERIFY(_Position._M_dereferenceable(), \
  131. _M_message(__gnu_debug::__msg_erase_bad) \
  132. ._M_sequence(*this, "this") \
  133. ._M_iterator(_Position, #_Position)); \
  134. _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this), \
  135. _M_message(__gnu_debug::__msg_erase_different) \
  136. ._M_sequence(*this, "this") \
  137. ._M_iterator(_Position, #_Position))
  138. /** Verify that we can erase the element after the iterator
  139. * _Position. We can erase the element if the _Position iterator is
  140. * before a dereferenceable one and references this sequence.
  141. */
  142. #define __glibcxx_check_erase_after(_Position) \
  143. _GLIBCXX_DEBUG_VERIFY(_Position._M_before_dereferenceable(), \
  144. _M_message(__gnu_debug::__msg_erase_after_bad) \
  145. ._M_sequence(*this, "this") \
  146. ._M_iterator(_Position, #_Position)); \
  147. _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this), \
  148. _M_message(__gnu_debug::__msg_erase_different) \
  149. ._M_sequence(*this, "this") \
  150. ._M_iterator(_Position, #_Position))
  151. /** Verify that we can erase the elements in the iterator range
  152. * [_First, _Last). We can erase the elements if [_First, _Last) is a
  153. * valid iterator range within this sequence.
  154. */
  155. #define __glibcxx_check_erase_range(_First,_Last) \
  156. __glibcxx_check_valid_range(_First,_Last); \
  157. _GLIBCXX_DEBUG_VERIFY(_First._M_attached_to(this), \
  158. _M_message(__gnu_debug::__msg_erase_different) \
  159. ._M_sequence(*this, "this") \
  160. ._M_iterator(_First, #_First) \
  161. ._M_iterator(_Last, #_Last))
  162. /** Verify that we can erase the elements in the iterator range
  163. * (_First, _Last). We can erase the elements if (_First, _Last) is a
  164. * valid iterator range within this sequence.
  165. */
  166. #define __glibcxx_check_erase_range_after(_First,_Last) \
  167. _GLIBCXX_DEBUG_VERIFY(_First._M_can_compare(_Last), \
  168. _M_message(__gnu_debug::__msg_erase_different) \
  169. ._M_sequence(*this, "this") \
  170. ._M_iterator(_First, #_First) \
  171. ._M_iterator(_Last, #_Last)); \
  172. _GLIBCXX_DEBUG_VERIFY(_First._M_attached_to(this), \
  173. _M_message(__gnu_debug::__msg_erase_different) \
  174. ._M_sequence(*this, "this") \
  175. ._M_iterator(_First, #_First)); \
  176. _GLIBCXX_DEBUG_VERIFY(_First != _Last, \
  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(_First._M_incrementable(), \
  182. _M_message(__gnu_debug::__msg_valid_range2) \
  183. ._M_sequence(*this, "this") \
  184. ._M_iterator(_First, #_First) \
  185. ._M_iterator(_Last, #_Last)); \
  186. _GLIBCXX_DEBUG_VERIFY(!_Last._M_is_before_begin(), \
  187. _M_message(__gnu_debug::__msg_valid_range2) \
  188. ._M_sequence(*this, "this") \
  189. ._M_iterator(_First, #_First) \
  190. ._M_iterator(_Last, #_Last)) \
  191. // Verify that the subscript _N is less than the container's size.
  192. #define __glibcxx_check_subscript(_N) \
  193. _GLIBCXX_DEBUG_VERIFY(_N < this->size(), \
  194. _M_message(__gnu_debug::__msg_subscript_oob) \
  195. ._M_sequence(*this, "this") \
  196. ._M_integer(_N, #_N) \
  197. ._M_integer(this->size(), "size"))
  198. // Verify that the bucket _N is less than the container's buckets count.
  199. #define __glibcxx_check_bucket_index(_N) \
  200. _GLIBCXX_DEBUG_VERIFY(_N < this->bucket_count(), \
  201. _M_message(__gnu_debug::__msg_bucket_index_oob) \
  202. ._M_sequence(*this, "this") \
  203. ._M_integer(_N, #_N) \
  204. ._M_integer(this->bucket_count(), "size"))
  205. // Verify that the container is nonempty
  206. #define __glibcxx_check_nonempty() \
  207. _GLIBCXX_DEBUG_VERIFY(! this->empty(), \
  208. _M_message(__gnu_debug::__msg_empty) \
  209. ._M_sequence(*this, "this"))
  210. // Verify that the iterator range [_First, _Last) is sorted
  211. #define __glibcxx_check_sorted(_First,_Last) \
  212. __glibcxx_check_valid_range(_First,_Last); \
  213. _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted( \
  214. __gnu_debug::__base(_First), \
  215. __gnu_debug::__base(_Last)), \
  216. _M_message(__gnu_debug::__msg_unsorted) \
  217. ._M_iterator(_First, #_First) \
  218. ._M_iterator(_Last, #_Last))
  219. /** Verify that the iterator range [_First, _Last) is sorted by the
  220. predicate _Pred. */
  221. #define __glibcxx_check_sorted_pred(_First,_Last,_Pred) \
  222. __glibcxx_check_valid_range(_First,_Last); \
  223. _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted( \
  224. __gnu_debug::__base(_First), \
  225. __gnu_debug::__base(_Last), _Pred), \
  226. _M_message(__gnu_debug::__msg_unsorted_pred) \
  227. ._M_iterator(_First, #_First) \
  228. ._M_iterator(_Last, #_Last) \
  229. ._M_string(#_Pred))
  230. // Special variant for std::merge, std::includes, std::set_*
  231. #define __glibcxx_check_sorted_set(_First1,_Last1,_First2) \
  232. __glibcxx_check_valid_range(_First1,_Last1); \
  233. _GLIBCXX_DEBUG_VERIFY( \
  234. __gnu_debug::__check_sorted_set(__gnu_debug::__base(_First1), \
  235. __gnu_debug::__base(_Last1), _First2),\
  236. _M_message(__gnu_debug::__msg_unsorted) \
  237. ._M_iterator(_First1, #_First1) \
  238. ._M_iterator(_Last1, #_Last1))
  239. // Likewise with a _Pred.
  240. #define __glibcxx_check_sorted_set_pred(_First1,_Last1,_First2,_Pred) \
  241. __glibcxx_check_valid_range(_First1,_Last1); \
  242. _GLIBCXX_DEBUG_VERIFY( \
  243. __gnu_debug::__check_sorted_set(__gnu_debug::__base(_First1), \
  244. __gnu_debug::__base(_Last1), \
  245. _First2, _Pred), \
  246. _M_message(__gnu_debug::__msg_unsorted_pred) \
  247. ._M_iterator(_First1, #_First1) \
  248. ._M_iterator(_Last1, #_Last1) \
  249. ._M_string(#_Pred))
  250. /** Verify that the iterator range [_First, _Last) is partitioned
  251. w.r.t. the value _Value. */
  252. #define __glibcxx_check_partitioned_lower(_First,_Last,_Value) \
  253. __glibcxx_check_valid_range(_First,_Last); \
  254. _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_lower( \
  255. __gnu_debug::__base(_First), \
  256. __gnu_debug::__base(_Last), _Value), \
  257. _M_message(__gnu_debug::__msg_unpartitioned) \
  258. ._M_iterator(_First, #_First) \
  259. ._M_iterator(_Last, #_Last) \
  260. ._M_string(#_Value))
  261. #define __glibcxx_check_partitioned_upper(_First,_Last,_Value) \
  262. __glibcxx_check_valid_range(_First,_Last); \
  263. _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_upper( \
  264. __gnu_debug::__base(_First), \
  265. __gnu_debug::__base(_Last), _Value), \
  266. _M_message(__gnu_debug::__msg_unpartitioned) \
  267. ._M_iterator(_First, #_First) \
  268. ._M_iterator(_Last, #_Last) \
  269. ._M_string(#_Value))
  270. /** Verify that the iterator range [_First, _Last) is partitioned
  271. w.r.t. the value _Value and predicate _Pred. */
  272. #define __glibcxx_check_partitioned_lower_pred(_First,_Last,_Value,_Pred) \
  273. __glibcxx_check_valid_range(_First,_Last); \
  274. _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_lower( \
  275. __gnu_debug::__base(_First), \
  276. __gnu_debug::__base(_Last), _Value, _Pred), \
  277. _M_message(__gnu_debug::__msg_unpartitioned_pred) \
  278. ._M_iterator(_First, #_First) \
  279. ._M_iterator(_Last, #_Last) \
  280. ._M_string(#_Pred) \
  281. ._M_string(#_Value))
  282. /** Verify that the iterator range [_First, _Last) is partitioned
  283. w.r.t. the value _Value and predicate _Pred. */
  284. #define __glibcxx_check_partitioned_upper_pred(_First,_Last,_Value,_Pred) \
  285. __glibcxx_check_valid_range(_First,_Last); \
  286. _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned_upper( \
  287. __gnu_debug::__base(_First), \
  288. __gnu_debug::__base(_Last), _Value, _Pred), \
  289. _M_message(__gnu_debug::__msg_unpartitioned_pred) \
  290. ._M_iterator(_First, #_First) \
  291. ._M_iterator(_Last, #_Last) \
  292. ._M_string(#_Pred) \
  293. ._M_string(#_Value))
  294. // Verify that the iterator range [_First, _Last) is a heap
  295. #define __glibcxx_check_heap(_First,_Last) \
  296. _GLIBCXX_DEBUG_VERIFY(std::__is_heap(__gnu_debug::__base(_First), \
  297. __gnu_debug::__base(_Last)), \
  298. _M_message(__gnu_debug::__msg_not_heap) \
  299. ._M_iterator(_First, #_First) \
  300. ._M_iterator(_Last, #_Last))
  301. /** Verify that the iterator range [_First, _Last) is a heap
  302. w.r.t. the predicate _Pred. */
  303. #define __glibcxx_check_heap_pred(_First,_Last,_Pred) \
  304. _GLIBCXX_DEBUG_VERIFY(std::__is_heap(__gnu_debug::__base(_First), \
  305. __gnu_debug::__base(_Last), \
  306. _Pred), \
  307. _M_message(__gnu_debug::__msg_not_heap_pred) \
  308. ._M_iterator(_First, #_First) \
  309. ._M_iterator(_Last, #_Last) \
  310. ._M_string(#_Pred))
  311. // Verify that the container is not self move assigned
  312. #define __glibcxx_check_self_move_assign(_Other) \
  313. _GLIBCXX_DEBUG_VERIFY(this != &_Other, \
  314. _M_message(__gnu_debug::__msg_self_move_assign) \
  315. ._M_sequence(*this, "this"))
  316. // Verify that load factor is positive
  317. #define __glibcxx_check_max_load_factor(_F) \
  318. _GLIBCXX_DEBUG_VERIFY(_F > 0.0f, \
  319. _M_message(__gnu_debug::__msg_valid_load_factor) \
  320. ._M_sequence(*this, "this"))
  321. #define __glibcxx_check_equal_allocs(_This, _Other) \
  322. _GLIBCXX_DEBUG_VERIFY(_This.get_allocator() == _Other.get_allocator(), \
  323. _M_message(__gnu_debug::__msg_equal_allocs) \
  324. ._M_sequence(_This, "this"))
  325. #define __glibcxx_check_string(_String) _GLIBCXX_DEBUG_PEDASSERT(_String != 0)
  326. #define __glibcxx_check_string_len(_String,_Len) \
  327. _GLIBCXX_DEBUG_PEDASSERT(_String != 0 || _Len == 0)
  328. // Verify that a predicate is irreflexive
  329. #define __glibcxx_check_irreflexive(_First,_Last) \
  330. _GLIBCXX_DEBUG_VERIFY(_First == _Last || !(*_First < *_First), \
  331. _M_message(__gnu_debug::__msg_irreflexive_ordering) \
  332. ._M_iterator_value_type(_First, "< operator type"))
  333. #if __cplusplus >= 201103L
  334. # define __glibcxx_check_irreflexive2(_First,_Last) \
  335. _GLIBCXX_DEBUG_VERIFY(_First == _Last \
  336. || __gnu_debug::__is_irreflexive(_First), \
  337. _M_message(__gnu_debug::__msg_irreflexive_ordering) \
  338. ._M_iterator_value_type(_First, "< operator type"))
  339. #else
  340. # define __glibcxx_check_irreflexive2(_First,_Last)
  341. #endif
  342. #define __glibcxx_check_irreflexive_pred(_First,_Last,_Pred) \
  343. _GLIBCXX_DEBUG_VERIFY(_First == _Last || !_Pred(*_First, *_First), \
  344. _M_message(__gnu_debug::__msg_irreflexive_ordering) \
  345. ._M_instance(_Pred, "functor") \
  346. ._M_iterator_value_type(_First, "ordered type"))
  347. #if __cplusplus >= 201103L
  348. # define __glibcxx_check_irreflexive_pred2(_First,_Last,_Pred) \
  349. _GLIBCXX_DEBUG_VERIFY(_First == _Last \
  350. ||__gnu_debug::__is_irreflexive_pred(_First, _Pred), \
  351. _M_message(__gnu_debug::__msg_irreflexive_ordering) \
  352. ._M_instance(_Pred, "functor") \
  353. ._M_iterator_value_type(_First, "ordered type"))
  354. #else
  355. # define __glibcxx_check_irreflexive_pred2(_First,_Last,_Pred)
  356. #endif
  357. #endif