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 GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
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 "diagnostic.h"
38 #define obstack_chunk_alloc xmalloc
39 #define obstack_chunk_free free
41 #define output_formatted_integer(BUFFER, FORMAT, INTEGER) \
43 sprintf ((BUFFER)->digit_buffer, FORMAT, INTEGER); \
44 output_add_string (BUFFER, (BUFFER)->digit_buffer); \
47 #define output_text_length(BUFFER) (BUFFER)->line_length
48 #define is_starting_newline(BUFFER) (output_text_length (BUFFER) == 0)
49 #define output_prefix(BUFFER) (BUFFER)->state.prefix
50 #define line_wrap_cutoff(BUFFER) (BUFFER)->state.maximum_length
51 #define prefix_was_emitted_for(BUFFER) (BUFFER)->state.emitted_prefix_p
52 #define output_buffer_ptr_to_format_args(BUFFER) (BUFFER)->state.format_args
54 #define diagnostic_args output_buffer_ptr_to_format_args (diagnostic_buffer)
55 #define diagnostic_msg output_buffer_text_cursor (diagnostic_buffer)
58 static void diagnostic_finish
PARAMS ((output_buffer
*));
59 static void output_do_verbatim
PARAMS ((output_buffer
*,
60 const char *, va_list *));
61 static void output_buffer_to_stream
PARAMS ((output_buffer
*));
62 static void output_format
PARAMS ((output_buffer
*));
63 static void output_indent
PARAMS ((output_buffer
*));
65 static char *vbuild_message_string
PARAMS ((const char *, va_list))
66 ATTRIBUTE_PRINTF (1, 0);
67 static char *build_message_string
PARAMS ((const char *, ...))
69 static void output_do_printf
PARAMS ((output_buffer
*, const char *))
70 ATTRIBUTE_PRINTF (2, 0);
71 static void format_with_decl
PARAMS ((output_buffer
*, tree
));
72 static void diagnostic_for_decl
PARAMS ((tree
, const char *, va_list *, int));
73 static void set_real_maximum_length
PARAMS ((output_buffer
*));
75 static void output_unsigned_decimal
PARAMS ((output_buffer
*, unsigned int));
76 static void output_long_decimal
PARAMS ((output_buffer
*, long int));
77 static void output_long_unsigned_decimal
PARAMS ((output_buffer
*,
79 static void output_octal
PARAMS ((output_buffer
*, unsigned int));
80 static void output_long_octal
PARAMS ((output_buffer
*, unsigned long int));
81 static void output_hexadecimal
PARAMS ((output_buffer
*, unsigned int));
82 static void output_long_hexadecimal
PARAMS ((output_buffer
*,
84 static void output_append_r
PARAMS ((output_buffer
*, const char *, int));
85 static void wrap_text
PARAMS ((output_buffer
*, const char *, const char *));
86 static void maybe_wrap_text
PARAMS ((output_buffer
*, const char *,
88 static void clear_diagnostic_info
PARAMS ((output_buffer
*));
90 static void default_diagnostic_starter
PARAMS ((output_buffer
*,
91 diagnostic_context
*));
92 static void default_diagnostic_finalizer
PARAMS ((output_buffer
*,
93 diagnostic_context
*));
95 static void error_recursion
PARAMS ((void)) ATTRIBUTE_NORETURN
;
97 extern int rtl_dump_and_exit
;
98 extern int warnings_are_errors
;
100 /* A diagnostic_context surrogate for stderr. */
101 static diagnostic_context global_diagnostic_context
;
102 diagnostic_context
*global_dc
= &global_diagnostic_context
;
104 /* This will be removed shortly. */
105 output_buffer
*diagnostic_buffer
= &global_diagnostic_context
.buffer
;
107 /* Function of last error message;
108 more generally, function such that if next error message is in it
109 then we don't have to mention the function name. */
110 static tree last_error_function
= NULL
;
112 /* Used to detect when input_file_stack has changed since last described. */
113 static int last_error_tick
;
115 /* Called by report_error_function to print out function name.
116 Default may be overridden by language front-ends. */
118 void (*print_error_function
) PARAMS ((diagnostic_context
*, const char *))
119 = default_print_error_function
;
121 /* Prevent recursion into the error handler. */
122 static int diagnostic_lock
;
125 /* Return truthvalue if current input file is different from the most recent
126 file involved in a diagnostic message. */
129 error_module_changed ()
131 return last_error_tick
!= input_file_stack_tick
;
134 /* Remember current file as being the most recent file involved in a
135 diagnostic message. */
138 record_last_error_module ()
140 last_error_tick
= input_file_stack_tick
;
143 /* Same as error_module_changed, but for function. */
146 error_function_changed ()
148 return last_error_function
!= current_function_decl
;
151 /* Same as record_last_error_module, but for function. */
154 record_last_error_function ()
156 last_error_function
= current_function_decl
;
159 /* Initialize the diagnostic message outputting machinery. */
162 diagnostic_initialize (context
)
163 diagnostic_context
*context
;
165 memset (context
, 0, sizeof *context
);
166 obstack_init (&context
->buffer
.obstack
);
168 /* By default, diagnostics are sent to stderr. */
169 output_buffer_attached_stream (&context
->buffer
) = stderr
;
171 /* By default, we emit prefixes once per message. */
172 diagnostic_prefixing_rule (context
) = DIAGNOSTICS_SHOW_PREFIX_ONCE
;
174 diagnostic_starter (context
) = default_diagnostic_starter
;
175 diagnostic_finalizer (context
) = default_diagnostic_finalizer
;
178 /* Returns true if BUFFER is in line-wrapping mode. */
181 output_is_line_wrapping (buffer
)
182 output_buffer
*buffer
;
184 return diagnostic_line_cutoff (buffer
) > 0;
187 /* Return BUFFER's prefix. */
190 output_get_prefix (buffer
)
191 const output_buffer
*buffer
;
193 return output_prefix (buffer
);
196 /* Subroutine of output_set_maximum_length. Set up BUFFER's
197 internal maximum characters per line. */
200 set_real_maximum_length (buffer
)
201 output_buffer
*buffer
;
203 /* If we're told not to wrap lines then do the obvious thing. In case
204 we'll emit prefix only once per diagnostic message, it is appropriate
205 not to increase unnecessarily the line-length cut-off. */
206 if (! output_is_line_wrapping (buffer
)
207 || diagnostic_prefixing_rule (buffer
) == DIAGNOSTICS_SHOW_PREFIX_ONCE
208 || diagnostic_prefixing_rule (buffer
) == DIAGNOSTICS_SHOW_PREFIX_NEVER
)
209 line_wrap_cutoff (buffer
) = diagnostic_line_cutoff (buffer
);
213 output_prefix (buffer
) ? strlen (output_prefix (buffer
)) : 0;
214 /* If the prefix is ridiculously too long, output at least
216 if (diagnostic_line_cutoff (buffer
) - prefix_length
< 32)
217 line_wrap_cutoff (buffer
) = diagnostic_line_cutoff (buffer
) + 32;
219 line_wrap_cutoff (buffer
) = diagnostic_line_cutoff (buffer
);
223 /* Sets the number of maximum characters per line BUFFER can output
224 in line-wrapping mode. A LENGTH value 0 suppresses line-wrapping. */
227 output_set_maximum_length (buffer
, length
)
228 output_buffer
*buffer
;
231 diagnostic_line_cutoff (buffer
) = length
;
232 set_real_maximum_length (buffer
);
235 /* Sets BUFFER's PREFIX. */
238 output_set_prefix (buffer
, prefix
)
239 output_buffer
*buffer
;
242 output_prefix (buffer
) = prefix
;
243 set_real_maximum_length (buffer
);
244 prefix_was_emitted_for (buffer
) = 0;
245 output_indentation (buffer
) = 0;
248 /* Return a pointer to the last character emitted in the output
249 BUFFER area. A NULL pointer means no character available. */
251 output_last_position (buffer
)
252 const output_buffer
*buffer
;
254 const char *p
= NULL
;
256 if (obstack_base (&buffer
->obstack
) != obstack_next_free (&buffer
->obstack
))
257 p
= ((const char *) obstack_next_free (&buffer
->obstack
)) - 1;
261 /* Free BUFFER's prefix, a previously malloc'd string. */
264 output_destroy_prefix (buffer
)
265 output_buffer
*buffer
;
267 if (output_prefix (buffer
) != NULL
)
269 free ((char *) output_prefix (buffer
));
270 output_prefix (buffer
) = NULL
;
274 /* Zero out any text output so far in BUFFER. */
277 output_clear_message_text (buffer
)
278 output_buffer
*buffer
;
280 obstack_free (&buffer
->obstack
, obstack_base (&buffer
->obstack
));
281 output_text_length (buffer
) = 0;
284 /* Zero out any diagnostic data used so far by BUFFER. */
287 clear_diagnostic_info (buffer
)
288 output_buffer
*buffer
;
290 output_buffer_text_cursor (buffer
) = NULL
;
291 output_buffer_ptr_to_format_args (buffer
) = NULL
;
292 prefix_was_emitted_for (buffer
) = 0;
293 output_indentation (buffer
) = 0;
296 /* Construct an output BUFFER with PREFIX and of MAXIMUM_LENGTH
297 characters per line. */
300 init_output_buffer (buffer
, prefix
, maximum_length
)
301 output_buffer
*buffer
;
305 memset (buffer
, 0, sizeof (output_buffer
));
306 obstack_init (&buffer
->obstack
);
307 output_buffer_attached_stream (buffer
) = stderr
;
308 diagnostic_line_cutoff (buffer
) = maximum_length
;
309 diagnostic_prefixing_rule (buffer
) = diagnostic_prefixing_rule (global_dc
);
310 output_set_prefix (buffer
, prefix
);
311 output_text_length (buffer
) = 0;
312 clear_diagnostic_info (buffer
);
315 /* Reinitialize BUFFER. */
318 output_clear (buffer
)
319 output_buffer
*buffer
;
321 output_clear_message_text (buffer
);
322 clear_diagnostic_info (buffer
);
325 /* Finishes constructing a NULL-terminated character string representing
326 the BUFFERed message. */
329 output_finalize_message (buffer
)
330 output_buffer
*buffer
;
332 obstack_1grow (&buffer
->obstack
, '\0');
333 return output_message_text (buffer
);
337 flush_diagnostic_buffer ()
339 output_buffer_to_stream (diagnostic_buffer
);
340 fflush (output_buffer_attached_stream (diagnostic_buffer
));
343 /* Return the amount of characters BUFFER can accept to
347 output_space_left (buffer
)
348 const output_buffer
*buffer
;
350 return line_wrap_cutoff (buffer
) - output_text_length (buffer
);
353 /* Write out BUFFER's prefix. */
356 output_emit_prefix (buffer
)
357 output_buffer
*buffer
;
359 if (output_prefix (buffer
) != NULL
)
361 switch (diagnostic_prefixing_rule (buffer
))
364 case DIAGNOSTICS_SHOW_PREFIX_NEVER
:
367 case DIAGNOSTICS_SHOW_PREFIX_ONCE
:
368 if (prefix_was_emitted_for (buffer
))
370 output_indent (buffer
);
373 output_indentation (buffer
) += 3;
376 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE
:
378 int prefix_length
= strlen (output_prefix (buffer
));
379 output_append_r (buffer
, output_prefix (buffer
), prefix_length
);
380 prefix_was_emitted_for (buffer
) = 1;
387 /* Have BUFFER start a new line. */
390 output_add_newline (buffer
)
391 output_buffer
*buffer
;
393 obstack_1grow (&buffer
->obstack
, '\n');
394 output_text_length (buffer
) = 0;
397 /* Appends a character to BUFFER. */
400 output_add_character (buffer
, c
)
401 output_buffer
*buffer
;
404 if (output_is_line_wrapping (buffer
) && output_space_left (buffer
) <= 0)
405 output_add_newline (buffer
);
406 obstack_1grow (&buffer
->obstack
, c
);
407 ++output_text_length (buffer
);
410 /* Adds a space to BUFFER. */
413 output_add_space (buffer
)
414 output_buffer
*buffer
;
416 if (output_is_line_wrapping (buffer
) && output_space_left (buffer
) <= 0)
418 output_add_newline (buffer
);
421 obstack_1grow (&buffer
->obstack
, ' ');
422 ++output_text_length (buffer
);
425 /* These functions format an INTEGER into BUFFER as suggested by their
429 output_decimal (buffer
, i
)
430 output_buffer
*buffer
;
433 output_formatted_integer (buffer
, "%d", i
);
437 output_long_decimal (buffer
, i
)
438 output_buffer
*buffer
;
441 output_formatted_integer (buffer
, "%ld", i
);
445 output_unsigned_decimal (buffer
, i
)
446 output_buffer
*buffer
;
449 output_formatted_integer (buffer
, "%u", i
);
453 output_long_unsigned_decimal (buffer
, i
)
454 output_buffer
*buffer
;
457 output_formatted_integer (buffer
, "%lu", i
);
461 output_octal (buffer
, i
)
462 output_buffer
*buffer
;
465 output_formatted_integer (buffer
, "%o", i
);
469 output_long_octal (buffer
, i
)
470 output_buffer
*buffer
;
473 output_formatted_integer (buffer
, "%lo", i
);
477 output_hexadecimal (buffer
, i
)
478 output_buffer
*buffer
;
481 output_formatted_integer (buffer
, "%x", i
);
485 output_long_hexadecimal (buffer
, i
)
486 output_buffer
*buffer
;
489 output_formatted_integer (buffer
, "%lx", i
);
492 /* Append to BUFFER a string specified by its STARTING character
496 output_append_r (buffer
, start
, length
)
497 output_buffer
*buffer
;
501 obstack_grow (&buffer
->obstack
, start
, length
);
502 output_text_length (buffer
) += length
;
505 /* Append a string deliminated by START and END to BUFFER. No wrapping is
506 done. However, if beginning a new line then emit output_prefix (BUFFER)
507 and skip any leading whitespace if appropriate. The caller must ensure
508 that it is safe to do so. */
511 output_append (buffer
, start
, end
)
512 output_buffer
*buffer
;
516 /* Emit prefix and skip whitespace if we're starting a new line. */
517 if (is_starting_newline (buffer
))
519 output_emit_prefix (buffer
);
520 if (output_is_line_wrapping (buffer
))
521 while (start
!= end
&& *start
== ' ')
524 output_append_r (buffer
, start
, end
- start
);
528 output_indent (buffer
)
529 output_buffer
*buffer
;
531 int n
= output_indentation (buffer
);
534 for (i
= 0; i
< n
; ++i
)
535 output_add_character (buffer
, ' ');
538 /* Wrap a text delimited by START and END into BUFFER. */
541 wrap_text (buffer
, start
, end
)
542 output_buffer
*buffer
;
546 int is_wrapping
= output_is_line_wrapping (buffer
);
550 /* Dump anything bordered by whitespaces. */
552 const char *p
= start
;
553 while (p
!= end
&& *p
!= ' ' && *p
!= '\n')
555 if (is_wrapping
&& p
- start
>= output_space_left (buffer
))
556 output_add_newline (buffer
);
557 output_append (buffer
, start
, p
);
561 if (start
!= end
&& *start
== ' ')
563 output_add_space (buffer
);
566 if (start
!= end
&& *start
== '\n')
568 output_add_newline (buffer
);
574 /* Same as wrap_text but wrap text only when in line-wrapping mode. */
577 maybe_wrap_text (buffer
, start
, end
)
578 output_buffer
*buffer
;
582 if (output_is_line_wrapping (buffer
))
583 wrap_text (buffer
, start
, end
);
585 output_append (buffer
, start
, end
);
589 /* Append a STRING to BUFFER; the STRING might be line-wrapped if in
593 output_add_string (buffer
, str
)
594 output_buffer
*buffer
;
597 maybe_wrap_text (buffer
, str
, str
+ (str
? strlen (str
) : 0));
600 /* Flush the content of BUFFER onto the attached stream,
604 output_buffer_to_stream (buffer
)
605 output_buffer
*buffer
;
607 const char *text
= output_finalize_message (buffer
);
608 fputs (text
, output_buffer_attached_stream (buffer
));
609 output_clear_message_text (buffer
);
612 /* Format a message pointed to by output_buffer_text_cursor (BUFFER) using
613 output_buffer_format_args (BUFFER) as appropriate. The following format
614 specifiers are recognized as being language independent:
615 %d, %i: (signed) integer in base ten.
616 %u: unsigned integer in base ten.
617 %o: unsigned integer in base eight.
618 %x: unsigned integer in base sixteen.
619 %ld, %li, %lo, %lu, %lx: long versions of the above.
623 %*.s: a substring the length of which is specified by an integer. */
626 output_format (buffer
)
627 output_buffer
*buffer
;
629 for (; *output_buffer_text_cursor (buffer
);
630 ++output_buffer_text_cursor (buffer
))
632 int long_integer
= 0;
636 const char *p
= output_buffer_text_cursor (buffer
);
637 while (*p
&& *p
!= '%')
639 wrap_text (buffer
, output_buffer_text_cursor (buffer
), p
);
640 output_buffer_text_cursor (buffer
) = p
;
643 if (!*output_buffer_text_cursor (buffer
))
646 /* We got a '%'. Let's see what happens. Record whether we're
647 parsing a long integer format specifier. */
648 if (*++output_buffer_text_cursor (buffer
) == 'l')
651 ++output_buffer_text_cursor (buffer
);
654 /* Handle %c, %d, %i, %ld, %li, %lo, %lu, %lx, %o, %s, %u,
655 %x, %.*s; %%. And nothing else. Front-ends should install
656 printers to grok language specific format specifiers. */
657 switch (*output_buffer_text_cursor (buffer
))
661 (buffer
, va_arg (output_buffer_format_args (buffer
), int));
668 (buffer
, va_arg (output_buffer_format_args (buffer
), long int));
671 (buffer
, va_arg (output_buffer_format_args (buffer
), int));
676 output_long_octal (buffer
,
677 va_arg (output_buffer_format_args (buffer
),
680 output_octal (buffer
,
681 va_arg (output_buffer_format_args (buffer
),
686 output_add_string (buffer
,
687 va_arg (output_buffer_format_args (buffer
),
693 output_long_unsigned_decimal
694 (buffer
, va_arg (output_buffer_format_args (buffer
),
697 output_unsigned_decimal
698 (buffer
, va_arg (output_buffer_format_args (buffer
),
704 output_long_hexadecimal
705 (buffer
, va_arg (output_buffer_format_args (buffer
),
709 (buffer
, va_arg (output_buffer_format_args (buffer
),
714 output_add_character (buffer
, '%');
721 /* We handle no precision specifier but `%.*s'. */
722 if (*++output_buffer_text_cursor (buffer
) != '*')
724 else if (*++output_buffer_text_cursor (buffer
) != 's')
726 n
= va_arg (output_buffer_format_args (buffer
), int);
727 s
= va_arg (output_buffer_format_args (buffer
), const char *);
728 output_append (buffer
, s
, s
+ n
);
733 if (!buffer
->format_decoder
|| !(*buffer
->format_decoder
) (buffer
))
735 /* Hmmm. The front-end failed to install a format translator
736 but called us with an unrecognized format. Sorry. */
744 vbuild_message_string (msg
, ap
)
750 vasprintf (&str
, msg
, ap
);
754 /* Return a malloc'd string containing MSG formatted a la
755 printf. The caller is responsible for freeing the memory. */
758 build_message_string
VPARAMS ((const char *msg
, ...))
763 VA_FIXEDARG (ap
, const char *, msg
);
765 str
= vbuild_message_string (msg
, ap
);
772 /* Return a malloc'd string describing a location. The caller is
773 responsible for freeing the memory. */
776 context_as_prefix (file
, line
, warn
)
784 return build_message_string (_("%s:%d: warning: "), file
, line
);
786 return build_message_string ("%s:%d: ", file
, line
);
791 return build_message_string (_("%s: warning: "), progname
);
793 return build_message_string ("%s: ", progname
);
797 /* Same as context_as_prefix, but only the source FILE is given. */
800 file_name_as_prefix (f
)
803 return build_message_string ("%s: ", f
);
806 /* Format a MESSAGE into BUFFER. Automatically wrap lines. */
809 output_do_printf (buffer
, msg
)
810 output_buffer
*buffer
;
813 char *message
= vbuild_message_string (msg
,
814 output_buffer_format_args (buffer
));
816 wrap_text (buffer
, message
, message
+ strlen (message
));
821 /* Format a message into BUFFER a la printf. */
824 output_printf
VPARAMS ((struct output_buffer
*buffer
, const char *msgid
, ...))
829 VA_FIXEDARG (ap
, output_buffer
*, buffer
);
830 VA_FIXEDARG (ap
, const char *, msgid
);
832 old_args
= output_buffer_ptr_to_format_args (buffer
);
833 output_buffer_ptr_to_format_args (buffer
) = &ap
;
834 output_do_printf (buffer
, _(msgid
));
835 output_buffer_ptr_to_format_args (buffer
) = old_args
;
839 /* Print a message relevant to the given DECL. */
842 format_with_decl (buffer
, decl
)
843 output_buffer
*buffer
;
848 /* Do magic to get around lack of varargs support for insertion
849 of arguments into existing list. We know that the decl is first;
850 we ass_u_me that it will be printed with "%s". */
851 for (p
= output_buffer_text_cursor (buffer
); *p
; ++p
)
857 else if (*(p
+ 1) != 's')
864 /* Print the left-hand substring. */
865 maybe_wrap_text (buffer
, output_buffer_text_cursor (buffer
), p
);
867 if (*p
== '%') /* Print the name. */
869 const char *const n
= (DECL_NAME (decl
)
870 ? (*decl_printable_name
) (decl
, 2)
871 : _("((anonymous))"));
872 output_add_string (buffer
, n
);
876 if (ISALPHA (*(p
- 1) & 0xFF))
881 if (*p
) /* Print the rest of the message. */
883 output_buffer_text_cursor (buffer
) = p
;
884 output_format (buffer
);
889 /* Report a diagnostic MESSAGE at the declaration DECL.
890 MSG is a format string which uses %s to substitute the declaration
891 name; subsequent substitutions are a la output_format. */
894 diagnostic_for_decl (decl
, msgid
, args_ptr
, warn
)
902 if (diagnostic_lock
++)
905 if (count_error (warn
))
907 os
= output_buffer_state (diagnostic_buffer
);
908 report_error_function (DECL_SOURCE_FILE (decl
));
910 (diagnostic_buffer
, context_as_prefix
911 (DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
), warn
));
912 output_buffer_ptr_to_format_args (diagnostic_buffer
) = args_ptr
;
913 output_buffer_text_cursor (diagnostic_buffer
) = _(msgid
);
914 format_with_decl (diagnostic_buffer
, decl
);
915 diagnostic_finish ((output_buffer
*)global_dc
);
916 output_destroy_prefix (diagnostic_buffer
);
918 output_buffer_state (diagnostic_buffer
) = os
;
924 /* Count an error or warning. Return 1 if the message should be printed. */
927 count_error (warningp
)
930 if (warningp
&& !diagnostic_report_warnings_p ())
933 if (warningp
&& !warnings_are_errors
)
937 static int warning_message
= 0;
939 if (warningp
&& !warning_message
)
941 verbatim ("%s: warnings being treated as errors\n", progname
);
950 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
951 runs its second argument through gettext. */
954 fnotice
VPARAMS ((FILE *file
, const char *msgid
, ...))
957 VA_FIXEDARG (ap
, FILE *, file
);
958 VA_FIXEDARG (ap
, const char *, msgid
);
960 vfprintf (file
, _(msgid
), ap
);
965 /* Print a fatal I/O error message. Argument are like printf.
966 Also include a system error message based on `errno'. */
969 fatal_io_error
VPARAMS ((const char *msgid
, ...))
974 VA_FIXEDARG (ap
, const char *, msgid
);
976 os
= output_buffer_state (diagnostic_buffer
);
978 output_printf (diagnostic_buffer
, "%s: %s: ", progname
, xstrerror (errno
));
979 output_buffer_ptr_to_format_args (diagnostic_buffer
) = &ap
;
980 output_buffer_text_cursor (diagnostic_buffer
) = _(msgid
);
981 output_format (diagnostic_buffer
);
982 diagnostic_finish ((output_buffer
*)global_dc
);
983 output_buffer_state (diagnostic_buffer
) = os
;
985 exit (FATAL_EXIT_CODE
);
988 /* Issue a pedantic warning MSGID. */
991 pedwarn
VPARAMS ((const char *msgid
, ...))
993 diagnostic_context dc
;
996 VA_FIXEDARG (ap
, const char *, msgid
);
998 set_diagnostic_context
999 (&dc
, msgid
, &ap
, input_filename
, lineno
, !flag_pedantic_errors
);
1000 report_diagnostic (&dc
);
1004 /* Issue a pedantic waring about DECL. */
1007 pedwarn_with_decl
VPARAMS ((tree decl
, const char *msgid
, ...))
1009 VA_OPEN (ap
, msgid
);
1010 VA_FIXEDARG (ap
, tree
, decl
);
1011 VA_FIXEDARG (ap
, const char *, msgid
);
1013 /* We don't want -pedantic-errors to cause the compilation to fail from
1014 "errors" in system header files. Sometimes fixincludes can't fix what's
1015 broken (eg: unsigned char bitfields - fixing it may change the alignment
1016 which will cause programs to mysteriously fail because the C library
1017 or kernel uses the original layout). There's no point in issuing a
1018 warning either, it's just unnecessary noise. */
1019 if (!DECL_IN_SYSTEM_HEADER (decl
))
1020 diagnostic_for_decl (decl
, msgid
, &ap
, !flag_pedantic_errors
);
1024 /* Same as above but within the context FILE and LINE. */
1027 pedwarn_with_file_and_line
VPARAMS ((const char *file
, int line
,
1028 const char *msgid
, ...))
1030 diagnostic_context dc
;
1032 VA_OPEN (ap
, msgid
);
1033 VA_FIXEDARG (ap
, const char *, file
);
1034 VA_FIXEDARG (ap
, int, line
);
1035 VA_FIXEDARG (ap
, const char *, msgid
);
1037 set_diagnostic_context (&dc
, msgid
, &ap
, file
, line
, !flag_pedantic_errors
);
1038 report_diagnostic (&dc
);
1042 /* Just apologize with MSGID. */
1045 sorry
VPARAMS ((const char *msgid
, ...))
1049 VA_OPEN (ap
, msgid
);
1050 VA_FIXEDARG (ap
, const char *, msgid
);
1052 os
= output_buffer_state (diagnostic_buffer
);
1056 (diagnostic_buffer
, context_as_prefix (input_filename
, lineno
, 0));
1057 output_printf (diagnostic_buffer
, "sorry, not implemented: ");
1058 output_buffer_ptr_to_format_args (diagnostic_buffer
) = &ap
;
1059 output_buffer_text_cursor (diagnostic_buffer
) = _(msgid
);
1060 output_format (diagnostic_buffer
);
1061 diagnostic_finish ((output_buffer
*)global_dc
);
1062 output_buffer_state (diagnostic_buffer
) = os
;
1066 /* Called when the start of a function definition is parsed,
1067 this function prints on stderr the name of the function. */
1070 announce_function (decl
)
1075 if (rtl_dump_and_exit
)
1076 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl
)));
1078 verbatim (" %s", (*decl_printable_name
) (decl
, 2));
1080 output_needs_newline (diagnostic_buffer
) = 1;
1081 record_last_error_function ();
1085 /* The default function to print out name of current function that caused
1089 default_print_error_function (context
, file
)
1090 diagnostic_context
*context
;
1093 if (error_function_changed ())
1095 char *prefix
= file
? build_message_string ("%s: ", file
) : NULL
;
1098 os
= output_buffer_state (context
);
1099 output_set_prefix ((output_buffer
*)context
, prefix
);
1101 if (current_function_decl
== NULL
)
1102 output_add_string ((output_buffer
*)context
, _("At top level:"));
1105 if (TREE_CODE (TREE_TYPE (current_function_decl
)) == METHOD_TYPE
)
1107 ((output_buffer
*)context
, "In member function `%s':",
1108 (*decl_printable_name
) (current_function_decl
, 2));
1111 ((output_buffer
*)context
, "In function `%s':",
1112 (*decl_printable_name
) (current_function_decl
, 2));
1114 output_add_newline ((output_buffer
*)context
);
1116 record_last_error_function ();
1117 output_buffer_to_stream ((output_buffer
*)context
);
1118 output_buffer_state (context
) = os
;
1119 free ((char*) prefix
);
1123 /* Prints out, if necessary, the name of the current function
1124 that caused an error. Called from all error and warning functions.
1125 We ignore the FILE parameter, as it cannot be relied upon. */
1128 report_error_function (file
)
1129 const char *file ATTRIBUTE_UNUSED
;
1131 report_problematic_module ((output_buffer
*)global_dc
);
1132 (*print_error_function
) (global_dc
, input_filename
);
1136 error_with_file_and_line
VPARAMS ((const char *file
, int line
,
1137 const char *msgid
, ...))
1139 diagnostic_context dc
;
1141 VA_OPEN (ap
, msgid
);
1142 VA_FIXEDARG (ap
, const char *, file
);
1143 VA_FIXEDARG (ap
, int, line
);
1144 VA_FIXEDARG (ap
, const char *, msgid
);
1146 set_diagnostic_context (&dc
, msgid
, &ap
, file
, line
, /* warn = */ 0);
1147 report_diagnostic (&dc
);
1152 error_with_decl
VPARAMS ((tree decl
, const char *msgid
, ...))
1154 VA_OPEN (ap
, msgid
);
1155 VA_FIXEDARG (ap
, tree
, decl
);
1156 VA_FIXEDARG (ap
, const char *, msgid
);
1158 diagnostic_for_decl (decl
, msgid
, &ap
, /* warn = */ 0);
1163 /* Report an error message. The arguments are like that of printf. */
1166 error
VPARAMS ((const char *msgid
, ...))
1168 diagnostic_context dc
;
1170 VA_OPEN (ap
, msgid
);
1171 VA_FIXEDARG (ap
, const char *, msgid
);
1173 set_diagnostic_context
1174 (&dc
, msgid
, &ap
, input_filename
, lineno
, /* warn = */ 0);
1175 report_diagnostic (&dc
);
1179 /* Likewise, except that the compilation is terminated after printing the
1183 fatal_error
VPARAMS ((const char *msgid
, ...))
1185 diagnostic_context dc
;
1187 VA_OPEN (ap
, msgid
);
1188 VA_FIXEDARG (ap
, const char *, msgid
);
1190 set_diagnostic_context
1191 (&dc
, msgid
, &ap
, input_filename
, lineno
, /* warn = */ 0);
1192 report_diagnostic (&dc
);
1195 fnotice (stderr
, "compilation terminated.\n");
1196 exit (FATAL_EXIT_CODE
);
1199 /* Report a compiler error at the current line number. Allow a front end to
1200 intercept the message. */
1202 static void (*internal_error_function
) PARAMS ((const char *, va_list *));
1204 /* Set the function to call when a compiler error occurs. */
1207 set_internal_error_function (f
)
1208 void (*f
) PARAMS ((const char *, va_list *));
1210 internal_error_function
= f
;
1214 internal_error
VPARAMS ((const char *msgid
, ...))
1216 diagnostic_context dc
;
1218 VA_OPEN (ap
, msgid
);
1219 VA_FIXEDARG (ap
, const char *, msgid
);
1221 if (diagnostic_lock
)
1224 if (errorcount
> 0 || sorrycount
> 0)
1226 fnotice (stderr
, "%s:%d: confused by earlier errors, bailing out\n",
1227 input_filename
, lineno
);
1228 exit (FATAL_EXIT_CODE
);
1231 if (internal_error_function
!= 0)
1232 (*internal_error_function
) (_(msgid
), &ap
);
1234 set_diagnostic_context
1235 (&dc
, msgid
, &ap
, input_filename
, lineno
, /* warn = */0);
1236 report_diagnostic (&dc
);
1240 "Please submit a full bug report,\n\
1241 with preprocessed source if appropriate.\n\
1242 See %s for instructions.\n", GCCBUGURL
);
1243 exit (FATAL_EXIT_CODE
);
1247 warning_with_file_and_line
VPARAMS ((const char *file
, int line
,
1248 const char *msgid
, ...))
1250 diagnostic_context dc
;
1252 VA_OPEN (ap
, msgid
);
1253 VA_FIXEDARG (ap
, const char *, file
);
1254 VA_FIXEDARG (ap
, int, line
);
1255 VA_FIXEDARG (ap
, const char *, msgid
);
1257 set_diagnostic_context (&dc
, msgid
, &ap
, file
, line
, /* warn = */ 1);
1258 report_diagnostic (&dc
);
1263 warning_with_decl
VPARAMS ((tree decl
, const char *msgid
, ...))
1265 VA_OPEN (ap
, msgid
);
1266 VA_FIXEDARG (ap
, tree
, decl
);
1267 VA_FIXEDARG (ap
, const char *, msgid
);
1269 diagnostic_for_decl (decl
, msgid
, &ap
, /* warn = */ 1);
1274 warning
VPARAMS ((const char *msgid
, ...))
1276 diagnostic_context dc
;
1278 VA_OPEN (ap
, msgid
);
1279 VA_FIXEDARG (ap
, const char *, msgid
);
1281 set_diagnostic_context
1282 (&dc
, msgid
, &ap
, input_filename
, lineno
, /* warn = */ 1);
1283 report_diagnostic (&dc
);
1287 /* Flush diagnostic_buffer content on stderr. */
1290 diagnostic_finish (buffer
)
1291 output_buffer
*buffer
;
1293 output_buffer_to_stream (buffer
);
1294 clear_diagnostic_info (buffer
);
1295 fputc ('\n', output_buffer_attached_stream (buffer
));
1296 fflush (output_buffer_attached_stream (buffer
));
1299 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
1300 settings needed by BUFFER for a verbatim formatting. */
1303 output_do_verbatim (buffer
, msgid
, args_ptr
)
1304 output_buffer
*buffer
;
1310 os
= output_buffer_state (buffer
);
1311 output_prefix (buffer
) = NULL
;
1312 diagnostic_prefixing_rule (buffer
) = DIAGNOSTICS_SHOW_PREFIX_NEVER
;
1313 output_buffer_text_cursor (buffer
) = _(msgid
);
1314 output_buffer_ptr_to_format_args (buffer
) = args_ptr
;
1315 output_set_maximum_length (buffer
, 0);
1316 output_format (buffer
);
1317 output_buffer_state (buffer
) = os
;
1320 /* Output MESSAGE verbatim into BUFFER. */
1323 output_verbatim
VPARAMS ((output_buffer
*buffer
, const char *msgid
, ...))
1325 VA_OPEN (ap
, msgid
);
1326 VA_FIXEDARG (ap
, output_buffer
*, buffer
);
1327 VA_FIXEDARG (ap
, const char *, msgid
);
1329 output_do_verbatim (buffer
, msgid
, &ap
);
1333 /* Same as above but use diagnostic_buffer. */
1336 verbatim
VPARAMS ((const char *msgid
, ...))
1338 VA_OPEN (ap
, msgid
);
1339 VA_FIXEDARG (ap
, const char *, msgid
);
1341 output_do_verbatim (diagnostic_buffer
, msgid
, &ap
);
1342 output_buffer_to_stream (diagnostic_buffer
);
1346 /* Report a diagnostic message (an error or a warning) as specified by
1347 DC. This function is *the* subroutine in terms of which front-ends
1348 should implement their specific diagnostic handling modules. The
1349 front-end independent format specifiers are exactly those described
1350 in the documentation of output_format. */
1353 report_diagnostic (dc
)
1354 diagnostic_context
*dc
;
1358 if (diagnostic_lock
++)
1361 if (count_error (diagnostic_is_warning (dc
)))
1363 os
= output_buffer_state (diagnostic_buffer
);
1364 diagnostic_msg
= diagnostic_message (dc
);
1365 diagnostic_args
= diagnostic_argument_list (dc
);
1366 (*diagnostic_starter (dc
)) (diagnostic_buffer
, dc
);
1367 output_format (diagnostic_buffer
);
1368 (*diagnostic_finalizer (dc
)) (diagnostic_buffer
, dc
);
1369 diagnostic_finish ((output_buffer
*)global_dc
);
1370 output_buffer_state (diagnostic_buffer
) = os
;
1376 /* Inform the user that an error occurred while trying to report some
1377 other error. This indicates catastrophic internal inconsistencies,
1378 so give up now. But do try to flush out the previous error.
1379 This mustn't use internal_error, that will cause infinite recursion. */
1384 if (diagnostic_lock
< 3)
1385 diagnostic_finish ((output_buffer
*)global_dc
);
1388 "Internal compiler error: Error reporting routines re-entered.\n");
1390 "Please submit a full bug report,\n\
1391 with preprocessed source if appropriate.\n\
1392 See %s for instructions.\n", GCCBUGURL
);
1393 exit (FATAL_EXIT_CODE
);
1396 /* Given a partial pathname as input, return another pathname that
1397 shares no directory elements with the pathname of __FILE__. This
1398 is used by fancy_abort() to print `Internal compiler error in expr.c'
1399 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1402 trim_filename (name
)
1405 static const char this_file
[] = __FILE__
;
1406 const char *p
= name
, *q
= this_file
;
1408 /* First skip any "../" in each filename. This allows us to give a proper
1409 reference to a file in a subdirectory. */
1410 while (p
[0] == '.' && p
[1] == '.'
1411 && (p
[2] == DIR_SEPARATOR
1412 #ifdef DIR_SEPARATOR_2
1413 || p
[2] == DIR_SEPARATOR_2
1418 while (q
[0] == '.' && q
[1] == '.'
1419 && (q
[2] == DIR_SEPARATOR
1420 #ifdef DIR_SEPARATOR_2
1421 || p
[2] == DIR_SEPARATOR_2
1426 /* Now skip any parts the two filenames have in common. */
1427 while (*p
== *q
&& *p
!= 0 && *q
!= 0)
1430 /* Now go backwards until the previous directory separator. */
1431 while (p
> name
&& p
[-1] != DIR_SEPARATOR
1432 #ifdef DIR_SEPARATOR_2
1433 && p
[-1] != DIR_SEPARATOR_2
1441 /* Report an internal compiler error in a friendly manner and without
1445 fancy_abort (file
, line
, function
)
1448 const char *function
;
1450 internal_error ("Internal compiler error in %s, at %s:%d",
1451 function
, trim_filename (file
), line
);
1454 /* Setup DC for reporting a diagnostic MESSAGE (an error or a WARNING),
1455 using arguments pointed to by ARGS_PTR, issued at a location specified
1456 by FILE and LINE. */
1459 set_diagnostic_context (dc
, msgid
, args_ptr
, file
, line
, warn
)
1460 diagnostic_context
*dc
;
1467 memset (dc
, 0, sizeof (diagnostic_context
));
1468 diagnostic_message (dc
) = _(msgid
);
1469 diagnostic_argument_list (dc
) = args_ptr
;
1470 diagnostic_file_location (dc
) = file
;
1471 diagnostic_line_location (dc
) = line
;
1472 diagnostic_is_warning (dc
) = warn
;
1473 diagnostic_starter (dc
) = diagnostic_starter (global_dc
);
1474 diagnostic_finalizer (dc
) = diagnostic_finalizer (global_dc
);
1478 report_problematic_module (buffer
)
1479 output_buffer
*buffer
;
1481 struct file_stack
*p
;
1483 if (output_needs_newline (buffer
))
1485 output_add_newline (buffer
);
1486 output_needs_newline (buffer
) = 0;
1489 if (input_file_stack
&& input_file_stack
->next
!= 0
1490 && error_module_changed ())
1492 for (p
= input_file_stack
->next
; p
; p
= p
->next
)
1493 if (p
== input_file_stack
->next
)
1495 (buffer
, "In file included from %s:%d", p
->name
, p
->line
);
1498 (buffer
, ",\n from %s:%d", p
->name
, p
->line
);
1499 output_verbatim (buffer
, ":\n");
1500 record_last_error_module ();
1505 default_diagnostic_starter (buffer
, dc
)
1506 output_buffer
*buffer
;
1507 diagnostic_context
*dc
;
1509 report_error_function (diagnostic_file_location (dc
));
1510 output_set_prefix (buffer
,
1511 context_as_prefix (diagnostic_file_location (dc
),
1512 diagnostic_line_location (dc
),
1513 diagnostic_is_warning (dc
)));
1517 default_diagnostic_finalizer (buffer
, dc
)
1518 output_buffer
*buffer
;
1519 diagnostic_context
*dc
__attribute__((__unused__
));
1521 output_destroy_prefix (buffer
);