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