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