1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 /* This file implements the language independent aspect of diagnostic
27 #undef FLOAT /* This is for hpux. They should change hpux. */
28 #undef FFS /* Some systems define this in param.h. */
36 #include "diagnostic.h"
37 #include "langhooks.h"
38 #include "langhooks-def.h"
40 #define 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 line_wrap_cutoff(BUFFER) (BUFFER)->state.maximum_length
54 #define prefix_was_emitted_for(BUFFER) (BUFFER)->state.emitted_prefix_p
57 static void output_flush PARAMS ((output_buffer *));
58 static void output_do_verbatim PARAMS ((output_buffer *, text_info *));
59 static void output_buffer_to_stream PARAMS ((output_buffer *));
60 static void output_format PARAMS ((output_buffer *, text_info *));
61 static void output_indent PARAMS ((output_buffer *));
63 static char *vbuild_message_string PARAMS ((const char *, va_list))
64 ATTRIBUTE_PRINTF (1, 0);
65 static char *build_message_string PARAMS ((const char *, ...))
67 static void format_with_decl PARAMS ((output_buffer *, text_info *, tree));
68 static void diagnostic_for_decl PARAMS ((diagnostic_info *, tree));
69 static void set_real_maximum_length PARAMS ((output_buffer *));
71 static void output_unsigned_decimal PARAMS ((output_buffer *, unsigned int));
72 static void output_long_decimal PARAMS ((output_buffer *, long int));
73 static void output_long_unsigned_decimal PARAMS ((output_buffer *,
75 static void output_octal PARAMS ((output_buffer *, unsigned int));
76 static void output_long_octal PARAMS ((output_buffer *, unsigned long int));
77 static void output_hexadecimal PARAMS ((output_buffer *, unsigned int));
78 static void output_long_hexadecimal PARAMS ((output_buffer *,
80 static void output_append_r PARAMS ((output_buffer *, const char *, int));
81 static void wrap_text PARAMS ((output_buffer *, const char *, const char *));
82 static void maybe_wrap_text PARAMS ((output_buffer *, const char *,
84 static void output_clear_data PARAMS ((output_buffer *));
86 static void default_diagnostic_starter PARAMS ((diagnostic_context *,
88 static void default_diagnostic_finalizer PARAMS ((diagnostic_context *,
91 static void error_recursion PARAMS ((diagnostic_context *)) ATTRIBUTE_NORETURN;
93 extern int rtl_dump_and_exit;
94 extern int warnings_are_errors;
96 /* A diagnostic_context surrogate for stderr. */
97 static diagnostic_context global_diagnostic_context;
98 diagnostic_context *global_dc = &global_diagnostic_context;
101 /* Subroutine of output_set_maximum_length. Set up BUFFER's
102 internal maximum characters per line. */
104 set_real_maximum_length (buffer)
105 output_buffer *buffer;
107 /* If we're told not to wrap lines then do the obvious thing. In case
108 we'll emit prefix only once per diagnostic message, it is appropriate
109 not to increase unnecessarily the line-length cut-off. */
110 if (!output_is_line_wrapping (buffer)
111 || output_prefixing_rule (buffer) == DIAGNOSTICS_SHOW_PREFIX_ONCE
112 || output_prefixing_rule (buffer) == DIAGNOSTICS_SHOW_PREFIX_NEVER)
113 line_wrap_cutoff (buffer) = output_line_cutoff (buffer);
116 int prefix_length = buffer->state.prefix ?
117 strlen (buffer->state.prefix) : 0;
118 /* If the prefix is ridiculously too long, output at least
120 if (output_line_cutoff (buffer) - prefix_length < 32)
121 line_wrap_cutoff (buffer) = output_line_cutoff (buffer) + 32;
123 line_wrap_cutoff (buffer) = output_line_cutoff (buffer);
127 /* Sets the number of maximum characters per line BUFFER can output
128 in line-wrapping mode. A LENGTH value 0 suppresses line-wrapping. */
130 output_set_maximum_length (buffer, length)
131 output_buffer *buffer;
134 output_line_cutoff (buffer) = length;
135 set_real_maximum_length (buffer);
138 /* Sets BUFFER's PREFIX. */
140 output_set_prefix (buffer, prefix)
141 output_buffer *buffer;
144 buffer->state.prefix = prefix;
145 set_real_maximum_length (buffer);
146 prefix_was_emitted_for (buffer) = false;
147 output_indentation (buffer) = 0;
150 /* Return a pointer to the last character emitted in the output
151 BUFFER area. A NULL pointer means no character available. */
153 output_last_position (buffer)
154 const output_buffer *buffer;
156 const char *p = NULL;
158 if (obstack_base (&buffer->obstack) != obstack_next_free (&buffer->obstack))
159 p = ((const char *) obstack_next_free (&buffer->obstack)) - 1;
163 /* Free BUFFER's prefix, a previously malloc'd string. */
165 output_destroy_prefix (buffer)
166 output_buffer *buffer;
168 if (buffer->state.prefix != NULL)
170 free ((char *) buffer->state.prefix);
171 buffer->state.prefix = NULL;
175 /* Zero out any text output so far in BUFFER. */
177 output_clear_message_text (buffer)
178 output_buffer *buffer;
180 obstack_free (&buffer->obstack, obstack_base (&buffer->obstack));
181 output_text_length (buffer) = 0;
184 /* Zero out any formatting data used so far by BUFFER. */
186 output_clear_data (buffer)
187 output_buffer *buffer;
189 prefix_was_emitted_for (buffer) = false;
190 output_indentation (buffer) = 0;
193 /* Construct an output BUFFER with PREFIX and of MAXIMUM_LENGTH
194 characters per line. */
196 init_output_buffer (buffer, prefix, maximum_length)
197 output_buffer *buffer;
201 memset (buffer, 0, sizeof (output_buffer));
202 obstack_init (&buffer->obstack);
203 output_buffer_attached_stream (buffer) = stderr;
204 output_line_cutoff (buffer) = maximum_length;
205 output_prefixing_rule (buffer) = diagnostic_prefixing_rule (global_dc);
206 output_set_prefix (buffer, prefix);
207 output_text_length (buffer) = 0;
208 output_clear_data (buffer);
211 /* Reinitialize BUFFER. */
213 output_clear (buffer)
214 output_buffer *buffer;
216 output_clear_message_text (buffer);
217 output_clear_data (buffer);
220 /* Finishes constructing a NULL-terminated character string representing
221 the BUFFERed message. */
223 output_finalize_message (buffer)
224 output_buffer *buffer;
226 obstack_1grow (&buffer->obstack, '\0');
227 return output_message_text (buffer);
230 /* Return the amount of characters BUFFER can accept to
233 output_space_left (buffer)
234 const output_buffer *buffer;
236 return line_wrap_cutoff (buffer) - output_text_length (buffer);
239 /* Write out BUFFER's prefix. */
241 output_emit_prefix (buffer)
242 output_buffer *buffer;
244 if (buffer->state.prefix != NULL)
246 switch (output_prefixing_rule (buffer))
249 case DIAGNOSTICS_SHOW_PREFIX_NEVER:
252 case DIAGNOSTICS_SHOW_PREFIX_ONCE:
253 if (prefix_was_emitted_for (buffer))
255 output_indent (buffer);
258 output_indentation (buffer) += 3;
261 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
263 int prefix_length = strlen (buffer->state.prefix);
264 output_append_r (buffer, buffer->state.prefix, prefix_length);
265 prefix_was_emitted_for (buffer) = true;
272 /* Have BUFFER start a new line. */
274 output_add_newline (buffer)
275 output_buffer *buffer;
277 obstack_1grow (&buffer->obstack, '\n');
278 output_text_length (buffer) = 0;
281 /* Appends a character to BUFFER. */
283 output_add_character (buffer, c)
284 output_buffer *buffer;
287 if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
288 output_add_newline (buffer);
289 obstack_1grow (&buffer->obstack, c);
290 ++output_text_length (buffer);
293 /* Adds a space to BUFFER. */
295 output_add_space (buffer)
296 output_buffer *buffer;
298 if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
300 output_add_newline (buffer);
303 obstack_1grow (&buffer->obstack, ' ');
304 ++output_text_length (buffer);
307 /* These functions format an INTEGER into BUFFER as suggested by their
310 output_decimal (buffer, i)
311 output_buffer *buffer;
314 output_formatted_integer (buffer, "%d", i);
318 output_long_decimal (buffer, i)
319 output_buffer *buffer;
322 output_formatted_integer (buffer, "%ld", i);
326 output_unsigned_decimal (buffer, i)
327 output_buffer *buffer;
330 output_formatted_integer (buffer, "%u", i);
334 output_long_unsigned_decimal (buffer, i)
335 output_buffer *buffer;
338 output_formatted_integer (buffer, "%lu", i);
342 output_octal (buffer, i)
343 output_buffer *buffer;
346 output_formatted_integer (buffer, "%o", i);
350 output_long_octal (buffer, i)
351 output_buffer *buffer;
354 output_formatted_integer (buffer, "%lo", i);
358 output_hexadecimal (buffer, i)
359 output_buffer *buffer;
362 output_formatted_integer (buffer, "%x", i);
366 output_long_hexadecimal (buffer, i)
367 output_buffer *buffer;
370 output_formatted_integer (buffer, "%lx", i);
373 /* Append to BUFFER a string specified by its STARTING character
376 output_append_r (buffer, start, length)
377 output_buffer *buffer;
381 obstack_grow (&buffer->obstack, start, length);
382 output_text_length (buffer) += length;
385 /* Append a string deliminated by START and END to BUFFER. No wrapping is
386 done. However, if beginning a new line then emit BUFFER->state.prefix
387 and skip any leading whitespace if appropriate. The caller must ensure
388 that it is safe to do so. */
390 output_append (buffer, start, end)
391 output_buffer *buffer;
395 /* Emit prefix and skip whitespace if we're starting a new line. */
396 if (is_starting_newline (buffer))
398 output_emit_prefix (buffer);
399 if (output_is_line_wrapping (buffer))
400 while (start != end && *start == ' ')
403 output_append_r (buffer, start, end - start);
407 output_indent (buffer)
408 output_buffer *buffer;
410 int n = output_indentation (buffer);
413 for (i = 0; i < n; ++i)
414 output_add_character (buffer, ' ');
417 /* Wrap a text delimited by START and END into BUFFER. */
419 wrap_text (buffer, start, end)
420 output_buffer *buffer;
424 bool is_wrapping = output_is_line_wrapping (buffer);
428 /* Dump anything bordered by whitespaces. */
430 const char *p = start;
431 while (p != end && *p != ' ' && *p != '\n')
433 if (is_wrapping && p - start >= output_space_left (buffer))
434 output_add_newline (buffer);
435 output_append (buffer, start, p);
439 if (start != end && *start == ' ')
441 output_add_space (buffer);
444 if (start != end && *start == '\n')
446 output_add_newline (buffer);
452 /* Same as wrap_text but wrap text only when in line-wrapping mode. */
454 maybe_wrap_text (buffer, start, end)
455 output_buffer *buffer;
459 if (output_is_line_wrapping (buffer))
460 wrap_text (buffer, start, end);
462 output_append (buffer, start, end);
466 /* Append a STRING to BUFFER; the STRING might be line-wrapped if in
469 output_add_string (buffer, str)
470 output_buffer *buffer;
473 maybe_wrap_text (buffer, str, str + (str ? strlen (str) : 0));
476 /* Flush the content of BUFFER onto the attached stream,
480 output_buffer_to_stream (buffer)
481 output_buffer *buffer;
483 const char *text = output_finalize_message (buffer);
484 fputs (text, output_buffer_attached_stream (buffer));
485 output_clear_message_text (buffer);
488 /* Format a message pointed to by TEXT. The following format specifiers are
489 recognized as being language independent:
490 %d, %i: (signed) integer in base ten.
491 %u: unsigned integer in base ten.
492 %o: unsigned integer in base eight.
493 %x: unsigned integer in base sixteen.
494 %ld, %li, %lo, %lu, %lx: long versions of the above.
498 %*.s: a substring the length of which is specified by an integer. */
500 output_format (buffer, text)
501 output_buffer *buffer;
504 for (; *text->format_spec; ++text->format_spec)
506 bool long_integer = 0;
510 const char *p = text->format_spec;
511 while (*p && *p != '%')
513 wrap_text (buffer, text->format_spec, p);
514 text->format_spec = p;
517 if (*text->format_spec == '\0')
520 /* We got a '%'. Let's see what happens. Record whether we're
521 parsing a long integer format specifier. */
522 if (*++text->format_spec == 'l')
528 /* Handle %c, %d, %i, %ld, %li, %lo, %lu, %lx, %o, %s, %u,
529 %x, %.*s; %%. And nothing else. Front-ends should install
530 printers to grok language specific format specifiers. */
531 switch (*text->format_spec)
534 output_add_character (buffer, va_arg (*text->args_ptr, int));
540 output_long_decimal (buffer, va_arg (*text->args_ptr, long int));
542 output_decimal (buffer, va_arg (*text->args_ptr, int));
547 output_long_octal (buffer,
548 va_arg (*text->args_ptr, unsigned long int));
550 output_octal (buffer, va_arg (*text->args_ptr, unsigned int));
554 output_add_string (buffer, va_arg (*text->args_ptr, const char *));
559 output_long_unsigned_decimal
560 (buffer, va_arg (*text->args_ptr, long unsigned int));
562 output_unsigned_decimal
563 (buffer, va_arg (*text->args_ptr, unsigned int));
568 output_long_hexadecimal
569 (buffer, va_arg (*text->args_ptr, unsigned long int));
572 (buffer, va_arg (*text->args_ptr, unsigned int));
576 output_add_character (buffer, '%');
583 /* We handle no precision specifier but `%.*s'. */
584 if (*++text->format_spec != '*')
586 else if (*++text->format_spec != 's')
588 n = va_arg (*text->args_ptr, int);
589 s = va_arg (*text->args_ptr, const char *);
590 output_append (buffer, s, s + n);
595 if (!buffer->format_decoder
596 || !(*buffer->format_decoder) (buffer, text))
598 /* Hmmm. The front-end failed to install a format translator
599 but called us with an unrecognized format. Sorry. */
607 vbuild_message_string (msg, ap)
613 vasprintf (&str, msg, ap);
617 /* Return a malloc'd string containing MSG formatted a la
618 printf. The caller is responsible for freeing the memory. */
620 build_message_string VPARAMS ((const char *msg, ...))
625 VA_FIXEDARG (ap, const char *, msg);
627 str = vbuild_message_string (msg, ap);
634 /* Same as diagnsotic_build_prefix, but only the source FILE is given. */
636 file_name_as_prefix (f)
639 return build_message_string ("%s: ", f);
642 /* Format a message into BUFFER a la printf. */
644 output_printf VPARAMS ((struct output_buffer *buffer, const char *msgid, ...))
648 VA_FIXEDARG (ap, output_buffer *, buffer);
649 VA_FIXEDARG (ap, const char *, msgid);
652 text.format_spec = _(msgid);
653 output_format (buffer, &text);
657 /* Print a message relevant to the given DECL. */
659 format_with_decl (buffer, text, decl)
660 output_buffer *buffer;
666 /* Do magic to get around lack of varargs support for insertion
667 of arguments into existing list. We know that the decl is first;
668 we ass_u_me that it will be printed with "%s". */
669 for (p = text->format_spec; *p; ++p)
675 else if (*(p + 1) != 's')
682 /* Print the left-hand substring. */
683 maybe_wrap_text (buffer, text->format_spec, p);
685 if (*p == '%') /* Print the name. */
687 const char *const n = (DECL_NAME (decl)
688 ? (*lang_hooks.decl_printable_name) (decl, 2)
689 : _("((anonymous))"));
690 output_add_string (buffer, n);
694 if (ISALPHA (*(p - 1) & 0xFF))
699 if (*p) /* Print the rest of the message. */
701 text->format_spec = p;
702 output_format (buffer, text);
706 /* Flush the content of BUFFER onto the attached stream. */
708 output_flush (buffer)
709 output_buffer *buffer;
711 output_buffer_to_stream (buffer);
712 output_clear_data (buffer);
713 fputc ('\n', output_buffer_attached_stream (buffer));
714 fflush (output_buffer_attached_stream (buffer));
717 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
718 settings needed by BUFFER for a verbatim formatting. */
720 output_do_verbatim (buffer, text)
721 output_buffer *buffer;
724 diagnostic_prefixing_rule_t rule = output_prefixing_rule (buffer);
725 int line_cutoff = output_line_cutoff (buffer);
727 /* Set verbatim mode. */
728 output_prefixing_rule (buffer) = DIAGNOSTICS_SHOW_PREFIX_NEVER;
729 output_line_cutoff (buffer) = 0;
730 /* Do the actual formatting. */
731 output_format (buffer, text);
732 /* Restore previous settings. */
733 output_prefixing_rule (buffer) = rule;
734 output_line_cutoff (buffer) = line_cutoff;
737 /* Output MESSAGE verbatim into BUFFER. */
739 output_verbatim VPARAMS ((output_buffer *buffer, const char *msgid, ...))
743 VA_FIXEDARG (ap, output_buffer *, buffer);
744 VA_FIXEDARG (ap, const char *, msgid);
746 text.format_spec = msgid;
748 output_do_verbatim (buffer, &text);
753 /* Initialize the diagnostic message outputting machinery. */
755 diagnostic_initialize (context)
756 diagnostic_context *context;
758 memset (context, 0, sizeof *context);
759 obstack_init (&context->buffer.obstack);
761 /* By default, diagnostics are sent to stderr. */
762 output_buffer_attached_stream (&context->buffer) = stderr;
764 /* By default, we emit prefixes once per message. */
765 diagnostic_prefixing_rule (context) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
767 diagnostic_starter (context) = default_diagnostic_starter;
768 diagnostic_finalizer (context) = default_diagnostic_finalizer;
772 diagnostic_set_info (diagnostic, msgid, args, file, line, kind)
773 diagnostic_info *diagnostic;
780 diagnostic->message.format_spec = msgid;
781 diagnostic->message.args_ptr = args;
782 diagnostic->location.file = file;
783 diagnostic->location.line = line;
784 diagnostic->kind = kind;
787 /* Return a malloc'd string describing a location. The caller is
788 responsible for freeing the memory. */
790 diagnostic_build_prefix (diagnostic)
791 diagnostic_info *diagnostic;
793 static const char *diagnostic_kind_text[] = {
794 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
795 #include "diagnostic.def"
796 #undef DEFINE_DIAGNOSTIC_KIND
799 if (diagnostic->kind >= DK_LAST_DIAGNOSTIC_KIND)
802 return diagnostic->location.file
803 ? build_message_string ("%s:%d: %s",
804 diagnostic->location.file,
805 diagnostic->location.line,
806 _(diagnostic_kind_text[diagnostic->kind]))
807 : build_message_string ("%s: %s", progname,
808 _(diagnostic_kind_text[diagnostic->kind]));
811 /* Report a diagnostic MESSAGE at the declaration DECL.
812 MSG is a format string which uses %s to substitute the declaration
813 name; subsequent substitutions are a la output_format. */
815 diagnostic_for_decl (diagnostic, decl)
816 diagnostic_info *diagnostic;
819 if (global_dc->lock++)
820 error_recursion (global_dc);
822 if (diagnostic_count_error (global_dc, diagnostic->kind))
824 diagnostic_report_current_function (global_dc);
826 (&global_dc->buffer, diagnostic_build_prefix (diagnostic));
827 format_with_decl (&global_dc->buffer, &diagnostic->message, decl);
828 output_flush (&global_dc->buffer);
829 output_destroy_prefix (&global_dc->buffer);
835 diagnostic_flush_buffer (context)
836 diagnostic_context *context;
838 output_buffer_to_stream (&context->buffer);
839 fflush (output_buffer_attached_stream (&context->buffer));
842 /* Count an error or warning. Return true if the message should be
845 diagnostic_count_error (context, kind)
846 diagnostic_context *context;
849 if (kind == DK_WARNING && !diagnostic_report_warnings_p ())
852 if (kind == DK_WARNING && !warnings_are_errors)
853 ++diagnostic_kind_count (context, DK_WARNING);
856 static bool warning_message = false;
858 if (kind == DK_WARNING && !warning_message)
860 output_verbatim (&context->buffer,
861 "%s: warnings being treated as errors\n", progname);
862 warning_message = true;
864 ++diagnostic_kind_count (context, DK_ERROR);
870 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
871 runs its second argument through gettext. */
873 fnotice VPARAMS ((FILE *file, const char *msgid, ...))
876 VA_FIXEDARG (ap, FILE *, file);
877 VA_FIXEDARG (ap, const char *, msgid);
879 vfprintf (file, _(msgid), ap);
884 /* Print a fatal I/O error message. Argument are like printf.
885 Also include a system error message based on `errno'. */
887 fatal_io_error VPARAMS ((const char *msgid, ...))
891 VA_FIXEDARG (ap, const char *, msgid);
893 text.format_spec = _(msgid);
895 output_printf (&global_dc->buffer, "%s: %s: ", progname, xstrerror (errno));
896 output_format (&global_dc->buffer, &text);
897 output_flush (&global_dc->buffer);
899 exit (FATAL_EXIT_CODE);
902 /* Issue a pedantic warning MSGID. */
904 pedwarn VPARAMS ((const char *msgid, ...))
906 diagnostic_info diagnostic;
908 VA_FIXEDARG (ap, const char *, msgid);
910 diagnostic_set_info (&diagnostic, _(msgid), &ap, input_filename, lineno,
911 pedantic_error_kind ());
912 report_diagnostic (&diagnostic);
916 /* Issue a pedantic warning about DECL. */
918 pedwarn_with_decl VPARAMS ((tree decl, const char *msgid, ...))
920 diagnostic_info diagnostic;
922 VA_FIXEDARG (ap, tree, decl);
923 VA_FIXEDARG (ap, const char *, msgid);
925 diagnostic_set_info (&diagnostic, _(msgid), &ap,
926 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
927 pedantic_error_kind ());
929 /* We don't want -pedantic-errors to cause the compilation to fail from
930 "errors" in system header files. Sometimes fixincludes can't fix what's
931 broken (eg: unsigned char bitfields - fixing it may change the alignment
932 which will cause programs to mysteriously fail because the C library
933 or kernel uses the original layout). There's no point in issuing a
934 warning either, it's just unnecessary noise. */
935 if (!DECL_IN_SYSTEM_HEADER (decl))
936 diagnostic_for_decl (&diagnostic, decl);
940 /* Same as above but within the context FILE and LINE. */
942 pedwarn_with_file_and_line VPARAMS ((const char *file, int line,
943 const char *msgid, ...))
945 diagnostic_info diagnostic;
947 VA_FIXEDARG (ap, const char *, file);
948 VA_FIXEDARG (ap, int, line);
949 VA_FIXEDARG (ap, const char *, msgid);
951 diagnostic_set_info (&diagnostic, _(msgid), &ap, file, line,
952 pedantic_error_kind ());
953 report_diagnostic (&diagnostic);
957 /* Just apologize with MSGID. */
959 sorry VPARAMS ((const char *msgid, ...))
961 diagnostic_info diagnostic;
964 VA_FIXEDARG (ap, const char *, msgid);
967 diagnostic_set_info (&diagnostic, _(msgid), &ap,
968 input_filename, lineno, DK_SORRY);
971 (&global_dc->buffer, diagnostic_build_prefix (&diagnostic));
972 output_printf (&global_dc->buffer, "sorry, not implemented: ");
973 output_format (&global_dc->buffer, &diagnostic.message);
974 output_flush (&global_dc->buffer);
978 /* Called when the start of a function definition is parsed,
979 this function prints on stderr the name of the function. */
981 announce_function (decl)
986 if (rtl_dump_and_exit)
987 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl)));
989 verbatim (" %s", (*lang_hooks.decl_printable_name) (decl, 2));
991 output_needs_newline (&global_dc->buffer) = true;
992 diagnostic_set_last_function (global_dc);
996 /* The default function to print out name of current function that caused
999 lhd_print_error_function (context, file)
1000 diagnostic_context *context;
1003 if (diagnostic_last_function_changed (context))
1005 const char *old_prefix = output_prefix (&context->buffer);
1006 char *new_prefix = file ? build_message_string ("%s: ", file) : NULL;
1008 output_set_prefix (&context->buffer, new_prefix);
1010 if (current_function_decl == NULL)
1011 output_add_string (&context->buffer, _("At top level:"));
1014 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
1016 (&context->buffer, "In member function `%s':",
1017 (*lang_hooks.decl_printable_name) (current_function_decl, 2));
1020 (&context->buffer, "In function `%s':",
1021 (*lang_hooks.decl_printable_name) (current_function_decl, 2));
1023 output_add_newline (&context->buffer);
1025 diagnostic_set_last_function (context);
1026 output_buffer_to_stream (&context->buffer);
1027 context->buffer.state.prefix = old_prefix;
1028 free ((char*) new_prefix);
1032 /* Prints out, if necessary, the name of the current function
1033 that caused an error. Called from all error and warning functions.
1034 We ignore the FILE parameter, as it cannot be relied upon. */
1037 diagnostic_report_current_function (context)
1038 diagnostic_context *context;
1040 diagnostic_report_current_module (context);
1041 (*lang_hooks.print_error_function) (context, input_filename);
1045 error_with_file_and_line VPARAMS ((const char *file, int line,
1046 const char *msgid, ...))
1048 diagnostic_info diagnostic;
1050 VA_OPEN (ap, msgid);
1051 VA_FIXEDARG (ap, const char *, file);
1052 VA_FIXEDARG (ap, int, line);
1053 VA_FIXEDARG (ap, const char *, msgid);
1055 diagnostic_set_info (&diagnostic, msgid, &ap, file, line, DK_ERROR);
1056 report_diagnostic (&diagnostic);
1061 error_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1063 diagnostic_info diagnostic;
1064 VA_OPEN (ap, msgid);
1065 VA_FIXEDARG (ap, tree, decl);
1066 VA_FIXEDARG (ap, const char *, msgid);
1068 diagnostic_set_info (&diagnostic, msgid, &ap,
1069 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1071 diagnostic_for_decl (&diagnostic, decl);
1076 /* Report an error message. The arguments are like that of printf. */
1079 error VPARAMS ((const char *msgid, ...))
1081 diagnostic_info diagnostic;
1083 VA_OPEN (ap, msgid);
1084 VA_FIXEDARG (ap, const char *, msgid);
1086 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1088 report_diagnostic (&diagnostic);
1092 /* Likewise, except that the compilation is terminated after printing the
1096 fatal_error VPARAMS ((const char *msgid, ...))
1098 diagnostic_info diagnostic;
1100 VA_OPEN (ap, msgid);
1101 VA_FIXEDARG (ap, const char *, msgid);
1103 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1105 report_diagnostic (&diagnostic);
1108 fnotice (stderr, "compilation terminated.\n");
1109 exit (FATAL_EXIT_CODE);
1113 internal_error VPARAMS ((const char *msgid, ...))
1115 diagnostic_info diagnostic;
1117 VA_OPEN (ap, msgid);
1118 VA_FIXEDARG (ap, const char *, msgid);
1120 if (global_dc->lock)
1121 error_recursion (global_dc);
1123 #ifndef ENABLE_CHECKING
1124 if (errorcount > 0 || sorrycount > 0)
1126 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
1127 input_filename, lineno);
1128 exit (FATAL_EXIT_CODE);
1132 if (global_dc->internal_error != 0)
1133 (*global_dc->internal_error) (_(msgid), &ap);
1135 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1137 report_diagnostic (&diagnostic);
1141 "Please submit a full bug report,\n\
1142 with preprocessed source if appropriate.\n\
1143 See %s for instructions.\n", GCCBUGURL);
1144 exit (FATAL_EXIT_CODE);
1148 warning_with_file_and_line VPARAMS ((const char *file, int line,
1149 const char *msgid, ...))
1151 diagnostic_info diagnostic;
1153 VA_OPEN (ap, msgid);
1154 VA_FIXEDARG (ap, const char *, file);
1155 VA_FIXEDARG (ap, int, line);
1156 VA_FIXEDARG (ap, const char *, msgid);
1158 diagnostic_set_info (&diagnostic, msgid, &ap, file, line, DK_WARNING);
1159 report_diagnostic (&diagnostic);
1164 warning_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1166 diagnostic_info diagnostic;
1167 VA_OPEN (ap, msgid);
1168 VA_FIXEDARG (ap, tree, decl);
1169 VA_FIXEDARG (ap, const char *, msgid);
1171 diagnostic_set_info (&diagnostic, msgid, &ap,
1172 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1174 diagnostic_for_decl (&diagnostic, decl);
1179 warning VPARAMS ((const char *msgid, ...))
1181 diagnostic_info diagnostic;
1183 VA_OPEN (ap, msgid);
1184 VA_FIXEDARG (ap, const char *, msgid);
1186 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1188 report_diagnostic (&diagnostic);
1193 /* Same as above but use diagnostic_buffer. */
1196 verbatim VPARAMS ((const char *msgid, ...))
1199 VA_OPEN (ap, msgid);
1200 VA_FIXEDARG (ap, const char *, msgid);
1202 text.format_spec = _(msgid);
1203 text.args_ptr = ≈
1204 output_do_verbatim (&global_dc->buffer, &text);
1205 output_buffer_to_stream (&global_dc->buffer);
1209 /* Report a diagnostic message (an error or a warning) as specified by
1210 DC. This function is *the* subroutine in terms of which front-ends
1211 should implement their specific diagnostic handling modules. The
1212 front-end independent format specifiers are exactly those described
1213 in the documentation of output_format. */
1216 diagnostic_report_diagnostic (context, diagnostic)
1217 diagnostic_context *context;
1218 diagnostic_info *diagnostic;
1220 if (context->lock++)
1221 error_recursion (context);
1223 if (diagnostic_count_error (context, diagnostic->kind))
1225 (*diagnostic_starter (context)) (context, diagnostic);
1226 output_format (&context->buffer, &diagnostic->message);
1227 (*diagnostic_finalizer (context)) (context, diagnostic);
1228 output_flush (&context->buffer);
1234 /* Inform the user that an error occurred while trying to report some
1235 other error. This indicates catastrophic internal inconsistencies,
1236 so give up now. But do try to flush out the previous error.
1237 This mustn't use internal_error, that will cause infinite recursion. */
1240 error_recursion (context)
1241 diagnostic_context *context;
1243 if (context->lock < 3)
1244 output_flush (&context->buffer);
1247 "Internal compiler error: Error reporting routines re-entered.\n");
1249 "Please submit a full bug report,\n\
1250 with preprocessed source if appropriate.\n\
1251 See %s for instructions.\n", GCCBUGURL);
1252 exit (FATAL_EXIT_CODE);
1255 /* Given a partial pathname as input, return another pathname that
1256 shares no directory elements with the pathname of __FILE__. This
1257 is used by fancy_abort() to print `Internal compiler error in expr.c'
1258 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1261 trim_filename (name)
1264 static const char this_file[] = __FILE__;
1265 const char *p = name, *q = this_file;
1267 /* First skip any "../" in each filename. This allows us to give a proper
1268 reference to a file in a subdirectory. */
1269 while (p[0] == '.' && p[1] == '.'
1270 && (p[2] == DIR_SEPARATOR
1271 #ifdef DIR_SEPARATOR_2
1272 || p[2] == DIR_SEPARATOR_2
1277 while (q[0] == '.' && q[1] == '.'
1278 && (q[2] == DIR_SEPARATOR
1279 #ifdef DIR_SEPARATOR_2
1280 || p[2] == DIR_SEPARATOR_2
1285 /* Now skip any parts the two filenames have in common. */
1286 while (*p == *q && *p != 0 && *q != 0)
1289 /* Now go backwards until the previous directory separator. */
1290 while (p > name && p[-1] != DIR_SEPARATOR
1291 #ifdef DIR_SEPARATOR_2
1292 && p[-1] != DIR_SEPARATOR_2
1300 /* Report an internal compiler error in a friendly manner and without
1304 fancy_abort (file, line, function)
1307 const char *function;
1309 internal_error ("Internal compiler error in %s, at %s:%d",
1310 function, trim_filename (file), line);
1314 diagnostic_report_current_module (context)
1315 diagnostic_context *context;
1317 struct file_stack *p;
1319 if (output_needs_newline (&context->buffer))
1321 output_add_newline (&context->buffer);
1322 output_needs_newline (&context->buffer) = false;
1325 if (input_file_stack && input_file_stack->next != 0
1326 && diagnostic_last_module_changed (context))
1328 for (p = input_file_stack->next; p; p = p->next)
1329 if (p == input_file_stack->next)
1330 output_verbatim (&context->buffer,
1331 "In file included from %s:%d", p->name, p->line);
1333 output_verbatim (&context->buffer,
1334 ",\n from %s:%d", p->name, p->line);
1335 output_verbatim (&context->buffer, ":\n");
1336 diagnostic_set_last_module (context);
1341 default_diagnostic_starter (context, diagnostic)
1342 diagnostic_context *context;
1343 diagnostic_info *diagnostic;
1345 diagnostic_report_current_function (context);
1346 output_set_prefix (&context->buffer, diagnostic_build_prefix (diagnostic));
1350 default_diagnostic_finalizer (context, diagnostic)
1351 diagnostic_context *context;
1352 diagnostic_info *diagnostic __attribute__((unused));
1354 output_destroy_prefix (&context->buffer);
1358 warn_deprecated_use (node)
1361 if (node == 0 || !warn_deprecated_decl)
1365 warning ("`%s' is deprecated (declared at %s:%d)",
1366 IDENTIFIER_POINTER (DECL_NAME (node)),
1367 DECL_SOURCE_FILE (node), DECL_SOURCE_LINE (node));
1368 else if (TYPE_P (node))
1370 const char *what = NULL;
1371 tree decl = TYPE_STUB_DECL (node);
1373 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
1374 what = IDENTIFIER_POINTER (TYPE_NAME (node));
1375 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
1376 && DECL_NAME (TYPE_NAME (node)))
1377 what = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node)));
1382 warning ("`%s' is deprecated (declared at %s:%d)", what,
1383 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
1385 warning ("`%s' is deprecated", what);
1388 warning ("type is deprecated (declared at %s:%d)",
1389 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
1391 warning ("type is deprecated");