OSDN Git Service

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