OSDN Git Service

* c-decl.c (store_parm_decls_oldstyle): Let diagnostic machinery
[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
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, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, 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 \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 = xmalloc (sizeof (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->prefixing_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   context->abort_on_error = false;
106   context->internal_error = NULL;
107   diagnostic_starter (context) = default_diagnostic_starter;
108   diagnostic_finalizer (context) = default_diagnostic_finalizer;
109   context->last_module = 0;
110   context->last_function = NULL;
111   context->lock = 0;
112 }
113
114 void
115 diagnostic_set_info (diagnostic_info *diagnostic, const char *msgid,
116                      va_list *args, location_t location,
117                      diagnostic_t kind)
118 {
119   diagnostic->message.err_no = errno;
120   diagnostic->message.args_ptr = args;
121   diagnostic->message.format_spec = _(msgid);
122   diagnostic->location = location;
123   diagnostic->kind = kind;
124   diagnostic->option_index = 0;
125 }
126
127 /* Return a malloc'd string describing a location.  The caller is
128    responsible for freeing the memory.  */
129 char *
130 diagnostic_build_prefix (diagnostic_info *diagnostic)
131 {
132   static const char *const diagnostic_kind_text[] = {
133 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
134 #include "diagnostic.def"
135 #undef DEFINE_DIAGNOSTIC_KIND
136     "must-not-happen"
137   };
138   const char *text = _(diagnostic_kind_text[diagnostic->kind]);
139   expanded_location s = expand_location (diagnostic->location);
140   gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
141
142   return
143     (s.file == NULL
144      ? build_message_string ("%s: %s", progname, text)
145 #ifdef USE_MAPPED_LOCATION
146      : flag_show_column && s.column != 0
147      ? build_message_string ("%s:%d:%d: %s", s.file, s.line, s.column, text)
148 #endif
149      : build_message_string ("%s:%d: %s", s.file, s.line, text));
150 }
151
152 /* Count a diagnostic.  Return true if the message should be printed.  */
153 static bool
154 diagnostic_count_diagnostic (diagnostic_context *context,
155                              diagnostic_info *diagnostic)
156 {
157   diagnostic_t kind = diagnostic->kind;
158   switch (kind)
159     {
160     default:
161       gcc_unreachable ();
162
163     case DK_ICE:
164 #ifndef ENABLE_CHECKING
165       /* When not checking, ICEs are converted to fatal errors when an
166          error has already occurred.  This is counteracted by
167          abort_on_error.  */
168       if ((diagnostic_kind_count (context, DK_ERROR) > 0
169            || diagnostic_kind_count (context, DK_SORRY) > 0)
170           && !context->abort_on_error)
171         {
172           expanded_location s = expand_location (diagnostic->location);
173           fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
174                    s.file, s.line);
175           exit (FATAL_EXIT_CODE);
176         }
177 #endif
178       if (context->internal_error)
179         (*context->internal_error) (diagnostic->message.format_spec,
180                                     diagnostic->message.args_ptr);
181       /* Fall through.  */
182
183     case DK_FATAL: case DK_SORRY:
184     case DK_ANACHRONISM: case DK_NOTE:
185       ++diagnostic_kind_count (context, kind);
186       break;
187
188     case DK_WARNING:
189       if (!diagnostic_report_warnings_p ())
190         return false;
191
192       if (!context->warning_as_error_requested)
193         {
194           ++diagnostic_kind_count (context, DK_WARNING);
195           break;
196         }
197       else if (context->issue_warnings_are_errors_message)
198         {
199           pp_verbatim (context->printer,
200                        "%s: warnings being treated as errors\n", progname);
201           context->issue_warnings_are_errors_message = false;
202         }
203
204       /* And fall through.  */
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 (FATAL_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 {
264   diagnostic_report_current_module (context);
265   lang_hooks.print_error_function (context, input_filename);
266 }
267
268 void
269 diagnostic_report_current_module (diagnostic_context *context)
270 {
271   struct file_stack *p;
272
273   if (pp_needs_newline (context->printer))
274     {
275       pp_newline (context->printer);
276       pp_needs_newline (context->printer) = false;
277     }
278
279   p = input_file_stack;
280   if (p && diagnostic_last_module_changed (context))
281     {
282       expanded_location xloc = expand_location (p->location);
283       pp_verbatim (context->printer,
284                    "In file included from %s:%d",
285                    xloc.file, xloc.line);
286       while ((p = p->next) != NULL)
287         {
288           xloc = expand_location (p->location);
289           pp_verbatim (context->printer,
290                        ",\n                 from %s:%d",
291                        xloc.file, xloc.line);
292         }
293       pp_verbatim (context->printer, ":\n");
294       diagnostic_set_last_module (context);
295     }
296 }
297
298 static void
299 default_diagnostic_starter (diagnostic_context *context,
300                             diagnostic_info *diagnostic)
301 {
302   diagnostic_report_current_function (context);
303   pp_set_prefix (context->printer, diagnostic_build_prefix (diagnostic));
304 }
305
306 static void
307 default_diagnostic_finalizer (diagnostic_context *context,
308                               diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
309 {
310   pp_destroy_prefix (context->printer);
311 }
312
313 /* Report a diagnostic message (an error or a warning) as specified by
314    DC.  This function is *the* subroutine in terms of which front-ends
315    should implement their specific diagnostic handling modules.  The
316    front-end independent format specifiers are exactly those described
317    in the documentation of output_format.  */
318
319 void
320 diagnostic_report_diagnostic (diagnostic_context *context,
321                               diagnostic_info *diagnostic)
322 {
323   if (context->lock > 0)
324     {
325       /* If we're reporting an ICE in the middle of some other error,
326          try to flush out the previous error, then let this one
327          through.  Don't do this more than once.  */
328       if (diagnostic->kind == DK_ICE && context->lock == 1)
329         pp_flush (context->printer);
330       else
331         error_recursion (context);
332     }
333
334   context->lock++;
335
336   if (diagnostic_count_diagnostic (context, diagnostic))
337     {
338       if (diagnostics_show_options && diagnostic->option_index)
339         diagnostic->message.format_spec
340           = ACONCAT ((diagnostic->message.format_spec,
341                       " [", cl_options[diagnostic->option_index].opt_text, "]", NULL));
342
343       pp_prepare_to_format (context->printer, &diagnostic->message,
344                             &diagnostic->location);
345       (*diagnostic_starter (context)) (context, diagnostic);
346       pp_format_text (context->printer, &diagnostic->message);
347       (*diagnostic_finalizer (context)) (context, diagnostic);
348       pp_flush (context->printer);
349       diagnostic_action_after_output (context, diagnostic);
350     }
351
352   context->lock--;
353 }
354
355 /* Given a partial pathname as input, return another pathname that
356    shares no directory elements with the pathname of __FILE__.  This
357    is used by fancy_abort() to print `Internal compiler error in expr.c'
358    instead of `Internal compiler error in ../../GCC/gcc/expr.c'.  */
359
360 const char *
361 trim_filename (const char *name)
362 {
363   static const char this_file[] = __FILE__;
364   const char *p = name, *q = this_file;
365
366   /* First skip any "../" in each filename.  This allows us to give a proper
367      reference to a file in a subdirectory.  */
368   while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
369     p += 3;
370
371   while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
372     q += 3;
373
374   /* Now skip any parts the two filenames have in common.  */
375   while (*p == *q && *p != 0 && *q != 0)
376     p++, q++;
377
378   /* Now go backwards until the previous directory separator.  */
379   while (p > name && !IS_DIR_SEPARATOR (p[-1]))
380     p--;
381
382   return p;
383 }
384 \f
385 /* Standard error reporting routines in increasing order of severity.
386    All of these take arguments like printf.  */
387
388 /* Text to be emitted verbatim to the error message stream; this
389    produces no prefix and disables line-wrapping.  Use rarely.  */
390 void
391 verbatim (const char *msgid, ...)
392 {
393   text_info text;
394   va_list ap;
395
396   va_start (ap, msgid);
397   text.err_no = errno;
398   text.args_ptr = &ap;
399   text.format_spec = _(msgid);
400   pp_format_verbatim (global_dc->printer, &text);
401   pp_flush (global_dc->printer);
402   va_end (ap);
403 }
404
405 /* An informative note.  Use this for additional details on an error
406    message.  */
407 void
408 inform (const char *msgid, ...)
409 {
410   diagnostic_info diagnostic;
411   va_list ap;
412
413   va_start (ap, msgid);
414   diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_NOTE);
415   report_diagnostic (&diagnostic);
416   va_end (ap);
417 }
418
419 /* A warning.  Use this for code which is correct according to the
420    relevant language specification but is likely to be buggy anyway.  */
421 void
422 warning (int opt, const char *msgid, ...)
423 {
424   diagnostic_info diagnostic;
425   va_list ap;
426
427   if (opt && ! option_enabled (opt))
428     return;
429
430   va_start (ap, msgid);
431   diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_WARNING);
432   diagnostic.option_index = opt;
433
434   report_diagnostic (&diagnostic);
435   va_end (ap);
436 }
437
438 void
439 warning0 (const char *msgid, ...)
440 {
441   diagnostic_info diagnostic;
442   va_list ap;
443
444   va_start (ap, msgid);
445   diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_WARNING);
446   report_diagnostic (&diagnostic);
447   va_end (ap);
448 }
449
450 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
451    given on the command line, in which case it issues an error.  Use
452    this for diagnostics required by the relevant language standard,
453    if you have chosen not to make them errors.
454
455    Note that these diagnostics are issued independent of the setting
456    of the -pedantic command-line switch.  To get a warning enabled
457    only with that switch, write "if (pedantic) pedwarn (...);"  */
458 void
459 pedwarn (const char *msgid, ...)
460 {
461   diagnostic_info diagnostic;
462   va_list ap;
463
464   va_start (ap, msgid);
465   diagnostic_set_info (&diagnostic, msgid, &ap, input_location,
466                        pedantic_error_kind ());
467   report_diagnostic (&diagnostic);
468   va_end (ap);
469 }
470
471 /* A hard error: the code is definitely ill-formed, and an object file
472    will not be produced.  */
473 void
474 error (const char *msgid, ...)
475 {
476   diagnostic_info diagnostic;
477   va_list ap;
478
479   va_start (ap, msgid);
480   diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_ERROR);
481   report_diagnostic (&diagnostic);
482   va_end (ap);
483 }
484
485 /* "Sorry, not implemented."  Use for a language feature which is
486    required by the relevant specification but not implemented by GCC.
487    An object file will not be produced.  */
488 void
489 sorry (const char *msgid, ...)
490 {
491   diagnostic_info diagnostic;
492   va_list ap;
493
494   va_start (ap, msgid);
495   diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_SORRY);
496   report_diagnostic (&diagnostic);
497   va_end (ap);
498 }
499
500 /* An error which is severe enough that we make no attempt to
501    continue.  Do not use this for internal consistency checks; that's
502    internal_error.  Use of this function should be rare.  */
503 void
504 fatal_error (const char *msgid, ...)
505 {
506   diagnostic_info diagnostic;
507   va_list ap;
508
509   va_start (ap, msgid);
510   diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_FATAL);
511   report_diagnostic (&diagnostic);
512   va_end (ap);
513
514   gcc_unreachable ();
515 }
516
517 /* An internal consistency check has failed.  We make no attempt to
518    continue.  Note that unless there is debugging value to be had from
519    a more specific message, or some other good reason, you should use
520    abort () instead of calling this function directly.  */
521 void
522 internal_error (const char *msgid, ...)
523 {
524   diagnostic_info diagnostic;
525   va_list ap;
526
527   va_start (ap, msgid);
528   diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_ICE);
529   report_diagnostic (&diagnostic);
530   va_end (ap);
531
532   gcc_unreachable ();
533 }
534 \f
535 /* Special case error functions.  Most are implemented in terms of the
536    above, or should be.  */
537
538 /* Print a diagnostic MSGID on FILE.  This is just fprintf, except it
539    runs its second argument through gettext.  */
540 void
541 fnotice (FILE *file, const char *msgid, ...)
542 {
543   va_list ap;
544
545   va_start (ap, msgid);
546   vfprintf (file, _(msgid), ap);
547   va_end (ap);
548 }
549
550 /* Inform the user that an error occurred while trying to report some
551    other error.  This indicates catastrophic internal inconsistencies,
552    so give up now.  But do try to flush out the previous error.
553    This mustn't use internal_error, that will cause infinite recursion.  */
554
555 static void
556 error_recursion (diagnostic_context *context)
557 {
558   diagnostic_info diagnostic;
559
560   if (context->lock < 3)
561     pp_flush (context->printer);
562
563   fnotice (stderr,
564            "Internal compiler error: Error reporting routines re-entered.\n");
565
566   /* Call diagnostic_action_after_output to get the "please submit a bug
567      report" message.  It only looks at the kind field of diagnostic_info.  */
568   diagnostic.kind = DK_ICE;
569   diagnostic_action_after_output (context, &diagnostic);
570
571   /* Do not use gcc_unreachable here; that goes through internal_error
572      and therefore would cause infinite recursion.  */
573   real_abort ();
574 }
575
576 /* Report an internal compiler error in a friendly manner.  This is
577    the function that gets called upon use of abort() in the source
578    code generally, thanks to a special macro.  */
579
580 void
581 fancy_abort (const char *file, int line, const char *function)
582 {
583   internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
584 }
585
586 /* Really call the system 'abort'.  This has to go right at the end of
587    this file, so that there are no functions after it that call abort
588    and get the system abort instead of our macro.  */
589 #undef abort
590 static void
591 real_abort (void)
592 {
593   abort ();
594 }