formatter.h 14 KB

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