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