functions.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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/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/c++config.h>
  26. #include <bits/stl_iterator_base_types.h> // for iterator_traits, categories and
  27. // _Iter_base
  28. #include <bits/cpp_type_traits.h> // for __is_integer
  29. #include <bits/move.h> // for __addressof and addressof
  30. #include <bits/stl_function.h> // for less
  31. #if __cplusplus >= 201103L
  32. # include <type_traits> // for is_lvalue_reference and __and_
  33. #endif
  34. #include <debug/formatter.h>
  35. namespace __gnu_debug
  36. {
  37. template<typename _Iterator, typename _Sequence>
  38. class _Safe_iterator;
  39. template<typename _Iterator, typename _Sequence>
  40. class _Safe_local_iterator;
  41. template<typename _Sequence>
  42. struct _Insert_range_from_self_is_safe
  43. { enum { __value = 0 }; };
  44. template<typename _Sequence>
  45. struct _Is_contiguous_sequence : std::__false_type { };
  46. // An arbitrary iterator pointer is not singular.
  47. inline bool
  48. __check_singular_aux(const void*) { return false; }
  49. // We may have an iterator that derives from _Safe_iterator_base but isn't
  50. // a _Safe_iterator.
  51. template<typename _Iterator>
  52. inline bool
  53. __check_singular(const _Iterator& __x)
  54. { return __check_singular_aux(&__x); }
  55. /** Non-NULL pointers are nonsingular. */
  56. template<typename _Tp>
  57. inline bool
  58. __check_singular(const _Tp* __ptr)
  59. { return __ptr == 0; }
  60. /** Assume that some arbitrary iterator is dereferenceable, because we
  61. can't prove that it isn't. */
  62. template<typename _Iterator>
  63. inline bool
  64. __check_dereferenceable(const _Iterator&)
  65. { return true; }
  66. /** Non-NULL pointers are dereferenceable. */
  67. template<typename _Tp>
  68. inline bool
  69. __check_dereferenceable(const _Tp* __ptr)
  70. { return __ptr; }
  71. /** Safe iterators know if they are dereferenceable. */
  72. template<typename _Iterator, typename _Sequence>
  73. inline bool
  74. __check_dereferenceable(const _Safe_iterator<_Iterator, _Sequence>& __x)
  75. { return __x._M_dereferenceable(); }
  76. /** Safe local iterators know if they are dereferenceable. */
  77. template<typename _Iterator, typename _Sequence>
  78. inline bool
  79. __check_dereferenceable(const _Safe_local_iterator<_Iterator,
  80. _Sequence>& __x)
  81. { return __x._M_dereferenceable(); }
  82. /** If the distance between two random access iterators is
  83. * nonnegative, assume the range is valid.
  84. */
  85. template<typename _RandomAccessIterator>
  86. inline bool
  87. __valid_range_aux2(const _RandomAccessIterator& __first,
  88. const _RandomAccessIterator& __last,
  89. std::random_access_iterator_tag)
  90. { return __last - __first >= 0; }
  91. /** Can't test for a valid range with input iterators, because
  92. * iteration may be destructive. So we just assume that the range
  93. * is valid.
  94. */
  95. template<typename _InputIterator>
  96. inline bool
  97. __valid_range_aux2(const _InputIterator&, const _InputIterator&,
  98. std::input_iterator_tag)
  99. { return true; }
  100. /** We say that integral types for a valid range, and defer to other
  101. * routines to realize what to do with integral types instead of
  102. * iterators.
  103. */
  104. template<typename _Integral>
  105. inline bool
  106. __valid_range_aux(const _Integral&, const _Integral&, std::__true_type)
  107. { return true; }
  108. /** We have iterators, so figure out what kind of iterators that are
  109. * to see if we can check the range ahead of time.
  110. */
  111. template<typename _InputIterator>
  112. inline bool
  113. __valid_range_aux(const _InputIterator& __first,
  114. const _InputIterator& __last, std::__false_type)
  115. { return __valid_range_aux2(__first, __last,
  116. std::__iterator_category(__first)); }
  117. /** Don't know what these iterators are, or if they are even
  118. * iterators (we may get an integral type for InputIterator), so
  119. * see if they are integral and pass them on to the next phase
  120. * otherwise.
  121. */
  122. template<typename _InputIterator>
  123. inline bool
  124. __valid_range(const _InputIterator& __first, const _InputIterator& __last)
  125. {
  126. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  127. return __valid_range_aux(__first, __last, _Integral());
  128. }
  129. /** Safe iterators know how to check if they form a valid range. */
  130. template<typename _Iterator, typename _Sequence>
  131. inline bool
  132. __valid_range(const _Safe_iterator<_Iterator, _Sequence>& __first,
  133. const _Safe_iterator<_Iterator, _Sequence>& __last)
  134. { return __first._M_valid_range(__last); }
  135. /** Safe local iterators know how to check if they form a valid range. */
  136. template<typename _Iterator, typename _Sequence>
  137. inline bool
  138. __valid_range(const _Safe_local_iterator<_Iterator, _Sequence>& __first,
  139. const _Safe_local_iterator<_Iterator, _Sequence>& __last)
  140. { return __first._M_valid_range(__last); }
  141. /* Checks that [first, last) is a valid range, and then returns
  142. * __first. This routine is useful when we can't use a separate
  143. * assertion statement because, e.g., we are in a constructor.
  144. */
  145. template<typename _InputIterator>
  146. inline _InputIterator
  147. __check_valid_range(const _InputIterator& __first,
  148. const _InputIterator& __last
  149. __attribute__((__unused__)))
  150. {
  151. __glibcxx_check_valid_range(__first, __last);
  152. return __first;
  153. }
  154. /* Handle the case where __other is a pointer to _Sequence::value_type. */
  155. template<typename _Iterator, typename _Sequence>
  156. inline bool
  157. __foreign_iterator_aux4(const _Safe_iterator<_Iterator, _Sequence>& __it,
  158. const typename _Sequence::value_type* __other)
  159. {
  160. typedef const typename _Sequence::value_type* _PointerType;
  161. typedef std::less<_PointerType> _Less;
  162. #if __cplusplus >= 201103L
  163. constexpr _Less __l{};
  164. #else
  165. const _Less __l = _Less();
  166. #endif
  167. const _Sequence* __seq = __it._M_get_sequence();
  168. const _PointerType __begin = std::__addressof(*__seq->_M_base().begin());
  169. const _PointerType __end = std::__addressof(*(__seq->_M_base().end()-1));
  170. // Check whether __other points within the contiguous storage.
  171. return __l(__other, __begin) || __l(__end, __other);
  172. }
  173. /* Fallback overload for when we can't tell, assume it is valid. */
  174. template<typename _Iterator, typename _Sequence>
  175. inline bool
  176. __foreign_iterator_aux4(const _Safe_iterator<_Iterator, _Sequence>&, ...)
  177. { return true; }
  178. /* Handle sequences with contiguous storage */
  179. template<typename _Iterator, typename _Sequence, typename _InputIterator>
  180. inline bool
  181. __foreign_iterator_aux3(const _Safe_iterator<_Iterator, _Sequence>& __it,
  182. const _InputIterator& __other,
  183. const _InputIterator& __other_end,
  184. std::__true_type)
  185. {
  186. if (__other == __other_end)
  187. return true; // inserting nothing is safe even if not foreign iters
  188. if (__it._M_get_sequence()->begin() == __it._M_get_sequence()->end())
  189. return true; // can't be self-inserting if self is empty
  190. return __foreign_iterator_aux4(__it, std::__addressof(*__other));
  191. }
  192. /* Handle non-contiguous containers, assume it is valid. */
  193. template<typename _Iterator, typename _Sequence, typename _InputIterator>
  194. inline bool
  195. __foreign_iterator_aux3(const _Safe_iterator<_Iterator, _Sequence>&,
  196. const _InputIterator&, const _InputIterator&,
  197. std::__false_type)
  198. { return true; }
  199. /** Handle debug iterators from the same type of container. */
  200. template<typename _Iterator, typename _Sequence, typename _OtherIterator>
  201. inline bool
  202. __foreign_iterator_aux2(const _Safe_iterator<_Iterator, _Sequence>& __it,
  203. const _Safe_iterator<_OtherIterator, _Sequence>& __other,
  204. const _Safe_iterator<_OtherIterator, _Sequence>&)
  205. { return __it._M_get_sequence() != __other._M_get_sequence(); }
  206. /** Handle debug iterators from different types of container. */
  207. template<typename _Iterator, typename _Sequence, typename _OtherIterator,
  208. typename _OtherSequence>
  209. inline bool
  210. __foreign_iterator_aux2(const _Safe_iterator<_Iterator, _Sequence>& __it,
  211. const _Safe_iterator<_OtherIterator, _OtherSequence>&,
  212. const _Safe_iterator<_OtherIterator, _OtherSequence>&)
  213. { return true; }
  214. /* Handle non-debug iterators. */
  215. template<typename _Iterator, typename _Sequence, typename _InputIterator>
  216. inline bool
  217. __foreign_iterator_aux2(const _Safe_iterator<_Iterator, _Sequence>& __it,
  218. const _InputIterator& __other,
  219. const _InputIterator& __other_end)
  220. {
  221. #if __cplusplus < 201103L
  222. typedef _Is_contiguous_sequence<_Sequence> __tag;
  223. #else
  224. using __lvalref = std::is_lvalue_reference<
  225. typename std::iterator_traits<_InputIterator>::reference>;
  226. using __contiguous = _Is_contiguous_sequence<_Sequence>;
  227. using __tag = typename std::conditional<__lvalref::value, __contiguous,
  228. std::__false_type>::type;
  229. #endif
  230. return __foreign_iterator_aux3(__it, __other, __other_end, __tag());
  231. }
  232. /* Handle the case where we aren't really inserting a range after all */
  233. template<typename _Iterator, typename _Sequence, typename _Integral>
  234. inline bool
  235. __foreign_iterator_aux(const _Safe_iterator<_Iterator, _Sequence>&,
  236. _Integral, _Integral,
  237. std::__true_type)
  238. { return true; }
  239. /* Handle all iterators. */
  240. template<typename _Iterator, typename _Sequence,
  241. typename _InputIterator>
  242. inline bool
  243. __foreign_iterator_aux(const _Safe_iterator<_Iterator, _Sequence>& __it,
  244. _InputIterator __other, _InputIterator __other_end,
  245. std::__false_type)
  246. {
  247. return _Insert_range_from_self_is_safe<_Sequence>::__value
  248. || __foreign_iterator_aux2(__it, __other, __other_end);
  249. }
  250. template<typename _Iterator, typename _Sequence,
  251. typename _InputIterator>
  252. inline bool
  253. __foreign_iterator(const _Safe_iterator<_Iterator, _Sequence>& __it,
  254. _InputIterator __other, _InputIterator __other_end)
  255. {
  256. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  257. return __foreign_iterator_aux(__it, __other, __other_end, _Integral());
  258. }
  259. /** Checks that __s is non-NULL or __n == 0, and then returns __s. */
  260. template<typename _CharT, typename _Integer>
  261. inline const _CharT*
  262. __check_string(const _CharT* __s,
  263. const _Integer& __n __attribute__((__unused__)))
  264. {
  265. #ifdef _GLIBCXX_DEBUG_PEDANTIC
  266. __glibcxx_assert(__s != 0 || __n == 0);
  267. #endif
  268. return __s;
  269. }
  270. /** Checks that __s is non-NULL and then returns __s. */
  271. template<typename _CharT>
  272. inline const _CharT*
  273. __check_string(const _CharT* __s)
  274. {
  275. #ifdef _GLIBCXX_DEBUG_PEDANTIC
  276. __glibcxx_assert(__s != 0);
  277. #endif
  278. return __s;
  279. }
  280. // Can't check if an input iterator sequence is sorted, because we
  281. // can't step through the sequence.
  282. template<typename _InputIterator>
  283. inline bool
  284. __check_sorted_aux(const _InputIterator&, const _InputIterator&,
  285. std::input_iterator_tag)
  286. { return true; }
  287. // Can verify if a forward iterator sequence is in fact sorted using
  288. // std::__is_sorted
  289. template<typename _ForwardIterator>
  290. inline bool
  291. __check_sorted_aux(_ForwardIterator __first, _ForwardIterator __last,
  292. std::forward_iterator_tag)
  293. {
  294. if (__first == __last)
  295. return true;
  296. _ForwardIterator __next = __first;
  297. for (++__next; __next != __last; __first = __next, ++__next)
  298. if (*__next < *__first)
  299. return false;
  300. return true;
  301. }
  302. // Can't check if an input iterator sequence is sorted, because we can't step
  303. // through the sequence.
  304. template<typename _InputIterator, typename _Predicate>
  305. inline bool
  306. __check_sorted_aux(const _InputIterator&, const _InputIterator&,
  307. _Predicate, std::input_iterator_tag)
  308. { return true; }
  309. // Can verify if a forward iterator sequence is in fact sorted using
  310. // std::__is_sorted
  311. template<typename _ForwardIterator, typename _Predicate>
  312. inline bool
  313. __check_sorted_aux(_ForwardIterator __first, _ForwardIterator __last,
  314. _Predicate __pred, std::forward_iterator_tag)
  315. {
  316. if (__first == __last)
  317. return true;
  318. _ForwardIterator __next = __first;
  319. for (++__next; __next != __last; __first = __next, ++__next)
  320. if (__pred(*__next, *__first))
  321. return false;
  322. return true;
  323. }
  324. // Determine if a sequence is sorted.
  325. template<typename _InputIterator>
  326. inline bool
  327. __check_sorted(const _InputIterator& __first, const _InputIterator& __last)
  328. {
  329. // Verify that the < operator for elements in the sequence is a
  330. // StrictWeakOrdering by checking that it is irreflexive.
  331. __glibcxx_assert(__first == __last || !(*__first < *__first));
  332. return __check_sorted_aux(__first, __last,
  333. std::__iterator_category(__first));
  334. }
  335. template<typename _InputIterator, typename _Predicate>
  336. inline bool
  337. __check_sorted(const _InputIterator& __first, const _InputIterator& __last,
  338. _Predicate __pred)
  339. {
  340. // Verify that the predicate is StrictWeakOrdering by checking that it
  341. // is irreflexive.
  342. __glibcxx_assert(__first == __last || !__pred(*__first, *__first));
  343. return __check_sorted_aux(__first, __last, __pred,
  344. std::__iterator_category(__first));
  345. }
  346. template<typename _InputIterator>
  347. inline bool
  348. __check_sorted_set_aux(const _InputIterator& __first,
  349. const _InputIterator& __last,
  350. std::__true_type)
  351. { return __check_sorted(__first, __last); }
  352. template<typename _InputIterator>
  353. inline bool
  354. __check_sorted_set_aux(const _InputIterator&,
  355. const _InputIterator&,
  356. std::__false_type)
  357. { return true; }
  358. template<typename _InputIterator, typename _Predicate>
  359. inline bool
  360. __check_sorted_set_aux(const _InputIterator& __first,
  361. const _InputIterator& __last,
  362. _Predicate __pred, std::__true_type)
  363. { return __check_sorted(__first, __last, __pred); }
  364. template<typename _InputIterator, typename _Predicate>
  365. inline bool
  366. __check_sorted_set_aux(const _InputIterator&,
  367. const _InputIterator&, _Predicate,
  368. std::__false_type)
  369. { return true; }
  370. // ... special variant used in std::merge, std::includes, std::set_*.
  371. template<typename _InputIterator1, typename _InputIterator2>
  372. inline bool
  373. __check_sorted_set(const _InputIterator1& __first,
  374. const _InputIterator1& __last,
  375. const _InputIterator2&)
  376. {
  377. typedef typename std::iterator_traits<_InputIterator1>::value_type
  378. _ValueType1;
  379. typedef typename std::iterator_traits<_InputIterator2>::value_type
  380. _ValueType2;
  381. typedef typename std::__are_same<_ValueType1, _ValueType2>::__type
  382. _SameType;
  383. return __check_sorted_set_aux(__first, __last, _SameType());
  384. }
  385. template<typename _InputIterator1, typename _InputIterator2,
  386. typename _Predicate>
  387. inline bool
  388. __check_sorted_set(const _InputIterator1& __first,
  389. const _InputIterator1& __last,
  390. const _InputIterator2&, _Predicate __pred)
  391. {
  392. typedef typename std::iterator_traits<_InputIterator1>::value_type
  393. _ValueType1;
  394. typedef typename std::iterator_traits<_InputIterator2>::value_type
  395. _ValueType2;
  396. typedef typename std::__are_same<_ValueType1, _ValueType2>::__type
  397. _SameType;
  398. return __check_sorted_set_aux(__first, __last, __pred, _SameType());
  399. }
  400. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  401. // 270. Binary search requirements overly strict
  402. // Determine if a sequence is partitioned w.r.t. this element.
  403. template<typename _ForwardIterator, typename _Tp>
  404. inline bool
  405. __check_partitioned_lower(_ForwardIterator __first,
  406. _ForwardIterator __last, const _Tp& __value)
  407. {
  408. while (__first != __last && *__first < __value)
  409. ++__first;
  410. if (__first != __last)
  411. {
  412. ++__first;
  413. while (__first != __last && !(*__first < __value))
  414. ++__first;
  415. }
  416. return __first == __last;
  417. }
  418. template<typename _ForwardIterator, typename _Tp>
  419. inline bool
  420. __check_partitioned_upper(_ForwardIterator __first,
  421. _ForwardIterator __last, const _Tp& __value)
  422. {
  423. while (__first != __last && !(__value < *__first))
  424. ++__first;
  425. if (__first != __last)
  426. {
  427. ++__first;
  428. while (__first != __last && __value < *__first)
  429. ++__first;
  430. }
  431. return __first == __last;
  432. }
  433. // Determine if a sequence is partitioned w.r.t. this element.
  434. template<typename _ForwardIterator, typename _Tp, typename _Pred>
  435. inline bool
  436. __check_partitioned_lower(_ForwardIterator __first,
  437. _ForwardIterator __last, const _Tp& __value,
  438. _Pred __pred)
  439. {
  440. while (__first != __last && bool(__pred(*__first, __value)))
  441. ++__first;
  442. if (__first != __last)
  443. {
  444. ++__first;
  445. while (__first != __last && !bool(__pred(*__first, __value)))
  446. ++__first;
  447. }
  448. return __first == __last;
  449. }
  450. template<typename _ForwardIterator, typename _Tp, typename _Pred>
  451. inline bool
  452. __check_partitioned_upper(_ForwardIterator __first,
  453. _ForwardIterator __last, const _Tp& __value,
  454. _Pred __pred)
  455. {
  456. while (__first != __last && !bool(__pred(__value, *__first)))
  457. ++__first;
  458. if (__first != __last)
  459. {
  460. ++__first;
  461. while (__first != __last && bool(__pred(__value, *__first)))
  462. ++__first;
  463. }
  464. return __first == __last;
  465. }
  466. // Helper struct to detect random access safe iterators.
  467. template<typename _Iterator>
  468. struct __is_safe_random_iterator
  469. {
  470. enum { __value = 0 };
  471. typedef std::__false_type __type;
  472. };
  473. template<typename _Iterator, typename _Sequence>
  474. struct __is_safe_random_iterator<_Safe_iterator<_Iterator, _Sequence> >
  475. : std::__are_same<std::random_access_iterator_tag,
  476. typename std::iterator_traits<_Iterator>::
  477. iterator_category>
  478. { };
  479. template<typename _Iterator>
  480. struct _Siter_base
  481. : std::_Iter_base<_Iterator, __is_safe_random_iterator<_Iterator>::__value>
  482. { };
  483. /** Helper function to extract base iterator of random access safe iterator
  484. in order to reduce performance impact of debug mode. Limited to random
  485. access iterator because it is the only category for which it is possible
  486. to check for correct iterators order in the __valid_range function
  487. thanks to the < operator.
  488. */
  489. template<typename _Iterator>
  490. inline typename _Siter_base<_Iterator>::iterator_type
  491. __base(_Iterator __it)
  492. { return _Siter_base<_Iterator>::_S_base(__it); }
  493. } // namespace __gnu_debug
  494. #endif