OSDN Git Service

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