1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 2009, 2010 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
28 #include "coretypes.h"
34 #include "diagnostic.h"
38 #define pedantic_warning_kind() (flag_pedantic_errors ? DK_ERROR : DK_WARNING)
39 #define permissive_error_kind() (flag_permissive ? DK_WARNING : DK_ERROR)
42 static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
44 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
46 static void diagnostic_action_after_output (diagnostic_context *,
48 static void real_abort (void) ATTRIBUTE_NORETURN;
50 /* A diagnostic_context surrogate for stderr. */
51 static diagnostic_context global_diagnostic_context;
52 diagnostic_context *global_dc = &global_diagnostic_context;
55 /* Return a malloc'd string containing MSG formatted a la printf. The
56 caller is responsible for freeing the memory. */
58 build_message_string (const char *msg, ...)
64 vasprintf (&str, msg, ap);
70 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
72 file_name_as_prefix (const char *f)
74 return build_message_string ("%s: ", f);
79 /* Initialize the diagnostic message outputting machinery. */
81 diagnostic_initialize (diagnostic_context *context)
83 /* Allocate a basic pretty-printer. Clients will replace this a
84 much more elaborated pretty-printer if they wish. */
85 context->printer = XNEW (pretty_printer);
86 pp_construct (context->printer, NULL, 0);
87 /* By default, diagnostics are sent to stderr. */
88 context->printer->buffer->stream = stderr;
89 /* By default, we emit prefixes once per message. */
90 context->printer->wrapping.rule = DIAGNOSTICS_SHOW_PREFIX_ONCE;
92 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
93 context->some_warnings_are_errors = false;
94 context->warning_as_error_requested = false;
95 memset (context->classify_diagnostic, DK_UNSPECIFIED,
96 sizeof context->classify_diagnostic);
97 context->show_option_requested = false;
98 context->abort_on_error = false;
99 context->internal_error = NULL;
100 diagnostic_starter (context) = default_diagnostic_starter;
101 diagnostic_finalizer (context) = default_diagnostic_finalizer;
102 context->last_module = 0;
103 context->x_data = NULL;
105 context->inhibit_notes_p = false;
108 /* Do any cleaning up required after the last diagnostic is emitted. */
111 diagnostic_finish (diagnostic_context *context)
113 /* Some of the errors may actually have been warnings. */
114 if (context->some_warnings_are_errors)
116 /* -Werror was given. */
117 if (context->warning_as_error_requested)
118 pp_verbatim (context->printer,
119 _("%s: all warnings being treated as errors\n"),
121 /* At least one -Werror= was given. */
123 pp_verbatim (context->printer,
124 _("%s: some warnings being treated as errors\n"),
126 pp_flush (context->printer);
130 /* Initialize DIAGNOSTIC, where the message MSG has already been
133 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
134 va_list *args, location_t location,
137 diagnostic->message.err_no = errno;
138 diagnostic->message.args_ptr = args;
139 diagnostic->message.format_spec = msg;
140 diagnostic->location = location;
141 diagnostic->override_column = 0;
142 diagnostic->kind = kind;
143 diagnostic->option_index = 0;
146 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
149 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
150 va_list *args, location_t location,
153 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
156 /* Return a malloc'd string describing a location. The caller is
157 responsible for freeing the memory. */
159 diagnostic_build_prefix (diagnostic_info *diagnostic)
161 static const char *const diagnostic_kind_text[] = {
162 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
163 #include "diagnostic.def"
164 #undef DEFINE_DIAGNOSTIC_KIND
167 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
168 expanded_location s = expand_location (diagnostic->location);
169 if (diagnostic->override_column)
170 s.column = diagnostic->override_column;
171 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
175 ? build_message_string ("%s: %s", progname, text)
177 ? build_message_string ("%s:%d:%d: %s", s.file, s.line, s.column, text)
178 : build_message_string ("%s:%d: %s", s.file, s.line, text));
181 /* Take any action which is expected to happen after the diagnostic
182 is written out. This function does not always return. */
184 diagnostic_action_after_output (diagnostic_context *context,
185 diagnostic_info *diagnostic)
187 switch (diagnostic->kind)
197 if (context->abort_on_error)
199 if (flag_fatal_errors)
201 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
202 diagnostic_finish (context);
203 exit (FATAL_EXIT_CODE);
208 if (context->abort_on_error)
211 fnotice (stderr, "Please submit a full bug report,\n"
212 "with preprocessed source if appropriate.\n"
213 "See %s for instructions.\n", bug_report_url);
214 exit (ICE_EXIT_CODE);
217 if (context->abort_on_error)
219 diagnostic_finish (context);
220 fnotice (stderr, "compilation terminated.\n");
221 exit (FATAL_EXIT_CODE);
229 diagnostic_report_current_module (diagnostic_context *context)
231 const struct line_map *map;
233 if (pp_needs_newline (context->printer))
235 pp_newline (context->printer);
236 pp_needs_newline (context->printer) = false;
239 if (input_location <= BUILTINS_LOCATION)
242 map = linemap_lookup (line_table, input_location);
243 if (map && diagnostic_last_module_changed (context, map))
245 diagnostic_set_last_module (context, map);
246 if (! MAIN_FILE_P (map))
248 map = INCLUDED_FROM (line_table, map);
249 if (flag_show_column)
250 pp_verbatim (context->printer,
251 "In file included from %s:%d:%d",
253 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
255 pp_verbatim (context->printer,
256 "In file included from %s:%d",
257 map->to_file, LAST_SOURCE_LINE (map));
258 while (! MAIN_FILE_P (map))
260 map = INCLUDED_FROM (line_table, map);
261 pp_verbatim (context->printer,
263 map->to_file, LAST_SOURCE_LINE (map));
265 pp_verbatim (context->printer, ":");
266 pp_newline (context->printer);
272 default_diagnostic_starter (diagnostic_context *context,
273 diagnostic_info *diagnostic)
275 diagnostic_report_current_module (context);
276 pp_set_prefix (context->printer, diagnostic_build_prefix (diagnostic));
280 default_diagnostic_finalizer (diagnostic_context *context,
281 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
283 pp_destroy_prefix (context->printer);
286 /* Interface to specify diagnostic kind overrides. Returns the
287 previous setting, or DK_UNSPECIFIED if the parameters are out of
290 diagnostic_classify_diagnostic (diagnostic_context *context,
292 diagnostic_t new_kind)
294 diagnostic_t old_kind;
296 if (option_index <= 0
297 || option_index >= N_OPTS
298 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
299 return DK_UNSPECIFIED;
301 old_kind = context->classify_diagnostic[option_index];
302 context->classify_diagnostic[option_index] = new_kind;
306 /* Report a diagnostic message (an error or a warning) as specified by
307 DC. This function is *the* subroutine in terms of which front-ends
308 should implement their specific diagnostic handling modules. The
309 front-end independent format specifiers are exactly those described
310 in the documentation of output_format.
311 Return true if a diagnostic was printed, false otherwise. */
314 diagnostic_report_diagnostic (diagnostic_context *context,
315 diagnostic_info *diagnostic)
317 location_t location = diagnostic->location;
318 diagnostic_t orig_diag_kind = diagnostic->kind;
319 const char *saved_format_spec;
321 /* Give preference to being able to inhibit warnings, before they
322 get reclassified to something else. */
323 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
324 && !diagnostic_report_warnings_p (location))
327 if (diagnostic->kind == DK_PEDWARN)
329 diagnostic->kind = pedantic_warning_kind ();
330 /* We do this to avoid giving the message for -pedantic-errors. */
331 orig_diag_kind = diagnostic->kind;
334 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
337 if (context->lock > 0)
339 /* If we're reporting an ICE in the middle of some other error,
340 try to flush out the previous error, then let this one
341 through. Don't do this more than once. */
342 if (diagnostic->kind == DK_ICE && context->lock == 1)
343 pp_flush (context->printer);
345 error_recursion (context);
348 /* If the user requested that warnings be treated as errors, so be
349 it. Note that we do this before the next block so that
350 individual warnings can be overridden back to warnings with
352 if (context->warning_as_error_requested
353 && diagnostic->kind == DK_WARNING)
355 diagnostic->kind = DK_ERROR;
358 if (diagnostic->option_index)
360 /* This tests if the user provided the appropriate -Wfoo or
362 if (! option_enabled (diagnostic->option_index))
364 /* This tests if the user provided the appropriate -Werror=foo
366 if (context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
368 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
370 /* This allows for future extensions, like temporarily disabling
371 warnings for ranges of source code. */
372 if (diagnostic->kind == DK_IGNORED)
376 if (orig_diag_kind == DK_WARNING && diagnostic->kind == DK_ERROR)
377 context->some_warnings_are_errors = true;
381 if (diagnostic->kind == DK_ICE && plugins_active_p ())
383 fnotice (stderr, "*** WARNING *** there are active plugins, do not report"
384 " this as a bug unless you can reproduce it without enabling"
386 dump_active_plugins (stderr);
389 if (diagnostic->kind == DK_ICE)
391 #ifndef ENABLE_CHECKING
392 /* When not checking, ICEs are converted to fatal errors when an
393 error has already occurred. This is counteracted by
395 if ((diagnostic_kind_count (context, DK_ERROR) > 0
396 || diagnostic_kind_count (context, DK_SORRY) > 0)
397 && !context->abort_on_error)
399 expanded_location s = expand_location (diagnostic->location);
400 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
402 exit (ICE_EXIT_CODE);
405 if (context->internal_error)
406 (*context->internal_error) (diagnostic->message.format_spec,
407 diagnostic->message.args_ptr);
409 ++diagnostic_kind_count (context, diagnostic->kind);
411 saved_format_spec = diagnostic->message.format_spec;
412 if (context->show_option_requested)
414 const char * option_text = NULL;
416 if (diagnostic->option_index)
418 /* A warning classified as an error. */
419 if ((orig_diag_kind == DK_WARNING || orig_diag_kind == DK_PEDWARN)
420 && diagnostic->kind == DK_ERROR)
422 = ACONCAT ((cl_options[OPT_Werror_].opt_text,
423 /* Skip over "-W". */
424 cl_options[diagnostic->option_index].opt_text + 2,
426 /* A warning with option. */
428 option_text = cl_options[diagnostic->option_index].opt_text;
430 /* A warning without option classified as an error. */
431 else if (orig_diag_kind == DK_WARNING || orig_diag_kind == DK_PEDWARN
432 || diagnostic->kind == DK_WARNING)
434 if (context->warning_as_error_requested)
435 option_text = cl_options[OPT_Werror].opt_text;
437 option_text = _("enabled by default");
441 diagnostic->message.format_spec
442 = ACONCAT ((diagnostic->message.format_spec,
444 "[", option_text, "]",
447 diagnostic->message.locus = &diagnostic->location;
448 diagnostic->message.x_data = &diagnostic->x_data;
449 diagnostic->x_data = NULL;
450 pp_format (context->printer, &diagnostic->message);
451 (*diagnostic_starter (context)) (context, diagnostic);
452 pp_output_formatted_text (context->printer);
453 (*diagnostic_finalizer (context)) (context, diagnostic);
454 pp_flush (context->printer);
455 diagnostic_action_after_output (context, diagnostic);
456 diagnostic->message.format_spec = saved_format_spec;
457 diagnostic->x_data = NULL;
464 /* Given a partial pathname as input, return another pathname that
465 shares no directory elements with the pathname of __FILE__. This
466 is used by fancy_abort() to print `Internal compiler error in expr.c'
467 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
470 trim_filename (const char *name)
472 static const char this_file[] = __FILE__;
473 const char *p = name, *q = this_file;
475 /* First skip any "../" in each filename. This allows us to give a proper
476 reference to a file in a subdirectory. */
477 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
480 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
483 /* Now skip any parts the two filenames have in common. */
484 while (*p == *q && *p != 0 && *q != 0)
487 /* Now go backwards until the previous directory separator. */
488 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
494 /* Standard error reporting routines in increasing order of severity.
495 All of these take arguments like printf. */
497 /* Text to be emitted verbatim to the error message stream; this
498 produces no prefix and disables line-wrapping. Use rarely. */
500 verbatim (const char *gmsgid, ...)
505 va_start (ap, gmsgid);
508 text.format_spec = _(gmsgid);
511 pp_format_verbatim (global_dc->printer, &text);
512 pp_flush (global_dc->printer);
517 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
518 const char *gmsgid, ...)
520 diagnostic_info diagnostic;
523 va_start (ap, gmsgid);
524 if (kind == DK_PERMERROR)
526 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
527 permissive_error_kind ());
528 diagnostic.option_index = OPT_fpermissive;
531 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, kind);
532 if (kind == DK_WARNING || kind == DK_PEDWARN)
533 diagnostic.option_index = opt;
537 return report_diagnostic (&diagnostic);
540 /* An informative note at LOCATION. Use this for additional details on an error
543 inform (location_t location, const char *gmsgid, ...)
545 diagnostic_info diagnostic;
548 va_start (ap, gmsgid);
549 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
550 report_diagnostic (&diagnostic);
554 /* An informative note at LOCATION. Use this for additional details on an
557 inform_n (location_t location, int n, const char *singular_gmsgid,
558 const char *plural_gmsgid, ...)
560 diagnostic_info diagnostic;
563 va_start (ap, plural_gmsgid);
564 diagnostic_set_info_translated (&diagnostic,
565 ngettext (singular_gmsgid, plural_gmsgid, n),
566 &ap, location, DK_NOTE);
567 report_diagnostic (&diagnostic);
571 /* A warning at INPUT_LOCATION. Use this for code which is correct according
572 to the relevant language specification but is likely to be buggy anyway.
573 Returns true if the warning was printed, false if it was inhibited. */
575 warning (int opt, const char *gmsgid, ...)
577 diagnostic_info diagnostic;
580 va_start (ap, gmsgid);
581 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
582 diagnostic.option_index = opt;
585 return report_diagnostic (&diagnostic);
588 /* A warning at LOCATION. Use this for code which is correct according to the
589 relevant language specification but is likely to be buggy anyway.
590 Returns true if the warning was printed, false if it was inhibited. */
593 warning_at (location_t location, int opt, const char *gmsgid, ...)
595 diagnostic_info diagnostic;
598 va_start (ap, gmsgid);
599 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_WARNING);
600 diagnostic.option_index = opt;
602 return report_diagnostic (&diagnostic);
605 /* A "pedantic" warning at LOCATION: issues a warning unless
606 -pedantic-errors was given on the command line, in which case it
607 issues an error. Use this for diagnostics required by the relevant
608 language standard, if you have chosen not to make them errors.
610 Note that these diagnostics are issued independent of the setting
611 of the -pedantic command-line switch. To get a warning enabled
612 only with that switch, use either "if (pedantic) pedwarn
613 (OPT_pedantic,...)" or just "pedwarn (OPT_pedantic,..)". To get a
614 pedwarn independently of the -pedantic switch use "pedwarn (0,...)".
616 Returns true if the warning was printed, false if it was inhibited. */
619 pedwarn (location_t location, int opt, const char *gmsgid, ...)
621 diagnostic_info diagnostic;
624 va_start (ap, gmsgid);
625 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_PEDWARN);
626 diagnostic.option_index = opt;
628 return report_diagnostic (&diagnostic);
631 /* A "permissive" error at LOCATION: issues an error unless
632 -fpermissive was given on the command line, in which case it issues
633 a warning. Use this for things that really should be errors but we
634 want to support legacy code.
636 Returns true if the warning was printed, false if it was inhibited. */
639 permerror (location_t location, const char *gmsgid, ...)
641 diagnostic_info diagnostic;
644 va_start (ap, gmsgid);
645 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
646 permissive_error_kind ());
647 diagnostic.option_index = OPT_fpermissive;
649 return report_diagnostic (&diagnostic);
652 /* A hard error: the code is definitely ill-formed, and an object file
653 will not be produced. */
655 error (const char *gmsgid, ...)
657 diagnostic_info diagnostic;
660 va_start (ap, gmsgid);
661 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
662 report_diagnostic (&diagnostic);
666 /* A hard error: the code is definitely ill-formed, and an object file
667 will not be produced. */
669 error_n (location_t location, int n, const char *singular_gmsgid,
670 const char *plural_gmsgid, ...)
672 diagnostic_info diagnostic;
675 va_start (ap, plural_gmsgid);
676 diagnostic_set_info_translated (&diagnostic,
677 ngettext (singular_gmsgid, plural_gmsgid, n),
678 &ap, location, DK_ERROR);
679 report_diagnostic (&diagnostic);
683 /* Same as ebove, but use location LOC instead of input_location. */
685 error_at (location_t loc, const char *gmsgid, ...)
687 diagnostic_info diagnostic;
690 va_start (ap, gmsgid);
691 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_ERROR);
692 report_diagnostic (&diagnostic);
696 /* "Sorry, not implemented." Use for a language feature which is
697 required by the relevant specification but not implemented by GCC.
698 An object file will not be produced. */
700 sorry (const char *gmsgid, ...)
702 diagnostic_info diagnostic;
705 va_start (ap, gmsgid);
706 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
707 report_diagnostic (&diagnostic);
711 /* An error which is severe enough that we make no attempt to
712 continue. Do not use this for internal consistency checks; that's
713 internal_error. Use of this function should be rare. */
715 fatal_error (const char *gmsgid, ...)
717 diagnostic_info diagnostic;
720 va_start (ap, gmsgid);
721 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
722 report_diagnostic (&diagnostic);
728 /* An internal consistency check has failed. We make no attempt to
729 continue. Note that unless there is debugging value to be had from
730 a more specific message, or some other good reason, you should use
731 abort () instead of calling this function directly. */
733 internal_error (const char *gmsgid, ...)
735 diagnostic_info diagnostic;
738 va_start (ap, gmsgid);
739 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
740 report_diagnostic (&diagnostic);
746 /* Special case error functions. Most are implemented in terms of the
747 above, or should be. */
749 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
750 runs its second argument through gettext. */
752 fnotice (FILE *file, const char *cmsgid, ...)
756 va_start (ap, cmsgid);
757 vfprintf (file, _(cmsgid), ap);
761 /* Inform the user that an error occurred while trying to report some
762 other error. This indicates catastrophic internal inconsistencies,
763 so give up now. But do try to flush out the previous error.
764 This mustn't use internal_error, that will cause infinite recursion. */
767 error_recursion (diagnostic_context *context)
769 diagnostic_info diagnostic;
771 if (context->lock < 3)
772 pp_flush (context->printer);
775 "Internal compiler error: Error reporting routines re-entered.\n");
777 /* Call diagnostic_action_after_output to get the "please submit a bug
778 report" message. It only looks at the kind field of diagnostic_info. */
779 diagnostic.kind = DK_ICE;
780 diagnostic_action_after_output (context, &diagnostic);
782 /* Do not use gcc_unreachable here; that goes through internal_error
783 and therefore would cause infinite recursion. */
787 /* Report an internal compiler error in a friendly manner. This is
788 the function that gets called upon use of abort() in the source
789 code generally, thanks to a special macro. */
792 fancy_abort (const char *file, int line, const char *function)
794 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
797 /* Really call the system 'abort'. This has to go right at the end of
798 this file, so that there are no functions after it that call abort
799 and get the system abort instead of our macro. */