re PR libstdc++/8716 (std::string( NULL, 0 ) throws exception also on zero length)
[official-gcc.git] / gcc / diagnostic.c
blobb6794b80ff8e16cc0557c8660c4ae6aa5ddbb01e
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_format (&global_dc->buffer, &diagnostic.message);
1027 output_flush (&global_dc->buffer);
1028 VA_CLOSE (ap);
1031 /* Called when the start of a function definition is parsed,
1032 this function prints on stderr the name of the function. */
1033 void
1034 announce_function (decl)
1035 tree decl;
1037 if (!quiet_flag)
1039 if (rtl_dump_and_exit)
1040 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl)));
1041 else
1042 verbatim (" %s", (*lang_hooks.decl_printable_name) (decl, 2));
1043 fflush (stderr);
1044 output_needs_newline (&global_dc->buffer) = true;
1045 diagnostic_set_last_function (global_dc);
1049 /* The default function to print out name of current function that caused
1050 an error. */
1051 void
1052 lhd_print_error_function (context, file)
1053 diagnostic_context *context;
1054 const char *file;
1056 if (diagnostic_last_function_changed (context))
1058 const char *old_prefix = output_prefix (&context->buffer);
1059 char *new_prefix = file ? build_message_string ("%s: ", file) : NULL;
1061 output_set_prefix (&context->buffer, new_prefix);
1063 if (current_function_decl == NULL)
1064 output_add_string (&context->buffer, _("At top level:"));
1065 else
1067 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
1068 output_printf
1069 (&context->buffer, "In member function `%s':",
1070 (*lang_hooks.decl_printable_name) (current_function_decl, 2));
1071 else
1072 output_printf
1073 (&context->buffer, "In function `%s':",
1074 (*lang_hooks.decl_printable_name) (current_function_decl, 2));
1076 output_add_newline (&context->buffer);
1078 diagnostic_set_last_function (context);
1079 output_buffer_to_stream (&context->buffer);
1080 context->buffer.state.prefix = old_prefix;
1081 free ((char*) new_prefix);
1085 /* Prints out, if necessary, the name of the current function
1086 that caused an error. Called from all error and warning functions.
1087 We ignore the FILE parameter, as it cannot be relied upon. */
1089 void
1090 diagnostic_report_current_function (context)
1091 diagnostic_context *context;
1093 diagnostic_report_current_module (context);
1094 (*lang_hooks.print_error_function) (context, input_filename);
1097 void
1098 error_with_file_and_line VPARAMS ((const char *file, int line,
1099 const char *msgid, ...))
1101 diagnostic_info diagnostic;
1103 VA_OPEN (ap, msgid);
1104 VA_FIXEDARG (ap, const char *, file);
1105 VA_FIXEDARG (ap, int, line);
1106 VA_FIXEDARG (ap, const char *, msgid);
1108 diagnostic_set_info (&diagnostic, msgid, &ap, file, line, DK_ERROR);
1109 report_diagnostic (&diagnostic);
1110 VA_CLOSE (ap);
1113 void
1114 error_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1116 diagnostic_info diagnostic;
1117 VA_OPEN (ap, msgid);
1118 VA_FIXEDARG (ap, tree, decl);
1119 VA_FIXEDARG (ap, const char *, msgid);
1121 diagnostic_set_info (&diagnostic, msgid, &ap,
1122 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1123 DK_ERROR);
1124 diagnostic_for_decl (&diagnostic, decl);
1125 VA_CLOSE (ap);
1129 /* Report an error message. The arguments are like that of printf. */
1131 void
1132 error VPARAMS ((const char *msgid, ...))
1134 diagnostic_info diagnostic;
1136 VA_OPEN (ap, msgid);
1137 VA_FIXEDARG (ap, const char *, msgid);
1139 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1140 DK_ERROR);
1141 report_diagnostic (&diagnostic);
1142 VA_CLOSE (ap);
1145 /* Likewise, except that the compilation is terminated after printing the
1146 error message. */
1148 void
1149 fatal_error VPARAMS ((const char *msgid, ...))
1151 diagnostic_info diagnostic;
1153 VA_OPEN (ap, msgid);
1154 VA_FIXEDARG (ap, const char *, msgid);
1156 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1157 DK_FATAL);
1158 report_diagnostic (&diagnostic);
1159 VA_CLOSE (ap);
1161 fnotice (stderr, "compilation terminated.\n");
1162 exit (FATAL_EXIT_CODE);
1165 void
1166 internal_error VPARAMS ((const char *msgid, ...))
1168 diagnostic_info diagnostic;
1170 VA_OPEN (ap, msgid);
1171 VA_FIXEDARG (ap, const char *, msgid);
1173 if (global_dc->lock)
1174 error_recursion (global_dc);
1176 #ifndef ENABLE_CHECKING
1177 if (errorcount > 0 || sorrycount > 0)
1179 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
1180 input_filename, lineno);
1181 exit (FATAL_EXIT_CODE);
1183 #endif
1185 if (global_dc->internal_error != 0)
1186 (*global_dc->internal_error) (_(msgid), &ap);
1188 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1189 DK_ICE);
1190 report_diagnostic (&diagnostic);
1191 VA_CLOSE (ap);
1193 fnotice (stderr,
1194 "Please submit a full bug report,\n\
1195 with preprocessed source if appropriate.\n\
1196 See %s for instructions.\n", bug_report_url);
1197 exit (FATAL_EXIT_CODE);
1200 void
1201 warning_with_file_and_line VPARAMS ((const char *file, int line,
1202 const char *msgid, ...))
1204 diagnostic_info diagnostic;
1206 VA_OPEN (ap, msgid);
1207 VA_FIXEDARG (ap, const char *, file);
1208 VA_FIXEDARG (ap, int, line);
1209 VA_FIXEDARG (ap, const char *, msgid);
1211 diagnostic_set_info (&diagnostic, msgid, &ap, file, line, DK_WARNING);
1212 report_diagnostic (&diagnostic);
1213 VA_CLOSE (ap);
1216 void
1217 warning_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1219 diagnostic_info diagnostic;
1220 VA_OPEN (ap, msgid);
1221 VA_FIXEDARG (ap, tree, decl);
1222 VA_FIXEDARG (ap, const char *, msgid);
1224 diagnostic_set_info (&diagnostic, msgid, &ap,
1225 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1226 DK_WARNING);
1227 diagnostic_for_decl (&diagnostic, decl);
1228 VA_CLOSE (ap);
1231 void
1232 warning VPARAMS ((const char *msgid, ...))
1234 diagnostic_info diagnostic;
1236 VA_OPEN (ap, msgid);
1237 VA_FIXEDARG (ap, const char *, msgid);
1239 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1240 DK_WARNING);
1241 report_diagnostic (&diagnostic);
1242 VA_CLOSE (ap);
1246 /* Same as above but use diagnostic_buffer. */
1248 void
1249 verbatim VPARAMS ((const char *msgid, ...))
1251 text_info text;
1252 VA_OPEN (ap, msgid);
1253 VA_FIXEDARG (ap, const char *, msgid);
1255 text.format_spec = _(msgid);
1256 text.args_ptr = &ap;
1257 output_do_verbatim (&global_dc->buffer, &text);
1258 output_buffer_to_stream (&global_dc->buffer);
1259 VA_CLOSE (ap);
1262 /* Report a diagnostic message (an error or a warning) as specified by
1263 DC. This function is *the* subroutine in terms of which front-ends
1264 should implement their specific diagnostic handling modules. The
1265 front-end independent format specifiers are exactly those described
1266 in the documentation of output_format. */
1268 void
1269 diagnostic_report_diagnostic (context, diagnostic)
1270 diagnostic_context *context;
1271 diagnostic_info *diagnostic;
1273 if (context->lock++)
1274 error_recursion (context);
1276 if (diagnostic_count_diagnostic (context, diagnostic->kind))
1278 (*diagnostic_starter (context)) (context, diagnostic);
1279 output_format (&context->buffer, &diagnostic->message);
1280 (*diagnostic_finalizer (context)) (context, diagnostic);
1281 output_flush (&context->buffer);
1284 --context->lock;
1287 /* Inform the user that an error occurred while trying to report some
1288 other error. This indicates catastrophic internal inconsistencies,
1289 so give up now. But do try to flush out the previous error.
1290 This mustn't use internal_error, that will cause infinite recursion. */
1292 static void
1293 error_recursion (context)
1294 diagnostic_context *context;
1296 if (context->lock < 3)
1297 output_flush (&context->buffer);
1299 fnotice (stderr,
1300 "Internal compiler error: Error reporting routines re-entered.\n");
1301 fnotice (stderr,
1302 "Please submit a full bug report,\n\
1303 with preprocessed source if appropriate.\n\
1304 See %s for instructions.\n", bug_report_url);
1305 exit (FATAL_EXIT_CODE);
1308 /* Given a partial pathname as input, return another pathname that
1309 shares no directory elements with the pathname of __FILE__. This
1310 is used by fancy_abort() to print `Internal compiler error in expr.c'
1311 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1313 const char *
1314 trim_filename (name)
1315 const char *name;
1317 static const char this_file[] = __FILE__;
1318 const char *p = name, *q = this_file;
1320 /* First skip any "../" in each filename. This allows us to give a proper
1321 reference to a file in a subdirectory. */
1322 while (p[0] == '.' && p[1] == '.'
1323 && (p[2] == DIR_SEPARATOR
1324 #ifdef DIR_SEPARATOR_2
1325 || p[2] == DIR_SEPARATOR_2
1326 #endif
1328 p += 3;
1330 while (q[0] == '.' && q[1] == '.'
1331 && (q[2] == DIR_SEPARATOR
1332 #ifdef DIR_SEPARATOR_2
1333 || p[2] == DIR_SEPARATOR_2
1334 #endif
1336 q += 3;
1338 /* Now skip any parts the two filenames have in common. */
1339 while (*p == *q && *p != 0 && *q != 0)
1340 p++, q++;
1342 /* Now go backwards until the previous directory separator. */
1343 while (p > name && p[-1] != DIR_SEPARATOR
1344 #ifdef DIR_SEPARATOR_2
1345 && p[-1] != DIR_SEPARATOR_2
1346 #endif
1348 p--;
1350 return p;
1353 /* Report an internal compiler error in a friendly manner and without
1354 dumping core. */
1356 void
1357 fancy_abort (file, line, function)
1358 const char *file;
1359 int line;
1360 const char *function;
1362 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1365 void
1366 diagnostic_report_current_module (context)
1367 diagnostic_context *context;
1369 struct file_stack *p;
1371 if (output_needs_newline (&context->buffer))
1373 output_add_newline (&context->buffer);
1374 output_needs_newline (&context->buffer) = false;
1377 if (input_file_stack && input_file_stack->next != 0
1378 && diagnostic_last_module_changed (context))
1380 for (p = input_file_stack->next; p; p = p->next)
1381 if (p == input_file_stack->next)
1382 output_verbatim (&context->buffer,
1383 "In file included from %s:%d", p->name, p->line);
1384 else
1385 output_verbatim (&context->buffer,
1386 ",\n from %s:%d", p->name, p->line);
1387 output_verbatim (&context->buffer, ":\n");
1388 diagnostic_set_last_module (context);
1392 static void
1393 default_diagnostic_starter (context, diagnostic)
1394 diagnostic_context *context;
1395 diagnostic_info *diagnostic;
1397 diagnostic_report_current_function (context);
1398 output_set_prefix (&context->buffer, diagnostic_build_prefix (diagnostic));
1401 static void
1402 default_diagnostic_finalizer (context, diagnostic)
1403 diagnostic_context *context;
1404 diagnostic_info *diagnostic __attribute__((unused));
1406 output_destroy_prefix (&context->buffer);
1409 void
1410 inform VPARAMS ((const char *msgid, ...))
1412 diagnostic_info diagnostic;
1414 VA_OPEN (ap, msgid);
1415 VA_FIXEDARG (ap, const char *, msgid);
1417 diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1418 DK_NOTE);
1419 report_diagnostic (&diagnostic);
1420 VA_CLOSE (ap);
1423 void
1424 warn_deprecated_use (node)
1425 tree node;
1427 if (node == 0 || !warn_deprecated_decl)
1428 return;
1430 if (DECL_P (node))
1431 warning ("`%s' is deprecated (declared at %s:%d)",
1432 IDENTIFIER_POINTER (DECL_NAME (node)),
1433 DECL_SOURCE_FILE (node), DECL_SOURCE_LINE (node));
1434 else if (TYPE_P (node))
1436 const char *what = NULL;
1437 tree decl = TYPE_STUB_DECL (node);
1439 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
1440 what = IDENTIFIER_POINTER (TYPE_NAME (node));
1441 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
1442 && DECL_NAME (TYPE_NAME (node)))
1443 what = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node)));
1445 if (what)
1447 if (decl)
1448 warning ("`%s' is deprecated (declared at %s:%d)", what,
1449 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
1450 else
1451 warning ("`%s' is deprecated", what);
1453 else if (decl)
1454 warning ("type is deprecated (declared at %s:%d)",
1455 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
1456 else
1457 warning ("type is deprecated");