* include/bits/list.tcc (list::operator=(const list&), list::merge):
[official-gcc.git] / libstdc++-v3 / include / debug / formatter.h
blob56ee8078681f6d8b72be9056f444a01be0213c4a
1 // Debug-mode error formatting implementation -*- C++ -*-
3 // Copyright (C) 2003-2015 Free Software Foundation, Inc.
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
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
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.
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.
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/>.
25 /** @file debug/formatter.h
26 * This file is a GNU debug extension to the Standard C++ Library.
29 #ifndef _GLIBCXX_DEBUG_FORMATTER_H
30 #define _GLIBCXX_DEBUG_FORMATTER_H 1
32 #include <bits/c++config.h>
33 #include <bits/cpp_type_traits.h>
35 #if __cpp_rtti
36 # include <typeinfo>
37 # define _GLIBCXX_TYPEID(_Type) &typeid(_Type)
38 #else
39 namespace std
41 class type_info;
43 # define _GLIBCXX_TYPEID(_Type) 0
44 #endif
46 namespace __gnu_debug
48 using std::type_info;
50 template<typename _Iterator>
51 bool __check_singular(const _Iterator&);
53 class _Safe_sequence_base;
55 template<typename _Iterator, typename _Sequence>
56 class _Safe_iterator;
58 template<typename _Iterator, typename _Sequence>
59 class _Safe_local_iterator;
61 template<typename _Sequence>
62 class _Safe_sequence;
64 enum _Debug_msg_id
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,
114 __msg_inc_istreambuf,
115 // forward_list
116 __msg_insert_after_end,
117 __msg_erase_after_bad,
118 __msg_valid_range2,
119 // unordered container local iterators
120 __msg_local_iter_compare_bad,
121 __msg_non_empty_range,
122 // self move assign
123 __msg_self_move_assign,
124 // unordered container buckets
125 __msg_bucket_index_oob,
126 __msg_valid_load_factor,
127 // others
128 __msg_equal_allocs,
129 __msg_insert_range_from_self
132 class _Error_formatter
134 /// Whether an iterator is constant, mutable, or unknown
135 enum _Constness
137 __unknown_constness,
138 __const_iterator,
139 __mutable_iterator,
140 __last_constness
143 // The state of the iterator (fine-grained), if we know it.
144 enum _Iterator_state
146 __unknown_state,
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
152 __last_state
155 // Tags denoting the type of parameter for construction
156 struct _Is_iterator { };
157 struct _Is_iterator_value_type { };
158 struct _Is_sequence { };
159 struct _Is_instance { };
161 public:
162 // A parameter that may be referenced by an error message
163 struct _Parameter
165 enum
167 __unused_param,
168 __iterator,
169 __sequence,
170 __integer,
171 __string,
172 __instance,
173 __iterator_value_type
174 } _M_kind;
176 struct _Type
178 const char* _M_name;
179 const type_info* _M_type;
182 struct _Instance : _Type
184 const void* _M_address;
187 union
189 // When _M_kind == __iterator
190 struct : _Instance
192 _Constness _M_constness;
193 _Iterator_state _M_state;
194 const void* _M_sequence;
195 const type_info* _M_seq_type;
196 } _M_iterator;
198 // When _M_kind == __sequence
199 _Instance _M_sequence;
201 // When _M_kind == __integer
202 struct
204 const char* _M_name;
205 long _M_value;
206 } _M_integer;
208 // When _M_kind == __string
209 struct
211 const char* _M_name;
212 const char* _M_value;
213 } _M_string;
215 // When _M_kind == __instance
216 _Instance _M_instance;
218 // When _M_kind == __iterator_value_type
219 _Type _M_iterator_value_type;
220 } _M_variant;
222 _Parameter() : _M_kind(__unused_param), _M_variant() { }
224 _Parameter(long __value, const char* __name)
225 : _M_kind(__integer), _M_variant()
227 _M_variant._M_integer._M_name = __name;
228 _M_variant._M_integer._M_value = __value;
231 _Parameter(const char* __value, const char* __name)
232 : _M_kind(__string), _M_variant()
234 _M_variant._M_string._M_name = __name;
235 _M_variant._M_string._M_value = __value;
238 template<typename _Iterator, typename _Sequence>
239 _Parameter(_Safe_iterator<_Iterator, _Sequence> const& __it,
240 const char* __name, _Is_iterator)
241 : _M_kind(__iterator), _M_variant()
243 _M_variant._M_iterator._M_name = __name;
244 _M_variant._M_iterator._M_address = std::__addressof(__it);
245 _M_variant._M_iterator._M_type = _GLIBCXX_TYPEID(__it);
246 _M_variant._M_iterator._M_constness =
247 std::__are_same<_Safe_iterator<_Iterator, _Sequence>,
248 typename _Sequence::iterator>::
249 __value ? __mutable_iterator : __const_iterator;
250 _M_variant._M_iterator._M_sequence = __it._M_get_sequence();
251 _M_variant._M_iterator._M_seq_type = _GLIBCXX_TYPEID(_Sequence);
253 if (__it._M_singular())
254 _M_variant._M_iterator._M_state = __singular;
255 else
257 if (__it._M_is_before_begin())
258 _M_variant._M_iterator._M_state = __before_begin;
259 else if (__it._M_is_end())
260 _M_variant._M_iterator._M_state = __end;
261 else if (__it._M_is_begin())
262 _M_variant._M_iterator._M_state = __begin;
263 else
264 _M_variant._M_iterator._M_state = __middle;
268 template<typename _Iterator, typename _Sequence>
269 _Parameter(_Safe_local_iterator<_Iterator, _Sequence> const& __it,
270 const char* __name, _Is_iterator)
271 : _M_kind(__iterator), _M_variant()
273 _M_variant._M_iterator._M_name = __name;
274 _M_variant._M_iterator._M_address = std::__addressof(__it);
275 _M_variant._M_iterator._M_type = _GLIBCXX_TYPEID(__it);
276 _M_variant._M_iterator._M_constness =
277 std::__are_same<_Safe_local_iterator<_Iterator, _Sequence>,
278 typename _Sequence::local_iterator>::
279 __value ? __mutable_iterator : __const_iterator;
280 _M_variant._M_iterator._M_sequence = __it._M_get_sequence();
281 _M_variant._M_iterator._M_seq_type = _GLIBCXX_TYPEID(_Sequence);
283 if (__it._M_singular())
284 _M_variant._M_iterator._M_state = __singular;
285 else
287 if (__it._M_is_end())
288 _M_variant._M_iterator._M_state = __end;
289 else if (__it._M_is_begin())
290 _M_variant._M_iterator._M_state = __begin;
291 else
292 _M_variant._M_iterator._M_state = __middle;
296 template<typename _Type>
297 _Parameter(const _Type* const& __it, const char* __name, _Is_iterator)
298 : _M_kind(__iterator), _M_variant()
300 _M_variant._M_iterator._M_name = __name;
301 _M_variant._M_iterator._M_address = std::__addressof(__it);
302 _M_variant._M_iterator._M_type = _GLIBCXX_TYPEID(__it);
303 _M_variant._M_iterator._M_constness = __const_iterator;
304 _M_variant._M_iterator._M_state = __it ? __unknown_state : __singular;
305 _M_variant._M_iterator._M_sequence = 0;
306 _M_variant._M_iterator._M_seq_type = 0;
309 template<typename _Type>
310 _Parameter(_Type* const& __it, const char* __name, _Is_iterator)
311 : _M_kind(__iterator), _M_variant()
313 _M_variant._M_iterator._M_name = __name;
314 _M_variant._M_iterator._M_address = std::__addressof(__it);
315 _M_variant._M_iterator._M_type = _GLIBCXX_TYPEID(__it);
316 _M_variant._M_iterator._M_constness = __mutable_iterator;
317 _M_variant._M_iterator._M_state = __it ? __unknown_state : __singular;
318 _M_variant._M_iterator._M_sequence = 0;
319 _M_variant._M_iterator._M_seq_type = 0;
322 template<typename _Iterator>
323 _Parameter(_Iterator const& __it, const char* __name, _Is_iterator)
324 : _M_kind(__iterator), _M_variant()
326 _M_variant._M_iterator._M_name = __name;
327 _M_variant._M_iterator._M_address = std::__addressof(__it);
328 _M_variant._M_iterator._M_type = _GLIBCXX_TYPEID(__it);
329 _M_variant._M_iterator._M_constness = __unknown_constness;
330 _M_variant._M_iterator._M_state =
331 __gnu_debug::__check_singular(__it) ? __singular : __unknown_state;
332 _M_variant._M_iterator._M_sequence = 0;
333 _M_variant._M_iterator._M_seq_type = 0;
336 template<typename _Sequence>
337 _Parameter(const _Safe_sequence<_Sequence>& __seq,
338 const char* __name, _Is_sequence)
339 : _M_kind(__sequence), _M_variant()
341 _M_variant._M_sequence._M_name = __name;
342 _M_variant._M_sequence._M_address =
343 static_cast<const _Sequence*>(std::__addressof(__seq));
344 _M_variant._M_sequence._M_type = _GLIBCXX_TYPEID(_Sequence);
347 template<typename _Sequence>
348 _Parameter(const _Sequence& __seq, const char* __name, _Is_sequence)
349 : _M_kind(__sequence), _M_variant()
351 _M_variant._M_sequence._M_name = __name;
352 _M_variant._M_sequence._M_address = std::__addressof(__seq);
353 _M_variant._M_sequence._M_type = _GLIBCXX_TYPEID(_Sequence);
356 template<typename _Iterator>
357 _Parameter(const _Iterator& __it, const char* __name,
358 _Is_iterator_value_type)
359 : _M_kind(__iterator_value_type), _M_variant()
361 _M_variant._M_iterator_value_type._M_name = __name;
362 _M_variant._M_iterator_value_type._M_type =
363 _GLIBCXX_TYPEID(typename std::iterator_traits<_Iterator>::value_type);
366 template<typename _Type>
367 _Parameter(const _Type& __inst, const char* __name, _Is_instance)
368 : _M_kind(__instance), _M_variant()
370 _M_variant._M_instance._M_name = __name;
371 _M_variant._M_instance._M_address = &__inst;
372 _M_variant._M_instance._M_type = _GLIBCXX_TYPEID(_Type);
375 void
376 _M_print_field(const _Error_formatter* __formatter,
377 const char* __name) const;
379 void
380 _M_print_description(const _Error_formatter* __formatter) const;
383 template<typename _Iterator>
384 const _Error_formatter&
385 _M_iterator(const _Iterator& __it, const char* __name = 0) const
387 if (_M_num_parameters < std::size_t(__max_parameters))
388 _M_parameters[_M_num_parameters++] = _Parameter(__it, __name,
389 _Is_iterator());
390 return *this;
393 template<typename _Iterator>
394 const _Error_formatter&
395 _M_iterator_value_type(const _Iterator& __it,
396 const char* __name = 0) const
398 if (_M_num_parameters < std::size_t(__max_parameters))
399 _M_parameters[_M_num_parameters++] =
400 _Parameter(__it, __name, _Is_iterator_value_type());
401 return *this;
404 const _Error_formatter&
405 _M_integer(long __value, const char* __name = 0) const
407 if (_M_num_parameters < std::size_t(__max_parameters))
408 _M_parameters[_M_num_parameters++] = _Parameter(__value, __name);
409 return *this;
412 const _Error_formatter&
413 _M_string(const char* __value, const char* __name = 0) const
415 if (_M_num_parameters < std::size_t(__max_parameters))
416 _M_parameters[_M_num_parameters++] = _Parameter(__value, __name);
417 return *this;
420 template<typename _Sequence>
421 const _Error_formatter&
422 _M_sequence(const _Sequence& __seq, const char* __name = 0) const
424 if (_M_num_parameters < std::size_t(__max_parameters))
425 _M_parameters[_M_num_parameters++] = _Parameter(__seq, __name,
426 _Is_sequence());
427 return *this;
430 template<typename _Type>
431 const _Error_formatter&
432 _M_instance(const _Type& __inst, const char* __name = 0) const
434 if (_M_num_parameters < std::size_t(__max_parameters))
435 _M_parameters[_M_num_parameters++] = _Parameter(__inst, __name,
436 _Is_instance());
437 return *this;
440 const _Error_formatter&
441 _M_message(const char* __text) const
442 { _M_text = __text; return *this; }
444 const _Error_formatter&
445 _M_message(_Debug_msg_id __id) const throw ();
447 _GLIBCXX_NORETURN void
448 _M_error() const;
450 template<typename _Tp>
451 void
452 _M_format_word(char*, int, const char*, _Tp) const throw ();
454 void
455 _M_print_word(const char* __word) const;
457 void
458 _M_print_string(const char* __string) const;
460 private:
461 _Error_formatter(const char* __file, std::size_t __line)
462 : _M_file(__file), _M_line(__line), _M_num_parameters(0), _M_text(0),
463 _M_max_length(78), _M_column(1), _M_first_line(true), _M_wordwrap(false)
464 { _M_get_max_length(); }
466 void
467 _M_get_max_length() const throw ();
469 enum { __max_parameters = 9 };
471 const char* _M_file;
472 std::size_t _M_line;
473 mutable _Parameter _M_parameters[__max_parameters];
474 mutable std::size_t _M_num_parameters;
475 mutable const char* _M_text;
476 mutable std::size_t _M_max_length;
477 enum { _M_indent = 4 } ;
478 mutable std::size_t _M_column;
479 mutable bool _M_first_line;
480 mutable bool _M_wordwrap;
482 public:
483 static _Error_formatter
484 _M_at(const char* __file, std::size_t __line)
485 { return _Error_formatter(__file, __line); }
487 } // namespace __gnu_debug
489 #undef _GLIBCXX_TYPEID
491 #endif