1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
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"
38 #include "diagnostic.h"
39 #include "langhooks.h"
40 #include "langhooks-def.h"
44 static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1
;
46 static void default_diagnostic_starter (diagnostic_context
*,
48 static void default_diagnostic_finalizer (diagnostic_context
*,
51 static void error_recursion (diagnostic_context
*) ATTRIBUTE_NORETURN
;
52 static bool text_specifies_location (text_info
*, location_t
*);
53 static bool diagnostic_count_diagnostic (diagnostic_context
*,
55 static void diagnostic_action_after_output (diagnostic_context
*,
57 static void real_abort (void) ATTRIBUTE_NORETURN
;
59 extern int rtl_dump_and_exit
;
61 /* A diagnostic_context surrogate for stderr. */
62 static diagnostic_context global_diagnostic_context
;
63 diagnostic_context
*global_dc
= &global_diagnostic_context
;
65 /* Boilerplate text used in two locations. */
66 #define bug_report_request \
67 "Please submit a full bug report,\n\
68 with preprocessed source if appropriate.\n\
69 See %s for instructions.\n"
72 /* Return a malloc'd string containing MSG formatted a la printf. The
73 caller is responsible for freeing the memory. */
75 build_message_string (const char *msg
, ...)
81 vasprintf (&str
, msg
, ap
);
87 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
89 file_name_as_prefix (const char *f
)
91 return build_message_string ("%s: ", f
);
96 /* Initialize the diagnostic message outputting machinery. */
98 diagnostic_initialize (diagnostic_context
*context
)
100 /* Allocate a basic pretty-printer. Clients will replace this a
101 much more elaborated pretty-printer if they wish. */
102 context
->printer
= xmalloc (sizeof (pretty_printer
));
103 pp_construct (context
->printer
, NULL
, 0);
104 /* By default, diagnostics are sent to stderr. */
105 context
->printer
->buffer
->stream
= stderr
;
106 /* By default, we emit prefixes once per message. */
107 context
->printer
->prefixing_rule
= DIAGNOSTICS_SHOW_PREFIX_ONCE
;
109 memset (context
->diagnostic_count
, 0, sizeof context
->diagnostic_count
);
110 context
->warnings_are_errors_message
= warnings_are_errors
;
111 context
->abort_on_error
= false;
112 context
->internal_error
= NULL
;
113 diagnostic_starter (context
) = default_diagnostic_starter
;
114 diagnostic_finalizer (context
) = default_diagnostic_finalizer
;
115 context
->last_module
= 0;
116 context
->last_function
= NULL
;
118 context
->x_data
= NULL
;
121 /* Returns true if the next format specifier in TEXT is a format specifier
122 for a location_t. If so, update the object pointed by LOCUS to reflect
123 the specified location in *TEXT->args_ptr. */
125 text_specifies_location (text_info
*text
, location_t
*locus
)
128 /* Skip any leading text. */
129 for (p
= text
->format_spec
; *p
&& *p
!= '%'; ++p
)
132 /* Extract the location information if any. */
133 if (*p
== '%' && *++p
== 'H')
135 *locus
= *va_arg (*text
->args_ptr
, location_t
*);
136 text
->format_spec
= p
+ 1;
144 diagnostic_set_info (diagnostic_info
*diagnostic
, const char *msgid
,
145 va_list *args
, location_t location
,
148 diagnostic
->message
.err_no
= errno
;
149 diagnostic
->message
.args_ptr
= args
;
150 diagnostic
->message
.format_spec
= _(msgid
);
151 /* If the diagnostic message doesn't specify a location,
153 if (!text_specifies_location (&diagnostic
->message
, &diagnostic
->location
))
154 diagnostic
->location
= location
;
155 diagnostic
->kind
= kind
;
158 /* Return a malloc'd string describing a location. The caller is
159 responsible for freeing the memory. */
161 diagnostic_build_prefix (diagnostic_info
*diagnostic
)
163 static const char *const diagnostic_kind_text
[] = {
164 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
165 #include "diagnostic.def"
166 #undef DEFINE_DIAGNOSTIC_KIND
169 if (diagnostic
->kind
>= DK_LAST_DIAGNOSTIC_KIND
)
172 return diagnostic
->location
.file
173 ? build_message_string ("%s:%d: %s",
174 diagnostic
->location
.file
,
175 diagnostic
->location
.line
,
176 _(diagnostic_kind_text
[diagnostic
->kind
]))
177 : build_message_string ("%s: %s", progname
,
178 _(diagnostic_kind_text
[diagnostic
->kind
]));
181 /* Count a diagnostic. Return true if the message should be printed. */
183 diagnostic_count_diagnostic (diagnostic_context
*context
,
184 diagnostic_info
*diagnostic
)
186 diagnostic_t kind
= diagnostic
->kind
;
194 #ifndef ENABLE_CHECKING
195 /* When not checking, ICEs are converted to fatal errors when an
196 error has already occurred. This is counteracted by
198 if ((diagnostic_kind_count (context
, DK_ERROR
) > 0
199 || diagnostic_kind_count (context
, DK_SORRY
) > 0)
200 && !context
->abort_on_error
)
202 fnotice (stderr
, "%s:%d: confused by earlier errors, bailing out\n",
203 diagnostic
->location
.file
, diagnostic
->location
.line
);
204 exit (FATAL_EXIT_CODE
);
207 if (context
->internal_error
)
208 (*context
->internal_error
) (diagnostic
->message
.format_spec
,
209 diagnostic
->message
.args_ptr
);
212 case DK_FATAL
: case DK_SORRY
:
213 case DK_ANACHRONISM
: case DK_NOTE
:
214 ++diagnostic_kind_count (context
, kind
);
218 if (!diagnostic_report_warnings_p ())
221 if (!warnings_are_errors
)
223 ++diagnostic_kind_count (context
, DK_WARNING
);
227 if (context
->warnings_are_errors_message
)
229 pp_verbatim (context
->printer
,
230 "%s: warnings being treated as errors\n", progname
);
231 context
->warnings_are_errors_message
= false;
234 /* And fall through. */
236 ++diagnostic_kind_count (context
, DK_ERROR
);
243 /* Take any action which is expected to happen after the diagnostic
244 is written out. This function does not always return. */
246 diagnostic_action_after_output (diagnostic_context
*context
,
247 diagnostic_info
*diagnostic
)
249 switch (diagnostic
->kind
)
259 if (context
->abort_on_error
)
264 if (context
->abort_on_error
)
267 fnotice (stderr
, bug_report_request
, bug_report_url
);
268 exit (FATAL_EXIT_CODE
);
271 if (context
->abort_on_error
)
274 fnotice (stderr
, "compilation terminated.\n");
275 exit (FATAL_EXIT_CODE
);
282 /* Called when the start of a function definition is parsed,
283 this function prints on stderr the name of the function. */
285 announce_function (tree decl
)
289 if (rtl_dump_and_exit
)
290 verbatim ("%s ", IDENTIFIER_POINTER (DECL_NAME (decl
)));
292 verbatim (" %s", (*lang_hooks
.decl_printable_name
) (decl
, 2));
294 global_dc
->printer
->need_newline
= true;
295 diagnostic_set_last_function (global_dc
);
299 /* The default function to print out name of current function that caused
302 lhd_print_error_function (diagnostic_context
*context
, const char *file
)
304 if (diagnostic_last_function_changed (context
))
306 const char *old_prefix
= context
->printer
->prefix
;
307 char *new_prefix
= file
? build_message_string ("%s: ", file
) : NULL
;
309 pp_set_prefix (context
->printer
, new_prefix
);
311 if (current_function_decl
== NULL
)
312 pp_string (context
->printer
, _("At top level:"));
315 if (TREE_CODE (TREE_TYPE (current_function_decl
)) == METHOD_TYPE
)
317 (context
->printer
, "In member function `%s':",
318 (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2));
321 (context
->printer
, "In function `%s':",
322 (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2));
324 pp_newline (context
->printer
);
326 diagnostic_set_last_function (context
);
327 pp_flush (context
->printer
);
328 context
->printer
->prefix
= old_prefix
;
329 free ((char*) new_prefix
);
333 /* Prints out, if necessary, the name of the current function
334 that caused an error. Called from all error and warning functions.
335 We ignore the FILE parameter, as it cannot be relied upon. */
338 diagnostic_report_current_function (diagnostic_context
*context
)
340 diagnostic_report_current_module (context
);
341 (*lang_hooks
.print_error_function
) (context
, input_filename
);
345 diagnostic_report_current_module (diagnostic_context
*context
)
347 struct file_stack
*p
;
349 if (pp_needs_newline (context
->printer
))
351 pp_newline (context
->printer
);
352 pp_needs_newline (context
->printer
) = false;
355 if (input_file_stack
&& diagnostic_last_module_changed (context
))
357 p
= input_file_stack
;
358 pp_verbatim (context
->printer
,
359 "In file included from %s:%d",
360 p
->location
.file
, p
->location
.line
);
361 while ((p
= p
->next
) != NULL
)
362 pp_verbatim (context
->printer
,
364 p
->location
.file
, p
->location
.line
);
365 pp_verbatim (context
->printer
, ":\n");
366 diagnostic_set_last_module (context
);
371 default_diagnostic_starter (diagnostic_context
*context
,
372 diagnostic_info
*diagnostic
)
374 diagnostic_report_current_function (context
);
375 pp_set_prefix (context
->printer
, diagnostic_build_prefix (diagnostic
));
379 default_diagnostic_finalizer (diagnostic_context
*context
,
380 diagnostic_info
*diagnostic
__attribute__((unused
)))
382 pp_destroy_prefix (context
->printer
);
385 /* Report a diagnostic message (an error or a warning) as specified by
386 DC. This function is *the* subroutine in terms of which front-ends
387 should implement their specific diagnostic handling modules. The
388 front-end independent format specifiers are exactly those described
389 in the documentation of output_format. */
392 diagnostic_report_diagnostic (diagnostic_context
*context
,
393 diagnostic_info
*diagnostic
)
395 if (context
->lock
++ && diagnostic
->kind
< DK_SORRY
)
396 error_recursion (context
);
398 if (diagnostic_count_diagnostic (context
, diagnostic
))
400 (*diagnostic_starter (context
)) (context
, diagnostic
);
401 pp_format_text (context
->printer
, &diagnostic
->message
);
402 (*diagnostic_finalizer (context
)) (context
, diagnostic
);
403 pp_flush (context
->printer
);
404 diagnostic_action_after_output (context
, diagnostic
);
410 /* Given a partial pathname as input, return another pathname that
411 shares no directory elements with the pathname of __FILE__. This
412 is used by fancy_abort() to print `Internal compiler error in expr.c'
413 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
416 trim_filename (const char *name
)
418 static const char this_file
[] = __FILE__
;
419 const char *p
= name
, *q
= this_file
;
421 /* First skip any "../" in each filename. This allows us to give a proper
422 reference to a file in a subdirectory. */
423 while (p
[0] == '.' && p
[1] == '.'
424 && (p
[2] == DIR_SEPARATOR
425 #ifdef DIR_SEPARATOR_2
426 || p
[2] == DIR_SEPARATOR_2
431 while (q
[0] == '.' && q
[1] == '.'
432 && (q
[2] == DIR_SEPARATOR
433 #ifdef DIR_SEPARATOR_2
434 || p
[2] == DIR_SEPARATOR_2
439 /* Now skip any parts the two filenames have in common. */
440 while (*p
== *q
&& *p
!= 0 && *q
!= 0)
443 /* Now go backwards until the previous directory separator. */
444 while (p
> name
&& p
[-1] != DIR_SEPARATOR
445 #ifdef DIR_SEPARATOR_2
446 && p
[-1] != DIR_SEPARATOR_2
454 /* Standard error reporting routines in increasing order of severity.
455 All of these take arguments like printf. */
457 /* Text to be emitted verbatim to the error message stream; this
458 produces no prefix and disables line-wrapping. Use rarely. */
460 verbatim (const char *msgid
, ...)
465 va_start (ap
, msgid
);
468 text
.format_spec
= _(msgid
);
469 pp_format_verbatim (global_dc
->printer
, &text
);
470 pp_flush (global_dc
->printer
);
474 /* An informative note. Use this for additional details on an error
477 inform (const char *msgid
, ...)
479 diagnostic_info diagnostic
;
482 va_start (ap
, msgid
);
483 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_location
, DK_NOTE
);
484 report_diagnostic (&diagnostic
);
488 /* A warning. Use this for code which is correct according to the
489 relevant language specification but is likely to be buggy anyway. */
491 warning (const char *msgid
, ...)
493 diagnostic_info diagnostic
;
496 va_start (ap
, msgid
);
497 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_location
, DK_WARNING
);
498 report_diagnostic (&diagnostic
);
502 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
503 given on the command line, in which case it issues an error. Use
504 this for diagnostics required by the relevant language standard,
505 if you have chosen not to make them errors.
507 Note that these diagnostics are issued independent of the setting
508 of the -pedantic command-line switch. To get a warning enabled
509 only with that switch, write "if (pedantic) pedwarn (...);" */
511 pedwarn (const char *msgid
, ...)
513 diagnostic_info diagnostic
;
516 va_start (ap
, msgid
);
517 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_location
,
518 pedantic_error_kind ());
519 report_diagnostic (&diagnostic
);
523 /* A hard error: the code is definitely ill-formed, and an object file
524 will not be produced. */
526 error (const char *msgid
, ...)
528 diagnostic_info diagnostic
;
531 va_start (ap
, msgid
);
532 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_location
, DK_ERROR
);
533 report_diagnostic (&diagnostic
);
537 /* "Sorry, not implemented." Use for a language feature which is
538 required by the relevant specification but not implemented by GCC.
539 An object file will not be produced. */
541 sorry (const char *msgid
, ...)
543 diagnostic_info diagnostic
;
546 va_start (ap
, msgid
);
547 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_location
, DK_SORRY
);
548 report_diagnostic (&diagnostic
);
552 /* An error which is severe enough that we make no attempt to
553 continue. Do not use this for internal consistency checks; that's
554 internal_error. Use of this function should be rare. */
556 fatal_error (const char *msgid
, ...)
558 diagnostic_info diagnostic
;
561 va_start (ap
, msgid
);
562 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_location
, DK_FATAL
);
563 report_diagnostic (&diagnostic
);
570 /* An internal consistency check has failed. We make no attempt to
571 continue. Note that unless there is debugging value to be had from
572 a more specific message, or some other good reason, you should use
573 abort () instead of calling this function directly. */
575 internal_error (const char *msgid
, ...)
577 diagnostic_info diagnostic
;
580 va_start (ap
, msgid
);
581 diagnostic_set_info (&diagnostic
, msgid
, &ap
, input_location
, DK_ICE
);
582 report_diagnostic (&diagnostic
);
589 /* Special case error functions. Most are implemented in terms of the
590 above, or should be. */
592 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
593 runs its second argument through gettext. */
595 fnotice (FILE *file
, const char *msgid
, ...)
599 va_start (ap
, msgid
);
600 vfprintf (file
, _(msgid
), ap
);
604 /* Warn about a use of an identifier which was marked deprecated. */
606 warn_deprecated_use (tree node
)
608 if (node
== 0 || !warn_deprecated_decl
)
612 warning ("`%s' is deprecated (declared at %s:%d)",
613 IDENTIFIER_POINTER (DECL_NAME (node
)),
614 DECL_SOURCE_FILE (node
), DECL_SOURCE_LINE (node
));
615 else if (TYPE_P (node
))
617 const char *what
= NULL
;
618 tree decl
= TYPE_STUB_DECL (node
);
620 if (TREE_CODE (TYPE_NAME (node
)) == IDENTIFIER_NODE
)
621 what
= IDENTIFIER_POINTER (TYPE_NAME (node
));
622 else if (TREE_CODE (TYPE_NAME (node
)) == TYPE_DECL
623 && DECL_NAME (TYPE_NAME (node
)))
624 what
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node
)));
629 warning ("`%s' is deprecated (declared at %s:%d)", what
,
630 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
));
632 warning ("`%s' is deprecated", what
);
635 warning ("type is deprecated (declared at %s:%d)",
636 DECL_SOURCE_FILE (decl
), DECL_SOURCE_LINE (decl
));
638 warning ("type is deprecated");
642 /* Inform the user that an error occurred while trying to report some
643 other error. This indicates catastrophic internal inconsistencies,
644 so give up now. But do try to flush out the previous error.
645 This mustn't use internal_error, that will cause infinite recursion. */
648 error_recursion (diagnostic_context
*context
)
650 if (context
->lock
< 3)
651 pp_flush (context
->printer
);
654 "Internal compiler error: Error reporting routines re-entered.\n");
655 fnotice (stderr
, bug_report_request
, bug_report_url
);
656 exit (FATAL_EXIT_CODE
);
659 /* Report an internal compiler error in a friendly manner. This is
660 the function that gets called upon use of abort() in the source
661 code generally, thanks to a special macro. */
664 fancy_abort (const char *file
, int line
, const char *function
)
666 internal_error ("in %s, at %s:%d", function
, trim_filename (file
), line
);
669 /* Really call the system 'abort'. This has to go right at the end of
670 this file, so that there are no functions after it that call abort
671 and get the system abort instead of our macro. */