OSDN Git Service

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