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)
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-codes.h"
38 #include "insn-config.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) \
48 sprintf (digit_buffer, FORMAT, INTEGER); \
49 output_add_string (BUFFER, digit_buffer); \
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)
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 *));
70 static void output_indent PARAMS ((output_buffer *));
72 static char *vbuild_message_string PARAMS ((const char *, va_list))
73 ATTRIBUTE_PRINTF (1, 0);
74 static char *build_message_string PARAMS ((const char *, ...))
76 static void output_do_printf PARAMS ((output_buffer *, const char *))
77 ATTRIBUTE_PRINTF (2, 0);
78 static void format_with_decl PARAMS ((output_buffer *, tree));
79 static void file_and_line_for_asm PARAMS ((rtx, const char **, int *));
80 static void diagnostic_for_asm PARAMS ((rtx, const char *, va_list *, int));
81 static void diagnostic_for_decl PARAMS ((tree, const char *, va_list *, int));
82 static void vnotice PARAMS ((FILE *, const char *, va_list))
83 ATTRIBUTE_PRINTF (2, 0);
84 static void set_real_maximum_length PARAMS ((output_buffer *));
86 static void output_unsigned_decimal PARAMS ((output_buffer *, unsigned int));
87 static void output_long_decimal PARAMS ((output_buffer *, long int));
88 static void output_long_unsigned_decimal PARAMS ((output_buffer *,
90 static void output_octal PARAMS ((output_buffer *, unsigned int));
91 static void output_long_octal PARAMS ((output_buffer *, unsigned long int));
92 static void output_hexadecimal PARAMS ((output_buffer *, unsigned int));
93 static void output_long_hexadecimal PARAMS ((output_buffer *,
95 static void output_append_r PARAMS ((output_buffer *, const char *, int));
96 static void wrap_text PARAMS ((output_buffer *, const char *, const char *));
97 static void maybe_wrap_text PARAMS ((output_buffer *, const char *,
99 static void clear_diagnostic_info PARAMS ((output_buffer *));
101 static void default_diagnostic_starter PARAMS ((output_buffer *,
102 diagnostic_context *));
103 static void default_diagnostic_finalizer PARAMS ((output_buffer *,
104 diagnostic_context *));
106 static void error_recursion PARAMS ((void)) ATTRIBUTE_NORETURN;
107 static const char *trim_filename PARAMS ((const char *));
109 extern int rtl_dump_and_exit;
110 extern int inhibit_warnings;
111 extern int warnings_are_errors;
112 extern int warningcount;
113 extern int errorcount;
115 /* Front-end specific tree formatter, if non-NULL. */
116 printer_fn lang_printer = NULL;
118 /* This must be large enough to hold any printed integer or
119 floating-point value. */
120 static char digit_buffer[128];
122 /* An output_buffer surrogate for stderr. */
123 static output_buffer global_output_buffer;
124 output_buffer *diagnostic_buffer = &global_output_buffer;
126 /* Function of last error message;
127 more generally, function such that if next error message is in it
128 then we don't have to mention the function name. */
129 static tree last_error_function = NULL;
131 /* Used to detect when input_file_stack has changed since last described. */
132 static int last_error_tick;
134 /* Called by report_error_function to print out function name.
135 Default may be overridden by language front-ends. */
137 void (*print_error_function) PARAMS ((const char *)) =
138 default_print_error_function;
140 /* Hooks for language specific diagnostic messages pager and finalizer. */
141 diagnostic_starter_fn lang_diagnostic_starter;
142 diagnostic_finalizer_fn lang_diagnostic_finalizer;
144 /* Maximum characters per line in automatic line wrapping mode.
145 Zero means don't wrap lines. */
147 int diagnostic_message_length_per_line;
149 /* Used to control every diagnostic message formatting. Front-ends should
150 call set_message_prefixing_rule to set up their policies. */
151 static int current_prefixing_rule;
153 /* Prevent recursion into the error handler. */
154 static int diagnostic_lock;
157 /* Return truthvalue if current input file is different from the most recent
158 file involved in a diagnostic message. */
161 error_module_changed ()
163 return last_error_tick != input_file_stack_tick;
166 /* Remember current file as being the most recent file involved in a
167 diagnostic message. */
170 record_last_error_module ()
172 last_error_tick = input_file_stack_tick;
175 /* Same as error_module_changed, but for function. */
178 error_function_changed ()
180 return last_error_function != current_function_decl;
183 /* Same as record_last_error_module, but for function. */
186 record_last_error_function ()
188 last_error_function = current_function_decl;
191 /* Initialize the diagnostic message outputting machinery. */
194 initialize_diagnostics ()
196 /* By default, we don't line-wrap messages. */
197 diagnostic_message_length_per_line = 0;
198 set_message_prefixing_rule (DIAGNOSTICS_SHOW_PREFIX_ONCE);
200 /* Proceed to actual initialization. */
201 default_initialize_buffer (diagnostic_buffer);
203 lang_diagnostic_starter = default_diagnostic_starter;
204 lang_diagnostic_finalizer = default_diagnostic_finalizer;
208 set_message_prefixing_rule (rule)
211 current_prefixing_rule = rule;
214 /* Returns true if BUFFER is in line-wrappind mode. */
217 output_is_line_wrapping (buffer)
218 output_buffer *buffer;
220 return ideal_line_wrap_cutoff (buffer) > 0;
223 /* Return BUFFER's prefix. */
226 output_get_prefix (buffer)
227 const output_buffer *buffer;
229 return output_prefix (buffer);
232 /* Subroutine of output_set_maximum_length. Set up BUFFER's
233 internal maximum characters per line. */
236 set_real_maximum_length (buffer)
237 output_buffer *buffer;
239 /* If we're told not to wrap lines then do the obvious thing. In case
240 we'll emit prefix only once per diagnostic message, it is appropriate
241 not to increase unncessarily the line-length cut-off. */
242 if (! output_is_line_wrapping (buffer)
243 || prefixing_policy (buffer) == DIAGNOSTICS_SHOW_PREFIX_ONCE
244 || prefixing_policy (buffer) == DIAGNOSTICS_SHOW_PREFIX_NEVER)
245 line_wrap_cutoff (buffer) = ideal_line_wrap_cutoff (buffer);
249 output_prefix (buffer) ? strlen (output_prefix (buffer)) : 0;
250 /* If the prefix is ridiculously too long, output at least
252 if (ideal_line_wrap_cutoff (buffer) - prefix_length < 32)
253 line_wrap_cutoff (buffer) = ideal_line_wrap_cutoff (buffer) + 32;
255 line_wrap_cutoff (buffer) = ideal_line_wrap_cutoff (buffer);
259 /* Sets the number of maximum characters per line BUFFER can output
260 in line-wrapping mode. A LENGTH value 0 suppresses line-wrapping. */
263 output_set_maximum_length (buffer, length)
264 output_buffer *buffer;
267 ideal_line_wrap_cutoff (buffer) = length;
268 set_real_maximum_length (buffer);
271 /* Sets BUFFER's PREFIX. */
274 output_set_prefix (buffer, prefix)
275 output_buffer *buffer;
278 output_prefix (buffer) = prefix;
279 set_real_maximum_length (buffer);
280 prefix_was_emitted_for (buffer) = 0;
281 output_indentation (buffer) = 0;
284 /* Return a pointer to the last character emitted in the output
285 BUFFER area. A NULL pointer means no character available. */
287 output_last_position (buffer)
288 const output_buffer *buffer;
290 const char *p = NULL;
292 if (obstack_base (&buffer->obstack) != obstack_next_free (&buffer->obstack))
293 p = ((const char *) obstack_next_free (&buffer->obstack)) - 1;
297 /* Free BUFFER's prefix, a previously malloc'd string. */
300 output_destroy_prefix (buffer)
301 output_buffer *buffer;
303 if (output_prefix (buffer) != NULL)
305 free ((char *) output_prefix (buffer));
306 output_prefix (buffer) = NULL;
310 /* Zero out any text output so far in BUFFER. */
313 output_clear_message_text (buffer)
314 output_buffer *buffer;
316 obstack_free (&buffer->obstack, obstack_base (&buffer->obstack));
317 output_text_length (buffer) = 0;
320 /* Zero out any diagnostic data used so far by BUFFER. */
323 clear_diagnostic_info (buffer)
324 output_buffer *buffer;
326 output_buffer_text_cursor (buffer) = NULL;
327 output_buffer_ptr_to_format_args (buffer) = NULL;
328 prefix_was_emitted_for (buffer) = 0;
329 output_indentation (buffer) = 0;
332 /* Construct an output BUFFER with PREFIX and of MAXIMUM_LENGTH
333 characters per line. */
336 init_output_buffer (buffer, prefix, maximum_length)
337 output_buffer *buffer;
341 memset (buffer, 0, sizeof (output_buffer));
342 obstack_init (&buffer->obstack);
343 ideal_line_wrap_cutoff (buffer) = maximum_length;
344 prefixing_policy (buffer) = current_prefixing_rule;
345 output_set_prefix (buffer, prefix);
346 output_text_length (buffer) = 0;
347 clear_diagnostic_info (buffer);
350 /* Initialize BUFFER with a NULL prefix and current diagnostic message
354 default_initialize_buffer (buffer)
355 output_buffer *buffer;
357 init_output_buffer (buffer, NULL, diagnostic_message_length_per_line);
360 /* Recompute diagnostic_buffer's attributes to reflect any change
361 in diagnostic formatting global options. */
364 reshape_diagnostic_buffer ()
366 ideal_line_wrap_cutoff (diagnostic_buffer) =
367 diagnostic_message_length_per_line;
368 prefixing_policy (diagnostic_buffer) = current_prefixing_rule;
369 set_real_maximum_length (diagnostic_buffer);
372 /* Reinitialize BUFFER. */
375 output_clear (buffer)
376 output_buffer *buffer;
378 output_clear_message_text (buffer);
379 clear_diagnostic_info (buffer);
382 /* Finishes constructing a NULL-terminated character string representing
383 the BUFFERed message. */
386 output_finalize_message (buffer)
387 output_buffer *buffer;
389 obstack_1grow (&buffer->obstack, '\0');
390 return output_message_text (buffer);
394 flush_diagnostic_buffer ()
396 output_to_stream (diagnostic_buffer, stderr);
400 /* Return the amount of characters BUFFER can accept to
404 output_space_left (buffer)
405 const output_buffer *buffer;
407 return line_wrap_cutoff (buffer) - output_text_length (buffer);
410 /* Write out BUFFER's prefix. */
413 output_emit_prefix (buffer)
414 output_buffer *buffer;
416 if (output_prefix (buffer) != NULL)
418 switch (prefixing_policy (buffer))
421 case DIAGNOSTICS_SHOW_PREFIX_NEVER:
424 case DIAGNOSTICS_SHOW_PREFIX_ONCE:
425 if (prefix_was_emitted_for (buffer))
427 output_indent (buffer);
430 output_indentation (buffer) += 3;
433 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
435 int prefix_length = strlen (output_prefix (buffer));
436 output_append_r (buffer, output_prefix (buffer), prefix_length);
437 prefix_was_emitted_for (buffer) = 1;
444 /* Have BUFFER start a new line. */
447 output_add_newline (buffer)
448 output_buffer *buffer;
450 obstack_1grow (&buffer->obstack, '\n');
451 output_text_length (buffer) = 0;
454 /* Appends a character to BUFFER. */
457 output_add_character (buffer, c)
458 output_buffer *buffer;
461 if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
462 output_add_newline (buffer);
463 obstack_1grow (&buffer->obstack, c);
464 ++output_text_length (buffer);
467 /* Adds a space to BUFFER. */
470 output_add_space (buffer)
471 output_buffer *buffer;
473 if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
475 output_add_newline (buffer);
478 obstack_1grow (&buffer->obstack, ' ');
479 ++output_text_length (buffer);
482 /* These functions format an INTEGER into BUFFER as suggested by their
486 output_decimal (buffer, i)
487 output_buffer *buffer;
490 output_formatted_integer (buffer, "%d", i);
494 output_long_decimal (buffer, i)
495 output_buffer *buffer;
498 output_formatted_integer (buffer, "%ld", i);
502 output_unsigned_decimal (buffer, i)
503 output_buffer *buffer;
506 output_formatted_integer (buffer, "%u", i);
510 output_long_unsigned_decimal (buffer, i)
511 output_buffer *buffer;
514 output_formatted_integer (buffer, "%lu", i);
518 output_octal (buffer, i)
519 output_buffer *buffer;
522 output_formatted_integer (buffer, "%o", i);
526 output_long_octal (buffer, i)
527 output_buffer *buffer;
530 output_formatted_integer (buffer, "%lo", i);
534 output_hexadecimal (buffer, i)
535 output_buffer *buffer;
538 output_formatted_integer (buffer, "%x", i);
542 output_long_hexadecimal (buffer, i)
543 output_buffer *buffer;
546 output_formatted_integer (buffer, "%lx", i);
549 /* Append to BUFFER a string specified by its STARTING character
553 output_append_r (buffer, start, length)
554 output_buffer *buffer;
558 obstack_grow (&buffer->obstack, start, length);
559 output_text_length (buffer) += length;
562 /* Append a string deliminated by START and END to BUFFER. No wrapping is
563 done. However, if beginning a new line then emit output_prefix (BUFFER)
564 and skip any leading whitespace if appropriate. The caller must ensure
565 that it is safe to do so. */
568 output_append (buffer, start, end)
569 output_buffer *buffer;
573 /* Emit prefix and skip whitespace if we're starting a new line. */
574 if (is_starting_newline (buffer))
576 output_emit_prefix (buffer);
577 if (output_is_line_wrapping (buffer))
578 while (start != end && *start == ' ')
581 output_append_r (buffer, start, end - start);
585 output_indent (buffer)
586 output_buffer *buffer;
588 int n = output_indentation (buffer);
591 for (i = 0; i < n; ++i)
592 output_add_character (buffer, ' ');
595 /* Wrap a text delimited by START and END into BUFFER. */
598 wrap_text (buffer, start, end)
599 output_buffer *buffer;
603 int is_wrapping = output_is_line_wrapping (buffer);
607 /* Dump anything bodered by whitespaces. */
609 const char *p = start;
610 while (p != end && *p != ' ' && *p != '\n')
612 if (is_wrapping && p - start >= output_space_left (buffer))
613 output_add_newline (buffer);
614 output_append (buffer, start, p);
618 if (start != end && *start == ' ')
620 output_add_space (buffer);
623 if (start != end && *start == '\n')
625 output_add_newline (buffer);
631 /* Same as wrap_text but wrap text only when in line-wrapping mode. */
634 maybe_wrap_text (buffer, start, end)
635 output_buffer *buffer;
639 if (output_is_line_wrapping (buffer))
640 wrap_text (buffer, start, end);
642 output_append (buffer, start, end);
646 /* Append a STRING to BUFFER; the STRING might be line-wrapped if in
650 output_add_string (buffer, str)
651 output_buffer *buffer;
654 maybe_wrap_text (buffer, str, str + (str ? strlen (str) : 0));
657 /* Flush the content of BUFFER onto FILE and reinitialize BUFFER. */
660 output_to_stream (buffer, file)
661 output_buffer *buffer;
664 const char *text = output_finalize_message (buffer);
666 output_clear_message_text (buffer);
669 /* Format a message pointed to by output_buffer_text_cursor (BUFFER) using
670 output_buffer_format_args (BUFFER) as appropriate. The following format
671 specifiers are recognized as being language independent:
672 %d, %i: (signed) integer in base ten.
673 %u: unsigned integer in base ten.
674 %o: unsigned integer in base eight.
675 %x: unsigned integer in base sixteen.
676 %ld, %li, %lo, %lu, %lx: long versions of the above.
680 %*.s: a substring the length of which is specified by an integer. */
683 output_format (buffer)
684 output_buffer *buffer;
686 for (; *output_buffer_text_cursor (buffer);
687 ++output_buffer_text_cursor (buffer))
689 int long_integer = 0;
693 const char *p = output_buffer_text_cursor (buffer);
694 while (*p && *p != '%')
696 wrap_text (buffer, output_buffer_text_cursor (buffer), p);
697 output_buffer_text_cursor (buffer) = p;
700 if (!*output_buffer_text_cursor (buffer))
703 /* We got a '%'. Let's see what happens. Record whether we're
704 parsing a long integer format specifier. */
705 if (*++output_buffer_text_cursor (buffer) == 'l')
708 ++output_buffer_text_cursor (buffer);
711 /* Handle %c, %d, %i, %ld, %li, %lo, %lu, %lx, %o, %s, %u,
712 %x, %.*s; %%. And nothing else. Front-ends should install
713 printers to grok language specific format specifiers. */
714 switch (*output_buffer_text_cursor (buffer))
718 (buffer, va_arg (output_buffer_format_args (buffer), int));
725 (buffer, va_arg (output_buffer_format_args (buffer), long int));
728 (buffer, va_arg (output_buffer_format_args (buffer), int));
733 output_long_octal (buffer,
734 va_arg (output_buffer_format_args (buffer),
737 output_octal (buffer,
738 va_arg (output_buffer_format_args (buffer),
743 output_add_string (buffer,
744 va_arg (output_buffer_format_args (buffer),
750 output_long_unsigned_decimal
751 (buffer, va_arg (output_buffer_format_args (buffer),
754 output_unsigned_decimal
755 (buffer, va_arg (output_buffer_format_args (buffer),
761 output_long_hexadecimal
762 (buffer, va_arg (output_buffer_format_args (buffer),
766 (buffer, va_arg (output_buffer_format_args (buffer),
771 output_add_character (buffer, '%');
778 /* We handle no precision specifier but `%.*s'. */
779 if (*++output_buffer_text_cursor (buffer) != '*')
781 else if (*++output_buffer_text_cursor (buffer) != 's')
783 n = va_arg (output_buffer_format_args (buffer), int);
784 s = va_arg (output_buffer_format_args (buffer), const char *);
785 output_append (buffer, s, s + n);
790 if (! lang_printer || !(*lang_printer) (buffer))
792 /* Hmmm. The front-end failed to install a format translator
793 but called us with an unrecognized format. Sorry. */
801 vbuild_message_string (msgid, ap)
807 vasprintf (&str, msgid, ap);
811 /* Return a malloc'd string containing MSGID formatted a la
812 printf. The caller is reponsible for freeing the memory. */
815 build_message_string VPARAMS ((const char *msgid, ...))
817 #ifndef ANSI_PROTOTYPES
823 VA_START (ap, msgid);
825 #ifndef ANSI_PROTOTYPES
826 msgid = va_arg (ap, const char *);
829 str = vbuild_message_string (msgid, ap);
836 /* Return a malloc'd string describing a location. The caller is
837 responsible for freeing the memory. */
840 context_as_prefix (file, line, warn)
848 return build_message_string ("%s:%d: warning: ", file, line);
850 return build_message_string ("%s:%d: ", file, line);
855 return build_message_string ("%s: warning: ", progname);
857 return build_message_string ("%s: ", progname);
861 /* Same as context_as_prefix, but only the source FILE is given. */
864 file_name_as_prefix (f)
867 return build_message_string ("%s: ", f);
870 /* Format a MESSAGE into BUFFER. Automatically wrap lines. */
873 output_do_printf (buffer, msgid)
874 output_buffer *buffer;
877 char *message = vbuild_message_string (msgid,
878 output_buffer_format_args (buffer));
880 wrap_text (buffer, message, message + strlen (message));
885 /* Format a message into BUFFER a la printf. */
888 output_printf VPARAMS ((struct output_buffer *buffer, const char *msgid, ...))
890 #ifndef ANSI_PROTOTYPES
891 struct output_buffer *buffer;
897 VA_START (ap, msgid);
898 #ifndef ANSI_PROTOTYPES
899 buffer = va_arg (ap, output_buffer *);
900 msgid = va_arg (ap, const char *);
902 old_args = output_buffer_ptr_to_format_args (buffer);
903 output_buffer_ptr_to_format_args (buffer) = ≈
904 output_do_printf (buffer, msgid);
905 output_buffer_ptr_to_format_args (buffer) = old_args;
909 /* Print the message MSGID in FILE. */
912 vnotice (file, msgid, ap)
917 vfprintf (file, _(msgid), ap);
920 /* Print a message relevant to the given DECL. */
923 format_with_decl (buffer, decl)
924 output_buffer *buffer;
929 /* Do magic to get around lack of varargs support for insertion
930 of arguments into existing list. We know that the decl is first;
931 we ass_u_me that it will be printed with "%s". */
932 for (p = output_buffer_text_cursor (buffer); *p; ++p)
938 else if (*(p + 1) != 's')
945 /* Print the left-hand substring. */
946 maybe_wrap_text (buffer, output_buffer_text_cursor (buffer), p);
948 if (*p == '%') /* Print the name. */
950 const char *n = (DECL_NAME (decl)
951 ? (*decl_printable_name) (decl, 2)
952 : _("((anonymous))"));
953 output_add_string (buffer, n);
957 if (ISALPHA (*(p - 1) & 0xFF))
962 if (*p) /* Print the rest of the message. */
964 output_buffer_text_cursor (buffer) = p;
965 output_format (buffer);
969 /* Figure file and line of the given INSN. */
972 file_and_line_for_asm (insn, pfile, pline)
977 rtx body = PATTERN (insn);
980 /* Find the (or one of the) ASM_OPERANDS in the insn. */
981 if (GET_CODE (body) == SET && GET_CODE (SET_SRC (body)) == ASM_OPERANDS)
982 asmop = SET_SRC (body);
983 else if (GET_CODE (body) == ASM_OPERANDS)
985 else if (GET_CODE (body) == PARALLEL
986 && GET_CODE (XVECEXP (body, 0, 0)) == SET)
987 asmop = SET_SRC (XVECEXP (body, 0, 0));
988 else if (GET_CODE (body) == PARALLEL
989 && GET_CODE (XVECEXP (body, 0, 0)) == ASM_OPERANDS)
990 asmop = XVECEXP (body, 0, 0);
996 *pfile = ASM_OPERANDS_SOURCE_FILE (asmop);
997 *pline = ASM_OPERANDS_SOURCE_LINE (asmop);
1001 *pfile = input_filename;
1006 /* Report a diagnostic MESSAGE (an errror or a WARNING) at the line number
1007 of the insn INSN. This is used only when INSN is an `asm' with operands,
1008 and each ASM_OPERANDS records its own source file and line. */
1011 diagnostic_for_asm (insn, msg, args_ptr, warn)
1017 diagnostic_context dc;
1019 set_diagnostic_context (&dc, msg, args_ptr, NULL, 0, warn);
1020 file_and_line_for_asm (insn, &diagnostic_file_location (&dc),
1021 &diagnostic_line_location (&dc));
1022 report_diagnostic (&dc);
1025 /* Report a diagnostic MESSAGE at the declaration DECL.
1026 MSG is a format string which uses %s to substitute the declaration
1027 name; subsequent substitutions are a la output_format. */
1030 diagnostic_for_decl (decl, msg, args_ptr, warn)
1038 if (diagnostic_lock++)
1041 if (count_error (warn))
1043 os = output_buffer_state (diagnostic_buffer);
1044 report_error_function (DECL_SOURCE_FILE (decl));
1046 (diagnostic_buffer, context_as_prefix
1047 (DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl), warn));
1048 output_buffer_ptr_to_format_args (diagnostic_buffer) = args_ptr;
1049 output_buffer_text_cursor (diagnostic_buffer) = msg;
1050 format_with_decl (diagnostic_buffer, decl);
1051 finish_diagnostic ();
1052 output_destroy_prefix (diagnostic_buffer);
1054 output_buffer_state (diagnostic_buffer) = os;
1060 /* Count an error or warning. Return 1 if the message should be printed. */
1063 count_error (warningp)
1067 && (inhibit_warnings
1068 || (in_system_header && !warn_system_headers)))
1071 if (warningp && !warnings_are_errors)
1075 static int warning_message = 0;
1077 if (warningp && !warning_message)
1079 verbatim ("%s: warnings being treated as errors\n", progname);
1080 warning_message = 1;
1088 /* Print a diagnistic MSGID on FILE. */
1091 fnotice VPARAMS ((FILE *file, const char *msgid, ...))
1093 #ifndef ANSI_PROTOTYPES
1099 VA_START (ap, msgid);
1101 #ifndef ANSI_PROTOTYPES
1102 file = va_arg (ap, FILE *);
1103 msgid = va_arg (ap, const char *);
1106 vnotice (file, msgid, ap);
1111 /* Print a fatal error message. NAME is the text.
1112 Also include a system error message based on `errno'. */
1115 pfatal_with_name (name)
1118 fprintf (stderr, "%s: ", progname);
1120 exit (FATAL_EXIT_CODE);
1124 fatal_io_error (name)
1127 verbatim ("%s: %s: I/O error\n", progname, name);
1128 exit (FATAL_EXIT_CODE);
1131 /* Issue a pedantic warning MSGID. */
1134 pedwarn VPARAMS ((const char *msgid, ...))
1136 #ifndef ANSI_PROTOTYPES
1140 diagnostic_context dc;
1142 VA_START (ap, msgid);
1144 #ifndef ANSI_PROTOTYPES
1145 msgid = va_arg (ap, const char *);
1148 set_diagnostic_context
1149 (&dc, msgid, &ap, input_filename, lineno, !flag_pedantic_errors);
1150 report_diagnostic (&dc);
1154 /* Issue a pedantic waring about DECL. */
1157 pedwarn_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1159 #ifndef ANSI_PROTOTYPES
1165 VA_START (ap, msgid);
1167 #ifndef ANSI_PROTOTYPES
1168 decl = va_arg (ap, tree);
1169 msgid = va_arg (ap, const char *);
1171 /* We don't want -pedantic-errors to cause the compilation to fail from
1172 "errors" in system header files. Sometimes fixincludes can't fix what's
1173 broken (eg: unsigned char bitfields - fixing it may change the alignment
1174 which will cause programs to mysteriously fail because the C library
1175 or kernel uses the original layout). There's no point in issuing a
1176 warning either, it's just unnecessary noise. */
1177 if (!DECL_IN_SYSTEM_HEADER (decl))
1178 diagnostic_for_decl (decl, msgid, &ap, !flag_pedantic_errors);
1182 /* Same as above but within the context FILE and LINE. */
1185 pedwarn_with_file_and_line VPARAMS ((const char *file, int line,
1186 const char *msgid, ...))
1188 #ifndef ANSI_PROTOTYPES
1194 diagnostic_context dc;
1196 VA_START (ap, msgid);
1198 #ifndef ANSI_PROTOTYPES
1199 file = va_arg (ap, const char *);
1200 line = va_arg (ap, int);
1201 msgid = va_arg (ap, const char *);
1204 set_diagnostic_context (&dc, msgid, &ap, file, line, !flag_pedantic_errors);
1205 report_diagnostic (&dc);
1209 /* Just apologize with MSGID. */
1212 sorry VPARAMS ((const char *msgid, ...))
1214 #ifndef ANSI_PROTOTYPES
1220 os = output_buffer_state (diagnostic_buffer);
1221 VA_START (ap, msgid);
1223 #ifndef ANSI_PROTOTYPES
1224 msgid = va_arg (ap, const char *);
1228 (diagnostic_buffer, context_as_prefix (input_filename, lineno, 0));
1229 output_printf (diagnostic_buffer, "sorry, not implemented: ");
1230 output_buffer_ptr_to_format_args (diagnostic_buffer) = ≈
1231 output_buffer_text_cursor (diagnostic_buffer) = msgid;
1232 output_format (diagnostic_buffer);
1233 finish_diagnostic ();
1234 output_buffer_state (diagnostic_buffer) = os;
1238 /* Called when the start of a function definition is parsed,
1239 this function prints on stderr the name of the function. */
1242 announce_function (decl)
1247 if (rtl_dump_and_exit)
1248 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl)));
1250 verbatim (" %s", (*decl_printable_name) (decl, 2));
1252 output_needs_newline (diagnostic_buffer) = 1;
1253 record_last_error_function ();
1257 /* The default function to print out name of current function that caused
1261 default_print_error_function (file)
1264 if (error_function_changed ())
1266 char *prefix = file ? build_message_string ("%s: ", file) : NULL;
1269 os = output_buffer_state (diagnostic_buffer);
1270 output_set_prefix (diagnostic_buffer, prefix);
1272 if (current_function_decl == NULL)
1273 output_add_string (diagnostic_buffer, "At top level:");
1276 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
1278 (diagnostic_buffer, "In method `%s':",
1279 (*decl_printable_name) (current_function_decl, 2));
1282 (diagnostic_buffer, "In function `%s':",
1283 (*decl_printable_name) (current_function_decl, 2));
1285 output_add_newline (diagnostic_buffer);
1287 record_last_error_function ();
1288 output_to_stream (diagnostic_buffer, stderr);
1289 output_buffer_state (diagnostic_buffer) = os;
1290 free ((char*) prefix);
1294 /* Prints out, if necessary, the name of the current function
1295 that caused an error. Called from all error and warning functions.
1296 We ignore the FILE parameter, as it cannot be relied upon. */
1299 report_error_function (file)
1300 const char *file ATTRIBUTE_UNUSED;
1302 report_problematic_module (diagnostic_buffer);
1303 (*print_error_function) (input_filename);
1307 error_with_file_and_line VPARAMS ((const char *file, int line,
1308 const char *msgid, ...))
1310 #ifndef ANSI_PROTOTYPES
1316 diagnostic_context dc;
1318 VA_START (ap, msgid);
1320 #ifndef ANSI_PROTOTYPES
1321 file = va_arg (ap, const char *);
1322 line = va_arg (ap, int);
1323 msgid = va_arg (ap, const char *);
1326 set_diagnostic_context (&dc, msgid, &ap, file, line, /* warn = */ 0);
1327 report_diagnostic (&dc);
1332 error_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1334 #ifndef ANSI_PROTOTYPES
1340 VA_START (ap, msgid);
1342 #ifndef ANSI_PROTOTYPES
1343 decl = va_arg (ap, tree);
1344 msgid = va_arg (ap, const char *);
1347 diagnostic_for_decl (decl, msgid, &ap, /* warn = */ 0);
1352 error_for_asm VPARAMS ((rtx insn, const char *msgid, ...))
1354 #ifndef ANSI_PROTOTYPES
1360 VA_START (ap, msgid);
1362 #ifndef ANSI_PROTOTYPES
1363 insn = va_arg (ap, rtx);
1364 msgid = va_arg (ap, const char *);
1367 diagnostic_for_asm (insn, msgid, &ap, /* warn = */ 0);
1372 error VPARAMS ((const char *msgid, ...))
1374 #ifndef ANSI_PROTOTYPES
1378 diagnostic_context dc;
1380 VA_START (ap, msgid);
1382 #ifndef ANSI_PROTOTYPES
1383 msgid = va_arg (ap, const char *);
1386 set_diagnostic_context
1387 (&dc, msgid, &ap, input_filename, lineno, /* warn = */ 0);
1388 report_diagnostic (&dc);
1392 /* Report a fatal error at the current line number. Allow a front end to
1393 intercept the message. */
1395 static void (*fatal_function) PARAMS((const char *, va_list *));
1397 /* Set the function to call when a fatal error occurs. */
1400 set_fatal_function (f)
1401 void (*f) PARAMS ((const char *, va_list *));
1407 fatal VPARAMS ((const char *msgid, ...))
1409 #ifndef ANSI_PROTOTYPES
1413 diagnostic_context dc;
1415 VA_START (ap, msgid);
1417 #ifndef ANSI_PROTOTYPES
1418 msgid = va_arg (ap, const char *);
1421 if (errorcount > 1 || sorrycount > 0)
1423 fprintf (stderr, "confused by earlier errors, bailing out\n");
1424 exit (FATAL_EXIT_CODE);
1427 if (fatal_function != 0)
1428 (*fatal_function) (_(msgid), &ap);
1430 set_diagnostic_context
1431 (&dc, msgid, &ap, input_filename, lineno, /* warn = */0);
1432 report_diagnostic (&dc);
1435 fprintf (stderr, "Please submit a full bug report, with preprocessed source if appropriate.\n");
1436 fprintf (stderr, "See %s for instructions.\n", GCCBUGURL);
1437 exit (FATAL_EXIT_CODE);
1441 _fatal_insn (msgid, insn, file, line, function)
1446 const char *function;
1448 error ("%s", msgid);
1450 fancy_abort (file, line, function);
1454 _fatal_insn_not_found (insn, file, line, function)
1458 const char *function;
1460 if (INSN_CODE (insn) < 0)
1461 _fatal_insn ("Unrecognizable insn:", insn, file, line, function);
1463 _fatal_insn ("Insn does not satisfy its constraints:",
1464 insn, file, line, function);
1468 warning_with_file_and_line VPARAMS ((const char *file, int line,
1469 const char *msgid, ...))
1471 #ifndef ANSI_PROTOTYPES
1477 diagnostic_context dc;
1479 VA_START (ap, msgid);
1481 #ifndef ANSI_PROTOTYPES
1482 file = va_arg (ap, const char *);
1483 line = va_arg (ap, int);
1484 msgid = va_arg (ap, const char *);
1487 set_diagnostic_context (&dc, msgid, &ap, file, line, /* warn = */ 1);
1488 report_diagnostic (&dc);
1493 warning_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1495 #ifndef ANSI_PROTOTYPES
1501 VA_START (ap, msgid);
1503 #ifndef ANSI_PROTOTYPES
1504 decl = va_arg (ap, tree);
1505 msgid = va_arg (ap, const char *);
1508 diagnostic_for_decl (decl, msgid, &ap, /* warn = */ 1);
1513 warning_for_asm VPARAMS ((rtx insn, const char *msgid, ...))
1515 #ifndef ANSI_PROTOTYPES
1521 VA_START (ap, msgid);
1523 #ifndef ANSI_PROTOTYPES
1524 insn = va_arg (ap, rtx);
1525 msgid = va_arg (ap, const char *);
1528 diagnostic_for_asm (insn, msgid, &ap, /* warn = */ 1);
1533 warning VPARAMS ((const char *msgid, ...))
1535 #ifndef ANSI_PROTOTYPES
1539 diagnostic_context dc;
1541 VA_START (ap, msgid);
1543 #ifndef ANSI_PROTOTYPES
1544 msgid = va_arg (ap, const char *);
1547 set_diagnostic_context
1548 (&dc, msgid, &ap, input_filename, lineno, /* warn = */ 1);
1549 report_diagnostic (&dc);
1553 /* Flush diagnostic_buffer content on stderr. */
1556 finish_diagnostic ()
1558 output_to_stream (diagnostic_buffer, stderr);
1559 clear_diagnostic_info (diagnostic_buffer);
1560 fputc ('\n', stderr);
1564 /* Helper subroutine of output_verbatim and verbatim. Do the approriate
1565 settings needed by BUFFER for a verbatim formatting. */
1568 output_do_verbatim (buffer, msg, args_ptr)
1569 output_buffer *buffer;
1575 os = output_buffer_state (buffer);
1576 output_prefix (buffer) = NULL;
1577 prefixing_policy (buffer) = DIAGNOSTICS_SHOW_PREFIX_NEVER;
1578 output_buffer_text_cursor (buffer) = msg;
1579 output_buffer_ptr_to_format_args (buffer) = args_ptr;
1580 output_set_maximum_length (buffer, 0);
1581 output_format (buffer);
1582 output_buffer_state (buffer) = os;
1585 /* Output MESSAGE verbatim into BUFFER. */
1588 output_verbatim VPARAMS ((output_buffer *buffer, const char *msg, ...))
1590 #ifndef ANSI_PROTOTYPES
1591 output_buffer *buffer;
1597 #ifndef ANSI_PROTOTYPES
1598 buffer = va_arg (ap, output_buffer *);
1599 msg = va_arg (ap, const char *);
1601 output_do_verbatim (buffer, msg, &ap);
1605 /* Same as above but use diagnostic_buffer. */
1608 verbatim VPARAMS ((const char *msg, ...))
1610 #ifndef ANSI_PROTOTYPES
1616 #ifndef ANSI_PROTOTYPES
1617 msg = va_arg (ap, const char *);
1619 output_do_verbatim (diagnostic_buffer, msg, &ap);
1620 output_to_stream (diagnostic_buffer, stderr);
1624 /* Report a diagnostic message (an error or a warning) as specified by
1625 DC. This function is *the* subroutine in terms of which front-ends
1626 should implement their specific diagnostic handling modules. The
1627 front-end independent format specifiers are exactly those described
1628 in the documentation of output_format. */
1631 report_diagnostic (dc)
1632 diagnostic_context *dc;
1636 if (diagnostic_lock++)
1639 if (count_error (diagnostic_is_warning (dc)))
1641 os = output_buffer_state (diagnostic_buffer);
1642 diagnostic_msg = diagnostic_message (dc);
1643 diagnostic_args = diagnostic_argument_list (dc);
1644 (*diagnostic_starter (dc)) (diagnostic_buffer, dc);
1645 output_format (diagnostic_buffer);
1646 (*diagnostic_finalizer (dc)) (diagnostic_buffer, dc);
1647 finish_diagnostic ();
1648 output_buffer_state (diagnostic_buffer) = os;
1654 /* Inform the user that an error occurred while trying to report some
1655 other error. This indicates catastrophic internal inconsistencies,
1656 so give up now. But do try to flush out the previous error. */
1661 if (diagnostic_lock < 3)
1662 finish_diagnostic ();
1664 fatal ("Internal compiler error: Error reporting routines re-entered.");
1667 /* Given a partial pathname as input, return another pathname that
1668 shares no directory elements with the pathname of __FILE__. This
1669 is used by fancy_abort() to print `Internal compiler error in expr.c'
1670 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1673 trim_filename (name)
1676 static const char this_file[] = __FILE__;
1677 const char *p = name, *q = this_file;
1679 /* First skip any "../" in each filename. This allows us to give a proper
1680 reference to a file in a subdirectory. */
1681 while (p[0] == '.' && p[1] == '.'
1682 && (p[2] == DIR_SEPARATOR
1683 #ifdef DIR_SEPARATOR_2
1684 || p[2] == DIR_SEPARATOR_2
1689 while (q[0] == '.' && q[1] == '.'
1690 && (q[2] == DIR_SEPARATOR
1691 #ifdef DIR_SEPARATOR_2
1692 || p[2] == DIR_SEPARATOR_2
1697 /* Now skip any parts the two filenames have in common. */
1698 while (*p == *q && *p != 0 && *q != 0)
1701 /* Now go backwards until the previous directory separator. */
1702 while (p > name && p[-1] != DIR_SEPARATOR
1703 #ifdef DIR_SEPARATOR_2
1704 && p[-1] != DIR_SEPARATOR_2
1712 /* Report an internal compiler error in a friendly manner and without
1716 fancy_abort (file, line, function)
1719 const char *function;
1721 fatal ("Internal compiler error in %s, at %s:%d",
1722 function, trim_filename (file), line);
1725 /* Setup DC for reporting a diagnostic MESSAGE (an error or a WARNING),
1726 using arguments pointed to by ARGS_PTR, issued at a location specified
1727 by FILE and LINE. */
1730 set_diagnostic_context (dc, message, args_ptr, file, line, warn)
1731 diagnostic_context *dc;
1732 const char *message;
1738 memset (dc, 0, sizeof (diagnostic_context));
1739 diagnostic_message (dc) = message;
1740 diagnostic_argument_list (dc) = args_ptr;
1741 diagnostic_file_location (dc) = file;
1742 diagnostic_line_location (dc) = line;
1743 diagnostic_is_warning (dc) = warn;
1744 diagnostic_starter (dc) = lang_diagnostic_starter;
1745 diagnostic_finalizer (dc) = lang_diagnostic_finalizer;
1749 report_problematic_module (buffer)
1750 output_buffer *buffer;
1752 struct file_stack *p;
1754 if (output_needs_newline (buffer))
1756 output_add_newline (buffer);
1757 output_needs_newline (buffer) = 0;
1760 if (input_file_stack && input_file_stack->next != 0
1761 && error_module_changed ())
1763 for (p = input_file_stack->next; p; p = p->next)
1764 if (p == input_file_stack->next)
1766 (buffer, "In file included from %s:%d", p->name, p->line);
1769 (buffer, ",\n from %s:%d", p->name, p->line);
1770 output_verbatim (buffer, ":\n");
1771 record_last_error_module ();
1776 default_diagnostic_starter (buffer, dc)
1777 output_buffer *buffer;
1778 diagnostic_context *dc;
1780 report_error_function (diagnostic_file_location (dc));
1781 output_set_prefix (buffer,
1782 context_as_prefix (diagnostic_file_location (dc),
1783 diagnostic_line_location (dc),
1784 diagnostic_is_warning (dc)));
1788 default_diagnostic_finalizer (buffer, dc)
1789 output_buffer *buffer;
1790 diagnostic_context *dc __attribute__((__unused__));
1792 output_destroy_prefix (buffer);