PR C++/689
[official-gcc.git] / gcc / diagnostic.c
blobce6c8bc60bbce719b3cd00e46a8698437a791686
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_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 *,
63 long unsigned int));
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 *,
68 unsigned long int));
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 *,
72 const char *));
73 static void output_clear_data PARAMS ((output_buffer *));
75 static void default_diagnostic_starter PARAMS ((diagnostic_context *,
76 diagnostic_info *));
77 static void default_diagnostic_finalizer PARAMS ((diagnostic_context *,
78 diagnostic_info *));
80 static void error_recursion PARAMS ((diagnostic_context *)) ATTRIBUTE_NORETURN;
81 static bool text_specifies_location PARAMS ((text_info *, location_t *));
82 static void real_abort PARAMS ((void)) ATTRIBUTE_NORETURN;
84 extern int rtl_dump_and_exit;
85 extern int warnings_are_errors;
87 /* A diagnostic_context surrogate for stderr. */
88 static diagnostic_context global_diagnostic_context;
89 diagnostic_context *global_dc = &global_diagnostic_context;
92 /* Subroutine of output_set_maximum_length. Set up BUFFER's
93 internal maximum characters per line. */
94 static void
95 set_real_maximum_length (buffer)
96 output_buffer *buffer;
98 /* If we're told not to wrap lines then do the obvious thing. In case
99 we'll emit prefix only once per diagnostic message, it is appropriate
100 not to increase unnecessarily the line-length cut-off. */
101 if (!output_is_line_wrapping (buffer)
102 || output_prefixing_rule (buffer) == DIAGNOSTICS_SHOW_PREFIX_ONCE
103 || output_prefixing_rule (buffer) == DIAGNOSTICS_SHOW_PREFIX_NEVER)
104 line_wrap_cutoff (buffer) = output_line_cutoff (buffer);
105 else
107 int prefix_length = buffer->state.prefix ?
108 strlen (buffer->state.prefix) : 0;
109 /* If the prefix is ridiculously too long, output at least
110 32 characters. */
111 if (output_line_cutoff (buffer) - prefix_length < 32)
112 line_wrap_cutoff (buffer) = output_line_cutoff (buffer) + 32;
113 else
114 line_wrap_cutoff (buffer) = output_line_cutoff (buffer);
118 /* Sets the number of maximum characters per line BUFFER can output
119 in line-wrapping mode. A LENGTH value 0 suppresses line-wrapping. */
120 void
121 output_set_maximum_length (buffer, length)
122 output_buffer *buffer;
123 int length;
125 output_line_cutoff (buffer) = length;
126 set_real_maximum_length (buffer);
129 /* Sets BUFFER's PREFIX. */
130 void
131 output_set_prefix (buffer, prefix)
132 output_buffer *buffer;
133 const char *prefix;
135 buffer->state.prefix = prefix;
136 set_real_maximum_length (buffer);
137 prefix_was_emitted_for (buffer) = false;
138 output_indentation (buffer) = 0;
141 /* Return a pointer to the last character emitted in the output
142 BUFFER area. A NULL pointer means no character available. */
143 const char *
144 output_last_position (buffer)
145 const output_buffer *buffer;
147 const char *p = NULL;
149 if (obstack_base (&buffer->obstack) != obstack_next_free (&buffer->obstack))
150 p = ((const char *) obstack_next_free (&buffer->obstack)) - 1;
151 return p;
154 /* Free BUFFER's prefix, a previously malloc'd string. */
155 void
156 output_destroy_prefix (buffer)
157 output_buffer *buffer;
159 if (buffer->state.prefix != NULL)
161 free ((char *) buffer->state.prefix);
162 buffer->state.prefix = NULL;
166 /* Zero out any text output so far in BUFFER. */
167 void
168 output_clear_message_text (buffer)
169 output_buffer *buffer;
171 obstack_free (&buffer->obstack, obstack_base (&buffer->obstack));
172 output_text_length (buffer) = 0;
175 /* Zero out any formatting data used so far by BUFFER. */
176 static void
177 output_clear_data (buffer)
178 output_buffer *buffer;
180 prefix_was_emitted_for (buffer) = false;
181 output_indentation (buffer) = 0;
184 /* Construct an output BUFFER with PREFIX and of MAXIMUM_LENGTH
185 characters per line. */
186 void
187 init_output_buffer (buffer, prefix, maximum_length)
188 output_buffer *buffer;
189 const char *prefix;
190 int maximum_length;
192 memset (buffer, 0, sizeof (output_buffer));
193 obstack_init (&buffer->obstack);
194 output_buffer_attached_stream (buffer) = stderr;
195 output_line_cutoff (buffer) = maximum_length;
196 output_prefixing_rule (buffer) = diagnostic_prefixing_rule (global_dc);
197 output_set_prefix (buffer, prefix);
198 output_text_length (buffer) = 0;
199 output_clear_data (buffer);
202 /* Reinitialize BUFFER. */
203 void
204 output_clear (buffer)
205 output_buffer *buffer;
207 output_clear_message_text (buffer);
208 output_clear_data (buffer);
211 /* Finishes constructing a NULL-terminated character string representing
212 the BUFFERed message. */
213 const char *
214 output_finalize_message (buffer)
215 output_buffer *buffer;
217 obstack_1grow (&buffer->obstack, '\0');
218 return output_message_text (buffer);
221 /* Return the amount of characters BUFFER can accept to
222 make a full line. */
224 output_space_left (buffer)
225 const output_buffer *buffer;
227 return line_wrap_cutoff (buffer) - output_text_length (buffer);
230 /* Write out BUFFER's prefix. */
231 void
232 output_emit_prefix (buffer)
233 output_buffer *buffer;
235 if (buffer->state.prefix != NULL)
237 switch (output_prefixing_rule (buffer))
239 default:
240 case DIAGNOSTICS_SHOW_PREFIX_NEVER:
241 break;
243 case DIAGNOSTICS_SHOW_PREFIX_ONCE:
244 if (prefix_was_emitted_for (buffer))
246 output_indent (buffer);
247 break;
249 output_indentation (buffer) += 3;
250 /* Fall through. */
252 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
254 int prefix_length = strlen (buffer->state.prefix);
255 output_append_r (buffer, buffer->state.prefix, prefix_length);
256 prefix_was_emitted_for (buffer) = true;
258 break;
263 /* Have BUFFER start a new line. */
264 void
265 output_add_newline (buffer)
266 output_buffer *buffer;
268 obstack_1grow (&buffer->obstack, '\n');
269 output_text_length (buffer) = 0;
272 /* Appends a character to BUFFER. */
273 void
274 output_add_character (buffer, c)
275 output_buffer *buffer;
276 int c;
278 if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
279 output_add_newline (buffer);
280 obstack_1grow (&buffer->obstack, c);
281 ++output_text_length (buffer);
284 /* Adds a space to BUFFER. */
285 void
286 output_add_space (buffer)
287 output_buffer *buffer;
289 if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
291 output_add_newline (buffer);
292 return;
294 obstack_1grow (&buffer->obstack, ' ');
295 ++output_text_length (buffer);
298 /* These functions format an INTEGER into BUFFER as suggested by their
299 names. */
300 void
301 output_decimal (buffer, i)
302 output_buffer *buffer;
303 int i;
305 output_formatted_scalar (buffer, "%d", i);
308 static inline void
309 output_long_decimal (output_buffer *buffer, long int i)
311 output_formatted_scalar (buffer, "%ld", i);
314 static inline void
315 output_unsigned_decimal (output_buffer *buffer, unsigned int i)
317 output_formatted_scalar (buffer, "%u", i);
320 static inline void
321 output_long_unsigned_decimal (output_buffer *buffer, long unsigned int i)
323 output_formatted_scalar (buffer, "%lu", i);
326 static inline void
327 output_octal (output_buffer *buffer, unsigned int i)
329 output_formatted_scalar (buffer, "%o", i);
332 static inline void
333 output_long_octal (output_buffer *buffer, long unsigned int i)
335 output_formatted_scalar (buffer, "%lo", i);
338 static inline void
339 output_hexadecimal (output_buffer *buffer, unsigned int i)
341 output_formatted_scalar (buffer, "%x", i);
344 static inline void
345 output_long_hexadecimal (output_buffer *buffer, long unsigned int i)
347 output_formatted_scalar (buffer, "%lx", i);
350 static inline void
351 output_pointer (output_buffer *buffer, void *p)
353 output_formatted_scalar (buffer, HOST_PTR_PRINTF, p);
356 /* Append to BUFFER a string specified by its STARTING character
357 and LENGTH. */
358 static void
359 output_append_r (buffer, start, length)
360 output_buffer *buffer;
361 const char *start;
362 int length;
364 obstack_grow (&buffer->obstack, start, length);
365 output_text_length (buffer) += length;
368 /* Append a string deliminated by START and END to BUFFER. No wrapping is
369 done. However, if beginning a new line then emit BUFFER->state.prefix
370 and skip any leading whitespace if appropriate. The caller must ensure
371 that it is safe to do so. */
372 void
373 output_append (buffer, start, end)
374 output_buffer *buffer;
375 const char *start;
376 const char *end;
378 /* Emit prefix and skip whitespace if we're starting a new line. */
379 if (is_starting_newline (buffer))
381 output_emit_prefix (buffer);
382 if (output_is_line_wrapping (buffer))
383 while (start != end && *start == ' ')
384 ++start;
386 output_append_r (buffer, start, end - start);
389 /* Insert enough spaces into BUFFER to bring the column position to
390 the current indentation level, assuming that a newline has just
391 been written to the buffer. */
392 static void
393 output_indent (buffer)
394 output_buffer *buffer;
396 int n = output_indentation (buffer);
397 int i;
399 for (i = 0; i < n; ++i)
400 output_add_character (buffer, ' ');
403 /* Wrap a text delimited by START and END into BUFFER. */
404 static void
405 wrap_text (buffer, start, end)
406 output_buffer *buffer;
407 const char *start;
408 const char *end;
410 bool is_wrapping = output_is_line_wrapping (buffer);
412 while (start != end)
414 /* Dump anything bordered by whitespaces. */
416 const char *p = start;
417 while (p != end && *p != ' ' && *p != '\n')
418 ++p;
419 if (is_wrapping && p - start >= output_space_left (buffer))
420 output_add_newline (buffer);
421 output_append (buffer, start, p);
422 start = p;
425 if (start != end && *start == ' ')
427 output_add_space (buffer);
428 ++start;
430 if (start != end && *start == '\n')
432 output_add_newline (buffer);
433 ++start;
438 /* Same as wrap_text but wrap text only when in line-wrapping mode. */
439 static void
440 maybe_wrap_text (buffer, start, end)
441 output_buffer *buffer;
442 const char *start;
443 const char *end;
445 if (output_is_line_wrapping (buffer))
446 wrap_text (buffer, start, end);
447 else
448 output_append (buffer, start, end);
452 /* Append a STRING to BUFFER; the STRING might be line-wrapped if in
453 appropriate mode. */
454 void
455 output_add_string (buffer, str)
456 output_buffer *buffer;
457 const char *str;
459 maybe_wrap_text (buffer, str, str + (str ? strlen (str) : 0));
462 /* Append an identifier ID to BUFFER. */
463 void
464 output_add_identifier (buffer, id)
465 output_buffer *buffer;
466 tree id;
468 output_append (buffer, IDENTIFIER_POINTER (id),
469 IDENTIFIER_POINTER (id) + IDENTIFIER_LENGTH (id));
472 /* Flush the content of BUFFER onto the attached stream,
473 and reinitialize. */
475 static void
476 output_buffer_to_stream (buffer)
477 output_buffer *buffer;
479 const char *text = output_finalize_message (buffer);
480 fputs (text, output_buffer_attached_stream (buffer));
481 output_clear_message_text (buffer);
484 /* Format a message pointed to by TEXT. The following format specifiers are
485 recognized as being language independent:
486 %d, %i: (signed) integer in base ten.
487 %u: unsigned integer in base ten.
488 %o: unsigned integer in base eight.
489 %x: unsigned integer in base sixteen.
490 %ld, %li, %lo, %lu, %lx: long versions of the above.
491 %c: character.
492 %s: string.
493 %p: pointer.
494 %%: `%'.
495 %*.s: a substring the length of which is specified by an integer.
496 %H: location_t. */
497 static void
498 output_format (buffer, text)
499 output_buffer *buffer;
500 text_info *text;
502 for (; *text->format_spec; ++text->format_spec)
504 bool long_integer = 0;
506 /* Ignore text. */
508 const char *p = text->format_spec;
509 while (*p && *p != '%')
510 ++p;
511 wrap_text (buffer, text->format_spec, p);
512 text->format_spec = p;
515 if (*text->format_spec == '\0')
516 break;
518 /* We got a '%'. Let's see what happens. Record whether we're
519 parsing a long integer format specifier. */
520 if (*++text->format_spec == 'l')
522 long_integer = true;
523 ++text->format_spec;
526 /* Handle %c, %d, %i, %ld, %li, %lo, %lu, %lx, %o, %s, %u,
527 %x, %p, %.*s; %%. And nothing else. Front-ends should install
528 printers to grok language specific format specifiers. */
529 switch (*text->format_spec)
531 case 'c':
532 output_add_character (buffer, va_arg (*text->args_ptr, int));
533 break;
535 case 'd':
536 case 'i':
537 if (long_integer)
538 output_long_decimal (buffer, va_arg (*text->args_ptr, long int));
539 else
540 output_decimal (buffer, va_arg (*text->args_ptr, int));
541 break;
543 case 'o':
544 if (long_integer)
545 output_long_octal (buffer,
546 va_arg (*text->args_ptr, unsigned long int));
547 else
548 output_octal (buffer, va_arg (*text->args_ptr, unsigned int));
549 break;
551 case 's':
552 output_add_string (buffer, va_arg (*text->args_ptr, const char *));
553 break;
555 case 'p':
556 output_pointer (buffer, va_arg (*text->args_ptr, void *));
557 break;
559 case 'u':
560 if (long_integer)
561 output_long_unsigned_decimal
562 (buffer, va_arg (*text->args_ptr, long unsigned int));
563 else
564 output_unsigned_decimal
565 (buffer, va_arg (*text->args_ptr, unsigned int));
566 break;
568 case 'x':
569 if (long_integer)
570 output_long_hexadecimal
571 (buffer, va_arg (*text->args_ptr, unsigned long int));
572 else
573 output_hexadecimal
574 (buffer, va_arg (*text->args_ptr, unsigned int));
575 break;
577 case '%':
578 output_add_character (buffer, '%');
579 break;
581 case 'H':
583 const location_t *locus = va_arg (*text->args_ptr, location_t *);
584 output_add_string (buffer, "file '");
585 output_add_string (buffer, locus->file);
586 output_add_string (buffer, "', line ");
587 output_decimal (buffer, locus->line);
589 break;
591 case '.':
593 int n;
594 const char *s;
595 /* We handle no precision specifier but `%.*s'. */
596 if (*++text->format_spec != '*')
597 abort ();
598 else if (*++text->format_spec != 's')
599 abort ();
600 n = va_arg (*text->args_ptr, int);
601 s = va_arg (*text->args_ptr, const char *);
602 output_append (buffer, s, s + n);
604 break;
606 default:
607 if (!buffer->format_decoder
608 || !(*buffer->format_decoder) (buffer, text))
610 /* Hmmm. The front-end failed to install a format translator
611 but called us with an unrecognized format. Or, maybe, the
612 translated string just contains an invalid format, or
613 has formats in the wrong order. Sorry. */
614 abort ();
620 /* Return a malloc'd string containing MSG formatted a la printf. The
621 caller is responsible for freeing the memory. */
622 static char *
623 build_message_string VPARAMS ((const char *msg, ...))
625 char *str;
627 VA_OPEN (ap, msg);
628 VA_FIXEDARG (ap, const char *, msg);
630 vasprintf (&str, msg, ap);
632 VA_CLOSE (ap);
634 return str;
637 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
638 char *
639 file_name_as_prefix (f)
640 const char *f;
642 return build_message_string ("%s: ", f);
645 /* Format a message into BUFFER a la printf. */
646 void
647 output_printf VPARAMS ((struct output_buffer *buffer, const char *msgid, ...))
649 text_info text;
650 VA_OPEN (ap, msgid);
651 VA_FIXEDARG (ap, output_buffer *, buffer);
652 VA_FIXEDARG (ap, const char *, msgid);
654 text.args_ptr = &ap;
655 text.format_spec = _(msgid);
656 output_format (buffer, &text);
657 VA_CLOSE (ap);
660 /* Print a message relevant to the given DECL. */
661 static void
662 format_with_decl (buffer, text, decl)
663 output_buffer *buffer;
664 text_info *text;
665 tree decl;
667 const char *p;
669 /* Do magic to get around lack of varargs support for insertion
670 of arguments into existing list. We know that the decl is first;
671 we ass_u_me that it will be printed with "%s". */
672 for (p = text->format_spec; *p; ++p)
674 if (*p == '%')
676 if (*(p + 1) == '%')
677 ++p;
678 else if (*(p + 1) != 's')
679 abort ();
680 else
681 break;
685 /* Print the left-hand substring. */
686 maybe_wrap_text (buffer, text->format_spec, p);
688 if (*p == '%') /* Print the name. */
690 const char *const n = (DECL_NAME (decl)
691 ? (*lang_hooks.decl_printable_name) (decl, 2)
692 : _("((anonymous))"));
693 output_add_string (buffer, n);
694 while (*p)
696 ++p;
697 if (ISALPHA (*(p - 1) & 0xFF))
698 break;
702 if (*p) /* Print the rest of the message. */
704 text->format_spec = p;
705 output_format (buffer, text);
709 /* Flush the content of BUFFER onto the attached stream. */
710 static void
711 output_flush (buffer)
712 output_buffer *buffer;
714 output_buffer_to_stream (buffer);
715 output_clear_data (buffer);
716 fputc ('\n', output_buffer_attached_stream (buffer));
717 fflush (output_buffer_attached_stream (buffer));
720 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
721 settings needed by BUFFER for a verbatim formatting. */
722 static void
723 output_do_verbatim (buffer, text)
724 output_buffer *buffer;
725 text_info *text;
727 diagnostic_prefixing_rule_t rule = output_prefixing_rule (buffer);
728 int line_cutoff = output_line_cutoff (buffer);
730 /* Set verbatim mode. */
731 output_prefixing_rule (buffer) = DIAGNOSTICS_SHOW_PREFIX_NEVER;
732 output_line_cutoff (buffer) = 0;
733 /* Do the actual formatting. */
734 output_format (buffer, text);
735 /* Restore previous settings. */
736 output_prefixing_rule (buffer) = rule;
737 output_line_cutoff (buffer) = line_cutoff;
740 /* Output MESSAGE verbatim into BUFFER. */
741 void
742 output_verbatim VPARAMS ((output_buffer *buffer, const char *msgid, ...))
744 text_info text;
745 VA_OPEN (ap, msgid);
746 VA_FIXEDARG (ap, output_buffer *, buffer);
747 VA_FIXEDARG (ap, const char *, msgid);
749 text.format_spec = msgid;
750 text.args_ptr = &ap;
751 output_do_verbatim (buffer, &text);
752 VA_CLOSE (ap);
756 /* Initialize the diagnostic message outputting machinery. */
757 void
758 diagnostic_initialize (context)
759 diagnostic_context *context;
761 memset (context, 0, sizeof *context);
762 obstack_init (&context->buffer.obstack);
764 /* By default, diagnostics are sent to stderr. */
765 output_buffer_attached_stream (&context->buffer) = stderr;
767 /* By default, we emit prefixes once per message. */
768 diagnostic_prefixing_rule (context) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
770 diagnostic_starter (context) = default_diagnostic_starter;
771 diagnostic_finalizer (context) = default_diagnostic_finalizer;
772 context->warnings_are_errors_message = warnings_are_errors;
775 /* Returns true if the next format specifier in TEXT is a format specifier
776 for a location_t. If so, update the object pointed by LOCUS to reflect
777 the specified location in *TEXT->args_ptr. */
778 static bool
779 text_specifies_location (text, locus)
780 text_info *text;
781 location_t *locus;
783 const char *p;
784 /* Skip any leading text. */
785 for (p = text->format_spec; *p && *p != '%'; ++p)
788 /* Extract the location information if any. */
789 if (*p == '%' && *++p == 'H')
791 *locus = *va_arg (*text->args_ptr, location_t *);
792 text->format_spec = p + 1;
793 return true;
796 return false;
799 void
800 diagnostic_set_info (diagnostic, msgid, args, file, line, kind)
801 diagnostic_info *diagnostic;
802 const char *msgid;
803 va_list *args;
804 const char *file;
805 int line;
806 diagnostic_t kind;
808 diagnostic->message.format_spec = msgid;
809 diagnostic->message.args_ptr = args;
810 /* If the diagnostic message doesn't specify a location,
811 use FILE and LINE. */
812 if (!text_specifies_location (&diagnostic->message, &diagnostic->location))
814 diagnostic->location.file = file;
815 diagnostic->location.line = line;
817 diagnostic->kind = kind;
820 /* Return a malloc'd string describing a location. The caller is
821 responsible for freeing the memory. */
822 char *
823 diagnostic_build_prefix (diagnostic)
824 diagnostic_info *diagnostic;
826 static const char *const diagnostic_kind_text[] = {
827 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
828 #include "diagnostic.def"
829 #undef DEFINE_DIAGNOSTIC_KIND
830 "must-not-happen"
832 if (diagnostic->kind >= DK_LAST_DIAGNOSTIC_KIND)
833 abort();
835 return diagnostic->location.file
836 ? build_message_string ("%s:%d: %s",
837 diagnostic->location.file,
838 diagnostic->location.line,
839 _(diagnostic_kind_text[diagnostic->kind]))
840 : build_message_string ("%s: %s", progname,
841 _(diagnostic_kind_text[diagnostic->kind]));
844 void
845 diagnostic_flush_buffer (context)
846 diagnostic_context *context;
848 output_buffer_to_stream (&context->buffer);
849 fflush (output_buffer_attached_stream (&context->buffer));
852 /* Count a diagnostic. Return true if the message should be printed. */
853 bool
854 diagnostic_count_diagnostic (context, kind)
855 diagnostic_context *context;
856 diagnostic_t kind;
858 switch (kind)
860 default:
861 abort();
862 break;
864 case DK_FATAL: case DK_ICE: case DK_SORRY:
865 case DK_ANACHRONISM: case DK_NOTE:
866 ++diagnostic_kind_count (context, kind);
867 break;
869 case DK_WARNING:
870 if (!diagnostic_report_warnings_p ())
871 return false;
872 else if (!warnings_are_errors)
874 ++diagnostic_kind_count (context, DK_WARNING);
875 break;
877 /* else fall through. */
879 case DK_ERROR:
880 if (kind == DK_WARNING && context->warnings_are_errors_message)
882 output_verbatim (&context->buffer,
883 "%s: warnings being treated as errors\n", progname);
884 context->warnings_are_errors_message = false;
886 ++diagnostic_kind_count (context, DK_ERROR);
887 break;
890 return true;
893 /* Called when the start of a function definition is parsed,
894 this function prints on stderr the name of the function. */
895 void
896 announce_function (decl)
897 tree decl;
899 if (!quiet_flag)
901 if (rtl_dump_and_exit)
902 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl)));
903 else
904 verbatim (" %s", (*lang_hooks.decl_printable_name) (decl, 2));
905 fflush (stderr);
906 output_needs_newline (&global_dc->buffer) = true;
907 diagnostic_set_last_function (global_dc);
911 /* The default function to print out name of current function that caused
912 an error. */
913 void
914 lhd_print_error_function (context, file)
915 diagnostic_context *context;
916 const char *file;
918 if (diagnostic_last_function_changed (context))
920 const char *old_prefix = output_prefix (&context->buffer);
921 char *new_prefix = file ? build_message_string ("%s: ", file) : NULL;
923 output_set_prefix (&context->buffer, new_prefix);
925 if (current_function_decl == NULL)
926 output_add_string (&context->buffer, _("At top level:"));
927 else
929 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
930 output_printf
931 (&context->buffer, "In member function `%s':",
932 (*lang_hooks.decl_printable_name) (current_function_decl, 2));
933 else
934 output_printf
935 (&context->buffer, "In function `%s':",
936 (*lang_hooks.decl_printable_name) (current_function_decl, 2));
938 output_add_newline (&context->buffer);
940 diagnostic_set_last_function (context);
941 output_buffer_to_stream (&context->buffer);
942 context->buffer.state.prefix = old_prefix;
943 free ((char*) new_prefix);
947 /* Prints out, if necessary, the name of the current function
948 that caused an error. Called from all error and warning functions.
949 We ignore the FILE parameter, as it cannot be relied upon. */
951 void
952 diagnostic_report_current_function (context)
953 diagnostic_context *context;
955 diagnostic_report_current_module (context);
956 (*lang_hooks.print_error_function) (context, input_filename);
959 void
960 diagnostic_report_current_module (context)
961 diagnostic_context *context;
963 struct file_stack *p;
965 if (output_needs_newline (&context->buffer))
967 output_add_newline (&context->buffer);
968 output_needs_newline (&context->buffer) = false;
971 if (input_file_stack && input_file_stack->next != 0
972 && diagnostic_last_module_changed (context))
974 for (p = input_file_stack->next; p; p = p->next)
975 if (p == input_file_stack->next)
976 output_verbatim (&context->buffer,
977 "In file included from %s:%d",
978 p->location.file, p->location.line);
979 else
980 output_verbatim (&context->buffer,
981 ",\n from %s:%d",
982 p->location.file, p->location.line);
983 output_verbatim (&context->buffer, ":\n");
984 diagnostic_set_last_module (context);
988 static void
989 default_diagnostic_starter (context, diagnostic)
990 diagnostic_context *context;
991 diagnostic_info *diagnostic;
993 diagnostic_report_current_function (context);
994 output_set_prefix (&context->buffer, diagnostic_build_prefix (diagnostic));
997 static void
998 default_diagnostic_finalizer (context, diagnostic)
999 diagnostic_context *context;
1000 diagnostic_info *diagnostic __attribute__((unused));
1002 output_destroy_prefix (&context->buffer);
1005 /* Report a diagnostic message (an error or a warning) as specified by
1006 DC. This function is *the* subroutine in terms of which front-ends
1007 should implement their specific diagnostic handling modules. The
1008 front-end independent format specifiers are exactly those described
1009 in the documentation of output_format. */
1011 void
1012 diagnostic_report_diagnostic (context, diagnostic)
1013 diagnostic_context *context;
1014 diagnostic_info *diagnostic;
1016 if (context->lock++)
1017 error_recursion (context);
1019 if (diagnostic_count_diagnostic (context, diagnostic->kind))
1021 (*diagnostic_starter (context)) (context, diagnostic);
1022 output_format (&context->buffer, &diagnostic->message);
1023 (*diagnostic_finalizer (context)) (context, diagnostic);
1024 output_flush (&context->buffer);
1027 if (context->abort_on_error && diagnostic->kind <= DK_ERROR)
1028 real_abort();
1029 --context->lock;
1032 /* Report a diagnostic MESSAGE at the declaration DECL.
1033 MSG is a format string which uses %s to substitute the declaration
1034 name; subsequent substitutions are a la output_format. */
1035 static void
1036 diagnostic_for_decl (diagnostic, decl)
1037 diagnostic_info *diagnostic;
1038 tree decl;
1040 if (global_dc->lock++)
1041 error_recursion (global_dc);
1043 if (diagnostic_count_diagnostic (global_dc, diagnostic->kind))
1045 diagnostic_report_current_function (global_dc);
1046 output_set_prefix
1047 (&global_dc->buffer, diagnostic_build_prefix (diagnostic));
1048 format_with_decl (&global_dc->buffer, &diagnostic->message, decl);
1049 output_flush (&global_dc->buffer);
1050 output_destroy_prefix (&global_dc->buffer);
1052 global_dc->lock--;
1055 /* Given a partial pathname as input, return another pathname that
1056 shares no directory elements with the pathname of __FILE__. This
1057 is used by fancy_abort() to print `Internal compiler error in expr.c'
1058 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1060 const char *
1061 trim_filename (name)
1062 const char *name;
1064 static const char this_file[] = __FILE__;
1065 const char *p = name, *q = this_file;
1067 /* First skip any "../" in each filename. This allows us to give a proper
1068 reference to a file in a subdirectory. */
1069 while (p[0] == '.' && p[1] == '.'
1070 && (p[2] == DIR_SEPARATOR
1071 #ifdef DIR_SEPARATOR_2
1072 || p[2] == DIR_SEPARATOR_2
1073 #endif
1075 p += 3;
1077 while (q[0] == '.' && q[1] == '.'
1078 && (q[2] == DIR_SEPARATOR
1079 #ifdef DIR_SEPARATOR_2
1080 || p[2] == DIR_SEPARATOR_2
1081 #endif
1083 q += 3;
1085 /* Now skip any parts the two filenames have in common. */
1086 while (*p == *q && *p != 0 && *q != 0)
1087 p++, q++;
1089 /* Now go backwards until the previous directory separator. */
1090 while (p > name && p[-1] != DIR_SEPARATOR
1091 #ifdef DIR_SEPARATOR_2
1092 && p[-1] != DIR_SEPARATOR_2
1093 #endif
1095 p--;
1097 return p;
1100 /* Standard error reporting routines in increasing order of severity.
1101 All of these take arguments like printf. */
1103 /* Text to be emitted verbatim to the error message stream; this
1104 produces no prefix and disables line-wrapping. Use rarely. */
1105 void
1106 verbatim VPARAMS ((const char *msgid, ...))
1108 text_info text;
1109 VA_OPEN (ap, msgid);
1110 VA_FIXEDARG (ap, const char *, msgid);
1112 text.format_spec = _(msgid);
1113 text.args_ptr = &ap;
1114 output_do_verbatim (&global_dc->buffer, &text);
1115 output_buffer_to_stream (&global_dc->buffer);
1116 VA_CLOSE (ap);
1119 /* An informative note. Use this for additional details on an error
1120 message. */
1121 void
1122 inform VPARAMS ((const char *msgid, ...))
1124 diagnostic_info diagnostic;
1126 VA_OPEN (ap, msgid);
1127 VA_FIXEDARG (ap, const char *, msgid);
1129 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, input_line,
1130 DK_NOTE);
1131 report_diagnostic (&diagnostic);
1132 VA_CLOSE (ap);
1135 /* A warning. Use this for code which is correct according to the
1136 relevant language specification but is likely to be buggy anyway. */
1137 void
1138 warning VPARAMS ((const char *msgid, ...))
1140 diagnostic_info diagnostic;
1142 VA_OPEN (ap, msgid);
1143 VA_FIXEDARG (ap, const char *, msgid);
1145 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, input_line,
1146 DK_WARNING);
1147 report_diagnostic (&diagnostic);
1148 VA_CLOSE (ap);
1151 /* A "pedantic" warning. Use this for code which triggers a
1152 diagnostic which is required by the relevant language
1153 specification, but which is considered unhelpful (i.e. there isn't
1154 anything *really* wrong with the construct in the language as she
1155 is spoke). It is a normal warning unless -pedantic-errors is
1156 applied, which turns it into an error. Note that pedwarn-s still
1157 happen if -pedantic is not given; you must write
1158 "if (pedantic) pedwarn (...)" to get a warning enabled only under
1159 -pedantic. All such warnings should, however, use pedwarn. */
1160 void
1161 pedwarn VPARAMS ((const char *msgid, ...))
1163 diagnostic_info diagnostic;
1164 VA_OPEN (ap, msgid);
1165 VA_FIXEDARG (ap, const char *, msgid);
1167 diagnostic_set_info (&diagnostic, _(msgid), &ap, input_filename, input_line,
1168 pedantic_error_kind ());
1169 report_diagnostic (&diagnostic);
1170 VA_CLOSE (ap);
1173 /* A hard error: the code is definitely ill-formed, and an object file
1174 will not be produced. */
1175 void
1176 error VPARAMS ((const char *msgid, ...))
1178 diagnostic_info diagnostic;
1180 VA_OPEN (ap, msgid);
1181 VA_FIXEDARG (ap, const char *, msgid);
1183 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, input_line,
1184 DK_ERROR);
1185 report_diagnostic (&diagnostic);
1186 VA_CLOSE (ap);
1189 /* "Sorry, not implemented." Use for a language feature which is
1190 required by the relevant specification but not implemented by GCC.
1191 An object file will not be produced. */
1192 void
1193 sorry VPARAMS ((const char *msgid, ...))
1195 diagnostic_info diagnostic;
1197 VA_OPEN (ap, msgid);
1198 VA_FIXEDARG (ap, const char *, msgid);
1200 ++sorrycount;
1201 diagnostic_set_info (&diagnostic, _(msgid), &ap,
1202 input_filename, input_line, DK_SORRY);
1204 output_set_prefix
1205 (&global_dc->buffer, diagnostic_build_prefix (&diagnostic));
1206 output_format (&global_dc->buffer, &diagnostic.message);
1207 output_flush (&global_dc->buffer);
1208 VA_CLOSE (ap);
1211 /* An error which is severe enough that we make no attempt to
1212 continue. Do not use this for internal consistency checks; that's
1213 internal_error. Use of this function should be rare. */
1214 void
1215 fatal_error VPARAMS ((const char *msgid, ...))
1217 diagnostic_info diagnostic;
1219 VA_OPEN (ap, msgid);
1220 VA_FIXEDARG (ap, const char *, msgid);
1222 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, input_line,
1223 DK_FATAL);
1224 report_diagnostic (&diagnostic);
1225 VA_CLOSE (ap);
1227 fnotice (stderr, "compilation terminated.\n");
1228 exit (FATAL_EXIT_CODE);
1231 /* An internal consistency check has failed. We make no attempt to
1232 continue. Note that unless there is debugging value to be had from
1233 a more specific message, or some other good reason, you should use
1234 abort () instead of calling this function directly. */
1235 void
1236 internal_error VPARAMS ((const char *msgid, ...))
1238 diagnostic_info diagnostic;
1240 VA_OPEN (ap, msgid);
1241 VA_FIXEDARG (ap, const char *, msgid);
1243 if (global_dc->lock)
1244 error_recursion (global_dc);
1246 #ifndef ENABLE_CHECKING
1247 if (errorcount > 0 || sorrycount > 0)
1249 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
1250 input_filename, input_line);
1251 exit (FATAL_EXIT_CODE);
1253 #endif
1255 if (global_dc->internal_error != 0)
1256 (*global_dc->internal_error) (_(msgid), &ap);
1258 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, input_line,
1259 DK_ICE);
1260 report_diagnostic (&diagnostic);
1261 VA_CLOSE (ap);
1263 fnotice (stderr,
1264 "Please submit a full bug report,\n\
1265 with preprocessed source if appropriate.\n\
1266 See %s for instructions.\n", bug_report_url);
1267 exit (FATAL_EXIT_CODE);
1270 /* Variants of some of the above, which make reference to a particular
1271 DECL node. These are deprecated. */
1273 void
1274 warning_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1276 diagnostic_info diagnostic;
1277 VA_OPEN (ap, msgid);
1278 VA_FIXEDARG (ap, tree, decl);
1279 VA_FIXEDARG (ap, const char *, msgid);
1281 diagnostic_set_info (&diagnostic, msgid, &ap,
1282 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1283 DK_WARNING);
1284 diagnostic_for_decl (&diagnostic, decl);
1285 VA_CLOSE (ap);
1288 void
1289 pedwarn_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1291 diagnostic_info diagnostic;
1292 VA_OPEN (ap, msgid);
1293 VA_FIXEDARG (ap, tree, decl);
1294 VA_FIXEDARG (ap, const char *, msgid);
1296 diagnostic_set_info (&diagnostic, _(msgid), &ap,
1297 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1298 pedantic_error_kind ());
1300 /* We don't want -pedantic-errors to cause the compilation to fail from
1301 "errors" in system header files. Sometimes fixincludes can't fix what's
1302 broken (eg: unsigned char bitfields - fixing it may change the alignment
1303 which will cause programs to mysteriously fail because the C library
1304 or kernel uses the original layout). There's no point in issuing a
1305 warning either, it's just unnecessary noise. */
1306 if (!DECL_IN_SYSTEM_HEADER (decl))
1307 diagnostic_for_decl (&diagnostic, decl);
1308 VA_CLOSE (ap);
1311 void
1312 error_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1314 diagnostic_info diagnostic;
1315 VA_OPEN (ap, msgid);
1316 VA_FIXEDARG (ap, tree, decl);
1317 VA_FIXEDARG (ap, const char *, msgid);
1319 diagnostic_set_info (&diagnostic, msgid, &ap,
1320 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1321 DK_ERROR);
1322 diagnostic_for_decl (&diagnostic, decl);
1323 VA_CLOSE (ap);
1326 /* Special case error functions. Most are implemented in terms of the
1327 above, or should be. */
1329 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1330 runs its second argument through gettext. */
1331 void
1332 fnotice VPARAMS ((FILE *file, const char *msgid, ...))
1334 VA_OPEN (ap, msgid);
1335 VA_FIXEDARG (ap, FILE *, file);
1336 VA_FIXEDARG (ap, const char *, msgid);
1338 vfprintf (file, _(msgid), ap);
1339 VA_CLOSE (ap);
1342 /* Warn about a use of an identifier which was marked deprecated. */
1343 void
1344 warn_deprecated_use (node)
1345 tree node;
1347 if (node == 0 || !warn_deprecated_decl)
1348 return;
1350 if (DECL_P (node))
1351 warning ("`%s' is deprecated (declared at %s:%d)",
1352 IDENTIFIER_POINTER (DECL_NAME (node)),
1353 DECL_SOURCE_FILE (node), DECL_SOURCE_LINE (node));
1354 else if (TYPE_P (node))
1356 const char *what = NULL;
1357 tree decl = TYPE_STUB_DECL (node);
1359 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
1360 what = IDENTIFIER_POINTER (TYPE_NAME (node));
1361 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
1362 && DECL_NAME (TYPE_NAME (node)))
1363 what = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node)));
1365 if (what)
1367 if (decl)
1368 warning ("`%s' is deprecated (declared at %s:%d)", what,
1369 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
1370 else
1371 warning ("`%s' is deprecated", what);
1373 else if (decl)
1374 warning ("type is deprecated (declared at %s:%d)",
1375 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
1376 else
1377 warning ("type is deprecated");
1381 /* Print a fatal I/O error message. Argument are like printf.
1382 Also include a system error message based on `errno'. */
1383 void
1384 fatal_io_error VPARAMS ((const char *msgid, ...))
1386 text_info text;
1387 VA_OPEN (ap, msgid);
1388 VA_FIXEDARG (ap, const char *, msgid);
1390 text.format_spec = _(msgid);
1391 text.args_ptr = &ap;
1392 output_printf (&global_dc->buffer, "%s: %s: ", progname, xstrerror (errno));
1393 output_format (&global_dc->buffer, &text);
1394 output_flush (&global_dc->buffer);
1395 VA_CLOSE (ap);
1396 exit (FATAL_EXIT_CODE);
1399 /* Inform the user that an error occurred while trying to report some
1400 other error. This indicates catastrophic internal inconsistencies,
1401 so give up now. But do try to flush out the previous error.
1402 This mustn't use internal_error, that will cause infinite recursion. */
1404 static void
1405 error_recursion (context)
1406 diagnostic_context *context;
1408 if (context->lock < 3)
1409 output_flush (&context->buffer);
1411 fnotice (stderr,
1412 "Internal compiler error: Error reporting routines re-entered.\n");
1413 fnotice (stderr,
1414 "Please submit a full bug report,\n\
1415 with preprocessed source if appropriate.\n\
1416 See %s for instructions.\n", bug_report_url);
1417 exit (FATAL_EXIT_CODE);
1420 /* Report an internal compiler error in a friendly manner. This is
1421 the function that gets called upon use of abort() in the source
1422 code generally, thanks to a special macro. */
1424 void
1425 fancy_abort (file, line, function)
1426 const char *file;
1427 int line;
1428 const char *function;
1430 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1433 /* Really call the system 'abort'. This has to go right at the end of
1434 this file, so that there are no functions after it that call abort
1435 and get the system abort instead of our macro. */
1436 #undef abort
1437 static void
1438 real_abort ()
1440 abort ();