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 output_text_length(BUFFER) (BUFFER)->line_length
41 #define is_starting_newline(BUFFER) (output_text_length (BUFFER) == 0)
42 #define line_wrap_cutoff(BUFFER) (BUFFER)->state.maximum_length
43 #define prefix_was_emitted_for(BUFFER) (BUFFER)->state.emitted_prefix_p
46 static void output_flush PARAMS ((output_buffer *));
47 static void output_do_verbatim PARAMS ((output_buffer *, text_info *));
48 static void output_buffer_to_stream PARAMS ((output_buffer *));
49 static void output_format PARAMS ((output_buffer *, text_info *));
50 static void output_indent PARAMS ((output_buffer *));
52 static char *vbuild_message_string PARAMS ((const char *, va_list))
53 ATTRIBUTE_PRINTF (1, 0);
54 static char *build_message_string PARAMS ((const char *, ...))
56 static void format_with_decl PARAMS ((output_buffer *, text_info *, tree));
57 static void diagnostic_for_decl PARAMS ((diagnostic_info *, tree));
58 static void set_real_maximum_length PARAMS ((output_buffer *));
60 static void output_unsigned_decimal PARAMS ((output_buffer *, unsigned int));
61 static void output_long_decimal PARAMS ((output_buffer *, long int));
62 static void output_long_unsigned_decimal PARAMS ((output_buffer *,
64 static void output_octal PARAMS ((output_buffer *, unsigned int));
65 static void output_long_octal PARAMS ((output_buffer *, unsigned long int));
66 static void output_hexadecimal PARAMS ((output_buffer *, unsigned int));
67 static void output_long_hexadecimal PARAMS ((output_buffer *,
69 static void output_append_r PARAMS ((output_buffer *, const char *, int));
70 static void wrap_text PARAMS ((output_buffer *, const char *, const char *));
71 static void maybe_wrap_text PARAMS ((output_buffer *, const char *,
73 static void output_clear_data PARAMS ((output_buffer *));
75 static void default_diagnostic_starter PARAMS ((diagnostic_context *,
77 static void default_diagnostic_finalizer PARAMS ((diagnostic_context *,
80 static void error_recursion PARAMS ((diagnostic_context *)) ATTRIBUTE_NORETURN;
81 static bool text_specifies_location PARAMS ((text_info *, location_t *));
83 extern int rtl_dump_and_exit;
84 extern int warnings_are_errors;
86 /* A diagnostic_context surrogate for stderr. */
87 static diagnostic_context global_diagnostic_context;
88 diagnostic_context *global_dc = &global_diagnostic_context;
91 /* Subroutine of output_set_maximum_length. Set up BUFFER's
92 internal maximum characters per line. */
94 set_real_maximum_length (buffer)
95 output_buffer *buffer;
97 /* If we're told not to wrap lines then do the obvious thing. In case
98 we'll emit prefix only once per diagnostic message, it is appropriate
99 not to increase unnecessarily the line-length cut-off. */
100 if (!output_is_line_wrapping (buffer)
101 || output_prefixing_rule (buffer) == DIAGNOSTICS_SHOW_PREFIX_ONCE
102 || output_prefixing_rule (buffer) == DIAGNOSTICS_SHOW_PREFIX_NEVER)
103 line_wrap_cutoff (buffer) = output_line_cutoff (buffer);
106 int prefix_length = buffer->state.prefix ?
107 strlen (buffer->state.prefix) : 0;
108 /* If the prefix is ridiculously too long, output at least
110 if (output_line_cutoff (buffer) - prefix_length < 32)
111 line_wrap_cutoff (buffer) = output_line_cutoff (buffer) + 32;
113 line_wrap_cutoff (buffer) = output_line_cutoff (buffer);
117 /* Sets the number of maximum characters per line BUFFER can output
118 in line-wrapping mode. A LENGTH value 0 suppresses line-wrapping. */
120 output_set_maximum_length (buffer, length)
121 output_buffer *buffer;
124 output_line_cutoff (buffer) = length;
125 set_real_maximum_length (buffer);
128 /* Sets BUFFER's PREFIX. */
130 output_set_prefix (buffer, prefix)
131 output_buffer *buffer;
134 buffer->state.prefix = prefix;
135 set_real_maximum_length (buffer);
136 prefix_was_emitted_for (buffer) = false;
137 output_indentation (buffer) = 0;
140 /* Return a pointer to the last character emitted in the output
141 BUFFER area. A NULL pointer means no character available. */
143 output_last_position (buffer)
144 const output_buffer *buffer;
146 const char *p = NULL;
148 if (obstack_base (&buffer->obstack) != obstack_next_free (&buffer->obstack))
149 p = ((const char *) obstack_next_free (&buffer->obstack)) - 1;
153 /* Free BUFFER's prefix, a previously malloc'd string. */
155 output_destroy_prefix (buffer)
156 output_buffer *buffer;
158 if (buffer->state.prefix != NULL)
160 free ((char *) buffer->state.prefix);
161 buffer->state.prefix = NULL;
165 /* Zero out any text output so far in BUFFER. */
167 output_clear_message_text (buffer)
168 output_buffer *buffer;
170 obstack_free (&buffer->obstack, obstack_base (&buffer->obstack));
171 output_text_length (buffer) = 0;
174 /* Zero out any formatting data used so far by BUFFER. */
176 output_clear_data (buffer)
177 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 (buffer, prefix, maximum_length)
187 output_buffer *buffer;
191 memset (buffer, 0, sizeof (output_buffer));
192 obstack_init (&buffer->obstack);
193 output_buffer_attached_stream (buffer) = stderr;
194 output_line_cutoff (buffer) = maximum_length;
195 output_prefixing_rule (buffer) = diagnostic_prefixing_rule (global_dc);
196 output_set_prefix (buffer, prefix);
197 output_text_length (buffer) = 0;
198 output_clear_data (buffer);
201 /* Reinitialize BUFFER. */
203 output_clear (buffer)
204 output_buffer *buffer;
206 output_clear_message_text (buffer);
207 output_clear_data (buffer);
210 /* Finishes constructing a NULL-terminated character string representing
211 the BUFFERed message. */
213 output_finalize_message (buffer)
214 output_buffer *buffer;
216 obstack_1grow (&buffer->obstack, '\0');
217 return output_message_text (buffer);
220 /* Return the amount of characters BUFFER can accept to
223 output_space_left (buffer)
224 const output_buffer *buffer;
226 return line_wrap_cutoff (buffer) - output_text_length (buffer);
229 /* Write out BUFFER's prefix. */
231 output_emit_prefix (buffer)
232 output_buffer *buffer;
234 if (buffer->state.prefix != NULL)
236 switch (output_prefixing_rule (buffer))
239 case DIAGNOSTICS_SHOW_PREFIX_NEVER:
242 case DIAGNOSTICS_SHOW_PREFIX_ONCE:
243 if (prefix_was_emitted_for (buffer))
245 output_indent (buffer);
248 output_indentation (buffer) += 3;
251 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
253 int prefix_length = strlen (buffer->state.prefix);
254 output_append_r (buffer, buffer->state.prefix, prefix_length);
255 prefix_was_emitted_for (buffer) = true;
262 /* Have BUFFER start a new line. */
264 output_add_newline (buffer)
265 output_buffer *buffer;
267 obstack_1grow (&buffer->obstack, '\n');
268 output_text_length (buffer) = 0;
271 /* Appends a character to BUFFER. */
273 output_add_character (buffer, c)
274 output_buffer *buffer;
277 if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
278 output_add_newline (buffer);
279 obstack_1grow (&buffer->obstack, c);
280 ++output_text_length (buffer);
283 /* Adds a space to BUFFER. */
285 output_add_space (buffer)
286 output_buffer *buffer;
288 if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
290 output_add_newline (buffer);
293 obstack_1grow (&buffer->obstack, ' ');
294 ++output_text_length (buffer);
297 /* These functions format an INTEGER into BUFFER as suggested by their
300 output_decimal (buffer, i)
301 output_buffer *buffer;
304 output_formatted_integer (buffer, "%d", i);
308 output_long_decimal (buffer, i)
309 output_buffer *buffer;
312 output_formatted_integer (buffer, "%ld", i);
316 output_unsigned_decimal (buffer, i)
317 output_buffer *buffer;
320 output_formatted_integer (buffer, "%u", i);
324 output_long_unsigned_decimal (buffer, i)
325 output_buffer *buffer;
328 output_formatted_integer (buffer, "%lu", i);
332 output_octal (buffer, i)
333 output_buffer *buffer;
336 output_formatted_integer (buffer, "%o", i);
340 output_long_octal (buffer, i)
341 output_buffer *buffer;
344 output_formatted_integer (buffer, "%lo", i);
348 output_hexadecimal (buffer, i)
349 output_buffer *buffer;
352 output_formatted_integer (buffer, "%x", i);
356 output_long_hexadecimal (buffer, i)
357 output_buffer *buffer;
360 output_formatted_integer (buffer, "%lx", i);
363 /* Append to BUFFER a string specified by its STARTING character
366 output_append_r (buffer, start, length)
367 output_buffer *buffer;
371 obstack_grow (&buffer->obstack, start, length);
372 output_text_length (buffer) += length;
375 /* Append a string deliminated by START and END to BUFFER. No wrapping is
376 done. However, if beginning a new line then emit BUFFER->state.prefix
377 and skip any leading whitespace if appropriate. The caller must ensure
378 that it is safe to do so. */
380 output_append (buffer, start, end)
381 output_buffer *buffer;
385 /* Emit prefix and skip whitespace if we're starting a new line. */
386 if (is_starting_newline (buffer))
388 output_emit_prefix (buffer);
389 if (output_is_line_wrapping (buffer))
390 while (start != end && *start == ' ')
393 output_append_r (buffer, start, end - start);
397 output_indent (buffer)
398 output_buffer *buffer;
400 int n = output_indentation (buffer);
403 for (i = 0; i < n; ++i)
404 output_add_character (buffer, ' ');
407 /* Wrap a text delimited by START and END into BUFFER. */
409 wrap_text (buffer, start, end)
410 output_buffer *buffer;
414 bool is_wrapping = output_is_line_wrapping (buffer);
418 /* Dump anything bordered by whitespaces. */
420 const char *p = start;
421 while (p != end && *p != ' ' && *p != '\n')
423 if (is_wrapping && p - start >= output_space_left (buffer))
424 output_add_newline (buffer);
425 output_append (buffer, start, p);
429 if (start != end && *start == ' ')
431 output_add_space (buffer);
434 if (start != end && *start == '\n')
436 output_add_newline (buffer);
442 /* Same as wrap_text but wrap text only when in line-wrapping mode. */
444 maybe_wrap_text (buffer, start, end)
445 output_buffer *buffer;
449 if (output_is_line_wrapping (buffer))
450 wrap_text (buffer, start, end);
452 output_append (buffer, start, end);
456 /* Append a STRING to BUFFER; the STRING might be line-wrapped if in
459 output_add_string (buffer, str)
460 output_buffer *buffer;
463 maybe_wrap_text (buffer, str, str + (str ? strlen (str) : 0));
466 /* Flush the content of BUFFER onto the attached stream,
470 output_buffer_to_stream (buffer)
471 output_buffer *buffer;
473 const char *text = output_finalize_message (buffer);
474 fputs (text, output_buffer_attached_stream (buffer));
475 output_clear_message_text (buffer);
478 /* Format a message pointed to by TEXT. The following format specifiers are
479 recognized as being language independent:
480 %d, %i: (signed) integer in base ten.
481 %u: unsigned integer in base ten.
482 %o: unsigned integer in base eight.
483 %x: unsigned integer in base sixteen.
484 %ld, %li, %lo, %lu, %lx: long versions of the above.
488 %*.s: a substring the length of which is specified by an integer.
491 output_format (buffer, text)
492 output_buffer *buffer;
495 for (; *text->format_spec; ++text->format_spec)
497 bool long_integer = 0;
501 const char *p = text->format_spec;
502 while (*p && *p != '%')
504 wrap_text (buffer, text->format_spec, p);
505 text->format_spec = p;
508 if (*text->format_spec == '\0')
511 /* We got a '%'. Let's see what happens. Record whether we're
512 parsing a long integer format specifier. */
513 if (*++text->format_spec == 'l')
519 /* Handle %c, %d, %i, %ld, %li, %lo, %lu, %lx, %o, %s, %u,
520 %x, %.*s; %%. And nothing else. Front-ends should install
521 printers to grok language specific format specifiers. */
522 switch (*text->format_spec)
525 output_add_character (buffer, va_arg (*text->args_ptr, int));
531 output_long_decimal (buffer, va_arg (*text->args_ptr, long int));
533 output_decimal (buffer, va_arg (*text->args_ptr, int));
538 output_long_octal (buffer,
539 va_arg (*text->args_ptr, unsigned long int));
541 output_octal (buffer, va_arg (*text->args_ptr, unsigned int));
545 output_add_string (buffer, va_arg (*text->args_ptr, const char *));
550 output_long_unsigned_decimal
551 (buffer, va_arg (*text->args_ptr, long unsigned int));
553 output_unsigned_decimal
554 (buffer, va_arg (*text->args_ptr, unsigned int));
559 output_long_hexadecimal
560 (buffer, va_arg (*text->args_ptr, unsigned long int));
563 (buffer, va_arg (*text->args_ptr, unsigned int));
567 output_add_character (buffer, '%');
572 const location_t *locus = va_arg (*text->args_ptr, location_t *);
573 output_add_string (buffer, "file '");
574 output_add_string (buffer, locus->file);
575 output_add_string (buffer, "', line ");
576 output_decimal (buffer, locus->line);
584 /* We handle no precision specifier but `%.*s'. */
585 if (*++text->format_spec != '*')
587 else if (*++text->format_spec != 's')
589 n = va_arg (*text->args_ptr, int);
590 s = va_arg (*text->args_ptr, const char *);
591 output_append (buffer, s, s + n);
596 if (!buffer->format_decoder
597 || !(*buffer->format_decoder) (buffer, text))
599 /* Hmmm. The front-end failed to install a format translator
600 but called us with an unrecognized format. Sorry. */
608 vbuild_message_string (msg, ap)
614 vasprintf (&str, msg, ap);
618 /* Return a malloc'd string containing MSG formatted a la
619 printf. The caller is responsible for freeing the memory. */
621 build_message_string VPARAMS ((const char *msg, ...))
626 VA_FIXEDARG (ap, const char *, msg);
628 str = vbuild_message_string (msg, ap);
635 /* Same as diagnsotic_build_prefix, but only the source FILE is given. */
637 file_name_as_prefix (f)
640 return build_message_string ("%s: ", f);
643 /* Format a message into BUFFER a la printf. */
645 output_printf VPARAMS ((struct output_buffer *buffer, const char *msgid, ...))
649 VA_FIXEDARG (ap, output_buffer *, buffer);
650 VA_FIXEDARG (ap, const char *, msgid);
653 text.format_spec = _(msgid);
654 output_format (buffer, &text);
658 /* Print a message relevant to the given DECL. */
660 format_with_decl (buffer, text, decl)
661 output_buffer *buffer;
667 /* Do magic to get around lack of varargs support for insertion
668 of arguments into existing list. We know that the decl is first;
669 we ass_u_me that it will be printed with "%s". */
670 for (p = text->format_spec; *p; ++p)
676 else if (*(p + 1) != 's')
683 /* Print the left-hand substring. */
684 maybe_wrap_text (buffer, text->format_spec, p);
686 if (*p == '%') /* Print the name. */
688 const char *const n = (DECL_NAME (decl)
689 ? (*lang_hooks.decl_printable_name) (decl, 2)
690 : _("((anonymous))"));
691 output_add_string (buffer, n);
695 if (ISALPHA (*(p - 1) & 0xFF))
700 if (*p) /* Print the rest of the message. */
702 text->format_spec = p;
703 output_format (buffer, text);
707 /* Flush the content of BUFFER onto the attached stream. */
709 output_flush (buffer)
710 output_buffer *buffer;
712 output_buffer_to_stream (buffer);
713 output_clear_data (buffer);
714 fputc ('\n', output_buffer_attached_stream (buffer));
715 fflush (output_buffer_attached_stream (buffer));
718 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
719 settings needed by BUFFER for a verbatim formatting. */
721 output_do_verbatim (buffer, text)
722 output_buffer *buffer;
725 diagnostic_prefixing_rule_t rule = output_prefixing_rule (buffer);
726 int line_cutoff = output_line_cutoff (buffer);
728 /* Set verbatim mode. */
729 output_prefixing_rule (buffer) = DIAGNOSTICS_SHOW_PREFIX_NEVER;
730 output_line_cutoff (buffer) = 0;
731 /* Do the actual formatting. */
732 output_format (buffer, text);
733 /* Restore previous settings. */
734 output_prefixing_rule (buffer) = rule;
735 output_line_cutoff (buffer) = line_cutoff;
738 /* Output MESSAGE verbatim into BUFFER. */
740 output_verbatim VPARAMS ((output_buffer *buffer, const char *msgid, ...))
744 VA_FIXEDARG (ap, output_buffer *, buffer);
745 VA_FIXEDARG (ap, const char *, msgid);
747 text.format_spec = msgid;
749 output_do_verbatim (buffer, &text);
754 /* Initialize the diagnostic message outputting machinery. */
756 diagnostic_initialize (context)
757 diagnostic_context *context;
759 memset (context, 0, sizeof *context);
760 obstack_init (&context->buffer.obstack);
762 /* By default, diagnostics are sent to stderr. */
763 output_buffer_attached_stream (&context->buffer) = stderr;
765 /* By default, we emit prefixes once per message. */
766 diagnostic_prefixing_rule (context) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
768 diagnostic_starter (context) = default_diagnostic_starter;
769 diagnostic_finalizer (context) = default_diagnostic_finalizer;
770 context->warnings_are_errors_message = warnings_are_errors;
773 /* Returns true if the next format specifier in TEXT is a format specifier
774 for a location_t. If so, update the object pointed by LOCUS to reflect
775 the specified location in *TEXT->args_ptr. */
777 text_specifies_location (text, locus)
782 /* Skip any leading text. */
783 for (p = text->format_spec; *p && *p != '%'; ++p)
786 /* Extract the location information if any. */
787 if (*p == '%' && *++p == 'H')
789 *locus = *va_arg (*text->args_ptr, location_t *);
790 text->format_spec = p + 1;
798 diagnostic_set_info (diagnostic, msgid, args, file, line, kind)
799 diagnostic_info *diagnostic;
806 diagnostic->message.format_spec = msgid;
807 diagnostic->message.args_ptr = args;
808 /* If the diagnostic message doesn't specify a loccation,
809 use FILE and LINE. */
810 if (!text_specifies_location (&diagnostic->message, &diagnostic->location))
812 diagnostic->location.file = file;
813 diagnostic->location.line = line;
815 diagnostic->kind = kind;
818 /* Return a malloc'd string describing a location. The caller is
819 responsible for freeing the memory. */
821 diagnostic_build_prefix (diagnostic)
822 diagnostic_info *diagnostic;
824 static const char *const diagnostic_kind_text[] = {
825 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
826 #include "diagnostic.def"
827 #undef DEFINE_DIAGNOSTIC_KIND
830 if (diagnostic->kind >= DK_LAST_DIAGNOSTIC_KIND)
833 return diagnostic->location.file
834 ? build_message_string ("%s:%d: %s",
835 diagnostic->location.file,
836 diagnostic->location.line,
837 _(diagnostic_kind_text[diagnostic->kind]))
838 : build_message_string ("%s: %s", progname,
839 _(diagnostic_kind_text[diagnostic->kind]));
842 /* Report a diagnostic MESSAGE at the declaration DECL.
843 MSG is a format string which uses %s to substitute the declaration
844 name; subsequent substitutions are a la output_format. */
846 diagnostic_for_decl (diagnostic, decl)
847 diagnostic_info *diagnostic;
850 if (global_dc->lock++)
851 error_recursion (global_dc);
853 if (diagnostic_count_diagnostic (global_dc, diagnostic->kind))
855 diagnostic_report_current_function (global_dc);
857 (&global_dc->buffer, diagnostic_build_prefix (diagnostic));
858 format_with_decl (&global_dc->buffer, &diagnostic->message, decl);
859 output_flush (&global_dc->buffer);
860 output_destroy_prefix (&global_dc->buffer);
866 diagnostic_flush_buffer (context)
867 diagnostic_context *context;
869 output_buffer_to_stream (&context->buffer);
870 fflush (output_buffer_attached_stream (&context->buffer));
873 /* Count a diagnostic. Return true if the message should be printed. */
875 diagnostic_count_diagnostic (context, kind)
876 diagnostic_context *context;
885 case DK_FATAL: case DK_ICE: case DK_SORRY:
886 case DK_ANACHRONISM: case DK_NOTE:
887 ++diagnostic_kind_count (context, kind);
891 if (!diagnostic_report_warnings_p ())
893 else if (!warnings_are_errors)
895 ++diagnostic_kind_count (context, DK_WARNING);
898 /* else fall through. */
901 if (kind == DK_WARNING && context->warnings_are_errors_message)
903 output_verbatim (&context->buffer,
904 "%s: warnings being treated as errors\n", progname);
905 context->warnings_are_errors_message = false;
907 ++diagnostic_kind_count (context, DK_ERROR);
914 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
915 runs its second argument through gettext. */
917 fnotice VPARAMS ((FILE *file, const char *msgid, ...))
920 VA_FIXEDARG (ap, FILE *, file);
921 VA_FIXEDARG (ap, const char *, msgid);
923 vfprintf (file, _(msgid), ap);
928 /* Print a fatal I/O error message. Argument are like printf.
929 Also include a system error message based on `errno'. */
931 fatal_io_error VPARAMS ((const char *msgid, ...))
935 VA_FIXEDARG (ap, const char *, msgid);
937 text.format_spec = _(msgid);
939 output_printf (&global_dc->buffer, "%s: %s: ", progname, xstrerror (errno));
940 output_format (&global_dc->buffer, &text);
941 output_flush (&global_dc->buffer);
943 exit (FATAL_EXIT_CODE);
946 /* Issue a pedantic warning MSGID. */
948 pedwarn VPARAMS ((const char *msgid, ...))
950 diagnostic_info diagnostic;
952 VA_FIXEDARG (ap, const char *, msgid);
954 diagnostic_set_info (&diagnostic, _(msgid), &ap, input_filename, lineno,
955 pedantic_error_kind ());
956 report_diagnostic (&diagnostic);
960 /* Issue a pedantic warning about DECL. */
962 pedwarn_with_decl VPARAMS ((tree decl, const char *msgid, ...))
964 diagnostic_info diagnostic;
966 VA_FIXEDARG (ap, tree, decl);
967 VA_FIXEDARG (ap, const char *, msgid);
969 diagnostic_set_info (&diagnostic, _(msgid), &ap,
970 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
971 pedantic_error_kind ());
973 /* We don't want -pedantic-errors to cause the compilation to fail from
974 "errors" in system header files. Sometimes fixincludes can't fix what's
975 broken (eg: unsigned char bitfields - fixing it may change the alignment
976 which will cause programs to mysteriously fail because the C library
977 or kernel uses the original layout). There's no point in issuing a
978 warning either, it's just unnecessary noise. */
979 if (!DECL_IN_SYSTEM_HEADER (decl))
980 diagnostic_for_decl (&diagnostic, decl);
984 /* Same as above but within the context FILE and LINE. */
986 pedwarn_with_file_and_line VPARAMS ((const char *file, int line,
987 const char *msgid, ...))
989 diagnostic_info diagnostic;
991 VA_FIXEDARG (ap, const char *, file);
992 VA_FIXEDARG (ap, int, line);
993 VA_FIXEDARG (ap, const char *, msgid);
995 diagnostic_set_info (&diagnostic, _(msgid), &ap, file, line,
996 pedantic_error_kind ());
997 report_diagnostic (&diagnostic);
1001 /* Just apologize with MSGID. */
1003 sorry VPARAMS ((const char *msgid, ...))
1005 diagnostic_info diagnostic;
1007 VA_OPEN (ap, msgid);
1008 VA_FIXEDARG (ap, const char *, msgid);
1011 diagnostic_set_info (&diagnostic, _(msgid), &ap,
1012 input_filename, lineno, DK_SORRY);
1015 (&global_dc->buffer, diagnostic_build_prefix (&diagnostic));
1016 output_printf (&global_dc->buffer, "sorry, not implemented: ");
1017 output_format (&global_dc->buffer, &diagnostic.message);
1018 output_flush (&global_dc->buffer);
1022 /* Called when the start of a function definition is parsed,
1023 this function prints on stderr the name of the function. */
1025 announce_function (decl)
1030 if (rtl_dump_and_exit)
1031 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl)));
1033 verbatim (" %s", (*lang_hooks.decl_printable_name) (decl, 2));
1035 output_needs_newline (&global_dc->buffer) = true;
1036 diagnostic_set_last_function (global_dc);
1040 /* The default function to print out name of current function that caused
1043 lhd_print_error_function (context, file)
1044 diagnostic_context *context;
1047 if (diagnostic_last_function_changed (context))
1049 const char *old_prefix = output_prefix (&context->buffer);
1050 char *new_prefix = file ? build_message_string ("%s: ", file) : NULL;
1052 output_set_prefix (&context->buffer, new_prefix);
1054 if (current_function_decl == NULL)
1055 output_add_string (&context->buffer, _("At top level:"));
1058 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
1060 (&context->buffer, "In member function `%s':",
1061 (*lang_hooks.decl_printable_name) (current_function_decl, 2));
1064 (&context->buffer, "In function `%s':",
1065 (*lang_hooks.decl_printable_name) (current_function_decl, 2));
1067 output_add_newline (&context->buffer);
1069 diagnostic_set_last_function (context);
1070 output_buffer_to_stream (&context->buffer);
1071 context->buffer.state.prefix = old_prefix;
1072 free ((char*) new_prefix);
1076 /* Prints out, if necessary, the name of the current function
1077 that caused an error. Called from all error and warning functions.
1078 We ignore the FILE parameter, as it cannot be relied upon. */
1081 diagnostic_report_current_function (context)
1082 diagnostic_context *context;
1084 diagnostic_report_current_module (context);
1085 (*lang_hooks.print_error_function) (context, input_filename);
1089 error_with_file_and_line VPARAMS ((const char *file, int line,
1090 const char *msgid, ...))
1092 diagnostic_info diagnostic;
1094 VA_OPEN (ap, msgid);
1095 VA_FIXEDARG (ap, const char *, file);
1096 VA_FIXEDARG (ap, int, line);
1097 VA_FIXEDARG (ap, const char *, msgid);
1099 diagnostic_set_info (&diagnostic, msgid, &ap, file, line, DK_ERROR);
1100 report_diagnostic (&diagnostic);
1105 error_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1107 diagnostic_info diagnostic;
1108 VA_OPEN (ap, msgid);
1109 VA_FIXEDARG (ap, tree, decl);
1110 VA_FIXEDARG (ap, const char *, msgid);
1112 diagnostic_set_info (&diagnostic, msgid, &ap,
1113 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1115 diagnostic_for_decl (&diagnostic, decl);
1120 /* Report an error message. The arguments are like that of printf. */
1123 error VPARAMS ((const char *msgid, ...))
1125 diagnostic_info diagnostic;
1127 VA_OPEN (ap, msgid);
1128 VA_FIXEDARG (ap, const char *, msgid);
1130 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1132 report_diagnostic (&diagnostic);
1136 /* Likewise, except that the compilation is terminated after printing the
1140 fatal_error VPARAMS ((const char *msgid, ...))
1142 diagnostic_info diagnostic;
1144 VA_OPEN (ap, msgid);
1145 VA_FIXEDARG (ap, const char *, msgid);
1147 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1149 report_diagnostic (&diagnostic);
1152 fnotice (stderr, "compilation terminated.\n");
1153 exit (FATAL_EXIT_CODE);
1157 internal_error VPARAMS ((const char *msgid, ...))
1159 diagnostic_info diagnostic;
1161 VA_OPEN (ap, msgid);
1162 VA_FIXEDARG (ap, const char *, msgid);
1164 if (global_dc->lock)
1165 error_recursion (global_dc);
1167 #ifndef ENABLE_CHECKING
1168 if (errorcount > 0 || sorrycount > 0)
1170 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
1171 input_filename, lineno);
1172 exit (FATAL_EXIT_CODE);
1176 if (global_dc->internal_error != 0)
1177 (*global_dc->internal_error) (_(msgid), &ap);
1179 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1181 report_diagnostic (&diagnostic);
1185 "Please submit a full bug report,\n\
1186 with preprocessed source if appropriate.\n\
1187 See %s for instructions.\n", GCCBUGURL);
1188 exit (FATAL_EXIT_CODE);
1192 warning_with_file_and_line VPARAMS ((const char *file, int line,
1193 const char *msgid, ...))
1195 diagnostic_info diagnostic;
1197 VA_OPEN (ap, msgid);
1198 VA_FIXEDARG (ap, const char *, file);
1199 VA_FIXEDARG (ap, int, line);
1200 VA_FIXEDARG (ap, const char *, msgid);
1202 diagnostic_set_info (&diagnostic, msgid, &ap, file, line, DK_WARNING);
1203 report_diagnostic (&diagnostic);
1208 warning_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1210 diagnostic_info diagnostic;
1211 VA_OPEN (ap, msgid);
1212 VA_FIXEDARG (ap, tree, decl);
1213 VA_FIXEDARG (ap, const char *, msgid);
1215 diagnostic_set_info (&diagnostic, msgid, &ap,
1216 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1218 diagnostic_for_decl (&diagnostic, decl);
1223 warning VPARAMS ((const char *msgid, ...))
1225 diagnostic_info diagnostic;
1227 VA_OPEN (ap, msgid);
1228 VA_FIXEDARG (ap, const char *, msgid);
1230 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1232 report_diagnostic (&diagnostic);
1237 /* Same as above but use diagnostic_buffer. */
1240 verbatim VPARAMS ((const char *msgid, ...))
1243 VA_OPEN (ap, msgid);
1244 VA_FIXEDARG (ap, const char *, msgid);
1246 text.format_spec = _(msgid);
1247 text.args_ptr = ≈
1248 output_do_verbatim (&global_dc->buffer, &text);
1249 output_buffer_to_stream (&global_dc->buffer);
1253 /* Report a diagnostic message (an error or a warning) as specified by
1254 DC. This function is *the* subroutine in terms of which front-ends
1255 should implement their specific diagnostic handling modules. The
1256 front-end independent format specifiers are exactly those described
1257 in the documentation of output_format. */
1260 diagnostic_report_diagnostic (context, diagnostic)
1261 diagnostic_context *context;
1262 diagnostic_info *diagnostic;
1264 if (context->lock++)
1265 error_recursion (context);
1267 if (diagnostic_count_diagnostic (context, diagnostic->kind))
1269 (*diagnostic_starter (context)) (context, diagnostic);
1270 output_format (&context->buffer, &diagnostic->message);
1271 (*diagnostic_finalizer (context)) (context, diagnostic);
1272 output_flush (&context->buffer);
1278 /* Inform the user that an error occurred while trying to report some
1279 other error. This indicates catastrophic internal inconsistencies,
1280 so give up now. But do try to flush out the previous error.
1281 This mustn't use internal_error, that will cause infinite recursion. */
1284 error_recursion (context)
1285 diagnostic_context *context;
1287 if (context->lock < 3)
1288 output_flush (&context->buffer);
1291 "Internal compiler error: Error reporting routines re-entered.\n");
1293 "Please submit a full bug report,\n\
1294 with preprocessed source if appropriate.\n\
1295 See %s for instructions.\n", GCCBUGURL);
1296 exit (FATAL_EXIT_CODE);
1299 /* Given a partial pathname as input, return another pathname that
1300 shares no directory elements with the pathname of __FILE__. This
1301 is used by fancy_abort() to print `Internal compiler error in expr.c'
1302 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1305 trim_filename (name)
1308 static const char this_file[] = __FILE__;
1309 const char *p = name, *q = this_file;
1311 /* First skip any "../" in each filename. This allows us to give a proper
1312 reference to a file in a subdirectory. */
1313 while (p[0] == '.' && p[1] == '.'
1314 && (p[2] == DIR_SEPARATOR
1315 #ifdef DIR_SEPARATOR_2
1316 || p[2] == DIR_SEPARATOR_2
1321 while (q[0] == '.' && q[1] == '.'
1322 && (q[2] == DIR_SEPARATOR
1323 #ifdef DIR_SEPARATOR_2
1324 || p[2] == DIR_SEPARATOR_2
1329 /* Now skip any parts the two filenames have in common. */
1330 while (*p == *q && *p != 0 && *q != 0)
1333 /* Now go backwards until the previous directory separator. */
1334 while (p > name && p[-1] != DIR_SEPARATOR
1335 #ifdef DIR_SEPARATOR_2
1336 && p[-1] != DIR_SEPARATOR_2
1344 /* Report an internal compiler error in a friendly manner and without
1348 fancy_abort (file, line, function)
1351 const char *function;
1353 internal_error ("Internal compiler error in %s, at %s:%d",
1354 function, trim_filename (file), line);
1358 diagnostic_report_current_module (context)
1359 diagnostic_context *context;
1361 struct file_stack *p;
1363 if (output_needs_newline (&context->buffer))
1365 output_add_newline (&context->buffer);
1366 output_needs_newline (&context->buffer) = false;
1369 if (input_file_stack && input_file_stack->next != 0
1370 && diagnostic_last_module_changed (context))
1372 for (p = input_file_stack->next; p; p = p->next)
1373 if (p == input_file_stack->next)
1374 output_verbatim (&context->buffer,
1375 "In file included from %s:%d", p->name, p->line);
1377 output_verbatim (&context->buffer,
1378 ",\n from %s:%d", p->name, p->line);
1379 output_verbatim (&context->buffer, ":\n");
1380 diagnostic_set_last_module (context);
1385 default_diagnostic_starter (context, diagnostic)
1386 diagnostic_context *context;
1387 diagnostic_info *diagnostic;
1389 diagnostic_report_current_function (context);
1390 output_set_prefix (&context->buffer, diagnostic_build_prefix (diagnostic));
1394 default_diagnostic_finalizer (context, diagnostic)
1395 diagnostic_context *context;
1396 diagnostic_info *diagnostic __attribute__((unused));
1398 output_destroy_prefix (&context->buffer);
1402 inform VPARAMS ((const char *msgid, ...))
1404 diagnostic_info diagnostic;
1406 VA_OPEN (ap, msgid);
1407 VA_FIXEDARG (ap, const char *, msgid);
1409 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1411 report_diagnostic (&diagnostic);
1416 warn_deprecated_use (node)
1419 if (node == 0 || !warn_deprecated_decl)
1423 warning ("`%s' is deprecated (declared at %s:%d)",
1424 IDENTIFIER_POINTER (DECL_NAME (node)),
1425 DECL_SOURCE_FILE (node), DECL_SOURCE_LINE (node));
1426 else if (TYPE_P (node))
1428 const char *what = NULL;
1429 tree decl = TYPE_STUB_DECL (node);
1431 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
1432 what = IDENTIFIER_POINTER (TYPE_NAME (node));
1433 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
1434 && DECL_NAME (TYPE_NAME (node)))
1435 what = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node)));
1440 warning ("`%s' is deprecated (declared at %s:%d)", what,
1441 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
1443 warning ("`%s' is deprecated", what);
1446 warning ("type is deprecated (declared at %s:%d)",
1447 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
1449 warning ("type is deprecated");