OSDN Git Service

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