OSDN Git Service

2008-08-04 Robert Dewar <dewar@adacore.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, use either "if (pedantic) pedwarn
526    (OPT_pedantic,...)" or just "pedwarn (OPT_pedantic,..)".  To get a
527    pedwarn independently of the -pedantic switch use "pedwarn (0,...)".  */
528
529 void
530 pedwarn (int opt, const char *gmsgid, ...)
531 {
532   diagnostic_info diagnostic;
533   va_list ap;
534
535   va_start (ap, gmsgid);
536   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location,
537                       pedantic_warning_kind ());
538   diagnostic.option_index = opt;
539
540   report_diagnostic (&diagnostic);
541   va_end (ap);
542 }
543
544 void
545 pedwarn0 (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                       pedantic_warning_kind ());
553   report_diagnostic (&diagnostic);
554   va_end (ap);
555 }
556
557 /* A "permissive" error: issues an error unless -fpermissive was given
558    on the command line, in which case it issues a warning.  Use this
559    for things that really should be errors but we want to support
560    legacy code.  */
561
562 void
563 permerror (const char *gmsgid, ...)
564 {
565   diagnostic_info diagnostic;
566   va_list ap;
567
568   va_start (ap, gmsgid);
569   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location,
570                        permissive_error_kind ());
571   diagnostic.option_index = OPT_fpermissive;
572   report_diagnostic (&diagnostic);
573   va_end (ap);
574 }
575
576
577 /* A hard error: the code is definitely ill-formed, and an object file
578    will not be produced.  */
579 void
580 error (const char *gmsgid, ...)
581 {
582   diagnostic_info diagnostic;
583   va_list ap;
584
585   va_start (ap, gmsgid);
586   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
587   report_diagnostic (&diagnostic);
588   va_end (ap);
589 }
590
591 /* "Sorry, not implemented."  Use for a language feature which is
592    required by the relevant specification but not implemented by GCC.
593    An object file will not be produced.  */
594 void
595 sorry (const char *gmsgid, ...)
596 {
597   diagnostic_info diagnostic;
598   va_list ap;
599
600   va_start (ap, gmsgid);
601   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
602   report_diagnostic (&diagnostic);
603   va_end (ap);
604 }
605
606 /* An error which is severe enough that we make no attempt to
607    continue.  Do not use this for internal consistency checks; that's
608    internal_error.  Use of this function should be rare.  */
609 void
610 fatal_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_FATAL);
617   report_diagnostic (&diagnostic);
618   va_end (ap);
619
620   gcc_unreachable ();
621 }
622
623 /* An internal consistency check has failed.  We make no attempt to
624    continue.  Note that unless there is debugging value to be had from
625    a more specific message, or some other good reason, you should use
626    abort () instead of calling this function directly.  */
627 void
628 internal_error (const char *gmsgid, ...)
629 {
630   diagnostic_info diagnostic;
631   va_list ap;
632
633   va_start (ap, gmsgid);
634   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
635   report_diagnostic (&diagnostic);
636   va_end (ap);
637
638   gcc_unreachable ();
639 }
640 \f
641 /* Special case error functions.  Most are implemented in terms of the
642    above, or should be.  */
643
644 /* Print a diagnostic MSGID on FILE.  This is just fprintf, except it
645    runs its second argument through gettext.  */
646 void
647 fnotice (FILE *file, const char *cmsgid, ...)
648 {
649   va_list ap;
650
651   va_start (ap, cmsgid);
652   vfprintf (file, _(cmsgid), ap);
653   va_end (ap);
654 }
655
656 /* Inform the user that an error occurred while trying to report some
657    other error.  This indicates catastrophic internal inconsistencies,
658    so give up now.  But do try to flush out the previous error.
659    This mustn't use internal_error, that will cause infinite recursion.  */
660
661 static void
662 error_recursion (diagnostic_context *context)
663 {
664   diagnostic_info diagnostic;
665
666   if (context->lock < 3)
667     pp_flush (context->printer);
668
669   fnotice (stderr,
670            "Internal compiler error: Error reporting routines re-entered.\n");
671
672   /* Call diagnostic_action_after_output to get the "please submit a bug
673      report" message.  It only looks at the kind field of diagnostic_info.  */
674   diagnostic.kind = DK_ICE;
675   diagnostic_action_after_output (context, &diagnostic);
676
677   /* Do not use gcc_unreachable here; that goes through internal_error
678      and therefore would cause infinite recursion.  */
679   real_abort ();
680 }
681
682 /* Report an internal compiler error in a friendly manner.  This is
683    the function that gets called upon use of abort() in the source
684    code generally, thanks to a special macro.  */
685
686 void
687 fancy_abort (const char *file, int line, const char *function)
688 {
689   internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
690 }
691
692 /* Really call the system 'abort'.  This has to go right at the end of
693    this file, so that there are no functions after it that call abort
694    and get the system abort instead of our macro.  */
695 #undef abort
696 static void
697 real_abort (void)
698 {
699   abort ();
700 }