1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999, 2000, 2001, 2002, 2003 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. */
30 #include "coretypes.h"
38 #include "diagnostic.h"
39 #include "langhooks.h"
40 #include "langhooks-def.h"
42 #define output_text_length(BUFFER) (BUFFER)->line_length
43 #define is_starting_newline(BUFFER) (output_text_length (BUFFER) == 0)
44 #define line_wrap_cutoff(BUFFER) (BUFFER)->state.maximum_length
45 #define prefix_was_emitted_for(BUFFER) (BUFFER)->state.emitted_prefix_p
48 static void output_flush (output_buffer *);
49 static void output_do_verbatim (output_buffer *, text_info *);
50 static void output_buffer_to_stream (output_buffer *);
51 static void output_format (output_buffer *, text_info *);
52 static void output_indent (output_buffer *);
54 static char *build_message_string (const char *, ...)
56 static void format_with_decl (output_buffer *, text_info *, tree);
57 static void diagnostic_for_decl (diagnostic_context *, diagnostic_info *,
59 static void set_real_maximum_length (output_buffer *);
61 static void output_unsigned_decimal (output_buffer *, unsigned int);
62 static void output_long_decimal (output_buffer *, long int);
63 static void output_long_unsigned_decimal (output_buffer *,
65 static void output_octal (output_buffer *, unsigned int);
66 static void output_long_octal (output_buffer *, unsigned long int);
67 static void output_hexadecimal (output_buffer *, unsigned int);
68 static void output_long_hexadecimal (output_buffer *, unsigned long int);
69 static void output_append_r (output_buffer *, const char *, int);
70 static void wrap_text (output_buffer *, const char *, const char *);
71 static void maybe_wrap_text (output_buffer *, const char *, const char *);
72 static void output_clear_data (output_buffer *);
74 static void default_diagnostic_starter (diagnostic_context *,
76 static void default_diagnostic_finalizer (diagnostic_context *,
79 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
80 static bool text_specifies_location (text_info *, location_t *);
81 static bool diagnostic_count_diagnostic (diagnostic_context *,
83 static void diagnostic_action_after_output (diagnostic_context *,
85 static void real_abort (void) ATTRIBUTE_NORETURN;
87 extern int rtl_dump_and_exit;
89 /* A diagnostic_context surrogate for stderr. */
90 static diagnostic_context global_diagnostic_context;
91 diagnostic_context *global_dc = &global_diagnostic_context;
93 /* Boilerplate text used in two locations. */
94 #define bug_report_request \
95 "Please submit a full bug report,\n\
96 with preprocessed source if appropriate.\n\
97 See %s for instructions.\n"
100 /* Subroutine of output_set_maximum_length. Set up BUFFER's
101 internal maximum characters per line. */
103 set_real_maximum_length (output_buffer *buffer)
105 /* If we're told not to wrap lines then do the obvious thing. In case
106 we'll emit prefix only once per diagnostic message, it is appropriate
107 not to increase unnecessarily the line-length cut-off. */
108 if (!output_is_line_wrapping (buffer)
109 || output_prefixing_rule (buffer) == DIAGNOSTICS_SHOW_PREFIX_ONCE
110 || output_prefixing_rule (buffer) == DIAGNOSTICS_SHOW_PREFIX_NEVER)
111 line_wrap_cutoff (buffer) = output_line_cutoff (buffer);
114 int prefix_length = buffer->state.prefix ?
115 strlen (buffer->state.prefix) : 0;
116 /* If the prefix is ridiculously too long, output at least
118 if (output_line_cutoff (buffer) - prefix_length < 32)
119 line_wrap_cutoff (buffer) = output_line_cutoff (buffer) + 32;
121 line_wrap_cutoff (buffer) = output_line_cutoff (buffer);
125 /* Sets the number of maximum characters per line BUFFER can output
126 in line-wrapping mode. A LENGTH value 0 suppresses line-wrapping. */
128 output_set_maximum_length (output_buffer *buffer, int length)
130 output_line_cutoff (buffer) = length;
131 set_real_maximum_length (buffer);
134 /* Sets BUFFER's PREFIX. */
136 output_set_prefix (output_buffer *buffer, const char *prefix)
138 buffer->state.prefix = prefix;
139 set_real_maximum_length (buffer);
140 prefix_was_emitted_for (buffer) = false;
141 output_indentation (buffer) = 0;
144 /* Return a pointer to the last character emitted in the output
145 BUFFER area. A NULL pointer means no character available. */
147 output_last_position (const output_buffer *buffer)
149 const char *p = NULL;
151 if (obstack_base (&buffer->obstack) != obstack_next_free (&buffer->obstack))
152 p = ((const char *) obstack_next_free (&buffer->obstack)) - 1;
156 /* Free BUFFER's prefix, a previously malloc'd string. */
158 output_destroy_prefix (output_buffer *buffer)
160 if (buffer->state.prefix != NULL)
162 free ((char *) buffer->state.prefix);
163 buffer->state.prefix = NULL;
167 /* Zero out any text output so far in BUFFER. */
169 output_clear_message_text (output_buffer *buffer)
171 obstack_free (&buffer->obstack, obstack_base (&buffer->obstack));
172 output_text_length (buffer) = 0;
175 /* Zero out any formatting data used so far by BUFFER. */
177 output_clear_data (output_buffer *buffer)
179 prefix_was_emitted_for (buffer) = false;
180 output_indentation (buffer) = 0;
183 /* Construct an output BUFFER with PREFIX and of MAXIMUM_LENGTH
184 characters per line. */
186 init_output_buffer (output_buffer *buffer, const char *prefix,
189 memset (buffer, 0, sizeof (output_buffer));
190 obstack_init (&buffer->obstack);
191 output_buffer_attached_stream (buffer) = stderr;
192 output_line_cutoff (buffer) = maximum_length;
193 output_prefixing_rule (buffer) = diagnostic_prefixing_rule (global_dc);
194 output_set_prefix (buffer, prefix);
195 output_text_length (buffer) = 0;
196 output_clear_data (buffer);
199 /* Reinitialize BUFFER. */
201 output_clear (output_buffer *buffer)
203 output_clear_message_text (buffer);
204 output_clear_data (buffer);
207 /* Finishes constructing a NULL-terminated character string representing
208 the BUFFERed message. */
210 output_finalize_message (output_buffer *buffer)
212 obstack_1grow (&buffer->obstack, '\0');
213 return output_message_text (buffer);
216 /* Return the amount of characters BUFFER can accept to
219 output_space_left (const output_buffer *buffer)
221 return line_wrap_cutoff (buffer) - output_text_length (buffer);
224 /* Write out BUFFER's prefix. */
226 output_emit_prefix (output_buffer *buffer)
228 if (buffer->state.prefix != NULL)
230 switch (output_prefixing_rule (buffer))
233 case DIAGNOSTICS_SHOW_PREFIX_NEVER:
236 case DIAGNOSTICS_SHOW_PREFIX_ONCE:
237 if (prefix_was_emitted_for (buffer))
239 output_indent (buffer);
242 output_indentation (buffer) += 3;
245 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
247 int prefix_length = strlen (buffer->state.prefix);
248 output_append_r (buffer, buffer->state.prefix, prefix_length);
249 prefix_was_emitted_for (buffer) = true;
256 /* Have BUFFER start a new line. */
258 output_add_newline (output_buffer *buffer)
260 obstack_1grow (&buffer->obstack, '\n');
261 output_text_length (buffer) = 0;
264 /* Appends a character to BUFFER. */
266 output_add_character (output_buffer *buffer, int c)
268 if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
269 output_add_newline (buffer);
270 obstack_1grow (&buffer->obstack, c);
271 ++output_text_length (buffer);
274 /* Adds a space to BUFFER. */
276 output_add_space (output_buffer *buffer)
278 if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
280 output_add_newline (buffer);
283 obstack_1grow (&buffer->obstack, ' ');
284 ++output_text_length (buffer);
287 /* These functions format an INTEGER into BUFFER as suggested by their
290 output_decimal (output_buffer *buffer, int i)
292 output_formatted_scalar (buffer, "%d", i);
296 output_long_decimal (output_buffer *buffer, long int i)
298 output_formatted_scalar (buffer, "%ld", i);
302 output_unsigned_decimal (output_buffer *buffer, unsigned int i)
304 output_formatted_scalar (buffer, "%u", i);
308 output_long_unsigned_decimal (output_buffer *buffer, long unsigned int i)
310 output_formatted_scalar (buffer, "%lu", i);
314 output_octal (output_buffer *buffer, unsigned int i)
316 output_formatted_scalar (buffer, "%o", i);
320 output_long_octal (output_buffer *buffer, long unsigned int i)
322 output_formatted_scalar (buffer, "%lo", i);
326 output_hexadecimal (output_buffer *buffer, unsigned int i)
328 output_formatted_scalar (buffer, "%x", i);
332 output_long_hexadecimal (output_buffer *buffer, long unsigned int i)
334 output_formatted_scalar (buffer, "%lx", i);
338 output_long_long_decimal (output_buffer *buffer, long long int i)
340 output_formatted_scalar (buffer, "%lld", i);
344 output_host_wide_integer (output_buffer *buffer, HOST_WIDE_INT i)
346 output_formatted_scalar (buffer, HOST_WIDE_INT_PRINT_DEC, i);
350 output_pointer (output_buffer *buffer, void *p)
352 output_formatted_scalar (buffer, HOST_PTR_PRINTF, p);
355 /* Append to BUFFER a string specified by its STARTING character
358 output_append_r (output_buffer *buffer, const char *start, int length)
360 obstack_grow (&buffer->obstack, start, length);
361 output_text_length (buffer) += length;
364 /* Append a string deliminated by START and END to BUFFER. No wrapping is
365 done. However, if beginning a new line then emit BUFFER->state.prefix
366 and skip any leading whitespace if appropriate. The caller must ensure
367 that it is safe to do so. */
369 output_append (output_buffer *buffer, const char *start, const char *end)
371 /* Emit prefix and skip whitespace if we're starting a new line. */
372 if (is_starting_newline (buffer))
374 output_emit_prefix (buffer);
375 if (output_is_line_wrapping (buffer))
376 while (start != end && *start == ' ')
379 output_append_r (buffer, start, end - start);
382 /* Insert enough spaces into BUFFER to bring the column position to
383 the current indentation level, assuming that a newline has just
384 been written to the buffer. */
386 output_indent (output_buffer *buffer)
388 int n = output_indentation (buffer);
391 for (i = 0; i < n; ++i)
392 output_add_character (buffer, ' ');
395 /* Wrap a text delimited by START and END into BUFFER. */
397 wrap_text (output_buffer *buffer, const char *start, const char *end)
399 bool is_wrapping = output_is_line_wrapping (buffer);
403 /* Dump anything bordered by whitespaces. */
405 const char *p = start;
406 while (p != end && *p != ' ' && *p != '\n')
408 if (is_wrapping && p - start >= output_space_left (buffer))
409 output_add_newline (buffer);
410 output_append (buffer, start, p);
414 if (start != end && *start == ' ')
416 output_add_space (buffer);
419 if (start != end && *start == '\n')
421 output_add_newline (buffer);
427 /* Same as wrap_text but wrap text only when in line-wrapping mode. */
429 maybe_wrap_text (output_buffer *buffer, const char *start, const char *end)
431 if (output_is_line_wrapping (buffer))
432 wrap_text (buffer, start, end);
434 output_append (buffer, start, end);
438 /* Append a STRING to BUFFER; the STRING might be line-wrapped if in
441 output_add_string (output_buffer *buffer, const char *str)
443 maybe_wrap_text (buffer, str, str + (str ? strlen (str) : 0));
446 /* Append an identifier ID to BUFFER. */
448 output_add_identifier (output_buffer *buffer, tree id)
450 output_append (buffer, IDENTIFIER_POINTER (id),
451 IDENTIFIER_POINTER (id) + IDENTIFIER_LENGTH (id));
454 /* Flush the content of BUFFER onto the attached stream,
458 output_buffer_to_stream (output_buffer *buffer)
460 const char *text = output_finalize_message (buffer);
461 fputs (text, output_buffer_attached_stream (buffer));
462 output_clear_message_text (buffer);
465 /* Format a message pointed to by TEXT. The following format specifiers are
466 recognized as being language independent:
467 %d, %i: (signed) integer in base ten.
468 %u: unsigned integer in base ten.
469 %o: unsigned integer in base eight.
470 %x: unsigned integer in base sixteen.
471 %ld, %li, %lo, %lu, %lx: long versions of the above.
473 %w: and integer of type HOST_WIDE_INT.
477 %m: strerror(text->err_no) - does not consume a value from args_ptr.
479 %*.s: a substring the length of which is specified by an integer.
482 output_format (output_buffer *buffer, text_info *text)
484 for (; *text->format_spec; ++text->format_spec)
486 bool long_integer = 0;
490 const char *p = text->format_spec;
491 while (*p && *p != '%')
493 wrap_text (buffer, text->format_spec, p);
494 text->format_spec = p;
497 if (*text->format_spec == '\0')
500 /* We got a '%'. Let's see what happens. Record whether we're
501 parsing a long integer format specifier. */
502 if (*++text->format_spec == 'l')
508 /* Handle %c, %d, %i, %ld, %li, %lo, %lu, %lx, %m, %o, %s, %u,
509 %x, %p, %.*s; %%. And nothing else. Front-ends should install
510 printers to grok language specific format specifiers. */
511 switch (*text->format_spec)
514 output_add_character (buffer, va_arg (*text->args_ptr, int));
520 output_long_decimal (buffer, va_arg (*text->args_ptr, long int));
522 output_decimal (buffer, va_arg (*text->args_ptr, int));
527 output_long_octal (buffer,
528 va_arg (*text->args_ptr, unsigned long int));
530 output_octal (buffer, va_arg (*text->args_ptr, unsigned int));
534 output_add_string (buffer, va_arg (*text->args_ptr, const char *));
538 output_pointer (buffer, va_arg (*text->args_ptr, void *));
543 output_long_unsigned_decimal
544 (buffer, va_arg (*text->args_ptr, long unsigned int));
546 output_unsigned_decimal
547 (buffer, va_arg (*text->args_ptr, unsigned int));
552 output_long_hexadecimal
553 (buffer, va_arg (*text->args_ptr, unsigned long int));
556 (buffer, va_arg (*text->args_ptr, unsigned int));
561 output_long_long_decimal
562 (buffer, va_arg (*text->args_ptr, long long));
564 /* Sould not happen. */
569 output_add_string (buffer, xstrerror (text->err_no));
573 output_add_character (buffer, '%');
578 const location_t *locus = va_arg (*text->args_ptr, location_t *);
579 output_add_string (buffer, "file '");
580 output_add_string (buffer, locus->file);
581 output_add_string (buffer, "', line ");
582 output_decimal (buffer, locus->line);
590 /* We handle no precision specifier but `%.*s'. */
591 if (*++text->format_spec != '*')
593 else if (*++text->format_spec != 's')
595 n = va_arg (*text->args_ptr, int);
596 s = va_arg (*text->args_ptr, const char *);
597 output_append (buffer, s, s + n);
602 output_host_wide_integer
603 (buffer, va_arg (*text->args_ptr, HOST_WIDE_INT));
607 if (!buffer->format_decoder
608 || !(*buffer->format_decoder) (buffer, text))
610 /* Hmmm. The front-end failed to install a format translator
611 but called us with an unrecognized format. Or, maybe, the
612 translated string just contains an invalid format, or
613 has formats in the wrong order. Sorry. */
620 /* Return a malloc'd string containing MSG formatted a la printf. The
621 caller is responsible for freeing the memory. */
623 build_message_string (const char *msg, ...)
629 vasprintf (&str, msg, ap);
635 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
637 file_name_as_prefix (const char *f)
639 return build_message_string ("%s: ", f);
642 /* Format a message into BUFFER a la printf. */
644 output_printf (struct output_buffer *buffer, const char *msgid, ...)
649 va_start (ap, msgid);
652 text.format_spec = _(msgid);
653 output_format (buffer, &text);
657 /* Print a message relevant to the given DECL. */
659 format_with_decl (output_buffer *buffer, text_info *text, tree decl)
663 /* Do magic to get around lack of varargs support for insertion
664 of arguments into existing list. We know that the decl is first;
665 we ass_u_me that it will be printed with "%s". */
666 for (p = text->format_spec; *p; ++p)
672 else if (*(p + 1) != 's')
679 /* Print the left-hand substring. */
680 maybe_wrap_text (buffer, text->format_spec, p);
682 if (*p == '%') /* Print the name. */
684 const char *const n = (DECL_NAME (decl)
685 ? (*lang_hooks.decl_printable_name) (decl, 2)
686 : _("((anonymous))"));
687 output_add_string (buffer, n);
691 if (ISALPHA (*(p - 1) & 0xFF))
696 if (*p) /* Print the rest of the message. */
698 text->format_spec = p;
699 output_format (buffer, text);
703 /* Flush the content of BUFFER onto the attached stream. */
705 output_flush (output_buffer *buffer)
707 output_buffer_to_stream (buffer);
708 output_clear_data (buffer);
709 fputc ('\n', output_buffer_attached_stream (buffer));
710 fflush (output_buffer_attached_stream (buffer));
713 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
714 settings needed by BUFFER for a verbatim formatting. */
716 output_do_verbatim (output_buffer *buffer, text_info *text)
718 diagnostic_prefixing_rule_t rule = output_prefixing_rule (buffer);
719 int line_cutoff = output_line_cutoff (buffer);
721 /* Set verbatim mode. */
722 output_prefixing_rule (buffer) = DIAGNOSTICS_SHOW_PREFIX_NEVER;
723 output_line_cutoff (buffer) = 0;
724 /* Do the actual formatting. */
725 output_format (buffer, text);
726 /* Restore previous settings. */
727 output_prefixing_rule (buffer) = rule;
728 output_line_cutoff (buffer) = line_cutoff;
731 /* Output MESSAGE verbatim into BUFFER. */
733 output_verbatim (output_buffer *buffer, const char *msgid, ...)
738 va_start (ap, msgid);
741 text.format_spec = _(msgid);
742 output_do_verbatim (buffer, &text);
747 /* Initialize the diagnostic message outputting machinery. */
749 diagnostic_initialize (diagnostic_context *context)
751 memset (context, 0, sizeof *context);
752 obstack_init (&context->buffer.obstack);
754 /* By default, diagnostics are sent to stderr. */
755 output_buffer_attached_stream (&context->buffer) = stderr;
757 /* By default, we emit prefixes once per message. */
758 diagnostic_prefixing_rule (context) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
760 diagnostic_starter (context) = default_diagnostic_starter;
761 diagnostic_finalizer (context) = default_diagnostic_finalizer;
762 context->warnings_are_errors_message = warnings_are_errors;
765 /* Returns true if the next format specifier in TEXT is a format specifier
766 for a location_t. If so, update the object pointed by LOCUS to reflect
767 the specified location in *TEXT->args_ptr. */
769 text_specifies_location (text_info *text, location_t *locus)
772 /* Skip any leading text. */
773 for (p = text->format_spec; *p && *p != '%'; ++p)
776 /* Extract the location information if any. */
777 if (*p == '%' && *++p == 'H')
779 *locus = *va_arg (*text->args_ptr, location_t *);
780 text->format_spec = p + 1;
788 diagnostic_set_info (diagnostic_info *diagnostic, const char *msgid,
789 va_list *args, const char *file, int line,
792 diagnostic->message.err_no = errno;
793 diagnostic->message.args_ptr = args;
794 diagnostic->message.format_spec = _(msgid);
795 /* If the diagnostic message doesn't specify a location,
796 use FILE and LINE. */
797 if (!text_specifies_location (&diagnostic->message, &diagnostic->location))
799 diagnostic->location.file = file;
800 diagnostic->location.line = line;
802 diagnostic->kind = kind;
805 /* Return a malloc'd string describing a location. The caller is
806 responsible for freeing the memory. */
808 diagnostic_build_prefix (diagnostic_info *diagnostic)
810 static const char *const diagnostic_kind_text[] = {
811 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
812 #include "diagnostic.def"
813 #undef DEFINE_DIAGNOSTIC_KIND
816 if (diagnostic->kind >= DK_LAST_DIAGNOSTIC_KIND)
819 return diagnostic->location.file
820 ? build_message_string ("%s:%d: %s",
821 diagnostic->location.file,
822 diagnostic->location.line,
823 _(diagnostic_kind_text[diagnostic->kind]))
824 : build_message_string ("%s: %s", progname,
825 _(diagnostic_kind_text[diagnostic->kind]));
829 diagnostic_flush_buffer (diagnostic_context *context)
831 output_buffer_to_stream (&context->buffer);
832 fflush (output_buffer_attached_stream (&context->buffer));
835 /* Count a diagnostic. Return true if the message should be printed. */
837 diagnostic_count_diagnostic (diagnostic_context *context,
838 diagnostic_info *diagnostic)
840 diagnostic_t kind = diagnostic->kind;
848 #ifndef ENABLE_CHECKING
849 /* When not checking, ICEs are converted to fatal errors when an
850 error has already occurred. This is counteracted by
852 if ((diagnostic_kind_count (context, DK_ERROR) > 0
853 || diagnostic_kind_count (context, DK_SORRY) > 0)
854 && !context->abort_on_error)
856 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
857 diagnostic->location.file, diagnostic->location.line);
858 exit (FATAL_EXIT_CODE);
861 if (context->internal_error)
862 (*context->internal_error) (diagnostic->message.format_spec,
863 diagnostic->message.args_ptr);
866 case DK_FATAL: case DK_SORRY:
867 case DK_ANACHRONISM: case DK_NOTE:
868 ++diagnostic_kind_count (context, kind);
872 if (!diagnostic_report_warnings_p ())
875 if (!warnings_are_errors)
877 ++diagnostic_kind_count (context, DK_WARNING);
881 if (context->warnings_are_errors_message)
883 output_verbatim (&context->buffer,
884 "%s: warnings being treated as errors\n", progname);
885 context->warnings_are_errors_message = false;
888 /* and fall through */
890 ++diagnostic_kind_count (context, DK_ERROR);
897 /* Take any action which is expected to happen after the diagnostic
898 is written out. This function does not always return. */
900 diagnostic_action_after_output (diagnostic_context *context,
901 diagnostic_info *diagnostic)
903 switch (diagnostic->kind)
913 if (context->abort_on_error)
918 if (context->abort_on_error)
921 fnotice (stderr, bug_report_request, bug_report_url);
922 exit (FATAL_EXIT_CODE);
925 if (context->abort_on_error)
928 fnotice (stderr, "compilation terminated.\n");
929 exit (FATAL_EXIT_CODE);
936 /* Called when the start of a function definition is parsed,
937 this function prints on stderr the name of the function. */
939 announce_function (tree decl)
943 if (rtl_dump_and_exit)
944 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl)));
946 verbatim (" %s", (*lang_hooks.decl_printable_name) (decl, 2));
948 output_needs_newline (&global_dc->buffer) = true;
949 diagnostic_set_last_function (global_dc);
953 /* The default function to print out name of current function that caused
956 lhd_print_error_function (diagnostic_context *context, const char *file)
958 if (diagnostic_last_function_changed (context))
960 const char *old_prefix = output_prefix (&context->buffer);
961 char *new_prefix = file ? build_message_string ("%s: ", file) : NULL;
963 output_set_prefix (&context->buffer, new_prefix);
965 if (current_function_decl == NULL)
966 output_add_string (&context->buffer, _("At top level:"));
969 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
971 (&context->buffer, "In member function `%s':",
972 (*lang_hooks.decl_printable_name) (current_function_decl, 2));
975 (&context->buffer, "In function `%s':",
976 (*lang_hooks.decl_printable_name) (current_function_decl, 2));
978 output_add_newline (&context->buffer);
980 diagnostic_set_last_function (context);
981 output_buffer_to_stream (&context->buffer);
982 context->buffer.state.prefix = old_prefix;
983 free ((char*) new_prefix);
987 /* Prints out, if necessary, the name of the current function
988 that caused an error. Called from all error and warning functions.
989 We ignore the FILE parameter, as it cannot be relied upon. */
992 diagnostic_report_current_function (diagnostic_context *context)
994 diagnostic_report_current_module (context);
995 (*lang_hooks.print_error_function) (context, input_filename);
999 diagnostic_report_current_module (diagnostic_context *context)
1001 struct file_stack *p;
1003 if (output_needs_newline (&context->buffer))
1005 output_add_newline (&context->buffer);
1006 output_needs_newline (&context->buffer) = false;
1009 if (input_file_stack && input_file_stack->next != 0
1010 && diagnostic_last_module_changed (context))
1012 for (p = input_file_stack->next; p; p = p->next)
1013 if (p == input_file_stack->next)
1014 output_verbatim (&context->buffer,
1015 "In file included from %s:%d",
1016 p->location.file, p->location.line);
1018 output_verbatim (&context->buffer,
1020 p->location.file, p->location.line);
1021 output_verbatim (&context->buffer, ":\n");
1022 diagnostic_set_last_module (context);
1027 default_diagnostic_starter (diagnostic_context *context,
1028 diagnostic_info *diagnostic)
1030 diagnostic_report_current_function (context);
1031 output_set_prefix (&context->buffer, diagnostic_build_prefix (diagnostic));
1035 default_diagnostic_finalizer (context, diagnostic)
1036 diagnostic_context *context;
1037 diagnostic_info *diagnostic __attribute__((unused));
1039 output_destroy_prefix (&context->buffer);
1042 /* Report a diagnostic message (an error or a warning) as specified by
1043 DC. This function is *the* subroutine in terms of which front-ends
1044 should implement their specific diagnostic handling modules. The
1045 front-end independent format specifiers are exactly those described
1046 in the documentation of output_format. */
1049 diagnostic_report_diagnostic (diagnostic_context *context,
1050 diagnostic_info *diagnostic)
1052 if (context->lock++)
1053 error_recursion (context);
1055 if (diagnostic_count_diagnostic (context, diagnostic))
1057 (*diagnostic_starter (context)) (context, diagnostic);
1058 output_format (&context->buffer, &diagnostic->message);
1059 (*diagnostic_finalizer (context)) (context, diagnostic);
1060 output_flush (&context->buffer);
1061 diagnostic_action_after_output (context, diagnostic);
1067 /* Report a diagnostic MESSAGE at the declaration DECL.
1068 MSG is a format string which uses %s to substitute the declaration
1069 name; subsequent substitutions are a la output_format. */
1071 diagnostic_for_decl (diagnostic_context *context,
1072 diagnostic_info *diagnostic, tree decl)
1074 if (context->lock++)
1075 error_recursion (context);
1077 if (diagnostic_count_diagnostic (context, diagnostic))
1079 (*diagnostic_starter (context)) (context, diagnostic);
1080 format_with_decl (&context->buffer, &diagnostic->message, decl);
1081 (*diagnostic_finalizer (context)) (context, diagnostic);
1082 output_flush (&context->buffer);
1083 diagnostic_action_after_output (context, diagnostic);
1089 /* Given a partial pathname as input, return another pathname that
1090 shares no directory elements with the pathname of __FILE__. This
1091 is used by fancy_abort() to print `Internal compiler error in expr.c'
1092 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1095 trim_filename (const char *name)
1097 static const char this_file[] = __FILE__;
1098 const char *p = name, *q = this_file;
1100 /* First skip any "../" in each filename. This allows us to give a proper
1101 reference to a file in a subdirectory. */
1102 while (p[0] == '.' && p[1] == '.'
1103 && (p[2] == DIR_SEPARATOR
1104 #ifdef DIR_SEPARATOR_2
1105 || p[2] == DIR_SEPARATOR_2
1110 while (q[0] == '.' && q[1] == '.'
1111 && (q[2] == DIR_SEPARATOR
1112 #ifdef DIR_SEPARATOR_2
1113 || p[2] == DIR_SEPARATOR_2
1118 /* Now skip any parts the two filenames have in common. */
1119 while (*p == *q && *p != 0 && *q != 0)
1122 /* Now go backwards until the previous directory separator. */
1123 while (p > name && p[-1] != DIR_SEPARATOR
1124 #ifdef DIR_SEPARATOR_2
1125 && p[-1] != DIR_SEPARATOR_2
1133 /* Standard error reporting routines in increasing order of severity.
1134 All of these take arguments like printf. */
1136 /* Text to be emitted verbatim to the error message stream; this
1137 produces no prefix and disables line-wrapping. Use rarely. */
1139 verbatim (const char *msgid, ...)
1144 va_start (ap, msgid);
1145 text.err_no = errno;
1146 text.args_ptr = ≈
1147 text.format_spec = _(msgid);
1148 output_do_verbatim (&global_dc->buffer, &text);
1149 output_buffer_to_stream (&global_dc->buffer);
1153 /* An informative note. Use this for additional details on an error
1156 inform (const char *msgid, ...)
1158 diagnostic_info diagnostic;
1161 va_start (ap, msgid);
1162 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, input_line,
1164 report_diagnostic (&diagnostic);
1168 /* A warning. Use this for code which is correct according to the
1169 relevant language specification but is likely to be buggy anyway. */
1171 warning (const char *msgid, ...)
1173 diagnostic_info diagnostic;
1176 va_start (ap, msgid);
1177 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, input_line,
1179 report_diagnostic (&diagnostic);
1183 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
1184 given on the command line, in which case it issues an error. Use
1185 this for diagnostics required by the relevant language standard,
1186 if you have chosen not to make them errors.
1188 Note that these diagnostics are issued independent of the setting
1189 of the -pedantic command-line switch. To get a warning enabled
1190 only with that switch, write "if (pedantic) pedwarn (...);" */
1192 pedwarn (const char *msgid, ...)
1194 diagnostic_info diagnostic;
1197 va_start (ap, msgid);
1198 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, input_line,
1199 pedantic_error_kind ());
1200 report_diagnostic (&diagnostic);
1204 /* A hard error: the code is definitely ill-formed, and an object file
1205 will not be produced. */
1207 error (const char *msgid, ...)
1209 diagnostic_info diagnostic;
1212 va_start (ap, msgid);
1213 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, input_line,
1215 report_diagnostic (&diagnostic);
1219 /* "Sorry, not implemented." Use for a language feature which is
1220 required by the relevant specification but not implemented by GCC.
1221 An object file will not be produced. */
1223 sorry (const char *msgid, ...)
1225 diagnostic_info diagnostic;
1228 va_start (ap, msgid);
1229 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, input_line,
1231 report_diagnostic (&diagnostic);
1235 /* An error which is severe enough that we make no attempt to
1236 continue. Do not use this for internal consistency checks; that's
1237 internal_error. Use of this function should be rare. */
1239 fatal_error (const char *msgid, ...)
1241 diagnostic_info diagnostic;
1244 va_start (ap, msgid);
1245 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, input_line,
1247 report_diagnostic (&diagnostic);
1254 /* An internal consistency check has failed. We make no attempt to
1255 continue. Note that unless there is debugging value to be had from
1256 a more specific message, or some other good reason, you should use
1257 abort () instead of calling this function directly. */
1259 internal_error (const char *msgid, ...)
1261 diagnostic_info diagnostic;
1264 va_start (ap, msgid);
1265 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, input_line,
1267 report_diagnostic (&diagnostic);
1274 /* Variants of some of the above, which make reference to a particular
1275 DECL node. These are deprecated. */
1278 warning_with_decl (tree decl, const char *msgid, ...)
1280 diagnostic_info diagnostic;
1283 va_start (ap, msgid);
1285 /* Do not issue a warning about a decl which came from a system header,
1286 unless -Wsystem-headers. */
1287 if (DECL_IN_SYSTEM_HEADER (decl) && !warn_system_headers)
1290 diagnostic_set_info (&diagnostic, msgid, &ap,
1291 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1293 diagnostic_for_decl (global_dc, &diagnostic, decl);
1298 pedwarn_with_decl (tree decl, const char *msgid, ...)
1300 diagnostic_info diagnostic;
1303 va_start (ap, msgid);
1305 /* Do not issue a warning about a decl which came from a system header,
1306 unless -Wsystem-headers. */
1307 if (DECL_IN_SYSTEM_HEADER (decl) && !warn_system_headers)
1310 diagnostic_set_info (&diagnostic, msgid, &ap,
1311 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1312 pedantic_error_kind ());
1313 diagnostic_for_decl (global_dc, &diagnostic, decl);
1319 error_with_decl (tree decl, const char *msgid, ...)
1321 diagnostic_info diagnostic;
1324 va_start (ap, msgid);
1325 diagnostic_set_info (&diagnostic, msgid, &ap,
1326 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1328 diagnostic_for_decl (global_dc, &diagnostic, decl);
1332 /* Special case error functions. Most are implemented in terms of the
1333 above, or should be. */
1335 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1336 runs its second argument through gettext. */
1338 fnotice (FILE *file, const char *msgid, ...)
1342 va_start (ap, msgid);
1343 vfprintf (file, _(msgid), ap);
1347 /* Warn about a use of an identifier which was marked deprecated. */
1349 warn_deprecated_use (tree node)
1351 if (node == 0 || !warn_deprecated_decl)
1355 warning ("`%s' is deprecated (declared at %s:%d)",
1356 IDENTIFIER_POINTER (DECL_NAME (node)),
1357 DECL_SOURCE_FILE (node), DECL_SOURCE_LINE (node));
1358 else if (TYPE_P (node))
1360 const char *what = NULL;
1361 tree decl = TYPE_STUB_DECL (node);
1363 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
1364 what = IDENTIFIER_POINTER (TYPE_NAME (node));
1365 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
1366 && DECL_NAME (TYPE_NAME (node)))
1367 what = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node)));
1372 warning ("`%s' is deprecated (declared at %s:%d)", what,
1373 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
1375 warning ("`%s' is deprecated", what);
1378 warning ("type is deprecated (declared at %s:%d)",
1379 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
1381 warning ("type is deprecated");
1385 /* Inform the user that an error occurred while trying to report some
1386 other error. This indicates catastrophic internal inconsistencies,
1387 so give up now. But do try to flush out the previous error.
1388 This mustn't use internal_error, that will cause infinite recursion. */
1391 error_recursion (diagnostic_context *context)
1393 if (context->lock < 3)
1394 output_flush (&context->buffer);
1397 "Internal compiler error: Error reporting routines re-entered.\n");
1398 fnotice (stderr, bug_report_request, bug_report_url);
1399 exit (FATAL_EXIT_CODE);
1402 /* Report an internal compiler error in a friendly manner. This is
1403 the function that gets called upon use of abort() in the source
1404 code generally, thanks to a special macro. */
1407 fancy_abort (const char *file, int line, const char *function)
1409 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1412 /* Really call the system 'abort'. This has to go right at the end of
1413 this file, so that there are no functions after it that call abort
1414 and get the system abort instead of our macro. */