OSDN Git Service

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