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;
55 static void diagnostic_action_after_output (diagnostic_context *,
57 static void real_abort (void) ATTRIBUTE_NORETURN;
59 /* A diagnostic_context surrogate for stderr. */
60 static diagnostic_context global_diagnostic_context;
61 diagnostic_context *global_dc = &global_diagnostic_context;
64 /* Return a malloc'd string containing MSG formatted a la printf. The
65 caller is responsible for freeing the memory. */
67 build_message_string (const char *msg, ...)
73 vasprintf (&str, msg, ap);
79 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
81 file_name_as_prefix (const char *f)
83 return build_message_string ("%s: ", f);
88 /* Initialize the diagnostic message outputting machinery. */
90 diagnostic_initialize (diagnostic_context *context)
92 /* Allocate a basic pretty-printer. Clients will replace this a
93 much more elaborated pretty-printer if they wish. */
94 context->printer = XNEW (pretty_printer);
95 pp_construct (context->printer, NULL, 0);
96 /* By default, diagnostics are sent to stderr. */
97 context->printer->buffer->stream = stderr;
98 /* By default, we emit prefixes once per message. */
99 context->printer->wrapping.rule = DIAGNOSTICS_SHOW_PREFIX_ONCE;
101 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
102 context->issue_warnings_are_errors_message = true;
103 context->warning_as_error_requested = false;
104 memset (context->classify_diagnostic, DK_UNSPECIFIED,
105 sizeof context->classify_diagnostic);
106 context->show_option_requested = false;
107 context->abort_on_error = false;
108 context->internal_error = NULL;
109 diagnostic_starter (context) = default_diagnostic_starter;
110 diagnostic_finalizer (context) = default_diagnostic_finalizer;
111 context->last_module = 0;
112 context->last_function = NULL;
116 /* Initialize DIAGNOSTIC, where the message MSG has already been
119 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
120 va_list *args, location_t location,
123 diagnostic->message.err_no = errno;
124 diagnostic->message.args_ptr = args;
125 diagnostic->message.format_spec = msg;
126 diagnostic->location = location;
127 diagnostic->kind = kind;
128 diagnostic->option_index = 0;
131 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
134 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
135 va_list *args, location_t location,
138 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
141 /* Return a malloc'd string describing a location. The caller is
142 responsible for freeing the memory. */
144 diagnostic_build_prefix (diagnostic_info *diagnostic)
146 static const char *const diagnostic_kind_text[] = {
147 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
148 #include "diagnostic.def"
149 #undef DEFINE_DIAGNOSTIC_KIND
152 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
153 expanded_location s = expand_location (diagnostic->location);
154 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
158 ? build_message_string ("%s: %s", progname, text)
159 : flag_show_column && s.column != 0
160 ? build_message_string ("%s:%d:%d: %s", s.file, s.line, s.column, text)
161 : build_message_string ("%s:%d: %s", s.file, s.line, text));
164 /* Take any action which is expected to happen after the diagnostic
165 is written out. This function does not always return. */
167 diagnostic_action_after_output (diagnostic_context *context,
168 diagnostic_info *diagnostic)
170 switch (diagnostic->kind)
180 if (context->abort_on_error)
182 if (flag_fatal_errors)
184 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
185 exit (FATAL_EXIT_CODE);
190 if (context->abort_on_error)
193 fnotice (stderr, "Please submit a full bug report,\n"
194 "with preprocessed source if appropriate.\n"
195 "See %s for instructions.\n", bug_report_url);
196 exit (ICE_EXIT_CODE);
199 if (context->abort_on_error)
202 fnotice (stderr, "compilation terminated.\n");
203 exit (FATAL_EXIT_CODE);
210 /* Prints out, if necessary, the name of the current function
211 that caused an error. Called from all error and warning functions. */
213 diagnostic_report_current_function (diagnostic_context *context,
214 diagnostic_info *diagnostic)
216 diagnostic_report_current_module (context);
217 lang_hooks.print_error_function (context, input_filename, diagnostic);
221 diagnostic_report_current_module (diagnostic_context *context)
223 const struct line_map *map;
225 if (pp_needs_newline (context->printer))
227 pp_newline (context->printer);
228 pp_needs_newline (context->printer) = false;
231 if (input_location <= BUILTINS_LOCATION)
234 map = linemap_lookup (line_table, input_location);
235 if (map && diagnostic_last_module_changed (context, map))
237 diagnostic_set_last_module (context, map);
238 if (! MAIN_FILE_P (map))
240 map = INCLUDED_FROM (line_table, map);
241 pp_verbatim (context->printer,
242 "In file included from %s:%d",
243 map->to_file, LAST_SOURCE_LINE (map));
244 while (! MAIN_FILE_P (map))
246 map = INCLUDED_FROM (line_table, map);
247 pp_verbatim (context->printer,
249 map->to_file, LAST_SOURCE_LINE (map));
251 pp_verbatim (context->printer, ":");
252 pp_newline (context->printer);
258 default_diagnostic_starter (diagnostic_context *context,
259 diagnostic_info *diagnostic)
261 diagnostic_report_current_function (context, diagnostic);
262 pp_set_prefix (context->printer, diagnostic_build_prefix (diagnostic));
266 default_diagnostic_finalizer (diagnostic_context *context,
267 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
269 pp_destroy_prefix (context->printer);
272 /* Interface to specify diagnostic kind overrides. Returns the
273 previous setting, or DK_UNSPECIFIED if the parameters are out of
276 diagnostic_classify_diagnostic (diagnostic_context *context,
278 diagnostic_t new_kind)
280 diagnostic_t old_kind;
282 if (option_index <= 0
283 || option_index >= N_OPTS
284 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
285 return DK_UNSPECIFIED;
287 old_kind = context->classify_diagnostic[option_index];
288 context->classify_diagnostic[option_index] = new_kind;
292 /* Report a diagnostic message (an error or a warning) as specified by
293 DC. This function is *the* subroutine in terms of which front-ends
294 should implement their specific diagnostic handling modules. The
295 front-end independent format specifiers are exactly those described
296 in the documentation of output_format. */
299 diagnostic_report_diagnostic (diagnostic_context *context,
300 diagnostic_info *diagnostic)
302 location_t location = diagnostic->location;
303 bool maybe_print_warnings_as_errors_message = false;
304 const char *saved_format_spec;
306 /* Give preference to being able to inhibit warnings, before they
307 get reclassified to something else. */
308 if (diagnostic->kind == DK_WARNING
309 && !diagnostic_report_warnings_p (location))
312 if (context->lock > 0)
314 /* If we're reporting an ICE in the middle of some other error,
315 try to flush out the previous error, then let this one
316 through. Don't do this more than once. */
317 if (diagnostic->kind == DK_ICE && context->lock == 1)
318 pp_flush (context->printer);
320 error_recursion (context);
323 /* If the user requested that warnings be treated as errors, so be
324 it. Note that we do this before the next block so that
325 individual warnings can be overridden back to warnings with
327 if (context->warning_as_error_requested
328 && diagnostic->kind == DK_WARNING)
330 diagnostic->kind = DK_ERROR;
331 maybe_print_warnings_as_errors_message = true;
334 if (diagnostic->option_index)
336 /* This tests if the user provided the appropriate -Wfoo or
338 if (! option_enabled (diagnostic->option_index))
340 /* This tests if the user provided the appropriate -Werror=foo
342 if (context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
344 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
345 maybe_print_warnings_as_errors_message = false;
347 /* This allows for future extensions, like temporarily disabling
348 warnings for ranges of source code. */
349 if (diagnostic->kind == DK_IGNORED)
353 /* If we changed the kind due to -Werror, and didn't override it, we
354 need to print this message. */
355 if (context->issue_warnings_are_errors_message
356 && maybe_print_warnings_as_errors_message)
358 pp_verbatim (context->printer,
359 "%s: warnings being treated as errors\n", progname);
360 context->issue_warnings_are_errors_message = false;
365 if (diagnostic->kind == DK_ICE)
367 #ifndef ENABLE_CHECKING
368 /* When not checking, ICEs are converted to fatal errors when an
369 error has already occurred. This is counteracted by
371 if ((diagnostic_kind_count (context, DK_ERROR) > 0
372 || diagnostic_kind_count (context, DK_SORRY) > 0)
373 && !context->abort_on_error)
375 expanded_location s = expand_location (diagnostic->location);
376 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
378 exit (ICE_EXIT_CODE);
381 if (context->internal_error)
382 (*context->internal_error) (diagnostic->message.format_spec,
383 diagnostic->message.args_ptr);
385 ++diagnostic_kind_count (context, diagnostic->kind);
387 saved_format_spec = diagnostic->message.format_spec;
388 if (context->show_option_requested && diagnostic->option_index)
389 diagnostic->message.format_spec
390 = ACONCAT ((diagnostic->message.format_spec,
391 " [", cl_options[diagnostic->option_index].opt_text, "]", NULL));
393 diagnostic->message.locus = &diagnostic->location;
394 diagnostic->message.abstract_origin = &diagnostic->abstract_origin;
395 diagnostic->abstract_origin = NULL;
396 pp_format (context->printer, &diagnostic->message);
397 (*diagnostic_starter (context)) (context, diagnostic);
398 pp_output_formatted_text (context->printer);
399 (*diagnostic_finalizer (context)) (context, diagnostic);
400 pp_flush (context->printer);
401 diagnostic_action_after_output (context, diagnostic);
402 diagnostic->message.format_spec = saved_format_spec;
403 diagnostic->abstract_origin = NULL;
408 /* Given a partial pathname as input, return another pathname that
409 shares no directory elements with the pathname of __FILE__. This
410 is used by fancy_abort() to print `Internal compiler error in expr.c'
411 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
414 trim_filename (const char *name)
416 static const char this_file[] = __FILE__;
417 const char *p = name, *q = this_file;
419 /* First skip any "../" in each filename. This allows us to give a proper
420 reference to a file in a subdirectory. */
421 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
424 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
427 /* Now skip any parts the two filenames have in common. */
428 while (*p == *q && *p != 0 && *q != 0)
431 /* Now go backwards until the previous directory separator. */
432 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
438 /* Standard error reporting routines in increasing order of severity.
439 All of these take arguments like printf. */
441 /* Text to be emitted verbatim to the error message stream; this
442 produces no prefix and disables line-wrapping. Use rarely. */
444 verbatim (const char *gmsgid, ...)
449 va_start (ap, gmsgid);
452 text.format_spec = _(gmsgid);
454 text.abstract_origin = NULL;
455 pp_format_verbatim (global_dc->printer, &text);
456 pp_flush (global_dc->printer);
460 /* An informative note. Use this for additional details on an error
463 inform (const char *gmsgid, ...)
465 diagnostic_info diagnostic;
468 va_start (ap, gmsgid);
469 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_NOTE);
470 report_diagnostic (&diagnostic);
474 /* A warning at INPUT_LOCATION. Use this for code which is correct according
475 to the relevant language specification but is likely to be buggy anyway. */
477 warning (int opt, const char *gmsgid, ...)
479 diagnostic_info diagnostic;
482 va_start (ap, gmsgid);
483 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
484 diagnostic.option_index = opt;
486 report_diagnostic (&diagnostic);
491 warning0 (const char *gmsgid, ...)
493 diagnostic_info diagnostic;
496 va_start (ap, gmsgid);
497 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
498 report_diagnostic (&diagnostic);
502 /* A warning at LOCATION. Use this for code which is correct according to the
503 relevant language specification but is likely to be buggy anyway. */
505 warning_at (location_t location, int opt, const char *gmsgid, ...)
507 diagnostic_info diagnostic;
510 va_start (ap, gmsgid);
511 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_WARNING);
512 diagnostic.option_index = opt;
514 report_diagnostic (&diagnostic);
518 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
519 given on the command line, in which case it issues an error. Use
520 this for diagnostics required by the relevant language standard,
521 if you have chosen not to make them errors.
523 Note that these diagnostics are issued independent of the setting
524 of the -pedantic command-line switch. To get a warning enabled
525 only with that switch, write "if (pedantic) pedwarn (...);" */
527 pedwarn (const char *gmsgid, ...)
529 diagnostic_info diagnostic;
532 va_start (ap, gmsgid);
533 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location,
534 pedantic_warning_kind ());
535 report_diagnostic (&diagnostic);
539 /* A "permissive" error: issues an error unless -fpermissive was given
540 on the command line, in which case it issues a warning. Use this
541 for things that really should be errors but we want to support
545 permerror (const char *gmsgid, ...)
547 diagnostic_info diagnostic;
550 va_start (ap, gmsgid);
551 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location,
552 permissive_error_kind ());
553 diagnostic.option_index = OPT_fpermissive;
554 report_diagnostic (&diagnostic);
559 /* A hard error: the code is definitely ill-formed, and an object file
560 will not be produced. */
562 error (const char *gmsgid, ...)
564 diagnostic_info diagnostic;
567 va_start (ap, gmsgid);
568 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
569 report_diagnostic (&diagnostic);
573 /* "Sorry, not implemented." Use for a language feature which is
574 required by the relevant specification but not implemented by GCC.
575 An object file will not be produced. */
577 sorry (const char *gmsgid, ...)
579 diagnostic_info diagnostic;
582 va_start (ap, gmsgid);
583 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
584 report_diagnostic (&diagnostic);
588 /* An error which is severe enough that we make no attempt to
589 continue. Do not use this for internal consistency checks; that's
590 internal_error. Use of this function should be rare. */
592 fatal_error (const char *gmsgid, ...)
594 diagnostic_info diagnostic;
597 va_start (ap, gmsgid);
598 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
599 report_diagnostic (&diagnostic);
605 /* An internal consistency check has failed. We make no attempt to
606 continue. Note that unless there is debugging value to be had from
607 a more specific message, or some other good reason, you should use
608 abort () instead of calling this function directly. */
610 internal_error (const char *gmsgid, ...)
612 diagnostic_info diagnostic;
615 va_start (ap, gmsgid);
616 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
617 report_diagnostic (&diagnostic);
623 /* Special case error functions. Most are implemented in terms of the
624 above, or should be. */
626 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
627 runs its second argument through gettext. */
629 fnotice (FILE *file, const char *cmsgid, ...)
633 va_start (ap, cmsgid);
634 vfprintf (file, _(cmsgid), ap);
638 /* Inform the user that an error occurred while trying to report some
639 other error. This indicates catastrophic internal inconsistencies,
640 so give up now. But do try to flush out the previous error.
641 This mustn't use internal_error, that will cause infinite recursion. */
644 error_recursion (diagnostic_context *context)
646 diagnostic_info diagnostic;
648 if (context->lock < 3)
649 pp_flush (context->printer);
652 "Internal compiler error: Error reporting routines re-entered.\n");
654 /* Call diagnostic_action_after_output to get the "please submit a bug
655 report" message. It only looks at the kind field of diagnostic_info. */
656 diagnostic.kind = DK_ICE;
657 diagnostic_action_after_output (context, &diagnostic);
659 /* Do not use gcc_unreachable here; that goes through internal_error
660 and therefore would cause infinite recursion. */
664 /* Report an internal compiler error in a friendly manner. This is
665 the function that gets called upon use of abort() in the source
666 code generally, thanks to a special macro. */
669 fancy_abort (const char *file, int line, const char *function)
671 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
674 /* Really call the system 'abort'. This has to go right at the end of
675 this file, so that there are no functions after it that call abort
676 and get the system abort instead of our macro. */