gcc41 removal: Part 1 of 2: makefiles
[dragonfly.git] / contrib / gcc-4.1 / libstdc++-v3 / src / debug.cc
1 // Debugging mode support code -*- C++ -*-
2
3 // Copyright (C) 2003, 2004, 2005
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 2, 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 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING.  If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction.  Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License.  This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 #include <debug/debug.h>
32 #include <debug/safe_sequence.h>
33 #include <debug/safe_iterator.h>
34 #include <algorithm>
35 #include <cassert>
36 #include <cstring>
37 #include <cctype>
38 #include <bits/concurrence.h>
39
40 using namespace std;
41
42 namespace __gnu_internal
43 {
44   static __glibcxx_mutex_define_initialized(iterator_base_mutex);
45 } // namespace __gnu_internal
46
47 namespace __gnu_debug
48 {
49   const char* _S_debug_messages[] = 
50   {
51     "function requires a valid iterator range [%1.name;, %2.name;)",
52     "attempt to insert into container with a singular iterator",
53     "attempt to insert into container with an iterator"
54     " from a different container",
55     "attempt to erase from container with a %2.state; iterator",
56     "attempt to erase from container with an iterator"
57     " from a different container",
58     "attempt to subscript container with out-of-bounds index %2;,"
59     " but container only holds %3; elements",
60     "attempt to access an element in an empty container",
61     "elements in iterator range [%1.name;, %2.name;)"
62     " are not partitioned by the value %3;",
63     "elements in iterator range [%1.name;, %2.name;)"
64     " are not partitioned by the predicate %3; and value %4;",
65     "elements in iterator range [%1.name;, %2.name;) are not sorted",
66     "elements in iterator range [%1.name;, %2.name;)"
67     " are not sorted according to the predicate %3;",
68     "elements in iterator range [%1.name;, %2.name;) do not form a heap",
69     "elements in iterator range [%1.name;, %2.name;)"
70     " do not form a heap with respect to the predicate %3;",
71     "attempt to write through a singular bitset reference",
72     "attempt to read from a singular bitset reference",
73     "attempt to flip a singular bitset reference",
74     "attempt to splice a list into itself",
75     "attempt to splice lists with inequal allocators",
76     "attempt to splice elements referenced by a %1.state; iterator",
77     "attempt to splice an iterator from a different container",
78     "splice destination %1.name;"
79     " occurs within source range [%2.name;, %3.name;)",
80     "attempt to initialize an iterator that will immediately become singular",
81     "attempt to copy-construct an iterator from a singular iterator",
82     "attempt to construct a constant iterator"
83     " from a singular mutable iterator",
84     "attempt to copy from a singular iterator",
85     "attempt to dereference a %1.state; iterator",
86     "attempt to increment a %1.state; iterator",
87     "attempt to decrement a %1.state; iterator",
88     "attempt to subscript a %1.state; iterator %2; step from"
89     " its current position, which falls outside its dereferenceable range",
90     "attempt to advance a %1.state; iterator %2; steps,"
91     " which falls outside its valid range",
92     "attempt to retreat a %1.state; iterator %2; steps,"
93     " which falls outside its valid range",
94     "attempt to compare a %1.state; iterator to a %2.state; iterator",
95     "attempt to compare iterators from different sequences",
96     "attempt to order a %1.state; iterator to a %2.state; iterator",
97     "attempt to order iterators from different sequences",
98     "attempt to compute the difference between a %1.state;"
99     " iterator to a %2.state; iterator",
100     "attempt to compute the different between two iterators"
101     " from different sequences",
102     "attempt to dereference an end-of-stream istream_iterator",
103     "attempt to increment an end-of-stream istream_iterator",
104     "attempt to output via an ostream_iterator with no associated stream",
105     "attempt to dereference an end-of-stream istreambuf_iterator"
106     " (this is a GNU extension)",
107     "attempt to increment an end-of-stream istreambuf_iterator"
108   };
109
110   void 
111   _Safe_sequence_base::
112   _M_detach_all()
113   {
114     for (_Safe_iterator_base* __iter = _M_iterators; __iter; )
115       {
116         _Safe_iterator_base* __old = __iter;
117         __iter = __iter->_M_next;
118         __old->_M_attach(0, false);
119       }
120     
121     for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2; )
122       {
123         _Safe_iterator_base* __old = __iter2;
124         __iter2 = __iter2->_M_next;
125         __old->_M_attach(0, true);
126       }
127   }
128
129   void 
130   _Safe_sequence_base::
131   _M_detach_singular()
132   {
133     for (_Safe_iterator_base* __iter = _M_iterators; __iter; )
134       {
135         _Safe_iterator_base* __old = __iter;
136         __iter = __iter->_M_next;
137         if (__old->_M_singular())
138           __old->_M_attach(0, false);
139       }
140
141     for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2; )
142       {
143         _Safe_iterator_base* __old = __iter2;
144         __iter2 = __iter2->_M_next;
145         if (__old->_M_singular())
146           __old->_M_attach(0, true);
147       }
148   }
149
150   void
151   _Safe_sequence_base::
152   _M_revalidate_singular()
153   {
154     for (_Safe_iterator_base* __iter = _M_iterators; __iter;
155          __iter = __iter->_M_next)
156       __iter->_M_version = _M_version;
157
158     for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2;
159          __iter2 = __iter2->_M_next)
160       __iter2->_M_version = _M_version;
161   }
162
163   void 
164   _Safe_sequence_base::
165   _M_swap(_Safe_sequence_base& __x)
166   {
167     swap(_M_iterators, __x._M_iterators);
168     swap(_M_const_iterators, __x._M_const_iterators);
169     swap(_M_version, __x._M_version);
170     _Safe_iterator_base* __iter;
171     for (__iter = _M_iterators; __iter; __iter = __iter->_M_next)
172       __iter->_M_sequence = this;
173     for (__iter = __x._M_iterators; __iter; __iter = __iter->_M_next)
174       __iter->_M_sequence = &__x;
175     for (__iter = _M_const_iterators; __iter; __iter = __iter->_M_next)
176       __iter->_M_sequence = this;
177     for (__iter = __x._M_const_iterators; __iter; __iter = __iter->_M_next)
178       __iter->_M_sequence = &__x;
179   }
180   
181   void 
182   _Safe_iterator_base::
183   _M_attach(_Safe_sequence_base* __seq, bool __constant)
184   {
185     _M_detach();
186     
187     // Attach to the new sequence (if there is one)
188     if (__seq)
189       {
190         __gnu_cxx::lock sentry(__gnu_internal::iterator_base_mutex);
191         _M_sequence = __seq;
192         _M_version = _M_sequence->_M_version;
193         _M_prior = 0;
194         if (__constant)
195           {
196             _M_next = _M_sequence->_M_const_iterators;
197             if (_M_next)
198               _M_next->_M_prior = this;
199             _M_sequence->_M_const_iterators = this;
200           }
201         else
202           {
203             _M_next = _M_sequence->_M_iterators;
204             if (_M_next)
205               _M_next->_M_prior = this;
206             _M_sequence->_M_iterators = this;
207           }
208       }
209   }
210
211   void 
212   _Safe_iterator_base::
213   _M_detach()
214   {
215     __gnu_cxx::lock sentry(__gnu_internal::iterator_base_mutex);
216     if (_M_sequence)
217       {
218         // Remove us from this sequence's list
219         if (_M_prior) 
220           _M_prior->_M_next = _M_next;
221         if (_M_next)  
222           _M_next->_M_prior = _M_prior;
223         
224         if (_M_sequence->_M_const_iterators == this)
225           _M_sequence->_M_const_iterators = _M_next;
226         if (_M_sequence->_M_iterators == this)
227           _M_sequence->_M_iterators = _M_next;
228       }
229
230     _M_sequence = 0;
231     _M_version = 0;
232     _M_prior = 0;
233     _M_next = 0;
234   }
235   
236   bool
237   _Safe_iterator_base::
238   _M_singular() const
239   { return !_M_sequence || _M_version != _M_sequence->_M_version; }
240     
241   bool
242   _Safe_iterator_base::
243   _M_can_compare(const _Safe_iterator_base& __x) const
244   {
245     return (!_M_singular() && !__x._M_singular() 
246             && _M_sequence == __x._M_sequence);
247   }
248
249   void
250   _Error_formatter::_Parameter::
251   _M_print_field(const _Error_formatter* __formatter, const char* __name) const
252   {
253     assert(this->_M_kind != _Parameter::__unused_param);
254     const int __bufsize = 64;
255     char __buf[__bufsize];
256     
257     if (_M_kind == __iterator)
258       {
259         if (strcmp(__name, "name") == 0)
260           {
261             assert(_M_variant._M_iterator._M_name);
262             __formatter->_M_print_word(_M_variant._M_iterator._M_name);
263           }
264         else if (strcmp(__name, "address") == 0)
265           {
266             __formatter->_M_format_word(__buf, __bufsize, "%p", 
267                                         _M_variant._M_iterator._M_address);
268             __formatter->_M_print_word(__buf);
269           }
270         else if (strcmp(__name, "type") == 0)
271           {
272             assert(_M_variant._M_iterator._M_type);
273             // TBD: demangle!
274             __formatter->_M_print_word(_M_variant._M_iterator._M_type->name());
275           }
276         else if (strcmp(__name, "constness") == 0)
277           {
278             static const char* __constness_names[__last_constness] =
279               {
280                 "<unknown>",
281                 "constant",
282                 "mutable"
283               };
284             __formatter->_M_print_word(__constness_names[_M_variant._M_iterator._M_constness]);
285           }
286         else if (strcmp(__name, "state") == 0)
287           {
288             static const char* __state_names[__last_state] = 
289               {
290                 "<unknown>",
291                 "singular",
292                 "dereferenceable (start-of-sequence)",
293                 "dereferenceable",
294                 "past-the-end"
295               };
296             __formatter->_M_print_word(__state_names[_M_variant._M_iterator._M_state]);
297           }
298         else if (strcmp(__name, "sequence") == 0)
299           {
300             assert(_M_variant._M_iterator._M_sequence);
301             __formatter->_M_format_word(__buf, __bufsize, "%p", 
302                                         _M_variant._M_iterator._M_sequence);
303             __formatter->_M_print_word(__buf);
304           }
305         else if (strcmp(__name, "seq_type") == 0)
306           {
307             // TBD: demangle!
308             assert(_M_variant._M_iterator._M_seq_type);
309             __formatter->_M_print_word(_M_variant._M_iterator._M_seq_type->name());
310           }
311         else
312           assert(false);
313       }
314     else if (_M_kind == __sequence)
315       {
316         if (strcmp(__name, "name") == 0)
317           {
318             assert(_M_variant._M_sequence._M_name);
319             __formatter->_M_print_word(_M_variant._M_sequence._M_name);
320           }
321         else if (strcmp(__name, "address") == 0)
322           {
323             assert(_M_variant._M_sequence._M_address);
324             __formatter->_M_format_word(__buf, __bufsize, "%p", 
325                                         _M_variant._M_sequence._M_address);
326             __formatter->_M_print_word(__buf);
327           }
328         else if (strcmp(__name, "type") == 0)
329           {
330             // TBD: demangle!
331             assert(_M_variant._M_sequence._M_type);
332             __formatter->_M_print_word(_M_variant._M_sequence._M_type->name());
333           }
334         else
335           assert(false);
336       }
337     else if (_M_kind == __integer)
338       {
339         if (strcmp(__name, "name") == 0)
340           {
341             assert(_M_variant._M_integer._M_name);
342             __formatter->_M_print_word(_M_variant._M_integer._M_name);
343           }
344         else
345         assert(false);
346       }
347     else if (_M_kind == __string)
348       {
349         if (strcmp(__name, "name") == 0)
350           {
351             assert(_M_variant._M_string._M_name);
352             __formatter->_M_print_word(_M_variant._M_string._M_name);
353           }
354         else
355           assert(false);
356       }
357     else
358       {
359         assert(false);
360       }
361   }
362   
363   void
364   _Error_formatter::_Parameter::
365   _M_print_description(const _Error_formatter* __formatter) const
366   {
367     const int __bufsize = 128;
368     char __buf[__bufsize];
369     
370     if (_M_kind == __iterator)
371       {
372         __formatter->_M_print_word("iterator ");
373         if (_M_variant._M_iterator._M_name)
374           {
375             __formatter->_M_format_word(__buf, __bufsize, "\"%s\" ", 
376                                         _M_variant._M_iterator._M_name);
377             __formatter->_M_print_word(__buf);
378           }
379         
380         __formatter->_M_format_word(__buf, __bufsize, "@ 0x%p {\n", 
381                                     _M_variant._M_iterator._M_address);
382         __formatter->_M_print_word(__buf);
383         if (_M_variant._M_iterator._M_type)
384           {
385             __formatter->_M_print_word("type = ");
386             _M_print_field(__formatter, "type");
387             
388             if (_M_variant._M_iterator._M_constness != __unknown_constness)
389               {
390                 __formatter->_M_print_word(" (");
391                 _M_print_field(__formatter, "constness");
392                 __formatter->_M_print_word(" iterator)");
393               }
394             __formatter->_M_print_word(";\n");
395           }
396         
397         if (_M_variant._M_iterator._M_state != __unknown_state)
398           {
399             __formatter->_M_print_word("  state = ");
400             _M_print_field(__formatter, "state");
401             __formatter->_M_print_word(";\n");
402           }
403         
404         if (_M_variant._M_iterator._M_sequence)
405           {
406             __formatter->_M_print_word("  references sequence ");
407             if (_M_variant._M_iterator._M_seq_type)
408               {
409                 __formatter->_M_print_word("with type `");
410                 _M_print_field(__formatter, "seq_type");
411                 __formatter->_M_print_word("' ");
412               }
413             
414             __formatter->_M_format_word(__buf, __bufsize, "@ 0x%p\n", 
415                                         _M_variant._M_sequence._M_address);
416             __formatter->_M_print_word(__buf);
417           }
418         __formatter->_M_print_word("}\n");
419       }
420     else if (_M_kind == __sequence)
421       {
422         __formatter->_M_print_word("sequence ");
423         if (_M_variant._M_sequence._M_name)
424           {
425             __formatter->_M_format_word(__buf, __bufsize, "\"%s\" ", 
426                                         _M_variant._M_sequence._M_name);
427             __formatter->_M_print_word(__buf);
428           }
429         
430         __formatter->_M_format_word(__buf, __bufsize, "@ 0x%p {\n", 
431                                     _M_variant._M_sequence._M_address);
432         __formatter->_M_print_word(__buf);
433         
434         if (_M_variant._M_sequence._M_type)
435           {
436             __formatter->_M_print_word("  type = ");
437             _M_print_field(__formatter, "type");
438             __formatter->_M_print_word(";\n");
439           }       
440         __formatter->_M_print_word("}\n");
441       }
442   }
443
444   const _Error_formatter&
445   _Error_formatter::_M_message(_Debug_msg_id __id) const
446   { return this->_M_message(_S_debug_messages[__id]); }
447   
448   void
449   _Error_formatter::_M_error() const
450   {
451     const int __bufsize = 128;
452     char __buf[__bufsize];
453     
454     // Emit file & line number information
455     _M_column = 1;
456     _M_wordwrap = false;
457     if (_M_file)
458       {
459         _M_format_word(__buf, __bufsize, "%s:", _M_file);
460         _M_print_word(__buf);
461         _M_column += strlen(__buf);
462       }
463     
464     if (_M_line > 0)
465       {
466         _M_format_word(__buf, __bufsize, "%u:", _M_line);
467         _M_print_word(__buf);
468         _M_column += strlen(__buf);
469       }
470     
471     _M_wordwrap = true;
472     _M_print_word("error: ");
473     
474     // Print the error message
475     assert(_M_text);
476     _M_print_string(_M_text);
477     _M_print_word(".\n");
478     
479     // Emit descriptions of the objects involved in the operation
480     _M_wordwrap = false;
481     bool __has_noninteger_parameters = false;
482     for (unsigned int __i = 0; __i < _M_num_parameters; ++__i)
483       {
484         if (_M_parameters[__i]._M_kind == _Parameter::__iterator
485             || _M_parameters[__i]._M_kind == _Parameter::__sequence)
486           {
487             if (!__has_noninteger_parameters)
488               {
489                 _M_first_line = true;
490                 _M_print_word("\nObjects involved in the operation:\n");
491                 __has_noninteger_parameters = true;
492               }
493             _M_parameters[__i]._M_print_description(this);
494           }
495       }
496     
497     abort();
498   }
499
500   template<typename _Tp>
501     void
502     _Error_formatter::_M_format_word(char* __buf, 
503                                      int __n __attribute__ ((__unused__)), 
504                                      const char* __fmt, _Tp __s) const
505     {
506 #ifdef _GLIBCXX_USE_C99
507       std::snprintf(__buf, __n, __fmt, __s);
508 #else
509       std::sprintf(__buf, __fmt, __s);
510 #endif
511     }
512
513   
514   void 
515   _Error_formatter::_M_print_word(const char* __word) const
516   {
517     if (!_M_wordwrap) 
518       {
519         fprintf(stderr, "%s", __word);
520         return;
521       }
522     
523     size_t __length = strlen(__word);
524     if (__length == 0)
525       return;
526     
527     if ((_M_column + __length < _M_max_length)
528         || (__length >= _M_max_length && _M_column == 1)) 
529       {
530         // If this isn't the first line, indent
531         if (_M_column == 1 && !_M_first_line)
532           {
533             char __spacing[_M_indent + 1];
534             for (int i = 0; i < _M_indent; ++i)
535               __spacing[i] = ' ';
536             __spacing[_M_indent] = '\0';
537             fprintf(stderr, "%s", __spacing);
538             _M_column += _M_indent;
539           }
540         
541         fprintf(stderr, "%s", __word);
542         _M_column += __length;
543         
544         if (__word[__length - 1] == '\n') 
545           {
546             _M_first_line = false;
547             _M_column = 1;
548           }
549       }
550     else
551       {
552         _M_column = 1;
553         _M_print_word("\n");
554         _M_print_word(__word);
555       }
556   }
557   
558   void
559   _Error_formatter::
560   _M_print_string(const char* __string) const
561   {
562     const char* __start = __string;
563     const char* __end = __start;
564     const int __bufsize = 128;
565     char __buf[__bufsize];
566
567     while (*__start)
568       {
569         if (*__start != '%')
570           {
571             // [__start, __end) denotes the next word
572             __end = __start;
573             while (isalnum(*__end))
574               ++__end;
575             if (__start == __end)
576               ++__end;
577             if (isspace(*__end))
578               ++__end;
579             
580             const ptrdiff_t __len = __end - __start;
581             assert(__len < __bufsize);
582             memcpy(__buf, __start, __len);
583             __buf[__len] = '\0';
584             _M_print_word(__buf);
585             __start = __end;
586             
587             // Skip extra whitespace
588             while (*__start == ' ') 
589               ++__start;
590             
591             continue;
592           } 
593         
594         ++__start;
595         assert(*__start);
596         if (*__start == '%')
597           {
598             _M_print_word("%");
599             ++__start;
600             continue;
601           }
602         
603         // Get the parameter number
604         assert(*__start >= '1' && *__start <= '9');
605         size_t __param = *__start - '0';
606         --__param;
607         assert(__param < _M_num_parameters);
608       
609         // '.' separates the parameter number from the field
610         // name, if there is one.
611         ++__start;
612         if (*__start != '.')
613           {
614             assert(*__start == ';');
615             ++__start;
616             __buf[0] = '\0';
617             if (_M_parameters[__param]._M_kind == _Parameter::__integer)
618               {
619                 _M_format_word(__buf, __bufsize, "%ld", 
620                                _M_parameters[__param]._M_variant._M_integer._M_value);
621                 _M_print_word(__buf);
622               }
623             else if (_M_parameters[__param]._M_kind == _Parameter::__string)
624               _M_print_string(_M_parameters[__param]._M_variant._M_string._M_value);
625             continue;
626           }
627         
628         // Extract the field name we want
629         enum { __max_field_len = 16 };
630         char __field[__max_field_len];
631         int __field_idx = 0;
632         ++__start;
633         while (*__start != ';')
634           {
635             assert(*__start);
636             assert(__field_idx < __max_field_len-1);
637             __field[__field_idx++] = *__start++;
638           }
639         ++__start;
640         __field[__field_idx] = 0;
641         
642         _M_parameters[__param]._M_print_field(this, __field);             
643       }
644   }
645
646   // Instantiations.
647   template
648     void
649     _Error_formatter::_M_format_word(char*, int, const char*, 
650                                      const void*) const;
651
652   template
653     void
654     _Error_formatter::_M_format_word(char*, int, const char*, long) const;
655
656   template
657     void
658     _Error_formatter::_M_format_word(char*, int, const char*, 
659                                      std::size_t) const;
660
661   template
662     void
663     _Error_formatter::_M_format_word(char*, int, const char*, 
664                                      const char*) const;
665 } // namespace __gnu_debug