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 (output_buffer
*);
49 static void output_do_verbatim (output_buffer
*, text_info
*);
50 static void output_buffer_to_stream (output_buffer
*);
51 static void output_format (output_buffer
*, text_info
*);
52 static void output_indent (output_buffer
*);
54 static char *build_message_string (const char *, ...)
56 static void format_with_decl (output_buffer
*, text_info
*, tree
);
57 static void diagnostic_for_decl (diagnostic_context
*, diagnostic_info
*,
59 static void set_real_maximum_length (output_buffer
*);
61 static void output_unsigned_decimal (output_buffer
*, unsigned int);
62 static void output_long_decimal (output_buffer
*, long int);
63 static void output_long_unsigned_decimal (output_buffer
*,
65 static void output_octal (output_buffer
*, unsigned int);
66 static void output_long_octal (output_buffer
*, unsigned long int);
67 static void output_hexadecimal (output_buffer
*, unsigned int);
68 static void output_long_hexadecimal (output_buffer
*, unsigned long int);
69 static void output_append_r (output_buffer
*, const char *, int);
70 static void wrap_text (output_buffer
*, const char *, const char *);
71 static void maybe_wrap_text (output_buffer
*, const char *, const char *);
72 static void output_clear_data (output_buffer
*);
74 static void default_diagnostic_starter (diagnostic_context
*,
76 static void default_diagnostic_finalizer (diagnostic_context
*,
79 static void error_recursion (diagnostic_context
*) ATTRIBUTE_NORETURN
;
80 static bool text_specifies_location (text_info
*, location_t
*);
81 static bool diagnostic_count_diagnostic (diagnostic_context
*,
83 static void diagnostic_action_after_output (diagnostic_context
*,
85 static void real_abort (void) ATTRIBUTE_NORETURN
;
87 extern int rtl_dump_and_exit
;
88 extern int warnings_are_errors
;
90 /* A diagnostic_context surrogate for stderr. */
91 static diagnostic_context global_diagnostic_context
;
92 diagnostic_context
*global_dc
= &global_diagnostic_context
;
94 /* Boilerplate text used in two locations. */
95 #define bug_report_request \
96 "Please submit a full bug report,\n\
97 with preprocessed source if appropriate.\n\
98 See %s for instructions.\n"
101 /* Subroutine of output_set_maximum_length. Set up BUFFER's
102 internal maximum characters per line. */
104 set_real_maximum_length (output_buffer
*buffer
)
106 /* If we're told not to wrap lines then do the obvious thing. In case
107 we'll emit prefix only once per diagnostic message, it is appropriate
108 not to increase unnecessarily the line-length cut-off. */
109 if (!output_is_line_wrapping (buffer
)
110 || output_prefixing_rule (buffer
) == DIAGNOSTICS_SHOW_PREFIX_ONCE
111 || output_prefixing_rule (buffer
) == DIAGNOSTICS_SHOW_PREFIX_NEVER
)
112 line_wrap_cutoff (buffer
) = output_line_cutoff (buffer
);
115 int prefix_length
= buffer
->state
.prefix
?
116 strlen (buffer
->state
.prefix
) : 0;
117 /* If the prefix is ridiculously too long, output at least
119 if (output_line_cutoff (buffer
) - prefix_length
< 32)
120 line_wrap_cutoff (buffer
) = output_line_cutoff (buffer
) + 32;
122 line_wrap_cutoff (buffer
) = output_line_cutoff (buffer
);
126 /* Sets the number of maximum characters per line BUFFER can output
127 in line-wrapping mode. A LENGTH value 0 suppresses line-wrapping. */
129 output_set_maximum_length (output_buffer
*buffer
, int length
)
131 output_line_cutoff (buffer
) = length
;
132 set_real_maximum_length (buffer
);
135 /* Sets BUFFER's PREFIX. */
137 output_set_prefix (output_buffer
*buffer
, const char *prefix
)
139 buffer
->state
.prefix
= prefix
;
140 set_real_maximum_length (buffer
);
141 prefix_was_emitted_for (buffer
) = false;
142 output_indentation (buffer
) = 0;
145 /* Return a pointer to the last character emitted in the output
146 BUFFER area. A NULL pointer means no character available. */
148 output_last_position (const output_buffer
*buffer
)
150 const char *p
= NULL
;
152 if (obstack_base (&buffer
->obstack
) != obstack_next_free (&buffer
->obstack
))
153 p
= ((const char *) obstack_next_free (&buffer
->obstack
)) - 1;
157 /* Free BUFFER's prefix, a previously malloc'd string. */
159 output_destroy_prefix (output_buffer
*buffer
)
161 if (buffer
->state
.prefix
!= NULL
)
163 free ((char *) buffer
->state
.prefix
);
164 buffer
->state
.prefix
= NULL
;
168 /* Zero out any text output so far in BUFFER. */
170 output_clear_message_text (output_buffer
*buffer
)
172 obstack_free (&buffer
->obstack
, obstack_base (&buffer
->obstack
));
173 output_text_length (buffer
) = 0;
176 /* Zero out any formatting data used so far by BUFFER. */
178 output_clear_data (output_buffer
*buffer
)
180 prefix_was_emitted_for (buffer
) = false;
181 output_indentation (buffer
) = 0;
184 /* Construct an output BUFFER with PREFIX and of MAXIMUM_LENGTH
185 characters per line. */
187 init_output_buffer (output_buffer
*buffer
, const char *prefix
,
190 memset (buffer
, 0, sizeof (output_buffer
));
191 obstack_init (&buffer
->obstack
);
192 output_buffer_attached_stream (buffer
) = stderr
;
193 output_line_cutoff (buffer
) = maximum_length
;
194 output_prefixing_rule (buffer
) = diagnostic_prefixing_rule (global_dc
);
195 output_set_prefix (buffer
, prefix
);
196 output_text_length (buffer
) = 0;
197 output_clear_data (buffer
);
200 /* Reinitialize BUFFER. */
202 output_clear (output_buffer
*buffer
)
204 output_clear_message_text (buffer
);
205 output_clear_data (buffer
);
208 /* Finishes constructing a NULL-terminated character string representing
209 the BUFFERed message. */
211 output_finalize_message (output_buffer
*buffer
)
213 obstack_1grow (&buffer
->obstack
, '\0');
214 return output_message_text (buffer
);
217 /* Return the amount of characters BUFFER can accept to
220 output_space_left (const output_buffer
*buffer
)
222 return line_wrap_cutoff (buffer
) - output_text_length (buffer
);
225 /* Write out BUFFER's prefix. */
227 output_emit_prefix (output_buffer
*buffer
)
229 if (buffer
->state
.prefix
!= NULL
)
231 switch (output_prefixing_rule (buffer
))
234 case DIAGNOSTICS_SHOW_PREFIX_NEVER
:
237 case DIAGNOSTICS_SHOW_PREFIX_ONCE
:
238 if (prefix_was_emitted_for (buffer
))
240 output_indent (buffer
);
243 output_indentation (buffer
) += 3;
246 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE
:
248 int prefix_length
= strlen (buffer
->state
.prefix
);
249 output_append_r (buffer
, buffer
->state
.prefix
, prefix_length
);
250 prefix_was_emitted_for (buffer
) = true;
257 /* Have BUFFER start a new line. */
259 output_add_newline (output_buffer
*buffer
)
261 obstack_1grow (&buffer
->obstack
, '\n');
262 output_text_length (buffer
) = 0;
265 /* Appends a character to BUFFER. */
267 output_add_character (output_buffer
*buffer
, int c
)
269 if (output_is_line_wrapping (buffer
) && output_space_left (buffer
) <= 0)
270 output_add_newline (buffer
);
271 obstack_1grow (&buffer
->obstack
, c
);
272 ++output_text_length (buffer
);
275 /* Adds a space to BUFFER. */
277 output_add_space (output_buffer
*buffer
)
279 if (output_is_line_wrapping (buffer
) && output_space_left (buffer
) <= 0)
281 output_add_newline (buffer
);
284 obstack_1grow (&buffer
->obstack
, ' ');
285 ++output_text_length (buffer
);
288 /* These functions format an INTEGER into BUFFER as suggested by their
291 output_decimal (output_buffer
*buffer
, int i
)
293 output_formatted_scalar (buffer
, "%d", i
);
297 output_long_decimal (output_buffer
*buffer
, long int i
)
299 output_formatted_scalar (buffer
, "%ld", i
);
303 output_unsigned_decimal (output_buffer
*buffer
, unsigned int i
)
305 output_formatted_scalar (buffer
, "%u", i
);
309 output_long_unsigned_decimal (output_buffer
*buffer
, long unsigned int i
)
311 output_formatted_scalar (buffer
, "%lu", i
);
315 output_octal (output_buffer
*buffer
, unsigned int i
)
317 output_formatted_scalar (buffer
, "%o", i
);
321 output_long_octal (output_buffer
*buffer
, long unsigned int i
)
323 output_formatted_scalar (buffer
, "%lo", i
);
327 output_hexadecimal (output_buffer
*buffer
, unsigned int i
)
329 output_formatted_scalar (buffer
, "%x", i
);
333 output_long_hexadecimal (output_buffer
*buffer
, long unsigned int i
)
335 output_formatted_scalar (buffer
, "%lx", i
);
339 output_pointer (output_buffer
*buffer
, void *p
)
341 output_formatted_scalar (buffer
, HOST_PTR_PRINTF
, p
);
344 /* Append to BUFFER a string specified by its STARTING character
347 output_append_r (output_buffer
*buffer
, const char *start
, int length
)
349 obstack_grow (&buffer
->obstack
, start
, length
);
350 output_text_length (buffer
) += length
;
353 /* Append a string deliminated by START and END to BUFFER. No wrapping is
354 done. However, if beginning a new line then emit BUFFER->state.prefix
355 and skip any leading whitespace if appropriate. The caller must ensure
356 that it is safe to do so. */
358 output_append (output_buffer
*buffer
, const char *start
, const char *end
)
360 /* Emit prefix and skip whitespace if we're starting a new line. */
361 if (is_starting_newline (buffer
))
363 output_emit_prefix (buffer
);
364 if (output_is_line_wrapping (buffer
))
365 while (start
!= end
&& *start
== ' ')
368 output_append_r (buffer
, start
, end
- start
);
371 /* Insert enough spaces into BUFFER to bring the column position to
372 the current indentation level, assuming that a newline has just
373 been written to the buffer. */
375 output_indent (output_buffer
*buffer
)
377 int n
= output_indentation (buffer
);
380 for (i
= 0; i
< n
; ++i
)
381 output_add_character (buffer
, ' ');
384 /* Wrap a text delimited by START and END into BUFFER. */
386 wrap_text (output_buffer
*buffer
, const char *start
, const char *end
)
388 bool is_wrapping
= output_is_line_wrapping (buffer
);
392 /* Dump anything bordered by whitespaces. */
394 const char *p
= start
;
395 while (p
!= end
&& *p
!= ' ' && *p
!= '\n')
397 if (is_wrapping
&& p
- start
>= output_space_left (buffer
))
398 output_add_newline (buffer
);
399 output_append (buffer
, start
, p
);
403 if (start
!= end
&& *start
== ' ')
405 output_add_space (buffer
);
408 if (start
!= end
&& *start
== '\n')
410 output_add_newline (buffer
);
416 /* Same as wrap_text but wrap text only when in line-wrapping mode. */
418 maybe_wrap_text (output_buffer
*buffer
, const char *start
, const char *end
)
420 if (output_is_line_wrapping (buffer
))
421 wrap_text (buffer
, start
, end
);
423 output_append (buffer
, start
, end
);
427 /* Append a STRING to BUFFER; the STRING might be line-wrapped if in
430 output_add_string (output_buffer
*buffer
, const char *str
)
432 maybe_wrap_text (buffer
, str
, str
+ (str
? strlen (str
) : 0));
435 /* Append an identifier ID to BUFFER. */
437 output_add_identifier (output_buffer
*buffer
, tree id
)
439 output_append (buffer
, IDENTIFIER_POINTER (id
),
440 IDENTIFIER_POINTER (id
) + IDENTIFIER_LENGTH (id
));
443 /* Flush the content of BUFFER onto the attached stream,
447 output_buffer_to_stream (output_buffer
*buffer
)
449 const char *text
= output_finalize_message (buffer
);
450 fputs (text
, output_buffer_attached_stream (buffer
));
451 output_clear_message_text (buffer
);
454 /* Format a message pointed to by TEXT. The following format specifiers are
455 recognized as being language independent:
456 %d, %i: (signed) integer in base ten.
457 %u: unsigned integer in base ten.
458 %o: unsigned integer in base eight.
459 %x: unsigned integer in base sixteen.
460 %ld, %li, %lo, %lu, %lx: long versions of the above.
464 %m: strerror(text->err_no) - does not consume a value from args_ptr.
466 %*.s: a substring the length of which is specified by an integer.
469 output_format (output_buffer
*buffer
, text_info
*text
)
471 for (; *text
->format_spec
; ++text
->format_spec
)
473 bool long_integer
= 0;
477 const char *p
= text
->format_spec
;
478 while (*p
&& *p
!= '%')
480 wrap_text (buffer
, text
->format_spec
, p
);
481 text
->format_spec
= p
;
484 if (*text
->format_spec
== '\0')
487 /* We got a '%'. Let's see what happens. Record whether we're
488 parsing a long integer format specifier. */
489 if (*++text
->format_spec
== 'l')
495 /* Handle %c, %d, %i, %ld, %li, %lo, %lu, %lx, %m, %o, %s, %u,
496 %x, %p, %.*s; %%. And nothing else. Front-ends should install
497 printers to grok language specific format specifiers. */
498 switch (*text
->format_spec
)
501 output_add_character (buffer
, va_arg (*text
->args_ptr
, int));
507 output_long_decimal (buffer
, va_arg (*text
->args_ptr
, long int));
509 output_decimal (buffer
, va_arg (*text
->args_ptr
, int));
514 output_long_octal (buffer
,
515 va_arg (*text
->args_ptr
, unsigned long int));
517 output_octal (buffer
, va_arg (*text
->args_ptr
, unsigned int));
521 output_add_string (buffer
, va_arg (*text
->args_ptr
, const char *));
525 output_pointer (buffer
, va_arg (*text
->args_ptr
, void *));
530 output_long_unsigned_decimal
531 (buffer
, va_arg (*text
->args_ptr
, long unsigned int));
533 output_unsigned_decimal
534 (buffer
, va_arg (*text
->args_ptr
, unsigned int));
539 output_long_hexadecimal
540 (buffer
, va_arg (*text
->args_ptr
, unsigned long int));
543 (buffer
, va_arg (*text
->args_ptr
, unsigned int));
547 output_add_string (buffer
, xstrerror (text
->err_no
));
551 output_add_character (buffer
, '%');
556 const location_t
*locus
= va_arg (*text
->args_ptr
, location_t
*);
557 output_add_string (buffer
, "file '");
558 output_add_string (buffer
, locus
->file
);
559 output_add_string (buffer
, "', line ");
560 output_decimal (buffer
, locus
->line
);
568 /* We handle no precision specifier but `%.*s'. */
569 if (*++text
->format_spec
!= '*')
571 else if (*++text
->format_spec
!= 's')
573 n
= va_arg (*text
->args_ptr
, int);
574 s
= va_arg (*text
->args_ptr
, const char *);
575 output_append (buffer
, s
, s
+ n
);
580 if (!buffer
->format_decoder
581 || !(*buffer
->format_decoder
) (buffer
, text
))
583 /* Hmmm. The front-end failed to install a format translator
584 but called us with an unrecognized format. Or, maybe, the
585 translated string just contains an invalid format, or
586 has formats in the wrong order. Sorry. */
593 /* Return a malloc'd string containing MSG formatted a la printf. The
594 caller is responsible for freeing the memory. */
596 build_message_string (const char *msg
, ...)
602 vasprintf (&str
, msg
, ap
);
608 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
610 file_name_as_prefix (const char *f
)
612 return build_message_string ("%s: ", f
);
615 /* Format a message into BUFFER a la printf. */
617 output_printf (struct output_buffer
*buffer
, const char *msgid
, ...)
622 va_start (ap
, msgid
);
625 text
.format_spec
= _(msgid
);
626 output_format (buffer
, &text
);
630 /* Print a message relevant to the given DECL. */
632 format_with_decl (output_buffer
*buffer
, text_info
*text
, tree decl
)
636 /* Do magic to get around lack of varargs support for insertion
637 of arguments into existing list. We know that the decl is first;
638 we ass_u_me that it will be printed with "%s". */
639 for (p
= text
->format_spec
; *p
; ++p
)
645 else if (*(p
+ 1) != 's')
652 /* Print the left-hand substring. */
653 maybe_wrap_text (buffer
, text
->format_spec
, p
);
655 if (*p
== '%') /* Print the name. */
657 const char *const n
= (DECL_NAME (decl
)
658 ? (*lang_hooks
.decl_printable_name
) (decl
, 2)
659 : _("((anonymous))"));
660 output_add_string (buffer
, n
);
664 if (ISALPHA (*(p
- 1) & 0xFF))
669 if (*p
) /* Print the rest of the message. */
671 text
->format_spec
= p
;
672 output_format (buffer
, text
);
676 /* Flush the content of BUFFER onto the attached stream. */
678 output_flush (output_buffer
*buffer
)
680 output_buffer_to_stream (buffer
);
681 output_clear_data (buffer
);
682 fputc ('\n', output_buffer_attached_stream (buffer
));
683 fflush (output_buffer_attached_stream (buffer
));
686 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
687 settings needed by BUFFER for a verbatim formatting. */
689 output_do_verbatim (output_buffer
*buffer
, text_info
*text
)
691 diagnostic_prefixing_rule_t rule
= output_prefixing_rule (buffer
);
692 int line_cutoff
= output_line_cutoff (buffer
);
694 /* Set verbatim mode. */
695 output_prefixing_rule (buffer
) = DIAGNOSTICS_SHOW_PREFIX_NEVER
;
696 output_line_cutoff (buffer
) = 0;
697 /* Do the actual formatting. */
698 output_format (buffer
, text
);
699 /* Restore previous settings. */
700 output_prefixing_rule (buffer
) = rule
;
701 output_line_cutoff (buffer
) = line_cutoff
;
704 /* Output MESSAGE verbatim into BUFFER. */
706 output_verbatim (output_buffer
*buffer
, const char *msgid
, ...)
711 va_start (ap
, msgid
);
714 text
.format_spec
= _(msgid
);
715 output_do_verbatim (buffer
, &text
);
720 /* Initialize the diagnostic message outputting machinery. */
722 diagnostic_initialize (diagnostic_context
*context
)
724 memset (context
, 0, sizeof *context
);
725 obstack_init (&context
->buffer
.obstack
);
727 /* By default, diagnostics are sent to stderr. */
728 output_buffer_attached_stream (&context
->buffer
) = stderr
;
730 /* By default, we emit prefixes once per message. */
731 diagnostic_prefixing_rule (context
) = DIAGNOSTICS_SHOW_PREFIX_ONCE
;
733 diagnostic_starter (context
) = default_diagnostic_starter
;
734 diagnostic_finalizer (context
) = default_diagnostic_finalizer
;
735 context
->warnings_are_errors_message
= warnings_are_errors
;
738 /* Returns true if the next format specifier in TEXT is a format specifier
739 for a location_t. If so, update the object pointed by LOCUS to reflect
740 the specified location in *TEXT->args_ptr. */
742 text_specifies_location (text_info
*text
, location_t
*locus
)
745 /* Skip any leading text. */
746 for (p
= text
->format_spec
; *p
&& *p
!= '%'; ++p
)
749 /* Extract the location information if any. */
750 if (*p
== '%' && *++p
== 'H')
752 *locus
= *va_arg (*text
->args_ptr
, location_t
*);
753 text
->format_spec
= p
+ 1;
761 diagnostic_set_info (diagnostic_info
*diagnostic
, const char *msgid
,
762 va_list *args
, const char *file
, int line
,
765 diagnostic
->message
.err_no
= errno
;
766 diagnostic
->message
.args_ptr
= args
;
767 diagnostic
->message
.format_spec
= _(msgid
);
768 /* If the diagnostic message doesn't specify a location,
769 use FILE and LINE. */
770 if (!text_specifies_location (&diagnostic
->message
, &diagnostic
->location
))
772 diagnostic
->location
.file
= file
;
773 diagnostic
->location
.line
= line
;
775 diagnostic
->kind
= kind
;
778 /* Return a malloc'd string describing a location. The caller is
779 responsible for freeing the memory. */
781 diagnostic_build_prefix (diagnostic_info
*diagnostic
)
783 static const char *const diagnostic_kind_text
[] = {
784 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
785 #include "diagnostic.def"
786 #undef DEFINE_DIAGNOSTIC_KIND
789 if (diagnostic
->kind
>= DK_LAST_DIAGNOSTIC_KIND
)
792 return diagnostic
->location
.file
793 ? build_message_string ("%s:%d: %s",
794 diagnostic
->location
.file
,
795 diagnostic
->location
.line
,
796 _(diagnostic_kind_text
[diagnostic
->kind
]))
797 : build_message_string ("%s: %s", progname
,
798 _(diagnostic_kind_text
[diagnostic
->kind
]));
802 diagnostic_flush_buffer (diagnostic_context
*context
)
804 output_buffer_to_stream (&context
->buffer
);
805 fflush (output_buffer_attached_stream (&context
->buffer
));
808 /* Count a diagnostic. Return true if the message should be printed. */
810 diagnostic_count_diagnostic (diagnostic_context
*context
,
811 diagnostic_info
*diagnostic
)
813 diagnostic_t kind
= diagnostic
->kind
;
821 #ifndef ENABLE_CHECKING
822 /* When not checking, ICEs are converted to fatal errors when an
823 error has already occurred. This is counteracted by
825 if ((diagnostic_kind_count (context
, DK_ERROR
) > 0
826 || diagnostic_kind_count (context
, DK_SORRY
) > 0)
827 && !context
->abort_on_error
)
829 fnotice (stderr
, "%s:%d: confused by earlier errors, bailing out\n",
830 diagnostic
->location
.file
, diagnostic
->location
.line
);
831 exit (FATAL_EXIT_CODE
);
834 if (context
->internal_error
)
835 (*context
->internal_error
) (diagnostic
->message
.format_spec
,
836 diagnostic
->message
.args_ptr
);
839 case DK_FATAL
: case DK_SORRY
:
840 case DK_ANACHRONISM
: case DK_NOTE
:
841 ++diagnostic_kind_count (context
, kind
);
845 if (!diagnostic_report_warnings_p ())
848 if (!warnings_are_errors
)
850 ++diagnostic_kind_count (context
, DK_WARNING
);
854 if (context
->warnings_are_errors_message
)
856 output_verbatim (&context
->buffer
,
857 "%s: warnings being treated as errors\n", progname
);
858 context
->warnings_are_errors_message
= false;
861 /* and fall through */
863 ++diagnostic_kind_count (context
, DK_ERROR
);
870 /* Take any action which is expected to happen after the diagnostic
871 is written out. This function does not always return. */
873 diagnostic_action_after_output (diagnostic_context
*context
,
874 diagnostic_info
*diagnostic
)
876 switch (diagnostic
->kind
)
886 if (context
->abort_on_error
)
891 if (context
->abort_on_error
)
894 fnotice (stderr
, bug_report_request
, bug_report_url
);
895 exit (FATAL_EXIT_CODE
);
898 if (context
->abort_on_error
)
901 fnotice (stderr
, "compilation terminated.\n");
902 exit (FATAL_EXIT_CODE
);
909 /* Called when the start of a function definition is parsed,
910 this function prints on stderr the name of the function. */
912 announce_function (tree decl
)
916 if (rtl_dump_and_exit
)
917 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl
)));
919 verbatim (" %s", (*lang_hooks
.decl_printable_name
) (decl
, 2));
921 output_needs_newline (&global_dc
->buffer
) = true;
922 diagnostic_set_last_function (global_dc
);
926 /* The default function to print out name of current function that caused
929 lhd_print_error_function (diagnostic_context
*context
, const char *file
)
931 if (diagnostic_last_function_changed (context
))
933 const char *old_prefix
= output_prefix (&context
->buffer
);
934 char *new_prefix
= file
? build_message_string ("%s: ", file
) : NULL
;
936 output_set_prefix (&context
->buffer
, new_prefix
);
938 if (current_function_decl
== NULL
)
939 output_add_string (&context
->buffer
, _("At top level:"));
942 if (TREE_CODE (TREE_TYPE (current_function_decl
)) == METHOD_TYPE
)
944 (&context
->buffer
, "In member function `%s':",
945 (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2));
948 (&context
->buffer
, "In function `%s':",
949 (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2));
951 output_add_newline (&context
->buffer
);
953 diagnostic_set_last_function (context
);
954 output_buffer_to_stream (&context
->buffer
);
955 context
->buffer
.state
.prefix
= old_prefix
;
956 free ((char*) new_prefix
);
960 /* Prints out, if necessary, the name of the current function
961 that caused an error. Called from all error and warning functions.
962 We ignore the FILE parameter, as it cannot be relied upon. */
965 diagnostic_report_current_function (diagnostic_context
*context
)
967 diagnostic_report_current_module (context
);
968 (*lang_hooks
.print_error_function
) (context
, input_filename
);
972 diagnostic_report_current_module (diagnostic_context
*context
)
974 struct file_stack
*p
;
976 if (output_needs_newline (&context
->buffer
))
978 output_add_newline (&context
->buffer
);
979 output_needs_newline (&context
->buffer
) = false;
982 if (input_file_stack
&& input_file_stack
->next
!= 0
983 && diagnostic_last_module_changed (context
))
985 for (p
= input_file_stack
->next
; p
; p
= p
->next
)
986 if (p
== input_file_stack
->next
)
987 output_verbatim (&context
->buffer
,
988 "In file included from %s:%d",
989 p
->location
.file
, p
->location
.line
);
991 output_verbatim (&context
->buffer
,
993 p
->location
.file
, p
->location
.line
);
994 output_verbatim (&context
->buffer
, ":\n");
995 diagnostic_set_last_module (context
);
1000 default_diagnostic_starter (diagnostic_context
*context
,
1001 diagnostic_info
*diagnostic
)
1003 diagnostic_report_current_function (context
);
1004 output_set_prefix (&context
->buffer
, diagnostic_build_prefix (diagnostic
));
1008 default_diagnostic_finalizer (context
, diagnostic
)
1009 diagnostic_context
*context
;
1010 diagnostic_info
*diagnostic
__attribute__((unused
));
1012 output_destroy_prefix (&context
->buffer
);
1015 /* Report a diagnostic message (an error or a warning) as specified by
1016 DC. This function is *the* subroutine in terms of which front-ends
1017 should implement their specific diagnostic handling modules. The
1018 front-end independent format specifiers are exactly those described
1019 in the documentation of output_format. */
1022 diagnostic_report_diagnostic (diagnostic_context
*context
,
1023 diagnostic_info
*diagnostic
)
1025 if (context
->lock
++)
1026 error_recursion (context
);
1028 if (diagnostic_count_diagnostic (context
, diagnostic
))
1030 (*diagnostic_starter (context
)) (context
, diagnostic
);
1031 output_format (&context
->buffer
, &diagnostic
->message
);
1032 (*diagnostic_finalizer (context
)) (context
, diagnostic
);
1033 output_flush (&context
->buffer
);
1034 diagnostic_action_after_output (context
, diagnostic
);
1040 /* Report a diagnostic MESSAGE at the declaration DECL.
1041 MSG is a format string which uses %s to substitute the declaration
1042 name; subsequent substitutions are a la output_format. */
1044 diagnostic_for_decl (diagnostic_context
*context
,
1045 diagnostic_info
*diagnostic
, tree decl
)
1047 if (context
->lock
++)
1048 error_recursion (context
);
1050 if (diagnostic_count_diagnostic (context
, diagnostic
))
1052 (*diagnostic_starter (context
)) (context
, diagnostic
);
1053 format_with_decl (&context
->buffer
, &diagnostic
->message
, decl
);
1054 (*diagnostic_finalizer (context
)) (context
, diagnostic
);
1055 output_flush (&context
->buffer
);
1056 diagnostic_action_after_output (context
, diagnostic
);
1062 /* Given a partial pathname as input, return another pathname that
1063 shares no directory elements with the pathname of __FILE__. This
1064 is used by fancy_abort() to print `Internal compiler error in expr.c'
1065 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1068 trim_filename (const char *name
)
1070 static const char this_file
[] = __FILE__
;
1071 const char *p
= name
, *q
= this_file
;
1073 /* First skip any "../" in each filename. This allows us to give a proper
1074 reference to a file in a subdirectory. */
1075 while (p
[0] == '.' && p
[1] == '.'
1076 && (p
[2] == DIR_SEPARATOR
1077 #ifdef DIR_SEPARATOR_2
1078 || p
[2] == DIR_SEPARATOR_2
1083 while (q
[0] == '.' && q
[1] == '.'
1084 && (q
[2] == DIR_SEPARATOR
1085 #ifdef DIR_SEPARATOR_2
1086 || p
[2] == DIR_SEPARATOR_2
1091 /* Now skip any parts the two filenames have in common. */
1092 while (*p
== *q
&& *p
!= 0 && *q
!= 0)
1095 /* Now go backwards until the previous directory separator. */
1096 while (p
> name
&& p
[-1] != DIR_SEPARATOR
1097 #ifdef DIR_SEPARATOR_2
1098 && p
[-1] != DIR_SEPARATOR_2
1106 /* Standard error reporting routines in increasing order of severity.
1107 All of these take arguments like printf. */
1109 /* Text to be emitted verbatim to the error message stream; this
1110 produces no prefix and disables line-wrapping. Use rarely. */
1112 verbatim (const char *msgid
, ...)
1117 va_start (ap
, msgid
);
1118 text
.err_no
= errno
;
1119 text
.args_ptr
= &ap
;
1120 text
.format_spec
= _(msgid
);
1121 output_do_verbatim (&global_dc
->buffer
, &text
);
1122 output_buffer_to_stream (&global_dc
->buffer
);
1126 /* An informative note. Use this for additional details on an error
1129 inform (const char *msgid
, ...)
1131 diagnostic_info diagnostic
;
1134 va_start (ap
, msgid
);
1135 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, input_line
,
1137 report_diagnostic (&diagnostic
);
1141 /* A warning. Use this for code which is correct according to the
1142 relevant language specification but is likely to be buggy anyway. */
1144 warning (const char *msgid
, ...)
1146 diagnostic_info diagnostic
;
1149 va_start (ap
, msgid
);
1150 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, input_line
,
1152 report_diagnostic (&diagnostic
);
1156 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
1157 given on the command line, in which case it issues an error. Use
1158 this for diagnostics required by the relevant language standard,
1159 if you have chosen not to make them errors.
1161 Note that these diagnostics are issued independent of the setting
1162 of the -pedantic command-line switch. To get a warning enabled
1163 only with that switch, write "if (pedantic) pedwarn (...);" */
1165 pedwarn (const char *msgid
, ...)
1167 diagnostic_info diagnostic
;
1170 va_start (ap
, msgid
);
1171 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, input_line
,
1172 pedantic_error_kind ());
1173 report_diagnostic (&diagnostic
);
1177 /* A hard error: the code is definitely ill-formed, and an object file
1178 will not be produced. */
1180 error (const char *msgid
, ...)
1182 diagnostic_info diagnostic
;
1185 va_start (ap
, msgid
);
1186 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, input_line
,
1188 report_diagnostic (&diagnostic
);
1192 /* "Sorry, not implemented." Use for a language feature which is
1193 required by the relevant specification but not implemented by GCC.
1194 An object file will not be produced. */
1196 sorry (const char *msgid
, ...)
1198 diagnostic_info diagnostic
;
1201 va_start (ap
, msgid
);
1202 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, input_line
,
1204 report_diagnostic (&diagnostic
);
1208 /* An error which is severe enough that we make no attempt to
1209 continue. Do not use this for internal consistency checks; that's
1210 internal_error. Use of this function should be rare. */
1212 fatal_error (const char *msgid
, ...)
1214 diagnostic_info diagnostic
;
1217 va_start (ap
, msgid
);
1218 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, input_line
,
1220 report_diagnostic (&diagnostic
);
1227 /* An internal consistency check has failed. We make no attempt to
1228 continue. Note that unless there is debugging value to be had from
1229 a more specific message, or some other good reason, you should use
1230 abort () instead of calling this function directly. */
1232 internal_error (const char *msgid
, ...)
1234 diagnostic_info diagnostic
;
1237 va_start (ap
, msgid
);
1238 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, input_line
,
1240 report_diagnostic (&diagnostic
);
1247 /* Variants of some of the above, which make reference to a particular
1248 DECL node. These are deprecated. */
1251 warning_with_decl (tree decl
, const char *msgid
, ...)
1253 diagnostic_info diagnostic
;
1256 va_start (ap
, msgid
);
1258 /* Do not issue a warning about a decl which came from a system header,
1259 unless -Wsystem-headers. */
1260 if (DECL_IN_SYSTEM_HEADER (decl
) && !warn_system_headers
)
1263 diagnostic_set_info (&diagnostic
, msgid
, &ap
,
1264 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
),
1266 diagnostic_for_decl (global_dc
, &diagnostic
, decl
);
1271 pedwarn_with_decl (tree decl
, const char *msgid
, ...)
1273 diagnostic_info diagnostic
;
1276 va_start (ap
, msgid
);
1278 /* Do not issue a warning about a decl which came from a system header,
1279 unless -Wsystem-headers. */
1280 if (DECL_IN_SYSTEM_HEADER (decl
) && !warn_system_headers
)
1283 diagnostic_set_info (&diagnostic
, msgid
, &ap
,
1284 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
),
1285 pedantic_error_kind ());
1286 diagnostic_for_decl (global_dc
, &diagnostic
, decl
);
1292 error_with_decl (tree decl
, const char *msgid
, ...)
1294 diagnostic_info diagnostic
;
1297 va_start (ap
, msgid
);
1298 diagnostic_set_info (&diagnostic
, msgid
, &ap
,
1299 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
),
1301 diagnostic_for_decl (global_dc
, &diagnostic
, decl
);
1305 /* Special case error functions. Most are implemented in terms of the
1306 above, or should be. */
1308 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1309 runs its second argument through gettext. */
1311 fnotice (FILE *file
, const char *msgid
, ...)
1315 va_start (ap
, msgid
);
1316 vfprintf (file
, _(msgid
), ap
);
1320 /* Warn about a use of an identifier which was marked deprecated. */
1322 warn_deprecated_use (tree node
)
1324 if (node
== 0 || !warn_deprecated_decl
)
1328 warning ("`%s' is deprecated (declared at %s:%d)",
1329 IDENTIFIER_POINTER (DECL_NAME (node
)),
1330 DECL_SOURCE_FILE (node
), DECL_SOURCE_LINE (node
));
1331 else if (TYPE_P (node
))
1333 const char *what
= NULL
;
1334 tree decl
= TYPE_STUB_DECL (node
);
1336 if (TREE_CODE (TYPE_NAME (node
)) == IDENTIFIER_NODE
)
1337 what
= IDENTIFIER_POINTER (TYPE_NAME (node
));
1338 else if (TREE_CODE (TYPE_NAME (node
)) == TYPE_DECL
1339 && DECL_NAME (TYPE_NAME (node
)))
1340 what
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node
)));
1345 warning ("`%s' is deprecated (declared at %s:%d)", what
,
1346 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
));
1348 warning ("`%s' is deprecated", what
);
1351 warning ("type is deprecated (declared at %s:%d)",
1352 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
));
1354 warning ("type is deprecated");
1358 /* Inform the user that an error occurred while trying to report some
1359 other error. This indicates catastrophic internal inconsistencies,
1360 so give up now. But do try to flush out the previous error.
1361 This mustn't use internal_error, that will cause infinite recursion. */
1364 error_recursion (diagnostic_context
*context
)
1366 if (context
->lock
< 3)
1367 output_flush (&context
->buffer
);
1370 "Internal compiler error: Error reporting routines re-entered.\n");
1371 fnotice (stderr
, bug_report_request
, bug_report_url
);
1372 exit (FATAL_EXIT_CODE
);
1375 /* Report an internal compiler error in a friendly manner. This is
1376 the function that gets called upon use of abort() in the source
1377 code generally, thanks to a special macro. */
1380 fancy_abort (const char *file
, int line
, const char *function
)
1382 internal_error ("in %s, at %s:%d", function
, trim_filename (file
), line
);
1385 /* Really call the system 'abort'. This has to go right at the end of
1386 this file, so that there are no functions after it that call abort
1387 and get the system abort instead of our macro. */