OSDN Git Service

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