OSDN Git Service

* objc/objc-act.c (warn_with_ivar): Adjust calls to
[pf3gnuchains/gcc-fork.git] / gcc / diagnostic.c
1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2    Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3    Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22
23 /* This file implements the language independent aspect of diagnostic
24    message module.  */
25
26 #include "config.h"
27 #undef FLOAT /* This is for hpux. They should change hpux.  */
28 #undef FFS  /* Some systems define this in param.h.  */
29 #include "system.h"
30 #include "tree.h"
31 #include "tm_p.h"
32 #include "flags.h"
33 #include "input.h"
34 #include "toplev.h"
35 #include "intl.h"
36 #include "diagnostic.h"
37 #include "langhooks.h"
38 #include "langhooks-def.h"
39
40 #define obstack_chunk_alloc xmalloc
41 #define obstack_chunk_free  free
42
43 #define output_formatted_integer(BUFFER, FORMAT, INTEGER)       \
44   do                                                            \
45     {                                                           \
46       sprintf ((BUFFER)->digit_buffer, FORMAT, INTEGER);        \
47       output_add_string (BUFFER, (BUFFER)->digit_buffer);       \
48     }                                                           \
49   while (0)
50
51 #define output_text_length(BUFFER) (BUFFER)->line_length
52 #define is_starting_newline(BUFFER) (output_text_length (BUFFER) == 0)
53 #define line_wrap_cutoff(BUFFER) (BUFFER)->state.maximum_length
54 #define prefix_was_emitted_for(BUFFER) (BUFFER)->state.emitted_prefix_p
55
56 /* Prototypes.  */
57 static void output_flush PARAMS ((output_buffer *));
58 static void output_do_verbatim PARAMS ((output_buffer *, text_info *));
59 static void output_buffer_to_stream PARAMS ((output_buffer *));
60 static void output_format PARAMS ((output_buffer *, text_info *));
61 static void output_indent PARAMS ((output_buffer *));
62
63 static char *vbuild_message_string PARAMS ((const char *, va_list))
64      ATTRIBUTE_PRINTF (1, 0);
65 static char *build_message_string PARAMS ((const char *, ...))
66      ATTRIBUTE_PRINTF_1;
67 static void format_with_decl PARAMS ((output_buffer *, text_info *, tree));
68 static void diagnostic_for_decl PARAMS ((diagnostic_info *, tree));
69 static void set_real_maximum_length PARAMS ((output_buffer *));
70
71 static void output_unsigned_decimal PARAMS ((output_buffer *, unsigned int));
72 static void output_long_decimal PARAMS ((output_buffer *, long int));
73 static void output_long_unsigned_decimal PARAMS ((output_buffer *,
74                                                   long unsigned int));
75 static void output_octal PARAMS ((output_buffer *, unsigned int));
76 static void output_long_octal PARAMS ((output_buffer *, unsigned long int));
77 static void output_hexadecimal PARAMS ((output_buffer *, unsigned int));
78 static void output_long_hexadecimal PARAMS ((output_buffer *,
79                                              unsigned long int));
80 static void output_append_r PARAMS ((output_buffer *, const char *, int));
81 static void wrap_text PARAMS ((output_buffer *, const char *, const char *));
82 static void maybe_wrap_text PARAMS ((output_buffer *, const char *,
83                                      const char *));
84 static void output_clear_data PARAMS ((output_buffer *));
85
86 static void default_diagnostic_starter PARAMS ((diagnostic_context *,
87                                                 diagnostic_info *));
88 static void default_diagnostic_finalizer PARAMS ((diagnostic_context *,
89                                                   diagnostic_info *));
90
91 static void error_recursion PARAMS ((diagnostic_context *)) ATTRIBUTE_NORETURN;
92
93 extern int rtl_dump_and_exit;
94 extern int warnings_are_errors;
95
96 /* A diagnostic_context surrogate for stderr.  */
97 static diagnostic_context global_diagnostic_context;
98 diagnostic_context *global_dc = &global_diagnostic_context;
99
100 \f
101 /* Subroutine of output_set_maximum_length.  Set up BUFFER's
102    internal maximum characters per line.  */
103 static void
104 set_real_maximum_length (buffer)
105      output_buffer *buffer;
106 {
107   /* If we're told not to wrap lines then do the obvious thing.  In case
108    we'll emit prefix only once per diagnostic message, it is appropriate
109   not to increase unnecessarily the line-length cut-off.  */
110   if (!output_is_line_wrapping (buffer)
111       || output_prefixing_rule (buffer) == DIAGNOSTICS_SHOW_PREFIX_ONCE
112       || output_prefixing_rule (buffer) == DIAGNOSTICS_SHOW_PREFIX_NEVER)
113     line_wrap_cutoff (buffer) = output_line_cutoff (buffer);
114   else
115     {
116       int prefix_length = buffer->state.prefix ?
117         strlen (buffer->state.prefix) : 0;
118       /* If the prefix is ridiculously too long, output at least
119          32 characters.  */
120       if (output_line_cutoff (buffer) - prefix_length < 32)
121         line_wrap_cutoff (buffer) = output_line_cutoff (buffer) + 32;
122       else
123         line_wrap_cutoff (buffer) = output_line_cutoff (buffer);
124     }
125 }
126
127 /* Sets the number of maximum characters per line BUFFER can output
128    in line-wrapping mode.  A LENGTH value 0 suppresses line-wrapping.  */
129 void
130 output_set_maximum_length (buffer, length)
131      output_buffer *buffer;
132      int length;
133 {
134   output_line_cutoff (buffer) = length;
135   set_real_maximum_length (buffer);
136 }
137
138 /* Sets BUFFER's PREFIX.  */
139 void
140 output_set_prefix (buffer, prefix)
141      output_buffer *buffer;
142      const char *prefix;
143 {
144   buffer->state.prefix = prefix;
145   set_real_maximum_length (buffer);
146   prefix_was_emitted_for (buffer) = false;
147   output_indentation (buffer) = 0;
148 }
149
150 /*  Return a pointer to the last character emitted in the output
151     BUFFER area.  A NULL pointer means no character available.  */
152 const char *
153 output_last_position (buffer)
154      const output_buffer *buffer;
155 {
156   const char *p = NULL;
157
158   if (obstack_base (&buffer->obstack) != obstack_next_free (&buffer->obstack))
159     p = ((const char *) obstack_next_free (&buffer->obstack)) - 1;
160   return p;
161 }
162
163 /* Free BUFFER's prefix, a previously malloc'd string.  */
164 void
165 output_destroy_prefix (buffer)
166      output_buffer *buffer;
167 {
168   if (buffer->state.prefix != NULL)
169     {
170       free ((char *) buffer->state.prefix);
171       buffer->state.prefix = NULL;
172     }
173 }
174
175 /* Zero out any text output so far in BUFFER.  */
176 void
177 output_clear_message_text (buffer)
178      output_buffer *buffer;
179 {
180   obstack_free (&buffer->obstack, obstack_base (&buffer->obstack));
181   output_text_length (buffer) = 0;
182 }
183
184 /* Zero out any formatting data used so far by BUFFER.  */
185 static void
186 output_clear_data (buffer)
187      output_buffer *buffer;
188 {
189   prefix_was_emitted_for (buffer) = false;
190   output_indentation (buffer) = 0;
191 }
192
193 /* Construct an output BUFFER with PREFIX and of MAXIMUM_LENGTH
194    characters per line.  */
195 void
196 init_output_buffer (buffer, prefix, maximum_length)
197      output_buffer *buffer;
198      const char *prefix;
199      int maximum_length;
200 {
201   memset (buffer, 0, sizeof (output_buffer));
202   obstack_init (&buffer->obstack);
203   output_buffer_attached_stream (buffer) = stderr;
204   output_line_cutoff (buffer) = maximum_length;
205   output_prefixing_rule (buffer) = diagnostic_prefixing_rule (global_dc);
206   output_set_prefix (buffer, prefix);
207   output_text_length (buffer) = 0;
208   output_clear_data (buffer);
209 }
210
211 /* Reinitialize BUFFER.  */
212 void
213 output_clear (buffer)
214      output_buffer *buffer;
215 {
216   output_clear_message_text (buffer);
217   output_clear_data (buffer);
218 }
219
220 /* Finishes constructing a NULL-terminated character string representing
221    the BUFFERed message.  */
222 const char *
223 output_finalize_message (buffer)
224      output_buffer *buffer;
225 {
226   obstack_1grow (&buffer->obstack, '\0');
227   return output_message_text (buffer);
228 }
229
230 /* Return the amount of characters BUFFER can accept to
231    make a full line.  */
232 int
233 output_space_left (buffer)
234      const output_buffer *buffer;
235 {
236   return line_wrap_cutoff (buffer) - output_text_length (buffer);
237 }
238
239 /* Write out BUFFER's prefix.  */
240 void
241 output_emit_prefix (buffer)
242      output_buffer *buffer;
243 {
244   if (buffer->state.prefix != NULL)
245     {
246       switch (output_prefixing_rule (buffer))
247         {
248         default:
249         case DIAGNOSTICS_SHOW_PREFIX_NEVER:
250           break;
251
252         case DIAGNOSTICS_SHOW_PREFIX_ONCE:
253           if (prefix_was_emitted_for (buffer))
254             {
255               output_indent (buffer);
256               break;
257             }
258           output_indentation (buffer) += 3;
259           /* Fall through.  */
260
261         case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
262           {
263             int prefix_length = strlen (buffer->state.prefix);
264             output_append_r (buffer, buffer->state.prefix, prefix_length);
265             prefix_was_emitted_for (buffer) = true;
266           }
267           break;
268         }
269     }
270 }
271
272 /* Have BUFFER start a new line.  */
273 void
274 output_add_newline (buffer)
275      output_buffer *buffer;
276 {
277   obstack_1grow (&buffer->obstack, '\n');
278   output_text_length (buffer) = 0;
279 }
280
281 /* Appends a character to BUFFER.  */
282 void
283 output_add_character (buffer, c)
284      output_buffer *buffer;
285      int c;
286 {
287   if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
288     output_add_newline (buffer);
289   obstack_1grow (&buffer->obstack, c);
290   ++output_text_length (buffer);
291 }
292
293 /* Adds a space to BUFFER.  */
294 void
295 output_add_space (buffer)
296      output_buffer *buffer;
297 {
298   if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
299     {
300       output_add_newline (buffer);
301       return;
302     }
303   obstack_1grow (&buffer->obstack, ' ');
304   ++output_text_length (buffer);
305 }
306
307 /* These functions format an INTEGER into BUFFER as suggested by their
308    names.  */
309 void
310 output_decimal (buffer, i)
311      output_buffer *buffer;
312      int i;
313 {
314   output_formatted_integer (buffer, "%d", i);
315 }
316
317 static void
318 output_long_decimal (buffer, i)
319      output_buffer *buffer;
320      long int i;
321 {
322   output_formatted_integer (buffer, "%ld", i);
323 }
324
325 static void
326 output_unsigned_decimal (buffer, i)
327      output_buffer *buffer;
328      unsigned int i;
329 {
330   output_formatted_integer (buffer, "%u", i);
331 }
332
333 static void
334 output_long_unsigned_decimal (buffer, i)
335      output_buffer *buffer;
336      long unsigned int i;
337 {
338   output_formatted_integer (buffer, "%lu", i);
339 }
340
341 static void
342 output_octal (buffer, i)
343      output_buffer *buffer;
344      unsigned int i;
345 {
346   output_formatted_integer (buffer, "%o", i);
347 }
348
349 static void
350 output_long_octal (buffer, i)
351      output_buffer *buffer;
352      unsigned long int i;
353 {
354   output_formatted_integer (buffer, "%lo", i);
355 }
356
357 static void
358 output_hexadecimal (buffer, i)
359      output_buffer *buffer;
360      unsigned int i;
361 {
362   output_formatted_integer (buffer, "%x", i);
363 }
364
365 static void
366 output_long_hexadecimal (buffer, i)
367      output_buffer *buffer;
368      unsigned long int i;
369 {
370   output_formatted_integer (buffer, "%lx", i);
371 }
372
373 /* Append to BUFFER a string specified by its STARTING character
374    and LENGTH.  */
375 static void
376 output_append_r (buffer, start, length)
377      output_buffer *buffer;
378      const char *start;
379      int length;
380 {
381   obstack_grow (&buffer->obstack, start, length);
382   output_text_length (buffer) += length;
383 }
384
385 /* Append a string deliminated by START and END to BUFFER.  No wrapping is
386    done.  However, if beginning a new line then emit BUFFER->state.prefix
387    and skip any leading whitespace if appropriate.  The caller must ensure
388    that it is safe to do so.  */
389 void
390 output_append (buffer, start, end)
391      output_buffer *buffer;
392      const char *start;
393      const char *end;
394 {
395   /* Emit prefix and skip whitespace if we're starting a new line.  */
396   if (is_starting_newline (buffer))
397     {
398       output_emit_prefix (buffer);
399       if (output_is_line_wrapping (buffer))
400         while (start != end && *start == ' ')
401           ++start;
402     }
403   output_append_r (buffer, start, end - start);
404 }
405
406 static void
407 output_indent (buffer)
408      output_buffer *buffer;
409 {
410   int n = output_indentation (buffer);
411   int i;
412
413   for (i = 0; i < n; ++i)
414     output_add_character (buffer, ' ');
415 }
416
417 /* Wrap a text delimited by START and END into BUFFER.  */
418 static void
419 wrap_text (buffer, start, end)
420      output_buffer *buffer;
421      const char *start;
422      const char *end;
423 {
424   bool is_wrapping = output_is_line_wrapping (buffer);
425
426   while (start != end)
427     {
428       /* Dump anything bordered by whitespaces.  */
429       {
430         const char *p = start;
431         while (p != end && *p != ' ' && *p != '\n')
432           ++p;
433         if (is_wrapping && p - start >= output_space_left (buffer))
434           output_add_newline (buffer);
435         output_append (buffer, start, p);
436         start = p;
437       }
438
439       if (start != end && *start == ' ')
440         {
441           output_add_space (buffer);
442           ++start;
443         }
444       if (start != end && *start == '\n')
445         {
446           output_add_newline (buffer);
447           ++start;
448         }
449     }
450 }
451
452 /* Same as wrap_text but wrap text only when in line-wrapping mode.  */
453 static void
454 maybe_wrap_text (buffer, start, end)
455      output_buffer *buffer;
456      const char *start;
457      const char *end;
458 {
459   if (output_is_line_wrapping (buffer))
460     wrap_text (buffer, start, end);
461   else
462     output_append (buffer, start, end);
463 }
464
465
466 /* Append a STRING to BUFFER; the STRING might be line-wrapped if in
467    appropriate mode.  */
468 void
469 output_add_string (buffer, str)
470      output_buffer *buffer;
471      const char *str;
472 {
473   maybe_wrap_text (buffer, str, str + (str ? strlen (str) : 0));
474 }
475
476 /* Flush the content of BUFFER onto the attached stream,
477    and reinitialize.  */
478
479 static void
480 output_buffer_to_stream (buffer)
481      output_buffer *buffer;
482 {
483   const char *text = output_finalize_message (buffer);
484   fputs (text, output_buffer_attached_stream (buffer));
485   output_clear_message_text (buffer);
486 }
487
488 /* Format a message pointed to by TEXT.  The following format specifiers are
489    recognized as being language independent:
490    %d, %i: (signed) integer in base ten.
491    %u: unsigned integer in base ten.
492    %o: unsigned integer in base eight.
493    %x: unsigned integer in base sixteen.
494    %ld, %li, %lo, %lu, %lx: long versions of the above.
495    %c: character.
496    %s: string.
497    %%: `%'.
498    %*.s: a substring the length of which is specified by an integer.  */
499 static void
500 output_format (buffer, text)
501      output_buffer *buffer;
502      text_info *text;
503 {
504   for (; *text->format_spec; ++text->format_spec)
505     {
506       bool long_integer = 0;
507
508       /* Ignore text.  */
509       {
510         const char *p = text->format_spec;
511         while (*p && *p != '%')
512           ++p;
513         wrap_text (buffer, text->format_spec, p);
514         text->format_spec = p;
515       }
516
517       if (*text->format_spec == '\0')
518         break;
519
520       /* We got a '%'.  Let's see what happens. Record whether we're
521          parsing a long integer format specifier.  */
522       if (*++text->format_spec == 'l')
523         {
524           long_integer = true;
525           ++text->format_spec;
526         }
527
528       /* Handle %c, %d, %i, %ld, %li, %lo, %lu, %lx, %o, %s, %u,
529          %x, %.*s; %%.  And nothing else.  Front-ends should install
530          printers to grok language specific format specifiers.  */
531       switch (*text->format_spec)
532         {
533         case 'c':
534           output_add_character (buffer, va_arg (*text->args_ptr, int));
535           break;
536
537         case 'd':
538         case 'i':
539           if (long_integer)
540             output_long_decimal (buffer, va_arg (*text->args_ptr, long int));
541           else
542             output_decimal (buffer, va_arg (*text->args_ptr, int));
543           break;
544
545         case 'o':
546           if (long_integer)
547             output_long_octal (buffer,
548                                va_arg (*text->args_ptr, unsigned long int));
549           else
550             output_octal (buffer, va_arg (*text->args_ptr, unsigned int));
551           break;
552
553         case 's':
554           output_add_string (buffer, va_arg (*text->args_ptr, const char *));
555           break;
556
557         case 'u':
558           if (long_integer)
559             output_long_unsigned_decimal
560               (buffer, va_arg (*text->args_ptr, long unsigned int));
561           else
562             output_unsigned_decimal
563               (buffer, va_arg (*text->args_ptr, unsigned int));
564           break;
565
566         case 'x':
567           if (long_integer)
568             output_long_hexadecimal
569               (buffer, va_arg (*text->args_ptr, unsigned long int));
570           else
571             output_hexadecimal
572               (buffer, va_arg (*text->args_ptr, unsigned int));
573           break;
574
575         case '%':
576           output_add_character (buffer, '%');
577           break;
578
579         case '.':
580           {
581             int n;
582             const char *s;
583             /* We handle no precision specifier but `%.*s'.  */
584             if (*++text->format_spec != '*')
585               abort ();
586             else if (*++text->format_spec != 's')
587               abort ();
588             n = va_arg (*text->args_ptr, int);
589             s = va_arg (*text->args_ptr, const char *);
590             output_append (buffer, s, s + n);
591           }
592           break;
593
594         default:
595           if (!buffer->format_decoder
596               || !(*buffer->format_decoder) (buffer, text))
597             {
598               /* Hmmm.  The front-end failed to install a format translator
599                  but called us with an unrecognized format.  Sorry.  */
600               abort ();
601             }
602         }
603     }
604 }
605
606 static char *
607 vbuild_message_string (msg, ap)
608      const char *msg;
609      va_list ap;
610 {
611   char *str;
612
613   vasprintf (&str, msg, ap);
614   return str;
615 }
616
617 /*  Return a malloc'd string containing MSG formatted a la
618     printf.  The caller is responsible for freeing the memory.  */
619 static char *
620 build_message_string VPARAMS ((const char *msg, ...))
621 {
622   char *str;
623
624   VA_OPEN (ap, msg);
625   VA_FIXEDARG (ap, const char *, msg);
626
627   str = vbuild_message_string (msg, ap);
628
629   VA_CLOSE (ap);
630
631   return str;
632 }
633
634 /* Same as diagnsotic_build_prefix, but only the source FILE is given.  */
635 char *
636 file_name_as_prefix (f)
637      const char *f;
638 {
639   return build_message_string ("%s: ", f);
640 }
641
642 /* Format a message into BUFFER a la printf.  */
643 void
644 output_printf VPARAMS ((struct output_buffer *buffer, const char *msgid, ...))
645 {
646   text_info text;
647   VA_OPEN (ap, msgid);
648   VA_FIXEDARG (ap, output_buffer *, buffer);
649   VA_FIXEDARG (ap, const char *, msgid);
650
651   text.args_ptr = &ap;
652   text.format_spec = _(msgid);
653   output_format (buffer, &text);
654   VA_CLOSE (ap);
655 }
656
657 /* Print a message relevant to the given DECL.  */
658 static void
659 format_with_decl (buffer, text, decl)
660      output_buffer *buffer;
661      text_info *text;
662      tree decl;
663 {
664   const char *p;
665
666   /* Do magic to get around lack of varargs support for insertion
667      of arguments into existing list.  We know that the decl is first;
668      we ass_u_me that it will be printed with "%s".  */
669   for (p = text->format_spec; *p; ++p)
670     {
671       if (*p == '%')
672         {
673           if (*(p + 1) == '%')
674             ++p;
675           else if (*(p + 1) != 's')
676             abort ();
677           else
678             break;
679         }
680     }
681
682   /* Print the left-hand substring.  */
683   maybe_wrap_text (buffer, text->format_spec, p);
684
685   if (*p == '%')                /* Print the name.  */
686     {
687       const char *const n = (DECL_NAME (decl)
688                              ? (*lang_hooks.decl_printable_name) (decl, 2)
689                              : _("((anonymous))"));
690       output_add_string (buffer, n);
691       while (*p)
692         {
693           ++p;
694           if (ISALPHA (*(p - 1) & 0xFF))
695             break;
696         }
697     }
698
699   if (*p)                       /* Print the rest of the message.  */
700     {
701       text->format_spec = p;
702       output_format (buffer, text);
703     }
704 }
705
706 /* Flush the content of BUFFER onto the attached stream.  */
707 static void
708 output_flush (buffer)
709      output_buffer *buffer;
710 {
711   output_buffer_to_stream (buffer);
712   output_clear_data (buffer);
713   fputc ('\n', output_buffer_attached_stream (buffer));
714   fflush (output_buffer_attached_stream (buffer));
715 }
716
717 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
718    settings needed by BUFFER for a verbatim formatting.  */
719 static void
720 output_do_verbatim (buffer, text)
721      output_buffer *buffer;
722      text_info *text;
723 {
724   diagnostic_prefixing_rule_t rule = output_prefixing_rule (buffer);
725   int line_cutoff = output_line_cutoff (buffer);
726
727   /* Set verbatim mode.  */
728   output_prefixing_rule (buffer) = DIAGNOSTICS_SHOW_PREFIX_NEVER;
729   output_line_cutoff (buffer) = 0;
730   /* Do the actual formatting.  */
731   output_format (buffer, text);
732   /* Restore previous settings.  */
733   output_prefixing_rule (buffer) = rule;
734   output_line_cutoff (buffer) = line_cutoff;
735 }
736
737 /* Output MESSAGE verbatim into BUFFER.  */
738 void
739 output_verbatim VPARAMS ((output_buffer *buffer, const char *msgid, ...))
740 {
741   text_info text;
742   VA_OPEN (ap, msgid);
743   VA_FIXEDARG (ap, output_buffer *, buffer);
744   VA_FIXEDARG (ap, const char *, msgid);
745
746   text.format_spec = msgid;
747   text.args_ptr = &ap;
748   output_do_verbatim (buffer, &text);
749   VA_CLOSE (ap);
750 }
751
752 \f
753 /* Initialize the diagnostic message outputting machinery.  */
754 void
755 diagnostic_initialize (context)
756      diagnostic_context *context;
757 {
758   memset (context, 0, sizeof *context);
759   obstack_init (&context->buffer.obstack);
760
761   /* By default, diagnostics are sent to stderr.  */
762   output_buffer_attached_stream (&context->buffer) = stderr;
763
764   /* By default, we emit prefixes once per message.  */
765   diagnostic_prefixing_rule (context) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
766
767   diagnostic_starter (context) = default_diagnostic_starter;
768   diagnostic_finalizer (context) = default_diagnostic_finalizer;
769   context->warnings_are_errors_message = warnings_are_errors;
770 }
771
772 void
773 diagnostic_set_info (diagnostic, msgid, args, file, line, kind)
774      diagnostic_info *diagnostic;
775      const char *msgid;
776      va_list *args;
777      const char *file;
778      int line;
779      diagnostic_t kind;
780 {
781   diagnostic->message.format_spec = msgid;
782   diagnostic->message.args_ptr = args;
783   diagnostic->location.file = file;
784   diagnostic->location.line = line;
785   diagnostic->kind = kind;
786 }
787
788 /* Return a malloc'd string describing a location.  The caller is
789    responsible for freeing the memory.  */
790 char *
791 diagnostic_build_prefix (diagnostic)
792      diagnostic_info *diagnostic;
793 {
794   static const char *diagnostic_kind_text[] = {
795 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
796 #include "diagnostic.def"
797 #undef DEFINE_DIAGNOSTIC_KIND
798     "must-not-happen"
799   };
800    if (diagnostic->kind >= DK_LAST_DIAGNOSTIC_KIND)
801      abort();
802
803   return diagnostic->location.file
804     ? build_message_string ("%s:%d: %s",
805                             diagnostic->location.file,
806                             diagnostic->location.line,
807                             _(diagnostic_kind_text[diagnostic->kind]))
808     : build_message_string ("%s: %s", progname,
809                             _(diagnostic_kind_text[diagnostic->kind]));
810 }
811
812 /* Report a diagnostic MESSAGE at the declaration DECL.
813    MSG is a format string which uses %s to substitute the declaration
814    name; subsequent substitutions are a la output_format.  */
815 static void
816 diagnostic_for_decl (diagnostic, decl)
817      diagnostic_info *diagnostic;
818      tree decl;
819 {
820   if (global_dc->lock++)
821     error_recursion (global_dc);
822
823   if (diagnostic_count_diagnostic (global_dc, diagnostic->kind))
824     {
825       diagnostic_report_current_function (global_dc);
826       output_set_prefix
827         (&global_dc->buffer, diagnostic_build_prefix (diagnostic));
828       format_with_decl (&global_dc->buffer, &diagnostic->message, decl);
829       output_flush (&global_dc->buffer);
830       output_destroy_prefix (&global_dc->buffer);
831     }
832   global_dc->lock--;
833 }
834
835 void
836 diagnostic_flush_buffer (context)
837      diagnostic_context *context;
838 {
839   output_buffer_to_stream (&context->buffer);
840   fflush (output_buffer_attached_stream (&context->buffer));
841 }
842
843 /* Count a diagnostic.  Return true if the message should be printed.  */
844 bool
845 diagnostic_count_diagnostic (context, kind)
846     diagnostic_context *context;
847     diagnostic_t kind;
848 {
849   switch (kind)
850     {
851     default:
852       abort();
853       break;
854       
855     case DK_FATAL: case DK_ICE: case DK_SORRY:
856     case DK_ANACHRONISM: case DK_NOTE:
857       ++diagnostic_kind_count (context, kind);
858       break;
859
860     case DK_WARNING:
861       if (!diagnostic_report_warnings_p ())
862         return false;
863       else if (!warnings_are_errors)
864         {
865           ++diagnostic_kind_count (context, DK_WARNING);
866           break;
867         }
868       /* else fall through.  */
869
870     case DK_ERROR:
871       if (kind == DK_WARNING && context->warnings_are_errors_message)
872         {
873           output_verbatim (&context->buffer,
874                            "%s: warnings being treated as errors\n", progname);
875           context->warnings_are_errors_message = false;
876         }
877       ++diagnostic_kind_count (context, DK_ERROR);
878       break;
879     }
880
881   return true;
882 }
883
884 /* Print a diagnostic MSGID on FILE.  This is just fprintf, except it
885    runs its second argument through gettext.  */
886 void
887 fnotice VPARAMS ((FILE *file, const char *msgid, ...))
888 {
889   VA_OPEN (ap, msgid);
890   VA_FIXEDARG (ap, FILE *, file);
891   VA_FIXEDARG (ap, const char *, msgid);
892
893   vfprintf (file, _(msgid), ap);
894   VA_CLOSE (ap);
895 }
896
897
898 /* Print a fatal I/O error message.  Argument are like printf.
899    Also include a system error message based on `errno'.  */
900 void
901 fatal_io_error VPARAMS ((const char *msgid, ...))
902 {
903   text_info text;
904   VA_OPEN (ap, msgid);
905   VA_FIXEDARG (ap, const char *, msgid);
906
907   text.format_spec = _(msgid);
908   text.args_ptr = &ap;
909   output_printf (&global_dc->buffer, "%s: %s: ", progname, xstrerror (errno));
910   output_format (&global_dc->buffer, &text);
911   output_flush (&global_dc->buffer);
912   VA_CLOSE (ap);
913   exit (FATAL_EXIT_CODE);
914 }
915
916 /* Issue a pedantic warning MSGID.  */
917 void
918 pedwarn VPARAMS ((const char *msgid, ...))
919 {
920   diagnostic_info diagnostic;
921   VA_OPEN (ap, msgid);
922   VA_FIXEDARG (ap, const char *, msgid);
923
924   diagnostic_set_info (&diagnostic, _(msgid), &ap, input_filename, lineno,
925                        pedantic_error_kind ());
926   report_diagnostic (&diagnostic);
927   VA_CLOSE (ap);
928 }
929
930 /* Issue a pedantic warning about DECL.  */
931 void
932 pedwarn_with_decl VPARAMS ((tree decl, const char *msgid, ...))
933 {
934   diagnostic_info diagnostic;
935   VA_OPEN (ap, msgid);
936   VA_FIXEDARG (ap, tree, decl);
937   VA_FIXEDARG (ap, const char *, msgid);
938
939   diagnostic_set_info (&diagnostic, _(msgid), &ap,
940                        DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
941                        pedantic_error_kind ());
942
943   /* We don't want -pedantic-errors to cause the compilation to fail from
944      "errors" in system header files.  Sometimes fixincludes can't fix what's
945      broken (eg: unsigned char bitfields - fixing it may change the alignment
946      which will cause programs to mysteriously fail because the C library
947      or kernel uses the original layout).  There's no point in issuing a
948      warning either, it's just unnecessary noise.  */
949   if (!DECL_IN_SYSTEM_HEADER (decl))
950     diagnostic_for_decl (&diagnostic, decl);
951   VA_CLOSE (ap);
952 }
953
954 /* Same as above but within the context FILE and LINE.  */
955 void
956 pedwarn_with_file_and_line VPARAMS ((const char *file, int line,
957                                      const char *msgid, ...))
958 {
959   diagnostic_info diagnostic;
960   VA_OPEN (ap, msgid);
961   VA_FIXEDARG (ap, const char *, file);
962   VA_FIXEDARG (ap, int, line);
963   VA_FIXEDARG (ap, const char *, msgid);
964
965   diagnostic_set_info (&diagnostic, _(msgid), &ap, file, line,
966                        pedantic_error_kind ());
967   report_diagnostic (&diagnostic);
968   VA_CLOSE (ap);
969 }
970
971 /* Just apologize with MSGID.  */
972 void
973 sorry VPARAMS ((const char *msgid, ...))
974 {
975   diagnostic_info diagnostic;
976
977   VA_OPEN (ap, msgid);
978   VA_FIXEDARG (ap, const char *, msgid);
979
980   ++sorrycount;
981   diagnostic_set_info (&diagnostic, _(msgid), &ap,
982                        input_filename, lineno, DK_SORRY);
983
984   output_set_prefix
985     (&global_dc->buffer, diagnostic_build_prefix (&diagnostic));
986   output_printf (&global_dc->buffer, "sorry, not implemented: ");
987   output_format (&global_dc->buffer, &diagnostic.message);
988   output_flush (&global_dc->buffer);
989   VA_CLOSE (ap);
990 }
991
992 /* Called when the start of a function definition is parsed,
993    this function prints on stderr the name of the function.  */
994 void
995 announce_function (decl)
996      tree decl;
997 {
998   if (!quiet_flag)
999     {
1000       if (rtl_dump_and_exit)
1001         verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl)));
1002       else
1003         verbatim (" %s", (*lang_hooks.decl_printable_name) (decl, 2));
1004       fflush (stderr);
1005       output_needs_newline (&global_dc->buffer) = true;
1006       diagnostic_set_last_function (global_dc);
1007     }
1008 }
1009
1010 /* The default function to print out name of current function that caused
1011    an error.  */
1012 void
1013 lhd_print_error_function (context, file)
1014      diagnostic_context *context;
1015      const char *file;
1016 {
1017   if (diagnostic_last_function_changed (context))
1018     {
1019       const char *old_prefix = output_prefix (&context->buffer);
1020       char *new_prefix = file ? build_message_string ("%s: ", file) : NULL;
1021
1022       output_set_prefix (&context->buffer, new_prefix);
1023
1024       if (current_function_decl == NULL)
1025         output_add_string (&context->buffer, _("At top level:"));
1026       else
1027         {
1028           if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
1029             output_printf
1030               (&context->buffer, "In member function `%s':",
1031                (*lang_hooks.decl_printable_name) (current_function_decl, 2));
1032           else
1033             output_printf
1034               (&context->buffer, "In function `%s':",
1035                (*lang_hooks.decl_printable_name) (current_function_decl, 2));
1036         }
1037       output_add_newline (&context->buffer);
1038
1039       diagnostic_set_last_function (context);
1040       output_buffer_to_stream (&context->buffer);
1041       context->buffer.state.prefix = old_prefix;
1042       free ((char*) new_prefix);
1043     }
1044 }
1045
1046 /* Prints out, if necessary, the name of the current function
1047   that caused an error.  Called from all error and warning functions.
1048   We ignore the FILE parameter, as it cannot be relied upon.  */
1049
1050 void
1051 diagnostic_report_current_function (context)
1052      diagnostic_context *context;
1053 {
1054   diagnostic_report_current_module (context);
1055   (*lang_hooks.print_error_function) (context, input_filename);
1056 }
1057
1058 void
1059 error_with_file_and_line VPARAMS ((const char *file, int line,
1060                                    const char *msgid, ...))
1061 {
1062   diagnostic_info diagnostic;
1063
1064   VA_OPEN (ap, msgid);
1065   VA_FIXEDARG (ap, const char *, file);
1066   VA_FIXEDARG (ap, int, line);
1067   VA_FIXEDARG (ap, const char *, msgid);
1068
1069   diagnostic_set_info (&diagnostic, msgid, &ap, file, line, DK_ERROR);
1070   report_diagnostic (&diagnostic);
1071   VA_CLOSE (ap);
1072 }
1073
1074 void
1075 error_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1076 {
1077   diagnostic_info diagnostic;
1078   VA_OPEN (ap, msgid);
1079   VA_FIXEDARG (ap, tree, decl);
1080   VA_FIXEDARG (ap, const char *, msgid);
1081
1082   diagnostic_set_info (&diagnostic, msgid, &ap,
1083                        DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1084                        DK_ERROR);
1085   diagnostic_for_decl (&diagnostic, decl);
1086   VA_CLOSE (ap);
1087 }
1088
1089
1090 /* Report an error message.  The arguments are like that of printf.  */
1091
1092 void
1093 error VPARAMS ((const char *msgid, ...))
1094 {
1095   diagnostic_info diagnostic;
1096
1097   VA_OPEN (ap, msgid);
1098   VA_FIXEDARG (ap, const char *, msgid);
1099
1100   diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1101                        DK_ERROR);
1102   report_diagnostic (&diagnostic);
1103   VA_CLOSE (ap);
1104 }
1105
1106 /* Likewise, except that the compilation is terminated after printing the
1107    error message.  */
1108
1109 void
1110 fatal_error VPARAMS ((const char *msgid, ...))
1111 {
1112   diagnostic_info diagnostic;
1113
1114   VA_OPEN (ap, msgid);
1115   VA_FIXEDARG (ap, const char *, msgid);
1116
1117   diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1118                        DK_FATAL);
1119   report_diagnostic (&diagnostic);
1120   VA_CLOSE (ap);
1121
1122   fnotice (stderr, "compilation terminated.\n");
1123   exit (FATAL_EXIT_CODE);
1124 }
1125
1126 void
1127 internal_error VPARAMS ((const char *msgid, ...))
1128 {
1129   diagnostic_info diagnostic;
1130
1131   VA_OPEN (ap, msgid);
1132   VA_FIXEDARG (ap, const char *, msgid);
1133
1134   if (global_dc->lock)
1135     error_recursion (global_dc);
1136
1137 #ifndef ENABLE_CHECKING
1138   if (errorcount > 0 || sorrycount > 0)
1139     {
1140       fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
1141                input_filename, lineno);
1142       exit (FATAL_EXIT_CODE);
1143     }
1144 #endif
1145
1146   if (global_dc->internal_error != 0)
1147     (*global_dc->internal_error) (_(msgid), &ap);
1148
1149   diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1150                        DK_ICE);
1151   report_diagnostic (&diagnostic);
1152   VA_CLOSE (ap);
1153
1154   fnotice (stderr,
1155 "Please submit a full bug report,\n\
1156 with preprocessed source if appropriate.\n\
1157 See %s for instructions.\n", GCCBUGURL);
1158   exit (FATAL_EXIT_CODE);
1159 }
1160
1161 void
1162 warning_with_file_and_line VPARAMS ((const char *file, int line,
1163                                      const char *msgid, ...))
1164 {
1165   diagnostic_info diagnostic;
1166
1167   VA_OPEN (ap, msgid);
1168   VA_FIXEDARG (ap, const char *, file);
1169   VA_FIXEDARG (ap, int, line);
1170   VA_FIXEDARG (ap, const char *, msgid);
1171
1172   diagnostic_set_info (&diagnostic, msgid, &ap, file, line, DK_WARNING);
1173   report_diagnostic (&diagnostic);
1174   VA_CLOSE (ap);
1175 }
1176
1177 void
1178 warning_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1179 {
1180   diagnostic_info diagnostic;
1181   VA_OPEN (ap, msgid);
1182   VA_FIXEDARG (ap, tree, decl);
1183   VA_FIXEDARG (ap, const char *, msgid);
1184
1185   diagnostic_set_info (&diagnostic, msgid, &ap,
1186                        DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1187                        DK_WARNING);
1188   diagnostic_for_decl (&diagnostic, decl);
1189   VA_CLOSE (ap);
1190 }
1191
1192 void
1193 warning VPARAMS ((const char *msgid, ...))
1194 {
1195   diagnostic_info diagnostic;
1196
1197   VA_OPEN (ap, msgid);
1198   VA_FIXEDARG (ap, const char *, msgid);
1199
1200   diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1201                        DK_WARNING);
1202   report_diagnostic (&diagnostic);
1203   VA_CLOSE (ap);
1204 }
1205
1206
1207 /* Same as above but use diagnostic_buffer.  */
1208
1209 void
1210 verbatim VPARAMS ((const char *msgid, ...))
1211 {
1212   text_info text;
1213   VA_OPEN (ap, msgid);
1214   VA_FIXEDARG (ap, const char *, msgid);
1215
1216   text.format_spec = _(msgid);
1217   text.args_ptr = &ap;
1218   output_do_verbatim (&global_dc->buffer, &text);
1219   output_buffer_to_stream (&global_dc->buffer);
1220   VA_CLOSE (ap);
1221 }
1222
1223 /* Report a diagnostic message (an error or a warning) as specified by
1224    DC.  This function is *the* subroutine in terms of which front-ends
1225    should implement their specific diagnostic handling modules.  The
1226    front-end independent format specifiers are exactly those described
1227    in the documentation of output_format.  */
1228
1229 void
1230 diagnostic_report_diagnostic (context, diagnostic)
1231      diagnostic_context *context;
1232      diagnostic_info *diagnostic;
1233 {
1234   if (context->lock++)
1235     error_recursion (context);
1236
1237   if (diagnostic_count_diagnostic (context, diagnostic->kind))
1238     {
1239       (*diagnostic_starter (context)) (context, diagnostic);
1240       output_format (&context->buffer, &diagnostic->message);
1241       (*diagnostic_finalizer (context)) (context, diagnostic);
1242       output_flush (&context->buffer);
1243     }
1244
1245   --context->lock;
1246 }
1247
1248 /* Inform the user that an error occurred while trying to report some
1249    other error.  This indicates catastrophic internal inconsistencies,
1250    so give up now.  But do try to flush out the previous error.
1251    This mustn't use internal_error, that will cause infinite recursion.  */
1252
1253 static void
1254 error_recursion (context)
1255      diagnostic_context *context;
1256 {
1257   if (context->lock < 3)
1258     output_flush (&context->buffer);
1259
1260   fnotice (stderr,
1261            "Internal compiler error: Error reporting routines re-entered.\n");
1262   fnotice (stderr,
1263 "Please submit a full bug report,\n\
1264 with preprocessed source if appropriate.\n\
1265 See %s for instructions.\n", GCCBUGURL);
1266   exit (FATAL_EXIT_CODE);
1267 }
1268
1269 /* Given a partial pathname as input, return another pathname that
1270    shares no directory elements with the pathname of __FILE__.  This
1271    is used by fancy_abort() to print `Internal compiler error in expr.c'
1272    instead of `Internal compiler error in ../../GCC/gcc/expr.c'.  */
1273
1274 const char *
1275 trim_filename (name)
1276      const char *name;
1277 {
1278   static const char this_file[] = __FILE__;
1279   const char *p = name, *q = this_file;
1280
1281   /* First skip any "../" in each filename.  This allows us to give a proper
1282      reference to a file in a subdirectory.  */
1283   while (p[0] == '.' && p[1] == '.'
1284          && (p[2] == DIR_SEPARATOR
1285 #ifdef DIR_SEPARATOR_2
1286              || p[2] == DIR_SEPARATOR_2
1287 #endif
1288              ))
1289     p += 3;
1290
1291   while (q[0] == '.' && q[1] == '.'
1292          && (q[2] == DIR_SEPARATOR
1293 #ifdef DIR_SEPARATOR_2
1294              || p[2] == DIR_SEPARATOR_2
1295 #endif
1296              ))
1297     q += 3;
1298
1299   /* Now skip any parts the two filenames have in common.  */
1300   while (*p == *q && *p != 0 && *q != 0)
1301     p++, q++;
1302
1303   /* Now go backwards until the previous directory separator.  */
1304   while (p > name && p[-1] != DIR_SEPARATOR
1305 #ifdef DIR_SEPARATOR_2
1306          && p[-1] != DIR_SEPARATOR_2
1307 #endif
1308          )
1309     p--;
1310
1311   return p;
1312 }
1313
1314 /* Report an internal compiler error in a friendly manner and without
1315    dumping core.  */
1316
1317 void
1318 fancy_abort (file, line, function)
1319      const char *file;
1320      int line;
1321      const char *function;
1322 {
1323   internal_error ("Internal compiler error in %s, at %s:%d",
1324                   function, trim_filename (file), line);
1325 }
1326
1327 void
1328 diagnostic_report_current_module (context)
1329      diagnostic_context *context;
1330 {
1331   struct file_stack *p;
1332
1333   if (output_needs_newline (&context->buffer))
1334     {
1335       output_add_newline (&context->buffer);
1336       output_needs_newline (&context->buffer) = false;
1337     }
1338
1339   if (input_file_stack && input_file_stack->next != 0
1340       && diagnostic_last_module_changed (context))
1341     {
1342       for (p = input_file_stack->next; p; p = p->next)
1343         if (p == input_file_stack->next)
1344           output_verbatim (&context->buffer,
1345                            "In file included from %s:%d", p->name, p->line);
1346         else
1347           output_verbatim (&context->buffer,
1348                            ",\n                 from %s:%d", p->name, p->line);
1349       output_verbatim (&context->buffer, ":\n");
1350       diagnostic_set_last_module (context);
1351     }
1352 }
1353
1354 static void
1355 default_diagnostic_starter (context, diagnostic)
1356      diagnostic_context *context;
1357      diagnostic_info *diagnostic;
1358 {
1359   diagnostic_report_current_function (context);
1360   output_set_prefix (&context->buffer, diagnostic_build_prefix (diagnostic));
1361 }
1362
1363 static void
1364 default_diagnostic_finalizer (context, diagnostic)
1365      diagnostic_context *context;
1366      diagnostic_info *diagnostic __attribute__((unused));
1367 {
1368   output_destroy_prefix (&context->buffer);
1369 }
1370
1371 void
1372 warn_deprecated_use (node)
1373      tree node;
1374 {
1375   if (node == 0 || !warn_deprecated_decl)
1376     return;
1377
1378   if (DECL_P (node))
1379     warning ("`%s' is deprecated (declared at %s:%d)",
1380              IDENTIFIER_POINTER (DECL_NAME (node)),
1381              DECL_SOURCE_FILE (node), DECL_SOURCE_LINE (node));
1382   else if (TYPE_P (node))
1383     {
1384       const char *what = NULL;
1385       tree decl = TYPE_STUB_DECL (node);
1386
1387       if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
1388         what = IDENTIFIER_POINTER (TYPE_NAME (node));
1389       else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
1390                && DECL_NAME (TYPE_NAME (node)))
1391         what = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node)));
1392
1393       if (what)
1394         {
1395           if (decl)
1396             warning ("`%s' is deprecated (declared at %s:%d)", what,
1397                      DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
1398           else
1399             warning ("`%s' is deprecated", what);
1400         }
1401       else if (decl)
1402         warning ("type is deprecated (declared at %s:%d)",
1403                  DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
1404       else
1405         warning ("type is deprecated");
1406     }
1407 }