OSDN Git Service

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