1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This file implements the language independent aspect of diagnostic
27 #undef FLOAT /* This is for hpux. They should change hpux. */
28 #undef FFS /* Some systems define this in param.h. */
30 #include "coretypes.h"
39 #include "diagnostic.h"
40 #include "langhooks.h"
41 #include "langhooks-def.h"
46 static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
48 static void default_diagnostic_starter (diagnostic_context *,
50 static void default_diagnostic_finalizer (diagnostic_context *,
53 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
54 static bool diagnostic_count_diagnostic (diagnostic_context *,
56 static void diagnostic_action_after_output (diagnostic_context *,
58 static void real_abort (void) ATTRIBUTE_NORETURN;
60 /* A diagnostic_context surrogate for stderr. */
61 static diagnostic_context global_diagnostic_context;
62 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 = XNEW (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->wrapping.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 memset (context->classify_diagnostic, DK_UNSPECIFIED,
106 sizeof context->classify_diagnostic);
107 context->show_option_requested = false;
108 context->abort_on_error = false;
109 context->internal_error = NULL;
110 diagnostic_starter (context) = default_diagnostic_starter;
111 diagnostic_finalizer (context) = default_diagnostic_finalizer;
112 context->last_module = 0;
113 context->last_function = NULL;
117 /* Initialize DIAGNOSTIC, where the message MSG has already been
120 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
121 va_list *args, location_t location,
124 diagnostic->message.err_no = errno;
125 diagnostic->message.args_ptr = args;
126 diagnostic->message.format_spec = msg;
127 diagnostic->location = location;
128 diagnostic->kind = kind;
129 diagnostic->option_index = 0;
132 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
135 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
136 va_list *args, location_t location,
139 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
142 /* Return a malloc'd string describing a location. The caller is
143 responsible for freeing the memory. */
145 diagnostic_build_prefix (diagnostic_info *diagnostic)
147 static const char *const diagnostic_kind_text[] = {
148 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
149 #include "diagnostic.def"
150 #undef DEFINE_DIAGNOSTIC_KIND
153 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
154 expanded_location s = expand_location (diagnostic->location);
155 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
159 ? build_message_string ("%s: %s", progname, text)
160 : flag_show_column && s.column != 0
161 ? build_message_string ("%s:%d:%d: %s", s.file, s.line, s.column, text)
162 : build_message_string ("%s:%d: %s", s.file, s.line, text));
165 /* Count a diagnostic. Return true if the message should be printed. */
167 diagnostic_count_diagnostic (diagnostic_context *context,
168 diagnostic_info *diagnostic)
170 diagnostic_t kind = diagnostic->kind;
177 #ifndef ENABLE_CHECKING
178 /* When not checking, ICEs are converted to fatal errors when an
179 error has already occurred. This is counteracted by
181 if ((diagnostic_kind_count (context, DK_ERROR) > 0
182 || diagnostic_kind_count (context, DK_SORRY) > 0)
183 && !context->abort_on_error)
185 expanded_location s = expand_location (diagnostic->location);
186 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
188 exit (ICE_EXIT_CODE);
191 if (context->internal_error)
192 (*context->internal_error) (diagnostic->message.format_spec,
193 diagnostic->message.args_ptr);
196 case DK_FATAL: case DK_SORRY:
197 case DK_ANACHRONISM: case DK_NOTE:
198 ++diagnostic_kind_count (context, kind);
202 ++diagnostic_kind_count (context, DK_WARNING);
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 (ICE_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,
263 diagnostic_info *diagnostic)
265 diagnostic_report_current_module (context);
266 lang_hooks.print_error_function (context, input_filename, diagnostic);
270 diagnostic_report_current_module (diagnostic_context *context)
272 struct file_stack *p;
274 if (pp_needs_newline (context->printer))
276 pp_newline (context->printer);
277 pp_needs_newline (context->printer) = false;
280 p = input_file_stack;
281 if (p && diagnostic_last_module_changed (context))
283 expanded_location xloc = expand_location (p->location);
284 pp_verbatim (context->printer,
285 "In file included from %s:%d",
286 xloc.file, xloc.line);
287 while ((p = p->next) != NULL)
289 xloc = expand_location (p->location);
290 pp_verbatim (context->printer,
292 xloc.file, xloc.line);
294 pp_verbatim (context->printer, ":");
295 diagnostic_set_last_module (context);
296 pp_newline (context->printer);
301 default_diagnostic_starter (diagnostic_context *context,
302 diagnostic_info *diagnostic)
304 diagnostic_report_current_function (context, diagnostic);
305 pp_set_prefix (context->printer, diagnostic_build_prefix (diagnostic));
309 default_diagnostic_finalizer (diagnostic_context *context,
310 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
312 pp_destroy_prefix (context->printer);
315 /* Interface to specify diagnostic kind overrides. Returns the
316 previous setting, or DK_UNSPECIFIED if the parameters are out of
319 diagnostic_classify_diagnostic (diagnostic_context *context,
321 diagnostic_t new_kind)
323 diagnostic_t old_kind;
325 if (option_index <= 0
326 || option_index >= N_OPTS
327 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
328 return DK_UNSPECIFIED;
330 old_kind = context->classify_diagnostic[option_index];
331 context->classify_diagnostic[option_index] = new_kind;
335 /* Report a diagnostic message (an error or a warning) as specified by
336 DC. This function is *the* subroutine in terms of which front-ends
337 should implement their specific diagnostic handling modules. The
338 front-end independent format specifiers are exactly those described
339 in the documentation of output_format. */
342 diagnostic_report_diagnostic (diagnostic_context *context,
343 diagnostic_info *diagnostic)
345 bool maybe_print_warnings_as_errors_message = false;
347 /* Give preference to being able to inhibit warnings, before they
348 get reclassified to something else. */
349 if (diagnostic->kind == DK_WARNING
350 && !diagnostic_report_warnings_p ())
353 if (context->lock > 0)
355 /* If we're reporting an ICE in the middle of some other error,
356 try to flush out the previous error, then let this one
357 through. Don't do this more than once. */
358 if (diagnostic->kind == DK_ICE && context->lock == 1)
359 pp_flush (context->printer);
361 error_recursion (context);
364 /* If the user requested that warnings be treated as errors, so be
365 it. Note that we do this before the next block so that
366 individual warnings can be overridden back to warnings with
368 if (context->warning_as_error_requested
369 && diagnostic->kind == DK_WARNING)
371 diagnostic->kind = DK_ERROR;
372 maybe_print_warnings_as_errors_message = true;
375 if (diagnostic->option_index)
377 /* This tests if the user provided the appropriate -Wfoo or
379 if (! option_enabled (diagnostic->option_index))
381 /* This tests if the user provided the appropriate -Werror=foo
383 if (context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
385 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
386 maybe_print_warnings_as_errors_message = false;
388 /* This allows for future extensions, like temporarily disabling
389 warnings for ranges of source code. */
390 if (diagnostic->kind == DK_IGNORED)
394 /* If we changed the kind due to -Werror, and didn't override it, we
395 need to print this message. */
396 if (context->issue_warnings_are_errors_message
397 && maybe_print_warnings_as_errors_message)
399 pp_verbatim (context->printer,
400 "%s: warnings being treated as errors\n", progname);
401 context->issue_warnings_are_errors_message = false;
406 if (diagnostic_count_diagnostic (context, diagnostic))
408 const char *saved_format_spec = diagnostic->message.format_spec;
410 if (context->show_option_requested && diagnostic->option_index)
411 diagnostic->message.format_spec
412 = ACONCAT ((diagnostic->message.format_spec,
413 " [", cl_options[diagnostic->option_index].opt_text, "]", NULL));
415 diagnostic->message.locus = &diagnostic->location;
416 diagnostic->message.abstract_origin = &diagnostic->abstract_origin;
417 diagnostic->abstract_origin = NULL;
418 pp_format (context->printer, &diagnostic->message);
419 (*diagnostic_starter (context)) (context, diagnostic);
420 pp_output_formatted_text (context->printer);
421 (*diagnostic_finalizer (context)) (context, diagnostic);
422 pp_flush (context->printer);
423 diagnostic_action_after_output (context, diagnostic);
424 diagnostic->message.format_spec = saved_format_spec;
425 diagnostic->abstract_origin = NULL;
431 /* Given a partial pathname as input, return another pathname that
432 shares no directory elements with the pathname of __FILE__. This
433 is used by fancy_abort() to print `Internal compiler error in expr.c'
434 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
437 trim_filename (const char *name)
439 static const char this_file[] = __FILE__;
440 const char *p = name, *q = this_file;
442 /* First skip any "../" in each filename. This allows us to give a proper
443 reference to a file in a subdirectory. */
444 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
447 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
450 /* Now skip any parts the two filenames have in common. */
451 while (*p == *q && *p != 0 && *q != 0)
454 /* Now go backwards until the previous directory separator. */
455 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
461 /* Standard error reporting routines in increasing order of severity.
462 All of these take arguments like printf. */
464 /* Text to be emitted verbatim to the error message stream; this
465 produces no prefix and disables line-wrapping. Use rarely. */
467 verbatim (const char *gmsgid, ...)
472 va_start (ap, gmsgid);
475 text.format_spec = _(gmsgid);
477 text.abstract_origin = NULL;
478 pp_format_verbatim (global_dc->printer, &text);
479 pp_flush (global_dc->printer);
483 /* An informative note. Use this for additional details on an error
486 inform (const char *gmsgid, ...)
488 diagnostic_info diagnostic;
491 va_start (ap, gmsgid);
492 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_NOTE);
493 report_diagnostic (&diagnostic);
497 /* A warning. Use this for code which is correct according to the
498 relevant language specification but is likely to be buggy anyway. */
500 warning (int opt, const char *gmsgid, ...)
502 diagnostic_info diagnostic;
505 va_start (ap, gmsgid);
506 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
507 diagnostic.option_index = opt;
509 report_diagnostic (&diagnostic);
514 warning0 (const char *gmsgid, ...)
516 diagnostic_info diagnostic;
519 va_start (ap, gmsgid);
520 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
521 report_diagnostic (&diagnostic);
525 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
526 given on the command line, in which case it issues an error. Use
527 this for diagnostics required by the relevant language standard,
528 if you have chosen not to make them errors.
530 Note that these diagnostics are issued independent of the setting
531 of the -pedantic command-line switch. To get a warning enabled
532 only with that switch, write "if (pedantic) pedwarn (...);" */
534 pedwarn (const char *gmsgid, ...)
536 diagnostic_info diagnostic;
539 va_start (ap, gmsgid);
540 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location,
541 pedantic_error_kind ());
542 report_diagnostic (&diagnostic);
546 /* A hard error: the code is definitely ill-formed, and an object file
547 will not be produced. */
549 error (const char *gmsgid, ...)
551 diagnostic_info diagnostic;
554 va_start (ap, gmsgid);
555 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
556 report_diagnostic (&diagnostic);
560 /* "Sorry, not implemented." Use for a language feature which is
561 required by the relevant specification but not implemented by GCC.
562 An object file will not be produced. */
564 sorry (const char *gmsgid, ...)
566 diagnostic_info diagnostic;
569 va_start (ap, gmsgid);
570 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
571 report_diagnostic (&diagnostic);
575 /* An error which is severe enough that we make no attempt to
576 continue. Do not use this for internal consistency checks; that's
577 internal_error. Use of this function should be rare. */
579 fatal_error (const char *gmsgid, ...)
581 diagnostic_info diagnostic;
584 va_start (ap, gmsgid);
585 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
586 report_diagnostic (&diagnostic);
592 /* An internal consistency check has failed. We make no attempt to
593 continue. Note that unless there is debugging value to be had from
594 a more specific message, or some other good reason, you should use
595 abort () instead of calling this function directly. */
597 internal_error (const char *gmsgid, ...)
599 diagnostic_info diagnostic;
602 va_start (ap, gmsgid);
603 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
604 report_diagnostic (&diagnostic);
610 /* Special case error functions. Most are implemented in terms of the
611 above, or should be. */
613 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
614 runs its second argument through gettext. */
616 fnotice (FILE *file, const char *cmsgid, ...)
620 va_start (ap, cmsgid);
621 vfprintf (file, _(cmsgid), ap);
625 /* Inform the user that an error occurred while trying to report some
626 other error. This indicates catastrophic internal inconsistencies,
627 so give up now. But do try to flush out the previous error.
628 This mustn't use internal_error, that will cause infinite recursion. */
631 error_recursion (diagnostic_context *context)
633 diagnostic_info diagnostic;
635 if (context->lock < 3)
636 pp_flush (context->printer);
639 "Internal compiler error: Error reporting routines re-entered.\n");
641 /* Call diagnostic_action_after_output to get the "please submit a bug
642 report" message. It only looks at the kind field of diagnostic_info. */
643 diagnostic.kind = DK_ICE;
644 diagnostic_action_after_output (context, &diagnostic);
646 /* Do not use gcc_unreachable here; that goes through internal_error
647 and therefore would cause infinite recursion. */
651 /* Report an internal compiler error in a friendly manner. This is
652 the function that gets called upon use of abort() in the source
653 code generally, thanks to a special macro. */
656 fancy_abort (const char *file, int line, const char *function)
658 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
661 /* Really call the system 'abort'. This has to go right at the end of
662 this file, so that there are no functions after it that call abort
663 and get the system abort instead of our macro. */