* arm.md (stack_tie): New insn. Use an idiom that the alias code
[official-gcc.git] / gcc / diagnostic.c
blob120441f290f130bdd3c1c127d6a3e63583936508
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 obstack_chunk_alloc xmalloc
41 #define obstack_chunk_free free
43 #define output_formatted_integer(BUFFER, FORMAT, INTEGER) \
44 do \
45 { \
46 sprintf ((BUFFER)->digit_buffer, FORMAT, INTEGER); \
47 output_add_string (BUFFER, (BUFFER)->digit_buffer); \
48 } \
49 while (0)
51 #define output_text_length(BUFFER) (BUFFER)->line_length
52 #define is_starting_newline(BUFFER) (output_text_length (BUFFER) == 0)
53 #define line_wrap_cutoff(BUFFER) (BUFFER)->state.maximum_length
54 #define prefix_was_emitted_for(BUFFER) (BUFFER)->state.emitted_prefix_p
56 /* Prototypes. */
57 static void output_flush PARAMS ((output_buffer *));
58 static void output_do_verbatim PARAMS ((output_buffer *, text_info *));
59 static void output_buffer_to_stream PARAMS ((output_buffer *));
60 static void output_format PARAMS ((output_buffer *, text_info *));
61 static void output_indent PARAMS ((output_buffer *));
63 static char *vbuild_message_string PARAMS ((const char *, va_list))
64 ATTRIBUTE_PRINTF (1, 0);
65 static char *build_message_string PARAMS ((const char *, ...))
66 ATTRIBUTE_PRINTF_1;
67 static void format_with_decl PARAMS ((output_buffer *, text_info *, tree));
68 static void diagnostic_for_decl PARAMS ((diagnostic_info *, tree));
69 static void set_real_maximum_length PARAMS ((output_buffer *));
71 static void output_unsigned_decimal PARAMS ((output_buffer *, unsigned int));
72 static void output_long_decimal PARAMS ((output_buffer *, long int));
73 static void output_long_unsigned_decimal PARAMS ((output_buffer *,
74 long unsigned int));
75 static void output_octal PARAMS ((output_buffer *, unsigned int));
76 static void output_long_octal PARAMS ((output_buffer *, unsigned long int));
77 static void output_hexadecimal PARAMS ((output_buffer *, unsigned int));
78 static void output_long_hexadecimal PARAMS ((output_buffer *,
79 unsigned long int));
80 static void output_append_r PARAMS ((output_buffer *, const char *, int));
81 static void wrap_text PARAMS ((output_buffer *, const char *, const char *));
82 static void maybe_wrap_text PARAMS ((output_buffer *, const char *,
83 const char *));
84 static void output_clear_data PARAMS ((output_buffer *));
86 static void default_diagnostic_starter PARAMS ((diagnostic_context *,
87 diagnostic_info *));
88 static void default_diagnostic_finalizer PARAMS ((diagnostic_context *,
89 diagnostic_info *));
91 static void error_recursion PARAMS ((diagnostic_context *)) ATTRIBUTE_NORETURN;
92 static bool text_specifies_location PARAMS ((text_info *, location_t *));
94 extern int rtl_dump_and_exit;
95 extern int warnings_are_errors;
97 /* A diagnostic_context surrogate for stderr. */
98 static diagnostic_context global_diagnostic_context;
99 diagnostic_context *global_dc = &global_diagnostic_context;
102 /* Subroutine of output_set_maximum_length. Set up BUFFER's
103 internal maximum characters per line. */
104 static void
105 set_real_maximum_length (buffer)
106 output_buffer *buffer;
108 /* If we're told not to wrap lines then do the obvious thing. In case
109 we'll emit prefix only once per diagnostic message, it is appropriate
110 not to increase unnecessarily the line-length cut-off. */
111 if (!output_is_line_wrapping (buffer)
112 || output_prefixing_rule (buffer) == DIAGNOSTICS_SHOW_PREFIX_ONCE
113 || output_prefixing_rule (buffer) == DIAGNOSTICS_SHOW_PREFIX_NEVER)
114 line_wrap_cutoff (buffer) = output_line_cutoff (buffer);
115 else
117 int prefix_length = buffer->state.prefix ?
118 strlen (buffer->state.prefix) : 0;
119 /* If the prefix is ridiculously too long, output at least
120 32 characters. */
121 if (output_line_cutoff (buffer) - prefix_length < 32)
122 line_wrap_cutoff (buffer) = output_line_cutoff (buffer) + 32;
123 else
124 line_wrap_cutoff (buffer) = output_line_cutoff (buffer);
128 /* Sets the number of maximum characters per line BUFFER can output
129 in line-wrapping mode. A LENGTH value 0 suppresses line-wrapping. */
130 void
131 output_set_maximum_length (buffer, length)
132 output_buffer *buffer;
133 int length;
135 output_line_cutoff (buffer) = length;
136 set_real_maximum_length (buffer);
139 /* Sets BUFFER's PREFIX. */
140 void
141 output_set_prefix (buffer, prefix)
142 output_buffer *buffer;
143 const char *prefix;
145 buffer->state.prefix = prefix;
146 set_real_maximum_length (buffer);
147 prefix_was_emitted_for (buffer) = false;
148 output_indentation (buffer) = 0;
151 /* Return a pointer to the last character emitted in the output
152 BUFFER area. A NULL pointer means no character available. */
153 const char *
154 output_last_position (buffer)
155 const output_buffer *buffer;
157 const char *p = NULL;
159 if (obstack_base (&buffer->obstack) != obstack_next_free (&buffer->obstack))
160 p = ((const char *) obstack_next_free (&buffer->obstack)) - 1;
161 return p;
164 /* Free BUFFER's prefix, a previously malloc'd string. */
165 void
166 output_destroy_prefix (buffer)
167 output_buffer *buffer;
169 if (buffer->state.prefix != NULL)
171 free ((char *) buffer->state.prefix);
172 buffer->state.prefix = NULL;
176 /* Zero out any text output so far in BUFFER. */
177 void
178 output_clear_message_text (buffer)
179 output_buffer *buffer;
181 obstack_free (&buffer->obstack, obstack_base (&buffer->obstack));
182 output_text_length (buffer) = 0;
185 /* Zero out any formatting data used so far by BUFFER. */
186 static void
187 output_clear_data (buffer)
188 output_buffer *buffer;
190 prefix_was_emitted_for (buffer) = false;
191 output_indentation (buffer) = 0;
194 /* Construct an output BUFFER with PREFIX and of MAXIMUM_LENGTH
195 characters per line. */
196 void
197 init_output_buffer (buffer, prefix, maximum_length)
198 output_buffer *buffer;
199 const char *prefix;
200 int maximum_length;
202 memset (buffer, 0, sizeof (output_buffer));
203 obstack_init (&buffer->obstack);
204 output_buffer_attached_stream (buffer) = stderr;
205 output_line_cutoff (buffer) = maximum_length;
206 output_prefixing_rule (buffer) = diagnostic_prefixing_rule (global_dc);
207 output_set_prefix (buffer, prefix);
208 output_text_length (buffer) = 0;
209 output_clear_data (buffer);
212 /* Reinitialize BUFFER. */
213 void
214 output_clear (buffer)
215 output_buffer *buffer;
217 output_clear_message_text (buffer);
218 output_clear_data (buffer);
221 /* Finishes constructing a NULL-terminated character string representing
222 the BUFFERed message. */
223 const char *
224 output_finalize_message (buffer)
225 output_buffer *buffer;
227 obstack_1grow (&buffer->obstack, '\0');
228 return output_message_text (buffer);
231 /* Return the amount of characters BUFFER can accept to
232 make a full line. */
234 output_space_left (buffer)
235 const output_buffer *buffer;
237 return line_wrap_cutoff (buffer) - output_text_length (buffer);
240 /* Write out BUFFER's prefix. */
241 void
242 output_emit_prefix (buffer)
243 output_buffer *buffer;
245 if (buffer->state.prefix != NULL)
247 switch (output_prefixing_rule (buffer))
249 default:
250 case DIAGNOSTICS_SHOW_PREFIX_NEVER:
251 break;
253 case DIAGNOSTICS_SHOW_PREFIX_ONCE:
254 if (prefix_was_emitted_for (buffer))
256 output_indent (buffer);
257 break;
259 output_indentation (buffer) += 3;
260 /* Fall through. */
262 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
264 int prefix_length = strlen (buffer->state.prefix);
265 output_append_r (buffer, buffer->state.prefix, prefix_length);
266 prefix_was_emitted_for (buffer) = true;
268 break;
273 /* Have BUFFER start a new line. */
274 void
275 output_add_newline (buffer)
276 output_buffer *buffer;
278 obstack_1grow (&buffer->obstack, '\n');
279 output_text_length (buffer) = 0;
282 /* Appends a character to BUFFER. */
283 void
284 output_add_character (buffer, c)
285 output_buffer *buffer;
286 int c;
288 if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
289 output_add_newline (buffer);
290 obstack_1grow (&buffer->obstack, c);
291 ++output_text_length (buffer);
294 /* Adds a space to BUFFER. */
295 void
296 output_add_space (buffer)
297 output_buffer *buffer;
299 if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
301 output_add_newline (buffer);
302 return;
304 obstack_1grow (&buffer->obstack, ' ');
305 ++output_text_length (buffer);
308 /* These functions format an INTEGER into BUFFER as suggested by their
309 names. */
310 void
311 output_decimal (buffer, i)
312 output_buffer *buffer;
313 int i;
315 output_formatted_integer (buffer, "%d", i);
318 static void
319 output_long_decimal (buffer, i)
320 output_buffer *buffer;
321 long int i;
323 output_formatted_integer (buffer, "%ld", i);
326 static void
327 output_unsigned_decimal (buffer, i)
328 output_buffer *buffer;
329 unsigned int i;
331 output_formatted_integer (buffer, "%u", i);
334 static void
335 output_long_unsigned_decimal (buffer, i)
336 output_buffer *buffer;
337 long unsigned int i;
339 output_formatted_integer (buffer, "%lu", i);
342 static void
343 output_octal (buffer, i)
344 output_buffer *buffer;
345 unsigned int i;
347 output_formatted_integer (buffer, "%o", i);
350 static void
351 output_long_octal (buffer, i)
352 output_buffer *buffer;
353 unsigned long int i;
355 output_formatted_integer (buffer, "%lo", i);
358 static void
359 output_hexadecimal (buffer, i)
360 output_buffer *buffer;
361 unsigned int i;
363 output_formatted_integer (buffer, "%x", i);
366 static void
367 output_long_hexadecimal (buffer, i)
368 output_buffer *buffer;
369 unsigned long int i;
371 output_formatted_integer (buffer, "%lx", i);
374 /* Append to BUFFER a string specified by its STARTING character
375 and LENGTH. */
376 static void
377 output_append_r (buffer, start, length)
378 output_buffer *buffer;
379 const char *start;
380 int length;
382 obstack_grow (&buffer->obstack, start, length);
383 output_text_length (buffer) += length;
386 /* Append a string deliminated by START and END to BUFFER. No wrapping is
387 done. However, if beginning a new line then emit BUFFER->state.prefix
388 and skip any leading whitespace if appropriate. The caller must ensure
389 that it is safe to do so. */
390 void
391 output_append (buffer, start, end)
392 output_buffer *buffer;
393 const char *start;
394 const char *end;
396 /* Emit prefix and skip whitespace if we're starting a new line. */
397 if (is_starting_newline (buffer))
399 output_emit_prefix (buffer);
400 if (output_is_line_wrapping (buffer))
401 while (start != end && *start == ' ')
402 ++start;
404 output_append_r (buffer, start, end - start);
407 static void
408 output_indent (buffer)
409 output_buffer *buffer;
411 int n = output_indentation (buffer);
412 int i;
414 for (i = 0; i < n; ++i)
415 output_add_character (buffer, ' ');
418 /* Wrap a text delimited by START and END into BUFFER. */
419 static void
420 wrap_text (buffer, start, end)
421 output_buffer *buffer;
422 const char *start;
423 const char *end;
425 bool is_wrapping = output_is_line_wrapping (buffer);
427 while (start != end)
429 /* Dump anything bordered by whitespaces. */
431 const char *p = start;
432 while (p != end && *p != ' ' && *p != '\n')
433 ++p;
434 if (is_wrapping && p - start >= output_space_left (buffer))
435 output_add_newline (buffer);
436 output_append (buffer, start, p);
437 start = p;
440 if (start != end && *start == ' ')
442 output_add_space (buffer);
443 ++start;
445 if (start != end && *start == '\n')
447 output_add_newline (buffer);
448 ++start;
453 /* Same as wrap_text but wrap text only when in line-wrapping mode. */
454 static void
455 maybe_wrap_text (buffer, start, end)
456 output_buffer *buffer;
457 const char *start;
458 const char *end;
460 if (output_is_line_wrapping (buffer))
461 wrap_text (buffer, start, end);
462 else
463 output_append (buffer, start, end);
467 /* Append a STRING to BUFFER; the STRING might be line-wrapped if in
468 appropriate mode. */
469 void
470 output_add_string (buffer, str)
471 output_buffer *buffer;
472 const char *str;
474 maybe_wrap_text (buffer, str, str + (str ? strlen (str) : 0));
477 /* Flush the content of BUFFER onto the attached stream,
478 and reinitialize. */
480 static void
481 output_buffer_to_stream (buffer)
482 output_buffer *buffer;
484 const char *text = output_finalize_message (buffer);
485 fputs (text, output_buffer_attached_stream (buffer));
486 output_clear_message_text (buffer);
489 /* Format a message pointed to by TEXT. The following format specifiers are
490 recognized as being language independent:
491 %d, %i: (signed) integer in base ten.
492 %u: unsigned integer in base ten.
493 %o: unsigned integer in base eight.
494 %x: unsigned integer in base sixteen.
495 %ld, %li, %lo, %lu, %lx: long versions of the above.
496 %c: character.
497 %s: string.
498 %%: `%'.
499 %*.s: a substring the length of which is specified by an integer.
500 %H: location_t. */
501 static void
502 output_format (buffer, text)
503 output_buffer *buffer;
504 text_info *text;
506 for (; *text->format_spec; ++text->format_spec)
508 bool long_integer = 0;
510 /* Ignore text. */
512 const char *p = text->format_spec;
513 while (*p && *p != '%')
514 ++p;
515 wrap_text (buffer, text->format_spec, p);
516 text->format_spec = p;
519 if (*text->format_spec == '\0')
520 break;
522 /* We got a '%'. Let's see what happens. Record whether we're
523 parsing a long integer format specifier. */
524 if (*++text->format_spec == 'l')
526 long_integer = true;
527 ++text->format_spec;
530 /* Handle %c, %d, %i, %ld, %li, %lo, %lu, %lx, %o, %s, %u,
531 %x, %.*s; %%. And nothing else. Front-ends should install
532 printers to grok language specific format specifiers. */
533 switch (*text->format_spec)
535 case 'c':
536 output_add_character (buffer, va_arg (*text->args_ptr, int));
537 break;
539 case 'd':
540 case 'i':
541 if (long_integer)
542 output_long_decimal (buffer, va_arg (*text->args_ptr, long int));
543 else
544 output_decimal (buffer, va_arg (*text->args_ptr, int));
545 break;
547 case 'o':
548 if (long_integer)
549 output_long_octal (buffer,
550 va_arg (*text->args_ptr, unsigned long int));
551 else
552 output_octal (buffer, va_arg (*text->args_ptr, unsigned int));
553 break;
555 case 's':
556 output_add_string (buffer, va_arg (*text->args_ptr, const char *));
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. Sorry. */
612 abort ();
618 static char *
619 vbuild_message_string (msg, ap)
620 const char *msg;
621 va_list ap;
623 char *str;
625 vasprintf (&str, msg, ap);
626 return str;
629 /* Return a malloc'd string containing MSG formatted a la
630 printf. The caller is responsible for freeing the memory. */
631 static char *
632 build_message_string VPARAMS ((const char *msg, ...))
634 char *str;
636 VA_OPEN (ap, msg);
637 VA_FIXEDARG (ap, const char *, msg);
639 str = vbuild_message_string (msg, ap);
641 VA_CLOSE (ap);
643 return str;
646 /* Same as diagnsotic_build_prefix, but only the source FILE is given. */
647 char *
648 file_name_as_prefix (f)
649 const char *f;
651 return build_message_string ("%s: ", f);
654 /* Format a message into BUFFER a la printf. */
655 void
656 output_printf VPARAMS ((struct output_buffer *buffer, const char *msgid, ...))
658 text_info text;
659 VA_OPEN (ap, msgid);
660 VA_FIXEDARG (ap, output_buffer *, buffer);
661 VA_FIXEDARG (ap, const char *, msgid);
663 text.args_ptr = &ap;
664 text.format_spec = _(msgid);
665 output_format (buffer, &text);
666 VA_CLOSE (ap);
669 /* Print a message relevant to the given DECL. */
670 static void
671 format_with_decl (buffer, text, decl)
672 output_buffer *buffer;
673 text_info *text;
674 tree decl;
676 const char *p;
678 /* Do magic to get around lack of varargs support for insertion
679 of arguments into existing list. We know that the decl is first;
680 we ass_u_me that it will be printed with "%s". */
681 for (p = text->format_spec; *p; ++p)
683 if (*p == '%')
685 if (*(p + 1) == '%')
686 ++p;
687 else if (*(p + 1) != 's')
688 abort ();
689 else
690 break;
694 /* Print the left-hand substring. */
695 maybe_wrap_text (buffer, text->format_spec, p);
697 if (*p == '%') /* Print the name. */
699 const char *const n = (DECL_NAME (decl)
700 ? (*lang_hooks.decl_printable_name) (decl, 2)
701 : _("((anonymous))"));
702 output_add_string (buffer, n);
703 while (*p)
705 ++p;
706 if (ISALPHA (*(p - 1) & 0xFF))
707 break;
711 if (*p) /* Print the rest of the message. */
713 text->format_spec = p;
714 output_format (buffer, text);
718 /* Flush the content of BUFFER onto the attached stream. */
719 static void
720 output_flush (buffer)
721 output_buffer *buffer;
723 output_buffer_to_stream (buffer);
724 output_clear_data (buffer);
725 fputc ('\n', output_buffer_attached_stream (buffer));
726 fflush (output_buffer_attached_stream (buffer));
729 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
730 settings needed by BUFFER for a verbatim formatting. */
731 static void
732 output_do_verbatim (buffer, text)
733 output_buffer *buffer;
734 text_info *text;
736 diagnostic_prefixing_rule_t rule = output_prefixing_rule (buffer);
737 int line_cutoff = output_line_cutoff (buffer);
739 /* Set verbatim mode. */
740 output_prefixing_rule (buffer) = DIAGNOSTICS_SHOW_PREFIX_NEVER;
741 output_line_cutoff (buffer) = 0;
742 /* Do the actual formatting. */
743 output_format (buffer, text);
744 /* Restore previous settings. */
745 output_prefixing_rule (buffer) = rule;
746 output_line_cutoff (buffer) = line_cutoff;
749 /* Output MESSAGE verbatim into BUFFER. */
750 void
751 output_verbatim VPARAMS ((output_buffer *buffer, const char *msgid, ...))
753 text_info text;
754 VA_OPEN (ap, msgid);
755 VA_FIXEDARG (ap, output_buffer *, buffer);
756 VA_FIXEDARG (ap, const char *, msgid);
758 text.format_spec = msgid;
759 text.args_ptr = &ap;
760 output_do_verbatim (buffer, &text);
761 VA_CLOSE (ap);
765 /* Initialize the diagnostic message outputting machinery. */
766 void
767 diagnostic_initialize (context)
768 diagnostic_context *context;
770 memset (context, 0, sizeof *context);
771 obstack_init (&context->buffer.obstack);
773 /* By default, diagnostics are sent to stderr. */
774 output_buffer_attached_stream (&context->buffer) = stderr;
776 /* By default, we emit prefixes once per message. */
777 diagnostic_prefixing_rule (context) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
779 diagnostic_starter (context) = default_diagnostic_starter;
780 diagnostic_finalizer (context) = default_diagnostic_finalizer;
781 context->warnings_are_errors_message = warnings_are_errors;
784 /* Returns true if the next format specifier in TEXT is a format specifier
785 for a location_t. If so, update the object pointed by LOCUS to reflect
786 the specified location in *TEXT->args_ptr. */
787 static bool
788 text_specifies_location (text, locus)
789 text_info *text;
790 location_t *locus;
792 const char *p;
793 /* Skip any leading text. */
794 for (p = text->format_spec; *p && *p != '%'; ++p)
797 /* Extract the location information if any. */
798 if (*p == '%' && *++p == 'H')
800 *locus = *va_arg (*text->args_ptr, location_t *);
801 text->format_spec = p + 1;
802 return true;
805 return false;
808 void
809 diagnostic_set_info (diagnostic, msgid, args, file, line, kind)
810 diagnostic_info *diagnostic;
811 const char *msgid;
812 va_list *args;
813 const char *file;
814 int line;
815 diagnostic_t kind;
817 diagnostic->message.format_spec = msgid;
818 diagnostic->message.args_ptr = args;
819 /* If the diagnostic message doesn't specify a loccation,
820 use FILE and LINE. */
821 if (!text_specifies_location (&diagnostic->message, &diagnostic->location))
823 diagnostic->location.file = file;
824 diagnostic->location.line = line;
826 diagnostic->kind = kind;
829 /* Return a malloc'd string describing a location. The caller is
830 responsible for freeing the memory. */
831 char *
832 diagnostic_build_prefix (diagnostic)
833 diagnostic_info *diagnostic;
835 static const char *const diagnostic_kind_text[] = {
836 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
837 #include "diagnostic.def"
838 #undef DEFINE_DIAGNOSTIC_KIND
839 "must-not-happen"
841 if (diagnostic->kind >= DK_LAST_DIAGNOSTIC_KIND)
842 abort();
844 return diagnostic->location.file
845 ? build_message_string ("%s:%d: %s",
846 diagnostic->location.file,
847 diagnostic->location.line,
848 _(diagnostic_kind_text[diagnostic->kind]))
849 : build_message_string ("%s: %s", progname,
850 _(diagnostic_kind_text[diagnostic->kind]));
853 /* Report a diagnostic MESSAGE at the declaration DECL.
854 MSG is a format string which uses %s to substitute the declaration
855 name; subsequent substitutions are a la output_format. */
856 static void
857 diagnostic_for_decl (diagnostic, decl)
858 diagnostic_info *diagnostic;
859 tree decl;
861 if (global_dc->lock++)
862 error_recursion (global_dc);
864 if (diagnostic_count_diagnostic (global_dc, diagnostic->kind))
866 diagnostic_report_current_function (global_dc);
867 output_set_prefix
868 (&global_dc->buffer, diagnostic_build_prefix (diagnostic));
869 format_with_decl (&global_dc->buffer, &diagnostic->message, decl);
870 output_flush (&global_dc->buffer);
871 output_destroy_prefix (&global_dc->buffer);
873 global_dc->lock--;
876 void
877 diagnostic_flush_buffer (context)
878 diagnostic_context *context;
880 output_buffer_to_stream (&context->buffer);
881 fflush (output_buffer_attached_stream (&context->buffer));
884 /* Count a diagnostic. Return true if the message should be printed. */
885 bool
886 diagnostic_count_diagnostic (context, kind)
887 diagnostic_context *context;
888 diagnostic_t kind;
890 switch (kind)
892 default:
893 abort();
894 break;
896 case DK_FATAL: case DK_ICE: case DK_SORRY:
897 case DK_ANACHRONISM: case DK_NOTE:
898 ++diagnostic_kind_count (context, kind);
899 break;
901 case DK_WARNING:
902 if (!diagnostic_report_warnings_p ())
903 return false;
904 else if (!warnings_are_errors)
906 ++diagnostic_kind_count (context, DK_WARNING);
907 break;
909 /* else fall through. */
911 case DK_ERROR:
912 if (kind == DK_WARNING && context->warnings_are_errors_message)
914 output_verbatim (&context->buffer,
915 "%s: warnings being treated as errors\n", progname);
916 context->warnings_are_errors_message = false;
918 ++diagnostic_kind_count (context, DK_ERROR);
919 break;
922 return true;
925 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
926 runs its second argument through gettext. */
927 void
928 fnotice VPARAMS ((FILE *file, const char *msgid, ...))
930 VA_OPEN (ap, msgid);
931 VA_FIXEDARG (ap, FILE *, file);
932 VA_FIXEDARG (ap, const char *, msgid);
934 vfprintf (file, _(msgid), ap);
935 VA_CLOSE (ap);
939 /* Print a fatal I/O error message. Argument are like printf.
940 Also include a system error message based on `errno'. */
941 void
942 fatal_io_error VPARAMS ((const char *msgid, ...))
944 text_info text;
945 VA_OPEN (ap, msgid);
946 VA_FIXEDARG (ap, const char *, msgid);
948 text.format_spec = _(msgid);
949 text.args_ptr = &ap;
950 output_printf (&global_dc->buffer, "%s: %s: ", progname, xstrerror (errno));
951 output_format (&global_dc->buffer, &text);
952 output_flush (&global_dc->buffer);
953 VA_CLOSE (ap);
954 exit (FATAL_EXIT_CODE);
957 /* Issue a pedantic warning MSGID. */
958 void
959 pedwarn VPARAMS ((const char *msgid, ...))
961 diagnostic_info diagnostic;
962 VA_OPEN (ap, msgid);
963 VA_FIXEDARG (ap, const char *, msgid);
965 diagnostic_set_info (&diagnostic, _(msgid), &ap, input_filename, lineno,
966 pedantic_error_kind ());
967 report_diagnostic (&diagnostic);
968 VA_CLOSE (ap);
971 /* Issue a pedantic warning about DECL. */
972 void
973 pedwarn_with_decl VPARAMS ((tree decl, const char *msgid, ...))
975 diagnostic_info diagnostic;
976 VA_OPEN (ap, msgid);
977 VA_FIXEDARG (ap, tree, decl);
978 VA_FIXEDARG (ap, const char *, msgid);
980 diagnostic_set_info (&diagnostic, _(msgid), &ap,
981 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
982 pedantic_error_kind ());
984 /* We don't want -pedantic-errors to cause the compilation to fail from
985 "errors" in system header files. Sometimes fixincludes can't fix what's
986 broken (eg: unsigned char bitfields - fixing it may change the alignment
987 which will cause programs to mysteriously fail because the C library
988 or kernel uses the original layout). There's no point in issuing a
989 warning either, it's just unnecessary noise. */
990 if (!DECL_IN_SYSTEM_HEADER (decl))
991 diagnostic_for_decl (&diagnostic, decl);
992 VA_CLOSE (ap);
995 /* Same as above but within the context FILE and LINE. */
996 void
997 pedwarn_with_file_and_line VPARAMS ((const char *file, int line,
998 const char *msgid, ...))
1000 diagnostic_info diagnostic;
1001 VA_OPEN (ap, msgid);
1002 VA_FIXEDARG (ap, const char *, file);
1003 VA_FIXEDARG (ap, int, line);
1004 VA_FIXEDARG (ap, const char *, msgid);
1006 diagnostic_set_info (&diagnostic, _(msgid), &ap, file, line,
1007 pedantic_error_kind ());
1008 report_diagnostic (&diagnostic);
1009 VA_CLOSE (ap);
1012 /* Just apologize with MSGID. */
1013 void
1014 sorry VPARAMS ((const char *msgid, ...))
1016 diagnostic_info diagnostic;
1018 VA_OPEN (ap, msgid);
1019 VA_FIXEDARG (ap, const char *, msgid);
1021 ++sorrycount;
1022 diagnostic_set_info (&diagnostic, _(msgid), &ap,
1023 input_filename, lineno, DK_SORRY);
1025 output_set_prefix
1026 (&global_dc->buffer, diagnostic_build_prefix (&diagnostic));
1027 output_printf (&global_dc->buffer, "sorry, not implemented: ");
1028 output_format (&global_dc->buffer, &diagnostic.message);
1029 output_flush (&global_dc->buffer);
1030 VA_CLOSE (ap);
1033 /* Called when the start of a function definition is parsed,
1034 this function prints on stderr the name of the function. */
1035 void
1036 announce_function (decl)
1037 tree decl;
1039 if (!quiet_flag)
1041 if (rtl_dump_and_exit)
1042 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl)));
1043 else
1044 verbatim (" %s", (*lang_hooks.decl_printable_name) (decl, 2));
1045 fflush (stderr);
1046 output_needs_newline (&global_dc->buffer) = true;
1047 diagnostic_set_last_function (global_dc);
1051 /* The default function to print out name of current function that caused
1052 an error. */
1053 void
1054 lhd_print_error_function (context, file)
1055 diagnostic_context *context;
1056 const char *file;
1058 if (diagnostic_last_function_changed (context))
1060 const char *old_prefix = output_prefix (&context->buffer);
1061 char *new_prefix = file ? build_message_string ("%s: ", file) : NULL;
1063 output_set_prefix (&context->buffer, new_prefix);
1065 if (current_function_decl == NULL)
1066 output_add_string (&context->buffer, _("At top level:"));
1067 else
1069 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
1070 output_printf
1071 (&context->buffer, "In member function `%s':",
1072 (*lang_hooks.decl_printable_name) (current_function_decl, 2));
1073 else
1074 output_printf
1075 (&context->buffer, "In function `%s':",
1076 (*lang_hooks.decl_printable_name) (current_function_decl, 2));
1078 output_add_newline (&context->buffer);
1080 diagnostic_set_last_function (context);
1081 output_buffer_to_stream (&context->buffer);
1082 context->buffer.state.prefix = old_prefix;
1083 free ((char*) new_prefix);
1087 /* Prints out, if necessary, the name of the current function
1088 that caused an error. Called from all error and warning functions.
1089 We ignore the FILE parameter, as it cannot be relied upon. */
1091 void
1092 diagnostic_report_current_function (context)
1093 diagnostic_context *context;
1095 diagnostic_report_current_module (context);
1096 (*lang_hooks.print_error_function) (context, input_filename);
1099 void
1100 error_with_file_and_line VPARAMS ((const char *file, int line,
1101 const char *msgid, ...))
1103 diagnostic_info diagnostic;
1105 VA_OPEN (ap, msgid);
1106 VA_FIXEDARG (ap, const char *, file);
1107 VA_FIXEDARG (ap, int, line);
1108 VA_FIXEDARG (ap, const char *, msgid);
1110 diagnostic_set_info (&diagnostic, msgid, &ap, file, line, DK_ERROR);
1111 report_diagnostic (&diagnostic);
1112 VA_CLOSE (ap);
1115 void
1116 error_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1118 diagnostic_info diagnostic;
1119 VA_OPEN (ap, msgid);
1120 VA_FIXEDARG (ap, tree, decl);
1121 VA_FIXEDARG (ap, const char *, msgid);
1123 diagnostic_set_info (&diagnostic, msgid, &ap,
1124 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1125 DK_ERROR);
1126 diagnostic_for_decl (&diagnostic, decl);
1127 VA_CLOSE (ap);
1131 /* Report an error message. The arguments are like that of printf. */
1133 void
1134 error VPARAMS ((const char *msgid, ...))
1136 diagnostic_info diagnostic;
1138 VA_OPEN (ap, msgid);
1139 VA_FIXEDARG (ap, const char *, msgid);
1141 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1142 DK_ERROR);
1143 report_diagnostic (&diagnostic);
1144 VA_CLOSE (ap);
1147 /* Likewise, except that the compilation is terminated after printing the
1148 error message. */
1150 void
1151 fatal_error VPARAMS ((const char *msgid, ...))
1153 diagnostic_info diagnostic;
1155 VA_OPEN (ap, msgid);
1156 VA_FIXEDARG (ap, const char *, msgid);
1158 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1159 DK_FATAL);
1160 report_diagnostic (&diagnostic);
1161 VA_CLOSE (ap);
1163 fnotice (stderr, "compilation terminated.\n");
1164 exit (FATAL_EXIT_CODE);
1167 void
1168 internal_error VPARAMS ((const char *msgid, ...))
1170 diagnostic_info diagnostic;
1172 VA_OPEN (ap, msgid);
1173 VA_FIXEDARG (ap, const char *, msgid);
1175 if (global_dc->lock)
1176 error_recursion (global_dc);
1178 #ifndef ENABLE_CHECKING
1179 if (errorcount > 0 || sorrycount > 0)
1181 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
1182 input_filename, lineno);
1183 exit (FATAL_EXIT_CODE);
1185 #endif
1187 if (global_dc->internal_error != 0)
1188 (*global_dc->internal_error) (_(msgid), &ap);
1190 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1191 DK_ICE);
1192 report_diagnostic (&diagnostic);
1193 VA_CLOSE (ap);
1195 fnotice (stderr,
1196 "Please submit a full bug report,\n\
1197 with preprocessed source if appropriate.\n\
1198 See %s for instructions.\n", GCCBUGURL);
1199 exit (FATAL_EXIT_CODE);
1202 void
1203 warning_with_file_and_line VPARAMS ((const char *file, int line,
1204 const char *msgid, ...))
1206 diagnostic_info diagnostic;
1208 VA_OPEN (ap, msgid);
1209 VA_FIXEDARG (ap, const char *, file);
1210 VA_FIXEDARG (ap, int, line);
1211 VA_FIXEDARG (ap, const char *, msgid);
1213 diagnostic_set_info (&diagnostic, msgid, &ap, file, line, DK_WARNING);
1214 report_diagnostic (&diagnostic);
1215 VA_CLOSE (ap);
1218 void
1219 warning_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1221 diagnostic_info diagnostic;
1222 VA_OPEN (ap, msgid);
1223 VA_FIXEDARG (ap, tree, decl);
1224 VA_FIXEDARG (ap, const char *, msgid);
1226 diagnostic_set_info (&diagnostic, msgid, &ap,
1227 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1228 DK_WARNING);
1229 diagnostic_for_decl (&diagnostic, decl);
1230 VA_CLOSE (ap);
1233 void
1234 warning VPARAMS ((const char *msgid, ...))
1236 diagnostic_info diagnostic;
1238 VA_OPEN (ap, msgid);
1239 VA_FIXEDARG (ap, const char *, msgid);
1241 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1242 DK_WARNING);
1243 report_diagnostic (&diagnostic);
1244 VA_CLOSE (ap);
1248 /* Same as above but use diagnostic_buffer. */
1250 void
1251 verbatim VPARAMS ((const char *msgid, ...))
1253 text_info text;
1254 VA_OPEN (ap, msgid);
1255 VA_FIXEDARG (ap, const char *, msgid);
1257 text.format_spec = _(msgid);
1258 text.args_ptr = &ap;
1259 output_do_verbatim (&global_dc->buffer, &text);
1260 output_buffer_to_stream (&global_dc->buffer);
1261 VA_CLOSE (ap);
1264 /* Report a diagnostic message (an error or a warning) as specified by
1265 DC. This function is *the* subroutine in terms of which front-ends
1266 should implement their specific diagnostic handling modules. The
1267 front-end independent format specifiers are exactly those described
1268 in the documentation of output_format. */
1270 void
1271 diagnostic_report_diagnostic (context, diagnostic)
1272 diagnostic_context *context;
1273 diagnostic_info *diagnostic;
1275 if (context->lock++)
1276 error_recursion (context);
1278 if (diagnostic_count_diagnostic (context, diagnostic->kind))
1280 (*diagnostic_starter (context)) (context, diagnostic);
1281 output_format (&context->buffer, &diagnostic->message);
1282 (*diagnostic_finalizer (context)) (context, diagnostic);
1283 output_flush (&context->buffer);
1286 --context->lock;
1289 /* Inform the user that an error occurred while trying to report some
1290 other error. This indicates catastrophic internal inconsistencies,
1291 so give up now. But do try to flush out the previous error.
1292 This mustn't use internal_error, that will cause infinite recursion. */
1294 static void
1295 error_recursion (context)
1296 diagnostic_context *context;
1298 if (context->lock < 3)
1299 output_flush (&context->buffer);
1301 fnotice (stderr,
1302 "Internal compiler error: Error reporting routines re-entered.\n");
1303 fnotice (stderr,
1304 "Please submit a full bug report,\n\
1305 with preprocessed source if appropriate.\n\
1306 See %s for instructions.\n", GCCBUGURL);
1307 exit (FATAL_EXIT_CODE);
1310 /* Given a partial pathname as input, return another pathname that
1311 shares no directory elements with the pathname of __FILE__. This
1312 is used by fancy_abort() to print `Internal compiler error in expr.c'
1313 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1315 const char *
1316 trim_filename (name)
1317 const char *name;
1319 static const char this_file[] = __FILE__;
1320 const char *p = name, *q = this_file;
1322 /* First skip any "../" in each filename. This allows us to give a proper
1323 reference to a file in a subdirectory. */
1324 while (p[0] == '.' && p[1] == '.'
1325 && (p[2] == DIR_SEPARATOR
1326 #ifdef DIR_SEPARATOR_2
1327 || p[2] == DIR_SEPARATOR_2
1328 #endif
1330 p += 3;
1332 while (q[0] == '.' && q[1] == '.'
1333 && (q[2] == DIR_SEPARATOR
1334 #ifdef DIR_SEPARATOR_2
1335 || p[2] == DIR_SEPARATOR_2
1336 #endif
1338 q += 3;
1340 /* Now skip any parts the two filenames have in common. */
1341 while (*p == *q && *p != 0 && *q != 0)
1342 p++, q++;
1344 /* Now go backwards until the previous directory separator. */
1345 while (p > name && p[-1] != DIR_SEPARATOR
1346 #ifdef DIR_SEPARATOR_2
1347 && p[-1] != DIR_SEPARATOR_2
1348 #endif
1350 p--;
1352 return p;
1355 /* Report an internal compiler error in a friendly manner and without
1356 dumping core. */
1358 void
1359 fancy_abort (file, line, function)
1360 const char *file;
1361 int line;
1362 const char *function;
1364 internal_error ("Internal compiler error in %s, at %s:%d",
1365 function, trim_filename (file), line);
1368 void
1369 diagnostic_report_current_module (context)
1370 diagnostic_context *context;
1372 struct file_stack *p;
1374 if (output_needs_newline (&context->buffer))
1376 output_add_newline (&context->buffer);
1377 output_needs_newline (&context->buffer) = false;
1380 if (input_file_stack && input_file_stack->next != 0
1381 && diagnostic_last_module_changed (context))
1383 for (p = input_file_stack->next; p; p = p->next)
1384 if (p == input_file_stack->next)
1385 output_verbatim (&context->buffer,
1386 "In file included from %s:%d", p->name, p->line);
1387 else
1388 output_verbatim (&context->buffer,
1389 ",\n from %s:%d", p->name, p->line);
1390 output_verbatim (&context->buffer, ":\n");
1391 diagnostic_set_last_module (context);
1395 static void
1396 default_diagnostic_starter (context, diagnostic)
1397 diagnostic_context *context;
1398 diagnostic_info *diagnostic;
1400 diagnostic_report_current_function (context);
1401 output_set_prefix (&context->buffer, diagnostic_build_prefix (diagnostic));
1404 static void
1405 default_diagnostic_finalizer (context, diagnostic)
1406 diagnostic_context *context;
1407 diagnostic_info *diagnostic __attribute__((unused));
1409 output_destroy_prefix (&context->buffer);
1412 void
1413 warn_deprecated_use (node)
1414 tree node;
1416 if (node == 0 || !warn_deprecated_decl)
1417 return;
1419 if (DECL_P (node))
1420 warning ("`%s' is deprecated (declared at %s:%d)",
1421 IDENTIFIER_POINTER (DECL_NAME (node)),
1422 DECL_SOURCE_FILE (node), DECL_SOURCE_LINE (node));
1423 else if (TYPE_P (node))
1425 const char *what = NULL;
1426 tree decl = TYPE_STUB_DECL (node);
1428 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
1429 what = IDENTIFIER_POINTER (TYPE_NAME (node));
1430 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
1431 && DECL_NAME (TYPE_NAME (node)))
1432 what = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node)));
1434 if (what)
1436 if (decl)
1437 warning ("`%s' is deprecated (declared at %s:%d)", what,
1438 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
1439 else
1440 warning ("`%s' is deprecated", what);
1442 else if (decl)
1443 warning ("type is deprecated (declared at %s:%d)",
1444 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
1445 else
1446 warning ("type is deprecated");