1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999, 2000, 2001, 2002, 2003 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. */
30 #include "coretypes.h"
38 #include "diagnostic.h"
39 #include "langhooks.h"
40 #include "langhooks-def.h"
42 #define output_text_length(BUFFER) (BUFFER)->line_length
43 #define is_starting_newline(BUFFER) (output_text_length (BUFFER) == 0)
44 #define line_wrap_cutoff(BUFFER) (BUFFER)->state.maximum_length
45 #define prefix_was_emitted_for(BUFFER) (BUFFER)->state.emitted_prefix_p
48 static void output_flush
PARAMS ((output_buffer
*));
49 static void output_do_verbatim
PARAMS ((output_buffer
*, text_info
*));
50 static void output_buffer_to_stream
PARAMS ((output_buffer
*));
51 static void output_format
PARAMS ((output_buffer
*, text_info
*));
52 static void output_indent
PARAMS ((output_buffer
*));
54 static char *build_message_string
PARAMS ((const char *, ...))
56 static void format_with_decl
PARAMS ((output_buffer
*, text_info
*, tree
));
57 static void diagnostic_for_decl
PARAMS ((diagnostic_context
*,
58 diagnostic_info
*, tree
));
59 static void set_real_maximum_length
PARAMS ((output_buffer
*));
61 static void output_unsigned_decimal
PARAMS ((output_buffer
*, unsigned int));
62 static void output_long_decimal
PARAMS ((output_buffer
*, long int));
63 static void output_long_unsigned_decimal
PARAMS ((output_buffer
*,
65 static void output_octal
PARAMS ((output_buffer
*, unsigned int));
66 static void output_long_octal
PARAMS ((output_buffer
*, unsigned long int));
67 static void output_hexadecimal
PARAMS ((output_buffer
*, unsigned int));
68 static void output_long_hexadecimal
PARAMS ((output_buffer
*,
70 static void output_append_r
PARAMS ((output_buffer
*, const char *, int));
71 static void wrap_text
PARAMS ((output_buffer
*, const char *, const char *));
72 static void maybe_wrap_text
PARAMS ((output_buffer
*, const char *,
74 static void output_clear_data
PARAMS ((output_buffer
*));
76 static void default_diagnostic_starter
PARAMS ((diagnostic_context
*,
78 static void default_diagnostic_finalizer
PARAMS ((diagnostic_context
*,
81 static void error_recursion
PARAMS ((diagnostic_context
*)) ATTRIBUTE_NORETURN
;
82 static bool text_specifies_location
PARAMS ((text_info
*, location_t
*));
83 static bool diagnostic_count_diagnostic
PARAMS ((diagnostic_context
*,
85 static void diagnostic_action_after_output
PARAMS ((diagnostic_context
*,
87 static void real_abort
PARAMS ((void)) ATTRIBUTE_NORETURN
;
89 extern int rtl_dump_and_exit
;
90 extern int warnings_are_errors
;
92 /* A diagnostic_context surrogate for stderr. */
93 static diagnostic_context global_diagnostic_context
;
94 diagnostic_context
*global_dc
= &global_diagnostic_context
;
96 /* Boilerplate text used in two locations. */
97 #define bug_report_request \
98 "Please submit a full bug report,\n\
99 with preprocessed source if appropriate.\n\
100 See %s for instructions.\n"
103 /* Subroutine of output_set_maximum_length. Set up BUFFER's
104 internal maximum characters per line. */
106 set_real_maximum_length (buffer
)
107 output_buffer
*buffer
;
109 /* If we're told not to wrap lines then do the obvious thing. In case
110 we'll emit prefix only once per diagnostic message, it is appropriate
111 not to increase unnecessarily the line-length cut-off. */
112 if (!output_is_line_wrapping (buffer
)
113 || output_prefixing_rule (buffer
) == DIAGNOSTICS_SHOW_PREFIX_ONCE
114 || output_prefixing_rule (buffer
) == DIAGNOSTICS_SHOW_PREFIX_NEVER
)
115 line_wrap_cutoff (buffer
) = output_line_cutoff (buffer
);
118 int prefix_length
= buffer
->state
.prefix
?
119 strlen (buffer
->state
.prefix
) : 0;
120 /* If the prefix is ridiculously too long, output at least
122 if (output_line_cutoff (buffer
) - prefix_length
< 32)
123 line_wrap_cutoff (buffer
) = output_line_cutoff (buffer
) + 32;
125 line_wrap_cutoff (buffer
) = output_line_cutoff (buffer
);
129 /* Sets the number of maximum characters per line BUFFER can output
130 in line-wrapping mode. A LENGTH value 0 suppresses line-wrapping. */
132 output_set_maximum_length (buffer
, length
)
133 output_buffer
*buffer
;
136 output_line_cutoff (buffer
) = length
;
137 set_real_maximum_length (buffer
);
140 /* Sets BUFFER's PREFIX. */
142 output_set_prefix (buffer
, prefix
)
143 output_buffer
*buffer
;
146 buffer
->state
.prefix
= prefix
;
147 set_real_maximum_length (buffer
);
148 prefix_was_emitted_for (buffer
) = false;
149 output_indentation (buffer
) = 0;
152 /* Return a pointer to the last character emitted in the output
153 BUFFER area. A NULL pointer means no character available. */
155 output_last_position (buffer
)
156 const output_buffer
*buffer
;
158 const char *p
= NULL
;
160 if (obstack_base (&buffer
->obstack
) != obstack_next_free (&buffer
->obstack
))
161 p
= ((const char *) obstack_next_free (&buffer
->obstack
)) - 1;
165 /* Free BUFFER's prefix, a previously malloc'd string. */
167 output_destroy_prefix (buffer
)
168 output_buffer
*buffer
;
170 if (buffer
->state
.prefix
!= NULL
)
172 free ((char *) buffer
->state
.prefix
);
173 buffer
->state
.prefix
= NULL
;
177 /* Zero out any text output so far in BUFFER. */
179 output_clear_message_text (buffer
)
180 output_buffer
*buffer
;
182 obstack_free (&buffer
->obstack
, obstack_base (&buffer
->obstack
));
183 output_text_length (buffer
) = 0;
186 /* Zero out any formatting data used so far by BUFFER. */
188 output_clear_data (buffer
)
189 output_buffer
*buffer
;
191 prefix_was_emitted_for (buffer
) = false;
192 output_indentation (buffer
) = 0;
195 /* Construct an output BUFFER with PREFIX and of MAXIMUM_LENGTH
196 characters per line. */
198 init_output_buffer (buffer
, prefix
, maximum_length
)
199 output_buffer
*buffer
;
203 memset (buffer
, 0, sizeof (output_buffer
));
204 obstack_init (&buffer
->obstack
);
205 output_buffer_attached_stream (buffer
) = stderr
;
206 output_line_cutoff (buffer
) = maximum_length
;
207 output_prefixing_rule (buffer
) = diagnostic_prefixing_rule (global_dc
);
208 output_set_prefix (buffer
, prefix
);
209 output_text_length (buffer
) = 0;
210 output_clear_data (buffer
);
213 /* Reinitialize BUFFER. */
215 output_clear (buffer
)
216 output_buffer
*buffer
;
218 output_clear_message_text (buffer
);
219 output_clear_data (buffer
);
222 /* Finishes constructing a NULL-terminated character string representing
223 the BUFFERed message. */
225 output_finalize_message (buffer
)
226 output_buffer
*buffer
;
228 obstack_1grow (&buffer
->obstack
, '\0');
229 return output_message_text (buffer
);
232 /* Return the amount of characters BUFFER can accept to
235 output_space_left (buffer
)
236 const output_buffer
*buffer
;
238 return line_wrap_cutoff (buffer
) - output_text_length (buffer
);
241 /* Write out BUFFER's prefix. */
243 output_emit_prefix (buffer
)
244 output_buffer
*buffer
;
246 if (buffer
->state
.prefix
!= NULL
)
248 switch (output_prefixing_rule (buffer
))
251 case DIAGNOSTICS_SHOW_PREFIX_NEVER
:
254 case DIAGNOSTICS_SHOW_PREFIX_ONCE
:
255 if (prefix_was_emitted_for (buffer
))
257 output_indent (buffer
);
260 output_indentation (buffer
) += 3;
263 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE
:
265 int prefix_length
= strlen (buffer
->state
.prefix
);
266 output_append_r (buffer
, buffer
->state
.prefix
, prefix_length
);
267 prefix_was_emitted_for (buffer
) = true;
274 /* Have BUFFER start a new line. */
276 output_add_newline (buffer
)
277 output_buffer
*buffer
;
279 obstack_1grow (&buffer
->obstack
, '\n');
280 output_text_length (buffer
) = 0;
283 /* Appends a character to BUFFER. */
285 output_add_character (buffer
, c
)
286 output_buffer
*buffer
;
289 if (output_is_line_wrapping (buffer
) && output_space_left (buffer
) <= 0)
290 output_add_newline (buffer
);
291 obstack_1grow (&buffer
->obstack
, c
);
292 ++output_text_length (buffer
);
295 /* Adds a space to BUFFER. */
297 output_add_space (buffer
)
298 output_buffer
*buffer
;
300 if (output_is_line_wrapping (buffer
) && output_space_left (buffer
) <= 0)
302 output_add_newline (buffer
);
305 obstack_1grow (&buffer
->obstack
, ' ');
306 ++output_text_length (buffer
);
309 /* These functions format an INTEGER into BUFFER as suggested by their
312 output_decimal (buffer
, i
)
313 output_buffer
*buffer
;
316 output_formatted_scalar (buffer
, "%d", i
);
320 output_long_decimal (output_buffer
*buffer
, long int i
)
322 output_formatted_scalar (buffer
, "%ld", i
);
326 output_unsigned_decimal (output_buffer
*buffer
, unsigned int i
)
328 output_formatted_scalar (buffer
, "%u", i
);
332 output_long_unsigned_decimal (output_buffer
*buffer
, long unsigned int i
)
334 output_formatted_scalar (buffer
, "%lu", i
);
338 output_octal (output_buffer
*buffer
, unsigned int i
)
340 output_formatted_scalar (buffer
, "%o", i
);
344 output_long_octal (output_buffer
*buffer
, long unsigned int i
)
346 output_formatted_scalar (buffer
, "%lo", i
);
350 output_hexadecimal (output_buffer
*buffer
, unsigned int i
)
352 output_formatted_scalar (buffer
, "%x", i
);
356 output_long_hexadecimal (output_buffer
*buffer
, long unsigned int i
)
358 output_formatted_scalar (buffer
, "%lx", i
);
362 output_pointer (output_buffer
*buffer
, void *p
)
364 output_formatted_scalar (buffer
, HOST_PTR_PRINTF
, p
);
367 /* Append to BUFFER a string specified by its STARTING character
370 output_append_r (buffer
, start
, length
)
371 output_buffer
*buffer
;
375 obstack_grow (&buffer
->obstack
, start
, length
);
376 output_text_length (buffer
) += length
;
379 /* Append a string deliminated by START and END to BUFFER. No wrapping is
380 done. However, if beginning a new line then emit BUFFER->state.prefix
381 and skip any leading whitespace if appropriate. The caller must ensure
382 that it is safe to do so. */
384 output_append (buffer
, start
, end
)
385 output_buffer
*buffer
;
389 /* Emit prefix and skip whitespace if we're starting a new line. */
390 if (is_starting_newline (buffer
))
392 output_emit_prefix (buffer
);
393 if (output_is_line_wrapping (buffer
))
394 while (start
!= end
&& *start
== ' ')
397 output_append_r (buffer
, start
, end
- start
);
400 /* Insert enough spaces into BUFFER to bring the column position to
401 the current indentation level, assuming that a newline has just
402 been written to the buffer. */
404 output_indent (buffer
)
405 output_buffer
*buffer
;
407 int n
= output_indentation (buffer
);
410 for (i
= 0; i
< n
; ++i
)
411 output_add_character (buffer
, ' ');
414 /* Wrap a text delimited by START and END into BUFFER. */
416 wrap_text (buffer
, start
, end
)
417 output_buffer
*buffer
;
421 bool is_wrapping
= output_is_line_wrapping (buffer
);
425 /* Dump anything bordered by whitespaces. */
427 const char *p
= start
;
428 while (p
!= end
&& *p
!= ' ' && *p
!= '\n')
430 if (is_wrapping
&& p
- start
>= output_space_left (buffer
))
431 output_add_newline (buffer
);
432 output_append (buffer
, start
, p
);
436 if (start
!= end
&& *start
== ' ')
438 output_add_space (buffer
);
441 if (start
!= end
&& *start
== '\n')
443 output_add_newline (buffer
);
449 /* Same as wrap_text but wrap text only when in line-wrapping mode. */
451 maybe_wrap_text (buffer
, start
, end
)
452 output_buffer
*buffer
;
456 if (output_is_line_wrapping (buffer
))
457 wrap_text (buffer
, start
, end
);
459 output_append (buffer
, start
, end
);
463 /* Append a STRING to BUFFER; the STRING might be line-wrapped if in
466 output_add_string (buffer
, str
)
467 output_buffer
*buffer
;
470 maybe_wrap_text (buffer
, str
, str
+ (str
? strlen (str
) : 0));
473 /* Append an identifier ID to BUFFER. */
475 output_add_identifier (buffer
, id
)
476 output_buffer
*buffer
;
479 output_append (buffer
, IDENTIFIER_POINTER (id
),
480 IDENTIFIER_POINTER (id
) + IDENTIFIER_LENGTH (id
));
483 /* Flush the content of BUFFER onto the attached stream,
487 output_buffer_to_stream (buffer
)
488 output_buffer
*buffer
;
490 const char *text
= output_finalize_message (buffer
);
491 fputs (text
, output_buffer_attached_stream (buffer
));
492 output_clear_message_text (buffer
);
495 /* Format a message pointed to by TEXT. The following format specifiers are
496 recognized as being language independent:
497 %d, %i: (signed) integer in base ten.
498 %u: unsigned integer in base ten.
499 %o: unsigned integer in base eight.
500 %x: unsigned integer in base sixteen.
501 %ld, %li, %lo, %lu, %lx: long versions of the above.
505 %m: strerror(text->err_no) - does not consume a value from args_ptr.
507 %*.s: a substring the length of which is specified by an integer.
510 output_format (buffer
, text
)
511 output_buffer
*buffer
;
514 for (; *text
->format_spec
; ++text
->format_spec
)
516 bool long_integer
= 0;
520 const char *p
= text
->format_spec
;
521 while (*p
&& *p
!= '%')
523 wrap_text (buffer
, text
->format_spec
, p
);
524 text
->format_spec
= p
;
527 if (*text
->format_spec
== '\0')
530 /* We got a '%'. Let's see what happens. Record whether we're
531 parsing a long integer format specifier. */
532 if (*++text
->format_spec
== 'l')
538 /* Handle %c, %d, %i, %ld, %li, %lo, %lu, %lx, %m, %o, %s, %u,
539 %x, %p, %.*s; %%. And nothing else. Front-ends should install
540 printers to grok language specific format specifiers. */
541 switch (*text
->format_spec
)
544 output_add_character (buffer
, va_arg (*text
->args_ptr
, int));
550 output_long_decimal (buffer
, va_arg (*text
->args_ptr
, long int));
552 output_decimal (buffer
, va_arg (*text
->args_ptr
, int));
557 output_long_octal (buffer
,
558 va_arg (*text
->args_ptr
, unsigned long int));
560 output_octal (buffer
, va_arg (*text
->args_ptr
, unsigned int));
564 output_add_string (buffer
, va_arg (*text
->args_ptr
, const char *));
568 output_pointer (buffer
, va_arg (*text
->args_ptr
, void *));
573 output_long_unsigned_decimal
574 (buffer
, va_arg (*text
->args_ptr
, long unsigned int));
576 output_unsigned_decimal
577 (buffer
, va_arg (*text
->args_ptr
, unsigned int));
582 output_long_hexadecimal
583 (buffer
, va_arg (*text
->args_ptr
, unsigned long int));
586 (buffer
, va_arg (*text
->args_ptr
, unsigned int));
590 output_add_string (buffer
, xstrerror (text
->err_no
));
594 output_add_character (buffer
, '%');
599 const location_t
*locus
= va_arg (*text
->args_ptr
, location_t
*);
600 output_add_string (buffer
, "file '");
601 output_add_string (buffer
, locus
->file
);
602 output_add_string (buffer
, "', line ");
603 output_decimal (buffer
, locus
->line
);
611 /* We handle no precision specifier but `%.*s'. */
612 if (*++text
->format_spec
!= '*')
614 else if (*++text
->format_spec
!= 's')
616 n
= va_arg (*text
->args_ptr
, int);
617 s
= va_arg (*text
->args_ptr
, const char *);
618 output_append (buffer
, s
, s
+ n
);
623 if (!buffer
->format_decoder
624 || !(*buffer
->format_decoder
) (buffer
, text
))
626 /* Hmmm. The front-end failed to install a format translator
627 but called us with an unrecognized format. Or, maybe, the
628 translated string just contains an invalid format, or
629 has formats in the wrong order. Sorry. */
636 /* Return a malloc'd string containing MSG formatted a la printf. The
637 caller is responsible for freeing the memory. */
639 build_message_string (const char *msg
, ...)
645 vasprintf (&str
, msg
, ap
);
651 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
653 file_name_as_prefix (f
)
656 return build_message_string ("%s: ", f
);
659 /* Format a message into BUFFER a la printf. */
661 output_printf (struct output_buffer
*buffer
, const char *msgid
, ...)
666 va_start (ap
, msgid
);
669 text
.format_spec
= _(msgid
);
670 output_format (buffer
, &text
);
674 /* Print a message relevant to the given DECL. */
676 format_with_decl (buffer
, text
, decl
)
677 output_buffer
*buffer
;
683 /* Do magic to get around lack of varargs support for insertion
684 of arguments into existing list. We know that the decl is first;
685 we ass_u_me that it will be printed with "%s". */
686 for (p
= text
->format_spec
; *p
; ++p
)
692 else if (*(p
+ 1) != 's')
699 /* Print the left-hand substring. */
700 maybe_wrap_text (buffer
, text
->format_spec
, p
);
702 if (*p
== '%') /* Print the name. */
704 const char *const n
= (DECL_NAME (decl
)
705 ? (*lang_hooks
.decl_printable_name
) (decl
, 2)
706 : _("((anonymous))"));
707 output_add_string (buffer
, n
);
711 if (ISALPHA (*(p
- 1) & 0xFF))
716 if (*p
) /* Print the rest of the message. */
718 text
->format_spec
= p
;
719 output_format (buffer
, text
);
723 /* Flush the content of BUFFER onto the attached stream. */
725 output_flush (buffer
)
726 output_buffer
*buffer
;
728 output_buffer_to_stream (buffer
);
729 output_clear_data (buffer
);
730 fputc ('\n', output_buffer_attached_stream (buffer
));
731 fflush (output_buffer_attached_stream (buffer
));
734 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
735 settings needed by BUFFER for a verbatim formatting. */
737 output_do_verbatim (buffer
, text
)
738 output_buffer
*buffer
;
741 diagnostic_prefixing_rule_t rule
= output_prefixing_rule (buffer
);
742 int line_cutoff
= output_line_cutoff (buffer
);
744 /* Set verbatim mode. */
745 output_prefixing_rule (buffer
) = DIAGNOSTICS_SHOW_PREFIX_NEVER
;
746 output_line_cutoff (buffer
) = 0;
747 /* Do the actual formatting. */
748 output_format (buffer
, text
);
749 /* Restore previous settings. */
750 output_prefixing_rule (buffer
) = rule
;
751 output_line_cutoff (buffer
) = line_cutoff
;
754 /* Output MESSAGE verbatim into BUFFER. */
756 output_verbatim (output_buffer
*buffer
, const char *msgid
, ...)
761 va_start (ap
, msgid
);
764 text
.format_spec
= _(msgid
);
765 output_do_verbatim (buffer
, &text
);
770 /* Initialize the diagnostic message outputting machinery. */
772 diagnostic_initialize (context
)
773 diagnostic_context
*context
;
775 memset (context
, 0, sizeof *context
);
776 obstack_init (&context
->buffer
.obstack
);
778 /* By default, diagnostics are sent to stderr. */
779 output_buffer_attached_stream (&context
->buffer
) = stderr
;
781 /* By default, we emit prefixes once per message. */
782 diagnostic_prefixing_rule (context
) = DIAGNOSTICS_SHOW_PREFIX_ONCE
;
784 diagnostic_starter (context
) = default_diagnostic_starter
;
785 diagnostic_finalizer (context
) = default_diagnostic_finalizer
;
786 context
->warnings_are_errors_message
= warnings_are_errors
;
789 /* Returns true if the next format specifier in TEXT is a format specifier
790 for a location_t. If so, update the object pointed by LOCUS to reflect
791 the specified location in *TEXT->args_ptr. */
793 text_specifies_location (text
, locus
)
798 /* Skip any leading text. */
799 for (p
= text
->format_spec
; *p
&& *p
!= '%'; ++p
)
802 /* Extract the location information if any. */
803 if (*p
== '%' && *++p
== 'H')
805 *locus
= *va_arg (*text
->args_ptr
, location_t
*);
806 text
->format_spec
= p
+ 1;
814 diagnostic_set_info (diagnostic
, msgid
, args
, file
, line
, kind
)
815 diagnostic_info
*diagnostic
;
822 diagnostic
->message
.err_no
= errno
;
823 diagnostic
->message
.args_ptr
= args
;
824 diagnostic
->message
.format_spec
= _(msgid
);
825 /* If the diagnostic message doesn't specify a location,
826 use FILE and LINE. */
827 if (!text_specifies_location (&diagnostic
->message
, &diagnostic
->location
))
829 diagnostic
->location
.file
= file
;
830 diagnostic
->location
.line
= line
;
832 diagnostic
->kind
= kind
;
835 /* Return a malloc'd string describing a location. The caller is
836 responsible for freeing the memory. */
838 diagnostic_build_prefix (diagnostic
)
839 diagnostic_info
*diagnostic
;
841 static const char *const diagnostic_kind_text
[] = {
842 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
843 #include "diagnostic.def"
844 #undef DEFINE_DIAGNOSTIC_KIND
847 if (diagnostic
->kind
>= DK_LAST_DIAGNOSTIC_KIND
)
850 return diagnostic
->location
.file
851 ? build_message_string ("%s:%d: %s",
852 diagnostic
->location
.file
,
853 diagnostic
->location
.line
,
854 _(diagnostic_kind_text
[diagnostic
->kind
]))
855 : build_message_string ("%s: %s", progname
,
856 _(diagnostic_kind_text
[diagnostic
->kind
]));
860 diagnostic_flush_buffer (context
)
861 diagnostic_context
*context
;
863 output_buffer_to_stream (&context
->buffer
);
864 fflush (output_buffer_attached_stream (&context
->buffer
));
867 /* Count a diagnostic. Return true if the message should be printed. */
869 diagnostic_count_diagnostic (context
, diagnostic
)
870 diagnostic_context
*context
;
871 diagnostic_info
*diagnostic
;
873 diagnostic_t kind
= diagnostic
->kind
;
881 #ifndef ENABLE_CHECKING
882 /* When not checking, ICEs are converted to fatal errors when an
883 error has already occurred. This is counteracted by
885 if ((diagnostic_kind_count (context
, DK_ERROR
) > 0
886 || diagnostic_kind_count (context
, DK_SORRY
) > 0)
887 && !context
->abort_on_error
)
889 fnotice (stderr
, "%s:%d: confused by earlier errors, bailing out\n",
890 diagnostic
->location
.file
, diagnostic
->location
.line
);
891 exit (FATAL_EXIT_CODE
);
894 if (context
->internal_error
)
895 (*context
->internal_error
) (diagnostic
->message
.format_spec
,
896 diagnostic
->message
.args_ptr
);
899 case DK_FATAL
: case DK_SORRY
:
900 case DK_ANACHRONISM
: case DK_NOTE
:
901 ++diagnostic_kind_count (context
, kind
);
905 if (!diagnostic_report_warnings_p ())
908 if (!warnings_are_errors
)
910 ++diagnostic_kind_count (context
, DK_WARNING
);
914 if (context
->warnings_are_errors_message
)
916 output_verbatim (&context
->buffer
,
917 "%s: warnings being treated as errors\n", progname
);
918 context
->warnings_are_errors_message
= false;
921 /* and fall through */
923 ++diagnostic_kind_count (context
, DK_ERROR
);
930 /* Take any action which is expected to happen after the diagnostic
931 is written out. This function does not always return. */
933 diagnostic_action_after_output (context
, diagnostic
)
934 diagnostic_context
*context
;
935 diagnostic_info
*diagnostic
;
937 switch (diagnostic
->kind
)
947 if (context
->abort_on_error
)
952 if (context
->abort_on_error
)
955 fnotice (stderr
, bug_report_request
, bug_report_url
);
956 exit (FATAL_EXIT_CODE
);
959 if (context
->abort_on_error
)
962 fnotice (stderr
, "compilation terminated.\n");
963 exit (FATAL_EXIT_CODE
);
970 /* Called when the start of a function definition is parsed,
971 this function prints on stderr the name of the function. */
973 announce_function (decl
)
978 if (rtl_dump_and_exit
)
979 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl
)));
981 verbatim (" %s", (*lang_hooks
.decl_printable_name
) (decl
, 2));
983 output_needs_newline (&global_dc
->buffer
) = true;
984 diagnostic_set_last_function (global_dc
);
988 /* The default function to print out name of current function that caused
991 lhd_print_error_function (context
, file
)
992 diagnostic_context
*context
;
995 if (diagnostic_last_function_changed (context
))
997 const char *old_prefix
= output_prefix (&context
->buffer
);
998 char *new_prefix
= file
? build_message_string ("%s: ", file
) : NULL
;
1000 output_set_prefix (&context
->buffer
, new_prefix
);
1002 if (current_function_decl
== NULL
)
1003 output_add_string (&context
->buffer
, _("At top level:"));
1006 if (TREE_CODE (TREE_TYPE (current_function_decl
)) == METHOD_TYPE
)
1008 (&context
->buffer
, "In member function `%s':",
1009 (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2));
1012 (&context
->buffer
, "In function `%s':",
1013 (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2));
1015 output_add_newline (&context
->buffer
);
1017 diagnostic_set_last_function (context
);
1018 output_buffer_to_stream (&context
->buffer
);
1019 context
->buffer
.state
.prefix
= old_prefix
;
1020 free ((char*) new_prefix
);
1024 /* Prints out, if necessary, the name of the current function
1025 that caused an error. Called from all error and warning functions.
1026 We ignore the FILE parameter, as it cannot be relied upon. */
1029 diagnostic_report_current_function (context
)
1030 diagnostic_context
*context
;
1032 diagnostic_report_current_module (context
);
1033 (*lang_hooks
.print_error_function
) (context
, input_filename
);
1037 diagnostic_report_current_module (context
)
1038 diagnostic_context
*context
;
1040 struct file_stack
*p
;
1042 if (output_needs_newline (&context
->buffer
))
1044 output_add_newline (&context
->buffer
);
1045 output_needs_newline (&context
->buffer
) = false;
1048 if (input_file_stack
&& input_file_stack
->next
!= 0
1049 && diagnostic_last_module_changed (context
))
1051 for (p
= input_file_stack
->next
; p
; p
= p
->next
)
1052 if (p
== input_file_stack
->next
)
1053 output_verbatim (&context
->buffer
,
1054 "In file included from %s:%d",
1055 p
->location
.file
, p
->location
.line
);
1057 output_verbatim (&context
->buffer
,
1059 p
->location
.file
, p
->location
.line
);
1060 output_verbatim (&context
->buffer
, ":\n");
1061 diagnostic_set_last_module (context
);
1066 default_diagnostic_starter (context
, diagnostic
)
1067 diagnostic_context
*context
;
1068 diagnostic_info
*diagnostic
;
1070 diagnostic_report_current_function (context
);
1071 output_set_prefix (&context
->buffer
, diagnostic_build_prefix (diagnostic
));
1075 default_diagnostic_finalizer (context
, diagnostic
)
1076 diagnostic_context
*context
;
1077 diagnostic_info
*diagnostic
__attribute__((unused
));
1079 output_destroy_prefix (&context
->buffer
);
1082 /* Report a diagnostic message (an error or a warning) as specified by
1083 DC. This function is *the* subroutine in terms of which front-ends
1084 should implement their specific diagnostic handling modules. The
1085 front-end independent format specifiers are exactly those described
1086 in the documentation of output_format. */
1089 diagnostic_report_diagnostic (context
, diagnostic
)
1090 diagnostic_context
*context
;
1091 diagnostic_info
*diagnostic
;
1093 if (context
->lock
++)
1094 error_recursion (context
);
1096 if (diagnostic_count_diagnostic (context
, diagnostic
))
1098 (*diagnostic_starter (context
)) (context
, diagnostic
);
1099 output_format (&context
->buffer
, &diagnostic
->message
);
1100 (*diagnostic_finalizer (context
)) (context
, diagnostic
);
1101 output_flush (&context
->buffer
);
1102 diagnostic_action_after_output (context
, diagnostic
);
1108 /* Report a diagnostic MESSAGE at the declaration DECL.
1109 MSG is a format string which uses %s to substitute the declaration
1110 name; subsequent substitutions are a la output_format. */
1112 diagnostic_for_decl (context
, diagnostic
, decl
)
1113 diagnostic_context
*context
;
1114 diagnostic_info
*diagnostic
;
1117 if (context
->lock
++)
1118 error_recursion (context
);
1120 if (diagnostic_count_diagnostic (context
, diagnostic
))
1122 (*diagnostic_starter (context
)) (context
, diagnostic
);
1123 format_with_decl (&context
->buffer
, &diagnostic
->message
, decl
);
1124 (*diagnostic_finalizer (context
)) (context
, diagnostic
);
1125 output_flush (&context
->buffer
);
1126 diagnostic_action_after_output (context
, diagnostic
);
1132 /* Given a partial pathname as input, return another pathname that
1133 shares no directory elements with the pathname of __FILE__. This
1134 is used by fancy_abort() to print `Internal compiler error in expr.c'
1135 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1138 trim_filename (name
)
1141 static const char this_file
[] = __FILE__
;
1142 const char *p
= name
, *q
= this_file
;
1144 /* First skip any "../" in each filename. This allows us to give a proper
1145 reference to a file in a subdirectory. */
1146 while (p
[0] == '.' && p
[1] == '.'
1147 && (p
[2] == DIR_SEPARATOR
1148 #ifdef DIR_SEPARATOR_2
1149 || p
[2] == DIR_SEPARATOR_2
1154 while (q
[0] == '.' && q
[1] == '.'
1155 && (q
[2] == DIR_SEPARATOR
1156 #ifdef DIR_SEPARATOR_2
1157 || p
[2] == DIR_SEPARATOR_2
1162 /* Now skip any parts the two filenames have in common. */
1163 while (*p
== *q
&& *p
!= 0 && *q
!= 0)
1166 /* Now go backwards until the previous directory separator. */
1167 while (p
> name
&& p
[-1] != DIR_SEPARATOR
1168 #ifdef DIR_SEPARATOR_2
1169 && p
[-1] != DIR_SEPARATOR_2
1177 /* Standard error reporting routines in increasing order of severity.
1178 All of these take arguments like printf. */
1180 /* Text to be emitted verbatim to the error message stream; this
1181 produces no prefix and disables line-wrapping. Use rarely. */
1183 verbatim (const char *msgid
, ...)
1188 va_start (ap
, msgid
);
1189 text
.err_no
= errno
;
1190 text
.args_ptr
= &ap
;
1191 text
.format_spec
= _(msgid
);
1192 output_do_verbatim (&global_dc
->buffer
, &text
);
1193 output_buffer_to_stream (&global_dc
->buffer
);
1197 /* An informative note. Use this for additional details on an error
1200 inform (const char *msgid
, ...)
1202 diagnostic_info diagnostic
;
1205 va_start (ap
, msgid
);
1206 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, input_line
,
1208 report_diagnostic (&diagnostic
);
1212 /* A warning. Use this for code which is correct according to the
1213 relevant language specification but is likely to be buggy anyway. */
1215 warning (const char *msgid
, ...)
1217 diagnostic_info diagnostic
;
1220 va_start (ap
, msgid
);
1221 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, input_line
,
1223 report_diagnostic (&diagnostic
);
1227 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
1228 given on the command line, in which case it issues an error. Use
1229 this for diagnostics required by the relevant language standard,
1230 if you have chosen not to make them errors.
1232 Note that these diagnostics are issued independent of the setting
1233 of the -pedantic command-line switch. To get a warning enabled
1234 only with that switch, write "if (pedantic) pedwarn (...);" */
1236 pedwarn (const char *msgid
, ...)
1238 diagnostic_info diagnostic
;
1241 va_start (ap
, msgid
);
1242 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, input_line
,
1243 pedantic_error_kind ());
1244 report_diagnostic (&diagnostic
);
1248 /* A hard error: the code is definitely ill-formed, and an object file
1249 will not be produced. */
1251 error (const char *msgid
, ...)
1253 diagnostic_info diagnostic
;
1256 va_start (ap
, msgid
);
1257 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, input_line
,
1259 report_diagnostic (&diagnostic
);
1263 /* "Sorry, not implemented." Use for a language feature which is
1264 required by the relevant specification but not implemented by GCC.
1265 An object file will not be produced. */
1267 sorry (const char *msgid
, ...)
1269 diagnostic_info diagnostic
;
1272 va_start (ap
, msgid
);
1273 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, input_line
,
1275 report_diagnostic (&diagnostic
);
1279 /* An error which is severe enough that we make no attempt to
1280 continue. Do not use this for internal consistency checks; that's
1281 internal_error. Use of this function should be rare. */
1283 fatal_error (const char *msgid
, ...)
1285 diagnostic_info diagnostic
;
1288 va_start (ap
, msgid
);
1289 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, input_line
,
1291 report_diagnostic (&diagnostic
);
1298 /* An internal consistency check has failed. We make no attempt to
1299 continue. Note that unless there is debugging value to be had from
1300 a more specific message, or some other good reason, you should use
1301 abort () instead of calling this function directly. */
1303 internal_error (const char *msgid
, ...)
1305 diagnostic_info diagnostic
;
1308 va_start (ap
, msgid
);
1309 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, input_line
,
1311 report_diagnostic (&diagnostic
);
1318 /* Variants of some of the above, which make reference to a particular
1319 DECL node. These are deprecated. */
1322 warning_with_decl (tree decl
, const char *msgid
, ...)
1324 diagnostic_info diagnostic
;
1327 va_start (ap
, msgid
);
1329 /* Do not issue a warning about a decl which came from a system header,
1330 unless -Wsystem-headers. */
1331 if (DECL_IN_SYSTEM_HEADER (decl
) && !warn_system_headers
)
1334 diagnostic_set_info (&diagnostic
, msgid
, &ap
,
1335 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
),
1337 diagnostic_for_decl (global_dc
, &diagnostic
, decl
);
1342 pedwarn_with_decl (tree decl
, const char *msgid
, ...)
1344 diagnostic_info diagnostic
;
1347 va_start (ap
, msgid
);
1349 /* Do not issue a warning about a decl which came from a system header,
1350 unless -Wsystem-headers. */
1351 if (DECL_IN_SYSTEM_HEADER (decl
) && !warn_system_headers
)
1354 diagnostic_set_info (&diagnostic
, msgid
, &ap
,
1355 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
),
1356 pedantic_error_kind ());
1357 diagnostic_for_decl (global_dc
, &diagnostic
, decl
);
1363 error_with_decl (tree decl
, const char *msgid
, ...)
1365 diagnostic_info diagnostic
;
1368 va_start (ap
, msgid
);
1369 diagnostic_set_info (&diagnostic
, msgid
, &ap
,
1370 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
),
1372 diagnostic_for_decl (global_dc
, &diagnostic
, decl
);
1376 /* Special case error functions. Most are implemented in terms of the
1377 above, or should be. */
1379 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1380 runs its second argument through gettext. */
1382 fnotice (FILE *file
, const char *msgid
, ...)
1386 va_start (ap
, msgid
);
1387 vfprintf (file
, _(msgid
), ap
);
1391 /* Warn about a use of an identifier which was marked deprecated. */
1393 warn_deprecated_use (node
)
1396 if (node
== 0 || !warn_deprecated_decl
)
1400 warning ("`%s' is deprecated (declared at %s:%d)",
1401 IDENTIFIER_POINTER (DECL_NAME (node
)),
1402 DECL_SOURCE_FILE (node
), DECL_SOURCE_LINE (node
));
1403 else if (TYPE_P (node
))
1405 const char *what
= NULL
;
1406 tree decl
= TYPE_STUB_DECL (node
);
1408 if (TREE_CODE (TYPE_NAME (node
)) == IDENTIFIER_NODE
)
1409 what
= IDENTIFIER_POINTER (TYPE_NAME (node
));
1410 else if (TREE_CODE (TYPE_NAME (node
)) == TYPE_DECL
1411 && DECL_NAME (TYPE_NAME (node
)))
1412 what
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node
)));
1417 warning ("`%s' is deprecated (declared at %s:%d)", what
,
1418 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
));
1420 warning ("`%s' is deprecated", what
);
1423 warning ("type is deprecated (declared at %s:%d)",
1424 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
));
1426 warning ("type is deprecated");
1430 /* Inform the user that an error occurred while trying to report some
1431 other error. This indicates catastrophic internal inconsistencies,
1432 so give up now. But do try to flush out the previous error.
1433 This mustn't use internal_error, that will cause infinite recursion. */
1436 error_recursion (context
)
1437 diagnostic_context
*context
;
1439 if (context
->lock
< 3)
1440 output_flush (&context
->buffer
);
1443 "Internal compiler error: Error reporting routines re-entered.\n");
1444 fnotice (stderr
, bug_report_request
, bug_report_url
);
1445 exit (FATAL_EXIT_CODE
);
1448 /* Report an internal compiler error in a friendly manner. This is
1449 the function that gets called upon use of abort() in the source
1450 code generally, thanks to a special macro. */
1453 fancy_abort (file
, line
, function
)
1456 const char *function
;
1458 internal_error ("in %s, at %s:%d", function
, trim_filename (file
), line
);
1461 /* Really call the system 'abort'. This has to go right at the end of
1462 this file, so that there are no functions after it that call abort
1463 and get the system abort instead of our macro. */