OSDN Git Service

2001-05-23 Zack Weinberg <zackw@stanford.edu>
[pf3gnuchains/gcc-fork.git] / gcc / diagnostic.c
1 /* Language-independent diagnostic subroutines for the GNU C compiler
2    Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3    Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 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
31 #include "tree.h"
32 #include "rtl.h"
33 #include "tm_p.h"
34 #include "flags.h"
35 #include "input.h"
36 #include "insn-attr.h"
37 #include "insn-config.h"
38 #include "toplev.h"
39 #include "intl.h"
40 #include "diagnostic.h"
41
42 #define obstack_chunk_alloc xmalloc
43 #define obstack_chunk_free  free
44
45 #define output_formatted_integer(BUFFER, FORMAT, INTEGER) \
46   do {                                                    \
47     sprintf (digit_buffer, FORMAT, INTEGER);              \
48     output_add_string (BUFFER, digit_buffer);             \
49   } while (0)
50
51 #define output_text_length(BUFFER) (BUFFER)->line_length
52 #define is_starting_newline(BUFFER) (output_text_length (BUFFER) == 0)
53 #define output_prefix(BUFFER) (BUFFER)->state.prefix
54 #define line_wrap_cutoff(BUFFER) (BUFFER)->state.maximum_length
55 #define ideal_line_wrap_cutoff(BUFFER) (BUFFER)->state.ideal_maximum_length
56 #define prefix_was_emitted_for(BUFFER) (BUFFER)->state.emitted_prefix_p
57 #define prefixing_policy(BUFFER) (BUFFER)->state.prefixing_rule
58 #define output_buffer_ptr_to_format_args(BUFFER) (BUFFER)->state.format_args
59
60 #define diagnostic_args output_buffer_ptr_to_format_args (diagnostic_buffer)
61 #define diagnostic_msg output_buffer_text_cursor (diagnostic_buffer)
62
63 /* Prototypes. */
64 static void finish_diagnostic PARAMS ((void));
65 static void output_do_verbatim PARAMS ((output_buffer *,
66                                         const char *, va_list *));
67 static void output_buffer_to_stream PARAMS ((output_buffer *));
68 static void output_format PARAMS ((output_buffer *));
69 static void output_indent PARAMS ((output_buffer *));
70
71 static char *vbuild_message_string PARAMS ((const char *, va_list))
72      ATTRIBUTE_PRINTF (1, 0);
73 static char *build_message_string PARAMS ((const char *, ...))
74      ATTRIBUTE_PRINTF_1;
75 static void output_do_printf PARAMS ((output_buffer *, const char *))
76      ATTRIBUTE_PRINTF (2, 0);
77 static void format_with_decl PARAMS ((output_buffer *, tree));
78 static void file_and_line_for_asm PARAMS ((rtx, const char **, int *));
79 static void diagnostic_for_asm PARAMS ((rtx, const char *, va_list *, int));
80 static void diagnostic_for_decl PARAMS ((tree, const char *, va_list *, int));
81 static void set_real_maximum_length PARAMS ((output_buffer *));
82
83 static void output_unsigned_decimal PARAMS ((output_buffer *, unsigned int));
84 static void output_long_decimal PARAMS ((output_buffer *, long int));
85 static void output_long_unsigned_decimal PARAMS ((output_buffer *,
86                                                   long unsigned int));
87 static void output_octal PARAMS ((output_buffer *, unsigned int));
88 static void output_long_octal PARAMS ((output_buffer *, unsigned long int));
89 static void output_hexadecimal PARAMS ((output_buffer *, unsigned int));
90 static void output_long_hexadecimal PARAMS ((output_buffer *,
91                                              unsigned long int));
92 static void output_append_r PARAMS ((output_buffer *, const char *, int));
93 static void wrap_text PARAMS ((output_buffer *, const char *, const char *));
94 static void maybe_wrap_text PARAMS ((output_buffer *, const char *,
95                                      const char *));
96 static void clear_diagnostic_info PARAMS ((output_buffer *));
97
98 static void default_diagnostic_starter PARAMS ((output_buffer *,
99                                                 diagnostic_context *));
100 static void default_diagnostic_finalizer PARAMS ((output_buffer *,
101                                                   diagnostic_context *));
102
103 static void error_recursion PARAMS ((void)) ATTRIBUTE_NORETURN;
104
105 extern int rtl_dump_and_exit;
106 extern int inhibit_warnings;
107 extern int warnings_are_errors;
108 extern int warningcount;
109 extern int errorcount;
110
111 /* Front-end specific tree formatter, if non-NULL.  */
112 printer_fn lang_printer = NULL;
113
114 /* This must be large enough to hold any printed integer or
115    floating-point value.  */
116 static char digit_buffer[128];
117
118 /* An output_buffer surrogate for stderr.  */
119 static output_buffer global_output_buffer;
120 output_buffer *diagnostic_buffer = &global_output_buffer;
121
122 /* Function of last error message;
123    more generally, function such that if next error message is in it
124    then we don't have to mention the function name.  */
125 static tree last_error_function = NULL;
126
127 /* Used to detect when input_file_stack has changed since last described.  */
128 static int last_error_tick;
129
130 /* Called by report_error_function to print out function name.
131    Default may be overridden by language front-ends.  */
132
133 void (*print_error_function) PARAMS ((const char *)) =
134   default_print_error_function;
135
136 /* Hooks for language specific diagnostic messages pager and finalizer.  */
137 diagnostic_starter_fn lang_diagnostic_starter;
138 diagnostic_finalizer_fn lang_diagnostic_finalizer;
139
140 /* Maximum characters per line in automatic line wrapping mode.
141    Zero means don't wrap lines. */
142
143 int diagnostic_message_length_per_line;
144
145 /* Used to control every diagnostic message formatting.  Front-ends should
146    call set_message_prefixing_rule to set up their policies.  */
147 static int current_prefixing_rule;
148
149 /* Prevent recursion into the error handler.  */
150 static int diagnostic_lock;
151
152 \f
153 /* Return truthvalue if current input file is different from the most recent
154    file involved in a diagnostic message.  */
155
156 int
157 error_module_changed ()
158 {
159   return last_error_tick != input_file_stack_tick;
160 }
161
162 /* Remember current file as being the most recent file involved in a
163    diagnostic message.  */
164
165 void
166 record_last_error_module ()
167 {
168   last_error_tick = input_file_stack_tick;
169 }
170
171 /* Same as error_module_changed, but for function.  */
172
173 int
174 error_function_changed ()
175 {
176   return last_error_function != current_function_decl;
177 }
178
179 /* Same as record_last_error_module, but for function.  */
180
181 void
182 record_last_error_function ()
183 {
184   last_error_function = current_function_decl;
185 }
186
187 /* Initialize the diagnostic message outputting machinery.  */
188
189 void
190 initialize_diagnostics ()
191 {
192   /* By default, we don't line-wrap messages.  */
193   diagnostic_message_length_per_line = 0;
194   set_message_prefixing_rule (DIAGNOSTICS_SHOW_PREFIX_ONCE);
195
196   /* Proceed to actual initialization.  */
197   default_initialize_buffer (diagnostic_buffer);
198
199   lang_diagnostic_starter = default_diagnostic_starter;
200   lang_diagnostic_finalizer = default_diagnostic_finalizer;
201 }
202
203 void
204 set_message_prefixing_rule (rule)
205      int rule;
206 {
207   current_prefixing_rule = rule;
208 }
209
210 /* Returns true if BUFFER is in line-wrappind mode.  */
211
212 int
213 output_is_line_wrapping (buffer)
214      output_buffer *buffer;
215 {
216   return ideal_line_wrap_cutoff (buffer) > 0;
217 }
218
219 /* Return BUFFER's prefix.  */
220
221 const char *
222 output_get_prefix (buffer)
223      const output_buffer *buffer;
224 {
225   return output_prefix (buffer);
226 }
227
228 /* Subroutine of output_set_maximum_length.  Set up BUFFER's
229    internal maximum characters per line.  */
230
231 static void
232 set_real_maximum_length (buffer)
233      output_buffer *buffer;
234 {
235   /* If we're told not to wrap lines then do the obvious thing.  In case
236    we'll emit prefix only once per diagnostic message, it is appropriate
237   not to increase unncessarily the line-length cut-off.  */
238   if (! output_is_line_wrapping (buffer)
239       || prefixing_policy (buffer) == DIAGNOSTICS_SHOW_PREFIX_ONCE
240       || prefixing_policy (buffer) == DIAGNOSTICS_SHOW_PREFIX_NEVER)
241     line_wrap_cutoff (buffer) = ideal_line_wrap_cutoff (buffer);
242   else
243     {
244       int prefix_length =
245         output_prefix (buffer) ? strlen (output_prefix (buffer)) : 0;
246       /* If the prefix is ridiculously too long, output at least
247          32 characters.  */
248       if (ideal_line_wrap_cutoff (buffer) - prefix_length < 32)
249         line_wrap_cutoff (buffer) = ideal_line_wrap_cutoff (buffer) + 32;
250       else
251         line_wrap_cutoff (buffer) = ideal_line_wrap_cutoff (buffer);
252     }
253 }
254
255 /* Sets the number of maximum characters per line BUFFER can output
256    in line-wrapping mode.  A LENGTH value 0 suppresses line-wrapping.  */
257
258 void
259 output_set_maximum_length (buffer, length)
260      output_buffer *buffer;
261      int length;
262 {
263  ideal_line_wrap_cutoff (buffer) = length;
264   set_real_maximum_length (buffer);
265 }
266
267 /* Sets BUFFER's PREFIX.  */
268
269 void
270 output_set_prefix (buffer, prefix)
271      output_buffer *buffer;
272      const char *prefix;
273 {
274   output_prefix (buffer) = prefix;
275   set_real_maximum_length (buffer);
276   prefix_was_emitted_for (buffer) = 0;
277   output_indentation (buffer) = 0;
278 }
279
280 /*  Return a pointer to the last character emitted in the output
281     BUFFER area.  A NULL pointer means no character available.  */
282 const char *
283 output_last_position (buffer)
284      const output_buffer *buffer;
285 {
286   const char *p = NULL;
287   
288   if (obstack_base (&buffer->obstack) != obstack_next_free (&buffer->obstack))
289     p = ((const char *) obstack_next_free (&buffer->obstack)) - 1;
290   return p;
291 }
292
293 /* Free BUFFER's prefix, a previously malloc'd string.  */
294
295 void
296 output_destroy_prefix (buffer)
297      output_buffer *buffer;
298 {
299   if (output_prefix (buffer) != NULL)
300     {
301       free ((char *) output_prefix (buffer));
302       output_prefix (buffer) = NULL;
303     }
304 }
305
306 /* Zero out any text output so far in BUFFER.  */
307
308 void
309 output_clear_message_text (buffer)
310      output_buffer *buffer;
311 {
312   obstack_free (&buffer->obstack, obstack_base (&buffer->obstack));
313   output_text_length (buffer) = 0;
314 }
315
316 /* Zero out any diagnostic data used so far by BUFFER.  */
317
318 static void
319 clear_diagnostic_info (buffer)
320      output_buffer *buffer;
321 {
322   output_buffer_text_cursor (buffer) = NULL;
323   output_buffer_ptr_to_format_args (buffer) = NULL;
324   prefix_was_emitted_for (buffer) = 0;
325   output_indentation (buffer) = 0;
326 }
327
328 /* Construct an output BUFFER with PREFIX and of MAXIMUM_LENGTH
329    characters per line.  */
330
331 void
332 init_output_buffer (buffer, prefix, maximum_length)
333      output_buffer *buffer;
334      const char *prefix;
335      int maximum_length;
336 {
337   memset (buffer, 0, sizeof (output_buffer));
338   obstack_init (&buffer->obstack);
339   output_buffer_attached_stream (buffer) = stderr;
340   ideal_line_wrap_cutoff (buffer) = maximum_length;
341   prefixing_policy (buffer) = current_prefixing_rule;
342   output_set_prefix (buffer, prefix);
343   output_text_length (buffer) = 0;
344   clear_diagnostic_info (buffer);  
345 }
346
347 /* Initialize BUFFER with a NULL prefix and current diagnostic message
348    length cutoff.  */
349
350 void
351 default_initialize_buffer (buffer)
352      output_buffer *buffer;
353 {
354   init_output_buffer (buffer, NULL, diagnostic_message_length_per_line);
355 }
356
357 /* Recompute diagnostic_buffer's attributes to reflect any change
358    in diagnostic formatting global options.  */
359
360 void
361 reshape_diagnostic_buffer ()
362 {
363   ideal_line_wrap_cutoff (diagnostic_buffer) =
364     diagnostic_message_length_per_line;
365   prefixing_policy (diagnostic_buffer) = current_prefixing_rule;
366   set_real_maximum_length (diagnostic_buffer);
367 }
368
369 /* Reinitialize BUFFER.  */
370
371 void
372 output_clear (buffer)
373      output_buffer *buffer;
374 {
375   output_clear_message_text (buffer);
376   clear_diagnostic_info (buffer);
377 }
378
379 /* Finishes constructing a NULL-terminated character string representing
380    the BUFFERed message.  */
381
382 const char *
383 output_finalize_message (buffer)
384      output_buffer *buffer;
385 {
386   obstack_1grow (&buffer->obstack, '\0');
387   return output_message_text (buffer);
388 }
389
390 void
391 flush_diagnostic_buffer ()
392 {
393   output_buffer_to_stream (diagnostic_buffer);
394   fflush (output_buffer_attached_stream (diagnostic_buffer));
395 }
396
397 /* Return the amount of characters BUFFER can accept to
398    make a full line.  */
399
400 int
401 output_space_left (buffer)
402      const output_buffer *buffer;
403 {
404   return line_wrap_cutoff (buffer) - output_text_length (buffer);
405 }
406
407 /* Write out BUFFER's prefix.  */
408
409 void
410 output_emit_prefix (buffer)
411      output_buffer *buffer;
412 {
413   if (output_prefix (buffer) != NULL)
414     {
415       switch (prefixing_policy (buffer))
416         {
417         default:
418         case DIAGNOSTICS_SHOW_PREFIX_NEVER:
419           break;
420
421         case DIAGNOSTICS_SHOW_PREFIX_ONCE:
422           if (prefix_was_emitted_for (buffer))
423             {
424               output_indent (buffer);
425               break;
426             }
427           output_indentation (buffer) += 3;          
428           /* Fall through.  */
429
430         case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
431           {
432             int prefix_length = strlen (output_prefix (buffer));
433             output_append_r (buffer, output_prefix (buffer), prefix_length);
434             prefix_was_emitted_for (buffer) = 1;
435           }
436           break;
437         }
438     }
439 }
440
441 /* Have BUFFER start a new line.  */
442
443 void
444 output_add_newline (buffer)
445      output_buffer *buffer;
446 {
447   obstack_1grow (&buffer->obstack, '\n');
448   output_text_length (buffer) = 0;
449 }
450
451 /* Appends a character to BUFFER.  */
452
453 void
454 output_add_character (buffer, c)
455      output_buffer *buffer;
456      int c;
457 {
458   if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
459     output_add_newline (buffer);
460   obstack_1grow (&buffer->obstack, c);
461   ++output_text_length (buffer);
462 }
463
464 /* Adds a space to BUFFER.  */
465
466 void
467 output_add_space (buffer)
468      output_buffer *buffer;
469 {
470   if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
471     {
472       output_add_newline (buffer);
473       return;
474     }
475   obstack_1grow (&buffer->obstack, ' ');
476   ++output_text_length (buffer);
477 }
478
479 /* These functions format an INTEGER into BUFFER as suggested by their
480    names.  */
481
482 void
483 output_decimal (buffer, i)
484      output_buffer *buffer;
485      int i;
486 {
487   output_formatted_integer (buffer, "%d", i);
488 }
489
490 static void
491 output_long_decimal (buffer, i)
492      output_buffer *buffer;
493      long int i;
494 {
495   output_formatted_integer (buffer, "%ld", i);
496 }
497
498 static void
499 output_unsigned_decimal (buffer, i)
500      output_buffer *buffer;
501      unsigned int i;
502 {
503   output_formatted_integer (buffer, "%u", i);
504 }
505
506 static void
507 output_long_unsigned_decimal (buffer, i)
508      output_buffer *buffer;
509      long unsigned int i;
510 {
511   output_formatted_integer (buffer, "%lu", i);
512 }
513
514 static void
515 output_octal (buffer, i)
516      output_buffer *buffer;
517      unsigned int i;
518 {
519   output_formatted_integer (buffer, "%o", i);
520 }
521
522 static void
523 output_long_octal (buffer, i)
524      output_buffer *buffer;
525      unsigned long int i;
526 {
527   output_formatted_integer (buffer, "%lo", i);
528 }
529
530 static void
531 output_hexadecimal (buffer, i)
532      output_buffer *buffer;
533      unsigned int i;
534 {
535   output_formatted_integer (buffer, "%x", i);
536 }
537
538 static void
539 output_long_hexadecimal (buffer, i)
540      output_buffer *buffer;
541      unsigned long int i;
542 {
543   output_formatted_integer (buffer, "%lx", i);
544 }
545
546 /* Append to BUFFER a string specified by its STARTING character
547    and LENGTH.  */
548
549 static void
550 output_append_r (buffer, start, length)
551      output_buffer *buffer;
552      const char *start;
553      int length;
554 {
555   obstack_grow (&buffer->obstack, start, length);
556   output_text_length (buffer) += length;
557 }
558
559 /* Append a string deliminated by START and END to BUFFER.  No wrapping is
560    done.  However, if beginning a new line then emit output_prefix (BUFFER)
561    and skip any leading whitespace if appropriate.  The caller must ensure
562    that it is safe to do so.  */
563
564 void
565 output_append (buffer, start, end)
566      output_buffer *buffer;
567      const char *start;
568      const char *end;
569 {
570   /* Emit prefix and skip whitespace if we're starting a new line.  */
571   if (is_starting_newline (buffer))
572     {
573       output_emit_prefix (buffer);
574       if (output_is_line_wrapping (buffer))
575         while (start != end && *start == ' ')
576           ++start;
577     }
578   output_append_r (buffer, start, end - start);
579 }
580
581 static void
582 output_indent (buffer)
583      output_buffer *buffer;
584 {
585   int n = output_indentation (buffer);
586   int i;
587
588   for (i = 0; i < n; ++i)
589     output_add_character (buffer, ' ');
590 }
591
592 /* Wrap a text delimited by START and END into BUFFER.  */
593
594 static void
595 wrap_text (buffer, start, end)
596      output_buffer *buffer;
597      const char *start;
598      const char *end;
599 {
600   int is_wrapping = output_is_line_wrapping (buffer);
601   
602   while (start != end)
603     {
604       /* Dump anything bodered by whitespaces.  */ 
605       {
606         const char *p = start;
607         while (p != end && *p != ' ' && *p != '\n')
608           ++p;
609         if (is_wrapping && p - start >= output_space_left (buffer))
610           output_add_newline (buffer);
611         output_append (buffer, start, p);
612         start = p;
613       }
614
615       if (start != end && *start == ' ')
616         {
617           output_add_space (buffer);
618           ++start;
619         }
620       if (start != end && *start == '\n')
621         {
622           output_add_newline (buffer);
623           ++start;
624         }
625     }
626 }
627
628 /* Same as wrap_text but wrap text only when in line-wrapping mode.  */
629
630 static void
631 maybe_wrap_text (buffer, start, end)
632      output_buffer *buffer;
633      const char *start;
634      const char *end;
635 {
636   if (output_is_line_wrapping (buffer))
637     wrap_text (buffer, start, end);
638   else
639     output_append (buffer, start, end);
640 }
641
642
643 /* Append a STRING to BUFFER; the STRING might be line-wrapped if in
644    appropriate mode.  */
645
646 void
647 output_add_string (buffer, str)
648      output_buffer *buffer;
649      const char *str;
650 {
651   maybe_wrap_text (buffer, str, str + (str ? strlen (str) : 0));
652 }
653
654 /* Flush the content of BUFFER onto the attached stream,
655    and reinitialize.  */
656
657 static void
658 output_buffer_to_stream (buffer)
659      output_buffer *buffer;
660 {
661   const char *text = output_finalize_message (buffer);
662   fputs (text, output_buffer_attached_stream (buffer));
663   output_clear_message_text (buffer);
664 }
665
666 /* Format a message pointed to by output_buffer_text_cursor (BUFFER) using
667    output_buffer_format_args (BUFFER) as appropriate.  The following format
668    specifiers are recognized as being language independent:
669    %d, %i: (signed) integer in base ten.
670    %u: unsigned integer in base ten.
671    %o: unsigned integer in base eight.
672    %x: unsigned integer in base sixteen.
673    %ld, %li, %lo, %lu, %lx: long versions of the above.
674    %c: character.
675    %s: string.
676    %%: `%'.
677    %*.s: a substring the length of which is specified by an integer.  */
678
679 static void
680 output_format (buffer)
681      output_buffer *buffer;
682 {
683   for (; *output_buffer_text_cursor (buffer);
684        ++output_buffer_text_cursor (buffer))
685     {
686       int long_integer = 0;
687
688       /* Ignore text.  */
689       {
690         const char *p = output_buffer_text_cursor (buffer);
691         while (*p && *p != '%')
692           ++p;
693         wrap_text (buffer, output_buffer_text_cursor (buffer), p);
694         output_buffer_text_cursor (buffer) = p;
695       }
696
697       if (!*output_buffer_text_cursor (buffer))
698         break;
699
700       /* We got a '%'.  Let's see what happens. Record whether we're
701          parsing a long integer format specifier.  */
702       if (*++output_buffer_text_cursor (buffer) == 'l')
703         {
704           long_integer = 1;
705           ++output_buffer_text_cursor (buffer);
706         }
707
708       /* Handle %c, %d, %i, %ld, %li, %lo, %lu, %lx, %o, %s, %u,
709          %x, %.*s; %%.  And nothing else.  Front-ends should install
710          printers to grok language specific format specifiers.  */
711       switch (*output_buffer_text_cursor (buffer))
712         {
713         case 'c':
714           output_add_character
715             (buffer, va_arg (output_buffer_format_args (buffer), int));
716           break;
717           
718         case 'd':
719         case 'i':
720           if (long_integer)
721             output_long_decimal
722               (buffer, va_arg (output_buffer_format_args (buffer), long int));
723           else
724             output_decimal
725               (buffer, va_arg (output_buffer_format_args (buffer), int));
726           break;
727
728         case 'o':
729           if (long_integer)
730             output_long_octal (buffer,
731                                va_arg (output_buffer_format_args (buffer),
732                                        unsigned long int));
733           else
734             output_octal (buffer,
735                           va_arg (output_buffer_format_args (buffer),
736                                   unsigned int));
737           break;
738
739         case 's':
740           output_add_string (buffer,
741                              va_arg (output_buffer_format_args (buffer),
742                                      const char *));
743           break;
744
745         case 'u':
746           if (long_integer)
747             output_long_unsigned_decimal
748               (buffer, va_arg (output_buffer_format_args (buffer),
749                                long unsigned int));
750           else
751             output_unsigned_decimal
752               (buffer, va_arg (output_buffer_format_args (buffer),
753                                unsigned int));
754           break;
755           
756         case 'x':
757           if (long_integer)
758             output_long_hexadecimal
759               (buffer, va_arg (output_buffer_format_args (buffer),
760                                unsigned long int));
761           else
762             output_hexadecimal
763               (buffer, va_arg (output_buffer_format_args (buffer),
764                                unsigned int));
765           break;
766
767         case '%':
768           output_add_character (buffer, '%');
769           break;
770
771         case '.':
772           {
773             int n;
774             const char *s;
775             /* We handle no precision specifier but `%.*s'.  */
776             if (*++output_buffer_text_cursor (buffer) != '*')
777               abort ();
778             else if (*++output_buffer_text_cursor (buffer) != 's')
779               abort();
780             n = va_arg (output_buffer_format_args (buffer), int);
781             s = va_arg (output_buffer_format_args (buffer), const char *);
782             output_append (buffer, s, s + n);
783           }
784           break;
785
786         default:
787           if (! lang_printer || !(*lang_printer) (buffer))
788             {
789               /* Hmmm.  The front-end failed to install a format translator
790                  but called us with an unrecognized format.  Sorry.  */
791               abort ();
792             }
793         }
794     }
795 }
796
797 static char *
798 vbuild_message_string (msg, ap)
799      const char *msg;
800      va_list ap;
801 {
802   char *str;
803
804   vasprintf (&str, msg, ap);
805   return str;
806 }
807
808 /*  Return a malloc'd string containing MSG formatted a la
809     printf.  The caller is reponsible for freeing the memory.  */
810
811 static char *
812 build_message_string VPARAMS ((const char *msg, ...))
813 {
814 #ifndef ANSI_PROTOTYPES
815   const char *msg;
816 #endif
817   va_list ap;
818   char *str;
819
820   VA_START (ap, msg);
821
822 #ifndef ANSI_PROTOTYPES
823   msg = va_arg (ap, const char *);
824 #endif
825
826   str = vbuild_message_string (msg, ap);
827
828   va_end (ap);
829
830   return str;
831 }
832
833 /* Return a malloc'd string describing a location.  The caller is
834    responsible for freeing the memory.  */
835
836 char *
837 context_as_prefix (file, line, warn)
838      const char *file;
839      int line;
840      int warn;
841 {
842   if (file)
843     {
844       if (warn)
845         return build_message_string (_("%s:%d: warning: "), file, line);
846       else
847         return build_message_string ("%s:%d: ", file, line);
848     }
849   else
850     {
851       if (warn)
852         return build_message_string (_("%s: warning: "), progname);
853       else
854         return build_message_string ("%s: ", progname);
855     }
856 }
857
858 /* Same as context_as_prefix, but only the source FILE is given.  */
859
860 char *
861 file_name_as_prefix (f)
862      const char *f;
863 {
864   return build_message_string ("%s: ", f);
865 }
866
867 /* Format a MESSAGE into BUFFER.  Automatically wrap lines.  */
868
869 static void
870 output_do_printf (buffer, msg)
871      output_buffer *buffer;
872      const char *msg;
873 {
874   char *message = vbuild_message_string (msg,
875                                          output_buffer_format_args (buffer));
876
877   wrap_text (buffer, message, message + strlen (message));
878   free (message);
879 }
880
881
882 /* Format a message into BUFFER a la printf.  */
883
884 void
885 output_printf VPARAMS ((struct output_buffer *buffer, const char *msgid, ...))
886 {
887 #ifndef ANSI_PROTOTYPES
888   struct output_buffer *buffer;
889   const char *msgid;
890 #endif
891   va_list ap;
892   va_list *old_args;
893
894   VA_START (ap, msgid);
895 #ifndef ANSI_PROTOTYPES
896   buffer = va_arg (ap, output_buffer *);
897   msgid = va_arg (ap, const char *);
898 #endif
899   old_args = output_buffer_ptr_to_format_args (buffer);
900   output_buffer_ptr_to_format_args (buffer) = &ap;
901   output_do_printf (buffer, _(msgid));
902   output_buffer_ptr_to_format_args (buffer) = old_args;
903   va_end (ap);
904 }
905
906 /* Print a message relevant to the given DECL.  */
907
908 static void
909 format_with_decl (buffer, decl)
910      output_buffer *buffer;
911      tree decl;
912 {
913   const char *p;
914   
915   /* Do magic to get around lack of varargs support for insertion
916      of arguments into existing list.  We know that the decl is first;
917      we ass_u_me that it will be printed with "%s".  */
918   for (p = output_buffer_text_cursor (buffer); *p; ++p)
919     {
920       if (*p == '%')
921         {
922           if (*(p + 1) == '%')
923             ++p;
924           else if (*(p + 1) != 's')
925             abort ();
926           else
927             break;
928         }
929     }
930
931   /* Print the left-hand substring.  */
932   maybe_wrap_text (buffer, output_buffer_text_cursor (buffer), p);
933   
934   if (*p == '%')                /* Print the name.  */
935     {
936       const char *n = (DECL_NAME (decl)
937                  ? (*decl_printable_name) (decl, 2)
938                  : _("((anonymous))"));
939       output_add_string (buffer, n);
940       while (*p)
941         {
942           ++p;
943           if (ISALPHA (*(p - 1) & 0xFF))
944             break;
945         }
946     }
947
948   if (*p)                       /* Print the rest of the message.  */
949     {
950       output_buffer_text_cursor (buffer) = p;
951       output_format (buffer);
952     }
953 }
954
955 /* Figure file and line of the given INSN.  */
956
957 static void
958 file_and_line_for_asm (insn, pfile, pline)
959      rtx insn;
960      const char **pfile;
961      int *pline;
962 {
963   rtx body = PATTERN (insn);
964   rtx asmop;
965
966   /* Find the (or one of the) ASM_OPERANDS in the insn.  */
967   if (GET_CODE (body) == SET && GET_CODE (SET_SRC (body)) == ASM_OPERANDS)
968     asmop = SET_SRC (body);
969   else if (GET_CODE (body) == ASM_OPERANDS)
970     asmop = body;
971   else if (GET_CODE (body) == PARALLEL
972            && GET_CODE (XVECEXP (body, 0, 0)) == SET)
973     asmop = SET_SRC (XVECEXP (body, 0, 0));
974   else if (GET_CODE (body) == PARALLEL
975            && GET_CODE (XVECEXP (body, 0, 0)) == ASM_OPERANDS)
976     asmop = XVECEXP (body, 0, 0);
977   else
978     asmop = NULL;
979
980   if (asmop)
981     {
982       *pfile = ASM_OPERANDS_SOURCE_FILE (asmop);
983       *pline = ASM_OPERANDS_SOURCE_LINE (asmop);
984     }
985   else
986     {
987       *pfile = input_filename;
988       *pline = lineno;
989     }
990 }
991
992 /* Report a diagnostic MESSAGE (an errror or a WARNING) at the line number
993    of the insn INSN.  This is used only when INSN is an `asm' with operands,
994    and each ASM_OPERANDS records its own source file and line.  */
995
996 static void
997 diagnostic_for_asm (insn, msg, args_ptr, warn)
998      rtx insn;
999      const char *msg;
1000      va_list *args_ptr;
1001      int warn;
1002 {
1003   diagnostic_context dc;
1004
1005   set_diagnostic_context (&dc, msg, args_ptr, NULL, 0, warn);
1006   file_and_line_for_asm (insn, &diagnostic_file_location (&dc),
1007                          &diagnostic_line_location (&dc));
1008   report_diagnostic (&dc);
1009 }
1010
1011 /* Report a diagnostic MESSAGE at the declaration DECL.
1012    MSG is a format string which uses %s to substitute the declaration
1013    name; subsequent substitutions are a la output_format.  */
1014
1015 static void
1016 diagnostic_for_decl (decl, msgid, args_ptr, warn)
1017      tree decl;
1018      const char *msgid;
1019      va_list *args_ptr;
1020      int warn;
1021 {
1022   output_state os;
1023
1024   if (diagnostic_lock++)
1025     error_recursion ();
1026
1027   if (count_error (warn))
1028     {
1029       os = output_buffer_state (diagnostic_buffer);
1030       report_error_function (DECL_SOURCE_FILE (decl));
1031       output_set_prefix
1032         (diagnostic_buffer, context_as_prefix
1033          (DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl), warn));
1034       output_buffer_ptr_to_format_args (diagnostic_buffer) = args_ptr;
1035       output_buffer_text_cursor (diagnostic_buffer) = _(msgid);
1036       format_with_decl (diagnostic_buffer, decl);
1037       finish_diagnostic ();
1038       output_destroy_prefix (diagnostic_buffer);
1039   
1040       output_buffer_state (diagnostic_buffer) = os;
1041     }
1042   diagnostic_lock--;
1043 }
1044
1045 \f
1046 /* Count an error or warning.  Return 1 if the message should be printed.  */
1047
1048 int
1049 count_error (warningp)
1050      int warningp;
1051 {
1052   if (warningp
1053       && (inhibit_warnings
1054           || (in_system_header && !warn_system_headers)))
1055     return 0;
1056
1057   if (warningp && !warnings_are_errors)
1058     warningcount++;
1059   else
1060     {
1061       static int warning_message = 0;
1062
1063       if (warningp && !warning_message)
1064         {
1065           verbatim ("%s: warnings being treated as errors\n", progname);
1066           warning_message = 1;
1067         }
1068       errorcount++;
1069     }
1070
1071   return 1;
1072 }
1073
1074 /* Print a diagnostic MSGID on FILE.  This is just fprintf, except it
1075    runs its second argument through gettext.  */
1076
1077 void
1078 fnotice VPARAMS ((FILE *file, const char *msgid, ...))
1079 {
1080 #ifndef ANSI_PROTOTYPES
1081   FILE *file;
1082   const char *msgid;
1083 #endif
1084   va_list ap;
1085
1086   VA_START (ap, msgid);
1087
1088 #ifndef ANSI_PROTOTYPES
1089   file = va_arg (ap, FILE *);
1090   msgid = va_arg (ap, const char *);
1091 #endif
1092
1093   vfprintf (file, _(msgid), ap);
1094   va_end (ap);
1095 }
1096
1097
1098 /* Print a fatal I/O error message.  Argument are like printf.
1099    Also include a system error message based on `errno'.  */
1100
1101 void
1102 fatal_io_error VPARAMS ((const char *msgid, ...))
1103 {
1104 #ifndef ANSI_PROTOTYPES
1105   const char *msgid;
1106 #endif
1107   va_list ap;
1108   output_state os;
1109
1110   os = output_buffer_state (diagnostic_buffer);
1111   VA_START (ap, msgid);
1112
1113 #ifndef ANSI_PROTOTYPES
1114   msgid = va_arg (ap, const char *);
1115 #endif
1116
1117   output_printf (diagnostic_buffer, "%s: %s: ", progname, xstrerror (errno));
1118   output_buffer_ptr_to_format_args (diagnostic_buffer) = &ap;
1119   output_buffer_text_cursor (diagnostic_buffer) = _(msgid);
1120   output_format (diagnostic_buffer);
1121   finish_diagnostic ();
1122   output_buffer_state (diagnostic_buffer) = os;
1123   va_end (ap);
1124   exit (FATAL_EXIT_CODE);
1125 }
1126
1127 /* Issue a pedantic warning MSGID.  */
1128
1129 void
1130 pedwarn VPARAMS ((const char *msgid, ...))
1131 {
1132 #ifndef ANSI_PROTOTYPES
1133   const char *msgid;
1134 #endif
1135   va_list ap;
1136   diagnostic_context dc;
1137
1138   VA_START (ap, msgid);
1139
1140 #ifndef ANSI_PROTOTYPES
1141   msgid = va_arg (ap, const char *);
1142 #endif
1143
1144   set_diagnostic_context
1145     (&dc, msgid, &ap, input_filename, lineno, !flag_pedantic_errors);
1146   report_diagnostic (&dc);
1147   va_end (ap);
1148 }
1149
1150 /* Issue a pedantic waring about DECL.  */
1151
1152 void
1153 pedwarn_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1154 {
1155 #ifndef ANSI_PROTOTYPES
1156   tree decl;
1157   const char *msgid;
1158 #endif
1159   va_list ap;
1160
1161   VA_START (ap, msgid);
1162
1163 #ifndef ANSI_PROTOTYPES
1164   decl = va_arg (ap, tree);
1165   msgid = va_arg (ap, const char *);
1166 #endif
1167   /* We don't want -pedantic-errors to cause the compilation to fail from
1168      "errors" in system header files.  Sometimes fixincludes can't fix what's
1169      broken (eg: unsigned char bitfields - fixing it may change the alignment
1170      which will cause programs to mysteriously fail because the C library
1171      or kernel uses the original layout).  There's no point in issuing a
1172      warning either, it's just unnecessary noise.  */
1173   if (!DECL_IN_SYSTEM_HEADER (decl))
1174     diagnostic_for_decl (decl, msgid, &ap, !flag_pedantic_errors);
1175   va_end (ap);
1176 }
1177
1178 /* Same as above but within the context FILE and LINE. */
1179
1180 void
1181 pedwarn_with_file_and_line VPARAMS ((const char *file, int line,
1182                                      const char *msgid, ...))
1183 {
1184 #ifndef ANSI_PROTOTYPES
1185   const char *file;
1186   int line;
1187   const char *msgid;
1188 #endif
1189   va_list ap;
1190   diagnostic_context dc;
1191
1192   VA_START (ap, msgid);
1193
1194 #ifndef ANSI_PROTOTYPES
1195   file = va_arg (ap, const char *);
1196   line = va_arg (ap, int);
1197   msgid = va_arg (ap, const char *);
1198 #endif
1199
1200   set_diagnostic_context (&dc, msgid, &ap, file, line, !flag_pedantic_errors);
1201   report_diagnostic (&dc);
1202   va_end (ap);
1203 }
1204
1205 /* Just apologize with MSGID.  */
1206
1207 void
1208 sorry VPARAMS ((const char *msgid, ...))
1209 {
1210 #ifndef ANSI_PROTOTYPES
1211   const char *msgid;
1212 #endif
1213   va_list ap;
1214   output_state os;
1215
1216   os = output_buffer_state (diagnostic_buffer);
1217   VA_START (ap, msgid);
1218
1219 #ifndef ANSI_PROTOTYPES
1220   msgid = va_arg (ap, const char *);
1221 #endif
1222   ++sorrycount;
1223   output_set_prefix
1224     (diagnostic_buffer, context_as_prefix (input_filename, lineno, 0));
1225   output_printf (diagnostic_buffer, "sorry, not implemented: ");
1226   output_buffer_ptr_to_format_args (diagnostic_buffer) = &ap;
1227   output_buffer_text_cursor (diagnostic_buffer) = _(msgid);
1228   output_format (diagnostic_buffer);
1229   finish_diagnostic ();
1230   output_buffer_state (diagnostic_buffer) = os;
1231   va_end (ap);
1232 }
1233
1234 /* Called when the start of a function definition is parsed,
1235    this function prints on stderr the name of the function.  */
1236
1237 void
1238 announce_function (decl)
1239      tree decl;
1240 {
1241   if (! quiet_flag)
1242     {
1243       if (rtl_dump_and_exit)
1244         verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl)));
1245       else
1246         verbatim (" %s", (*decl_printable_name) (decl, 2));
1247       fflush (stderr);
1248       output_needs_newline (diagnostic_buffer) = 1;
1249       record_last_error_function ();
1250     }
1251 }
1252
1253 /* The default function to print out name of current function that caused
1254    an error.  */
1255
1256 void
1257 default_print_error_function (file)
1258   const char *file;
1259 {
1260   if (error_function_changed ())
1261     {
1262       char *prefix = file ? build_message_string ("%s: ", file) : NULL;
1263       output_state os;
1264
1265       os = output_buffer_state (diagnostic_buffer);
1266       output_set_prefix (diagnostic_buffer, prefix);
1267       
1268       if (current_function_decl == NULL)
1269           output_add_string (diagnostic_buffer, _("At top level:"));
1270       else
1271         {
1272           if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
1273             output_printf
1274               (diagnostic_buffer, "In method `%s':",
1275                (*decl_printable_name) (current_function_decl, 2));
1276           else
1277             output_printf
1278               (diagnostic_buffer, "In function `%s':",
1279                (*decl_printable_name) (current_function_decl, 2));
1280         }
1281       output_add_newline (diagnostic_buffer);
1282
1283       record_last_error_function ();
1284       output_buffer_to_stream (diagnostic_buffer);
1285       output_buffer_state (diagnostic_buffer) = os;
1286       free ((char*) prefix);
1287     }
1288 }
1289
1290 /* Prints out, if necessary, the name of the current function
1291   that caused an error.  Called from all error and warning functions.
1292   We ignore the FILE parameter, as it cannot be relied upon.  */
1293
1294 void
1295 report_error_function (file)
1296   const char *file ATTRIBUTE_UNUSED;
1297 {
1298   report_problematic_module (diagnostic_buffer);
1299   (*print_error_function) (input_filename);
1300 }
1301
1302 void
1303 error_with_file_and_line VPARAMS ((const char *file, int line,
1304                                    const char *msgid, ...))
1305 {
1306 #ifndef ANSI_PROTOTYPES
1307   const char *file;
1308   int line;
1309   const char *msgid;
1310 #endif
1311   va_list ap;
1312   diagnostic_context dc;
1313
1314   VA_START (ap, msgid);
1315
1316 #ifndef ANSI_PROTOTYPES
1317   file = va_arg (ap, const char *);
1318   line = va_arg (ap, int);
1319   msgid = va_arg (ap, const char *);
1320 #endif
1321
1322   set_diagnostic_context (&dc, msgid, &ap, file, line, /* warn = */ 0);
1323   report_diagnostic (&dc);
1324   va_end (ap);
1325 }
1326
1327 void
1328 error_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1329 {
1330 #ifndef ANSI_PROTOTYPES
1331   tree decl;
1332   const char *msgid;
1333 #endif
1334   va_list ap;
1335
1336   VA_START (ap, msgid);
1337
1338 #ifndef ANSI_PROTOTYPES
1339   decl = va_arg (ap, tree);
1340   msgid = va_arg (ap, const char *);
1341 #endif
1342
1343   diagnostic_for_decl (decl, msgid, &ap, /* warn = */ 0);
1344   va_end (ap);
1345 }
1346
1347 void
1348 error_for_asm VPARAMS ((rtx insn, const char *msgid, ...))
1349 {
1350 #ifndef ANSI_PROTOTYPES
1351   rtx insn;
1352   const char *msgid;
1353 #endif
1354   va_list ap;
1355
1356   VA_START (ap, msgid);
1357
1358 #ifndef ANSI_PROTOTYPES
1359   insn = va_arg (ap, rtx);
1360   msgid = va_arg (ap, const char *);
1361 #endif
1362
1363   diagnostic_for_asm (insn, msgid, &ap, /* warn = */ 0);
1364   va_end (ap);
1365 }
1366
1367 /* Report an error message.  The arguments are like that of printf.  */
1368
1369 void
1370 error VPARAMS ((const char *msgid, ...))
1371 {
1372 #ifndef ANSI_PROTOTYPES
1373   const char *msgid;
1374 #endif
1375   va_list ap;
1376   diagnostic_context dc;
1377
1378   VA_START (ap, msgid);
1379
1380 #ifndef ANSI_PROTOTYPES
1381   msgid = va_arg (ap, const char *);
1382 #endif
1383
1384   set_diagnostic_context
1385     (&dc, msgid, &ap, input_filename, lineno, /* warn = */ 0);
1386   report_diagnostic (&dc);
1387   va_end (ap);
1388 }
1389
1390 /* Likewise, except that the compilation is terminated after printing the
1391    error message.  */
1392
1393 void
1394 fatal_error VPARAMS ((const char *msgid, ...))
1395 {
1396 #ifndef ANSI_PROTOTYPES
1397   const char *msgid;
1398 #endif
1399   va_list ap;
1400   diagnostic_context dc;
1401
1402   VA_START (ap, msgid);
1403
1404 #ifndef ANSI_PROTOTYPES
1405   msgid = va_arg (ap, const char *);
1406 #endif
1407
1408   set_diagnostic_context
1409     (&dc, msgid, &ap, input_filename, lineno, /* warn = */ 0);
1410   report_diagnostic (&dc);
1411   va_end (ap);
1412
1413   fnotice (stderr, "compilation terminated.\n");
1414   exit (FATAL_EXIT_CODE);
1415 }
1416
1417 /* Report a compiler error at the current line number.  Allow a front end to
1418    intercept the message.  */
1419
1420 static void (*internal_error_function) PARAMS ((const char *, va_list *));
1421
1422 /* Set the function to call when a compiler error occurs.  */
1423
1424 void
1425 set_internal_error_function (f)
1426      void (*f) PARAMS ((const char *, va_list *));
1427 {
1428   internal_error_function = f;
1429 }
1430
1431 void
1432 internal_error VPARAMS ((const char *msgid, ...))
1433 {
1434 #ifndef ANSI_PROTOTYPES
1435   const char *msgid;
1436 #endif
1437   va_list ap;
1438   diagnostic_context dc;
1439
1440   VA_START (ap, msgid);
1441
1442 #ifndef ANSI_PROTOTYPES
1443   msgid = va_arg (ap, const char *);
1444 #endif
1445
1446   if (errorcount > 0 || sorrycount > 0)
1447     {
1448       fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
1449                input_filename, lineno);
1450       exit (FATAL_EXIT_CODE);
1451     }
1452
1453   if (internal_error_function != 0)
1454     (*internal_error_function) (_(msgid), &ap);
1455   
1456   set_diagnostic_context
1457     (&dc, msgid, &ap, input_filename, lineno, /* warn = */0);
1458   report_diagnostic (&dc);
1459   va_end (ap);
1460
1461   fnotice (stderr,
1462 "Please submit a full bug report,\n\
1463 with preprocessed source if appropriate.\n\
1464 See %s for instructions.\n", GCCBUGURL);
1465   exit (FATAL_EXIT_CODE);
1466 }
1467
1468 void
1469 _fatal_insn (msgid, insn, file, line, function)
1470      const char *msgid;
1471      rtx insn;
1472      const char *file;
1473      int line;
1474      const char *function;
1475 {
1476   error ("%s", _(msgid));
1477
1478   /* The above incremented error_count, but isn't an error that we want to
1479      count, so reset it here.  */
1480   errorcount--;
1481
1482   debug_rtx (insn);
1483   fancy_abort (file, line, function);
1484 }
1485
1486 void
1487 _fatal_insn_not_found (insn, file, line, function)
1488      rtx insn;
1489      const char *file;
1490      int line;
1491      const char *function;
1492 {
1493   if (INSN_CODE (insn) < 0)
1494     _fatal_insn ("Unrecognizable insn:", insn, file, line, function);
1495   else
1496     _fatal_insn ("Insn does not satisfy its constraints:",
1497                 insn, file, line, function);
1498 }
1499
1500 void
1501 warning_with_file_and_line VPARAMS ((const char *file, int line,
1502                                      const char *msgid, ...))
1503 {
1504 #ifndef ANSI_PROTOTYPES
1505   const char *file;
1506   int line;
1507   const char *msgid;
1508 #endif
1509   va_list ap;
1510   diagnostic_context dc;
1511
1512   VA_START (ap, msgid);
1513
1514 #ifndef ANSI_PROTOTYPES
1515   file = va_arg (ap, const char *);
1516   line = va_arg (ap, int);
1517   msgid = va_arg (ap, const char *);
1518 #endif
1519
1520   set_diagnostic_context (&dc, msgid, &ap, file, line, /* warn = */ 1);
1521   report_diagnostic (&dc);
1522   va_end (ap);
1523 }
1524
1525 void
1526 warning_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1527 {
1528 #ifndef ANSI_PROTOTYPES
1529   tree decl;
1530   const char *msgid;
1531 #endif
1532   va_list ap;
1533
1534   VA_START (ap, msgid);
1535
1536 #ifndef ANSI_PROTOTYPES
1537   decl = va_arg (ap, tree);
1538   msgid = va_arg (ap, const char *);
1539 #endif
1540
1541   diagnostic_for_decl (decl, msgid, &ap, /* warn = */ 1);
1542   va_end (ap);
1543 }
1544
1545 void
1546 warning_for_asm VPARAMS ((rtx insn, const char *msgid, ...))
1547 {
1548 #ifndef ANSI_PROTOTYPES
1549   rtx insn;
1550   const char *msgid;
1551 #endif
1552   va_list ap;
1553
1554   VA_START (ap, msgid);
1555
1556 #ifndef ANSI_PROTOTYPES
1557   insn = va_arg (ap, rtx);
1558   msgid = va_arg (ap, const char *);
1559 #endif
1560
1561   diagnostic_for_asm (insn, msgid, &ap, /* warn = */ 1);
1562   va_end (ap);
1563 }
1564
1565 void
1566 warning VPARAMS ((const char *msgid, ...))
1567 {
1568 #ifndef ANSI_PROTOTYPES
1569   const char *msgid;
1570 #endif
1571   va_list ap;
1572   diagnostic_context dc;
1573
1574   VA_START (ap, msgid);
1575
1576 #ifndef ANSI_PROTOTYPES
1577   msgid = va_arg (ap, const char *);
1578 #endif
1579
1580   set_diagnostic_context
1581     (&dc, msgid, &ap, input_filename, lineno, /* warn = */ 1);
1582   report_diagnostic (&dc);
1583   va_end (ap);
1584 }
1585
1586 /* Flush diagnostic_buffer content on stderr.  */
1587
1588 static void
1589 finish_diagnostic ()
1590 {
1591   output_buffer_to_stream (diagnostic_buffer);
1592   clear_diagnostic_info (diagnostic_buffer);
1593   fputc ('\n', output_buffer_attached_stream (diagnostic_buffer));
1594   fflush (output_buffer_attached_stream (diagnostic_buffer));
1595 }
1596
1597 /* Helper subroutine of output_verbatim and verbatim. Do the approriate
1598    settings needed by BUFFER for a verbatim formatting.  */
1599
1600 static void
1601 output_do_verbatim (buffer, msgid, args_ptr)
1602      output_buffer *buffer;
1603      const char *msgid;
1604      va_list *args_ptr;
1605 {
1606   output_state os;
1607
1608   os = output_buffer_state (buffer);
1609   output_prefix (buffer) = NULL;
1610   prefixing_policy (buffer) = DIAGNOSTICS_SHOW_PREFIX_NEVER;
1611   output_buffer_text_cursor (buffer) = _(msgid);
1612   output_buffer_ptr_to_format_args (buffer) = args_ptr;
1613   output_set_maximum_length (buffer, 0);
1614   output_format (buffer);
1615   output_buffer_state (buffer) = os;
1616 }
1617
1618 /* Output MESSAGE verbatim into BUFFER.  */
1619
1620 void
1621 output_verbatim VPARAMS ((output_buffer *buffer, const char *msgid, ...))
1622 {
1623 #ifndef ANSI_PROTOTYPES
1624   output_buffer *buffer;
1625   const char *msgid;
1626 #endif
1627   va_list ap;
1628
1629   VA_START (ap, msgid);
1630 #ifndef ANSI_PROTOTYPES
1631   buffer = va_arg (ap, output_buffer *);
1632   msg = va_arg (ap, const char *);
1633 #endif
1634   output_do_verbatim (buffer, msgid, &ap);
1635   va_end (ap);
1636 }
1637
1638 /* Same as above but use diagnostic_buffer.  */
1639
1640 void
1641 verbatim VPARAMS ((const char *msgid, ...))
1642 {
1643 #ifndef ANSI_PROTOTYPES
1644   const char *msgid;
1645 #endif
1646   va_list ap;
1647
1648   VA_START (ap, msgid);
1649 #ifndef ANSI_PROTOTYPES
1650   msgid = va_arg (ap, const char *);
1651 #endif
1652   output_do_verbatim (diagnostic_buffer, msgid, &ap);
1653   output_buffer_to_stream (diagnostic_buffer);
1654   va_end (ap);
1655 }
1656
1657 /* Report a diagnostic message (an error or a warning) as specified by
1658    DC.  This function is *the* subroutine in terms of which front-ends
1659    should implement their specific diagnostic handling modules.  The
1660    front-end independent format specifiers are exactly those described
1661    in the documentation of output_format.  */
1662
1663 void
1664 report_diagnostic (dc)
1665      diagnostic_context *dc;
1666 {
1667   output_state os;
1668
1669   if (diagnostic_lock++)
1670     error_recursion ();
1671
1672   if (count_error (diagnostic_is_warning (dc)))
1673     {
1674       os = output_buffer_state (diagnostic_buffer);
1675       diagnostic_msg = diagnostic_message (dc);
1676       diagnostic_args = diagnostic_argument_list (dc);
1677       (*diagnostic_starter (dc)) (diagnostic_buffer, dc);
1678       output_format (diagnostic_buffer);
1679       (*diagnostic_finalizer (dc)) (diagnostic_buffer, dc);
1680       finish_diagnostic ();
1681       output_buffer_state (diagnostic_buffer) = os;
1682     }
1683
1684   diagnostic_lock--;
1685 }
1686
1687 /* Inform the user that an error occurred while trying to report some
1688    other error.  This indicates catastrophic internal inconsistencies,
1689    so give up now.  But do try to flush out the previous error.
1690    This mustn't use internal_error, that will cause infinite recursion.  */
1691
1692 static void
1693 error_recursion ()
1694 {
1695   if (diagnostic_lock < 3)
1696     finish_diagnostic ();
1697
1698   fnotice (stderr,
1699            "Internal compiler error: Error reporting routines re-entered.\n");
1700   fnotice (stderr,
1701 "Please submit a full bug report,\n\
1702 with preprocessed source if appropriate.\n\
1703 See %s for instructions.\n", GCCBUGURL);
1704   exit (FATAL_EXIT_CODE);
1705 }
1706
1707 /* Given a partial pathname as input, return another pathname that
1708    shares no directory elements with the pathname of __FILE__.  This
1709    is used by fancy_abort() to print `Internal compiler error in expr.c'
1710    instead of `Internal compiler error in ../../GCC/gcc/expr.c'.  */
1711
1712 const char *
1713 trim_filename (name)
1714      const char *name;
1715 {
1716   static const char this_file[] = __FILE__;
1717   const char *p = name, *q = this_file;
1718
1719   /* First skip any "../" in each filename.  This allows us to give a proper
1720      reference to a file in a subdirectory.  */
1721   while (p[0] == '.' && p[1] == '.'
1722          && (p[2] == DIR_SEPARATOR
1723 #ifdef DIR_SEPARATOR_2
1724              || p[2] == DIR_SEPARATOR_2
1725 #endif
1726              ))
1727     p += 3;
1728
1729   while (q[0] == '.' && q[1] == '.'
1730          && (q[2] == DIR_SEPARATOR
1731 #ifdef DIR_SEPARATOR_2
1732              || p[2] == DIR_SEPARATOR_2
1733 #endif
1734              ))
1735     q += 3;
1736
1737   /* Now skip any parts the two filenames have in common.  */
1738   while (*p == *q && *p != 0 && *q != 0)
1739     p++, q++;
1740
1741   /* Now go backwards until the previous directory separator.  */
1742   while (p > name && p[-1] != DIR_SEPARATOR
1743 #ifdef DIR_SEPARATOR_2
1744          && p[-1] != DIR_SEPARATOR_2
1745 #endif
1746          )
1747     p--;
1748
1749   return p;
1750 }
1751
1752 /* Report an internal compiler error in a friendly manner and without
1753    dumping core.  */
1754
1755 void
1756 fancy_abort (file, line, function)
1757      const char *file;
1758      int line;
1759      const char *function;
1760 {
1761   internal_error ("Internal compiler error in %s, at %s:%d",
1762                   function, trim_filename (file), line);
1763 }
1764
1765 /* Setup DC for reporting a diagnostic MESSAGE (an error or a WARNING),
1766    using arguments pointed to by ARGS_PTR, issued at a location specified
1767    by FILE and LINE.  */
1768
1769 void
1770 set_diagnostic_context (dc, msgid, args_ptr, file, line, warn)
1771      diagnostic_context *dc;
1772      const char *msgid;
1773      va_list *args_ptr;
1774      const char *file;
1775      int line;
1776      int warn;
1777 {
1778   memset (dc, 0, sizeof (diagnostic_context));
1779   diagnostic_message (dc) = _(msgid);
1780   diagnostic_argument_list (dc) = args_ptr;
1781   diagnostic_file_location (dc) = file;
1782   diagnostic_line_location (dc) = line;
1783   diagnostic_is_warning (dc) = warn;
1784   diagnostic_starter (dc) = lang_diagnostic_starter;
1785   diagnostic_finalizer (dc) = lang_diagnostic_finalizer;
1786 }
1787
1788 void
1789 report_problematic_module (buffer)
1790      output_buffer *buffer;
1791 {
1792   struct file_stack *p;
1793
1794   if (output_needs_newline (buffer))
1795     {
1796       output_add_newline (buffer);
1797       output_needs_newline (buffer) = 0;
1798     }
1799
1800   if (input_file_stack && input_file_stack->next != 0
1801       && error_module_changed ())
1802     {
1803       for (p = input_file_stack->next; p; p = p->next)
1804         if (p == input_file_stack->next)
1805           output_verbatim
1806             (buffer, "In file included from %s:%d", p->name, p->line);
1807         else
1808           output_verbatim
1809             (buffer, ",\n                 from %s:%d", p->name, p->line);
1810       output_verbatim (buffer, ":\n");
1811       record_last_error_module ();
1812     }
1813 }
1814
1815 static void
1816 default_diagnostic_starter (buffer, dc)
1817      output_buffer *buffer;
1818      diagnostic_context *dc;
1819 {
1820   report_error_function (diagnostic_file_location (dc));
1821   output_set_prefix (buffer,
1822                      context_as_prefix (diagnostic_file_location (dc),
1823                                         diagnostic_line_location (dc),
1824                                         diagnostic_is_warning (dc)));
1825 }
1826
1827 static void
1828 default_diagnostic_finalizer (buffer, dc)
1829      output_buffer *buffer;
1830      diagnostic_context *dc __attribute__((__unused__));
1831 {
1832   output_destroy_prefix (buffer);
1833 }