OSDN Git Service

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