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
*));
71 static char *vbuild_message_string
PARAMS ((const char *, va_list));
72 static char *build_message_string
PARAMS ((const char *, ...))
74 static char *context_as_prefix
PARAMS ((const char *, int, int));
75 static void output_do_printf
PARAMS ((output_buffer
*, const char *));
76 static void format_with_decl
PARAMS ((output_buffer
*, tree
));
77 static void file_and_line_for_asm
PARAMS ((rtx
, const char **, int *));
78 static void diagnostic_for_asm
PARAMS ((rtx
, const char *, va_list *, int));
79 static void diagnostic_for_decl
PARAMS ((tree
, const char *, va_list *, int));
80 static void vnotice
PARAMS ((FILE *, const char *, va_list));
81 static void set_real_maximum_length
PARAMS ((output_buffer
*));
83 static void output_unsigned_decimal
PARAMS ((output_buffer
*, unsigned int));
84 static void output_long_decimal
PARAMS ((output_buffer
*, long int));
85 static void output_long_unsigned_decimal
PARAMS ((output_buffer
*,
87 static void output_octal
PARAMS ((output_buffer
*, unsigned int));
88 static void output_long_octal
PARAMS ((output_buffer
*, unsigned long int));
89 static void output_hexadecimal
PARAMS ((output_buffer
*, unsigned int));
90 static void output_long_hexadecimal
PARAMS ((output_buffer
*,
92 static void output_append_r
PARAMS ((output_buffer
*, const char *, int));
93 static void wrap_text
PARAMS ((output_buffer
*, const char *, const char *));
94 static void maybe_wrap_text
PARAMS ((output_buffer
*, const char *,
96 static void clear_text_info
PARAMS ((output_buffer
*));
97 static void clear_diagnostic_info
PARAMS ((output_buffer
*));
99 static void error_recursion
PARAMS ((void)) ATTRIBUTE_NORETURN
;
100 static const char *trim_filename
PARAMS ((const char *));
102 extern int rtl_dump_and_exit
;
103 extern int inhibit_warnings
;
104 extern int warnings_are_errors
;
105 extern int warningcount
;
106 extern int errorcount
;
108 /* Front-end specific tree formatter, if non-NULL. */
109 printer_fn lang_printer
= NULL
;
111 /* This must be large enough to hold any printed integer or
112 floating-point value. */
113 static char digit_buffer
[128];
115 /* An output_buffer surrogate for stderr. */
116 static output_buffer global_output_buffer
;
117 output_buffer
*diagnostic_buffer
= &global_output_buffer
;
119 static int need_error_newline
;
121 /* Function of last error message;
122 more generally, function such that if next error message is in it
123 then we don't have to mention the function name. */
124 static tree last_error_function
= NULL
;
126 /* Used to detect when input_file_stack has changed since last described. */
127 static int last_error_tick
;
129 /* Called by report_error_function to print out function name.
130 Default may be overridden by language front-ends. */
132 void (*print_error_function
) PARAMS ((const char *)) =
133 default_print_error_function
;
135 /* Maximum characters per line in automatic line wrapping mode.
136 Zero means don't wrap lines. */
138 int diagnostic_message_length_per_line
;
140 /* Used to control every diagnostic message formatting. Front-ends should
141 call set_message_prefixing_rule to set up their politics. */
142 static int current_prefixing_rule
;
144 /* Prevent recursion into the error handler. */
145 static int diagnostic_lock
;
148 /* Initialize the diagnostic message outputting machinery. */
151 initialize_diagnostics ()
153 /* By default, we don't line-wrap messages. */
154 diagnostic_message_length_per_line
= 0;
155 set_message_prefixing_rule (DIAGNOSTICS_SHOW_PREFIX_ONCE
);
157 /* Proceed to actual initialization. */
158 default_initialize_buffer (diagnostic_buffer
);
162 set_message_prefixing_rule (rule
)
165 current_prefixing_rule
= rule
;
168 /* Returns true if BUFFER is in line-wrappind mode. */
171 output_is_line_wrapping (buffer
)
172 output_buffer
*buffer
;
174 return ideal_line_wrap_cutoff (buffer
) > 0;
177 /* Return BUFFER's prefix. */
180 output_get_prefix (buffer
)
181 const output_buffer
*buffer
;
183 return output_prefix (buffer
);
186 /* Subroutine of output_set_maximum_length. Set up BUFFER's
187 internal maximum characters per line. */
190 set_real_maximum_length (buffer
)
191 output_buffer
*buffer
;
193 /* If we're told not to wrap lines then do the obvious thing. */
194 if (! output_is_line_wrapping (buffer
))
195 line_wrap_cutoff (buffer
) = ideal_line_wrap_cutoff (buffer
);
199 output_prefix (buffer
) ? strlen (output_prefix (buffer
)) : 0;
200 /* If the prefix is ridiculously too long, output at least
202 if (ideal_line_wrap_cutoff (buffer
) - prefix_length
< 32)
203 line_wrap_cutoff (buffer
) = ideal_line_wrap_cutoff (buffer
) + 32;
205 line_wrap_cutoff (buffer
) = ideal_line_wrap_cutoff (buffer
);
209 /* Sets the number of maximum characters per line BUFFER can output
210 in line-wrapping mode. A LENGTH value 0 suppresses line-wrapping. */
213 output_set_maximum_length (buffer
, length
)
214 output_buffer
*buffer
;
217 ideal_line_wrap_cutoff (buffer
) = length
;
218 set_real_maximum_length (buffer
);
221 /* Sets BUFFER's PREFIX. */
224 output_set_prefix (buffer
, prefix
)
225 output_buffer
*buffer
;
228 output_prefix (buffer
) = prefix
;
229 set_real_maximum_length (buffer
);
230 prefix_was_emitted_for (buffer
) = 0;
233 /* Free BUFFER's prefix, a previously malloc'd string. */
236 output_destroy_prefix (buffer
)
237 output_buffer
*buffer
;
239 if (output_prefix (buffer
) != NULL
)
241 free ((char *) output_prefix (buffer
));
242 output_prefix (buffer
) = NULL
;
246 /* Zero out any text output so far in BUFFER. */
249 clear_text_info (buffer
)
250 output_buffer
*buffer
;
252 obstack_free (&buffer
->obstack
, obstack_base (&buffer
->obstack
));
253 output_text_length (buffer
) = 0;
256 /* Zero out any diagnostic data used so far by BUFFER. */
259 clear_diagnostic_info (buffer
)
260 output_buffer
*buffer
;
262 output_buffer_text_cursor (buffer
) = NULL
;
263 output_buffer_ptr_to_format_args (buffer
) = NULL
;
264 prefix_was_emitted_for (buffer
) = 0;
267 /* Construct an output BUFFER with PREFIX and of MAXIMUM_LENGTH
268 characters per line. */
271 init_output_buffer (buffer
, prefix
, maximum_length
)
272 output_buffer
*buffer
;
276 obstack_init (&buffer
->obstack
);
277 ideal_line_wrap_cutoff (buffer
) = maximum_length
;
278 prefixing_policy (buffer
) = current_prefixing_rule
;
279 output_set_prefix (buffer
, prefix
);
280 output_text_length (buffer
) = 0;
281 clear_diagnostic_info (buffer
);
284 /* Initialize BUFFER with a NULL prefix and current diagnostic message
287 default_initialize_buffer (buffer
)
288 output_buffer
*buffer
;
290 init_output_buffer (buffer
, NULL
, diagnostic_message_length_per_line
);
293 /* Recompute diagnostic_buffer's attributes to reflect any change
294 in diagnostic formatting global options. */
297 reshape_diagnostic_buffer ()
299 ideal_line_wrap_cutoff (diagnostic_buffer
) =
300 diagnostic_message_length_per_line
;
301 prefixing_policy (diagnostic_buffer
) = current_prefixing_rule
;
302 set_real_maximum_length (diagnostic_buffer
);
305 /* Reinitialize BUFFER. */
307 output_clear (buffer
)
308 output_buffer
*buffer
;
310 clear_text_info (buffer
);
311 clear_diagnostic_info (buffer
);
314 /* Finishes to construct a NULL-terminated character string representing
315 the BUFFERed message. */
318 output_finish (buffer
)
319 output_buffer
*buffer
;
321 obstack_1grow (&buffer
->obstack
, '\0');
322 return (const char *) obstack_finish (&buffer
->obstack
);
325 /* Return the amount of characters BUFFER can accept to
329 output_space_left (buffer
)
330 const output_buffer
*buffer
;
332 return line_wrap_cutoff (buffer
) - output_text_length (buffer
);
335 /* Write out BUFFER's prefix. */
338 output_emit_prefix (buffer
)
339 output_buffer
*buffer
;
341 if (output_prefix (buffer
) != NULL
)
343 switch (prefixing_policy (buffer
))
346 case DIAGNOSTICS_SHOW_PREFIX_NEVER
:
349 case DIAGNOSTICS_SHOW_PREFIX_ONCE
:
350 if (prefix_was_emitted_for (buffer
))
352 /* Else fall through. */
354 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE
:
356 int prefix_length
= strlen (output_prefix (buffer
));
357 output_append_r (buffer
, output_prefix (buffer
), prefix_length
);
358 prefix_was_emitted_for (buffer
) = 1;
365 /* Have BUFFER start a new line. */
368 output_add_newline (buffer
)
369 output_buffer
*buffer
;
371 obstack_1grow (&buffer
->obstack
, '\n');
372 output_text_length (buffer
) = 0;
375 /* Appends a character to BUFFER. */
378 output_add_character (buffer
, c
)
379 output_buffer
*buffer
;
382 if (output_is_line_wrapping (buffer
) && output_space_left (buffer
) <= 0)
383 output_add_newline (buffer
);
384 obstack_1grow (&buffer
->obstack
, c
);
385 ++output_text_length (buffer
);
388 /* Adds a space to BUFFER. */
391 output_add_space (buffer
)
392 output_buffer
*buffer
;
394 if (output_is_line_wrapping (buffer
) && output_space_left (buffer
) <= 0)
396 output_add_newline (buffer
);
399 obstack_1grow (&buffer
->obstack
, ' ');
400 ++output_text_length (buffer
);
403 /* These functions format an INTEGER into BUFFER as suggested by their
407 output_decimal (buffer
, i
)
408 output_buffer
*buffer
;
411 output_formatted_integer (buffer
, "%d", i
);
415 output_long_decimal (buffer
, i
)
416 output_buffer
*buffer
;
419 output_formatted_integer (buffer
, "%ld", i
);
423 output_unsigned_decimal (buffer
, i
)
424 output_buffer
*buffer
;
427 output_formatted_integer (buffer
, "%u", i
);
431 output_long_unsigned_decimal (buffer
, i
)
432 output_buffer
*buffer
;
435 output_formatted_integer (buffer
, "%lu", i
);
439 output_octal (buffer
, i
)
440 output_buffer
*buffer
;
443 output_formatted_integer (buffer
, "%o", i
);
447 output_long_octal (buffer
, i
)
448 output_buffer
*buffer
;
451 output_formatted_integer (buffer
, "%lo", i
);
455 output_hexadecimal (buffer
, i
)
456 output_buffer
*buffer
;
459 output_formatted_integer (buffer
, "%x", i
);
463 output_long_hexadecimal (buffer
, i
)
464 output_buffer
*buffer
;
467 output_formatted_integer (buffer
, "%lx", i
);
470 /* Append to BUFFER a string specified by its STARTING character
474 output_append_r (buffer
, start
, length
)
475 output_buffer
*buffer
;
479 obstack_grow (&buffer
->obstack
, start
, length
);
480 output_text_length (buffer
) += length
;
483 /* Append a string deliminated by START and END to BUFFER. No wrapping is
484 done. However, if beginning a new line then emit output_prefix (BUFFER)
485 and skip any leading whitespace if appropriate. The caller must ensure
486 that it is safe to do so. */
489 output_append (buffer
, start
, end
)
490 output_buffer
*buffer
;
494 /* Emit prefix and skip whitespace if we're starting a new line. */
495 if (is_starting_newline (buffer
))
497 output_emit_prefix (buffer
);
498 if (output_is_line_wrapping (buffer
))
499 while (start
!= end
&& *start
== ' ')
502 output_append_r (buffer
, start
, end
- start
);
505 /* Wrap a text delimited by START and END into BUFFER. */
508 wrap_text (buffer
, start
, end
)
509 output_buffer
*buffer
;
515 /* Dump anything bodered by whitespaces. */
517 const char *p
= start
;
518 while (p
!= end
&& *p
!= ' ' && *p
!= '\n')
520 if (p
- start
>= output_space_left (buffer
))
521 output_add_newline (buffer
);
522 output_append (buffer
, start
, p
);
526 if (start
!= end
&& *start
== ' ')
528 output_add_space (buffer
);
531 if (start
!= end
&& *start
== '\n')
533 output_add_newline (buffer
);
539 /* Same as wrap_text but wrap text only when in line-wrapping mode. */
541 maybe_wrap_text (buffer
, start
, end
)
542 output_buffer
*buffer
;
546 if (output_is_line_wrapping (buffer
))
547 wrap_text (buffer
, start
, end
);
549 output_append (buffer
, start
, end
);
553 /* Append a STRING to BUFFER; the STRING maybe be line-wrapped if in
557 output_add_string (buffer
, str
)
558 output_buffer
*buffer
;
561 maybe_wrap_text (buffer
, str
, str
+ (str
? strlen (str
) : 0));
564 /* Flush the content of BUFFER onto FILE and reinitialize BUFFER. */
567 output_to_stream (buffer
, file
)
568 output_buffer
*buffer
;
571 const char *text
= output_finish (buffer
);
573 clear_text_info (buffer
);
576 /* Format a message pointed to by output_buffer_text_cursor (BUFFER) using
577 output_buffer_format_args (BUFFER) as appropriate. The following format
578 specifiers are recognized as being language independent:
579 %d, %i: (signed) integer in base ten.
580 %u: unsigned integer in base ten.
581 %o: unsigned integer in base eight.
582 %x: unsigned integer in base sixteen.
583 %ld, %li, %lo, %lu, %lx: long versions of the above.
587 %*.s: a substring the length of which is specified by an integer. */
590 output_format (buffer
)
591 output_buffer
*buffer
;
593 for (; *output_buffer_text_cursor (buffer
);
594 ++output_buffer_text_cursor (buffer
))
596 int long_integer
= 0;
600 const char *p
= output_buffer_text_cursor (buffer
);
601 while (*p
&& *p
!= '%')
603 maybe_wrap_text (buffer
, output_buffer_text_cursor (buffer
), p
);
604 output_buffer_text_cursor (buffer
) = p
;
607 if (!*output_buffer_text_cursor (buffer
))
610 /* We got a '%'. Let's see what happens. Record whether we're
611 parsing a long integer format specifier. */
612 if (*++output_buffer_text_cursor (buffer
) == 'l')
615 ++output_buffer_text_cursor (buffer
);
618 /* Handle %c, %d, %i, %ld, %li, %lo, %lu, %lx, %o, %s, %u,
619 %x, %.*s; %%. And nothing else. Front-ends should install
620 printers to grok language specific format specifiers. */
621 switch (*output_buffer_text_cursor (buffer
))
625 (buffer
, va_arg (output_buffer_format_args (buffer
), int));
632 (buffer
, va_arg (output_buffer_format_args (buffer
), long int));
635 (buffer
, va_arg (output_buffer_format_args (buffer
), int));
640 output_long_octal (buffer
,
641 va_arg (output_buffer_format_args (buffer
),
644 output_octal (buffer
,
645 va_arg (output_buffer_format_args (buffer
),
650 output_add_string (buffer
,
651 va_arg (output_buffer_format_args (buffer
),
657 output_long_unsigned_decimal
658 (buffer
, va_arg (output_buffer_format_args (buffer
),
661 output_unsigned_decimal
662 (buffer
, va_arg (output_buffer_format_args (buffer
),
667 output_long_hexadecimal
668 (buffer
, va_arg (output_buffer_format_args (buffer
),
672 (buffer
, va_arg (output_buffer_format_args (buffer
),
677 output_add_character (buffer
, '%');
684 /* We handle no precision specifier but `%.*s'. */
685 if (*++output_buffer_text_cursor (buffer
) != '*')
687 else if (*++output_buffer_text_cursor (buffer
) != 's')
689 n
= va_arg (output_buffer_format_args (buffer
), int);
690 s
= va_arg (output_buffer_format_args (buffer
), const char *);
691 output_append (buffer
, s
, s
+ n
);
696 if (! lang_printer
|| !(*lang_printer
) (buffer
))
698 /* Hmmm. The front-end failed to install a format translator
699 but called us with an unrecognized format. Sorry. */
707 vbuild_message_string (msgid
, ap
)
713 vasprintf (&str
, msgid
, ap
);
717 /* Return a malloc'd string containing MSGID formatted a la
718 printf. The caller is reponsible for freeing the memory. */
721 build_message_string
VPARAMS ((const char *msgid
, ...))
723 #ifndef ANSI_PROTOTYPES
729 VA_START (ap
, msgid
);
731 #ifndef ANSI_PROTOTYPES
732 msgid
= va_arg (ap
, const char *);
735 str
= vbuild_message_string (msgid
, ap
);
743 /* Return a malloc'd string describing a location. The caller is
744 responsible for freeing the memory. */
747 context_as_prefix (file
, line
, warn
)
755 return build_message_string ("%s:%d: warning: ", file
, line
);
757 return build_message_string ("%s:%d: ", file
, line
);
762 return build_message_string ("%s: warning: ", progname
);
764 return build_message_string ("%s: ", progname
);
768 /* Format a MESSAGE into BUFFER. Automatically wrap lines. */
771 output_do_printf (buffer
, msgid
)
772 output_buffer
*buffer
;
775 char *message
= vbuild_message_string (msgid
,
776 output_buffer_format_args (buffer
));
778 output_add_string (buffer
, message
);
783 /* Format a message into BUFFER a la printf. */
786 output_printf
VPARAMS ((struct output_buffer
*buffer
, const char *msgid
, ...))
788 #ifndef ANSI_PROTOTYPES
789 struct output_buffer
*buffer
;
795 VA_START (ap
, msgid
);
796 #ifndef ANSI_PROTOTYPES
797 buffer
= va_arg (ap
, output_buffer
*);
798 msgid
= va_arg (ap
, const char *);
800 old_args
= output_buffer_ptr_to_format_args (buffer
);
801 output_buffer_ptr_to_format_args (buffer
) = &ap
;
802 output_do_printf (buffer
, msgid
);
803 output_buffer_ptr_to_format_args (buffer
) = old_args
;
807 /* Print the message MSGID in FILE. */
810 vnotice (file
, msgid
, ap
)
815 vfprintf (file
, _(msgid
), ap
);
818 /* Print a message relevant to the given DECL. */
821 format_with_decl (buffer
, decl
)
822 output_buffer
*buffer
;
827 /* Do magic to get around lack of varargs support for insertion
828 of arguments into existing list. We know that the decl is first;
829 we ass_u_me that it will be printed with "%s". */
830 for (p
= output_buffer_text_cursor (buffer
); *p
; ++p
)
836 else if (*(p
+ 1) != 's')
843 /* Print the left-hand substring. */
844 maybe_wrap_text (buffer
, output_buffer_text_cursor (buffer
), p
);
846 if (*p
== '%') /* Print the name. */
848 const char *n
= (DECL_NAME (decl
)
849 ? (*decl_printable_name
) (decl
, 2)
850 : _("((anonymous))"));
851 output_add_string (buffer
, n
);
855 if (ISALPHA (*(p
- 1) & 0xFF))
860 if (*p
) /* Print the rest of the message. */
862 output_buffer_text_cursor (buffer
) = p
;
863 output_format (buffer
);
867 /* Figure file and line of the given INSN. */
870 file_and_line_for_asm (insn
, pfile
, pline
)
875 rtx body
= PATTERN (insn
);
878 /* Find the (or one of the) ASM_OPERANDS in the insn. */
879 if (GET_CODE (body
) == SET
&& GET_CODE (SET_SRC (body
)) == ASM_OPERANDS
)
880 asmop
= SET_SRC (body
);
881 else if (GET_CODE (body
) == ASM_OPERANDS
)
883 else if (GET_CODE (body
) == PARALLEL
884 && GET_CODE (XVECEXP (body
, 0, 0)) == SET
)
885 asmop
= SET_SRC (XVECEXP (body
, 0, 0));
886 else if (GET_CODE (body
) == PARALLEL
887 && GET_CODE (XVECEXP (body
, 0, 0)) == ASM_OPERANDS
)
888 asmop
= XVECEXP (body
, 0, 0);
894 *pfile
= ASM_OPERANDS_SOURCE_FILE (asmop
);
895 *pline
= ASM_OPERANDS_SOURCE_LINE (asmop
);
899 *pfile
= input_filename
;
904 /* Report a diagnostic MESSAGE (an errror or a WARNING) at the line number
905 of the insn INSN. This is used only when INSN is an `asm' with operands,
906 and each ASM_OPERANDS records its own source file and line. */
909 diagnostic_for_asm (insn
, msg
, args_ptr
, warn
)
918 file_and_line_for_asm (insn
, &file
, &line
);
919 report_diagnostic (msg
, args_ptr
, file
, line
, warn
);
922 /* Report a diagnostic MESSAGE at the declaration DECL.
923 MSG is a format string which uses %s to substitute the declaration
924 name; subsequent substitutions are a la output_format. */
927 diagnostic_for_decl (decl
, msg
, args_ptr
, warn
)
935 if (diagnostic_lock
++)
938 if (count_error (warn
))
940 os
= diagnostic_buffer
->state
;
941 report_error_function (DECL_SOURCE_FILE (decl
));
943 (diagnostic_buffer
, context_as_prefix
944 (DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
), warn
));
945 output_buffer_ptr_to_format_args (diagnostic_buffer
) = args_ptr
;
946 output_buffer_text_cursor (diagnostic_buffer
) = msg
;
947 format_with_decl (diagnostic_buffer
, decl
);
948 finish_diagnostic ();
949 output_destroy_prefix (diagnostic_buffer
);
951 diagnostic_buffer
->state
= os
;
957 /* Count an error or warning. Return 1 if the message should be printed. */
960 count_error (warningp
)
963 if (warningp
&& inhibit_warnings
)
966 if (warningp
&& !warnings_are_errors
)
970 static int warning_message
= 0;
972 if (warningp
&& !warning_message
)
974 verbatim ("%s: warnings being treated as errors\n", progname
);
983 /* Print a diagnistic MSGID on FILE. */
986 fnotice
VPARAMS ((FILE *file
, const char *msgid
, ...))
988 #ifndef ANSI_PROTOTYPES
994 VA_START (ap
, msgid
);
996 #ifndef ANSI_PROTOTYPES
997 file
= va_arg (ap
, FILE *);
998 msgid
= va_arg (ap
, const char *);
1001 vnotice (file
, msgid
, ap
);
1006 /* Print a fatal error message. NAME is the text.
1007 Also include a system error message based on `errno'. */
1010 pfatal_with_name (name
)
1013 fprintf (stderr
, "%s: ", progname
);
1015 exit (FATAL_EXIT_CODE
);
1019 fatal_io_error (name
)
1022 verbatim ("%s: %s: I/O error\n", progname
, name
);
1023 exit (FATAL_EXIT_CODE
);
1026 /* Issue a pedantic warning MSGID. */
1029 pedwarn
VPARAMS ((const char *msgid
, ...))
1031 #ifndef ANSI_PROTOTYPES
1036 VA_START (ap
, msgid
);
1038 #ifndef ANSI_PROTOTYPES
1039 msgid
= va_arg (ap
, const char *);
1042 report_diagnostic (msgid
, &ap
, input_filename
, lineno
,
1043 !flag_pedantic_errors
);
1047 /* Issue a pedantic waring about DECL. */
1050 pedwarn_with_decl
VPARAMS ((tree decl
, const char *msgid
, ...))
1052 #ifndef ANSI_PROTOTYPES
1058 VA_START (ap
, msgid
);
1060 #ifndef ANSI_PROTOTYPES
1061 decl
= va_arg (ap
, tree
);
1062 msgid
= va_arg (ap
, const char *);
1064 /* We don't want -pedantic-errors to cause the compilation to fail from
1065 "errors" in system header files. Sometimes fixincludes can't fix what's
1066 broken (eg: unsigned char bitfields - fixing it may change the alignment
1067 which will cause programs to mysteriously fail because the C library
1068 or kernel uses the original layout). There's no point in issuing a
1069 warning either, it's just unnecessary noise. */
1070 if (!DECL_IN_SYSTEM_HEADER (decl
))
1071 diagnostic_for_decl (decl
, msgid
, &ap
, !flag_pedantic_errors
);
1075 /* Same as above but within the context FILE and LINE. */
1078 pedwarn_with_file_and_line
VPARAMS ((const char *file
, int line
,
1079 const char *msgid
, ...))
1081 #ifndef ANSI_PROTOTYPES
1088 VA_START (ap
, msgid
);
1090 #ifndef ANSI_PROTOTYPES
1091 file
= va_arg (ap
, const char *);
1092 line
= va_arg (ap
, int);
1093 msgid
= va_arg (ap
, const char *);
1096 report_diagnostic (msgid
, &ap
, file
, line
, !flag_pedantic_errors
);
1100 /* Just apologize with MSGID. */
1103 sorry
VPARAMS ((const char *msgid
, ...))
1105 #ifndef ANSI_PROTOTYPES
1111 os
= diagnostic_buffer
->state
;
1112 VA_START (ap
, msgid
);
1114 #ifndef ANSI_PROTOTYPES
1115 msgid
= va_arg (ap
, const char *);
1119 (diagnostic_buffer
, context_as_prefix (input_filename
, lineno
, 0));
1120 output_printf (diagnostic_buffer
, "sorry, not implemented: ");
1121 output_buffer_ptr_to_format_args (diagnostic_buffer
) = &ap
;
1122 output_buffer_text_cursor (diagnostic_buffer
) = msgid
;
1123 output_format (diagnostic_buffer
);
1124 finish_diagnostic ();
1125 diagnostic_buffer
->state
= os
;
1129 /* Called when the start of a function definition is parsed,
1130 this function prints on stderr the name of the function. */
1133 announce_function (decl
)
1138 if (rtl_dump_and_exit
)
1139 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl
)));
1141 verbatim (" %s", (*decl_printable_name
) (decl
, 2));
1143 need_error_newline
= 1;
1144 last_error_function
= current_function_decl
;
1148 /* The default function to print out name of current function that caused
1152 default_print_error_function (file
)
1155 if (last_error_function
!= current_function_decl
)
1157 char *prefix
= file
? build_message_string ("%s: ", file
) : NULL
;
1160 os
= diagnostic_buffer
->state
;
1161 output_set_prefix (diagnostic_buffer
, prefix
);
1163 if (current_function_decl
== NULL
)
1165 output_add_string (diagnostic_buffer
, "At top level:");
1166 output_add_newline (diagnostic_buffer
);
1170 if (TREE_CODE (TREE_TYPE (current_function_decl
)) == METHOD_TYPE
)
1172 (diagnostic_buffer
, "In method `%s':\n",
1173 (*decl_printable_name
) (current_function_decl
, 2));
1176 (diagnostic_buffer
, "In function `%s':\n",
1177 (*decl_printable_name
) (current_function_decl
, 2));
1180 last_error_function
= current_function_decl
;
1181 output_to_stream (diagnostic_buffer
, stderr
);
1182 diagnostic_buffer
->state
= os
;
1183 free ((char*) prefix
);
1187 /* Prints out, if necessary, the name of the current function
1188 that caused an error. Called from all error and warning functions.
1189 We ignore the FILE parameter, as it cannot be relied upon. */
1192 report_error_function (file
)
1193 const char *file ATTRIBUTE_UNUSED
;
1195 struct file_stack
*p
;
1197 if (need_error_newline
)
1200 need_error_newline
= 0;
1203 if (input_file_stack
&& input_file_stack
->next
!= 0
1204 && input_file_stack_tick
!= last_error_tick
)
1206 for (p
= input_file_stack
->next
; p
; p
= p
->next
)
1207 if (p
== input_file_stack
->next
)
1208 verbatim ("In file included from %s:%d", p
->name
, p
->line
);
1210 verbatim (",\n from %s:%d", p
->name
, p
->line
);
1212 last_error_tick
= input_file_stack_tick
;
1215 (*print_error_function
) (input_filename
);
1219 error_with_file_and_line
VPARAMS ((const char *file
, int line
,
1220 const char *msgid
, ...))
1222 #ifndef ANSI_PROTOTYPES
1229 VA_START (ap
, msgid
);
1231 #ifndef ANSI_PROTOTYPES
1232 file
= va_arg (ap
, const char *);
1233 line
= va_arg (ap
, int);
1234 msgid
= va_arg (ap
, const char *);
1237 report_diagnostic (msgid
, &ap
, file
, line
, /* warn = */ 0);
1242 error_with_decl
VPARAMS ((tree decl
, const char *msgid
, ...))
1244 #ifndef ANSI_PROTOTYPES
1250 VA_START (ap
, msgid
);
1252 #ifndef ANSI_PROTOTYPES
1253 decl
= va_arg (ap
, tree
);
1254 msgid
= va_arg (ap
, const char *);
1257 diagnostic_for_decl (decl
, msgid
, &ap
, /* warn = */ 0);
1262 error_for_asm
VPARAMS ((rtx insn
, const char *msgid
, ...))
1264 #ifndef ANSI_PROTOTYPES
1270 VA_START (ap
, msgid
);
1272 #ifndef ANSI_PROTOTYPES
1273 insn
= va_arg (ap
, rtx
);
1274 msgid
= va_arg (ap
, const char *);
1277 diagnostic_for_asm (insn
, msgid
, &ap
, /* warn = */ 0);
1282 error
VPARAMS ((const char *msgid
, ...))
1284 #ifndef ANSI_PROTOTYPES
1289 VA_START (ap
, msgid
);
1291 #ifndef ANSI_PROTOTYPES
1292 msgid
= va_arg (ap
, const char *);
1295 report_diagnostic (msgid
, &ap
, input_filename
, lineno
, /* warn = */ 0);
1299 /* Report a fatal error at the current line number. Allow a front end to
1300 intercept the message. */
1302 static void (*fatal_function
) PARAMS((const char *, va_list *));
1304 /* Set the function to call when a fatal error occurs. */
1307 set_fatal_function (f
)
1308 void (*f
) PARAMS ((const char *, va_list *));
1314 fatal
VPARAMS ((const char *msgid
, ...))
1316 #ifndef ANSI_PROTOTYPES
1321 VA_START (ap
, msgid
);
1323 #ifndef ANSI_PROTOTYPES
1324 msgid
= va_arg (ap
, const char *);
1327 if (fatal_function
!= 0)
1328 (*fatal_function
) (_(msgid
), &ap
);
1330 report_diagnostic (msgid
, &ap
, input_filename
, lineno
, 0);
1332 exit (FATAL_EXIT_CODE
);
1336 _fatal_insn (msgid
, insn
, file
, line
, function
)
1341 const char *function
;
1343 error ("%s", msgid
);
1345 fancy_abort (file
, line
, function
);
1349 _fatal_insn_not_found (insn
, file
, line
, function
)
1353 const char *function
;
1355 if (INSN_CODE (insn
) < 0)
1356 _fatal_insn ("Unrecognizable insn:", insn
, file
, line
, function
);
1358 _fatal_insn ("Insn does not satisfy its constraints:",
1359 insn
, file
, line
, function
);
1363 warning_with_file_and_line
VPARAMS ((const char *file
, int line
,
1364 const char *msgid
, ...))
1366 #ifndef ANSI_PROTOTYPES
1373 VA_START (ap
, msgid
);
1375 #ifndef ANSI_PROTOTYPES
1376 file
= va_arg (ap
, const char *);
1377 line
= va_arg (ap
, int);
1378 msgid
= va_arg (ap
, const char *);
1381 report_diagnostic (msgid
, &ap
, file
, line
, /* warn = */ 1);
1386 warning_with_decl
VPARAMS ((tree decl
, const char *msgid
, ...))
1388 #ifndef ANSI_PROTOTYPES
1394 VA_START (ap
, msgid
);
1396 #ifndef ANSI_PROTOTYPES
1397 decl
= va_arg (ap
, tree
);
1398 msgid
= va_arg (ap
, const char *);
1401 diagnostic_for_decl (decl
, msgid
, &ap
, /* warn = */ 1);
1406 warning_for_asm
VPARAMS ((rtx insn
, const char *msgid
, ...))
1408 #ifndef ANSI_PROTOTYPES
1414 VA_START (ap
, msgid
);
1416 #ifndef ANSI_PROTOTYPES
1417 insn
= va_arg (ap
, rtx
);
1418 msgid
= va_arg (ap
, const char *);
1421 diagnostic_for_asm (insn
, msgid
, &ap
, /* warn = */ 1);
1426 warning
VPARAMS ((const char *msgid
, ...))
1428 #ifndef ANSI_PROTOTYPES
1433 VA_START (ap
, msgid
);
1435 #ifndef ANSI_PROTOTYPES
1436 msgid
= va_arg (ap
, const char *);
1439 report_diagnostic (msgid
, &ap
, input_filename
, lineno
, /* warn = */ 1);
1443 /* Flush diagnostic_buffer content on stderr. */
1445 finish_diagnostic ()
1447 output_to_stream (diagnostic_buffer
, stderr
);
1448 clear_diagnostic_info (diagnostic_buffer
);
1449 fputc ('\n', stderr
);
1453 /* Helper subroutine of output_verbatim and verbatim. Do the approriate
1454 settings needed by BUFFER for a verbatim formatting. */
1456 output_do_verbatim (buffer
, msg
, args_ptr
)
1457 output_buffer
*buffer
;
1464 output_prefix (buffer
) = NULL
;
1465 prefixing_policy (buffer
) = DIAGNOSTICS_SHOW_PREFIX_NEVER
;
1466 output_buffer_text_cursor (buffer
) = msg
;
1467 output_buffer_ptr_to_format_args (buffer
) = args_ptr
;
1468 output_set_maximum_length (buffer
, 0);
1469 output_format (buffer
);
1473 /* Output MESSAGE verbatim into BUFFER. */
1475 output_verbatim
VPARAMS ((output_buffer
*buffer
, const char *msg
, ...))
1477 #ifndef ANSI_PROTOTYPES
1478 output_buffer
*buffer
;
1484 #ifndef ANSI_PROTOTYPES
1485 buffer
= va_arg (ap
, output_buffer
*);
1486 msg
= va_arg (ap
, const char *);
1488 output_do_verbatim (buffer
, msg
, &ap
);
1492 /* Same as above but use diagnostic_buffer. */
1494 verbatim
VPARAMS ((const char *msg
, ...))
1496 #ifndef ANSI_PROTOTYPES
1502 #ifndef ANSI_PROTOTYPES
1503 msg
= va_arg (ap
, const char *);
1505 output_do_verbatim (diagnostic_buffer
, msg
, &ap
);
1506 output_to_stream (diagnostic_buffer
, stderr
);
1510 /* Report a diagnostic MESSAGE (an error or a WARNING) involving
1511 entities in ARGUMENTS. FILE and LINE indicate where the diagnostic
1512 occurs. This function is *the* subroutine in terms of which front-ends
1513 should implement their specific diagnostic handling modules.
1514 The front-end independent format specifiers are exactly those described
1515 in the documentation of output_format. */
1517 report_diagnostic (msg
, args_ptr
, file
, line
, warn
)
1526 if (diagnostic_lock
++)
1529 if (count_error (warn
))
1531 os
= diagnostic_buffer
->state
;
1532 diagnostic_msg
= msg
;
1533 diagnostic_args
= args_ptr
;
1534 report_error_function (file
);
1536 (diagnostic_buffer
, context_as_prefix (file
, line
, warn
));
1537 output_format (diagnostic_buffer
);
1538 finish_diagnostic ();
1539 output_destroy_prefix (diagnostic_buffer
);
1540 diagnostic_buffer
->state
= os
;
1546 /* Inform the user that an error occurred while trying to report some
1547 other error. This indicates catastrophic internal inconsistencies,
1548 so give up now. But do try to flush out the previous error. */
1552 if (diagnostic_lock
< 3)
1553 finish_diagnostic ();
1556 "Internal compiler error: Error reporting routines re-entered.\n\
1557 Please submit a full bug report.\n\
1558 See %s for instructions.\n", GCCBUGURL
);
1560 exit (FATAL_EXIT_CODE
);
1563 /* Given a partial pathname as input, return another pathname that
1564 shares no directory elements with the pathname of __FILE__. This
1565 is used by fancy_abort() to print `Internal compiler error in expr.c'
1566 instead of `Internal compiler error in ../../egcs/gcc/expr.c'. */
1568 trim_filename (name
)
1571 static const char this_file
[] = __FILE__
;
1572 const char *p
= name
, *q
= this_file
;
1574 while (*p
== *q
&& *p
!= 0 && *q
!= 0) p
++, q
++;
1575 while (p
> name
&& p
[-1] != DIR_SEPARATOR
1576 #ifdef DIR_SEPARATOR_2
1577 && p
[-1] != DIR_SEPARATOR_2
1585 /* Report an internal compiler error in a friendly manner and without
1589 fancy_abort (file
, line
, function
)
1592 const char *function
;
1595 "Internal compiler error in %s, at %s:%d\n\
1596 Please submit a full bug report.\n\
1597 See %s for instructions.",
1598 function
, trim_filename (file
), line
, GCCBUGURL
);