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 diagnostic_finish
PARAMS ((output_buffer
*));
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 ((diagnostic_context
*, 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
) = &ap
;
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 diagnostic_finish ((output_buffer
*)global_dc
);
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
) = &ap
;
1069 output_buffer_text_cursor (diagnostic_buffer
) = _(msgid
);
1070 output_format (diagnostic_buffer
);
1071 diagnostic_finish ((output_buffer
*)global_dc
);
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
) = &ap
;
1177 output_buffer_text_cursor (diagnostic_buffer
) = _(msgid
);
1178 output_format (diagnostic_buffer
);
1179 diagnostic_finish ((output_buffer
*)global_dc
);
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 (context
, file
)
1208 diagnostic_context
*context
;
1211 if (error_function_changed ())
1213 char *prefix
= file
? build_message_string ("%s: ", file
) : NULL
;
1216 os
= output_buffer_state (context
);
1217 output_set_prefix ((output_buffer
*)context
, prefix
);
1219 if (current_function_decl
== NULL
)
1220 output_add_string ((output_buffer
*)context
, _("At top level:"));
1223 if (TREE_CODE (TREE_TYPE (current_function_decl
)) == METHOD_TYPE
)
1225 ((output_buffer
*)context
, "In member function `%s':",
1226 (*decl_printable_name
) (current_function_decl
, 2));
1229 ((output_buffer
*)context
, "In function `%s':",
1230 (*decl_printable_name
) (current_function_decl
, 2));
1232 output_add_newline ((output_buffer
*)context
);
1234 record_last_error_function ();
1235 output_buffer_to_stream ((output_buffer
*)context
);
1236 output_buffer_state (context
) = os
;
1237 free ((char*) prefix
);
1241 /* Prints out, if necessary, the name of the current function
1242 that caused an error. Called from all error and warning functions.
1243 We ignore the FILE parameter, as it cannot be relied upon. */
1246 report_error_function (file
)
1247 const char *file ATTRIBUTE_UNUSED
;
1249 report_problematic_module ((output_buffer
*)global_dc
);
1250 (*print_error_function
) (global_dc
, input_filename
);
1254 error_with_file_and_line
VPARAMS ((const char *file
, int line
,
1255 const char *msgid
, ...))
1257 #ifndef ANSI_PROTOTYPES
1263 diagnostic_context dc
;
1265 VA_START (ap
, msgid
);
1267 #ifndef ANSI_PROTOTYPES
1268 file
= va_arg (ap
, const char *);
1269 line
= va_arg (ap
, int);
1270 msgid
= va_arg (ap
, const char *);
1273 set_diagnostic_context (&dc
, msgid
, &ap
, file
, line
, /* warn = */ 0);
1274 report_diagnostic (&dc
);
1279 error_with_decl
VPARAMS ((tree decl
, const char *msgid
, ...))
1281 #ifndef ANSI_PROTOTYPES
1287 VA_START (ap
, msgid
);
1289 #ifndef ANSI_PROTOTYPES
1290 decl
= va_arg (ap
, tree
);
1291 msgid
= va_arg (ap
, const char *);
1294 diagnostic_for_decl (decl
, msgid
, &ap
, /* warn = */ 0);
1299 error_for_asm
VPARAMS ((rtx insn
, const char *msgid
, ...))
1301 #ifndef ANSI_PROTOTYPES
1307 VA_START (ap
, msgid
);
1309 #ifndef ANSI_PROTOTYPES
1310 insn
= va_arg (ap
, rtx
);
1311 msgid
= va_arg (ap
, const char *);
1314 diagnostic_for_asm (insn
, msgid
, &ap
, /* warn = */ 0);
1318 /* Report an error message. The arguments are like that of printf. */
1321 error
VPARAMS ((const char *msgid
, ...))
1323 #ifndef ANSI_PROTOTYPES
1327 diagnostic_context dc
;
1329 VA_START (ap
, msgid
);
1331 #ifndef ANSI_PROTOTYPES
1332 msgid
= va_arg (ap
, const char *);
1335 set_diagnostic_context
1336 (&dc
, msgid
, &ap
, input_filename
, lineno
, /* warn = */ 0);
1337 report_diagnostic (&dc
);
1341 /* Likewise, except that the compilation is terminated after printing the
1345 fatal_error
VPARAMS ((const char *msgid
, ...))
1347 #ifndef ANSI_PROTOTYPES
1351 diagnostic_context dc
;
1353 VA_START (ap
, msgid
);
1355 #ifndef ANSI_PROTOTYPES
1356 msgid
= va_arg (ap
, const char *);
1359 set_diagnostic_context
1360 (&dc
, msgid
, &ap
, input_filename
, lineno
, /* warn = */ 0);
1361 report_diagnostic (&dc
);
1364 fnotice (stderr
, "compilation terminated.\n");
1365 exit (FATAL_EXIT_CODE
);
1368 /* Report a compiler error at the current line number. Allow a front end to
1369 intercept the message. */
1371 static void (*internal_error_function
) PARAMS ((const char *, va_list *));
1373 /* Set the function to call when a compiler error occurs. */
1376 set_internal_error_function (f
)
1377 void (*f
) PARAMS ((const char *, va_list *));
1379 internal_error_function
= f
;
1383 internal_error
VPARAMS ((const char *msgid
, ...))
1385 #ifndef ANSI_PROTOTYPES
1389 diagnostic_context dc
;
1391 VA_START (ap
, msgid
);
1393 #ifndef ANSI_PROTOTYPES
1394 msgid
= va_arg (ap
, const char *);
1397 if (errorcount
> 0 || sorrycount
> 0)
1399 fnotice (stderr
, "%s:%d: confused by earlier errors, bailing out\n",
1400 input_filename
, lineno
);
1401 exit (FATAL_EXIT_CODE
);
1404 if (internal_error_function
!= 0)
1405 (*internal_error_function
) (_(msgid
), &ap
);
1407 set_diagnostic_context
1408 (&dc
, msgid
, &ap
, input_filename
, lineno
, /* warn = */0);
1409 report_diagnostic (&dc
);
1413 "Please submit a full bug report,\n\
1414 with preprocessed source if appropriate.\n\
1415 See %s for instructions.\n", GCCBUGURL
);
1416 exit (FATAL_EXIT_CODE
);
1420 _fatal_insn (msgid
, insn
, file
, line
, function
)
1425 const char *function
;
1427 error ("%s", _(msgid
));
1429 /* The above incremented error_count, but isn't an error that we want to
1430 count, so reset it here. */
1434 fancy_abort (file
, line
, function
);
1438 _fatal_insn_not_found (insn
, file
, line
, function
)
1442 const char *function
;
1444 if (INSN_CODE (insn
) < 0)
1445 _fatal_insn ("Unrecognizable insn:", insn
, file
, line
, function
);
1447 _fatal_insn ("Insn does not satisfy its constraints:",
1448 insn
, file
, line
, function
);
1452 warning_with_file_and_line
VPARAMS ((const char *file
, int line
,
1453 const char *msgid
, ...))
1455 #ifndef ANSI_PROTOTYPES
1461 diagnostic_context dc
;
1463 VA_START (ap
, msgid
);
1465 #ifndef ANSI_PROTOTYPES
1466 file
= va_arg (ap
, const char *);
1467 line
= va_arg (ap
, int);
1468 msgid
= va_arg (ap
, const char *);
1471 set_diagnostic_context (&dc
, msgid
, &ap
, file
, line
, /* warn = */ 1);
1472 report_diagnostic (&dc
);
1477 warning_with_decl
VPARAMS ((tree decl
, const char *msgid
, ...))
1479 #ifndef ANSI_PROTOTYPES
1485 VA_START (ap
, msgid
);
1487 #ifndef ANSI_PROTOTYPES
1488 decl
= va_arg (ap
, tree
);
1489 msgid
= va_arg (ap
, const char *);
1492 diagnostic_for_decl (decl
, msgid
, &ap
, /* warn = */ 1);
1497 warning_for_asm
VPARAMS ((rtx insn
, const char *msgid
, ...))
1499 #ifndef ANSI_PROTOTYPES
1505 VA_START (ap
, msgid
);
1507 #ifndef ANSI_PROTOTYPES
1508 insn
= va_arg (ap
, rtx
);
1509 msgid
= va_arg (ap
, const char *);
1512 diagnostic_for_asm (insn
, msgid
, &ap
, /* warn = */ 1);
1517 warning
VPARAMS ((const char *msgid
, ...))
1519 #ifndef ANSI_PROTOTYPES
1523 diagnostic_context dc
;
1525 VA_START (ap
, msgid
);
1527 #ifndef ANSI_PROTOTYPES
1528 msgid
= va_arg (ap
, const char *);
1531 set_diagnostic_context
1532 (&dc
, msgid
, &ap
, input_filename
, lineno
, /* warn = */ 1);
1533 report_diagnostic (&dc
);
1537 /* Flush diagnostic_buffer content on stderr. */
1540 diagnostic_finish (buffer
)
1541 output_buffer
*buffer
;
1543 output_buffer_to_stream (buffer
);
1544 clear_diagnostic_info (buffer
);
1545 fputc ('\n', output_buffer_attached_stream (buffer
));
1546 fflush (output_buffer_attached_stream (buffer
));
1549 /* Helper subroutine of output_verbatim and verbatim. Do the approriate
1550 settings needed by BUFFER for a verbatim formatting. */
1553 output_do_verbatim (buffer
, msgid
, args_ptr
)
1554 output_buffer
*buffer
;
1560 os
= output_buffer_state (buffer
);
1561 output_prefix (buffer
) = NULL
;
1562 diagnostic_prefixing_rule (buffer
) = DIAGNOSTICS_SHOW_PREFIX_NEVER
;
1563 output_buffer_text_cursor (buffer
) = _(msgid
);
1564 output_buffer_ptr_to_format_args (buffer
) = args_ptr
;
1565 output_set_maximum_length (buffer
, 0);
1566 output_format (buffer
);
1567 output_buffer_state (buffer
) = os
;
1570 /* Output MESSAGE verbatim into BUFFER. */
1573 output_verbatim
VPARAMS ((output_buffer
*buffer
, const char *msgid
, ...))
1575 #ifndef ANSI_PROTOTYPES
1576 output_buffer
*buffer
;
1581 VA_START (ap
, msgid
);
1582 #ifndef ANSI_PROTOTYPES
1583 buffer
= va_arg (ap
, output_buffer
*);
1584 msg
= va_arg (ap
, const char *);
1586 output_do_verbatim (buffer
, msgid
, &ap
);
1590 /* Same as above but use diagnostic_buffer. */
1593 verbatim
VPARAMS ((const char *msgid
, ...))
1595 #ifndef ANSI_PROTOTYPES
1600 VA_START (ap
, msgid
);
1601 #ifndef ANSI_PROTOTYPES
1602 msgid
= va_arg (ap
, const char *);
1604 output_do_verbatim (diagnostic_buffer
, msgid
, &ap
);
1605 output_buffer_to_stream (diagnostic_buffer
);
1609 /* Report a diagnostic message (an error or a warning) as specified by
1610 DC. This function is *the* subroutine in terms of which front-ends
1611 should implement their specific diagnostic handling modules. The
1612 front-end independent format specifiers are exactly those described
1613 in the documentation of output_format. */
1616 report_diagnostic (dc
)
1617 diagnostic_context
*dc
;
1621 if (diagnostic_lock
++)
1624 if (count_error (diagnostic_is_warning (dc
)))
1626 os
= output_buffer_state (diagnostic_buffer
);
1627 diagnostic_msg
= diagnostic_message (dc
);
1628 diagnostic_args
= diagnostic_argument_list (dc
);
1629 (*diagnostic_starter (dc
)) (diagnostic_buffer
, dc
);
1630 output_format (diagnostic_buffer
);
1631 (*diagnostic_finalizer (dc
)) (diagnostic_buffer
, dc
);
1632 diagnostic_finish ((output_buffer
*)global_dc
);
1633 output_buffer_state (diagnostic_buffer
) = os
;
1639 /* Inform the user that an error occurred while trying to report some
1640 other error. This indicates catastrophic internal inconsistencies,
1641 so give up now. But do try to flush out the previous error.
1642 This mustn't use internal_error, that will cause infinite recursion. */
1647 if (diagnostic_lock
< 3)
1648 diagnostic_finish ((output_buffer
*)global_dc
);
1651 "Internal compiler error: Error reporting routines re-entered.\n");
1653 "Please submit a full bug report,\n\
1654 with preprocessed source if appropriate.\n\
1655 See %s for instructions.\n", GCCBUGURL
);
1656 exit (FATAL_EXIT_CODE
);
1659 /* Given a partial pathname as input, return another pathname that
1660 shares no directory elements with the pathname of __FILE__. This
1661 is used by fancy_abort() to print `Internal compiler error in expr.c'
1662 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1665 trim_filename (name
)
1668 static const char this_file
[] = __FILE__
;
1669 const char *p
= name
, *q
= this_file
;
1671 /* First skip any "../" in each filename. This allows us to give a proper
1672 reference to a file in a subdirectory. */
1673 while (p
[0] == '.' && p
[1] == '.'
1674 && (p
[2] == DIR_SEPARATOR
1675 #ifdef DIR_SEPARATOR_2
1676 || p
[2] == DIR_SEPARATOR_2
1681 while (q
[0] == '.' && q
[1] == '.'
1682 && (q
[2] == DIR_SEPARATOR
1683 #ifdef DIR_SEPARATOR_2
1684 || p
[2] == DIR_SEPARATOR_2
1689 /* Now skip any parts the two filenames have in common. */
1690 while (*p
== *q
&& *p
!= 0 && *q
!= 0)
1693 /* Now go backwards until the previous directory separator. */
1694 while (p
> name
&& p
[-1] != DIR_SEPARATOR
1695 #ifdef DIR_SEPARATOR_2
1696 && p
[-1] != DIR_SEPARATOR_2
1704 /* Report an internal compiler error in a friendly manner and without
1708 fancy_abort (file
, line
, function
)
1711 const char *function
;
1713 internal_error ("Internal compiler error in %s, at %s:%d",
1714 function
, trim_filename (file
), line
);
1717 /* Setup DC for reporting a diagnostic MESSAGE (an error or a WARNING),
1718 using arguments pointed to by ARGS_PTR, issued at a location specified
1719 by FILE and LINE. */
1722 set_diagnostic_context (dc
, msgid
, args_ptr
, file
, line
, warn
)
1723 diagnostic_context
*dc
;
1730 memset (dc
, 0, sizeof (diagnostic_context
));
1731 diagnostic_message (dc
) = _(msgid
);
1732 diagnostic_argument_list (dc
) = args_ptr
;
1733 diagnostic_file_location (dc
) = file
;
1734 diagnostic_line_location (dc
) = line
;
1735 diagnostic_is_warning (dc
) = warn
;
1736 diagnostic_starter (dc
) = diagnostic_starter (global_dc
);
1737 diagnostic_finalizer (dc
) = diagnostic_finalizer (global_dc
);
1741 report_problematic_module (buffer
)
1742 output_buffer
*buffer
;
1744 struct file_stack
*p
;
1746 if (output_needs_newline (buffer
))
1748 output_add_newline (buffer
);
1749 output_needs_newline (buffer
) = 0;
1752 if (input_file_stack
&& input_file_stack
->next
!= 0
1753 && error_module_changed ())
1755 for (p
= input_file_stack
->next
; p
; p
= p
->next
)
1756 if (p
== input_file_stack
->next
)
1758 (buffer
, "In file included from %s:%d", p
->name
, p
->line
);
1761 (buffer
, ",\n from %s:%d", p
->name
, p
->line
);
1762 output_verbatim (buffer
, ":\n");
1763 record_last_error_module ();
1768 default_diagnostic_starter (buffer
, dc
)
1769 output_buffer
*buffer
;
1770 diagnostic_context
*dc
;
1772 report_error_function (diagnostic_file_location (dc
));
1773 output_set_prefix (buffer
,
1774 context_as_prefix (diagnostic_file_location (dc
),
1775 diagnostic_line_location (dc
),
1776 diagnostic_is_warning (dc
)));
1780 default_diagnostic_finalizer (buffer
, dc
)
1781 output_buffer
*buffer
;
1782 diagnostic_context
*dc
__attribute__((__unused__
));
1784 output_destroy_prefix (buffer
);