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) \
45 sprintf ((BUFFER)->digit_buffer, FORMAT, INTEGER); \
46 output_add_string (BUFFER, (BUFFER)->digit_buffer); \
49 #define output_text_length(BUFFER) (BUFFER)->line_length
50 #define is_starting_newline(BUFFER) (output_text_length (BUFFER) == 0)
51 #define output_prefix(BUFFER) (BUFFER)->state.prefix
52 #define line_wrap_cutoff(BUFFER) (BUFFER)->state.maximum_length
53 #define prefix_was_emitted_for(BUFFER) (BUFFER)->state.emitted_prefix_p
54 #define output_buffer_ptr_to_format_args(BUFFER) (BUFFER)->state.format_args
56 #define diagnostic_args output_buffer_ptr_to_format_args (diagnostic_buffer)
57 #define diagnostic_msg output_buffer_text_cursor (diagnostic_buffer)
60 static void diagnostic_finish
PARAMS ((output_buffer
*));
61 static void output_do_verbatim
PARAMS ((output_buffer
*,
62 const char *, va_list *));
63 static void output_buffer_to_stream
PARAMS ((output_buffer
*));
64 static void output_format
PARAMS ((output_buffer
*));
65 static void output_indent
PARAMS ((output_buffer
*));
67 static char *vbuild_message_string
PARAMS ((const char *, va_list))
68 ATTRIBUTE_PRINTF (1, 0);
69 static char *build_message_string
PARAMS ((const char *, ...))
71 static void output_do_printf
PARAMS ((output_buffer
*, const char *))
72 ATTRIBUTE_PRINTF (2, 0);
73 static void format_with_decl
PARAMS ((output_buffer
*, tree
));
74 static void diagnostic_for_decl
PARAMS ((tree
, const char *, va_list *, int));
75 static void set_real_maximum_length
PARAMS ((output_buffer
*));
77 static void output_unsigned_decimal
PARAMS ((output_buffer
*, unsigned int));
78 static void output_long_decimal
PARAMS ((output_buffer
*, long int));
79 static void output_long_unsigned_decimal
PARAMS ((output_buffer
*,
81 static void output_octal
PARAMS ((output_buffer
*, unsigned int));
82 static void output_long_octal
PARAMS ((output_buffer
*, unsigned long int));
83 static void output_hexadecimal
PARAMS ((output_buffer
*, unsigned int));
84 static void output_long_hexadecimal
PARAMS ((output_buffer
*,
86 static void output_append_r
PARAMS ((output_buffer
*, const char *, int));
87 static void wrap_text
PARAMS ((output_buffer
*, const char *, const char *));
88 static void maybe_wrap_text
PARAMS ((output_buffer
*, const char *,
90 static void clear_diagnostic_info
PARAMS ((output_buffer
*));
92 static void default_diagnostic_starter
PARAMS ((output_buffer
*,
93 diagnostic_context
*));
94 static void default_diagnostic_finalizer
PARAMS ((output_buffer
*,
95 diagnostic_context
*));
97 static void error_recursion
PARAMS ((void)) ATTRIBUTE_NORETURN
;
99 extern int rtl_dump_and_exit
;
100 extern int warnings_are_errors
;
102 /* A diagnostic_context surrogate for stderr. */
103 static diagnostic_context global_diagnostic_context
;
104 diagnostic_context
*global_dc
= &global_diagnostic_context
;
106 /* Function of last error message;
107 more generally, function such that if next error message is in it
108 then we don't have to mention the function name. */
109 static tree last_error_function
= NULL
;
111 /* Used to detect when input_file_stack has changed since last described. */
112 static int last_error_tick
;
114 /* Prevent recursion into the error handler. */
115 static int diagnostic_lock
;
118 /* Return truthvalue if current input file is different from the most recent
119 file involved in a diagnostic message. */
122 error_module_changed ()
124 return last_error_tick
!= input_file_stack_tick
;
127 /* Remember current file as being the most recent file involved in a
128 diagnostic message. */
131 record_last_error_module ()
133 last_error_tick
= input_file_stack_tick
;
136 /* Same as error_module_changed, but for function. */
139 error_function_changed ()
141 return last_error_function
!= current_function_decl
;
144 /* Same as record_last_error_module, but for function. */
147 record_last_error_function ()
149 last_error_function
= current_function_decl
;
152 /* Initialize the diagnostic message outputting machinery. */
155 diagnostic_initialize (context
)
156 diagnostic_context
*context
;
158 memset (context
, 0, sizeof *context
);
159 obstack_init (&context
->buffer
.obstack
);
161 /* By default, diagnostics are sent to stderr. */
162 output_buffer_attached_stream (&context
->buffer
) = stderr
;
164 /* By default, we emit prefixes once per message. */
165 diagnostic_prefixing_rule (context
) = DIAGNOSTICS_SHOW_PREFIX_ONCE
;
167 diagnostic_starter (context
) = default_diagnostic_starter
;
168 diagnostic_finalizer (context
) = default_diagnostic_finalizer
;
171 /* Returns true if BUFFER is in line-wrapping mode. */
174 output_is_line_wrapping (buffer
)
175 output_buffer
*buffer
;
177 return output_line_cutoff (buffer
) > 0;
180 /* Return BUFFER's prefix. */
183 output_get_prefix (buffer
)
184 const output_buffer
*buffer
;
186 return output_prefix (buffer
);
189 /* Subroutine of output_set_maximum_length. Set up BUFFER's
190 internal maximum characters per line. */
193 set_real_maximum_length (buffer
)
194 output_buffer
*buffer
;
196 /* If we're told not to wrap lines then do the obvious thing. In case
197 we'll emit prefix only once per diagnostic message, it is appropriate
198 not to increase unnecessarily the line-length cut-off. */
199 if (! output_is_line_wrapping (buffer
)
200 || output_prefixing_rule (buffer
) == DIAGNOSTICS_SHOW_PREFIX_ONCE
201 || output_prefixing_rule (buffer
) == DIAGNOSTICS_SHOW_PREFIX_NEVER
)
202 line_wrap_cutoff (buffer
) = output_line_cutoff (buffer
);
206 output_prefix (buffer
) ? strlen (output_prefix (buffer
)) : 0;
207 /* If the prefix is ridiculously too long, output at least
209 if (output_line_cutoff (buffer
) - prefix_length
< 32)
210 line_wrap_cutoff (buffer
) = output_line_cutoff (buffer
) + 32;
212 line_wrap_cutoff (buffer
) = output_line_cutoff (buffer
);
216 /* Sets the number of maximum characters per line BUFFER can output
217 in line-wrapping mode. A LENGTH value 0 suppresses line-wrapping. */
220 output_set_maximum_length (buffer
, length
)
221 output_buffer
*buffer
;
224 output_line_cutoff (buffer
) = length
;
225 set_real_maximum_length (buffer
);
228 /* Sets BUFFER's PREFIX. */
231 output_set_prefix (buffer
, prefix
)
232 output_buffer
*buffer
;
235 output_prefix (buffer
) = prefix
;
236 set_real_maximum_length (buffer
);
237 prefix_was_emitted_for (buffer
) = 0;
238 output_indentation (buffer
) = 0;
241 /* Return a pointer to the last character emitted in the output
242 BUFFER area. A NULL pointer means no character available. */
244 output_last_position (buffer
)
245 const output_buffer
*buffer
;
247 const char *p
= NULL
;
249 if (obstack_base (&buffer
->obstack
) != obstack_next_free (&buffer
->obstack
))
250 p
= ((const char *) obstack_next_free (&buffer
->obstack
)) - 1;
254 /* Free BUFFER's prefix, a previously malloc'd string. */
257 output_destroy_prefix (buffer
)
258 output_buffer
*buffer
;
260 if (output_prefix (buffer
) != NULL
)
262 free ((char *) output_prefix (buffer
));
263 output_prefix (buffer
) = NULL
;
267 /* Zero out any text output so far in BUFFER. */
270 output_clear_message_text (buffer
)
271 output_buffer
*buffer
;
273 obstack_free (&buffer
->obstack
, obstack_base (&buffer
->obstack
));
274 output_text_length (buffer
) = 0;
277 /* Zero out any diagnostic data used so far by BUFFER. */
280 clear_diagnostic_info (buffer
)
281 output_buffer
*buffer
;
283 output_buffer_text_cursor (buffer
) = NULL
;
284 output_buffer_ptr_to_format_args (buffer
) = NULL
;
285 prefix_was_emitted_for (buffer
) = 0;
286 output_indentation (buffer
) = 0;
289 /* Construct an output BUFFER with PREFIX and of MAXIMUM_LENGTH
290 characters per line. */
293 init_output_buffer (buffer
, prefix
, maximum_length
)
294 output_buffer
*buffer
;
298 memset (buffer
, 0, sizeof (output_buffer
));
299 obstack_init (&buffer
->obstack
);
300 output_buffer_attached_stream (buffer
) = stderr
;
301 output_line_cutoff (buffer
) = maximum_length
;
302 output_prefixing_rule (buffer
) = diagnostic_prefixing_rule (global_dc
);
303 output_set_prefix (buffer
, prefix
);
304 output_text_length (buffer
) = 0;
305 clear_diagnostic_info (buffer
);
308 /* Reinitialize BUFFER. */
311 output_clear (buffer
)
312 output_buffer
*buffer
;
314 output_clear_message_text (buffer
);
315 clear_diagnostic_info (buffer
);
318 /* Finishes constructing a NULL-terminated character string representing
319 the BUFFERed message. */
322 output_finalize_message (buffer
)
323 output_buffer
*buffer
;
325 obstack_1grow (&buffer
->obstack
, '\0');
326 return output_message_text (buffer
);
330 flush_diagnostic_buffer ()
332 output_buffer_to_stream (diagnostic_buffer
);
333 fflush (output_buffer_attached_stream (diagnostic_buffer
));
336 /* Return the amount of characters BUFFER can accept to
340 output_space_left (buffer
)
341 const output_buffer
*buffer
;
343 return line_wrap_cutoff (buffer
) - output_text_length (buffer
);
346 /* Write out BUFFER's prefix. */
349 output_emit_prefix (buffer
)
350 output_buffer
*buffer
;
352 if (output_prefix (buffer
) != NULL
)
354 switch (output_prefixing_rule (buffer
))
357 case DIAGNOSTICS_SHOW_PREFIX_NEVER
:
360 case DIAGNOSTICS_SHOW_PREFIX_ONCE
:
361 if (prefix_was_emitted_for (buffer
))
363 output_indent (buffer
);
366 output_indentation (buffer
) += 3;
369 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE
:
371 int prefix_length
= strlen (output_prefix (buffer
));
372 output_append_r (buffer
, output_prefix (buffer
), prefix_length
);
373 prefix_was_emitted_for (buffer
) = 1;
380 /* Have BUFFER start a new line. */
383 output_add_newline (buffer
)
384 output_buffer
*buffer
;
386 obstack_1grow (&buffer
->obstack
, '\n');
387 output_text_length (buffer
) = 0;
390 /* Appends a character to BUFFER. */
393 output_add_character (buffer
, c
)
394 output_buffer
*buffer
;
397 if (output_is_line_wrapping (buffer
) && output_space_left (buffer
) <= 0)
398 output_add_newline (buffer
);
399 obstack_1grow (&buffer
->obstack
, c
);
400 ++output_text_length (buffer
);
403 /* Adds a space to BUFFER. */
406 output_add_space (buffer
)
407 output_buffer
*buffer
;
409 if (output_is_line_wrapping (buffer
) && output_space_left (buffer
) <= 0)
411 output_add_newline (buffer
);
414 obstack_1grow (&buffer
->obstack
, ' ');
415 ++output_text_length (buffer
);
418 /* These functions format an INTEGER into BUFFER as suggested by their
422 output_decimal (buffer
, i
)
423 output_buffer
*buffer
;
426 output_formatted_integer (buffer
, "%d", i
);
430 output_long_decimal (buffer
, i
)
431 output_buffer
*buffer
;
434 output_formatted_integer (buffer
, "%ld", i
);
438 output_unsigned_decimal (buffer
, i
)
439 output_buffer
*buffer
;
442 output_formatted_integer (buffer
, "%u", i
);
446 output_long_unsigned_decimal (buffer
, i
)
447 output_buffer
*buffer
;
450 output_formatted_integer (buffer
, "%lu", i
);
454 output_octal (buffer
, i
)
455 output_buffer
*buffer
;
458 output_formatted_integer (buffer
, "%o", i
);
462 output_long_octal (buffer
, i
)
463 output_buffer
*buffer
;
466 output_formatted_integer (buffer
, "%lo", i
);
470 output_hexadecimal (buffer
, i
)
471 output_buffer
*buffer
;
474 output_formatted_integer (buffer
, "%x", i
);
478 output_long_hexadecimal (buffer
, i
)
479 output_buffer
*buffer
;
482 output_formatted_integer (buffer
, "%lx", i
);
485 /* Append to BUFFER a string specified by its STARTING character
489 output_append_r (buffer
, start
, length
)
490 output_buffer
*buffer
;
494 obstack_grow (&buffer
->obstack
, start
, length
);
495 output_text_length (buffer
) += length
;
498 /* Append a string deliminated by START and END to BUFFER. No wrapping is
499 done. However, if beginning a new line then emit output_prefix (BUFFER)
500 and skip any leading whitespace if appropriate. The caller must ensure
501 that it is safe to do so. */
504 output_append (buffer
, start
, end
)
505 output_buffer
*buffer
;
509 /* Emit prefix and skip whitespace if we're starting a new line. */
510 if (is_starting_newline (buffer
))
512 output_emit_prefix (buffer
);
513 if (output_is_line_wrapping (buffer
))
514 while (start
!= end
&& *start
== ' ')
517 output_append_r (buffer
, start
, end
- start
);
521 output_indent (buffer
)
522 output_buffer
*buffer
;
524 int n
= output_indentation (buffer
);
527 for (i
= 0; i
< n
; ++i
)
528 output_add_character (buffer
, ' ');
531 /* Wrap a text delimited by START and END into BUFFER. */
534 wrap_text (buffer
, start
, end
)
535 output_buffer
*buffer
;
539 int is_wrapping
= output_is_line_wrapping (buffer
);
543 /* Dump anything bordered by whitespaces. */
545 const char *p
= start
;
546 while (p
!= end
&& *p
!= ' ' && *p
!= '\n')
548 if (is_wrapping
&& p
- start
>= output_space_left (buffer
))
549 output_add_newline (buffer
);
550 output_append (buffer
, start
, p
);
554 if (start
!= end
&& *start
== ' ')
556 output_add_space (buffer
);
559 if (start
!= end
&& *start
== '\n')
561 output_add_newline (buffer
);
567 /* Same as wrap_text but wrap text only when in line-wrapping mode. */
570 maybe_wrap_text (buffer
, start
, end
)
571 output_buffer
*buffer
;
575 if (output_is_line_wrapping (buffer
))
576 wrap_text (buffer
, start
, end
);
578 output_append (buffer
, start
, end
);
582 /* Append a STRING to BUFFER; the STRING might be line-wrapped if in
586 output_add_string (buffer
, str
)
587 output_buffer
*buffer
;
590 maybe_wrap_text (buffer
, str
, str
+ (str
? strlen (str
) : 0));
593 /* Flush the content of BUFFER onto the attached stream,
597 output_buffer_to_stream (buffer
)
598 output_buffer
*buffer
;
600 const char *text
= output_finalize_message (buffer
);
601 fputs (text
, output_buffer_attached_stream (buffer
));
602 output_clear_message_text (buffer
);
605 /* Format a message pointed to by output_buffer_text_cursor (BUFFER) using
606 output_buffer_format_args (BUFFER) as appropriate. The following format
607 specifiers are recognized as being language independent:
608 %d, %i: (signed) integer in base ten.
609 %u: unsigned integer in base ten.
610 %o: unsigned integer in base eight.
611 %x: unsigned integer in base sixteen.
612 %ld, %li, %lo, %lu, %lx: long versions of the above.
616 %*.s: a substring the length of which is specified by an integer. */
619 output_format (buffer
)
620 output_buffer
*buffer
;
622 for (; *output_buffer_text_cursor (buffer
);
623 ++output_buffer_text_cursor (buffer
))
625 int long_integer
= 0;
629 const char *p
= output_buffer_text_cursor (buffer
);
630 while (*p
&& *p
!= '%')
632 wrap_text (buffer
, output_buffer_text_cursor (buffer
), p
);
633 output_buffer_text_cursor (buffer
) = p
;
636 if (!*output_buffer_text_cursor (buffer
))
639 /* We got a '%'. Let's see what happens. Record whether we're
640 parsing a long integer format specifier. */
641 if (*++output_buffer_text_cursor (buffer
) == 'l')
644 ++output_buffer_text_cursor (buffer
);
647 /* Handle %c, %d, %i, %ld, %li, %lo, %lu, %lx, %o, %s, %u,
648 %x, %.*s; %%. And nothing else. Front-ends should install
649 printers to grok language specific format specifiers. */
650 switch (*output_buffer_text_cursor (buffer
))
654 (buffer
, va_arg (output_buffer_format_args (buffer
), int));
661 (buffer
, va_arg (output_buffer_format_args (buffer
), long int));
664 (buffer
, va_arg (output_buffer_format_args (buffer
), int));
669 output_long_octal (buffer
,
670 va_arg (output_buffer_format_args (buffer
),
673 output_octal (buffer
,
674 va_arg (output_buffer_format_args (buffer
),
679 output_add_string (buffer
,
680 va_arg (output_buffer_format_args (buffer
),
686 output_long_unsigned_decimal
687 (buffer
, va_arg (output_buffer_format_args (buffer
),
690 output_unsigned_decimal
691 (buffer
, va_arg (output_buffer_format_args (buffer
),
697 output_long_hexadecimal
698 (buffer
, va_arg (output_buffer_format_args (buffer
),
702 (buffer
, va_arg (output_buffer_format_args (buffer
),
707 output_add_character (buffer
, '%');
714 /* We handle no precision specifier but `%.*s'. */
715 if (*++output_buffer_text_cursor (buffer
) != '*')
717 else if (*++output_buffer_text_cursor (buffer
) != 's')
719 n
= va_arg (output_buffer_format_args (buffer
), int);
720 s
= va_arg (output_buffer_format_args (buffer
), const char *);
721 output_append (buffer
, s
, s
+ n
);
726 if (!buffer
->format_decoder
|| !(*buffer
->format_decoder
) (buffer
))
728 /* Hmmm. The front-end failed to install a format translator
729 but called us with an unrecognized format. Sorry. */
737 vbuild_message_string (msg
, ap
)
743 vasprintf (&str
, msg
, ap
);
747 /* Return a malloc'd string containing MSG formatted a la
748 printf. The caller is responsible for freeing the memory. */
751 build_message_string
VPARAMS ((const char *msg
, ...))
756 VA_FIXEDARG (ap
, const char *, msg
);
758 str
= vbuild_message_string (msg
, ap
);
765 /* Return a malloc'd string describing a location. The caller is
766 responsible for freeing the memory. */
769 context_as_prefix (file
, line
, warn
)
777 return build_message_string (_("%s:%d: warning: "), file
, line
);
779 return build_message_string ("%s:%d: ", file
, line
);
784 return build_message_string (_("%s: warning: "), progname
);
786 return build_message_string ("%s: ", progname
);
790 /* Same as context_as_prefix, but only the source FILE is given. */
793 file_name_as_prefix (f
)
796 return build_message_string ("%s: ", f
);
799 /* Format a MESSAGE into BUFFER. Automatically wrap lines. */
802 output_do_printf (buffer
, msg
)
803 output_buffer
*buffer
;
806 char *message
= vbuild_message_string (msg
,
807 output_buffer_format_args (buffer
));
809 wrap_text (buffer
, message
, message
+ strlen (message
));
814 /* Format a message into BUFFER a la printf. */
817 output_printf
VPARAMS ((struct output_buffer
*buffer
, const char *msgid
, ...))
822 VA_FIXEDARG (ap
, output_buffer
*, buffer
);
823 VA_FIXEDARG (ap
, const char *, msgid
);
825 old_args
= output_buffer_ptr_to_format_args (buffer
);
826 output_buffer_ptr_to_format_args (buffer
) = &ap
;
827 output_do_printf (buffer
, _(msgid
));
828 output_buffer_ptr_to_format_args (buffer
) = old_args
;
832 /* Print a message relevant to the given DECL. */
835 format_with_decl (buffer
, decl
)
836 output_buffer
*buffer
;
841 /* Do magic to get around lack of varargs support for insertion
842 of arguments into existing list. We know that the decl is first;
843 we ass_u_me that it will be printed with "%s". */
844 for (p
= output_buffer_text_cursor (buffer
); *p
; ++p
)
850 else if (*(p
+ 1) != 's')
857 /* Print the left-hand substring. */
858 maybe_wrap_text (buffer
, output_buffer_text_cursor (buffer
), p
);
860 if (*p
== '%') /* Print the name. */
862 const char *const n
= (DECL_NAME (decl
)
863 ? (*lang_hooks
.decl_printable_name
) (decl
, 2)
864 : _("((anonymous))"));
865 output_add_string (buffer
, n
);
869 if (ISALPHA (*(p
- 1) & 0xFF))
874 if (*p
) /* Print the rest of the message. */
876 output_buffer_text_cursor (buffer
) = p
;
877 output_format (buffer
);
882 /* Report a diagnostic MESSAGE at the declaration DECL.
883 MSG is a format string which uses %s to substitute the declaration
884 name; subsequent substitutions are a la output_format. */
887 diagnostic_for_decl (decl
, msgid
, args_ptr
, warn
)
895 if (diagnostic_lock
++)
898 if (count_error (warn
))
900 os
= output_buffer_state (diagnostic_buffer
);
901 report_error_function (DECL_SOURCE_FILE (decl
));
903 (diagnostic_buffer
, context_as_prefix
904 (DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
), warn
));
905 output_buffer_ptr_to_format_args (diagnostic_buffer
) = args_ptr
;
906 output_buffer_text_cursor (diagnostic_buffer
) = _(msgid
);
907 format_with_decl (diagnostic_buffer
, decl
);
908 diagnostic_finish ((output_buffer
*) global_dc
);
909 output_destroy_prefix (diagnostic_buffer
);
911 output_buffer_state (diagnostic_buffer
) = os
;
917 /* Count an error or warning. Return 1 if the message should be printed. */
920 count_error (warningp
)
923 if (warningp
&& !diagnostic_report_warnings_p ())
926 if (warningp
&& !warnings_are_errors
)
930 static int warning_message
= 0;
932 if (warningp
&& !warning_message
)
934 verbatim ("%s: warnings being treated as errors\n", progname
);
943 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
944 runs its second argument through gettext. */
947 fnotice
VPARAMS ((FILE *file
, const char *msgid
, ...))
950 VA_FIXEDARG (ap
, FILE *, file
);
951 VA_FIXEDARG (ap
, const char *, msgid
);
953 vfprintf (file
, _(msgid
), ap
);
958 /* Print a fatal I/O error message. Argument are like printf.
959 Also include a system error message based on `errno'. */
962 fatal_io_error
VPARAMS ((const char *msgid
, ...))
967 VA_FIXEDARG (ap
, const char *, msgid
);
969 os
= output_buffer_state (diagnostic_buffer
);
971 output_printf (diagnostic_buffer
, "%s: %s: ", progname
, xstrerror (errno
));
972 output_buffer_ptr_to_format_args (diagnostic_buffer
) = &ap
;
973 output_buffer_text_cursor (diagnostic_buffer
) = _(msgid
);
974 output_format (diagnostic_buffer
);
975 diagnostic_finish ((output_buffer
*) global_dc
);
976 output_buffer_state (diagnostic_buffer
) = os
;
978 exit (FATAL_EXIT_CODE
);
981 /* Issue a pedantic warning MSGID. */
984 pedwarn
VPARAMS ((const char *msgid
, ...))
986 diagnostic_context dc
;
989 VA_FIXEDARG (ap
, const char *, msgid
);
991 set_diagnostic_context
992 (&dc
, msgid
, &ap
, input_filename
, lineno
, !flag_pedantic_errors
);
993 report_diagnostic (&dc
);
997 /* Issue a pedantic waring about DECL. */
1000 pedwarn_with_decl
VPARAMS ((tree decl
, const char *msgid
, ...))
1002 VA_OPEN (ap
, msgid
);
1003 VA_FIXEDARG (ap
, tree
, decl
);
1004 VA_FIXEDARG (ap
, const char *, msgid
);
1006 /* We don't want -pedantic-errors to cause the compilation to fail from
1007 "errors" in system header files. Sometimes fixincludes can't fix what's
1008 broken (eg: unsigned char bitfields - fixing it may change the alignment
1009 which will cause programs to mysteriously fail because the C library
1010 or kernel uses the original layout). There's no point in issuing a
1011 warning either, it's just unnecessary noise. */
1012 if (!DECL_IN_SYSTEM_HEADER (decl
))
1013 diagnostic_for_decl (decl
, msgid
, &ap
, !flag_pedantic_errors
);
1017 /* Same as above but within the context FILE and LINE. */
1020 pedwarn_with_file_and_line
VPARAMS ((const char *file
, int line
,
1021 const char *msgid
, ...))
1023 diagnostic_context dc
;
1025 VA_OPEN (ap
, msgid
);
1026 VA_FIXEDARG (ap
, const char *, file
);
1027 VA_FIXEDARG (ap
, int, line
);
1028 VA_FIXEDARG (ap
, const char *, msgid
);
1030 set_diagnostic_context (&dc
, msgid
, &ap
, file
, line
, !flag_pedantic_errors
);
1031 report_diagnostic (&dc
);
1035 /* Just apologize with MSGID. */
1038 sorry
VPARAMS ((const char *msgid
, ...))
1042 VA_OPEN (ap
, msgid
);
1043 VA_FIXEDARG (ap
, const char *, msgid
);
1046 os
= output_buffer_state (diagnostic_buffer
);
1049 (diagnostic_buffer
, context_as_prefix (input_filename
, lineno
, 0));
1050 output_printf (diagnostic_buffer
, "sorry, not implemented: ");
1051 output_buffer_ptr_to_format_args (diagnostic_buffer
) = &ap
;
1052 output_buffer_text_cursor (diagnostic_buffer
) = _(msgid
);
1053 output_format (diagnostic_buffer
);
1054 diagnostic_finish ((output_buffer
*) global_dc
);
1055 output_buffer_state (diagnostic_buffer
) = os
;
1059 /* Called when the start of a function definition is parsed,
1060 this function prints on stderr the name of the function. */
1063 announce_function (decl
)
1068 if (rtl_dump_and_exit
)
1069 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl
)));
1071 verbatim (" %s", (*lang_hooks
.decl_printable_name
) (decl
, 2));
1073 output_needs_newline (diagnostic_buffer
) = 1;
1074 record_last_error_function ();
1078 /* The default function to print out name of current function that caused
1082 lhd_print_error_function (context
, file
)
1083 diagnostic_context
*context
;
1086 if (error_function_changed ())
1088 char *prefix
= file
? build_message_string ("%s: ", file
) : NULL
;
1091 os
= diagnostic_state (context
);
1092 output_set_prefix ((output_buffer
*) context
, prefix
);
1094 if (current_function_decl
== NULL
)
1095 output_add_string ((output_buffer
*) context
, _("At top level:"));
1098 if (TREE_CODE (TREE_TYPE (current_function_decl
)) == METHOD_TYPE
)
1100 ((output_buffer
*) context
, "In member function `%s':",
1101 (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2));
1104 ((output_buffer
*) context
, "In function `%s':",
1105 (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2));
1107 output_add_newline ((output_buffer
*) context
);
1109 record_last_error_function ();
1110 output_buffer_to_stream ((output_buffer
*) context
);
1111 diagnostic_state (context
) = os
;
1112 free ((char*) prefix
);
1116 /* Prints out, if necessary, the name of the current function
1117 that caused an error. Called from all error and warning functions.
1118 We ignore the FILE parameter, as it cannot be relied upon. */
1121 report_error_function (file
)
1122 const char *file ATTRIBUTE_UNUSED
;
1124 report_problematic_module ((output_buffer
*) global_dc
);
1125 (*lang_hooks
.print_error_function
) (global_dc
, input_filename
);
1129 error_with_file_and_line
VPARAMS ((const char *file
, int line
,
1130 const char *msgid
, ...))
1132 diagnostic_context dc
;
1134 VA_OPEN (ap
, msgid
);
1135 VA_FIXEDARG (ap
, const char *, file
);
1136 VA_FIXEDARG (ap
, int, line
);
1137 VA_FIXEDARG (ap
, const char *, msgid
);
1139 set_diagnostic_context (&dc
, msgid
, &ap
, file
, line
, /* warn = */ 0);
1140 report_diagnostic (&dc
);
1145 error_with_decl
VPARAMS ((tree decl
, const char *msgid
, ...))
1147 VA_OPEN (ap
, msgid
);
1148 VA_FIXEDARG (ap
, tree
, decl
);
1149 VA_FIXEDARG (ap
, const char *, msgid
);
1151 diagnostic_for_decl (decl
, msgid
, &ap
, /* warn = */ 0);
1156 /* Report an error message. The arguments are like that of printf. */
1159 error
VPARAMS ((const char *msgid
, ...))
1161 diagnostic_context dc
;
1163 VA_OPEN (ap
, msgid
);
1164 VA_FIXEDARG (ap
, const char *, msgid
);
1166 set_diagnostic_context
1167 (&dc
, msgid
, &ap
, input_filename
, lineno
, /* warn = */ 0);
1168 report_diagnostic (&dc
);
1172 /* Likewise, except that the compilation is terminated after printing the
1176 fatal_error
VPARAMS ((const char *msgid
, ...))
1178 diagnostic_context dc
;
1180 VA_OPEN (ap
, msgid
);
1181 VA_FIXEDARG (ap
, const char *, msgid
);
1183 set_diagnostic_context
1184 (&dc
, msgid
, &ap
, input_filename
, lineno
, /* warn = */ 0);
1185 report_diagnostic (&dc
);
1188 fnotice (stderr
, "compilation terminated.\n");
1189 exit (FATAL_EXIT_CODE
);
1192 /* Report a compiler error at the current line number. Allow a front end to
1193 intercept the message. */
1195 static void (*internal_error_function
) PARAMS ((const char *, va_list *));
1197 /* Set the function to call when a compiler error occurs. */
1200 set_internal_error_function (f
)
1201 void (*f
) PARAMS ((const char *, va_list *));
1203 internal_error_function
= f
;
1207 internal_error
VPARAMS ((const char *msgid
, ...))
1209 diagnostic_context dc
;
1211 VA_OPEN (ap
, msgid
);
1212 VA_FIXEDARG (ap
, const char *, msgid
);
1214 if (diagnostic_lock
)
1217 #ifndef ENABLE_CHECKING
1218 if (errorcount
> 0 || sorrycount
> 0)
1220 fnotice (stderr
, "%s:%d: confused by earlier errors, bailing out\n",
1221 input_filename
, lineno
);
1222 exit (FATAL_EXIT_CODE
);
1226 if (internal_error_function
!= 0)
1227 (*internal_error_function
) (_(msgid
), &ap
);
1229 set_diagnostic_context
1230 (&dc
, msgid
, &ap
, input_filename
, lineno
, /* warn = */0);
1231 report_diagnostic (&dc
);
1235 "Please submit a full bug report,\n\
1236 with preprocessed source if appropriate.\n\
1237 See %s for instructions.\n", GCCBUGURL
);
1238 exit (FATAL_EXIT_CODE
);
1242 warning_with_file_and_line
VPARAMS ((const char *file
, int line
,
1243 const char *msgid
, ...))
1245 diagnostic_context dc
;
1247 VA_OPEN (ap
, msgid
);
1248 VA_FIXEDARG (ap
, const char *, file
);
1249 VA_FIXEDARG (ap
, int, line
);
1250 VA_FIXEDARG (ap
, const char *, msgid
);
1252 set_diagnostic_context (&dc
, msgid
, &ap
, file
, line
, /* warn = */ 1);
1253 report_diagnostic (&dc
);
1258 warning_with_decl
VPARAMS ((tree decl
, const char *msgid
, ...))
1260 VA_OPEN (ap
, msgid
);
1261 VA_FIXEDARG (ap
, tree
, decl
);
1262 VA_FIXEDARG (ap
, const char *, msgid
);
1264 diagnostic_for_decl (decl
, msgid
, &ap
, /* warn = */ 1);
1269 warning
VPARAMS ((const char *msgid
, ...))
1271 diagnostic_context dc
;
1273 VA_OPEN (ap
, msgid
);
1274 VA_FIXEDARG (ap
, const char *, msgid
);
1276 set_diagnostic_context
1277 (&dc
, msgid
, &ap
, input_filename
, lineno
, /* warn = */ 1);
1278 report_diagnostic (&dc
);
1282 /* Flush diagnostic_buffer content on stderr. */
1285 diagnostic_finish (buffer
)
1286 output_buffer
*buffer
;
1288 output_buffer_to_stream (buffer
);
1289 clear_diagnostic_info (buffer
);
1290 fputc ('\n', output_buffer_attached_stream (buffer
));
1291 fflush (output_buffer_attached_stream (buffer
));
1294 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
1295 settings needed by BUFFER for a verbatim formatting. */
1298 output_do_verbatim (buffer
, msgid
, args_ptr
)
1299 output_buffer
*buffer
;
1305 os
= output_buffer_state (buffer
);
1306 output_prefix (buffer
) = NULL
;
1307 output_prefixing_rule (buffer
) = DIAGNOSTICS_SHOW_PREFIX_NEVER
;
1308 output_buffer_text_cursor (buffer
) = _(msgid
);
1309 output_buffer_ptr_to_format_args (buffer
) = args_ptr
;
1310 output_set_maximum_length (buffer
, 0);
1311 output_format (buffer
);
1312 output_buffer_state (buffer
) = os
;
1315 /* Output MESSAGE verbatim into BUFFER. */
1318 output_verbatim
VPARAMS ((output_buffer
*buffer
, const char *msgid
, ...))
1320 VA_OPEN (ap
, msgid
);
1321 VA_FIXEDARG (ap
, output_buffer
*, buffer
);
1322 VA_FIXEDARG (ap
, const char *, msgid
);
1324 output_do_verbatim (buffer
, msgid
, &ap
);
1328 /* Same as above but use diagnostic_buffer. */
1331 verbatim
VPARAMS ((const char *msgid
, ...))
1333 VA_OPEN (ap
, msgid
);
1334 VA_FIXEDARG (ap
, const char *, msgid
);
1336 output_do_verbatim (diagnostic_buffer
, msgid
, &ap
);
1337 output_buffer_to_stream (diagnostic_buffer
);
1341 /* Report a diagnostic message (an error or a warning) as specified by
1342 DC. This function is *the* subroutine in terms of which front-ends
1343 should implement their specific diagnostic handling modules. The
1344 front-end independent format specifiers are exactly those described
1345 in the documentation of output_format. */
1348 report_diagnostic (dc
)
1349 diagnostic_context
*dc
;
1353 if (diagnostic_lock
++)
1356 if (count_error (diagnostic_is_warning (dc
)))
1358 os
= output_buffer_state (diagnostic_buffer
);
1359 diagnostic_msg
= diagnostic_message (dc
);
1360 diagnostic_args
= diagnostic_argument_list (dc
);
1361 (*diagnostic_starter (dc
)) (diagnostic_buffer
, dc
);
1362 output_format (diagnostic_buffer
);
1363 (*diagnostic_finalizer (dc
)) (diagnostic_buffer
, dc
);
1364 diagnostic_finish ((output_buffer
*) global_dc
);
1365 output_buffer_state (diagnostic_buffer
) = os
;
1371 /* Inform the user that an error occurred while trying to report some
1372 other error. This indicates catastrophic internal inconsistencies,
1373 so give up now. But do try to flush out the previous error.
1374 This mustn't use internal_error, that will cause infinite recursion. */
1379 if (diagnostic_lock
< 3)
1380 diagnostic_finish ((output_buffer
*) global_dc
);
1383 "Internal compiler error: Error reporting routines re-entered.\n");
1385 "Please submit a full bug report,\n\
1386 with preprocessed source if appropriate.\n\
1387 See %s for instructions.\n", GCCBUGURL
);
1388 exit (FATAL_EXIT_CODE
);
1391 /* Given a partial pathname as input, return another pathname that
1392 shares no directory elements with the pathname of __FILE__. This
1393 is used by fancy_abort() to print `Internal compiler error in expr.c'
1394 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1397 trim_filename (name
)
1400 static const char this_file
[] = __FILE__
;
1401 const char *p
= name
, *q
= this_file
;
1403 /* First skip any "../" in each filename. This allows us to give a proper
1404 reference to a file in a subdirectory. */
1405 while (p
[0] == '.' && p
[1] == '.'
1406 && (p
[2] == DIR_SEPARATOR
1407 #ifdef DIR_SEPARATOR_2
1408 || p
[2] == DIR_SEPARATOR_2
1413 while (q
[0] == '.' && q
[1] == '.'
1414 && (q
[2] == DIR_SEPARATOR
1415 #ifdef DIR_SEPARATOR_2
1416 || p
[2] == DIR_SEPARATOR_2
1421 /* Now skip any parts the two filenames have in common. */
1422 while (*p
== *q
&& *p
!= 0 && *q
!= 0)
1425 /* Now go backwards until the previous directory separator. */
1426 while (p
> name
&& p
[-1] != DIR_SEPARATOR
1427 #ifdef DIR_SEPARATOR_2
1428 && p
[-1] != DIR_SEPARATOR_2
1436 /* Report an internal compiler error in a friendly manner and without
1440 fancy_abort (file
, line
, function
)
1443 const char *function
;
1445 internal_error ("Internal compiler error in %s, at %s:%d",
1446 function
, trim_filename (file
), line
);
1449 /* Setup DC for reporting a diagnostic MESSAGE (an error or a WARNING),
1450 using arguments pointed to by ARGS_PTR, issued at a location specified
1451 by FILE and LINE. */
1454 set_diagnostic_context (dc
, msgid
, args_ptr
, file
, line
, warn
)
1455 diagnostic_context
*dc
;
1462 memset (dc
, 0, sizeof (diagnostic_context
));
1463 diagnostic_message (dc
) = _(msgid
);
1464 diagnostic_argument_list (dc
) = args_ptr
;
1465 diagnostic_file_location (dc
) = file
;
1466 diagnostic_line_location (dc
) = line
;
1467 diagnostic_is_warning (dc
) = warn
;
1468 diagnostic_starter (dc
) = diagnostic_starter (global_dc
);
1469 diagnostic_finalizer (dc
) = diagnostic_finalizer (global_dc
);
1473 report_problematic_module (buffer
)
1474 output_buffer
*buffer
;
1476 struct file_stack
*p
;
1478 if (output_needs_newline (buffer
))
1480 output_add_newline (buffer
);
1481 output_needs_newline (buffer
) = 0;
1484 if (input_file_stack
&& input_file_stack
->next
!= 0
1485 && error_module_changed ())
1487 for (p
= input_file_stack
->next
; p
; p
= p
->next
)
1488 if (p
== input_file_stack
->next
)
1490 (buffer
, "In file included from %s:%d", p
->name
, p
->line
);
1493 (buffer
, ",\n from %s:%d", p
->name
, p
->line
);
1494 output_verbatim (buffer
, ":\n");
1495 record_last_error_module ();
1500 default_diagnostic_starter (buffer
, dc
)
1501 output_buffer
*buffer
;
1502 diagnostic_context
*dc
;
1504 report_error_function (diagnostic_file_location (dc
));
1505 output_set_prefix (buffer
,
1506 context_as_prefix (diagnostic_file_location (dc
),
1507 diagnostic_line_location (dc
),
1508 diagnostic_is_warning (dc
)));
1512 default_diagnostic_finalizer (buffer
, dc
)
1513 output_buffer
*buffer
;
1514 diagnostic_context
*dc
__attribute__((__unused__
));
1516 output_destroy_prefix (buffer
);
1520 warn_deprecated_use (node
)
1523 if (node
== 0 || !warn_deprecated_decl
)
1527 warning ("`%s' is deprecated (declared at %s:%d)",
1528 IDENTIFIER_POINTER (DECL_NAME (node
)),
1529 DECL_SOURCE_FILE (node
), DECL_SOURCE_LINE (node
));
1530 else if (TYPE_P (node
))
1532 const char *what
= NULL
;
1533 tree decl
= TYPE_STUB_DECL (node
);
1535 if (TREE_CODE (TYPE_NAME (node
)) == IDENTIFIER_NODE
)
1536 what
= IDENTIFIER_POINTER (TYPE_NAME (node
));
1537 else if (TREE_CODE (TYPE_NAME (node
)) == TYPE_DECL
1538 && DECL_NAME (TYPE_NAME (node
)))
1539 what
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node
)));
1544 warning ("`%s' is deprecated (declared at %s:%d)", what
,
1545 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
));
1547 warning ("`%s' is deprecated", what
);
1550 warning ("type is deprecated (declared at %s:%d)",
1551 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
));
1553 warning ("type is deprecated");