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"
33 #include "diagnostic.h"
36 #define pedantic_warning_kind(DC) \
37 ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
38 #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR)
41 static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
43 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
45 static void diagnostic_action_after_output (diagnostic_context *,
47 static void real_abort (void) ATTRIBUTE_NORETURN;
49 /* A diagnostic_context surrogate for stderr. */
50 static diagnostic_context global_diagnostic_context;
51 diagnostic_context *global_dc = &global_diagnostic_context;
54 /* Return a malloc'd string containing MSG formatted a la printf. The
55 caller is responsible for freeing the memory. */
57 build_message_string (const char *msg, ...)
63 vasprintf (&str, msg, ap);
69 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
71 file_name_as_prefix (const char *f)
73 return build_message_string ("%s: ", f);
78 /* Initialize the diagnostic message outputting machinery. */
80 diagnostic_initialize (diagnostic_context *context)
82 /* Allocate a basic pretty-printer. Clients will replace this a
83 much more elaborated pretty-printer if they wish. */
84 context->printer = XNEW (pretty_printer);
85 pp_construct (context->printer, NULL, 0);
86 /* By default, diagnostics are sent to stderr. */
87 context->printer->buffer->stream = stderr;
88 /* By default, we emit prefixes once per message. */
89 context->printer->wrapping.rule = DIAGNOSTICS_SHOW_PREFIX_ONCE;
91 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
92 context->some_warnings_are_errors = false;
93 context->warning_as_error_requested = false;
94 memset (context->classify_diagnostic, DK_UNSPECIFIED,
95 sizeof context->classify_diagnostic);
96 context->show_option_requested = false;
97 context->abort_on_error = false;
98 context->internal_error = NULL;
99 diagnostic_starter (context) = default_diagnostic_starter;
100 diagnostic_finalizer (context) = default_diagnostic_finalizer;
101 context->last_module = 0;
102 context->x_data = NULL;
104 context->inhibit_notes_p = false;
107 /* Do any cleaning up required after the last diagnostic is emitted. */
110 diagnostic_finish (diagnostic_context *context)
112 /* Some of the errors may actually have been warnings. */
113 if (context->some_warnings_are_errors)
115 /* -Werror was given. */
116 if (context->warning_as_error_requested)
117 pp_verbatim (context->printer,
118 _("%s: all warnings being treated as errors\n"),
120 /* At least one -Werror= was given. */
122 pp_verbatim (context->printer,
123 _("%s: some warnings being treated as errors\n"),
125 pp_flush (context->printer);
129 /* Initialize DIAGNOSTIC, where the message MSG has already been
132 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
133 va_list *args, location_t location,
136 diagnostic->message.err_no = errno;
137 diagnostic->message.args_ptr = args;
138 diagnostic->message.format_spec = msg;
139 diagnostic->location = location;
140 diagnostic->override_column = 0;
141 diagnostic->kind = kind;
142 diagnostic->option_index = 0;
145 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
148 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
149 va_list *args, location_t location,
152 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
155 /* Return a malloc'd string describing a location. The caller is
156 responsible for freeing the memory. */
158 diagnostic_build_prefix (diagnostic_context *context,
159 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)
176 : context->show_column
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 (context->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 (context->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 (context,
281 default_diagnostic_finalizer (diagnostic_context *context,
282 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
284 pp_destroy_prefix (context->printer);
287 /* Interface to specify diagnostic kind overrides. Returns the
288 previous setting, or DK_UNSPECIFIED if the parameters are out of
291 diagnostic_classify_diagnostic (diagnostic_context *context,
293 diagnostic_t new_kind)
295 diagnostic_t old_kind;
297 if (option_index <= 0
298 || option_index >= N_OPTS
299 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
300 return DK_UNSPECIFIED;
302 old_kind = context->classify_diagnostic[option_index];
303 context->classify_diagnostic[option_index] = new_kind;
307 /* Report a diagnostic message (an error or a warning) as specified by
308 DC. This function is *the* subroutine in terms of which front-ends
309 should implement their specific diagnostic handling modules. The
310 front-end independent format specifiers are exactly those described
311 in the documentation of output_format.
312 Return true if a diagnostic was printed, false otherwise. */
315 diagnostic_report_diagnostic (diagnostic_context *context,
316 diagnostic_info *diagnostic)
318 location_t location = diagnostic->location;
319 diagnostic_t orig_diag_kind = diagnostic->kind;
320 const char *saved_format_spec;
322 /* Give preference to being able to inhibit warnings, before they
323 get reclassified to something else. */
324 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
325 && !diagnostic_report_warnings_p (location))
328 if (diagnostic->kind == DK_PEDWARN)
330 diagnostic->kind = pedantic_warning_kind (context);
331 /* We do this to avoid giving the message for -pedantic-errors. */
332 orig_diag_kind = diagnostic->kind;
335 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
338 if (context->lock > 0)
340 /* If we're reporting an ICE in the middle of some other error,
341 try to flush out the previous error, then let this one
342 through. Don't do this more than once. */
343 if (diagnostic->kind == DK_ICE && context->lock == 1)
344 pp_flush (context->printer);
346 error_recursion (context);
349 /* If the user requested that warnings be treated as errors, so be
350 it. Note that we do this before the next block so that
351 individual warnings can be overridden back to warnings with
353 if (context->warning_as_error_requested
354 && diagnostic->kind == DK_WARNING)
356 diagnostic->kind = DK_ERROR;
359 if (diagnostic->option_index)
361 /* This tests if the user provided the appropriate -Wfoo or
363 if (! option_enabled (diagnostic->option_index))
365 /* This tests if the user provided the appropriate -Werror=foo
367 if (context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
369 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
371 /* This allows for future extensions, like temporarily disabling
372 warnings for ranges of source code. */
373 if (diagnostic->kind == DK_IGNORED)
377 if (orig_diag_kind == DK_WARNING && diagnostic->kind == DK_ERROR)
378 context->some_warnings_are_errors = true;
382 if (diagnostic->kind == DK_ICE)
384 #ifndef ENABLE_CHECKING
385 /* When not checking, ICEs are converted to fatal errors when an
386 error has already occurred. This is counteracted by
388 if ((diagnostic_kind_count (context, DK_ERROR) > 0
389 || diagnostic_kind_count (context, DK_SORRY) > 0)
390 && !context->abort_on_error)
392 expanded_location s = expand_location (diagnostic->location);
393 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
395 exit (ICE_EXIT_CODE);
398 if (context->internal_error)
399 (*context->internal_error) (context,
400 diagnostic->message.format_spec,
401 diagnostic->message.args_ptr);
403 ++diagnostic_kind_count (context, diagnostic->kind);
405 saved_format_spec = diagnostic->message.format_spec;
406 if (context->show_option_requested)
408 const char * option_text = NULL;
410 if (diagnostic->option_index)
412 /* A warning classified as an error. */
413 if ((orig_diag_kind == DK_WARNING || orig_diag_kind == DK_PEDWARN)
414 && diagnostic->kind == DK_ERROR)
416 = ACONCAT ((cl_options[OPT_Werror_].opt_text,
417 /* Skip over "-W". */
418 cl_options[diagnostic->option_index].opt_text + 2,
420 /* A warning with option. */
422 option_text = cl_options[diagnostic->option_index].opt_text;
424 /* A warning without option classified as an error. */
425 else if (orig_diag_kind == DK_WARNING || orig_diag_kind == DK_PEDWARN
426 || diagnostic->kind == DK_WARNING)
428 if (context->warning_as_error_requested)
429 option_text = cl_options[OPT_Werror].opt_text;
431 option_text = _("enabled by default");
435 diagnostic->message.format_spec
436 = ACONCAT ((diagnostic->message.format_spec,
438 "[", option_text, "]",
441 diagnostic->message.locus = &diagnostic->location;
442 diagnostic->message.x_data = &diagnostic->x_data;
443 diagnostic->x_data = NULL;
444 pp_format (context->printer, &diagnostic->message);
445 (*diagnostic_starter (context)) (context, diagnostic);
446 pp_output_formatted_text (context->printer);
447 (*diagnostic_finalizer (context)) (context, diagnostic);
448 pp_flush (context->printer);
449 diagnostic_action_after_output (context, diagnostic);
450 diagnostic->message.format_spec = saved_format_spec;
451 diagnostic->x_data = NULL;
458 /* Given a partial pathname as input, return another pathname that
459 shares no directory elements with the pathname of __FILE__. This
460 is used by fancy_abort() to print `Internal compiler error in expr.c'
461 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
464 trim_filename (const char *name)
466 static const char this_file[] = __FILE__;
467 const char *p = name, *q = this_file;
469 /* First skip any "../" in each filename. This allows us to give a proper
470 reference to a file in a subdirectory. */
471 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
474 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
477 /* Now skip any parts the two filenames have in common. */
478 while (*p == *q && *p != 0 && *q != 0)
481 /* Now go backwards until the previous directory separator. */
482 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
488 /* Standard error reporting routines in increasing order of severity.
489 All of these take arguments like printf. */
491 /* Text to be emitted verbatim to the error message stream; this
492 produces no prefix and disables line-wrapping. Use rarely. */
494 verbatim (const char *gmsgid, ...)
499 va_start (ap, gmsgid);
502 text.format_spec = _(gmsgid);
505 pp_format_verbatim (global_dc->printer, &text);
506 pp_flush (global_dc->printer);
511 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
512 const char *gmsgid, ...)
514 diagnostic_info diagnostic;
517 va_start (ap, gmsgid);
518 if (kind == DK_PERMERROR)
520 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
521 permissive_error_kind (global_dc));
522 diagnostic.option_index = OPT_fpermissive;
525 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, kind);
526 if (kind == DK_WARNING || kind == DK_PEDWARN)
527 diagnostic.option_index = opt;
531 return report_diagnostic (&diagnostic);
534 /* An informative note at LOCATION. Use this for additional details on an error
537 inform (location_t location, const char *gmsgid, ...)
539 diagnostic_info diagnostic;
542 va_start (ap, gmsgid);
543 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
544 report_diagnostic (&diagnostic);
548 /* An informative note at LOCATION. Use this for additional details on an
551 inform_n (location_t location, int n, const char *singular_gmsgid,
552 const char *plural_gmsgid, ...)
554 diagnostic_info diagnostic;
557 va_start (ap, plural_gmsgid);
558 diagnostic_set_info_translated (&diagnostic,
559 ngettext (singular_gmsgid, plural_gmsgid, n),
560 &ap, location, DK_NOTE);
561 report_diagnostic (&diagnostic);
565 /* A warning at INPUT_LOCATION. Use this for code which is correct according
566 to the relevant language specification but is likely to be buggy anyway.
567 Returns true if the warning was printed, false if it was inhibited. */
569 warning (int opt, const char *gmsgid, ...)
571 diagnostic_info diagnostic;
574 va_start (ap, gmsgid);
575 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
576 diagnostic.option_index = opt;
579 return report_diagnostic (&diagnostic);
582 /* A warning at LOCATION. Use this for code which is correct according to the
583 relevant language specification but is likely to be buggy anyway.
584 Returns true if the warning was printed, false if it was inhibited. */
587 warning_at (location_t location, int opt, const char *gmsgid, ...)
589 diagnostic_info diagnostic;
592 va_start (ap, gmsgid);
593 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_WARNING);
594 diagnostic.option_index = opt;
596 return report_diagnostic (&diagnostic);
599 /* A "pedantic" warning at LOCATION: issues a warning unless
600 -pedantic-errors was given on the command line, in which case it
601 issues an error. Use this for diagnostics required by the relevant
602 language standard, if you have chosen not to make them errors.
604 Note that these diagnostics are issued independent of the setting
605 of the -pedantic command-line switch. To get a warning enabled
606 only with that switch, use either "if (pedantic) pedwarn
607 (OPT_pedantic,...)" or just "pedwarn (OPT_pedantic,..)". To get a
608 pedwarn independently of the -pedantic switch use "pedwarn (0,...)".
610 Returns true if the warning was printed, false if it was inhibited. */
613 pedwarn (location_t location, int opt, const char *gmsgid, ...)
615 diagnostic_info diagnostic;
618 va_start (ap, gmsgid);
619 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_PEDWARN);
620 diagnostic.option_index = opt;
622 return report_diagnostic (&diagnostic);
625 /* A "permissive" error at LOCATION: issues an error unless
626 -fpermissive was given on the command line, in which case it issues
627 a warning. Use this for things that really should be errors but we
628 want to support legacy code.
630 Returns true if the warning was printed, false if it was inhibited. */
633 permerror (location_t location, const char *gmsgid, ...)
635 diagnostic_info diagnostic;
638 va_start (ap, gmsgid);
639 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
640 permissive_error_kind (global_dc));
641 diagnostic.option_index = OPT_fpermissive;
643 return report_diagnostic (&diagnostic);
646 /* A hard error: the code is definitely ill-formed, and an object file
647 will not be produced. */
649 error (const char *gmsgid, ...)
651 diagnostic_info diagnostic;
654 va_start (ap, gmsgid);
655 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
656 report_diagnostic (&diagnostic);
660 /* A hard error: the code is definitely ill-formed, and an object file
661 will not be produced. */
663 error_n (location_t location, int n, const char *singular_gmsgid,
664 const char *plural_gmsgid, ...)
666 diagnostic_info diagnostic;
669 va_start (ap, plural_gmsgid);
670 diagnostic_set_info_translated (&diagnostic,
671 ngettext (singular_gmsgid, plural_gmsgid, n),
672 &ap, location, DK_ERROR);
673 report_diagnostic (&diagnostic);
677 /* Same as ebove, but use location LOC instead of input_location. */
679 error_at (location_t loc, const char *gmsgid, ...)
681 diagnostic_info diagnostic;
684 va_start (ap, gmsgid);
685 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_ERROR);
686 report_diagnostic (&diagnostic);
690 /* "Sorry, not implemented." Use for a language feature which is
691 required by the relevant specification but not implemented by GCC.
692 An object file will not be produced. */
694 sorry (const char *gmsgid, ...)
696 diagnostic_info diagnostic;
699 va_start (ap, gmsgid);
700 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
701 report_diagnostic (&diagnostic);
705 /* An error which is severe enough that we make no attempt to
706 continue. Do not use this for internal consistency checks; that's
707 internal_error. Use of this function should be rare. */
709 fatal_error (const char *gmsgid, ...)
711 diagnostic_info diagnostic;
714 va_start (ap, gmsgid);
715 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
716 report_diagnostic (&diagnostic);
722 /* An internal consistency check has failed. We make no attempt to
723 continue. Note that unless there is debugging value to be had from
724 a more specific message, or some other good reason, you should use
725 abort () instead of calling this function directly. */
727 internal_error (const char *gmsgid, ...)
729 diagnostic_info diagnostic;
732 va_start (ap, gmsgid);
733 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
734 report_diagnostic (&diagnostic);
740 /* Special case error functions. Most are implemented in terms of the
741 above, or should be. */
743 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
744 runs its second argument through gettext. */
746 fnotice (FILE *file, const char *cmsgid, ...)
750 va_start (ap, cmsgid);
751 vfprintf (file, _(cmsgid), ap);
755 /* Inform the user that an error occurred while trying to report some
756 other error. This indicates catastrophic internal inconsistencies,
757 so give up now. But do try to flush out the previous error.
758 This mustn't use internal_error, that will cause infinite recursion. */
761 error_recursion (diagnostic_context *context)
763 diagnostic_info diagnostic;
765 if (context->lock < 3)
766 pp_flush (context->printer);
769 "Internal compiler error: Error reporting routines re-entered.\n");
771 /* Call diagnostic_action_after_output to get the "please submit a bug
772 report" message. It only looks at the kind field of diagnostic_info. */
773 diagnostic.kind = DK_ICE;
774 diagnostic_action_after_output (context, &diagnostic);
776 /* Do not use gcc_unreachable here; that goes through internal_error
777 and therefore would cause infinite recursion. */
781 /* Report an internal compiler error in a friendly manner. This is
782 the function that gets called upon use of abort() in the source
783 code generally, thanks to a special macro. */
786 fancy_abort (const char *file, int line, const char *function)
788 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
791 /* Really call the system 'abort'. This has to go right at the end of
792 this file, so that there are no functions after it that call abort
793 and get the system abort instead of our macro. */