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 diagnostic_finish 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 clear_diagnostic_info 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 diagnostic data used so far by BUFFER. */
282 clear_diagnostic_info (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) = 0;
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 clear_diagnostic_info (buffer);
310 /* Reinitialize BUFFER. */
313 output_clear (buffer)
314 output_buffer *buffer;
316 output_clear_message_text (buffer);
317 clear_diagnostic_info (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) = ≈
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 diagnostic_finish ((output_buffer *) global_dc);
911 output_destroy_prefix (diagnostic_buffer);
913 output_buffer_state (diagnostic_buffer) = os;
919 /* Count an error or warning. Return 1 if the message should be printed. */
922 count_error (warningp)
925 if (warningp && !diagnostic_report_warnings_p ())
928 if (warningp && !warnings_are_errors)
932 static int warning_message = 0;
934 if (warningp && !warning_message)
936 verbatim ("%s: warnings being treated as errors\n", progname);
945 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
946 runs its second argument through gettext. */
949 fnotice VPARAMS ((FILE *file, const char *msgid, ...))
952 VA_FIXEDARG (ap, FILE *, file);
953 VA_FIXEDARG (ap, const char *, msgid);
955 vfprintf (file, _(msgid), ap);
960 /* Print a fatal I/O error message. Argument are like printf.
961 Also include a system error message based on `errno'. */
964 fatal_io_error VPARAMS ((const char *msgid, ...))
969 VA_FIXEDARG (ap, const char *, msgid);
971 os = output_buffer_state (diagnostic_buffer);
973 output_printf (diagnostic_buffer, "%s: %s: ", progname, xstrerror (errno));
974 output_buffer_ptr_to_format_args (diagnostic_buffer) = ≈
975 output_buffer_text_cursor (diagnostic_buffer) = _(msgid);
976 output_format (diagnostic_buffer);
977 diagnostic_finish ((output_buffer *) global_dc);
978 output_buffer_state (diagnostic_buffer) = os;
980 exit (FATAL_EXIT_CODE);
983 /* Issue a pedantic warning MSGID. */
986 pedwarn VPARAMS ((const char *msgid, ...))
988 diagnostic_context dc;
991 VA_FIXEDARG (ap, const char *, msgid);
993 set_diagnostic_context
994 (&dc, msgid, &ap, input_filename, lineno, !flag_pedantic_errors);
995 report_diagnostic (&dc);
999 /* Issue a pedantic waring about DECL. */
1002 pedwarn_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1004 VA_OPEN (ap, msgid);
1005 VA_FIXEDARG (ap, tree, decl);
1006 VA_FIXEDARG (ap, const char *, msgid);
1008 /* We don't want -pedantic-errors to cause the compilation to fail from
1009 "errors" in system header files. Sometimes fixincludes can't fix what's
1010 broken (eg: unsigned char bitfields - fixing it may change the alignment
1011 which will cause programs to mysteriously fail because the C library
1012 or kernel uses the original layout). There's no point in issuing a
1013 warning either, it's just unnecessary noise. */
1014 if (!DECL_IN_SYSTEM_HEADER (decl))
1015 diagnostic_for_decl (decl, msgid, &ap, !flag_pedantic_errors);
1019 /* Same as above but within the context FILE and LINE. */
1022 pedwarn_with_file_and_line VPARAMS ((const char *file, int line,
1023 const char *msgid, ...))
1025 diagnostic_context dc;
1027 VA_OPEN (ap, msgid);
1028 VA_FIXEDARG (ap, const char *, file);
1029 VA_FIXEDARG (ap, int, line);
1030 VA_FIXEDARG (ap, const char *, msgid);
1032 set_diagnostic_context (&dc, msgid, &ap, file, line, !flag_pedantic_errors);
1033 report_diagnostic (&dc);
1037 /* Just apologize with MSGID. */
1040 sorry VPARAMS ((const char *msgid, ...))
1044 VA_OPEN (ap, msgid);
1045 VA_FIXEDARG (ap, const char *, msgid);
1048 os = output_buffer_state (diagnostic_buffer);
1051 (diagnostic_buffer, context_as_prefix (input_filename, lineno, 0));
1052 output_printf (diagnostic_buffer, "sorry, not implemented: ");
1053 output_buffer_ptr_to_format_args (diagnostic_buffer) = ≈
1054 output_buffer_text_cursor (diagnostic_buffer) = _(msgid);
1055 output_format (diagnostic_buffer);
1056 diagnostic_finish ((output_buffer *) global_dc);
1057 output_buffer_state (diagnostic_buffer) = os;
1061 /* Called when the start of a function definition is parsed,
1062 this function prints on stderr the name of the function. */
1065 announce_function (decl)
1070 if (rtl_dump_and_exit)
1071 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl)));
1073 verbatim (" %s", (*lang_hooks.decl_printable_name) (decl, 2));
1075 output_needs_newline (diagnostic_buffer) = 1;
1076 record_last_error_function ();
1080 /* The default function to print out name of current function that caused
1084 lhd_print_error_function (context, file)
1085 diagnostic_context *context;
1088 if (error_function_changed ())
1090 char *prefix = file ? build_message_string ("%s: ", file) : NULL;
1093 os = diagnostic_state (context);
1094 output_set_prefix ((output_buffer *) context, prefix);
1096 if (current_function_decl == NULL)
1097 output_add_string ((output_buffer *) context, _("At top level:"));
1100 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
1102 ((output_buffer *) context, "In member function `%s':",
1103 (*lang_hooks.decl_printable_name) (current_function_decl, 2));
1106 ((output_buffer *) context, "In function `%s':",
1107 (*lang_hooks.decl_printable_name) (current_function_decl, 2));
1109 output_add_newline ((output_buffer *) context);
1111 record_last_error_function ();
1112 output_buffer_to_stream ((output_buffer *) context);
1113 diagnostic_state (context) = os;
1114 free ((char*) prefix);
1118 /* Prints out, if necessary, the name of the current function
1119 that caused an error. Called from all error and warning functions.
1120 We ignore the FILE parameter, as it cannot be relied upon. */
1123 report_error_function (file)
1124 const char *file ATTRIBUTE_UNUSED;
1126 report_problematic_module ((output_buffer *) global_dc);
1127 (*lang_hooks.print_error_function) (global_dc, input_filename);
1131 error_with_file_and_line VPARAMS ((const char *file, int line,
1132 const char *msgid, ...))
1134 diagnostic_context dc;
1136 VA_OPEN (ap, msgid);
1137 VA_FIXEDARG (ap, const char *, file);
1138 VA_FIXEDARG (ap, int, line);
1139 VA_FIXEDARG (ap, const char *, msgid);
1141 set_diagnostic_context (&dc, msgid, &ap, file, line, /* warn = */ 0);
1142 report_diagnostic (&dc);
1147 error_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1149 VA_OPEN (ap, msgid);
1150 VA_FIXEDARG (ap, tree, decl);
1151 VA_FIXEDARG (ap, const char *, msgid);
1153 diagnostic_for_decl (decl, msgid, &ap, /* warn = */ 0);
1158 /* Report an error message. The arguments are like that of printf. */
1161 error VPARAMS ((const char *msgid, ...))
1163 diagnostic_context dc;
1165 VA_OPEN (ap, msgid);
1166 VA_FIXEDARG (ap, const char *, msgid);
1168 set_diagnostic_context
1169 (&dc, msgid, &ap, input_filename, lineno, /* warn = */ 0);
1170 report_diagnostic (&dc);
1174 /* Likewise, except that the compilation is terminated after printing the
1178 fatal_error VPARAMS ((const char *msgid, ...))
1180 diagnostic_context dc;
1182 VA_OPEN (ap, msgid);
1183 VA_FIXEDARG (ap, const char *, msgid);
1185 set_diagnostic_context
1186 (&dc, msgid, &ap, input_filename, lineno, /* warn = */ 0);
1187 report_diagnostic (&dc);
1190 fnotice (stderr, "compilation terminated.\n");
1191 exit (FATAL_EXIT_CODE);
1194 /* Report a compiler error at the current line number. Allow a front end to
1195 intercept the message. */
1197 static void (*internal_error_function) PARAMS ((const char *, va_list *));
1199 /* Set the function to call when a compiler error occurs. */
1202 set_internal_error_function (f)
1203 void (*f) PARAMS ((const char *, va_list *));
1205 internal_error_function = f;
1209 internal_error VPARAMS ((const char *msgid, ...))
1211 diagnostic_context dc;
1213 VA_OPEN (ap, msgid);
1214 VA_FIXEDARG (ap, const char *, msgid);
1216 if (diagnostic_lock)
1219 #ifndef ENABLE_CHECKING
1220 if (errorcount > 0 || sorrycount > 0)
1222 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
1223 input_filename, lineno);
1224 exit (FATAL_EXIT_CODE);
1228 if (internal_error_function != 0)
1229 (*internal_error_function) (_(msgid), &ap);
1231 set_diagnostic_context
1232 (&dc, msgid, &ap, input_filename, lineno, /* warn = */0);
1233 report_diagnostic (&dc);
1237 "Please submit a full bug report,\n\
1238 with preprocessed source if appropriate.\n\
1239 See %s for instructions.\n", GCCBUGURL);
1240 exit (FATAL_EXIT_CODE);
1244 warning_with_file_and_line VPARAMS ((const char *file, int line,
1245 const char *msgid, ...))
1247 diagnostic_context dc;
1249 VA_OPEN (ap, msgid);
1250 VA_FIXEDARG (ap, const char *, file);
1251 VA_FIXEDARG (ap, int, line);
1252 VA_FIXEDARG (ap, const char *, msgid);
1254 set_diagnostic_context (&dc, msgid, &ap, file, line, /* warn = */ 1);
1255 report_diagnostic (&dc);
1260 warning_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1262 VA_OPEN (ap, msgid);
1263 VA_FIXEDARG (ap, tree, decl);
1264 VA_FIXEDARG (ap, const char *, msgid);
1266 diagnostic_for_decl (decl, msgid, &ap, /* warn = */ 1);
1271 warning VPARAMS ((const char *msgid, ...))
1273 diagnostic_context dc;
1275 VA_OPEN (ap, msgid);
1276 VA_FIXEDARG (ap, const char *, msgid);
1278 set_diagnostic_context
1279 (&dc, msgid, &ap, input_filename, lineno, /* warn = */ 1);
1280 report_diagnostic (&dc);
1284 /* Flush diagnostic_buffer content on stderr. */
1287 diagnostic_finish (buffer)
1288 output_buffer *buffer;
1290 output_buffer_to_stream (buffer);
1291 clear_diagnostic_info (buffer);
1292 fputc ('\n', output_buffer_attached_stream (buffer));
1293 fflush (output_buffer_attached_stream (buffer));
1296 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
1297 settings needed by BUFFER for a verbatim formatting. */
1300 output_do_verbatim (buffer, msgid, args_ptr)
1301 output_buffer *buffer;
1307 os = output_buffer_state (buffer);
1308 output_prefix (buffer) = NULL;
1309 output_prefixing_rule (buffer) = DIAGNOSTICS_SHOW_PREFIX_NEVER;
1310 output_buffer_text_cursor (buffer) = _(msgid);
1311 output_buffer_ptr_to_format_args (buffer) = args_ptr;
1312 output_set_maximum_length (buffer, 0);
1313 output_format (buffer);
1314 output_buffer_state (buffer) = os;
1317 /* Output MESSAGE verbatim into BUFFER. */
1320 output_verbatim VPARAMS ((output_buffer *buffer, const char *msgid, ...))
1322 VA_OPEN (ap, msgid);
1323 VA_FIXEDARG (ap, output_buffer *, buffer);
1324 VA_FIXEDARG (ap, const char *, msgid);
1326 output_do_verbatim (buffer, msgid, &ap);
1330 /* Same as above but use diagnostic_buffer. */
1333 verbatim VPARAMS ((const char *msgid, ...))
1335 VA_OPEN (ap, msgid);
1336 VA_FIXEDARG (ap, const char *, msgid);
1338 output_do_verbatim (diagnostic_buffer, msgid, &ap);
1339 output_buffer_to_stream (diagnostic_buffer);
1343 /* Report a diagnostic message (an error or a warning) as specified by
1344 DC. This function is *the* subroutine in terms of which front-ends
1345 should implement their specific diagnostic handling modules. The
1346 front-end independent format specifiers are exactly those described
1347 in the documentation of output_format. */
1350 report_diagnostic (dc)
1351 diagnostic_context *dc;
1355 if (diagnostic_lock++)
1358 if (count_error (diagnostic_is_warning (dc)))
1360 os = output_buffer_state (diagnostic_buffer);
1361 diagnostic_msg = diagnostic_message (dc);
1362 diagnostic_args = diagnostic_argument_list (dc);
1363 (*diagnostic_starter (dc)) (diagnostic_buffer, dc);
1364 output_format (diagnostic_buffer);
1365 (*diagnostic_finalizer (dc)) (diagnostic_buffer, dc);
1366 diagnostic_finish ((output_buffer *) global_dc);
1367 output_buffer_state (diagnostic_buffer) = os;
1373 /* Inform the user that an error occurred while trying to report some
1374 other error. This indicates catastrophic internal inconsistencies,
1375 so give up now. But do try to flush out the previous error.
1376 This mustn't use internal_error, that will cause infinite recursion. */
1381 if (diagnostic_lock < 3)
1382 diagnostic_finish ((output_buffer *) global_dc);
1385 "Internal compiler error: Error reporting routines re-entered.\n");
1387 "Please submit a full bug report,\n\
1388 with preprocessed source if appropriate.\n\
1389 See %s for instructions.\n", GCCBUGURL);
1390 exit (FATAL_EXIT_CODE);
1393 /* Given a partial pathname as input, return another pathname that
1394 shares no directory elements with the pathname of __FILE__. This
1395 is used by fancy_abort() to print `Internal compiler error in expr.c'
1396 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1399 trim_filename (name)
1402 static const char this_file[] = __FILE__;
1403 const char *p = name, *q = this_file;
1405 /* First skip any "../" in each filename. This allows us to give a proper
1406 reference to a file in a subdirectory. */
1407 while (p[0] == '.' && p[1] == '.'
1408 && (p[2] == DIR_SEPARATOR
1409 #ifdef DIR_SEPARATOR_2
1410 || p[2] == DIR_SEPARATOR_2
1415 while (q[0] == '.' && q[1] == '.'
1416 && (q[2] == DIR_SEPARATOR
1417 #ifdef DIR_SEPARATOR_2
1418 || p[2] == DIR_SEPARATOR_2
1423 /* Now skip any parts the two filenames have in common. */
1424 while (*p == *q && *p != 0 && *q != 0)
1427 /* Now go backwards until the previous directory separator. */
1428 while (p > name && p[-1] != DIR_SEPARATOR
1429 #ifdef DIR_SEPARATOR_2
1430 && p[-1] != DIR_SEPARATOR_2
1438 /* Report an internal compiler error in a friendly manner and without
1442 fancy_abort (file, line, function)
1445 const char *function;
1447 internal_error ("Internal compiler error in %s, at %s:%d",
1448 function, trim_filename (file), line);
1451 /* Setup DC for reporting a diagnostic MESSAGE (an error or a WARNING),
1452 using arguments pointed to by ARGS_PTR, issued at a location specified
1453 by FILE and LINE. */
1456 set_diagnostic_context (dc, msgid, args_ptr, file, line, warn)
1457 diagnostic_context *dc;
1464 memset (dc, 0, sizeof (diagnostic_context));
1465 diagnostic_message (dc) = _(msgid);
1466 diagnostic_argument_list (dc) = args_ptr;
1467 diagnostic_file_location (dc) = file;
1468 diagnostic_line_location (dc) = line;
1469 diagnostic_is_warning (dc) = warn;
1470 diagnostic_starter (dc) = diagnostic_starter (global_dc);
1471 diagnostic_finalizer (dc) = diagnostic_finalizer (global_dc);
1475 report_problematic_module (buffer)
1476 output_buffer *buffer;
1478 struct file_stack *p;
1480 if (output_needs_newline (buffer))
1482 output_add_newline (buffer);
1483 output_needs_newline (buffer) = 0;
1486 if (input_file_stack && input_file_stack->next != 0
1487 && error_module_changed ())
1489 for (p = input_file_stack->next; p; p = p->next)
1490 if (p == input_file_stack->next)
1492 (buffer, "In file included from %s:%d", p->name, p->line);
1495 (buffer, ",\n from %s:%d", p->name, p->line);
1496 output_verbatim (buffer, ":\n");
1497 record_last_error_module ();
1502 default_diagnostic_starter (buffer, dc)
1503 output_buffer *buffer;
1504 diagnostic_context *dc;
1506 report_error_function (diagnostic_file_location (dc));
1507 output_set_prefix (buffer,
1508 context_as_prefix (diagnostic_file_location (dc),
1509 diagnostic_line_location (dc),
1510 diagnostic_is_warning (dc)));
1514 default_diagnostic_finalizer (buffer, dc)
1515 output_buffer *buffer;
1516 diagnostic_context *dc __attribute__((__unused__));
1518 output_destroy_prefix (buffer);
1522 warn_deprecated_use (node)
1525 if (node == 0 || !warn_deprecated_decl)
1529 warning ("`%s' is deprecated (declared at %s:%d)",
1530 IDENTIFIER_POINTER (DECL_NAME (node)),
1531 DECL_SOURCE_FILE (node), DECL_SOURCE_LINE (node));
1532 else if (TYPE_P (node))
1534 const char *what = NULL;
1535 tree decl = TYPE_STUB_DECL (node);
1537 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
1538 what = IDENTIFIER_POINTER (TYPE_NAME (node));
1539 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
1540 && DECL_NAME (TYPE_NAME (node)))
1541 what = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node)));
1546 warning ("`%s' is deprecated (declared at %s:%d)", what,
1547 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
1549 warning ("`%s' is deprecated", what);
1552 warning ("type is deprecated (declared at %s:%d)",
1553 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
1555 warning ("type is deprecated");