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. */
36 #include "diagnostic.h"
37 #include "langhooks.h"
38 #include "langhooks-def.h"
40 #define output_text_length(BUFFER) (BUFFER)->line_length
41 #define is_starting_newline(BUFFER) (output_text_length (BUFFER) == 0)
42 #define line_wrap_cutoff(BUFFER) (BUFFER)->state.maximum_length
43 #define prefix_was_emitted_for(BUFFER) (BUFFER)->state.emitted_prefix_p
46 static void output_flush
PARAMS ((output_buffer
*));
47 static void output_do_verbatim
PARAMS ((output_buffer
*, text_info
*));
48 static void output_buffer_to_stream
PARAMS ((output_buffer
*));
49 static void output_format
PARAMS ((output_buffer
*, text_info
*));
50 static void output_indent
PARAMS ((output_buffer
*));
52 static char *vbuild_message_string
PARAMS ((const char *, va_list))
53 ATTRIBUTE_PRINTF (1, 0);
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
*));
83 extern int rtl_dump_and_exit
;
84 extern int warnings_are_errors
;
86 /* A diagnostic_context surrogate for stderr. */
87 static diagnostic_context global_diagnostic_context
;
88 diagnostic_context
*global_dc
= &global_diagnostic_context
;
91 /* Subroutine of output_set_maximum_length. Set up BUFFER's
92 internal maximum characters per line. */
94 set_real_maximum_length (buffer
)
95 output_buffer
*buffer
;
97 /* If we're told not to wrap lines then do the obvious thing. In case
98 we'll emit prefix only once per diagnostic message, it is appropriate
99 not to increase unnecessarily the line-length cut-off. */
100 if (!output_is_line_wrapping (buffer
)
101 || output_prefixing_rule (buffer
) == DIAGNOSTICS_SHOW_PREFIX_ONCE
102 || output_prefixing_rule (buffer
) == DIAGNOSTICS_SHOW_PREFIX_NEVER
)
103 line_wrap_cutoff (buffer
) = output_line_cutoff (buffer
);
106 int prefix_length
= buffer
->state
.prefix
?
107 strlen (buffer
->state
.prefix
) : 0;
108 /* If the prefix is ridiculously too long, output at least
110 if (output_line_cutoff (buffer
) - prefix_length
< 32)
111 line_wrap_cutoff (buffer
) = output_line_cutoff (buffer
) + 32;
113 line_wrap_cutoff (buffer
) = output_line_cutoff (buffer
);
117 /* Sets the number of maximum characters per line BUFFER can output
118 in line-wrapping mode. A LENGTH value 0 suppresses line-wrapping. */
120 output_set_maximum_length (buffer
, length
)
121 output_buffer
*buffer
;
124 output_line_cutoff (buffer
) = length
;
125 set_real_maximum_length (buffer
);
128 /* Sets BUFFER's PREFIX. */
130 output_set_prefix (buffer
, prefix
)
131 output_buffer
*buffer
;
134 buffer
->state
.prefix
= prefix
;
135 set_real_maximum_length (buffer
);
136 prefix_was_emitted_for (buffer
) = false;
137 output_indentation (buffer
) = 0;
140 /* Return a pointer to the last character emitted in the output
141 BUFFER area. A NULL pointer means no character available. */
143 output_last_position (buffer
)
144 const output_buffer
*buffer
;
146 const char *p
= NULL
;
148 if (obstack_base (&buffer
->obstack
) != obstack_next_free (&buffer
->obstack
))
149 p
= ((const char *) obstack_next_free (&buffer
->obstack
)) - 1;
153 /* Free BUFFER's prefix, a previously malloc'd string. */
155 output_destroy_prefix (buffer
)
156 output_buffer
*buffer
;
158 if (buffer
->state
.prefix
!= NULL
)
160 free ((char *) buffer
->state
.prefix
);
161 buffer
->state
.prefix
= NULL
;
165 /* Zero out any text output so far in BUFFER. */
167 output_clear_message_text (buffer
)
168 output_buffer
*buffer
;
170 obstack_free (&buffer
->obstack
, obstack_base (&buffer
->obstack
));
171 output_text_length (buffer
) = 0;
174 /* Zero out any formatting data used so far by BUFFER. */
176 output_clear_data (buffer
)
177 output_buffer
*buffer
;
179 prefix_was_emitted_for (buffer
) = false;
180 output_indentation (buffer
) = 0;
183 /* Construct an output BUFFER with PREFIX and of MAXIMUM_LENGTH
184 characters per line. */
186 init_output_buffer (buffer
, prefix
, maximum_length
)
187 output_buffer
*buffer
;
191 memset (buffer
, 0, sizeof (output_buffer
));
192 obstack_init (&buffer
->obstack
);
193 output_buffer_attached_stream (buffer
) = stderr
;
194 output_line_cutoff (buffer
) = maximum_length
;
195 output_prefixing_rule (buffer
) = diagnostic_prefixing_rule (global_dc
);
196 output_set_prefix (buffer
, prefix
);
197 output_text_length (buffer
) = 0;
198 output_clear_data (buffer
);
201 /* Reinitialize BUFFER. */
203 output_clear (buffer
)
204 output_buffer
*buffer
;
206 output_clear_message_text (buffer
);
207 output_clear_data (buffer
);
210 /* Finishes constructing a NULL-terminated character string representing
211 the BUFFERed message. */
213 output_finalize_message (buffer
)
214 output_buffer
*buffer
;
216 obstack_1grow (&buffer
->obstack
, '\0');
217 return output_message_text (buffer
);
220 /* Return the amount of characters BUFFER can accept to
223 output_space_left (buffer
)
224 const output_buffer
*buffer
;
226 return line_wrap_cutoff (buffer
) - output_text_length (buffer
);
229 /* Write out BUFFER's prefix. */
231 output_emit_prefix (buffer
)
232 output_buffer
*buffer
;
234 if (buffer
->state
.prefix
!= NULL
)
236 switch (output_prefixing_rule (buffer
))
239 case DIAGNOSTICS_SHOW_PREFIX_NEVER
:
242 case DIAGNOSTICS_SHOW_PREFIX_ONCE
:
243 if (prefix_was_emitted_for (buffer
))
245 output_indent (buffer
);
248 output_indentation (buffer
) += 3;
251 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE
:
253 int prefix_length
= strlen (buffer
->state
.prefix
);
254 output_append_r (buffer
, buffer
->state
.prefix
, prefix_length
);
255 prefix_was_emitted_for (buffer
) = true;
262 /* Have BUFFER start a new line. */
264 output_add_newline (buffer
)
265 output_buffer
*buffer
;
267 obstack_1grow (&buffer
->obstack
, '\n');
268 output_text_length (buffer
) = 0;
271 /* Appends a character to BUFFER. */
273 output_add_character (buffer
, c
)
274 output_buffer
*buffer
;
277 if (output_is_line_wrapping (buffer
) && output_space_left (buffer
) <= 0)
278 output_add_newline (buffer
);
279 obstack_1grow (&buffer
->obstack
, c
);
280 ++output_text_length (buffer
);
283 /* Adds a space to BUFFER. */
285 output_add_space (buffer
)
286 output_buffer
*buffer
;
288 if (output_is_line_wrapping (buffer
) && output_space_left (buffer
) <= 0)
290 output_add_newline (buffer
);
293 obstack_1grow (&buffer
->obstack
, ' ');
294 ++output_text_length (buffer
);
297 /* These functions format an INTEGER into BUFFER as suggested by their
300 output_decimal (buffer
, i
)
301 output_buffer
*buffer
;
304 output_formatted_scalar (buffer
, "%d", i
);
308 output_long_decimal (buffer
, i
)
309 output_buffer
*buffer
;
312 output_formatted_scalar (buffer
, "%ld", i
);
316 output_unsigned_decimal (buffer
, i
)
317 output_buffer
*buffer
;
320 output_formatted_scalar (buffer
, "%u", i
);
324 output_long_unsigned_decimal (buffer
, i
)
325 output_buffer
*buffer
;
328 output_formatted_scalar (buffer
, "%lu", i
);
332 output_octal (buffer
, i
)
333 output_buffer
*buffer
;
336 output_formatted_scalar (buffer
, "%o", i
);
340 output_long_octal (buffer
, i
)
341 output_buffer
*buffer
;
344 output_formatted_scalar (buffer
, "%lo", i
);
348 output_hexadecimal (buffer
, i
)
349 output_buffer
*buffer
;
352 output_formatted_scalar (buffer
, "%x", i
);
356 output_long_hexadecimal (buffer
, i
)
357 output_buffer
*buffer
;
360 output_formatted_scalar (buffer
, "%lx", i
);
363 /* Append to BUFFER a string specified by its STARTING character
366 output_append_r (buffer
, start
, length
)
367 output_buffer
*buffer
;
371 obstack_grow (&buffer
->obstack
, start
, length
);
372 output_text_length (buffer
) += length
;
375 /* Append a string deliminated by START and END to BUFFER. No wrapping is
376 done. However, if beginning a new line then emit BUFFER->state.prefix
377 and skip any leading whitespace if appropriate. The caller must ensure
378 that it is safe to do so. */
380 output_append (buffer
, start
, end
)
381 output_buffer
*buffer
;
385 /* Emit prefix and skip whitespace if we're starting a new line. */
386 if (is_starting_newline (buffer
))
388 output_emit_prefix (buffer
);
389 if (output_is_line_wrapping (buffer
))
390 while (start
!= end
&& *start
== ' ')
393 output_append_r (buffer
, start
, end
- start
);
397 output_indent (buffer
)
398 output_buffer
*buffer
;
400 int n
= output_indentation (buffer
);
403 for (i
= 0; i
< n
; ++i
)
404 output_add_character (buffer
, ' ');
407 /* Wrap a text delimited by START and END into BUFFER. */
409 wrap_text (buffer
, start
, end
)
410 output_buffer
*buffer
;
414 bool is_wrapping
= output_is_line_wrapping (buffer
);
418 /* Dump anything bordered by whitespaces. */
420 const char *p
= start
;
421 while (p
!= end
&& *p
!= ' ' && *p
!= '\n')
423 if (is_wrapping
&& p
- start
>= output_space_left (buffer
))
424 output_add_newline (buffer
);
425 output_append (buffer
, start
, p
);
429 if (start
!= end
&& *start
== ' ')
431 output_add_space (buffer
);
434 if (start
!= end
&& *start
== '\n')
436 output_add_newline (buffer
);
442 /* Same as wrap_text but wrap text only when in line-wrapping mode. */
444 maybe_wrap_text (buffer
, start
, end
)
445 output_buffer
*buffer
;
449 if (output_is_line_wrapping (buffer
))
450 wrap_text (buffer
, start
, end
);
452 output_append (buffer
, start
, end
);
456 /* Append a STRING to BUFFER; the STRING might be line-wrapped if in
459 output_add_string (buffer
, str
)
460 output_buffer
*buffer
;
463 maybe_wrap_text (buffer
, str
, str
+ (str
? strlen (str
) : 0));
466 /* Append an identifier ID to BUFFER. */
468 output_add_identifier (buffer
, id
)
469 output_buffer
*buffer
;
472 output_append (buffer
, IDENTIFIER_POINTER (id
),
473 IDENTIFIER_POINTER (id
) + IDENTIFIER_LENGTH (id
));
476 /* Flush the content of BUFFER onto the attached stream,
480 output_buffer_to_stream (buffer
)
481 output_buffer
*buffer
;
483 const char *text
= output_finalize_message (buffer
);
484 fputs (text
, output_buffer_attached_stream (buffer
));
485 output_clear_message_text (buffer
);
488 /* Format a message pointed to by TEXT. The following format specifiers are
489 recognized as being language independent:
490 %d, %i: (signed) integer in base ten.
491 %u: unsigned integer in base ten.
492 %o: unsigned integer in base eight.
493 %x: unsigned integer in base sixteen.
494 %ld, %li, %lo, %lu, %lx: long versions of the above.
498 %*.s: a substring the length of which is specified by an integer.
501 output_format (buffer
, text
)
502 output_buffer
*buffer
;
505 for (; *text
->format_spec
; ++text
->format_spec
)
507 bool long_integer
= 0;
511 const char *p
= text
->format_spec
;
512 while (*p
&& *p
!= '%')
514 wrap_text (buffer
, text
->format_spec
, p
);
515 text
->format_spec
= p
;
518 if (*text
->format_spec
== '\0')
521 /* We got a '%'. Let's see what happens. Record whether we're
522 parsing a long integer format specifier. */
523 if (*++text
->format_spec
== 'l')
529 /* Handle %c, %d, %i, %ld, %li, %lo, %lu, %lx, %o, %s, %u,
530 %x, %.*s; %%. And nothing else. Front-ends should install
531 printers to grok language specific format specifiers. */
532 switch (*text
->format_spec
)
535 output_add_character (buffer
, va_arg (*text
->args_ptr
, int));
541 output_long_decimal (buffer
, va_arg (*text
->args_ptr
, long int));
543 output_decimal (buffer
, va_arg (*text
->args_ptr
, int));
548 output_long_octal (buffer
,
549 va_arg (*text
->args_ptr
, unsigned long int));
551 output_octal (buffer
, va_arg (*text
->args_ptr
, unsigned int));
555 output_add_string (buffer
, va_arg (*text
->args_ptr
, const char *));
560 output_long_unsigned_decimal
561 (buffer
, va_arg (*text
->args_ptr
, long unsigned int));
563 output_unsigned_decimal
564 (buffer
, va_arg (*text
->args_ptr
, unsigned int));
569 output_long_hexadecimal
570 (buffer
, va_arg (*text
->args_ptr
, unsigned long int));
573 (buffer
, va_arg (*text
->args_ptr
, unsigned int));
577 output_add_character (buffer
, '%');
582 const location_t
*locus
= va_arg (*text
->args_ptr
, location_t
*);
583 output_add_string (buffer
, "file '");
584 output_add_string (buffer
, locus
->file
);
585 output_add_string (buffer
, "', line ");
586 output_decimal (buffer
, locus
->line
);
594 /* We handle no precision specifier but `%.*s'. */
595 if (*++text
->format_spec
!= '*')
597 else if (*++text
->format_spec
!= 's')
599 n
= va_arg (*text
->args_ptr
, int);
600 s
= va_arg (*text
->args_ptr
, const char *);
601 output_append (buffer
, s
, s
+ n
);
606 if (!buffer
->format_decoder
607 || !(*buffer
->format_decoder
) (buffer
, text
))
609 /* Hmmm. The front-end failed to install a format translator
610 but called us with an unrecognized format. Sorry. */
618 vbuild_message_string (msg
, ap
)
624 vasprintf (&str
, msg
, ap
);
628 /* Return a malloc'd string containing MSG formatted a la
629 printf. The caller is responsible for freeing the memory. */
631 build_message_string
VPARAMS ((const char *msg
, ...))
636 VA_FIXEDARG (ap
, const char *, msg
);
638 str
= vbuild_message_string (msg
, ap
);
645 /* Same as diagnsotic_build_prefix, but only the source FILE is given. */
647 file_name_as_prefix (f
)
650 return build_message_string ("%s: ", f
);
653 /* Format a message into BUFFER a la printf. */
655 output_printf
VPARAMS ((struct output_buffer
*buffer
, const char *msgid
, ...))
659 VA_FIXEDARG (ap
, output_buffer
*, buffer
);
660 VA_FIXEDARG (ap
, const char *, msgid
);
663 text
.format_spec
= _(msgid
);
664 output_format (buffer
, &text
);
668 /* Print a message relevant to the given DECL. */
670 format_with_decl (buffer
, text
, decl
)
671 output_buffer
*buffer
;
677 /* Do magic to get around lack of varargs support for insertion
678 of arguments into existing list. We know that the decl is first;
679 we ass_u_me that it will be printed with "%s". */
680 for (p
= text
->format_spec
; *p
; ++p
)
686 else if (*(p
+ 1) != 's')
693 /* Print the left-hand substring. */
694 maybe_wrap_text (buffer
, text
->format_spec
, p
);
696 if (*p
== '%') /* Print the name. */
698 const char *const n
= (DECL_NAME (decl
)
699 ? (*lang_hooks
.decl_printable_name
) (decl
, 2)
700 : _("((anonymous))"));
701 output_add_string (buffer
, n
);
705 if (ISALPHA (*(p
- 1) & 0xFF))
710 if (*p
) /* Print the rest of the message. */
712 text
->format_spec
= p
;
713 output_format (buffer
, text
);
717 /* Flush the content of BUFFER onto the attached stream. */
719 output_flush (buffer
)
720 output_buffer
*buffer
;
722 output_buffer_to_stream (buffer
);
723 output_clear_data (buffer
);
724 fputc ('\n', output_buffer_attached_stream (buffer
));
725 fflush (output_buffer_attached_stream (buffer
));
728 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
729 settings needed by BUFFER for a verbatim formatting. */
731 output_do_verbatim (buffer
, text
)
732 output_buffer
*buffer
;
735 diagnostic_prefixing_rule_t rule
= output_prefixing_rule (buffer
);
736 int line_cutoff
= output_line_cutoff (buffer
);
738 /* Set verbatim mode. */
739 output_prefixing_rule (buffer
) = DIAGNOSTICS_SHOW_PREFIX_NEVER
;
740 output_line_cutoff (buffer
) = 0;
741 /* Do the actual formatting. */
742 output_format (buffer
, text
);
743 /* Restore previous settings. */
744 output_prefixing_rule (buffer
) = rule
;
745 output_line_cutoff (buffer
) = line_cutoff
;
748 /* Output MESSAGE verbatim into BUFFER. */
750 output_verbatim
VPARAMS ((output_buffer
*buffer
, const char *msgid
, ...))
754 VA_FIXEDARG (ap
, output_buffer
*, buffer
);
755 VA_FIXEDARG (ap
, const char *, msgid
);
757 text
.format_spec
= msgid
;
759 output_do_verbatim (buffer
, &text
);
764 /* Initialize the diagnostic message outputting machinery. */
766 diagnostic_initialize (context
)
767 diagnostic_context
*context
;
769 memset (context
, 0, sizeof *context
);
770 obstack_init (&context
->buffer
.obstack
);
772 /* By default, diagnostics are sent to stderr. */
773 output_buffer_attached_stream (&context
->buffer
) = stderr
;
775 /* By default, we emit prefixes once per message. */
776 diagnostic_prefixing_rule (context
) = DIAGNOSTICS_SHOW_PREFIX_ONCE
;
778 diagnostic_starter (context
) = default_diagnostic_starter
;
779 diagnostic_finalizer (context
) = default_diagnostic_finalizer
;
780 context
->warnings_are_errors_message
= warnings_are_errors
;
783 /* Returns true if the next format specifier in TEXT is a format specifier
784 for a location_t. If so, update the object pointed by LOCUS to reflect
785 the specified location in *TEXT->args_ptr. */
787 text_specifies_location (text
, locus
)
792 /* Skip any leading text. */
793 for (p
= text
->format_spec
; *p
&& *p
!= '%'; ++p
)
796 /* Extract the location information if any. */
797 if (*p
== '%' && *++p
== 'H')
799 *locus
= *va_arg (*text
->args_ptr
, location_t
*);
800 text
->format_spec
= p
+ 1;
808 diagnostic_set_info (diagnostic
, msgid
, args
, file
, line
, kind
)
809 diagnostic_info
*diagnostic
;
816 diagnostic
->message
.format_spec
= msgid
;
817 diagnostic
->message
.args_ptr
= args
;
818 /* If the diagnostic message doesn't specify a loccation,
819 use FILE and LINE. */
820 if (!text_specifies_location (&diagnostic
->message
, &diagnostic
->location
))
822 diagnostic
->location
.file
= file
;
823 diagnostic
->location
.line
= line
;
825 diagnostic
->kind
= kind
;
828 /* Return a malloc'd string describing a location. The caller is
829 responsible for freeing the memory. */
831 diagnostic_build_prefix (diagnostic
)
832 diagnostic_info
*diagnostic
;
834 static const char *const diagnostic_kind_text
[] = {
835 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
836 #include "diagnostic.def"
837 #undef DEFINE_DIAGNOSTIC_KIND
840 if (diagnostic
->kind
>= DK_LAST_DIAGNOSTIC_KIND
)
843 return diagnostic
->location
.file
844 ? build_message_string ("%s:%d: %s",
845 diagnostic
->location
.file
,
846 diagnostic
->location
.line
,
847 _(diagnostic_kind_text
[diagnostic
->kind
]))
848 : build_message_string ("%s: %s", progname
,
849 _(diagnostic_kind_text
[diagnostic
->kind
]));
852 /* Report a diagnostic MESSAGE at the declaration DECL.
853 MSG is a format string which uses %s to substitute the declaration
854 name; subsequent substitutions are a la output_format. */
856 diagnostic_for_decl (diagnostic
, decl
)
857 diagnostic_info
*diagnostic
;
860 if (global_dc
->lock
++)
861 error_recursion (global_dc
);
863 if (diagnostic_count_diagnostic (global_dc
, diagnostic
->kind
))
865 diagnostic_report_current_function (global_dc
);
867 (&global_dc
->buffer
, diagnostic_build_prefix (diagnostic
));
868 format_with_decl (&global_dc
->buffer
, &diagnostic
->message
, decl
);
869 output_flush (&global_dc
->buffer
);
870 output_destroy_prefix (&global_dc
->buffer
);
876 diagnostic_flush_buffer (context
)
877 diagnostic_context
*context
;
879 output_buffer_to_stream (&context
->buffer
);
880 fflush (output_buffer_attached_stream (&context
->buffer
));
883 /* Count a diagnostic. Return true if the message should be printed. */
885 diagnostic_count_diagnostic (context
, kind
)
886 diagnostic_context
*context
;
895 case DK_FATAL
: case DK_ICE
: case DK_SORRY
:
896 case DK_ANACHRONISM
: case DK_NOTE
:
897 ++diagnostic_kind_count (context
, kind
);
901 if (!diagnostic_report_warnings_p ())
903 else if (!warnings_are_errors
)
905 ++diagnostic_kind_count (context
, DK_WARNING
);
908 /* else fall through. */
911 if (kind
== DK_WARNING
&& context
->warnings_are_errors_message
)
913 output_verbatim (&context
->buffer
,
914 "%s: warnings being treated as errors\n", progname
);
915 context
->warnings_are_errors_message
= false;
917 ++diagnostic_kind_count (context
, DK_ERROR
);
924 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
925 runs its second argument through gettext. */
927 fnotice
VPARAMS ((FILE *file
, const char *msgid
, ...))
930 VA_FIXEDARG (ap
, FILE *, file
);
931 VA_FIXEDARG (ap
, const char *, msgid
);
933 vfprintf (file
, _(msgid
), ap
);
938 /* Print a fatal I/O error message. Argument are like printf.
939 Also include a system error message based on `errno'. */
941 fatal_io_error
VPARAMS ((const char *msgid
, ...))
945 VA_FIXEDARG (ap
, const char *, msgid
);
947 text
.format_spec
= _(msgid
);
949 output_printf (&global_dc
->buffer
, "%s: %s: ", progname
, xstrerror (errno
));
950 output_format (&global_dc
->buffer
, &text
);
951 output_flush (&global_dc
->buffer
);
953 exit (FATAL_EXIT_CODE
);
956 /* Issue a pedantic warning MSGID. */
958 pedwarn
VPARAMS ((const char *msgid
, ...))
960 diagnostic_info diagnostic
;
962 VA_FIXEDARG (ap
, const char *, msgid
);
964 diagnostic_set_info (&diagnostic
, _(msgid
), &ap
, input_filename
, lineno
,
965 pedantic_error_kind ());
966 report_diagnostic (&diagnostic
);
970 /* Issue a pedantic warning about DECL. */
972 pedwarn_with_decl
VPARAMS ((tree decl
, const char *msgid
, ...))
974 diagnostic_info diagnostic
;
976 VA_FIXEDARG (ap
, tree
, decl
);
977 VA_FIXEDARG (ap
, const char *, msgid
);
979 diagnostic_set_info (&diagnostic
, _(msgid
), &ap
,
980 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
),
981 pedantic_error_kind ());
983 /* We don't want -pedantic-errors to cause the compilation to fail from
984 "errors" in system header files. Sometimes fixincludes can't fix what's
985 broken (eg: unsigned char bitfields - fixing it may change the alignment
986 which will cause programs to mysteriously fail because the C library
987 or kernel uses the original layout). There's no point in issuing a
988 warning either, it's just unnecessary noise. */
989 if (!DECL_IN_SYSTEM_HEADER (decl
))
990 diagnostic_for_decl (&diagnostic
, decl
);
994 /* Same as above but within the context FILE and LINE. */
996 pedwarn_with_file_and_line
VPARAMS ((const char *file
, int line
,
997 const char *msgid
, ...))
999 diagnostic_info diagnostic
;
1000 VA_OPEN (ap
, msgid
);
1001 VA_FIXEDARG (ap
, const char *, file
);
1002 VA_FIXEDARG (ap
, int, line
);
1003 VA_FIXEDARG (ap
, const char *, msgid
);
1005 diagnostic_set_info (&diagnostic
, _(msgid
), &ap
, file
, line
,
1006 pedantic_error_kind ());
1007 report_diagnostic (&diagnostic
);
1011 /* Just apologize with MSGID. */
1013 sorry
VPARAMS ((const char *msgid
, ...))
1015 diagnostic_info diagnostic
;
1017 VA_OPEN (ap
, msgid
);
1018 VA_FIXEDARG (ap
, const char *, msgid
);
1021 diagnostic_set_info (&diagnostic
, _(msgid
), &ap
,
1022 input_filename
, lineno
, DK_SORRY
);
1025 (&global_dc
->buffer
, diagnostic_build_prefix (&diagnostic
));
1026 output_printf (&global_dc
->buffer
, "sorry, not implemented: ");
1027 output_format (&global_dc
->buffer
, &diagnostic
.message
);
1028 output_flush (&global_dc
->buffer
);
1032 /* Called when the start of a function definition is parsed,
1033 this function prints on stderr the name of the function. */
1035 announce_function (decl
)
1040 if (rtl_dump_and_exit
)
1041 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl
)));
1043 verbatim (" %s", (*lang_hooks
.decl_printable_name
) (decl
, 2));
1045 output_needs_newline (&global_dc
->buffer
) = true;
1046 diagnostic_set_last_function (global_dc
);
1050 /* The default function to print out name of current function that caused
1053 lhd_print_error_function (context
, file
)
1054 diagnostic_context
*context
;
1057 if (diagnostic_last_function_changed (context
))
1059 const char *old_prefix
= output_prefix (&context
->buffer
);
1060 char *new_prefix
= file
? build_message_string ("%s: ", file
) : NULL
;
1062 output_set_prefix (&context
->buffer
, new_prefix
);
1064 if (current_function_decl
== NULL
)
1065 output_add_string (&context
->buffer
, _("At top level:"));
1068 if (TREE_CODE (TREE_TYPE (current_function_decl
)) == METHOD_TYPE
)
1070 (&context
->buffer
, "In member function `%s':",
1071 (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2));
1074 (&context
->buffer
, "In function `%s':",
1075 (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2));
1077 output_add_newline (&context
->buffer
);
1079 diagnostic_set_last_function (context
);
1080 output_buffer_to_stream (&context
->buffer
);
1081 context
->buffer
.state
.prefix
= old_prefix
;
1082 free ((char*) new_prefix
);
1086 /* Prints out, if necessary, the name of the current function
1087 that caused an error. Called from all error and warning functions.
1088 We ignore the FILE parameter, as it cannot be relied upon. */
1091 diagnostic_report_current_function (context
)
1092 diagnostic_context
*context
;
1094 diagnostic_report_current_module (context
);
1095 (*lang_hooks
.print_error_function
) (context
, input_filename
);
1099 error_with_file_and_line
VPARAMS ((const char *file
, int line
,
1100 const char *msgid
, ...))
1102 diagnostic_info diagnostic
;
1104 VA_OPEN (ap
, msgid
);
1105 VA_FIXEDARG (ap
, const char *, file
);
1106 VA_FIXEDARG (ap
, int, line
);
1107 VA_FIXEDARG (ap
, const char *, msgid
);
1109 diagnostic_set_info (&diagnostic
, msgid
, &ap
, file
, line
, DK_ERROR
);
1110 report_diagnostic (&diagnostic
);
1115 error_with_decl
VPARAMS ((tree decl
, const char *msgid
, ...))
1117 diagnostic_info diagnostic
;
1118 VA_OPEN (ap
, msgid
);
1119 VA_FIXEDARG (ap
, tree
, decl
);
1120 VA_FIXEDARG (ap
, const char *, msgid
);
1122 diagnostic_set_info (&diagnostic
, msgid
, &ap
,
1123 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
),
1125 diagnostic_for_decl (&diagnostic
, decl
);
1130 /* Report an error message. The arguments are like that of printf. */
1133 error
VPARAMS ((const char *msgid
, ...))
1135 diagnostic_info diagnostic
;
1137 VA_OPEN (ap
, msgid
);
1138 VA_FIXEDARG (ap
, const char *, msgid
);
1140 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, lineno
,
1142 report_diagnostic (&diagnostic
);
1146 /* Likewise, except that the compilation is terminated after printing the
1150 fatal_error
VPARAMS ((const char *msgid
, ...))
1152 diagnostic_info diagnostic
;
1154 VA_OPEN (ap
, msgid
);
1155 VA_FIXEDARG (ap
, const char *, msgid
);
1157 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, lineno
,
1159 report_diagnostic (&diagnostic
);
1162 fnotice (stderr
, "compilation terminated.\n");
1163 exit (FATAL_EXIT_CODE
);
1167 internal_error
VPARAMS ((const char *msgid
, ...))
1169 diagnostic_info diagnostic
;
1171 VA_OPEN (ap
, msgid
);
1172 VA_FIXEDARG (ap
, const char *, msgid
);
1174 if (global_dc
->lock
)
1175 error_recursion (global_dc
);
1177 #ifndef ENABLE_CHECKING
1178 if (errorcount
> 0 || sorrycount
> 0)
1180 fnotice (stderr
, "%s:%d: confused by earlier errors, bailing out\n",
1181 input_filename
, lineno
);
1182 exit (FATAL_EXIT_CODE
);
1186 if (global_dc
->internal_error
!= 0)
1187 (*global_dc
->internal_error
) (_(msgid
), &ap
);
1189 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, lineno
,
1191 report_diagnostic (&diagnostic
);
1195 "Please submit a full bug report,\n\
1196 with preprocessed source if appropriate.\n\
1197 See %s for instructions.\n", bug_report_url
);
1198 exit (FATAL_EXIT_CODE
);
1202 warning_with_file_and_line
VPARAMS ((const char *file
, int line
,
1203 const char *msgid
, ...))
1205 diagnostic_info diagnostic
;
1207 VA_OPEN (ap
, msgid
);
1208 VA_FIXEDARG (ap
, const char *, file
);
1209 VA_FIXEDARG (ap
, int, line
);
1210 VA_FIXEDARG (ap
, const char *, msgid
);
1212 diagnostic_set_info (&diagnostic
, msgid
, &ap
, file
, line
, DK_WARNING
);
1213 report_diagnostic (&diagnostic
);
1218 warning_with_decl
VPARAMS ((tree decl
, const char *msgid
, ...))
1220 diagnostic_info diagnostic
;
1221 VA_OPEN (ap
, msgid
);
1222 VA_FIXEDARG (ap
, tree
, decl
);
1223 VA_FIXEDARG (ap
, const char *, msgid
);
1225 diagnostic_set_info (&diagnostic
, msgid
, &ap
,
1226 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
),
1228 diagnostic_for_decl (&diagnostic
, decl
);
1233 warning
VPARAMS ((const char *msgid
, ...))
1235 diagnostic_info diagnostic
;
1237 VA_OPEN (ap
, msgid
);
1238 VA_FIXEDARG (ap
, const char *, msgid
);
1240 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, lineno
,
1242 report_diagnostic (&diagnostic
);
1247 /* Same as above but use diagnostic_buffer. */
1250 verbatim
VPARAMS ((const char *msgid
, ...))
1253 VA_OPEN (ap
, msgid
);
1254 VA_FIXEDARG (ap
, const char *, msgid
);
1256 text
.format_spec
= _(msgid
);
1257 text
.args_ptr
= &ap
;
1258 output_do_verbatim (&global_dc
->buffer
, &text
);
1259 output_buffer_to_stream (&global_dc
->buffer
);
1263 /* Report a diagnostic message (an error or a warning) as specified by
1264 DC. This function is *the* subroutine in terms of which front-ends
1265 should implement their specific diagnostic handling modules. The
1266 front-end independent format specifiers are exactly those described
1267 in the documentation of output_format. */
1270 diagnostic_report_diagnostic (context
, diagnostic
)
1271 diagnostic_context
*context
;
1272 diagnostic_info
*diagnostic
;
1274 if (context
->lock
++)
1275 error_recursion (context
);
1277 if (diagnostic_count_diagnostic (context
, diagnostic
->kind
))
1279 (*diagnostic_starter (context
)) (context
, diagnostic
);
1280 output_format (&context
->buffer
, &diagnostic
->message
);
1281 (*diagnostic_finalizer (context
)) (context
, diagnostic
);
1282 output_flush (&context
->buffer
);
1288 /* Inform the user that an error occurred while trying to report some
1289 other error. This indicates catastrophic internal inconsistencies,
1290 so give up now. But do try to flush out the previous error.
1291 This mustn't use internal_error, that will cause infinite recursion. */
1294 error_recursion (context
)
1295 diagnostic_context
*context
;
1297 if (context
->lock
< 3)
1298 output_flush (&context
->buffer
);
1301 "Internal compiler error: Error reporting routines re-entered.\n");
1303 "Please submit a full bug report,\n\
1304 with preprocessed source if appropriate.\n\
1305 See %s for instructions.\n", bug_report_url
);
1306 exit (FATAL_EXIT_CODE
);
1309 /* Given a partial pathname as input, return another pathname that
1310 shares no directory elements with the pathname of __FILE__. This
1311 is used by fancy_abort() to print `Internal compiler error in expr.c'
1312 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1315 trim_filename (name
)
1318 static const char this_file
[] = __FILE__
;
1319 const char *p
= name
, *q
= this_file
;
1321 /* First skip any "../" in each filename. This allows us to give a proper
1322 reference to a file in a subdirectory. */
1323 while (p
[0] == '.' && p
[1] == '.'
1324 && (p
[2] == DIR_SEPARATOR
1325 #ifdef DIR_SEPARATOR_2
1326 || p
[2] == DIR_SEPARATOR_2
1331 while (q
[0] == '.' && q
[1] == '.'
1332 && (q
[2] == DIR_SEPARATOR
1333 #ifdef DIR_SEPARATOR_2
1334 || p
[2] == DIR_SEPARATOR_2
1339 /* Now skip any parts the two filenames have in common. */
1340 while (*p
== *q
&& *p
!= 0 && *q
!= 0)
1343 /* Now go backwards until the previous directory separator. */
1344 while (p
> name
&& p
[-1] != DIR_SEPARATOR
1345 #ifdef DIR_SEPARATOR_2
1346 && p
[-1] != DIR_SEPARATOR_2
1354 /* Report an internal compiler error in a friendly manner and without
1358 fancy_abort (file
, line
, function
)
1361 const char *function
;
1363 internal_error ("in %s, at %s:%d", function
, trim_filename (file
), line
);
1367 diagnostic_report_current_module (context
)
1368 diagnostic_context
*context
;
1370 struct file_stack
*p
;
1372 if (output_needs_newline (&context
->buffer
))
1374 output_add_newline (&context
->buffer
);
1375 output_needs_newline (&context
->buffer
) = false;
1378 if (input_file_stack
&& input_file_stack
->next
!= 0
1379 && diagnostic_last_module_changed (context
))
1381 for (p
= input_file_stack
->next
; p
; p
= p
->next
)
1382 if (p
== input_file_stack
->next
)
1383 output_verbatim (&context
->buffer
,
1384 "In file included from %s:%d", p
->name
, p
->line
);
1386 output_verbatim (&context
->buffer
,
1387 ",\n from %s:%d", p
->name
, p
->line
);
1388 output_verbatim (&context
->buffer
, ":\n");
1389 diagnostic_set_last_module (context
);
1394 default_diagnostic_starter (context
, diagnostic
)
1395 diagnostic_context
*context
;
1396 diagnostic_info
*diagnostic
;
1398 diagnostic_report_current_function (context
);
1399 output_set_prefix (&context
->buffer
, diagnostic_build_prefix (diagnostic
));
1403 default_diagnostic_finalizer (context
, diagnostic
)
1404 diagnostic_context
*context
;
1405 diagnostic_info
*diagnostic
__attribute__((unused
));
1407 output_destroy_prefix (&context
->buffer
);
1411 inform
VPARAMS ((const char *msgid
, ...))
1413 diagnostic_info diagnostic
;
1415 VA_OPEN (ap
, msgid
);
1416 VA_FIXEDARG (ap
, const char *, msgid
);
1418 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_filename
, lineno
,
1420 report_diagnostic (&diagnostic
);
1425 warn_deprecated_use (node
)
1428 if (node
== 0 || !warn_deprecated_decl
)
1432 warning ("`%s' is deprecated (declared at %s:%d)",
1433 IDENTIFIER_POINTER (DECL_NAME (node
)),
1434 DECL_SOURCE_FILE (node
), DECL_SOURCE_LINE (node
));
1435 else if (TYPE_P (node
))
1437 const char *what
= NULL
;
1438 tree decl
= TYPE_STUB_DECL (node
);
1440 if (TREE_CODE (TYPE_NAME (node
)) == IDENTIFIER_NODE
)
1441 what
= IDENTIFIER_POINTER (TYPE_NAME (node
));
1442 else if (TREE_CODE (TYPE_NAME (node
)) == TYPE_DECL
1443 && DECL_NAME (TYPE_NAME (node
)))
1444 what
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node
)));
1449 warning ("`%s' is deprecated (declared at %s:%d)", what
,
1450 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
));
1452 warning ("`%s' is deprecated", what
);
1455 warning ("type is deprecated (declared at %s:%d)",
1456 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
));
1458 warning ("type is deprecated");