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;
769 context->warnings_are_errors_message = warnings_are_errors;
773 diagnostic_set_info (diagnostic, msgid, args, file, line, kind)
774 diagnostic_info *diagnostic;
781 diagnostic->message.format_spec = msgid;
782 diagnostic->message.args_ptr = args;
783 diagnostic->location.file = file;
784 diagnostic->location.line = line;
785 diagnostic->kind = kind;
788 /* Return a malloc'd string describing a location. The caller is
789 responsible for freeing the memory. */
791 diagnostic_build_prefix (diagnostic)
792 diagnostic_info *diagnostic;
794 static const char *diagnostic_kind_text[] = {
795 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
796 #include "diagnostic.def"
797 #undef DEFINE_DIAGNOSTIC_KIND
800 if (diagnostic->kind >= DK_LAST_DIAGNOSTIC_KIND)
803 return diagnostic->location.file
804 ? build_message_string ("%s:%d: %s",
805 diagnostic->location.file,
806 diagnostic->location.line,
807 _(diagnostic_kind_text[diagnostic->kind]))
808 : build_message_string ("%s: %s", progname,
809 _(diagnostic_kind_text[diagnostic->kind]));
812 /* Report a diagnostic MESSAGE at the declaration DECL.
813 MSG is a format string which uses %s to substitute the declaration
814 name; subsequent substitutions are a la output_format. */
816 diagnostic_for_decl (diagnostic, decl)
817 diagnostic_info *diagnostic;
820 if (global_dc->lock++)
821 error_recursion (global_dc);
823 if (diagnostic_count_diagnostic (global_dc, diagnostic->kind))
825 diagnostic_report_current_function (global_dc);
827 (&global_dc->buffer, diagnostic_build_prefix (diagnostic));
828 format_with_decl (&global_dc->buffer, &diagnostic->message, decl);
829 output_flush (&global_dc->buffer);
830 output_destroy_prefix (&global_dc->buffer);
836 diagnostic_flush_buffer (context)
837 diagnostic_context *context;
839 output_buffer_to_stream (&context->buffer);
840 fflush (output_buffer_attached_stream (&context->buffer));
843 /* Count a diagnostic. Return true if the message should be printed. */
845 diagnostic_count_diagnostic (context, kind)
846 diagnostic_context *context;
855 case DK_FATAL: case DK_ICE: case DK_SORRY:
856 case DK_ANACHRONISM: case DK_NOTE:
857 ++diagnostic_kind_count (context, kind);
861 if (!diagnostic_report_warnings_p ())
863 else if (!warnings_are_errors)
865 ++diagnostic_kind_count (context, DK_WARNING);
868 /* else fall through. */
871 if (kind == DK_WARNING && context->warnings_are_errors_message)
873 output_verbatim (&context->buffer,
874 "%s: warnings being treated as errors\n", progname);
875 context->warnings_are_errors_message = false;
877 ++diagnostic_kind_count (context, DK_ERROR);
884 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
885 runs its second argument through gettext. */
887 fnotice VPARAMS ((FILE *file, const char *msgid, ...))
890 VA_FIXEDARG (ap, FILE *, file);
891 VA_FIXEDARG (ap, const char *, msgid);
893 vfprintf (file, _(msgid), ap);
898 /* Print a fatal I/O error message. Argument are like printf.
899 Also include a system error message based on `errno'. */
901 fatal_io_error VPARAMS ((const char *msgid, ...))
905 VA_FIXEDARG (ap, const char *, msgid);
907 text.format_spec = _(msgid);
909 output_printf (&global_dc->buffer, "%s: %s: ", progname, xstrerror (errno));
910 output_format (&global_dc->buffer, &text);
911 output_flush (&global_dc->buffer);
913 exit (FATAL_EXIT_CODE);
916 /* Issue a pedantic warning MSGID. */
918 pedwarn VPARAMS ((const char *msgid, ...))
920 diagnostic_info diagnostic;
922 VA_FIXEDARG (ap, const char *, msgid);
924 diagnostic_set_info (&diagnostic, _(msgid), &ap, input_filename, lineno,
925 pedantic_error_kind ());
926 report_diagnostic (&diagnostic);
930 /* Issue a pedantic warning about DECL. */
932 pedwarn_with_decl VPARAMS ((tree decl, const char *msgid, ...))
934 diagnostic_info diagnostic;
936 VA_FIXEDARG (ap, tree, decl);
937 VA_FIXEDARG (ap, const char *, msgid);
939 diagnostic_set_info (&diagnostic, _(msgid), &ap,
940 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
941 pedantic_error_kind ());
943 /* We don't want -pedantic-errors to cause the compilation to fail from
944 "errors" in system header files. Sometimes fixincludes can't fix what's
945 broken (eg: unsigned char bitfields - fixing it may change the alignment
946 which will cause programs to mysteriously fail because the C library
947 or kernel uses the original layout). There's no point in issuing a
948 warning either, it's just unnecessary noise. */
949 if (!DECL_IN_SYSTEM_HEADER (decl))
950 diagnostic_for_decl (&diagnostic, decl);
954 /* Same as above but within the context FILE and LINE. */
956 pedwarn_with_file_and_line VPARAMS ((const char *file, int line,
957 const char *msgid, ...))
959 diagnostic_info diagnostic;
961 VA_FIXEDARG (ap, const char *, file);
962 VA_FIXEDARG (ap, int, line);
963 VA_FIXEDARG (ap, const char *, msgid);
965 diagnostic_set_info (&diagnostic, _(msgid), &ap, file, line,
966 pedantic_error_kind ());
967 report_diagnostic (&diagnostic);
971 /* Just apologize with MSGID. */
973 sorry VPARAMS ((const char *msgid, ...))
975 diagnostic_info diagnostic;
978 VA_FIXEDARG (ap, const char *, msgid);
981 diagnostic_set_info (&diagnostic, _(msgid), &ap,
982 input_filename, lineno, DK_SORRY);
985 (&global_dc->buffer, diagnostic_build_prefix (&diagnostic));
986 output_printf (&global_dc->buffer, "sorry, not implemented: ");
987 output_format (&global_dc->buffer, &diagnostic.message);
988 output_flush (&global_dc->buffer);
992 /* Called when the start of a function definition is parsed,
993 this function prints on stderr the name of the function. */
995 announce_function (decl)
1000 if (rtl_dump_and_exit)
1001 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl)));
1003 verbatim (" %s", (*lang_hooks.decl_printable_name) (decl, 2));
1005 output_needs_newline (&global_dc->buffer) = true;
1006 diagnostic_set_last_function (global_dc);
1010 /* The default function to print out name of current function that caused
1013 lhd_print_error_function (context, file)
1014 diagnostic_context *context;
1017 if (diagnostic_last_function_changed (context))
1019 const char *old_prefix = output_prefix (&context->buffer);
1020 char *new_prefix = file ? build_message_string ("%s: ", file) : NULL;
1022 output_set_prefix (&context->buffer, new_prefix);
1024 if (current_function_decl == NULL)
1025 output_add_string (&context->buffer, _("At top level:"));
1028 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
1030 (&context->buffer, "In member function `%s':",
1031 (*lang_hooks.decl_printable_name) (current_function_decl, 2));
1034 (&context->buffer, "In function `%s':",
1035 (*lang_hooks.decl_printable_name) (current_function_decl, 2));
1037 output_add_newline (&context->buffer);
1039 diagnostic_set_last_function (context);
1040 output_buffer_to_stream (&context->buffer);
1041 context->buffer.state.prefix = old_prefix;
1042 free ((char*) new_prefix);
1046 /* Prints out, if necessary, the name of the current function
1047 that caused an error. Called from all error and warning functions.
1048 We ignore the FILE parameter, as it cannot be relied upon. */
1051 diagnostic_report_current_function (context)
1052 diagnostic_context *context;
1054 diagnostic_report_current_module (context);
1055 (*lang_hooks.print_error_function) (context, input_filename);
1059 error_with_file_and_line VPARAMS ((const char *file, int line,
1060 const char *msgid, ...))
1062 diagnostic_info diagnostic;
1064 VA_OPEN (ap, msgid);
1065 VA_FIXEDARG (ap, const char *, file);
1066 VA_FIXEDARG (ap, int, line);
1067 VA_FIXEDARG (ap, const char *, msgid);
1069 diagnostic_set_info (&diagnostic, msgid, &ap, file, line, DK_ERROR);
1070 report_diagnostic (&diagnostic);
1075 error_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1077 diagnostic_info diagnostic;
1078 VA_OPEN (ap, msgid);
1079 VA_FIXEDARG (ap, tree, decl);
1080 VA_FIXEDARG (ap, const char *, msgid);
1082 diagnostic_set_info (&diagnostic, msgid, &ap,
1083 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1085 diagnostic_for_decl (&diagnostic, decl);
1090 /* Report an error message. The arguments are like that of printf. */
1093 error VPARAMS ((const char *msgid, ...))
1095 diagnostic_info diagnostic;
1097 VA_OPEN (ap, msgid);
1098 VA_FIXEDARG (ap, const char *, msgid);
1100 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1102 report_diagnostic (&diagnostic);
1106 /* Likewise, except that the compilation is terminated after printing the
1110 fatal_error VPARAMS ((const char *msgid, ...))
1112 diagnostic_info diagnostic;
1114 VA_OPEN (ap, msgid);
1115 VA_FIXEDARG (ap, const char *, msgid);
1117 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1119 report_diagnostic (&diagnostic);
1122 fnotice (stderr, "compilation terminated.\n");
1123 exit (FATAL_EXIT_CODE);
1127 internal_error VPARAMS ((const char *msgid, ...))
1129 diagnostic_info diagnostic;
1131 VA_OPEN (ap, msgid);
1132 VA_FIXEDARG (ap, const char *, msgid);
1134 if (global_dc->lock)
1135 error_recursion (global_dc);
1137 #ifndef ENABLE_CHECKING
1138 if (errorcount > 0 || sorrycount > 0)
1140 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
1141 input_filename, lineno);
1142 exit (FATAL_EXIT_CODE);
1146 if (global_dc->internal_error != 0)
1147 (*global_dc->internal_error) (_(msgid), &ap);
1149 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1151 report_diagnostic (&diagnostic);
1155 "Please submit a full bug report,\n\
1156 with preprocessed source if appropriate.\n\
1157 See %s for instructions.\n", GCCBUGURL);
1158 exit (FATAL_EXIT_CODE);
1162 warning_with_file_and_line VPARAMS ((const char *file, int line,
1163 const char *msgid, ...))
1165 diagnostic_info diagnostic;
1167 VA_OPEN (ap, msgid);
1168 VA_FIXEDARG (ap, const char *, file);
1169 VA_FIXEDARG (ap, int, line);
1170 VA_FIXEDARG (ap, const char *, msgid);
1172 diagnostic_set_info (&diagnostic, msgid, &ap, file, line, DK_WARNING);
1173 report_diagnostic (&diagnostic);
1178 warning_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1180 diagnostic_info diagnostic;
1181 VA_OPEN (ap, msgid);
1182 VA_FIXEDARG (ap, tree, decl);
1183 VA_FIXEDARG (ap, const char *, msgid);
1185 diagnostic_set_info (&diagnostic, msgid, &ap,
1186 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1188 diagnostic_for_decl (&diagnostic, decl);
1193 warning VPARAMS ((const char *msgid, ...))
1195 diagnostic_info diagnostic;
1197 VA_OPEN (ap, msgid);
1198 VA_FIXEDARG (ap, const char *, msgid);
1200 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1202 report_diagnostic (&diagnostic);
1207 /* Same as above but use diagnostic_buffer. */
1210 verbatim VPARAMS ((const char *msgid, ...))
1213 VA_OPEN (ap, msgid);
1214 VA_FIXEDARG (ap, const char *, msgid);
1216 text.format_spec = _(msgid);
1217 text.args_ptr = ≈
1218 output_do_verbatim (&global_dc->buffer, &text);
1219 output_buffer_to_stream (&global_dc->buffer);
1223 /* Report a diagnostic message (an error or a warning) as specified by
1224 DC. This function is *the* subroutine in terms of which front-ends
1225 should implement their specific diagnostic handling modules. The
1226 front-end independent format specifiers are exactly those described
1227 in the documentation of output_format. */
1230 diagnostic_report_diagnostic (context, diagnostic)
1231 diagnostic_context *context;
1232 diagnostic_info *diagnostic;
1234 if (context->lock++)
1235 error_recursion (context);
1237 if (diagnostic_count_diagnostic (context, diagnostic->kind))
1239 (*diagnostic_starter (context)) (context, diagnostic);
1240 output_format (&context->buffer, &diagnostic->message);
1241 (*diagnostic_finalizer (context)) (context, diagnostic);
1242 output_flush (&context->buffer);
1248 /* Inform the user that an error occurred while trying to report some
1249 other error. This indicates catastrophic internal inconsistencies,
1250 so give up now. But do try to flush out the previous error.
1251 This mustn't use internal_error, that will cause infinite recursion. */
1254 error_recursion (context)
1255 diagnostic_context *context;
1257 if (context->lock < 3)
1258 output_flush (&context->buffer);
1261 "Internal compiler error: Error reporting routines re-entered.\n");
1263 "Please submit a full bug report,\n\
1264 with preprocessed source if appropriate.\n\
1265 See %s for instructions.\n", GCCBUGURL);
1266 exit (FATAL_EXIT_CODE);
1269 /* Given a partial pathname as input, return another pathname that
1270 shares no directory elements with the pathname of __FILE__. This
1271 is used by fancy_abort() to print `Internal compiler error in expr.c'
1272 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1275 trim_filename (name)
1278 static const char this_file[] = __FILE__;
1279 const char *p = name, *q = this_file;
1281 /* First skip any "../" in each filename. This allows us to give a proper
1282 reference to a file in a subdirectory. */
1283 while (p[0] == '.' && p[1] == '.'
1284 && (p[2] == DIR_SEPARATOR
1285 #ifdef DIR_SEPARATOR_2
1286 || p[2] == DIR_SEPARATOR_2
1291 while (q[0] == '.' && q[1] == '.'
1292 && (q[2] == DIR_SEPARATOR
1293 #ifdef DIR_SEPARATOR_2
1294 || p[2] == DIR_SEPARATOR_2
1299 /* Now skip any parts the two filenames have in common. */
1300 while (*p == *q && *p != 0 && *q != 0)
1303 /* Now go backwards until the previous directory separator. */
1304 while (p > name && p[-1] != DIR_SEPARATOR
1305 #ifdef DIR_SEPARATOR_2
1306 && p[-1] != DIR_SEPARATOR_2
1314 /* Report an internal compiler error in a friendly manner and without
1318 fancy_abort (file, line, function)
1321 const char *function;
1323 internal_error ("Internal compiler error in %s, at %s:%d",
1324 function, trim_filename (file), line);
1328 diagnostic_report_current_module (context)
1329 diagnostic_context *context;
1331 struct file_stack *p;
1333 if (output_needs_newline (&context->buffer))
1335 output_add_newline (&context->buffer);
1336 output_needs_newline (&context->buffer) = false;
1339 if (input_file_stack && input_file_stack->next != 0
1340 && diagnostic_last_module_changed (context))
1342 for (p = input_file_stack->next; p; p = p->next)
1343 if (p == input_file_stack->next)
1344 output_verbatim (&context->buffer,
1345 "In file included from %s:%d", p->name, p->line);
1347 output_verbatim (&context->buffer,
1348 ",\n from %s:%d", p->name, p->line);
1349 output_verbatim (&context->buffer, ":\n");
1350 diagnostic_set_last_module (context);
1355 default_diagnostic_starter (context, diagnostic)
1356 diagnostic_context *context;
1357 diagnostic_info *diagnostic;
1359 diagnostic_report_current_function (context);
1360 output_set_prefix (&context->buffer, diagnostic_build_prefix (diagnostic));
1364 default_diagnostic_finalizer (context, diagnostic)
1365 diagnostic_context *context;
1366 diagnostic_info *diagnostic __attribute__((unused));
1368 output_destroy_prefix (&context->buffer);
1372 warn_deprecated_use (node)
1375 if (node == 0 || !warn_deprecated_decl)
1379 warning ("`%s' is deprecated (declared at %s:%d)",
1380 IDENTIFIER_POINTER (DECL_NAME (node)),
1381 DECL_SOURCE_FILE (node), DECL_SOURCE_LINE (node));
1382 else if (TYPE_P (node))
1384 const char *what = NULL;
1385 tree decl = TYPE_STUB_DECL (node);
1387 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
1388 what = IDENTIFIER_POINTER (TYPE_NAME (node));
1389 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
1390 && DECL_NAME (TYPE_NAME (node)))
1391 what = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node)));
1396 warning ("`%s' is deprecated (declared at %s:%d)", what,
1397 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
1399 warning ("`%s' is deprecated", what);
1402 warning ("type is deprecated (declared at %s:%d)",
1403 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
1405 warning ("type is deprecated");