Add x prefix to v850e case for handling --with-cpu=v850e.
[official-gcc.git] / gcc / diagnostic.c
blob3828d5e55881f88e0230403362ddbfaef6781591
1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
23 /* This file implements the language independent aspect of diagnostic
24 message module. */
26 #include "config.h"
27 #undef FLOAT /* This is for hpux. They should change hpux. */
28 #undef FFS /* Some systems define this in param.h. */
29 #include "system.h"
30 #include "tree.h"
31 #include "tm_p.h"
32 #include "flags.h"
33 #include "input.h"
34 #include "toplev.h"
35 #include "intl.h"
36 #include "diagnostic.h"
37 #include "langhooks.h"
38 #include "langhooks-def.h"
40 #define output_text_length(BUFFER) (BUFFER)->line_length
41 #define is_starting_newline(BUFFER) (output_text_length (BUFFER) == 0)
42 #define line_wrap_cutoff(BUFFER) (BUFFER)->state.maximum_length
43 #define prefix_was_emitted_for(BUFFER) (BUFFER)->state.emitted_prefix_p
45 /* Prototypes. */
46 static void output_flush PARAMS ((output_buffer *));
47 static void output_do_verbatim PARAMS ((output_buffer *, text_info *));
48 static void output_buffer_to_stream PARAMS ((output_buffer *));
49 static void output_format PARAMS ((output_buffer *, text_info *));
50 static void output_indent PARAMS ((output_buffer *));
52 static char *vbuild_message_string PARAMS ((const char *, va_list))
53 ATTRIBUTE_PRINTF (1, 0);
54 static char *build_message_string PARAMS ((const char *, ...))
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 *));
83 extern int rtl_dump_and_exit;
84 extern int warnings_are_errors;
86 /* A diagnostic_context surrogate for stderr. */
87 static diagnostic_context global_diagnostic_context;
88 diagnostic_context *global_dc = &global_diagnostic_context;
91 /* Subroutine of output_set_maximum_length. Set up BUFFER's
92 internal maximum characters per line. */
93 static void
94 set_real_maximum_length (buffer)
95 output_buffer *buffer;
97 /* If we're told not to wrap lines then do the obvious thing. In case
98 we'll emit prefix only once per diagnostic message, it is appropriate
99 not to increase unnecessarily the line-length cut-off. */
100 if (!output_is_line_wrapping (buffer)
101 || output_prefixing_rule (buffer) == DIAGNOSTICS_SHOW_PREFIX_ONCE
102 || output_prefixing_rule (buffer) == DIAGNOSTICS_SHOW_PREFIX_NEVER)
103 line_wrap_cutoff (buffer) = output_line_cutoff (buffer);
104 else
106 int prefix_length = buffer->state.prefix ?
107 strlen (buffer->state.prefix) : 0;
108 /* If the prefix is ridiculously too long, output at least
109 32 characters. */
110 if (output_line_cutoff (buffer) - prefix_length < 32)
111 line_wrap_cutoff (buffer) = output_line_cutoff (buffer) + 32;
112 else
113 line_wrap_cutoff (buffer) = output_line_cutoff (buffer);
117 /* Sets the number of maximum characters per line BUFFER can output
118 in line-wrapping mode. A LENGTH value 0 suppresses line-wrapping. */
119 void
120 output_set_maximum_length (buffer, length)
121 output_buffer *buffer;
122 int length;
124 output_line_cutoff (buffer) = length;
125 set_real_maximum_length (buffer);
128 /* Sets BUFFER's PREFIX. */
129 void
130 output_set_prefix (buffer, prefix)
131 output_buffer *buffer;
132 const char *prefix;
134 buffer->state.prefix = prefix;
135 set_real_maximum_length (buffer);
136 prefix_was_emitted_for (buffer) = false;
137 output_indentation (buffer) = 0;
140 /* Return a pointer to the last character emitted in the output
141 BUFFER area. A NULL pointer means no character available. */
142 const char *
143 output_last_position (buffer)
144 const output_buffer *buffer;
146 const char *p = NULL;
148 if (obstack_base (&buffer->obstack) != obstack_next_free (&buffer->obstack))
149 p = ((const char *) obstack_next_free (&buffer->obstack)) - 1;
150 return p;
153 /* Free BUFFER's prefix, a previously malloc'd string. */
154 void
155 output_destroy_prefix (buffer)
156 output_buffer *buffer;
158 if (buffer->state.prefix != NULL)
160 free ((char *) buffer->state.prefix);
161 buffer->state.prefix = NULL;
165 /* Zero out any text output so far in BUFFER. */
166 void
167 output_clear_message_text (buffer)
168 output_buffer *buffer;
170 obstack_free (&buffer->obstack, obstack_base (&buffer->obstack));
171 output_text_length (buffer) = 0;
174 /* Zero out any formatting data used so far by BUFFER. */
175 static void
176 output_clear_data (buffer)
177 output_buffer *buffer;
179 prefix_was_emitted_for (buffer) = false;
180 output_indentation (buffer) = 0;
183 /* Construct an output BUFFER with PREFIX and of MAXIMUM_LENGTH
184 characters per line. */
185 void
186 init_output_buffer (buffer, prefix, maximum_length)
187 output_buffer *buffer;
188 const char *prefix;
189 int maximum_length;
191 memset (buffer, 0, sizeof (output_buffer));
192 obstack_init (&buffer->obstack);
193 output_buffer_attached_stream (buffer) = stderr;
194 output_line_cutoff (buffer) = maximum_length;
195 output_prefixing_rule (buffer) = diagnostic_prefixing_rule (global_dc);
196 output_set_prefix (buffer, prefix);
197 output_text_length (buffer) = 0;
198 output_clear_data (buffer);
201 /* Reinitialize BUFFER. */
202 void
203 output_clear (buffer)
204 output_buffer *buffer;
206 output_clear_message_text (buffer);
207 output_clear_data (buffer);
210 /* Finishes constructing a NULL-terminated character string representing
211 the BUFFERed message. */
212 const char *
213 output_finalize_message (buffer)
214 output_buffer *buffer;
216 obstack_1grow (&buffer->obstack, '\0');
217 return output_message_text (buffer);
220 /* Return the amount of characters BUFFER can accept to
221 make a full line. */
223 output_space_left (buffer)
224 const output_buffer *buffer;
226 return line_wrap_cutoff (buffer) - output_text_length (buffer);
229 /* Write out BUFFER's prefix. */
230 void
231 output_emit_prefix (buffer)
232 output_buffer *buffer;
234 if (buffer->state.prefix != NULL)
236 switch (output_prefixing_rule (buffer))
238 default:
239 case DIAGNOSTICS_SHOW_PREFIX_NEVER:
240 break;
242 case DIAGNOSTICS_SHOW_PREFIX_ONCE:
243 if (prefix_was_emitted_for (buffer))
245 output_indent (buffer);
246 break;
248 output_indentation (buffer) += 3;
249 /* Fall through. */
251 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
253 int prefix_length = strlen (buffer->state.prefix);
254 output_append_r (buffer, buffer->state.prefix, prefix_length);
255 prefix_was_emitted_for (buffer) = true;
257 break;
262 /* Have BUFFER start a new line. */
263 void
264 output_add_newline (buffer)
265 output_buffer *buffer;
267 obstack_1grow (&buffer->obstack, '\n');
268 output_text_length (buffer) = 0;
271 /* Appends a character to BUFFER. */
272 void
273 output_add_character (buffer, c)
274 output_buffer *buffer;
275 int c;
277 if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
278 output_add_newline (buffer);
279 obstack_1grow (&buffer->obstack, c);
280 ++output_text_length (buffer);
283 /* Adds a space to BUFFER. */
284 void
285 output_add_space (buffer)
286 output_buffer *buffer;
288 if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
290 output_add_newline (buffer);
291 return;
293 obstack_1grow (&buffer->obstack, ' ');
294 ++output_text_length (buffer);
297 /* These functions format an INTEGER into BUFFER as suggested by their
298 names. */
299 void
300 output_decimal (buffer, i)
301 output_buffer *buffer;
302 int i;
304 output_formatted_scalar (buffer, "%d", i);
307 static void
308 output_long_decimal (buffer, i)
309 output_buffer *buffer;
310 long int i;
312 output_formatted_scalar (buffer, "%ld", i);
315 static void
316 output_unsigned_decimal (buffer, i)
317 output_buffer *buffer;
318 unsigned int i;
320 output_formatted_scalar (buffer, "%u", i);
323 static void
324 output_long_unsigned_decimal (buffer, i)
325 output_buffer *buffer;
326 long unsigned int i;
328 output_formatted_scalar (buffer, "%lu", i);
331 static void
332 output_octal (buffer, i)
333 output_buffer *buffer;
334 unsigned int i;
336 output_formatted_scalar (buffer, "%o", i);
339 static void
340 output_long_octal (buffer, i)
341 output_buffer *buffer;
342 unsigned long int i;
344 output_formatted_scalar (buffer, "%lo", i);
347 static void
348 output_hexadecimal (buffer, i)
349 output_buffer *buffer;
350 unsigned int i;
352 output_formatted_scalar (buffer, "%x", i);
355 static void
356 output_long_hexadecimal (buffer, i)
357 output_buffer *buffer;
358 unsigned long int i;
360 output_formatted_scalar (buffer, "%lx", i);
363 /* Append to BUFFER a string specified by its STARTING character
364 and LENGTH. */
365 static void
366 output_append_r (buffer, start, length)
367 output_buffer *buffer;
368 const char *start;
369 int length;
371 obstack_grow (&buffer->obstack, start, length);
372 output_text_length (buffer) += length;
375 /* Append a string deliminated by START and END to BUFFER. No wrapping is
376 done. However, if beginning a new line then emit BUFFER->state.prefix
377 and skip any leading whitespace if appropriate. The caller must ensure
378 that it is safe to do so. */
379 void
380 output_append (buffer, start, end)
381 output_buffer *buffer;
382 const char *start;
383 const char *end;
385 /* Emit prefix and skip whitespace if we're starting a new line. */
386 if (is_starting_newline (buffer))
388 output_emit_prefix (buffer);
389 if (output_is_line_wrapping (buffer))
390 while (start != end && *start == ' ')
391 ++start;
393 output_append_r (buffer, start, end - start);
396 static void
397 output_indent (buffer)
398 output_buffer *buffer;
400 int n = output_indentation (buffer);
401 int i;
403 for (i = 0; i < n; ++i)
404 output_add_character (buffer, ' ');
407 /* Wrap a text delimited by START and END into BUFFER. */
408 static void
409 wrap_text (buffer, start, end)
410 output_buffer *buffer;
411 const char *start;
412 const char *end;
414 bool is_wrapping = output_is_line_wrapping (buffer);
416 while (start != end)
418 /* Dump anything bordered by whitespaces. */
420 const char *p = start;
421 while (p != end && *p != ' ' && *p != '\n')
422 ++p;
423 if (is_wrapping && p - start >= output_space_left (buffer))
424 output_add_newline (buffer);
425 output_append (buffer, start, p);
426 start = p;
429 if (start != end && *start == ' ')
431 output_add_space (buffer);
432 ++start;
434 if (start != end && *start == '\n')
436 output_add_newline (buffer);
437 ++start;
442 /* Same as wrap_text but wrap text only when in line-wrapping mode. */
443 static void
444 maybe_wrap_text (buffer, start, end)
445 output_buffer *buffer;
446 const char *start;
447 const char *end;
449 if (output_is_line_wrapping (buffer))
450 wrap_text (buffer, start, end);
451 else
452 output_append (buffer, start, end);
456 /* Append a STRING to BUFFER; the STRING might be line-wrapped if in
457 appropriate mode. */
458 void
459 output_add_string (buffer, str)
460 output_buffer *buffer;
461 const char *str;
463 maybe_wrap_text (buffer, str, str + (str ? strlen (str) : 0));
466 /* Append an identifier ID to BUFFER. */
467 void
468 output_add_identifier (buffer, id)
469 output_buffer *buffer;
470 tree id;
472 output_append (buffer, IDENTIFIER_POINTER (id),
473 IDENTIFIER_POINTER (id) + IDENTIFIER_LENGTH (id));
476 /* Flush the content of BUFFER onto the attached stream,
477 and reinitialize. */
479 static void
480 output_buffer_to_stream (buffer)
481 output_buffer *buffer;
483 const char *text = output_finalize_message (buffer);
484 fputs (text, output_buffer_attached_stream (buffer));
485 output_clear_message_text (buffer);
488 /* Format a message pointed to by TEXT. The following format specifiers are
489 recognized as being language independent:
490 %d, %i: (signed) integer in base ten.
491 %u: unsigned integer in base ten.
492 %o: unsigned integer in base eight.
493 %x: unsigned integer in base sixteen.
494 %ld, %li, %lo, %lu, %lx: long versions of the above.
495 %c: character.
496 %s: string.
497 %%: `%'.
498 %*.s: a substring the length of which is specified by an integer.
499 %H: location_t. */
500 static void
501 output_format (buffer, text)
502 output_buffer *buffer;
503 text_info *text;
505 for (; *text->format_spec; ++text->format_spec)
507 bool long_integer = 0;
509 /* Ignore text. */
511 const char *p = text->format_spec;
512 while (*p && *p != '%')
513 ++p;
514 wrap_text (buffer, text->format_spec, p);
515 text->format_spec = p;
518 if (*text->format_spec == '\0')
519 break;
521 /* We got a '%'. Let's see what happens. Record whether we're
522 parsing a long integer format specifier. */
523 if (*++text->format_spec == 'l')
525 long_integer = true;
526 ++text->format_spec;
529 /* Handle %c, %d, %i, %ld, %li, %lo, %lu, %lx, %o, %s, %u,
530 %x, %.*s; %%. And nothing else. Front-ends should install
531 printers to grok language specific format specifiers. */
532 switch (*text->format_spec)
534 case 'c':
535 output_add_character (buffer, va_arg (*text->args_ptr, int));
536 break;
538 case 'd':
539 case 'i':
540 if (long_integer)
541 output_long_decimal (buffer, va_arg (*text->args_ptr, long int));
542 else
543 output_decimal (buffer, va_arg (*text->args_ptr, int));
544 break;
546 case 'o':
547 if (long_integer)
548 output_long_octal (buffer,
549 va_arg (*text->args_ptr, unsigned long int));
550 else
551 output_octal (buffer, va_arg (*text->args_ptr, unsigned int));
552 break;
554 case 's':
555 output_add_string (buffer, va_arg (*text->args_ptr, const char *));
556 break;
558 case 'u':
559 if (long_integer)
560 output_long_unsigned_decimal
561 (buffer, va_arg (*text->args_ptr, long unsigned int));
562 else
563 output_unsigned_decimal
564 (buffer, va_arg (*text->args_ptr, unsigned int));
565 break;
567 case 'x':
568 if (long_integer)
569 output_long_hexadecimal
570 (buffer, va_arg (*text->args_ptr, unsigned long int));
571 else
572 output_hexadecimal
573 (buffer, va_arg (*text->args_ptr, unsigned int));
574 break;
576 case '%':
577 output_add_character (buffer, '%');
578 break;
580 case 'H':
582 const location_t *locus = va_arg (*text->args_ptr, location_t *);
583 output_add_string (buffer, "file '");
584 output_add_string (buffer, locus->file);
585 output_add_string (buffer, "', line ");
586 output_decimal (buffer, locus->line);
588 break;
590 case '.':
592 int n;
593 const char *s;
594 /* We handle no precision specifier but `%.*s'. */
595 if (*++text->format_spec != '*')
596 abort ();
597 else if (*++text->format_spec != 's')
598 abort ();
599 n = va_arg (*text->args_ptr, int);
600 s = va_arg (*text->args_ptr, const char *);
601 output_append (buffer, s, s + n);
603 break;
605 default:
606 if (!buffer->format_decoder
607 || !(*buffer->format_decoder) (buffer, text))
609 /* Hmmm. The front-end failed to install a format translator
610 but called us with an unrecognized format. Sorry. */
611 abort ();
617 static char *
618 vbuild_message_string (msg, ap)
619 const char *msg;
620 va_list ap;
622 char *str;
624 vasprintf (&str, msg, ap);
625 return str;
628 /* Return a malloc'd string containing MSG formatted a la
629 printf. The caller is responsible for freeing the memory. */
630 static char *
631 build_message_string VPARAMS ((const char *msg, ...))
633 char *str;
635 VA_OPEN (ap, msg);
636 VA_FIXEDARG (ap, const char *, msg);
638 str = vbuild_message_string (msg, ap);
640 VA_CLOSE (ap);
642 return str;
645 /* Same as diagnsotic_build_prefix, but only the source FILE is given. */
646 char *
647 file_name_as_prefix (f)
648 const char *f;
650 return build_message_string ("%s: ", f);
653 /* Format a message into BUFFER a la printf. */
654 void
655 output_printf VPARAMS ((struct output_buffer *buffer, const char *msgid, ...))
657 text_info text;
658 VA_OPEN (ap, msgid);
659 VA_FIXEDARG (ap, output_buffer *, buffer);
660 VA_FIXEDARG (ap, const char *, msgid);
662 text.args_ptr = &ap;
663 text.format_spec = _(msgid);
664 output_format (buffer, &text);
665 VA_CLOSE (ap);
668 /* Print a message relevant to the given DECL. */
669 static void
670 format_with_decl (buffer, text, decl)
671 output_buffer *buffer;
672 text_info *text;
673 tree decl;
675 const char *p;
677 /* Do magic to get around lack of varargs support for insertion
678 of arguments into existing list. We know that the decl is first;
679 we ass_u_me that it will be printed with "%s". */
680 for (p = text->format_spec; *p; ++p)
682 if (*p == '%')
684 if (*(p + 1) == '%')
685 ++p;
686 else if (*(p + 1) != 's')
687 abort ();
688 else
689 break;
693 /* Print the left-hand substring. */
694 maybe_wrap_text (buffer, text->format_spec, p);
696 if (*p == '%') /* Print the name. */
698 const char *const n = (DECL_NAME (decl)
699 ? (*lang_hooks.decl_printable_name) (decl, 2)
700 : _("((anonymous))"));
701 output_add_string (buffer, n);
702 while (*p)
704 ++p;
705 if (ISALPHA (*(p - 1) & 0xFF))
706 break;
710 if (*p) /* Print the rest of the message. */
712 text->format_spec = p;
713 output_format (buffer, text);
717 /* Flush the content of BUFFER onto the attached stream. */
718 static void
719 output_flush (buffer)
720 output_buffer *buffer;
722 output_buffer_to_stream (buffer);
723 output_clear_data (buffer);
724 fputc ('\n', output_buffer_attached_stream (buffer));
725 fflush (output_buffer_attached_stream (buffer));
728 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
729 settings needed by BUFFER for a verbatim formatting. */
730 static void
731 output_do_verbatim (buffer, text)
732 output_buffer *buffer;
733 text_info *text;
735 diagnostic_prefixing_rule_t rule = output_prefixing_rule (buffer);
736 int line_cutoff = output_line_cutoff (buffer);
738 /* Set verbatim mode. */
739 output_prefixing_rule (buffer) = DIAGNOSTICS_SHOW_PREFIX_NEVER;
740 output_line_cutoff (buffer) = 0;
741 /* Do the actual formatting. */
742 output_format (buffer, text);
743 /* Restore previous settings. */
744 output_prefixing_rule (buffer) = rule;
745 output_line_cutoff (buffer) = line_cutoff;
748 /* Output MESSAGE verbatim into BUFFER. */
749 void
750 output_verbatim VPARAMS ((output_buffer *buffer, const char *msgid, ...))
752 text_info text;
753 VA_OPEN (ap, msgid);
754 VA_FIXEDARG (ap, output_buffer *, buffer);
755 VA_FIXEDARG (ap, const char *, msgid);
757 text.format_spec = msgid;
758 text.args_ptr = &ap;
759 output_do_verbatim (buffer, &text);
760 VA_CLOSE (ap);
764 /* Initialize the diagnostic message outputting machinery. */
765 void
766 diagnostic_initialize (context)
767 diagnostic_context *context;
769 memset (context, 0, sizeof *context);
770 obstack_init (&context->buffer.obstack);
772 /* By default, diagnostics are sent to stderr. */
773 output_buffer_attached_stream (&context->buffer) = stderr;
775 /* By default, we emit prefixes once per message. */
776 diagnostic_prefixing_rule (context) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
778 diagnostic_starter (context) = default_diagnostic_starter;
779 diagnostic_finalizer (context) = default_diagnostic_finalizer;
780 context->warnings_are_errors_message = warnings_are_errors;
783 /* Returns true if the next format specifier in TEXT is a format specifier
784 for a location_t. If so, update the object pointed by LOCUS to reflect
785 the specified location in *TEXT->args_ptr. */
786 static bool
787 text_specifies_location (text, locus)
788 text_info *text;
789 location_t *locus;
791 const char *p;
792 /* Skip any leading text. */
793 for (p = text->format_spec; *p && *p != '%'; ++p)
796 /* Extract the location information if any. */
797 if (*p == '%' && *++p == 'H')
799 *locus = *va_arg (*text->args_ptr, location_t *);
800 text->format_spec = p + 1;
801 return true;
804 return false;
807 void
808 diagnostic_set_info (diagnostic, msgid, args, file, line, kind)
809 diagnostic_info *diagnostic;
810 const char *msgid;
811 va_list *args;
812 const char *file;
813 int line;
814 diagnostic_t kind;
816 diagnostic->message.format_spec = msgid;
817 diagnostic->message.args_ptr = args;
818 /* If the diagnostic message doesn't specify a loccation,
819 use FILE and LINE. */
820 if (!text_specifies_location (&diagnostic->message, &diagnostic->location))
822 diagnostic->location.file = file;
823 diagnostic->location.line = line;
825 diagnostic->kind = kind;
828 /* Return a malloc'd string describing a location. The caller is
829 responsible for freeing the memory. */
830 char *
831 diagnostic_build_prefix (diagnostic)
832 diagnostic_info *diagnostic;
834 static const char *const diagnostic_kind_text[] = {
835 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
836 #include "diagnostic.def"
837 #undef DEFINE_DIAGNOSTIC_KIND
838 "must-not-happen"
840 if (diagnostic->kind >= DK_LAST_DIAGNOSTIC_KIND)
841 abort();
843 return diagnostic->location.file
844 ? build_message_string ("%s:%d: %s",
845 diagnostic->location.file,
846 diagnostic->location.line,
847 _(diagnostic_kind_text[diagnostic->kind]))
848 : build_message_string ("%s: %s", progname,
849 _(diagnostic_kind_text[diagnostic->kind]));
852 /* Report a diagnostic MESSAGE at the declaration DECL.
853 MSG is a format string which uses %s to substitute the declaration
854 name; subsequent substitutions are a la output_format. */
855 static void
856 diagnostic_for_decl (diagnostic, decl)
857 diagnostic_info *diagnostic;
858 tree decl;
860 if (global_dc->lock++)
861 error_recursion (global_dc);
863 if (diagnostic_count_diagnostic (global_dc, diagnostic->kind))
865 diagnostic_report_current_function (global_dc);
866 output_set_prefix
867 (&global_dc->buffer, diagnostic_build_prefix (diagnostic));
868 format_with_decl (&global_dc->buffer, &diagnostic->message, decl);
869 output_flush (&global_dc->buffer);
870 output_destroy_prefix (&global_dc->buffer);
872 global_dc->lock--;
875 void
876 diagnostic_flush_buffer (context)
877 diagnostic_context *context;
879 output_buffer_to_stream (&context->buffer);
880 fflush (output_buffer_attached_stream (&context->buffer));
883 /* Count a diagnostic. Return true if the message should be printed. */
884 bool
885 diagnostic_count_diagnostic (context, kind)
886 diagnostic_context *context;
887 diagnostic_t kind;
889 switch (kind)
891 default:
892 abort();
893 break;
895 case DK_FATAL: case DK_ICE: case DK_SORRY:
896 case DK_ANACHRONISM: case DK_NOTE:
897 ++diagnostic_kind_count (context, kind);
898 break;
900 case DK_WARNING:
901 if (!diagnostic_report_warnings_p ())
902 return false;
903 else if (!warnings_are_errors)
905 ++diagnostic_kind_count (context, DK_WARNING);
906 break;
908 /* else fall through. */
910 case DK_ERROR:
911 if (kind == DK_WARNING && context->warnings_are_errors_message)
913 output_verbatim (&context->buffer,
914 "%s: warnings being treated as errors\n", progname);
915 context->warnings_are_errors_message = false;
917 ++diagnostic_kind_count (context, DK_ERROR);
918 break;
921 return true;
924 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
925 runs its second argument through gettext. */
926 void
927 fnotice VPARAMS ((FILE *file, const char *msgid, ...))
929 VA_OPEN (ap, msgid);
930 VA_FIXEDARG (ap, FILE *, file);
931 VA_FIXEDARG (ap, const char *, msgid);
933 vfprintf (file, _(msgid), ap);
934 VA_CLOSE (ap);
938 /* Print a fatal I/O error message. Argument are like printf.
939 Also include a system error message based on `errno'. */
940 void
941 fatal_io_error VPARAMS ((const char *msgid, ...))
943 text_info text;
944 VA_OPEN (ap, msgid);
945 VA_FIXEDARG (ap, const char *, msgid);
947 text.format_spec = _(msgid);
948 text.args_ptr = &ap;
949 output_printf (&global_dc->buffer, "%s: %s: ", progname, xstrerror (errno));
950 output_format (&global_dc->buffer, &text);
951 output_flush (&global_dc->buffer);
952 VA_CLOSE (ap);
953 exit (FATAL_EXIT_CODE);
956 /* Issue a pedantic warning MSGID. */
957 void
958 pedwarn VPARAMS ((const char *msgid, ...))
960 diagnostic_info diagnostic;
961 VA_OPEN (ap, msgid);
962 VA_FIXEDARG (ap, const char *, msgid);
964 diagnostic_set_info (&diagnostic, _(msgid), &ap, input_filename, lineno,
965 pedantic_error_kind ());
966 report_diagnostic (&diagnostic);
967 VA_CLOSE (ap);
970 /* Issue a pedantic warning about DECL. */
971 void
972 pedwarn_with_decl VPARAMS ((tree decl, const char *msgid, ...))
974 diagnostic_info diagnostic;
975 VA_OPEN (ap, msgid);
976 VA_FIXEDARG (ap, tree, decl);
977 VA_FIXEDARG (ap, const char *, msgid);
979 diagnostic_set_info (&diagnostic, _(msgid), &ap,
980 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
981 pedantic_error_kind ());
983 /* We don't want -pedantic-errors to cause the compilation to fail from
984 "errors" in system header files. Sometimes fixincludes can't fix what's
985 broken (eg: unsigned char bitfields - fixing it may change the alignment
986 which will cause programs to mysteriously fail because the C library
987 or kernel uses the original layout). There's no point in issuing a
988 warning either, it's just unnecessary noise. */
989 if (!DECL_IN_SYSTEM_HEADER (decl))
990 diagnostic_for_decl (&diagnostic, decl);
991 VA_CLOSE (ap);
994 /* Same as above but within the context FILE and LINE. */
995 void
996 pedwarn_with_file_and_line VPARAMS ((const char *file, int line,
997 const char *msgid, ...))
999 diagnostic_info diagnostic;
1000 VA_OPEN (ap, msgid);
1001 VA_FIXEDARG (ap, const char *, file);
1002 VA_FIXEDARG (ap, int, line);
1003 VA_FIXEDARG (ap, const char *, msgid);
1005 diagnostic_set_info (&diagnostic, _(msgid), &ap, file, line,
1006 pedantic_error_kind ());
1007 report_diagnostic (&diagnostic);
1008 VA_CLOSE (ap);
1011 /* Just apologize with MSGID. */
1012 void
1013 sorry VPARAMS ((const char *msgid, ...))
1015 diagnostic_info diagnostic;
1017 VA_OPEN (ap, msgid);
1018 VA_FIXEDARG (ap, const char *, msgid);
1020 ++sorrycount;
1021 diagnostic_set_info (&diagnostic, _(msgid), &ap,
1022 input_filename, lineno, DK_SORRY);
1024 output_set_prefix
1025 (&global_dc->buffer, diagnostic_build_prefix (&diagnostic));
1026 output_printf (&global_dc->buffer, "sorry, not implemented: ");
1027 output_format (&global_dc->buffer, &diagnostic.message);
1028 output_flush (&global_dc->buffer);
1029 VA_CLOSE (ap);
1032 /* Called when the start of a function definition is parsed,
1033 this function prints on stderr the name of the function. */
1034 void
1035 announce_function (decl)
1036 tree decl;
1038 if (!quiet_flag)
1040 if (rtl_dump_and_exit)
1041 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl)));
1042 else
1043 verbatim (" %s", (*lang_hooks.decl_printable_name) (decl, 2));
1044 fflush (stderr);
1045 output_needs_newline (&global_dc->buffer) = true;
1046 diagnostic_set_last_function (global_dc);
1050 /* The default function to print out name of current function that caused
1051 an error. */
1052 void
1053 lhd_print_error_function (context, file)
1054 diagnostic_context *context;
1055 const char *file;
1057 if (diagnostic_last_function_changed (context))
1059 const char *old_prefix = output_prefix (&context->buffer);
1060 char *new_prefix = file ? build_message_string ("%s: ", file) : NULL;
1062 output_set_prefix (&context->buffer, new_prefix);
1064 if (current_function_decl == NULL)
1065 output_add_string (&context->buffer, _("At top level:"));
1066 else
1068 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
1069 output_printf
1070 (&context->buffer, "In member function `%s':",
1071 (*lang_hooks.decl_printable_name) (current_function_decl, 2));
1072 else
1073 output_printf
1074 (&context->buffer, "In function `%s':",
1075 (*lang_hooks.decl_printable_name) (current_function_decl, 2));
1077 output_add_newline (&context->buffer);
1079 diagnostic_set_last_function (context);
1080 output_buffer_to_stream (&context->buffer);
1081 context->buffer.state.prefix = old_prefix;
1082 free ((char*) new_prefix);
1086 /* Prints out, if necessary, the name of the current function
1087 that caused an error. Called from all error and warning functions.
1088 We ignore the FILE parameter, as it cannot be relied upon. */
1090 void
1091 diagnostic_report_current_function (context)
1092 diagnostic_context *context;
1094 diagnostic_report_current_module (context);
1095 (*lang_hooks.print_error_function) (context, input_filename);
1098 void
1099 error_with_file_and_line VPARAMS ((const char *file, int line,
1100 const char *msgid, ...))
1102 diagnostic_info diagnostic;
1104 VA_OPEN (ap, msgid);
1105 VA_FIXEDARG (ap, const char *, file);
1106 VA_FIXEDARG (ap, int, line);
1107 VA_FIXEDARG (ap, const char *, msgid);
1109 diagnostic_set_info (&diagnostic, msgid, &ap, file, line, DK_ERROR);
1110 report_diagnostic (&diagnostic);
1111 VA_CLOSE (ap);
1114 void
1115 error_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1117 diagnostic_info diagnostic;
1118 VA_OPEN (ap, msgid);
1119 VA_FIXEDARG (ap, tree, decl);
1120 VA_FIXEDARG (ap, const char *, msgid);
1122 diagnostic_set_info (&diagnostic, msgid, &ap,
1123 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1124 DK_ERROR);
1125 diagnostic_for_decl (&diagnostic, decl);
1126 VA_CLOSE (ap);
1130 /* Report an error message. The arguments are like that of printf. */
1132 void
1133 error VPARAMS ((const char *msgid, ...))
1135 diagnostic_info diagnostic;
1137 VA_OPEN (ap, msgid);
1138 VA_FIXEDARG (ap, const char *, msgid);
1140 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1141 DK_ERROR);
1142 report_diagnostic (&diagnostic);
1143 VA_CLOSE (ap);
1146 /* Likewise, except that the compilation is terminated after printing the
1147 error message. */
1149 void
1150 fatal_error VPARAMS ((const char *msgid, ...))
1152 diagnostic_info diagnostic;
1154 VA_OPEN (ap, msgid);
1155 VA_FIXEDARG (ap, const char *, msgid);
1157 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1158 DK_FATAL);
1159 report_diagnostic (&diagnostic);
1160 VA_CLOSE (ap);
1162 fnotice (stderr, "compilation terminated.\n");
1163 exit (FATAL_EXIT_CODE);
1166 void
1167 internal_error VPARAMS ((const char *msgid, ...))
1169 diagnostic_info diagnostic;
1171 VA_OPEN (ap, msgid);
1172 VA_FIXEDARG (ap, const char *, msgid);
1174 if (global_dc->lock)
1175 error_recursion (global_dc);
1177 #ifndef ENABLE_CHECKING
1178 if (errorcount > 0 || sorrycount > 0)
1180 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
1181 input_filename, lineno);
1182 exit (FATAL_EXIT_CODE);
1184 #endif
1186 if (global_dc->internal_error != 0)
1187 (*global_dc->internal_error) (_(msgid), &ap);
1189 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1190 DK_ICE);
1191 report_diagnostic (&diagnostic);
1192 VA_CLOSE (ap);
1194 fnotice (stderr,
1195 "Please submit a full bug report,\n\
1196 with preprocessed source if appropriate.\n\
1197 See %s for instructions.\n", GCCBUGURL);
1198 exit (FATAL_EXIT_CODE);
1201 void
1202 warning_with_file_and_line VPARAMS ((const char *file, int line,
1203 const char *msgid, ...))
1205 diagnostic_info diagnostic;
1207 VA_OPEN (ap, msgid);
1208 VA_FIXEDARG (ap, const char *, file);
1209 VA_FIXEDARG (ap, int, line);
1210 VA_FIXEDARG (ap, const char *, msgid);
1212 diagnostic_set_info (&diagnostic, msgid, &ap, file, line, DK_WARNING);
1213 report_diagnostic (&diagnostic);
1214 VA_CLOSE (ap);
1217 void
1218 warning_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1220 diagnostic_info diagnostic;
1221 VA_OPEN (ap, msgid);
1222 VA_FIXEDARG (ap, tree, decl);
1223 VA_FIXEDARG (ap, const char *, msgid);
1225 diagnostic_set_info (&diagnostic, msgid, &ap,
1226 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1227 DK_WARNING);
1228 diagnostic_for_decl (&diagnostic, decl);
1229 VA_CLOSE (ap);
1232 void
1233 warning VPARAMS ((const char *msgid, ...))
1235 diagnostic_info diagnostic;
1237 VA_OPEN (ap, msgid);
1238 VA_FIXEDARG (ap, const char *, msgid);
1240 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1241 DK_WARNING);
1242 report_diagnostic (&diagnostic);
1243 VA_CLOSE (ap);
1247 /* Same as above but use diagnostic_buffer. */
1249 void
1250 verbatim VPARAMS ((const char *msgid, ...))
1252 text_info text;
1253 VA_OPEN (ap, msgid);
1254 VA_FIXEDARG (ap, const char *, msgid);
1256 text.format_spec = _(msgid);
1257 text.args_ptr = &ap;
1258 output_do_verbatim (&global_dc->buffer, &text);
1259 output_buffer_to_stream (&global_dc->buffer);
1260 VA_CLOSE (ap);
1263 /* Report a diagnostic message (an error or a warning) as specified by
1264 DC. This function is *the* subroutine in terms of which front-ends
1265 should implement their specific diagnostic handling modules. The
1266 front-end independent format specifiers are exactly those described
1267 in the documentation of output_format. */
1269 void
1270 diagnostic_report_diagnostic (context, diagnostic)
1271 diagnostic_context *context;
1272 diagnostic_info *diagnostic;
1274 if (context->lock++)
1275 error_recursion (context);
1277 if (diagnostic_count_diagnostic (context, diagnostic->kind))
1279 (*diagnostic_starter (context)) (context, diagnostic);
1280 output_format (&context->buffer, &diagnostic->message);
1281 (*diagnostic_finalizer (context)) (context, diagnostic);
1282 output_flush (&context->buffer);
1285 --context->lock;
1288 /* Inform the user that an error occurred while trying to report some
1289 other error. This indicates catastrophic internal inconsistencies,
1290 so give up now. But do try to flush out the previous error.
1291 This mustn't use internal_error, that will cause infinite recursion. */
1293 static void
1294 error_recursion (context)
1295 diagnostic_context *context;
1297 if (context->lock < 3)
1298 output_flush (&context->buffer);
1300 fnotice (stderr,
1301 "Internal compiler error: Error reporting routines re-entered.\n");
1302 fnotice (stderr,
1303 "Please submit a full bug report,\n\
1304 with preprocessed source if appropriate.\n\
1305 See %s for instructions.\n", GCCBUGURL);
1306 exit (FATAL_EXIT_CODE);
1309 /* Given a partial pathname as input, return another pathname that
1310 shares no directory elements with the pathname of __FILE__. This
1311 is used by fancy_abort() to print `Internal compiler error in expr.c'
1312 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1314 const char *
1315 trim_filename (name)
1316 const char *name;
1318 static const char this_file[] = __FILE__;
1319 const char *p = name, *q = this_file;
1321 /* First skip any "../" in each filename. This allows us to give a proper
1322 reference to a file in a subdirectory. */
1323 while (p[0] == '.' && p[1] == '.'
1324 && (p[2] == DIR_SEPARATOR
1325 #ifdef DIR_SEPARATOR_2
1326 || p[2] == DIR_SEPARATOR_2
1327 #endif
1329 p += 3;
1331 while (q[0] == '.' && q[1] == '.'
1332 && (q[2] == DIR_SEPARATOR
1333 #ifdef DIR_SEPARATOR_2
1334 || p[2] == DIR_SEPARATOR_2
1335 #endif
1337 q += 3;
1339 /* Now skip any parts the two filenames have in common. */
1340 while (*p == *q && *p != 0 && *q != 0)
1341 p++, q++;
1343 /* Now go backwards until the previous directory separator. */
1344 while (p > name && p[-1] != DIR_SEPARATOR
1345 #ifdef DIR_SEPARATOR_2
1346 && p[-1] != DIR_SEPARATOR_2
1347 #endif
1349 p--;
1351 return p;
1354 /* Report an internal compiler error in a friendly manner and without
1355 dumping core. */
1357 void
1358 fancy_abort (file, line, function)
1359 const char *file;
1360 int line;
1361 const char *function;
1363 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1366 void
1367 diagnostic_report_current_module (context)
1368 diagnostic_context *context;
1370 struct file_stack *p;
1372 if (output_needs_newline (&context->buffer))
1374 output_add_newline (&context->buffer);
1375 output_needs_newline (&context->buffer) = false;
1378 if (input_file_stack && input_file_stack->next != 0
1379 && diagnostic_last_module_changed (context))
1381 for (p = input_file_stack->next; p; p = p->next)
1382 if (p == input_file_stack->next)
1383 output_verbatim (&context->buffer,
1384 "In file included from %s:%d", p->name, p->line);
1385 else
1386 output_verbatim (&context->buffer,
1387 ",\n from %s:%d", p->name, p->line);
1388 output_verbatim (&context->buffer, ":\n");
1389 diagnostic_set_last_module (context);
1393 static void
1394 default_diagnostic_starter (context, diagnostic)
1395 diagnostic_context *context;
1396 diagnostic_info *diagnostic;
1398 diagnostic_report_current_function (context);
1399 output_set_prefix (&context->buffer, diagnostic_build_prefix (diagnostic));
1402 static void
1403 default_diagnostic_finalizer (context, diagnostic)
1404 diagnostic_context *context;
1405 diagnostic_info *diagnostic __attribute__((unused));
1407 output_destroy_prefix (&context->buffer);
1410 void
1411 inform VPARAMS ((const char *msgid, ...))
1413 diagnostic_info diagnostic;
1415 VA_OPEN (ap, msgid);
1416 VA_FIXEDARG (ap, const char *, msgid);
1418 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1419 DK_NOTE);
1420 report_diagnostic (&diagnostic);
1421 VA_CLOSE (ap);
1424 void
1425 warn_deprecated_use (node)
1426 tree node;
1428 if (node == 0 || !warn_deprecated_decl)
1429 return;
1431 if (DECL_P (node))
1432 warning ("`%s' is deprecated (declared at %s:%d)",
1433 IDENTIFIER_POINTER (DECL_NAME (node)),
1434 DECL_SOURCE_FILE (node), DECL_SOURCE_LINE (node));
1435 else if (TYPE_P (node))
1437 const char *what = NULL;
1438 tree decl = TYPE_STUB_DECL (node);
1440 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
1441 what = IDENTIFIER_POINTER (TYPE_NAME (node));
1442 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
1443 && DECL_NAME (TYPE_NAME (node)))
1444 what = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node)));
1446 if (what)
1448 if (decl)
1449 warning ("`%s' is deprecated (declared at %s:%d)", what,
1450 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
1451 else
1452 warning ("`%s' is deprecated", what);
1454 else if (decl)
1455 warning ("type is deprecated (declared at %s:%d)",
1456 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
1457 else
1458 warning ("type is deprecated");