OSDN Git Service

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