1// Debug-mode error formatting implementation -*- C++ -*- 2 3// Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 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 <typeinfo> 35#include <debug/debug.h> 36 37namespace __gnu_debug 38{ 39 using std::type_info; 40 41 /** Determine if the two types are the same. */ 42 template<typename _Type1, typename _Type2> 43 struct __is_same 44 { 45 static const bool value = false; 46 }; 47 48 template<typename _Type> 49 struct __is_same<_Type, _Type> 50 { 51 static const bool value = true; 52 }; 53 54 template<bool> struct __truth { }; 55 56 class _Safe_sequence_base; 57 58 template<typename _Iterator, typename _Sequence> 59 class _Safe_iterator; 60 61 template<typename _Sequence> 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, 114 __msg_inc_istreambuf 115 }; 116 117 class _Error_formatter 118 { 119 /// Whether an iterator is constant, mutable, or unknown 120 enum _Constness 121 { 122 __unknown_constness, 123 __const_iterator, 124 __mutable_iterator, 125 __last_constness 126 }; 127 128 // The state of the iterator (fine-grained), if we know it. 129 enum _Iterator_state 130 { 131 __unknown_state, 132 __singular, // singular, may still be attached to a sequence 133 __begin, // dereferenceable, and at the beginning 134 __middle, // dereferenceable, not at the beginning 135 __end, // past-the-end, may be at beginning if sequence empty 136 __last_state 137 }; 138 139 // Tags denoting the type of parameter for construction 140 struct _Is_iterator { }; 141 struct _Is_sequence { }; 142 143 // A parameter that may be referenced by an error message 144 struct _Parameter 145 { 146 enum 147 { 148 __unused_param, 149 __iterator, 150 __sequence, 151 __integer, 152 __string 153 } _M_kind; 154 155 union 156 { 157 // When _M_kind == __iterator 158 struct 159 { 160 const char* _M_name; 161 const void* _M_address; 162 const type_info* _M_type; 163 _Constness _M_constness; 164 _Iterator_state _M_state; 165 const void* _M_sequence; 166 const type_info* _M_seq_type; 167 } _M_iterator; 168 169 // When _M_kind == __sequence 170 struct 171 { 172 const char* _M_name; 173 const void* _M_address; 174 const type_info* _M_type; 175 } _M_sequence; 176 177 // When _M_kind == __integer 178 struct 179 { 180 const char* _M_name; 181 long _M_value; 182 } _M_integer; 183 184 // When _M_kind == __string 185 struct 186 { 187 const char* _M_name; 188 const char* _M_value; 189 } _M_string; 190 } _M_variant; 191 192 _Parameter() : _M_kind(__unused_param), _M_variant() { } 193 194 _Parameter(long __value, const char* __name) 195 : _M_kind(__integer), _M_variant() 196 { 197 _M_variant._M_integer._M_name = __name; 198 _M_variant._M_integer._M_value = __value; 199 } 200 201 _Parameter(const char* __value, const char* __name) 202 : _M_kind(__string), _M_variant() 203 { 204 _M_variant._M_string._M_name = __name; 205 _M_variant._M_string._M_value = __value; 206 } 207 208 template<typename _Iterator, typename _Sequence> 209 _Parameter(const _Safe_iterator<_Iterator, _Sequence>& __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 = &__it; 215#ifdef __GXX_RTTI 216 _M_variant._M_iterator._M_type = &typeid(__it); 217#else 218 _M_variant._M_iterator._M_type = 0; 219#endif 220 _M_variant._M_iterator._M_constness = 221 __is_same<_Safe_iterator<_Iterator, _Sequence>, 222 typename _Sequence::iterator>:: 223 value? __mutable_iterator : __const_iterator; 224 _M_variant._M_iterator._M_sequence = __it._M_get_sequence(); 225#ifdef __GXX_RTTI 226 _M_variant._M_iterator._M_seq_type = &typeid(_Sequence); 227#else 228 _M_variant._M_iterator._M_seq_type = 0; 229#endif 230 231 if (__it._M_singular()) 232 _M_variant._M_iterator._M_state = __singular; 233 else 234 { 235 bool __is_begin = __it._M_is_begin(); 236 bool __is_end = __it._M_is_end(); 237 if (__is_end) 238 _M_variant._M_iterator._M_state = __end; 239 else if (__is_begin) 240 _M_variant._M_iterator._M_state = __begin; 241 else 242 _M_variant._M_iterator._M_state = __middle; 243 } 244 } 245 246 template<typename _Type> 247 _Parameter(const _Type*& __it, 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 = __mutable_iterator; 258 _M_variant._M_iterator._M_state = __it? __unknown_state : __singular; 259 _M_variant._M_iterator._M_sequence = 0; 260 _M_variant._M_iterator._M_seq_type = 0; 261 } 262 263 template<typename _Type> 264 _Parameter(_Type*& __it, const char* __name, _Is_iterator) 265 : _M_kind(__iterator), _M_variant() 266 { 267 _M_variant._M_iterator._M_name = __name; 268 _M_variant._M_iterator._M_address = &__it; 269#ifdef __GXX_RTTI 270 _M_variant._M_iterator._M_type = &typeid(__it); 271#else 272 _M_variant._M_iterator._M_type = 0; 273#endif 274 _M_variant._M_iterator._M_constness = __const_iterator; 275 _M_variant._M_iterator._M_state = __it? __unknown_state : __singular; 276 _M_variant._M_iterator._M_sequence = 0; 277 _M_variant._M_iterator._M_seq_type = 0; 278 } 279 280 template<typename _Iterator> 281 _Parameter(const _Iterator& __it, const char* __name, _Is_iterator) 282 : _M_kind(__iterator), _M_variant() 283 { 284 _M_variant._M_iterator._M_name = __name; 285 _M_variant._M_iterator._M_address = &__it; 286#ifdef __GXX_RTTI 287 _M_variant._M_iterator._M_type = &typeid(__it); 288#else 289 _M_variant._M_iterator._M_type = 0; 290#endif 291 _M_variant._M_iterator._M_constness = __unknown_constness; 292 _M_variant._M_iterator._M_state = 293 __gnu_debug::__check_singular(__it)? __singular : __unknown_state; 294 _M_variant._M_iterator._M_sequence = 0; 295 _M_variant._M_iterator._M_seq_type = 0; 296 } 297 298 template<typename _Sequence> 299 _Parameter(const _Safe_sequence<_Sequence>& __seq, 300 const char* __name, _Is_sequence) 301 : _M_kind(__sequence), _M_variant() 302 { 303 _M_variant._M_sequence._M_name = __name; 304 _M_variant._M_sequence._M_address = 305 static_cast<const _Sequence*>(&__seq); 306#ifdef __GXX_RTTI 307 _M_variant._M_sequence._M_type = &typeid(_Sequence); 308#else 309 _M_variant._M_sequence._M_type = 0; 310#endif 311 } 312 313 template<typename _Sequence> 314 _Parameter(const _Sequence& __seq, const char* __name, _Is_sequence) 315 : _M_kind(__sequence), _M_variant() 316 { 317 _M_variant._M_sequence._M_name = __name; 318 _M_variant._M_sequence._M_address = &__seq; 319#ifdef __GXX_RTTI 320 _M_variant._M_sequence._M_type = &typeid(_Sequence); 321#else 322 _M_variant._M_sequence._M_type = 0; 323#endif 324 } 325 326 void 327 _M_print_field(const _Error_formatter* __formatter, 328 const char* __name) const; 329 330 void 331 _M_print_description(const _Error_formatter* __formatter) const; 332 }; 333 334 friend struct _Parameter; 335 336 public: 337 template<typename _Iterator> 338 const _Error_formatter& 339 _M_iterator(const _Iterator& __it, const char* __name = 0) const 340 { 341 if (_M_num_parameters < size_t(__max_parameters)) 342 _M_parameters[_M_num_parameters++] = _Parameter(__it, __name, 343 _Is_iterator()); 344 return *this; 345 } 346 347 const _Error_formatter& 348 _M_integer(long __value, const char* __name = 0) const 349 { 350 if (_M_num_parameters < size_t(__max_parameters)) 351 _M_parameters[_M_num_parameters++] = _Parameter(__value, __name); 352 return *this; 353 } 354 355 const _Error_formatter& 356 _M_string(const char* __value, const char* __name = 0) const 357 { 358 if (_M_num_parameters < size_t(__max_parameters)) 359 _M_parameters[_M_num_parameters++] = _Parameter(__value, __name); 360 return *this; 361 } 362 363 template<typename _Sequence> 364 const _Error_formatter& 365 _M_sequence(const _Sequence& __seq, const char* __name = 0) const 366 { 367 if (_M_num_parameters < size_t(__max_parameters)) 368 _M_parameters[_M_num_parameters++] = _Parameter(__seq, __name, 369 _Is_sequence()); 370 return *this; 371 } 372 373 const _Error_formatter& 374 _M_message(const char* __text) const 375 { _M_text = __text; return *this; } 376 377 const _Error_formatter& 378 _M_message(_Debug_msg_id __id) const throw (); 379 380 _GLIBCXX_NORETURN void 381 _M_error() const; 382 383 private: 384 _Error_formatter(const char* __file, size_t __line) 385 : _M_file(__file), _M_line(__line), _M_num_parameters(0), _M_text(0), 386 _M_max_length(78), _M_column(1), _M_first_line(true), _M_wordwrap(false) 387 { _M_get_max_length(); } 388 389 template<typename _Tp> 390 void 391 _M_format_word(char*, int, const char*, _Tp) const throw (); 392 393 void 394 _M_print_word(const char* __word) const; 395 396 void 397 _M_print_string(const char* __string) const; 398 399 void 400 _M_get_max_length() const throw (); 401 402 enum { __max_parameters = 9 }; 403 404 const char* _M_file; 405 size_t _M_line; 406 mutable _Parameter _M_parameters[__max_parameters]; 407 mutable size_t _M_num_parameters; 408 mutable const char* _M_text; 409 mutable size_t _M_max_length; 410 enum { _M_indent = 4 } ; 411 mutable size_t _M_column; 412 mutable bool _M_first_line; 413 mutable bool _M_wordwrap; 414 415 public: 416 static _Error_formatter 417 _M_at(const char* __file, size_t __line) 418 { return _Error_formatter(__file, __line); } 419 }; 420} // namespace __gnu_debug 421 422#endif 423