Make sure THEN block has any insns at before testing for indirect jump
[official-gcc.git] / gcc / diagnostic.c
blobfbaaec6f1db3e2fe7b84e2034abf29ce5dc3ea3f
1 /* Language-independent diagnostic subroutines for the GNU C compiler
2 Copyright (C) 1999, 2000 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* This file implements the language independent aspect of diagnostic
24 message module. */
26 #include "config.h"
27 #undef FLOAT /* This is for hpux. They should change hpux. */
28 #undef FFS /* Some systems define this in param.h. */
29 #include "system.h"
31 #include "tree.h"
32 #include "rtl.h"
33 #include "tm_p.h"
34 #include "flags.h"
35 #include "input.h"
36 #include "insn-attr.h"
37 #include "insn-codes.h"
38 #include "insn-config.h"
39 #include "toplev.h"
40 #include "intl.h"
41 #include "diagnostic.h"
43 #define obstack_chunk_alloc xmalloc
44 #define obstack_chunk_free free
46 #define output_formatted_integer(BUFFER, FORMAT, INTEGER) \
47 do { \
48 sprintf (digit_buffer, FORMAT, INTEGER); \
49 output_add_string (BUFFER, digit_buffer); \
50 } while (0)
52 #define output_text_length(BUFFER) (BUFFER)->line_length
53 #define is_starting_newline(BUFFER) (output_text_length (BUFFER) == 0)
54 #define output_prefix(BUFFER) (BUFFER)->state.prefix
55 #define line_wrap_cutoff(BUFFER) (BUFFER)->state.maximum_length
56 #define ideal_line_wrap_cutoff(BUFFER) (BUFFER)->state.ideal_maximum_length
57 #define prefix_was_emitted_for(BUFFER) (BUFFER)->state.emitted_prefix_p
58 #define prefixing_policy(BUFFER) (BUFFER)->state.prefixing_rule
59 #define output_buffer_ptr_to_format_args(BUFFER) (BUFFER)->state.format_args
61 #define diagnostic_args output_buffer_ptr_to_format_args (diagnostic_buffer)
62 #define diagnostic_msg output_buffer_text_cursor (diagnostic_buffer)
64 /* Prototypes. */
65 static void finish_diagnostic PARAMS ((void));
66 static void output_do_verbatim PARAMS ((output_buffer *,
67 const char *, va_list *));
68 static void output_to_stream PARAMS ((output_buffer *, FILE *));
69 static void output_format PARAMS ((output_buffer *));
71 static char *vbuild_message_string PARAMS ((const char *, va_list));
72 static char *build_message_string PARAMS ((const char *, ...))
73 ATTRIBUTE_PRINTF_1;
74 static char *context_as_prefix PARAMS ((const char *, int, int));
75 static void output_do_printf PARAMS ((output_buffer *, const char *));
76 static void format_with_decl PARAMS ((output_buffer *, tree));
77 static void file_and_line_for_asm PARAMS ((rtx, const char **, int *));
78 static void diagnostic_for_asm PARAMS ((rtx, const char *, va_list *, int));
79 static void diagnostic_for_decl PARAMS ((tree, const char *, va_list *, int));
80 static void vnotice PARAMS ((FILE *, const char *, va_list));
81 static void set_real_maximum_length PARAMS ((output_buffer *));
83 static void output_unsigned_decimal PARAMS ((output_buffer *, unsigned int));
84 static void output_long_decimal PARAMS ((output_buffer *, long int));
85 static void output_long_unsigned_decimal PARAMS ((output_buffer *,
86 long unsigned int));
87 static void output_octal PARAMS ((output_buffer *, unsigned int));
88 static void output_long_octal PARAMS ((output_buffer *, unsigned long int));
89 static void output_hexadecimal PARAMS ((output_buffer *, unsigned int));
90 static void output_long_hexadecimal PARAMS ((output_buffer *,
91 unsigned long int));
92 static void output_append_r PARAMS ((output_buffer *, const char *, int));
93 static void wrap_text PARAMS ((output_buffer *, const char *, const char *));
94 static void maybe_wrap_text PARAMS ((output_buffer *, const char *,
95 const char *));
96 static void clear_text_info PARAMS ((output_buffer *));
97 static void clear_diagnostic_info PARAMS ((output_buffer *));
99 static void error_recursion PARAMS ((void)) ATTRIBUTE_NORETURN;
100 static const char *trim_filename PARAMS ((const char *));
102 extern int rtl_dump_and_exit;
103 extern int inhibit_warnings;
104 extern int warnings_are_errors;
105 extern int warningcount;
106 extern int errorcount;
108 /* Front-end specific tree formatter, if non-NULL. */
109 printer_fn lang_printer = NULL;
111 /* This must be large enough to hold any printed integer or
112 floating-point value. */
113 static char digit_buffer[128];
115 /* An output_buffer surrogate for stderr. */
116 static output_buffer global_output_buffer;
117 output_buffer *diagnostic_buffer = &global_output_buffer;
119 static int need_error_newline;
121 /* Function of last error message;
122 more generally, function such that if next error message is in it
123 then we don't have to mention the function name. */
124 static tree last_error_function = NULL;
126 /* Used to detect when input_file_stack has changed since last described. */
127 static int last_error_tick;
129 /* Called by report_error_function to print out function name.
130 Default may be overridden by language front-ends. */
132 void (*print_error_function) PARAMS ((const char *)) =
133 default_print_error_function;
135 /* Maximum characters per line in automatic line wrapping mode.
136 Zero means don't wrap lines. */
138 int diagnostic_message_length_per_line;
140 /* Used to control every diagnostic message formatting. Front-ends should
141 call set_message_prefixing_rule to set up their politics. */
142 static int current_prefixing_rule;
144 /* Prevent recursion into the error handler. */
145 static int diagnostic_lock;
148 /* Initialize the diagnostic message outputting machinery. */
150 void
151 initialize_diagnostics ()
153 /* By default, we don't line-wrap messages. */
154 diagnostic_message_length_per_line = 0;
155 set_message_prefixing_rule (DIAGNOSTICS_SHOW_PREFIX_ONCE);
157 /* Proceed to actual initialization. */
158 default_initialize_buffer (diagnostic_buffer);
161 void
162 set_message_prefixing_rule (rule)
163 int rule;
165 current_prefixing_rule = rule;
168 /* Returns true if BUFFER is in line-wrappind mode. */
171 output_is_line_wrapping (buffer)
172 output_buffer *buffer;
174 return ideal_line_wrap_cutoff (buffer) > 0;
177 /* Return BUFFER's prefix. */
179 const char *
180 output_get_prefix (buffer)
181 const output_buffer *buffer;
183 return output_prefix (buffer);
186 /* Subroutine of output_set_maximum_length. Set up BUFFER's
187 internal maximum characters per line. */
189 static void
190 set_real_maximum_length (buffer)
191 output_buffer *buffer;
193 /* If we're told not to wrap lines then do the obvious thing. */
194 if (! output_is_line_wrapping (buffer))
195 line_wrap_cutoff (buffer) = ideal_line_wrap_cutoff (buffer);
196 else
198 int prefix_length =
199 output_prefix (buffer) ? strlen (output_prefix (buffer)) : 0;
200 /* If the prefix is ridiculously too long, output at least
201 32 characters. */
202 if (ideal_line_wrap_cutoff (buffer) - prefix_length < 32)
203 line_wrap_cutoff (buffer) = ideal_line_wrap_cutoff (buffer) + 32;
204 else
205 line_wrap_cutoff (buffer) = ideal_line_wrap_cutoff (buffer);
209 /* Sets the number of maximum characters per line BUFFER can output
210 in line-wrapping mode. A LENGTH value 0 suppresses line-wrapping. */
212 void
213 output_set_maximum_length (buffer, length)
214 output_buffer *buffer;
215 int length;
217 ideal_line_wrap_cutoff (buffer) = length;
218 set_real_maximum_length (buffer);
221 /* Sets BUFFER's PREFIX. */
223 void
224 output_set_prefix (buffer, prefix)
225 output_buffer *buffer;
226 const char *prefix;
228 output_prefix (buffer) = prefix;
229 set_real_maximum_length (buffer);
230 prefix_was_emitted_for (buffer) = 0;
233 /* Free BUFFER's prefix, a previously malloc'd string. */
235 void
236 output_destroy_prefix (buffer)
237 output_buffer *buffer;
239 if (output_prefix (buffer) != NULL)
241 free ((char *) output_prefix (buffer));
242 output_prefix (buffer) = NULL;
246 /* Zero out any text output so far in BUFFER. */
248 static void
249 clear_text_info (buffer)
250 output_buffer *buffer;
252 obstack_free (&buffer->obstack, obstack_base (&buffer->obstack));
253 output_text_length (buffer) = 0;
256 /* Zero out any diagnostic data used so far by BUFFER. */
258 static void
259 clear_diagnostic_info (buffer)
260 output_buffer *buffer;
262 output_buffer_text_cursor (buffer) = NULL;
263 output_buffer_ptr_to_format_args (buffer) = NULL;
264 prefix_was_emitted_for (buffer) = 0;
267 /* Construct an output BUFFER with PREFIX and of MAXIMUM_LENGTH
268 characters per line. */
270 void
271 init_output_buffer (buffer, prefix, maximum_length)
272 output_buffer *buffer;
273 const char *prefix;
274 int maximum_length;
276 obstack_init (&buffer->obstack);
277 ideal_line_wrap_cutoff (buffer) = maximum_length;
278 prefixing_policy (buffer) = current_prefixing_rule;
279 output_set_prefix (buffer, prefix);
280 output_text_length (buffer) = 0;
281 clear_diagnostic_info (buffer);
284 /* Initialize BUFFER with a NULL prefix and current diagnostic message
285 length cutoff. */
286 void
287 default_initialize_buffer (buffer)
288 output_buffer *buffer;
290 init_output_buffer (buffer, NULL, diagnostic_message_length_per_line);
293 /* Recompute diagnostic_buffer's attributes to reflect any change
294 in diagnostic formatting global options. */
296 void
297 reshape_diagnostic_buffer ()
299 ideal_line_wrap_cutoff (diagnostic_buffer) =
300 diagnostic_message_length_per_line;
301 prefixing_policy (diagnostic_buffer) = current_prefixing_rule;
302 set_real_maximum_length (diagnostic_buffer);
305 /* Reinitialize BUFFER. */
306 void
307 output_clear (buffer)
308 output_buffer *buffer;
310 clear_text_info (buffer);
311 clear_diagnostic_info (buffer);
314 /* Finishes to construct a NULL-terminated character string representing
315 the BUFFERed message. */
317 const char *
318 output_finish (buffer)
319 output_buffer *buffer;
321 obstack_1grow (&buffer->obstack, '\0');
322 return (const char *) obstack_finish (&buffer->obstack);
325 /* Return the amount of characters BUFFER can accept to
326 make a full line. */
329 output_space_left (buffer)
330 const output_buffer *buffer;
332 return line_wrap_cutoff (buffer) - output_text_length (buffer);
335 /* Write out BUFFER's prefix. */
337 void
338 output_emit_prefix (buffer)
339 output_buffer *buffer;
341 if (output_prefix (buffer) != NULL)
343 switch (prefixing_policy (buffer))
345 default:
346 case DIAGNOSTICS_SHOW_PREFIX_NEVER:
347 break;
349 case DIAGNOSTICS_SHOW_PREFIX_ONCE:
350 if (prefix_was_emitted_for (buffer))
351 break;
352 /* Else fall through. */
354 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
356 int prefix_length = strlen (output_prefix (buffer));
357 output_append_r (buffer, output_prefix (buffer), prefix_length);
358 prefix_was_emitted_for (buffer) = 1;
360 break;
365 /* Have BUFFER start a new line. */
367 void
368 output_add_newline (buffer)
369 output_buffer *buffer;
371 obstack_1grow (&buffer->obstack, '\n');
372 output_text_length (buffer) = 0;
375 /* Appends a character to BUFFER. */
377 void
378 output_add_character (buffer, c)
379 output_buffer *buffer;
380 int c;
382 if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
383 output_add_newline (buffer);
384 obstack_1grow (&buffer->obstack, c);
385 ++output_text_length (buffer);
388 /* Adds a space to BUFFER. */
390 void
391 output_add_space (buffer)
392 output_buffer *buffer;
394 if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
396 output_add_newline (buffer);
397 return;
399 obstack_1grow (&buffer->obstack, ' ');
400 ++output_text_length (buffer);
403 /* These functions format an INTEGER into BUFFER as suggested by their
404 names. */
406 void
407 output_decimal (buffer, i)
408 output_buffer *buffer;
409 int i;
411 output_formatted_integer (buffer, "%d", i);
414 static void
415 output_long_decimal (buffer, i)
416 output_buffer *buffer;
417 long int i;
419 output_formatted_integer (buffer, "%ld", i);
422 static void
423 output_unsigned_decimal (buffer, i)
424 output_buffer *buffer;
425 unsigned int i;
427 output_formatted_integer (buffer, "%u", i);
430 static void
431 output_long_unsigned_decimal (buffer, i)
432 output_buffer *buffer;
433 long unsigned int i;
435 output_formatted_integer (buffer, "%lu", i);
438 static void
439 output_octal (buffer, i)
440 output_buffer *buffer;
441 unsigned int i;
443 output_formatted_integer (buffer, "%o", i);
446 static void
447 output_long_octal (buffer, i)
448 output_buffer *buffer;
449 unsigned long int i;
451 output_formatted_integer (buffer, "%lo", i);
454 static void
455 output_hexadecimal (buffer, i)
456 output_buffer *buffer;
457 unsigned int i;
459 output_formatted_integer (buffer, "%x", i);
462 static void
463 output_long_hexadecimal (buffer, i)
464 output_buffer *buffer;
465 unsigned long int i;
467 output_formatted_integer (buffer, "%lx", i);
470 /* Append to BUFFER a string specified by its STARTING character
471 and LENGTH. */
473 static void
474 output_append_r (buffer, start, length)
475 output_buffer *buffer;
476 const char *start;
477 int length;
479 obstack_grow (&buffer->obstack, start, length);
480 output_text_length (buffer) += length;
483 /* Append a string deliminated by START and END to BUFFER. No wrapping is
484 done. However, if beginning a new line then emit output_prefix (BUFFER)
485 and skip any leading whitespace if appropriate. The caller must ensure
486 that it is safe to do so. */
488 void
489 output_append (buffer, start, end)
490 output_buffer *buffer;
491 const char *start;
492 const char *end;
494 /* Emit prefix and skip whitespace if we're starting a new line. */
495 if (is_starting_newline (buffer))
497 output_emit_prefix (buffer);
498 if (output_is_line_wrapping (buffer))
499 while (start != end && *start == ' ')
500 ++start;
502 output_append_r (buffer, start, end - start);
505 /* Wrap a text delimited by START and END into BUFFER. */
507 static void
508 wrap_text (buffer, start, end)
509 output_buffer *buffer;
510 const char *start;
511 const char *end;
513 while (start != end)
515 /* Dump anything bodered by whitespaces. */
517 const char *p = start;
518 while (p != end && *p != ' ' && *p != '\n')
519 ++p;
520 if (p - start >= output_space_left (buffer))
521 output_add_newline (buffer);
522 output_append (buffer, start, p);
523 start = p;
526 if (start != end && *start == ' ')
528 output_add_space (buffer);
529 ++start;
531 if (start != end && *start == '\n')
533 output_add_newline (buffer);
534 ++start;
539 /* Same as wrap_text but wrap text only when in line-wrapping mode. */
540 static void
541 maybe_wrap_text (buffer, start, end)
542 output_buffer *buffer;
543 const char *start;
544 const char *end;
546 if (output_is_line_wrapping (buffer))
547 wrap_text (buffer, start, end);
548 else
549 output_append (buffer, start, end);
553 /* Append a STRING to BUFFER; the STRING maybe be line-wrapped if in
554 appropriate mode. */
556 void
557 output_add_string (buffer, str)
558 output_buffer *buffer;
559 const char *str;
561 maybe_wrap_text (buffer, str, str + (str ? strlen (str) : 0));
564 /* Flush the content of BUFFER onto FILE and reinitialize BUFFER. */
566 static void
567 output_to_stream (buffer, file)
568 output_buffer *buffer;
569 FILE *file;
571 const char *text = output_finish (buffer);
572 fputs (text, file);
573 clear_text_info (buffer);
576 /* Format a message pointed to by output_buffer_text_cursor (BUFFER) using
577 output_buffer_format_args (BUFFER) as appropriate. The following format
578 specifiers are recognized as being language independent:
579 %d, %i: (signed) integer in base ten.
580 %u: unsigned integer in base ten.
581 %o: unsigned integer in base eight.
582 %x: unsigned integer in base sixteen.
583 %ld, %li, %lo, %lu, %lx: long versions of the above.
584 %c: character.
585 %s: string.
586 %%: `%'.
587 %*.s: a substring the length of which is specified by an integer. */
589 static void
590 output_format (buffer)
591 output_buffer *buffer;
593 for (; *output_buffer_text_cursor (buffer);
594 ++output_buffer_text_cursor (buffer))
596 int long_integer = 0;
598 /* Ignore text. */
600 const char *p = output_buffer_text_cursor (buffer);
601 while (*p && *p != '%')
602 ++p;
603 maybe_wrap_text (buffer, output_buffer_text_cursor (buffer), p);
604 output_buffer_text_cursor (buffer) = p;
607 if (!*output_buffer_text_cursor (buffer))
608 break;
610 /* We got a '%'. Let's see what happens. Record whether we're
611 parsing a long integer format specifier. */
612 if (*++output_buffer_text_cursor (buffer) == 'l')
614 long_integer = 1;
615 ++output_buffer_text_cursor (buffer);
618 /* Handle %c, %d, %i, %ld, %li, %lo, %lu, %lx, %o, %s, %u,
619 %x, %.*s; %%. And nothing else. Front-ends should install
620 printers to grok language specific format specifiers. */
621 switch (*output_buffer_text_cursor (buffer))
623 case 'c':
624 output_add_character
625 (buffer, va_arg (output_buffer_format_args (buffer), int));
626 break;
628 case 'd':
629 case 'i':
630 if (long_integer)
631 output_long_decimal
632 (buffer, va_arg (output_buffer_format_args (buffer), long int));
633 else
634 output_decimal
635 (buffer, va_arg (output_buffer_format_args (buffer), int));
636 break;
638 case 'o':
639 if (long_integer)
640 output_long_octal (buffer,
641 va_arg (output_buffer_format_args (buffer),
642 unsigned long int));
643 else
644 output_octal (buffer,
645 va_arg (output_buffer_format_args (buffer),
646 unsigned int));
647 break;
649 case 's':
650 output_add_string (buffer,
651 va_arg (output_buffer_format_args (buffer),
652 const char *));
653 break;
655 case 'u':
656 if (long_integer)
657 output_long_unsigned_decimal
658 (buffer, va_arg (output_buffer_format_args (buffer),
659 long unsigned int));
660 else
661 output_unsigned_decimal
662 (buffer, va_arg (output_buffer_format_args (buffer),
663 unsigned int));
665 case 'x':
666 if (long_integer)
667 output_long_hexadecimal
668 (buffer, va_arg (output_buffer_format_args (buffer),
669 unsigned long int));
670 else
671 output_hexadecimal
672 (buffer, va_arg (output_buffer_format_args (buffer),
673 unsigned int));
674 break;
676 case '%':
677 output_add_character (buffer, '%');
678 break;
680 case '.':
682 int n;
683 const char *s;
684 /* We handle no precision specifier but `%.*s'. */
685 if (*++output_buffer_text_cursor (buffer) != '*')
686 abort ();
687 else if (*++output_buffer_text_cursor (buffer) != 's')
688 abort();
689 n = va_arg (output_buffer_format_args (buffer), int);
690 s = va_arg (output_buffer_format_args (buffer), const char *);
691 output_append (buffer, s, s + n);
693 break;
695 default:
696 if (! lang_printer || !(*lang_printer) (buffer))
698 /* Hmmm. The front-end failed to install a format translator
699 but called us with an unrecognized format. Sorry. */
700 abort ();
706 static char *
707 vbuild_message_string (msgid, ap)
708 const char *msgid;
709 va_list ap;
711 char *str;
713 vasprintf (&str, msgid, ap);
714 return str;
717 /* Return a malloc'd string containing MSGID formatted a la
718 printf. The caller is reponsible for freeing the memory. */
720 static char *
721 build_message_string VPARAMS ((const char *msgid, ...))
723 #ifndef ANSI_PROTOTYPES
724 const char *msgid;
725 #endif
726 va_list ap;
727 char *str;
729 VA_START (ap, msgid);
731 #ifndef ANSI_PROTOTYPES
732 msgid = va_arg (ap, const char *);
733 #endif
735 str = vbuild_message_string (msgid, ap);
737 va_end (ap);
739 return str;
743 /* Return a malloc'd string describing a location. The caller is
744 responsible for freeing the memory. */
746 static char *
747 context_as_prefix (file, line, warn)
748 const char *file;
749 int line;
750 int warn;
752 if (file)
754 if (warn)
755 return build_message_string ("%s:%d: warning: ", file, line);
756 else
757 return build_message_string ("%s:%d: ", file, line);
759 else
761 if (warn)
762 return build_message_string ("%s: warning: ", progname);
763 else
764 return build_message_string ("%s: ", progname);
768 /* Format a MESSAGE into BUFFER. Automatically wrap lines. */
770 static void
771 output_do_printf (buffer, msgid)
772 output_buffer *buffer;
773 const char *msgid;
775 char *message = vbuild_message_string (msgid,
776 output_buffer_format_args (buffer));
778 output_add_string (buffer, message);
779 free (message);
783 /* Format a message into BUFFER a la printf. */
785 void
786 output_printf VPARAMS ((struct output_buffer *buffer, const char *msgid, ...))
788 #ifndef ANSI_PROTOTYPES
789 struct output_buffer *buffer;
790 const char *msgid;
791 #endif
792 va_list ap;
793 va_list *old_args;
795 VA_START (ap, msgid);
796 #ifndef ANSI_PROTOTYPES
797 buffer = va_arg (ap, output_buffer *);
798 msgid = va_arg (ap, const char *);
799 #endif
800 old_args = output_buffer_ptr_to_format_args (buffer);
801 output_buffer_ptr_to_format_args (buffer) = &ap;
802 output_do_printf (buffer, msgid);
803 output_buffer_ptr_to_format_args (buffer) = old_args;
804 va_end (ap);
807 /* Print the message MSGID in FILE. */
809 static void
810 vnotice (file, msgid, ap)
811 FILE *file;
812 const char *msgid;
813 va_list ap;
815 vfprintf (file, _(msgid), ap);
818 /* Print a message relevant to the given DECL. */
820 static void
821 format_with_decl (buffer, decl)
822 output_buffer *buffer;
823 tree decl;
825 const char *p;
827 /* Do magic to get around lack of varargs support for insertion
828 of arguments into existing list. We know that the decl is first;
829 we ass_u_me that it will be printed with "%s". */
830 for (p = output_buffer_text_cursor (buffer); *p; ++p)
832 if (*p == '%')
834 if (*(p + 1) == '%')
835 ++p;
836 else if (*(p + 1) != 's')
837 abort ();
838 else
839 break;
843 /* Print the left-hand substring. */
844 maybe_wrap_text (buffer, output_buffer_text_cursor (buffer), p);
846 if (*p == '%') /* Print the name. */
848 const char *n = (DECL_NAME (decl)
849 ? (*decl_printable_name) (decl, 2)
850 : _("((anonymous))"));
851 output_add_string (buffer, n);
852 while (*p)
854 ++p;
855 if (ISALPHA (*(p - 1) & 0xFF))
856 break;
860 if (*p) /* Print the rest of the message. */
862 output_buffer_text_cursor (buffer) = p;
863 output_format (buffer);
867 /* Figure file and line of the given INSN. */
869 static void
870 file_and_line_for_asm (insn, pfile, pline)
871 rtx insn;
872 const char **pfile;
873 int *pline;
875 rtx body = PATTERN (insn);
876 rtx asmop;
878 /* Find the (or one of the) ASM_OPERANDS in the insn. */
879 if (GET_CODE (body) == SET && GET_CODE (SET_SRC (body)) == ASM_OPERANDS)
880 asmop = SET_SRC (body);
881 else if (GET_CODE (body) == ASM_OPERANDS)
882 asmop = body;
883 else if (GET_CODE (body) == PARALLEL
884 && GET_CODE (XVECEXP (body, 0, 0)) == SET)
885 asmop = SET_SRC (XVECEXP (body, 0, 0));
886 else if (GET_CODE (body) == PARALLEL
887 && GET_CODE (XVECEXP (body, 0, 0)) == ASM_OPERANDS)
888 asmop = XVECEXP (body, 0, 0);
889 else
890 asmop = NULL;
892 if (asmop)
894 *pfile = ASM_OPERANDS_SOURCE_FILE (asmop);
895 *pline = ASM_OPERANDS_SOURCE_LINE (asmop);
897 else
899 *pfile = input_filename;
900 *pline = lineno;
904 /* Report a diagnostic MESSAGE (an errror or a WARNING) at the line number
905 of the insn INSN. This is used only when INSN is an `asm' with operands,
906 and each ASM_OPERANDS records its own source file and line. */
908 static void
909 diagnostic_for_asm (insn, msg, args_ptr, warn)
910 rtx insn;
911 const char *msg;
912 va_list *args_ptr;
913 int warn;
915 const char *file;
916 int line;
918 file_and_line_for_asm (insn, &file, &line);
919 report_diagnostic (msg, args_ptr, file, line, warn);
922 /* Report a diagnostic MESSAGE at the declaration DECL.
923 MSG is a format string which uses %s to substitute the declaration
924 name; subsequent substitutions are a la output_format. */
926 static void
927 diagnostic_for_decl (decl, msg, args_ptr, warn)
928 tree decl;
929 const char *msg;
930 va_list *args_ptr;
931 int warn;
933 output_state os;
935 if (diagnostic_lock++)
936 error_recursion ();
938 if (count_error (warn))
940 os = diagnostic_buffer->state;
941 report_error_function (DECL_SOURCE_FILE (decl));
942 output_set_prefix
943 (diagnostic_buffer, context_as_prefix
944 (DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl), warn));
945 output_buffer_ptr_to_format_args (diagnostic_buffer) = args_ptr;
946 output_buffer_text_cursor (diagnostic_buffer) = msg;
947 format_with_decl (diagnostic_buffer, decl);
948 finish_diagnostic ();
949 output_destroy_prefix (diagnostic_buffer);
951 diagnostic_buffer->state = os;
953 diagnostic_lock--;
957 /* Count an error or warning. Return 1 if the message should be printed. */
960 count_error (warningp)
961 int warningp;
963 if (warningp && inhibit_warnings)
964 return 0;
966 if (warningp && !warnings_are_errors)
967 warningcount++;
968 else
970 static int warning_message = 0;
972 if (warningp && !warning_message)
974 verbatim ("%s: warnings being treated as errors\n", progname);
975 warning_message = 1;
977 errorcount++;
980 return 1;
983 /* Print a diagnistic MSGID on FILE. */
985 void
986 fnotice VPARAMS ((FILE *file, const char *msgid, ...))
988 #ifndef ANSI_PROTOTYPES
989 FILE *file;
990 const char *msgid;
991 #endif
992 va_list ap;
994 VA_START (ap, msgid);
996 #ifndef ANSI_PROTOTYPES
997 file = va_arg (ap, FILE *);
998 msgid = va_arg (ap, const char *);
999 #endif
1001 vnotice (file, msgid, ap);
1002 va_end (ap);
1006 /* Print a fatal error message. NAME is the text.
1007 Also include a system error message based on `errno'. */
1009 void
1010 pfatal_with_name (name)
1011 const char *name;
1013 fprintf (stderr, "%s: ", progname);
1014 perror (name);
1015 exit (FATAL_EXIT_CODE);
1018 void
1019 fatal_io_error (name)
1020 const char *name;
1022 verbatim ("%s: %s: I/O error\n", progname, name);
1023 exit (FATAL_EXIT_CODE);
1026 /* Issue a pedantic warning MSGID. */
1028 void
1029 pedwarn VPARAMS ((const char *msgid, ...))
1031 #ifndef ANSI_PROTOTYPES
1032 const char *msgid;
1033 #endif
1034 va_list ap;
1036 VA_START (ap, msgid);
1038 #ifndef ANSI_PROTOTYPES
1039 msgid = va_arg (ap, const char *);
1040 #endif
1042 report_diagnostic (msgid, &ap, input_filename, lineno,
1043 !flag_pedantic_errors);
1044 va_end (ap);
1047 /* Issue a pedantic waring about DECL. */
1049 void
1050 pedwarn_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1052 #ifndef ANSI_PROTOTYPES
1053 tree decl;
1054 const char *msgid;
1055 #endif
1056 va_list ap;
1058 VA_START (ap, msgid);
1060 #ifndef ANSI_PROTOTYPES
1061 decl = va_arg (ap, tree);
1062 msgid = va_arg (ap, const char *);
1063 #endif
1064 /* We don't want -pedantic-errors to cause the compilation to fail from
1065 "errors" in system header files. Sometimes fixincludes can't fix what's
1066 broken (eg: unsigned char bitfields - fixing it may change the alignment
1067 which will cause programs to mysteriously fail because the C library
1068 or kernel uses the original layout). There's no point in issuing a
1069 warning either, it's just unnecessary noise. */
1070 if (!DECL_IN_SYSTEM_HEADER (decl))
1071 diagnostic_for_decl (decl, msgid, &ap, !flag_pedantic_errors);
1072 va_end (ap);
1075 /* Same as above but within the context FILE and LINE. */
1077 void
1078 pedwarn_with_file_and_line VPARAMS ((const char *file, int line,
1079 const char *msgid, ...))
1081 #ifndef ANSI_PROTOTYPES
1082 const char *file;
1083 int line;
1084 const char *msgid;
1085 #endif
1086 va_list ap;
1088 VA_START (ap, msgid);
1090 #ifndef ANSI_PROTOTYPES
1091 file = va_arg (ap, const char *);
1092 line = va_arg (ap, int);
1093 msgid = va_arg (ap, const char *);
1094 #endif
1096 report_diagnostic (msgid, &ap, file, line, !flag_pedantic_errors);
1097 va_end (ap);
1100 /* Just apologize with MSGID. */
1102 void
1103 sorry VPARAMS ((const char *msgid, ...))
1105 #ifndef ANSI_PROTOTYPES
1106 const char *msgid;
1107 #endif
1108 va_list ap;
1109 output_state os;
1111 os = diagnostic_buffer->state;
1112 VA_START (ap, msgid);
1114 #ifndef ANSI_PROTOTYPES
1115 msgid = va_arg (ap, const char *);
1116 #endif
1117 ++sorrycount;
1118 output_set_prefix
1119 (diagnostic_buffer, context_as_prefix (input_filename, lineno, 0));
1120 output_printf (diagnostic_buffer, "sorry, not implemented: ");
1121 output_buffer_ptr_to_format_args (diagnostic_buffer) = &ap;
1122 output_buffer_text_cursor (diagnostic_buffer) = msgid;
1123 output_format (diagnostic_buffer);
1124 finish_diagnostic ();
1125 diagnostic_buffer->state = os;
1126 va_end (ap);
1129 /* Called when the start of a function definition is parsed,
1130 this function prints on stderr the name of the function. */
1132 void
1133 announce_function (decl)
1134 tree decl;
1136 if (! quiet_flag)
1138 if (rtl_dump_and_exit)
1139 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl)));
1140 else
1141 verbatim (" %s", (*decl_printable_name) (decl, 2));
1142 fflush (stderr);
1143 need_error_newline = 1;
1144 last_error_function = current_function_decl;
1148 /* The default function to print out name of current function that caused
1149 an error. */
1151 void
1152 default_print_error_function (file)
1153 const char *file;
1155 if (last_error_function != current_function_decl)
1157 char *prefix = file ? build_message_string ("%s: ", file) : NULL;
1158 output_state os;
1160 os = diagnostic_buffer->state;
1161 output_set_prefix (diagnostic_buffer, prefix);
1163 if (current_function_decl == NULL)
1165 output_add_string (diagnostic_buffer, "At top level:");
1166 output_add_newline (diagnostic_buffer);
1168 else
1170 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
1171 output_printf
1172 (diagnostic_buffer, "In method `%s':\n",
1173 (*decl_printable_name) (current_function_decl, 2));
1174 else
1175 output_printf
1176 (diagnostic_buffer, "In function `%s':\n",
1177 (*decl_printable_name) (current_function_decl, 2));
1180 last_error_function = current_function_decl;
1181 output_to_stream (diagnostic_buffer, stderr);
1182 diagnostic_buffer->state = os;
1183 free ((char*) prefix);
1187 /* Prints out, if necessary, the name of the current function
1188 that caused an error. Called from all error and warning functions.
1189 We ignore the FILE parameter, as it cannot be relied upon. */
1191 void
1192 report_error_function (file)
1193 const char *file ATTRIBUTE_UNUSED;
1195 struct file_stack *p;
1197 if (need_error_newline)
1199 verbatim ("\n");
1200 need_error_newline = 0;
1203 if (input_file_stack && input_file_stack->next != 0
1204 && input_file_stack_tick != last_error_tick)
1206 for (p = input_file_stack->next; p; p = p->next)
1207 if (p == input_file_stack->next)
1208 verbatim ("In file included from %s:%d", p->name, p->line);
1209 else
1210 verbatim (",\n from %s:%d", p->name, p->line);
1211 verbatim (":\n");
1212 last_error_tick = input_file_stack_tick;
1215 (*print_error_function) (input_filename);
1218 void
1219 error_with_file_and_line VPARAMS ((const char *file, int line,
1220 const char *msgid, ...))
1222 #ifndef ANSI_PROTOTYPES
1223 const char *file;
1224 int line;
1225 const char *msgid;
1226 #endif
1227 va_list ap;
1229 VA_START (ap, msgid);
1231 #ifndef ANSI_PROTOTYPES
1232 file = va_arg (ap, const char *);
1233 line = va_arg (ap, int);
1234 msgid = va_arg (ap, const char *);
1235 #endif
1237 report_diagnostic (msgid, &ap, file, line, /* warn = */ 0);
1238 va_end (ap);
1241 void
1242 error_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1244 #ifndef ANSI_PROTOTYPES
1245 tree decl;
1246 const char *msgid;
1247 #endif
1248 va_list ap;
1250 VA_START (ap, msgid);
1252 #ifndef ANSI_PROTOTYPES
1253 decl = va_arg (ap, tree);
1254 msgid = va_arg (ap, const char *);
1255 #endif
1257 diagnostic_for_decl (decl, msgid, &ap, /* warn = */ 0);
1258 va_end (ap);
1261 void
1262 error_for_asm VPARAMS ((rtx insn, const char *msgid, ...))
1264 #ifndef ANSI_PROTOTYPES
1265 rtx insn;
1266 const char *msgid;
1267 #endif
1268 va_list ap;
1270 VA_START (ap, msgid);
1272 #ifndef ANSI_PROTOTYPES
1273 insn = va_arg (ap, rtx);
1274 msgid = va_arg (ap, const char *);
1275 #endif
1277 diagnostic_for_asm (insn, msgid, &ap, /* warn = */ 0);
1278 va_end (ap);
1281 void
1282 error VPARAMS ((const char *msgid, ...))
1284 #ifndef ANSI_PROTOTYPES
1285 const char *msgid;
1286 #endif
1287 va_list ap;
1289 VA_START (ap, msgid);
1291 #ifndef ANSI_PROTOTYPES
1292 msgid = va_arg (ap, const char *);
1293 #endif
1295 report_diagnostic (msgid, &ap, input_filename, lineno, /* warn = */ 0);
1296 va_end (ap);
1299 /* Report a fatal error at the current line number. Allow a front end to
1300 intercept the message. */
1302 static void (*fatal_function) PARAMS((const char *, va_list *));
1304 /* Set the function to call when a fatal error occurs. */
1306 void
1307 set_fatal_function (f)
1308 void (*f) PARAMS ((const char *, va_list *));
1310 fatal_function = f;
1313 void
1314 fatal VPARAMS ((const char *msgid, ...))
1316 #ifndef ANSI_PROTOTYPES
1317 const char *msgid;
1318 #endif
1319 va_list ap;
1321 VA_START (ap, msgid);
1323 #ifndef ANSI_PROTOTYPES
1324 msgid = va_arg (ap, const char *);
1325 #endif
1327 if (fatal_function != 0)
1328 (*fatal_function) (_(msgid), &ap);
1330 report_diagnostic (msgid, &ap, input_filename, lineno, 0);
1331 va_end (ap);
1332 exit (FATAL_EXIT_CODE);
1335 void
1336 _fatal_insn (msgid, insn, file, line, function)
1337 const char *msgid;
1338 rtx insn;
1339 const char *file;
1340 int line;
1341 const char *function;
1343 error ("%s", msgid);
1344 debug_rtx (insn);
1345 fancy_abort (file, line, function);
1348 void
1349 _fatal_insn_not_found (insn, file, line, function)
1350 rtx insn;
1351 const char *file;
1352 int line;
1353 const char *function;
1355 if (INSN_CODE (insn) < 0)
1356 _fatal_insn ("Unrecognizable insn:", insn, file, line, function);
1357 else
1358 _fatal_insn ("Insn does not satisfy its constraints:",
1359 insn, file, line, function);
1362 void
1363 warning_with_file_and_line VPARAMS ((const char *file, int line,
1364 const char *msgid, ...))
1366 #ifndef ANSI_PROTOTYPES
1367 const char *file;
1368 int line;
1369 const char *msgid;
1370 #endif
1371 va_list ap;
1373 VA_START (ap, msgid);
1375 #ifndef ANSI_PROTOTYPES
1376 file = va_arg (ap, const char *);
1377 line = va_arg (ap, int);
1378 msgid = va_arg (ap, const char *);
1379 #endif
1381 report_diagnostic (msgid, &ap, file, line, /* warn = */ 1);
1382 va_end (ap);
1385 void
1386 warning_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1388 #ifndef ANSI_PROTOTYPES
1389 tree decl;
1390 const char *msgid;
1391 #endif
1392 va_list ap;
1394 VA_START (ap, msgid);
1396 #ifndef ANSI_PROTOTYPES
1397 decl = va_arg (ap, tree);
1398 msgid = va_arg (ap, const char *);
1399 #endif
1401 diagnostic_for_decl (decl, msgid, &ap, /* warn = */ 1);
1402 va_end (ap);
1405 void
1406 warning_for_asm VPARAMS ((rtx insn, const char *msgid, ...))
1408 #ifndef ANSI_PROTOTYPES
1409 rtx insn;
1410 const char *msgid;
1411 #endif
1412 va_list ap;
1414 VA_START (ap, msgid);
1416 #ifndef ANSI_PROTOTYPES
1417 insn = va_arg (ap, rtx);
1418 msgid = va_arg (ap, const char *);
1419 #endif
1421 diagnostic_for_asm (insn, msgid, &ap, /* warn = */ 1);
1422 va_end (ap);
1425 void
1426 warning VPARAMS ((const char *msgid, ...))
1428 #ifndef ANSI_PROTOTYPES
1429 const char *msgid;
1430 #endif
1431 va_list ap;
1433 VA_START (ap, msgid);
1435 #ifndef ANSI_PROTOTYPES
1436 msgid = va_arg (ap, const char *);
1437 #endif
1439 report_diagnostic (msgid, &ap, input_filename, lineno, /* warn = */ 1);
1440 va_end (ap);
1443 /* Flush diagnostic_buffer content on stderr. */
1444 static void
1445 finish_diagnostic ()
1447 output_to_stream (diagnostic_buffer, stderr);
1448 clear_diagnostic_info (diagnostic_buffer);
1449 fputc ('\n', stderr);
1450 fflush (stderr);
1453 /* Helper subroutine of output_verbatim and verbatim. Do the approriate
1454 settings needed by BUFFER for a verbatim formatting. */
1455 static void
1456 output_do_verbatim (buffer, msg, args_ptr)
1457 output_buffer *buffer;
1458 const char *msg;
1459 va_list *args_ptr;
1461 output_state os;
1463 os = buffer->state;
1464 output_prefix (buffer) = NULL;
1465 prefixing_policy (buffer) = DIAGNOSTICS_SHOW_PREFIX_NEVER;
1466 output_buffer_text_cursor (buffer) = msg;
1467 output_buffer_ptr_to_format_args (buffer) = args_ptr;
1468 output_set_maximum_length (buffer, 0);
1469 output_format (buffer);
1470 buffer->state = os;
1473 /* Output MESSAGE verbatim into BUFFER. */
1474 void
1475 output_verbatim VPARAMS ((output_buffer *buffer, const char *msg, ...))
1477 #ifndef ANSI_PROTOTYPES
1478 output_buffer *buffer;
1479 const char *msg;
1480 #endif
1481 va_list ap;
1483 VA_START (ap, msg);
1484 #ifndef ANSI_PROTOTYPES
1485 buffer = va_arg (ap, output_buffer *);
1486 msg = va_arg (ap, const char *);
1487 #endif
1488 output_do_verbatim (buffer, msg, &ap);
1489 va_end (ap);
1492 /* Same as above but use diagnostic_buffer. */
1493 void
1494 verbatim VPARAMS ((const char *msg, ...))
1496 #ifndef ANSI_PROTOTYPES
1497 const char *msg;
1498 #endif
1499 va_list ap;
1501 VA_START (ap, msg);
1502 #ifndef ANSI_PROTOTYPES
1503 msg = va_arg (ap, const char *);
1504 #endif
1505 output_do_verbatim (diagnostic_buffer, msg, &ap);
1506 output_to_stream (diagnostic_buffer, stderr);
1507 va_end (ap);
1510 /* Report a diagnostic MESSAGE (an error or a WARNING) involving
1511 entities in ARGUMENTS. FILE and LINE indicate where the diagnostic
1512 occurs. This function is *the* subroutine in terms of which front-ends
1513 should implement their specific diagnostic handling modules.
1514 The front-end independent format specifiers are exactly those described
1515 in the documentation of output_format. */
1516 void
1517 report_diagnostic (msg, args_ptr, file, line, warn)
1518 const char *msg;
1519 va_list *args_ptr;
1520 const char *file;
1521 int line;
1522 int warn;
1524 output_state os;
1526 if (diagnostic_lock++)
1527 error_recursion ();
1529 if (count_error (warn))
1531 os = diagnostic_buffer->state;
1532 diagnostic_msg = msg;
1533 diagnostic_args = args_ptr;
1534 report_error_function (file);
1535 output_set_prefix
1536 (diagnostic_buffer, context_as_prefix (file, line, warn));
1537 output_format (diagnostic_buffer);
1538 finish_diagnostic ();
1539 output_destroy_prefix (diagnostic_buffer);
1540 diagnostic_buffer->state = os;
1543 diagnostic_lock--;
1546 /* Inform the user that an error occurred while trying to report some
1547 other error. This indicates catastrophic internal inconsistencies,
1548 so give up now. But do try to flush out the previous error. */
1549 static void
1550 error_recursion ()
1552 if (diagnostic_lock < 3)
1553 finish_diagnostic ();
1555 fprintf (stderr,
1556 "Internal compiler error: Error reporting routines re-entered.\n\
1557 Please submit a full bug report.\n\
1558 See %s for instructions.\n", GCCBUGURL);
1560 exit (FATAL_EXIT_CODE);
1563 /* Given a partial pathname as input, return another pathname that
1564 shares no directory elements with the pathname of __FILE__. This
1565 is used by fancy_abort() to print `Internal compiler error in expr.c'
1566 instead of `Internal compiler error in ../../egcs/gcc/expr.c'. */
1567 static const char *
1568 trim_filename (name)
1569 const char *name;
1571 static const char this_file[] = __FILE__;
1572 const char *p = name, *q = this_file;
1574 while (*p == *q && *p != 0 && *q != 0) p++, q++;
1575 while (p > name && p[-1] != DIR_SEPARATOR
1576 #ifdef DIR_SEPARATOR_2
1577 && p[-1] != DIR_SEPARATOR_2
1578 #endif
1580 p--;
1582 return p;
1585 /* Report an internal compiler error in a friendly manner and without
1586 dumping core. */
1588 void
1589 fancy_abort (file, line, function)
1590 const char *file;
1591 int line;
1592 const char *function;
1594 fatal (
1595 "Internal compiler error in %s, at %s:%d\n\
1596 Please submit a full bug report.\n\
1597 See %s for instructions.",
1598 function, trim_filename (file), line, GCCBUGURL);