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