formatter.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. // Debug-mode error formatting implementation -*- C++ -*-
  2. // Copyright (C) 2003-2022 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 _Named
  179. {
  180. const char* _M_name;
  181. };
  182. struct _Type : _Named
  183. {
  184. const type_info* _M_type;
  185. };
  186. struct _Instance : _Type
  187. {
  188. const void* _M_address;
  189. };
  190. union
  191. {
  192. // When _M_kind == __iterator
  193. struct : _Instance
  194. {
  195. _Constness _M_constness;
  196. _Iterator_state _M_state;
  197. const void* _M_sequence;
  198. const type_info* _M_seq_type;
  199. } _M_iterator;
  200. // When _M_kind == __sequence
  201. _Instance _M_sequence;
  202. // When _M_kind == __integer
  203. struct : _Named
  204. {
  205. long _M_value;
  206. } _M_integer;
  207. // When _M_kind == __string
  208. struct : _Named
  209. {
  210. const char* _M_value;
  211. } _M_string;
  212. // When _M_kind == __instance
  213. _Instance _M_instance;
  214. // When _M_kind == __iterator_value_type
  215. _Type _M_iterator_value_type;
  216. } _M_variant;
  217. _Parameter() : _M_kind(__unused_param), _M_variant() { }
  218. _Parameter(long __value, const char* __name)
  219. : _M_kind(__integer), _M_variant()
  220. {
  221. _M_variant._M_integer._M_name = __name;
  222. _M_variant._M_integer._M_value = __value;
  223. }
  224. _Parameter(const char* __value, const char* __name)
  225. : _M_kind(__string), _M_variant()
  226. {
  227. _M_variant._M_string._M_name = __name;
  228. _M_variant._M_string._M_value = __value;
  229. }
  230. template<typename _Iterator, typename _Sequence, typename _Category>
  231. _Parameter(_Safe_iterator<_Iterator, _Sequence, _Category> const& __it,
  232. const char* __name, _Is_iterator)
  233. : _M_kind(__iterator), _M_variant()
  234. {
  235. _M_variant._M_iterator._M_name = __name;
  236. _M_variant._M_iterator._M_address = std::__addressof(__it);
  237. _M_variant._M_iterator._M_type = _GLIBCXX_TYPEID(_Iterator);
  238. _M_variant._M_iterator._M_constness =
  239. __it._S_constant() ? __const_iterator : __mutable_iterator;
  240. _M_variant._M_iterator._M_sequence = __it._M_get_sequence();
  241. _M_variant._M_iterator._M_seq_type = _GLIBCXX_TYPEID(_Sequence);
  242. if (__it._M_singular())
  243. _M_variant._M_iterator._M_state = __singular;
  244. else
  245. {
  246. if (__it._M_is_before_begin())
  247. _M_variant._M_iterator._M_state = __before_begin;
  248. else if (__it._M_is_end())
  249. _M_variant._M_iterator._M_state = __end;
  250. else if (__it._M_is_begin())
  251. _M_variant._M_iterator._M_state = __begin;
  252. else
  253. _M_variant._M_iterator._M_state = __middle;
  254. }
  255. }
  256. template<typename _Iterator, typename _Sequence>
  257. _Parameter(_Safe_local_iterator<_Iterator, _Sequence> const& __it,
  258. const char* __name, _Is_iterator)
  259. : _M_kind(__iterator), _M_variant()
  260. {
  261. _M_variant._M_iterator._M_name = __name;
  262. _M_variant._M_iterator._M_address = std::__addressof(__it);
  263. _M_variant._M_iterator._M_type = _GLIBCXX_TYPEID(_Iterator);
  264. _M_variant._M_iterator._M_constness =
  265. __it._S_constant() ? __const_iterator : __mutable_iterator;
  266. _M_variant._M_iterator._M_sequence = __it._M_get_sequence();
  267. _M_variant._M_iterator._M_seq_type = _GLIBCXX_TYPEID(_Sequence);
  268. if (__it._M_singular())
  269. _M_variant._M_iterator._M_state = __singular;
  270. else
  271. {
  272. if (__it._M_is_end())
  273. _M_variant._M_iterator._M_state = __end;
  274. else if (__it._M_is_begin())
  275. _M_variant._M_iterator._M_state = __begin;
  276. else
  277. _M_variant._M_iterator._M_state = __middle;
  278. }
  279. }
  280. template<typename _Type>
  281. _Parameter(const _Type* const& __it, const char* __name, _Is_iterator)
  282. : _M_kind(__iterator), _M_variant()
  283. {
  284. _M_variant._M_iterator._M_name = __name;
  285. _M_variant._M_iterator._M_address = std::__addressof(__it);
  286. _M_variant._M_iterator._M_type = _GLIBCXX_TYPEID(__it);
  287. _M_variant._M_iterator._M_constness = __const_iterator;
  288. _M_variant._M_iterator._M_state = __it ? __unknown_state : __singular;
  289. _M_variant._M_iterator._M_sequence = 0;
  290. _M_variant._M_iterator._M_seq_type = 0;
  291. }
  292. template<typename _Type>
  293. _Parameter(_Type* const& __it, const char* __name, _Is_iterator)
  294. : _M_kind(__iterator), _M_variant()
  295. {
  296. _M_variant._M_iterator._M_name = __name;
  297. _M_variant._M_iterator._M_address = std::__addressof(__it);
  298. _M_variant._M_iterator._M_type = _GLIBCXX_TYPEID(__it);
  299. _M_variant._M_iterator._M_constness = __mutable_iterator;
  300. _M_variant._M_iterator._M_state = __it ? __unknown_state : __singular;
  301. _M_variant._M_iterator._M_sequence = 0;
  302. _M_variant._M_iterator._M_seq_type = 0;
  303. }
  304. template<typename _Iterator>
  305. _Parameter(_Iterator const& __it, const char* __name, _Is_iterator)
  306. : _M_kind(__iterator), _M_variant()
  307. {
  308. _M_variant._M_iterator._M_name = __name;
  309. _M_variant._M_iterator._M_address = std::__addressof(__it);
  310. _M_variant._M_iterator._M_type = _GLIBCXX_TYPEID(__it);
  311. _M_variant._M_iterator._M_constness = __unknown_constness;
  312. _M_variant._M_iterator._M_state =
  313. __gnu_debug::__check_singular(__it) ? __singular : __unknown_state;
  314. _M_variant._M_iterator._M_sequence = 0;
  315. _M_variant._M_iterator._M_seq_type = 0;
  316. }
  317. #if __cplusplus >= 201103L
  318. // The following constructors are only defined in C++11 to take
  319. // advantage of the constructor delegation feature.
  320. template<typename _Iterator, typename _Container>
  321. _Parameter(
  322. __gnu_cxx::__normal_iterator<_Iterator, _Container> const& __it,
  323. const char* __name, _Is_iterator)
  324. : _Parameter(__it.base(), __name, _Is_iterator{})
  325. { _M_variant._M_iterator._M_type = _GLIBCXX_TYPEID(__it); }
  326. template<typename _Iterator>
  327. _Parameter(std::reverse_iterator<_Iterator> const& __it,
  328. const char* __name, _Is_iterator)
  329. : _Parameter(__it.base(), __name, _Is_iterator{})
  330. {
  331. _M_variant._M_iterator._M_type = _GLIBCXX_TYPEID(__it);
  332. _M_variant._M_iterator._M_state
  333. = _S_reverse_state(_M_variant._M_iterator._M_state);
  334. }
  335. template<typename _Iterator, typename _Sequence, typename _Category>
  336. _Parameter(std::reverse_iterator<_Safe_iterator<_Iterator, _Sequence,
  337. _Category>> const& __it,
  338. const char* __name, _Is_iterator)
  339. : _Parameter(__it.base(), __name, _Is_iterator{})
  340. {
  341. _M_variant._M_iterator._M_type
  342. = _GLIBCXX_TYPEID(std::reverse_iterator<_Iterator>);
  343. _M_variant._M_iterator._M_state
  344. = _S_reverse_state(_M_variant._M_iterator._M_state);
  345. }
  346. template<typename _Iterator>
  347. _Parameter(std::move_iterator<_Iterator> const& __it,
  348. const char* __name, _Is_iterator)
  349. : _Parameter(__it.base(), __name, _Is_iterator{})
  350. { _M_variant._M_iterator._M_type = _GLIBCXX_TYPEID(__it); }
  351. template<typename _Iterator, typename _Sequence, typename _Category>
  352. _Parameter(std::move_iterator<_Safe_iterator<_Iterator, _Sequence,
  353. _Category>> const& __it,
  354. const char* __name, _Is_iterator)
  355. : _Parameter(__it.base(), __name, _Is_iterator{})
  356. {
  357. _M_variant._M_iterator._M_type
  358. = _GLIBCXX_TYPEID(std::move_iterator<_Iterator>);
  359. }
  360. private:
  361. _Iterator_state
  362. _S_reverse_state(_Iterator_state __state)
  363. {
  364. switch (__state)
  365. {
  366. case __begin:
  367. return __rend;
  368. case __middle:
  369. return __rmiddle;
  370. case __end:
  371. return __rbegin;
  372. default:
  373. return __state;
  374. }
  375. }
  376. public:
  377. #endif
  378. template<typename _Sequence>
  379. _Parameter(const _Safe_sequence<_Sequence>& __seq,
  380. const char* __name, _Is_sequence)
  381. : _M_kind(__sequence), _M_variant()
  382. {
  383. _M_variant._M_sequence._M_name = __name;
  384. _M_variant._M_sequence._M_address =
  385. static_cast<const _Sequence*>(std::__addressof(__seq));
  386. _M_variant._M_sequence._M_type = _GLIBCXX_TYPEID(_Sequence);
  387. }
  388. template<typename _Sequence>
  389. _Parameter(const _Sequence& __seq, const char* __name, _Is_sequence)
  390. : _M_kind(__sequence), _M_variant()
  391. {
  392. _M_variant._M_sequence._M_name = __name;
  393. _M_variant._M_sequence._M_address = std::__addressof(__seq);
  394. _M_variant._M_sequence._M_type = _GLIBCXX_TYPEID(_Sequence);
  395. }
  396. template<typename _Iterator>
  397. _Parameter(const _Iterator& __it, const char* __name,
  398. _Is_iterator_value_type)
  399. : _M_kind(__iterator_value_type), _M_variant()
  400. {
  401. _M_variant._M_iterator_value_type._M_name = __name;
  402. _M_variant._M_iterator_value_type._M_type =
  403. _GLIBCXX_TYPEID(typename std::iterator_traits<_Iterator>::value_type);
  404. }
  405. template<typename _Type>
  406. _Parameter(const _Type& __inst, const char* __name, _Is_instance)
  407. : _M_kind(__instance), _M_variant()
  408. {
  409. _M_variant._M_instance._M_name = __name;
  410. _M_variant._M_instance._M_address = &__inst;
  411. _M_variant._M_instance._M_type = _GLIBCXX_TYPEID(_Type);
  412. }
  413. #if !_GLIBCXX_INLINE_VERSION
  414. void
  415. _M_print_field(const _Error_formatter* __formatter,
  416. const char* __name) const _GLIBCXX_DEPRECATED;
  417. void
  418. _M_print_description(const _Error_formatter* __formatter)
  419. const _GLIBCXX_DEPRECATED;
  420. #endif
  421. };
  422. template<typename _Iterator>
  423. _Error_formatter&
  424. _M_iterator(const _Iterator& __it, const char* __name = 0)
  425. {
  426. if (_M_num_parameters < std::size_t(__max_parameters))
  427. _M_parameters[_M_num_parameters++] = _Parameter(__it, __name,
  428. _Is_iterator());
  429. return *this;
  430. }
  431. template<typename _Iterator>
  432. _Error_formatter&
  433. _M_iterator_value_type(const _Iterator& __it,
  434. const char* __name = 0)
  435. {
  436. if (_M_num_parameters < __max_parameters)
  437. _M_parameters[_M_num_parameters++] =
  438. _Parameter(__it, __name, _Is_iterator_value_type());
  439. return *this;
  440. }
  441. _Error_formatter&
  442. _M_integer(long __value, const char* __name = 0)
  443. {
  444. if (_M_num_parameters < __max_parameters)
  445. _M_parameters[_M_num_parameters++] = _Parameter(__value, __name);
  446. return *this;
  447. }
  448. _Error_formatter&
  449. _M_string(const char* __value, const char* __name = 0)
  450. {
  451. if (_M_num_parameters < __max_parameters)
  452. _M_parameters[_M_num_parameters++] = _Parameter(__value, __name);
  453. return *this;
  454. }
  455. template<typename _Sequence>
  456. _Error_formatter&
  457. _M_sequence(const _Sequence& __seq, const char* __name = 0)
  458. {
  459. if (_M_num_parameters < __max_parameters)
  460. _M_parameters[_M_num_parameters++] = _Parameter(__seq, __name,
  461. _Is_sequence());
  462. return *this;
  463. }
  464. template<typename _Type>
  465. _Error_formatter&
  466. _M_instance(const _Type& __inst, const char* __name = 0)
  467. {
  468. if (_M_num_parameters < __max_parameters)
  469. _M_parameters[_M_num_parameters++] = _Parameter(__inst, __name,
  470. _Is_instance());
  471. return *this;
  472. }
  473. _Error_formatter&
  474. _M_message(const char* __text)
  475. { _M_text = __text; return *this; }
  476. // Kept const qualifier for backward compatibility, to keep the same
  477. // exported symbol.
  478. _Error_formatter&
  479. _M_message(_Debug_msg_id __id) const throw ();
  480. _GLIBCXX_NORETURN void
  481. _M_error() const;
  482. #if !_GLIBCXX_INLINE_VERSION
  483. template<typename _Tp>
  484. void
  485. _M_format_word(char*, int, const char*, _Tp)
  486. const throw () _GLIBCXX_DEPRECATED;
  487. void
  488. _M_print_word(const char* __word) const _GLIBCXX_DEPRECATED;
  489. void
  490. _M_print_string(const char* __string) const _GLIBCXX_DEPRECATED;
  491. #endif
  492. private:
  493. _Error_formatter(const char* __file, unsigned int __line,
  494. const char* __function)
  495. : _M_file(__file), _M_line(__line), _M_num_parameters(0), _M_text(0)
  496. , _M_function(__function)
  497. { }
  498. #if !_GLIBCXX_INLINE_VERSION
  499. void
  500. _M_get_max_length() const throw () _GLIBCXX_DEPRECATED;
  501. #endif
  502. enum { __max_parameters = 9 };
  503. const char* _M_file;
  504. unsigned int _M_line;
  505. _Parameter _M_parameters[__max_parameters];
  506. unsigned int _M_num_parameters;
  507. const char* _M_text;
  508. const char* _M_function;
  509. public:
  510. static _Error_formatter&
  511. _S_at(const char* __file, unsigned int __line, const char* __function)
  512. {
  513. static _Error_formatter __formatter(__file, __line, __function);
  514. return __formatter;
  515. }
  516. };
  517. } // namespace __gnu_debug
  518. #undef _GLIBCXX_TYPEID
  519. #endif