1 /* Language-independent diagnostic subroutines for the GNU C compiler
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
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)
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
27 #undef FLOAT /* This is for hpux. They should change hpux. */
28 #undef FFS /* Some systems define this in param.h. */
36 #include "insn-attr.h"
37 #include "insn-config.h"
40 #include "diagnostic.h"
42 #define obstack_chunk_alloc xmalloc
43 #define obstack_chunk_free free
45 #define output_formatted_integer(BUFFER, FORMAT, INTEGER) \
47 sprintf (digit_buffer, FORMAT, INTEGER); \
48 output_add_string (BUFFER, digit_buffer); \
51 #define output_text_length(BUFFER) (BUFFER)->line_length
52 #define is_starting_newline(BUFFER) (output_text_length (BUFFER) == 0)
53 #define output_prefix(BUFFER) (BUFFER)->state.prefix
54 #define line_wrap_cutoff(BUFFER) (BUFFER)->state.maximum_length
55 #define ideal_line_wrap_cutoff(BUFFER) (BUFFER)->state.ideal_maximum_length
56 #define prefix_was_emitted_for(BUFFER) (BUFFER)->state.emitted_prefix_p
57 #define prefixing_policy(BUFFER) (BUFFER)->state.prefixing_rule
58 #define output_buffer_ptr_to_format_args(BUFFER) (BUFFER)->state.format_args
60 #define diagnostic_args output_buffer_ptr_to_format_args (diagnostic_buffer)
61 #define diagnostic_msg output_buffer_text_cursor (diagnostic_buffer)
64 static void finish_diagnostic PARAMS ((void));
65 static void output_do_verbatim PARAMS ((output_buffer *,
66 const char *, va_list *));
67 static void output_buffer_to_stream PARAMS ((output_buffer *));
68 static void output_format PARAMS ((output_buffer *));
69 static void output_indent PARAMS ((output_buffer *));
71 static char *vbuild_message_string PARAMS ((const char *, va_list))
72 ATTRIBUTE_PRINTF (1, 0);
73 static char *build_message_string PARAMS ((const char *, ...))
75 static void output_do_printf PARAMS ((output_buffer *, const char *))
76 ATTRIBUTE_PRINTF (2, 0);
77 static void format_with_decl PARAMS ((output_buffer *, tree));
78 static void file_and_line_for_asm PARAMS ((rtx, const char **, int *));
79 static void diagnostic_for_asm PARAMS ((rtx, const char *, va_list *, int));
80 static void diagnostic_for_decl PARAMS ((tree, const char *, va_list *, int));
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 *,
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 *,
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 *,
96 static void clear_diagnostic_info PARAMS ((output_buffer *));
98 static void default_diagnostic_starter PARAMS ((output_buffer *,
99 diagnostic_context *));
100 static void default_diagnostic_finalizer PARAMS ((output_buffer *,
101 diagnostic_context *));
103 static void error_recursion PARAMS ((void)) ATTRIBUTE_NORETURN;
105 extern int rtl_dump_and_exit;
106 extern int inhibit_warnings;
107 extern int warnings_are_errors;
108 extern int warningcount;
109 extern int errorcount;
111 /* Front-end specific tree formatter, if non-NULL. */
112 printer_fn lang_printer = NULL;
114 /* This must be large enough to hold any printed integer or
115 floating-point value. */
116 static char digit_buffer[128];
118 /* An output_buffer surrogate for stderr. */
119 static output_buffer global_output_buffer;
120 output_buffer *diagnostic_buffer = &global_output_buffer;
122 /* Function of last error message;
123 more generally, function such that if next error message is in it
124 then we don't have to mention the function name. */
125 static tree last_error_function = NULL;
127 /* Used to detect when input_file_stack has changed since last described. */
128 static int last_error_tick;
130 /* Called by report_error_function to print out function name.
131 Default may be overridden by language front-ends. */
133 void (*print_error_function) PARAMS ((const char *)) =
134 default_print_error_function;
136 /* Hooks for language specific diagnostic messages pager and finalizer. */
137 diagnostic_starter_fn lang_diagnostic_starter;
138 diagnostic_finalizer_fn lang_diagnostic_finalizer;
140 /* Maximum characters per line in automatic line wrapping mode.
141 Zero means don't wrap lines. */
143 int diagnostic_message_length_per_line;
145 /* Used to control every diagnostic message formatting. Front-ends should
146 call set_message_prefixing_rule to set up their policies. */
147 static int current_prefixing_rule;
149 /* Prevent recursion into the error handler. */
150 static int diagnostic_lock;
153 /* Return truthvalue if current input file is different from the most recent
154 file involved in a diagnostic message. */
157 error_module_changed ()
159 return last_error_tick != input_file_stack_tick;
162 /* Remember current file as being the most recent file involved in a
163 diagnostic message. */
166 record_last_error_module ()
168 last_error_tick = input_file_stack_tick;
171 /* Same as error_module_changed, but for function. */
174 error_function_changed ()
176 return last_error_function != current_function_decl;
179 /* Same as record_last_error_module, but for function. */
182 record_last_error_function ()
184 last_error_function = current_function_decl;
187 /* Initialize the diagnostic message outputting machinery. */
190 initialize_diagnostics ()
192 /* By default, we don't line-wrap messages. */
193 diagnostic_message_length_per_line = 0;
194 set_message_prefixing_rule (DIAGNOSTICS_SHOW_PREFIX_ONCE);
196 /* Proceed to actual initialization. */
197 default_initialize_buffer (diagnostic_buffer);
199 lang_diagnostic_starter = default_diagnostic_starter;
200 lang_diagnostic_finalizer = default_diagnostic_finalizer;
204 set_message_prefixing_rule (rule)
207 current_prefixing_rule = rule;
210 /* Returns true if BUFFER is in line-wrappind mode. */
213 output_is_line_wrapping (buffer)
214 output_buffer *buffer;
216 return ideal_line_wrap_cutoff (buffer) > 0;
219 /* Return BUFFER's prefix. */
222 output_get_prefix (buffer)
223 const output_buffer *buffer;
225 return output_prefix (buffer);
228 /* Subroutine of output_set_maximum_length. Set up BUFFER's
229 internal maximum characters per line. */
232 set_real_maximum_length (buffer)
233 output_buffer *buffer;
235 /* If we're told not to wrap lines then do the obvious thing. In case
236 we'll emit prefix only once per diagnostic message, it is appropriate
237 not to increase unncessarily the line-length cut-off. */
238 if (! output_is_line_wrapping (buffer)
239 || prefixing_policy (buffer) == DIAGNOSTICS_SHOW_PREFIX_ONCE
240 || prefixing_policy (buffer) == DIAGNOSTICS_SHOW_PREFIX_NEVER)
241 line_wrap_cutoff (buffer) = ideal_line_wrap_cutoff (buffer);
245 output_prefix (buffer) ? strlen (output_prefix (buffer)) : 0;
246 /* If the prefix is ridiculously too long, output at least
248 if (ideal_line_wrap_cutoff (buffer) - prefix_length < 32)
249 line_wrap_cutoff (buffer) = ideal_line_wrap_cutoff (buffer) + 32;
251 line_wrap_cutoff (buffer) = ideal_line_wrap_cutoff (buffer);
255 /* Sets the number of maximum characters per line BUFFER can output
256 in line-wrapping mode. A LENGTH value 0 suppresses line-wrapping. */
259 output_set_maximum_length (buffer, length)
260 output_buffer *buffer;
263 ideal_line_wrap_cutoff (buffer) = length;
264 set_real_maximum_length (buffer);
267 /* Sets BUFFER's PREFIX. */
270 output_set_prefix (buffer, prefix)
271 output_buffer *buffer;
274 output_prefix (buffer) = prefix;
275 set_real_maximum_length (buffer);
276 prefix_was_emitted_for (buffer) = 0;
277 output_indentation (buffer) = 0;
280 /* Return a pointer to the last character emitted in the output
281 BUFFER area. A NULL pointer means no character available. */
283 output_last_position (buffer)
284 const output_buffer *buffer;
286 const char *p = NULL;
288 if (obstack_base (&buffer->obstack) != obstack_next_free (&buffer->obstack))
289 p = ((const char *) obstack_next_free (&buffer->obstack)) - 1;
293 /* Free BUFFER's prefix, a previously malloc'd string. */
296 output_destroy_prefix (buffer)
297 output_buffer *buffer;
299 if (output_prefix (buffer) != NULL)
301 free ((char *) output_prefix (buffer));
302 output_prefix (buffer) = NULL;
306 /* Zero out any text output so far in BUFFER. */
309 output_clear_message_text (buffer)
310 output_buffer *buffer;
312 obstack_free (&buffer->obstack, obstack_base (&buffer->obstack));
313 output_text_length (buffer) = 0;
316 /* Zero out any diagnostic data used so far by BUFFER. */
319 clear_diagnostic_info (buffer)
320 output_buffer *buffer;
322 output_buffer_text_cursor (buffer) = NULL;
323 output_buffer_ptr_to_format_args (buffer) = NULL;
324 prefix_was_emitted_for (buffer) = 0;
325 output_indentation (buffer) = 0;
328 /* Construct an output BUFFER with PREFIX and of MAXIMUM_LENGTH
329 characters per line. */
332 init_output_buffer (buffer, prefix, maximum_length)
333 output_buffer *buffer;
337 memset (buffer, 0, sizeof (output_buffer));
338 obstack_init (&buffer->obstack);
339 output_buffer_attached_stream (buffer) = stderr;
340 ideal_line_wrap_cutoff (buffer) = maximum_length;
341 prefixing_policy (buffer) = current_prefixing_rule;
342 output_set_prefix (buffer, prefix);
343 output_text_length (buffer) = 0;
344 clear_diagnostic_info (buffer);
347 /* Initialize BUFFER with a NULL prefix and current diagnostic message
351 default_initialize_buffer (buffer)
352 output_buffer *buffer;
354 init_output_buffer (buffer, NULL, diagnostic_message_length_per_line);
357 /* Recompute diagnostic_buffer's attributes to reflect any change
358 in diagnostic formatting global options. */
361 reshape_diagnostic_buffer ()
363 ideal_line_wrap_cutoff (diagnostic_buffer) =
364 diagnostic_message_length_per_line;
365 prefixing_policy (diagnostic_buffer) = current_prefixing_rule;
366 set_real_maximum_length (diagnostic_buffer);
369 /* Reinitialize BUFFER. */
372 output_clear (buffer)
373 output_buffer *buffer;
375 output_clear_message_text (buffer);
376 clear_diagnostic_info (buffer);
379 /* Finishes constructing a NULL-terminated character string representing
380 the BUFFERed message. */
383 output_finalize_message (buffer)
384 output_buffer *buffer;
386 obstack_1grow (&buffer->obstack, '\0');
387 return output_message_text (buffer);
391 flush_diagnostic_buffer ()
393 output_buffer_to_stream (diagnostic_buffer);
394 fflush (output_buffer_attached_stream (diagnostic_buffer));
397 /* Return the amount of characters BUFFER can accept to
401 output_space_left (buffer)
402 const output_buffer *buffer;
404 return line_wrap_cutoff (buffer) - output_text_length (buffer);
407 /* Write out BUFFER's prefix. */
410 output_emit_prefix (buffer)
411 output_buffer *buffer;
413 if (output_prefix (buffer) != NULL)
415 switch (prefixing_policy (buffer))
418 case DIAGNOSTICS_SHOW_PREFIX_NEVER:
421 case DIAGNOSTICS_SHOW_PREFIX_ONCE:
422 if (prefix_was_emitted_for (buffer))
424 output_indent (buffer);
427 output_indentation (buffer) += 3;
430 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
432 int prefix_length = strlen (output_prefix (buffer));
433 output_append_r (buffer, output_prefix (buffer), prefix_length);
434 prefix_was_emitted_for (buffer) = 1;
441 /* Have BUFFER start a new line. */
444 output_add_newline (buffer)
445 output_buffer *buffer;
447 obstack_1grow (&buffer->obstack, '\n');
448 output_text_length (buffer) = 0;
451 /* Appends a character to BUFFER. */
454 output_add_character (buffer, c)
455 output_buffer *buffer;
458 if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
459 output_add_newline (buffer);
460 obstack_1grow (&buffer->obstack, c);
461 ++output_text_length (buffer);
464 /* Adds a space to BUFFER. */
467 output_add_space (buffer)
468 output_buffer *buffer;
470 if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
472 output_add_newline (buffer);
475 obstack_1grow (&buffer->obstack, ' ');
476 ++output_text_length (buffer);
479 /* These functions format an INTEGER into BUFFER as suggested by their
483 output_decimal (buffer, i)
484 output_buffer *buffer;
487 output_formatted_integer (buffer, "%d", i);
491 output_long_decimal (buffer, i)
492 output_buffer *buffer;
495 output_formatted_integer (buffer, "%ld", i);
499 output_unsigned_decimal (buffer, i)
500 output_buffer *buffer;
503 output_formatted_integer (buffer, "%u", i);
507 output_long_unsigned_decimal (buffer, i)
508 output_buffer *buffer;
511 output_formatted_integer (buffer, "%lu", i);
515 output_octal (buffer, i)
516 output_buffer *buffer;
519 output_formatted_integer (buffer, "%o", i);
523 output_long_octal (buffer, i)
524 output_buffer *buffer;
527 output_formatted_integer (buffer, "%lo", i);
531 output_hexadecimal (buffer, i)
532 output_buffer *buffer;
535 output_formatted_integer (buffer, "%x", i);
539 output_long_hexadecimal (buffer, i)
540 output_buffer *buffer;
543 output_formatted_integer (buffer, "%lx", i);
546 /* Append to BUFFER a string specified by its STARTING character
550 output_append_r (buffer, start, length)
551 output_buffer *buffer;
555 obstack_grow (&buffer->obstack, start, length);
556 output_text_length (buffer) += length;
559 /* Append a string deliminated by START and END to BUFFER. No wrapping is
560 done. However, if beginning a new line then emit output_prefix (BUFFER)
561 and skip any leading whitespace if appropriate. The caller must ensure
562 that it is safe to do so. */
565 output_append (buffer, start, end)
566 output_buffer *buffer;
570 /* Emit prefix and skip whitespace if we're starting a new line. */
571 if (is_starting_newline (buffer))
573 output_emit_prefix (buffer);
574 if (output_is_line_wrapping (buffer))
575 while (start != end && *start == ' ')
578 output_append_r (buffer, start, end - start);
582 output_indent (buffer)
583 output_buffer *buffer;
585 int n = output_indentation (buffer);
588 for (i = 0; i < n; ++i)
589 output_add_character (buffer, ' ');
592 /* Wrap a text delimited by START and END into BUFFER. */
595 wrap_text (buffer, start, end)
596 output_buffer *buffer;
600 int is_wrapping = output_is_line_wrapping (buffer);
604 /* Dump anything bodered by whitespaces. */
606 const char *p = start;
607 while (p != end && *p != ' ' && *p != '\n')
609 if (is_wrapping && p - start >= output_space_left (buffer))
610 output_add_newline (buffer);
611 output_append (buffer, start, p);
615 if (start != end && *start == ' ')
617 output_add_space (buffer);
620 if (start != end && *start == '\n')
622 output_add_newline (buffer);
628 /* Same as wrap_text but wrap text only when in line-wrapping mode. */
631 maybe_wrap_text (buffer, start, end)
632 output_buffer *buffer;
636 if (output_is_line_wrapping (buffer))
637 wrap_text (buffer, start, end);
639 output_append (buffer, start, end);
643 /* Append a STRING to BUFFER; the STRING might be line-wrapped if in
647 output_add_string (buffer, str)
648 output_buffer *buffer;
651 maybe_wrap_text (buffer, str, str + (str ? strlen (str) : 0));
654 /* Flush the content of BUFFER onto the attached stream,
658 output_buffer_to_stream (buffer)
659 output_buffer *buffer;
661 const char *text = output_finalize_message (buffer);
662 fputs (text, output_buffer_attached_stream (buffer));
663 output_clear_message_text (buffer);
666 /* Format a message pointed to by output_buffer_text_cursor (BUFFER) using
667 output_buffer_format_args (BUFFER) as appropriate. The following format
668 specifiers are recognized as being language independent:
669 %d, %i: (signed) integer in base ten.
670 %u: unsigned integer in base ten.
671 %o: unsigned integer in base eight.
672 %x: unsigned integer in base sixteen.
673 %ld, %li, %lo, %lu, %lx: long versions of the above.
677 %*.s: a substring the length of which is specified by an integer. */
680 output_format (buffer)
681 output_buffer *buffer;
683 for (; *output_buffer_text_cursor (buffer);
684 ++output_buffer_text_cursor (buffer))
686 int long_integer = 0;
690 const char *p = output_buffer_text_cursor (buffer);
691 while (*p && *p != '%')
693 wrap_text (buffer, output_buffer_text_cursor (buffer), p);
694 output_buffer_text_cursor (buffer) = p;
697 if (!*output_buffer_text_cursor (buffer))
700 /* We got a '%'. Let's see what happens. Record whether we're
701 parsing a long integer format specifier. */
702 if (*++output_buffer_text_cursor (buffer) == 'l')
705 ++output_buffer_text_cursor (buffer);
708 /* Handle %c, %d, %i, %ld, %li, %lo, %lu, %lx, %o, %s, %u,
709 %x, %.*s; %%. And nothing else. Front-ends should install
710 printers to grok language specific format specifiers. */
711 switch (*output_buffer_text_cursor (buffer))
715 (buffer, va_arg (output_buffer_format_args (buffer), int));
722 (buffer, va_arg (output_buffer_format_args (buffer), long int));
725 (buffer, va_arg (output_buffer_format_args (buffer), int));
730 output_long_octal (buffer,
731 va_arg (output_buffer_format_args (buffer),
734 output_octal (buffer,
735 va_arg (output_buffer_format_args (buffer),
740 output_add_string (buffer,
741 va_arg (output_buffer_format_args (buffer),
747 output_long_unsigned_decimal
748 (buffer, va_arg (output_buffer_format_args (buffer),
751 output_unsigned_decimal
752 (buffer, va_arg (output_buffer_format_args (buffer),
758 output_long_hexadecimal
759 (buffer, va_arg (output_buffer_format_args (buffer),
763 (buffer, va_arg (output_buffer_format_args (buffer),
768 output_add_character (buffer, '%');
775 /* We handle no precision specifier but `%.*s'. */
776 if (*++output_buffer_text_cursor (buffer) != '*')
778 else if (*++output_buffer_text_cursor (buffer) != 's')
780 n = va_arg (output_buffer_format_args (buffer), int);
781 s = va_arg (output_buffer_format_args (buffer), const char *);
782 output_append (buffer, s, s + n);
787 if (! lang_printer || !(*lang_printer) (buffer))
789 /* Hmmm. The front-end failed to install a format translator
790 but called us with an unrecognized format. Sorry. */
798 vbuild_message_string (msg, ap)
804 vasprintf (&str, msg, ap);
808 /* Return a malloc'd string containing MSG formatted a la
809 printf. The caller is reponsible for freeing the memory. */
812 build_message_string VPARAMS ((const char *msg, ...))
814 #ifndef ANSI_PROTOTYPES
822 #ifndef ANSI_PROTOTYPES
823 msg = va_arg (ap, const char *);
826 str = vbuild_message_string (msg, ap);
833 /* Return a malloc'd string describing a location. The caller is
834 responsible for freeing the memory. */
837 context_as_prefix (file, line, warn)
845 return build_message_string (_("%s:%d: warning: "), file, line);
847 return build_message_string ("%s:%d: ", file, line);
852 return build_message_string (_("%s: warning: "), progname);
854 return build_message_string ("%s: ", progname);
858 /* Same as context_as_prefix, but only the source FILE is given. */
861 file_name_as_prefix (f)
864 return build_message_string ("%s: ", f);
867 /* Format a MESSAGE into BUFFER. Automatically wrap lines. */
870 output_do_printf (buffer, msg)
871 output_buffer *buffer;
874 char *message = vbuild_message_string (msg,
875 output_buffer_format_args (buffer));
877 wrap_text (buffer, message, message + strlen (message));
882 /* Format a message into BUFFER a la printf. */
885 output_printf VPARAMS ((struct output_buffer *buffer, const char *msgid, ...))
887 #ifndef ANSI_PROTOTYPES
888 struct output_buffer *buffer;
894 VA_START (ap, msgid);
895 #ifndef ANSI_PROTOTYPES
896 buffer = va_arg (ap, output_buffer *);
897 msgid = va_arg (ap, const char *);
899 old_args = output_buffer_ptr_to_format_args (buffer);
900 output_buffer_ptr_to_format_args (buffer) = ≈
901 output_do_printf (buffer, _(msgid));
902 output_buffer_ptr_to_format_args (buffer) = old_args;
906 /* Print a message relevant to the given DECL. */
909 format_with_decl (buffer, decl)
910 output_buffer *buffer;
915 /* Do magic to get around lack of varargs support for insertion
916 of arguments into existing list. We know that the decl is first;
917 we ass_u_me that it will be printed with "%s". */
918 for (p = output_buffer_text_cursor (buffer); *p; ++p)
924 else if (*(p + 1) != 's')
931 /* Print the left-hand substring. */
932 maybe_wrap_text (buffer, output_buffer_text_cursor (buffer), p);
934 if (*p == '%') /* Print the name. */
936 const char *n = (DECL_NAME (decl)
937 ? (*decl_printable_name) (decl, 2)
938 : _("((anonymous))"));
939 output_add_string (buffer, n);
943 if (ISALPHA (*(p - 1) & 0xFF))
948 if (*p) /* Print the rest of the message. */
950 output_buffer_text_cursor (buffer) = p;
951 output_format (buffer);
955 /* Figure file and line of the given INSN. */
958 file_and_line_for_asm (insn, pfile, pline)
963 rtx body = PATTERN (insn);
966 /* Find the (or one of the) ASM_OPERANDS in the insn. */
967 if (GET_CODE (body) == SET && GET_CODE (SET_SRC (body)) == ASM_OPERANDS)
968 asmop = SET_SRC (body);
969 else if (GET_CODE (body) == ASM_OPERANDS)
971 else if (GET_CODE (body) == PARALLEL
972 && GET_CODE (XVECEXP (body, 0, 0)) == SET)
973 asmop = SET_SRC (XVECEXP (body, 0, 0));
974 else if (GET_CODE (body) == PARALLEL
975 && GET_CODE (XVECEXP (body, 0, 0)) == ASM_OPERANDS)
976 asmop = XVECEXP (body, 0, 0);
982 *pfile = ASM_OPERANDS_SOURCE_FILE (asmop);
983 *pline = ASM_OPERANDS_SOURCE_LINE (asmop);
987 *pfile = input_filename;
992 /* Report a diagnostic MESSAGE (an errror or a WARNING) at the line number
993 of the insn INSN. This is used only when INSN is an `asm' with operands,
994 and each ASM_OPERANDS records its own source file and line. */
997 diagnostic_for_asm (insn, msg, args_ptr, warn)
1003 diagnostic_context dc;
1005 set_diagnostic_context (&dc, msg, args_ptr, NULL, 0, warn);
1006 file_and_line_for_asm (insn, &diagnostic_file_location (&dc),
1007 &diagnostic_line_location (&dc));
1008 report_diagnostic (&dc);
1011 /* Report a diagnostic MESSAGE at the declaration DECL.
1012 MSG is a format string which uses %s to substitute the declaration
1013 name; subsequent substitutions are a la output_format. */
1016 diagnostic_for_decl (decl, msgid, args_ptr, warn)
1024 if (diagnostic_lock++)
1027 if (count_error (warn))
1029 os = output_buffer_state (diagnostic_buffer);
1030 report_error_function (DECL_SOURCE_FILE (decl));
1032 (diagnostic_buffer, context_as_prefix
1033 (DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl), warn));
1034 output_buffer_ptr_to_format_args (diagnostic_buffer) = args_ptr;
1035 output_buffer_text_cursor (diagnostic_buffer) = _(msgid);
1036 format_with_decl (diagnostic_buffer, decl);
1037 finish_diagnostic ();
1038 output_destroy_prefix (diagnostic_buffer);
1040 output_buffer_state (diagnostic_buffer) = os;
1046 /* Count an error or warning. Return 1 if the message should be printed. */
1049 count_error (warningp)
1053 && (inhibit_warnings
1054 || (in_system_header && !warn_system_headers)))
1057 if (warningp && !warnings_are_errors)
1061 static int warning_message = 0;
1063 if (warningp && !warning_message)
1065 verbatim ("%s: warnings being treated as errors\n", progname);
1066 warning_message = 1;
1074 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1075 runs its second argument through gettext. */
1078 fnotice VPARAMS ((FILE *file, const char *msgid, ...))
1080 #ifndef ANSI_PROTOTYPES
1086 VA_START (ap, msgid);
1088 #ifndef ANSI_PROTOTYPES
1089 file = va_arg (ap, FILE *);
1090 msgid = va_arg (ap, const char *);
1093 vfprintf (file, _(msgid), ap);
1098 /* Print a fatal I/O error message. Argument are like printf.
1099 Also include a system error message based on `errno'. */
1102 fatal_io_error VPARAMS ((const char *msgid, ...))
1104 #ifndef ANSI_PROTOTYPES
1110 os = output_buffer_state (diagnostic_buffer);
1111 VA_START (ap, msgid);
1113 #ifndef ANSI_PROTOTYPES
1114 msgid = va_arg (ap, const char *);
1117 output_printf (diagnostic_buffer, "%s: %s: ", progname, xstrerror (errno));
1118 output_buffer_ptr_to_format_args (diagnostic_buffer) = ≈
1119 output_buffer_text_cursor (diagnostic_buffer) = _(msgid);
1120 output_format (diagnostic_buffer);
1121 finish_diagnostic ();
1122 output_buffer_state (diagnostic_buffer) = os;
1124 exit (FATAL_EXIT_CODE);
1127 /* Issue a pedantic warning MSGID. */
1130 pedwarn VPARAMS ((const char *msgid, ...))
1132 #ifndef ANSI_PROTOTYPES
1136 diagnostic_context dc;
1138 VA_START (ap, msgid);
1140 #ifndef ANSI_PROTOTYPES
1141 msgid = va_arg (ap, const char *);
1144 set_diagnostic_context
1145 (&dc, msgid, &ap, input_filename, lineno, !flag_pedantic_errors);
1146 report_diagnostic (&dc);
1150 /* Issue a pedantic waring about DECL. */
1153 pedwarn_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1155 #ifndef ANSI_PROTOTYPES
1161 VA_START (ap, msgid);
1163 #ifndef ANSI_PROTOTYPES
1164 decl = va_arg (ap, tree);
1165 msgid = va_arg (ap, const char *);
1167 /* We don't want -pedantic-errors to cause the compilation to fail from
1168 "errors" in system header files. Sometimes fixincludes can't fix what's
1169 broken (eg: unsigned char bitfields - fixing it may change the alignment
1170 which will cause programs to mysteriously fail because the C library
1171 or kernel uses the original layout). There's no point in issuing a
1172 warning either, it's just unnecessary noise. */
1173 if (!DECL_IN_SYSTEM_HEADER (decl))
1174 diagnostic_for_decl (decl, msgid, &ap, !flag_pedantic_errors);
1178 /* Same as above but within the context FILE and LINE. */
1181 pedwarn_with_file_and_line VPARAMS ((const char *file, int line,
1182 const char *msgid, ...))
1184 #ifndef ANSI_PROTOTYPES
1190 diagnostic_context dc;
1192 VA_START (ap, msgid);
1194 #ifndef ANSI_PROTOTYPES
1195 file = va_arg (ap, const char *);
1196 line = va_arg (ap, int);
1197 msgid = va_arg (ap, const char *);
1200 set_diagnostic_context (&dc, msgid, &ap, file, line, !flag_pedantic_errors);
1201 report_diagnostic (&dc);
1205 /* Just apologize with MSGID. */
1208 sorry VPARAMS ((const char *msgid, ...))
1210 #ifndef ANSI_PROTOTYPES
1216 os = output_buffer_state (diagnostic_buffer);
1217 VA_START (ap, msgid);
1219 #ifndef ANSI_PROTOTYPES
1220 msgid = va_arg (ap, const char *);
1224 (diagnostic_buffer, context_as_prefix (input_filename, lineno, 0));
1225 output_printf (diagnostic_buffer, "sorry, not implemented: ");
1226 output_buffer_ptr_to_format_args (diagnostic_buffer) = ≈
1227 output_buffer_text_cursor (diagnostic_buffer) = _(msgid);
1228 output_format (diagnostic_buffer);
1229 finish_diagnostic ();
1230 output_buffer_state (diagnostic_buffer) = os;
1234 /* Called when the start of a function definition is parsed,
1235 this function prints on stderr the name of the function. */
1238 announce_function (decl)
1243 if (rtl_dump_and_exit)
1244 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl)));
1246 verbatim (" %s", (*decl_printable_name) (decl, 2));
1248 output_needs_newline (diagnostic_buffer) = 1;
1249 record_last_error_function ();
1253 /* The default function to print out name of current function that caused
1257 default_print_error_function (file)
1260 if (error_function_changed ())
1262 char *prefix = file ? build_message_string ("%s: ", file) : NULL;
1265 os = output_buffer_state (diagnostic_buffer);
1266 output_set_prefix (diagnostic_buffer, prefix);
1268 if (current_function_decl == NULL)
1269 output_add_string (diagnostic_buffer, _("At top level:"));
1272 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
1274 (diagnostic_buffer, "In method `%s':",
1275 (*decl_printable_name) (current_function_decl, 2));
1278 (diagnostic_buffer, "In function `%s':",
1279 (*decl_printable_name) (current_function_decl, 2));
1281 output_add_newline (diagnostic_buffer);
1283 record_last_error_function ();
1284 output_buffer_to_stream (diagnostic_buffer);
1285 output_buffer_state (diagnostic_buffer) = os;
1286 free ((char*) prefix);
1290 /* Prints out, if necessary, the name of the current function
1291 that caused an error. Called from all error and warning functions.
1292 We ignore the FILE parameter, as it cannot be relied upon. */
1295 report_error_function (file)
1296 const char *file ATTRIBUTE_UNUSED;
1298 report_problematic_module (diagnostic_buffer);
1299 (*print_error_function) (input_filename);
1303 error_with_file_and_line VPARAMS ((const char *file, int line,
1304 const char *msgid, ...))
1306 #ifndef ANSI_PROTOTYPES
1312 diagnostic_context dc;
1314 VA_START (ap, msgid);
1316 #ifndef ANSI_PROTOTYPES
1317 file = va_arg (ap, const char *);
1318 line = va_arg (ap, int);
1319 msgid = va_arg (ap, const char *);
1322 set_diagnostic_context (&dc, msgid, &ap, file, line, /* warn = */ 0);
1323 report_diagnostic (&dc);
1328 error_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1330 #ifndef ANSI_PROTOTYPES
1336 VA_START (ap, msgid);
1338 #ifndef ANSI_PROTOTYPES
1339 decl = va_arg (ap, tree);
1340 msgid = va_arg (ap, const char *);
1343 diagnostic_for_decl (decl, msgid, &ap, /* warn = */ 0);
1348 error_for_asm VPARAMS ((rtx insn, const char *msgid, ...))
1350 #ifndef ANSI_PROTOTYPES
1356 VA_START (ap, msgid);
1358 #ifndef ANSI_PROTOTYPES
1359 insn = va_arg (ap, rtx);
1360 msgid = va_arg (ap, const char *);
1363 diagnostic_for_asm (insn, msgid, &ap, /* warn = */ 0);
1367 /* Report an error message. The arguments are like that of printf. */
1370 error VPARAMS ((const char *msgid, ...))
1372 #ifndef ANSI_PROTOTYPES
1376 diagnostic_context dc;
1378 VA_START (ap, msgid);
1380 #ifndef ANSI_PROTOTYPES
1381 msgid = va_arg (ap, const char *);
1384 set_diagnostic_context
1385 (&dc, msgid, &ap, input_filename, lineno, /* warn = */ 0);
1386 report_diagnostic (&dc);
1390 /* Likewise, except that the compilation is terminated after printing the
1394 fatal_error VPARAMS ((const char *msgid, ...))
1396 #ifndef ANSI_PROTOTYPES
1400 diagnostic_context dc;
1402 VA_START (ap, msgid);
1404 #ifndef ANSI_PROTOTYPES
1405 msgid = va_arg (ap, const char *);
1408 set_diagnostic_context
1409 (&dc, msgid, &ap, input_filename, lineno, /* warn = */ 0);
1410 report_diagnostic (&dc);
1413 fnotice (stderr, "compilation terminated.\n");
1414 exit (FATAL_EXIT_CODE);
1417 /* Report a compiler error at the current line number. Allow a front end to
1418 intercept the message. */
1420 static void (*internal_error_function) PARAMS ((const char *, va_list *));
1422 /* Set the function to call when a compiler error occurs. */
1425 set_internal_error_function (f)
1426 void (*f) PARAMS ((const char *, va_list *));
1428 internal_error_function = f;
1432 internal_error VPARAMS ((const char *msgid, ...))
1434 #ifndef ANSI_PROTOTYPES
1438 diagnostic_context dc;
1440 VA_START (ap, msgid);
1442 #ifndef ANSI_PROTOTYPES
1443 msgid = va_arg (ap, const char *);
1446 if (errorcount > 0 || sorrycount > 0)
1448 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
1449 input_filename, lineno);
1450 exit (FATAL_EXIT_CODE);
1453 if (internal_error_function != 0)
1454 (*internal_error_function) (_(msgid), &ap);
1456 set_diagnostic_context
1457 (&dc, msgid, &ap, input_filename, lineno, /* warn = */0);
1458 report_diagnostic (&dc);
1462 "Please submit a full bug report,\n\
1463 with preprocessed source if appropriate.\n\
1464 See %s for instructions.\n", GCCBUGURL);
1465 exit (FATAL_EXIT_CODE);
1469 _fatal_insn (msgid, insn, file, line, function)
1474 const char *function;
1476 error ("%s", _(msgid));
1478 /* The above incremented error_count, but isn't an error that we want to
1479 count, so reset it here. */
1483 fancy_abort (file, line, function);
1487 _fatal_insn_not_found (insn, file, line, function)
1491 const char *function;
1493 if (INSN_CODE (insn) < 0)
1494 _fatal_insn ("Unrecognizable insn:", insn, file, line, function);
1496 _fatal_insn ("Insn does not satisfy its constraints:",
1497 insn, file, line, function);
1501 warning_with_file_and_line VPARAMS ((const char *file, int line,
1502 const char *msgid, ...))
1504 #ifndef ANSI_PROTOTYPES
1510 diagnostic_context dc;
1512 VA_START (ap, msgid);
1514 #ifndef ANSI_PROTOTYPES
1515 file = va_arg (ap, const char *);
1516 line = va_arg (ap, int);
1517 msgid = va_arg (ap, const char *);
1520 set_diagnostic_context (&dc, msgid, &ap, file, line, /* warn = */ 1);
1521 report_diagnostic (&dc);
1526 warning_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1528 #ifndef ANSI_PROTOTYPES
1534 VA_START (ap, msgid);
1536 #ifndef ANSI_PROTOTYPES
1537 decl = va_arg (ap, tree);
1538 msgid = va_arg (ap, const char *);
1541 diagnostic_for_decl (decl, msgid, &ap, /* warn = */ 1);
1546 warning_for_asm VPARAMS ((rtx insn, const char *msgid, ...))
1548 #ifndef ANSI_PROTOTYPES
1554 VA_START (ap, msgid);
1556 #ifndef ANSI_PROTOTYPES
1557 insn = va_arg (ap, rtx);
1558 msgid = va_arg (ap, const char *);
1561 diagnostic_for_asm (insn, msgid, &ap, /* warn = */ 1);
1566 warning VPARAMS ((const char *msgid, ...))
1568 #ifndef ANSI_PROTOTYPES
1572 diagnostic_context dc;
1574 VA_START (ap, msgid);
1576 #ifndef ANSI_PROTOTYPES
1577 msgid = va_arg (ap, const char *);
1580 set_diagnostic_context
1581 (&dc, msgid, &ap, input_filename, lineno, /* warn = */ 1);
1582 report_diagnostic (&dc);
1586 /* Flush diagnostic_buffer content on stderr. */
1589 finish_diagnostic ()
1591 output_buffer_to_stream (diagnostic_buffer);
1592 clear_diagnostic_info (diagnostic_buffer);
1593 fputc ('\n', output_buffer_attached_stream (diagnostic_buffer));
1594 fflush (output_buffer_attached_stream (diagnostic_buffer));
1597 /* Helper subroutine of output_verbatim and verbatim. Do the approriate
1598 settings needed by BUFFER for a verbatim formatting. */
1601 output_do_verbatim (buffer, msgid, args_ptr)
1602 output_buffer *buffer;
1608 os = output_buffer_state (buffer);
1609 output_prefix (buffer) = NULL;
1610 prefixing_policy (buffer) = DIAGNOSTICS_SHOW_PREFIX_NEVER;
1611 output_buffer_text_cursor (buffer) = _(msgid);
1612 output_buffer_ptr_to_format_args (buffer) = args_ptr;
1613 output_set_maximum_length (buffer, 0);
1614 output_format (buffer);
1615 output_buffer_state (buffer) = os;
1618 /* Output MESSAGE verbatim into BUFFER. */
1621 output_verbatim VPARAMS ((output_buffer *buffer, const char *msgid, ...))
1623 #ifndef ANSI_PROTOTYPES
1624 output_buffer *buffer;
1629 VA_START (ap, msgid);
1630 #ifndef ANSI_PROTOTYPES
1631 buffer = va_arg (ap, output_buffer *);
1632 msg = va_arg (ap, const char *);
1634 output_do_verbatim (buffer, msgid, &ap);
1638 /* Same as above but use diagnostic_buffer. */
1641 verbatim VPARAMS ((const char *msgid, ...))
1643 #ifndef ANSI_PROTOTYPES
1648 VA_START (ap, msgid);
1649 #ifndef ANSI_PROTOTYPES
1650 msgid = va_arg (ap, const char *);
1652 output_do_verbatim (diagnostic_buffer, msgid, &ap);
1653 output_buffer_to_stream (diagnostic_buffer);
1657 /* Report a diagnostic message (an error or a warning) as specified by
1658 DC. This function is *the* subroutine in terms of which front-ends
1659 should implement their specific diagnostic handling modules. The
1660 front-end independent format specifiers are exactly those described
1661 in the documentation of output_format. */
1664 report_diagnostic (dc)
1665 diagnostic_context *dc;
1669 if (diagnostic_lock++)
1672 if (count_error (diagnostic_is_warning (dc)))
1674 os = output_buffer_state (diagnostic_buffer);
1675 diagnostic_msg = diagnostic_message (dc);
1676 diagnostic_args = diagnostic_argument_list (dc);
1677 (*diagnostic_starter (dc)) (diagnostic_buffer, dc);
1678 output_format (diagnostic_buffer);
1679 (*diagnostic_finalizer (dc)) (diagnostic_buffer, dc);
1680 finish_diagnostic ();
1681 output_buffer_state (diagnostic_buffer) = os;
1687 /* Inform the user that an error occurred while trying to report some
1688 other error. This indicates catastrophic internal inconsistencies,
1689 so give up now. But do try to flush out the previous error.
1690 This mustn't use internal_error, that will cause infinite recursion. */
1695 if (diagnostic_lock < 3)
1696 finish_diagnostic ();
1699 "Internal compiler error: Error reporting routines re-entered.\n");
1701 "Please submit a full bug report,\n\
1702 with preprocessed source if appropriate.\n\
1703 See %s for instructions.\n", GCCBUGURL);
1704 exit (FATAL_EXIT_CODE);
1707 /* Given a partial pathname as input, return another pathname that
1708 shares no directory elements with the pathname of __FILE__. This
1709 is used by fancy_abort() to print `Internal compiler error in expr.c'
1710 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1713 trim_filename (name)
1716 static const char this_file[] = __FILE__;
1717 const char *p = name, *q = this_file;
1719 /* First skip any "../" in each filename. This allows us to give a proper
1720 reference to a file in a subdirectory. */
1721 while (p[0] == '.' && p[1] == '.'
1722 && (p[2] == DIR_SEPARATOR
1723 #ifdef DIR_SEPARATOR_2
1724 || p[2] == DIR_SEPARATOR_2
1729 while (q[0] == '.' && q[1] == '.'
1730 && (q[2] == DIR_SEPARATOR
1731 #ifdef DIR_SEPARATOR_2
1732 || p[2] == DIR_SEPARATOR_2
1737 /* Now skip any parts the two filenames have in common. */
1738 while (*p == *q && *p != 0 && *q != 0)
1741 /* Now go backwards until the previous directory separator. */
1742 while (p > name && p[-1] != DIR_SEPARATOR
1743 #ifdef DIR_SEPARATOR_2
1744 && p[-1] != DIR_SEPARATOR_2
1752 /* Report an internal compiler error in a friendly manner and without
1756 fancy_abort (file, line, function)
1759 const char *function;
1761 internal_error ("Internal compiler error in %s, at %s:%d",
1762 function, trim_filename (file), line);
1765 /* Setup DC for reporting a diagnostic MESSAGE (an error or a WARNING),
1766 using arguments pointed to by ARGS_PTR, issued at a location specified
1767 by FILE and LINE. */
1770 set_diagnostic_context (dc, msgid, args_ptr, file, line, warn)
1771 diagnostic_context *dc;
1778 memset (dc, 0, sizeof (diagnostic_context));
1779 diagnostic_message (dc) = _(msgid);
1780 diagnostic_argument_list (dc) = args_ptr;
1781 diagnostic_file_location (dc) = file;
1782 diagnostic_line_location (dc) = line;
1783 diagnostic_is_warning (dc) = warn;
1784 diagnostic_starter (dc) = lang_diagnostic_starter;
1785 diagnostic_finalizer (dc) = lang_diagnostic_finalizer;
1789 report_problematic_module (buffer)
1790 output_buffer *buffer;
1792 struct file_stack *p;
1794 if (output_needs_newline (buffer))
1796 output_add_newline (buffer);
1797 output_needs_newline (buffer) = 0;
1800 if (input_file_stack && input_file_stack->next != 0
1801 && error_module_changed ())
1803 for (p = input_file_stack->next; p; p = p->next)
1804 if (p == input_file_stack->next)
1806 (buffer, "In file included from %s:%d", p->name, p->line);
1809 (buffer, ",\n from %s:%d", p->name, p->line);
1810 output_verbatim (buffer, ":\n");
1811 record_last_error_module ();
1816 default_diagnostic_starter (buffer, dc)
1817 output_buffer *buffer;
1818 diagnostic_context *dc;
1820 report_error_function (diagnostic_file_location (dc));
1821 output_set_prefix (buffer,
1822 context_as_prefix (diagnostic_file_location (dc),
1823 diagnostic_line_location (dc),
1824 diagnostic_is_warning (dc)));
1828 default_diagnostic_finalizer (buffer, dc)
1829 output_buffer *buffer;
1830 diagnostic_context *dc __attribute__((__unused__));
1832 output_destroy_prefix (buffer);