|
libstdc++
|
00001 // Debug-mode error formatting implementation -*- C++ -*- 00002 00003 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 00004 // Free Software Foundation, Inc. 00005 // 00006 // This file is part of the GNU ISO C++ Library. This library is free 00007 // software; you can redistribute it and/or modify it under the 00008 // terms of the GNU General Public License as published by the 00009 // Free Software Foundation; either version 3, or (at your option) 00010 // any later version. 00011 00012 // This library is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 00017 // Under Section 7 of GPL version 3, you are granted additional 00018 // permissions described in the GCC Runtime Library Exception, version 00019 // 3.1, as published by the Free Software Foundation. 00020 00021 // You should have received a copy of the GNU General Public License and 00022 // a copy of the GCC Runtime Library Exception along with this program; 00023 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00024 // <http://www.gnu.org/licenses/>. 00025 00026 /** @file debug/formatter.h 00027 * This file is a GNU debug extension to the Standard C++ Library. 00028 */ 00029 00030 #ifndef _GLIBCXX_DEBUG_FORMATTER_H 00031 #define _GLIBCXX_DEBUG_FORMATTER_H 1 00032 00033 #include <bits/c++config.h> 00034 #include <bits/cpp_type_traits.h> 00035 #include <typeinfo> 00036 00037 namespace __gnu_debug 00038 { 00039 using std::type_info; 00040 00041 template<typename _Iterator> 00042 bool __check_singular(_Iterator&); 00043 00044 class _Safe_sequence_base; 00045 00046 template<typename _Iterator, typename _Sequence> 00047 class _Safe_iterator; 00048 00049 template<typename _Iterator, typename _Sequence> 00050 class _Safe_local_iterator; 00051 00052 template<typename _Sequence> 00053 class _Safe_sequence; 00054 00055 enum _Debug_msg_id 00056 { 00057 // General checks 00058 __msg_valid_range, 00059 __msg_insert_singular, 00060 __msg_insert_different, 00061 __msg_erase_bad, 00062 __msg_erase_different, 00063 __msg_subscript_oob, 00064 __msg_empty, 00065 __msg_unpartitioned, 00066 __msg_unpartitioned_pred, 00067 __msg_unsorted, 00068 __msg_unsorted_pred, 00069 __msg_not_heap, 00070 __msg_not_heap_pred, 00071 // std::bitset checks 00072 __msg_bad_bitset_write, 00073 __msg_bad_bitset_read, 00074 __msg_bad_bitset_flip, 00075 // std::list checks 00076 __msg_self_splice, 00077 __msg_splice_alloc, 00078 __msg_splice_bad, 00079 __msg_splice_other, 00080 __msg_splice_overlap, 00081 // iterator checks 00082 __msg_init_singular, 00083 __msg_init_copy_singular, 00084 __msg_init_const_singular, 00085 __msg_copy_singular, 00086 __msg_bad_deref, 00087 __msg_bad_inc, 00088 __msg_bad_dec, 00089 __msg_iter_subscript_oob, 00090 __msg_advance_oob, 00091 __msg_retreat_oob, 00092 __msg_iter_compare_bad, 00093 __msg_compare_different, 00094 __msg_iter_order_bad, 00095 __msg_order_different, 00096 __msg_distance_bad, 00097 __msg_distance_different, 00098 // istream_iterator 00099 __msg_deref_istream, 00100 __msg_inc_istream, 00101 // ostream_iterator 00102 __msg_output_ostream, 00103 // istreambuf_iterator 00104 __msg_deref_istreambuf, 00105 __msg_inc_istreambuf, 00106 // forward_list 00107 __msg_insert_after_end, 00108 __msg_erase_after_bad, 00109 __msg_valid_range2, 00110 // unordered sequence local iterators 00111 __msg_local_iter_compare_bad, 00112 __msg_non_empty_range, 00113 // self move assign 00114 __msg_self_move_assign 00115 }; 00116 00117 class _Error_formatter 00118 { 00119 /// Whether an iterator is constant, mutable, or unknown 00120 enum _Constness 00121 { 00122 __unknown_constness, 00123 __const_iterator, 00124 __mutable_iterator, 00125 __last_constness 00126 }; 00127 00128 // The state of the iterator (fine-grained), if we know it. 00129 enum _Iterator_state 00130 { 00131 __unknown_state, 00132 __singular, // singular, may still be attached to a sequence 00133 __begin, // dereferenceable, and at the beginning 00134 __middle, // dereferenceable, not at the beginning 00135 __end, // past-the-end, may be at beginning if sequence empty 00136 __before_begin, // before begin 00137 __last_state 00138 }; 00139 00140 // Tags denoting the type of parameter for construction 00141 struct _Is_iterator { }; 00142 struct _Is_sequence { }; 00143 00144 // A parameter that may be referenced by an error message 00145 struct _Parameter 00146 { 00147 enum 00148 { 00149 __unused_param, 00150 __iterator, 00151 __sequence, 00152 __integer, 00153 __string 00154 } _M_kind; 00155 00156 union 00157 { 00158 // When _M_kind == __iterator 00159 struct 00160 { 00161 const char* _M_name; 00162 const void* _M_address; 00163 const type_info* _M_type; 00164 _Constness _M_constness; 00165 _Iterator_state _M_state; 00166 const void* _M_sequence; 00167 const type_info* _M_seq_type; 00168 } _M_iterator; 00169 00170 // When _M_kind == __sequence 00171 struct 00172 { 00173 const char* _M_name; 00174 const void* _M_address; 00175 const type_info* _M_type; 00176 } _M_sequence; 00177 00178 // When _M_kind == __integer 00179 struct 00180 { 00181 const char* _M_name; 00182 long _M_value; 00183 } _M_integer; 00184 00185 // When _M_kind == __string 00186 struct 00187 { 00188 const char* _M_name; 00189 const char* _M_value; 00190 } _M_string; 00191 } _M_variant; 00192 00193 _Parameter() : _M_kind(__unused_param), _M_variant() { } 00194 00195 _Parameter(long __value, const char* __name) 00196 : _M_kind(__integer), _M_variant() 00197 { 00198 _M_variant._M_integer._M_name = __name; 00199 _M_variant._M_integer._M_value = __value; 00200 } 00201 00202 _Parameter(const char* __value, const char* __name) 00203 : _M_kind(__string), _M_variant() 00204 { 00205 _M_variant._M_string._M_name = __name; 00206 _M_variant._M_string._M_value = __value; 00207 } 00208 00209 template<typename _Iterator, typename _Sequence> 00210 _Parameter(const _Safe_iterator<_Iterator, _Sequence>& __it, 00211 const char* __name, _Is_iterator) 00212 : _M_kind(__iterator), _M_variant() 00213 { 00214 _M_variant._M_iterator._M_name = __name; 00215 _M_variant._M_iterator._M_address = &__it; 00216 #ifdef __GXX_RTTI 00217 _M_variant._M_iterator._M_type = &typeid(__it); 00218 #else 00219 _M_variant._M_iterator._M_type = 0; 00220 #endif 00221 _M_variant._M_iterator._M_constness = 00222 std::__are_same<_Safe_iterator<_Iterator, _Sequence>, 00223 typename _Sequence::iterator>:: 00224 __value ? __mutable_iterator : __const_iterator; 00225 _M_variant._M_iterator._M_sequence = __it._M_get_sequence(); 00226 #ifdef __GXX_RTTI 00227 _M_variant._M_iterator._M_seq_type = &typeid(_Sequence); 00228 #else 00229 _M_variant._M_iterator._M_seq_type = 0; 00230 #endif 00231 00232 if (__it._M_singular()) 00233 _M_variant._M_iterator._M_state = __singular; 00234 else 00235 { 00236 if (__it._M_is_before_begin()) 00237 _M_variant._M_iterator._M_state = __before_begin; 00238 else if (__it._M_is_end()) 00239 _M_variant._M_iterator._M_state = __end; 00240 else if (__it._M_is_begin()) 00241 _M_variant._M_iterator._M_state = __begin; 00242 else 00243 _M_variant._M_iterator._M_state = __middle; 00244 } 00245 } 00246 00247 template<typename _Iterator, typename _Sequence> 00248 _Parameter(const _Safe_local_iterator<_Iterator, _Sequence>& __it, 00249 const char* __name, _Is_iterator) 00250 : _M_kind(__iterator), _M_variant() 00251 { 00252 _M_variant._M_iterator._M_name = __name; 00253 _M_variant._M_iterator._M_address = &__it; 00254 #ifdef __GXX_RTTI 00255 _M_variant._M_iterator._M_type = &typeid(__it); 00256 #else 00257 _M_variant._M_iterator._M_type = 0; 00258 #endif 00259 _M_variant._M_iterator._M_constness = 00260 std::__are_same<_Safe_local_iterator<_Iterator, _Sequence>, 00261 typename _Sequence::local_iterator>:: 00262 __value ? __mutable_iterator : __const_iterator; 00263 _M_variant._M_iterator._M_sequence = __it._M_get_sequence(); 00264 #ifdef __GXX_RTTI 00265 _M_variant._M_iterator._M_seq_type = &typeid(_Sequence); 00266 #else 00267 _M_variant._M_iterator._M_seq_type = 0; 00268 #endif 00269 00270 if (__it._M_singular()) 00271 _M_variant._M_iterator._M_state = __singular; 00272 else 00273 { 00274 if (__it._M_is_end()) 00275 _M_variant._M_iterator._M_state = __end; 00276 else if (__it._M_is_begin()) 00277 _M_variant._M_iterator._M_state = __begin; 00278 else 00279 _M_variant._M_iterator._M_state = __middle; 00280 } 00281 } 00282 00283 template<typename _Type> 00284 _Parameter(const _Type*& __it, const char* __name, _Is_iterator) 00285 : _M_kind(__iterator), _M_variant() 00286 { 00287 _M_variant._M_iterator._M_name = __name; 00288 _M_variant._M_iterator._M_address = &__it; 00289 #ifdef __GXX_RTTI 00290 _M_variant._M_iterator._M_type = &typeid(__it); 00291 #else 00292 _M_variant._M_iterator._M_type = 0; 00293 #endif 00294 _M_variant._M_iterator._M_constness = __mutable_iterator; 00295 _M_variant._M_iterator._M_state = __it? __unknown_state : __singular; 00296 _M_variant._M_iterator._M_sequence = 0; 00297 _M_variant._M_iterator._M_seq_type = 0; 00298 } 00299 00300 template<typename _Type> 00301 _Parameter(_Type*& __it, const char* __name, _Is_iterator) 00302 : _M_kind(__iterator), _M_variant() 00303 { 00304 _M_variant._M_iterator._M_name = __name; 00305 _M_variant._M_iterator._M_address = &__it; 00306 #ifdef __GXX_RTTI 00307 _M_variant._M_iterator._M_type = &typeid(__it); 00308 #else 00309 _M_variant._M_iterator._M_type = 0; 00310 #endif 00311 _M_variant._M_iterator._M_constness = __const_iterator; 00312 _M_variant._M_iterator._M_state = __it? __unknown_state : __singular; 00313 _M_variant._M_iterator._M_sequence = 0; 00314 _M_variant._M_iterator._M_seq_type = 0; 00315 } 00316 00317 template<typename _Iterator> 00318 _Parameter(const _Iterator& __it, const char* __name, _Is_iterator) 00319 : _M_kind(__iterator), _M_variant() 00320 { 00321 _M_variant._M_iterator._M_name = __name; 00322 _M_variant._M_iterator._M_address = &__it; 00323 #ifdef __GXX_RTTI 00324 _M_variant._M_iterator._M_type = &typeid(__it); 00325 #else 00326 _M_variant._M_iterator._M_type = 0; 00327 #endif 00328 _M_variant._M_iterator._M_constness = __unknown_constness; 00329 _M_variant._M_iterator._M_state = 00330 __gnu_debug::__check_singular(__it)? __singular : __unknown_state; 00331 _M_variant._M_iterator._M_sequence = 0; 00332 _M_variant._M_iterator._M_seq_type = 0; 00333 } 00334 00335 template<typename _Sequence> 00336 _Parameter(const _Safe_sequence<_Sequence>& __seq, 00337 const char* __name, _Is_sequence) 00338 : _M_kind(__sequence), _M_variant() 00339 { 00340 _M_variant._M_sequence._M_name = __name; 00341 _M_variant._M_sequence._M_address = 00342 static_cast<const _Sequence*>(&__seq); 00343 #ifdef __GXX_RTTI 00344 _M_variant._M_sequence._M_type = &typeid(_Sequence); 00345 #else 00346 _M_variant._M_sequence._M_type = 0; 00347 #endif 00348 } 00349 00350 template<typename _Sequence> 00351 _Parameter(const _Sequence& __seq, const char* __name, _Is_sequence) 00352 : _M_kind(__sequence), _M_variant() 00353 { 00354 _M_variant._M_sequence._M_name = __name; 00355 _M_variant._M_sequence._M_address = &__seq; 00356 #ifdef __GXX_RTTI 00357 _M_variant._M_sequence._M_type = &typeid(_Sequence); 00358 #else 00359 _M_variant._M_sequence._M_type = 0; 00360 #endif 00361 } 00362 00363 void 00364 _M_print_field(const _Error_formatter* __formatter, 00365 const char* __name) const; 00366 00367 void 00368 _M_print_description(const _Error_formatter* __formatter) const; 00369 }; 00370 00371 friend struct _Parameter; 00372 00373 public: 00374 template<typename _Iterator> 00375 const _Error_formatter& 00376 _M_iterator(const _Iterator& __it, const char* __name = 0) const 00377 { 00378 if (_M_num_parameters < std::size_t(__max_parameters)) 00379 _M_parameters[_M_num_parameters++] = _Parameter(__it, __name, 00380 _Is_iterator()); 00381 return *this; 00382 } 00383 00384 const _Error_formatter& 00385 _M_integer(long __value, const char* __name = 0) const 00386 { 00387 if (_M_num_parameters < std::size_t(__max_parameters)) 00388 _M_parameters[_M_num_parameters++] = _Parameter(__value, __name); 00389 return *this; 00390 } 00391 00392 const _Error_formatter& 00393 _M_string(const char* __value, const char* __name = 0) const 00394 { 00395 if (_M_num_parameters < std::size_t(__max_parameters)) 00396 _M_parameters[_M_num_parameters++] = _Parameter(__value, __name); 00397 return *this; 00398 } 00399 00400 template<typename _Sequence> 00401 const _Error_formatter& 00402 _M_sequence(const _Sequence& __seq, const char* __name = 0) const 00403 { 00404 if (_M_num_parameters < std::size_t(__max_parameters)) 00405 _M_parameters[_M_num_parameters++] = _Parameter(__seq, __name, 00406 _Is_sequence()); 00407 return *this; 00408 } 00409 00410 const _Error_formatter& 00411 _M_message(const char* __text) const 00412 { _M_text = __text; return *this; } 00413 00414 const _Error_formatter& 00415 _M_message(_Debug_msg_id __id) const throw (); 00416 00417 _GLIBCXX_NORETURN void 00418 _M_error() const; 00419 00420 private: 00421 _Error_formatter(const char* __file, std::size_t __line) 00422 : _M_file(__file), _M_line(__line), _M_num_parameters(0), _M_text(0), 00423 _M_max_length(78), _M_column(1), _M_first_line(true), _M_wordwrap(false) 00424 { _M_get_max_length(); } 00425 00426 template<typename _Tp> 00427 void 00428 _M_format_word(char*, int, const char*, _Tp) const throw (); 00429 00430 void 00431 _M_print_word(const char* __word) const; 00432 00433 void 00434 _M_print_string(const char* __string) const; 00435 00436 void 00437 _M_get_max_length() const throw (); 00438 00439 enum { __max_parameters = 9 }; 00440 00441 const char* _M_file; 00442 std::size_t _M_line; 00443 mutable _Parameter _M_parameters[__max_parameters]; 00444 mutable std::size_t _M_num_parameters; 00445 mutable const char* _M_text; 00446 mutable std::size_t _M_max_length; 00447 enum { _M_indent = 4 } ; 00448 mutable std::size_t _M_column; 00449 mutable bool _M_first_line; 00450 mutable bool _M_wordwrap; 00451 00452 public: 00453 static _Error_formatter 00454 _M_at(const char* __file, std::size_t __line) 00455 { return _Error_formatter(__file, __line); } 00456 }; 00457 } // namespace __gnu_debug 00458 00459 #endif