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 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"
45 #define pedantic_warning_kind() (flag_pedantic_errors ? DK_ERROR : DK_WARNING)
46 #define permissive_error_kind() (flag_permissive ? DK_WARNING : DK_ERROR)
49 static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1
;
51 static void error_recursion (diagnostic_context
*) ATTRIBUTE_NORETURN
;
53 static void diagnostic_action_after_output (diagnostic_context
*,
55 static void real_abort (void) ATTRIBUTE_NORETURN
;
57 /* A diagnostic_context surrogate for stderr. */
58 static diagnostic_context global_diagnostic_context
;
59 diagnostic_context
*global_dc
= &global_diagnostic_context
;
62 /* Return a malloc'd string containing MSG formatted a la printf. The
63 caller is responsible for freeing the memory. */
65 build_message_string (const char *msg
, ...)
71 vasprintf (&str
, msg
, ap
);
77 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
79 file_name_as_prefix (const char *f
)
81 return build_message_string ("%s: ", f
);
86 /* Initialize the diagnostic message outputting machinery. */
88 diagnostic_initialize (diagnostic_context
*context
)
90 /* Allocate a basic pretty-printer. Clients will replace this a
91 much more elaborated pretty-printer if they wish. */
92 context
->printer
= XNEW (pretty_printer
);
93 pp_construct (context
->printer
, NULL
, 0);
94 /* By default, diagnostics are sent to stderr. */
95 context
->printer
->buffer
->stream
= stderr
;
96 /* By default, we emit prefixes once per message. */
97 context
->printer
->wrapping
.rule
= DIAGNOSTICS_SHOW_PREFIX_ONCE
;
99 memset (context
->diagnostic_count
, 0, sizeof context
->diagnostic_count
);
100 context
->issue_warnings_are_errors_message
= true;
101 context
->warning_as_error_requested
= false;
102 memset (context
->classify_diagnostic
, DK_UNSPECIFIED
,
103 sizeof context
->classify_diagnostic
);
104 context
->show_option_requested
= false;
105 context
->abort_on_error
= false;
106 context
->internal_error
= NULL
;
107 diagnostic_starter (context
) = default_diagnostic_starter
;
108 diagnostic_finalizer (context
) = default_diagnostic_finalizer
;
109 context
->last_module
= 0;
110 context
->last_function
= NULL
;
114 /* Initialize DIAGNOSTIC, where the message MSG has already been
117 diagnostic_set_info_translated (diagnostic_info
*diagnostic
, const char *msg
,
118 va_list *args
, location_t location
,
121 diagnostic
->message
.err_no
= errno
;
122 diagnostic
->message
.args_ptr
= args
;
123 diagnostic
->message
.format_spec
= msg
;
124 diagnostic
->location
= location
;
125 diagnostic
->override_column
= 0;
126 diagnostic
->kind
= kind
;
127 diagnostic
->option_index
= 0;
130 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
133 diagnostic_set_info (diagnostic_info
*diagnostic
, const char *gmsgid
,
134 va_list *args
, location_t location
,
137 diagnostic_set_info_translated (diagnostic
, _(gmsgid
), args
, location
, kind
);
140 /* Return a malloc'd string describing a location. The caller is
141 responsible for freeing the memory. */
143 diagnostic_build_prefix (diagnostic_info
*diagnostic
)
145 static const char *const diagnostic_kind_text
[] = {
146 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
147 #include "diagnostic.def"
148 #undef DEFINE_DIAGNOSTIC_KIND
151 const char *text
= _(diagnostic_kind_text
[diagnostic
->kind
]);
152 expanded_location s
= expand_location (diagnostic
->location
);
153 if (diagnostic
->override_column
)
154 s
.column
= diagnostic
->override_column
;
155 gcc_assert (diagnostic
->kind
< DK_LAST_DIAGNOSTIC_KIND
);
159 ? build_message_string ("%s: %s", progname
, text
)
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 /* Take any action which is expected to happen after the diagnostic
166 is written out. This function does not always return. */
168 diagnostic_action_after_output (diagnostic_context
*context
,
169 diagnostic_info
*diagnostic
)
171 switch (diagnostic
->kind
)
181 if (context
->abort_on_error
)
183 if (flag_fatal_errors
)
185 fnotice (stderr
, "compilation terminated due to -Wfatal-errors.\n");
186 exit (FATAL_EXIT_CODE
);
191 if (context
->abort_on_error
)
194 fnotice (stderr
, "Please submit a full bug report,\n"
195 "with preprocessed source if appropriate.\n"
196 "See %s for instructions.\n", bug_report_url
);
197 exit (ICE_EXIT_CODE
);
200 if (context
->abort_on_error
)
203 fnotice (stderr
, "compilation terminated.\n");
204 exit (FATAL_EXIT_CODE
);
211 /* Prints out, if necessary, the name of the current function
212 that caused an error. Called from all error and warning functions. */
214 diagnostic_report_current_function (diagnostic_context
*context
,
215 diagnostic_info
*diagnostic
)
217 diagnostic_report_current_module (context
);
218 lang_hooks
.print_error_function (context
, input_filename
, diagnostic
);
222 diagnostic_report_current_module (diagnostic_context
*context
)
224 const struct line_map
*map
;
226 if (pp_needs_newline (context
->printer
))
228 pp_newline (context
->printer
);
229 pp_needs_newline (context
->printer
) = false;
232 if (input_location
<= BUILTINS_LOCATION
)
235 map
= linemap_lookup (line_table
, input_location
);
236 if (map
&& diagnostic_last_module_changed (context
, map
))
238 diagnostic_set_last_module (context
, map
);
239 if (! MAIN_FILE_P (map
))
241 map
= INCLUDED_FROM (line_table
, map
);
242 if (flag_show_column
)
243 pp_verbatim (context
->printer
,
244 "In file included from %s:%d:%d",
246 LAST_SOURCE_LINE (map
), LAST_SOURCE_COLUMN (map
));
248 pp_verbatim (context
->printer
,
249 "In file included from %s:%d",
250 map
->to_file
, LAST_SOURCE_LINE (map
));
251 while (! MAIN_FILE_P (map
))
253 map
= INCLUDED_FROM (line_table
, map
);
254 pp_verbatim (context
->printer
,
256 map
->to_file
, LAST_SOURCE_LINE (map
));
258 pp_verbatim (context
->printer
, ":");
259 pp_newline (context
->printer
);
265 default_diagnostic_starter (diagnostic_context
*context
,
266 diagnostic_info
*diagnostic
)
268 diagnostic_report_current_function (context
, diagnostic
);
269 pp_set_prefix (context
->printer
, diagnostic_build_prefix (diagnostic
));
273 default_diagnostic_finalizer (diagnostic_context
*context
,
274 diagnostic_info
*diagnostic ATTRIBUTE_UNUSED
)
276 pp_destroy_prefix (context
->printer
);
279 /* Interface to specify diagnostic kind overrides. Returns the
280 previous setting, or DK_UNSPECIFIED if the parameters are out of
283 diagnostic_classify_diagnostic (diagnostic_context
*context
,
285 diagnostic_t new_kind
)
287 diagnostic_t old_kind
;
289 if (option_index
<= 0
290 || option_index
>= N_OPTS
291 || new_kind
>= DK_LAST_DIAGNOSTIC_KIND
)
292 return DK_UNSPECIFIED
;
294 old_kind
= context
->classify_diagnostic
[option_index
];
295 context
->classify_diagnostic
[option_index
] = new_kind
;
299 /* Report a diagnostic message (an error or a warning) as specified by
300 DC. This function is *the* subroutine in terms of which front-ends
301 should implement their specific diagnostic handling modules. The
302 front-end independent format specifiers are exactly those described
303 in the documentation of output_format.
304 Return true if a diagnostic was printed, false otherwise. */
307 diagnostic_report_diagnostic (diagnostic_context
*context
,
308 diagnostic_info
*diagnostic
)
310 location_t location
= diagnostic
->location
;
311 bool maybe_print_warnings_as_errors_message
= false;
312 const char *saved_format_spec
;
314 /* Give preference to being able to inhibit warnings, before they
315 get reclassified to something else. */
316 if ((diagnostic
->kind
== DK_WARNING
|| diagnostic
->kind
== DK_PEDWARN
)
317 && !diagnostic_report_warnings_p (location
))
320 if (diagnostic
->kind
== DK_NOTE
&& flag_compare_debug
)
323 if (diagnostic
->kind
== DK_PEDWARN
)
324 diagnostic
->kind
= pedantic_warning_kind ();
326 if (context
->lock
> 0)
328 /* If we're reporting an ICE in the middle of some other error,
329 try to flush out the previous error, then let this one
330 through. Don't do this more than once. */
331 if (diagnostic
->kind
== DK_ICE
&& context
->lock
== 1)
332 pp_flush (context
->printer
);
334 error_recursion (context
);
337 /* If the user requested that warnings be treated as errors, so be
338 it. Note that we do this before the next block so that
339 individual warnings can be overridden back to warnings with
341 if (context
->warning_as_error_requested
342 && diagnostic
->kind
== DK_WARNING
)
344 diagnostic
->kind
= DK_ERROR
;
345 maybe_print_warnings_as_errors_message
= true;
348 if (diagnostic
->option_index
)
350 /* This tests if the user provided the appropriate -Wfoo or
352 if (! option_enabled (diagnostic
->option_index
))
354 /* This tests if the user provided the appropriate -Werror=foo
356 if (context
->classify_diagnostic
[diagnostic
->option_index
] != DK_UNSPECIFIED
)
358 diagnostic
->kind
= context
->classify_diagnostic
[diagnostic
->option_index
];
359 maybe_print_warnings_as_errors_message
= false;
361 /* This allows for future extensions, like temporarily disabling
362 warnings for ranges of source code. */
363 if (diagnostic
->kind
== DK_IGNORED
)
367 /* If we changed the kind due to -Werror, and didn't override it, we
368 need to print this message. */
369 if (context
->issue_warnings_are_errors_message
370 && maybe_print_warnings_as_errors_message
)
372 pp_verbatim (context
->printer
,
373 "%s: warnings being treated as errors\n", progname
);
374 context
->issue_warnings_are_errors_message
= false;
379 if (diagnostic
->kind
== DK_ICE
&& plugins_active_p ())
381 fnotice (stderr
, "*** WARNING *** there are active plugins, do not report"
382 " this as a bug unless you can reproduce it without enabling"
384 dump_active_plugins (stderr
);
387 if (diagnostic
->kind
== DK_ICE
)
389 #ifndef ENABLE_CHECKING
390 /* When not checking, ICEs are converted to fatal errors when an
391 error has already occurred. This is counteracted by
393 if ((diagnostic_kind_count (context
, DK_ERROR
) > 0
394 || diagnostic_kind_count (context
, DK_SORRY
) > 0)
395 && !context
->abort_on_error
)
397 expanded_location s
= expand_location (diagnostic
->location
);
398 fnotice (stderr
, "%s:%d: confused by earlier errors, bailing out\n",
400 exit (ICE_EXIT_CODE
);
403 if (context
->internal_error
)
404 (*context
->internal_error
) (diagnostic
->message
.format_spec
,
405 diagnostic
->message
.args_ptr
);
407 ++diagnostic_kind_count (context
, diagnostic
->kind
);
409 saved_format_spec
= diagnostic
->message
.format_spec
;
410 if (context
->show_option_requested
&& diagnostic
->option_index
)
411 diagnostic
->message
.format_spec
412 = ACONCAT ((diagnostic
->message
.format_spec
,
413 " [", cl_options
[diagnostic
->option_index
].opt_text
, "]", NULL
));
415 diagnostic
->message
.locus
= &diagnostic
->location
;
416 diagnostic
->message
.abstract_origin
= &diagnostic
->abstract_origin
;
417 diagnostic
->abstract_origin
= NULL
;
418 pp_format (context
->printer
, &diagnostic
->message
);
419 (*diagnostic_starter (context
)) (context
, diagnostic
);
420 pp_output_formatted_text (context
->printer
);
421 (*diagnostic_finalizer (context
)) (context
, diagnostic
);
422 pp_flush (context
->printer
);
423 diagnostic_action_after_output (context
, diagnostic
);
424 diagnostic
->message
.format_spec
= saved_format_spec
;
425 diagnostic
->abstract_origin
= NULL
;
432 /* Given a partial pathname as input, return another pathname that
433 shares no directory elements with the pathname of __FILE__. This
434 is used by fancy_abort() to print `Internal compiler error in expr.c'
435 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
438 trim_filename (const char *name
)
440 static const char this_file
[] = __FILE__
;
441 const char *p
= name
, *q
= this_file
;
443 /* First skip any "../" in each filename. This allows us to give a proper
444 reference to a file in a subdirectory. */
445 while (p
[0] == '.' && p
[1] == '.' && IS_DIR_SEPARATOR (p
[2]))
448 while (q
[0] == '.' && q
[1] == '.' && IS_DIR_SEPARATOR (q
[2]))
451 /* Now skip any parts the two filenames have in common. */
452 while (*p
== *q
&& *p
!= 0 && *q
!= 0)
455 /* Now go backwards until the previous directory separator. */
456 while (p
> name
&& !IS_DIR_SEPARATOR (p
[-1]))
462 /* Standard error reporting routines in increasing order of severity.
463 All of these take arguments like printf. */
465 /* Text to be emitted verbatim to the error message stream; this
466 produces no prefix and disables line-wrapping. Use rarely. */
468 verbatim (const char *gmsgid
, ...)
473 va_start (ap
, gmsgid
);
476 text
.format_spec
= _(gmsgid
);
478 text
.abstract_origin
= NULL
;
479 pp_format_verbatim (global_dc
->printer
, &text
);
480 pp_flush (global_dc
->printer
);
485 emit_diagnostic (diagnostic_t kind
, location_t location
, int opt
,
486 const char *gmsgid
, ...)
488 diagnostic_info diagnostic
;
491 va_start (ap
, gmsgid
);
492 if (kind
== DK_PERMERROR
)
494 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
,
495 permissive_error_kind ());
496 diagnostic
.option_index
= OPT_fpermissive
;
499 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
, kind
);
500 if (kind
== DK_WARNING
|| kind
== DK_PEDWARN
)
501 diagnostic
.option_index
= opt
;
505 return report_diagnostic (&diagnostic
);
508 /* An informative note at LOCATION. Use this for additional details on an error
511 inform (location_t location
, const char *gmsgid
, ...)
513 diagnostic_info diagnostic
;
516 va_start (ap
, gmsgid
);
517 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
, DK_NOTE
);
518 report_diagnostic (&diagnostic
);
522 /* A warning at INPUT_LOCATION. Use this for code which is correct according
523 to the relevant language specification but is likely to be buggy anyway.
524 Returns true if the warning was printed, false if it was inhibited. */
526 warning (int opt
, const char *gmsgid
, ...)
528 diagnostic_info diagnostic
;
531 va_start (ap
, gmsgid
);
532 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_WARNING
);
533 diagnostic
.option_index
= opt
;
536 return report_diagnostic (&diagnostic
);
539 /* A warning at LOCATION. Use this for code which is correct according to the
540 relevant language specification but is likely to be buggy anyway.
541 Returns true if the warning was printed, false if it was inhibited. */
544 warning_at (location_t location
, int opt
, const char *gmsgid
, ...)
546 diagnostic_info diagnostic
;
549 va_start (ap
, gmsgid
);
550 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
, DK_WARNING
);
551 diagnostic
.option_index
= opt
;
553 return report_diagnostic (&diagnostic
);
556 /* A "pedantic" warning at LOCATION: issues a warning unless
557 -pedantic-errors was given on the command line, in which case it
558 issues an error. Use this for diagnostics required by the relevant
559 language standard, if you have chosen not to make them errors.
561 Note that these diagnostics are issued independent of the setting
562 of the -pedantic command-line switch. To get a warning enabled
563 only with that switch, use either "if (pedantic) pedwarn
564 (OPT_pedantic,...)" or just "pedwarn (OPT_pedantic,..)". To get a
565 pedwarn independently of the -pedantic switch use "pedwarn (0,...)".
567 Returns true if the warning was printed, false if it was inhibited. */
570 pedwarn (location_t location
, int opt
, const char *gmsgid
, ...)
572 diagnostic_info diagnostic
;
575 va_start (ap
, gmsgid
);
576 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
, DK_PEDWARN
);
577 diagnostic
.option_index
= opt
;
579 return report_diagnostic (&diagnostic
);
582 /* A "permissive" error at LOCATION: issues an error unless
583 -fpermissive was given on the command line, in which case it issues
584 a warning. Use this for things that really should be errors but we
585 want to support legacy code.
587 Returns true if the warning was printed, false if it was inhibited. */
590 permerror (location_t location
, const char *gmsgid
, ...)
592 diagnostic_info diagnostic
;
595 va_start (ap
, gmsgid
);
596 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
,
597 permissive_error_kind ());
598 diagnostic
.option_index
= OPT_fpermissive
;
600 return report_diagnostic (&diagnostic
);
603 /* A hard error: the code is definitely ill-formed, and an object file
604 will not be produced. */
606 error (const char *gmsgid
, ...)
608 diagnostic_info diagnostic
;
611 va_start (ap
, gmsgid
);
612 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_ERROR
);
613 report_diagnostic (&diagnostic
);
617 /* Same as ebove, but use location LOC instead of input_location. */
619 error_at (location_t loc
, const char *gmsgid
, ...)
621 diagnostic_info diagnostic
;
624 va_start (ap
, gmsgid
);
625 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, loc
, DK_ERROR
);
626 report_diagnostic (&diagnostic
);
630 /* "Sorry, not implemented." Use for a language feature which is
631 required by the relevant specification but not implemented by GCC.
632 An object file will not be produced. */
634 sorry (const char *gmsgid
, ...)
636 diagnostic_info diagnostic
;
639 va_start (ap
, gmsgid
);
640 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_SORRY
);
641 report_diagnostic (&diagnostic
);
645 /* An error which is severe enough that we make no attempt to
646 continue. Do not use this for internal consistency checks; that's
647 internal_error. Use of this function should be rare. */
649 fatal_error (const char *gmsgid
, ...)
651 diagnostic_info diagnostic
;
654 va_start (ap
, gmsgid
);
655 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_FATAL
);
656 report_diagnostic (&diagnostic
);
662 /* An internal consistency check has failed. We make no attempt to
663 continue. Note that unless there is debugging value to be had from
664 a more specific message, or some other good reason, you should use
665 abort () instead of calling this function directly. */
667 internal_error (const char *gmsgid
, ...)
669 diagnostic_info diagnostic
;
672 va_start (ap
, gmsgid
);
673 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_ICE
);
674 report_diagnostic (&diagnostic
);
680 /* Special case error functions. Most are implemented in terms of the
681 above, or should be. */
683 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
684 runs its second argument through gettext. */
686 fnotice (FILE *file
, const char *cmsgid
, ...)
690 va_start (ap
, cmsgid
);
691 vfprintf (file
, _(cmsgid
), ap
);
695 /* Inform the user that an error occurred while trying to report some
696 other error. This indicates catastrophic internal inconsistencies,
697 so give up now. But do try to flush out the previous error.
698 This mustn't use internal_error, that will cause infinite recursion. */
701 error_recursion (diagnostic_context
*context
)
703 diagnostic_info diagnostic
;
705 if (context
->lock
< 3)
706 pp_flush (context
->printer
);
709 "Internal compiler error: Error reporting routines re-entered.\n");
711 /* Call diagnostic_action_after_output to get the "please submit a bug
712 report" message. It only looks at the kind field of diagnostic_info. */
713 diagnostic
.kind
= DK_ICE
;
714 diagnostic_action_after_output (context
, &diagnostic
);
716 /* Do not use gcc_unreachable here; that goes through internal_error
717 and therefore would cause infinite recursion. */
721 /* Report an internal compiler error in a friendly manner. This is
722 the function that gets called upon use of abort() in the source
723 code generally, thanks to a special macro. */
726 fancy_abort (const char *file
, int line
, const char *function
)
728 internal_error ("in %s, at %s:%d", function
, trim_filename (file
), line
);
731 /* Really call the system 'abort'. This has to go right at the end of
732 this file, so that there are no functions after it that call abort
733 and get the system abort instead of our macro. */