OSDN Git Service

PR testsuite/34168
[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   const struct line_map *map;
273
274   if (pp_needs_newline (context->printer))
275     {
276       pp_newline (context->printer);
277       pp_needs_newline (context->printer) = false;
278     }
279
280   if (input_location <= BUILTINS_LOCATION)
281     return;
282
283   map = linemap_lookup (line_table, input_location);
284   if (map && diagnostic_last_module_changed (context, map))
285     {
286       diagnostic_set_last_module (context, map);
287       if (! MAIN_FILE_P (map))
288         {
289           map = INCLUDED_FROM (line_table, map);
290           pp_verbatim (context->printer,
291                        "In file included from %s:%d",
292                        map->to_file, LAST_SOURCE_LINE (map));
293           while (! MAIN_FILE_P (map))
294             {
295               map = INCLUDED_FROM (line_table, map);
296               pp_verbatim (context->printer,
297                            ",\n                 from %s:%d",
298                            map->to_file, LAST_SOURCE_LINE (map));
299             }
300           pp_verbatim (context->printer, ":");
301           pp_newline (context->printer);
302         }
303     }
304 }
305
306 static void
307 default_diagnostic_starter (diagnostic_context *context,
308                             diagnostic_info *diagnostic)
309 {
310   diagnostic_report_current_function (context, diagnostic);
311   pp_set_prefix (context->printer, diagnostic_build_prefix (diagnostic));
312 }
313
314 static void
315 default_diagnostic_finalizer (diagnostic_context *context,
316                               diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
317 {
318   pp_destroy_prefix (context->printer);
319 }
320
321 /* Interface to specify diagnostic kind overrides.  Returns the
322    previous setting, or DK_UNSPECIFIED if the parameters are out of
323    range.  */
324 diagnostic_t
325 diagnostic_classify_diagnostic (diagnostic_context *context,
326                                 int option_index,
327                                 diagnostic_t new_kind)
328 {
329   diagnostic_t old_kind;
330
331   if (option_index <= 0
332       || option_index >= N_OPTS
333       || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
334     return DK_UNSPECIFIED;
335
336   old_kind = context->classify_diagnostic[option_index];
337   context->classify_diagnostic[option_index] = new_kind;
338   return old_kind;
339 }
340
341 /* Report a diagnostic message (an error or a warning) as specified by
342    DC.  This function is *the* subroutine in terms of which front-ends
343    should implement their specific diagnostic handling modules.  The
344    front-end independent format specifiers are exactly those described
345    in the documentation of output_format.  */
346
347 void
348 diagnostic_report_diagnostic (diagnostic_context *context,
349                               diagnostic_info *diagnostic)
350 {
351   bool maybe_print_warnings_as_errors_message = false;
352
353   /* Give preference to being able to inhibit warnings, before they
354      get reclassified to something else.  */
355   if (diagnostic->kind == DK_WARNING 
356       && !diagnostic_report_warnings_p ())
357     return;
358   
359   if (context->lock > 0)
360     {
361       /* If we're reporting an ICE in the middle of some other error,
362          try to flush out the previous error, then let this one
363          through.  Don't do this more than once.  */
364       if (diagnostic->kind == DK_ICE && context->lock == 1)
365         pp_flush (context->printer);
366       else
367         error_recursion (context);
368     }
369
370   /* If the user requested that warnings be treated as errors, so be
371      it.  Note that we do this before the next block so that
372      individual warnings can be overridden back to warnings with
373      -Wno-error=*.  */
374   if (context->warning_as_error_requested
375       && diagnostic->kind == DK_WARNING)
376     {
377       diagnostic->kind = DK_ERROR;
378       maybe_print_warnings_as_errors_message = true;
379     }
380   
381   if (diagnostic->option_index)
382     {
383       /* This tests if the user provided the appropriate -Wfoo or
384          -Wno-foo option.  */
385       if (! option_enabled (diagnostic->option_index))
386         return;
387       /* This tests if the user provided the appropriate -Werror=foo
388          option.  */
389       if (context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
390         {
391           diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
392           maybe_print_warnings_as_errors_message = false;
393         }
394       /* This allows for future extensions, like temporarily disabling
395          warnings for ranges of source code.  */
396       if (diagnostic->kind == DK_IGNORED)
397         return;
398     }
399
400   /* If we changed the kind due to -Werror, and didn't override it, we
401      need to print this message.  */
402   if (context->issue_warnings_are_errors_message
403       && maybe_print_warnings_as_errors_message)
404     {
405       pp_verbatim (context->printer,
406                    "%s: warnings being treated as errors\n", progname);
407       context->issue_warnings_are_errors_message = false;
408     }
409
410   context->lock++;
411
412   if (diagnostic_count_diagnostic (context, diagnostic))
413     {
414       const char *saved_format_spec = diagnostic->message.format_spec;
415
416       if (context->show_option_requested && diagnostic->option_index)
417         diagnostic->message.format_spec
418           = ACONCAT ((diagnostic->message.format_spec,
419                       " [", cl_options[diagnostic->option_index].opt_text, "]", NULL));
420
421       diagnostic->message.locus = &diagnostic->location;
422       diagnostic->message.abstract_origin = &diagnostic->abstract_origin;
423       diagnostic->abstract_origin = NULL;
424       pp_format (context->printer, &diagnostic->message);
425       (*diagnostic_starter (context)) (context, diagnostic);
426       pp_output_formatted_text (context->printer);
427       (*diagnostic_finalizer (context)) (context, diagnostic);
428       pp_flush (context->printer);
429       diagnostic_action_after_output (context, diagnostic);
430       diagnostic->message.format_spec = saved_format_spec;
431       diagnostic->abstract_origin = NULL;
432     }
433
434   context->lock--;
435 }
436
437 /* Given a partial pathname as input, return another pathname that
438    shares no directory elements with the pathname of __FILE__.  This
439    is used by fancy_abort() to print `Internal compiler error in expr.c'
440    instead of `Internal compiler error in ../../GCC/gcc/expr.c'.  */
441
442 const char *
443 trim_filename (const char *name)
444 {
445   static const char this_file[] = __FILE__;
446   const char *p = name, *q = this_file;
447
448   /* First skip any "../" in each filename.  This allows us to give a proper
449      reference to a file in a subdirectory.  */
450   while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
451     p += 3;
452
453   while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
454     q += 3;
455
456   /* Now skip any parts the two filenames have in common.  */
457   while (*p == *q && *p != 0 && *q != 0)
458     p++, q++;
459
460   /* Now go backwards until the previous directory separator.  */
461   while (p > name && !IS_DIR_SEPARATOR (p[-1]))
462     p--;
463
464   return p;
465 }
466 \f
467 /* Standard error reporting routines in increasing order of severity.
468    All of these take arguments like printf.  */
469
470 /* Text to be emitted verbatim to the error message stream; this
471    produces no prefix and disables line-wrapping.  Use rarely.  */
472 void
473 verbatim (const char *gmsgid, ...)
474 {
475   text_info text;
476   va_list ap;
477
478   va_start (ap, gmsgid);
479   text.err_no = errno;
480   text.args_ptr = &ap;
481   text.format_spec = _(gmsgid);
482   text.locus = NULL;
483   text.abstract_origin = NULL;
484   pp_format_verbatim (global_dc->printer, &text);
485   pp_flush (global_dc->printer);
486   va_end (ap);
487 }
488
489 /* An informative note.  Use this for additional details on an error
490    message.  */
491 void
492 inform (const char *gmsgid, ...)
493 {
494   diagnostic_info diagnostic;
495   va_list ap;
496
497   va_start (ap, gmsgid);
498   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_NOTE);
499   report_diagnostic (&diagnostic);
500   va_end (ap);
501 }
502
503 /* A warning.  Use this for code which is correct according to the
504    relevant language specification but is likely to be buggy anyway.  */
505 void
506 warning (int opt, const char *gmsgid, ...)
507 {
508   diagnostic_info diagnostic;
509   va_list ap;
510
511   va_start (ap, gmsgid);
512   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
513   diagnostic.option_index = opt;
514
515   report_diagnostic (&diagnostic);
516   va_end (ap);
517 }
518
519 void
520 warning0 (const char *gmsgid, ...)
521 {
522   diagnostic_info diagnostic;
523   va_list ap;
524
525   va_start (ap, gmsgid);
526   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
527   report_diagnostic (&diagnostic);
528   va_end (ap);
529 }
530
531 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
532    given on the command line, in which case it issues an error.  Use
533    this for diagnostics required by the relevant language standard,
534    if you have chosen not to make them errors.
535
536    Note that these diagnostics are issued independent of the setting
537    of the -pedantic command-line switch.  To get a warning enabled
538    only with that switch, write "if (pedantic) pedwarn (...);"  */
539 void
540 pedwarn (const char *gmsgid, ...)
541 {
542   diagnostic_info diagnostic;
543   va_list ap;
544
545   va_start (ap, gmsgid);
546   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location,
547                        pedantic_warning_kind ());
548   report_diagnostic (&diagnostic);
549   va_end (ap);
550 }
551
552 /* A "permissive" error: issues an error unless -fpermissive was given
553    on the command line, in which case it issues a warning.  Use this
554    for things that really should be errors but we want to support
555    legacy code.  */
556
557 void
558 permerror (const char *gmsgid, ...)
559 {
560   diagnostic_info diagnostic;
561   va_list ap;
562
563   va_start (ap, gmsgid);
564   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location,
565                        permissive_error_kind ());
566   diagnostic.option_index = OPT_fpermissive;
567   report_diagnostic (&diagnostic);
568   va_end (ap);
569 }
570
571
572 /* A hard error: the code is definitely ill-formed, and an object file
573    will not be produced.  */
574 void
575 error (const char *gmsgid, ...)
576 {
577   diagnostic_info diagnostic;
578   va_list ap;
579
580   va_start (ap, gmsgid);
581   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
582   report_diagnostic (&diagnostic);
583   va_end (ap);
584 }
585
586 /* "Sorry, not implemented."  Use for a language feature which is
587    required by the relevant specification but not implemented by GCC.
588    An object file will not be produced.  */
589 void
590 sorry (const char *gmsgid, ...)
591 {
592   diagnostic_info diagnostic;
593   va_list ap;
594
595   va_start (ap, gmsgid);
596   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
597   report_diagnostic (&diagnostic);
598   va_end (ap);
599 }
600
601 /* An error which is severe enough that we make no attempt to
602    continue.  Do not use this for internal consistency checks; that's
603    internal_error.  Use of this function should be rare.  */
604 void
605 fatal_error (const char *gmsgid, ...)
606 {
607   diagnostic_info diagnostic;
608   va_list ap;
609
610   va_start (ap, gmsgid);
611   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
612   report_diagnostic (&diagnostic);
613   va_end (ap);
614
615   gcc_unreachable ();
616 }
617
618 /* An internal consistency check has failed.  We make no attempt to
619    continue.  Note that unless there is debugging value to be had from
620    a more specific message, or some other good reason, you should use
621    abort () instead of calling this function directly.  */
622 void
623 internal_error (const char *gmsgid, ...)
624 {
625   diagnostic_info diagnostic;
626   va_list ap;
627
628   va_start (ap, gmsgid);
629   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
630   report_diagnostic (&diagnostic);
631   va_end (ap);
632
633   gcc_unreachable ();
634 }
635 \f
636 /* Special case error functions.  Most are implemented in terms of the
637    above, or should be.  */
638
639 /* Print a diagnostic MSGID on FILE.  This is just fprintf, except it
640    runs its second argument through gettext.  */
641 void
642 fnotice (FILE *file, const char *cmsgid, ...)
643 {
644   va_list ap;
645
646   va_start (ap, cmsgid);
647   vfprintf (file, _(cmsgid), ap);
648   va_end (ap);
649 }
650
651 /* Inform the user that an error occurred while trying to report some
652    other error.  This indicates catastrophic internal inconsistencies,
653    so give up now.  But do try to flush out the previous error.
654    This mustn't use internal_error, that will cause infinite recursion.  */
655
656 static void
657 error_recursion (diagnostic_context *context)
658 {
659   diagnostic_info diagnostic;
660
661   if (context->lock < 3)
662     pp_flush (context->printer);
663
664   fnotice (stderr,
665            "Internal compiler error: Error reporting routines re-entered.\n");
666
667   /* Call diagnostic_action_after_output to get the "please submit a bug
668      report" message.  It only looks at the kind field of diagnostic_info.  */
669   diagnostic.kind = DK_ICE;
670   diagnostic_action_after_output (context, &diagnostic);
671
672   /* Do not use gcc_unreachable here; that goes through internal_error
673      and therefore would cause infinite recursion.  */
674   real_abort ();
675 }
676
677 /* Report an internal compiler error in a friendly manner.  This is
678    the function that gets called upon use of abort() in the source
679    code generally, thanks to a special macro.  */
680
681 void
682 fancy_abort (const char *file, int line, const char *function)
683 {
684   internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
685 }
686
687 /* Really call the system 'abort'.  This has to go right at the end of
688    this file, so that there are no functions after it that call abort
689    and get the system abort instead of our macro.  */
690 #undef abort
691 static void
692 real_abort (void)
693 {
694   abort ();
695 }