OSDN Git Service

* diagnostic.c (diagnostic_build_prefix): Fix initialization.
[pf3gnuchains/gcc-fork.git] / gcc / diagnostic.c
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>
4
5 This file is part of GCC.
6
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.
11
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.
16
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.  */
21
22
23 /* This file implements the language independent aspect of diagnostic
24    message module.  */
25
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"
39
40 #define obstack_chunk_alloc xmalloc
41 #define obstack_chunk_free  free
42
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)
50
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
55
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 *));
62
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 *));
70
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 *));
85
86 static void default_diagnostic_starter PARAMS ((diagnostic_context *,
87                                                 diagnostic_info *));
88 static void default_diagnostic_finalizer PARAMS ((diagnostic_context *,
89                                                   diagnostic_info *));
90
91 static void error_recursion PARAMS ((diagnostic_context *)) ATTRIBUTE_NORETURN;
92
93 extern int rtl_dump_and_exit;
94 extern int warnings_are_errors;
95
96 /* A diagnostic_context surrogate for stderr.  */
97 static diagnostic_context global_diagnostic_context;
98 diagnostic_context *global_dc = &global_diagnostic_context;
99
100 \f
101 /* Subroutine of output_set_maximum_length.  Set up BUFFER's
102    internal maximum characters per line.  */
103 static void
104 set_real_maximum_length (buffer)
105      output_buffer *buffer;
106 {
107   /* If we're told not to wrap lines then do the obvious thing.  In case
108    we'll emit prefix only once per diagnostic message, it is appropriate
109   not to increase unnecessarily the line-length cut-off.  */
110   if (!output_is_line_wrapping (buffer)
111       || output_prefixing_rule (buffer) == DIAGNOSTICS_SHOW_PREFIX_ONCE
112       || output_prefixing_rule (buffer) == DIAGNOSTICS_SHOW_PREFIX_NEVER)
113     line_wrap_cutoff (buffer) = output_line_cutoff (buffer);
114   else
115     {
116       int prefix_length = buffer->state.prefix ?
117         strlen (buffer->state.prefix) : 0;
118       /* If the prefix is ridiculously too long, output at least
119          32 characters.  */
120       if (output_line_cutoff (buffer) - prefix_length < 32)
121         line_wrap_cutoff (buffer) = output_line_cutoff (buffer) + 32;
122       else
123         line_wrap_cutoff (buffer) = output_line_cutoff (buffer);
124     }
125 }
126
127 /* Sets the number of maximum characters per line BUFFER can output
128    in line-wrapping mode.  A LENGTH value 0 suppresses line-wrapping.  */
129 void
130 output_set_maximum_length (buffer, length)
131      output_buffer *buffer;
132      int length;
133 {
134   output_line_cutoff (buffer) = length;
135   set_real_maximum_length (buffer);
136 }
137
138 /* Sets BUFFER's PREFIX.  */
139 void
140 output_set_prefix (buffer, prefix)
141      output_buffer *buffer;
142      const char *prefix;
143 {
144   buffer->state.prefix = prefix;
145   set_real_maximum_length (buffer);
146   prefix_was_emitted_for (buffer) = false;
147   output_indentation (buffer) = 0;
148 }
149
150 /*  Return a pointer to the last character emitted in the output
151     BUFFER area.  A NULL pointer means no character available.  */
152 const char *
153 output_last_position (buffer)
154      const output_buffer *buffer;
155 {
156   const char *p = NULL;
157
158   if (obstack_base (&buffer->obstack) != obstack_next_free (&buffer->obstack))
159     p = ((const char *) obstack_next_free (&buffer->obstack)) - 1;
160   return p;
161 }
162
163 /* Free BUFFER's prefix, a previously malloc'd string.  */
164 void
165 output_destroy_prefix (buffer)
166      output_buffer *buffer;
167 {
168   if (buffer->state.prefix != NULL)
169     {
170       free ((char *) buffer->state.prefix);
171       buffer->state.prefix = NULL;
172     }
173 }
174
175 /* Zero out any text output so far in BUFFER.  */
176 void
177 output_clear_message_text (buffer)
178      output_buffer *buffer;
179 {
180   obstack_free (&buffer->obstack, obstack_base (&buffer->obstack));
181   output_text_length (buffer) = 0;
182 }
183
184 /* Zero out any formatting data used so far by BUFFER.  */
185 static void
186 output_clear_data (buffer)
187      output_buffer *buffer;
188 {
189   prefix_was_emitted_for (buffer) = false;
190   output_indentation (buffer) = 0;
191 }
192
193 /* Construct an output BUFFER with PREFIX and of MAXIMUM_LENGTH
194    characters per line.  */
195 void
196 init_output_buffer (buffer, prefix, maximum_length)
197      output_buffer *buffer;
198      const char *prefix;
199      int maximum_length;
200 {
201   memset (buffer, 0, sizeof (output_buffer));
202   obstack_init (&buffer->obstack);
203   output_buffer_attached_stream (buffer) = stderr;
204   output_line_cutoff (buffer) = maximum_length;
205   output_prefixing_rule (buffer) = diagnostic_prefixing_rule (global_dc);
206   output_set_prefix (buffer, prefix);
207   output_text_length (buffer) = 0;
208   output_clear_data (buffer);
209 }
210
211 /* Reinitialize BUFFER.  */
212 void
213 output_clear (buffer)
214      output_buffer *buffer;
215 {
216   output_clear_message_text (buffer);
217   output_clear_data (buffer);
218 }
219
220 /* Finishes constructing a NULL-terminated character string representing
221    the BUFFERed message.  */
222 const char *
223 output_finalize_message (buffer)
224      output_buffer *buffer;
225 {
226   obstack_1grow (&buffer->obstack, '\0');
227   return output_message_text (buffer);
228 }
229
230 /* Return the amount of characters BUFFER can accept to
231    make a full line.  */
232 int
233 output_space_left (buffer)
234      const output_buffer *buffer;
235 {
236   return line_wrap_cutoff (buffer) - output_text_length (buffer);
237 }
238
239 /* Write out BUFFER's prefix.  */
240 void
241 output_emit_prefix (buffer)
242      output_buffer *buffer;
243 {
244   if (buffer->state.prefix != NULL)
245     {
246       switch (output_prefixing_rule (buffer))
247         {
248         default:
249         case DIAGNOSTICS_SHOW_PREFIX_NEVER:
250           break;
251
252         case DIAGNOSTICS_SHOW_PREFIX_ONCE:
253           if (prefix_was_emitted_for (buffer))
254             {
255               output_indent (buffer);
256               break;
257             }
258           output_indentation (buffer) += 3;
259           /* Fall through.  */
260
261         case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
262           {
263             int prefix_length = strlen (buffer->state.prefix);
264             output_append_r (buffer, buffer->state.prefix, prefix_length);
265             prefix_was_emitted_for (buffer) = true;
266           }
267           break;
268         }
269     }
270 }
271
272 /* Have BUFFER start a new line.  */
273 void
274 output_add_newline (buffer)
275      output_buffer *buffer;
276 {
277   obstack_1grow (&buffer->obstack, '\n');
278   output_text_length (buffer) = 0;
279 }
280
281 /* Appends a character to BUFFER.  */
282 void
283 output_add_character (buffer, c)
284      output_buffer *buffer;
285      int c;
286 {
287   if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
288     output_add_newline (buffer);
289   obstack_1grow (&buffer->obstack, c);
290   ++output_text_length (buffer);
291 }
292
293 /* Adds a space to BUFFER.  */
294 void
295 output_add_space (buffer)
296      output_buffer *buffer;
297 {
298   if (output_is_line_wrapping (buffer) && output_space_left (buffer) <= 0)
299     {
300       output_add_newline (buffer);
301       return;
302     }
303   obstack_1grow (&buffer->obstack, ' ');
304   ++output_text_length (buffer);
305 }
306
307 /* These functions format an INTEGER into BUFFER as suggested by their
308    names.  */
309 void
310 output_decimal (buffer, i)
311      output_buffer *buffer;
312      int i;
313 {
314   output_formatted_integer (buffer, "%d", i);
315 }
316
317 static void
318 output_long_decimal (buffer, i)
319      output_buffer *buffer;
320      long int i;
321 {
322   output_formatted_integer (buffer, "%ld", i);
323 }
324
325 static void
326 output_unsigned_decimal (buffer, i)
327      output_buffer *buffer;
328      unsigned int i;
329 {
330   output_formatted_integer (buffer, "%u", i);
331 }
332
333 static void
334 output_long_unsigned_decimal (buffer, i)
335      output_buffer *buffer;
336      long unsigned int i;
337 {
338   output_formatted_integer (buffer, "%lu", i);
339 }
340
341 static void
342 output_octal (buffer, i)
343      output_buffer *buffer;
344      unsigned int i;
345 {
346   output_formatted_integer (buffer, "%o", i);
347 }
348
349 static void
350 output_long_octal (buffer, i)
351      output_buffer *buffer;
352      unsigned long int i;
353 {
354   output_formatted_integer (buffer, "%lo", i);
355 }
356
357 static void
358 output_hexadecimal (buffer, i)
359      output_buffer *buffer;
360      unsigned int i;
361 {
362   output_formatted_integer (buffer, "%x", i);
363 }
364
365 static void
366 output_long_hexadecimal (buffer, i)
367      output_buffer *buffer;
368      unsigned long int i;
369 {
370   output_formatted_integer (buffer, "%lx", i);
371 }
372
373 /* Append to BUFFER a string specified by its STARTING character
374    and LENGTH.  */
375 static void
376 output_append_r (buffer, start, length)
377      output_buffer *buffer;
378      const char *start;
379      int length;
380 {
381   obstack_grow (&buffer->obstack, start, length);
382   output_text_length (buffer) += length;
383 }
384
385 /* Append a string deliminated by START and END to BUFFER.  No wrapping is
386    done.  However, if beginning a new line then emit BUFFER->state.prefix
387    and skip any leading whitespace if appropriate.  The caller must ensure
388    that it is safe to do so.  */
389 void
390 output_append (buffer, start, end)
391      output_buffer *buffer;
392      const char *start;
393      const char *end;
394 {
395   /* Emit prefix and skip whitespace if we're starting a new line.  */
396   if (is_starting_newline (buffer))
397     {
398       output_emit_prefix (buffer);
399       if (output_is_line_wrapping (buffer))
400         while (start != end && *start == ' ')
401           ++start;
402     }
403   output_append_r (buffer, start, end - start);
404 }
405
406 static void
407 output_indent (buffer)
408      output_buffer *buffer;
409 {
410   int n = output_indentation (buffer);
411   int i;
412
413   for (i = 0; i < n; ++i)
414     output_add_character (buffer, ' ');
415 }
416
417 /* Wrap a text delimited by START and END into BUFFER.  */
418 static void
419 wrap_text (buffer, start, end)
420      output_buffer *buffer;
421      const char *start;
422      const char *end;
423 {
424   bool is_wrapping = output_is_line_wrapping (buffer);
425
426   while (start != end)
427     {
428       /* Dump anything bordered by whitespaces.  */
429       {
430         const char *p = start;
431         while (p != end && *p != ' ' && *p != '\n')
432           ++p;
433         if (is_wrapping && p - start >= output_space_left (buffer))
434           output_add_newline (buffer);
435         output_append (buffer, start, p);
436         start = p;
437       }
438
439       if (start != end && *start == ' ')
440         {
441           output_add_space (buffer);
442           ++start;
443         }
444       if (start != end && *start == '\n')
445         {
446           output_add_newline (buffer);
447           ++start;
448         }
449     }
450 }
451
452 /* Same as wrap_text but wrap text only when in line-wrapping mode.  */
453 static void
454 maybe_wrap_text (buffer, start, end)
455      output_buffer *buffer;
456      const char *start;
457      const char *end;
458 {
459   if (output_is_line_wrapping (buffer))
460     wrap_text (buffer, start, end);
461   else
462     output_append (buffer, start, end);
463 }
464
465
466 /* Append a STRING to BUFFER; the STRING might be line-wrapped if in
467    appropriate mode.  */
468 void
469 output_add_string (buffer, str)
470      output_buffer *buffer;
471      const char *str;
472 {
473   maybe_wrap_text (buffer, str, str + (str ? strlen (str) : 0));
474 }
475
476 /* Flush the content of BUFFER onto the attached stream,
477    and reinitialize.  */
478
479 static void
480 output_buffer_to_stream (buffer)
481      output_buffer *buffer;
482 {
483   const char *text = output_finalize_message (buffer);
484   fputs (text, output_buffer_attached_stream (buffer));
485   output_clear_message_text (buffer);
486 }
487
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 static void
500 output_format (buffer, text)
501      output_buffer *buffer;
502      text_info *text;
503 {
504   for (; *text->format_spec; ++text->format_spec)
505     {
506       bool long_integer = 0;
507
508       /* Ignore text.  */
509       {
510         const char *p = text->format_spec;
511         while (*p && *p != '%')
512           ++p;
513         wrap_text (buffer, text->format_spec, p);
514         text->format_spec = p;
515       }
516
517       if (*text->format_spec == '\0')
518         break;
519
520       /* We got a '%'.  Let's see what happens. Record whether we're
521          parsing a long integer format specifier.  */
522       if (*++text->format_spec == 'l')
523         {
524           long_integer = true;
525           ++text->format_spec;
526         }
527
528       /* Handle %c, %d, %i, %ld, %li, %lo, %lu, %lx, %o, %s, %u,
529          %x, %.*s; %%.  And nothing else.  Front-ends should install
530          printers to grok language specific format specifiers.  */
531       switch (*text->format_spec)
532         {
533         case 'c':
534           output_add_character (buffer, va_arg (*text->args_ptr, int));
535           break;
536
537         case 'd':
538         case 'i':
539           if (long_integer)
540             output_long_decimal (buffer, va_arg (*text->args_ptr, long int));
541           else
542             output_decimal (buffer, va_arg (*text->args_ptr, int));
543           break;
544
545         case 'o':
546           if (long_integer)
547             output_long_octal (buffer,
548                                va_arg (*text->args_ptr, unsigned long int));
549           else
550             output_octal (buffer, va_arg (*text->args_ptr, unsigned int));
551           break;
552
553         case 's':
554           output_add_string (buffer, va_arg (*text->args_ptr, const char *));
555           break;
556
557         case 'u':
558           if (long_integer)
559             output_long_unsigned_decimal
560               (buffer, va_arg (*text->args_ptr, long unsigned int));
561           else
562             output_unsigned_decimal
563               (buffer, va_arg (*text->args_ptr, unsigned int));
564           break;
565
566         case 'x':
567           if (long_integer)
568             output_long_hexadecimal
569               (buffer, va_arg (*text->args_ptr, unsigned long int));
570           else
571             output_hexadecimal
572               (buffer, va_arg (*text->args_ptr, unsigned int));
573           break;
574
575         case '%':
576           output_add_character (buffer, '%');
577           break;
578
579         case '.':
580           {
581             int n;
582             const char *s;
583             /* We handle no precision specifier but `%.*s'.  */
584             if (*++text->format_spec != '*')
585               abort ();
586             else if (*++text->format_spec != 's')
587               abort ();
588             n = va_arg (*text->args_ptr, int);
589             s = va_arg (*text->args_ptr, const char *);
590             output_append (buffer, s, s + n);
591           }
592           break;
593
594         default:
595           if (!buffer->format_decoder
596               || !(*buffer->format_decoder) (buffer, text))
597             {
598               /* Hmmm.  The front-end failed to install a format translator
599                  but called us with an unrecognized format.  Sorry.  */
600               abort ();
601             }
602         }
603     }
604 }
605
606 static char *
607 vbuild_message_string (msg, ap)
608      const char *msg;
609      va_list ap;
610 {
611   char *str;
612
613   vasprintf (&str, msg, ap);
614   return str;
615 }
616
617 /*  Return a malloc'd string containing MSG formatted a la
618     printf.  The caller is responsible for freeing the memory.  */
619 static char *
620 build_message_string VPARAMS ((const char *msg, ...))
621 {
622   char *str;
623
624   VA_OPEN (ap, msg);
625   VA_FIXEDARG (ap, const char *, msg);
626
627   str = vbuild_message_string (msg, ap);
628
629   VA_CLOSE (ap);
630
631   return str;
632 }
633
634 /* Same as diagnsotic_build_prefix, but only the source FILE is given.  */
635 char *
636 file_name_as_prefix (f)
637      const char *f;
638 {
639   return build_message_string ("%s: ", f);
640 }
641
642 /* Format a message into BUFFER a la printf.  */
643 void
644 output_printf VPARAMS ((struct output_buffer *buffer, const char *msgid, ...))
645 {
646   text_info text;
647   VA_OPEN (ap, msgid);
648   VA_FIXEDARG (ap, output_buffer *, buffer);
649   VA_FIXEDARG (ap, const char *, msgid);
650
651   text.args_ptr = &ap;
652   text.format_spec = _(msgid);
653   output_format (buffer, &text);
654   VA_CLOSE (ap);
655 }
656
657 /* Print a message relevant to the given DECL.  */
658 static void
659 format_with_decl (buffer, text, decl)
660      output_buffer *buffer;
661      text_info *text;
662      tree decl;
663 {
664   const char *p;
665
666   /* Do magic to get around lack of varargs support for insertion
667      of arguments into existing list.  We know that the decl is first;
668      we ass_u_me that it will be printed with "%s".  */
669   for (p = text->format_spec; *p; ++p)
670     {
671       if (*p == '%')
672         {
673           if (*(p + 1) == '%')
674             ++p;
675           else if (*(p + 1) != 's')
676             abort ();
677           else
678             break;
679         }
680     }
681
682   /* Print the left-hand substring.  */
683   maybe_wrap_text (buffer, text->format_spec, p);
684
685   if (*p == '%')                /* Print the name.  */
686     {
687       const char *const n = (DECL_NAME (decl)
688                              ? (*lang_hooks.decl_printable_name) (decl, 2)
689                              : _("((anonymous))"));
690       output_add_string (buffer, n);
691       while (*p)
692         {
693           ++p;
694           if (ISALPHA (*(p - 1) & 0xFF))
695             break;
696         }
697     }
698
699   if (*p)                       /* Print the rest of the message.  */
700     {
701       text->format_spec = p;
702       output_format (buffer, text);
703     }
704 }
705
706 /* Flush the content of BUFFER onto the attached stream.  */
707 static void
708 output_flush (buffer)
709      output_buffer *buffer;
710 {
711   output_buffer_to_stream (buffer);
712   output_clear_data (buffer);
713   fputc ('\n', output_buffer_attached_stream (buffer));
714   fflush (output_buffer_attached_stream (buffer));
715 }
716
717 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
718    settings needed by BUFFER for a verbatim formatting.  */
719 static void
720 output_do_verbatim (buffer, text)
721      output_buffer *buffer;
722      text_info *text;
723 {
724   diagnostic_prefixing_rule_t rule = output_prefixing_rule (buffer);
725   int line_cutoff = output_line_cutoff (buffer);
726
727   /* Set verbatim mode.  */
728   output_prefixing_rule (buffer) = DIAGNOSTICS_SHOW_PREFIX_NEVER;
729   output_line_cutoff (buffer) = 0;
730   /* Do the actual formatting.  */
731   output_format (buffer, text);
732   /* Restore previous settings.  */
733   output_prefixing_rule (buffer) = rule;
734   output_line_cutoff (buffer) = line_cutoff;
735 }
736
737 /* Output MESSAGE verbatim into BUFFER.  */
738 void
739 output_verbatim VPARAMS ((output_buffer *buffer, const char *msgid, ...))
740 {
741   text_info text;
742   VA_OPEN (ap, msgid);
743   VA_FIXEDARG (ap, output_buffer *, buffer);
744   VA_FIXEDARG (ap, const char *, msgid);
745
746   text.format_spec = msgid;
747   text.args_ptr = &ap;
748   output_do_verbatim (buffer, &text);
749   VA_CLOSE (ap);
750 }
751
752 \f
753 /* Initialize the diagnostic message outputting machinery.  */
754 void
755 diagnostic_initialize (context)
756      diagnostic_context *context;
757 {
758   memset (context, 0, sizeof *context);
759   obstack_init (&context->buffer.obstack);
760
761   /* By default, diagnostics are sent to stderr.  */
762   output_buffer_attached_stream (&context->buffer) = stderr;
763
764   /* By default, we emit prefixes once per message.  */
765   diagnostic_prefixing_rule (context) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
766
767   diagnostic_starter (context) = default_diagnostic_starter;
768   diagnostic_finalizer (context) = default_diagnostic_finalizer;
769 }
770
771 void
772 diagnostic_set_info (diagnostic, msgid, args, file, line, kind)
773      diagnostic_info *diagnostic;
774      const char *msgid;
775      va_list *args;
776      const char *file;
777      int line;
778      diagnostic_t kind;
779 {
780   diagnostic->message.format_spec = msgid;
781   diagnostic->message.args_ptr = args;
782   diagnostic->location.file = file;
783   diagnostic->location.line = line;
784   diagnostic->kind = kind;
785 }
786
787 /* Return a malloc'd string describing a location.  The caller is
788    responsible for freeing the memory.  */
789 char *
790 diagnostic_build_prefix (diagnostic)
791      diagnostic_info *diagnostic;
792 {
793   static const char *diagnostic_kind_text[] = {
794 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
795 #include "diagnostic.def"
796 #undef DEFINE_DIAGNOSTIC_KIND
797     "must-not-happen"
798   };
799    if (diagnostic->kind >= DK_LAST_DIAGNOSTIC_KIND)
800      abort();
801
802   return diagnostic->location.file
803     ? build_message_string ("%s:%d: %s",
804                             diagnostic->location.file,
805                             diagnostic->location.line,
806                             _(diagnostic_kind_text[diagnostic->kind]))
807     : build_message_string ("%s: %s", progname,
808                             _(diagnostic_kind_text[diagnostic->kind]));
809 }
810
811 /* Report a diagnostic MESSAGE at the declaration DECL.
812    MSG is a format string which uses %s to substitute the declaration
813    name; subsequent substitutions are a la output_format.  */
814 static void
815 diagnostic_for_decl (diagnostic, decl)
816      diagnostic_info *diagnostic;
817      tree decl;
818 {
819   if (global_dc->lock++)
820     error_recursion (global_dc);
821
822   if (diagnostic_count_error (global_dc, diagnostic->kind))
823     {
824       diagnostic_report_current_function (global_dc);
825       output_set_prefix
826         (&global_dc->buffer, diagnostic_build_prefix (diagnostic));
827       format_with_decl (&global_dc->buffer, &diagnostic->message, decl);
828       output_flush (&global_dc->buffer);
829       output_destroy_prefix (&global_dc->buffer);
830     }
831   global_dc->lock--;
832 }
833
834 void
835 diagnostic_flush_buffer (context)
836      diagnostic_context *context;
837 {
838   output_buffer_to_stream (&context->buffer);
839   fflush (output_buffer_attached_stream (&context->buffer));
840 }
841
842 /* Count an error or warning.  Return true if the message should be
843    printed.  */
844 bool
845 diagnostic_count_error (context, kind)
846     diagnostic_context *context;
847     diagnostic_t kind;
848 {
849   if (kind == DK_WARNING && !diagnostic_report_warnings_p ())
850     return false;
851
852   if (kind == DK_WARNING && !warnings_are_errors)
853     ++diagnostic_kind_count (context, DK_WARNING);
854   else
855     {
856       static bool warning_message = false;
857
858       if (kind == DK_WARNING && !warning_message)
859         {
860           output_verbatim (&context->buffer,
861                            "%s: warnings being treated as errors\n", progname);
862           warning_message = true;
863         }
864       ++diagnostic_kind_count (context, DK_ERROR);
865     }
866
867   return true;
868 }
869
870 /* Print a diagnostic MSGID on FILE.  This is just fprintf, except it
871    runs its second argument through gettext.  */
872 void
873 fnotice VPARAMS ((FILE *file, const char *msgid, ...))
874 {
875   VA_OPEN (ap, msgid);
876   VA_FIXEDARG (ap, FILE *, file);
877   VA_FIXEDARG (ap, const char *, msgid);
878
879   vfprintf (file, _(msgid), ap);
880   VA_CLOSE (ap);
881 }
882
883
884 /* Print a fatal I/O error message.  Argument are like printf.
885    Also include a system error message based on `errno'.  */
886 void
887 fatal_io_error VPARAMS ((const char *msgid, ...))
888 {
889   text_info text;
890   VA_OPEN (ap, msgid);
891   VA_FIXEDARG (ap, const char *, msgid);
892
893   text.format_spec = _(msgid);
894   text.args_ptr = &ap;
895   output_printf (&global_dc->buffer, "%s: %s: ", progname, xstrerror (errno));
896   output_format (&global_dc->buffer, &text);
897   output_flush (&global_dc->buffer);
898   VA_CLOSE (ap);
899   exit (FATAL_EXIT_CODE);
900 }
901
902 /* Issue a pedantic warning MSGID.  */
903 void
904 pedwarn VPARAMS ((const char *msgid, ...))
905 {
906   diagnostic_info diagnostic;
907   VA_OPEN (ap, msgid);
908   VA_FIXEDARG (ap, const char *, msgid);
909
910   diagnostic_set_info (&diagnostic, _(msgid), &ap, input_filename, lineno,
911                        pedantic_error_kind ());
912   report_diagnostic (&diagnostic);
913   VA_CLOSE (ap);
914 }
915
916 /* Issue a pedantic warning about DECL.  */
917 void
918 pedwarn_with_decl VPARAMS ((tree decl, const char *msgid, ...))
919 {
920   diagnostic_info diagnostic;
921   VA_OPEN (ap, msgid);
922   VA_FIXEDARG (ap, tree, decl);
923   VA_FIXEDARG (ap, const char *, msgid);
924
925   diagnostic_set_info (&diagnostic, _(msgid), &ap,
926                        DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
927                        pedantic_error_kind ());
928
929   /* We don't want -pedantic-errors to cause the compilation to fail from
930      "errors" in system header files.  Sometimes fixincludes can't fix what's
931      broken (eg: unsigned char bitfields - fixing it may change the alignment
932      which will cause programs to mysteriously fail because the C library
933      or kernel uses the original layout).  There's no point in issuing a
934      warning either, it's just unnecessary noise.  */
935   if (!DECL_IN_SYSTEM_HEADER (decl))
936     diagnostic_for_decl (&diagnostic, decl);
937   VA_CLOSE (ap);
938 }
939
940 /* Same as above but within the context FILE and LINE.  */
941 void
942 pedwarn_with_file_and_line VPARAMS ((const char *file, int line,
943                                      const char *msgid, ...))
944 {
945   diagnostic_info diagnostic;
946   VA_OPEN (ap, msgid);
947   VA_FIXEDARG (ap, const char *, file);
948   VA_FIXEDARG (ap, int, line);
949   VA_FIXEDARG (ap, const char *, msgid);
950
951   diagnostic_set_info (&diagnostic, _(msgid), &ap, file, line,
952                        pedantic_error_kind ());
953   report_diagnostic (&diagnostic);
954   VA_CLOSE (ap);
955 }
956
957 /* Just apologize with MSGID.  */
958 void
959 sorry VPARAMS ((const char *msgid, ...))
960 {
961   diagnostic_info diagnostic;
962
963   VA_OPEN (ap, msgid);
964   VA_FIXEDARG (ap, const char *, msgid);
965
966   ++sorrycount;
967   diagnostic_set_info (&diagnostic, _(msgid), &ap,
968                        input_filename, lineno, DK_SORRY);
969
970   output_set_prefix
971     (&global_dc->buffer, diagnostic_build_prefix (&diagnostic));
972   output_printf (&global_dc->buffer, "sorry, not implemented: ");
973   output_format (&global_dc->buffer, &diagnostic.message);
974   output_flush (&global_dc->buffer);
975   VA_CLOSE (ap);
976 }
977
978 /* Called when the start of a function definition is parsed,
979    this function prints on stderr the name of the function.  */
980 void
981 announce_function (decl)
982      tree decl;
983 {
984   if (!quiet_flag)
985     {
986       if (rtl_dump_and_exit)
987         verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl)));
988       else
989         verbatim (" %s", (*lang_hooks.decl_printable_name) (decl, 2));
990       fflush (stderr);
991       output_needs_newline (&global_dc->buffer) = true;
992       diagnostic_set_last_function (global_dc);
993     }
994 }
995
996 /* The default function to print out name of current function that caused
997    an error.  */
998 void
999 lhd_print_error_function (context, file)
1000      diagnostic_context *context;
1001      const char *file;
1002 {
1003   if (diagnostic_last_function_changed (context))
1004     {
1005       const char *old_prefix = output_prefix (&context->buffer);
1006       char *new_prefix = file ? build_message_string ("%s: ", file) : NULL;
1007
1008       output_set_prefix (&context->buffer, new_prefix);
1009
1010       if (current_function_decl == NULL)
1011         output_add_string (&context->buffer, _("At top level:"));
1012       else
1013         {
1014           if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
1015             output_printf
1016               (&context->buffer, "In member function `%s':",
1017                (*lang_hooks.decl_printable_name) (current_function_decl, 2));
1018           else
1019             output_printf
1020               (&context->buffer, "In function `%s':",
1021                (*lang_hooks.decl_printable_name) (current_function_decl, 2));
1022         }
1023       output_add_newline (&context->buffer);
1024
1025       diagnostic_set_last_function (context);
1026       output_buffer_to_stream (&context->buffer);
1027       context->buffer.state.prefix = old_prefix;
1028       free ((char*) new_prefix);
1029     }
1030 }
1031
1032 /* Prints out, if necessary, the name of the current function
1033   that caused an error.  Called from all error and warning functions.
1034   We ignore the FILE parameter, as it cannot be relied upon.  */
1035
1036 void
1037 diagnostic_report_current_function (context)
1038      diagnostic_context *context;
1039 {
1040   diagnostic_report_current_module (context);
1041   (*lang_hooks.print_error_function) (context, input_filename);
1042 }
1043
1044 void
1045 error_with_file_and_line VPARAMS ((const char *file, int line,
1046                                    const char *msgid, ...))
1047 {
1048   diagnostic_info diagnostic;
1049
1050   VA_OPEN (ap, msgid);
1051   VA_FIXEDARG (ap, const char *, file);
1052   VA_FIXEDARG (ap, int, line);
1053   VA_FIXEDARG (ap, const char *, msgid);
1054
1055   diagnostic_set_info (&diagnostic, msgid, &ap, file, line, DK_ERROR);
1056   report_diagnostic (&diagnostic);
1057   VA_CLOSE (ap);
1058 }
1059
1060 void
1061 error_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1062 {
1063   diagnostic_info diagnostic;
1064   VA_OPEN (ap, msgid);
1065   VA_FIXEDARG (ap, tree, decl);
1066   VA_FIXEDARG (ap, const char *, msgid);
1067
1068   diagnostic_set_info (&diagnostic, msgid, &ap,
1069                        DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1070                        DK_ERROR);
1071   diagnostic_for_decl (&diagnostic, decl);
1072   VA_CLOSE (ap);
1073 }
1074
1075
1076 /* Report an error message.  The arguments are like that of printf.  */
1077
1078 void
1079 error VPARAMS ((const char *msgid, ...))
1080 {
1081   diagnostic_info diagnostic;
1082
1083   VA_OPEN (ap, msgid);
1084   VA_FIXEDARG (ap, const char *, msgid);
1085
1086   diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1087                        DK_ERROR);
1088   report_diagnostic (&diagnostic);
1089   VA_CLOSE (ap);
1090 }
1091
1092 /* Likewise, except that the compilation is terminated after printing the
1093    error message.  */
1094
1095 void
1096 fatal_error VPARAMS ((const char *msgid, ...))
1097 {
1098   diagnostic_info diagnostic;
1099
1100   VA_OPEN (ap, msgid);
1101   VA_FIXEDARG (ap, const char *, msgid);
1102
1103   diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1104                        DK_FATAL);
1105   report_diagnostic (&diagnostic);
1106   VA_CLOSE (ap);
1107
1108   fnotice (stderr, "compilation terminated.\n");
1109   exit (FATAL_EXIT_CODE);
1110 }
1111
1112 void
1113 internal_error VPARAMS ((const char *msgid, ...))
1114 {
1115   diagnostic_info diagnostic;
1116
1117   VA_OPEN (ap, msgid);
1118   VA_FIXEDARG (ap, const char *, msgid);
1119
1120   if (global_dc->lock)
1121     error_recursion (global_dc);
1122
1123 #ifndef ENABLE_CHECKING
1124   if (errorcount > 0 || sorrycount > 0)
1125     {
1126       fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
1127                input_filename, lineno);
1128       exit (FATAL_EXIT_CODE);
1129     }
1130 #endif
1131
1132   if (global_dc->internal_error != 0)
1133     (*global_dc->internal_error) (_(msgid), &ap);
1134
1135   diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1136                        DK_ICE);
1137   report_diagnostic (&diagnostic);
1138   VA_CLOSE (ap);
1139
1140   fnotice (stderr,
1141 "Please submit a full bug report,\n\
1142 with preprocessed source if appropriate.\n\
1143 See %s for instructions.\n", GCCBUGURL);
1144   exit (FATAL_EXIT_CODE);
1145 }
1146
1147 void
1148 warning_with_file_and_line VPARAMS ((const char *file, int line,
1149                                      const char *msgid, ...))
1150 {
1151   diagnostic_info diagnostic;
1152
1153   VA_OPEN (ap, msgid);
1154   VA_FIXEDARG (ap, const char *, file);
1155   VA_FIXEDARG (ap, int, line);
1156   VA_FIXEDARG (ap, const char *, msgid);
1157
1158   diagnostic_set_info (&diagnostic, msgid, &ap, file, line, DK_WARNING);
1159   report_diagnostic (&diagnostic);
1160   VA_CLOSE (ap);
1161 }
1162
1163 void
1164 warning_with_decl VPARAMS ((tree decl, const char *msgid, ...))
1165 {
1166   diagnostic_info diagnostic;
1167   VA_OPEN (ap, msgid);
1168   VA_FIXEDARG (ap, tree, decl);
1169   VA_FIXEDARG (ap, const char *, msgid);
1170
1171   diagnostic_set_info (&diagnostic, msgid, &ap,
1172                        DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1173                        DK_WARNING);
1174   diagnostic_for_decl (&diagnostic, decl);
1175   VA_CLOSE (ap);
1176 }
1177
1178 void
1179 warning VPARAMS ((const char *msgid, ...))
1180 {
1181   diagnostic_info diagnostic;
1182
1183   VA_OPEN (ap, msgid);
1184   VA_FIXEDARG (ap, const char *, msgid);
1185
1186   diagnostic_set_info (&diagnostic, msgid, &ap, input_filename, lineno,
1187                        DK_WARNING);
1188   report_diagnostic (&diagnostic);
1189   VA_CLOSE (ap);
1190 }
1191
1192
1193 /* Same as above but use diagnostic_buffer.  */
1194
1195 void
1196 verbatim VPARAMS ((const char *msgid, ...))
1197 {
1198   text_info text;
1199   VA_OPEN (ap, msgid);
1200   VA_FIXEDARG (ap, const char *, msgid);
1201
1202   text.format_spec = _(msgid);
1203   text.args_ptr = &ap;
1204   output_do_verbatim (&global_dc->buffer, &text);
1205   output_buffer_to_stream (&global_dc->buffer);
1206   VA_CLOSE (ap);
1207 }
1208
1209 /* Report a diagnostic message (an error or a warning) as specified by
1210    DC.  This function is *the* subroutine in terms of which front-ends
1211    should implement their specific diagnostic handling modules.  The
1212    front-end independent format specifiers are exactly those described
1213    in the documentation of output_format.  */
1214
1215 void
1216 diagnostic_report_diagnostic (context, diagnostic)
1217      diagnostic_context *context;
1218      diagnostic_info *diagnostic;
1219 {
1220   if (context->lock++)
1221     error_recursion (context);
1222
1223   if (diagnostic_count_error (context, diagnostic->kind))
1224     {
1225       (*diagnostic_starter (context)) (context, diagnostic);
1226       output_format (&context->buffer, &diagnostic->message);
1227       (*diagnostic_finalizer (context)) (context, diagnostic);
1228       output_flush (&context->buffer);
1229     }
1230
1231   --context->lock;
1232 }
1233
1234 /* Inform the user that an error occurred while trying to report some
1235    other error.  This indicates catastrophic internal inconsistencies,
1236    so give up now.  But do try to flush out the previous error.
1237    This mustn't use internal_error, that will cause infinite recursion.  */
1238
1239 static void
1240 error_recursion (context)
1241      diagnostic_context *context;
1242 {
1243   if (context->lock < 3)
1244     output_flush (&context->buffer);
1245
1246   fnotice (stderr,
1247            "Internal compiler error: Error reporting routines re-entered.\n");
1248   fnotice (stderr,
1249 "Please submit a full bug report,\n\
1250 with preprocessed source if appropriate.\n\
1251 See %s for instructions.\n", GCCBUGURL);
1252   exit (FATAL_EXIT_CODE);
1253 }
1254
1255 /* Given a partial pathname as input, return another pathname that
1256    shares no directory elements with the pathname of __FILE__.  This
1257    is used by fancy_abort() to print `Internal compiler error in expr.c'
1258    instead of `Internal compiler error in ../../GCC/gcc/expr.c'.  */
1259
1260 const char *
1261 trim_filename (name)
1262      const char *name;
1263 {
1264   static const char this_file[] = __FILE__;
1265   const char *p = name, *q = this_file;
1266
1267   /* First skip any "../" in each filename.  This allows us to give a proper
1268      reference to a file in a subdirectory.  */
1269   while (p[0] == '.' && p[1] == '.'
1270          && (p[2] == DIR_SEPARATOR
1271 #ifdef DIR_SEPARATOR_2
1272              || p[2] == DIR_SEPARATOR_2
1273 #endif
1274              ))
1275     p += 3;
1276
1277   while (q[0] == '.' && q[1] == '.'
1278          && (q[2] == DIR_SEPARATOR
1279 #ifdef DIR_SEPARATOR_2
1280              || p[2] == DIR_SEPARATOR_2
1281 #endif
1282              ))
1283     q += 3;
1284
1285   /* Now skip any parts the two filenames have in common.  */
1286   while (*p == *q && *p != 0 && *q != 0)
1287     p++, q++;
1288
1289   /* Now go backwards until the previous directory separator.  */
1290   while (p > name && p[-1] != DIR_SEPARATOR
1291 #ifdef DIR_SEPARATOR_2
1292          && p[-1] != DIR_SEPARATOR_2
1293 #endif
1294          )
1295     p--;
1296
1297   return p;
1298 }
1299
1300 /* Report an internal compiler error in a friendly manner and without
1301    dumping core.  */
1302
1303 void
1304 fancy_abort (file, line, function)
1305      const char *file;
1306      int line;
1307      const char *function;
1308 {
1309   internal_error ("Internal compiler error in %s, at %s:%d",
1310                   function, trim_filename (file), line);
1311 }
1312
1313 void
1314 diagnostic_report_current_module (context)
1315      diagnostic_context *context;
1316 {
1317   struct file_stack *p;
1318
1319   if (output_needs_newline (&context->buffer))
1320     {
1321       output_add_newline (&context->buffer);
1322       output_needs_newline (&context->buffer) = false;
1323     }
1324
1325   if (input_file_stack && input_file_stack->next != 0
1326       && diagnostic_last_module_changed (context))
1327     {
1328       for (p = input_file_stack->next; p; p = p->next)
1329         if (p == input_file_stack->next)
1330           output_verbatim (&context->buffer,
1331                            "In file included from %s:%d", p->name, p->line);
1332         else
1333           output_verbatim (&context->buffer,
1334                            ",\n                 from %s:%d", p->name, p->line);
1335       output_verbatim (&context->buffer, ":\n");
1336       diagnostic_set_last_module (context);
1337     }
1338 }
1339
1340 static void
1341 default_diagnostic_starter (context, diagnostic)
1342      diagnostic_context *context;
1343      diagnostic_info *diagnostic;
1344 {
1345   diagnostic_report_current_function (context);
1346   output_set_prefix (&context->buffer, diagnostic_build_prefix (diagnostic));
1347 }
1348
1349 static void
1350 default_diagnostic_finalizer (context, diagnostic)
1351      diagnostic_context *context;
1352      diagnostic_info *diagnostic __attribute__((unused));
1353 {
1354   output_destroy_prefix (&context->buffer);
1355 }
1356
1357 void
1358 warn_deprecated_use (node)
1359      tree node;
1360 {
1361   if (node == 0 || !warn_deprecated_decl)
1362     return;
1363
1364   if (DECL_P (node))
1365     warning ("`%s' is deprecated (declared at %s:%d)",
1366              IDENTIFIER_POINTER (DECL_NAME (node)),
1367              DECL_SOURCE_FILE (node), DECL_SOURCE_LINE (node));
1368   else if (TYPE_P (node))
1369     {
1370       const char *what = NULL;
1371       tree decl = TYPE_STUB_DECL (node);
1372
1373       if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
1374         what = IDENTIFIER_POINTER (TYPE_NAME (node));
1375       else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
1376                && DECL_NAME (TYPE_NAME (node)))
1377         what = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node)));
1378
1379       if (what)
1380         {
1381           if (decl)
1382             warning ("`%s' is deprecated (declared at %s:%d)", what,
1383                      DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
1384           else
1385             warning ("`%s' is deprecated", what);
1386         }
1387       else if (decl)
1388         warning ("type is deprecated (declared at %s:%d)",
1389                  DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
1390       else
1391         warning ("type is deprecated");
1392     }
1393 }