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>
6 This file is part of GCC.
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
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
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
24 /* This file implements the language independent aspect of diagnostic
28 #undef FLOAT /* This is for hpux. They should change hpux. */
29 #undef FFS /* Some systems define this in param.h. */
31 #include "coretypes.h"
40 #include "diagnostic.h"
41 #include "langhooks.h"
42 #include "langhooks-def.h"
47 static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
49 static void default_diagnostic_starter (diagnostic_context *,
51 static void default_diagnostic_finalizer (diagnostic_context *,
54 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
55 static bool diagnostic_count_diagnostic (diagnostic_context *,
57 static void diagnostic_action_after_output (diagnostic_context *,
59 static void real_abort (void) ATTRIBUTE_NORETURN;
61 /* A diagnostic_context surrogate for stderr. */
62 static diagnostic_context global_diagnostic_context;
63 diagnostic_context *global_dc = &global_diagnostic_context;
65 /* Return a malloc'd string containing MSG formatted a la printf. The
66 caller is responsible for freeing the memory. */
68 build_message_string (const char *msg, ...)
74 vasprintf (&str, msg, ap);
80 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
82 file_name_as_prefix (const char *f)
84 return build_message_string ("%s: ", f);
89 /* Initialize the diagnostic message outputting machinery. */
91 diagnostic_initialize (diagnostic_context *context)
93 /* Allocate a basic pretty-printer. Clients will replace this a
94 much more elaborated pretty-printer if they wish. */
95 context->printer = xmalloc (sizeof (pretty_printer));
96 pp_construct (context->printer, NULL, 0);
97 /* By default, diagnostics are sent to stderr. */
98 context->printer->buffer->stream = stderr;
99 /* By default, we emit prefixes once per message. */
100 context->printer->prefixing_rule = DIAGNOSTICS_SHOW_PREFIX_ONCE;
102 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
103 context->issue_warnings_are_errors_message = true;
104 context->warning_as_error_requested = false;
105 context->abort_on_error = false;
106 context->internal_error = NULL;
107 diagnostic_starter (context) = default_diagnostic_starter;
108 diagnostic_finalizer (context) = default_diagnostic_finalizer;
109 context->last_module = 0;
110 context->last_function = NULL;
115 diagnostic_set_info (diagnostic_info *diagnostic, const char *msgid,
116 va_list *args, location_t location,
119 diagnostic->message.err_no = errno;
120 diagnostic->message.args_ptr = args;
121 diagnostic->message.format_spec = _(msgid);
122 diagnostic->location = location;
123 diagnostic->kind = kind;
124 diagnostic->option_index = 0;
127 /* Return a malloc'd string describing a location. The caller is
128 responsible for freeing the memory. */
130 diagnostic_build_prefix (diagnostic_info *diagnostic)
132 static const char *const diagnostic_kind_text[] = {
133 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
134 #include "diagnostic.def"
135 #undef DEFINE_DIAGNOSTIC_KIND
138 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
139 expanded_location s = expand_location (diagnostic->location);
140 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
144 ? build_message_string ("%s: %s", progname, text)
145 #ifdef USE_MAPPED_LOCATION
146 : flag_show_column && s.column != 0
147 ? build_message_string ("%s:%d:%d: %s", s.file, s.line, s.column, text)
149 : build_message_string ("%s:%d: %s", s.file, s.line, text));
152 /* Count a diagnostic. Return true if the message should be printed. */
154 diagnostic_count_diagnostic (diagnostic_context *context,
155 diagnostic_info *diagnostic)
157 diagnostic_t kind = diagnostic->kind;
164 #ifndef ENABLE_CHECKING
165 /* When not checking, ICEs are converted to fatal errors when an
166 error has already occurred. This is counteracted by
168 if ((diagnostic_kind_count (context, DK_ERROR) > 0
169 || diagnostic_kind_count (context, DK_SORRY) > 0)
170 && !context->abort_on_error)
172 expanded_location s = expand_location (diagnostic->location);
173 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
175 exit (FATAL_EXIT_CODE);
178 if (context->internal_error)
179 (*context->internal_error) (diagnostic->message.format_spec,
180 diagnostic->message.args_ptr);
183 case DK_FATAL: case DK_SORRY:
184 case DK_ANACHRONISM: case DK_NOTE:
185 ++diagnostic_kind_count (context, kind);
189 if (!diagnostic_report_warnings_p ())
192 if (!context->warning_as_error_requested)
194 ++diagnostic_kind_count (context, DK_WARNING);
197 else if (context->issue_warnings_are_errors_message)
199 pp_verbatim (context->printer,
200 "%s: warnings being treated as errors\n", progname);
201 context->issue_warnings_are_errors_message = false;
204 /* And fall through. */
206 ++diagnostic_kind_count (context, DK_ERROR);
213 /* Take any action which is expected to happen after the diagnostic
214 is written out. This function does not always return. */
216 diagnostic_action_after_output (diagnostic_context *context,
217 diagnostic_info *diagnostic)
219 switch (diagnostic->kind)
229 if (context->abort_on_error)
231 if (flag_fatal_errors)
233 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
234 exit (FATAL_EXIT_CODE);
239 if (context->abort_on_error)
242 fnotice (stderr, "Please submit a full bug report,\n"
243 "with preprocessed source if appropriate.\n"
244 "See %s for instructions.\n", bug_report_url);
245 exit (FATAL_EXIT_CODE);
248 if (context->abort_on_error)
251 fnotice (stderr, "compilation terminated.\n");
252 exit (FATAL_EXIT_CODE);
259 /* Prints out, if necessary, the name of the current function
260 that caused an error. Called from all error and warning functions. */
262 diagnostic_report_current_function (diagnostic_context *context)
264 diagnostic_report_current_module (context);
265 lang_hooks.print_error_function (context, input_filename);
269 diagnostic_report_current_module (diagnostic_context *context)
271 struct file_stack *p;
273 if (pp_needs_newline (context->printer))
275 pp_newline (context->printer);
276 pp_needs_newline (context->printer) = false;
279 p = input_file_stack;
280 if (p && diagnostic_last_module_changed (context))
282 expanded_location xloc = expand_location (p->location);
283 pp_verbatim (context->printer,
284 "In file included from %s:%d",
285 xloc.file, xloc.line);
286 while ((p = p->next) != NULL)
288 xloc = expand_location (p->location);
289 pp_verbatim (context->printer,
291 xloc.file, xloc.line);
293 pp_verbatim (context->printer, ":\n");
294 diagnostic_set_last_module (context);
299 default_diagnostic_starter (diagnostic_context *context,
300 diagnostic_info *diagnostic)
302 diagnostic_report_current_function (context);
303 pp_set_prefix (context->printer, diagnostic_build_prefix (diagnostic));
307 default_diagnostic_finalizer (diagnostic_context *context,
308 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
310 pp_destroy_prefix (context->printer);
313 /* Report a diagnostic message (an error or a warning) as specified by
314 DC. This function is *the* subroutine in terms of which front-ends
315 should implement their specific diagnostic handling modules. The
316 front-end independent format specifiers are exactly those described
317 in the documentation of output_format. */
320 diagnostic_report_diagnostic (diagnostic_context *context,
321 diagnostic_info *diagnostic)
323 if (context->lock > 0)
325 /* If we're reporting an ICE in the middle of some other error,
326 try to flush out the previous error, then let this one
327 through. Don't do this more than once. */
328 if (diagnostic->kind == DK_ICE && context->lock == 1)
329 pp_flush (context->printer);
331 error_recursion (context);
336 if (diagnostic_count_diagnostic (context, diagnostic))
338 if (diagnostics_show_options && diagnostic->option_index)
339 diagnostic->message.format_spec
340 = ACONCAT ((diagnostic->message.format_spec,
341 " [", cl_options[diagnostic->option_index].opt_text, "]", NULL));
343 pp_prepare_to_format (context->printer, &diagnostic->message,
344 &diagnostic->location);
345 (*diagnostic_starter (context)) (context, diagnostic);
346 pp_format_text (context->printer, &diagnostic->message);
347 (*diagnostic_finalizer (context)) (context, diagnostic);
348 pp_flush (context->printer);
349 diagnostic_action_after_output (context, diagnostic);
355 /* Given a partial pathname as input, return another pathname that
356 shares no directory elements with the pathname of __FILE__. This
357 is used by fancy_abort() to print `Internal compiler error in expr.c'
358 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
361 trim_filename (const char *name)
363 static const char this_file[] = __FILE__;
364 const char *p = name, *q = this_file;
366 /* First skip any "../" in each filename. This allows us to give a proper
367 reference to a file in a subdirectory. */
368 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
371 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
374 /* Now skip any parts the two filenames have in common. */
375 while (*p == *q && *p != 0 && *q != 0)
378 /* Now go backwards until the previous directory separator. */
379 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
385 /* Standard error reporting routines in increasing order of severity.
386 All of these take arguments like printf. */
388 /* Text to be emitted verbatim to the error message stream; this
389 produces no prefix and disables line-wrapping. Use rarely. */
391 verbatim (const char *msgid, ...)
396 va_start (ap, msgid);
399 text.format_spec = _(msgid);
400 pp_format_verbatim (global_dc->printer, &text);
401 pp_flush (global_dc->printer);
405 /* An informative note. Use this for additional details on an error
408 inform (const char *msgid, ...)
410 diagnostic_info diagnostic;
413 va_start (ap, msgid);
414 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_NOTE);
415 report_diagnostic (&diagnostic);
419 /* A warning. Use this for code which is correct according to the
420 relevant language specification but is likely to be buggy anyway. */
422 warning (int opt, const char *msgid, ...)
424 diagnostic_info diagnostic;
427 if (opt && ! option_enabled (opt))
430 va_start (ap, msgid);
431 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_WARNING);
432 diagnostic.option_index = opt;
434 report_diagnostic (&diagnostic);
439 warning0 (const char *msgid, ...)
441 diagnostic_info diagnostic;
444 va_start (ap, msgid);
445 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_WARNING);
446 report_diagnostic (&diagnostic);
450 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
451 given on the command line, in which case it issues an error. Use
452 this for diagnostics required by the relevant language standard,
453 if you have chosen not to make them errors.
455 Note that these diagnostics are issued independent of the setting
456 of the -pedantic command-line switch. To get a warning enabled
457 only with that switch, write "if (pedantic) pedwarn (...);" */
459 pedwarn (const char *msgid, ...)
461 diagnostic_info diagnostic;
464 va_start (ap, msgid);
465 diagnostic_set_info (&diagnostic, msgid, &ap, input_location,
466 pedantic_error_kind ());
467 report_diagnostic (&diagnostic);
471 /* A hard error: the code is definitely ill-formed, and an object file
472 will not be produced. */
474 error (const char *msgid, ...)
476 diagnostic_info diagnostic;
479 va_start (ap, msgid);
480 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_ERROR);
481 report_diagnostic (&diagnostic);
485 /* "Sorry, not implemented." Use for a language feature which is
486 required by the relevant specification but not implemented by GCC.
487 An object file will not be produced. */
489 sorry (const char *msgid, ...)
491 diagnostic_info diagnostic;
494 va_start (ap, msgid);
495 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_SORRY);
496 report_diagnostic (&diagnostic);
500 /* An error which is severe enough that we make no attempt to
501 continue. Do not use this for internal consistency checks; that's
502 internal_error. Use of this function should be rare. */
504 fatal_error (const char *msgid, ...)
506 diagnostic_info diagnostic;
509 va_start (ap, msgid);
510 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_FATAL);
511 report_diagnostic (&diagnostic);
517 /* An internal consistency check has failed. We make no attempt to
518 continue. Note that unless there is debugging value to be had from
519 a more specific message, or some other good reason, you should use
520 abort () instead of calling this function directly. */
522 internal_error (const char *msgid, ...)
524 diagnostic_info diagnostic;
527 va_start (ap, msgid);
528 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_ICE);
529 report_diagnostic (&diagnostic);
535 /* Special case error functions. Most are implemented in terms of the
536 above, or should be. */
538 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
539 runs its second argument through gettext. */
541 fnotice (FILE *file, const char *msgid, ...)
545 va_start (ap, msgid);
546 vfprintf (file, _(msgid), ap);
550 /* Inform the user that an error occurred while trying to report some
551 other error. This indicates catastrophic internal inconsistencies,
552 so give up now. But do try to flush out the previous error.
553 This mustn't use internal_error, that will cause infinite recursion. */
556 error_recursion (diagnostic_context *context)
558 diagnostic_info diagnostic;
560 if (context->lock < 3)
561 pp_flush (context->printer);
564 "Internal compiler error: Error reporting routines re-entered.\n");
566 /* Call diagnostic_action_after_output to get the "please submit a bug
567 report" message. It only looks at the kind field of diagnostic_info. */
568 diagnostic.kind = DK_ICE;
569 diagnostic_action_after_output (context, &diagnostic);
571 /* Do not use gcc_unreachable here; that goes through internal_error
572 and therefore would cause infinite recursion. */
576 /* Report an internal compiler error in a friendly manner. This is
577 the function that gets called upon use of abort() in the source
578 code generally, thanks to a special macro. */
581 fancy_abort (const char *file, int line, const char *function)
583 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
586 /* Really call the system 'abort'. This has to go right at the end of
587 this file, so that there are no functions after it that call abort
588 and get the system abort instead of our macro. */