OSDN Git Service

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