1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005
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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 /* This file implements the language independent aspect of diagnostic
28 #undef FLOAT /* This is for hpux. They should change hpux. */
29 #undef FFS /* Some systems define this in param.h. */
31 #include "coretypes.h"
40 #include "diagnostic.h"
41 #include "langhooks.h"
42 #include "langhooks-def.h"
47 static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1
;
49 static void default_diagnostic_starter (diagnostic_context
*,
51 static void default_diagnostic_finalizer (diagnostic_context
*,
54 static void error_recursion (diagnostic_context
*) ATTRIBUTE_NORETURN
;
55 static bool diagnostic_count_diagnostic (diagnostic_context
*,
57 static void diagnostic_action_after_output (diagnostic_context
*,
59 static void real_abort (void) ATTRIBUTE_NORETURN
;
61 /* A diagnostic_context surrogate for stderr. */
62 static diagnostic_context global_diagnostic_context
;
63 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
= xmalloc (sizeof (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
->prefixing_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 context
->show_option_requested
= false;
106 context
->abort_on_error
= false;
107 context
->internal_error
= NULL
;
108 diagnostic_starter (context
) = default_diagnostic_starter
;
109 diagnostic_finalizer (context
) = default_diagnostic_finalizer
;
110 context
->last_module
= 0;
111 context
->last_function
= NULL
;
116 diagnostic_set_info (diagnostic_info
*diagnostic
, const char *msgid
,
117 va_list *args
, location_t location
,
120 diagnostic
->message
.err_no
= errno
;
121 diagnostic
->message
.args_ptr
= args
;
122 diagnostic
->message
.format_spec
= _(msgid
);
123 diagnostic
->location
= location
;
124 diagnostic
->kind
= kind
;
125 diagnostic
->option_index
= 0;
128 /* Return a malloc'd string describing a location. The caller is
129 responsible for freeing the memory. */
131 diagnostic_build_prefix (diagnostic_info
*diagnostic
)
133 static const char *const diagnostic_kind_text
[] = {
134 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
135 #include "diagnostic.def"
136 #undef DEFINE_DIAGNOSTIC_KIND
139 const char *text
= _(diagnostic_kind_text
[diagnostic
->kind
]);
140 expanded_location s
= expand_location (diagnostic
->location
);
141 gcc_assert (diagnostic
->kind
< DK_LAST_DIAGNOSTIC_KIND
);
145 ? build_message_string ("%s: %s", progname
, text
)
146 #ifdef USE_MAPPED_LOCATION
147 : flag_show_column
&& s
.column
!= 0
148 ? build_message_string ("%s:%d:%d: %s", s
.file
, s
.line
, s
.column
, text
)
150 : build_message_string ("%s:%d: %s", s
.file
, s
.line
, text
));
153 /* Count a diagnostic. Return true if the message should be printed. */
155 diagnostic_count_diagnostic (diagnostic_context
*context
,
156 diagnostic_info
*diagnostic
)
158 diagnostic_t kind
= diagnostic
->kind
;
165 #ifndef ENABLE_CHECKING
166 /* When not checking, ICEs are converted to fatal errors when an
167 error has already occurred. This is counteracted by
169 if ((diagnostic_kind_count (context
, DK_ERROR
) > 0
170 || diagnostic_kind_count (context
, DK_SORRY
) > 0)
171 && !context
->abort_on_error
)
173 expanded_location s
= expand_location (diagnostic
->location
);
174 fnotice (stderr
, "%s:%d: confused by earlier errors, bailing out\n",
176 exit (FATAL_EXIT_CODE
);
179 if (context
->internal_error
)
180 (*context
->internal_error
) (diagnostic
->message
.format_spec
,
181 diagnostic
->message
.args_ptr
);
184 case DK_FATAL
: case DK_SORRY
:
185 case DK_ANACHRONISM
: case DK_NOTE
:
186 ++diagnostic_kind_count (context
, kind
);
190 if (!diagnostic_report_warnings_p ())
193 if (!context
->warning_as_error_requested
)
195 ++diagnostic_kind_count (context
, DK_WARNING
);
198 else if (context
->issue_warnings_are_errors_message
)
200 pp_verbatim (context
->printer
,
201 "%s: warnings being treated as errors\n", progname
);
202 context
->issue_warnings_are_errors_message
= false;
205 /* And fall through. */
207 ++diagnostic_kind_count (context
, DK_ERROR
);
214 /* Take any action which is expected to happen after the diagnostic
215 is written out. This function does not always return. */
217 diagnostic_action_after_output (diagnostic_context
*context
,
218 diagnostic_info
*diagnostic
)
220 switch (diagnostic
->kind
)
230 if (context
->abort_on_error
)
232 if (flag_fatal_errors
)
234 fnotice (stderr
, "compilation terminated due to -Wfatal-errors.\n");
235 exit (FATAL_EXIT_CODE
);
240 if (context
->abort_on_error
)
243 fnotice (stderr
, "Please submit a full bug report,\n"
244 "with preprocessed source if appropriate.\n"
245 "See %s for instructions.\n", bug_report_url
);
246 exit (FATAL_EXIT_CODE
);
249 if (context
->abort_on_error
)
252 fnotice (stderr
, "compilation terminated.\n");
253 exit (FATAL_EXIT_CODE
);
260 /* Prints out, if necessary, the name of the current function
261 that caused an error. Called from all error and warning functions. */
263 diagnostic_report_current_function (diagnostic_context
*context
)
265 diagnostic_report_current_module (context
);
266 lang_hooks
.print_error_function (context
, input_filename
);
270 diagnostic_report_current_module (diagnostic_context
*context
)
272 struct file_stack
*p
;
274 if (pp_needs_newline (context
->printer
))
276 pp_newline (context
->printer
);
277 pp_needs_newline (context
->printer
) = false;
280 p
= input_file_stack
;
281 if (p
&& diagnostic_last_module_changed (context
))
283 expanded_location xloc
= expand_location (p
->location
);
284 pp_verbatim (context
->printer
,
285 "In file included from %s:%d",
286 xloc
.file
, xloc
.line
);
287 while ((p
= p
->next
) != NULL
)
289 xloc
= expand_location (p
->location
);
290 pp_verbatim (context
->printer
,
292 xloc
.file
, xloc
.line
);
294 pp_verbatim (context
->printer
, ":\n");
295 diagnostic_set_last_module (context
);
300 default_diagnostic_starter (diagnostic_context
*context
,
301 diagnostic_info
*diagnostic
)
303 diagnostic_report_current_function (context
);
304 pp_set_prefix (context
->printer
, diagnostic_build_prefix (diagnostic
));
308 default_diagnostic_finalizer (diagnostic_context
*context
,
309 diagnostic_info
*diagnostic ATTRIBUTE_UNUSED
)
311 pp_destroy_prefix (context
->printer
);
314 /* Report a diagnostic message (an error or a warning) as specified by
315 DC. This function is *the* subroutine in terms of which front-ends
316 should implement their specific diagnostic handling modules. The
317 front-end independent format specifiers are exactly those described
318 in the documentation of output_format. */
321 diagnostic_report_diagnostic (diagnostic_context
*context
,
322 diagnostic_info
*diagnostic
)
324 if (context
->lock
> 0)
326 /* If we're reporting an ICE in the middle of some other error,
327 try to flush out the previous error, then let this one
328 through. Don't do this more than once. */
329 if (diagnostic
->kind
== DK_ICE
&& context
->lock
== 1)
330 pp_flush (context
->printer
);
332 error_recursion (context
);
335 if (diagnostic
->option_index
336 && ! option_enabled (diagnostic
->option_index
))
341 if (diagnostic_count_diagnostic (context
, diagnostic
))
343 const char *saved_format_spec
= diagnostic
->message
.format_spec
;
345 if (context
->show_option_requested
&& diagnostic
->option_index
)
346 diagnostic
->message
.format_spec
347 = ACONCAT ((diagnostic
->message
.format_spec
,
348 " [", cl_options
[diagnostic
->option_index
].opt_text
, "]", NULL
));
350 pp_prepare_to_format (context
->printer
, &diagnostic
->message
,
351 &diagnostic
->location
);
352 (*diagnostic_starter (context
)) (context
, diagnostic
);
353 pp_format_text (context
->printer
, &diagnostic
->message
);
354 (*diagnostic_finalizer (context
)) (context
, diagnostic
);
355 pp_flush (context
->printer
);
356 diagnostic_action_after_output (context
, diagnostic
);
357 diagnostic
->message
.format_spec
= saved_format_spec
;
363 /* Given a partial pathname as input, return another pathname that
364 shares no directory elements with the pathname of __FILE__. This
365 is used by fancy_abort() to print `Internal compiler error in expr.c'
366 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
369 trim_filename (const char *name
)
371 static const char this_file
[] = __FILE__
;
372 const char *p
= name
, *q
= this_file
;
374 /* First skip any "../" in each filename. This allows us to give a proper
375 reference to a file in a subdirectory. */
376 while (p
[0] == '.' && p
[1] == '.' && IS_DIR_SEPARATOR (p
[2]))
379 while (q
[0] == '.' && q
[1] == '.' && IS_DIR_SEPARATOR (q
[2]))
382 /* Now skip any parts the two filenames have in common. */
383 while (*p
== *q
&& *p
!= 0 && *q
!= 0)
386 /* Now go backwards until the previous directory separator. */
387 while (p
> name
&& !IS_DIR_SEPARATOR (p
[-1]))
393 /* Standard error reporting routines in increasing order of severity.
394 All of these take arguments like printf. */
396 /* Text to be emitted verbatim to the error message stream; this
397 produces no prefix and disables line-wrapping. Use rarely. */
399 verbatim (const char *msgid
, ...)
404 va_start (ap
, msgid
);
407 text
.format_spec
= _(msgid
);
408 pp_format_verbatim (global_dc
->printer
, &text
);
409 pp_flush (global_dc
->printer
);
413 /* An informative note. Use this for additional details on an error
416 inform (const char *msgid
, ...)
418 diagnostic_info diagnostic
;
421 va_start (ap
, msgid
);
422 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_location
, DK_NOTE
);
423 report_diagnostic (&diagnostic
);
427 /* A warning. Use this for code which is correct according to the
428 relevant language specification but is likely to be buggy anyway. */
430 warning (int opt
, const char *msgid
, ...)
432 diagnostic_info diagnostic
;
435 va_start (ap
, msgid
);
436 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_location
, DK_WARNING
);
437 diagnostic
.option_index
= opt
;
439 report_diagnostic (&diagnostic
);
444 warning0 (const char *msgid
, ...)
446 diagnostic_info diagnostic
;
449 va_start (ap
, msgid
);
450 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_location
, DK_WARNING
);
451 report_diagnostic (&diagnostic
);
455 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
456 given on the command line, in which case it issues an error. Use
457 this for diagnostics required by the relevant language standard,
458 if you have chosen not to make them errors.
460 Note that these diagnostics are issued independent of the setting
461 of the -pedantic command-line switch. To get a warning enabled
462 only with that switch, write "if (pedantic) pedwarn (...);" */
464 pedwarn (const char *msgid
, ...)
466 diagnostic_info diagnostic
;
469 va_start (ap
, msgid
);
470 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_location
,
471 pedantic_error_kind ());
472 report_diagnostic (&diagnostic
);
476 /* A hard error: the code is definitely ill-formed, and an object file
477 will not be produced. */
479 error (const char *msgid
, ...)
481 diagnostic_info diagnostic
;
484 va_start (ap
, msgid
);
485 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_location
, DK_ERROR
);
486 report_diagnostic (&diagnostic
);
490 /* "Sorry, not implemented." Use for a language feature which is
491 required by the relevant specification but not implemented by GCC.
492 An object file will not be produced. */
494 sorry (const char *msgid
, ...)
496 diagnostic_info diagnostic
;
499 va_start (ap
, msgid
);
500 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_location
, DK_SORRY
);
501 report_diagnostic (&diagnostic
);
505 /* An error which is severe enough that we make no attempt to
506 continue. Do not use this for internal consistency checks; that's
507 internal_error. Use of this function should be rare. */
509 fatal_error (const char *msgid
, ...)
511 diagnostic_info diagnostic
;
514 va_start (ap
, msgid
);
515 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_location
, DK_FATAL
);
516 report_diagnostic (&diagnostic
);
522 /* An internal consistency check has failed. We make no attempt to
523 continue. Note that unless there is debugging value to be had from
524 a more specific message, or some other good reason, you should use
525 abort () instead of calling this function directly. */
527 internal_error (const char *msgid
, ...)
529 diagnostic_info diagnostic
;
532 va_start (ap
, msgid
);
533 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_location
, DK_ICE
);
534 report_diagnostic (&diagnostic
);
540 /* Special case error functions. Most are implemented in terms of the
541 above, or should be. */
543 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
544 runs its second argument through gettext. */
546 fnotice (FILE *file
, const char *msgid
, ...)
550 va_start (ap
, msgid
);
551 vfprintf (file
, _(msgid
), ap
);
555 /* Inform the user that an error occurred while trying to report some
556 other error. This indicates catastrophic internal inconsistencies,
557 so give up now. But do try to flush out the previous error.
558 This mustn't use internal_error, that will cause infinite recursion. */
561 error_recursion (diagnostic_context
*context
)
563 diagnostic_info diagnostic
;
565 if (context
->lock
< 3)
566 pp_flush (context
->printer
);
569 "Internal compiler error: Error reporting routines re-entered.\n");
571 /* Call diagnostic_action_after_output to get the "please submit a bug
572 report" message. It only looks at the kind field of diagnostic_info. */
573 diagnostic
.kind
= DK_ICE
;
574 diagnostic_action_after_output (context
, &diagnostic
);
576 /* Do not use gcc_unreachable here; that goes through internal_error
577 and therefore would cause infinite recursion. */
581 /* Report an internal compiler error in a friendly manner. This is
582 the function that gets called upon use of abort() in the source
583 code generally, thanks to a special macro. */
586 fancy_abort (const char *file
, int line
, const char *function
)
588 internal_error ("in %s, at %s:%d", function
, trim_filename (file
), line
);
591 /* Really call the system 'abort'. This has to go right at the end of
592 this file, so that there are no functions after it that call abort
593 and get the system abort instead of our macro. */