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
47 /* Format an integer given by va_arg (ARG, type-specifier T) where
48 type-specifier is a precision modifier as indicated by PREC. F is
49 a string used to construct the appropriate format-specifier. */
50 #define output_integer_with_precision(BUFFER, ARG, PREC, T, F) \
55 output_formatted_scalar \
56 (BUFFER, "%" F, va_arg (ARG, T)); \
60 output_formatted_scalar \
61 (BUFFER, "%l" F, va_arg (ARG, long T)); \
65 output_formatted_scalar \
66 (BUFFER, "%ll" F, va_arg (ARG, long long T)); \
76 static void output_flush (output_buffer
*);
77 static void output_do_verbatim (output_buffer
*, text_info
*);
78 static void output_buffer_to_stream (output_buffer
*);
79 static void output_format (output_buffer
*, text_info
*);
80 static void output_indent (output_buffer
*);
82 static char *build_message_string (const char *, ...)
84 static void format_with_decl (output_buffer
*, text_info
*, tree
);
85 static void diagnostic_for_decl (diagnostic_context
*, diagnostic_info
*,
87 static void set_real_maximum_length (output_buffer
*);
88 static void output_append_r (output_buffer
*, const char *, int);
89 static void wrap_text (output_buffer
*, const char *, const char *);
90 static void maybe_wrap_text (output_buffer
*, const char *, const char *);
91 static void output_clear_data (output_buffer
*);
93 static void default_diagnostic_starter (diagnostic_context
*,
95 static void default_diagnostic_finalizer (diagnostic_context
*,
98 static void error_recursion (diagnostic_context
*) ATTRIBUTE_NORETURN
;
99 static bool text_specifies_location (text_info
*, location_t
*);
100 static bool diagnostic_count_diagnostic (diagnostic_context
*,
102 static void diagnostic_action_after_output (diagnostic_context
*,
104 static void real_abort (void) ATTRIBUTE_NORETURN
;
106 extern int rtl_dump_and_exit
;
108 /* A diagnostic_context surrogate for stderr. */
109 static diagnostic_context global_diagnostic_context
;
110 diagnostic_context
*global_dc
= &global_diagnostic_context
;
112 /* Boilerplate text used in two locations. */
113 #define bug_report_request \
114 "Please submit a full bug report,\n\
115 with preprocessed source if appropriate.\n\
116 See %s for instructions.\n"
119 /* Subroutine of output_set_maximum_length. Set up BUFFER's
120 internal maximum characters per line. */
122 set_real_maximum_length (output_buffer
*buffer
)
124 /* If we're told not to wrap lines then do the obvious thing. In case
125 we'll emit prefix only once per diagnostic message, it is appropriate
126 not to increase unnecessarily the line-length cut-off. */
127 if (!output_is_line_wrapping (buffer
)
128 || output_prefixing_rule (buffer
) == DIAGNOSTICS_SHOW_PREFIX_ONCE
129 || output_prefixing_rule (buffer
) == DIAGNOSTICS_SHOW_PREFIX_NEVER
)
130 line_wrap_cutoff (buffer
) = output_line_cutoff (buffer
);
133 int prefix_length
= buffer
->state
.prefix
?
134 strlen (buffer
->state
.prefix
) : 0;
135 /* If the prefix is ridiculously too long, output at least
137 if (output_line_cutoff (buffer
) - prefix_length
< 32)
138 line_wrap_cutoff (buffer
) = output_line_cutoff (buffer
) + 32;
140 line_wrap_cutoff (buffer
) = output_line_cutoff (buffer
);
144 /* Sets the number of maximum characters per line BUFFER can output
145 in line-wrapping mode. A LENGTH value 0 suppresses line-wrapping. */
147 output_set_maximum_length (output_buffer
*buffer
, int length
)
149 output_line_cutoff (buffer
) = length
;
150 set_real_maximum_length (buffer
);
153 /* Sets BUFFER's PREFIX. */
155 output_set_prefix (output_buffer
*buffer
, const char *prefix
)
157 buffer
->state
.prefix
= prefix
;
158 set_real_maximum_length (buffer
);
159 prefix_was_emitted_for (buffer
) = false;
160 output_indentation (buffer
) = 0;
163 /* Return a pointer to the last character emitted in the output
164 BUFFER area. A NULL pointer means no character available. */
166 output_last_position (const output_buffer
*buffer
)
168 const char *p
= NULL
;
170 if (obstack_base (&buffer
->obstack
) != obstack_next_free (&buffer
->obstack
))
171 p
= ((const char *) obstack_next_free (&buffer
->obstack
)) - 1;
175 /* Free BUFFER's prefix, a previously malloc'd string. */
177 output_destroy_prefix (output_buffer
*buffer
)
179 if (buffer
->state
.prefix
!= NULL
)
181 free ((char *) buffer
->state
.prefix
);
182 buffer
->state
.prefix
= NULL
;
186 /* Zero out any text output so far in BUFFER. */
188 output_clear_message_text (output_buffer
*buffer
)
190 obstack_free (&buffer
->obstack
, obstack_base (&buffer
->obstack
));
191 output_text_length (buffer
) = 0;
194 /* Zero out any formatting data used so far by BUFFER. */
196 output_clear_data (output_buffer
*buffer
)
198 prefix_was_emitted_for (buffer
) = false;
199 output_indentation (buffer
) = 0;
202 /* Construct an output BUFFER with PREFIX and of MAXIMUM_LENGTH
203 characters per line. */
205 init_output_buffer (output_buffer
*buffer
, const char *prefix
,
208 memset (buffer
, 0, sizeof (output_buffer
));
209 obstack_init (&buffer
->obstack
);
210 output_buffer_attached_stream (buffer
) = stderr
;
211 output_line_cutoff (buffer
) = maximum_length
;
212 output_prefixing_rule (buffer
) = diagnostic_prefixing_rule (global_dc
);
213 output_set_prefix (buffer
, prefix
);
214 output_text_length (buffer
) = 0;
215 output_clear_data (buffer
);
218 /* Reinitialize BUFFER. */
220 output_clear (output_buffer
*buffer
)
222 output_clear_message_text (buffer
);
223 output_clear_data (buffer
);
226 /* Finishes constructing a NULL-terminated character string representing
227 the BUFFERed message. */
229 output_finalize_message (output_buffer
*buffer
)
231 obstack_1grow (&buffer
->obstack
, '\0');
232 return output_message_text (buffer
);
235 /* Return the amount of characters BUFFER can accept to
238 output_space_left (const output_buffer
*buffer
)
240 return line_wrap_cutoff (buffer
) - output_text_length (buffer
);
243 /* Write out BUFFER's prefix. */
245 output_emit_prefix (output_buffer
*buffer
)
247 if (buffer
->state
.prefix
!= NULL
)
249 switch (output_prefixing_rule (buffer
))
252 case DIAGNOSTICS_SHOW_PREFIX_NEVER
:
255 case DIAGNOSTICS_SHOW_PREFIX_ONCE
:
256 if (prefix_was_emitted_for (buffer
))
258 output_indent (buffer
);
261 output_indentation (buffer
) += 3;
264 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE
:
266 int prefix_length
= strlen (buffer
->state
.prefix
);
267 output_append_r (buffer
, buffer
->state
.prefix
, prefix_length
);
268 prefix_was_emitted_for (buffer
) = true;
275 /* Have BUFFER start a new line. */
277 output_add_newline (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 (output_buffer
*buffer
, int c
)
287 if (output_is_line_wrapping (buffer
) && output_space_left (buffer
) <= 0)
288 output_add_newline (buffer
);
289 obstack_1grow (&buffer
->obstack
, c
);
290 ++output_text_length (buffer
);
293 /* Adds a space to BUFFER. */
295 output_add_space (output_buffer
*buffer
)
297 if (output_is_line_wrapping (buffer
) && output_space_left (buffer
) <= 0)
299 output_add_newline (buffer
);
302 obstack_1grow (&buffer
->obstack
, ' ');
303 ++output_text_length (buffer
);
306 /* These functions format an INTEGER into BUFFER as suggested by their
309 output_decimal (output_buffer
*buffer
, int i
)
311 output_formatted_scalar (buffer
, "%d", i
);
315 output_host_wide_integer (output_buffer
*buffer
, HOST_WIDE_INT i
)
317 output_formatted_scalar (buffer
, HOST_WIDE_INT_PRINT_DEC
, i
);
321 output_pointer (output_buffer
*buffer
, void *p
)
323 output_formatted_scalar (buffer
, HOST_PTR_PRINTF
, p
);
326 /* Append to BUFFER a string specified by its STARTING character
329 output_append_r (output_buffer
*buffer
, const char *start
, int length
)
331 obstack_grow (&buffer
->obstack
, start
, length
);
332 output_text_length (buffer
) += length
;
335 /* Append a string delimited by START and END to BUFFER. No wrapping is
336 done. However, if beginning a new line then emit BUFFER->state.prefix
337 and skip any leading whitespace if appropriate. The caller must ensure
338 that it is safe to do so. */
340 output_append (output_buffer
*buffer
, const char *start
, const char *end
)
342 /* Emit prefix and skip whitespace if we're starting a new line. */
343 if (is_starting_newline (buffer
))
345 output_emit_prefix (buffer
);
346 if (output_is_line_wrapping (buffer
))
347 while (start
!= end
&& *start
== ' ')
350 output_append_r (buffer
, start
, end
- start
);
353 /* Insert enough spaces into BUFFER to bring the column position to
354 the current indentation level, assuming that a newline has just
355 been written to the buffer. */
357 output_indent (output_buffer
*buffer
)
359 int n
= output_indentation (buffer
);
362 for (i
= 0; i
< n
; ++i
)
363 output_add_character (buffer
, ' ');
366 /* Wrap a text delimited by START and END into BUFFER. */
368 wrap_text (output_buffer
*buffer
, const char *start
, const char *end
)
370 bool is_wrapping
= output_is_line_wrapping (buffer
);
374 /* Dump anything bordered by whitespaces. */
376 const char *p
= start
;
377 while (p
!= end
&& *p
!= ' ' && *p
!= '\n')
379 if (is_wrapping
&& p
- start
>= output_space_left (buffer
))
380 output_add_newline (buffer
);
381 output_append (buffer
, start
, p
);
385 if (start
!= end
&& *start
== ' ')
387 output_add_space (buffer
);
390 if (start
!= end
&& *start
== '\n')
392 output_add_newline (buffer
);
398 /* Same as wrap_text but wrap text only when in line-wrapping mode. */
400 maybe_wrap_text (output_buffer
*buffer
, const char *start
, const char *end
)
402 if (output_is_line_wrapping (buffer
))
403 wrap_text (buffer
, start
, end
);
405 output_append (buffer
, start
, end
);
409 /* Append a STRING to BUFFER; the STRING might be line-wrapped if in
412 output_add_string (output_buffer
*buffer
, const char *str
)
414 maybe_wrap_text (buffer
, str
, str
+ (str
? strlen (str
) : 0));
417 /* Append an identifier ID to BUFFER. */
419 output_add_identifier (output_buffer
*buffer
, tree id
)
421 output_append (buffer
, IDENTIFIER_POINTER (id
),
422 IDENTIFIER_POINTER (id
) + IDENTIFIER_LENGTH (id
));
425 /* Flush the content of BUFFER onto the attached stream,
429 output_buffer_to_stream (output_buffer
*buffer
)
431 const char *text
= output_finalize_message (buffer
);
432 fputs (text
, output_buffer_attached_stream (buffer
));
433 output_clear_message_text (buffer
);
436 /* Format a message pointed to by TEXT. The following format specifiers are
437 recognized as being language independent:
438 %d, %i: (signed) integer in base ten.
439 %u: unsigned integer in base ten.
440 %o: unsigned integer in base eight.
441 %x: unsigned integer in base sixteen.
442 %ld, %li, %lo, %lu, %lx: long versions of the above.
443 %lld, %lli, %llo, %llu, %llx: long long versions.
444 %wd, %wi, %wo, %wu, %wx: HOST_WIDE_INT versions.
448 %m: strerror(text->err_no) - does not consume a value from args_ptr.
450 %*.s: a substring the length of which is specified by an integer.
453 output_format (output_buffer
*buffer
, text_info
*text
)
455 for (; *text
->format_spec
; ++text
->format_spec
)
462 const char *p
= text
->format_spec
;
463 while (*p
&& *p
!= '%')
465 wrap_text (buffer
, text
->format_spec
, p
);
466 text
->format_spec
= p
;
469 if (*text
->format_spec
== '\0')
472 /* We got a '%'. Parse precision modifiers, if any. */
473 switch (*++text
->format_spec
)
483 while (*++text
->format_spec
== 'l');
489 /* We don't support precision behond that of "long long". */
493 switch (*text
->format_spec
)
496 output_add_character (buffer
, va_arg (*text
->args_ptr
, int));
502 output_formatted_scalar
503 (buffer
, HOST_WIDE_INT_PRINT_DEC
,
504 va_arg (*text
->args_ptr
, HOST_WIDE_INT
));
506 output_integer_with_precision
507 (buffer
, *text
->args_ptr
, precision
, int, "d");
512 output_formatted_scalar
513 (buffer
, "%" HOST_WIDE_INT_PRINT
"o",
514 va_arg (*text
->args_ptr
, unsigned HOST_WIDE_INT
));
516 output_integer_with_precision
517 (buffer
, *text
->args_ptr
, precision
, unsigned, "u");
521 output_add_string (buffer
, va_arg (*text
->args_ptr
, const char *));
525 output_pointer (buffer
, va_arg (*text
->args_ptr
, void *));
530 output_formatted_scalar
531 (buffer
, HOST_WIDE_INT_PRINT_UNSIGNED
,
532 va_arg (*text
->args_ptr
, unsigned HOST_WIDE_INT
));
534 output_integer_with_precision
535 (buffer
, *text
->args_ptr
, precision
, unsigned, "u");
540 output_formatted_scalar
541 (buffer
, HOST_WIDE_INT_PRINT_HEX
,
542 va_arg (*text
->args_ptr
, unsigned HOST_WIDE_INT
));
544 output_integer_with_precision
545 (buffer
, *text
->args_ptr
, precision
, unsigned, "x");
549 output_add_string (buffer
, xstrerror (text
->err_no
));
553 output_add_character (buffer
, '%');
558 const location_t
*locus
= va_arg (*text
->args_ptr
, location_t
*);
559 output_add_string (buffer
, "file '");
560 output_add_string (buffer
, locus
->file
);
561 output_add_string (buffer
, "', line ");
562 output_decimal (buffer
, locus
->line
);
570 /* We handle no precision specifier but `%.*s'. */
571 if (*++text
->format_spec
!= '*')
573 else if (*++text
->format_spec
!= 's')
575 n
= va_arg (*text
->args_ptr
, int);
576 s
= va_arg (*text
->args_ptr
, const char *);
577 output_append (buffer
, s
, s
+ n
);
582 if (!buffer
->format_decoder
583 || !(*buffer
->format_decoder
) (buffer
, text
))
585 /* Hmmm. The front-end failed to install a format translator
586 but called us with an unrecognized format. Or, maybe, the
587 translated string just contains an invalid format, or
588 has formats in the wrong order. Sorry. */
595 /* Return a malloc'd string containing MSG formatted a la printf. The
596 caller is responsible for freeing the memory. */
598 build_message_string (const char *msg
, ...)
604 vasprintf (&str
, msg
, ap
);
610 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
612 file_name_as_prefix (const char *f
)
614 return build_message_string ("%s: ", f
);
617 /* Format a message into BUFFER a la printf. */
619 output_printf (struct output_buffer
*buffer
, const char *msgid
, ...)
624 va_start (ap
, msgid
);
627 text
.format_spec
= _(msgid
);
628 output_format (buffer
, &text
);
632 /* Print a message relevant to the given DECL. */
634 format_with_decl (output_buffer
*buffer
, text_info
*text
, tree decl
)
638 /* Do magic to get around lack of varargs support for insertion
639 of arguments into existing list. We know that the decl is first;
640 we ass_u_me that it will be printed with "%s". */
641 for (p
= text
->format_spec
; *p
; ++p
)
647 else if (*(p
+ 1) != 's')
654 /* Print the left-hand substring. */
655 maybe_wrap_text (buffer
, text
->format_spec
, p
);
657 if (*p
== '%') /* Print the name. */
659 const char *const n
= (DECL_NAME (decl
)
660 ? (*lang_hooks
.decl_printable_name
) (decl
, 2)
661 : _("((anonymous))"));
662 output_add_string (buffer
, n
);
666 if (ISALPHA (*(p
- 1) & 0xFF))
671 if (*p
) /* Print the rest of the message. */
673 text
->format_spec
= p
;
674 output_format (buffer
, text
);
678 /* Flush the content of BUFFER onto the attached stream. */
680 output_flush (output_buffer
*buffer
)
682 output_buffer_to_stream (buffer
);
683 output_clear_data (buffer
);
684 fputc ('\n', output_buffer_attached_stream (buffer
));
685 fflush (output_buffer_attached_stream (buffer
));
688 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
689 settings needed by BUFFER for a verbatim formatting. */
691 output_do_verbatim (output_buffer
*buffer
, text_info
*text
)
693 diagnostic_prefixing_rule_t rule
= output_prefixing_rule (buffer
);
694 int line_cutoff
= output_line_cutoff (buffer
);
696 /* Set verbatim mode. */
697 output_prefixing_rule (buffer
) = DIAGNOSTICS_SHOW_PREFIX_NEVER
;
698 output_line_cutoff (buffer
) = 0;
699 /* Do the actual formatting. */
700 output_format (buffer
, text
);
701 /* Restore previous settings. */
702 output_prefixing_rule (buffer
) = rule
;
703 output_line_cutoff (buffer
) = line_cutoff
;
706 /* Output MESSAGE verbatim into BUFFER. */
708 output_verbatim (output_buffer
*buffer
, const char *msgid
, ...)
713 va_start (ap
, msgid
);
716 text
.format_spec
= _(msgid
);
717 output_do_verbatim (buffer
, &text
);
722 /* Initialize the diagnostic message outputting machinery. */
724 diagnostic_initialize (diagnostic_context
*context
)
726 memset (context
, 0, sizeof *context
);
727 obstack_init (&context
->buffer
.obstack
);
729 /* By default, diagnostics are sent to stderr. */
730 output_buffer_attached_stream (&context
->buffer
) = stderr
;
732 /* By default, we emit prefixes once per message. */
733 diagnostic_prefixing_rule (context
) = DIAGNOSTICS_SHOW_PREFIX_ONCE
;
735 diagnostic_starter (context
) = default_diagnostic_starter
;
736 diagnostic_finalizer (context
) = default_diagnostic_finalizer
;
737 context
->warnings_are_errors_message
= warnings_are_errors
;
740 /* Returns true if the next format specifier in TEXT is a format specifier
741 for a location_t. If so, update the object pointed by LOCUS to reflect
742 the specified location in *TEXT->args_ptr. */
744 text_specifies_location (text_info
*text
, location_t
*locus
)
747 /* Skip any leading text. */
748 for (p
= text
->format_spec
; *p
&& *p
!= '%'; ++p
)
751 /* Extract the location information if any. */
752 if (*p
== '%' && *++p
== 'H')
754 *locus
= *va_arg (*text
->args_ptr
, location_t
*);
755 text
->format_spec
= p
+ 1;
763 diagnostic_set_info (diagnostic_info
*diagnostic
, const char *msgid
,
764 va_list *args
, location_t location
,
767 diagnostic
->message
.err_no
= errno
;
768 diagnostic
->message
.args_ptr
= args
;
769 diagnostic
->message
.format_spec
= _(msgid
);
770 /* If the diagnostic message doesn't specify a location,
772 if (!text_specifies_location (&diagnostic
->message
, &diagnostic
->location
))
773 diagnostic
->location
= location
;
774 diagnostic
->kind
= kind
;
777 /* Return a malloc'd string describing a location. The caller is
778 responsible for freeing the memory. */
780 diagnostic_build_prefix (diagnostic_info
*diagnostic
)
782 static const char *const diagnostic_kind_text
[] = {
783 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
784 #include "diagnostic.def"
785 #undef DEFINE_DIAGNOSTIC_KIND
788 if (diagnostic
->kind
>= DK_LAST_DIAGNOSTIC_KIND
)
791 return diagnostic
->location
.file
792 ? build_message_string ("%s:%d: %s",
793 diagnostic
->location
.file
,
794 diagnostic
->location
.line
,
795 _(diagnostic_kind_text
[diagnostic
->kind
]))
796 : build_message_string ("%s: %s", progname
,
797 _(diagnostic_kind_text
[diagnostic
->kind
]));
801 diagnostic_flush_buffer (diagnostic_context
*context
)
803 output_buffer_to_stream (&context
->buffer
);
804 fflush (output_buffer_attached_stream (&context
->buffer
));
807 /* Count a diagnostic. Return true if the message should be printed. */
809 diagnostic_count_diagnostic (diagnostic_context
*context
,
810 diagnostic_info
*diagnostic
)
812 diagnostic_t kind
= diagnostic
->kind
;
820 #ifndef ENABLE_CHECKING
821 /* When not checking, ICEs are converted to fatal errors when an
822 error has already occurred. This is counteracted by
824 if ((diagnostic_kind_count (context
, DK_ERROR
) > 0
825 || diagnostic_kind_count (context
, DK_SORRY
) > 0)
826 && !context
->abort_on_error
)
828 fnotice (stderr
, "%s:%d: confused by earlier errors, bailing out\n",
829 diagnostic
->location
.file
, diagnostic
->location
.line
);
830 exit (FATAL_EXIT_CODE
);
833 if (context
->internal_error
)
834 (*context
->internal_error
) (diagnostic
->message
.format_spec
,
835 diagnostic
->message
.args_ptr
);
838 case DK_FATAL
: case DK_SORRY
:
839 case DK_ANACHRONISM
: case DK_NOTE
:
840 ++diagnostic_kind_count (context
, kind
);
844 if (!diagnostic_report_warnings_p ())
847 if (!warnings_are_errors
)
849 ++diagnostic_kind_count (context
, DK_WARNING
);
853 if (context
->warnings_are_errors_message
)
855 output_verbatim (&context
->buffer
,
856 "%s: warnings being treated as errors\n", progname
);
857 context
->warnings_are_errors_message
= false;
860 /* and fall through */
862 ++diagnostic_kind_count (context
, DK_ERROR
);
869 /* Take any action which is expected to happen after the diagnostic
870 is written out. This function does not always return. */
872 diagnostic_action_after_output (diagnostic_context
*context
,
873 diagnostic_info
*diagnostic
)
875 switch (diagnostic
->kind
)
885 if (context
->abort_on_error
)
890 if (context
->abort_on_error
)
893 fnotice (stderr
, bug_report_request
, bug_report_url
);
894 exit (FATAL_EXIT_CODE
);
897 if (context
->abort_on_error
)
900 fnotice (stderr
, "compilation terminated.\n");
901 exit (FATAL_EXIT_CODE
);
908 /* Called when the start of a function definition is parsed,
909 this function prints on stderr the name of the function. */
911 announce_function (tree decl
)
915 if (rtl_dump_and_exit
)
916 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl
)));
918 verbatim (" %s", (*lang_hooks
.decl_printable_name
) (decl
, 2));
920 output_needs_newline (&global_dc
->buffer
) = true;
921 diagnostic_set_last_function (global_dc
);
925 /* The default function to print out name of current function that caused
928 lhd_print_error_function (diagnostic_context
*context
, const char *file
)
930 if (diagnostic_last_function_changed (context
))
932 const char *old_prefix
= output_prefix (&context
->buffer
);
933 char *new_prefix
= file
? build_message_string ("%s: ", file
) : NULL
;
935 output_set_prefix (&context
->buffer
, new_prefix
);
937 if (current_function_decl
== NULL
)
938 output_add_string (&context
->buffer
, _("At top level:"));
941 if (TREE_CODE (TREE_TYPE (current_function_decl
)) == METHOD_TYPE
)
943 (&context
->buffer
, "In member function `%s':",
944 (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2));
947 (&context
->buffer
, "In function `%s':",
948 (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2));
950 output_add_newline (&context
->buffer
);
952 diagnostic_set_last_function (context
);
953 output_buffer_to_stream (&context
->buffer
);
954 context
->buffer
.state
.prefix
= old_prefix
;
955 free ((char*) new_prefix
);
959 /* Prints out, if necessary, the name of the current function
960 that caused an error. Called from all error and warning functions.
961 We ignore the FILE parameter, as it cannot be relied upon. */
964 diagnostic_report_current_function (diagnostic_context
*context
)
966 diagnostic_report_current_module (context
);
967 (*lang_hooks
.print_error_function
) (context
, input_filename
);
971 diagnostic_report_current_module (diagnostic_context
*context
)
973 struct file_stack
*p
;
975 if (output_needs_newline (&context
->buffer
))
977 output_add_newline (&context
->buffer
);
978 output_needs_newline (&context
->buffer
) = false;
981 if (input_file_stack
&& input_file_stack
->next
!= 0
982 && diagnostic_last_module_changed (context
))
984 for (p
= input_file_stack
->next
; p
; p
= p
->next
)
985 if (p
== input_file_stack
->next
)
986 output_verbatim (&context
->buffer
,
987 "In file included from %s:%d",
988 p
->location
.file
, p
->location
.line
);
990 output_verbatim (&context
->buffer
,
992 p
->location
.file
, p
->location
.line
);
993 output_verbatim (&context
->buffer
, ":\n");
994 diagnostic_set_last_module (context
);
999 default_diagnostic_starter (diagnostic_context
*context
,
1000 diagnostic_info
*diagnostic
)
1002 diagnostic_report_current_function (context
);
1003 output_set_prefix (&context
->buffer
, diagnostic_build_prefix (diagnostic
));
1007 default_diagnostic_finalizer (diagnostic_context
*context
,
1008 diagnostic_info
*diagnostic
__attribute__((unused
)))
1010 output_destroy_prefix (&context
->buffer
);
1013 /* Report a diagnostic message (an error or a warning) as specified by
1014 DC. This function is *the* subroutine in terms of which front-ends
1015 should implement their specific diagnostic handling modules. The
1016 front-end independent format specifiers are exactly those described
1017 in the documentation of output_format. */
1020 diagnostic_report_diagnostic (diagnostic_context
*context
,
1021 diagnostic_info
*diagnostic
)
1023 if (context
->lock
++)
1024 error_recursion (context
);
1026 if (diagnostic_count_diagnostic (context
, diagnostic
))
1028 (*diagnostic_starter (context
)) (context
, diagnostic
);
1029 output_format (&context
->buffer
, &diagnostic
->message
);
1030 (*diagnostic_finalizer (context
)) (context
, diagnostic
);
1031 output_flush (&context
->buffer
);
1032 diagnostic_action_after_output (context
, diagnostic
);
1038 /* Report a diagnostic MESSAGE at the declaration DECL.
1039 MSG is a format string which uses %s to substitute the declaration
1040 name; subsequent substitutions are a la output_format. */
1042 diagnostic_for_decl (diagnostic_context
*context
,
1043 diagnostic_info
*diagnostic
, tree decl
)
1045 if (context
->lock
++)
1046 error_recursion (context
);
1048 if (diagnostic_count_diagnostic (context
, diagnostic
))
1050 (*diagnostic_starter (context
)) (context
, diagnostic
);
1051 format_with_decl (&context
->buffer
, &diagnostic
->message
, decl
);
1052 (*diagnostic_finalizer (context
)) (context
, diagnostic
);
1053 output_flush (&context
->buffer
);
1054 diagnostic_action_after_output (context
, diagnostic
);
1060 /* Given a partial pathname as input, return another pathname that
1061 shares no directory elements with the pathname of __FILE__. This
1062 is used by fancy_abort() to print `Internal compiler error in expr.c'
1063 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1066 trim_filename (const char *name
)
1068 static const char this_file
[] = __FILE__
;
1069 const char *p
= name
, *q
= this_file
;
1071 /* First skip any "../" in each filename. This allows us to give a proper
1072 reference to a file in a subdirectory. */
1073 while (p
[0] == '.' && p
[1] == '.'
1074 && (p
[2] == DIR_SEPARATOR
1075 #ifdef DIR_SEPARATOR_2
1076 || p
[2] == DIR_SEPARATOR_2
1081 while (q
[0] == '.' && q
[1] == '.'
1082 && (q
[2] == DIR_SEPARATOR
1083 #ifdef DIR_SEPARATOR_2
1084 || p
[2] == DIR_SEPARATOR_2
1089 /* Now skip any parts the two filenames have in common. */
1090 while (*p
== *q
&& *p
!= 0 && *q
!= 0)
1093 /* Now go backwards until the previous directory separator. */
1094 while (p
> name
&& p
[-1] != DIR_SEPARATOR
1095 #ifdef DIR_SEPARATOR_2
1096 && p
[-1] != DIR_SEPARATOR_2
1104 /* Standard error reporting routines in increasing order of severity.
1105 All of these take arguments like printf. */
1107 /* Text to be emitted verbatim to the error message stream; this
1108 produces no prefix and disables line-wrapping. Use rarely. */
1110 verbatim (const char *msgid
, ...)
1115 va_start (ap
, msgid
);
1116 text
.err_no
= errno
;
1117 text
.args_ptr
= &ap
;
1118 text
.format_spec
= _(msgid
);
1119 output_do_verbatim (&global_dc
->buffer
, &text
);
1120 output_buffer_to_stream (&global_dc
->buffer
);
1124 /* An informative note. Use this for additional details on an error
1127 inform (const char *msgid
, ...)
1129 diagnostic_info diagnostic
;
1132 va_start (ap
, msgid
);
1133 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_location
, DK_NOTE
);
1134 report_diagnostic (&diagnostic
);
1138 /* A warning. Use this for code which is correct according to the
1139 relevant language specification but is likely to be buggy anyway. */
1141 warning (const char *msgid
, ...)
1143 diagnostic_info diagnostic
;
1146 va_start (ap
, msgid
);
1147 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_location
, DK_WARNING
);
1148 report_diagnostic (&diagnostic
);
1152 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
1153 given on the command line, in which case it issues an error. Use
1154 this for diagnostics required by the relevant language standard,
1155 if you have chosen not to make them errors.
1157 Note that these diagnostics are issued independent of the setting
1158 of the -pedantic command-line switch. To get a warning enabled
1159 only with that switch, write "if (pedantic) pedwarn (...);" */
1161 pedwarn (const char *msgid
, ...)
1163 diagnostic_info diagnostic
;
1166 va_start (ap
, msgid
);
1167 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_location
,
1168 pedantic_error_kind ());
1169 report_diagnostic (&diagnostic
);
1173 /* A hard error: the code is definitely ill-formed, and an object file
1174 will not be produced. */
1176 error (const char *msgid
, ...)
1178 diagnostic_info diagnostic
;
1181 va_start (ap
, msgid
);
1182 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_location
, DK_ERROR
);
1183 report_diagnostic (&diagnostic
);
1187 /* "Sorry, not implemented." Use for a language feature which is
1188 required by the relevant specification but not implemented by GCC.
1189 An object file will not be produced. */
1191 sorry (const char *msgid
, ...)
1193 diagnostic_info diagnostic
;
1196 va_start (ap
, msgid
);
1197 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_location
, DK_SORRY
);
1198 report_diagnostic (&diagnostic
);
1202 /* An error which is severe enough that we make no attempt to
1203 continue. Do not use this for internal consistency checks; that's
1204 internal_error. Use of this function should be rare. */
1206 fatal_error (const char *msgid
, ...)
1208 diagnostic_info diagnostic
;
1211 va_start (ap
, msgid
);
1212 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_location
, DK_FATAL
);
1213 report_diagnostic (&diagnostic
);
1220 /* An internal consistency check has failed. We make no attempt to
1221 continue. Note that unless there is debugging value to be had from
1222 a more specific message, or some other good reason, you should use
1223 abort () instead of calling this function directly. */
1225 internal_error (const char *msgid
, ...)
1227 diagnostic_info diagnostic
;
1230 va_start (ap
, msgid
);
1231 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_location
, DK_ICE
);
1232 report_diagnostic (&diagnostic
);
1239 /* Variants of some of the above, which make reference to a particular
1240 DECL node. These are deprecated. */
1243 warning_with_decl (tree decl
, const char *msgid
, ...)
1245 diagnostic_info diagnostic
;
1248 va_start (ap
, msgid
);
1250 /* Do not issue a warning about a decl which came from a system header,
1251 unless -Wsystem-headers. */
1252 if (DECL_IN_SYSTEM_HEADER (decl
) && !warn_system_headers
)
1255 diagnostic_set_info (&diagnostic
, msgid
, &ap
,
1256 DECL_SOURCE_LOCATION (decl
), DK_WARNING
);
1257 diagnostic_for_decl (global_dc
, &diagnostic
, decl
);
1262 pedwarn_with_decl (tree decl
, const char *msgid
, ...)
1264 diagnostic_info diagnostic
;
1267 va_start (ap
, msgid
);
1269 /* Do not issue a warning about a decl which came from a system header,
1270 unless -Wsystem-headers. */
1271 if (DECL_IN_SYSTEM_HEADER (decl
) && !warn_system_headers
)
1274 diagnostic_set_info (&diagnostic
, msgid
, &ap
,
1275 DECL_SOURCE_LOCATION (decl
), pedantic_error_kind ());
1276 diagnostic_for_decl (global_dc
, &diagnostic
, decl
);
1282 error_with_decl (tree decl
, const char *msgid
, ...)
1284 diagnostic_info diagnostic
;
1287 va_start (ap
, msgid
);
1288 diagnostic_set_info (&diagnostic
, msgid
, &ap
,
1289 DECL_SOURCE_LOCATION (decl
), DK_ERROR
);
1290 diagnostic_for_decl (global_dc
, &diagnostic
, decl
);
1294 /* Special case error functions. Most are implemented in terms of the
1295 above, or should be. */
1297 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1298 runs its second argument through gettext. */
1300 fnotice (FILE *file
, const char *msgid
, ...)
1304 va_start (ap
, msgid
);
1305 vfprintf (file
, _(msgid
), ap
);
1309 /* Warn about a use of an identifier which was marked deprecated. */
1311 warn_deprecated_use (tree node
)
1313 if (node
== 0 || !warn_deprecated_decl
)
1317 warning ("`%s' is deprecated (declared at %s:%d)",
1318 IDENTIFIER_POINTER (DECL_NAME (node
)),
1319 DECL_SOURCE_FILE (node
), DECL_SOURCE_LINE (node
));
1320 else if (TYPE_P (node
))
1322 const char *what
= NULL
;
1323 tree decl
= TYPE_STUB_DECL (node
);
1325 if (TREE_CODE (TYPE_NAME (node
)) == IDENTIFIER_NODE
)
1326 what
= IDENTIFIER_POINTER (TYPE_NAME (node
));
1327 else if (TREE_CODE (TYPE_NAME (node
)) == TYPE_DECL
1328 && DECL_NAME (TYPE_NAME (node
)))
1329 what
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node
)));
1334 warning ("`%s' is deprecated (declared at %s:%d)", what
,
1335 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
));
1337 warning ("`%s' is deprecated", what
);
1340 warning ("type is deprecated (declared at %s:%d)",
1341 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
));
1343 warning ("type is deprecated");
1347 /* Inform the user that an error occurred while trying to report some
1348 other error. This indicates catastrophic internal inconsistencies,
1349 so give up now. But do try to flush out the previous error.
1350 This mustn't use internal_error, that will cause infinite recursion. */
1353 error_recursion (diagnostic_context
*context
)
1355 if (context
->lock
< 3)
1356 output_flush (&context
->buffer
);
1359 "Internal compiler error: Error reporting routines re-entered.\n");
1360 fnotice (stderr
, bug_report_request
, bug_report_url
);
1361 exit (FATAL_EXIT_CODE
);
1364 /* Report an internal compiler error in a friendly manner. This is
1365 the function that gets called upon use of abort() in the source
1366 code generally, thanks to a special macro. */
1369 fancy_abort (const char *file
, int line
, const char *function
)
1371 internal_error ("in %s, at %s:%d", function
, trim_filename (file
), line
);
1374 /* Really call the system 'abort'. This has to go right at the end of
1375 this file, so that there are no functions after it that call abort
1376 and get the system abort instead of our macro. */