]>
Commit | Line | Data |
---|---|---|
285b36d6 BK |
1 | // Debug-mode error formatting implementation -*- C++ -*- |
2 | ||
5624e564 | 3 | // Copyright (C) 2003-2015 Free Software Foundation, Inc. |
285b36d6 BK |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
285b36d6 BK |
9 | // any later version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
748086b7 JJ |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
285b36d6 | 19 | |
748086b7 JJ |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
285b36d6 | 24 | |
78a53887 BK |
25 | /** @file debug/formatter.h |
26 | * This file is a GNU debug extension to the Standard C++ Library. | |
27 | */ | |
28 | ||
285b36d6 BK |
29 | #ifndef _GLIBCXX_DEBUG_FORMATTER_H |
30 | #define _GLIBCXX_DEBUG_FORMATTER_H 1 | |
31 | ||
b8add594 | 32 | #include <bits/c++config.h> |
67c495be | 33 | #include <bits/cpp_type_traits.h> |
fa5cc2db FD |
34 | |
35 | #if __cpp_rtti | |
36 | # include <typeinfo> | |
37 | # define _GLIBCXX_TYPEID(_Type) &typeid(_Type) | |
38 | #else | |
39 | namespace std | |
40 | { | |
41 | class type_info; | |
42 | } | |
43 | # define _GLIBCXX_TYPEID(_Type) 0 | |
44 | #endif | |
285b36d6 BK |
45 | |
46 | namespace __gnu_debug | |
47 | { | |
45f388bb BK |
48 | using std::type_info; |
49 | ||
1f5ca1a1 | 50 | template<typename _Iterator> |
3ff4317f | 51 | bool __check_singular(const _Iterator&); |
1f5ca1a1 | 52 | |
285b36d6 BK |
53 | class _Safe_sequence_base; |
54 | ||
526da49c | 55 | template<typename _Iterator, typename _Sequence> |
285b36d6 BK |
56 | class _Safe_iterator; |
57 | ||
77e0bf4e FD |
58 | template<typename _Iterator, typename _Sequence> |
59 | class _Safe_local_iterator; | |
60 | ||
526da49c | 61 | template<typename _Sequence> |
285b36d6 BK |
62 | class _Safe_sequence; |
63 | ||
64 | enum _Debug_msg_id | |
65 | { | |
66 | // General checks | |
67 | __msg_valid_range, | |
68 | __msg_insert_singular, | |
69 | __msg_insert_different, | |
70 | __msg_erase_bad, | |
71 | __msg_erase_different, | |
72 | __msg_subscript_oob, | |
73 | __msg_empty, | |
74 | __msg_unpartitioned, | |
75 | __msg_unpartitioned_pred, | |
76 | __msg_unsorted, | |
77 | __msg_unsorted_pred, | |
78 | __msg_not_heap, | |
79 | __msg_not_heap_pred, | |
80 | // std::bitset checks | |
81 | __msg_bad_bitset_write, | |
82 | __msg_bad_bitset_read, | |
83 | __msg_bad_bitset_flip, | |
84 | // std::list checks | |
85 | __msg_self_splice, | |
86 | __msg_splice_alloc, | |
87 | __msg_splice_bad, | |
88 | __msg_splice_other, | |
89 | __msg_splice_overlap, | |
90 | // iterator checks | |
91 | __msg_init_singular, | |
92 | __msg_init_copy_singular, | |
93 | __msg_init_const_singular, | |
94 | __msg_copy_singular, | |
95 | __msg_bad_deref, | |
96 | __msg_bad_inc, | |
97 | __msg_bad_dec, | |
98 | __msg_iter_subscript_oob, | |
99 | __msg_advance_oob, | |
100 | __msg_retreat_oob, | |
101 | __msg_iter_compare_bad, | |
102 | __msg_compare_different, | |
103 | __msg_iter_order_bad, | |
104 | __msg_order_different, | |
105 | __msg_distance_bad, | |
106 | __msg_distance_different, | |
107 | // istream_iterator | |
108 | __msg_deref_istream, | |
109 | __msg_inc_istream, | |
110 | // ostream_iterator | |
111 | __msg_output_ostream, | |
112 | // istreambuf_iterator | |
113 | __msg_deref_istreambuf, | |
b8b4301e PC |
114 | __msg_inc_istreambuf, |
115 | // forward_list | |
116 | __msg_insert_after_end, | |
117 | __msg_erase_after_bad, | |
77e0bf4e | 118 | __msg_valid_range2, |
7181e991 | 119 | // unordered container local iterators |
a8028a3e | 120 | __msg_local_iter_compare_bad, |
739fd6a6 PC |
121 | __msg_non_empty_range, |
122 | // self move assign | |
7181e991 FD |
123 | __msg_self_move_assign, |
124 | // unordered container buckets | |
14cbb5d8 | 125 | __msg_bucket_index_oob, |
e77c9aed | 126 | __msg_valid_load_factor, |
5720787a FD |
127 | // others |
128 | __msg_equal_allocs, | |
129 | __msg_insert_range_from_self | |
285b36d6 BK |
130 | }; |
131 | ||
132 | class _Error_formatter | |
133 | { | |
134 | /// Whether an iterator is constant, mutable, or unknown | |
135 | enum _Constness | |
136 | { | |
137 | __unknown_constness, | |
138 | __const_iterator, | |
139 | __mutable_iterator, | |
140 | __last_constness | |
526da49c | 141 | }; |
285b36d6 BK |
142 | |
143 | // The state of the iterator (fine-grained), if we know it. | |
144 | enum _Iterator_state | |
145 | { | |
146 | __unknown_state, | |
fa5cc2db FD |
147 | __singular, // singular, may still be attached to a sequence |
148 | __begin, // dereferenceable, and at the beginning | |
149 | __middle, // dereferenceable, not at the beginning | |
150 | __end, // past-the-end, may be at beginning if sequence empty | |
151 | __before_begin, // before begin | |
285b36d6 BK |
152 | __last_state |
153 | }; | |
154 | ||
155 | // Tags denoting the type of parameter for construction | |
156 | struct _Is_iterator { }; | |
157 | struct _Is_sequence { }; | |
158 | ||
159 | // A parameter that may be referenced by an error message | |
160 | struct _Parameter | |
161 | { | |
526da49c BI |
162 | enum |
163 | { | |
164 | __unused_param, | |
165 | __iterator, | |
166 | __sequence, | |
285b36d6 BK |
167 | __integer, |
168 | __string | |
169 | } _M_kind; | |
526da49c | 170 | |
285b36d6 BK |
171 | union |
172 | { | |
173 | // When _M_kind == __iterator | |
526da49c | 174 | struct |
285b36d6 | 175 | { |
fa5cc2db FD |
176 | const char* _M_name; |
177 | const void* _M_address; | |
178 | const type_info* _M_type; | |
179 | _Constness _M_constness; | |
180 | _Iterator_state _M_state; | |
181 | const void* _M_sequence; | |
182 | const type_info* _M_seq_type; | |
285b36d6 | 183 | } _M_iterator; |
526da49c | 184 | |
285b36d6 BK |
185 | // When _M_kind == __sequence |
186 | struct | |
187 | { | |
fa5cc2db FD |
188 | const char* _M_name; |
189 | const void* _M_address; | |
190 | const type_info* _M_type; | |
285b36d6 BK |
191 | } _M_sequence; |
192 | ||
193 | // When _M_kind == __integer | |
194 | struct | |
195 | { | |
fa5cc2db FD |
196 | const char* _M_name; |
197 | long _M_value; | |
285b36d6 BK |
198 | } _M_integer; |
199 | ||
200 | // When _M_kind == __string | |
201 | struct | |
202 | { | |
fa5cc2db FD |
203 | const char* _M_name; |
204 | const char* _M_value; | |
285b36d6 BK |
205 | } _M_string; |
206 | } _M_variant; | |
207 | ||
26c691a8 | 208 | _Parameter() : _M_kind(__unused_param), _M_variant() { } |
526da49c | 209 | |
fa5cc2db | 210 | _Parameter(long __value, const char* __name) |
26c691a8 | 211 | : _M_kind(__integer), _M_variant() |
526da49c | 212 | { |
285b36d6 | 213 | _M_variant._M_integer._M_name = __name; |
526da49c | 214 | _M_variant._M_integer._M_value = __value; |
285b36d6 BK |
215 | } |
216 | ||
fa5cc2db | 217 | _Parameter(const char* __value, const char* __name) |
26c691a8 | 218 | : _M_kind(__string), _M_variant() |
285b36d6 BK |
219 | { |
220 | _M_variant._M_string._M_name = __name; | |
526da49c | 221 | _M_variant._M_string._M_value = __value; |
285b36d6 BK |
222 | } |
223 | ||
224 | template<typename _Iterator, typename _Sequence> | |
fa5cc2db | 225 | _Parameter(const _Safe_iterator<_Iterator, _Sequence>& __it, |
285b36d6 | 226 | const char* __name, _Is_iterator) |
26c691a8 | 227 | : _M_kind(__iterator), _M_variant() |
fa5cc2db | 228 | { |
285b36d6 BK |
229 | _M_variant._M_iterator._M_name = __name; |
230 | _M_variant._M_iterator._M_address = &__it; | |
fa5cc2db | 231 | _M_variant._M_iterator._M_type = _GLIBCXX_TYPEID(__it); |
526da49c | 232 | _M_variant._M_iterator._M_constness = |
67c495be | 233 | std::__are_same<_Safe_iterator<_Iterator, _Sequence>, |
fa5cc2db | 234 | typename _Sequence::iterator>:: |
67c495be | 235 | __value ? __mutable_iterator : __const_iterator; |
285b36d6 | 236 | _M_variant._M_iterator._M_sequence = __it._M_get_sequence(); |
fa5cc2db | 237 | _M_variant._M_iterator._M_seq_type = _GLIBCXX_TYPEID(_Sequence); |
285b36d6 BK |
238 | |
239 | if (__it._M_singular()) | |
240 | _M_variant._M_iterator._M_state = __singular; | |
241 | else | |
242 | { | |
b8b4301e PC |
243 | if (__it._M_is_before_begin()) |
244 | _M_variant._M_iterator._M_state = __before_begin; | |
245 | else if (__it._M_is_end()) | |
285b36d6 | 246 | _M_variant._M_iterator._M_state = __end; |
b8b4301e | 247 | else if (__it._M_is_begin()) |
285b36d6 BK |
248 | _M_variant._M_iterator._M_state = __begin; |
249 | else | |
250 | _M_variant._M_iterator._M_state = __middle; | |
251 | } | |
252 | } | |
253 | ||
77e0bf4e FD |
254 | template<typename _Iterator, typename _Sequence> |
255 | _Parameter(const _Safe_local_iterator<_Iterator, _Sequence>& __it, | |
256 | const char* __name, _Is_iterator) | |
257 | : _M_kind(__iterator), _M_variant() | |
258 | { | |
259 | _M_variant._M_iterator._M_name = __name; | |
260 | _M_variant._M_iterator._M_address = &__it; | |
fa5cc2db | 261 | _M_variant._M_iterator._M_type = _GLIBCXX_TYPEID(__it); |
77e0bf4e FD |
262 | _M_variant._M_iterator._M_constness = |
263 | std::__are_same<_Safe_local_iterator<_Iterator, _Sequence>, | |
fa5cc2db | 264 | typename _Sequence::local_iterator>:: |
77e0bf4e FD |
265 | __value ? __mutable_iterator : __const_iterator; |
266 | _M_variant._M_iterator._M_sequence = __it._M_get_sequence(); | |
fa5cc2db | 267 | _M_variant._M_iterator._M_seq_type = _GLIBCXX_TYPEID(_Sequence); |
77e0bf4e FD |
268 | |
269 | if (__it._M_singular()) | |
270 | _M_variant._M_iterator._M_state = __singular; | |
271 | else | |
272 | { | |
273 | if (__it._M_is_end()) | |
274 | _M_variant._M_iterator._M_state = __end; | |
275 | else if (__it._M_is_begin()) | |
276 | _M_variant._M_iterator._M_state = __begin; | |
277 | else | |
278 | _M_variant._M_iterator._M_state = __middle; | |
279 | } | |
280 | } | |
281 | ||
285b36d6 | 282 | template<typename _Type> |
fa5cc2db FD |
283 | _Parameter(const _Type*& __it, const char* __name, _Is_iterator) |
284 | : _M_kind(__iterator), _M_variant() | |
285 | { | |
285b36d6 BK |
286 | _M_variant._M_iterator._M_name = __name; |
287 | _M_variant._M_iterator._M_address = &__it; | |
fa5cc2db | 288 | _M_variant._M_iterator._M_type = _GLIBCXX_TYPEID(__it); |
285b36d6 BK |
289 | _M_variant._M_iterator._M_constness = __mutable_iterator; |
290 | _M_variant._M_iterator._M_state = __it? __unknown_state : __singular; | |
291 | _M_variant._M_iterator._M_sequence = 0; | |
292 | _M_variant._M_iterator._M_seq_type = 0; | |
293 | } | |
294 | ||
295 | template<typename _Type> | |
fa5cc2db FD |
296 | _Parameter(_Type*& __it, const char* __name, _Is_iterator) |
297 | : _M_kind(__iterator), _M_variant() | |
298 | { | |
285b36d6 BK |
299 | _M_variant._M_iterator._M_name = __name; |
300 | _M_variant._M_iterator._M_address = &__it; | |
fa5cc2db | 301 | _M_variant._M_iterator._M_type = _GLIBCXX_TYPEID(__it); |
285b36d6 BK |
302 | _M_variant._M_iterator._M_constness = __const_iterator; |
303 | _M_variant._M_iterator._M_state = __it? __unknown_state : __singular; | |
304 | _M_variant._M_iterator._M_sequence = 0; | |
305 | _M_variant._M_iterator._M_seq_type = 0; | |
306 | } | |
526da49c | 307 | |
285b36d6 | 308 | template<typename _Iterator> |
fa5cc2db FD |
309 | _Parameter(const _Iterator& __it, const char* __name, _Is_iterator) |
310 | : _M_kind(__iterator), _M_variant() | |
311 | { | |
285b36d6 BK |
312 | _M_variant._M_iterator._M_name = __name; |
313 | _M_variant._M_iterator._M_address = &__it; | |
fa5cc2db | 314 | _M_variant._M_iterator._M_type = _GLIBCXX_TYPEID(__it); |
285b36d6 | 315 | _M_variant._M_iterator._M_constness = __unknown_constness; |
526da49c | 316 | _M_variant._M_iterator._M_state = |
285b36d6 BK |
317 | __gnu_debug::__check_singular(__it)? __singular : __unknown_state; |
318 | _M_variant._M_iterator._M_sequence = 0; | |
319 | _M_variant._M_iterator._M_seq_type = 0; | |
320 | } | |
321 | ||
322 | template<typename _Sequence> | |
fa5cc2db | 323 | _Parameter(const _Safe_sequence<_Sequence>& __seq, |
285b36d6 | 324 | const char* __name, _Is_sequence) |
fa5cc2db FD |
325 | : _M_kind(__sequence), _M_variant() |
326 | { | |
285b36d6 | 327 | _M_variant._M_sequence._M_name = __name; |
526da49c | 328 | _M_variant._M_sequence._M_address = |
285b36d6 | 329 | static_cast<const _Sequence*>(&__seq); |
fa5cc2db | 330 | _M_variant._M_sequence._M_type = _GLIBCXX_TYPEID(_Sequence); |
285b36d6 BK |
331 | } |
332 | ||
333 | template<typename _Sequence> | |
fa5cc2db FD |
334 | _Parameter(const _Sequence& __seq, const char* __name, _Is_sequence) |
335 | : _M_kind(__sequence), _M_variant() | |
336 | { | |
285b36d6 BK |
337 | _M_variant._M_sequence._M_name = __name; |
338 | _M_variant._M_sequence._M_address = &__seq; | |
fa5cc2db | 339 | _M_variant._M_sequence._M_type = _GLIBCXX_TYPEID(_Sequence); |
285b36d6 | 340 | } |
526da49c | 341 | |
285b36d6 | 342 | void |
526da49c | 343 | _M_print_field(const _Error_formatter* __formatter, |
285b36d6 | 344 | const char* __name) const; |
526da49c | 345 | |
285b36d6 BK |
346 | void |
347 | _M_print_description(const _Error_formatter* __formatter) const; | |
348 | }; | |
349 | ||
350 | friend struct _Parameter; | |
351 | ||
526da49c | 352 | public: |
285b36d6 BK |
353 | template<typename _Iterator> |
354 | const _Error_formatter& | |
355 | _M_iterator(const _Iterator& __it, const char* __name = 0) const | |
356 | { | |
8fc81078 | 357 | if (_M_num_parameters < std::size_t(__max_parameters)) |
285b36d6 BK |
358 | _M_parameters[_M_num_parameters++] = _Parameter(__it, __name, |
359 | _Is_iterator()); | |
360 | return *this; | |
361 | } | |
362 | ||
363 | const _Error_formatter& | |
364 | _M_integer(long __value, const char* __name = 0) const | |
365 | { | |
8fc81078 | 366 | if (_M_num_parameters < std::size_t(__max_parameters)) |
285b36d6 BK |
367 | _M_parameters[_M_num_parameters++] = _Parameter(__value, __name); |
368 | return *this; | |
369 | } | |
370 | ||
371 | const _Error_formatter& | |
372 | _M_string(const char* __value, const char* __name = 0) const | |
373 | { | |
8fc81078 | 374 | if (_M_num_parameters < std::size_t(__max_parameters)) |
285b36d6 BK |
375 | _M_parameters[_M_num_parameters++] = _Parameter(__value, __name); |
376 | return *this; | |
377 | } | |
378 | ||
379 | template<typename _Sequence> | |
380 | const _Error_formatter& | |
381 | _M_sequence(const _Sequence& __seq, const char* __name = 0) const | |
382 | { | |
8fc81078 | 383 | if (_M_num_parameters < std::size_t(__max_parameters)) |
526da49c | 384 | _M_parameters[_M_num_parameters++] = _Parameter(__seq, __name, |
285b36d6 BK |
385 | _Is_sequence()); |
386 | return *this; | |
387 | } | |
388 | ||
389 | const _Error_formatter& | |
390 | _M_message(const char* __text) const | |
391 | { _M_text = __text; return *this; } | |
392 | ||
393 | const _Error_formatter& | |
5d51b87a | 394 | _M_message(_Debug_msg_id __id) const throw (); |
285b36d6 | 395 | |
b8add594 | 396 | _GLIBCXX_NORETURN void |
285b36d6 BK |
397 | _M_error() const; |
398 | ||
399 | private: | |
8fc81078 | 400 | _Error_formatter(const char* __file, std::size_t __line) |
285b36d6 BK |
401 | : _M_file(__file), _M_line(__line), _M_num_parameters(0), _M_text(0), |
402 | _M_max_length(78), _M_column(1), _M_first_line(true), _M_wordwrap(false) | |
5dddb7e5 | 403 | { _M_get_max_length(); } |
285b36d6 | 404 | |
9dc420e6 | 405 | template<typename _Tp> |
4be58168 | 406 | void |
5d51b87a | 407 | _M_format_word(char*, int, const char*, _Tp) const throw (); |
4be58168 | 408 | |
526da49c | 409 | void |
285b36d6 BK |
410 | _M_print_word(const char* __word) const; |
411 | ||
526da49c | 412 | void |
285b36d6 BK |
413 | _M_print_string(const char* __string) const; |
414 | ||
fa5cc2db FD |
415 | void |
416 | _M_print_type(const type_info* __info, | |
417 | const char* __unknown_name) const; | |
418 | ||
5dddb7e5 | 419 | void |
5d51b87a | 420 | _M_get_max_length() const throw (); |
5dddb7e5 | 421 | |
285b36d6 BK |
422 | enum { __max_parameters = 9 }; |
423 | ||
fa5cc2db FD |
424 | const char* _M_file; |
425 | std::size_t _M_line; | |
426 | mutable _Parameter _M_parameters[__max_parameters]; | |
427 | mutable std::size_t _M_num_parameters; | |
428 | mutable const char* _M_text; | |
429 | mutable std::size_t _M_max_length; | |
285b36d6 | 430 | enum { _M_indent = 4 } ; |
fa5cc2db FD |
431 | mutable std::size_t _M_column; |
432 | mutable bool _M_first_line; | |
433 | mutable bool _M_wordwrap; | |
285b36d6 BK |
434 | |
435 | public: | |
436 | static _Error_formatter | |
8fc81078 | 437 | _M_at(const char* __file, std::size_t __line) |
285b36d6 BK |
438 | { return _Error_formatter(__file, __line); } |
439 | }; | |
440 | } // namespace __gnu_debug | |
441 | ||
fa5cc2db FD |
442 | #undef _GLIBCXX_TYPEID |
443 | ||
526da49c | 444 | #endif |