OSDN Git Service

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