OSDN Git Service

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