OSDN Git Service

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