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 ((BUFFER)->digit_buffer, FORMAT, INTEGER); \
48 output_add_string (BUFFER, (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 prefix_was_emitted_for(BUFFER) (BUFFER)->state.emitted_prefix_p
56 #define output_buffer_ptr_to_format_args(BUFFER) (BUFFER)->state.format_args
58 #define diagnostic_args output_buffer_ptr_to_format_args (diagnostic_buffer)
59 #define diagnostic_msg output_buffer_text_cursor (diagnostic_buffer)
62 static void finish_diagnostic PARAMS ((void));
63 static void output_do_verbatim PARAMS ((output_buffer *,
64 const char *, va_list *));
65 static void output_buffer_to_stream PARAMS ((output_buffer *));
66 static void output_format PARAMS ((output_buffer *));
67 static void output_indent PARAMS ((output_buffer *));
69 static char *vbuild_message_string PARAMS ((const char *, va_list))
70 ATTRIBUTE_PRINTF (1, 0);
71 static char *build_message_string PARAMS ((const char *, ...))
73 static void output_do_printf PARAMS ((output_buffer *, const char *))
74 ATTRIBUTE_PRINTF (2, 0);
75 static void format_with_decl PARAMS ((output_buffer *, tree));
76 static void file_and_line_for_asm PARAMS ((rtx, const char **, int *));
77 static void diagnostic_for_asm PARAMS ((rtx, const char *, va_list *, int));
78 static void diagnostic_for_decl PARAMS ((tree, const char *, va_list *, int));
79 static void set_real_maximum_length PARAMS ((output_buffer *));
81 static void output_unsigned_decimal PARAMS ((output_buffer *, unsigned int));
82 static void output_long_decimal PARAMS ((output_buffer *, long int));
83 static void output_long_unsigned_decimal PARAMS ((output_buffer *,
85 static void output_octal PARAMS ((output_buffer *, unsigned int));
86 static void output_long_octal PARAMS ((output_buffer *, unsigned long int));
87 static void output_hexadecimal PARAMS ((output_buffer *, unsigned int));
88 static void output_long_hexadecimal PARAMS ((output_buffer *,
90 static void output_append_r PARAMS ((output_buffer *, const char *, int));
91 static void wrap_text PARAMS ((output_buffer *, const char *, const char *));
92 static void maybe_wrap_text PARAMS ((output_buffer *, const char *,
94 static void clear_diagnostic_info PARAMS ((output_buffer *));
96 static void default_diagnostic_starter PARAMS ((output_buffer *,
97 diagnostic_context *));
98 static void default_diagnostic_finalizer PARAMS ((output_buffer *,
99 diagnostic_context *));
101 static void error_recursion PARAMS ((void)) ATTRIBUTE_NORETURN;
103 extern int rtl_dump_and_exit;
104 extern int warnings_are_errors;
106 /* A diagnostic_context surrogate for stderr. */
107 static diagnostic_context global_diagnostic_context;
108 diagnostic_context *global_dc = &global_diagnostic_context;
110 /* This will be removed shortly. */
111 output_buffer *diagnostic_buffer = &global_diagnostic_context.buffer;
113 /* Function of last error message;
114 more generally, function such that if next error message is in it
115 then we don't have to mention the function name. */
116 static tree last_error_function = NULL;
118 /* Used to detect when input_file_stack has changed since last described. */
119 static int last_error_tick;
121 /* Called by report_error_function to print out function name.
122 Default may be overridden by language front-ends. */
124 void (*print_error_function) PARAMS ((const char *)) =
125 default_print_error_function;
127 /* Prevent recursion into the error handler. */
128 static int diagnostic_lock;
131 /* Return truthvalue if current input file is different from the most recent
132 file involved in a diagnostic message. */
135 error_module_changed ()
137 return last_error_tick != input_file_stack_tick;
140 /* Remember current file as being the most recent file involved in a
141 diagnostic message. */
144 record_last_error_module ()
146 last_error_tick = input_file_stack_tick;
149 /* Same as error_module_changed, but for function. */
152 error_function_changed ()
154 return last_error_function != current_function_decl;
157 /* Same as record_last_error_module, but for function. */
160 record_last_error_function ()
162 last_error_function = current_function_decl;
165 /* Initialize the diagnostic message outputting machinery. */
168 diagnostic_initialize (context)
169 diagnostic_context *context;
171 memset (context, 0, sizeof *context);
172 obstack_init (&context->buffer.obstack);
174 /* By default, diagnostics are sent to stderr. */
175 output_buffer_attached_stream (&context->buffer) = stderr;
177 /* By default, we emit prefixes once per message. */
178 diagnostic_prefixing_rule (context) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
180 diagnostic_starter (context) = default_diagnostic_starter;
181 diagnostic_finalizer (context) = default_diagnostic_finalizer;
184 /* Returns true if BUFFER is in line-wrappind mode. */
187 output_is_line_wrapping (buffer)
188 output_buffer *buffer;
190 return diagnostic_line_cutoff (buffer) > 0;
193 /* Return BUFFER's prefix. */
196 output_get_prefix (buffer)
197 const output_buffer *buffer;
199 return output_prefix (buffer);
202 /* Subroutine of output_set_maximum_length. Set up BUFFER's
203 internal maximum characters per line. */
206 set_real_maximum_length (buffer)
207 output_buffer *buffer;
209 /* If we're told not to wrap lines then do the obvious thing. In case
210 we'll emit prefix only once per diagnostic message, it is appropriate
211 not to increase unncessarily the line-length cut-off. */
212 if (! output_is_line_wrapping (buffer)
213 || diagnostic_prefixing_rule (buffer) == DIAGNOSTICS_SHOW_PREFIX_ONCE
214 || diagnostic_prefixing_rule (buffer) == DIAGNOSTICS_SHOW_PREFIX_NEVER)
215 line_wrap_cutoff (buffer) = diagnostic_line_cutoff (buffer);
219 output_prefix (buffer) ? strlen (output_prefix (buffer)) : 0;
220 /* If the prefix is ridiculously too long, output at least
222 if (diagnostic_line_cutoff (buffer) - prefix_length < 32)
223 line_wrap_cutoff (buffer) = diagnostic_line_cutoff (buffer) + 32;
225 line_wrap_cutoff (buffer) = diagnostic_line_cutoff (buffer);
229 /* Sets the number of maximum characters per line BUFFER can output
230 in line-wrapping mode. A LENGTH value 0 suppresses line-wrapping. */
233 output_set_maximum_length (buffer, length)
234 output_buffer *buffer;
237 diagnostic_line_cutoff (buffer) = length;
238 set_real_maximum_length (buffer);
241 /* Sets BUFFER's PREFIX. */
244 output_set_prefix (buffer, prefix)
245 output_buffer *buffer;
248 output_prefix (buffer) = prefix;
249 set_real_maximum_length (buffer);
250 prefix_was_emitted_for (buffer) = 0;
251 output_indentation (buffer) = 0;
254 /* Return a pointer to the last character emitted in the output
255 BUFFER area. A NULL pointer means no character available. */
257 output_last_position (buffer)
258 const output_buffer *buffer;
260 const char *p = NULL;
262 if (obstack_base (&buffer->obstack) != obstack_next_free (&buffer->obstack))
263 p = ((const char *) obstack_next_free (&buffer->obstack)) - 1;
267 /* Free BUFFER's prefix, a previously malloc'd string. */
270 output_destroy_prefix (buffer)
271 output_buffer *buffer;
273 if (output_prefix (buffer) != NULL)
275 free ((char *) output_prefix (buffer));
276 output_prefix (buffer) = NULL;
280 /* Zero out any text output so far in BUFFER. */
283 output_clear_message_text (buffer)
284 output_buffer *buffer;
286 obstack_free (&buffer->obstack, obstack_base (&buffer->obstack));
287 output_text_length (buffer) = 0;
290 /* Zero out any diagnostic data used so far by BUFFER. */
293 clear_diagnostic_info (buffer)
294 output_buffer *buffer;
296 output_buffer_text_cursor (buffer) = NULL;
297 output_buffer_ptr_to_format_args (buffer) = NULL;
298 prefix_was_emitted_for (buffer) = 0;
299 output_indentation (buffer) = 0;
302 /* Construct an output BUFFER with PREFIX and of MAXIMUM_LENGTH
303 characters per line. */
306 init_output_buffer (buffer, prefix, maximum_length)
307 output_buffer *buffer;
311 memset (buffer, 0, sizeof (output_buffer));
312 obstack_init (&buffer->obstack);
313 output_buffer_attached_stream (buffer) = stderr;
314 diagnostic_line_cutoff (buffer) = maximum_length;
315 diagnostic_prefixing_rule (buffer) = diagnostic_prefixing_rule (global_dc);
316 output_set_prefix (buffer, prefix);
317 output_text_length (buffer) = 0;
318 clear_diagnostic_info (buffer);
321 /* Reinitialize BUFFER. */
324 output_clear (buffer)
325 output_buffer *buffer;
327 output_clear_message_text (buffer);
328 clear_diagnostic_info (buffer);
331 /* Finishes constructing a NULL-terminated character string representing
332 the BUFFERed message. */
335 output_finalize_message (buffer)
336 output_buffer *buffer;
338 obstack_1grow (&buffer->obstack, '\0');
339 return output_message_text (buffer);
343 flush_diagnostic_buffer ()
345 output_buffer_to_stream (diagnostic_buffer);
346 fflush (output_buffer_attached_stream (diagnostic_buffer));
349 /* Return the amount of characters BUFFER can accept to
353 output_space_left (buffer)
354 const output_buffer *buffer;
356 return line_wrap_cutoff (buffer) - output_text_length (buffer);
359 /* Write out BUFFER's prefix. */
362 output_emit_prefix (buffer)
363 output_buffer *buffer;
365 if (output_prefix (buffer) != NULL)
367 switch (diagnostic_prefixing_rule (buffer))
370 case DIAGNOSTICS_SHOW_PREFIX_NEVER:
373 case DIAGNOSTICS_SHOW_PREFIX_ONCE:
374 if (prefix_was_emitted_for (buffer))
376 output_indent (buffer);
379 output_indentation (buffer) += 3;
382 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
384 int prefix_length = strlen (output_prefix (buffer));
385 output_append_r (buffer, output_prefix (buffer), prefix_length);
386 prefix_was_emitted_for (buffer) = 1;
393 /* Have BUFFER start a new line. */
396 output_add_newline (buffer)
397 output_buffer *buffer;
399 obstack_1grow (&buffer->obstack, '\n');
400 output_text_length (buffer) = 0;
403 /* Appends a character to BUFFER. */
406 output_add_character (buffer, c)
407 output_buffer *buffer;
410 if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
411 output_add_newline (buffer);
412 obstack_1grow (&buffer->obstack, c);
413 ++output_text_length (buffer);
416 /* Adds a space to BUFFER. */
419 output_add_space (buffer)
420 output_buffer *buffer;
422 if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
424 output_add_newline (buffer);
427 obstack_1grow (&buffer->obstack, ' ');
428 ++output_text_length (buffer);
431 /* These functions format an INTEGER into BUFFER as suggested by their
435 output_decimal (buffer, i)
436 output_buffer *buffer;
439 output_formatted_integer (buffer, "%d", i);
443 output_long_decimal (buffer, i)
444 output_buffer *buffer;
447 output_formatted_integer (buffer, "%ld", i);
451 output_unsigned_decimal (buffer, i)
452 output_buffer *buffer;
455 output_formatted_integer (buffer, "%u", i);
459 output_long_unsigned_decimal (buffer, i)
460 output_buffer *buffer;
463 output_formatted_integer (buffer, "%lu", i);
467 output_octal (buffer, i)
468 output_buffer *buffer;
471 output_formatted_integer (buffer, "%o", i);
475 output_long_octal (buffer, i)
476 output_buffer *buffer;
479 output_formatted_integer (buffer, "%lo", i);
483 output_hexadecimal (buffer, i)
484 output_buffer *buffer;
487 output_formatted_integer (buffer, "%x", i);
491 output_long_hexadecimal (buffer, i)
492 output_buffer *buffer;
495 output_formatted_integer (buffer, "%lx", i);
498 /* Append to BUFFER a string specified by its STARTING character
502 output_append_r (buffer, start, length)
503 output_buffer *buffer;
507 obstack_grow (&buffer->obstack, start, length);
508 output_text_length (buffer) += length;
511 /* Append a string deliminated by START and END to BUFFER. No wrapping is
512 done. However, if beginning a new line then emit output_prefix (BUFFER)
513 and skip any leading whitespace if appropriate. The caller must ensure
514 that it is safe to do so. */
517 output_append (buffer, start, end)
518 output_buffer *buffer;
522 /* Emit prefix and skip whitespace if we're starting a new line. */
523 if (is_starting_newline (buffer))
525 output_emit_prefix (buffer);
526 if (output_is_line_wrapping (buffer))
527 while (start != end && *start == ' ')
530 output_append_r (buffer, start, end - start);
534 output_indent (buffer)
535 output_buffer *buffer;
537 int n = output_indentation (buffer);
540 for (i = 0; i < n; ++i)
541 output_add_character (buffer, ' ');
544 /* Wrap a text delimited by START and END into BUFFER. */
547 wrap_text (buffer, start, end)
548 output_buffer *buffer;
552 int is_wrapping = output_is_line_wrapping (buffer);
556 /* Dump anything bodered by whitespaces. */
558 const char *p = start;
559 while (p != end && *p != ' ' && *p != '\n')
561 if (is_wrapping && p - start >= output_space_left (buffer))
562 output_add_newline (buffer);
563 output_append (buffer, start, p);
567 if (start != end && *start == ' ')
569 output_add_space (buffer);
572 if (start != end && *start == '\n')
574 output_add_newline (buffer);
580 /* Same as wrap_text but wrap text only when in line-wrapping mode. */
583 maybe_wrap_text (buffer, start, end)
584 output_buffer *buffer;
588 if (output_is_line_wrapping (buffer))
589 wrap_text (buffer, start, end);
591 output_append (buffer, start, end);
595 /* Append a STRING to BUFFER; the STRING might be line-wrapped if in
599 output_add_string (buffer, str)
600 output_buffer *buffer;
603 maybe_wrap_text (buffer, str, str + (str ? strlen (str) : 0));
606 /* Flush the content of BUFFER onto the attached stream,
610 output_buffer_to_stream (buffer)
611 output_buffer *buffer;
613 const char *text = output_finalize_message (buffer);
614 fputs (text, output_buffer_attached_stream (buffer));
615 output_clear_message_text (buffer);
618 /* Format a message pointed to by output_buffer_text_cursor (BUFFER) using
619 output_buffer_format_args (BUFFER) as appropriate. The following format
620 specifiers are recognized as being language independent:
621 %d, %i: (signed) integer in base ten.
622 %u: unsigned integer in base ten.
623 %o: unsigned integer in base eight.
624 %x: unsigned integer in base sixteen.
625 %ld, %li, %lo, %lu, %lx: long versions of the above.
629 %*.s: a substring the length of which is specified by an integer. */
632 output_format (buffer)
633 output_buffer *buffer;
635 for (; *output_buffer_text_cursor (buffer);
636 ++output_buffer_text_cursor (buffer))
638 int long_integer = 0;
642 const char *p = output_buffer_text_cursor (buffer);
643 while (*p && *p != '%')
645 wrap_text (buffer, output_buffer_text_cursor (buffer), p);
646 output_buffer_text_cursor (buffer) = p;
649 if (!*output_buffer_text_cursor (buffer))
652 /* We got a '%'. Let's see what happens. Record whether we're
653 parsing a long integer format specifier. */
654 if (*++output_buffer_text_cursor (buffer) == 'l')
657 ++output_buffer_text_cursor (buffer);
660 /* Handle %c, %d, %i, %ld, %li, %lo, %lu, %lx, %o, %s, %u,
661 %x, %.*s; %%. And nothing else. Front-ends should install
662 printers to grok language specific format specifiers. */
663 switch (*output_buffer_text_cursor (buffer))
667 (buffer, va_arg (output_buffer_format_args (buffer), int));
674 (buffer, va_arg (output_buffer_format_args (buffer), long int));
677 (buffer, va_arg (output_buffer_format_args (buffer), int));
682 output_long_octal (buffer,
683 va_arg (output_buffer_format_args (buffer),
686 output_octal (buffer,
687 va_arg (output_buffer_format_args (buffer),
692 output_add_string (buffer,
693 va_arg (output_buffer_format_args (buffer),
699 output_long_unsigned_decimal
700 (buffer, va_arg (output_buffer_format_args (buffer),
703 output_unsigned_decimal
704 (buffer, va_arg (output_buffer_format_args (buffer),
710 output_long_hexadecimal
711 (buffer, va_arg (output_buffer_format_args (buffer),
715 (buffer, va_arg (output_buffer_format_args (buffer),
720 output_add_character (buffer, '%');
727 /* We handle no precision specifier but `%.*s'. */
728 if (*++output_buffer_text_cursor (buffer) != '*')
730 else if (*++output_buffer_text_cursor (buffer) != 's')
732 n = va_arg (output_buffer_format_args (buffer), int);
733 s = va_arg (output_buffer_format_args (buffer), const char *);
734 output_append (buffer, s, s + n);
739 if (!buffer->format_decoder || !(*buffer->format_decoder) (buffer))
741 /* Hmmm. The front-end failed to install a format translator
742 but called us with an unrecognized format. Sorry. */
750 vbuild_message_string (msg, ap)
756 vasprintf (&str, msg, ap);
760 /* Return a malloc'd string containing MSG formatted a la
761 printf. The caller is reponsible for freeing the memory. */
764 build_message_string VPARAMS ((const char *msg, ...))
766 #ifndef ANSI_PROTOTYPES
774 #ifndef ANSI_PROTOTYPES
775 msg = va_arg (ap, const char *);
778 str = vbuild_message_string (msg, ap);
785 /* Return a malloc'd string describing a location. The caller is
786 responsible for freeing the memory. */
789 context_as_prefix (file, line, warn)
797 return build_message_string (_("%s:%d: warning: "), file, line);
799 return build_message_string ("%s:%d: ", file, line);
804 return build_message_string (_("%s: warning: "), progname);
806 return build_message_string ("%s: ", progname);
810 /* Same as context_as_prefix, but only the source FILE is given. */
813 file_name_as_prefix (f)
816 return build_message_string ("%s: ", f);
819 /* Format a MESSAGE into BUFFER. Automatically wrap lines. */
822 output_do_printf (buffer, msg)
823 output_buffer *buffer;
826 char *message = vbuild_message_string (msg,
827 output_buffer_format_args (buffer));
829 wrap_text (buffer, message, message + strlen (message));
834 /* Format a message into BUFFER a la printf. */
837 output_printf VPARAMS ((struct output_buffer *buffer, const char *msgid, ...))
839 #ifndef ANSI_PROTOTYPES
840 struct output_buffer *buffer;
846 VA_START (ap, msgid);
847 #ifndef ANSI_PROTOTYPES
848 buffer = va_arg (ap, output_buffer *);
849 msgid = va_arg (ap, const char *);
851 old_args = output_buffer_ptr_to_format_args (buffer);
852 output_buffer_ptr_to_format_args (buffer) = ≈
853 output_do_printf (buffer, _(msgid));
854 output_buffer_ptr_to_format_args (buffer) = old_args;
858 /* Print a message relevant to the given DECL. */
861 format_with_decl (buffer, decl)
862 output_buffer *buffer;
867 /* Do magic to get around lack of varargs support for insertion
868 of arguments into existing list. We know that the decl is first;
869 we ass_u_me that it will be printed with "%s". */
870 for (p = output_buffer_text_cursor (buffer); *p; ++p)
876 else if (*(p + 1) != 's')
883 /* Print the left-hand substring. */
884 maybe_wrap_text (buffer, output_buffer_text_cursor (buffer), p);
886 if (*p == '%') /* Print the name. */
888 const char *n = (DECL_NAME (decl)
889 ? (*decl_printable_name) (decl, 2)
890 : _("((anonymous))"));
891 output_add_string (buffer, n);
895 if (ISALPHA (*(p - 1) & 0xFF))
900 if (*p) /* Print the rest of the message. */
902 output_buffer_text_cursor (buffer) = p;
903 output_format (buffer);
907 /* Figure file and line of the given INSN. */
910 file_and_line_for_asm (insn, pfile, pline)
915 rtx body = PATTERN (insn);
918 /* Find the (or one of the) ASM_OPERANDS in the insn. */
919 if (GET_CODE (body) == SET && GET_CODE (SET_SRC (body)) == ASM_OPERANDS)
920 asmop = SET_SRC (body);
921 else if (GET_CODE (body) == ASM_OPERANDS)
923 else if (GET_CODE (body) == PARALLEL
924 && GET_CODE (XVECEXP (body, 0, 0)) == SET)
925 asmop = SET_SRC (XVECEXP (body, 0, 0));
926 else if (GET_CODE (body) == PARALLEL
927 && GET_CODE (XVECEXP (body, 0, 0)) == ASM_OPERANDS)
928 asmop = XVECEXP (body, 0, 0);
934 *pfile = ASM_OPERANDS_SOURCE_FILE (asmop);
935 *pline = ASM_OPERANDS_SOURCE_LINE (asmop);
939 *pfile = input_filename;
944 /* Report a diagnostic MESSAGE (an errror or a WARNING) at the line number
945 of the insn INSN. This is used only when INSN is an `asm' with operands,
946 and each ASM_OPERANDS records its own source file and line. */
949 diagnostic_for_asm (insn, msg, args_ptr, warn)
955 diagnostic_context dc;
957 set_diagnostic_context (&dc, msg, args_ptr, NULL, 0, warn);
958 file_and_line_for_asm (insn, &diagnostic_file_location (&dc),
959 &diagnostic_line_location (&dc));
960 report_diagnostic (&dc);
963 /* Report a diagnostic MESSAGE at the declaration DECL.
964 MSG is a format string which uses %s to substitute the declaration
965 name; subsequent substitutions are a la output_format. */
968 diagnostic_for_decl (decl, msgid, args_ptr, warn)
976 if (diagnostic_lock++)
979 if (count_error (warn))
981 os = output_buffer_state (diagnostic_buffer);
982 report_error_function (DECL_SOURCE_FILE (decl));
984 (diagnostic_buffer, context_as_prefix
985 (DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl), warn));
986 output_buffer_ptr_to_format_args (diagnostic_buffer) = args_ptr;
987 output_buffer_text_cursor (diagnostic_buffer) = _(msgid);
988 format_with_decl (diagnostic_buffer, decl);
989 finish_diagnostic ();
990 output_destroy_prefix (diagnostic_buffer);
992 output_buffer_state (diagnostic_buffer) = os;
998 /* Count an error or warning. Return 1 if the message should be printed. */
1001 count_error (warningp)
1004 if (warningp && !diagnostic_report_warnings_p ())
1007 if (warningp && !warnings_are_errors)
1011 static int warning_message = 0;
1013 if (warningp && !warning_message)
1015 verbatim ("%s: warnings being treated as errors\n", progname);
1016 warning_message = 1;
1024 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1025 runs its second argument through gettext. */
1028 fnotice VPARAMS ((FILE *file, const char *msgid, ...))
1030 #ifndef ANSI_PROTOTYPES
1036 VA_START (ap, msgid);
1038 #ifndef ANSI_PROTOTYPES
1039 file = va_arg (ap, FILE *);
1040 msgid = va_arg (ap, const char *);
1043 vfprintf (file, _(msgid), ap);
1048 /* Print a fatal I/O error message. Argument are like printf.
1049 Also include a system error message based on `errno'. */
1052 fatal_io_error VPARAMS ((const char *msgid, ...))
1054 #ifndef ANSI_PROTOTYPES
1060 os = output_buffer_state (diagnostic_buffer);
1061 VA_START (ap, msgid);
1063 #ifndef ANSI_PROTOTYPES
1064 msgid = va_arg (ap, const char *);
1067 output_printf (diagnostic_buffer, "%s: %s: ", progname, xstrerror (errno));
1068 output_buffer_ptr_to_format_args (diagnostic_buffer) = ≈
1069 output_buffer_text_cursor (diagnostic_buffer) = _(msgid);
1070 output_format (diagnostic_buffer);
1071 finish_diagnostic ();
1072 output_buffer_state (diagnostic_buffer) = os;
1074 exit (FATAL_EXIT_CODE);
1077 /* Issue a pedantic warning MSGID. */
1080 pedwarn VPARAMS ((const char *msgid, ...))
1082 #ifndef ANSI_PROTOTYPES
1086 diagnostic_context dc;
1088 VA_START (ap, msgid);
1090 #ifndef ANSI_PROTOTYPES
1091 msgid = va_arg (ap, const char *);
1094 set_diagnostic_context
1095 (&dc, msgid, &ap, input_filename, lineno, !flag_pedantic_errors);
1096 report_diagnostic (&dc);
1100 /* Issue a pedantic waring about DECL. */
1103 pedwarn_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1105 #ifndef ANSI_PROTOTYPES
1111 VA_START (ap, msgid);
1113 #ifndef ANSI_PROTOTYPES
1114 decl = va_arg (ap, tree);
1115 msgid = va_arg (ap, const char *);
1117 /* We don't want -pedantic-errors to cause the compilation to fail from
1118 "errors" in system header files. Sometimes fixincludes can't fix what's
1119 broken (eg: unsigned char bitfields - fixing it may change the alignment
1120 which will cause programs to mysteriously fail because the C library
1121 or kernel uses the original layout). There's no point in issuing a
1122 warning either, it's just unnecessary noise. */
1123 if (!DECL_IN_SYSTEM_HEADER (decl))
1124 diagnostic_for_decl (decl, msgid, &ap, !flag_pedantic_errors);
1128 /* Same as above but within the context FILE and LINE. */
1131 pedwarn_with_file_and_line VPARAMS ((const char *file, int line,
1132 const char *msgid, ...))
1134 #ifndef ANSI_PROTOTYPES
1140 diagnostic_context dc;
1142 VA_START (ap, msgid);
1144 #ifndef ANSI_PROTOTYPES
1145 file = va_arg (ap, const char *);
1146 line = va_arg (ap, int);
1147 msgid = va_arg (ap, const char *);
1150 set_diagnostic_context (&dc, msgid, &ap, file, line, !flag_pedantic_errors);
1151 report_diagnostic (&dc);
1155 /* Just apologize with MSGID. */
1158 sorry VPARAMS ((const char *msgid, ...))
1160 #ifndef ANSI_PROTOTYPES
1166 os = output_buffer_state (diagnostic_buffer);
1167 VA_START (ap, msgid);
1169 #ifndef ANSI_PROTOTYPES
1170 msgid = va_arg (ap, const char *);
1174 (diagnostic_buffer, context_as_prefix (input_filename, lineno, 0));
1175 output_printf (diagnostic_buffer, "sorry, not implemented: ");
1176 output_buffer_ptr_to_format_args (diagnostic_buffer) = ≈
1177 output_buffer_text_cursor (diagnostic_buffer) = _(msgid);
1178 output_format (diagnostic_buffer);
1179 finish_diagnostic ();
1180 output_buffer_state (diagnostic_buffer) = os;
1184 /* Called when the start of a function definition is parsed,
1185 this function prints on stderr the name of the function. */
1188 announce_function (decl)
1193 if (rtl_dump_and_exit)
1194 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl)));
1196 verbatim (" %s", (*decl_printable_name) (decl, 2));
1198 output_needs_newline (diagnostic_buffer) = 1;
1199 record_last_error_function ();
1203 /* The default function to print out name of current function that caused
1207 default_print_error_function (file)
1210 if (error_function_changed ())
1212 char *prefix = file ? build_message_string ("%s: ", file) : NULL;
1215 os = output_buffer_state (diagnostic_buffer);
1216 output_set_prefix (diagnostic_buffer, prefix);
1218 if (current_function_decl == NULL)
1219 output_add_string (diagnostic_buffer, _("At top level:"));
1222 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
1224 (diagnostic_buffer, "In method `%s':",
1225 (*decl_printable_name) (current_function_decl, 2));
1228 (diagnostic_buffer, "In function `%s':",
1229 (*decl_printable_name) (current_function_decl, 2));
1231 output_add_newline (diagnostic_buffer);
1233 record_last_error_function ();
1234 output_buffer_to_stream (diagnostic_buffer);
1235 output_buffer_state (diagnostic_buffer) = os;
1236 free ((char*) prefix);
1240 /* Prints out, if necessary, the name of the current function
1241 that caused an error. Called from all error and warning functions.
1242 We ignore the FILE parameter, as it cannot be relied upon. */
1245 report_error_function (file)
1246 const char *file ATTRIBUTE_UNUSED;
1248 report_problematic_module (diagnostic_buffer);
1249 (*print_error_function) (input_filename);
1253 error_with_file_and_line VPARAMS ((const char *file, int line,
1254 const char *msgid, ...))
1256 #ifndef ANSI_PROTOTYPES
1262 diagnostic_context dc;
1264 VA_START (ap, msgid);
1266 #ifndef ANSI_PROTOTYPES
1267 file = va_arg (ap, const char *);
1268 line = va_arg (ap, int);
1269 msgid = va_arg (ap, const char *);
1272 set_diagnostic_context (&dc, msgid, &ap, file, line, /* warn = */ 0);
1273 report_diagnostic (&dc);
1278 error_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1280 #ifndef ANSI_PROTOTYPES
1286 VA_START (ap, msgid);
1288 #ifndef ANSI_PROTOTYPES
1289 decl = va_arg (ap, tree);
1290 msgid = va_arg (ap, const char *);
1293 diagnostic_for_decl (decl, msgid, &ap, /* warn = */ 0);
1298 error_for_asm VPARAMS ((rtx insn, const char *msgid, ...))
1300 #ifndef ANSI_PROTOTYPES
1306 VA_START (ap, msgid);
1308 #ifndef ANSI_PROTOTYPES
1309 insn = va_arg (ap, rtx);
1310 msgid = va_arg (ap, const char *);
1313 diagnostic_for_asm (insn, msgid, &ap, /* warn = */ 0);
1317 /* Report an error message. The arguments are like that of printf. */
1320 error VPARAMS ((const char *msgid, ...))
1322 #ifndef ANSI_PROTOTYPES
1326 diagnostic_context dc;
1328 VA_START (ap, msgid);
1330 #ifndef ANSI_PROTOTYPES
1331 msgid = va_arg (ap, const char *);
1334 set_diagnostic_context
1335 (&dc, msgid, &ap, input_filename, lineno, /* warn = */ 0);
1336 report_diagnostic (&dc);
1340 /* Likewise, except that the compilation is terminated after printing the
1344 fatal_error VPARAMS ((const char *msgid, ...))
1346 #ifndef ANSI_PROTOTYPES
1350 diagnostic_context dc;
1352 VA_START (ap, msgid);
1354 #ifndef ANSI_PROTOTYPES
1355 msgid = va_arg (ap, const char *);
1358 set_diagnostic_context
1359 (&dc, msgid, &ap, input_filename, lineno, /* warn = */ 0);
1360 report_diagnostic (&dc);
1363 fnotice (stderr, "compilation terminated.\n");
1364 exit (FATAL_EXIT_CODE);
1367 /* Report a compiler error at the current line number. Allow a front end to
1368 intercept the message. */
1370 static void (*internal_error_function) PARAMS ((const char *, va_list *));
1372 /* Set the function to call when a compiler error occurs. */
1375 set_internal_error_function (f)
1376 void (*f) PARAMS ((const char *, va_list *));
1378 internal_error_function = f;
1382 internal_error VPARAMS ((const char *msgid, ...))
1384 #ifndef ANSI_PROTOTYPES
1388 diagnostic_context dc;
1390 VA_START (ap, msgid);
1392 #ifndef ANSI_PROTOTYPES
1393 msgid = va_arg (ap, const char *);
1396 if (errorcount > 0 || sorrycount > 0)
1398 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
1399 input_filename, lineno);
1400 exit (FATAL_EXIT_CODE);
1403 if (internal_error_function != 0)
1404 (*internal_error_function) (_(msgid), &ap);
1406 set_diagnostic_context
1407 (&dc, msgid, &ap, input_filename, lineno, /* warn = */0);
1408 report_diagnostic (&dc);
1412 "Please submit a full bug report,\n\
1413 with preprocessed source if appropriate.\n\
1414 See %s for instructions.\n", GCCBUGURL);
1415 exit (FATAL_EXIT_CODE);
1419 _fatal_insn (msgid, insn, file, line, function)
1424 const char *function;
1426 error ("%s", _(msgid));
1428 /* The above incremented error_count, but isn't an error that we want to
1429 count, so reset it here. */
1433 fancy_abort (file, line, function);
1437 _fatal_insn_not_found (insn, file, line, function)
1441 const char *function;
1443 if (INSN_CODE (insn) < 0)
1444 _fatal_insn ("Unrecognizable insn:", insn, file, line, function);
1446 _fatal_insn ("Insn does not satisfy its constraints:",
1447 insn, file, line, function);
1451 warning_with_file_and_line VPARAMS ((const char *file, int line,
1452 const char *msgid, ...))
1454 #ifndef ANSI_PROTOTYPES
1460 diagnostic_context dc;
1462 VA_START (ap, msgid);
1464 #ifndef ANSI_PROTOTYPES
1465 file = va_arg (ap, const char *);
1466 line = va_arg (ap, int);
1467 msgid = va_arg (ap, const char *);
1470 set_diagnostic_context (&dc, msgid, &ap, file, line, /* warn = */ 1);
1471 report_diagnostic (&dc);
1476 warning_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1478 #ifndef ANSI_PROTOTYPES
1484 VA_START (ap, msgid);
1486 #ifndef ANSI_PROTOTYPES
1487 decl = va_arg (ap, tree);
1488 msgid = va_arg (ap, const char *);
1491 diagnostic_for_decl (decl, msgid, &ap, /* warn = */ 1);
1496 warning_for_asm VPARAMS ((rtx insn, const char *msgid, ...))
1498 #ifndef ANSI_PROTOTYPES
1504 VA_START (ap, msgid);
1506 #ifndef ANSI_PROTOTYPES
1507 insn = va_arg (ap, rtx);
1508 msgid = va_arg (ap, const char *);
1511 diagnostic_for_asm (insn, msgid, &ap, /* warn = */ 1);
1516 warning VPARAMS ((const char *msgid, ...))
1518 #ifndef ANSI_PROTOTYPES
1522 diagnostic_context dc;
1524 VA_START (ap, msgid);
1526 #ifndef ANSI_PROTOTYPES
1527 msgid = va_arg (ap, const char *);
1530 set_diagnostic_context
1531 (&dc, msgid, &ap, input_filename, lineno, /* warn = */ 1);
1532 report_diagnostic (&dc);
1536 /* Flush diagnostic_buffer content on stderr. */
1539 finish_diagnostic ()
1541 output_buffer_to_stream (diagnostic_buffer);
1542 clear_diagnostic_info (diagnostic_buffer);
1543 fputc ('\n', output_buffer_attached_stream (diagnostic_buffer));
1544 fflush (output_buffer_attached_stream (diagnostic_buffer));
1547 /* Helper subroutine of output_verbatim and verbatim. Do the approriate
1548 settings needed by BUFFER for a verbatim formatting. */
1551 output_do_verbatim (buffer, msgid, args_ptr)
1552 output_buffer *buffer;
1558 os = output_buffer_state (buffer);
1559 output_prefix (buffer) = NULL;
1560 diagnostic_prefixing_rule (buffer) = DIAGNOSTICS_SHOW_PREFIX_NEVER;
1561 output_buffer_text_cursor (buffer) = _(msgid);
1562 output_buffer_ptr_to_format_args (buffer) = args_ptr;
1563 output_set_maximum_length (buffer, 0);
1564 output_format (buffer);
1565 output_buffer_state (buffer) = os;
1568 /* Output MESSAGE verbatim into BUFFER. */
1571 output_verbatim VPARAMS ((output_buffer *buffer, const char *msgid, ...))
1573 #ifndef ANSI_PROTOTYPES
1574 output_buffer *buffer;
1579 VA_START (ap, msgid);
1580 #ifndef ANSI_PROTOTYPES
1581 buffer = va_arg (ap, output_buffer *);
1582 msg = va_arg (ap, const char *);
1584 output_do_verbatim (buffer, msgid, &ap);
1588 /* Same as above but use diagnostic_buffer. */
1591 verbatim VPARAMS ((const char *msgid, ...))
1593 #ifndef ANSI_PROTOTYPES
1598 VA_START (ap, msgid);
1599 #ifndef ANSI_PROTOTYPES
1600 msgid = va_arg (ap, const char *);
1602 output_do_verbatim (diagnostic_buffer, msgid, &ap);
1603 output_buffer_to_stream (diagnostic_buffer);
1607 /* Report a diagnostic message (an error or a warning) as specified by
1608 DC. This function is *the* subroutine in terms of which front-ends
1609 should implement their specific diagnostic handling modules. The
1610 front-end independent format specifiers are exactly those described
1611 in the documentation of output_format. */
1614 report_diagnostic (dc)
1615 diagnostic_context *dc;
1619 if (diagnostic_lock++)
1622 if (count_error (diagnostic_is_warning (dc)))
1624 os = output_buffer_state (diagnostic_buffer);
1625 diagnostic_msg = diagnostic_message (dc);
1626 diagnostic_args = diagnostic_argument_list (dc);
1627 (*diagnostic_starter (dc)) (diagnostic_buffer, dc);
1628 output_format (diagnostic_buffer);
1629 (*diagnostic_finalizer (dc)) (diagnostic_buffer, dc);
1630 finish_diagnostic ();
1631 output_buffer_state (diagnostic_buffer) = os;
1637 /* Inform the user that an error occurred while trying to report some
1638 other error. This indicates catastrophic internal inconsistencies,
1639 so give up now. But do try to flush out the previous error.
1640 This mustn't use internal_error, that will cause infinite recursion. */
1645 if (diagnostic_lock < 3)
1646 finish_diagnostic ();
1649 "Internal compiler error: Error reporting routines re-entered.\n");
1651 "Please submit a full bug report,\n\
1652 with preprocessed source if appropriate.\n\
1653 See %s for instructions.\n", GCCBUGURL);
1654 exit (FATAL_EXIT_CODE);
1657 /* Given a partial pathname as input, return another pathname that
1658 shares no directory elements with the pathname of __FILE__. This
1659 is used by fancy_abort() to print `Internal compiler error in expr.c'
1660 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1663 trim_filename (name)
1666 static const char this_file[] = __FILE__;
1667 const char *p = name, *q = this_file;
1669 /* First skip any "../" in each filename. This allows us to give a proper
1670 reference to a file in a subdirectory. */
1671 while (p[0] == '.' && p[1] == '.'
1672 && (p[2] == DIR_SEPARATOR
1673 #ifdef DIR_SEPARATOR_2
1674 || p[2] == DIR_SEPARATOR_2
1679 while (q[0] == '.' && q[1] == '.'
1680 && (q[2] == DIR_SEPARATOR
1681 #ifdef DIR_SEPARATOR_2
1682 || p[2] == DIR_SEPARATOR_2
1687 /* Now skip any parts the two filenames have in common. */
1688 while (*p == *q && *p != 0 && *q != 0)
1691 /* Now go backwards until the previous directory separator. */
1692 while (p > name && p[-1] != DIR_SEPARATOR
1693 #ifdef DIR_SEPARATOR_2
1694 && p[-1] != DIR_SEPARATOR_2
1702 /* Report an internal compiler error in a friendly manner and without
1706 fancy_abort (file, line, function)
1709 const char *function;
1711 internal_error ("Internal compiler error in %s, at %s:%d",
1712 function, trim_filename (file), line);
1715 /* Setup DC for reporting a diagnostic MESSAGE (an error or a WARNING),
1716 using arguments pointed to by ARGS_PTR, issued at a location specified
1717 by FILE and LINE. */
1720 set_diagnostic_context (dc, msgid, args_ptr, file, line, warn)
1721 diagnostic_context *dc;
1728 memset (dc, 0, sizeof (diagnostic_context));
1729 diagnostic_message (dc) = _(msgid);
1730 diagnostic_argument_list (dc) = args_ptr;
1731 diagnostic_file_location (dc) = file;
1732 diagnostic_line_location (dc) = line;
1733 diagnostic_is_warning (dc) = warn;
1734 diagnostic_starter (dc) = diagnostic_starter (global_dc);
1735 diagnostic_finalizer (dc) = diagnostic_finalizer (global_dc);
1739 report_problematic_module (buffer)
1740 output_buffer *buffer;
1742 struct file_stack *p;
1744 if (output_needs_newline (buffer))
1746 output_add_newline (buffer);
1747 output_needs_newline (buffer) = 0;
1750 if (input_file_stack && input_file_stack->next != 0
1751 && error_module_changed ())
1753 for (p = input_file_stack->next; p; p = p->next)
1754 if (p == input_file_stack->next)
1756 (buffer, "In file included from %s:%d", p->name, p->line);
1759 (buffer, ",\n from %s:%d", p->name, p->line);
1760 output_verbatim (buffer, ":\n");
1761 record_last_error_module ();
1766 default_diagnostic_starter (buffer, dc)
1767 output_buffer *buffer;
1768 diagnostic_context *dc;
1770 report_error_function (diagnostic_file_location (dc));
1771 output_set_prefix (buffer,
1772 context_as_prefix (diagnostic_file_location (dc),
1773 diagnostic_line_location (dc),
1774 diagnostic_is_warning (dc)));
1778 default_diagnostic_finalizer (buffer, dc)
1779 output_buffer *buffer;
1780 diagnostic_context *dc __attribute__((__unused__));
1782 output_destroy_prefix (buffer);