OSDN Git Service

2000-06-28 Philipp Thomas <pthomas@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / diagnostic.c
1 /* Language-independent diagnostic subroutines for the GNU C compiler
2    Copyright (C) 1999, 2000 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21
22 /* This file implements the language independant aspect of diagnostic
23    message module.  */
24
25 #include "config.h"
26 #undef FLOAT /* This is for hpux. They should change hpux.  */
27 #undef FFS  /* Some systems define this in param.h.  */
28 #include "system.h"
29
30 #include "tree.h"
31 #include "rtl.h"
32 #include "tm_p.h"
33 #include "flags.h"
34 #include "input.h"
35 #include "insn-attr.h"
36 #include "insn-codes.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 /* Prototypes. */
46 static int doing_line_wrapping PARAMS ((void));
47
48 static char *vbuild_message_string PARAMS ((const char *, va_list));
49 static char *build_message_string PARAMS ((const char *, ...))
50      ATTRIBUTE_PRINTF_1;
51 static char *build_location_prefix PARAMS ((const char *, int, int));
52 static void output_notice PARAMS ((output_buffer *, const char *));
53 static void line_wrapper_printf PARAMS ((FILE *, const char *, ...))
54      ATTRIBUTE_PRINTF_2;
55 static void vline_wrapper_message_with_location PARAMS ((const char *, int,
56                                                          int, const char *,
57                                                          va_list));
58 static void notice PARAMS ((const char *s, ...)) ATTRIBUTE_PRINTF_1;
59 static void v_message_with_file_and_line PARAMS ((const char *, int, int,
60                                                   const char *, va_list));
61 static void v_message_with_decl PARAMS ((tree, int, const char *, va_list));
62 static void file_and_line_for_asm PARAMS ((rtx, const char **, int *));
63 static void v_error_with_file_and_line PARAMS ((const char *, int,
64                                                 const char *, va_list));
65 static void v_error_with_decl PARAMS ((tree, const char *, va_list));
66 static void v_error_for_asm PARAMS ((rtx, const char *, va_list));
67 static void vfatal PARAMS ((const char *, va_list)) ATTRIBUTE_NORETURN;
68 static void v_warning_with_file_and_line PARAMS ((const char *, int,
69                                                   const char *, va_list));
70 static void v_warning_with_decl PARAMS ((tree, const char *, va_list));
71 static void v_warning_for_asm PARAMS ((rtx, const char *, va_list));
72 static void v_pedwarn_with_decl PARAMS ((tree, const char *, va_list));
73 static void v_pedwarn_with_file_and_line PARAMS ((const char *, int,
74                                                   const char *, va_list));
75 static void vsorry PARAMS ((const char *, va_list));
76 static void report_file_and_line PARAMS ((const char *, int, int));
77 static void vnotice PARAMS ((FILE *, const char *, va_list));
78 static void set_real_maximum_length PARAMS ((output_buffer *));
79
80 extern int rtl_dump_and_exit;
81 extern int inhibit_warnings;
82 extern int warnings_are_errors;
83 extern int warningcount;
84 extern int errorcount;
85
86 /* Front-end specific tree formatter, if non-NULL.  */
87 printer_fn lang_printer = NULL;
88
89 /* An output_buffer surrogate for stderr.  */
90 static output_buffer global_output_buffer;
91 output_buffer *diagnostic_buffer = &global_output_buffer;
92
93 static int need_error_newline;
94
95 /* Function of last error message;
96    more generally, function such that if next error message is in it
97    then we don't have to mention the function name.  */
98 static tree last_error_function = NULL;
99
100 /* Used to detect when input_file_stack has changed since last described.  */
101 static int last_error_tick;
102
103 /* Called by report_error_function to print out function name.
104  * Default may be overridden by language front-ends.  */
105
106 void (*print_error_function) PARAMS ((const char *)) =
107   default_print_error_function;
108
109 /* Maximum characters per line in automatic line wrapping mode.
110    Zero means don't wrap lines. */
111
112 int diagnostic_message_length_per_line;
113
114 /* Used to control every diagnostic message formatting.  Front-ends should
115    call set_message_prefixing_rule to set up their politics.  */
116 static int current_prefixing_rule;
117 \f
118 /* Initialize the diagnostic message outputting machinery.  */
119
120 void
121 initialize_diagnostics ()
122 {
123   /* By default, we don't line-wrap messages.  */
124   diagnostic_message_length_per_line = 0;
125   set_message_prefixing_rule (DIAGNOSTICS_SHOW_PREFIX_ONCE);
126   /* Proceed to actual initialization.  */
127   default_initialize_buffer (diagnostic_buffer);
128 }
129
130 /* Predicate. Return 1 if we're in automatic line wrapping mode.  */
131
132 static int
133 doing_line_wrapping ()
134 {
135   return diagnostic_message_length_per_line > 0;
136 }
137
138 void
139 set_message_prefixing_rule (rule)
140      int rule;
141 {
142   current_prefixing_rule = rule;
143 }
144
145 /* Returns true if BUFFER is in line-wrappind mode.  */
146 int
147 output_is_line_wrapping (buffer)
148      output_buffer *buffer;
149 {
150   return buffer->ideal_maximum_length > 0;
151 }
152
153 /* Return BUFFER's prefix.  */
154 const char *
155 output_get_prefix (buffer)
156      const output_buffer *buffer;
157 {
158   return buffer->prefix;
159 }
160
161 /* Subroutine of output_set_maximum_length.  Set up BUFFER's
162    internal maximum characters per line.  */
163 static void
164 set_real_maximum_length (buffer)
165      output_buffer *buffer;
166 {
167   /* If we're told not to wrap lines then do the obvious thing.  */
168   if (! output_is_line_wrapping (buffer))
169     buffer->maximum_length = buffer->ideal_maximum_length;
170   else
171     {
172       int prefix_length = buffer->prefix ? strlen (buffer->prefix) : 0;
173       /* If the prefix is ridiculously too long, output at least
174          32 characters.  */
175       if (buffer->ideal_maximum_length - prefix_length < 32)
176         buffer->maximum_length = buffer->ideal_maximum_length + 32;
177       else
178         buffer->maximum_length = buffer->ideal_maximum_length;
179     }
180 }
181
182 /* Sets the number of maximum characters per line BUFFER can output
183    in line-wrapping mode.  A LENGTH value 0 suppresses line-wrapping.  */
184 void
185 output_set_maximum_length (buffer, length)
186      output_buffer *buffer;
187      int length;
188 {
189   buffer->ideal_maximum_length = length;
190   set_real_maximum_length (buffer);
191 }
192
193 /* Sets BUFFER's PREFIX.  */
194 void
195 output_set_prefix (buffer, prefix)
196      output_buffer *buffer;
197      const char *prefix;
198 {
199   buffer->prefix = prefix;
200   set_real_maximum_length (buffer);
201   buffer->emitted_prefix_p = 0;
202 }
203
204 /* Free BUFFER's prefix, a previously malloc()'d string.  */
205
206 void
207 output_destroy_prefix (buffer)
208      output_buffer *buffer;
209 {
210   if (buffer->prefix)
211     {
212       free ((char *) buffer->prefix);
213       buffer->prefix = NULL;
214     }
215 }
216
217 /* Construct an output BUFFER with PREFIX and of MAXIMUM_LENGTH
218    characters per line.  */
219 void
220 init_output_buffer (buffer, prefix, maximum_length)
221      output_buffer *buffer;
222      const char *prefix;
223      int maximum_length;
224 {
225   obstack_init (&buffer->obstack);
226   buffer->ideal_maximum_length = maximum_length;
227   buffer->line_length = 0;
228   output_set_prefix (buffer, prefix);
229   buffer->emitted_prefix_p = 0;
230   buffer->prefixing_rule = current_prefixing_rule;
231   
232   buffer->cursor = NULL;
233 }
234
235 /* Initialize BUFFER with a NULL prefix and current diagnostic message
236    length cutoff.  */
237 void
238 default_initialize_buffer (buffer)
239      output_buffer *buffer;
240 {
241   init_output_buffer (buffer, NULL, diagnostic_message_length_per_line);
242 }
243
244 /* Recompute diagnostic_buffer's attributes to reflect any change
245    in diagnostic formatting global options.  */
246 void
247 reshape_diagnostic_buffer ()
248 {
249   diagnostic_buffer->ideal_maximum_length = diagnostic_message_length_per_line;
250   diagnostic_buffer->prefixing_rule = current_prefixing_rule;
251   set_real_maximum_length (diagnostic_buffer);
252 }
253
254 /* Reinitialize BUFFER.  */
255 void
256 output_clear (buffer)
257      output_buffer *buffer;
258 {
259   obstack_free (&buffer->obstack, obstack_base (&buffer->obstack));
260   buffer->line_length = 0;
261   buffer->cursor = NULL;
262   buffer->emitted_prefix_p = 0;
263 }
264
265 /* Finishes to construct a NULL-terminated character string representing
266    the BUFFERed message.  */
267 const char *
268 output_finish (buffer)
269      output_buffer *buffer;
270 {
271   obstack_1grow (&buffer->obstack, '\0');
272   return (const char *) obstack_finish (&buffer->obstack);
273 }
274
275 /* Return the amount of characters BUFFER can accept to
276    make a full line.  */
277 int
278 output_space_left (buffer)
279      const output_buffer *buffer;
280 {
281   return buffer->maximum_length - buffer->line_length;
282 }
283
284 /* Write out BUFFER's prefix.  */
285 void
286 output_emit_prefix (buffer)
287      output_buffer *buffer;
288 {
289   if (buffer->prefix)
290     {
291       switch (buffer->prefixing_rule)
292         {
293         default:
294         case DIAGNOSTICS_SHOW_PREFIX_NEVER:
295           break;
296
297         case DIAGNOSTICS_SHOW_PREFIX_ONCE:
298           if (buffer->emitted_prefix_p)
299             break;
300           else
301             buffer->emitted_prefix_p = 1;
302           /* Fall through.  */
303
304         case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
305           buffer->line_length += strlen (buffer->prefix);
306           obstack_grow
307             (&buffer->obstack, buffer->prefix, buffer->line_length);
308           break;
309         }
310     }
311 }
312
313 /* Have BUFFER start a new line.  */
314 void
315 output_add_newline (buffer)
316      output_buffer *buffer;
317 {
318   obstack_1grow (&buffer->obstack, '\n');
319   buffer->line_length = 0;
320 }
321
322 /* Appends a character to BUFFER.  */
323 void
324 output_add_character (buffer, c)
325      output_buffer *buffer;
326      int c;
327 {
328   if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
329     output_add_newline (buffer);
330   obstack_1grow (&buffer->obstack, c);
331   ++buffer->line_length;
332 }
333
334 /* Adds a space to BUFFER.  */
335 void
336 output_add_space (buffer)
337      output_buffer *buffer;
338 {
339   if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
340     {
341       output_add_newline (buffer);
342       return;
343     }
344   obstack_1grow (&buffer->obstack, ' ');
345   ++buffer->line_length;
346 }
347
348 /* Add the stringified version of an integer to BUFFER.  */
349 void
350 output_add_integer (buffer, i)
351      output_buffer *buffer;
352      HOST_WIDE_INT i;
353 {
354   /* This must be large enough to hold any printed integer or
355      floating-point value.  */
356   static char digit_buffer[128];
357
358   sprintf (digit_buffer, HOST_WIDE_INT_PRINT_DEC, i);
359   output_add_string (buffer, digit_buffer);
360 }
361
362 /* Append a string deliminated by START and END to BUFFER.  No wrapping is
363    done.  The caller must ensure that it is safe to do so.  */
364
365 void
366 output_append (buffer, start, end)
367      output_buffer *buffer;
368      const char *start;
369      const char *end;
370 {
371   int n;
372
373   /* Emit prefix and skip whitespace if we're starting a new line.  */
374   if (buffer->line_length == 0)
375     {
376       output_emit_prefix (buffer);
377       while (start != end && *start == ' ')
378         ++start;
379     }
380   n = end - start;
381   obstack_grow (&buffer->obstack, start, n);
382   buffer->line_length += n;
383 }
384
385 /* Wrap a STRing into BUFFER.  */
386
387 void
388 output_add_string (buffer, str)
389      output_buffer *buffer;
390      const char *str;
391 {
392   const char *p = str;
393
394   if (!output_is_line_wrapping (buffer))
395     output_append (buffer, str, str + strlen (str));
396   else while (*str)
397     {
398       while (*p && *p != ' ' && *p != '\n')
399         ++p;
400       
401       if (p - str < output_space_left (buffer))
402         output_append (buffer, str, p);
403       else
404         {
405           output_add_newline (buffer);
406           output_append (buffer, str, p);
407         }
408       
409       while (*p && *p == '\n')
410         {
411           output_add_newline (buffer);
412           ++p;
413         }
414
415       str = p++;
416     }
417 }
418
419 /* Flush the content of BUFFER onto FILE and reinitialize BUFFER.  */
420
421 void
422 output_flush_on (buffer, file)
423      output_buffer *buffer;
424      FILE *file;
425 {
426   const char *text = output_finish (buffer);
427   fputs (text, file);
428   output_clear (buffer);
429 }
430
431 /* Format MESSAGE into BUFFER.  */
432 void
433 output_format (buffer, msg)
434      output_buffer *buffer;
435      const char *msg;
436 {
437   for (buffer->cursor = msg; *buffer->cursor; ++buffer->cursor)
438     {
439       /* Ignore text.  */
440       if (*buffer->cursor != '%')
441         {
442           output_add_character (buffer, *buffer->cursor);
443           continue;
444         }
445
446       /* We got a '%'.  Let's see what happens.  */
447       ++buffer->cursor;
448
449       /* Let's handle the traditional cases.  */
450       if (*buffer->cursor == 's')
451         output_add_string (buffer, va_arg (buffer->format_args, const char *));
452       else if (*buffer->cursor == 'd')
453         output_add_integer (buffer, va_arg (buffer->format_args, int));
454       else if (*buffer->cursor == '%')
455         /* It was a '%%'.  Just output a '%'.  */
456         output_add_character (buffer, '%');
457       else if (lang_printer)
458         (*lang_printer) (buffer);
459       else
460         {
461           /* Hmmm.  The front-end failed to install a format translator
462              but called us with an unrecognized format.  Sorry.  */
463           abort();
464         }
465     }
466   output_finish (buffer);
467 }
468
469 static char *
470 vbuild_message_string (msgid, ap)
471      const char *msgid;
472      va_list ap;
473 {
474   char *str;
475
476   vasprintf (&str, msgid, ap);
477   return str;
478 }
479
480 /*  Return a malloc'd string containing MSGID formatted a la
481     printf.  The caller is reponsible for freeing the memory.  */
482
483 static char *
484 build_message_string VPARAMS ((const char *msgid, ...))
485 {
486 #ifndef ANSI_PROTOTYPES
487   const char *msgid;
488 #endif
489   va_list ap;
490   char *str;
491
492   VA_START (ap, msgid);
493
494 #ifndef ANSI_PROTOTYPES
495   msgid = va_arg (ap, const char *);
496 #endif
497
498   str = vbuild_message_string (msgid, ap);
499
500   va_end (ap);
501
502   return str;
503 }
504
505
506 /* Return a malloc'd string describing a location.  The caller is
507    responsible for freeing the memory.  */
508
509 static char *
510 build_location_prefix (file, line, warn)
511      const char *file;
512      int line;
513      int warn;
514 {
515   if (file)
516     {
517       if (warn)
518         return build_message_string ("%s:%d: warning: ", file, line);
519       else
520         return build_message_string ("%s:%d: ", file, line);
521     }
522   else
523     {
524       if (warn)
525         return build_message_string ("%s: warning: ", progname);
526       else
527         return build_message_string ("%s: ", progname);
528     }
529 }
530
531 /* Format a MESSAGE into BUFFER.  Automatically wrap lines.  */
532
533 static void
534 output_notice (buffer, msgid)
535      output_buffer *buffer;
536      const char *msgid;
537 {
538   char *message = vbuild_message_string (msgid, buffer->format_args);
539
540   output_add_string (buffer, message);
541   free (message);
542 }
543
544
545 /* Format a message into BUFFER a la printf.  */
546
547 void
548 output_printf VPARAMS ((struct output_buffer *buffer, const char *msgid, ...))
549 {
550 #ifndef ANSI_PROTOTYPES
551   struct output_buffer *buffer;
552   const char *msgid;
553 #endif
554   va_list ap;
555
556   VA_START (ap, msgid);
557
558 #ifndef ANSI_PROTOTYPES
559   buffer = va_arg (ap, struct output_buffer *);
560   msgid = va_arg (ap, const char *);
561 #endif
562
563   va_copy (buffer->format_args, ap);
564   output_notice (buffer, msgid);
565   va_end (buffer->format_args);
566 }
567
568
569 /* Format a MESSAGE into FILE.  Do line wrapping, starting new lines
570    with PREFIX.  */
571
572 static void
573 line_wrapper_printf VPARAMS ((FILE *file, const char *msgid, ...))
574 {
575 #ifndef ANSI_PROTOTYPES
576   FILE *file;
577   const char *msgid;
578 #endif
579   output_buffer buffer;
580   
581   init_output_buffer (&buffer, NULL, diagnostic_message_length_per_line);
582   VA_START (buffer.format_args, msgid);
583
584 #ifndef ANSI_PROTOTYPES
585   file = va_arg (buffer.format_args, FILE *);
586   msgid = va_arg (buffer.format_args, const char *);
587 #endif  
588
589   output_notice (&buffer, msgid);
590   output_flush_on (&buffer, file);
591
592   va_end (buffer.format_args);
593 }
594
595
596 static void
597 vline_wrapper_message_with_location (file, line, warn, msgid, ap)
598      const char *file;
599      int line;
600      int warn;
601      const char *msgid;
602      va_list ap;
603 {
604   output_buffer buffer;
605   
606   init_output_buffer (&buffer, build_location_prefix (file, line, warn),
607                       diagnostic_message_length_per_line);
608   va_copy (buffer.format_args, ap);
609   output_notice (&buffer, msgid);
610   output_flush_on (&buffer, stderr);
611
612   output_destroy_prefix (&buffer);
613   fputc ('\n', stderr);
614 }
615
616
617 /* Print the message MSGID in FILE.  */
618
619 static void
620 vnotice (file, msgid, ap)
621      FILE *file;
622      const char *msgid;
623      va_list ap;
624 {
625   vfprintf (file, _(msgid), ap);
626 }
627
628 /* Print MSGID on stderr.  */
629
630 static void
631 notice VPARAMS ((const char *msgid, ...))
632 {
633 #ifndef ANSI_PROTOTYPES
634   char *msgid;
635 #endif
636   va_list ap;
637
638   VA_START (ap, msgid);
639
640 #ifndef ANSI_PROTOTYPES
641   msgid = va_arg (ap, char *);
642 #endif
643
644   vnotice (stderr, msgid, ap);
645   va_end (ap);
646 }
647
648 /* Report FILE and LINE (or program name), and optionally just WARN.  */
649
650 static void
651 report_file_and_line (file, line, warn)
652      const char *file;
653      int line;
654      int warn;
655 {
656   if (file)
657     fprintf (stderr, "%s:%d: ", file, line);
658   else
659     fprintf (stderr, "%s: ", progname);
660
661   if (warn)
662     notice ("warning: ");
663 }
664
665 /* Print a message relevant to line LINE of file FILE.  */
666
667 static void
668 v_message_with_file_and_line (file, line, warn, msgid, ap)
669      const char *file;
670      int line;
671      int warn;
672      const char *msgid;
673      va_list ap;
674 {
675   report_file_and_line (file, line, warn);
676   vnotice (stderr, msgid, ap);
677   fputc ('\n', stderr);
678 }
679
680 /* Print a message relevant to the given DECL.  */
681
682 static void
683 v_message_with_decl (decl, warn, msgid, ap)
684      tree decl;
685      int warn;
686      const char *msgid;
687      va_list ap;
688 {
689   const char *p;
690   output_buffer buffer;
691
692   if (doing_line_wrapping ())
693     {
694       init_output_buffer
695         (&buffer, build_location_prefix
696          (DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl), warn),
697          diagnostic_message_length_per_line);
698     }
699   else
700     report_file_and_line (DECL_SOURCE_FILE (decl),
701                           DECL_SOURCE_LINE (decl), warn);
702
703   /* Do magic to get around lack of varargs support for insertion
704      of arguments into existing list.  We know that the decl is first;
705      we ass_u_me that it will be printed with "%s".  */
706
707   for (p = _(msgid); *p; ++p)
708     {
709       if (*p == '%')
710         {
711           if (*(p + 1) == '%')
712             ++p;
713           else if (*(p + 1) != 's')
714             abort ();
715           else
716             break;
717         }
718     }
719
720   if (p > _(msgid))                     /* Print the left-hand substring.  */
721     {
722       if (doing_line_wrapping ())
723         output_printf (&buffer, "%.*s", (int)(p - _(msgid)), _(msgid));
724       else
725         fprintf (stderr, "%.*s", (int)(p - _(msgid)), _(msgid));
726     }
727
728   if (*p == '%')                /* Print the name.  */
729     {
730       const char *n = (DECL_NAME (decl)
731                  ? (*decl_printable_name) (decl, 2)
732                  : _("((anonymous))"));
733       if (doing_line_wrapping ())
734         output_add_string (&buffer, n);
735       else
736         fputs (n, stderr);
737       while (*p)
738         {
739           ++p;
740           if (ISALPHA (*(p - 1) & 0xFF))
741             break;
742         }
743     }
744
745   if (*p)                       /* Print the rest of the message.  */
746     {
747       if (doing_line_wrapping ())
748         {
749           va_copy (buffer.format_args, ap);
750           output_notice (&buffer, p);
751           va_copy (ap, buffer.format_args);
752         }
753       else
754         vfprintf (stderr, p, ap);
755     }
756
757   if (doing_line_wrapping())
758     {
759       output_flush_on (&buffer, stderr);
760       output_destroy_prefix (&buffer);
761     }
762   
763   fputc ('\n', stderr);
764 }
765
766 /* Figure file and line of the given INSN.  */
767
768 static void
769 file_and_line_for_asm (insn, pfile, pline)
770      rtx insn;
771      const char **pfile;
772      int *pline;
773 {
774   rtx body = PATTERN (insn);
775   rtx asmop;
776
777   /* Find the (or one of the) ASM_OPERANDS in the insn.  */
778   if (GET_CODE (body) == SET && GET_CODE (SET_SRC (body)) == ASM_OPERANDS)
779     asmop = SET_SRC (body);
780   else if (GET_CODE (body) == ASM_OPERANDS)
781     asmop = body;
782   else if (GET_CODE (body) == PARALLEL
783            && GET_CODE (XVECEXP (body, 0, 0)) == SET)
784     asmop = SET_SRC (XVECEXP (body, 0, 0));
785   else if (GET_CODE (body) == PARALLEL
786            && GET_CODE (XVECEXP (body, 0, 0)) == ASM_OPERANDS)
787     asmop = XVECEXP (body, 0, 0);
788   else
789     asmop = NULL;
790
791   if (asmop)
792     {
793       *pfile = ASM_OPERANDS_SOURCE_FILE (asmop);
794       *pline = ASM_OPERANDS_SOURCE_LINE (asmop);
795     }
796   else
797     {
798       *pfile = input_filename;
799       *pline = lineno;
800     }
801 }
802
803 /* Report an error at line LINE of file FILE.  */
804
805 static void
806 v_error_with_file_and_line (file, line, msgid, ap)
807      const char *file;
808      int line;
809      const char *msgid;
810      va_list ap;
811 {
812   count_error (0);
813   report_error_function (file);
814   if (doing_line_wrapping ())
815     vline_wrapper_message_with_location (file, line, 0, msgid, ap);
816   else
817     v_message_with_file_and_line (file, line, 0, msgid, ap);
818 }
819
820 /* Report an error at the declaration DECL.
821    MSGID is a format string which uses %s to substitute the declaration
822    name; subsequent substitutions are a la printf.  */
823
824 static void
825 v_error_with_decl (decl, msgid, ap)
826      tree decl;
827      const char *msgid;
828      va_list ap;
829 {
830   count_error (0);
831   report_error_function (DECL_SOURCE_FILE (decl));
832   v_message_with_decl (decl, 0, msgid, ap);
833 }
834
835
836 /* Report an error at the line number of the insn INSN.
837    This is used only when INSN is an `asm' with operands,
838    and each ASM_OPERANDS records its own source file and line.  */
839
840 static void
841 v_error_for_asm (insn, msgid, ap)
842      rtx insn;
843      const char *msgid;
844      va_list ap;
845 {
846   const char *file;
847   int line;
848
849   count_error (0);
850   file_and_line_for_asm (insn, &file, &line);
851   report_error_function (file);
852   v_message_with_file_and_line (file, line, 0, msgid, ap);
853 }
854
855
856 /* Report an error at the current line number.  */
857
858 void
859 verror (msgid, ap)
860      const char *msgid;
861      va_list ap;
862 {
863   v_error_with_file_and_line (input_filename, lineno, msgid, ap);
864 }
865
866
867 /* Report a fatal error at the current line number.  Allow a front end to
868    intercept the message.  */
869
870 static void (*fatal_function) PARAMS ((const char *, va_list));
871
872 static void
873 vfatal (msgid, ap)
874      const char *msgid;
875      va_list ap;
876 {
877    if (fatal_function != 0)
878      (*fatal_function) (_(msgid), ap);
879
880   verror (msgid, ap);
881   exit (FATAL_EXIT_CODE);
882 }
883
884 /* Report a warning at line LINE of file FILE.  */
885
886 static void
887 v_warning_with_file_and_line (file, line, msgid, ap)
888      const char *file;
889      int line;
890      const char *msgid;
891      va_list ap;
892 {
893   if (count_error (1))
894     {
895       report_error_function (file);
896       if (doing_line_wrapping ())
897         vline_wrapper_message_with_location (file, line, 1, msgid, ap);
898       else
899         v_message_with_file_and_line (file, line, 1, msgid, ap);
900     }
901 }
902
903
904 /* Report a warning at the declaration DECL.
905    MSGID is a format string which uses %s to substitute the declaration
906    name; subsequent substitutions are a la printf.  */
907
908 static void
909 v_warning_with_decl (decl, msgid, ap)
910      tree decl;
911      const char *msgid;
912      va_list ap;
913 {
914   if (count_error (1))
915     {
916       report_error_function (DECL_SOURCE_FILE (decl));
917       v_message_with_decl (decl, 1, msgid, ap);
918     }
919 }
920
921
922 /* Report a warning at the line number of the insn INSN.
923    This is used only when INSN is an `asm' with operands,
924    and each ASM_OPERANDS records its own source file and line.  */
925
926 static void
927 v_warning_for_asm (insn, msgid, ap)
928      rtx insn;
929      const char *msgid;
930      va_list ap;
931 {
932   if (count_error (1))
933     {
934       const char *file;
935       int line;
936
937       file_and_line_for_asm (insn, &file, &line);
938       report_error_function (file);
939       v_message_with_file_and_line (file, line, 1, msgid, ap);
940     }
941 }
942
943
944 /* Report a warning at the current line number.  */
945
946 void
947 vwarning (msgid, ap)
948      const char *msgid;
949      va_list ap;
950 {
951   v_warning_with_file_and_line (input_filename, lineno, msgid, ap);
952 }
953
954 /* These functions issue either warnings or errors depending on
955    -pedantic-errors.  */
956
957 void
958 vpedwarn (msgid, ap)
959      const char *msgid;
960      va_list ap;
961 {
962   if (flag_pedantic_errors)
963     verror (msgid, ap);
964   else
965     vwarning (msgid, ap);
966 }
967
968
969 static void
970 v_pedwarn_with_decl (decl, msgid, ap)
971      tree decl;
972      const char *msgid;
973      va_list ap;
974 {
975   /* We don't want -pedantic-errors to cause the compilation to fail from
976      "errors" in system header files.  Sometimes fixincludes can't fix what's
977      broken (eg: unsigned char bitfields - fixing it may change the alignment
978      which will cause programs to mysteriously fail because the C library
979      or kernel uses the original layout).  There's no point in issuing a
980      warning either, it's just unnecessary noise.  */
981
982   if (! DECL_IN_SYSTEM_HEADER (decl))
983     {
984       if (flag_pedantic_errors)
985         v_error_with_decl (decl, msgid, ap);
986       else
987         v_warning_with_decl (decl, msgid, ap);
988     }
989 }
990
991
992 static void
993 v_pedwarn_with_file_and_line (file, line, msgid, ap)
994      const char *file;
995      int line;
996      const char *msgid;
997      va_list ap;
998 {
999   if (flag_pedantic_errors)
1000     v_error_with_file_and_line (file, line, msgid, ap);
1001   else
1002     v_warning_with_file_and_line (file, line, msgid, ap);
1003 }
1004
1005
1006 /* Apologize for not implementing some feature.  */
1007
1008 static void
1009 vsorry (msgid, ap)
1010      const char *msgid;
1011      va_list ap;
1012 {
1013   sorrycount++;
1014   if (input_filename)
1015     fprintf (stderr, "%s:%d: ", input_filename, lineno);
1016   else
1017     fprintf (stderr, "%s: ", progname);
1018   notice ("sorry, not implemented: ");
1019   vnotice (stderr, msgid, ap);
1020   fputc ('\n', stderr);
1021 }
1022
1023 \f
1024 /* Count an error or warning.  Return 1 if the message should be printed.  */
1025
1026 int
1027 count_error (warningp)
1028      int warningp;
1029 {
1030   if (warningp && inhibit_warnings)
1031     return 0;
1032
1033   if (warningp && !warnings_are_errors)
1034     warningcount++;
1035   else
1036     {
1037       static int warning_message = 0;
1038
1039       if (warningp && !warning_message)
1040         {
1041           notice ("%s: warnings being treated as errors\n", progname);
1042           warning_message = 1;
1043         }
1044       errorcount++;
1045     }
1046
1047   return 1;
1048 }
1049
1050 /* Print a diagnistic MSGID on FILE.  */
1051 void
1052 fnotice VPARAMS ((FILE *file, const char *msgid, ...))
1053 {
1054 #ifndef ANSI_PROTOTYPES
1055   FILE *file;
1056   const char *msgid;
1057 #endif
1058   va_list ap;
1059
1060   VA_START (ap, msgid);
1061
1062 #ifndef ANSI_PROTOTYPES
1063   file = va_arg (ap, FILE *);
1064   msgid = va_arg (ap, const char *);
1065 #endif
1066
1067   vnotice (file, msgid, ap);
1068   va_end (ap);
1069 }
1070
1071
1072 /* Print a fatal error message.  NAME is the text.
1073    Also include a system error message based on `errno'.  */
1074
1075 void
1076 pfatal_with_name (name)
1077   const char *name;
1078 {
1079   fprintf (stderr, "%s: ", progname);
1080   perror (name);
1081   exit (FATAL_EXIT_CODE);
1082 }
1083
1084 void
1085 fatal_io_error (name)
1086   const char *name;
1087 {
1088   notice ("%s: %s: I/O error\n", progname, name);
1089   exit (FATAL_EXIT_CODE);
1090 }
1091
1092 /* Issue a pedantic warning MSGID.  */
1093 void
1094 pedwarn VPARAMS ((const char *msgid, ...))
1095 {
1096 #ifndef ANSI_PROTOTYPES
1097   const char *msgid;
1098 #endif
1099   va_list ap;
1100
1101   VA_START (ap, msgid);
1102
1103 #ifndef ANSI_PROTOTYPES
1104   msgid = va_arg (ap, const char *);
1105 #endif
1106
1107   vpedwarn (msgid, ap);
1108   va_end (ap);
1109 }
1110
1111 /* Issue a pedantic waring about DECL.  */
1112 void
1113 pedwarn_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1114 {
1115 #ifndef ANSI_PROTOTYPES
1116   tree decl;
1117   const char *msgid;
1118 #endif
1119   va_list ap;
1120
1121   VA_START (ap, msgid);
1122
1123 #ifndef ANSI_PROTOTYPES
1124   decl = va_arg (ap, tree);
1125   msgid = va_arg (ap, const char *);
1126 #endif
1127
1128   v_pedwarn_with_decl (decl, msgid, ap);
1129   va_end (ap);
1130 }
1131
1132 /* Same as above but within the context FILE and LINE. */
1133 void
1134 pedwarn_with_file_and_line VPARAMS ((const char *file, int line,
1135                                      const char *msgid, ...))
1136 {
1137 #ifndef ANSI_PROTOTYPES
1138   const char *file;
1139   int line;
1140   const char *msgid;
1141 #endif
1142   va_list ap;
1143
1144   VA_START (ap, msgid);
1145
1146 #ifndef ANSI_PROTOTYPES
1147   file = va_arg (ap, const char *);
1148   line = va_arg (ap, int);
1149   msgid = va_arg (ap, const char *);
1150 #endif
1151
1152   v_pedwarn_with_file_and_line (file, line, msgid, ap);
1153   va_end (ap);
1154 }
1155
1156 /* Just apologize with MSGID.  */
1157 void
1158 sorry VPARAMS ((const char *msgid, ...))
1159 {
1160 #ifndef ANSI_PROTOTYPES
1161   const char *msgid;
1162 #endif
1163   va_list ap;
1164
1165   VA_START (ap, msgid);
1166
1167 #ifndef ANSI_PROTOTYPES
1168   msgid = va_arg (ap, const char *);
1169 #endif
1170
1171   vsorry (msgid, ap);
1172   va_end (ap);
1173 }
1174
1175 /* Called when the start of a function definition is parsed,
1176    this function prints on stderr the name of the function.  */
1177
1178 void
1179 announce_function (decl)
1180      tree decl;
1181 {
1182   if (! quiet_flag)
1183     {
1184       if (rtl_dump_and_exit)
1185         fprintf (stderr, "%s ", IDENTIFIER_POINTER (DECL_NAME (decl)));
1186       else
1187         {
1188           if (doing_line_wrapping ())
1189             line_wrapper_printf
1190               (stderr, " %s", (*decl_printable_name) (decl, 2));
1191           else
1192             fprintf (stderr, " %s", (*decl_printable_name) (decl, 2));
1193         }
1194       fflush (stderr);
1195       need_error_newline = 1;
1196       last_error_function = current_function_decl;
1197     }
1198 }
1199
1200 /* The default function to print out name of current function that caused
1201    an error.  */
1202
1203 void
1204 default_print_error_function (file)
1205   const char *file;
1206 {
1207   if (last_error_function != current_function_decl)
1208     {
1209       char *prefix = NULL;
1210       output_buffer buffer;
1211       
1212       if (file)
1213         prefix = build_message_string ("%s: ", file);
1214
1215       if (doing_line_wrapping ())
1216         init_output_buffer
1217           (&buffer, prefix, diagnostic_message_length_per_line);
1218       else
1219         {
1220           if (file)
1221             fprintf (stderr, "%s: ", file);
1222         }
1223       
1224       if (current_function_decl == NULL)
1225         {
1226           if (doing_line_wrapping ())
1227             output_printf (&buffer, "At top level:\n");
1228           else
1229             notice ("At top level:\n");
1230         }
1231       else
1232         {
1233           if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
1234             {
1235               if (doing_line_wrapping ())
1236                 output_printf
1237                   (&buffer, "In method `%s':\n",
1238                    (*decl_printable_name) (current_function_decl, 2));
1239               else
1240                 notice ("In method `%s':\n",
1241                         (*decl_printable_name) (current_function_decl, 2));
1242             }
1243           else
1244             {
1245               if (doing_line_wrapping ())
1246                 output_printf
1247                   (&buffer, "In function `%s':\n",
1248                    (*decl_printable_name) (current_function_decl, 2));
1249               else
1250                 notice ("In function `%s':\n",
1251                         (*decl_printable_name) (current_function_decl, 2));
1252             }
1253         }
1254
1255       last_error_function = current_function_decl;
1256
1257       if (doing_line_wrapping ())
1258         output_flush_on (&buffer, stderr);
1259       
1260       free ((char*) prefix);
1261     }
1262 }
1263
1264 /* Prints out, if necessary, the name of the current function
1265   that caused an error.  Called from all error and warning functions.
1266   We ignore the FILE parameter, as it cannot be relied upon.  */
1267
1268 void
1269 report_error_function (file)
1270   const char *file ATTRIBUTE_UNUSED;
1271 {
1272   struct file_stack *p;
1273
1274   if (need_error_newline)
1275     {
1276       fprintf (stderr, "\n");
1277       need_error_newline = 0;
1278     }
1279
1280   if (input_file_stack && input_file_stack->next != 0
1281       && input_file_stack_tick != last_error_tick)
1282     {
1283       for (p = input_file_stack->next; p; p = p->next)
1284         if (p == input_file_stack->next)
1285           notice ("In file included from %s:%d", p->name, p->line);
1286         else
1287           notice (",\n                 from %s:%d", p->name, p->line);
1288       fprintf (stderr, ":\n");
1289       last_error_tick = input_file_stack_tick;
1290     }
1291
1292   (*print_error_function) (input_filename);
1293 }
1294
1295 void
1296 error_with_file_and_line VPARAMS ((const char *file, int line,
1297                                    const char *msgid, ...))
1298 {
1299 #ifndef ANSI_PROTOTYPES
1300   const char *file;
1301   int line;
1302   const char *msgid;
1303 #endif
1304   va_list ap;
1305
1306   VA_START (ap, msgid);
1307
1308 #ifndef ANSI_PROTOTYPES
1309   file = va_arg (ap, const char *);
1310   line = va_arg (ap, int);
1311   msgid = va_arg (ap, const char *);
1312 #endif
1313
1314   v_error_with_file_and_line (file, line, msgid, ap);
1315   va_end (ap);
1316 }
1317
1318 void
1319 error_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1320 {
1321 #ifndef ANSI_PROTOTYPES
1322   tree decl;
1323   const char *msgid;
1324 #endif
1325   va_list ap;
1326
1327   VA_START (ap, msgid);
1328
1329 #ifndef ANSI_PROTOTYPES
1330   decl = va_arg (ap, tree);
1331   msgid = va_arg (ap, const char *);
1332 #endif
1333
1334   v_error_with_decl (decl, msgid, ap);
1335   va_end (ap);
1336 }
1337
1338 void
1339 error_for_asm VPARAMS ((rtx insn, const char *msgid, ...))
1340 {
1341 #ifndef ANSI_PROTOTYPES
1342   rtx insn;
1343   const char *msgid;
1344 #endif
1345   va_list ap;
1346
1347   VA_START (ap, msgid);
1348
1349 #ifndef ANSI_PROTOTYPES
1350   insn = va_arg (ap, rtx);
1351   msgid = va_arg (ap, const char *);
1352 #endif
1353
1354   v_error_for_asm (insn, msgid, ap);
1355   va_end (ap);
1356 }
1357
1358 void
1359 error VPARAMS ((const char *msgid, ...))
1360 {
1361 #ifndef ANSI_PROTOTYPES
1362   const char *msgid;
1363 #endif
1364   va_list ap;
1365
1366   VA_START (ap, msgid);
1367
1368 #ifndef ANSI_PROTOTYPES
1369   msgid = va_arg (ap, const char *);
1370 #endif
1371
1372   verror (msgid, ap);
1373   va_end (ap);
1374 }
1375
1376 /* Set the function to call when a fatal error occurs.  */
1377
1378 void
1379 set_fatal_function (f)
1380      void (*f) PARAMS ((const char *, va_list));
1381 {
1382   fatal_function = f;
1383 }
1384
1385 void
1386 fatal VPARAMS ((const char *msgid, ...))
1387 {
1388 #ifndef ANSI_PROTOTYPES
1389   const char *msgid;
1390 #endif
1391   va_list ap;
1392
1393   VA_START (ap, msgid);
1394
1395 #ifndef ANSI_PROTOTYPES
1396   msgid = va_arg (ap, const char *);
1397 #endif
1398
1399   vfatal (msgid, ap);
1400   va_end (ap);
1401 }
1402
1403 void
1404 _fatal_insn (msgid, insn, file, line, function)
1405      const char *msgid;
1406      rtx insn;
1407      const char *file;
1408      int line;
1409      const char *function;
1410 {
1411   error ("%s", msgid);
1412   debug_rtx (insn);
1413   fancy_abort (file, line, function);
1414 }
1415
1416 void
1417 _fatal_insn_not_found (insn, file, line, function)
1418      rtx insn;
1419      const char *file;
1420      int line;
1421      const char *function;
1422 {
1423   if (INSN_CODE (insn) < 0)
1424     _fatal_insn ("Unrecognizable insn:", insn, file, line, function);
1425   else
1426     _fatal_insn ("Insn does not satisfy its constraints:",
1427                 insn, file, line, function);
1428 }
1429
1430 void
1431 warning_with_file_and_line VPARAMS ((const char *file, int line,
1432                                      const char *msgid, ...))
1433 {
1434 #ifndef ANSI_PROTOTYPES
1435   const char *file;
1436   int line;
1437   const char *msgid;
1438 #endif
1439   va_list ap;
1440
1441   VA_START (ap, msgid);
1442
1443 #ifndef ANSI_PROTOTYPES
1444   file = va_arg (ap, const char *);
1445   line = va_arg (ap, int);
1446   msgid = va_arg (ap, const char *);
1447 #endif
1448
1449   v_warning_with_file_and_line (file, line, msgid, ap);
1450   va_end (ap);
1451 }
1452
1453 void
1454 warning_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1455 {
1456 #ifndef ANSI_PROTOTYPES
1457   tree decl;
1458   const char *msgid;
1459 #endif
1460   va_list ap;
1461
1462   VA_START (ap, msgid);
1463
1464 #ifndef ANSI_PROTOTYPES
1465   decl = va_arg (ap, tree);
1466   msgid = va_arg (ap, const char *);
1467 #endif
1468
1469   v_warning_with_decl (decl, msgid, ap);
1470   va_end (ap);
1471 }
1472
1473 void
1474 warning_for_asm VPARAMS ((rtx insn, const char *msgid, ...))
1475 {
1476 #ifndef ANSI_PROTOTYPES
1477   rtx insn;
1478   const char *msgid;
1479 #endif
1480   va_list ap;
1481
1482   VA_START (ap, msgid);
1483
1484 #ifndef ANSI_PROTOTYPES
1485   insn = va_arg (ap, rtx);
1486   msgid = va_arg (ap, const char *);
1487 #endif
1488
1489   v_warning_for_asm (insn, msgid, ap);
1490   va_end (ap);
1491 }
1492
1493 void
1494 warning VPARAMS ((const char *msgid, ...))
1495 {
1496 #ifndef ANSI_PROTOTYPES
1497   const char *msgid;
1498 #endif
1499   va_list ap;
1500
1501   VA_START (ap, msgid);
1502
1503 #ifndef ANSI_PROTOTYPES
1504   msgid = va_arg (ap, const char *);
1505 #endif
1506
1507   vwarning (msgid, ap);
1508   va_end (ap);
1509 }
1510