1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
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 #ifdef USE_MAPPED_LOCATION
161 : flag_show_column
&& s
.column
!= 0
162 ? build_message_string ("%s:%d:%d: %s", s
.file
, s
.line
, s
.column
, text
)
164 : build_message_string ("%s:%d: %s", s
.file
, s
.line
, text
));
167 /* Count a diagnostic. Return true if the message should be printed. */
169 diagnostic_count_diagnostic (diagnostic_context
*context
,
170 diagnostic_info
*diagnostic
)
172 diagnostic_t kind
= diagnostic
->kind
;
179 #ifndef ENABLE_CHECKING
180 /* When not checking, ICEs are converted to fatal errors when an
181 error has already occurred. This is counteracted by
183 if ((diagnostic_kind_count (context
, DK_ERROR
) > 0
184 || diagnostic_kind_count (context
, DK_SORRY
) > 0)
185 && !context
->abort_on_error
)
187 expanded_location s
= expand_location (diagnostic
->location
);
188 fnotice (stderr
, "%s:%d: confused by earlier errors, bailing out\n",
190 exit (ICE_EXIT_CODE
);
193 if (context
->internal_error
)
194 (*context
->internal_error
) (diagnostic
->message
.format_spec
,
195 diagnostic
->message
.args_ptr
);
198 case DK_FATAL
: case DK_SORRY
:
199 case DK_ANACHRONISM
: case DK_NOTE
:
200 ++diagnostic_kind_count (context
, kind
);
204 ++diagnostic_kind_count (context
, DK_WARNING
);
208 ++diagnostic_kind_count (context
, DK_ERROR
);
215 /* Take any action which is expected to happen after the diagnostic
216 is written out. This function does not always return. */
218 diagnostic_action_after_output (diagnostic_context
*context
,
219 diagnostic_info
*diagnostic
)
221 switch (diagnostic
->kind
)
231 if (context
->abort_on_error
)
233 if (flag_fatal_errors
)
235 fnotice (stderr
, "compilation terminated due to -Wfatal-errors.\n");
236 exit (FATAL_EXIT_CODE
);
241 if (context
->abort_on_error
)
244 fnotice (stderr
, "Please submit a full bug report,\n"
245 "with preprocessed source if appropriate.\n"
246 "See %s for instructions.\n", bug_report_url
);
247 exit (ICE_EXIT_CODE
);
250 if (context
->abort_on_error
)
253 fnotice (stderr
, "compilation terminated.\n");
254 exit (FATAL_EXIT_CODE
);
261 /* Prints out, if necessary, the name of the current function
262 that caused an error. Called from all error and warning functions. */
264 diagnostic_report_current_function (diagnostic_context
*context
)
266 diagnostic_report_current_module (context
);
267 lang_hooks
.print_error_function (context
, input_filename
);
271 diagnostic_report_current_module (diagnostic_context
*context
)
273 struct file_stack
*p
;
275 if (pp_needs_newline (context
->printer
))
277 pp_newline (context
->printer
);
278 pp_needs_newline (context
->printer
) = false;
281 p
= input_file_stack
;
282 if (p
&& diagnostic_last_module_changed (context
))
284 expanded_location xloc
= expand_location (p
->location
);
285 pp_verbatim (context
->printer
,
286 "In file included from %s:%d",
287 xloc
.file
, xloc
.line
);
288 while ((p
= p
->next
) != NULL
)
290 xloc
= expand_location (p
->location
);
291 pp_verbatim (context
->printer
,
293 xloc
.file
, xloc
.line
);
295 pp_verbatim (context
->printer
, ":");
296 diagnostic_set_last_module (context
);
297 pp_newline (context
->printer
);
302 default_diagnostic_starter (diagnostic_context
*context
,
303 diagnostic_info
*diagnostic
)
305 diagnostic_report_current_function (context
);
306 pp_set_prefix (context
->printer
, diagnostic_build_prefix (diagnostic
));
310 default_diagnostic_finalizer (diagnostic_context
*context
,
311 diagnostic_info
*diagnostic ATTRIBUTE_UNUSED
)
313 pp_destroy_prefix (context
->printer
);
316 /* Interface to specify diagnostic kind overrides. Returns the
317 previous setting, or DK_UNSPECIFIED if the parameters are out of
320 diagnostic_classify_diagnostic (diagnostic_context
*context
,
322 diagnostic_t new_kind
)
324 diagnostic_t old_kind
;
326 if (option_index
<= 0
327 || option_index
>= N_OPTS
328 || new_kind
>= DK_LAST_DIAGNOSTIC_KIND
)
329 return DK_UNSPECIFIED
;
331 old_kind
= context
->classify_diagnostic
[option_index
];
332 context
->classify_diagnostic
[option_index
] = new_kind
;
336 /* Report a diagnostic message (an error or a warning) as specified by
337 DC. This function is *the* subroutine in terms of which front-ends
338 should implement their specific diagnostic handling modules. The
339 front-end independent format specifiers are exactly those described
340 in the documentation of output_format. */
343 diagnostic_report_diagnostic (diagnostic_context
*context
,
344 diagnostic_info
*diagnostic
)
346 bool maybe_print_warnings_as_errors_message
= false;
348 /* Give preference to being able to inhibit warnings, before they
349 get reclassified to something else. */
350 if (diagnostic
->kind
== DK_WARNING
351 && !diagnostic_report_warnings_p ())
354 if (context
->lock
> 0)
356 /* If we're reporting an ICE in the middle of some other error,
357 try to flush out the previous error, then let this one
358 through. Don't do this more than once. */
359 if (diagnostic
->kind
== DK_ICE
&& context
->lock
== 1)
360 pp_flush (context
->printer
);
362 error_recursion (context
);
365 /* If the user requested that warnings be treated as errors, so be
366 it. Note that we do this before the next block so that
367 individual warnings can be overridden back to warnings with
369 if (context
->warning_as_error_requested
370 && diagnostic
->kind
== DK_WARNING
)
372 diagnostic
->kind
= DK_ERROR
;
373 maybe_print_warnings_as_errors_message
= true;
376 if (diagnostic
->option_index
)
378 /* This tests if the user provided the appropriate -Wfoo or
380 if (! option_enabled (diagnostic
->option_index
))
382 /* This tests if the user provided the appropriate -Werror=foo
384 if (context
->classify_diagnostic
[diagnostic
->option_index
] != DK_UNSPECIFIED
)
386 diagnostic
->kind
= context
->classify_diagnostic
[diagnostic
->option_index
];
387 maybe_print_warnings_as_errors_message
= false;
389 /* This allows for future extensions, like temporarily disabling
390 warnings for ranges of source code. */
391 if (diagnostic
->kind
== DK_IGNORED
)
395 /* If we changed the kind due to -Werror, and didn't override it, we
396 need to print this message. */
397 if (context
->issue_warnings_are_errors_message
398 && maybe_print_warnings_as_errors_message
)
400 pp_verbatim (context
->printer
,
401 "%s: warnings being treated as errors\n", progname
);
402 context
->issue_warnings_are_errors_message
= false;
407 if (diagnostic_count_diagnostic (context
, diagnostic
))
409 const char *saved_format_spec
= diagnostic
->message
.format_spec
;
411 if (context
->show_option_requested
&& diagnostic
->option_index
)
412 diagnostic
->message
.format_spec
413 = ACONCAT ((diagnostic
->message
.format_spec
,
414 " [", cl_options
[diagnostic
->option_index
].opt_text
, "]", NULL
));
416 diagnostic
->message
.locus
= &diagnostic
->location
;
417 pp_format (context
->printer
, &diagnostic
->message
);
418 (*diagnostic_starter (context
)) (context
, diagnostic
);
419 pp_output_formatted_text (context
->printer
);
420 (*diagnostic_finalizer (context
)) (context
, diagnostic
);
421 pp_flush (context
->printer
);
422 diagnostic_action_after_output (context
, diagnostic
);
423 diagnostic
->message
.format_spec
= saved_format_spec
;
429 /* Given a partial pathname as input, return another pathname that
430 shares no directory elements with the pathname of __FILE__. This
431 is used by fancy_abort() to print `Internal compiler error in expr.c'
432 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
435 trim_filename (const char *name
)
437 static const char this_file
[] = __FILE__
;
438 const char *p
= name
, *q
= this_file
;
440 /* First skip any "../" in each filename. This allows us to give a proper
441 reference to a file in a subdirectory. */
442 while (p
[0] == '.' && p
[1] == '.' && IS_DIR_SEPARATOR (p
[2]))
445 while (q
[0] == '.' && q
[1] == '.' && IS_DIR_SEPARATOR (q
[2]))
448 /* Now skip any parts the two filenames have in common. */
449 while (*p
== *q
&& *p
!= 0 && *q
!= 0)
452 /* Now go backwards until the previous directory separator. */
453 while (p
> name
&& !IS_DIR_SEPARATOR (p
[-1]))
459 /* Standard error reporting routines in increasing order of severity.
460 All of these take arguments like printf. */
462 /* Text to be emitted verbatim to the error message stream; this
463 produces no prefix and disables line-wrapping. Use rarely. */
465 verbatim (const char *gmsgid
, ...)
470 va_start (ap
, gmsgid
);
473 text
.format_spec
= _(gmsgid
);
475 pp_format_verbatim (global_dc
->printer
, &text
);
476 pp_flush (global_dc
->printer
);
480 /* An informative note. Use this for additional details on an error
483 inform (const char *gmsgid
, ...)
485 diagnostic_info diagnostic
;
488 va_start (ap
, gmsgid
);
489 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_NOTE
);
490 report_diagnostic (&diagnostic
);
494 /* A warning. Use this for code which is correct according to the
495 relevant language specification but is likely to be buggy anyway. */
497 warning (int opt
, const char *gmsgid
, ...)
499 diagnostic_info diagnostic
;
502 va_start (ap
, gmsgid
);
503 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_WARNING
);
504 diagnostic
.option_index
= opt
;
506 report_diagnostic (&diagnostic
);
511 warning0 (const char *gmsgid
, ...)
513 diagnostic_info diagnostic
;
516 va_start (ap
, gmsgid
);
517 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_WARNING
);
518 report_diagnostic (&diagnostic
);
522 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
523 given on the command line, in which case it issues an error. Use
524 this for diagnostics required by the relevant language standard,
525 if you have chosen not to make them errors.
527 Note that these diagnostics are issued independent of the setting
528 of the -pedantic command-line switch. To get a warning enabled
529 only with that switch, write "if (pedantic) pedwarn (...);" */
531 pedwarn (const char *gmsgid
, ...)
533 diagnostic_info diagnostic
;
536 va_start (ap
, gmsgid
);
537 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
,
538 pedantic_error_kind ());
539 report_diagnostic (&diagnostic
);
543 /* A hard error: the code is definitely ill-formed, and an object file
544 will not be produced. */
546 error (const char *gmsgid
, ...)
548 diagnostic_info diagnostic
;
551 va_start (ap
, gmsgid
);
552 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_ERROR
);
553 report_diagnostic (&diagnostic
);
557 /* "Sorry, not implemented." Use for a language feature which is
558 required by the relevant specification but not implemented by GCC.
559 An object file will not be produced. */
561 sorry (const char *gmsgid
, ...)
563 diagnostic_info diagnostic
;
566 va_start (ap
, gmsgid
);
567 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_SORRY
);
568 report_diagnostic (&diagnostic
);
572 /* An error which is severe enough that we make no attempt to
573 continue. Do not use this for internal consistency checks; that's
574 internal_error. Use of this function should be rare. */
576 fatal_error (const char *gmsgid
, ...)
578 diagnostic_info diagnostic
;
581 va_start (ap
, gmsgid
);
582 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_FATAL
);
583 report_diagnostic (&diagnostic
);
589 /* An internal consistency check has failed. We make no attempt to
590 continue. Note that unless there is debugging value to be had from
591 a more specific message, or some other good reason, you should use
592 abort () instead of calling this function directly. */
594 internal_error (const char *gmsgid
, ...)
596 diagnostic_info diagnostic
;
599 va_start (ap
, gmsgid
);
600 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_ICE
);
601 report_diagnostic (&diagnostic
);
607 /* Special case error functions. Most are implemented in terms of the
608 above, or should be. */
610 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
611 runs its second argument through gettext. */
613 fnotice (FILE *file
, const char *cmsgid
, ...)
617 va_start (ap
, cmsgid
);
618 vfprintf (file
, _(cmsgid
), ap
);
622 /* Inform the user that an error occurred while trying to report some
623 other error. This indicates catastrophic internal inconsistencies,
624 so give up now. But do try to flush out the previous error.
625 This mustn't use internal_error, that will cause infinite recursion. */
628 error_recursion (diagnostic_context
*context
)
630 diagnostic_info diagnostic
;
632 if (context
->lock
< 3)
633 pp_flush (context
->printer
);
636 "Internal compiler error: Error reporting routines re-entered.\n");
638 /* Call diagnostic_action_after_output to get the "please submit a bug
639 report" message. It only looks at the kind field of diagnostic_info. */
640 diagnostic
.kind
= DK_ICE
;
641 diagnostic_action_after_output (context
, &diagnostic
);
643 /* Do not use gcc_unreachable here; that goes through internal_error
644 and therefore would cause infinite recursion. */
648 /* Report an internal compiler error in a friendly manner. This is
649 the function that gets called upon use of abort() in the source
650 code generally, thanks to a special macro. */
653 fancy_abort (const char *file
, int line
, const char *function
)
655 internal_error ("in %s, at %s:%d", function
, trim_filename (file
), line
);
658 /* Really call the system 'abort'. This has to go right at the end of
659 this file, so that there are no functions after it that call abort
660 and get the system abort instead of our macro. */