1 // Debug-mode error formatting implementation -*- C++ -*-
3 // Copyright (C) 2003-2024 Free Software Foundation, Inc.
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)
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>
34 #if _GLIBCXX_HAVE_STACKTRACE
37 struct __glibcxx_backtrace_state
*
38 __glibcxx_backtrace_create_state(const char*, int,
39 void(*)(void*, const char*, int),
42 __glibcxx_backtrace_full(
43 struct __glibcxx_backtrace_state
*, int,
44 int (*)(void*, __UINTPTR_TYPE__
, const char *, int, const char*),
45 void (*)(void*, const char*, int),
52 # define _GLIBCXX_TYPEID(_Type) &typeid(_Type)
58 # define _GLIBCXX_TYPEID(_Type) 0
61 #if __cplusplus >= 201103L
64 _GLIBCXX_BEGIN_NAMESPACE_VERSION
66 template<typename _Iterator
, typename _Container
>
67 class __normal_iterator
;
69 _GLIBCXX_END_NAMESPACE_VERSION
74 _GLIBCXX_BEGIN_NAMESPACE_VERSION
76 template<typename _Iterator
>
77 class reverse_iterator
;
79 template<typename _Iterator
>
82 _GLIBCXX_END_NAMESPACE_VERSION
90 template<typename _Iterator
>
92 bool __check_singular(_Iterator
const&);
94 class _Safe_sequence_base
;
96 template<typename _Iterator
, typename _Sequence
, typename _Category
>
99 template<typename _Iterator
, typename _Sequence
>
100 class _Safe_local_iterator
;
102 template<typename _Sequence
>
103 class _Safe_sequence
;
109 __msg_insert_singular
,
110 __msg_insert_different
,
112 __msg_erase_different
,
116 __msg_unpartitioned_pred
,
121 // std::bitset checks
122 __msg_bad_bitset_write
,
123 __msg_bad_bitset_read
,
124 __msg_bad_bitset_flip
,
130 __msg_splice_overlap
,
133 __msg_init_copy_singular
,
134 __msg_init_const_singular
,
139 __msg_iter_subscript_oob
,
142 __msg_iter_compare_bad
,
143 __msg_compare_different
,
144 __msg_iter_order_bad
,
145 __msg_order_different
,
147 __msg_distance_different
,
152 __msg_output_ostream
,
153 // istreambuf_iterator
154 __msg_deref_istreambuf
,
155 __msg_inc_istreambuf
,
157 __msg_insert_after_end
,
158 __msg_erase_after_bad
,
160 // unordered container local iterators
161 __msg_local_iter_compare_bad
,
162 __msg_non_empty_range
,
163 // self move assign (no longer used)
164 __msg_self_move_assign
,
165 // unordered container buckets
166 __msg_bucket_index_oob
,
167 __msg_valid_load_factor
,
170 __msg_insert_range_from_self
,
171 __msg_irreflexive_ordering
174 class _Error_formatter
176 // Tags denoting the type of parameter for construction
177 struct _Is_iterator
{ };
178 struct _Is_iterator_value_type
{ };
179 struct _Is_sequence
{ };
180 struct _Is_instance
{ };
183 /// Whether an iterator is constant, mutable, or unknown
192 // The state of the iterator (fine-grained), if we know it.
196 __singular
, // singular, may still be attached to a sequence
197 __begin
, // dereferenceable, and at the beginning
198 __middle
, // dereferenceable, not at the beginning
199 __end
, // past-the-end, may be at beginning if sequence empty
200 __before_begin
, // before begin
201 __rbegin
, // dereferenceable, and at the reverse-beginning
202 __rmiddle
, // reverse-dereferenceable, not at the reverse-beginning
203 __rend
, // reverse-past-the-end
204 __singular_value_init
, // singular, value initialized
208 // A parameter that may be referenced by an error message
219 __iterator_value_type
227 struct _Type
: _Named
229 const type_info
* _M_type
;
232 struct _Instance
: _Type
234 const void* _M_address
;
239 // When _M_kind == __iterator
242 _Constness _M_constness
;
243 _Iterator_state _M_state
;
244 const void* _M_sequence
;
245 const type_info
* _M_seq_type
;
248 // When _M_kind == __sequence
249 _Instance _M_sequence
;
251 // When _M_kind == __integer
257 // When _M_kind == __string
260 const char* _M_value
;
263 // When _M_kind == __instance
264 _Instance _M_instance
;
266 // When _M_kind == __iterator_value_type
267 _Type _M_iterator_value_type
;
270 _Parameter() : _M_kind(__unused_param
), _M_variant() { }
272 _Parameter(long __value
, const char* __name
)
273 : _M_kind(__integer
), _M_variant()
275 _M_variant
._M_integer
._M_name
= __name
;
276 _M_variant
._M_integer
._M_value
= __value
;
279 _Parameter(const char* __value
, const char* __name
)
280 : _M_kind(__string
), _M_variant()
282 _M_variant
._M_string
._M_name
= __name
;
283 _M_variant
._M_string
._M_value
= __value
;
286 template<typename _Iterator
, typename _Sequence
, typename _Category
>
287 _Parameter(_Safe_iterator
<_Iterator
, _Sequence
, _Category
> const& __it
,
288 const char* __name
, _Is_iterator
)
289 : _M_kind(__iterator
), _M_variant()
291 _M_variant
._M_iterator
._M_name
= __name
;
292 _M_variant
._M_iterator
._M_address
= std::__addressof(__it
);
293 _M_variant
._M_iterator
._M_type
= _GLIBCXX_TYPEID(_Iterator
);
294 _M_variant
._M_iterator
._M_constness
=
295 __it
._S_constant() ? __const_iterator
: __mutable_iterator
;
296 _M_variant
._M_iterator
._M_sequence
= __it
._M_get_sequence();
297 _M_variant
._M_iterator
._M_seq_type
= _GLIBCXX_TYPEID(_Sequence
);
299 if (__it
._M_singular())
301 if (__it
._M_value_initialized())
302 _M_variant
._M_iterator
._M_state
= __singular_value_init
;
304 _M_variant
._M_iterator
._M_state
= __singular
;
308 if (__it
._M_is_before_begin())
309 _M_variant
._M_iterator
._M_state
= __before_begin
;
310 else if (__it
._M_is_end())
311 _M_variant
._M_iterator
._M_state
= __end
;
312 else if (__it
._M_is_begin())
313 _M_variant
._M_iterator
._M_state
= __begin
;
315 _M_variant
._M_iterator
._M_state
= __middle
;
319 template<typename _Iterator
, typename _Sequence
>
320 _Parameter(_Safe_local_iterator
<_Iterator
, _Sequence
> const& __it
,
321 const char* __name
, _Is_iterator
)
322 : _M_kind(__iterator
), _M_variant()
324 _M_variant
._M_iterator
._M_name
= __name
;
325 _M_variant
._M_iterator
._M_address
= std::__addressof(__it
);
326 _M_variant
._M_iterator
._M_type
= _GLIBCXX_TYPEID(_Iterator
);
327 _M_variant
._M_iterator
._M_constness
=
328 __it
._S_constant() ? __const_iterator
: __mutable_iterator
;
329 _M_variant
._M_iterator
._M_sequence
= __it
._M_get_sequence();
330 _M_variant
._M_iterator
._M_seq_type
= _GLIBCXX_TYPEID(_Sequence
);
332 if (__it
._M_singular())
334 if (__it
._M_value_initialized())
335 _M_variant
._M_iterator
._M_state
= __singular_value_init
;
337 _M_variant
._M_iterator
._M_state
= __singular
;
341 if (__it
._M_is_end())
342 _M_variant
._M_iterator
._M_state
= __end
;
343 else if (__it
._M_is_begin())
344 _M_variant
._M_iterator
._M_state
= __begin
;
346 _M_variant
._M_iterator
._M_state
= __middle
;
350 template<typename _Type
>
351 _Parameter(const _Type
* const& __it
, const char* __name
, _Is_iterator
)
352 : _M_kind(__iterator
), _M_variant()
354 _M_variant
._M_iterator
._M_name
= __name
;
355 _M_variant
._M_iterator
._M_address
= std::__addressof(__it
);
356 _M_variant
._M_iterator
._M_type
= _GLIBCXX_TYPEID(__it
);
357 _M_variant
._M_iterator
._M_constness
= __const_iterator
;
358 _M_variant
._M_iterator
._M_state
= __it
? __unknown_state
: __singular
;
359 _M_variant
._M_iterator
._M_sequence
= 0;
360 _M_variant
._M_iterator
._M_seq_type
= 0;
363 template<typename _Type
>
364 _Parameter(_Type
* const& __it
, const char* __name
, _Is_iterator
)
365 : _M_kind(__iterator
), _M_variant()
367 _M_variant
._M_iterator
._M_name
= __name
;
368 _M_variant
._M_iterator
._M_address
= std::__addressof(__it
);
369 _M_variant
._M_iterator
._M_type
= _GLIBCXX_TYPEID(__it
);
370 _M_variant
._M_iterator
._M_constness
= __mutable_iterator
;
371 _M_variant
._M_iterator
._M_state
= __it
? __unknown_state
: __singular
;
372 _M_variant
._M_iterator
._M_sequence
= 0;
373 _M_variant
._M_iterator
._M_seq_type
= 0;
376 template<typename _Iterator
>
377 _Parameter(_Iterator
const& __it
, const char* __name
, _Is_iterator
)
378 : _M_kind(__iterator
), _M_variant()
380 _M_variant
._M_iterator
._M_name
= __name
;
381 _M_variant
._M_iterator
._M_address
= std::__addressof(__it
);
382 _M_variant
._M_iterator
._M_type
= _GLIBCXX_TYPEID(__it
);
383 _M_variant
._M_iterator
._M_constness
= __unknown_constness
;
384 _M_variant
._M_iterator
._M_state
=
385 __gnu_debug::__check_singular(__it
) ? __singular
: __unknown_state
;
386 _M_variant
._M_iterator
._M_sequence
= 0;
387 _M_variant
._M_iterator
._M_seq_type
= 0;
390 #if __cplusplus >= 201103L
391 // The following constructors are only defined in C++11 to take
392 // advantage of the constructor delegation feature.
393 template<typename _Iterator
, typename _Container
>
395 __gnu_cxx::__normal_iterator
<_Iterator
, _Container
> const& __it
,
396 const char* __name
, _Is_iterator
)
397 : _Parameter(__it
.base(), __name
, _Is_iterator
{})
398 { _M_variant
._M_iterator
._M_type
= _GLIBCXX_TYPEID(__it
); }
400 template<typename _Iterator
>
401 _Parameter(std::reverse_iterator
<_Iterator
> const& __it
,
402 const char* __name
, _Is_iterator
)
403 : _Parameter(__it
.base(), __name
, _Is_iterator
{})
405 _M_variant
._M_iterator
._M_type
= _GLIBCXX_TYPEID(__it
);
406 _M_variant
._M_iterator
._M_state
407 = _S_reverse_state(_M_variant
._M_iterator
._M_state
);
410 template<typename _Iterator
, typename _Sequence
, typename _Category
>
411 _Parameter(std::reverse_iterator
<_Safe_iterator
<_Iterator
, _Sequence
,
412 _Category
>> const& __it
,
413 const char* __name
, _Is_iterator
)
414 : _Parameter(__it
.base(), __name
, _Is_iterator
{})
416 _M_variant
._M_iterator
._M_type
417 = _GLIBCXX_TYPEID(std::reverse_iterator
<_Iterator
>);
418 _M_variant
._M_iterator
._M_state
419 = _S_reverse_state(_M_variant
._M_iterator
._M_state
);
422 template<typename _Iterator
>
423 _Parameter(std::move_iterator
<_Iterator
> const& __it
,
424 const char* __name
, _Is_iterator
)
425 : _Parameter(__it
.base(), __name
, _Is_iterator
{})
426 { _M_variant
._M_iterator
._M_type
= _GLIBCXX_TYPEID(__it
); }
428 template<typename _Iterator
, typename _Sequence
, typename _Category
>
429 _Parameter(std::move_iterator
<_Safe_iterator
<_Iterator
, _Sequence
,
430 _Category
>> const& __it
,
431 const char* __name
, _Is_iterator
)
432 : _Parameter(__it
.base(), __name
, _Is_iterator
{})
434 _M_variant
._M_iterator
._M_type
435 = _GLIBCXX_TYPEID(std::move_iterator
<_Iterator
>);
440 _S_reverse_state(_Iterator_state __state
)
458 template<typename _Sequence
>
459 _Parameter(const _Safe_sequence
<_Sequence
>& __seq
,
460 const char* __name
, _Is_sequence
)
461 : _M_kind(__sequence
), _M_variant()
463 _M_variant
._M_sequence
._M_name
= __name
;
464 _M_variant
._M_sequence
._M_address
=
465 static_cast<const _Sequence
*>(std::__addressof(__seq
));
466 _M_variant
._M_sequence
._M_type
= _GLIBCXX_TYPEID(_Sequence
);
469 template<typename _Sequence
>
470 _Parameter(const _Sequence
& __seq
, const char* __name
, _Is_sequence
)
471 : _M_kind(__sequence
), _M_variant()
473 _M_variant
._M_sequence
._M_name
= __name
;
474 _M_variant
._M_sequence
._M_address
= std::__addressof(__seq
);
475 _M_variant
._M_sequence
._M_type
= _GLIBCXX_TYPEID(_Sequence
);
478 template<typename _Iterator
>
479 _Parameter(const _Iterator
& __it
, const char* __name
,
480 _Is_iterator_value_type
)
481 : _M_kind(__iterator_value_type
), _M_variant()
483 _M_variant
._M_iterator_value_type
._M_name
= __name
;
484 _M_variant
._M_iterator_value_type
._M_type
=
485 _GLIBCXX_TYPEID(typename
std::iterator_traits
<_Iterator
>::value_type
);
488 template<typename _Type
>
489 _Parameter(const _Type
& __inst
, const char* __name
, _Is_instance
)
490 : _M_kind(__instance
), _M_variant()
492 _M_variant
._M_instance
._M_name
= __name
;
493 _M_variant
._M_instance
._M_address
= &__inst
;
494 _M_variant
._M_instance
._M_type
= _GLIBCXX_TYPEID(_Type
);
497 #if !_GLIBCXX_INLINE_VERSION
499 _M_print_field(const _Error_formatter
* __formatter
,
500 const char* __name
) const _GLIBCXX_DEPRECATED
;
503 _M_print_description(const _Error_formatter
* __formatter
)
504 const _GLIBCXX_DEPRECATED
;
508 template<typename _Iterator
>
510 _M_iterator(const _Iterator
& __it
, const char* __name
= 0)
512 if (_M_num_parameters
< std::size_t(__max_parameters
))
513 _M_parameters
[_M_num_parameters
++] = _Parameter(__it
, __name
,
518 template<typename _Iterator
>
520 _M_iterator_value_type(const _Iterator
& __it
,
521 const char* __name
= 0)
523 if (_M_num_parameters
< __max_parameters
)
524 _M_parameters
[_M_num_parameters
++] =
525 _Parameter(__it
, __name
, _Is_iterator_value_type());
530 _M_integer(long __value
, const char* __name
= 0)
532 if (_M_num_parameters
< __max_parameters
)
533 _M_parameters
[_M_num_parameters
++] = _Parameter(__value
, __name
);
538 _M_string(const char* __value
, const char* __name
= 0)
540 if (_M_num_parameters
< __max_parameters
)
541 _M_parameters
[_M_num_parameters
++] = _Parameter(__value
, __name
);
545 template<typename _Sequence
>
547 _M_sequence(const _Sequence
& __seq
, const char* __name
= 0)
549 if (_M_num_parameters
< __max_parameters
)
550 _M_parameters
[_M_num_parameters
++] = _Parameter(__seq
, __name
,
555 template<typename _Type
>
557 _M_instance(const _Type
& __inst
, const char* __name
= 0)
559 if (_M_num_parameters
< __max_parameters
)
560 _M_parameters
[_M_num_parameters
++] = _Parameter(__inst
, __name
,
566 _M_message(const char* __text
)
567 { _M_text
= __text
; return *this; }
569 // Kept const qualifier for backward compatibility, to keep the same
572 _M_message(_Debug_msg_id __id
) const throw ();
574 _GLIBCXX_NORETURN
void
577 #if !_GLIBCXX_INLINE_VERSION
578 template<typename _Tp
>
580 _M_format_word(char*, int, const char*, _Tp
)
581 const throw () _GLIBCXX_DEPRECATED
;
584 _M_print_word(const char* __word
) const _GLIBCXX_DEPRECATED
;
587 _M_print_string(const char* __string
) const _GLIBCXX_DEPRECATED
;
591 _Error_formatter(const char* __file
, unsigned int __line
,
592 const char* __function
)
593 : _M_file(__file
), _M_line(__line
), _M_num_parameters(0), _M_text(0)
594 , _M_function(__function
)
595 #if _GLIBCXX_HAVE_STACKTRACE
596 # ifdef _GLIBCXX_DEBUG_BACKTRACE
597 , _M_backtrace_state(__glibcxx_backtrace_create_state(0, 1, _S_err
, 0))
598 , _M_backtrace_full(&__glibcxx_backtrace_full
)
600 , _M_backtrace_state(0)
605 #if !_GLIBCXX_INLINE_VERSION
607 _M_get_max_length() const throw () _GLIBCXX_DEPRECATED
;
610 enum { __max_parameters
= 9 };
613 unsigned int _M_line
;
614 _Parameter _M_parameters
[__max_parameters
];
615 unsigned int _M_num_parameters
;
617 const char* _M_function
;
618 #if _GLIBCXX_HAVE_STACKTRACE
619 struct __glibcxx_backtrace_state
* _M_backtrace_state
;
620 // TODO: Remove _M_backtrace_full after __glibcxx_backtrace_full is moved
621 // from libstdc++_libbacktrace.a to libstdc++.so:
622 __decltype(&__glibcxx_backtrace_full
) _M_backtrace_full
;
624 static void _S_err(void*, const char*, int) { }
628 static _Error_formatter
&
629 _S_at(const char* __file
, unsigned int __line
, const char* __function
)
631 static _Error_formatter
__formatter(__file
, __line
, __function
);
635 } // namespace __gnu_debug
637 #undef _GLIBCXX_TYPEID