1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999, 2000, 2001, 2002 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 *vbuild_message_string
PARAMS ((const char *, va_list))
55 ATTRIBUTE_PRINTF (1, 0);
56 static char *build_message_string
PARAMS ((const char *, ...))
58 static void format_with_decl
PARAMS ((output_buffer
*, text_info
*, tree
));
59 static void diagnostic_for_decl
PARAMS ((diagnostic_info
*, tree
));
60 static void set_real_maximum_length
PARAMS ((output_buffer
*));
62 static void output_unsigned_decimal
PARAMS ((output_buffer
*, unsigned int));
63 static void output_long_decimal
PARAMS ((output_buffer
*, long int));
64 static void output_long_unsigned_decimal
PARAMS ((output_buffer
*,
66 static void output_octal
PARAMS ((output_buffer
*, unsigned int));
67 static void output_long_octal
PARAMS ((output_buffer
*, unsigned long int));
68 static void output_hexadecimal
PARAMS ((output_buffer
*, unsigned int));
69 static void output_long_hexadecimal
PARAMS ((output_buffer
*,
71 static void output_append_r
PARAMS ((output_buffer
*, const char *, int));
72 static void wrap_text
PARAMS ((output_buffer
*, const char *, const char *));
73 static void maybe_wrap_text
PARAMS ((output_buffer
*, const char *,
75 static void output_clear_data
PARAMS ((output_buffer
*));
77 static void default_diagnostic_starter
PARAMS ((diagnostic_context
*,
79 static void default_diagnostic_finalizer
PARAMS ((diagnostic_context
*,
82 static void error_recursion
PARAMS ((diagnostic_context
*)) ATTRIBUTE_NORETURN
;
83 static bool text_specifies_location
PARAMS ((text_info
*, location_t
*));
84 static void real_abort
PARAMS ((void)) ATTRIBUTE_NORETURN
;
86 extern int rtl_dump_and_exit
;
87 extern int warnings_are_errors
;
89 /* A diagnostic_context surrogate for stderr. */
90 static diagnostic_context global_diagnostic_context
;
91 diagnostic_context
*global_dc
= &global_diagnostic_context
;
94 /* Subroutine of output_set_maximum_length. Set up BUFFER's
95 internal maximum characters per line. */
97 set_real_maximum_length (buffer
)
98 output_buffer
*buffer
;
100 /* If we're told not to wrap lines then do the obvious thing. In case
101 we'll emit prefix only once per diagnostic message, it is appropriate
102 not to increase unnecessarily the line-length cut-off. */
103 if (!output_is_line_wrapping (buffer
)
104 || output_prefixing_rule (buffer
) == DIAGNOSTICS_SHOW_PREFIX_ONCE
105 || output_prefixing_rule (buffer
) == DIAGNOSTICS_SHOW_PREFIX_NEVER
)
106 line_wrap_cutoff (buffer
) = output_line_cutoff (buffer
);
109 int prefix_length
= buffer
->state
.prefix
?
110 strlen (buffer
->state
.prefix
) : 0;
111 /* If the prefix is ridiculously too long, output at least
113 if (output_line_cutoff (buffer
) - prefix_length
< 32)
114 line_wrap_cutoff (buffer
) = output_line_cutoff (buffer
) + 32;
116 line_wrap_cutoff (buffer
) = output_line_cutoff (buffer
);
120 /* Sets the number of maximum characters per line BUFFER can output
121 in line-wrapping mode. A LENGTH value 0 suppresses line-wrapping. */
123 output_set_maximum_length (buffer
, length
)
124 output_buffer
*buffer
;
127 output_line_cutoff (buffer
) = length
;
128 set_real_maximum_length (buffer
);
131 /* Sets BUFFER's PREFIX. */
133 output_set_prefix (buffer
, prefix
)
134 output_buffer
*buffer
;
137 buffer
->state
.prefix
= prefix
;
138 set_real_maximum_length (buffer
);
139 prefix_was_emitted_for (buffer
) = false;
140 output_indentation (buffer
) = 0;
143 /* Return a pointer to the last character emitted in the output
144 BUFFER area. A NULL pointer means no character available. */
146 output_last_position (buffer
)
147 const output_buffer
*buffer
;
149 const char *p
= NULL
;
151 if (obstack_base (&buffer
->obstack
) != obstack_next_free (&buffer
->obstack
))
152 p
= ((const char *) obstack_next_free (&buffer
->obstack
)) - 1;
156 /* Free BUFFER's prefix, a previously malloc'd string. */
158 output_destroy_prefix (buffer
)
159 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 (buffer
)
171 output_buffer
*buffer
;
173 obstack_free (&buffer
->obstack
, obstack_base (&buffer
->obstack
));
174 output_text_length (buffer
) = 0;
177 /* Zero out any formatting data used so far by BUFFER. */
179 output_clear_data (buffer
)
180 output_buffer
*buffer
;
182 prefix_was_emitted_for (buffer
) = false;
183 output_indentation (buffer
) = 0;
186 /* Construct an output BUFFER with PREFIX and of MAXIMUM_LENGTH
187 characters per line. */
189 init_output_buffer (buffer
, prefix
, maximum_length
)
190 output_buffer
*buffer
;
194 memset (buffer
, 0, sizeof (output_buffer
));
195 obstack_init (&buffer
->obstack
);
196 output_buffer_attached_stream (buffer
) = stderr
;
197 output_line_cutoff (buffer
) = maximum_length
;
198 output_prefixing_rule (buffer
) = diagnostic_prefixing_rule (global_dc
);
199 output_set_prefix (buffer
, prefix
);
200 output_text_length (buffer
) = 0;
201 output_clear_data (buffer
);
204 /* Reinitialize BUFFER. */
206 output_clear (buffer
)
207 output_buffer
*buffer
;
209 output_clear_message_text (buffer
);
210 output_clear_data (buffer
);
213 /* Finishes constructing a NULL-terminated character string representing
214 the BUFFERed message. */
216 output_finalize_message (buffer
)
217 output_buffer
*buffer
;
219 obstack_1grow (&buffer
->obstack
, '\0');
220 return output_message_text (buffer
);
223 /* Return the amount of characters BUFFER can accept to
226 output_space_left (buffer
)
227 const output_buffer
*buffer
;
229 return line_wrap_cutoff (buffer
) - output_text_length (buffer
);
232 /* Write out BUFFER's prefix. */
234 output_emit_prefix (buffer
)
235 output_buffer
*buffer
;
237 if (buffer
->state
.prefix
!= NULL
)
239 switch (output_prefixing_rule (buffer
))
242 case DIAGNOSTICS_SHOW_PREFIX_NEVER
:
245 case DIAGNOSTICS_SHOW_PREFIX_ONCE
:
246 if (prefix_was_emitted_for (buffer
))
248 output_indent (buffer
);
251 output_indentation (buffer
) += 3;
254 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE
:
256 int prefix_length
= strlen (buffer
->state
.prefix
);
257 output_append_r (buffer
, buffer
->state
.prefix
, prefix_length
);
258 prefix_was_emitted_for (buffer
) = true;
265 /* Have BUFFER start a new line. */
267 output_add_newline (buffer
)
268 output_buffer
*buffer
;
270 obstack_1grow (&buffer
->obstack
, '\n');
271 output_text_length (buffer
) = 0;
274 /* Appends a character to BUFFER. */
276 output_add_character (buffer
, c
)
277 output_buffer
*buffer
;
280 if (output_is_line_wrapping (buffer
) && output_space_left (buffer
) <= 0)
281 output_add_newline (buffer
);
282 obstack_1grow (&buffer
->obstack
, c
);
283 ++output_text_length (buffer
);
286 /* Adds a space to BUFFER. */
288 output_add_space (buffer
)
289 output_buffer
*buffer
;
291 if (output_is_line_wrapping (buffer
) && output_space_left (buffer
) <= 0)
293 output_add_newline (buffer
);
296 obstack_1grow (&buffer
->obstack
, ' ');
297 ++output_text_length (buffer
);
300 /* These functions format an INTEGER into BUFFER as suggested by their
303 output_decimal (buffer
, i
)
304 output_buffer
*buffer
;
307 output_formatted_scalar (buffer
, "%d", i
);
311 output_long_decimal (buffer
, i
)
312 output_buffer
*buffer
;
315 output_formatted_scalar (buffer
, "%ld", i
);
319 output_unsigned_decimal (buffer
, i
)
320 output_buffer
*buffer
;
323 output_formatted_scalar (buffer
, "%u", i
);
327 output_long_unsigned_decimal (buffer
, i
)
328 output_buffer
*buffer
;
331 output_formatted_scalar (buffer
, "%lu", i
);
335 output_octal (buffer
, i
)
336 output_buffer
*buffer
;
339 output_formatted_scalar (buffer
, "%o", i
);
343 output_long_octal (buffer
, i
)
344 output_buffer
*buffer
;
347 output_formatted_scalar (buffer
, "%lo", i
);
351 output_hexadecimal (buffer
, i
)
352 output_buffer
*buffer
;
355 output_formatted_scalar (buffer
, "%x", i
);
359 output_long_hexadecimal (buffer
, i
)
360 output_buffer
*buffer
;
363 output_formatted_scalar (buffer
, "%lx", i
);
366 /* Append to BUFFER a string specified by its STARTING character
369 output_append_r (buffer
, start
, length
)
370 output_buffer
*buffer
;
374 obstack_grow (&buffer
->obstack
, start
, length
);
375 output_text_length (buffer
) += length
;
378 /* Append a string deliminated by START and END to BUFFER. No wrapping is
379 done. However, if beginning a new line then emit BUFFER->state.prefix
380 and skip any leading whitespace if appropriate. The caller must ensure
381 that it is safe to do so. */
383 output_append (buffer
, start
, end
)
384 output_buffer
*buffer
;
388 /* Emit prefix and skip whitespace if we're starting a new line. */
389 if (is_starting_newline (buffer
))
391 output_emit_prefix (buffer
);
392 if (output_is_line_wrapping (buffer
))
393 while (start
!= end
&& *start
== ' ')
396 output_append_r (buffer
, start
, end
- start
);
400 output_indent (buffer
)
401 output_buffer
*buffer
;
403 int n
= output_indentation (buffer
);
406 for (i
= 0; i
< n
; ++i
)
407 output_add_character (buffer
, ' ');
410 /* Wrap a text delimited by START and END into BUFFER. */
412 wrap_text (buffer
, start
, end
)
413 output_buffer
*buffer
;
417 bool is_wrapping
= output_is_line_wrapping (buffer
);
421 /* Dump anything bordered by whitespaces. */
423 const char *p
= start
;
424 while (p
!= end
&& *p
!= ' ' && *p
!= '\n')
426 if (is_wrapping
&& p
- start
>= output_space_left (buffer
))
427 output_add_newline (buffer
);
428 output_append (buffer
, start
, p
);
432 if (start
!= end
&& *start
== ' ')
434 output_add_space (buffer
);
437 if (start
!= end
&& *start
== '\n')
439 output_add_newline (buffer
);
445 /* Same as wrap_text but wrap text only when in line-wrapping mode. */
447 maybe_wrap_text (buffer
, start
, end
)
448 output_buffer
*buffer
;
452 if (output_is_line_wrapping (buffer
))
453 wrap_text (buffer
, start
, end
);
455 output_append (buffer
, start
, end
);
459 /* Append a STRING to BUFFER; the STRING might be line-wrapped if in
462 output_add_string (buffer
, str
)
463 output_buffer
*buffer
;
466 maybe_wrap_text (buffer
, str
, str
+ (str
? strlen (str
) : 0));
469 /* Append an identifier ID to BUFFER. */
471 output_add_identifier (buffer
, id
)
472 output_buffer
*buffer
;
475 output_append (buffer
, IDENTIFIER_POINTER (id
),
476 IDENTIFIER_POINTER (id
) + IDENTIFIER_LENGTH (id
));
479 /* Flush the content of BUFFER onto the attached stream,
483 output_buffer_to_stream (buffer
)
484 output_buffer
*buffer
;
486 const char *text
= output_finalize_message (buffer
);
487 fputs (text
, output_buffer_attached_stream (buffer
));
488 output_clear_message_text (buffer
);
491 /* Format a message pointed to by TEXT. The following format specifiers are
492 recognized as being language independent:
493 %d, %i: (signed) integer in base ten.
494 %u: unsigned integer in base ten.
495 %o: unsigned integer in base eight.
496 %x: unsigned integer in base sixteen.
497 %ld, %li, %lo, %lu, %lx: long versions of the above.
501 %*.s: a substring the length of which is specified by an integer.
504 output_format (buffer
, text
)
505 output_buffer
*buffer
;
508 for (; *text
->format_spec
; ++text
->format_spec
)
510 bool long_integer
= 0;
514 const char *p
= text
->format_spec
;
515 while (*p
&& *p
!= '%')
517 wrap_text (buffer
, text
->format_spec
, p
);
518 text
->format_spec
= p
;
521 if (*text
->format_spec
== '\0')
524 /* We got a '%'. Let's see what happens. Record whether we're
525 parsing a long integer format specifier. */
526 if (*++text
->format_spec
== 'l')
532 /* Handle %c, %d, %i, %ld, %li, %lo, %lu, %lx, %o, %s, %u,
533 %x, %.*s; %%. And nothing else. Front-ends should install
534 printers to grok language specific format specifiers. */
535 switch (*text
->format_spec
)
538 output_add_character (buffer
, va_arg (*text
->args_ptr
, int));
544 output_long_decimal (buffer
, va_arg (*text
->args_ptr
, long int));
546 output_decimal (buffer
, va_arg (*text
->args_ptr
, int));
551 output_long_octal (buffer
,
552 va_arg (*text
->args_ptr
, unsigned long int));
554 output_octal (buffer
, va_arg (*text
->args_ptr
, unsigned int));
558 output_add_string (buffer
, va_arg (*text
->args_ptr
, const char *));
563 output_long_unsigned_decimal
564 (buffer
, va_arg (*text
->args_ptr
, long unsigned int));
566 output_unsigned_decimal
567 (buffer
, va_arg (*text
->args_ptr
, unsigned int));
572 output_long_hexadecimal
573 (buffer
, va_arg (*text
->args_ptr
, unsigned long int));
576 (buffer
, va_arg (*text
->args_ptr
, unsigned int));
580 output_add_character (buffer
, '%');
585 const location_t
*locus
= va_arg (*text
->args_ptr
, location_t
*);
586 output_add_string (buffer
, "file '");
587 output_add_string (buffer
, locus
->file
);
588 output_add_string (buffer
, "', line ");
589 output_decimal (buffer
, locus
->line
);
597 /* We handle no precision specifier but `%.*s'. */
598 if (*++text
->format_spec
!= '*')
600 else if (*++text
->format_spec
!= 's')
602 n
= va_arg (*text
->args_ptr
, int);
603 s
= va_arg (*text
->args_ptr
, const char *);
604 output_append (buffer
, s
, s
+ n
);
609 if (!buffer
->format_decoder
610 || !(*buffer
->format_decoder
) (buffer
, text
))
612 /* Hmmm. The front-end failed to install a format translator
613 but called us with an unrecognized format. Sorry. */
621 vbuild_message_string (msg
, ap
)
627 vasprintf (&str
, msg
, ap
);
631 /* Return a malloc'd string containing MSG formatted a la
632 printf. The caller is responsible for freeing the memory. */
634 build_message_string
VPARAMS ((const char *msg
, ...))
639 VA_FIXEDARG (ap
, const char *, msg
);
641 str
= vbuild_message_string (msg
, ap
);
648 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
650 file_name_as_prefix (f
)
653 return build_message_string ("%s: ", f
);
656 /* Format a message into BUFFER a la printf. */
658 output_printf
VPARAMS ((struct output_buffer
*buffer
, const char *msgid
, ...))
662 VA_FIXEDARG (ap
, output_buffer
*, buffer
);
663 VA_FIXEDARG (ap
, const char *, msgid
);
666 text
.format_spec
= _(msgid
);
667 output_format (buffer
, &text
);
671 /* Print a message relevant to the given DECL. */
673 format_with_decl (buffer
, text
, decl
)
674 output_buffer
*buffer
;
680 /* Do magic to get around lack of varargs support for insertion
681 of arguments into existing list. We know that the decl is first;
682 we ass_u_me that it will be printed with "%s". */
683 for (p
= text
->format_spec
; *p
; ++p
)
689 else if (*(p
+ 1) != 's')
696 /* Print the left-hand substring. */
697 maybe_wrap_text (buffer
, text
->format_spec
, p
);
699 if (*p
== '%') /* Print the name. */
701 const char *const n
= (DECL_NAME (decl
)
702 ? (*lang_hooks
.decl_printable_name
) (decl
, 2)
703 : _("((anonymous))"));
704 output_add_string (buffer
, n
);
708 if (ISALPHA (*(p
- 1) & 0xFF))
713 if (*p
) /* Print the rest of the message. */
715 text
->format_spec
= p
;
716 output_format (buffer
, text
);
720 /* Flush the content of BUFFER onto the attached stream. */
722 output_flush (buffer
)
723 output_buffer
*buffer
;
725 output_buffer_to_stream (buffer
);
726 output_clear_data (buffer
);
727 fputc ('\n', output_buffer_attached_stream (buffer
));
728 fflush (output_buffer_attached_stream (buffer
));
731 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
732 settings needed by BUFFER for a verbatim formatting. */
734 output_do_verbatim (buffer
, text
)
735 output_buffer
*buffer
;
738 diagnostic_prefixing_rule_t rule
= output_prefixing_rule (buffer
);
739 int line_cutoff
= output_line_cutoff (buffer
);
741 /* Set verbatim mode. */
742 output_prefixing_rule (buffer
) = DIAGNOSTICS_SHOW_PREFIX_NEVER
;
743 output_line_cutoff (buffer
) = 0;
744 /* Do the actual formatting. */
745 output_format (buffer
, text
);
746 /* Restore previous settings. */
747 output_prefixing_rule (buffer
) = rule
;
748 output_line_cutoff (buffer
) = line_cutoff
;
751 /* Output MESSAGE verbatim into BUFFER. */
753 output_verbatim
VPARAMS ((output_buffer
*buffer
, const char *msgid
, ...))
757 VA_FIXEDARG (ap
, output_buffer
*, buffer
);
758 VA_FIXEDARG (ap
, const char *, msgid
);
760 text
.format_spec
= msgid
;
762 output_do_verbatim (buffer
, &text
);
767 /* Initialize the diagnostic message outputting machinery. */
769 diagnostic_initialize (context
)
770 diagnostic_context
*context
;
772 memset (context
, 0, sizeof *context
);
773 obstack_init (&context
->buffer
.obstack
);
775 /* By default, diagnostics are sent to stderr. */
776 output_buffer_attached_stream (&context
->buffer
) = stderr
;
778 /* By default, we emit prefixes once per message. */
779 diagnostic_prefixing_rule (context
) = DIAGNOSTICS_SHOW_PREFIX_ONCE
;
781 diagnostic_starter (context
) = default_diagnostic_starter
;
782 diagnostic_finalizer (context
) = default_diagnostic_finalizer
;
783 context
->warnings_are_errors_message
= warnings_are_errors
;
786 /* Returns true if the next format specifier in TEXT is a format specifier
787 for a location_t. If so, update the object pointed by LOCUS to reflect
788 the specified location in *TEXT->args_ptr. */
790 text_specifies_location (text
, locus
)
795 /* Skip any leading text. */
796 for (p
= text
->format_spec
; *p
&& *p
!= '%'; ++p
)
799 /* Extract the location information if any. */
800 if (*p
== '%' && *++p
== 'H')
802 *locus
= *va_arg (*text
->args_ptr
, location_t
*);
803 text
->format_spec
= p
+ 1;
811 diagnostic_set_info (diagnostic
, msgid
, args
, file
, line
, kind
)
812 diagnostic_info
*diagnostic
;
819 diagnostic
->message
.format_spec
= msgid
;
820 diagnostic
->message
.args_ptr
= args
;
821 /* If the diagnostic message doesn't specify a location,
822 use FILE and LINE. */
823 if (!text_specifies_location (&diagnostic
->message
, &diagnostic
->location
))
825 diagnostic
->location
.file
= file
;
826 diagnostic
->location
.line
= line
;
828 diagnostic
->kind
= kind
;
831 /* Return a malloc'd string describing a location. The caller is
832 responsible for freeing the memory. */
834 diagnostic_build_prefix (diagnostic
)
835 diagnostic_info
*diagnostic
;
837 static const char *const diagnostic_kind_text
[] = {
838 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
839 #include "diagnostic.def"
840 #undef DEFINE_DIAGNOSTIC_KIND
843 if (diagnostic
->kind
>= DK_LAST_DIAGNOSTIC_KIND
)
846 return diagnostic
->location
.file
847 ? build_message_string ("%s:%d: %s",
848 diagnostic
->location
.file
,
849 diagnostic
->location
.line
,
850 _(diagnostic_kind_text
[diagnostic
->kind
]))
851 : build_message_string ("%s: %s", progname
,
852 _(diagnostic_kind_text
[diagnostic
->kind
]));
855 /* Report a diagnostic MESSAGE at the declaration DECL.
856 MSG is a format string which uses %s to substitute the declaration
857 name; subsequent substitutions are a la output_format. */
859 diagnostic_for_decl (diagnostic
, decl
)
860 diagnostic_info
*diagnostic
;
863 if (global_dc
->lock
++)
864 error_recursion (global_dc
);
866 if (diagnostic_count_diagnostic (global_dc
, diagnostic
->kind
))
868 diagnostic_report_current_function (global_dc
);
870 (&global_dc
->buffer
, diagnostic_build_prefix (diagnostic
));
871 format_with_decl (&global_dc
->buffer
, &diagnostic
->message
, decl
);
872 output_flush (&global_dc
->buffer
);
873 output_destroy_prefix (&global_dc
->buffer
);
879 diagnostic_flush_buffer (context
)
880 diagnostic_context
*context
;
882 output_buffer_to_stream (&context
->buffer
);
883 fflush (output_buffer_attached_stream (&context
->buffer
));
886 /* Count a diagnostic. Return true if the message should be printed. */
888 diagnostic_count_diagnostic (context
, kind
)
889 diagnostic_context
*context
;
898 case DK_FATAL
: case DK_ICE
: case DK_SORRY
:
899 case DK_ANACHRONISM
: case DK_NOTE
:
900 ++diagnostic_kind_count (context
, kind
);
904 if (!diagnostic_report_warnings_p ())
906 else if (!warnings_are_errors
)
908 ++diagnostic_kind_count (context
, DK_WARNING
);
911 /* else fall through. */
914 if (kind
== DK_WARNING
&& context
->warnings_are_errors_message
)
916 output_verbatim (&context
->buffer
,
917 "%s: warnings being treated as errors\n", progname
);
918 context
->warnings_are_errors_message
= false;
920 ++diagnostic_kind_count (context
, DK_ERROR
);
927 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
928 runs its second argument through gettext. */
930 fnotice
VPARAMS ((FILE *file
, const char *msgid
, ...))
933 VA_FIXEDARG (ap
, FILE *, file
);
934 VA_FIXEDARG (ap
, const char *, msgid
);
936 vfprintf (file
, _(msgid
), ap
);
941 /* Print a fatal I/O error message. Argument are like printf.
942 Also include a system error message based on `errno'. */
944 fatal_io_error
VPARAMS ((const char *msgid
, ...))
948 VA_FIXEDARG (ap
, const char *, msgid
);
950 text
.format_spec
= _(msgid
);
952 output_printf (&global_dc
->buffer
, "%s: %s: ", progname
, xstrerror (errno
));
953 output_format (&global_dc
->buffer
, &text
);
954 output_flush (&global_dc
->buffer
);
956 exit (FATAL_EXIT_CODE
);
959 /* Issue a pedantic warning MSGID. */
961 pedwarn
VPARAMS ((const char *msgid
, ...))
963 diagnostic_info diagnostic
;
965 VA_FIXEDARG (ap
, const char *, msgid
);
967 diagnostic_set_info (&diagnostic
, _(msgid
), &ap
, input_filename
, lineno
,
968 pedantic_error_kind ());
969 report_diagnostic (&diagnostic
);
973 /* Issue a pedantic warning about DECL. */
975 pedwarn_with_decl
VPARAMS ((tree decl
, const char *msgid
, ...))
977 diagnostic_info diagnostic
;
979 VA_FIXEDARG (ap
, tree
, decl
);
980 VA_FIXEDARG (ap
, const char *, msgid
);
982 diagnostic_set_info (&diagnostic
, _(msgid
), &ap
,
983 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
),
984 pedantic_error_kind ());
986 /* We don't want -pedantic-errors to cause the compilation to fail from
987 "errors" in system header files. Sometimes fixincludes can't fix what's
988 broken (eg: unsigned char bitfields - fixing it may change the alignment
989 which will cause programs to mysteriously fail because the C library
990 or kernel uses the original layout). There's no point in issuing a
991 warning either, it's just unnecessary noise. */
992 if (!DECL_IN_SYSTEM_HEADER (decl
))
993 diagnostic_for_decl (&diagnostic
, decl
);
997 /* Same as above but within the context FILE and LINE. */
999 pedwarn_with_file_and_line
VPARAMS ((const char *file
, int line
,
1000 const char *msgid
, ...))
1002 diagnostic_info diagnostic
;
1003 VA_OPEN (ap
, msgid
);
1004 VA_FIXEDARG (ap
, const char *, file
);
1005 VA_FIXEDARG (ap
, int, line
);
1006 VA_FIXEDARG (ap
, const char *, msgid
);
1008 diagnostic_set_info (&diagnostic
, _(msgid
), &ap
, file
, line
,
1009 pedantic_error_kind ());
1010 report_diagnostic (&diagnostic
);
1014 /* Just apologize with MSGID. */
1016 sorry
VPARAMS ((const char *msgid
, ...))
1018 diagnostic_info diagnostic
;
1020 VA_OPEN (ap
, msgid
);
1021 VA_FIXEDARG (ap
, const char *, msgid
);
1024 diagnostic_set_info (&diagnostic
, _(msgid
), &ap
,
1025 input_filename
, lineno
, DK_SORRY
);
1028 (&global_dc
->buffer
, diagnostic_build_prefix (&diagnostic
));
1029 output_format (&global_dc
->buffer
, &diagnostic
.message
);
1030 output_flush (&global_dc
->buffer
);
1034 /* Called when the start of a function definition is parsed,
1035 this function prints on stderr the name of the function. */
1037 announce_function (decl
)
1042 if (rtl_dump_and_exit
)
1043 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl
)));
1045 verbatim (" %s", (*lang_hooks
.decl_printable_name
) (decl
, 2));
1047 output_needs_newline (&global_dc
->buffer
) = true;
1048 diagnostic_set_last_function (global_dc
);
1052 /* The default function to print out name of current function that caused
1055 lhd_print_error_function (context
, file
)
1056 diagnostic_context
*context
;
1059 if (diagnostic_last_function_changed (context
))
1061 const char *old_prefix
= output_prefix (&context
->buffer
);
1062 char *new_prefix
= file
? build_message_string ("%s: ", file
) : NULL
;
1064 output_set_prefix (&context
->buffer
, new_prefix
);
1066 if (current_function_decl
== NULL
)
1067 output_add_string (&context
->buffer
, _("At top level:"));
1070 if (TREE_CODE (TREE_TYPE (current_function_decl
)) == METHOD_TYPE
)
1072 (&context
->buffer
, "In member function `%s':",
1073 (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2));
1076 (&context
->buffer
, "In function `%s':",
1077 (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2));
1079 output_add_newline (&context
->buffer
);
1081 diagnostic_set_last_function (context
);
1082 output_buffer_to_stream (&context
->buffer
);
1083 context
->buffer
.state
.prefix
= old_prefix
;
1084 free ((char*) new_prefix
);
1088 /* Prints out, if necessary, the name of the current function
1089 that caused an error. Called from all error and warning functions.
1090 We ignore the FILE parameter, as it cannot be relied upon. */
1093 diagnostic_report_current_function (context
)
1094 diagnostic_context
*context
;
1096 diagnostic_report_current_module (context
);
1097 (*lang_hooks
.print_error_function
) (context
, input_filename
);
1101 error_with_file_and_line
VPARAMS ((const char *file
, int line
,
1102 const char *msgid
, ...))
1104 diagnostic_info diagnostic
;
1106 VA_OPEN (ap
, msgid
);
1107 VA_FIXEDARG (ap
, const char *, file
);
1108 VA_FIXEDARG (ap
, int, line
);
1109 VA_FIXEDARG (ap
, const char *, msgid
);
1111 diagnostic_set_info (&diagnostic
, msgid
, &ap
, file
, line
, DK_ERROR
);
1112 report_diagnostic (&diagnostic
);
1117 error_with_decl
VPARAMS ((tree decl
, const char *msgid
, ...))
1119 diagnostic_info diagnostic
;
1120 VA_OPEN (ap
, msgid
);
1121 VA_FIXEDARG (ap
, tree
, decl
);
1122 VA_FIXEDARG (ap
, const char *, msgid
);
1124 diagnostic_set_info (&diagnostic
, msgid
, &ap
,
1125 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
),
1127 diagnostic_for_decl (&diagnostic
, decl
);
1132 /* Report an error message. The arguments are like that of printf. */
1135 error
VPARAMS ((const char *msgid
, ...))
1137 diagnostic_info diagnostic
;
1139 VA_OPEN (ap
, msgid
);
1140 VA_FIXEDARG (ap
, const char *, msgid
);
1142 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, lineno
,
1144 report_diagnostic (&diagnostic
);
1148 /* Likewise, except that the compilation is terminated after printing the
1152 fatal_error
VPARAMS ((const char *msgid
, ...))
1154 diagnostic_info diagnostic
;
1156 VA_OPEN (ap
, msgid
);
1157 VA_FIXEDARG (ap
, const char *, msgid
);
1159 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, lineno
,
1161 report_diagnostic (&diagnostic
);
1164 fnotice (stderr
, "compilation terminated.\n");
1165 exit (FATAL_EXIT_CODE
);
1169 internal_error
VPARAMS ((const char *msgid
, ...))
1171 diagnostic_info diagnostic
;
1173 VA_OPEN (ap
, msgid
);
1174 VA_FIXEDARG (ap
, const char *, msgid
);
1176 if (global_dc
->lock
)
1177 error_recursion (global_dc
);
1179 #ifndef ENABLE_CHECKING
1180 if (errorcount
> 0 || sorrycount
> 0)
1182 fnotice (stderr
, "%s:%d: confused by earlier errors, bailing out\n",
1183 input_filename
, lineno
);
1184 exit (FATAL_EXIT_CODE
);
1188 if (global_dc
->internal_error
!= 0)
1189 (*global_dc
->internal_error
) (_(msgid
), &ap
);
1191 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, lineno
,
1193 report_diagnostic (&diagnostic
);
1197 "Please submit a full bug report,\n\
1198 with preprocessed source if appropriate.\n\
1199 See %s for instructions.\n", bug_report_url
);
1200 exit (FATAL_EXIT_CODE
);
1204 warning_with_file_and_line
VPARAMS ((const char *file
, int line
,
1205 const char *msgid
, ...))
1207 diagnostic_info diagnostic
;
1209 VA_OPEN (ap
, msgid
);
1210 VA_FIXEDARG (ap
, const char *, file
);
1211 VA_FIXEDARG (ap
, int, line
);
1212 VA_FIXEDARG (ap
, const char *, msgid
);
1214 diagnostic_set_info (&diagnostic
, msgid
, &ap
, file
, line
, DK_WARNING
);
1215 report_diagnostic (&diagnostic
);
1220 warning_with_decl
VPARAMS ((tree decl
, const char *msgid
, ...))
1222 diagnostic_info diagnostic
;
1223 VA_OPEN (ap
, msgid
);
1224 VA_FIXEDARG (ap
, tree
, decl
);
1225 VA_FIXEDARG (ap
, const char *, msgid
);
1227 diagnostic_set_info (&diagnostic
, msgid
, &ap
,
1228 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
),
1230 diagnostic_for_decl (&diagnostic
, decl
);
1235 warning
VPARAMS ((const char *msgid
, ...))
1237 diagnostic_info diagnostic
;
1239 VA_OPEN (ap
, msgid
);
1240 VA_FIXEDARG (ap
, const char *, msgid
);
1242 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, lineno
,
1244 report_diagnostic (&diagnostic
);
1249 /* Same as above but use diagnostic_buffer. */
1252 verbatim
VPARAMS ((const char *msgid
, ...))
1255 VA_OPEN (ap
, msgid
);
1256 VA_FIXEDARG (ap
, const char *, msgid
);
1258 text
.format_spec
= _(msgid
);
1259 text
.args_ptr
= &ap
;
1260 output_do_verbatim (&global_dc
->buffer
, &text
);
1261 output_buffer_to_stream (&global_dc
->buffer
);
1265 /* Report a diagnostic message (an error or a warning) as specified by
1266 DC. This function is *the* subroutine in terms of which front-ends
1267 should implement their specific diagnostic handling modules. The
1268 front-end independent format specifiers are exactly those described
1269 in the documentation of output_format. */
1272 diagnostic_report_diagnostic (context
, diagnostic
)
1273 diagnostic_context
*context
;
1274 diagnostic_info
*diagnostic
;
1276 if (context
->lock
++)
1277 error_recursion (context
);
1279 if (diagnostic_count_diagnostic (context
, diagnostic
->kind
))
1281 (*diagnostic_starter (context
)) (context
, diagnostic
);
1282 output_format (&context
->buffer
, &diagnostic
->message
);
1283 (*diagnostic_finalizer (context
)) (context
, diagnostic
);
1284 output_flush (&context
->buffer
);
1287 if (context
->abort_on_error
&& diagnostic
->kind
<= DK_ERROR
)
1292 /* Inform the user that an error occurred while trying to report some
1293 other error. This indicates catastrophic internal inconsistencies,
1294 so give up now. But do try to flush out the previous error.
1295 This mustn't use internal_error, that will cause infinite recursion. */
1298 error_recursion (context
)
1299 diagnostic_context
*context
;
1301 if (context
->lock
< 3)
1302 output_flush (&context
->buffer
);
1305 "Internal compiler error: Error reporting routines re-entered.\n");
1307 "Please submit a full bug report,\n\
1308 with preprocessed source if appropriate.\n\
1309 See %s for instructions.\n", bug_report_url
);
1310 exit (FATAL_EXIT_CODE
);
1313 /* Given a partial pathname as input, return another pathname that
1314 shares no directory elements with the pathname of __FILE__. This
1315 is used by fancy_abort() to print `Internal compiler error in expr.c'
1316 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1319 trim_filename (name
)
1322 static const char this_file
[] = __FILE__
;
1323 const char *p
= name
, *q
= this_file
;
1325 /* First skip any "../" in each filename. This allows us to give a proper
1326 reference to a file in a subdirectory. */
1327 while (p
[0] == '.' && p
[1] == '.'
1328 && (p
[2] == DIR_SEPARATOR
1329 #ifdef DIR_SEPARATOR_2
1330 || p
[2] == DIR_SEPARATOR_2
1335 while (q
[0] == '.' && q
[1] == '.'
1336 && (q
[2] == DIR_SEPARATOR
1337 #ifdef DIR_SEPARATOR_2
1338 || p
[2] == DIR_SEPARATOR_2
1343 /* Now skip any parts the two filenames have in common. */
1344 while (*p
== *q
&& *p
!= 0 && *q
!= 0)
1347 /* Now go backwards until the previous directory separator. */
1348 while (p
> name
&& p
[-1] != DIR_SEPARATOR
1349 #ifdef DIR_SEPARATOR_2
1350 && p
[-1] != DIR_SEPARATOR_2
1358 /* Report an internal compiler error in a friendly manner and without
1362 fancy_abort (file
, line
, function
)
1365 const char *function
;
1367 internal_error ("in %s, at %s:%d", function
, trim_filename (file
), line
);
1371 diagnostic_report_current_module (context
)
1372 diagnostic_context
*context
;
1374 struct file_stack
*p
;
1376 if (output_needs_newline (&context
->buffer
))
1378 output_add_newline (&context
->buffer
);
1379 output_needs_newline (&context
->buffer
) = false;
1382 if (input_file_stack
&& input_file_stack
->next
!= 0
1383 && diagnostic_last_module_changed (context
))
1385 for (p
= input_file_stack
->next
; p
; p
= p
->next
)
1386 if (p
== input_file_stack
->next
)
1387 output_verbatim (&context
->buffer
,
1388 "In file included from %s:%d", p
->name
, p
->line
);
1390 output_verbatim (&context
->buffer
,
1391 ",\n from %s:%d", p
->name
, p
->line
);
1392 output_verbatim (&context
->buffer
, ":\n");
1393 diagnostic_set_last_module (context
);
1398 default_diagnostic_starter (context
, diagnostic
)
1399 diagnostic_context
*context
;
1400 diagnostic_info
*diagnostic
;
1402 diagnostic_report_current_function (context
);
1403 output_set_prefix (&context
->buffer
, diagnostic_build_prefix (diagnostic
));
1407 default_diagnostic_finalizer (context
, diagnostic
)
1408 diagnostic_context
*context
;
1409 diagnostic_info
*diagnostic
__attribute__((unused
));
1411 output_destroy_prefix (&context
->buffer
);
1415 inform
VPARAMS ((const char *msgid
, ...))
1417 diagnostic_info diagnostic
;
1419 VA_OPEN (ap
, msgid
);
1420 VA_FIXEDARG (ap
, const char *, msgid
);
1422 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, lineno
,
1424 report_diagnostic (&diagnostic
);
1429 warn_deprecated_use (node
)
1432 if (node
== 0 || !warn_deprecated_decl
)
1436 warning ("`%s' is deprecated (declared at %s:%d)",
1437 IDENTIFIER_POINTER (DECL_NAME (node
)),
1438 DECL_SOURCE_FILE (node
), DECL_SOURCE_LINE (node
));
1439 else if (TYPE_P (node
))
1441 const char *what
= NULL
;
1442 tree decl
= TYPE_STUB_DECL (node
);
1444 if (TREE_CODE (TYPE_NAME (node
)) == IDENTIFIER_NODE
)
1445 what
= IDENTIFIER_POINTER (TYPE_NAME (node
));
1446 else if (TREE_CODE (TYPE_NAME (node
)) == TYPE_DECL
1447 && DECL_NAME (TYPE_NAME (node
)))
1448 what
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node
)));
1453 warning ("`%s' is deprecated (declared at %s:%d)", what
,
1454 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
));
1456 warning ("`%s' is deprecated", what
);
1459 warning ("type is deprecated (declared at %s:%d)",
1460 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
));
1462 warning ("type is deprecated");
1466 /* Really call the system 'abort'. This has to go right at the end of
1467 this file, so that there are no functions after it that call abort
1468 and get the system abort instead of our macro. */