functions.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. // Debugging support implementation -*- C++ -*-
  2. // Copyright (C) 2003-2019 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/functions.h
  21. * This file is a GNU debug extension to the Standard C++ Library.
  22. */
  23. #ifndef _GLIBCXX_DEBUG_FUNCTIONS_H
  24. #define _GLIBCXX_DEBUG_FUNCTIONS_H 1
  25. #include <bits/move.h> // for __addressof
  26. #include <bits/stl_function.h> // for less
  27. #if __cplusplus >= 201103L
  28. # include <bits/stl_iterator.h> // for __miter_base
  29. # include <type_traits> // for is_lvalue_reference and conditional.
  30. #endif
  31. #include <debug/helper_functions.h>
  32. #include <debug/formatter.h>
  33. namespace __gnu_debug
  34. {
  35. template<typename _Sequence>
  36. struct _Insert_range_from_self_is_safe
  37. { enum { __value = 0 }; };
  38. template<typename _Sequence>
  39. struct _Is_contiguous_sequence : std::__false_type { };
  40. // An arbitrary iterator pointer is not singular.
  41. inline bool
  42. __check_singular_aux(const void*) { return false; }
  43. // We may have an iterator that derives from _Safe_iterator_base but isn't
  44. // a _Safe_iterator.
  45. template<typename _Iterator>
  46. inline bool
  47. __check_singular(const _Iterator& __x)
  48. { return __check_singular_aux(std::__addressof(__x)); }
  49. /** Non-NULL pointers are nonsingular. */
  50. template<typename _Tp>
  51. inline bool
  52. __check_singular(const _Tp* __ptr)
  53. { return __ptr == 0; }
  54. /* Checks that [first, last) is a valid range, and then returns
  55. * __first. This routine is useful when we can't use a separate
  56. * assertion statement because, e.g., we are in a constructor.
  57. */
  58. template<typename _InputIterator>
  59. inline _InputIterator
  60. __check_valid_range(const _InputIterator& __first,
  61. const _InputIterator& __last,
  62. const char* __file,
  63. unsigned int __line,
  64. const char* __function)
  65. {
  66. __glibcxx_check_valid_range_at(__first, __last,
  67. __file, __line, __function);
  68. return __first;
  69. }
  70. /* Handle the case where __other is a pointer to _Sequence::value_type. */
  71. template<typename _Iterator, typename _Sequence, typename _Category>
  72. inline bool
  73. __foreign_iterator_aux4(
  74. const _Safe_iterator<_Iterator, _Sequence, _Category>& __it,
  75. const typename _Sequence::value_type* __other)
  76. {
  77. typedef const typename _Sequence::value_type* _PointerType;
  78. typedef std::less<_PointerType> _Less;
  79. #if __cplusplus >= 201103L
  80. constexpr _Less __l{};
  81. #else
  82. const _Less __l = _Less();
  83. #endif
  84. const _Sequence* __seq = __it._M_get_sequence();
  85. const _PointerType __begin = std::__addressof(*__seq->_M_base().begin());
  86. const _PointerType __end = std::__addressof(*(__seq->_M_base().end()-1));
  87. // Check whether __other points within the contiguous storage.
  88. return __l(__other, __begin) || __l(__end, __other);
  89. }
  90. /* Fallback overload for when we can't tell, assume it is valid. */
  91. template<typename _Iterator, typename _Sequence, typename _Category>
  92. inline bool
  93. __foreign_iterator_aux4(
  94. const _Safe_iterator<_Iterator, _Sequence, _Category>&, ...)
  95. { return true; }
  96. /* Handle sequences with contiguous storage */
  97. template<typename _Iterator, typename _Sequence, typename _Category,
  98. typename _InputIterator>
  99. inline bool
  100. __foreign_iterator_aux3(
  101. const _Safe_iterator<_Iterator, _Sequence, _Category>& __it,
  102. const _InputIterator& __other, const _InputIterator& __other_end,
  103. std::__true_type)
  104. {
  105. if (__other == __other_end)
  106. return true; // inserting nothing is safe even if not foreign iters
  107. if (__it._M_get_sequence()->empty())
  108. return true; // can't be self-inserting if self is empty
  109. return __foreign_iterator_aux4(__it, std::__addressof(*__other));
  110. }
  111. /* Handle non-contiguous containers, assume it is valid. */
  112. template<typename _Iterator, typename _Sequence, typename _Category,
  113. typename _InputIterator>
  114. inline bool
  115. __foreign_iterator_aux3(
  116. const _Safe_iterator<_Iterator, _Sequence, _Category>&,
  117. const _InputIterator&, const _InputIterator&,
  118. std::__false_type)
  119. { return true; }
  120. /** Handle debug iterators from the same type of container. */
  121. template<typename _Iterator, typename _Sequence, typename _Category,
  122. typename _OtherIterator>
  123. inline bool
  124. __foreign_iterator_aux2(
  125. const _Safe_iterator<_Iterator, _Sequence, _Category>& __it,
  126. const _Safe_iterator<_OtherIterator, _Sequence, _Category>& __other,
  127. const _Safe_iterator<_OtherIterator, _Sequence, _Category>&)
  128. { return __it._M_get_sequence() != __other._M_get_sequence(); }
  129. /** Handle debug iterators from different types of container. */
  130. template<typename _Iterator, typename _Sequence, typename _Category,
  131. typename _OtherIterator, typename _OtherSequence,
  132. typename _OtherCategory>
  133. inline bool
  134. __foreign_iterator_aux2(
  135. const _Safe_iterator<_Iterator, _Sequence, _Category>&,
  136. const _Safe_iterator<_OtherIterator, _OtherSequence,
  137. _OtherCategory>&,
  138. const _Safe_iterator<_OtherIterator, _OtherSequence,
  139. _OtherCategory>&)
  140. { return true; }
  141. /* Handle non-debug iterators. */
  142. template<typename _Iterator, typename _Sequence, typename _Category,
  143. typename _InputIterator>
  144. inline bool
  145. __foreign_iterator_aux2(
  146. const _Safe_iterator<_Iterator, _Sequence, _Category>& __it,
  147. const _InputIterator& __other,
  148. const _InputIterator& __other_end)
  149. {
  150. #if __cplusplus < 201103L
  151. typedef _Is_contiguous_sequence<_Sequence> __tag;
  152. #else
  153. using __lvalref = std::is_lvalue_reference<
  154. typename std::iterator_traits<_InputIterator>::reference>;
  155. using __contiguous = _Is_contiguous_sequence<_Sequence>;
  156. using __tag = typename std::conditional<__lvalref::value, __contiguous,
  157. std::__false_type>::type;
  158. #endif
  159. return __foreign_iterator_aux3(__it, __other, __other_end, __tag());
  160. }
  161. /* Handle the case where we aren't really inserting a range after all */
  162. template<typename _Iterator, typename _Sequence, typename _Category,
  163. typename _Integral>
  164. inline bool
  165. __foreign_iterator_aux(
  166. const _Safe_iterator<_Iterator, _Sequence, _Category>&,
  167. _Integral, _Integral, std::__true_type)
  168. { return true; }
  169. /* Handle all iterators. */
  170. template<typename _Iterator, typename _Sequence, typename _Category,
  171. typename _InputIterator>
  172. inline bool
  173. __foreign_iterator_aux(
  174. const _Safe_iterator<_Iterator, _Sequence, _Category>& __it,
  175. _InputIterator __other, _InputIterator __other_end,
  176. std::__false_type)
  177. {
  178. return _Insert_range_from_self_is_safe<_Sequence>::__value
  179. || __foreign_iterator_aux2(__it, std::__miter_base(__other),
  180. std::__miter_base(__other_end));
  181. }
  182. template<typename _Iterator, typename _Sequence, typename _Category,
  183. typename _InputIterator>
  184. inline bool
  185. __foreign_iterator(
  186. const _Safe_iterator<_Iterator, _Sequence, _Category>& __it,
  187. _InputIterator __other, _InputIterator __other_end)
  188. {
  189. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  190. return __foreign_iterator_aux(__it, __other, __other_end, _Integral());
  191. }
  192. // Can't check if an input iterator sequence is sorted, because we
  193. // can't step through the sequence.
  194. template<typename _InputIterator>
  195. inline bool
  196. __check_sorted_aux(const _InputIterator&, const _InputIterator&,
  197. std::input_iterator_tag)
  198. { return true; }
  199. // Can verify if a forward iterator sequence is in fact sorted using
  200. // std::__is_sorted
  201. template<typename _ForwardIterator>
  202. inline bool
  203. __check_sorted_aux(_ForwardIterator __first, _ForwardIterator __last,
  204. std::forward_iterator_tag)
  205. {
  206. if (__first == __last)
  207. return true;
  208. _ForwardIterator __next = __first;
  209. for (++__next; __next != __last; __first = __next, (void)++__next)
  210. if (*__next < *__first)
  211. return false;
  212. return true;
  213. }
  214. // Can't check if an input iterator sequence is sorted, because we can't step
  215. // through the sequence.
  216. template<typename _InputIterator, typename _Predicate>
  217. inline bool
  218. __check_sorted_aux(const _InputIterator&, const _InputIterator&,
  219. _Predicate, std::input_iterator_tag)
  220. { return true; }
  221. // Can verify if a forward iterator sequence is in fact sorted using
  222. // std::__is_sorted
  223. template<typename _ForwardIterator, typename _Predicate>
  224. inline bool
  225. __check_sorted_aux(_ForwardIterator __first, _ForwardIterator __last,
  226. _Predicate __pred, std::forward_iterator_tag)
  227. {
  228. if (__first == __last)
  229. return true;
  230. _ForwardIterator __next = __first;
  231. for (++__next; __next != __last; __first = __next, (void)++__next)
  232. if (__pred(*__next, *__first))
  233. return false;
  234. return true;
  235. }
  236. // Determine if a sequence is sorted.
  237. template<typename _InputIterator>
  238. inline bool
  239. __check_sorted(const _InputIterator& __first, const _InputIterator& __last)
  240. {
  241. // Verify that the < operator for elements in the sequence is a
  242. // StrictWeakOrdering by checking that it is irreflexive.
  243. __glibcxx_assert(__first == __last || !(*__first < *__first));
  244. return __check_sorted_aux(__first, __last,
  245. std::__iterator_category(__first));
  246. }
  247. template<typename _InputIterator, typename _Predicate>
  248. inline bool
  249. __check_sorted(const _InputIterator& __first, const _InputIterator& __last,
  250. _Predicate __pred)
  251. {
  252. // Verify that the predicate is StrictWeakOrdering by checking that it
  253. // is irreflexive.
  254. __glibcxx_assert(__first == __last || !__pred(*__first, *__first));
  255. return __check_sorted_aux(__first, __last, __pred,
  256. std::__iterator_category(__first));
  257. }
  258. template<typename _InputIterator>
  259. inline bool
  260. __check_sorted_set_aux(const _InputIterator& __first,
  261. const _InputIterator& __last,
  262. std::__true_type)
  263. { return __check_sorted(__first, __last); }
  264. template<typename _InputIterator>
  265. inline bool
  266. __check_sorted_set_aux(const _InputIterator&,
  267. const _InputIterator&,
  268. std::__false_type)
  269. { return true; }
  270. template<typename _InputIterator, typename _Predicate>
  271. inline bool
  272. __check_sorted_set_aux(const _InputIterator& __first,
  273. const _InputIterator& __last,
  274. _Predicate __pred, std::__true_type)
  275. { return __check_sorted(__first, __last, __pred); }
  276. template<typename _InputIterator, typename _Predicate>
  277. inline bool
  278. __check_sorted_set_aux(const _InputIterator&,
  279. const _InputIterator&, _Predicate,
  280. std::__false_type)
  281. { return true; }
  282. // ... special variant used in std::merge, std::includes, std::set_*.
  283. template<typename _InputIterator1, typename _InputIterator2>
  284. inline bool
  285. __check_sorted_set(const _InputIterator1& __first,
  286. const _InputIterator1& __last,
  287. const _InputIterator2&)
  288. {
  289. typedef typename std::iterator_traits<_InputIterator1>::value_type
  290. _ValueType1;
  291. typedef typename std::iterator_traits<_InputIterator2>::value_type
  292. _ValueType2;
  293. typedef typename std::__are_same<_ValueType1, _ValueType2>::__type
  294. _SameType;
  295. return __check_sorted_set_aux(__first, __last, _SameType());
  296. }
  297. template<typename _InputIterator1, typename _InputIterator2,
  298. typename _Predicate>
  299. inline bool
  300. __check_sorted_set(const _InputIterator1& __first,
  301. const _InputIterator1& __last,
  302. const _InputIterator2&, _Predicate __pred)
  303. {
  304. typedef typename std::iterator_traits<_InputIterator1>::value_type
  305. _ValueType1;
  306. typedef typename std::iterator_traits<_InputIterator2>::value_type
  307. _ValueType2;
  308. typedef typename std::__are_same<_ValueType1, _ValueType2>::__type
  309. _SameType;
  310. return __check_sorted_set_aux(__first, __last, __pred, _SameType());
  311. }
  312. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  313. // 270. Binary search requirements overly strict
  314. // Determine if a sequence is partitioned w.r.t. this element.
  315. template<typename _ForwardIterator, typename _Tp>
  316. inline bool
  317. __check_partitioned_lower(_ForwardIterator __first,
  318. _ForwardIterator __last, const _Tp& __value)
  319. {
  320. while (__first != __last && *__first < __value)
  321. ++__first;
  322. if (__first != __last)
  323. {
  324. ++__first;
  325. while (__first != __last && !(*__first < __value))
  326. ++__first;
  327. }
  328. return __first == __last;
  329. }
  330. template<typename _ForwardIterator, typename _Tp>
  331. inline bool
  332. __check_partitioned_upper(_ForwardIterator __first,
  333. _ForwardIterator __last, const _Tp& __value)
  334. {
  335. while (__first != __last && !(__value < *__first))
  336. ++__first;
  337. if (__first != __last)
  338. {
  339. ++__first;
  340. while (__first != __last && __value < *__first)
  341. ++__first;
  342. }
  343. return __first == __last;
  344. }
  345. // Determine if a sequence is partitioned w.r.t. this element.
  346. template<typename _ForwardIterator, typename _Tp, typename _Pred>
  347. inline bool
  348. __check_partitioned_lower(_ForwardIterator __first,
  349. _ForwardIterator __last, const _Tp& __value,
  350. _Pred __pred)
  351. {
  352. while (__first != __last && bool(__pred(*__first, __value)))
  353. ++__first;
  354. if (__first != __last)
  355. {
  356. ++__first;
  357. while (__first != __last && !bool(__pred(*__first, __value)))
  358. ++__first;
  359. }
  360. return __first == __last;
  361. }
  362. template<typename _ForwardIterator, typename _Tp, typename _Pred>
  363. inline bool
  364. __check_partitioned_upper(_ForwardIterator __first,
  365. _ForwardIterator __last, const _Tp& __value,
  366. _Pred __pred)
  367. {
  368. while (__first != __last && !bool(__pred(__value, *__first)))
  369. ++__first;
  370. if (__first != __last)
  371. {
  372. ++__first;
  373. while (__first != __last && bool(__pred(__value, *__first)))
  374. ++__first;
  375. }
  376. return __first == __last;
  377. }
  378. #if __cplusplus >= 201103L
  379. struct _Irreflexive_checker
  380. {
  381. template<typename _It>
  382. static typename std::iterator_traits<_It>::reference
  383. __deref();
  384. template<typename _It,
  385. typename = decltype(__deref<_It>() < __deref<_It>())>
  386. static bool
  387. _S_is_valid(_It __it)
  388. { return !(*__it < *__it); }
  389. // Fallback method if operator doesn't exist.
  390. template<typename... _Args>
  391. static bool
  392. _S_is_valid(_Args...)
  393. { return true; }
  394. template<typename _It, typename _Pred, typename
  395. = decltype(std::declval<_Pred>()(__deref<_It>(), __deref<_It>()))>
  396. static bool
  397. _S_is_valid_pred(_It __it, _Pred __pred)
  398. { return !__pred(*__it, *__it); }
  399. // Fallback method if predicate can't be invoked.
  400. template<typename... _Args>
  401. static bool
  402. _S_is_valid_pred(_Args...)
  403. { return true; }
  404. };
  405. template<typename _Iterator>
  406. inline bool
  407. __is_irreflexive(_Iterator __it)
  408. { return _Irreflexive_checker::_S_is_valid(__it); }
  409. template<typename _Iterator, typename _Pred>
  410. inline bool
  411. __is_irreflexive_pred(_Iterator __it, _Pred __pred)
  412. { return _Irreflexive_checker::_S_is_valid_pred(__it, __pred); }
  413. #endif
  414. } // namespace __gnu_debug
  415. #endif