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_info
*, tree
));
58 static void set_real_maximum_length
PARAMS ((output_buffer
*));
60 static void output_unsigned_decimal
PARAMS ((output_buffer
*, unsigned int));
61 static void output_long_decimal
PARAMS ((output_buffer
*, long int));
62 static void output_long_unsigned_decimal
PARAMS ((output_buffer
*,
64 static void output_octal
PARAMS ((output_buffer
*, unsigned int));
65 static void output_long_octal
PARAMS ((output_buffer
*, unsigned long int));
66 static void output_hexadecimal
PARAMS ((output_buffer
*, unsigned int));
67 static void output_long_hexadecimal
PARAMS ((output_buffer
*,
69 static void output_append_r
PARAMS ((output_buffer
*, const char *, int));
70 static void wrap_text
PARAMS ((output_buffer
*, const char *, const char *));
71 static void maybe_wrap_text
PARAMS ((output_buffer
*, const char *,
73 static void output_clear_data
PARAMS ((output_buffer
*));
75 static void default_diagnostic_starter
PARAMS ((diagnostic_context
*,
77 static void default_diagnostic_finalizer
PARAMS ((diagnostic_context
*,
80 static void error_recursion
PARAMS ((diagnostic_context
*)) ATTRIBUTE_NORETURN
;
81 static bool text_specifies_location
PARAMS ((text_info
*, location_t
*));
82 static void real_abort
PARAMS ((void)) ATTRIBUTE_NORETURN
;
84 extern int rtl_dump_and_exit
;
85 extern int warnings_are_errors
;
87 /* A diagnostic_context surrogate for stderr. */
88 static diagnostic_context global_diagnostic_context
;
89 diagnostic_context
*global_dc
= &global_diagnostic_context
;
92 /* Subroutine of output_set_maximum_length. Set up BUFFER's
93 internal maximum characters per line. */
95 set_real_maximum_length (buffer
)
96 output_buffer
*buffer
;
98 /* If we're told not to wrap lines then do the obvious thing. In case
99 we'll emit prefix only once per diagnostic message, it is appropriate
100 not to increase unnecessarily the line-length cut-off. */
101 if (!output_is_line_wrapping (buffer
)
102 || output_prefixing_rule (buffer
) == DIAGNOSTICS_SHOW_PREFIX_ONCE
103 || output_prefixing_rule (buffer
) == DIAGNOSTICS_SHOW_PREFIX_NEVER
)
104 line_wrap_cutoff (buffer
) = output_line_cutoff (buffer
);
107 int prefix_length
= buffer
->state
.prefix
?
108 strlen (buffer
->state
.prefix
) : 0;
109 /* If the prefix is ridiculously too long, output at least
111 if (output_line_cutoff (buffer
) - prefix_length
< 32)
112 line_wrap_cutoff (buffer
) = output_line_cutoff (buffer
) + 32;
114 line_wrap_cutoff (buffer
) = output_line_cutoff (buffer
);
118 /* Sets the number of maximum characters per line BUFFER can output
119 in line-wrapping mode. A LENGTH value 0 suppresses line-wrapping. */
121 output_set_maximum_length (buffer
, length
)
122 output_buffer
*buffer
;
125 output_line_cutoff (buffer
) = length
;
126 set_real_maximum_length (buffer
);
129 /* Sets BUFFER's PREFIX. */
131 output_set_prefix (buffer
, prefix
)
132 output_buffer
*buffer
;
135 buffer
->state
.prefix
= prefix
;
136 set_real_maximum_length (buffer
);
137 prefix_was_emitted_for (buffer
) = false;
138 output_indentation (buffer
) = 0;
141 /* Return a pointer to the last character emitted in the output
142 BUFFER area. A NULL pointer means no character available. */
144 output_last_position (buffer
)
145 const output_buffer
*buffer
;
147 const char *p
= NULL
;
149 if (obstack_base (&buffer
->obstack
) != obstack_next_free (&buffer
->obstack
))
150 p
= ((const char *) obstack_next_free (&buffer
->obstack
)) - 1;
154 /* Free BUFFER's prefix, a previously malloc'd string. */
156 output_destroy_prefix (buffer
)
157 output_buffer
*buffer
;
159 if (buffer
->state
.prefix
!= NULL
)
161 free ((char *) buffer
->state
.prefix
);
162 buffer
->state
.prefix
= NULL
;
166 /* Zero out any text output so far in BUFFER. */
168 output_clear_message_text (buffer
)
169 output_buffer
*buffer
;
171 obstack_free (&buffer
->obstack
, obstack_base (&buffer
->obstack
));
172 output_text_length (buffer
) = 0;
175 /* Zero out any formatting data used so far by BUFFER. */
177 output_clear_data (buffer
)
178 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 (buffer
, prefix
, maximum_length
)
188 output_buffer
*buffer
;
192 memset (buffer
, 0, sizeof (output_buffer
));
193 obstack_init (&buffer
->obstack
);
194 output_buffer_attached_stream (buffer
) = stderr
;
195 output_line_cutoff (buffer
) = maximum_length
;
196 output_prefixing_rule (buffer
) = diagnostic_prefixing_rule (global_dc
);
197 output_set_prefix (buffer
, prefix
);
198 output_text_length (buffer
) = 0;
199 output_clear_data (buffer
);
202 /* Reinitialize BUFFER. */
204 output_clear (buffer
)
205 output_buffer
*buffer
;
207 output_clear_message_text (buffer
);
208 output_clear_data (buffer
);
211 /* Finishes constructing a NULL-terminated character string representing
212 the BUFFERed message. */
214 output_finalize_message (buffer
)
215 output_buffer
*buffer
;
217 obstack_1grow (&buffer
->obstack
, '\0');
218 return output_message_text (buffer
);
221 /* Return the amount of characters BUFFER can accept to
224 output_space_left (buffer
)
225 const output_buffer
*buffer
;
227 return line_wrap_cutoff (buffer
) - output_text_length (buffer
);
230 /* Write out BUFFER's prefix. */
232 output_emit_prefix (buffer
)
233 output_buffer
*buffer
;
235 if (buffer
->state
.prefix
!= NULL
)
237 switch (output_prefixing_rule (buffer
))
240 case DIAGNOSTICS_SHOW_PREFIX_NEVER
:
243 case DIAGNOSTICS_SHOW_PREFIX_ONCE
:
244 if (prefix_was_emitted_for (buffer
))
246 output_indent (buffer
);
249 output_indentation (buffer
) += 3;
252 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE
:
254 int prefix_length
= strlen (buffer
->state
.prefix
);
255 output_append_r (buffer
, buffer
->state
.prefix
, prefix_length
);
256 prefix_was_emitted_for (buffer
) = true;
263 /* Have BUFFER start a new line. */
265 output_add_newline (buffer
)
266 output_buffer
*buffer
;
268 obstack_1grow (&buffer
->obstack
, '\n');
269 output_text_length (buffer
) = 0;
272 /* Appends a character to BUFFER. */
274 output_add_character (buffer
, c
)
275 output_buffer
*buffer
;
278 if (output_is_line_wrapping (buffer
) && output_space_left (buffer
) <= 0)
279 output_add_newline (buffer
);
280 obstack_1grow (&buffer
->obstack
, c
);
281 ++output_text_length (buffer
);
284 /* Adds a space to BUFFER. */
286 output_add_space (buffer
)
287 output_buffer
*buffer
;
289 if (output_is_line_wrapping (buffer
) && output_space_left (buffer
) <= 0)
291 output_add_newline (buffer
);
294 obstack_1grow (&buffer
->obstack
, ' ');
295 ++output_text_length (buffer
);
298 /* These functions format an INTEGER into BUFFER as suggested by their
301 output_decimal (buffer
, i
)
302 output_buffer
*buffer
;
305 output_formatted_scalar (buffer
, "%d", i
);
309 output_long_decimal (output_buffer
*buffer
, long int i
)
311 output_formatted_scalar (buffer
, "%ld", i
);
315 output_unsigned_decimal (output_buffer
*buffer
, unsigned int i
)
317 output_formatted_scalar (buffer
, "%u", i
);
321 output_long_unsigned_decimal (output_buffer
*buffer
, long unsigned int i
)
323 output_formatted_scalar (buffer
, "%lu", i
);
327 output_octal (output_buffer
*buffer
, unsigned int i
)
329 output_formatted_scalar (buffer
, "%o", i
);
333 output_long_octal (output_buffer
*buffer
, long unsigned int i
)
335 output_formatted_scalar (buffer
, "%lo", i
);
339 output_hexadecimal (output_buffer
*buffer
, unsigned int i
)
341 output_formatted_scalar (buffer
, "%x", i
);
345 output_long_hexadecimal (output_buffer
*buffer
, long unsigned int i
)
347 output_formatted_scalar (buffer
, "%lx", i
);
351 output_pointer (output_buffer
*buffer
, void *p
)
353 output_formatted_scalar (buffer
, HOST_PTR_PRINTF
, p
);
356 /* Append to BUFFER a string specified by its STARTING character
359 output_append_r (buffer
, start
, length
)
360 output_buffer
*buffer
;
364 obstack_grow (&buffer
->obstack
, start
, length
);
365 output_text_length (buffer
) += length
;
368 /* Append a string deliminated by START and END to BUFFER. No wrapping is
369 done. However, if beginning a new line then emit BUFFER->state.prefix
370 and skip any leading whitespace if appropriate. The caller must ensure
371 that it is safe to do so. */
373 output_append (buffer
, start
, end
)
374 output_buffer
*buffer
;
378 /* Emit prefix and skip whitespace if we're starting a new line. */
379 if (is_starting_newline (buffer
))
381 output_emit_prefix (buffer
);
382 if (output_is_line_wrapping (buffer
))
383 while (start
!= end
&& *start
== ' ')
386 output_append_r (buffer
, start
, end
- start
);
389 /* Insert enough spaces into BUFFER to bring the column position to
390 the current indentation level, assuming that a newline has just
391 been written to the buffer. */
393 output_indent (buffer
)
394 output_buffer
*buffer
;
396 int n
= output_indentation (buffer
);
399 for (i
= 0; i
< n
; ++i
)
400 output_add_character (buffer
, ' ');
403 /* Wrap a text delimited by START and END into BUFFER. */
405 wrap_text (buffer
, start
, end
)
406 output_buffer
*buffer
;
410 bool is_wrapping
= output_is_line_wrapping (buffer
);
414 /* Dump anything bordered by whitespaces. */
416 const char *p
= start
;
417 while (p
!= end
&& *p
!= ' ' && *p
!= '\n')
419 if (is_wrapping
&& p
- start
>= output_space_left (buffer
))
420 output_add_newline (buffer
);
421 output_append (buffer
, start
, p
);
425 if (start
!= end
&& *start
== ' ')
427 output_add_space (buffer
);
430 if (start
!= end
&& *start
== '\n')
432 output_add_newline (buffer
);
438 /* Same as wrap_text but wrap text only when in line-wrapping mode. */
440 maybe_wrap_text (buffer
, start
, end
)
441 output_buffer
*buffer
;
445 if (output_is_line_wrapping (buffer
))
446 wrap_text (buffer
, start
, end
);
448 output_append (buffer
, start
, end
);
452 /* Append a STRING to BUFFER; the STRING might be line-wrapped if in
455 output_add_string (buffer
, str
)
456 output_buffer
*buffer
;
459 maybe_wrap_text (buffer
, str
, str
+ (str
? strlen (str
) : 0));
462 /* Append an identifier ID to BUFFER. */
464 output_add_identifier (buffer
, id
)
465 output_buffer
*buffer
;
468 output_append (buffer
, IDENTIFIER_POINTER (id
),
469 IDENTIFIER_POINTER (id
) + IDENTIFIER_LENGTH (id
));
472 /* Flush the content of BUFFER onto the attached stream,
476 output_buffer_to_stream (buffer
)
477 output_buffer
*buffer
;
479 const char *text
= output_finalize_message (buffer
);
480 fputs (text
, output_buffer_attached_stream (buffer
));
481 output_clear_message_text (buffer
);
484 /* Format a message pointed to by TEXT. The following format specifiers are
485 recognized as being language independent:
486 %d, %i: (signed) integer in base ten.
487 %u: unsigned integer in base ten.
488 %o: unsigned integer in base eight.
489 %x: unsigned integer in base sixteen.
490 %ld, %li, %lo, %lu, %lx: long versions of the above.
495 %*.s: a substring the length of which is specified by an integer.
498 output_format (buffer
, text
)
499 output_buffer
*buffer
;
502 for (; *text
->format_spec
; ++text
->format_spec
)
504 bool long_integer
= 0;
508 const char *p
= text
->format_spec
;
509 while (*p
&& *p
!= '%')
511 wrap_text (buffer
, text
->format_spec
, p
);
512 text
->format_spec
= p
;
515 if (*text
->format_spec
== '\0')
518 /* We got a '%'. Let's see what happens. Record whether we're
519 parsing a long integer format specifier. */
520 if (*++text
->format_spec
== 'l')
526 /* Handle %c, %d, %i, %ld, %li, %lo, %lu, %lx, %o, %s, %u,
527 %x, %p, %.*s; %%. And nothing else. Front-ends should install
528 printers to grok language specific format specifiers. */
529 switch (*text
->format_spec
)
532 output_add_character (buffer
, va_arg (*text
->args_ptr
, int));
538 output_long_decimal (buffer
, va_arg (*text
->args_ptr
, long int));
540 output_decimal (buffer
, va_arg (*text
->args_ptr
, int));
545 output_long_octal (buffer
,
546 va_arg (*text
->args_ptr
, unsigned long int));
548 output_octal (buffer
, va_arg (*text
->args_ptr
, unsigned int));
552 output_add_string (buffer
, va_arg (*text
->args_ptr
, const char *));
556 output_pointer (buffer
, va_arg (*text
->args_ptr
, void *));
561 output_long_unsigned_decimal
562 (buffer
, va_arg (*text
->args_ptr
, long unsigned int));
564 output_unsigned_decimal
565 (buffer
, va_arg (*text
->args_ptr
, unsigned int));
570 output_long_hexadecimal
571 (buffer
, va_arg (*text
->args_ptr
, unsigned long int));
574 (buffer
, va_arg (*text
->args_ptr
, unsigned int));
578 output_add_character (buffer
, '%');
583 const location_t
*locus
= va_arg (*text
->args_ptr
, location_t
*);
584 output_add_string (buffer
, "file '");
585 output_add_string (buffer
, locus
->file
);
586 output_add_string (buffer
, "', line ");
587 output_decimal (buffer
, locus
->line
);
595 /* We handle no precision specifier but `%.*s'. */
596 if (*++text
->format_spec
!= '*')
598 else if (*++text
->format_spec
!= 's')
600 n
= va_arg (*text
->args_ptr
, int);
601 s
= va_arg (*text
->args_ptr
, const char *);
602 output_append (buffer
, s
, s
+ n
);
607 if (!buffer
->format_decoder
608 || !(*buffer
->format_decoder
) (buffer
, text
))
610 /* Hmmm. The front-end failed to install a format translator
611 but called us with an unrecognized format. Or, maybe, the
612 translated string just contains an invalid format, or
613 has formats in the wrong order. Sorry. */
620 /* Return a malloc'd string containing MSG formatted a la printf. The
621 caller is responsible for freeing the memory. */
623 build_message_string
VPARAMS ((const char *msg
, ...))
628 VA_FIXEDARG (ap
, const char *, msg
);
630 vasprintf (&str
, msg
, ap
);
637 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
639 file_name_as_prefix (f
)
642 return build_message_string ("%s: ", f
);
645 /* Format a message into BUFFER a la printf. */
647 output_printf
VPARAMS ((struct output_buffer
*buffer
, const char *msgid
, ...))
651 VA_FIXEDARG (ap
, output_buffer
*, buffer
);
652 VA_FIXEDARG (ap
, const char *, msgid
);
655 text
.format_spec
= _(msgid
);
656 output_format (buffer
, &text
);
660 /* Print a message relevant to the given DECL. */
662 format_with_decl (buffer
, text
, decl
)
663 output_buffer
*buffer
;
669 /* Do magic to get around lack of varargs support for insertion
670 of arguments into existing list. We know that the decl is first;
671 we ass_u_me that it will be printed with "%s". */
672 for (p
= text
->format_spec
; *p
; ++p
)
678 else if (*(p
+ 1) != 's')
685 /* Print the left-hand substring. */
686 maybe_wrap_text (buffer
, text
->format_spec
, p
);
688 if (*p
== '%') /* Print the name. */
690 const char *const n
= (DECL_NAME (decl
)
691 ? (*lang_hooks
.decl_printable_name
) (decl
, 2)
692 : _("((anonymous))"));
693 output_add_string (buffer
, n
);
697 if (ISALPHA (*(p
- 1) & 0xFF))
702 if (*p
) /* Print the rest of the message. */
704 text
->format_spec
= p
;
705 output_format (buffer
, text
);
709 /* Flush the content of BUFFER onto the attached stream. */
711 output_flush (buffer
)
712 output_buffer
*buffer
;
714 output_buffer_to_stream (buffer
);
715 output_clear_data (buffer
);
716 fputc ('\n', output_buffer_attached_stream (buffer
));
717 fflush (output_buffer_attached_stream (buffer
));
720 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
721 settings needed by BUFFER for a verbatim formatting. */
723 output_do_verbatim (buffer
, text
)
724 output_buffer
*buffer
;
727 diagnostic_prefixing_rule_t rule
= output_prefixing_rule (buffer
);
728 int line_cutoff
= output_line_cutoff (buffer
);
730 /* Set verbatim mode. */
731 output_prefixing_rule (buffer
) = DIAGNOSTICS_SHOW_PREFIX_NEVER
;
732 output_line_cutoff (buffer
) = 0;
733 /* Do the actual formatting. */
734 output_format (buffer
, text
);
735 /* Restore previous settings. */
736 output_prefixing_rule (buffer
) = rule
;
737 output_line_cutoff (buffer
) = line_cutoff
;
740 /* Output MESSAGE verbatim into BUFFER. */
742 output_verbatim
VPARAMS ((output_buffer
*buffer
, const char *msgid
, ...))
746 VA_FIXEDARG (ap
, output_buffer
*, buffer
);
747 VA_FIXEDARG (ap
, const char *, msgid
);
749 text
.format_spec
= msgid
;
751 output_do_verbatim (buffer
, &text
);
756 /* Initialize the diagnostic message outputting machinery. */
758 diagnostic_initialize (context
)
759 diagnostic_context
*context
;
761 memset (context
, 0, sizeof *context
);
762 obstack_init (&context
->buffer
.obstack
);
764 /* By default, diagnostics are sent to stderr. */
765 output_buffer_attached_stream (&context
->buffer
) = stderr
;
767 /* By default, we emit prefixes once per message. */
768 diagnostic_prefixing_rule (context
) = DIAGNOSTICS_SHOW_PREFIX_ONCE
;
770 diagnostic_starter (context
) = default_diagnostic_starter
;
771 diagnostic_finalizer (context
) = default_diagnostic_finalizer
;
772 context
->warnings_are_errors_message
= warnings_are_errors
;
775 /* Returns true if the next format specifier in TEXT is a format specifier
776 for a location_t. If so, update the object pointed by LOCUS to reflect
777 the specified location in *TEXT->args_ptr. */
779 text_specifies_location (text
, locus
)
784 /* Skip any leading text. */
785 for (p
= text
->format_spec
; *p
&& *p
!= '%'; ++p
)
788 /* Extract the location information if any. */
789 if (*p
== '%' && *++p
== 'H')
791 *locus
= *va_arg (*text
->args_ptr
, location_t
*);
792 text
->format_spec
= p
+ 1;
800 diagnostic_set_info (diagnostic
, msgid
, args
, file
, line
, kind
)
801 diagnostic_info
*diagnostic
;
808 diagnostic
->message
.format_spec
= msgid
;
809 diagnostic
->message
.args_ptr
= args
;
810 /* If the diagnostic message doesn't specify a location,
811 use FILE and LINE. */
812 if (!text_specifies_location (&diagnostic
->message
, &diagnostic
->location
))
814 diagnostic
->location
.file
= file
;
815 diagnostic
->location
.line
= line
;
817 diagnostic
->kind
= kind
;
820 /* Return a malloc'd string describing a location. The caller is
821 responsible for freeing the memory. */
823 diagnostic_build_prefix (diagnostic
)
824 diagnostic_info
*diagnostic
;
826 static const char *const diagnostic_kind_text
[] = {
827 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
828 #include "diagnostic.def"
829 #undef DEFINE_DIAGNOSTIC_KIND
832 if (diagnostic
->kind
>= DK_LAST_DIAGNOSTIC_KIND
)
835 return diagnostic
->location
.file
836 ? build_message_string ("%s:%d: %s",
837 diagnostic
->location
.file
,
838 diagnostic
->location
.line
,
839 _(diagnostic_kind_text
[diagnostic
->kind
]))
840 : build_message_string ("%s: %s", progname
,
841 _(diagnostic_kind_text
[diagnostic
->kind
]));
845 diagnostic_flush_buffer (context
)
846 diagnostic_context
*context
;
848 output_buffer_to_stream (&context
->buffer
);
849 fflush (output_buffer_attached_stream (&context
->buffer
));
852 /* Count a diagnostic. Return true if the message should be printed. */
854 diagnostic_count_diagnostic (context
, kind
)
855 diagnostic_context
*context
;
864 case DK_FATAL
: case DK_ICE
: case DK_SORRY
:
865 case DK_ANACHRONISM
: case DK_NOTE
:
866 ++diagnostic_kind_count (context
, kind
);
870 if (!diagnostic_report_warnings_p ())
872 else if (!warnings_are_errors
)
874 ++diagnostic_kind_count (context
, DK_WARNING
);
877 /* else fall through. */
880 if (kind
== DK_WARNING
&& context
->warnings_are_errors_message
)
882 output_verbatim (&context
->buffer
,
883 "%s: warnings being treated as errors\n", progname
);
884 context
->warnings_are_errors_message
= false;
886 ++diagnostic_kind_count (context
, DK_ERROR
);
893 /* Called when the start of a function definition is parsed,
894 this function prints on stderr the name of the function. */
896 announce_function (decl
)
901 if (rtl_dump_and_exit
)
902 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl
)));
904 verbatim (" %s", (*lang_hooks
.decl_printable_name
) (decl
, 2));
906 output_needs_newline (&global_dc
->buffer
) = true;
907 diagnostic_set_last_function (global_dc
);
911 /* The default function to print out name of current function that caused
914 lhd_print_error_function (context
, file
)
915 diagnostic_context
*context
;
918 if (diagnostic_last_function_changed (context
))
920 const char *old_prefix
= output_prefix (&context
->buffer
);
921 char *new_prefix
= file
? build_message_string ("%s: ", file
) : NULL
;
923 output_set_prefix (&context
->buffer
, new_prefix
);
925 if (current_function_decl
== NULL
)
926 output_add_string (&context
->buffer
, _("At top level:"));
929 if (TREE_CODE (TREE_TYPE (current_function_decl
)) == METHOD_TYPE
)
931 (&context
->buffer
, "In member function `%s':",
932 (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2));
935 (&context
->buffer
, "In function `%s':",
936 (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2));
938 output_add_newline (&context
->buffer
);
940 diagnostic_set_last_function (context
);
941 output_buffer_to_stream (&context
->buffer
);
942 context
->buffer
.state
.prefix
= old_prefix
;
943 free ((char*) new_prefix
);
947 /* Prints out, if necessary, the name of the current function
948 that caused an error. Called from all error and warning functions.
949 We ignore the FILE parameter, as it cannot be relied upon. */
952 diagnostic_report_current_function (context
)
953 diagnostic_context
*context
;
955 diagnostic_report_current_module (context
);
956 (*lang_hooks
.print_error_function
) (context
, input_filename
);
960 diagnostic_report_current_module (context
)
961 diagnostic_context
*context
;
963 struct file_stack
*p
;
965 if (output_needs_newline (&context
->buffer
))
967 output_add_newline (&context
->buffer
);
968 output_needs_newline (&context
->buffer
) = false;
971 if (input_file_stack
&& input_file_stack
->next
!= 0
972 && diagnostic_last_module_changed (context
))
974 for (p
= input_file_stack
->next
; p
; p
= p
->next
)
975 if (p
== input_file_stack
->next
)
976 output_verbatim (&context
->buffer
,
977 "In file included from %s:%d",
978 p
->location
.file
, p
->location
.line
);
980 output_verbatim (&context
->buffer
,
982 p
->location
.file
, p
->location
.line
);
983 output_verbatim (&context
->buffer
, ":\n");
984 diagnostic_set_last_module (context
);
989 default_diagnostic_starter (context
, diagnostic
)
990 diagnostic_context
*context
;
991 diagnostic_info
*diagnostic
;
993 diagnostic_report_current_function (context
);
994 output_set_prefix (&context
->buffer
, diagnostic_build_prefix (diagnostic
));
998 default_diagnostic_finalizer (context
, diagnostic
)
999 diagnostic_context
*context
;
1000 diagnostic_info
*diagnostic
__attribute__((unused
));
1002 output_destroy_prefix (&context
->buffer
);
1005 /* Report a diagnostic message (an error or a warning) as specified by
1006 DC. This function is *the* subroutine in terms of which front-ends
1007 should implement their specific diagnostic handling modules. The
1008 front-end independent format specifiers are exactly those described
1009 in the documentation of output_format. */
1012 diagnostic_report_diagnostic (context
, diagnostic
)
1013 diagnostic_context
*context
;
1014 diagnostic_info
*diagnostic
;
1016 if (context
->lock
++)
1017 error_recursion (context
);
1019 if (diagnostic_count_diagnostic (context
, diagnostic
->kind
))
1021 (*diagnostic_starter (context
)) (context
, diagnostic
);
1022 output_format (&context
->buffer
, &diagnostic
->message
);
1023 (*diagnostic_finalizer (context
)) (context
, diagnostic
);
1024 output_flush (&context
->buffer
);
1027 if (context
->abort_on_error
&& diagnostic
->kind
<= DK_ERROR
)
1032 /* Report a diagnostic MESSAGE at the declaration DECL.
1033 MSG is a format string which uses %s to substitute the declaration
1034 name; subsequent substitutions are a la output_format. */
1036 diagnostic_for_decl (diagnostic
, decl
)
1037 diagnostic_info
*diagnostic
;
1040 if (global_dc
->lock
++)
1041 error_recursion (global_dc
);
1043 if (diagnostic_count_diagnostic (global_dc
, diagnostic
->kind
))
1045 diagnostic_report_current_function (global_dc
);
1047 (&global_dc
->buffer
, diagnostic_build_prefix (diagnostic
));
1048 format_with_decl (&global_dc
->buffer
, &diagnostic
->message
, decl
);
1049 output_flush (&global_dc
->buffer
);
1050 output_destroy_prefix (&global_dc
->buffer
);
1055 /* Given a partial pathname as input, return another pathname that
1056 shares no directory elements with the pathname of __FILE__. This
1057 is used by fancy_abort() to print `Internal compiler error in expr.c'
1058 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1061 trim_filename (name
)
1064 static const char this_file
[] = __FILE__
;
1065 const char *p
= name
, *q
= this_file
;
1067 /* First skip any "../" in each filename. This allows us to give a proper
1068 reference to a file in a subdirectory. */
1069 while (p
[0] == '.' && p
[1] == '.'
1070 && (p
[2] == DIR_SEPARATOR
1071 #ifdef DIR_SEPARATOR_2
1072 || p
[2] == DIR_SEPARATOR_2
1077 while (q
[0] == '.' && q
[1] == '.'
1078 && (q
[2] == DIR_SEPARATOR
1079 #ifdef DIR_SEPARATOR_2
1080 || p
[2] == DIR_SEPARATOR_2
1085 /* Now skip any parts the two filenames have in common. */
1086 while (*p
== *q
&& *p
!= 0 && *q
!= 0)
1089 /* Now go backwards until the previous directory separator. */
1090 while (p
> name
&& p
[-1] != DIR_SEPARATOR
1091 #ifdef DIR_SEPARATOR_2
1092 && p
[-1] != DIR_SEPARATOR_2
1100 /* Standard error reporting routines in increasing order of severity.
1101 All of these take arguments like printf. */
1103 /* Text to be emitted verbatim to the error message stream; this
1104 produces no prefix and disables line-wrapping. Use rarely. */
1106 verbatim
VPARAMS ((const char *msgid
, ...))
1109 VA_OPEN (ap
, msgid
);
1110 VA_FIXEDARG (ap
, const char *, msgid
);
1112 text
.format_spec
= _(msgid
);
1113 text
.args_ptr
= &ap
;
1114 output_do_verbatim (&global_dc
->buffer
, &text
);
1115 output_buffer_to_stream (&global_dc
->buffer
);
1119 /* An informative note. Use this for additional details on an error
1122 inform
VPARAMS ((const char *msgid
, ...))
1124 diagnostic_info diagnostic
;
1126 VA_OPEN (ap
, msgid
);
1127 VA_FIXEDARG (ap
, const char *, msgid
);
1129 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, input_line
,
1131 report_diagnostic (&diagnostic
);
1135 /* A warning. Use this for code which is correct according to the
1136 relevant language specification but is likely to be buggy anyway. */
1138 warning
VPARAMS ((const char *msgid
, ...))
1140 diagnostic_info diagnostic
;
1142 VA_OPEN (ap
, msgid
);
1143 VA_FIXEDARG (ap
, const char *, msgid
);
1145 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, input_line
,
1147 report_diagnostic (&diagnostic
);
1151 /* A "pedantic" warning. Use this for code which triggers a
1152 diagnostic which is required by the relevant language
1153 specification, but which is considered unhelpful (i.e. there isn't
1154 anything *really* wrong with the construct in the language as she
1155 is spoke). It is a normal warning unless -pedantic-errors is
1156 applied, which turns it into an error. Note that pedwarn-s still
1157 happen if -pedantic is not given; you must write
1158 "if (pedantic) pedwarn (...)" to get a warning enabled only under
1159 -pedantic. All such warnings should, however, use pedwarn. */
1161 pedwarn
VPARAMS ((const char *msgid
, ...))
1163 diagnostic_info diagnostic
;
1164 VA_OPEN (ap
, msgid
);
1165 VA_FIXEDARG (ap
, const char *, msgid
);
1167 diagnostic_set_info (&diagnostic
, _(msgid
), &ap
, input_filename
, input_line
,
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
VPARAMS ((const char *msgid
, ...))
1178 diagnostic_info diagnostic
;
1180 VA_OPEN (ap
, msgid
);
1181 VA_FIXEDARG (ap
, const char *, msgid
);
1183 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, input_line
,
1185 report_diagnostic (&diagnostic
);
1189 /* "Sorry, not implemented." Use for a language feature which is
1190 required by the relevant specification but not implemented by GCC.
1191 An object file will not be produced. */
1193 sorry
VPARAMS ((const char *msgid
, ...))
1195 diagnostic_info diagnostic
;
1197 VA_OPEN (ap
, msgid
);
1198 VA_FIXEDARG (ap
, const char *, msgid
);
1201 diagnostic_set_info (&diagnostic
, _(msgid
), &ap
,
1202 input_filename
, input_line
, DK_SORRY
);
1205 (&global_dc
->buffer
, diagnostic_build_prefix (&diagnostic
));
1206 output_format (&global_dc
->buffer
, &diagnostic
.message
);
1207 output_flush (&global_dc
->buffer
);
1211 /* An error which is severe enough that we make no attempt to
1212 continue. Do not use this for internal consistency checks; that's
1213 internal_error. Use of this function should be rare. */
1215 fatal_error
VPARAMS ((const char *msgid
, ...))
1217 diagnostic_info diagnostic
;
1219 VA_OPEN (ap
, msgid
);
1220 VA_FIXEDARG (ap
, const char *, msgid
);
1222 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, input_line
,
1224 report_diagnostic (&diagnostic
);
1227 fnotice (stderr
, "compilation terminated.\n");
1228 exit (FATAL_EXIT_CODE
);
1231 /* An internal consistency check has failed. We make no attempt to
1232 continue. Note that unless there is debugging value to be had from
1233 a more specific message, or some other good reason, you should use
1234 abort () instead of calling this function directly. */
1236 internal_error
VPARAMS ((const char *msgid
, ...))
1238 diagnostic_info diagnostic
;
1240 VA_OPEN (ap
, msgid
);
1241 VA_FIXEDARG (ap
, const char *, msgid
);
1243 if (global_dc
->lock
)
1244 error_recursion (global_dc
);
1246 #ifndef ENABLE_CHECKING
1247 if (errorcount
> 0 || sorrycount
> 0)
1249 fnotice (stderr
, "%s:%d: confused by earlier errors, bailing out\n",
1250 input_filename
, input_line
);
1251 exit (FATAL_EXIT_CODE
);
1255 if (global_dc
->internal_error
!= 0)
1256 (*global_dc
->internal_error
) (_(msgid
), &ap
);
1258 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, input_line
,
1260 report_diagnostic (&diagnostic
);
1264 "Please submit a full bug report,\n\
1265 with preprocessed source if appropriate.\n\
1266 See %s for instructions.\n", bug_report_url
);
1267 exit (FATAL_EXIT_CODE
);
1270 /* Variants of some of the above, which make reference to a particular
1271 DECL node. These are deprecated. */
1274 warning_with_decl
VPARAMS ((tree decl
, const char *msgid
, ...))
1276 diagnostic_info diagnostic
;
1277 VA_OPEN (ap
, msgid
);
1278 VA_FIXEDARG (ap
, tree
, decl
);
1279 VA_FIXEDARG (ap
, const char *, msgid
);
1281 diagnostic_set_info (&diagnostic
, msgid
, &ap
,
1282 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
),
1284 diagnostic_for_decl (&diagnostic
, decl
);
1289 pedwarn_with_decl
VPARAMS ((tree decl
, const char *msgid
, ...))
1291 diagnostic_info diagnostic
;
1292 VA_OPEN (ap
, msgid
);
1293 VA_FIXEDARG (ap
, tree
, decl
);
1294 VA_FIXEDARG (ap
, const char *, msgid
);
1296 diagnostic_set_info (&diagnostic
, _(msgid
), &ap
,
1297 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
),
1298 pedantic_error_kind ());
1300 /* We don't want -pedantic-errors to cause the compilation to fail from
1301 "errors" in system header files. Sometimes fixincludes can't fix what's
1302 broken (eg: unsigned char bitfields - fixing it may change the alignment
1303 which will cause programs to mysteriously fail because the C library
1304 or kernel uses the original layout). There's no point in issuing a
1305 warning either, it's just unnecessary noise. */
1306 if (!DECL_IN_SYSTEM_HEADER (decl
))
1307 diagnostic_for_decl (&diagnostic
, decl
);
1312 error_with_decl
VPARAMS ((tree decl
, const char *msgid
, ...))
1314 diagnostic_info diagnostic
;
1315 VA_OPEN (ap
, msgid
);
1316 VA_FIXEDARG (ap
, tree
, decl
);
1317 VA_FIXEDARG (ap
, const char *, msgid
);
1319 diagnostic_set_info (&diagnostic
, msgid
, &ap
,
1320 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
),
1322 diagnostic_for_decl (&diagnostic
, decl
);
1326 /* Special case error functions. Most are implemented in terms of the
1327 above, or should be. */
1329 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1330 runs its second argument through gettext. */
1332 fnotice
VPARAMS ((FILE *file
, const char *msgid
, ...))
1334 VA_OPEN (ap
, msgid
);
1335 VA_FIXEDARG (ap
, FILE *, file
);
1336 VA_FIXEDARG (ap
, const char *, msgid
);
1338 vfprintf (file
, _(msgid
), ap
);
1342 /* Warn about a use of an identifier which was marked deprecated. */
1344 warn_deprecated_use (node
)
1347 if (node
== 0 || !warn_deprecated_decl
)
1351 warning ("`%s' is deprecated (declared at %s:%d)",
1352 IDENTIFIER_POINTER (DECL_NAME (node
)),
1353 DECL_SOURCE_FILE (node
), DECL_SOURCE_LINE (node
));
1354 else if (TYPE_P (node
))
1356 const char *what
= NULL
;
1357 tree decl
= TYPE_STUB_DECL (node
);
1359 if (TREE_CODE (TYPE_NAME (node
)) == IDENTIFIER_NODE
)
1360 what
= IDENTIFIER_POINTER (TYPE_NAME (node
));
1361 else if (TREE_CODE (TYPE_NAME (node
)) == TYPE_DECL
1362 && DECL_NAME (TYPE_NAME (node
)))
1363 what
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node
)));
1368 warning ("`%s' is deprecated (declared at %s:%d)", what
,
1369 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
));
1371 warning ("`%s' is deprecated", what
);
1374 warning ("type is deprecated (declared at %s:%d)",
1375 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
));
1377 warning ("type is deprecated");
1381 /* Print a fatal I/O error message. Argument are like printf.
1382 Also include a system error message based on `errno'. */
1384 fatal_io_error
VPARAMS ((const char *msgid
, ...))
1387 VA_OPEN (ap
, msgid
);
1388 VA_FIXEDARG (ap
, const char *, msgid
);
1390 text
.format_spec
= _(msgid
);
1391 text
.args_ptr
= &ap
;
1392 output_printf (&global_dc
->buffer
, "%s: %s: ", progname
, xstrerror (errno
));
1393 output_format (&global_dc
->buffer
, &text
);
1394 output_flush (&global_dc
->buffer
);
1396 exit (FATAL_EXIT_CODE
);
1399 /* Inform the user that an error occurred while trying to report some
1400 other error. This indicates catastrophic internal inconsistencies,
1401 so give up now. But do try to flush out the previous error.
1402 This mustn't use internal_error, that will cause infinite recursion. */
1405 error_recursion (context
)
1406 diagnostic_context
*context
;
1408 if (context
->lock
< 3)
1409 output_flush (&context
->buffer
);
1412 "Internal compiler error: Error reporting routines re-entered.\n");
1414 "Please submit a full bug report,\n\
1415 with preprocessed source if appropriate.\n\
1416 See %s for instructions.\n", bug_report_url
);
1417 exit (FATAL_EXIT_CODE
);
1420 /* Report an internal compiler error in a friendly manner. This is
1421 the function that gets called upon use of abort() in the source
1422 code generally, thanks to a special macro. */
1425 fancy_abort (file
, line
, function
)
1428 const char *function
;
1430 internal_error ("in %s, at %s:%d", function
, trim_filename (file
), line
);
1433 /* Really call the system 'abort'. This has to go right at the end of
1434 this file, so that there are no functions after it that call abort
1435 and get the system abort instead of our macro. */