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 const struct line_map *map;
274 if (pp_needs_newline (context->printer))
276 pp_newline (context->printer);
277 pp_needs_newline (context->printer) = false;
280 if (input_location <= BUILTINS_LOCATION)
283 map = linemap_lookup (line_table, input_location);
284 if (map && diagnostic_last_module_changed (context, map))
286 diagnostic_set_last_module (context, map);
287 if (! MAIN_FILE_P (map))
289 map = INCLUDED_FROM (line_table, map);
290 pp_verbatim (context->printer,
291 "In file included from %s:%d",
292 map->to_file, LAST_SOURCE_LINE (map));
293 while (! MAIN_FILE_P (map))
295 map = INCLUDED_FROM (line_table, map);
296 pp_verbatim (context->printer,
298 map->to_file, LAST_SOURCE_LINE (map));
300 pp_verbatim (context->printer, ":");
301 pp_newline (context->printer);
307 default_diagnostic_starter (diagnostic_context *context,
308 diagnostic_info *diagnostic)
310 diagnostic_report_current_function (context, diagnostic);
311 pp_set_prefix (context->printer, diagnostic_build_prefix (diagnostic));
315 default_diagnostic_finalizer (diagnostic_context *context,
316 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
318 pp_destroy_prefix (context->printer);
321 /* Interface to specify diagnostic kind overrides. Returns the
322 previous setting, or DK_UNSPECIFIED if the parameters are out of
325 diagnostic_classify_diagnostic (diagnostic_context *context,
327 diagnostic_t new_kind)
329 diagnostic_t old_kind;
331 if (option_index <= 0
332 || option_index >= N_OPTS
333 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
334 return DK_UNSPECIFIED;
336 old_kind = context->classify_diagnostic[option_index];
337 context->classify_diagnostic[option_index] = new_kind;
341 /* Report a diagnostic message (an error or a warning) as specified by
342 DC. This function is *the* subroutine in terms of which front-ends
343 should implement their specific diagnostic handling modules. The
344 front-end independent format specifiers are exactly those described
345 in the documentation of output_format. */
348 diagnostic_report_diagnostic (diagnostic_context *context,
349 diagnostic_info *diagnostic)
351 bool maybe_print_warnings_as_errors_message = false;
353 /* Give preference to being able to inhibit warnings, before they
354 get reclassified to something else. */
355 if (diagnostic->kind == DK_WARNING
356 && !diagnostic_report_warnings_p ())
359 if (context->lock > 0)
361 /* If we're reporting an ICE in the middle of some other error,
362 try to flush out the previous error, then let this one
363 through. Don't do this more than once. */
364 if (diagnostic->kind == DK_ICE && context->lock == 1)
365 pp_flush (context->printer);
367 error_recursion (context);
370 /* If the user requested that warnings be treated as errors, so be
371 it. Note that we do this before the next block so that
372 individual warnings can be overridden back to warnings with
374 if (context->warning_as_error_requested
375 && diagnostic->kind == DK_WARNING)
377 diagnostic->kind = DK_ERROR;
378 maybe_print_warnings_as_errors_message = true;
381 if (diagnostic->option_index)
383 /* This tests if the user provided the appropriate -Wfoo or
385 if (! option_enabled (diagnostic->option_index))
387 /* This tests if the user provided the appropriate -Werror=foo
389 if (context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
391 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
392 maybe_print_warnings_as_errors_message = false;
394 /* This allows for future extensions, like temporarily disabling
395 warnings for ranges of source code. */
396 if (diagnostic->kind == DK_IGNORED)
400 /* If we changed the kind due to -Werror, and didn't override it, we
401 need to print this message. */
402 if (context->issue_warnings_are_errors_message
403 && maybe_print_warnings_as_errors_message)
405 pp_verbatim (context->printer,
406 "%s: warnings being treated as errors\n", progname);
407 context->issue_warnings_are_errors_message = false;
412 if (diagnostic_count_diagnostic (context, diagnostic))
414 const char *saved_format_spec = diagnostic->message.format_spec;
416 if (context->show_option_requested && diagnostic->option_index)
417 diagnostic->message.format_spec
418 = ACONCAT ((diagnostic->message.format_spec,
419 " [", cl_options[diagnostic->option_index].opt_text, "]", NULL));
421 diagnostic->message.locus = &diagnostic->location;
422 diagnostic->message.abstract_origin = &diagnostic->abstract_origin;
423 diagnostic->abstract_origin = NULL;
424 pp_format (context->printer, &diagnostic->message);
425 (*diagnostic_starter (context)) (context, diagnostic);
426 pp_output_formatted_text (context->printer);
427 (*diagnostic_finalizer (context)) (context, diagnostic);
428 pp_flush (context->printer);
429 diagnostic_action_after_output (context, diagnostic);
430 diagnostic->message.format_spec = saved_format_spec;
431 diagnostic->abstract_origin = NULL;
437 /* Given a partial pathname as input, return another pathname that
438 shares no directory elements with the pathname of __FILE__. This
439 is used by fancy_abort() to print `Internal compiler error in expr.c'
440 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
443 trim_filename (const char *name)
445 static const char this_file[] = __FILE__;
446 const char *p = name, *q = this_file;
448 /* First skip any "../" in each filename. This allows us to give a proper
449 reference to a file in a subdirectory. */
450 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
453 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
456 /* Now skip any parts the two filenames have in common. */
457 while (*p == *q && *p != 0 && *q != 0)
460 /* Now go backwards until the previous directory separator. */
461 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
467 /* Standard error reporting routines in increasing order of severity.
468 All of these take arguments like printf. */
470 /* Text to be emitted verbatim to the error message stream; this
471 produces no prefix and disables line-wrapping. Use rarely. */
473 verbatim (const char *gmsgid, ...)
478 va_start (ap, gmsgid);
481 text.format_spec = _(gmsgid);
483 text.abstract_origin = NULL;
484 pp_format_verbatim (global_dc->printer, &text);
485 pp_flush (global_dc->printer);
489 /* An informative note. Use this for additional details on an error
492 inform (const char *gmsgid, ...)
494 diagnostic_info diagnostic;
497 va_start (ap, gmsgid);
498 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_NOTE);
499 report_diagnostic (&diagnostic);
503 /* A warning. Use this for code which is correct according to the
504 relevant language specification but is likely to be buggy anyway. */
506 warning (int opt, const char *gmsgid, ...)
508 diagnostic_info diagnostic;
511 va_start (ap, gmsgid);
512 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
513 diagnostic.option_index = opt;
515 report_diagnostic (&diagnostic);
520 warning0 (const char *gmsgid, ...)
522 diagnostic_info diagnostic;
525 va_start (ap, gmsgid);
526 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
527 report_diagnostic (&diagnostic);
531 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
532 given on the command line, in which case it issues an error. Use
533 this for diagnostics required by the relevant language standard,
534 if you have chosen not to make them errors.
536 Note that these diagnostics are issued independent of the setting
537 of the -pedantic command-line switch. To get a warning enabled
538 only with that switch, write "if (pedantic) pedwarn (...);" */
540 pedwarn (const char *gmsgid, ...)
542 diagnostic_info diagnostic;
545 va_start (ap, gmsgid);
546 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location,
547 pedantic_warning_kind ());
548 report_diagnostic (&diagnostic);
552 /* A "permissive" error: issues an error unless -fpermissive was given
553 on the command line, in which case it issues a warning. Use this
554 for things that really should be errors but we want to support
558 permerror (const char *gmsgid, ...)
560 diagnostic_info diagnostic;
563 va_start (ap, gmsgid);
564 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location,
565 permissive_error_kind ());
566 diagnostic.option_index = OPT_fpermissive;
567 report_diagnostic (&diagnostic);
572 /* A hard error: the code is definitely ill-formed, and an object file
573 will not be produced. */
575 error (const char *gmsgid, ...)
577 diagnostic_info diagnostic;
580 va_start (ap, gmsgid);
581 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
582 report_diagnostic (&diagnostic);
586 /* "Sorry, not implemented." Use for a language feature which is
587 required by the relevant specification but not implemented by GCC.
588 An object file will not be produced. */
590 sorry (const char *gmsgid, ...)
592 diagnostic_info diagnostic;
595 va_start (ap, gmsgid);
596 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
597 report_diagnostic (&diagnostic);
601 /* An error which is severe enough that we make no attempt to
602 continue. Do not use this for internal consistency checks; that's
603 internal_error. Use of this function should be rare. */
605 fatal_error (const char *gmsgid, ...)
607 diagnostic_info diagnostic;
610 va_start (ap, gmsgid);
611 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
612 report_diagnostic (&diagnostic);
618 /* An internal consistency check has failed. We make no attempt to
619 continue. Note that unless there is debugging value to be had from
620 a more specific message, or some other good reason, you should use
621 abort () instead of calling this function directly. */
623 internal_error (const char *gmsgid, ...)
625 diagnostic_info diagnostic;
628 va_start (ap, gmsgid);
629 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
630 report_diagnostic (&diagnostic);
636 /* Special case error functions. Most are implemented in terms of the
637 above, or should be. */
639 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
640 runs its second argument through gettext. */
642 fnotice (FILE *file, const char *cmsgid, ...)
646 va_start (ap, cmsgid);
647 vfprintf (file, _(cmsgid), ap);
651 /* Inform the user that an error occurred while trying to report some
652 other error. This indicates catastrophic internal inconsistencies,
653 so give up now. But do try to flush out the previous error.
654 This mustn't use internal_error, that will cause infinite recursion. */
657 error_recursion (diagnostic_context *context)
659 diagnostic_info diagnostic;
661 if (context->lock < 3)
662 pp_flush (context->printer);
665 "Internal compiler error: Error reporting routines re-entered.\n");
667 /* Call diagnostic_action_after_output to get the "please submit a bug
668 report" message. It only looks at the kind field of diagnostic_info. */
669 diagnostic.kind = DK_ICE;
670 diagnostic_action_after_output (context, &diagnostic);
672 /* Do not use gcc_unreachable here; that goes through internal_error
673 and therefore would cause infinite recursion. */
677 /* Report an internal compiler error in a friendly manner. This is
678 the function that gets called upon use of abort() in the source
679 code generally, thanks to a special macro. */
682 fancy_abort (const char *file, int line, const char *function)
684 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
687 /* Really call the system 'abort'. This has to go right at the end of
688 this file, so that there are no functions after it that call abort
689 and get the system abort instead of our macro. */