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