OSDN Git Service

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