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"
35 #define pedantic_warning_kind(DC) \
36 ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
37 #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR)
38 #define permissive_error_option(DC) ((DC)->opt_permissive)
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, int n_opts)
84 /* Allocate a basic pretty-printer. Clients will replace this a
85 much more elaborated pretty-printer if they wish. */
86 context->printer = XNEW (pretty_printer);
87 pp_construct (context->printer, NULL, 0);
88 /* By default, diagnostics are sent to stderr. */
89 context->printer->buffer->stream = stderr;
90 /* By default, we emit prefixes once per message. */
91 context->printer->wrapping.rule = DIAGNOSTICS_SHOW_PREFIX_ONCE;
93 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
94 context->some_warnings_are_errors = false;
95 context->warning_as_error_requested = false;
96 context->n_opts = n_opts;
97 context->classify_diagnostic = XNEWVEC (diagnostic_t, n_opts);
98 for (i = 0; i < n_opts; i++)
99 context->classify_diagnostic[i] = DK_UNSPECIFIED;
100 context->show_option_requested = false;
101 context->abort_on_error = false;
102 context->show_column = false;
103 context->pedantic_errors = false;
104 context->permissive = false;
105 context->opt_permissive = 0;
106 context->fatal_errors = false;
107 context->inhibit_warnings = false;
108 context->warn_system_headers = false;
109 context->internal_error = NULL;
110 diagnostic_starter (context) = default_diagnostic_starter;
111 diagnostic_finalizer (context) = default_diagnostic_finalizer;
112 context->option_enabled = NULL;
113 context->option_name = NULL;
114 context->last_module = 0;
115 context->x_data = NULL;
117 context->inhibit_notes_p = false;
120 /* Do any cleaning up required after the last diagnostic is emitted. */
123 diagnostic_finish (diagnostic_context *context)
125 /* Some of the errors may actually have been warnings. */
126 if (context->some_warnings_are_errors)
128 /* -Werror was given. */
129 if (context->warning_as_error_requested)
130 pp_verbatim (context->printer,
131 _("%s: all warnings being treated as errors\n"),
133 /* At least one -Werror= was given. */
135 pp_verbatim (context->printer,
136 _("%s: some warnings being treated as errors\n"),
138 pp_flush (context->printer);
142 /* Initialize DIAGNOSTIC, where the message MSG has already been
145 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
146 va_list *args, location_t location,
149 diagnostic->message.err_no = errno;
150 diagnostic->message.args_ptr = args;
151 diagnostic->message.format_spec = msg;
152 diagnostic->location = location;
153 diagnostic->override_column = 0;
154 diagnostic->kind = kind;
155 diagnostic->option_index = 0;
158 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
161 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
162 va_list *args, location_t location,
165 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
168 /* Return a malloc'd string describing a location. The caller is
169 responsible for freeing the memory. */
171 diagnostic_build_prefix (diagnostic_context *context,
172 diagnostic_info *diagnostic)
174 static const char *const diagnostic_kind_text[] = {
175 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
176 #include "diagnostic.def"
177 #undef DEFINE_DIAGNOSTIC_KIND
180 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
181 expanded_location s = expand_location (diagnostic->location);
182 if (diagnostic->override_column)
183 s.column = diagnostic->override_column;
184 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
188 ? build_message_string ("%s: %s", progname, text)
189 : context->show_column
190 ? build_message_string ("%s:%d:%d: %s", s.file, s.line, s.column, text)
191 : build_message_string ("%s:%d: %s", s.file, s.line, text));
194 /* Take any action which is expected to happen after the diagnostic
195 is written out. This function does not always return. */
197 diagnostic_action_after_output (diagnostic_context *context,
198 diagnostic_info *diagnostic)
200 switch (diagnostic->kind)
210 if (context->abort_on_error)
212 if (context->fatal_errors)
214 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
215 diagnostic_finish (context);
216 exit (FATAL_EXIT_CODE);
221 if (context->abort_on_error)
224 fnotice (stderr, "Please submit a full bug report,\n"
225 "with preprocessed source if appropriate.\n"
226 "See %s for instructions.\n", bug_report_url);
227 exit (ICE_EXIT_CODE);
230 if (context->abort_on_error)
232 diagnostic_finish (context);
233 fnotice (stderr, "compilation terminated.\n");
234 exit (FATAL_EXIT_CODE);
242 diagnostic_report_current_module (diagnostic_context *context)
244 const struct line_map *map;
246 if (pp_needs_newline (context->printer))
248 pp_newline (context->printer);
249 pp_needs_newline (context->printer) = false;
252 if (input_location <= BUILTINS_LOCATION)
255 map = linemap_lookup (line_table, input_location);
256 if (map && diagnostic_last_module_changed (context, map))
258 diagnostic_set_last_module (context, map);
259 if (! MAIN_FILE_P (map))
261 map = INCLUDED_FROM (line_table, map);
262 if (context->show_column)
263 pp_verbatim (context->printer,
264 "In file included from %s:%d:%d",
266 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
268 pp_verbatim (context->printer,
269 "In file included from %s:%d",
270 map->to_file, LAST_SOURCE_LINE (map));
271 while (! MAIN_FILE_P (map))
273 map = INCLUDED_FROM (line_table, map);
274 pp_verbatim (context->printer,
276 map->to_file, LAST_SOURCE_LINE (map));
278 pp_verbatim (context->printer, ":");
279 pp_newline (context->printer);
285 default_diagnostic_starter (diagnostic_context *context,
286 diagnostic_info *diagnostic)
288 diagnostic_report_current_module (context);
289 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
294 default_diagnostic_finalizer (diagnostic_context *context,
295 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
297 pp_destroy_prefix (context->printer);
300 /* Interface to specify diagnostic kind overrides. Returns the
301 previous setting, or DK_UNSPECIFIED if the parameters are out of
304 diagnostic_classify_diagnostic (diagnostic_context *context,
306 diagnostic_t new_kind)
308 diagnostic_t old_kind;
310 if (option_index <= 0
311 || option_index >= context->n_opts
312 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
313 return DK_UNSPECIFIED;
315 old_kind = context->classify_diagnostic[option_index];
316 context->classify_diagnostic[option_index] = new_kind;
320 /* Report a diagnostic message (an error or a warning) as specified by
321 DC. This function is *the* subroutine in terms of which front-ends
322 should implement their specific diagnostic handling modules. The
323 front-end independent format specifiers are exactly those described
324 in the documentation of output_format.
325 Return true if a diagnostic was printed, false otherwise. */
328 diagnostic_report_diagnostic (diagnostic_context *context,
329 diagnostic_info *diagnostic)
331 location_t location = diagnostic->location;
332 diagnostic_t orig_diag_kind = diagnostic->kind;
333 const char *saved_format_spec;
335 /* Give preference to being able to inhibit warnings, before they
336 get reclassified to something else. */
337 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
338 && !diagnostic_report_warnings_p (context, location))
341 if (diagnostic->kind == DK_PEDWARN)
343 diagnostic->kind = pedantic_warning_kind (context);
344 /* We do this to avoid giving the message for -pedantic-errors. */
345 orig_diag_kind = diagnostic->kind;
348 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
351 if (context->lock > 0)
353 /* If we're reporting an ICE in the middle of some other error,
354 try to flush out the previous error, then let this one
355 through. Don't do this more than once. */
356 if (diagnostic->kind == DK_ICE && context->lock == 1)
357 pp_flush (context->printer);
359 error_recursion (context);
362 /* If the user requested that warnings be treated as errors, so be
363 it. Note that we do this before the next block so that
364 individual warnings can be overridden back to warnings with
366 if (context->warning_as_error_requested
367 && diagnostic->kind == DK_WARNING)
369 diagnostic->kind = DK_ERROR;
372 if (diagnostic->option_index)
374 /* This tests if the user provided the appropriate -Wfoo or
376 if (! context->option_enabled (diagnostic->option_index))
378 /* This tests if the user provided the appropriate -Werror=foo
380 if (context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
382 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
384 /* This allows for future extensions, like temporarily disabling
385 warnings for ranges of source code. */
386 if (diagnostic->kind == DK_IGNORED)
390 if (orig_diag_kind == DK_WARNING && diagnostic->kind == DK_ERROR)
391 context->some_warnings_are_errors = true;
395 if (diagnostic->kind == DK_ICE)
397 #ifndef ENABLE_CHECKING
398 /* When not checking, ICEs are converted to fatal errors when an
399 error has already occurred. This is counteracted by
401 if ((diagnostic_kind_count (context, DK_ERROR) > 0
402 || diagnostic_kind_count (context, DK_SORRY) > 0)
403 && !context->abort_on_error)
405 expanded_location s = expand_location (diagnostic->location);
406 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
408 exit (ICE_EXIT_CODE);
411 if (context->internal_error)
412 (*context->internal_error) (context,
413 diagnostic->message.format_spec,
414 diagnostic->message.args_ptr);
416 ++diagnostic_kind_count (context, diagnostic->kind);
418 saved_format_spec = diagnostic->message.format_spec;
419 if (context->show_option_requested)
423 option_text = context->option_name (context, diagnostic->option_index,
424 orig_diag_kind, diagnostic->kind);
428 diagnostic->message.format_spec
429 = ACONCAT ((diagnostic->message.format_spec,
431 "[", option_text, "]",
436 diagnostic->message.locus = &diagnostic->location;
437 diagnostic->message.x_data = &diagnostic->x_data;
438 diagnostic->x_data = NULL;
439 pp_format (context->printer, &diagnostic->message);
440 (*diagnostic_starter (context)) (context, diagnostic);
441 pp_output_formatted_text (context->printer);
442 (*diagnostic_finalizer (context)) (context, diagnostic);
443 pp_flush (context->printer);
444 diagnostic_action_after_output (context, diagnostic);
445 diagnostic->message.format_spec = saved_format_spec;
446 diagnostic->x_data = NULL;
453 /* Given a partial pathname as input, return another pathname that
454 shares no directory elements with the pathname of __FILE__. This
455 is used by fancy_abort() to print `Internal compiler error in expr.c'
456 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
459 trim_filename (const char *name)
461 static const char this_file[] = __FILE__;
462 const char *p = name, *q = this_file;
464 /* First skip any "../" in each filename. This allows us to give a proper
465 reference to a file in a subdirectory. */
466 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
469 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
472 /* Now skip any parts the two filenames have in common. */
473 while (*p == *q && *p != 0 && *q != 0)
476 /* Now go backwards until the previous directory separator. */
477 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
483 /* Standard error reporting routines in increasing order of severity.
484 All of these take arguments like printf. */
486 /* Text to be emitted verbatim to the error message stream; this
487 produces no prefix and disables line-wrapping. Use rarely. */
489 verbatim (const char *gmsgid, ...)
494 va_start (ap, gmsgid);
497 text.format_spec = _(gmsgid);
500 pp_format_verbatim (global_dc->printer, &text);
501 pp_flush (global_dc->printer);
506 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
507 const char *gmsgid, ...)
509 diagnostic_info diagnostic;
512 va_start (ap, gmsgid);
513 if (kind == DK_PERMERROR)
515 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
516 permissive_error_kind (global_dc));
517 diagnostic.option_index = permissive_error_option (global_dc);
520 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, kind);
521 if (kind == DK_WARNING || kind == DK_PEDWARN)
522 diagnostic.option_index = opt;
526 return report_diagnostic (&diagnostic);
529 /* An informative note at LOCATION. Use this for additional details on an error
532 inform (location_t location, const char *gmsgid, ...)
534 diagnostic_info diagnostic;
537 va_start (ap, gmsgid);
538 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
539 report_diagnostic (&diagnostic);
543 /* An informative note at LOCATION. Use this for additional details on an
546 inform_n (location_t location, int n, const char *singular_gmsgid,
547 const char *plural_gmsgid, ...)
549 diagnostic_info diagnostic;
552 va_start (ap, plural_gmsgid);
553 diagnostic_set_info_translated (&diagnostic,
554 ngettext (singular_gmsgid, plural_gmsgid, n),
555 &ap, location, DK_NOTE);
556 report_diagnostic (&diagnostic);
560 /* A warning at INPUT_LOCATION. Use this for code which is correct according
561 to the relevant language specification but is likely to be buggy anyway.
562 Returns true if the warning was printed, false if it was inhibited. */
564 warning (int opt, const char *gmsgid, ...)
566 diagnostic_info diagnostic;
569 va_start (ap, gmsgid);
570 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
571 diagnostic.option_index = opt;
574 return report_diagnostic (&diagnostic);
577 /* A warning at LOCATION. Use this for code which is correct according to the
578 relevant language specification but is likely to be buggy anyway.
579 Returns true if the warning was printed, false if it was inhibited. */
582 warning_at (location_t location, int opt, const char *gmsgid, ...)
584 diagnostic_info diagnostic;
587 va_start (ap, gmsgid);
588 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_WARNING);
589 diagnostic.option_index = opt;
591 return report_diagnostic (&diagnostic);
594 /* A "pedantic" warning at LOCATION: issues a warning unless
595 -pedantic-errors was given on the command line, in which case it
596 issues an error. Use this for diagnostics required by the relevant
597 language standard, if you have chosen not to make them errors.
599 Note that these diagnostics are issued independent of the setting
600 of the -pedantic command-line switch. To get a warning enabled
601 only with that switch, use either "if (pedantic) pedwarn
602 (OPT_pedantic,...)" or just "pedwarn (OPT_pedantic,..)". To get a
603 pedwarn independently of the -pedantic switch use "pedwarn (0,...)".
605 Returns true if the warning was printed, false if it was inhibited. */
608 pedwarn (location_t location, int opt, const char *gmsgid, ...)
610 diagnostic_info diagnostic;
613 va_start (ap, gmsgid);
614 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_PEDWARN);
615 diagnostic.option_index = opt;
617 return report_diagnostic (&diagnostic);
620 /* A "permissive" error at LOCATION: issues an error unless
621 -fpermissive was given on the command line, in which case it issues
622 a warning. Use this for things that really should be errors but we
623 want to support legacy code.
625 Returns true if the warning was printed, false if it was inhibited. */
628 permerror (location_t location, const char *gmsgid, ...)
630 diagnostic_info diagnostic;
633 va_start (ap, gmsgid);
634 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
635 permissive_error_kind (global_dc));
636 diagnostic.option_index = permissive_error_option (global_dc);
638 return report_diagnostic (&diagnostic);
641 /* A hard error: the code is definitely ill-formed, and an object file
642 will not be produced. */
644 error (const char *gmsgid, ...)
646 diagnostic_info diagnostic;
649 va_start (ap, gmsgid);
650 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
651 report_diagnostic (&diagnostic);
655 /* A hard error: the code is definitely ill-formed, and an object file
656 will not be produced. */
658 error_n (location_t location, int n, const char *singular_gmsgid,
659 const char *plural_gmsgid, ...)
661 diagnostic_info diagnostic;
664 va_start (ap, plural_gmsgid);
665 diagnostic_set_info_translated (&diagnostic,
666 ngettext (singular_gmsgid, plural_gmsgid, n),
667 &ap, location, DK_ERROR);
668 report_diagnostic (&diagnostic);
672 /* Same as ebove, but use location LOC instead of input_location. */
674 error_at (location_t loc, const char *gmsgid, ...)
676 diagnostic_info diagnostic;
679 va_start (ap, gmsgid);
680 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_ERROR);
681 report_diagnostic (&diagnostic);
685 /* "Sorry, not implemented." Use for a language feature which is
686 required by the relevant specification but not implemented by GCC.
687 An object file will not be produced. */
689 sorry (const char *gmsgid, ...)
691 diagnostic_info diagnostic;
694 va_start (ap, gmsgid);
695 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
696 report_diagnostic (&diagnostic);
700 /* An error which is severe enough that we make no attempt to
701 continue. Do not use this for internal consistency checks; that's
702 internal_error. Use of this function should be rare. */
704 fatal_error (const char *gmsgid, ...)
706 diagnostic_info diagnostic;
709 va_start (ap, gmsgid);
710 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
711 report_diagnostic (&diagnostic);
717 /* An internal consistency check has failed. We make no attempt to
718 continue. Note that unless there is debugging value to be had from
719 a more specific message, or some other good reason, you should use
720 abort () instead of calling this function directly. */
722 internal_error (const char *gmsgid, ...)
724 diagnostic_info diagnostic;
727 va_start (ap, gmsgid);
728 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
729 report_diagnostic (&diagnostic);
735 /* Special case error functions. Most are implemented in terms of the
736 above, or should be. */
738 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
739 runs its second argument through gettext. */
741 fnotice (FILE *file, const char *cmsgid, ...)
745 va_start (ap, cmsgid);
746 vfprintf (file, _(cmsgid), ap);
750 /* Inform the user that an error occurred while trying to report some
751 other error. This indicates catastrophic internal inconsistencies,
752 so give up now. But do try to flush out the previous error.
753 This mustn't use internal_error, that will cause infinite recursion. */
756 error_recursion (diagnostic_context *context)
758 diagnostic_info diagnostic;
760 if (context->lock < 3)
761 pp_flush (context->printer);
764 "Internal compiler error: Error reporting routines re-entered.\n");
766 /* Call diagnostic_action_after_output to get the "please submit a bug
767 report" message. It only looks at the kind field of diagnostic_info. */
768 diagnostic.kind = DK_ICE;
769 diagnostic_action_after_output (context, &diagnostic);
771 /* Do not use gcc_unreachable here; that goes through internal_error
772 and therefore would cause infinite recursion. */
776 /* Report an internal compiler error in a friendly manner. This is
777 the function that gets called upon use of abort() in the source
778 code generally, thanks to a special macro. */
781 fancy_abort (const char *file, int line, const char *function)
783 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
786 /* Really call the system 'abort'. This has to go right at the end of
787 this file, so that there are no functions after it that call abort
788 and get the system abort instead of our macro. */