OSDN Git Service

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