formatter.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. // Debug-mode error formatting implementation -*- C++ -*-
  2. // Copyright (C) 2003-2021 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/formatter.h
  21. * This file is a GNU debug extension to the Standard C++ Library.
  22. */
  23. #ifndef _GLIBCXX_DEBUG_FORMATTER_H
  24. #define _GLIBCXX_DEBUG_FORMATTER_H 1
  25. #include <bits/c++config.h>
  26. #if __cpp_rtti
  27. # include <typeinfo>
  28. # define _GLIBCXX_TYPEID(_Type) &typeid(_Type)
  29. #else
  30. namespace std
  31. {
  32. class type_info;
  33. }
  34. # define _GLIBCXX_TYPEID(_Type) 0
  35. #endif
  36. #if __cplusplus >= 201103L
  37. namespace __gnu_cxx
  38. {
  39. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  40. template<typename _Iterator, typename _Container>
  41. class __normal_iterator;
  42. _GLIBCXX_END_NAMESPACE_VERSION
  43. }
  44. namespace std
  45. {
  46. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  47. template<typename _Iterator>
  48. class reverse_iterator;
  49. template<typename _Iterator>
  50. class move_iterator;
  51. _GLIBCXX_END_NAMESPACE_VERSION
  52. }
  53. #endif
  54. namespace __gnu_debug
  55. {
  56. using std::type_info;
  57. template<typename _Iterator>
  58. _GLIBCXX_CONSTEXPR
  59. bool __check_singular(_Iterator const&);
  60. class _Safe_sequence_base;
  61. template<typename _Iterator, typename _Sequence, typename _Category>
  62. class _Safe_iterator;
  63. template<typename _Iterator, typename _Sequence>
  64. class _Safe_local_iterator;
  65. template<typename _Sequence>
  66. class _Safe_sequence;
  67. enum _Debug_msg_id
  68. {
  69. // General checks
  70. __msg_valid_range,
  71. __msg_insert_singular,
  72. __msg_insert_different,
  73. __msg_erase_bad,
  74. __msg_erase_different,
  75. __msg_subscript_oob,
  76. __msg_empty,
  77. __msg_unpartitioned,
  78. __msg_unpartitioned_pred,
  79. __msg_unsorted,
  80. __msg_unsorted_pred,
  81. __msg_not_heap,
  82. __msg_not_heap_pred,
  83. // std::bitset checks
  84. __msg_bad_bitset_write,
  85. __msg_bad_bitset_read,
  86. __msg_bad_bitset_flip,
  87. // std::list checks
  88. __msg_self_splice,
  89. __msg_splice_alloc,
  90. __msg_splice_bad,
  91. __msg_splice_other,
  92. __msg_splice_overlap,
  93. // iterator checks
  94. __msg_init_singular,
  95. __msg_init_copy_singular,
  96. __msg_init_const_singular,
  97. __msg_copy_singular,
  98. __msg_bad_deref,
  99. __msg_bad_inc,
  100. __msg_bad_dec,
  101. __msg_iter_subscript_oob,
  102. __msg_advance_oob,
  103. __msg_retreat_oob,
  104. __msg_iter_compare_bad,
  105. __msg_compare_different,
  106. __msg_iter_order_bad,
  107. __msg_order_different,
  108. __msg_distance_bad,
  109. __msg_distance_different,
  110. // istream_iterator
  111. __msg_deref_istream,
  112. __msg_inc_istream,
  113. // ostream_iterator
  114. __msg_output_ostream,
  115. // istreambuf_iterator
  116. __msg_deref_istreambuf,
  117. __msg_inc_istreambuf,
  118. // forward_list
  119. __msg_insert_after_end,
  120. __msg_erase_after_bad,
  121. __msg_valid_range2,
  122. // unordered container local iterators
  123. __msg_local_iter_compare_bad,
  124. __msg_non_empty_range,
  125. // self move assign (no longer used)
  126. __msg_self_move_assign,
  127. // unordered container buckets
  128. __msg_bucket_index_oob,
  129. __msg_valid_load_factor,
  130. // others
  131. __msg_equal_allocs,
  132. __msg_insert_range_from_self,
  133. __msg_irreflexive_ordering
  134. };
  135. class _Error_formatter
  136. {
  137. // Tags denoting the type of parameter for construction
  138. struct _Is_iterator { };
  139. struct _Is_iterator_value_type { };
  140. struct _Is_sequence { };
  141. struct _Is_instance { };
  142. public:
  143. /// Whether an iterator is constant, mutable, or unknown
  144. enum _Constness
  145. {
  146. __unknown_constness,
  147. __const_iterator,
  148. __mutable_iterator,
  149. __last_constness
  150. };
  151. // The state of the iterator (fine-grained), if we know it.
  152. enum _Iterator_state
  153. {
  154. __unknown_state,
  155. __singular, // singular, may still be attached to a sequence
  156. __begin, // dereferenceable, and at the beginning
  157. __middle, // dereferenceable, not at the beginning
  158. __end, // past-the-end, may be at beginning if sequence empty
  159. __before_begin, // before begin
  160. __rbegin, // dereferenceable, and at the reverse-beginning
  161. __rmiddle, // reverse-dereferenceable, not at the reverse-beginning
  162. __rend, // reverse-past-the-end
  163. __last_state
  164. };
  165. // A parameter that may be referenced by an error message
  166. struct _Parameter
  167. {
  168. enum
  169. {
  170. __unused_param,
  171. __iterator,
  172. __sequence,
  173. __integer,
  174. __string,
  175. __instance,
  176. __iterator_value_type
  177. } _M_kind;
  178. struct _Type
  179. {
  180. const char* _M_name;
  181. const type_info* _M_type;
  182. };
  183. struct _Instance : _Type
  184. {
  185. const void* _M_address;
  186. };
  187. union
  188. {
  189. // When _M_kind == __iterator
  190. struct : _Instance
  191. {
  192. _Constness _M_constness;
  193. _Iterator_state _M_state;
  194. const void* _M_sequence;
  195. const type_info* _M_seq_type;
  196. } _M_iterator;
  197. // When _M_kind == __sequence
  198. _Instance _M_sequence;
  199. // When _M_kind == __integer
  200. struct
  201. {
  202. const char* _M_name;
  203. long _M_value;
  204. } _M_integer;
  205. // When _M_kind == __string
  206. struct
  207. {
  208. const char* _M_name;
  209. const char* _M_value;
  210. } _M_string;
  211. // When _M_kind == __instance
  212. _Instance _M_instance;
  213. // When _M_kind == __iterator_value_type
  214. _Type _M_iterator_value_type;
  215. } _M_variant;
  216. _Parameter() : _M_kind(__unused_param), _M_variant() { }
  217. _Parameter(long __value, const char* __name)
  218. : _M_kind(__integer), _M_variant()
  219. {
  220. _M_variant._M_integer._M_name = __name;
  221. _M_variant._M_integer._M_value = __value;
  222. }
  223. _Parameter(const char* __value, const char* __name)
  224. : _M_kind(__string), _M_variant()
  225. {
  226. _M_variant._M_string._M_name = __name;
  227. _M_variant._M_string._M_value = __value;
  228. }
  229. template<typename _Iterator, typename _Sequence, typename _Category>
  230. _Parameter(_Safe_iterator<_Iterator, _Sequence, _Category> const& __it,
  231. const char* __name, _Is_iterator)
  232. : _M_kind(__iterator), _M_variant()
  233. {
  234. _M_variant._M_iterator._M_name = __name;
  235. _M_variant._M_iterator._M_address = std::__addressof(__it);
  236. _M_variant._M_iterator._M_type = _GLIBCXX_TYPEID(_Iterator);
  237. _M_variant._M_iterator._M_constness =
  238. __it._S_constant() ? __const_iterator : __mutable_iterator;
  239. _M_variant._M_iterator._M_sequence = __it._M_get_sequence();
  240. _M_variant._M_iterator._M_seq_type = _GLIBCXX_TYPEID(_Sequence);
  241. if (__it._M_singular())
  242. _M_variant._M_iterator._M_state = __singular;
  243. else
  244. {
  245. if (__it._M_is_before_begin())
  246. _M_variant._M_iterator._M_state = __before_begin;
  247. else if (__it._M_is_end())
  248. _M_variant._M_iterator._M_state = __end;
  249. else if (__it._M_is_begin())
  250. _M_variant._M_iterator._M_state = __begin;
  251. else
  252. _M_variant._M_iterator._M_state = __middle;
  253. }
  254. }
  255. template<typename _Iterator, typename _Sequence>
  256. _Parameter(_Safe_local_iterator<_Iterator, _Sequence> const& __it,
  257. const char* __name, _Is_iterator)
  258. : _M_kind(__iterator), _M_variant()
  259. {
  260. _M_variant._M_iterator._M_name = __name;
  261. _M_variant._M_iterator._M_address = std::__addressof(__it);
  262. _M_variant._M_iterator._M_type = _GLIBCXX_TYPEID(_Iterator);
  263. _M_variant._M_iterator._M_constness =
  264. __it._S_constant() ? __const_iterator : __mutable_iterator;
  265. _M_variant._M_iterator._M_sequence = __it._M_get_sequence();
  266. _M_variant._M_iterator._M_seq_type = _GLIBCXX_TYPEID(_Sequence);
  267. if (__it._M_singular())
  268. _M_variant._M_iterator._M_state = __singular;
  269. else
  270. {
  271. if (__it._M_is_end())
  272. _M_variant._M_iterator._M_state = __end;
  273. else if (__it._M_is_begin())
  274. _M_variant._M_iterator._M_state = __begin;
  275. else
  276. _M_variant._M_iterator._M_state = __middle;
  277. }
  278. }
  279. template<typename _Type>
  280. _Parameter(const _Type* const& __it, const char* __name, _Is_iterator)
  281. : _M_kind(__iterator), _M_variant()
  282. {
  283. _M_variant._M_iterator._M_name = __name;
  284. _M_variant._M_iterator._M_address = std::__addressof(__it);
  285. _M_variant._M_iterator._M_type = _GLIBCXX_TYPEID(__it);
  286. _M_variant._M_iterator._M_constness = __const_iterator;
  287. _M_variant._M_iterator._M_state = __it ? __unknown_state : __singular;
  288. _M_variant._M_iterator._M_sequence = 0;
  289. _M_variant._M_iterator._M_seq_type = 0;
  290. }
  291. template<typename _Type>
  292. _Parameter(_Type* const& __it, const char* __name, _Is_iterator)
  293. : _M_kind(__iterator), _M_variant()
  294. {
  295. _M_variant._M_iterator._M_name = __name;
  296. _M_variant._M_iterator._M_address = std::__addressof(__it);
  297. _M_variant._M_iterator._M_type = _GLIBCXX_TYPEID(__it);
  298. _M_variant._M_iterator._M_constness = __mutable_iterator;
  299. _M_variant._M_iterator._M_state = __it ? __unknown_state : __singular;
  300. _M_variant._M_iterator._M_sequence = 0;
  301. _M_variant._M_iterator._M_seq_type = 0;
  302. }
  303. template<typename _Iterator>
  304. _Parameter(_Iterator const& __it, const char* __name, _Is_iterator)
  305. : _M_kind(__iterator), _M_variant()
  306. {
  307. _M_variant._M_iterator._M_name = __name;
  308. _M_variant._M_iterator._M_address = std::__addressof(__it);
  309. _M_variant._M_iterator._M_type = _GLIBCXX_TYPEID(__it);
  310. _M_variant._M_iterator._M_constness = __unknown_constness;
  311. _M_variant._M_iterator._M_state =
  312. __gnu_debug::__check_singular(__it) ? __singular : __unknown_state;
  313. _M_variant._M_iterator._M_sequence = 0;
  314. _M_variant._M_iterator._M_seq_type = 0;
  315. }
  316. #if __cplusplus >= 201103L
  317. // The following constructors are only defined in C++11 to take
  318. // advantage of the constructor delegation feature.
  319. template<typename _Iterator, typename _Container>
  320. _Parameter(
  321. __gnu_cxx::__normal_iterator<_Iterator, _Container> const& __it,
  322. const char* __name, _Is_iterator)
  323. : _Parameter(__it.base(), __name, _Is_iterator{})
  324. { _M_variant._M_iterator._M_type = _GLIBCXX_TYPEID(__it); }
  325. template<typename _Iterator>
  326. _Parameter(std::reverse_iterator<_Iterator> const& __it,
  327. const char* __name, _Is_iterator)
  328. : _Parameter(__it.base(), __name, _Is_iterator{})
  329. {
  330. _M_variant._M_iterator._M_type = _GLIBCXX_TYPEID(__it);
  331. _M_variant._M_iterator._M_state
  332. = _S_reverse_state(_M_variant._M_iterator._M_state);
  333. }
  334. template<typename _Iterator, typename _Sequence, typename _Category>
  335. _Parameter(std::reverse_iterator<_Safe_iterator<_Iterator, _Sequence,
  336. _Category>> const& __it,
  337. const char* __name, _Is_iterator)
  338. : _Parameter(__it.base(), __name, _Is_iterator{})
  339. {
  340. _M_variant._M_iterator._M_type
  341. = _GLIBCXX_TYPEID(std::reverse_iterator<_Iterator>);
  342. _M_variant._M_iterator._M_state
  343. = _S_reverse_state(_M_variant._M_iterator._M_state);
  344. }
  345. template<typename _Iterator>
  346. _Parameter(std::move_iterator<_Iterator> const& __it,
  347. const char* __name, _Is_iterator)
  348. : _Parameter(__it.base(), __name, _Is_iterator{})
  349. { _M_variant._M_iterator._M_type = _GLIBCXX_TYPEID(__it); }
  350. template<typename _Iterator, typename _Sequence, typename _Category>
  351. _Parameter(std::move_iterator<_Safe_iterator<_Iterator, _Sequence,
  352. _Category>> const& __it,
  353. const char* __name, _Is_iterator)
  354. : _Parameter(__it.base(), __name, _Is_iterator{})
  355. {
  356. _M_variant._M_iterator._M_type
  357. = _GLIBCXX_TYPEID(std::move_iterator<_Iterator>);
  358. }
  359. private:
  360. _Iterator_state
  361. _S_reverse_state(_Iterator_state __state)
  362. {
  363. switch (__state)
  364. {
  365. case __begin:
  366. return __rend;
  367. case __middle:
  368. return __rmiddle;
  369. case __end:
  370. return __rbegin;
  371. default:
  372. return __state;
  373. }
  374. }
  375. public:
  376. #endif
  377. template<typename _Sequence>
  378. _Parameter(const _Safe_sequence<_Sequence>& __seq,
  379. const char* __name, _Is_sequence)
  380. : _M_kind(__sequence), _M_variant()
  381. {
  382. _M_variant._M_sequence._M_name = __name;
  383. _M_variant._M_sequence._M_address =
  384. static_cast<const _Sequence*>(std::__addressof(__seq));
  385. _M_variant._M_sequence._M_type = _GLIBCXX_TYPEID(_Sequence);
  386. }
  387. template<typename _Sequence>
  388. _Parameter(const _Sequence& __seq, const char* __name, _Is_sequence)
  389. : _M_kind(__sequence), _M_variant()
  390. {
  391. _M_variant._M_sequence._M_name = __name;
  392. _M_variant._M_sequence._M_address = std::__addressof(__seq);
  393. _M_variant._M_sequence._M_type = _GLIBCXX_TYPEID(_Sequence);
  394. }
  395. template<typename _Iterator>
  396. _Parameter(const _Iterator& __it, const char* __name,
  397. _Is_iterator_value_type)
  398. : _M_kind(__iterator_value_type), _M_variant()
  399. {
  400. _M_variant._M_iterator_value_type._M_name = __name;
  401. _M_variant._M_iterator_value_type._M_type =
  402. _GLIBCXX_TYPEID(typename std::iterator_traits<_Iterator>::value_type);
  403. }
  404. template<typename _Type>
  405. _Parameter(const _Type& __inst, const char* __name, _Is_instance)
  406. : _M_kind(__instance), _M_variant()
  407. {
  408. _M_variant._M_instance._M_name = __name;
  409. _M_variant._M_instance._M_address = &__inst;
  410. _M_variant._M_instance._M_type = _GLIBCXX_TYPEID(_Type);
  411. }
  412. #if !_GLIBCXX_INLINE_VERSION
  413. void
  414. _M_print_field(const _Error_formatter* __formatter,
  415. const char* __name) const _GLIBCXX_DEPRECATED;
  416. void
  417. _M_print_description(const _Error_formatter* __formatter)
  418. const _GLIBCXX_DEPRECATED;
  419. #endif
  420. };
  421. template<typename _Iterator>
  422. _Error_formatter&
  423. _M_iterator(const _Iterator& __it, const char* __name = 0)
  424. {
  425. if (_M_num_parameters < std::size_t(__max_parameters))
  426. _M_parameters[_M_num_parameters++] = _Parameter(__it, __name,
  427. _Is_iterator());
  428. return *this;
  429. }
  430. template<typename _Iterator>
  431. _Error_formatter&
  432. _M_iterator_value_type(const _Iterator& __it,
  433. const char* __name = 0)
  434. {
  435. if (_M_num_parameters < __max_parameters)
  436. _M_parameters[_M_num_parameters++] =
  437. _Parameter(__it, __name, _Is_iterator_value_type());
  438. return *this;
  439. }
  440. _Error_formatter&
  441. _M_integer(long __value, const char* __name = 0)
  442. {
  443. if (_M_num_parameters < __max_parameters)
  444. _M_parameters[_M_num_parameters++] = _Parameter(__value, __name);
  445. return *this;
  446. }
  447. _Error_formatter&
  448. _M_string(const char* __value, const char* __name = 0)
  449. {
  450. if (_M_num_parameters < __max_parameters)
  451. _M_parameters[_M_num_parameters++] = _Parameter(__value, __name);
  452. return *this;
  453. }
  454. template<typename _Sequence>
  455. _Error_formatter&
  456. _M_sequence(const _Sequence& __seq, const char* __name = 0)
  457. {
  458. if (_M_num_parameters < __max_parameters)
  459. _M_parameters[_M_num_parameters++] = _Parameter(__seq, __name,
  460. _Is_sequence());
  461. return *this;
  462. }
  463. template<typename _Type>
  464. _Error_formatter&
  465. _M_instance(const _Type& __inst, const char* __name = 0)
  466. {
  467. if (_M_num_parameters < __max_parameters)
  468. _M_parameters[_M_num_parameters++] = _Parameter(__inst, __name,
  469. _Is_instance());
  470. return *this;
  471. }
  472. _Error_formatter&
  473. _M_message(const char* __text)
  474. { _M_text = __text; return *this; }
  475. // Kept const qualifier for backward compatibility, to keep the same
  476. // exported symbol.
  477. _Error_formatter&
  478. _M_message(_Debug_msg_id __id) const throw ();
  479. _GLIBCXX_NORETURN void
  480. _M_error() const;
  481. #if !_GLIBCXX_INLINE_VERSION
  482. template<typename _Tp>
  483. void
  484. _M_format_word(char*, int, const char*, _Tp)
  485. const throw () _GLIBCXX_DEPRECATED;
  486. void
  487. _M_print_word(const char* __word) const _GLIBCXX_DEPRECATED;
  488. void
  489. _M_print_string(const char* __string) const _GLIBCXX_DEPRECATED;
  490. #endif
  491. private:
  492. _Error_formatter(const char* __file, unsigned int __line,
  493. const char* __function)
  494. : _M_file(__file), _M_line(__line), _M_num_parameters(0), _M_text(0)
  495. , _M_function(__function)
  496. { }
  497. #if !_GLIBCXX_INLINE_VERSION
  498. void
  499. _M_get_max_length() const throw () _GLIBCXX_DEPRECATED;
  500. #endif
  501. enum { __max_parameters = 9 };
  502. const char* _M_file;
  503. unsigned int _M_line;
  504. _Parameter _M_parameters[__max_parameters];
  505. unsigned int _M_num_parameters;
  506. const char* _M_text;
  507. const char* _M_function;
  508. public:
  509. static _Error_formatter&
  510. _S_at(const char* __file, unsigned int __line, const char* __function)
  511. {
  512. static _Error_formatter __formatter(__file, __line, __function);
  513. return __formatter;
  514. }
  515. };
  516. } // namespace __gnu_debug
  517. #undef _GLIBCXX_TYPEID
  518. #endif