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 politics. */
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. */
160 error_module_changed ()
162 return last_error_tick != input_file_stack_tick;
165 /* Remember current file as being the most recent file involved in a
166 diagnostic message. */
168 record_last_error_module ()
170 last_error_tick = input_file_stack_tick;
173 /* Same as error_module_changed, but for function. */
175 error_function_changed ()
177 return last_error_function != current_function_decl;
180 /* 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 ideal_line_wrap_cutoff (buffer) = maximum_length;
340 prefixing_policy (buffer) = current_prefixing_rule;
341 output_set_prefix (buffer, prefix);
342 output_text_length (buffer) = 0;
343 clear_diagnostic_info (buffer);
346 /* Initialize BUFFER with a NULL prefix and current diagnostic message
349 default_initialize_buffer (buffer)
350 output_buffer *buffer;
352 init_output_buffer (buffer, NULL, diagnostic_message_length_per_line);
355 /* Recompute diagnostic_buffer's attributes to reflect any change
356 in diagnostic formatting global options. */
359 reshape_diagnostic_buffer ()
361 ideal_line_wrap_cutoff (diagnostic_buffer) =
362 diagnostic_message_length_per_line;
363 prefixing_policy (diagnostic_buffer) = current_prefixing_rule;
364 set_real_maximum_length (diagnostic_buffer);
367 /* Reinitialize BUFFER. */
369 output_clear (buffer)
370 output_buffer *buffer;
372 output_clear_message_text (buffer);
373 clear_diagnostic_info (buffer);
376 /* Finishes to construct a NULL-terminated character string representing
377 the BUFFERed message. */
380 output_finalize_message (buffer)
381 output_buffer *buffer;
383 obstack_1grow (&buffer->obstack, '\0');
384 return output_message_text (buffer);
388 flush_diagnostic_buffer ()
390 output_to_stream (diagnostic_buffer, stderr);
394 /* Return the amount of characters BUFFER can accept to
398 output_space_left (buffer)
399 const output_buffer *buffer;
401 return line_wrap_cutoff (buffer) - output_text_length (buffer);
404 /* Write out BUFFER's prefix. */
407 output_emit_prefix (buffer)
408 output_buffer *buffer;
410 if (output_prefix (buffer) != NULL)
412 switch (prefixing_policy (buffer))
415 case DIAGNOSTICS_SHOW_PREFIX_NEVER:
418 case DIAGNOSTICS_SHOW_PREFIX_ONCE:
419 if (prefix_was_emitted_for (buffer))
421 output_indent (buffer);
424 output_indentation (buffer) += 3;
427 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
429 int prefix_length = strlen (output_prefix (buffer));
430 output_append_r (buffer, output_prefix (buffer), prefix_length);
431 prefix_was_emitted_for (buffer) = 1;
438 /* Have BUFFER start a new line. */
441 output_add_newline (buffer)
442 output_buffer *buffer;
444 obstack_1grow (&buffer->obstack, '\n');
445 output_text_length (buffer) = 0;
448 /* Appends a character to BUFFER. */
451 output_add_character (buffer, c)
452 output_buffer *buffer;
455 if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
456 output_add_newline (buffer);
457 obstack_1grow (&buffer->obstack, c);
458 ++output_text_length (buffer);
461 /* Adds a space to BUFFER. */
464 output_add_space (buffer)
465 output_buffer *buffer;
467 if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
469 output_add_newline (buffer);
472 obstack_1grow (&buffer->obstack, ' ');
473 ++output_text_length (buffer);
476 /* These functions format an INTEGER into BUFFER as suggested by their
480 output_decimal (buffer, i)
481 output_buffer *buffer;
484 output_formatted_integer (buffer, "%d", i);
488 output_long_decimal (buffer, i)
489 output_buffer *buffer;
492 output_formatted_integer (buffer, "%ld", i);
496 output_unsigned_decimal (buffer, i)
497 output_buffer *buffer;
500 output_formatted_integer (buffer, "%u", i);
504 output_long_unsigned_decimal (buffer, i)
505 output_buffer *buffer;
508 output_formatted_integer (buffer, "%lu", i);
512 output_octal (buffer, i)
513 output_buffer *buffer;
516 output_formatted_integer (buffer, "%o", i);
520 output_long_octal (buffer, i)
521 output_buffer *buffer;
524 output_formatted_integer (buffer, "%lo", i);
528 output_hexadecimal (buffer, i)
529 output_buffer *buffer;
532 output_formatted_integer (buffer, "%x", i);
536 output_long_hexadecimal (buffer, i)
537 output_buffer *buffer;
540 output_formatted_integer (buffer, "%lx", i);
543 /* Append to BUFFER a string specified by its STARTING character
547 output_append_r (buffer, start, length)
548 output_buffer *buffer;
552 obstack_grow (&buffer->obstack, start, length);
553 output_text_length (buffer) += length;
556 /* Append a string deliminated by START and END to BUFFER. No wrapping is
557 done. However, if beginning a new line then emit output_prefix (BUFFER)
558 and skip any leading whitespace if appropriate. The caller must ensure
559 that it is safe to do so. */
562 output_append (buffer, start, end)
563 output_buffer *buffer;
567 /* Emit prefix and skip whitespace if we're starting a new line. */
568 if (is_starting_newline (buffer))
570 output_emit_prefix (buffer);
571 if (output_is_line_wrapping (buffer))
572 while (start != end && *start == ' ')
575 output_append_r (buffer, start, end - start);
579 output_indent (buffer)
580 output_buffer *buffer;
582 int n = output_indentation (buffer);
585 for (i = 0; i < n; ++i)
586 output_add_character (buffer, ' ');
589 /* Wrap a text delimited by START and END into BUFFER. */
592 wrap_text (buffer, start, end)
593 output_buffer *buffer;
597 int is_wrapping = output_is_line_wrapping (buffer);
601 /* Dump anything bodered by whitespaces. */
603 const char *p = start;
604 while (p != end && *p != ' ' && *p != '\n')
606 if (is_wrapping && p - start >= output_space_left (buffer))
607 output_add_newline (buffer);
608 output_append (buffer, start, p);
612 if (start != end && *start == ' ')
614 output_add_space (buffer);
617 if (start != end && *start == '\n')
619 output_add_newline (buffer);
625 /* Same as wrap_text but wrap text only when in line-wrapping mode. */
627 maybe_wrap_text (buffer, start, end)
628 output_buffer *buffer;
632 if (output_is_line_wrapping (buffer))
633 wrap_text (buffer, start, end);
635 output_append (buffer, start, end);
639 /* Append a STRING to BUFFER; the STRING maybe be line-wrapped if in
643 output_add_string (buffer, str)
644 output_buffer *buffer;
647 maybe_wrap_text (buffer, str, str + (str ? strlen (str) : 0));
650 /* Flush the content of BUFFER onto FILE and reinitialize BUFFER. */
653 output_to_stream (buffer, file)
654 output_buffer *buffer;
657 const char *text = output_finalize_message (buffer);
659 output_clear_message_text (buffer);
662 /* Format a message pointed to by output_buffer_text_cursor (BUFFER) using
663 output_buffer_format_args (BUFFER) as appropriate. The following format
664 specifiers are recognized as being language independent:
665 %d, %i: (signed) integer in base ten.
666 %u: unsigned integer in base ten.
667 %o: unsigned integer in base eight.
668 %x: unsigned integer in base sixteen.
669 %ld, %li, %lo, %lu, %lx: long versions of the above.
673 %*.s: a substring the length of which is specified by an integer. */
676 output_format (buffer)
677 output_buffer *buffer;
679 for (; *output_buffer_text_cursor (buffer);
680 ++output_buffer_text_cursor (buffer))
682 int long_integer = 0;
686 const char *p = output_buffer_text_cursor (buffer);
687 while (*p && *p != '%')
689 wrap_text (buffer, output_buffer_text_cursor (buffer), p);
690 output_buffer_text_cursor (buffer) = p;
693 if (!*output_buffer_text_cursor (buffer))
696 /* We got a '%'. Let's see what happens. Record whether we're
697 parsing a long integer format specifier. */
698 if (*++output_buffer_text_cursor (buffer) == 'l')
701 ++output_buffer_text_cursor (buffer);
704 /* Handle %c, %d, %i, %ld, %li, %lo, %lu, %lx, %o, %s, %u,
705 %x, %.*s; %%. And nothing else. Front-ends should install
706 printers to grok language specific format specifiers. */
707 switch (*output_buffer_text_cursor (buffer))
711 (buffer, va_arg (output_buffer_format_args (buffer), int));
718 (buffer, va_arg (output_buffer_format_args (buffer), long int));
721 (buffer, va_arg (output_buffer_format_args (buffer), int));
726 output_long_octal (buffer,
727 va_arg (output_buffer_format_args (buffer),
730 output_octal (buffer,
731 va_arg (output_buffer_format_args (buffer),
736 output_add_string (buffer,
737 va_arg (output_buffer_format_args (buffer),
743 output_long_unsigned_decimal
744 (buffer, va_arg (output_buffer_format_args (buffer),
747 output_unsigned_decimal
748 (buffer, va_arg (output_buffer_format_args (buffer),
754 output_long_hexadecimal
755 (buffer, va_arg (output_buffer_format_args (buffer),
759 (buffer, va_arg (output_buffer_format_args (buffer),
764 output_add_character (buffer, '%');
771 /* We handle no precision specifier but `%.*s'. */
772 if (*++output_buffer_text_cursor (buffer) != '*')
774 else if (*++output_buffer_text_cursor (buffer) != 's')
776 n = va_arg (output_buffer_format_args (buffer), int);
777 s = va_arg (output_buffer_format_args (buffer), const char *);
778 output_append (buffer, s, s + n);
783 if (! lang_printer || !(*lang_printer) (buffer))
785 /* Hmmm. The front-end failed to install a format translator
786 but called us with an unrecognized format. Sorry. */
794 vbuild_message_string (msgid, ap)
800 vasprintf (&str, msgid, ap);
804 /* Return a malloc'd string containing MSGID formatted a la
805 printf. The caller is reponsible for freeing the memory. */
808 build_message_string VPARAMS ((const char *msgid, ...))
810 #ifndef ANSI_PROTOTYPES
816 VA_START (ap, msgid);
818 #ifndef ANSI_PROTOTYPES
819 msgid = va_arg (ap, const char *);
822 str = vbuild_message_string (msgid, ap);
829 /* Return a malloc'd string describing a location. The caller is
830 responsible for freeing the memory. */
832 context_as_prefix (file, line, warn)
840 return build_message_string ("%s:%d: warning: ", file, line);
842 return build_message_string ("%s:%d: ", file, line);
847 return build_message_string ("%s: warning: ", progname);
849 return build_message_string ("%s: ", progname);
853 /* Same as context_as_prefix, but only the source FILE is given. */
855 file_name_as_prefix (f)
858 return build_message_string ("%s: ", f);
861 /* Format a MESSAGE into BUFFER. Automatically wrap lines. */
864 output_do_printf (buffer, msgid)
865 output_buffer *buffer;
868 char *message = vbuild_message_string (msgid,
869 output_buffer_format_args (buffer));
871 wrap_text (buffer, message, message + strlen (message));
876 /* Format a message into BUFFER a la printf. */
879 output_printf VPARAMS ((struct output_buffer *buffer, const char *msgid, ...))
881 #ifndef ANSI_PROTOTYPES
882 struct output_buffer *buffer;
888 VA_START (ap, msgid);
889 #ifndef ANSI_PROTOTYPES
890 buffer = va_arg (ap, output_buffer *);
891 msgid = va_arg (ap, const char *);
893 old_args = output_buffer_ptr_to_format_args (buffer);
894 output_buffer_ptr_to_format_args (buffer) = ≈
895 output_do_printf (buffer, msgid);
896 output_buffer_ptr_to_format_args (buffer) = old_args;
900 /* Print the message MSGID in FILE. */
903 vnotice (file, msgid, ap)
908 vfprintf (file, _(msgid), ap);
911 /* Print a message relevant to the given DECL. */
914 format_with_decl (buffer, decl)
915 output_buffer *buffer;
920 /* Do magic to get around lack of varargs support for insertion
921 of arguments into existing list. We know that the decl is first;
922 we ass_u_me that it will be printed with "%s". */
923 for (p = output_buffer_text_cursor (buffer); *p; ++p)
929 else if (*(p + 1) != 's')
936 /* Print the left-hand substring. */
937 maybe_wrap_text (buffer, output_buffer_text_cursor (buffer), p);
939 if (*p == '%') /* Print the name. */
941 const char *n = (DECL_NAME (decl)
942 ? (*decl_printable_name) (decl, 2)
943 : _("((anonymous))"));
944 output_add_string (buffer, n);
948 if (ISALPHA (*(p - 1) & 0xFF))
953 if (*p) /* Print the rest of the message. */
955 output_buffer_text_cursor (buffer) = p;
956 output_format (buffer);
960 /* Figure file and line of the given INSN. */
963 file_and_line_for_asm (insn, pfile, pline)
968 rtx body = PATTERN (insn);
971 /* Find the (or one of the) ASM_OPERANDS in the insn. */
972 if (GET_CODE (body) == SET && GET_CODE (SET_SRC (body)) == ASM_OPERANDS)
973 asmop = SET_SRC (body);
974 else if (GET_CODE (body) == ASM_OPERANDS)
976 else if (GET_CODE (body) == PARALLEL
977 && GET_CODE (XVECEXP (body, 0, 0)) == SET)
978 asmop = SET_SRC (XVECEXP (body, 0, 0));
979 else if (GET_CODE (body) == PARALLEL
980 && GET_CODE (XVECEXP (body, 0, 0)) == ASM_OPERANDS)
981 asmop = XVECEXP (body, 0, 0);
987 *pfile = ASM_OPERANDS_SOURCE_FILE (asmop);
988 *pline = ASM_OPERANDS_SOURCE_LINE (asmop);
992 *pfile = input_filename;
997 /* Report a diagnostic MESSAGE (an errror or a WARNING) at the line number
998 of the insn INSN. This is used only when INSN is an `asm' with operands,
999 and each ASM_OPERANDS records its own source file and line. */
1002 diagnostic_for_asm (insn, msg, args_ptr, warn)
1008 diagnostic_context dc;
1010 set_diagnostic_context (&dc, msg, args_ptr, NULL, 0, warn);
1011 file_and_line_for_asm (insn, &diagnostic_file_location (&dc),
1012 &diagnostic_line_location (&dc));
1013 report_diagnostic (&dc);
1016 /* Report a diagnostic MESSAGE at the declaration DECL.
1017 MSG is a format string which uses %s to substitute the declaration
1018 name; subsequent substitutions are a la output_format. */
1021 diagnostic_for_decl (decl, msg, args_ptr, warn)
1029 if (diagnostic_lock++)
1032 if (count_error (warn))
1034 os = output_buffer_state (diagnostic_buffer);
1035 report_error_function (DECL_SOURCE_FILE (decl));
1037 (diagnostic_buffer, context_as_prefix
1038 (DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl), warn));
1039 output_buffer_ptr_to_format_args (diagnostic_buffer) = args_ptr;
1040 output_buffer_text_cursor (diagnostic_buffer) = msg;
1041 format_with_decl (diagnostic_buffer, decl);
1042 finish_diagnostic ();
1043 output_destroy_prefix (diagnostic_buffer);
1045 output_buffer_state (diagnostic_buffer) = os;
1051 /* Count an error or warning. Return 1 if the message should be printed. */
1054 count_error (warningp)
1058 && (inhibit_warnings
1059 || (in_system_header && !warn_system_headers)))
1062 if (warningp && !warnings_are_errors)
1066 static int warning_message = 0;
1068 if (warningp && !warning_message)
1070 verbatim ("%s: warnings being treated as errors\n", progname);
1071 warning_message = 1;
1079 /* Print a diagnistic MSGID on FILE. */
1082 fnotice VPARAMS ((FILE *file, const char *msgid, ...))
1084 #ifndef ANSI_PROTOTYPES
1090 VA_START (ap, msgid);
1092 #ifndef ANSI_PROTOTYPES
1093 file = va_arg (ap, FILE *);
1094 msgid = va_arg (ap, const char *);
1097 vnotice (file, msgid, ap);
1102 /* Print a fatal error message. NAME is the text.
1103 Also include a system error message based on `errno'. */
1106 pfatal_with_name (name)
1109 fprintf (stderr, "%s: ", progname);
1111 exit (FATAL_EXIT_CODE);
1115 fatal_io_error (name)
1118 verbatim ("%s: %s: I/O error\n", progname, name);
1119 exit (FATAL_EXIT_CODE);
1122 /* Issue a pedantic warning MSGID. */
1125 pedwarn VPARAMS ((const char *msgid, ...))
1127 #ifndef ANSI_PROTOTYPES
1131 diagnostic_context dc;
1133 VA_START (ap, msgid);
1135 #ifndef ANSI_PROTOTYPES
1136 msgid = va_arg (ap, const char *);
1139 set_diagnostic_context
1140 (&dc, msgid, &ap, input_filename, lineno, !flag_pedantic_errors);
1141 report_diagnostic (&dc);
1145 /* Issue a pedantic waring about DECL. */
1148 pedwarn_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1150 #ifndef ANSI_PROTOTYPES
1156 VA_START (ap, msgid);
1158 #ifndef ANSI_PROTOTYPES
1159 decl = va_arg (ap, tree);
1160 msgid = va_arg (ap, const char *);
1162 /* We don't want -pedantic-errors to cause the compilation to fail from
1163 "errors" in system header files. Sometimes fixincludes can't fix what's
1164 broken (eg: unsigned char bitfields - fixing it may change the alignment
1165 which will cause programs to mysteriously fail because the C library
1166 or kernel uses the original layout). There's no point in issuing a
1167 warning either, it's just unnecessary noise. */
1168 if (!DECL_IN_SYSTEM_HEADER (decl))
1169 diagnostic_for_decl (decl, msgid, &ap, !flag_pedantic_errors);
1173 /* Same as above but within the context FILE and LINE. */
1176 pedwarn_with_file_and_line VPARAMS ((const char *file, int line,
1177 const char *msgid, ...))
1179 #ifndef ANSI_PROTOTYPES
1185 diagnostic_context dc;
1187 VA_START (ap, msgid);
1189 #ifndef ANSI_PROTOTYPES
1190 file = va_arg (ap, const char *);
1191 line = va_arg (ap, int);
1192 msgid = va_arg (ap, const char *);
1195 set_diagnostic_context (&dc, msgid, &ap, file, line, !flag_pedantic_errors);
1196 report_diagnostic (&dc);
1200 /* Just apologize with MSGID. */
1203 sorry VPARAMS ((const char *msgid, ...))
1205 #ifndef ANSI_PROTOTYPES
1211 os = output_buffer_state (diagnostic_buffer);
1212 VA_START (ap, msgid);
1214 #ifndef ANSI_PROTOTYPES
1215 msgid = va_arg (ap, const char *);
1219 (diagnostic_buffer, context_as_prefix (input_filename, lineno, 0));
1220 output_printf (diagnostic_buffer, "sorry, not implemented: ");
1221 output_buffer_ptr_to_format_args (diagnostic_buffer) = ≈
1222 output_buffer_text_cursor (diagnostic_buffer) = msgid;
1223 output_format (diagnostic_buffer);
1224 finish_diagnostic ();
1225 output_buffer_state (diagnostic_buffer) = os;
1229 /* Called when the start of a function definition is parsed,
1230 this function prints on stderr the name of the function. */
1233 announce_function (decl)
1238 if (rtl_dump_and_exit)
1239 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl)));
1241 verbatim (" %s", (*decl_printable_name) (decl, 2));
1243 output_needs_newline (diagnostic_buffer) = 1;
1244 record_last_error_function ();
1248 /* The default function to print out name of current function that caused
1252 default_print_error_function (file)
1255 if (error_function_changed ())
1257 char *prefix = file ? build_message_string ("%s: ", file) : NULL;
1260 os = output_buffer_state (diagnostic_buffer);
1261 output_set_prefix (diagnostic_buffer, prefix);
1263 if (current_function_decl == NULL)
1264 output_add_string (diagnostic_buffer, "At top level:");
1267 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
1269 (diagnostic_buffer, "In method `%s':",
1270 (*decl_printable_name) (current_function_decl, 2));
1273 (diagnostic_buffer, "In function `%s':",
1274 (*decl_printable_name) (current_function_decl, 2));
1276 output_add_newline (diagnostic_buffer);
1278 record_last_error_function ();
1279 output_to_stream (diagnostic_buffer, stderr);
1280 output_buffer_state (diagnostic_buffer) = os;
1281 free ((char*) prefix);
1285 /* Prints out, if necessary, the name of the current function
1286 that caused an error. Called from all error and warning functions.
1287 We ignore the FILE parameter, as it cannot be relied upon. */
1290 report_error_function (file)
1291 const char *file ATTRIBUTE_UNUSED;
1293 report_problematic_module (diagnostic_buffer);
1294 (*print_error_function) (input_filename);
1298 error_with_file_and_line VPARAMS ((const char *file, int line,
1299 const char *msgid, ...))
1301 #ifndef ANSI_PROTOTYPES
1307 diagnostic_context dc;
1309 VA_START (ap, msgid);
1311 #ifndef ANSI_PROTOTYPES
1312 file = va_arg (ap, const char *);
1313 line = va_arg (ap, int);
1314 msgid = va_arg (ap, const char *);
1317 set_diagnostic_context (&dc, msgid, &ap, file, line, /* warn = */ 0);
1318 report_diagnostic (&dc);
1323 error_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1325 #ifndef ANSI_PROTOTYPES
1331 VA_START (ap, msgid);
1333 #ifndef ANSI_PROTOTYPES
1334 decl = va_arg (ap, tree);
1335 msgid = va_arg (ap, const char *);
1338 diagnostic_for_decl (decl, msgid, &ap, /* warn = */ 0);
1343 error_for_asm VPARAMS ((rtx insn, const char *msgid, ...))
1345 #ifndef ANSI_PROTOTYPES
1351 VA_START (ap, msgid);
1353 #ifndef ANSI_PROTOTYPES
1354 insn = va_arg (ap, rtx);
1355 msgid = va_arg (ap, const char *);
1358 diagnostic_for_asm (insn, msgid, &ap, /* warn = */ 0);
1363 error VPARAMS ((const char *msgid, ...))
1365 #ifndef ANSI_PROTOTYPES
1369 diagnostic_context dc;
1371 VA_START (ap, msgid);
1373 #ifndef ANSI_PROTOTYPES
1374 msgid = va_arg (ap, const char *);
1377 set_diagnostic_context
1378 (&dc, msgid, &ap, input_filename, lineno, /* warn = */ 0);
1379 report_diagnostic (&dc);
1383 /* Report a fatal error at the current line number. Allow a front end to
1384 intercept the message. */
1386 static void (*fatal_function) PARAMS((const char *, va_list *));
1388 /* Set the function to call when a fatal error occurs. */
1391 set_fatal_function (f)
1392 void (*f) PARAMS ((const char *, va_list *));
1398 fatal VPARAMS ((const char *msgid, ...))
1400 #ifndef ANSI_PROTOTYPES
1404 diagnostic_context dc;
1406 VA_START (ap, msgid);
1408 #ifndef ANSI_PROTOTYPES
1409 msgid = va_arg (ap, const char *);
1412 if (fatal_function != 0)
1413 (*fatal_function) (_(msgid), &ap);
1415 set_diagnostic_context
1416 (&dc, msgid, &ap, input_filename, lineno, /* warn = */0);
1417 report_diagnostic (&dc);
1419 exit (FATAL_EXIT_CODE);
1423 _fatal_insn (msgid, insn, file, line, function)
1428 const char *function;
1430 error ("%s", msgid);
1432 fancy_abort (file, line, function);
1436 _fatal_insn_not_found (insn, file, line, function)
1440 const char *function;
1442 if (INSN_CODE (insn) < 0)
1443 _fatal_insn ("Unrecognizable insn:", insn, file, line, function);
1445 _fatal_insn ("Insn does not satisfy its constraints:",
1446 insn, file, line, function);
1450 warning_with_file_and_line VPARAMS ((const char *file, int line,
1451 const char *msgid, ...))
1453 #ifndef ANSI_PROTOTYPES
1459 diagnostic_context dc;
1461 VA_START (ap, msgid);
1463 #ifndef ANSI_PROTOTYPES
1464 file = va_arg (ap, const char *);
1465 line = va_arg (ap, int);
1466 msgid = va_arg (ap, const char *);
1469 set_diagnostic_context (&dc, msgid, &ap, file, line, /* warn = */ 1);
1470 report_diagnostic (&dc);
1475 warning_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1477 #ifndef ANSI_PROTOTYPES
1483 VA_START (ap, msgid);
1485 #ifndef ANSI_PROTOTYPES
1486 decl = va_arg (ap, tree);
1487 msgid = va_arg (ap, const char *);
1490 diagnostic_for_decl (decl, msgid, &ap, /* warn = */ 1);
1495 warning_for_asm VPARAMS ((rtx insn, const char *msgid, ...))
1497 #ifndef ANSI_PROTOTYPES
1503 VA_START (ap, msgid);
1505 #ifndef ANSI_PROTOTYPES
1506 insn = va_arg (ap, rtx);
1507 msgid = va_arg (ap, const char *);
1510 diagnostic_for_asm (insn, msgid, &ap, /* warn = */ 1);
1515 warning VPARAMS ((const char *msgid, ...))
1517 #ifndef ANSI_PROTOTYPES
1521 diagnostic_context dc;
1523 VA_START (ap, msgid);
1525 #ifndef ANSI_PROTOTYPES
1526 msgid = va_arg (ap, const char *);
1529 set_diagnostic_context
1530 (&dc, msgid, &ap, input_filename, lineno, /* warn = */ 1);
1531 report_diagnostic (&dc);
1535 /* Flush diagnostic_buffer content on stderr. */
1537 finish_diagnostic ()
1539 output_to_stream (diagnostic_buffer, stderr);
1540 clear_diagnostic_info (diagnostic_buffer);
1541 fputc ('\n', stderr);
1545 /* Helper subroutine of output_verbatim and verbatim. Do the approriate
1546 settings needed by BUFFER for a verbatim formatting. */
1548 output_do_verbatim (buffer, msg, args_ptr)
1549 output_buffer *buffer;
1555 os = output_buffer_state (buffer);
1556 output_prefix (buffer) = NULL;
1557 prefixing_policy (buffer) = DIAGNOSTICS_SHOW_PREFIX_NEVER;
1558 output_buffer_text_cursor (buffer) = msg;
1559 output_buffer_ptr_to_format_args (buffer) = args_ptr;
1560 output_set_maximum_length (buffer, 0);
1561 output_format (buffer);
1562 output_buffer_state (buffer) = os;
1565 /* Output MESSAGE verbatim into BUFFER. */
1567 output_verbatim VPARAMS ((output_buffer *buffer, const char *msg, ...))
1569 #ifndef ANSI_PROTOTYPES
1570 output_buffer *buffer;
1576 #ifndef ANSI_PROTOTYPES
1577 buffer = va_arg (ap, output_buffer *);
1578 msg = va_arg (ap, const char *);
1580 output_do_verbatim (buffer, msg, &ap);
1584 /* Same as above but use diagnostic_buffer. */
1586 verbatim VPARAMS ((const char *msg, ...))
1588 #ifndef ANSI_PROTOTYPES
1594 #ifndef ANSI_PROTOTYPES
1595 msg = va_arg (ap, const char *);
1597 output_do_verbatim (diagnostic_buffer, msg, &ap);
1598 output_to_stream (diagnostic_buffer, stderr);
1602 /* Report a diagnostic message (an error or a warning) as specified by
1603 DC. This function is *the* subroutine in terms of which front-ends
1604 should implement their specific diagnostic handling modules. The
1605 front-end independent format specifiers are exactly those described
1606 in the documentation of output_format. */
1608 report_diagnostic (dc)
1609 diagnostic_context *dc;
1613 if (diagnostic_lock++)
1616 if (count_error (diagnostic_is_warning (dc)))
1618 os = output_buffer_state (diagnostic_buffer);
1619 diagnostic_msg = diagnostic_message (dc);
1620 diagnostic_args = diagnostic_argument_list (dc);
1621 (*diagnostic_starter (dc)) (diagnostic_buffer, dc);
1622 output_format (diagnostic_buffer);
1623 (*diagnostic_finalizer (dc)) (diagnostic_buffer, dc);
1624 finish_diagnostic ();
1625 output_buffer_state (diagnostic_buffer) = os;
1631 /* Inform the user that an error occurred while trying to report some
1632 other error. This indicates catastrophic internal inconsistencies,
1633 so give up now. But do try to flush out the previous error. */
1637 if (diagnostic_lock < 3)
1638 finish_diagnostic ();
1641 "Internal compiler error: Error reporting routines re-entered.\n\
1642 Please submit a full bug report.\n\
1643 See %s for instructions.\n", GCCBUGURL);
1645 exit (FATAL_EXIT_CODE);
1648 /* Given a partial pathname as input, return another pathname that
1649 shares no directory elements with the pathname of __FILE__. This
1650 is used by fancy_abort() to print `Internal compiler error in expr.c'
1651 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1653 trim_filename (name)
1656 static const char this_file[] = __FILE__;
1657 const char *p = name, *q = this_file;
1659 while (*p == *q && *p != 0 && *q != 0) p++, q++;
1660 while (p > name && p[-1] != DIR_SEPARATOR
1661 #ifdef DIR_SEPARATOR_2
1662 && p[-1] != DIR_SEPARATOR_2
1670 /* Report an internal compiler error in a friendly manner and without
1674 fancy_abort (file, line, function)
1677 const char *function;
1680 "Internal compiler error in %s, at %s:%d\n\
1681 Please submit a full bug report.\n\
1682 See %s for instructions.",
1683 function, trim_filename (file), line, GCCBUGURL);
1686 /* Setup DC for reporting a diagnostic MESSAGE (an error of a WARNING),
1687 using arguments pointed to by ARGS_PTR, issued at a location specified
1688 by FILE and LINE. */
1690 set_diagnostic_context (dc, message, args_ptr, file, line, warn)
1691 diagnostic_context *dc;
1692 const char *message;
1698 memset (dc, 0, sizeof (diagnostic_context));
1699 diagnostic_message (dc) = message;
1700 diagnostic_argument_list (dc) = args_ptr;
1701 diagnostic_file_location (dc) = file;
1702 diagnostic_line_location (dc) = line;
1703 diagnostic_is_warning (dc) = warn;
1704 diagnostic_starter (dc) = lang_diagnostic_starter;
1705 diagnostic_finalizer (dc) = lang_diagnostic_finalizer;
1709 report_problematic_module (buffer)
1710 output_buffer *buffer;
1712 struct file_stack *p;
1714 if (output_needs_newline (buffer))
1716 output_add_newline (buffer);
1717 output_needs_newline (buffer) = 0;
1720 if (input_file_stack && input_file_stack->next != 0
1721 && error_function_changed ())
1723 for (p = input_file_stack->next; p; p = p->next)
1724 if (p == input_file_stack->next)
1726 (buffer, "In file included from %s:%d", p->name, p->line);
1729 (buffer, ",\n from %s:%d", p->name, p->line);
1730 output_verbatim (buffer, ":\n");
1731 record_last_error_function ();
1736 default_diagnostic_starter (buffer, dc)
1737 output_buffer *buffer;
1738 diagnostic_context *dc;
1740 report_error_function (diagnostic_file_location (dc));
1741 output_set_prefix (buffer,
1742 context_as_prefix (diagnostic_file_location (dc),
1743 diagnostic_line_location (dc),
1744 diagnostic_is_warning (dc)));
1748 default_diagnostic_finalizer (buffer, dc)
1749 output_buffer *buffer;
1750 diagnostic_context *dc __attribute__((__unused__));
1752 output_destroy_prefix (buffer);