2002-04-02 David S. Miller <davem@redhat.com>
[official-gcc.git] / gcc / diagnostic.c
blob247ee1d4e59598f3d1002a3d03e2c99729431c32
1 /* Language-independent diagnostic subroutines for the GNU C compiler
2 Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
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
15 for more details.
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
20 02111-1307, USA. */
23 /* This file implements the language independent aspect of diagnostic
24 message module. */
26 #include "config.h"
27 #undef FLOAT /* This is for hpux. They should change hpux. */
28 #undef FFS /* Some systems define this in param.h. */
29 #include "system.h"
30 #include "tree.h"
31 #include "tm_p.h"
32 #include "flags.h"
33 #include "input.h"
34 #include "toplev.h"
35 #include "intl.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) \
44 do { \
45 sprintf ((BUFFER)->digit_buffer, FORMAT, INTEGER); \
46 output_add_string (BUFFER, (BUFFER)->digit_buffer); \
47 } while (0)
49 #define output_text_length(BUFFER) (BUFFER)->line_length
50 #define is_starting_newline(BUFFER) (output_text_length (BUFFER) == 0)
51 #define output_prefix(BUFFER) (BUFFER)->state.prefix
52 #define line_wrap_cutoff(BUFFER) (BUFFER)->state.maximum_length
53 #define prefix_was_emitted_for(BUFFER) (BUFFER)->state.emitted_prefix_p
54 #define output_buffer_ptr_to_format_args(BUFFER) (BUFFER)->state.format_args
56 #define diagnostic_args output_buffer_ptr_to_format_args (diagnostic_buffer)
57 #define diagnostic_msg output_buffer_text_cursor (diagnostic_buffer)
59 /* Prototypes. */
60 static void diagnostic_finish PARAMS ((output_buffer *));
61 static void output_do_verbatim PARAMS ((output_buffer *,
62 const char *, va_list *));
63 static void output_buffer_to_stream PARAMS ((output_buffer *));
64 static void output_format PARAMS ((output_buffer *));
65 static void output_indent PARAMS ((output_buffer *));
67 static char *vbuild_message_string PARAMS ((const char *, va_list))
68 ATTRIBUTE_PRINTF (1, 0);
69 static char *build_message_string PARAMS ((const char *, ...))
70 ATTRIBUTE_PRINTF_1;
71 static void output_do_printf PARAMS ((output_buffer *, const char *))
72 ATTRIBUTE_PRINTF (2, 0);
73 static void format_with_decl PARAMS ((output_buffer *, tree));
74 static void diagnostic_for_decl PARAMS ((tree, const char *, va_list *, int));
75 static void set_real_maximum_length PARAMS ((output_buffer *));
77 static void output_unsigned_decimal PARAMS ((output_buffer *, unsigned int));
78 static void output_long_decimal PARAMS ((output_buffer *, long int));
79 static void output_long_unsigned_decimal PARAMS ((output_buffer *,
80 long unsigned int));
81 static void output_octal PARAMS ((output_buffer *, unsigned int));
82 static void output_long_octal PARAMS ((output_buffer *, unsigned long int));
83 static void output_hexadecimal PARAMS ((output_buffer *, unsigned int));
84 static void output_long_hexadecimal PARAMS ((output_buffer *,
85 unsigned long int));
86 static void output_append_r PARAMS ((output_buffer *, const char *, int));
87 static void wrap_text PARAMS ((output_buffer *, const char *, const char *));
88 static void maybe_wrap_text PARAMS ((output_buffer *, const char *,
89 const char *));
90 static void clear_diagnostic_info PARAMS ((output_buffer *));
92 static void default_diagnostic_starter PARAMS ((output_buffer *,
93 diagnostic_context *));
94 static void default_diagnostic_finalizer PARAMS ((output_buffer *,
95 diagnostic_context *));
97 static void error_recursion PARAMS ((void)) ATTRIBUTE_NORETURN;
99 extern int rtl_dump_and_exit;
100 extern int warnings_are_errors;
102 /* A diagnostic_context surrogate for stderr. */
103 static diagnostic_context global_diagnostic_context;
104 diagnostic_context *global_dc = &global_diagnostic_context;
106 /* This will be removed shortly. */
107 output_buffer *diagnostic_buffer = &global_diagnostic_context.buffer;
109 /* Function of last error message;
110 more generally, function such that if next error message is in it
111 then we don't have to mention the function name. */
112 static tree last_error_function = NULL;
114 /* Used to detect when input_file_stack has changed since last described. */
115 static int last_error_tick;
117 /* Prevent recursion into the error handler. */
118 static int diagnostic_lock;
121 /* Return truthvalue if current input file is different from the most recent
122 file involved in a diagnostic message. */
125 error_module_changed ()
127 return last_error_tick != input_file_stack_tick;
130 /* Remember current file as being the most recent file involved in a
131 diagnostic message. */
133 void
134 record_last_error_module ()
136 last_error_tick = input_file_stack_tick;
139 /* Same as error_module_changed, but for function. */
142 error_function_changed ()
144 return last_error_function != current_function_decl;
147 /* Same as record_last_error_module, but for function. */
149 void
150 record_last_error_function ()
152 last_error_function = current_function_decl;
155 /* Initialize the diagnostic message outputting machinery. */
157 void
158 diagnostic_initialize (context)
159 diagnostic_context *context;
161 memset (context, 0, sizeof *context);
162 obstack_init (&context->buffer.obstack);
164 /* By default, diagnostics are sent to stderr. */
165 output_buffer_attached_stream (&context->buffer) = stderr;
167 /* By default, we emit prefixes once per message. */
168 diagnostic_prefixing_rule (context) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
170 diagnostic_starter (context) = default_diagnostic_starter;
171 diagnostic_finalizer (context) = default_diagnostic_finalizer;
174 /* Returns true if BUFFER is in line-wrapping mode. */
177 output_is_line_wrapping (buffer)
178 output_buffer *buffer;
180 return diagnostic_line_cutoff (buffer) > 0;
183 /* Return BUFFER's prefix. */
185 const char *
186 output_get_prefix (buffer)
187 const output_buffer *buffer;
189 return output_prefix (buffer);
192 /* Subroutine of output_set_maximum_length. Set up BUFFER's
193 internal maximum characters per line. */
195 static void
196 set_real_maximum_length (buffer)
197 output_buffer *buffer;
199 /* If we're told not to wrap lines then do the obvious thing. In case
200 we'll emit prefix only once per diagnostic message, it is appropriate
201 not to increase unnecessarily the line-length cut-off. */
202 if (! output_is_line_wrapping (buffer)
203 || diagnostic_prefixing_rule (buffer) == DIAGNOSTICS_SHOW_PREFIX_ONCE
204 || diagnostic_prefixing_rule (buffer) == DIAGNOSTICS_SHOW_PREFIX_NEVER)
205 line_wrap_cutoff (buffer) = diagnostic_line_cutoff (buffer);
206 else
208 int prefix_length =
209 output_prefix (buffer) ? strlen (output_prefix (buffer)) : 0;
210 /* If the prefix is ridiculously too long, output at least
211 32 characters. */
212 if (diagnostic_line_cutoff (buffer) - prefix_length < 32)
213 line_wrap_cutoff (buffer) = diagnostic_line_cutoff (buffer) + 32;
214 else
215 line_wrap_cutoff (buffer) = diagnostic_line_cutoff (buffer);
219 /* Sets the number of maximum characters per line BUFFER can output
220 in line-wrapping mode. A LENGTH value 0 suppresses line-wrapping. */
222 void
223 output_set_maximum_length (buffer, length)
224 output_buffer *buffer;
225 int length;
227 diagnostic_line_cutoff (buffer) = length;
228 set_real_maximum_length (buffer);
231 /* Sets BUFFER's PREFIX. */
233 void
234 output_set_prefix (buffer, prefix)
235 output_buffer *buffer;
236 const char *prefix;
238 output_prefix (buffer) = prefix;
239 set_real_maximum_length (buffer);
240 prefix_was_emitted_for (buffer) = 0;
241 output_indentation (buffer) = 0;
244 /* Return a pointer to the last character emitted in the output
245 BUFFER area. A NULL pointer means no character available. */
246 const char *
247 output_last_position (buffer)
248 const output_buffer *buffer;
250 const char *p = NULL;
252 if (obstack_base (&buffer->obstack) != obstack_next_free (&buffer->obstack))
253 p = ((const char *) obstack_next_free (&buffer->obstack)) - 1;
254 return p;
257 /* Free BUFFER's prefix, a previously malloc'd string. */
259 void
260 output_destroy_prefix (buffer)
261 output_buffer *buffer;
263 if (output_prefix (buffer) != NULL)
265 free ((char *) output_prefix (buffer));
266 output_prefix (buffer) = NULL;
270 /* Zero out any text output so far in BUFFER. */
272 void
273 output_clear_message_text (buffer)
274 output_buffer *buffer;
276 obstack_free (&buffer->obstack, obstack_base (&buffer->obstack));
277 output_text_length (buffer) = 0;
280 /* Zero out any diagnostic data used so far by BUFFER. */
282 static void
283 clear_diagnostic_info (buffer)
284 output_buffer *buffer;
286 output_buffer_text_cursor (buffer) = NULL;
287 output_buffer_ptr_to_format_args (buffer) = NULL;
288 prefix_was_emitted_for (buffer) = 0;
289 output_indentation (buffer) = 0;
292 /* Construct an output BUFFER with PREFIX and of MAXIMUM_LENGTH
293 characters per line. */
295 void
296 init_output_buffer (buffer, prefix, maximum_length)
297 output_buffer *buffer;
298 const char *prefix;
299 int maximum_length;
301 memset (buffer, 0, sizeof (output_buffer));
302 obstack_init (&buffer->obstack);
303 output_buffer_attached_stream (buffer) = stderr;
304 diagnostic_line_cutoff (buffer) = maximum_length;
305 diagnostic_prefixing_rule (buffer) = diagnostic_prefixing_rule (global_dc);
306 output_set_prefix (buffer, prefix);
307 output_text_length (buffer) = 0;
308 clear_diagnostic_info (buffer);
311 /* Reinitialize BUFFER. */
313 void
314 output_clear (buffer)
315 output_buffer *buffer;
317 output_clear_message_text (buffer);
318 clear_diagnostic_info (buffer);
321 /* Finishes constructing a NULL-terminated character string representing
322 the BUFFERed message. */
324 const char *
325 output_finalize_message (buffer)
326 output_buffer *buffer;
328 obstack_1grow (&buffer->obstack, '\0');
329 return output_message_text (buffer);
332 void
333 flush_diagnostic_buffer ()
335 output_buffer_to_stream (diagnostic_buffer);
336 fflush (output_buffer_attached_stream (diagnostic_buffer));
339 /* Return the amount of characters BUFFER can accept to
340 make a full line. */
343 output_space_left (buffer)
344 const output_buffer *buffer;
346 return line_wrap_cutoff (buffer) - output_text_length (buffer);
349 /* Write out BUFFER's prefix. */
351 void
352 output_emit_prefix (buffer)
353 output_buffer *buffer;
355 if (output_prefix (buffer) != NULL)
357 switch (diagnostic_prefixing_rule (buffer))
359 default:
360 case DIAGNOSTICS_SHOW_PREFIX_NEVER:
361 break;
363 case DIAGNOSTICS_SHOW_PREFIX_ONCE:
364 if (prefix_was_emitted_for (buffer))
366 output_indent (buffer);
367 break;
369 output_indentation (buffer) += 3;
370 /* Fall through. */
372 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
374 int prefix_length = strlen (output_prefix (buffer));
375 output_append_r (buffer, output_prefix (buffer), prefix_length);
376 prefix_was_emitted_for (buffer) = 1;
378 break;
383 /* Have BUFFER start a new line. */
385 void
386 output_add_newline (buffer)
387 output_buffer *buffer;
389 obstack_1grow (&buffer->obstack, '\n');
390 output_text_length (buffer) = 0;
393 /* Appends a character to BUFFER. */
395 void
396 output_add_character (buffer, c)
397 output_buffer *buffer;
398 int c;
400 if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
401 output_add_newline (buffer);
402 obstack_1grow (&buffer->obstack, c);
403 ++output_text_length (buffer);
406 /* Adds a space to BUFFER. */
408 void
409 output_add_space (buffer)
410 output_buffer *buffer;
412 if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
414 output_add_newline (buffer);
415 return;
417 obstack_1grow (&buffer->obstack, ' ');
418 ++output_text_length (buffer);
421 /* These functions format an INTEGER into BUFFER as suggested by their
422 names. */
424 void
425 output_decimal (buffer, i)
426 output_buffer *buffer;
427 int i;
429 output_formatted_integer (buffer, "%d", i);
432 static void
433 output_long_decimal (buffer, i)
434 output_buffer *buffer;
435 long int i;
437 output_formatted_integer (buffer, "%ld", i);
440 static void
441 output_unsigned_decimal (buffer, i)
442 output_buffer *buffer;
443 unsigned int i;
445 output_formatted_integer (buffer, "%u", i);
448 static void
449 output_long_unsigned_decimal (buffer, i)
450 output_buffer *buffer;
451 long unsigned int i;
453 output_formatted_integer (buffer, "%lu", i);
456 static void
457 output_octal (buffer, i)
458 output_buffer *buffer;
459 unsigned int i;
461 output_formatted_integer (buffer, "%o", i);
464 static void
465 output_long_octal (buffer, i)
466 output_buffer *buffer;
467 unsigned long int i;
469 output_formatted_integer (buffer, "%lo", i);
472 static void
473 output_hexadecimal (buffer, i)
474 output_buffer *buffer;
475 unsigned int i;
477 output_formatted_integer (buffer, "%x", i);
480 static void
481 output_long_hexadecimal (buffer, i)
482 output_buffer *buffer;
483 unsigned long int i;
485 output_formatted_integer (buffer, "%lx", i);
488 /* Append to BUFFER a string specified by its STARTING character
489 and LENGTH. */
491 static void
492 output_append_r (buffer, start, length)
493 output_buffer *buffer;
494 const char *start;
495 int length;
497 obstack_grow (&buffer->obstack, start, length);
498 output_text_length (buffer) += length;
501 /* Append a string deliminated by START and END to BUFFER. No wrapping is
502 done. However, if beginning a new line then emit output_prefix (BUFFER)
503 and skip any leading whitespace if appropriate. The caller must ensure
504 that it is safe to do so. */
506 void
507 output_append (buffer, start, end)
508 output_buffer *buffer;
509 const char *start;
510 const char *end;
512 /* Emit prefix and skip whitespace if we're starting a new line. */
513 if (is_starting_newline (buffer))
515 output_emit_prefix (buffer);
516 if (output_is_line_wrapping (buffer))
517 while (start != end && *start == ' ')
518 ++start;
520 output_append_r (buffer, start, end - start);
523 static void
524 output_indent (buffer)
525 output_buffer *buffer;
527 int n = output_indentation (buffer);
528 int i;
530 for (i = 0; i < n; ++i)
531 output_add_character (buffer, ' ');
534 /* Wrap a text delimited by START and END into BUFFER. */
536 static void
537 wrap_text (buffer, start, end)
538 output_buffer *buffer;
539 const char *start;
540 const char *end;
542 int is_wrapping = output_is_line_wrapping (buffer);
544 while (start != end)
546 /* Dump anything bordered by whitespaces. */
548 const char *p = start;
549 while (p != end && *p != ' ' && *p != '\n')
550 ++p;
551 if (is_wrapping && p - start >= output_space_left (buffer))
552 output_add_newline (buffer);
553 output_append (buffer, start, p);
554 start = p;
557 if (start != end && *start == ' ')
559 output_add_space (buffer);
560 ++start;
562 if (start != end && *start == '\n')
564 output_add_newline (buffer);
565 ++start;
570 /* Same as wrap_text but wrap text only when in line-wrapping mode. */
572 static void
573 maybe_wrap_text (buffer, start, end)
574 output_buffer *buffer;
575 const char *start;
576 const char *end;
578 if (output_is_line_wrapping (buffer))
579 wrap_text (buffer, start, end);
580 else
581 output_append (buffer, start, end);
585 /* Append a STRING to BUFFER; the STRING might be line-wrapped if in
586 appropriate mode. */
588 void
589 output_add_string (buffer, str)
590 output_buffer *buffer;
591 const char *str;
593 maybe_wrap_text (buffer, str, str + (str ? strlen (str) : 0));
596 /* Flush the content of BUFFER onto the attached stream,
597 and reinitialize. */
599 static void
600 output_buffer_to_stream (buffer)
601 output_buffer *buffer;
603 const char *text = output_finalize_message (buffer);
604 fputs (text, output_buffer_attached_stream (buffer));
605 output_clear_message_text (buffer);
608 /* Format a message pointed to by output_buffer_text_cursor (BUFFER) using
609 output_buffer_format_args (BUFFER) as appropriate. The following format
610 specifiers are recognized as being language independent:
611 %d, %i: (signed) integer in base ten.
612 %u: unsigned integer in base ten.
613 %o: unsigned integer in base eight.
614 %x: unsigned integer in base sixteen.
615 %ld, %li, %lo, %lu, %lx: long versions of the above.
616 %c: character.
617 %s: string.
618 %%: `%'.
619 %*.s: a substring the length of which is specified by an integer. */
621 static void
622 output_format (buffer)
623 output_buffer *buffer;
625 for (; *output_buffer_text_cursor (buffer);
626 ++output_buffer_text_cursor (buffer))
628 int long_integer = 0;
630 /* Ignore text. */
632 const char *p = output_buffer_text_cursor (buffer);
633 while (*p && *p != '%')
634 ++p;
635 wrap_text (buffer, output_buffer_text_cursor (buffer), p);
636 output_buffer_text_cursor (buffer) = p;
639 if (!*output_buffer_text_cursor (buffer))
640 break;
642 /* We got a '%'. Let's see what happens. Record whether we're
643 parsing a long integer format specifier. */
644 if (*++output_buffer_text_cursor (buffer) == 'l')
646 long_integer = 1;
647 ++output_buffer_text_cursor (buffer);
650 /* Handle %c, %d, %i, %ld, %li, %lo, %lu, %lx, %o, %s, %u,
651 %x, %.*s; %%. And nothing else. Front-ends should install
652 printers to grok language specific format specifiers. */
653 switch (*output_buffer_text_cursor (buffer))
655 case 'c':
656 output_add_character
657 (buffer, va_arg (output_buffer_format_args (buffer), int));
658 break;
660 case 'd':
661 case 'i':
662 if (long_integer)
663 output_long_decimal
664 (buffer, va_arg (output_buffer_format_args (buffer), long int));
665 else
666 output_decimal
667 (buffer, va_arg (output_buffer_format_args (buffer), int));
668 break;
670 case 'o':
671 if (long_integer)
672 output_long_octal (buffer,
673 va_arg (output_buffer_format_args (buffer),
674 unsigned long int));
675 else
676 output_octal (buffer,
677 va_arg (output_buffer_format_args (buffer),
678 unsigned int));
679 break;
681 case 's':
682 output_add_string (buffer,
683 va_arg (output_buffer_format_args (buffer),
684 const char *));
685 break;
687 case 'u':
688 if (long_integer)
689 output_long_unsigned_decimal
690 (buffer, va_arg (output_buffer_format_args (buffer),
691 long unsigned int));
692 else
693 output_unsigned_decimal
694 (buffer, va_arg (output_buffer_format_args (buffer),
695 unsigned int));
696 break;
698 case 'x':
699 if (long_integer)
700 output_long_hexadecimal
701 (buffer, va_arg (output_buffer_format_args (buffer),
702 unsigned long int));
703 else
704 output_hexadecimal
705 (buffer, va_arg (output_buffer_format_args (buffer),
706 unsigned int));
707 break;
709 case '%':
710 output_add_character (buffer, '%');
711 break;
713 case '.':
715 int n;
716 const char *s;
717 /* We handle no precision specifier but `%.*s'. */
718 if (*++output_buffer_text_cursor (buffer) != '*')
719 abort ();
720 else if (*++output_buffer_text_cursor (buffer) != 's')
721 abort ();
722 n = va_arg (output_buffer_format_args (buffer), int);
723 s = va_arg (output_buffer_format_args (buffer), const char *);
724 output_append (buffer, s, s + n);
726 break;
728 default:
729 if (!buffer->format_decoder || !(*buffer->format_decoder) (buffer))
731 /* Hmmm. The front-end failed to install a format translator
732 but called us with an unrecognized format. Sorry. */
733 abort ();
739 static char *
740 vbuild_message_string (msg, ap)
741 const char *msg;
742 va_list ap;
744 char *str;
746 vasprintf (&str, msg, ap);
747 return str;
750 /* Return a malloc'd string containing MSG formatted a la
751 printf. The caller is responsible for freeing the memory. */
753 static char *
754 build_message_string VPARAMS ((const char *msg, ...))
756 char *str;
758 VA_OPEN (ap, msg);
759 VA_FIXEDARG (ap, const char *, msg);
761 str = vbuild_message_string (msg, ap);
763 VA_CLOSE (ap);
765 return str;
768 /* Return a malloc'd string describing a location. The caller is
769 responsible for freeing the memory. */
771 char *
772 context_as_prefix (file, line, warn)
773 const char *file;
774 int line;
775 int warn;
777 if (file)
779 if (warn)
780 return build_message_string (_("%s:%d: warning: "), file, line);
781 else
782 return build_message_string ("%s:%d: ", file, line);
784 else
786 if (warn)
787 return build_message_string (_("%s: warning: "), progname);
788 else
789 return build_message_string ("%s: ", progname);
793 /* Same as context_as_prefix, but only the source FILE is given. */
795 char *
796 file_name_as_prefix (f)
797 const char *f;
799 return build_message_string ("%s: ", f);
802 /* Format a MESSAGE into BUFFER. Automatically wrap lines. */
804 static void
805 output_do_printf (buffer, msg)
806 output_buffer *buffer;
807 const char *msg;
809 char *message = vbuild_message_string (msg,
810 output_buffer_format_args (buffer));
812 wrap_text (buffer, message, message + strlen (message));
813 free (message);
817 /* Format a message into BUFFER a la printf. */
819 void
820 output_printf VPARAMS ((struct output_buffer *buffer, const char *msgid, ...))
822 va_list *old_args;
824 VA_OPEN (ap, msgid);
825 VA_FIXEDARG (ap, output_buffer *, buffer);
826 VA_FIXEDARG (ap, const char *, msgid);
828 old_args = output_buffer_ptr_to_format_args (buffer);
829 output_buffer_ptr_to_format_args (buffer) = &ap;
830 output_do_printf (buffer, _(msgid));
831 output_buffer_ptr_to_format_args (buffer) = old_args;
832 VA_CLOSE (ap);
835 /* Print a message relevant to the given DECL. */
837 static void
838 format_with_decl (buffer, decl)
839 output_buffer *buffer;
840 tree decl;
842 const char *p;
844 /* Do magic to get around lack of varargs support for insertion
845 of arguments into existing list. We know that the decl is first;
846 we ass_u_me that it will be printed with "%s". */
847 for (p = output_buffer_text_cursor (buffer); *p; ++p)
849 if (*p == '%')
851 if (*(p + 1) == '%')
852 ++p;
853 else if (*(p + 1) != 's')
854 abort ();
855 else
856 break;
860 /* Print the left-hand substring. */
861 maybe_wrap_text (buffer, output_buffer_text_cursor (buffer), p);
863 if (*p == '%') /* Print the name. */
865 const char *const n = (DECL_NAME (decl)
866 ? (*lang_hooks.decl_printable_name) (decl, 2)
867 : _("((anonymous))"));
868 output_add_string (buffer, n);
869 while (*p)
871 ++p;
872 if (ISALPHA (*(p - 1) & 0xFF))
873 break;
877 if (*p) /* Print the rest of the message. */
879 output_buffer_text_cursor (buffer) = p;
880 output_format (buffer);
885 /* Report a diagnostic MESSAGE at the declaration DECL.
886 MSG is a format string which uses %s to substitute the declaration
887 name; subsequent substitutions are a la output_format. */
889 static void
890 diagnostic_for_decl (decl, msgid, args_ptr, warn)
891 tree decl;
892 const char *msgid;
893 va_list *args_ptr;
894 int warn;
896 output_state os;
898 if (diagnostic_lock++)
899 error_recursion ();
901 if (count_error (warn))
903 os = output_buffer_state (diagnostic_buffer);
904 report_error_function (DECL_SOURCE_FILE (decl));
905 output_set_prefix
906 (diagnostic_buffer, context_as_prefix
907 (DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl), warn));
908 output_buffer_ptr_to_format_args (diagnostic_buffer) = args_ptr;
909 output_buffer_text_cursor (diagnostic_buffer) = _(msgid);
910 format_with_decl (diagnostic_buffer, decl);
911 diagnostic_finish ((output_buffer *) global_dc);
912 output_destroy_prefix (diagnostic_buffer);
914 output_buffer_state (diagnostic_buffer) = os;
916 diagnostic_lock--;
920 /* Count an error or warning. Return 1 if the message should be printed. */
923 count_error (warningp)
924 int warningp;
926 if (warningp && !diagnostic_report_warnings_p ())
927 return 0;
929 if (warningp && !warnings_are_errors)
930 warningcount++;
931 else
933 static int warning_message = 0;
935 if (warningp && !warning_message)
937 verbatim ("%s: warnings being treated as errors\n", progname);
938 warning_message = 1;
940 errorcount++;
943 return 1;
946 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
947 runs its second argument through gettext. */
949 void
950 fnotice VPARAMS ((FILE *file, const char *msgid, ...))
952 VA_OPEN (ap, msgid);
953 VA_FIXEDARG (ap, FILE *, file);
954 VA_FIXEDARG (ap, const char *, msgid);
956 vfprintf (file, _(msgid), ap);
957 VA_CLOSE (ap);
961 /* Print a fatal I/O error message. Argument are like printf.
962 Also include a system error message based on `errno'. */
964 void
965 fatal_io_error VPARAMS ((const char *msgid, ...))
967 output_state os;
969 VA_OPEN (ap, msgid);
970 VA_FIXEDARG (ap, const char *, msgid);
972 os = output_buffer_state (diagnostic_buffer);
974 output_printf (diagnostic_buffer, "%s: %s: ", progname, xstrerror (errno));
975 output_buffer_ptr_to_format_args (diagnostic_buffer) = &ap;
976 output_buffer_text_cursor (diagnostic_buffer) = _(msgid);
977 output_format (diagnostic_buffer);
978 diagnostic_finish ((output_buffer *) global_dc);
979 output_buffer_state (diagnostic_buffer) = os;
980 VA_CLOSE (ap);
981 exit (FATAL_EXIT_CODE);
984 /* Issue a pedantic warning MSGID. */
986 void
987 pedwarn VPARAMS ((const char *msgid, ...))
989 diagnostic_context dc;
991 VA_OPEN (ap, msgid);
992 VA_FIXEDARG (ap, const char *, msgid);
994 set_diagnostic_context
995 (&dc, msgid, &ap, input_filename, lineno, !flag_pedantic_errors);
996 report_diagnostic (&dc);
997 VA_CLOSE (ap);
1000 /* Issue a pedantic waring about DECL. */
1002 void
1003 pedwarn_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1005 VA_OPEN (ap, msgid);
1006 VA_FIXEDARG (ap, tree, decl);
1007 VA_FIXEDARG (ap, const char *, msgid);
1009 /* We don't want -pedantic-errors to cause the compilation to fail from
1010 "errors" in system header files. Sometimes fixincludes can't fix what's
1011 broken (eg: unsigned char bitfields - fixing it may change the alignment
1012 which will cause programs to mysteriously fail because the C library
1013 or kernel uses the original layout). There's no point in issuing a
1014 warning either, it's just unnecessary noise. */
1015 if (!DECL_IN_SYSTEM_HEADER (decl))
1016 diagnostic_for_decl (decl, msgid, &ap, !flag_pedantic_errors);
1017 VA_CLOSE (ap);
1020 /* Same as above but within the context FILE and LINE. */
1022 void
1023 pedwarn_with_file_and_line VPARAMS ((const char *file, int line,
1024 const char *msgid, ...))
1026 diagnostic_context dc;
1028 VA_OPEN (ap, msgid);
1029 VA_FIXEDARG (ap, const char *, file);
1030 VA_FIXEDARG (ap, int, line);
1031 VA_FIXEDARG (ap, const char *, msgid);
1033 set_diagnostic_context (&dc, msgid, &ap, file, line, !flag_pedantic_errors);
1034 report_diagnostic (&dc);
1035 VA_CLOSE (ap);
1038 /* Just apologize with MSGID. */
1040 void
1041 sorry VPARAMS ((const char *msgid, ...))
1043 output_state os;
1045 VA_OPEN (ap, msgid);
1046 VA_FIXEDARG (ap, const char *, msgid);
1048 ++sorrycount;
1049 os = output_buffer_state (diagnostic_buffer);
1051 output_set_prefix
1052 (diagnostic_buffer, context_as_prefix (input_filename, lineno, 0));
1053 output_printf (diagnostic_buffer, "sorry, not implemented: ");
1054 output_buffer_ptr_to_format_args (diagnostic_buffer) = &ap;
1055 output_buffer_text_cursor (diagnostic_buffer) = _(msgid);
1056 output_format (diagnostic_buffer);
1057 diagnostic_finish ((output_buffer *) global_dc);
1058 output_buffer_state (diagnostic_buffer) = os;
1059 VA_CLOSE (ap);
1062 /* Called when the start of a function definition is parsed,
1063 this function prints on stderr the name of the function. */
1065 void
1066 announce_function (decl)
1067 tree decl;
1069 if (! quiet_flag)
1071 if (rtl_dump_and_exit)
1072 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl)));
1073 else
1074 verbatim (" %s", (*lang_hooks.decl_printable_name) (decl, 2));
1075 fflush (stderr);
1076 output_needs_newline (diagnostic_buffer) = 1;
1077 record_last_error_function ();
1081 /* The default function to print out name of current function that caused
1082 an error. */
1084 void
1085 lhd_print_error_function (context, file)
1086 diagnostic_context *context;
1087 const char *file;
1089 if (error_function_changed ())
1091 char *prefix = file ? build_message_string ("%s: ", file) : NULL;
1092 output_state os;
1094 os = output_buffer_state (context);
1095 output_set_prefix ((output_buffer *) context, prefix);
1097 if (current_function_decl == NULL)
1098 output_add_string ((output_buffer *) context, _("At top level:"));
1099 else
1101 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
1102 output_printf
1103 ((output_buffer *) context, "In member function `%s':",
1104 (*lang_hooks.decl_printable_name) (current_function_decl, 2));
1105 else
1106 output_printf
1107 ((output_buffer *) context, "In function `%s':",
1108 (*lang_hooks.decl_printable_name) (current_function_decl, 2));
1110 output_add_newline ((output_buffer *) context);
1112 record_last_error_function ();
1113 output_buffer_to_stream ((output_buffer *) context);
1114 output_buffer_state (context) = os;
1115 free ((char*) prefix);
1119 /* Prints out, if necessary, the name of the current function
1120 that caused an error. Called from all error and warning functions.
1121 We ignore the FILE parameter, as it cannot be relied upon. */
1123 void
1124 report_error_function (file)
1125 const char *file ATTRIBUTE_UNUSED;
1127 report_problematic_module ((output_buffer *) global_dc);
1128 (*lang_hooks.print_error_function) (global_dc, input_filename);
1131 void
1132 error_with_file_and_line VPARAMS ((const char *file, int line,
1133 const char *msgid, ...))
1135 diagnostic_context dc;
1137 VA_OPEN (ap, msgid);
1138 VA_FIXEDARG (ap, const char *, file);
1139 VA_FIXEDARG (ap, int, line);
1140 VA_FIXEDARG (ap, const char *, msgid);
1142 set_diagnostic_context (&dc, msgid, &ap, file, line, /* warn = */ 0);
1143 report_diagnostic (&dc);
1144 VA_CLOSE (ap);
1147 void
1148 error_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1150 VA_OPEN (ap, msgid);
1151 VA_FIXEDARG (ap, tree, decl);
1152 VA_FIXEDARG (ap, const char *, msgid);
1154 diagnostic_for_decl (decl, msgid, &ap, /* warn = */ 0);
1155 VA_CLOSE (ap);
1159 /* Report an error message. The arguments are like that of printf. */
1161 void
1162 error VPARAMS ((const char *msgid, ...))
1164 diagnostic_context dc;
1166 VA_OPEN (ap, msgid);
1167 VA_FIXEDARG (ap, const char *, msgid);
1169 set_diagnostic_context
1170 (&dc, msgid, &ap, input_filename, lineno, /* warn = */ 0);
1171 report_diagnostic (&dc);
1172 VA_CLOSE (ap);
1175 /* Likewise, except that the compilation is terminated after printing the
1176 error message. */
1178 void
1179 fatal_error VPARAMS ((const char *msgid, ...))
1181 diagnostic_context dc;
1183 VA_OPEN (ap, msgid);
1184 VA_FIXEDARG (ap, const char *, msgid);
1186 set_diagnostic_context
1187 (&dc, msgid, &ap, input_filename, lineno, /* warn = */ 0);
1188 report_diagnostic (&dc);
1189 VA_CLOSE (ap);
1191 fnotice (stderr, "compilation terminated.\n");
1192 exit (FATAL_EXIT_CODE);
1195 /* Report a compiler error at the current line number. Allow a front end to
1196 intercept the message. */
1198 static void (*internal_error_function) PARAMS ((const char *, va_list *));
1200 /* Set the function to call when a compiler error occurs. */
1202 void
1203 set_internal_error_function (f)
1204 void (*f) PARAMS ((const char *, va_list *));
1206 internal_error_function = f;
1209 void
1210 internal_error VPARAMS ((const char *msgid, ...))
1212 diagnostic_context dc;
1214 VA_OPEN (ap, msgid);
1215 VA_FIXEDARG (ap, const char *, msgid);
1217 if (diagnostic_lock)
1218 error_recursion ();
1220 #ifndef ENABLE_CHECKING
1221 if (errorcount > 0 || sorrycount > 0)
1223 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
1224 input_filename, lineno);
1225 exit (FATAL_EXIT_CODE);
1227 #endif
1229 if (internal_error_function != 0)
1230 (*internal_error_function) (_(msgid), &ap);
1232 set_diagnostic_context
1233 (&dc, msgid, &ap, input_filename, lineno, /* warn = */0);
1234 report_diagnostic (&dc);
1235 VA_CLOSE (ap);
1237 fnotice (stderr,
1238 "Please submit a full bug report,\n\
1239 with preprocessed source if appropriate.\n\
1240 See %s for instructions.\n", GCCBUGURL);
1241 exit (FATAL_EXIT_CODE);
1244 void
1245 warning_with_file_and_line VPARAMS ((const char *file, int line,
1246 const char *msgid, ...))
1248 diagnostic_context dc;
1250 VA_OPEN (ap, msgid);
1251 VA_FIXEDARG (ap, const char *, file);
1252 VA_FIXEDARG (ap, int, line);
1253 VA_FIXEDARG (ap, const char *, msgid);
1255 set_diagnostic_context (&dc, msgid, &ap, file, line, /* warn = */ 1);
1256 report_diagnostic (&dc);
1257 VA_CLOSE (ap);
1260 void
1261 warning_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1263 VA_OPEN (ap, msgid);
1264 VA_FIXEDARG (ap, tree, decl);
1265 VA_FIXEDARG (ap, const char *, msgid);
1267 diagnostic_for_decl (decl, msgid, &ap, /* warn = */ 1);
1268 VA_CLOSE (ap);
1271 void
1272 warning VPARAMS ((const char *msgid, ...))
1274 diagnostic_context dc;
1276 VA_OPEN (ap, msgid);
1277 VA_FIXEDARG (ap, const char *, msgid);
1279 set_diagnostic_context
1280 (&dc, msgid, &ap, input_filename, lineno, /* warn = */ 1);
1281 report_diagnostic (&dc);
1282 VA_CLOSE (ap);
1285 /* Flush diagnostic_buffer content on stderr. */
1287 static void
1288 diagnostic_finish (buffer)
1289 output_buffer *buffer;
1291 output_buffer_to_stream (buffer);
1292 clear_diagnostic_info (buffer);
1293 fputc ('\n', output_buffer_attached_stream (buffer));
1294 fflush (output_buffer_attached_stream (buffer));
1297 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
1298 settings needed by BUFFER for a verbatim formatting. */
1300 static void
1301 output_do_verbatim (buffer, msgid, args_ptr)
1302 output_buffer *buffer;
1303 const char *msgid;
1304 va_list *args_ptr;
1306 output_state os;
1308 os = output_buffer_state (buffer);
1309 output_prefix (buffer) = NULL;
1310 diagnostic_prefixing_rule (buffer) = DIAGNOSTICS_SHOW_PREFIX_NEVER;
1311 output_buffer_text_cursor (buffer) = _(msgid);
1312 output_buffer_ptr_to_format_args (buffer) = args_ptr;
1313 output_set_maximum_length (buffer, 0);
1314 output_format (buffer);
1315 output_buffer_state (buffer) = os;
1318 /* Output MESSAGE verbatim into BUFFER. */
1320 void
1321 output_verbatim VPARAMS ((output_buffer *buffer, const char *msgid, ...))
1323 VA_OPEN (ap, msgid);
1324 VA_FIXEDARG (ap, output_buffer *, buffer);
1325 VA_FIXEDARG (ap, const char *, msgid);
1327 output_do_verbatim (buffer, msgid, &ap);
1328 VA_CLOSE (ap);
1331 /* Same as above but use diagnostic_buffer. */
1333 void
1334 verbatim VPARAMS ((const char *msgid, ...))
1336 VA_OPEN (ap, msgid);
1337 VA_FIXEDARG (ap, const char *, msgid);
1339 output_do_verbatim (diagnostic_buffer, msgid, &ap);
1340 output_buffer_to_stream (diagnostic_buffer);
1341 VA_CLOSE (ap);
1344 /* Report a diagnostic message (an error or a warning) as specified by
1345 DC. This function is *the* subroutine in terms of which front-ends
1346 should implement their specific diagnostic handling modules. The
1347 front-end independent format specifiers are exactly those described
1348 in the documentation of output_format. */
1350 void
1351 report_diagnostic (dc)
1352 diagnostic_context *dc;
1354 output_state os;
1356 if (diagnostic_lock++)
1357 error_recursion ();
1359 if (count_error (diagnostic_is_warning (dc)))
1361 os = output_buffer_state (diagnostic_buffer);
1362 diagnostic_msg = diagnostic_message (dc);
1363 diagnostic_args = diagnostic_argument_list (dc);
1364 (*diagnostic_starter (dc)) (diagnostic_buffer, dc);
1365 output_format (diagnostic_buffer);
1366 (*diagnostic_finalizer (dc)) (diagnostic_buffer, dc);
1367 diagnostic_finish ((output_buffer *) global_dc);
1368 output_buffer_state (diagnostic_buffer) = os;
1371 diagnostic_lock--;
1374 /* Inform the user that an error occurred while trying to report some
1375 other error. This indicates catastrophic internal inconsistencies,
1376 so give up now. But do try to flush out the previous error.
1377 This mustn't use internal_error, that will cause infinite recursion. */
1379 static void
1380 error_recursion ()
1382 if (diagnostic_lock < 3)
1383 diagnostic_finish ((output_buffer *) global_dc);
1385 fnotice (stderr,
1386 "Internal compiler error: Error reporting routines re-entered.\n");
1387 fnotice (stderr,
1388 "Please submit a full bug report,\n\
1389 with preprocessed source if appropriate.\n\
1390 See %s for instructions.\n", GCCBUGURL);
1391 exit (FATAL_EXIT_CODE);
1394 /* Given a partial pathname as input, return another pathname that
1395 shares no directory elements with the pathname of __FILE__. This
1396 is used by fancy_abort() to print `Internal compiler error in expr.c'
1397 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1399 const char *
1400 trim_filename (name)
1401 const char *name;
1403 static const char this_file[] = __FILE__;
1404 const char *p = name, *q = this_file;
1406 /* First skip any "../" in each filename. This allows us to give a proper
1407 reference to a file in a subdirectory. */
1408 while (p[0] == '.' && p[1] == '.'
1409 && (p[2] == DIR_SEPARATOR
1410 #ifdef DIR_SEPARATOR_2
1411 || p[2] == DIR_SEPARATOR_2
1412 #endif
1414 p += 3;
1416 while (q[0] == '.' && q[1] == '.'
1417 && (q[2] == DIR_SEPARATOR
1418 #ifdef DIR_SEPARATOR_2
1419 || p[2] == DIR_SEPARATOR_2
1420 #endif
1422 q += 3;
1424 /* Now skip any parts the two filenames have in common. */
1425 while (*p == *q && *p != 0 && *q != 0)
1426 p++, q++;
1428 /* Now go backwards until the previous directory separator. */
1429 while (p > name && p[-1] != DIR_SEPARATOR
1430 #ifdef DIR_SEPARATOR_2
1431 && p[-1] != DIR_SEPARATOR_2
1432 #endif
1434 p--;
1436 return p;
1439 /* Report an internal compiler error in a friendly manner and without
1440 dumping core. */
1442 void
1443 fancy_abort (file, line, function)
1444 const char *file;
1445 int line;
1446 const char *function;
1448 internal_error ("Internal compiler error in %s, at %s:%d",
1449 function, trim_filename (file), line);
1452 /* Setup DC for reporting a diagnostic MESSAGE (an error or a WARNING),
1453 using arguments pointed to by ARGS_PTR, issued at a location specified
1454 by FILE and LINE. */
1456 void
1457 set_diagnostic_context (dc, msgid, args_ptr, file, line, warn)
1458 diagnostic_context *dc;
1459 const char *msgid;
1460 va_list *args_ptr;
1461 const char *file;
1462 int line;
1463 int warn;
1465 memset (dc, 0, sizeof (diagnostic_context));
1466 diagnostic_message (dc) = _(msgid);
1467 diagnostic_argument_list (dc) = args_ptr;
1468 diagnostic_file_location (dc) = file;
1469 diagnostic_line_location (dc) = line;
1470 diagnostic_is_warning (dc) = warn;
1471 diagnostic_starter (dc) = diagnostic_starter (global_dc);
1472 diagnostic_finalizer (dc) = diagnostic_finalizer (global_dc);
1475 void
1476 report_problematic_module (buffer)
1477 output_buffer *buffer;
1479 struct file_stack *p;
1481 if (output_needs_newline (buffer))
1483 output_add_newline (buffer);
1484 output_needs_newline (buffer) = 0;
1487 if (input_file_stack && input_file_stack->next != 0
1488 && error_module_changed ())
1490 for (p = input_file_stack->next; p; p = p->next)
1491 if (p == input_file_stack->next)
1492 output_verbatim
1493 (buffer, "In file included from %s:%d", p->name, p->line);
1494 else
1495 output_verbatim
1496 (buffer, ",\n from %s:%d", p->name, p->line);
1497 output_verbatim (buffer, ":\n");
1498 record_last_error_module ();
1502 static void
1503 default_diagnostic_starter (buffer, dc)
1504 output_buffer *buffer;
1505 diagnostic_context *dc;
1507 report_error_function (diagnostic_file_location (dc));
1508 output_set_prefix (buffer,
1509 context_as_prefix (diagnostic_file_location (dc),
1510 diagnostic_line_location (dc),
1511 diagnostic_is_warning (dc)));
1514 static void
1515 default_diagnostic_finalizer (buffer, dc)
1516 output_buffer *buffer;
1517 diagnostic_context *dc __attribute__((__unused__));
1519 output_destroy_prefix (buffer);
1522 void
1523 warn_deprecated_use (node)
1524 tree node;
1526 if (node == 0 || !warn_deprecated_decl)
1527 return;
1529 if (DECL_P (node))
1530 warning ("`%s' is deprecated (declared at %s:%d)",
1531 IDENTIFIER_POINTER (DECL_NAME (node)),
1532 DECL_SOURCE_FILE (node), DECL_SOURCE_LINE (node));
1533 else if (TYPE_P (node))
1535 const char *what = NULL;
1536 tree decl = TYPE_STUB_DECL (node);
1538 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
1539 what = IDENTIFIER_POINTER (TYPE_NAME (node));
1540 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
1541 && DECL_NAME (TYPE_NAME (node)))
1542 what = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node)));
1544 if (what)
1546 if (decl)
1547 warning ("`%s' is deprecated (declared at %s:%d)", what,
1548 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
1549 else
1550 warning ("`%s' is deprecated", what);
1552 else if (decl)
1553 warning ("type is deprecated (declared at %s:%d)",
1554 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
1555 else
1556 warning ("type is deprecated");