OSDN Git Service

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