2002-06-06 James Clark <jjc@jclark.com>
[official-gcc.git] / gcc / diagnostic.c
blob6d254337ce740908653261423ed2ae253cbe2918
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
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 "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "tm_p.h"
34 #include "flags.h"
35 #include "input.h"
36 #include "toplev.h"
37 #include "intl.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
47 /* Prototypes. */
48 static void output_flush PARAMS ((output_buffer *));
49 static void output_do_verbatim PARAMS ((output_buffer *, text_info *));
50 static void output_buffer_to_stream PARAMS ((output_buffer *));
51 static void output_format PARAMS ((output_buffer *, text_info *));
52 static void output_indent PARAMS ((output_buffer *));
54 static char *build_message_string PARAMS ((const char *, ...))
55 ATTRIBUTE_PRINTF_1;
56 static void format_with_decl PARAMS ((output_buffer *, text_info *, tree));
57 static void diagnostic_for_decl PARAMS ((diagnostic_context *,
58 diagnostic_info *, tree));
59 static void set_real_maximum_length PARAMS ((output_buffer *));
61 static void output_unsigned_decimal PARAMS ((output_buffer *, unsigned int));
62 static void output_long_decimal PARAMS ((output_buffer *, long int));
63 static void output_long_unsigned_decimal PARAMS ((output_buffer *,
64 long unsigned int));
65 static void output_octal PARAMS ((output_buffer *, unsigned int));
66 static void output_long_octal PARAMS ((output_buffer *, unsigned long int));
67 static void output_hexadecimal PARAMS ((output_buffer *, unsigned int));
68 static void output_long_hexadecimal PARAMS ((output_buffer *,
69 unsigned long int));
70 static void output_append_r PARAMS ((output_buffer *, const char *, int));
71 static void wrap_text PARAMS ((output_buffer *, const char *, const char *));
72 static void maybe_wrap_text PARAMS ((output_buffer *, const char *,
73 const char *));
74 static void output_clear_data PARAMS ((output_buffer *));
76 static void default_diagnostic_starter PARAMS ((diagnostic_context *,
77 diagnostic_info *));
78 static void default_diagnostic_finalizer PARAMS ((diagnostic_context *,
79 diagnostic_info *));
81 static void error_recursion PARAMS ((diagnostic_context *)) ATTRIBUTE_NORETURN;
82 static bool text_specifies_location PARAMS ((text_info *, location_t *));
83 static bool diagnostic_count_diagnostic PARAMS ((diagnostic_context *,
84 diagnostic_info *));
85 static void diagnostic_action_after_output PARAMS ((diagnostic_context *,
86 diagnostic_info *));
87 static void real_abort PARAMS ((void)) ATTRIBUTE_NORETURN;
89 extern int rtl_dump_and_exit;
90 extern int warnings_are_errors;
92 /* A diagnostic_context surrogate for stderr. */
93 static diagnostic_context global_diagnostic_context;
94 diagnostic_context *global_dc = &global_diagnostic_context;
96 /* Boilerplate text used in two locations. */
97 #define bug_report_request \
98 "Please submit a full bug report,\n\
99 with preprocessed source if appropriate.\n\
100 See %s for instructions.\n"
103 /* Subroutine of output_set_maximum_length. Set up BUFFER's
104 internal maximum characters per line. */
105 static void
106 set_real_maximum_length (buffer)
107 output_buffer *buffer;
109 /* If we're told not to wrap lines then do the obvious thing. In case
110 we'll emit prefix only once per diagnostic message, it is appropriate
111 not to increase unnecessarily the line-length cut-off. */
112 if (!output_is_line_wrapping (buffer)
113 || output_prefixing_rule (buffer) == DIAGNOSTICS_SHOW_PREFIX_ONCE
114 || output_prefixing_rule (buffer) == DIAGNOSTICS_SHOW_PREFIX_NEVER)
115 line_wrap_cutoff (buffer) = output_line_cutoff (buffer);
116 else
118 int prefix_length = buffer->state.prefix ?
119 strlen (buffer->state.prefix) : 0;
120 /* If the prefix is ridiculously too long, output at least
121 32 characters. */
122 if (output_line_cutoff (buffer) - prefix_length < 32)
123 line_wrap_cutoff (buffer) = output_line_cutoff (buffer) + 32;
124 else
125 line_wrap_cutoff (buffer) = output_line_cutoff (buffer);
129 /* Sets the number of maximum characters per line BUFFER can output
130 in line-wrapping mode. A LENGTH value 0 suppresses line-wrapping. */
131 void
132 output_set_maximum_length (buffer, length)
133 output_buffer *buffer;
134 int length;
136 output_line_cutoff (buffer) = length;
137 set_real_maximum_length (buffer);
140 /* Sets BUFFER's PREFIX. */
141 void
142 output_set_prefix (buffer, prefix)
143 output_buffer *buffer;
144 const char *prefix;
146 buffer->state.prefix = prefix;
147 set_real_maximum_length (buffer);
148 prefix_was_emitted_for (buffer) = false;
149 output_indentation (buffer) = 0;
152 /* Return a pointer to the last character emitted in the output
153 BUFFER area. A NULL pointer means no character available. */
154 const char *
155 output_last_position (buffer)
156 const output_buffer *buffer;
158 const char *p = NULL;
160 if (obstack_base (&buffer->obstack) != obstack_next_free (&buffer->obstack))
161 p = ((const char *) obstack_next_free (&buffer->obstack)) - 1;
162 return p;
165 /* Free BUFFER's prefix, a previously malloc'd string. */
166 void
167 output_destroy_prefix (buffer)
168 output_buffer *buffer;
170 if (buffer->state.prefix != NULL)
172 free ((char *) buffer->state.prefix);
173 buffer->state.prefix = NULL;
177 /* Zero out any text output so far in BUFFER. */
178 void
179 output_clear_message_text (buffer)
180 output_buffer *buffer;
182 obstack_free (&buffer->obstack, obstack_base (&buffer->obstack));
183 output_text_length (buffer) = 0;
186 /* Zero out any formatting data used so far by BUFFER. */
187 static void
188 output_clear_data (buffer)
189 output_buffer *buffer;
191 prefix_was_emitted_for (buffer) = false;
192 output_indentation (buffer) = 0;
195 /* Construct an output BUFFER with PREFIX and of MAXIMUM_LENGTH
196 characters per line. */
197 void
198 init_output_buffer (buffer, prefix, maximum_length)
199 output_buffer *buffer;
200 const char *prefix;
201 int maximum_length;
203 memset (buffer, 0, sizeof (output_buffer));
204 obstack_init (&buffer->obstack);
205 output_buffer_attached_stream (buffer) = stderr;
206 output_line_cutoff (buffer) = maximum_length;
207 output_prefixing_rule (buffer) = diagnostic_prefixing_rule (global_dc);
208 output_set_prefix (buffer, prefix);
209 output_text_length (buffer) = 0;
210 output_clear_data (buffer);
213 /* Reinitialize BUFFER. */
214 void
215 output_clear (buffer)
216 output_buffer *buffer;
218 output_clear_message_text (buffer);
219 output_clear_data (buffer);
222 /* Finishes constructing a NULL-terminated character string representing
223 the BUFFERed message. */
224 const char *
225 output_finalize_message (buffer)
226 output_buffer *buffer;
228 obstack_1grow (&buffer->obstack, '\0');
229 return output_message_text (buffer);
232 /* Return the amount of characters BUFFER can accept to
233 make a full line. */
235 output_space_left (buffer)
236 const output_buffer *buffer;
238 return line_wrap_cutoff (buffer) - output_text_length (buffer);
241 /* Write out BUFFER's prefix. */
242 void
243 output_emit_prefix (buffer)
244 output_buffer *buffer;
246 if (buffer->state.prefix != NULL)
248 switch (output_prefixing_rule (buffer))
250 default:
251 case DIAGNOSTICS_SHOW_PREFIX_NEVER:
252 break;
254 case DIAGNOSTICS_SHOW_PREFIX_ONCE:
255 if (prefix_was_emitted_for (buffer))
257 output_indent (buffer);
258 break;
260 output_indentation (buffer) += 3;
261 /* Fall through. */
263 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
265 int prefix_length = strlen (buffer->state.prefix);
266 output_append_r (buffer, buffer->state.prefix, prefix_length);
267 prefix_was_emitted_for (buffer) = true;
269 break;
274 /* Have BUFFER start a new line. */
275 void
276 output_add_newline (buffer)
277 output_buffer *buffer;
279 obstack_1grow (&buffer->obstack, '\n');
280 output_text_length (buffer) = 0;
283 /* Appends a character to BUFFER. */
284 void
285 output_add_character (buffer, c)
286 output_buffer *buffer;
287 int c;
289 if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
290 output_add_newline (buffer);
291 obstack_1grow (&buffer->obstack, c);
292 ++output_text_length (buffer);
295 /* Adds a space to BUFFER. */
296 void
297 output_add_space (buffer)
298 output_buffer *buffer;
300 if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
302 output_add_newline (buffer);
303 return;
305 obstack_1grow (&buffer->obstack, ' ');
306 ++output_text_length (buffer);
309 /* These functions format an INTEGER into BUFFER as suggested by their
310 names. */
311 void
312 output_decimal (buffer, i)
313 output_buffer *buffer;
314 int i;
316 output_formatted_scalar (buffer, "%d", i);
319 static inline void
320 output_long_decimal (output_buffer *buffer, long int i)
322 output_formatted_scalar (buffer, "%ld", i);
325 static inline void
326 output_unsigned_decimal (output_buffer *buffer, unsigned int i)
328 output_formatted_scalar (buffer, "%u", i);
331 static inline void
332 output_long_unsigned_decimal (output_buffer *buffer, long unsigned int i)
334 output_formatted_scalar (buffer, "%lu", i);
337 static inline void
338 output_octal (output_buffer *buffer, unsigned int i)
340 output_formatted_scalar (buffer, "%o", i);
343 static inline void
344 output_long_octal (output_buffer *buffer, long unsigned int i)
346 output_formatted_scalar (buffer, "%lo", i);
349 static inline void
350 output_hexadecimal (output_buffer *buffer, unsigned int i)
352 output_formatted_scalar (buffer, "%x", i);
355 static inline void
356 output_long_hexadecimal (output_buffer *buffer, long unsigned int i)
358 output_formatted_scalar (buffer, "%lx", i);
361 static inline void
362 output_pointer (output_buffer *buffer, void *p)
364 output_formatted_scalar (buffer, HOST_PTR_PRINTF, p);
367 /* Append to BUFFER a string specified by its STARTING character
368 and LENGTH. */
369 static void
370 output_append_r (buffer, start, length)
371 output_buffer *buffer;
372 const char *start;
373 int length;
375 obstack_grow (&buffer->obstack, start, length);
376 output_text_length (buffer) += length;
379 /* Append a string deliminated by START and END to BUFFER. No wrapping is
380 done. However, if beginning a new line then emit BUFFER->state.prefix
381 and skip any leading whitespace if appropriate. The caller must ensure
382 that it is safe to do so. */
383 void
384 output_append (buffer, start, end)
385 output_buffer *buffer;
386 const char *start;
387 const char *end;
389 /* Emit prefix and skip whitespace if we're starting a new line. */
390 if (is_starting_newline (buffer))
392 output_emit_prefix (buffer);
393 if (output_is_line_wrapping (buffer))
394 while (start != end && *start == ' ')
395 ++start;
397 output_append_r (buffer, start, end - start);
400 /* Insert enough spaces into BUFFER to bring the column position to
401 the current indentation level, assuming that a newline has just
402 been written to the buffer. */
403 static void
404 output_indent (buffer)
405 output_buffer *buffer;
407 int n = output_indentation (buffer);
408 int i;
410 for (i = 0; i < n; ++i)
411 output_add_character (buffer, ' ');
414 /* Wrap a text delimited by START and END into BUFFER. */
415 static void
416 wrap_text (buffer, start, end)
417 output_buffer *buffer;
418 const char *start;
419 const char *end;
421 bool is_wrapping = output_is_line_wrapping (buffer);
423 while (start != end)
425 /* Dump anything bordered by whitespaces. */
427 const char *p = start;
428 while (p != end && *p != ' ' && *p != '\n')
429 ++p;
430 if (is_wrapping && p - start >= output_space_left (buffer))
431 output_add_newline (buffer);
432 output_append (buffer, start, p);
433 start = p;
436 if (start != end && *start == ' ')
438 output_add_space (buffer);
439 ++start;
441 if (start != end && *start == '\n')
443 output_add_newline (buffer);
444 ++start;
449 /* Same as wrap_text but wrap text only when in line-wrapping mode. */
450 static void
451 maybe_wrap_text (buffer, start, end)
452 output_buffer *buffer;
453 const char *start;
454 const char *end;
456 if (output_is_line_wrapping (buffer))
457 wrap_text (buffer, start, end);
458 else
459 output_append (buffer, start, end);
463 /* Append a STRING to BUFFER; the STRING might be line-wrapped if in
464 appropriate mode. */
465 void
466 output_add_string (buffer, str)
467 output_buffer *buffer;
468 const char *str;
470 maybe_wrap_text (buffer, str, str + (str ? strlen (str) : 0));
473 /* Append an identifier ID to BUFFER. */
474 void
475 output_add_identifier (buffer, id)
476 output_buffer *buffer;
477 tree id;
479 output_append (buffer, IDENTIFIER_POINTER (id),
480 IDENTIFIER_POINTER (id) + IDENTIFIER_LENGTH (id));
483 /* Flush the content of BUFFER onto the attached stream,
484 and reinitialize. */
486 static void
487 output_buffer_to_stream (buffer)
488 output_buffer *buffer;
490 const char *text = output_finalize_message (buffer);
491 fputs (text, output_buffer_attached_stream (buffer));
492 output_clear_message_text (buffer);
495 /* Format a message pointed to by TEXT. The following format specifiers are
496 recognized as being language independent:
497 %d, %i: (signed) integer in base ten.
498 %u: unsigned integer in base ten.
499 %o: unsigned integer in base eight.
500 %x: unsigned integer in base sixteen.
501 %ld, %li, %lo, %lu, %lx: long versions of the above.
502 %c: character.
503 %s: string.
504 %p: pointer.
505 %m: strerror(text->err_no) - does not consume a value from args_ptr.
506 %%: `%'.
507 %*.s: a substring the length of which is specified by an integer.
508 %H: location_t. */
509 static void
510 output_format (buffer, text)
511 output_buffer *buffer;
512 text_info *text;
514 for (; *text->format_spec; ++text->format_spec)
516 bool long_integer = 0;
518 /* Ignore text. */
520 const char *p = text->format_spec;
521 while (*p && *p != '%')
522 ++p;
523 wrap_text (buffer, text->format_spec, p);
524 text->format_spec = p;
527 if (*text->format_spec == '\0')
528 break;
530 /* We got a '%'. Let's see what happens. Record whether we're
531 parsing a long integer format specifier. */
532 if (*++text->format_spec == 'l')
534 long_integer = true;
535 ++text->format_spec;
538 /* Handle %c, %d, %i, %ld, %li, %lo, %lu, %lx, %m, %o, %s, %u,
539 %x, %p, %.*s; %%. And nothing else. Front-ends should install
540 printers to grok language specific format specifiers. */
541 switch (*text->format_spec)
543 case 'c':
544 output_add_character (buffer, va_arg (*text->args_ptr, int));
545 break;
547 case 'd':
548 case 'i':
549 if (long_integer)
550 output_long_decimal (buffer, va_arg (*text->args_ptr, long int));
551 else
552 output_decimal (buffer, va_arg (*text->args_ptr, int));
553 break;
555 case 'o':
556 if (long_integer)
557 output_long_octal (buffer,
558 va_arg (*text->args_ptr, unsigned long int));
559 else
560 output_octal (buffer, va_arg (*text->args_ptr, unsigned int));
561 break;
563 case 's':
564 output_add_string (buffer, va_arg (*text->args_ptr, const char *));
565 break;
567 case 'p':
568 output_pointer (buffer, va_arg (*text->args_ptr, void *));
569 break;
571 case 'u':
572 if (long_integer)
573 output_long_unsigned_decimal
574 (buffer, va_arg (*text->args_ptr, long unsigned int));
575 else
576 output_unsigned_decimal
577 (buffer, va_arg (*text->args_ptr, unsigned int));
578 break;
580 case 'x':
581 if (long_integer)
582 output_long_hexadecimal
583 (buffer, va_arg (*text->args_ptr, unsigned long int));
584 else
585 output_hexadecimal
586 (buffer, va_arg (*text->args_ptr, unsigned int));
587 break;
589 case 'm':
590 output_add_string (buffer, xstrerror (text->err_no));
591 break;
593 case '%':
594 output_add_character (buffer, '%');
595 break;
597 case 'H':
599 const location_t *locus = va_arg (*text->args_ptr, location_t *);
600 output_add_string (buffer, "file '");
601 output_add_string (buffer, locus->file);
602 output_add_string (buffer, "', line ");
603 output_decimal (buffer, locus->line);
605 break;
607 case '.':
609 int n;
610 const char *s;
611 /* We handle no precision specifier but `%.*s'. */
612 if (*++text->format_spec != '*')
613 abort ();
614 else if (*++text->format_spec != 's')
615 abort ();
616 n = va_arg (*text->args_ptr, int);
617 s = va_arg (*text->args_ptr, const char *);
618 output_append (buffer, s, s + n);
620 break;
622 default:
623 if (!buffer->format_decoder
624 || !(*buffer->format_decoder) (buffer, text))
626 /* Hmmm. The front-end failed to install a format translator
627 but called us with an unrecognized format. Or, maybe, the
628 translated string just contains an invalid format, or
629 has formats in the wrong order. Sorry. */
630 abort ();
636 /* Return a malloc'd string containing MSG formatted a la printf. The
637 caller is responsible for freeing the memory. */
638 static char *
639 build_message_string (const char *msg, ...)
641 char *str;
642 va_list ap;
644 va_start (ap, msg);
645 vasprintf (&str, msg, ap);
646 va_end (ap);
648 return str;
651 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
652 char *
653 file_name_as_prefix (f)
654 const char *f;
656 return build_message_string ("%s: ", f);
659 /* Format a message into BUFFER a la printf. */
660 void
661 output_printf (struct output_buffer *buffer, const char *msgid, ...)
663 text_info text;
664 va_list ap;
666 va_start (ap, msgid);
667 text.err_no = errno;
668 text.args_ptr = &ap;
669 text.format_spec = _(msgid);
670 output_format (buffer, &text);
671 va_end (ap);
674 /* Print a message relevant to the given DECL. */
675 static void
676 format_with_decl (buffer, text, decl)
677 output_buffer *buffer;
678 text_info *text;
679 tree decl;
681 const char *p;
683 /* Do magic to get around lack of varargs support for insertion
684 of arguments into existing list. We know that the decl is first;
685 we ass_u_me that it will be printed with "%s". */
686 for (p = text->format_spec; *p; ++p)
688 if (*p == '%')
690 if (*(p + 1) == '%')
691 ++p;
692 else if (*(p + 1) != 's')
693 abort ();
694 else
695 break;
699 /* Print the left-hand substring. */
700 maybe_wrap_text (buffer, text->format_spec, p);
702 if (*p == '%') /* Print the name. */
704 const char *const n = (DECL_NAME (decl)
705 ? (*lang_hooks.decl_printable_name) (decl, 2)
706 : _("((anonymous))"));
707 output_add_string (buffer, n);
708 while (*p)
710 ++p;
711 if (ISALPHA (*(p - 1) & 0xFF))
712 break;
716 if (*p) /* Print the rest of the message. */
718 text->format_spec = p;
719 output_format (buffer, text);
723 /* Flush the content of BUFFER onto the attached stream. */
724 static void
725 output_flush (buffer)
726 output_buffer *buffer;
728 output_buffer_to_stream (buffer);
729 output_clear_data (buffer);
730 fputc ('\n', output_buffer_attached_stream (buffer));
731 fflush (output_buffer_attached_stream (buffer));
734 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
735 settings needed by BUFFER for a verbatim formatting. */
736 static void
737 output_do_verbatim (buffer, text)
738 output_buffer *buffer;
739 text_info *text;
741 diagnostic_prefixing_rule_t rule = output_prefixing_rule (buffer);
742 int line_cutoff = output_line_cutoff (buffer);
744 /* Set verbatim mode. */
745 output_prefixing_rule (buffer) = DIAGNOSTICS_SHOW_PREFIX_NEVER;
746 output_line_cutoff (buffer) = 0;
747 /* Do the actual formatting. */
748 output_format (buffer, text);
749 /* Restore previous settings. */
750 output_prefixing_rule (buffer) = rule;
751 output_line_cutoff (buffer) = line_cutoff;
754 /* Output MESSAGE verbatim into BUFFER. */
755 void
756 output_verbatim (output_buffer *buffer, const char *msgid, ...)
758 text_info text;
759 va_list ap;
761 va_start (ap, msgid);
762 text.err_no = errno;
763 text.args_ptr = &ap;
764 text.format_spec = _(msgid);
765 output_do_verbatim (buffer, &text);
766 va_end (ap);
770 /* Initialize the diagnostic message outputting machinery. */
771 void
772 diagnostic_initialize (context)
773 diagnostic_context *context;
775 memset (context, 0, sizeof *context);
776 obstack_init (&context->buffer.obstack);
778 /* By default, diagnostics are sent to stderr. */
779 output_buffer_attached_stream (&context->buffer) = stderr;
781 /* By default, we emit prefixes once per message. */
782 diagnostic_prefixing_rule (context) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
784 diagnostic_starter (context) = default_diagnostic_starter;
785 diagnostic_finalizer (context) = default_diagnostic_finalizer;
786 context->warnings_are_errors_message = warnings_are_errors;
789 /* Returns true if the next format specifier in TEXT is a format specifier
790 for a location_t. If so, update the object pointed by LOCUS to reflect
791 the specified location in *TEXT->args_ptr. */
792 static bool
793 text_specifies_location (text, locus)
794 text_info *text;
795 location_t *locus;
797 const char *p;
798 /* Skip any leading text. */
799 for (p = text->format_spec; *p && *p != '%'; ++p)
802 /* Extract the location information if any. */
803 if (*p == '%' && *++p == 'H')
805 *locus = *va_arg (*text->args_ptr, location_t *);
806 text->format_spec = p + 1;
807 return true;
810 return false;
813 void
814 diagnostic_set_info (diagnostic, msgid, args, file, line, kind)
815 diagnostic_info *diagnostic;
816 const char *msgid;
817 va_list *args;
818 const char *file;
819 int line;
820 diagnostic_t kind;
822 diagnostic->message.err_no = errno;
823 diagnostic->message.args_ptr = args;
824 diagnostic->message.format_spec = _(msgid);
825 /* If the diagnostic message doesn't specify a location,
826 use FILE and LINE. */
827 if (!text_specifies_location (&diagnostic->message, &diagnostic->location))
829 diagnostic->location.file = file;
830 diagnostic->location.line = line;
832 diagnostic->kind = kind;
835 /* Return a malloc'd string describing a location. The caller is
836 responsible for freeing the memory. */
837 char *
838 diagnostic_build_prefix (diagnostic)
839 diagnostic_info *diagnostic;
841 static const char *const diagnostic_kind_text[] = {
842 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
843 #include "diagnostic.def"
844 #undef DEFINE_DIAGNOSTIC_KIND
845 "must-not-happen"
847 if (diagnostic->kind >= DK_LAST_DIAGNOSTIC_KIND)
848 abort();
850 return diagnostic->location.file
851 ? build_message_string ("%s:%d: %s",
852 diagnostic->location.file,
853 diagnostic->location.line,
854 _(diagnostic_kind_text[diagnostic->kind]))
855 : build_message_string ("%s: %s", progname,
856 _(diagnostic_kind_text[diagnostic->kind]));
859 void
860 diagnostic_flush_buffer (context)
861 diagnostic_context *context;
863 output_buffer_to_stream (&context->buffer);
864 fflush (output_buffer_attached_stream (&context->buffer));
867 /* Count a diagnostic. Return true if the message should be printed. */
868 static bool
869 diagnostic_count_diagnostic (context, diagnostic)
870 diagnostic_context *context;
871 diagnostic_info *diagnostic;
873 diagnostic_t kind = diagnostic->kind;
874 switch (kind)
876 default:
877 abort();
878 break;
880 case DK_ICE:
881 #ifndef ENABLE_CHECKING
882 /* When not checking, ICEs are converted to fatal errors when an
883 error has already occurred. This is counteracted by
884 abort_on_error. */
885 if ((diagnostic_kind_count (context, DK_ERROR) > 0
886 || diagnostic_kind_count (context, DK_SORRY) > 0)
887 && !context->abort_on_error)
889 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
890 diagnostic->location.file, diagnostic->location.line);
891 exit (FATAL_EXIT_CODE);
893 #endif
894 if (context->internal_error)
895 (*context->internal_error) (diagnostic->message.format_spec,
896 diagnostic->message.args_ptr);
897 /* fall through */
899 case DK_FATAL: case DK_SORRY:
900 case DK_ANACHRONISM: case DK_NOTE:
901 ++diagnostic_kind_count (context, kind);
902 break;
904 case DK_WARNING:
905 if (!diagnostic_report_warnings_p ())
906 return false;
908 if (!warnings_are_errors)
910 ++diagnostic_kind_count (context, DK_WARNING);
911 break;
914 if (context->warnings_are_errors_message)
916 output_verbatim (&context->buffer,
917 "%s: warnings being treated as errors\n", progname);
918 context->warnings_are_errors_message = false;
921 /* and fall through */
922 case DK_ERROR:
923 ++diagnostic_kind_count (context, DK_ERROR);
924 break;
927 return true;
930 /* Take any action which is expected to happen after the diagnostic
931 is written out. This function does not always return. */
932 static void
933 diagnostic_action_after_output (context, diagnostic)
934 diagnostic_context *context;
935 diagnostic_info *diagnostic;
937 switch (diagnostic->kind)
939 case DK_DEBUG:
940 case DK_NOTE:
941 case DK_ANACHRONISM:
942 case DK_WARNING:
943 break;
945 case DK_ERROR:
946 case DK_SORRY:
947 if (context->abort_on_error)
948 real_abort ();
949 break;
951 case DK_ICE:
952 if (context->abort_on_error)
953 real_abort ();
955 fnotice (stderr, bug_report_request, bug_report_url);
956 exit (FATAL_EXIT_CODE);
958 case DK_FATAL:
959 if (context->abort_on_error)
960 real_abort ();
962 fnotice (stderr, "compilation terminated.\n");
963 exit (FATAL_EXIT_CODE);
965 default:
966 real_abort ();
970 /* Called when the start of a function definition is parsed,
971 this function prints on stderr the name of the function. */
972 void
973 announce_function (decl)
974 tree decl;
976 if (!quiet_flag)
978 if (rtl_dump_and_exit)
979 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl)));
980 else
981 verbatim (" %s", (*lang_hooks.decl_printable_name) (decl, 2));
982 fflush (stderr);
983 output_needs_newline (&global_dc->buffer) = true;
984 diagnostic_set_last_function (global_dc);
988 /* The default function to print out name of current function that caused
989 an error. */
990 void
991 lhd_print_error_function (context, file)
992 diagnostic_context *context;
993 const char *file;
995 if (diagnostic_last_function_changed (context))
997 const char *old_prefix = output_prefix (&context->buffer);
998 char *new_prefix = file ? build_message_string ("%s: ", file) : NULL;
1000 output_set_prefix (&context->buffer, new_prefix);
1002 if (current_function_decl == NULL)
1003 output_add_string (&context->buffer, _("At top level:"));
1004 else
1006 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
1007 output_printf
1008 (&context->buffer, "In member function `%s':",
1009 (*lang_hooks.decl_printable_name) (current_function_decl, 2));
1010 else
1011 output_printf
1012 (&context->buffer, "In function `%s':",
1013 (*lang_hooks.decl_printable_name) (current_function_decl, 2));
1015 output_add_newline (&context->buffer);
1017 diagnostic_set_last_function (context);
1018 output_buffer_to_stream (&context->buffer);
1019 context->buffer.state.prefix = old_prefix;
1020 free ((char*) new_prefix);
1024 /* Prints out, if necessary, the name of the current function
1025 that caused an error. Called from all error and warning functions.
1026 We ignore the FILE parameter, as it cannot be relied upon. */
1028 void
1029 diagnostic_report_current_function (context)
1030 diagnostic_context *context;
1032 diagnostic_report_current_module (context);
1033 (*lang_hooks.print_error_function) (context, input_filename);
1036 void
1037 diagnostic_report_current_module (context)
1038 diagnostic_context *context;
1040 struct file_stack *p;
1042 if (output_needs_newline (&context->buffer))
1044 output_add_newline (&context->buffer);
1045 output_needs_newline (&context->buffer) = false;
1048 if (input_file_stack && input_file_stack->next != 0
1049 && diagnostic_last_module_changed (context))
1051 for (p = input_file_stack->next; p; p = p->next)
1052 if (p == input_file_stack->next)
1053 output_verbatim (&context->buffer,
1054 "In file included from %s:%d",
1055 p->location.file, p->location.line);
1056 else
1057 output_verbatim (&context->buffer,
1058 ",\n from %s:%d",
1059 p->location.file, p->location.line);
1060 output_verbatim (&context->buffer, ":\n");
1061 diagnostic_set_last_module (context);
1065 static void
1066 default_diagnostic_starter (context, diagnostic)
1067 diagnostic_context *context;
1068 diagnostic_info *diagnostic;
1070 diagnostic_report_current_function (context);
1071 output_set_prefix (&context->buffer, diagnostic_build_prefix (diagnostic));
1074 static void
1075 default_diagnostic_finalizer (context, diagnostic)
1076 diagnostic_context *context;
1077 diagnostic_info *diagnostic __attribute__((unused));
1079 output_destroy_prefix (&context->buffer);
1082 /* Report a diagnostic message (an error or a warning) as specified by
1083 DC. This function is *the* subroutine in terms of which front-ends
1084 should implement their specific diagnostic handling modules. The
1085 front-end independent format specifiers are exactly those described
1086 in the documentation of output_format. */
1088 void
1089 diagnostic_report_diagnostic (context, diagnostic)
1090 diagnostic_context *context;
1091 diagnostic_info *diagnostic;
1093 if (context->lock++)
1094 error_recursion (context);
1096 if (diagnostic_count_diagnostic (context, diagnostic))
1098 (*diagnostic_starter (context)) (context, diagnostic);
1099 output_format (&context->buffer, &diagnostic->message);
1100 (*diagnostic_finalizer (context)) (context, diagnostic);
1101 output_flush (&context->buffer);
1102 diagnostic_action_after_output (context, diagnostic);
1105 context->lock--;
1108 /* Report a diagnostic MESSAGE at the declaration DECL.
1109 MSG is a format string which uses %s to substitute the declaration
1110 name; subsequent substitutions are a la output_format. */
1111 static void
1112 diagnostic_for_decl (context, diagnostic, decl)
1113 diagnostic_context *context;
1114 diagnostic_info *diagnostic;
1115 tree decl;
1117 if (context->lock++)
1118 error_recursion (context);
1120 if (diagnostic_count_diagnostic (context, diagnostic))
1122 (*diagnostic_starter (context)) (context, diagnostic);
1123 format_with_decl (&context->buffer, &diagnostic->message, decl);
1124 (*diagnostic_finalizer (context)) (context, diagnostic);
1125 output_flush (&context->buffer);
1126 diagnostic_action_after_output (context, diagnostic);
1129 context->lock--;
1132 /* Given a partial pathname as input, return another pathname that
1133 shares no directory elements with the pathname of __FILE__. This
1134 is used by fancy_abort() to print `Internal compiler error in expr.c'
1135 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1137 const char *
1138 trim_filename (name)
1139 const char *name;
1141 static const char this_file[] = __FILE__;
1142 const char *p = name, *q = this_file;
1144 /* First skip any "../" in each filename. This allows us to give a proper
1145 reference to a file in a subdirectory. */
1146 while (p[0] == '.' && p[1] == '.'
1147 && (p[2] == DIR_SEPARATOR
1148 #ifdef DIR_SEPARATOR_2
1149 || p[2] == DIR_SEPARATOR_2
1150 #endif
1152 p += 3;
1154 while (q[0] == '.' && q[1] == '.'
1155 && (q[2] == DIR_SEPARATOR
1156 #ifdef DIR_SEPARATOR_2
1157 || p[2] == DIR_SEPARATOR_2
1158 #endif
1160 q += 3;
1162 /* Now skip any parts the two filenames have in common. */
1163 while (*p == *q && *p != 0 && *q != 0)
1164 p++, q++;
1166 /* Now go backwards until the previous directory separator. */
1167 while (p > name && p[-1] != DIR_SEPARATOR
1168 #ifdef DIR_SEPARATOR_2
1169 && p[-1] != DIR_SEPARATOR_2
1170 #endif
1172 p--;
1174 return p;
1177 /* Standard error reporting routines in increasing order of severity.
1178 All of these take arguments like printf. */
1180 /* Text to be emitted verbatim to the error message stream; this
1181 produces no prefix and disables line-wrapping. Use rarely. */
1182 void
1183 verbatim (const char *msgid, ...)
1185 text_info text;
1186 va_list ap;
1188 va_start (ap, msgid);
1189 text.err_no = errno;
1190 text.args_ptr = &ap;
1191 text.format_spec = _(msgid);
1192 output_do_verbatim (&global_dc->buffer, &text);
1193 output_buffer_to_stream (&global_dc->buffer);
1194 va_end (ap);
1197 /* An informative note. Use this for additional details on an error
1198 message. */
1199 void
1200 inform (const char *msgid, ...)
1202 diagnostic_info diagnostic;
1203 va_list ap;
1205 va_start (ap, msgid);
1206 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, input_line,
1207 DK_NOTE);
1208 report_diagnostic (&diagnostic);
1209 va_end (ap);
1212 /* A warning. Use this for code which is correct according to the
1213 relevant language specification but is likely to be buggy anyway. */
1214 void
1215 warning (const char *msgid, ...)
1217 diagnostic_info diagnostic;
1218 va_list ap;
1220 va_start (ap, msgid);
1221 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, input_line,
1222 DK_WARNING);
1223 report_diagnostic (&diagnostic);
1224 va_end (ap);
1227 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
1228 given on the command line, in which case it issues an error. Use
1229 this for diagnostics required by the relevant language standard,
1230 if you have chosen not to make them errors.
1232 Note that these diagnostics are issued independent of the setting
1233 of the -pedantic command-line switch. To get a warning enabled
1234 only with that switch, write "if (pedantic) pedwarn (...);" */
1235 void
1236 pedwarn (const char *msgid, ...)
1238 diagnostic_info diagnostic;
1239 va_list ap;
1241 va_start (ap, msgid);
1242 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, input_line,
1243 pedantic_error_kind ());
1244 report_diagnostic (&diagnostic);
1245 va_end (ap);
1248 /* A hard error: the code is definitely ill-formed, and an object file
1249 will not be produced. */
1250 void
1251 error (const char *msgid, ...)
1253 diagnostic_info diagnostic;
1254 va_list ap;
1256 va_start (ap, msgid);
1257 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, input_line,
1258 DK_ERROR);
1259 report_diagnostic (&diagnostic);
1260 va_end (ap);
1263 /* "Sorry, not implemented." Use for a language feature which is
1264 required by the relevant specification but not implemented by GCC.
1265 An object file will not be produced. */
1266 void
1267 sorry (const char *msgid, ...)
1269 diagnostic_info diagnostic;
1270 va_list ap;
1272 va_start (ap, msgid);
1273 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, input_line,
1274 DK_SORRY);
1275 report_diagnostic (&diagnostic);
1276 va_end (ap);
1279 /* An error which is severe enough that we make no attempt to
1280 continue. Do not use this for internal consistency checks; that's
1281 internal_error. Use of this function should be rare. */
1282 void
1283 fatal_error (const char *msgid, ...)
1285 diagnostic_info diagnostic;
1286 va_list ap;
1288 va_start (ap, msgid);
1289 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, input_line,
1290 DK_FATAL);
1291 report_diagnostic (&diagnostic);
1292 va_end (ap);
1294 /* NOTREACHED */
1295 real_abort ();
1298 /* An internal consistency check has failed. We make no attempt to
1299 continue. Note that unless there is debugging value to be had from
1300 a more specific message, or some other good reason, you should use
1301 abort () instead of calling this function directly. */
1302 void
1303 internal_error (const char *msgid, ...)
1305 diagnostic_info diagnostic;
1306 va_list ap;
1308 va_start (ap, msgid);
1309 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, input_line,
1310 DK_ICE);
1311 report_diagnostic (&diagnostic);
1312 va_end (ap);
1314 /* NOTREACHED */
1315 real_abort ();
1318 /* Variants of some of the above, which make reference to a particular
1319 DECL node. These are deprecated. */
1321 void
1322 warning_with_decl (tree decl, const char *msgid, ...)
1324 diagnostic_info diagnostic;
1325 va_list ap;
1327 va_start (ap, msgid);
1329 /* Do not issue a warning about a decl which came from a system header,
1330 unless -Wsystem-headers. */
1331 if (DECL_IN_SYSTEM_HEADER (decl) && !warn_system_headers)
1332 return;
1334 diagnostic_set_info (&diagnostic, msgid, &ap,
1335 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1336 DK_WARNING);
1337 diagnostic_for_decl (global_dc, &diagnostic, decl);
1338 va_end (ap);
1341 void
1342 pedwarn_with_decl (tree decl, const char *msgid, ...)
1344 diagnostic_info diagnostic;
1345 va_list ap;
1347 va_start (ap, msgid);
1349 /* Do not issue a warning about a decl which came from a system header,
1350 unless -Wsystem-headers. */
1351 if (DECL_IN_SYSTEM_HEADER (decl) && !warn_system_headers)
1352 return;
1354 diagnostic_set_info (&diagnostic, msgid, &ap,
1355 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1356 pedantic_error_kind ());
1357 diagnostic_for_decl (global_dc, &diagnostic, decl);
1359 va_end (ap);
1362 void
1363 error_with_decl (tree decl, const char *msgid, ...)
1365 diagnostic_info diagnostic;
1366 va_list ap;
1368 va_start (ap, msgid);
1369 diagnostic_set_info (&diagnostic, msgid, &ap,
1370 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1371 DK_ERROR);
1372 diagnostic_for_decl (global_dc, &diagnostic, decl);
1373 va_end (ap);
1376 /* Special case error functions. Most are implemented in terms of the
1377 above, or should be. */
1379 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1380 runs its second argument through gettext. */
1381 void
1382 fnotice (FILE *file, const char *msgid, ...)
1384 va_list ap;
1386 va_start (ap, msgid);
1387 vfprintf (file, _(msgid), ap);
1388 va_end (ap);
1391 /* Warn about a use of an identifier which was marked deprecated. */
1392 void
1393 warn_deprecated_use (node)
1394 tree node;
1396 if (node == 0 || !warn_deprecated_decl)
1397 return;
1399 if (DECL_P (node))
1400 warning ("`%s' is deprecated (declared at %s:%d)",
1401 IDENTIFIER_POINTER (DECL_NAME (node)),
1402 DECL_SOURCE_FILE (node), DECL_SOURCE_LINE (node));
1403 else if (TYPE_P (node))
1405 const char *what = NULL;
1406 tree decl = TYPE_STUB_DECL (node);
1408 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
1409 what = IDENTIFIER_POINTER (TYPE_NAME (node));
1410 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
1411 && DECL_NAME (TYPE_NAME (node)))
1412 what = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node)));
1414 if (what)
1416 if (decl)
1417 warning ("`%s' is deprecated (declared at %s:%d)", what,
1418 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
1419 else
1420 warning ("`%s' is deprecated", what);
1422 else if (decl)
1423 warning ("type is deprecated (declared at %s:%d)",
1424 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
1425 else
1426 warning ("type is deprecated");
1430 /* Inform the user that an error occurred while trying to report some
1431 other error. This indicates catastrophic internal inconsistencies,
1432 so give up now. But do try to flush out the previous error.
1433 This mustn't use internal_error, that will cause infinite recursion. */
1435 static void
1436 error_recursion (context)
1437 diagnostic_context *context;
1439 if (context->lock < 3)
1440 output_flush (&context->buffer);
1442 fnotice (stderr,
1443 "Internal compiler error: Error reporting routines re-entered.\n");
1444 fnotice (stderr, bug_report_request, bug_report_url);
1445 exit (FATAL_EXIT_CODE);
1448 /* Report an internal compiler error in a friendly manner. This is
1449 the function that gets called upon use of abort() in the source
1450 code generally, thanks to a special macro. */
1452 void
1453 fancy_abort (file, line, function)
1454 const char *file;
1455 int line;
1456 const char *function;
1458 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1461 /* Really call the system 'abort'. This has to go right at the end of
1462 this file, so that there are no functions after it that call abort
1463 and get the system abort instead of our macro. */
1464 #undef abort
1465 static void
1466 real_abort ()
1468 abort ();