OSDN Git Service

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