OSDN Git Service

05f153387d7120d5693b047fea560892b6ad49c7
[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, 2004, 2005, 2006, 2007, 2008
3    Free Software Foundation, Inc.
4    Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
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 "version.h"
34 #include "tm_p.h"
35 #include "flags.h"
36 #include "input.h"
37 #include "toplev.h"
38 #include "intl.h"
39 #include "diagnostic.h"
40 #include "langhooks.h"
41 #include "langhooks-def.h"
42 #include "opts.h"
43
44
45 /* Prototypes.  */
46 static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
47
48 static void default_diagnostic_starter (diagnostic_context *,
49                                         diagnostic_info *);
50 static void default_diagnostic_finalizer (diagnostic_context *,
51                                           diagnostic_info *);
52
53 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
54 static bool diagnostic_count_diagnostic (diagnostic_context *,
55                                          diagnostic_info *);
56 static void diagnostic_action_after_output (diagnostic_context *,
57                                             diagnostic_info *);
58 static void real_abort (void) ATTRIBUTE_NORETURN;
59
60 /* A diagnostic_context surrogate for stderr.  */
61 static diagnostic_context global_diagnostic_context;
62 diagnostic_context *global_dc = &global_diagnostic_context;
63
64 \f
65 /* Return a malloc'd string containing MSG formatted a la printf.  The
66    caller is responsible for freeing the memory.  */
67 static char *
68 build_message_string (const char *msg, ...)
69 {
70   char *str;
71   va_list ap;
72
73   va_start (ap, msg);
74   vasprintf (&str, msg, ap);
75   va_end (ap);
76
77   return str;
78 }
79
80 /* Same as diagnostic_build_prefix, but only the source FILE is given.  */
81 char *
82 file_name_as_prefix (const char *f)
83 {
84   return build_message_string ("%s: ", f);
85 }
86
87
88 \f
89 /* Initialize the diagnostic message outputting machinery.  */
90 void
91 diagnostic_initialize (diagnostic_context *context)
92 {
93   /* Allocate a basic pretty-printer.  Clients will replace this a
94      much more elaborated pretty-printer if they wish.  */
95   context->printer = XNEW (pretty_printer);
96   pp_construct (context->printer, NULL, 0);
97   /* By default, diagnostics are sent to stderr.  */
98   context->printer->buffer->stream = stderr;
99   /* By default, we emit prefixes once per message.  */
100   context->printer->wrapping.rule = DIAGNOSTICS_SHOW_PREFIX_ONCE;
101
102   memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
103   context->issue_warnings_are_errors_message = true;
104   context->warning_as_error_requested = false;
105   memset (context->classify_diagnostic, DK_UNSPECIFIED,
106           sizeof context->classify_diagnostic);
107   context->show_option_requested = false;
108   context->abort_on_error = false;
109   context->internal_error = NULL;
110   diagnostic_starter (context) = default_diagnostic_starter;
111   diagnostic_finalizer (context) = default_diagnostic_finalizer;
112   context->last_module = 0;
113   context->last_function = NULL;
114   context->lock = 0;
115 }
116
117 /* Initialize DIAGNOSTIC, where the message MSG has already been
118    translated.  */
119 void
120 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
121                                 va_list *args, location_t location,
122                                 diagnostic_t kind)
123 {
124   diagnostic->message.err_no = errno;
125   diagnostic->message.args_ptr = args;
126   diagnostic->message.format_spec = msg;
127   diagnostic->location = location;
128   diagnostic->kind = kind;
129   diagnostic->option_index = 0;
130 }
131
132 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
133    translated.  */
134 void
135 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
136                      va_list *args, location_t location,
137                      diagnostic_t kind)
138 {
139   diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
140 }
141
142 /* Return a malloc'd string describing a location.  The caller is
143    responsible for freeing the memory.  */
144 char *
145 diagnostic_build_prefix (diagnostic_info *diagnostic)
146 {
147   static const char *const diagnostic_kind_text[] = {
148 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
149 #include "diagnostic.def"
150 #undef DEFINE_DIAGNOSTIC_KIND
151     "must-not-happen"
152   };
153   const char *text = _(diagnostic_kind_text[diagnostic->kind]);
154   expanded_location s = expand_location (diagnostic->location);
155   gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
156
157   return
158     (s.file == NULL
159      ? build_message_string ("%s: %s", progname, text)
160      : flag_show_column && s.column != 0
161      ? build_message_string ("%s:%d:%d: %s", s.file, s.line, s.column, text)
162      : build_message_string ("%s:%d: %s", s.file, s.line, text));
163 }
164
165 /* Count a diagnostic.  Return true if the message should be printed.  */
166 static bool
167 diagnostic_count_diagnostic (diagnostic_context *context,
168                              diagnostic_info *diagnostic)
169 {
170   diagnostic_t kind = diagnostic->kind;
171   switch (kind)
172     {
173     default:
174       gcc_unreachable ();
175
176     case DK_ICE:
177 #ifndef ENABLE_CHECKING
178       /* When not checking, ICEs are converted to fatal errors when an
179          error has already occurred.  This is counteracted by
180          abort_on_error.  */
181       if ((diagnostic_kind_count (context, DK_ERROR) > 0
182            || diagnostic_kind_count (context, DK_SORRY) > 0)
183           && !context->abort_on_error)
184         {
185           expanded_location s = expand_location (diagnostic->location);
186           fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
187                    s.file, s.line);
188           exit (ICE_EXIT_CODE);
189         }
190 #endif
191       if (context->internal_error)
192         (*context->internal_error) (diagnostic->message.format_spec,
193                                     diagnostic->message.args_ptr);
194       /* Fall through.  */
195
196     case DK_FATAL: case DK_SORRY:
197     case DK_ANACHRONISM: case DK_NOTE:
198       ++diagnostic_kind_count (context, kind);
199       break;
200
201     case DK_WARNING:
202       ++diagnostic_kind_count (context, DK_WARNING);
203       break;
204
205     case DK_ERROR:
206       ++diagnostic_kind_count (context, DK_ERROR);
207       break;
208     }
209
210   return true;
211 }
212
213 /* Take any action which is expected to happen after the diagnostic
214    is written out.  This function does not always return.  */
215 static void
216 diagnostic_action_after_output (diagnostic_context *context,
217                                 diagnostic_info *diagnostic)
218 {
219   switch (diagnostic->kind)
220     {
221     case DK_DEBUG:
222     case DK_NOTE:
223     case DK_ANACHRONISM:
224     case DK_WARNING:
225       break;
226
227     case DK_ERROR:
228     case DK_SORRY:
229       if (context->abort_on_error)
230         real_abort ();
231       if (flag_fatal_errors)
232         {
233           fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
234           exit (FATAL_EXIT_CODE);
235         }
236       break;
237
238     case DK_ICE:
239       if (context->abort_on_error)
240         real_abort ();
241
242       fnotice (stderr, "Please submit a full bug report,\n"
243                "with preprocessed source if appropriate.\n"
244                "See %s for instructions.\n", bug_report_url);
245       exit (ICE_EXIT_CODE);
246
247     case DK_FATAL:
248       if (context->abort_on_error)
249         real_abort ();
250
251       fnotice (stderr, "compilation terminated.\n");
252       exit (FATAL_EXIT_CODE);
253
254     default:
255       gcc_unreachable ();
256     }
257 }
258
259 /* Prints out, if necessary, the name of the current function
260    that caused an error.  Called from all error and warning functions.  */
261 void
262 diagnostic_report_current_function (diagnostic_context *context,
263                                     diagnostic_info *diagnostic)
264 {
265   diagnostic_report_current_module (context);
266   lang_hooks.print_error_function (context, input_filename, diagnostic);
267 }
268
269 void
270 diagnostic_report_current_module (diagnostic_context *context)
271 {
272   struct file_stack *p;
273
274   if (pp_needs_newline (context->printer))
275     {
276       pp_newline (context->printer);
277       pp_needs_newline (context->printer) = false;
278     }
279
280   p = input_file_stack;
281   if (p && diagnostic_last_module_changed (context))
282     {
283       expanded_location xloc = expand_location (p->location);
284       pp_verbatim (context->printer,
285                    "In file included from %s:%d",
286                    xloc.file, xloc.line);
287       while ((p = p->next) != NULL)
288         {
289           xloc = expand_location (p->location);
290           pp_verbatim (context->printer,
291                        ",\n                 from %s:%d",
292                        xloc.file, xloc.line);
293         }
294       pp_verbatim (context->printer, ":");
295       diagnostic_set_last_module (context);
296       pp_newline (context->printer);
297     }
298 }
299
300 static void
301 default_diagnostic_starter (diagnostic_context *context,
302                             diagnostic_info *diagnostic)
303 {
304   diagnostic_report_current_function (context, diagnostic);
305   pp_set_prefix (context->printer, diagnostic_build_prefix (diagnostic));
306 }
307
308 static void
309 default_diagnostic_finalizer (diagnostic_context *context,
310                               diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
311 {
312   pp_destroy_prefix (context->printer);
313 }
314
315 /* Interface to specify diagnostic kind overrides.  Returns the
316    previous setting, or DK_UNSPECIFIED if the parameters are out of
317    range.  */
318 diagnostic_t
319 diagnostic_classify_diagnostic (diagnostic_context *context,
320                                 int option_index,
321                                 diagnostic_t new_kind)
322 {
323   diagnostic_t old_kind;
324
325   if (option_index <= 0
326       || option_index >= N_OPTS
327       || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
328     return DK_UNSPECIFIED;
329
330   old_kind = context->classify_diagnostic[option_index];
331   context->classify_diagnostic[option_index] = new_kind;
332   return old_kind;
333 }
334
335 /* Report a diagnostic message (an error or a warning) as specified by
336    DC.  This function is *the* subroutine in terms of which front-ends
337    should implement their specific diagnostic handling modules.  The
338    front-end independent format specifiers are exactly those described
339    in the documentation of output_format.  */
340
341 void
342 diagnostic_report_diagnostic (diagnostic_context *context,
343                               diagnostic_info *diagnostic)
344 {
345   bool maybe_print_warnings_as_errors_message = false;
346
347   /* Give preference to being able to inhibit warnings, before they
348      get reclassified to something else.  */
349   if (diagnostic->kind == DK_WARNING 
350       && !diagnostic_report_warnings_p ())
351     return;
352   
353   if (context->lock > 0)
354     {
355       /* If we're reporting an ICE in the middle of some other error,
356          try to flush out the previous error, then let this one
357          through.  Don't do this more than once.  */
358       if (diagnostic->kind == DK_ICE && context->lock == 1)
359         pp_flush (context->printer);
360       else
361         error_recursion (context);
362     }
363
364   /* If the user requested that warnings be treated as errors, so be
365      it.  Note that we do this before the next block so that
366      individual warnings can be overridden back to warnings with
367      -Wno-error=*.  */
368   if (context->warning_as_error_requested
369       && diagnostic->kind == DK_WARNING)
370     {
371       diagnostic->kind = DK_ERROR;
372       maybe_print_warnings_as_errors_message = true;
373     }
374   
375   if (diagnostic->option_index)
376     {
377       /* This tests if the user provided the appropriate -Wfoo or
378          -Wno-foo option.  */
379       if (! option_enabled (diagnostic->option_index))
380         return;
381       /* This tests if the user provided the appropriate -Werror=foo
382          option.  */
383       if (context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
384         {
385           diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
386           maybe_print_warnings_as_errors_message = false;
387         }
388       /* This allows for future extensions, like temporarily disabling
389          warnings for ranges of source code.  */
390       if (diagnostic->kind == DK_IGNORED)
391         return;
392     }
393
394   /* If we changed the kind due to -Werror, and didn't override it, we
395      need to print this message.  */
396   if (context->issue_warnings_are_errors_message
397       && maybe_print_warnings_as_errors_message)
398     {
399       pp_verbatim (context->printer,
400                    "%s: warnings being treated as errors\n", progname);
401       context->issue_warnings_are_errors_message = false;
402     }
403
404   context->lock++;
405
406   if (diagnostic_count_diagnostic (context, diagnostic))
407     {
408       const char *saved_format_spec = diagnostic->message.format_spec;
409
410       if (context->show_option_requested && diagnostic->option_index)
411         diagnostic->message.format_spec
412           = ACONCAT ((diagnostic->message.format_spec,
413                       " [", cl_options[diagnostic->option_index].opt_text, "]", NULL));
414
415       diagnostic->message.locus = &diagnostic->location;
416       diagnostic->message.abstract_origin = &diagnostic->abstract_origin;
417       diagnostic->abstract_origin = NULL;
418       pp_format (context->printer, &diagnostic->message);
419       (*diagnostic_starter (context)) (context, diagnostic);
420       pp_output_formatted_text (context->printer);
421       (*diagnostic_finalizer (context)) (context, diagnostic);
422       pp_flush (context->printer);
423       diagnostic_action_after_output (context, diagnostic);
424       diagnostic->message.format_spec = saved_format_spec;
425       diagnostic->abstract_origin = NULL;
426     }
427
428   context->lock--;
429 }
430
431 /* Given a partial pathname as input, return another pathname that
432    shares no directory elements with the pathname of __FILE__.  This
433    is used by fancy_abort() to print `Internal compiler error in expr.c'
434    instead of `Internal compiler error in ../../GCC/gcc/expr.c'.  */
435
436 const char *
437 trim_filename (const char *name)
438 {
439   static const char this_file[] = __FILE__;
440   const char *p = name, *q = this_file;
441
442   /* First skip any "../" in each filename.  This allows us to give a proper
443      reference to a file in a subdirectory.  */
444   while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
445     p += 3;
446
447   while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
448     q += 3;
449
450   /* Now skip any parts the two filenames have in common.  */
451   while (*p == *q && *p != 0 && *q != 0)
452     p++, q++;
453
454   /* Now go backwards until the previous directory separator.  */
455   while (p > name && !IS_DIR_SEPARATOR (p[-1]))
456     p--;
457
458   return p;
459 }
460 \f
461 /* Standard error reporting routines in increasing order of severity.
462    All of these take arguments like printf.  */
463
464 /* Text to be emitted verbatim to the error message stream; this
465    produces no prefix and disables line-wrapping.  Use rarely.  */
466 void
467 verbatim (const char *gmsgid, ...)
468 {
469   text_info text;
470   va_list ap;
471
472   va_start (ap, gmsgid);
473   text.err_no = errno;
474   text.args_ptr = &ap;
475   text.format_spec = _(gmsgid);
476   text.locus = NULL;
477   text.abstract_origin = NULL;
478   pp_format_verbatim (global_dc->printer, &text);
479   pp_flush (global_dc->printer);
480   va_end (ap);
481 }
482
483 /* An informative note.  Use this for additional details on an error
484    message.  */
485 void
486 inform (const char *gmsgid, ...)
487 {
488   diagnostic_info diagnostic;
489   va_list ap;
490
491   va_start (ap, gmsgid);
492   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_NOTE);
493   report_diagnostic (&diagnostic);
494   va_end (ap);
495 }
496
497 /* A warning.  Use this for code which is correct according to the
498    relevant language specification but is likely to be buggy anyway.  */
499 void
500 warning (int opt, const char *gmsgid, ...)
501 {
502   diagnostic_info diagnostic;
503   va_list ap;
504
505   va_start (ap, gmsgid);
506   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
507   diagnostic.option_index = opt;
508
509   report_diagnostic (&diagnostic);
510   va_end (ap);
511 }
512
513 void
514 warning0 (const char *gmsgid, ...)
515 {
516   diagnostic_info diagnostic;
517   va_list ap;
518
519   va_start (ap, gmsgid);
520   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
521   report_diagnostic (&diagnostic);
522   va_end (ap);
523 }
524
525 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
526    given on the command line, in which case it issues an error.  Use
527    this for diagnostics required by the relevant language standard,
528    if you have chosen not to make them errors.
529
530    Note that these diagnostics are issued independent of the setting
531    of the -pedantic command-line switch.  To get a warning enabled
532    only with that switch, write "if (pedantic) pedwarn (...);"  */
533 void
534 pedwarn (const char *gmsgid, ...)
535 {
536   diagnostic_info diagnostic;
537   va_list ap;
538
539   va_start (ap, gmsgid);
540   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location,
541                        pedantic_error_kind ());
542   report_diagnostic (&diagnostic);
543   va_end (ap);
544 }
545
546 /* A hard error: the code is definitely ill-formed, and an object file
547    will not be produced.  */
548 void
549 error (const char *gmsgid, ...)
550 {
551   diagnostic_info diagnostic;
552   va_list ap;
553
554   va_start (ap, gmsgid);
555   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
556   report_diagnostic (&diagnostic);
557   va_end (ap);
558 }
559
560 /* "Sorry, not implemented."  Use for a language feature which is
561    required by the relevant specification but not implemented by GCC.
562    An object file will not be produced.  */
563 void
564 sorry (const char *gmsgid, ...)
565 {
566   diagnostic_info diagnostic;
567   va_list ap;
568
569   va_start (ap, gmsgid);
570   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
571   report_diagnostic (&diagnostic);
572   va_end (ap);
573 }
574
575 /* An error which is severe enough that we make no attempt to
576    continue.  Do not use this for internal consistency checks; that's
577    internal_error.  Use of this function should be rare.  */
578 void
579 fatal_error (const char *gmsgid, ...)
580 {
581   diagnostic_info diagnostic;
582   va_list ap;
583
584   va_start (ap, gmsgid);
585   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
586   report_diagnostic (&diagnostic);
587   va_end (ap);
588
589   gcc_unreachable ();
590 }
591
592 /* An internal consistency check has failed.  We make no attempt to
593    continue.  Note that unless there is debugging value to be had from
594    a more specific message, or some other good reason, you should use
595    abort () instead of calling this function directly.  */
596 void
597 internal_error (const char *gmsgid, ...)
598 {
599   diagnostic_info diagnostic;
600   va_list ap;
601
602   va_start (ap, gmsgid);
603   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
604   report_diagnostic (&diagnostic);
605   va_end (ap);
606
607   gcc_unreachable ();
608 }
609 \f
610 /* Special case error functions.  Most are implemented in terms of the
611    above, or should be.  */
612
613 /* Print a diagnostic MSGID on FILE.  This is just fprintf, except it
614    runs its second argument through gettext.  */
615 void
616 fnotice (FILE *file, const char *cmsgid, ...)
617 {
618   va_list ap;
619
620   va_start (ap, cmsgid);
621   vfprintf (file, _(cmsgid), ap);
622   va_end (ap);
623 }
624
625 /* Inform the user that an error occurred while trying to report some
626    other error.  This indicates catastrophic internal inconsistencies,
627    so give up now.  But do try to flush out the previous error.
628    This mustn't use internal_error, that will cause infinite recursion.  */
629
630 static void
631 error_recursion (diagnostic_context *context)
632 {
633   diagnostic_info diagnostic;
634
635   if (context->lock < 3)
636     pp_flush (context->printer);
637
638   fnotice (stderr,
639            "Internal compiler error: Error reporting routines re-entered.\n");
640
641   /* Call diagnostic_action_after_output to get the "please submit a bug
642      report" message.  It only looks at the kind field of diagnostic_info.  */
643   diagnostic.kind = DK_ICE;
644   diagnostic_action_after_output (context, &diagnostic);
645
646   /* Do not use gcc_unreachable here; that goes through internal_error
647      and therefore would cause infinite recursion.  */
648   real_abort ();
649 }
650
651 /* Report an internal compiler error in a friendly manner.  This is
652    the function that gets called upon use of abort() in the source
653    code generally, thanks to a special macro.  */
654
655 void
656 fancy_abort (const char *file, int line, const char *function)
657 {
658   internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
659 }
660
661 /* Really call the system 'abort'.  This has to go right at the end of
662    this file, so that there are no functions after it that call abort
663    and get the system abort instead of our macro.  */
664 #undef abort
665 static void
666 real_abort (void)
667 {
668   abort ();
669 }