1 /* Language-independent diagnostic subroutines for the GNU C compiler
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 obstack_chunk_alloc xmalloc
41 #define obstack_chunk_free free
43 #define output_formatted_integer(BUFFER, FORMAT, INTEGER) \
46 sprintf ((BUFFER)->digit_buffer, FORMAT, INTEGER); \
47 output_add_string (BUFFER, (BUFFER)->digit_buffer); \
51 #define output_text_length(BUFFER) (BUFFER)->line_length
52 #define is_starting_newline(BUFFER) (output_text_length (BUFFER) == 0)
53 #define output_prefix(BUFFER) (BUFFER)->state.prefix
54 #define line_wrap_cutoff(BUFFER) (BUFFER)->state.maximum_length
55 #define prefix_was_emitted_for(BUFFER) (BUFFER)->state.emitted_prefix_p
56 #define output_buffer_ptr_to_format_args(BUFFER) (BUFFER)->state.format_args
58 #define diagnostic_args output_buffer_ptr_to_format_args (diagnostic_buffer)
59 #define diagnostic_msg output_buffer_text_cursor (diagnostic_buffer)
62 static void output_flush
PARAMS ((output_buffer
*));
63 static void output_do_verbatim
PARAMS ((output_buffer
*,
64 const char *, va_list *));
65 static void output_buffer_to_stream
PARAMS ((output_buffer
*));
66 static void output_format
PARAMS ((output_buffer
*));
67 static void output_indent
PARAMS ((output_buffer
*));
69 static char *vbuild_message_string
PARAMS ((const char *, va_list))
70 ATTRIBUTE_PRINTF (1, 0);
71 static char *build_message_string
PARAMS ((const char *, ...))
73 static void output_do_printf
PARAMS ((output_buffer
*, const char *))
74 ATTRIBUTE_PRINTF (2, 0);
75 static void format_with_decl
PARAMS ((output_buffer
*, tree
));
76 static void diagnostic_for_decl
PARAMS ((tree
, const char *, va_list *, int));
77 static void set_real_maximum_length
PARAMS ((output_buffer
*));
79 static void output_unsigned_decimal
PARAMS ((output_buffer
*, unsigned int));
80 static void output_long_decimal
PARAMS ((output_buffer
*, long int));
81 static void output_long_unsigned_decimal
PARAMS ((output_buffer
*,
83 static void output_octal
PARAMS ((output_buffer
*, unsigned int));
84 static void output_long_octal
PARAMS ((output_buffer
*, unsigned long int));
85 static void output_hexadecimal
PARAMS ((output_buffer
*, unsigned int));
86 static void output_long_hexadecimal
PARAMS ((output_buffer
*,
88 static void output_append_r
PARAMS ((output_buffer
*, const char *, int));
89 static void wrap_text
PARAMS ((output_buffer
*, const char *, const char *));
90 static void maybe_wrap_text
PARAMS ((output_buffer
*, const char *,
92 static void output_clear_data
PARAMS ((output_buffer
*));
94 static void default_diagnostic_starter
PARAMS ((output_buffer
*,
95 diagnostic_context
*));
96 static void default_diagnostic_finalizer
PARAMS ((output_buffer
*,
97 diagnostic_context
*));
99 static void error_recursion
PARAMS ((void)) ATTRIBUTE_NORETURN
;
101 extern int rtl_dump_and_exit
;
102 extern int warnings_are_errors
;
104 /* A diagnostic_context surrogate for stderr. */
105 static diagnostic_context global_diagnostic_context
;
106 diagnostic_context
*global_dc
= &global_diagnostic_context
;
108 /* Function of last error message;
109 more generally, function such that if next error message is in it
110 then we don't have to mention the function name. */
111 static tree last_error_function
= NULL
;
113 /* Used to detect when input_file_stack has changed since last described. */
114 static int last_error_tick
;
116 /* Prevent recursion into the error handler. */
117 static int diagnostic_lock
;
120 /* Return truthvalue if current input file is different from the most recent
121 file involved in a diagnostic message. */
124 error_module_changed ()
126 return last_error_tick
!= input_file_stack_tick
;
129 /* Remember current file as being the most recent file involved in a
130 diagnostic message. */
133 record_last_error_module ()
135 last_error_tick
= input_file_stack_tick
;
138 /* Same as error_module_changed, but for function. */
141 error_function_changed ()
143 return last_error_function
!= current_function_decl
;
146 /* Same as record_last_error_module, but for function. */
149 record_last_error_function ()
151 last_error_function
= current_function_decl
;
154 /* Initialize the diagnostic message outputting machinery. */
157 diagnostic_initialize (context
)
158 diagnostic_context
*context
;
160 memset (context
, 0, sizeof *context
);
161 obstack_init (&context
->buffer
.obstack
);
163 /* By default, diagnostics are sent to stderr. */
164 output_buffer_attached_stream (&context
->buffer
) = stderr
;
166 /* By default, we emit prefixes once per message. */
167 diagnostic_prefixing_rule (context
) = DIAGNOSTICS_SHOW_PREFIX_ONCE
;
169 diagnostic_starter (context
) = default_diagnostic_starter
;
170 diagnostic_finalizer (context
) = default_diagnostic_finalizer
;
173 /* Returns true if BUFFER is in line-wrapping mode. */
176 output_is_line_wrapping (buffer
)
177 output_buffer
*buffer
;
179 return output_line_cutoff (buffer
) > 0;
182 /* Return BUFFER's prefix. */
185 output_get_prefix (buffer
)
186 const output_buffer
*buffer
;
188 return output_prefix (buffer
);
191 /* Subroutine of output_set_maximum_length. Set up BUFFER's
192 internal maximum characters per line. */
195 set_real_maximum_length (buffer
)
196 output_buffer
*buffer
;
198 /* If we're told not to wrap lines then do the obvious thing. In case
199 we'll emit prefix only once per diagnostic message, it is appropriate
200 not to increase unnecessarily the line-length cut-off. */
201 if (! output_is_line_wrapping (buffer
)
202 || output_prefixing_rule (buffer
) == DIAGNOSTICS_SHOW_PREFIX_ONCE
203 || output_prefixing_rule (buffer
) == DIAGNOSTICS_SHOW_PREFIX_NEVER
)
204 line_wrap_cutoff (buffer
) = output_line_cutoff (buffer
);
208 output_prefix (buffer
) ? strlen (output_prefix (buffer
)) : 0;
209 /* If the prefix is ridiculously too long, output at least
211 if (output_line_cutoff (buffer
) - prefix_length
< 32)
212 line_wrap_cutoff (buffer
) = output_line_cutoff (buffer
) + 32;
214 line_wrap_cutoff (buffer
) = output_line_cutoff (buffer
);
218 /* Sets the number of maximum characters per line BUFFER can output
219 in line-wrapping mode. A LENGTH value 0 suppresses line-wrapping. */
222 output_set_maximum_length (buffer
, length
)
223 output_buffer
*buffer
;
226 output_line_cutoff (buffer
) = length
;
227 set_real_maximum_length (buffer
);
230 /* Sets BUFFER's PREFIX. */
233 output_set_prefix (buffer
, prefix
)
234 output_buffer
*buffer
;
237 output_prefix (buffer
) = prefix
;
238 set_real_maximum_length (buffer
);
239 prefix_was_emitted_for (buffer
) = 0;
240 output_indentation (buffer
) = 0;
243 /* Return a pointer to the last character emitted in the output
244 BUFFER area. A NULL pointer means no character available. */
246 output_last_position (buffer
)
247 const output_buffer
*buffer
;
249 const char *p
= NULL
;
251 if (obstack_base (&buffer
->obstack
) != obstack_next_free (&buffer
->obstack
))
252 p
= ((const char *) obstack_next_free (&buffer
->obstack
)) - 1;
256 /* Free BUFFER's prefix, a previously malloc'd string. */
259 output_destroy_prefix (buffer
)
260 output_buffer
*buffer
;
262 if (output_prefix (buffer
) != NULL
)
264 free ((char *) output_prefix (buffer
));
265 output_prefix (buffer
) = NULL
;
269 /* Zero out any text output so far in BUFFER. */
272 output_clear_message_text (buffer
)
273 output_buffer
*buffer
;
275 obstack_free (&buffer
->obstack
, obstack_base (&buffer
->obstack
));
276 output_text_length (buffer
) = 0;
279 /* Zero out any formatting data used so far by BUFFER. */
282 output_clear_data (buffer
)
283 output_buffer
*buffer
;
285 output_buffer_text_cursor (buffer
) = NULL
;
286 output_buffer_ptr_to_format_args (buffer
) = NULL
;
287 prefix_was_emitted_for (buffer
) = false;
288 output_indentation (buffer
) = 0;
291 /* Construct an output BUFFER with PREFIX and of MAXIMUM_LENGTH
292 characters per line. */
295 init_output_buffer (buffer
, prefix
, maximum_length
)
296 output_buffer
*buffer
;
300 memset (buffer
, 0, sizeof (output_buffer
));
301 obstack_init (&buffer
->obstack
);
302 output_buffer_attached_stream (buffer
) = stderr
;
303 output_line_cutoff (buffer
) = maximum_length
;
304 output_prefixing_rule (buffer
) = diagnostic_prefixing_rule (global_dc
);
305 output_set_prefix (buffer
, prefix
);
306 output_text_length (buffer
) = 0;
307 output_clear_data (buffer
);
310 /* Reinitialize BUFFER. */
313 output_clear (buffer
)
314 output_buffer
*buffer
;
316 output_clear_message_text (buffer
);
317 output_clear_data (buffer
);
320 /* Finishes constructing a NULL-terminated character string representing
321 the BUFFERed message. */
324 output_finalize_message (buffer
)
325 output_buffer
*buffer
;
327 obstack_1grow (&buffer
->obstack
, '\0');
328 return output_message_text (buffer
);
332 flush_diagnostic_buffer ()
334 output_buffer_to_stream (diagnostic_buffer
);
335 fflush (output_buffer_attached_stream (diagnostic_buffer
));
338 /* Return the amount of characters BUFFER can accept to
342 output_space_left (buffer
)
343 const output_buffer
*buffer
;
345 return line_wrap_cutoff (buffer
) - output_text_length (buffer
);
348 /* Write out BUFFER's prefix. */
351 output_emit_prefix (buffer
)
352 output_buffer
*buffer
;
354 if (output_prefix (buffer
) != NULL
)
356 switch (output_prefixing_rule (buffer
))
359 case DIAGNOSTICS_SHOW_PREFIX_NEVER
:
362 case DIAGNOSTICS_SHOW_PREFIX_ONCE
:
363 if (prefix_was_emitted_for (buffer
))
365 output_indent (buffer
);
368 output_indentation (buffer
) += 3;
371 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE
:
373 int prefix_length
= strlen (output_prefix (buffer
));
374 output_append_r (buffer
, output_prefix (buffer
), prefix_length
);
375 prefix_was_emitted_for (buffer
) = 1;
382 /* Have BUFFER start a new line. */
385 output_add_newline (buffer
)
386 output_buffer
*buffer
;
388 obstack_1grow (&buffer
->obstack
, '\n');
389 output_text_length (buffer
) = 0;
392 /* Appends a character to BUFFER. */
395 output_add_character (buffer
, c
)
396 output_buffer
*buffer
;
399 if (output_is_line_wrapping (buffer
) && output_space_left (buffer
) <= 0)
400 output_add_newline (buffer
);
401 obstack_1grow (&buffer
->obstack
, c
);
402 ++output_text_length (buffer
);
405 /* Adds a space to BUFFER. */
408 output_add_space (buffer
)
409 output_buffer
*buffer
;
411 if (output_is_line_wrapping (buffer
) && output_space_left (buffer
) <= 0)
413 output_add_newline (buffer
);
416 obstack_1grow (&buffer
->obstack
, ' ');
417 ++output_text_length (buffer
);
420 /* These functions format an INTEGER into BUFFER as suggested by their
424 output_decimal (buffer
, i
)
425 output_buffer
*buffer
;
428 output_formatted_integer (buffer
, "%d", i
);
432 output_long_decimal (buffer
, i
)
433 output_buffer
*buffer
;
436 output_formatted_integer (buffer
, "%ld", i
);
440 output_unsigned_decimal (buffer
, i
)
441 output_buffer
*buffer
;
444 output_formatted_integer (buffer
, "%u", i
);
448 output_long_unsigned_decimal (buffer
, i
)
449 output_buffer
*buffer
;
452 output_formatted_integer (buffer
, "%lu", i
);
456 output_octal (buffer
, i
)
457 output_buffer
*buffer
;
460 output_formatted_integer (buffer
, "%o", i
);
464 output_long_octal (buffer
, i
)
465 output_buffer
*buffer
;
468 output_formatted_integer (buffer
, "%lo", i
);
472 output_hexadecimal (buffer
, i
)
473 output_buffer
*buffer
;
476 output_formatted_integer (buffer
, "%x", i
);
480 output_long_hexadecimal (buffer
, i
)
481 output_buffer
*buffer
;
484 output_formatted_integer (buffer
, "%lx", i
);
487 /* Append to BUFFER a string specified by its STARTING character
491 output_append_r (buffer
, start
, length
)
492 output_buffer
*buffer
;
496 obstack_grow (&buffer
->obstack
, start
, length
);
497 output_text_length (buffer
) += length
;
500 /* Append a string deliminated by START and END to BUFFER. No wrapping is
501 done. However, if beginning a new line then emit output_prefix (BUFFER)
502 and skip any leading whitespace if appropriate. The caller must ensure
503 that it is safe to do so. */
506 output_append (buffer
, start
, end
)
507 output_buffer
*buffer
;
511 /* Emit prefix and skip whitespace if we're starting a new line. */
512 if (is_starting_newline (buffer
))
514 output_emit_prefix (buffer
);
515 if (output_is_line_wrapping (buffer
))
516 while (start
!= end
&& *start
== ' ')
519 output_append_r (buffer
, start
, end
- start
);
523 output_indent (buffer
)
524 output_buffer
*buffer
;
526 int n
= output_indentation (buffer
);
529 for (i
= 0; i
< n
; ++i
)
530 output_add_character (buffer
, ' ');
533 /* Wrap a text delimited by START and END into BUFFER. */
536 wrap_text (buffer
, start
, end
)
537 output_buffer
*buffer
;
541 int is_wrapping
= output_is_line_wrapping (buffer
);
545 /* Dump anything bordered by whitespaces. */
547 const char *p
= start
;
548 while (p
!= end
&& *p
!= ' ' && *p
!= '\n')
550 if (is_wrapping
&& p
- start
>= output_space_left (buffer
))
551 output_add_newline (buffer
);
552 output_append (buffer
, start
, p
);
556 if (start
!= end
&& *start
== ' ')
558 output_add_space (buffer
);
561 if (start
!= end
&& *start
== '\n')
563 output_add_newline (buffer
);
569 /* Same as wrap_text but wrap text only when in line-wrapping mode. */
572 maybe_wrap_text (buffer
, start
, end
)
573 output_buffer
*buffer
;
577 if (output_is_line_wrapping (buffer
))
578 wrap_text (buffer
, start
, end
);
580 output_append (buffer
, start
, end
);
584 /* Append a STRING to BUFFER; the STRING might be line-wrapped if in
588 output_add_string (buffer
, str
)
589 output_buffer
*buffer
;
592 maybe_wrap_text (buffer
, str
, str
+ (str
? strlen (str
) : 0));
595 /* Flush the content of BUFFER onto the attached stream,
599 output_buffer_to_stream (buffer
)
600 output_buffer
*buffer
;
602 const char *text
= output_finalize_message (buffer
);
603 fputs (text
, output_buffer_attached_stream (buffer
));
604 output_clear_message_text (buffer
);
607 /* Format a message pointed to by output_buffer_text_cursor (BUFFER) using
608 output_buffer_format_args (BUFFER) as appropriate. The following format
609 specifiers are recognized as being language independent:
610 %d, %i: (signed) integer in base ten.
611 %u: unsigned integer in base ten.
612 %o: unsigned integer in base eight.
613 %x: unsigned integer in base sixteen.
614 %ld, %li, %lo, %lu, %lx: long versions of the above.
618 %*.s: a substring the length of which is specified by an integer. */
621 output_format (buffer
)
622 output_buffer
*buffer
;
624 for (; *output_buffer_text_cursor (buffer
);
625 ++output_buffer_text_cursor (buffer
))
627 int long_integer
= 0;
631 const char *p
= output_buffer_text_cursor (buffer
);
632 while (*p
&& *p
!= '%')
634 wrap_text (buffer
, output_buffer_text_cursor (buffer
), p
);
635 output_buffer_text_cursor (buffer
) = p
;
638 if (!*output_buffer_text_cursor (buffer
))
641 /* We got a '%'. Let's see what happens. Record whether we're
642 parsing a long integer format specifier. */
643 if (*++output_buffer_text_cursor (buffer
) == 'l')
646 ++output_buffer_text_cursor (buffer
);
649 /* Handle %c, %d, %i, %ld, %li, %lo, %lu, %lx, %o, %s, %u,
650 %x, %.*s; %%. And nothing else. Front-ends should install
651 printers to grok language specific format specifiers. */
652 switch (*output_buffer_text_cursor (buffer
))
656 (buffer
, va_arg (output_buffer_format_args (buffer
), int));
663 (buffer
, va_arg (output_buffer_format_args (buffer
), long int));
666 (buffer
, va_arg (output_buffer_format_args (buffer
), int));
671 output_long_octal (buffer
,
672 va_arg (output_buffer_format_args (buffer
),
675 output_octal (buffer
,
676 va_arg (output_buffer_format_args (buffer
),
681 output_add_string (buffer
,
682 va_arg (output_buffer_format_args (buffer
),
688 output_long_unsigned_decimal
689 (buffer
, va_arg (output_buffer_format_args (buffer
),
692 output_unsigned_decimal
693 (buffer
, va_arg (output_buffer_format_args (buffer
),
699 output_long_hexadecimal
700 (buffer
, va_arg (output_buffer_format_args (buffer
),
704 (buffer
, va_arg (output_buffer_format_args (buffer
),
709 output_add_character (buffer
, '%');
716 /* We handle no precision specifier but `%.*s'. */
717 if (*++output_buffer_text_cursor (buffer
) != '*')
719 else if (*++output_buffer_text_cursor (buffer
) != 's')
721 n
= va_arg (output_buffer_format_args (buffer
), int);
722 s
= va_arg (output_buffer_format_args (buffer
), const char *);
723 output_append (buffer
, s
, s
+ n
);
728 if (!buffer
->format_decoder
|| !(*buffer
->format_decoder
) (buffer
))
730 /* Hmmm. The front-end failed to install a format translator
731 but called us with an unrecognized format. Sorry. */
739 vbuild_message_string (msg
, ap
)
745 vasprintf (&str
, msg
, ap
);
749 /* Return a malloc'd string containing MSG formatted a la
750 printf. The caller is responsible for freeing the memory. */
753 build_message_string
VPARAMS ((const char *msg
, ...))
758 VA_FIXEDARG (ap
, const char *, msg
);
760 str
= vbuild_message_string (msg
, ap
);
767 /* Return a malloc'd string describing a location. The caller is
768 responsible for freeing the memory. */
771 context_as_prefix (file
, line
, warn
)
779 return build_message_string (_("%s:%d: warning: "), file
, line
);
781 return build_message_string ("%s:%d: ", file
, line
);
786 return build_message_string (_("%s: warning: "), progname
);
788 return build_message_string ("%s: ", progname
);
792 /* Same as context_as_prefix, but only the source FILE is given. */
795 file_name_as_prefix (f
)
798 return build_message_string ("%s: ", f
);
801 /* Format a MESSAGE into BUFFER. Automatically wrap lines. */
804 output_do_printf (buffer
, msg
)
805 output_buffer
*buffer
;
808 char *message
= vbuild_message_string (msg
,
809 output_buffer_format_args (buffer
));
811 wrap_text (buffer
, message
, message
+ strlen (message
));
816 /* Format a message into BUFFER a la printf. */
819 output_printf
VPARAMS ((struct output_buffer
*buffer
, const char *msgid
, ...))
824 VA_FIXEDARG (ap
, output_buffer
*, buffer
);
825 VA_FIXEDARG (ap
, const char *, msgid
);
827 old_args
= output_buffer_ptr_to_format_args (buffer
);
828 output_buffer_ptr_to_format_args (buffer
) = &ap
;
829 output_do_printf (buffer
, _(msgid
));
830 output_buffer_ptr_to_format_args (buffer
) = old_args
;
834 /* Print a message relevant to the given DECL. */
837 format_with_decl (buffer
, decl
)
838 output_buffer
*buffer
;
843 /* Do magic to get around lack of varargs support for insertion
844 of arguments into existing list. We know that the decl is first;
845 we ass_u_me that it will be printed with "%s". */
846 for (p
= output_buffer_text_cursor (buffer
); *p
; ++p
)
852 else if (*(p
+ 1) != 's')
859 /* Print the left-hand substring. */
860 maybe_wrap_text (buffer
, output_buffer_text_cursor (buffer
), p
);
862 if (*p
== '%') /* Print the name. */
864 const char *const n
= (DECL_NAME (decl
)
865 ? (*lang_hooks
.decl_printable_name
) (decl
, 2)
866 : _("((anonymous))"));
867 output_add_string (buffer
, n
);
871 if (ISALPHA (*(p
- 1) & 0xFF))
876 if (*p
) /* Print the rest of the message. */
878 output_buffer_text_cursor (buffer
) = p
;
879 output_format (buffer
);
884 /* Report a diagnostic MESSAGE at the declaration DECL.
885 MSG is a format string which uses %s to substitute the declaration
886 name; subsequent substitutions are a la output_format. */
889 diagnostic_for_decl (decl
, msgid
, args_ptr
, warn
)
897 if (diagnostic_lock
++)
900 if (count_error (warn
))
902 os
= output_buffer_state (diagnostic_buffer
);
903 report_error_function (DECL_SOURCE_FILE (decl
));
905 (diagnostic_buffer
, context_as_prefix
906 (DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
), warn
));
907 output_buffer_ptr_to_format_args (diagnostic_buffer
) = args_ptr
;
908 output_buffer_text_cursor (diagnostic_buffer
) = _(msgid
);
909 format_with_decl (diagnostic_buffer
, decl
);
910 output_flush (&global_dc
->buffer
);
911 output_destroy_prefix (diagnostic_buffer
);
913 output_buffer_state (diagnostic_buffer
) = os
;
918 /* Flush the content of BUFFER onto the attached stream. */
921 output_flush (buffer
)
922 output_buffer
*buffer
;
924 output_buffer_to_stream (buffer
);
925 output_clear_data (buffer
);
926 fputc ('\n', output_buffer_attached_stream (buffer
));
927 fflush (output_buffer_attached_stream (buffer
));
930 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
931 settings needed by BUFFER for a verbatim formatting. */
934 output_do_verbatim (buffer
, msgid
, args_ptr
)
935 output_buffer
*buffer
;
941 os
= output_buffer_state (buffer
);
942 output_prefix (buffer
) = NULL
;
943 output_prefixing_rule (buffer
) = DIAGNOSTICS_SHOW_PREFIX_NEVER
;
944 output_buffer_text_cursor (buffer
) = _(msgid
);
945 output_buffer_ptr_to_format_args (buffer
) = args_ptr
;
946 output_set_maximum_length (buffer
, 0);
947 output_format (buffer
);
948 output_buffer_state (buffer
) = os
;
951 /* Output MESSAGE verbatim into BUFFER. */
954 output_verbatim
VPARAMS ((output_buffer
*buffer
, const char *msgid
, ...))
957 VA_FIXEDARG (ap
, output_buffer
*, buffer
);
958 VA_FIXEDARG (ap
, const char *, msgid
);
960 output_do_verbatim (buffer
, msgid
, &ap
);
964 /* Count an error or warning. Return 1 if the message should be printed. */
967 count_error (warningp
)
970 if (warningp
&& !diagnostic_report_warnings_p ())
973 if (warningp
&& !warnings_are_errors
)
977 static int warning_message
= 0;
979 if (warningp
&& !warning_message
)
981 verbatim ("%s: warnings being treated as errors\n", progname
);
990 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
991 runs its second argument through gettext. */
994 fnotice
VPARAMS ((FILE *file
, const char *msgid
, ...))
997 VA_FIXEDARG (ap
, FILE *, file
);
998 VA_FIXEDARG (ap
, const char *, msgid
);
1000 vfprintf (file
, _(msgid
), ap
);
1005 /* Print a fatal I/O error message. Argument are like printf.
1006 Also include a system error message based on `errno'. */
1009 fatal_io_error
VPARAMS ((const char *msgid
, ...))
1013 VA_OPEN (ap
, msgid
);
1014 VA_FIXEDARG (ap
, const char *, msgid
);
1016 os
= output_buffer_state (diagnostic_buffer
);
1018 output_printf (diagnostic_buffer
, "%s: %s: ", progname
, xstrerror (errno
));
1019 output_buffer_ptr_to_format_args (diagnostic_buffer
) = &ap
;
1020 output_buffer_text_cursor (diagnostic_buffer
) = _(msgid
);
1021 output_format (diagnostic_buffer
);
1022 output_flush (&global_dc
->buffer
);
1023 output_buffer_state (diagnostic_buffer
) = os
;
1025 exit (FATAL_EXIT_CODE
);
1028 /* Issue a pedantic warning MSGID. */
1031 pedwarn
VPARAMS ((const char *msgid
, ...))
1033 diagnostic_context dc
;
1035 VA_OPEN (ap
, msgid
);
1036 VA_FIXEDARG (ap
, const char *, msgid
);
1038 set_diagnostic_context
1039 (&dc
, msgid
, &ap
, input_filename
, lineno
, !flag_pedantic_errors
);
1040 report_diagnostic (&dc
);
1044 /* Issue a pedantic warning about DECL. */
1047 pedwarn_with_decl
VPARAMS ((tree decl
, const char *msgid
, ...))
1049 VA_OPEN (ap
, msgid
);
1050 VA_FIXEDARG (ap
, tree
, decl
);
1051 VA_FIXEDARG (ap
, const char *, msgid
);
1053 /* We don't want -pedantic-errors to cause the compilation to fail from
1054 "errors" in system header files. Sometimes fixincludes can't fix what's
1055 broken (eg: unsigned char bitfields - fixing it may change the alignment
1056 which will cause programs to mysteriously fail because the C library
1057 or kernel uses the original layout). There's no point in issuing a
1058 warning either, it's just unnecessary noise. */
1059 if (!DECL_IN_SYSTEM_HEADER (decl
))
1060 diagnostic_for_decl (decl
, msgid
, &ap
, !flag_pedantic_errors
);
1064 /* Same as above but within the context FILE and LINE. */
1067 pedwarn_with_file_and_line
VPARAMS ((const char *file
, int line
,
1068 const char *msgid
, ...))
1070 diagnostic_context dc
;
1072 VA_OPEN (ap
, msgid
);
1073 VA_FIXEDARG (ap
, const char *, file
);
1074 VA_FIXEDARG (ap
, int, line
);
1075 VA_FIXEDARG (ap
, const char *, msgid
);
1077 set_diagnostic_context (&dc
, msgid
, &ap
, file
, line
, !flag_pedantic_errors
);
1078 report_diagnostic (&dc
);
1082 /* Just apologize with MSGID. */
1085 sorry
VPARAMS ((const char *msgid
, ...))
1089 VA_OPEN (ap
, msgid
);
1090 VA_FIXEDARG (ap
, const char *, msgid
);
1093 os
= output_buffer_state (diagnostic_buffer
);
1096 (diagnostic_buffer
, context_as_prefix (input_filename
, lineno
, 0));
1097 output_printf (diagnostic_buffer
, "sorry, not implemented: ");
1098 output_buffer_ptr_to_format_args (diagnostic_buffer
) = &ap
;
1099 output_buffer_text_cursor (diagnostic_buffer
) = _(msgid
);
1100 output_format (diagnostic_buffer
);
1101 output_flush (&global_dc
->buffer
);
1102 output_buffer_state (diagnostic_buffer
) = os
;
1106 /* Called when the start of a function definition is parsed,
1107 this function prints on stderr the name of the function. */
1110 announce_function (decl
)
1115 if (rtl_dump_and_exit
)
1116 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl
)));
1118 verbatim (" %s", (*lang_hooks
.decl_printable_name
) (decl
, 2));
1120 output_needs_newline (diagnostic_buffer
) = 1;
1121 record_last_error_function ();
1125 /* The default function to print out name of current function that caused
1129 lhd_print_error_function (context
, file
)
1130 diagnostic_context
*context
;
1133 if (error_function_changed ())
1135 char *prefix
= file
? build_message_string ("%s: ", file
) : NULL
;
1138 os
= diagnostic_state (context
);
1139 output_set_prefix ((output_buffer
*) context
, prefix
);
1141 if (current_function_decl
== NULL
)
1142 output_add_string ((output_buffer
*) context
, _("At top level:"));
1145 if (TREE_CODE (TREE_TYPE (current_function_decl
)) == METHOD_TYPE
)
1147 ((output_buffer
*) context
, "In member function `%s':",
1148 (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2));
1151 ((output_buffer
*) context
, "In function `%s':",
1152 (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2));
1154 output_add_newline ((output_buffer
*) context
);
1156 record_last_error_function ();
1157 output_buffer_to_stream ((output_buffer
*) context
);
1158 diagnostic_state (context
) = os
;
1159 free ((char*) prefix
);
1163 /* Prints out, if necessary, the name of the current function
1164 that caused an error. Called from all error and warning functions.
1165 We ignore the FILE parameter, as it cannot be relied upon. */
1168 report_error_function (file
)
1169 const char *file ATTRIBUTE_UNUSED
;
1171 report_problematic_module ((output_buffer
*) global_dc
);
1172 (*lang_hooks
.print_error_function
) (global_dc
, input_filename
);
1176 error_with_file_and_line
VPARAMS ((const char *file
, int line
,
1177 const char *msgid
, ...))
1179 diagnostic_context dc
;
1181 VA_OPEN (ap
, msgid
);
1182 VA_FIXEDARG (ap
, const char *, file
);
1183 VA_FIXEDARG (ap
, int, line
);
1184 VA_FIXEDARG (ap
, const char *, msgid
);
1186 set_diagnostic_context (&dc
, msgid
, &ap
, file
, line
, /* warn = */ 0);
1187 report_diagnostic (&dc
);
1192 error_with_decl
VPARAMS ((tree decl
, const char *msgid
, ...))
1194 VA_OPEN (ap
, msgid
);
1195 VA_FIXEDARG (ap
, tree
, decl
);
1196 VA_FIXEDARG (ap
, const char *, msgid
);
1198 diagnostic_for_decl (decl
, msgid
, &ap
, /* warn = */ 0);
1203 /* Report an error message. The arguments are like that of printf. */
1206 error
VPARAMS ((const char *msgid
, ...))
1208 diagnostic_context dc
;
1210 VA_OPEN (ap
, msgid
);
1211 VA_FIXEDARG (ap
, const char *, msgid
);
1213 set_diagnostic_context
1214 (&dc
, msgid
, &ap
, input_filename
, lineno
, /* warn = */ 0);
1215 report_diagnostic (&dc
);
1219 /* Likewise, except that the compilation is terminated after printing the
1223 fatal_error
VPARAMS ((const char *msgid
, ...))
1225 diagnostic_context dc
;
1227 VA_OPEN (ap
, msgid
);
1228 VA_FIXEDARG (ap
, const char *, msgid
);
1230 set_diagnostic_context
1231 (&dc
, msgid
, &ap
, input_filename
, lineno
, /* warn = */ 0);
1232 report_diagnostic (&dc
);
1235 fnotice (stderr
, "compilation terminated.\n");
1236 exit (FATAL_EXIT_CODE
);
1240 internal_error
VPARAMS ((const char *msgid
, ...))
1242 diagnostic_context dc
;
1244 VA_OPEN (ap
, msgid
);
1245 VA_FIXEDARG (ap
, const char *, msgid
);
1247 if (diagnostic_lock
)
1250 #ifndef ENABLE_CHECKING
1251 if (errorcount
> 0 || sorrycount
> 0)
1253 fnotice (stderr
, "%s:%d: confused by earlier errors, bailing out\n",
1254 input_filename
, lineno
);
1255 exit (FATAL_EXIT_CODE
);
1259 if (global_dc
->internal_error
!= 0)
1260 (*global_dc
->internal_error
) (_(msgid
), &ap
);
1262 set_diagnostic_context
1263 (&dc
, msgid
, &ap
, input_filename
, lineno
, /* warn = */0);
1264 report_diagnostic (&dc
);
1268 "Please submit a full bug report,\n\
1269 with preprocessed source if appropriate.\n\
1270 See %s for instructions.\n", GCCBUGURL
);
1271 exit (FATAL_EXIT_CODE
);
1275 warning_with_file_and_line
VPARAMS ((const char *file
, int line
,
1276 const char *msgid
, ...))
1278 diagnostic_context dc
;
1280 VA_OPEN (ap
, msgid
);
1281 VA_FIXEDARG (ap
, const char *, file
);
1282 VA_FIXEDARG (ap
, int, line
);
1283 VA_FIXEDARG (ap
, const char *, msgid
);
1285 set_diagnostic_context (&dc
, msgid
, &ap
, file
, line
, /* warn = */ 1);
1286 report_diagnostic (&dc
);
1291 warning_with_decl
VPARAMS ((tree decl
, const char *msgid
, ...))
1293 VA_OPEN (ap
, msgid
);
1294 VA_FIXEDARG (ap
, tree
, decl
);
1295 VA_FIXEDARG (ap
, const char *, msgid
);
1297 diagnostic_for_decl (decl
, msgid
, &ap
, /* warn = */ 1);
1302 warning
VPARAMS ((const char *msgid
, ...))
1304 diagnostic_context dc
;
1306 VA_OPEN (ap
, msgid
);
1307 VA_FIXEDARG (ap
, const char *, msgid
);
1309 set_diagnostic_context
1310 (&dc
, msgid
, &ap
, input_filename
, lineno
, /* warn = */ 1);
1311 report_diagnostic (&dc
);
1316 /* Same as above but use diagnostic_buffer. */
1319 verbatim
VPARAMS ((const char *msgid
, ...))
1321 VA_OPEN (ap
, msgid
);
1322 VA_FIXEDARG (ap
, const char *, msgid
);
1324 output_do_verbatim (diagnostic_buffer
, msgid
, &ap
);
1325 output_buffer_to_stream (diagnostic_buffer
);
1329 /* Report a diagnostic message (an error or a warning) as specified by
1330 DC. This function is *the* subroutine in terms of which front-ends
1331 should implement their specific diagnostic handling modules. The
1332 front-end independent format specifiers are exactly those described
1333 in the documentation of output_format. */
1336 report_diagnostic (dc
)
1337 diagnostic_context
*dc
;
1341 if (diagnostic_lock
++)
1344 if (count_error (diagnostic_is_warning (dc
)))
1346 os
= output_buffer_state (diagnostic_buffer
);
1347 diagnostic_msg
= diagnostic_message (dc
);
1348 diagnostic_args
= diagnostic_argument_list (dc
);
1349 (*diagnostic_starter (dc
)) (diagnostic_buffer
, dc
);
1350 output_format (diagnostic_buffer
);
1351 (*diagnostic_finalizer (dc
)) (diagnostic_buffer
, dc
);
1352 output_flush (&global_dc
->buffer
);
1353 output_buffer_state (diagnostic_buffer
) = os
;
1359 /* Inform the user that an error occurred while trying to report some
1360 other error. This indicates catastrophic internal inconsistencies,
1361 so give up now. But do try to flush out the previous error.
1362 This mustn't use internal_error, that will cause infinite recursion. */
1367 if (diagnostic_lock
< 3)
1368 output_flush (&global_dc
->buffer
);
1371 "Internal compiler error: Error reporting routines re-entered.\n");
1373 "Please submit a full bug report,\n\
1374 with preprocessed source if appropriate.\n\
1375 See %s for instructions.\n", GCCBUGURL
);
1376 exit (FATAL_EXIT_CODE
);
1379 /* Given a partial pathname as input, return another pathname that
1380 shares no directory elements with the pathname of __FILE__. This
1381 is used by fancy_abort() to print `Internal compiler error in expr.c'
1382 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1385 trim_filename (name
)
1388 static const char this_file
[] = __FILE__
;
1389 const char *p
= name
, *q
= this_file
;
1391 /* First skip any "../" in each filename. This allows us to give a proper
1392 reference to a file in a subdirectory. */
1393 while (p
[0] == '.' && p
[1] == '.'
1394 && (p
[2] == DIR_SEPARATOR
1395 #ifdef DIR_SEPARATOR_2
1396 || p
[2] == DIR_SEPARATOR_2
1401 while (q
[0] == '.' && q
[1] == '.'
1402 && (q
[2] == DIR_SEPARATOR
1403 #ifdef DIR_SEPARATOR_2
1404 || p
[2] == DIR_SEPARATOR_2
1409 /* Now skip any parts the two filenames have in common. */
1410 while (*p
== *q
&& *p
!= 0 && *q
!= 0)
1413 /* Now go backwards until the previous directory separator. */
1414 while (p
> name
&& p
[-1] != DIR_SEPARATOR
1415 #ifdef DIR_SEPARATOR_2
1416 && p
[-1] != DIR_SEPARATOR_2
1424 /* Report an internal compiler error in a friendly manner and without
1428 fancy_abort (file
, line
, function
)
1431 const char *function
;
1433 internal_error ("Internal compiler error in %s, at %s:%d",
1434 function
, trim_filename (file
), line
);
1437 /* Setup DC for reporting a diagnostic MESSAGE (an error or a WARNING),
1438 using arguments pointed to by ARGS_PTR, issued at a location specified
1439 by FILE and LINE. */
1442 set_diagnostic_context (dc
, msgid
, args_ptr
, file
, line
, warn
)
1443 diagnostic_context
*dc
;
1450 memset (dc
, 0, sizeof (diagnostic_context
));
1451 diagnostic_message (dc
) = _(msgid
);
1452 diagnostic_argument_list (dc
) = args_ptr
;
1453 diagnostic_file_location (dc
) = file
;
1454 diagnostic_line_location (dc
) = line
;
1455 diagnostic_is_warning (dc
) = warn
;
1456 diagnostic_starter (dc
) = diagnostic_starter (global_dc
);
1457 diagnostic_finalizer (dc
) = diagnostic_finalizer (global_dc
);
1461 report_problematic_module (buffer
)
1462 output_buffer
*buffer
;
1464 struct file_stack
*p
;
1466 if (output_needs_newline (buffer
))
1468 output_add_newline (buffer
);
1469 output_needs_newline (buffer
) = 0;
1472 if (input_file_stack
&& input_file_stack
->next
!= 0
1473 && error_module_changed ())
1475 for (p
= input_file_stack
->next
; p
; p
= p
->next
)
1476 if (p
== input_file_stack
->next
)
1478 (buffer
, "In file included from %s:%d", p
->name
, p
->line
);
1481 (buffer
, ",\n from %s:%d", p
->name
, p
->line
);
1482 output_verbatim (buffer
, ":\n");
1483 record_last_error_module ();
1488 default_diagnostic_starter (buffer
, dc
)
1489 output_buffer
*buffer
;
1490 diagnostic_context
*dc
;
1492 report_error_function (diagnostic_file_location (dc
));
1493 output_set_prefix (buffer
,
1494 context_as_prefix (diagnostic_file_location (dc
),
1495 diagnostic_line_location (dc
),
1496 diagnostic_is_warning (dc
)));
1500 default_diagnostic_finalizer (buffer
, dc
)
1501 output_buffer
*buffer
;
1502 diagnostic_context
*dc
__attribute__((__unused__
));
1504 output_destroy_prefix (buffer
);
1508 warn_deprecated_use (node
)
1511 if (node
== 0 || !warn_deprecated_decl
)
1515 warning ("`%s' is deprecated (declared at %s:%d)",
1516 IDENTIFIER_POINTER (DECL_NAME (node
)),
1517 DECL_SOURCE_FILE (node
), DECL_SOURCE_LINE (node
));
1518 else if (TYPE_P (node
))
1520 const char *what
= NULL
;
1521 tree decl
= TYPE_STUB_DECL (node
);
1523 if (TREE_CODE (TYPE_NAME (node
)) == IDENTIFIER_NODE
)
1524 what
= IDENTIFIER_POINTER (TYPE_NAME (node
));
1525 else if (TREE_CODE (TYPE_NAME (node
)) == TYPE_DECL
1526 && DECL_NAME (TYPE_NAME (node
)))
1527 what
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node
)));
1532 warning ("`%s' is deprecated (declared at %s:%d)", what
,
1533 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
));
1535 warning ("`%s' is deprecated", what
);
1538 warning ("type is deprecated (declared at %s:%d)",
1539 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
));
1541 warning ("type is deprecated");