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, 2010 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
28 #include "coretypes.h"
32 #include "diagnostic.h"
34 #define pedantic_warning_kind(DC) \
35 ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
36 #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR)
37 #define permissive_error_option(DC) ((DC)->opt_permissive)
40 static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1
;
42 static void error_recursion (diagnostic_context
*) ATTRIBUTE_NORETURN
;
44 static void diagnostic_action_after_output (diagnostic_context
*,
46 static void real_abort (void) ATTRIBUTE_NORETURN
;
48 /* Name of program invoked, sans directories. */
52 /* A diagnostic_context surrogate for stderr. */
53 static diagnostic_context global_diagnostic_context
;
54 diagnostic_context
*global_dc
= &global_diagnostic_context
;
57 /* Return a malloc'd string containing MSG formatted a la printf. The
58 caller is responsible for freeing the memory. */
60 build_message_string (const char *msg
, ...)
66 vasprintf (&str
, msg
, ap
);
72 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
74 file_name_as_prefix (const char *f
)
76 return build_message_string ("%s: ", f
);
81 /* Initialize the diagnostic message outputting machinery. */
83 diagnostic_initialize (diagnostic_context
*context
, int n_opts
)
87 /* Allocate a basic pretty-printer. Clients will replace this a
88 much more elaborated pretty-printer if they wish. */
89 context
->printer
= XNEW (pretty_printer
);
90 pp_construct (context
->printer
, NULL
, 0);
91 /* By default, diagnostics are sent to stderr. */
92 context
->printer
->buffer
->stream
= stderr
;
93 /* By default, we emit prefixes once per message. */
94 context
->printer
->wrapping
.rule
= DIAGNOSTICS_SHOW_PREFIX_ONCE
;
96 memset (context
->diagnostic_count
, 0, sizeof context
->diagnostic_count
);
97 context
->some_warnings_are_errors
= false;
98 context
->warning_as_error_requested
= false;
99 context
->n_opts
= n_opts
;
100 context
->classify_diagnostic
= XNEWVEC (diagnostic_t
, n_opts
);
101 for (i
= 0; i
< n_opts
; i
++)
102 context
->classify_diagnostic
[i
] = DK_UNSPECIFIED
;
103 context
->show_option_requested
= false;
104 context
->abort_on_error
= false;
105 context
->show_column
= false;
106 context
->pedantic_errors
= false;
107 context
->permissive
= false;
108 context
->opt_permissive
= 0;
109 context
->fatal_errors
= false;
110 context
->inhibit_warnings
= false;
111 context
->warn_system_headers
= false;
112 context
->internal_error
= NULL
;
113 diagnostic_starter (context
) = default_diagnostic_starter
;
114 diagnostic_finalizer (context
) = default_diagnostic_finalizer
;
115 context
->option_enabled
= NULL
;
116 context
->option_name
= NULL
;
117 context
->last_module
= 0;
118 context
->x_data
= NULL
;
120 context
->inhibit_notes_p
= false;
123 /* Do any cleaning up required after the last diagnostic is emitted. */
126 diagnostic_finish (diagnostic_context
*context
)
128 /* Some of the errors may actually have been warnings. */
129 if (context
->some_warnings_are_errors
)
131 /* -Werror was given. */
132 if (context
->warning_as_error_requested
)
133 pp_verbatim (context
->printer
,
134 _("%s: all warnings being treated as errors\n"),
136 /* At least one -Werror= was given. */
138 pp_verbatim (context
->printer
,
139 _("%s: some warnings being treated as errors\n"),
141 pp_flush (context
->printer
);
145 /* Initialize DIAGNOSTIC, where the message MSG has already been
148 diagnostic_set_info_translated (diagnostic_info
*diagnostic
, const char *msg
,
149 va_list *args
, location_t location
,
152 diagnostic
->message
.err_no
= errno
;
153 diagnostic
->message
.args_ptr
= args
;
154 diagnostic
->message
.format_spec
= msg
;
155 diagnostic
->location
= location
;
156 diagnostic
->override_column
= 0;
157 diagnostic
->kind
= kind
;
158 diagnostic
->option_index
= 0;
161 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
164 diagnostic_set_info (diagnostic_info
*diagnostic
, const char *gmsgid
,
165 va_list *args
, location_t location
,
168 diagnostic_set_info_translated (diagnostic
, _(gmsgid
), args
, location
, kind
);
171 /* Return a malloc'd string describing a location. The caller is
172 responsible for freeing the memory. */
174 diagnostic_build_prefix (diagnostic_context
*context
,
175 diagnostic_info
*diagnostic
)
177 static const char *const diagnostic_kind_text
[] = {
178 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
179 #include "diagnostic.def"
180 #undef DEFINE_DIAGNOSTIC_KIND
183 const char *text
= _(diagnostic_kind_text
[diagnostic
->kind
]);
184 expanded_location s
= expand_location (diagnostic
->location
);
185 if (diagnostic
->override_column
)
186 s
.column
= diagnostic
->override_column
;
187 gcc_assert (diagnostic
->kind
< DK_LAST_DIAGNOSTIC_KIND
);
191 ? build_message_string ("%s: %s", progname
, text
)
192 : context
->show_column
193 ? build_message_string ("%s:%d:%d: %s", s
.file
, s
.line
, s
.column
, text
)
194 : build_message_string ("%s:%d: %s", s
.file
, s
.line
, text
));
197 /* Take any action which is expected to happen after the diagnostic
198 is written out. This function does not always return. */
200 diagnostic_action_after_output (diagnostic_context
*context
,
201 diagnostic_info
*diagnostic
)
203 switch (diagnostic
->kind
)
213 if (context
->abort_on_error
)
215 if (context
->fatal_errors
)
217 fnotice (stderr
, "compilation terminated due to -Wfatal-errors.\n");
218 diagnostic_finish (context
);
219 exit (FATAL_EXIT_CODE
);
224 if (context
->abort_on_error
)
227 fnotice (stderr
, "Please submit a full bug report,\n"
228 "with preprocessed source if appropriate.\n"
229 "See %s for instructions.\n", bug_report_url
);
230 exit (ICE_EXIT_CODE
);
233 if (context
->abort_on_error
)
235 diagnostic_finish (context
);
236 fnotice (stderr
, "compilation terminated.\n");
237 exit (FATAL_EXIT_CODE
);
245 diagnostic_report_current_module (diagnostic_context
*context
)
247 const struct line_map
*map
;
249 if (pp_needs_newline (context
->printer
))
251 pp_newline (context
->printer
);
252 pp_needs_newline (context
->printer
) = false;
255 if (input_location
<= BUILTINS_LOCATION
)
258 map
= linemap_lookup (line_table
, input_location
);
259 if (map
&& diagnostic_last_module_changed (context
, map
))
261 diagnostic_set_last_module (context
, map
);
262 if (! MAIN_FILE_P (map
))
264 map
= INCLUDED_FROM (line_table
, map
);
265 if (context
->show_column
)
266 pp_verbatim (context
->printer
,
267 "In file included from %s:%d:%d",
269 LAST_SOURCE_LINE (map
), LAST_SOURCE_COLUMN (map
));
271 pp_verbatim (context
->printer
,
272 "In file included from %s:%d",
273 map
->to_file
, LAST_SOURCE_LINE (map
));
274 while (! MAIN_FILE_P (map
))
276 map
= INCLUDED_FROM (line_table
, map
);
277 pp_verbatim (context
->printer
,
279 map
->to_file
, LAST_SOURCE_LINE (map
));
281 pp_verbatim (context
->printer
, ":");
282 pp_newline (context
->printer
);
288 default_diagnostic_starter (diagnostic_context
*context
,
289 diagnostic_info
*diagnostic
)
291 diagnostic_report_current_module (context
);
292 pp_set_prefix (context
->printer
, diagnostic_build_prefix (context
,
297 default_diagnostic_finalizer (diagnostic_context
*context
,
298 diagnostic_info
*diagnostic ATTRIBUTE_UNUSED
)
300 pp_destroy_prefix (context
->printer
);
303 /* Interface to specify diagnostic kind overrides. Returns the
304 previous setting, or DK_UNSPECIFIED if the parameters are out of
307 diagnostic_classify_diagnostic (diagnostic_context
*context
,
309 diagnostic_t new_kind
)
311 diagnostic_t old_kind
;
313 if (option_index
<= 0
314 || option_index
>= context
->n_opts
315 || new_kind
>= DK_LAST_DIAGNOSTIC_KIND
)
316 return DK_UNSPECIFIED
;
318 old_kind
= context
->classify_diagnostic
[option_index
];
319 context
->classify_diagnostic
[option_index
] = new_kind
;
323 /* Report a diagnostic message (an error or a warning) as specified by
324 DC. This function is *the* subroutine in terms of which front-ends
325 should implement their specific diagnostic handling modules. The
326 front-end independent format specifiers are exactly those described
327 in the documentation of output_format.
328 Return true if a diagnostic was printed, false otherwise. */
331 diagnostic_report_diagnostic (diagnostic_context
*context
,
332 diagnostic_info
*diagnostic
)
334 location_t location
= diagnostic
->location
;
335 diagnostic_t orig_diag_kind
= diagnostic
->kind
;
336 const char *saved_format_spec
;
338 /* Give preference to being able to inhibit warnings, before they
339 get reclassified to something else. */
340 if ((diagnostic
->kind
== DK_WARNING
|| diagnostic
->kind
== DK_PEDWARN
)
341 && !diagnostic_report_warnings_p (context
, location
))
344 if (diagnostic
->kind
== DK_PEDWARN
)
346 diagnostic
->kind
= pedantic_warning_kind (context
);
347 /* We do this to avoid giving the message for -pedantic-errors. */
348 orig_diag_kind
= diagnostic
->kind
;
351 if (diagnostic
->kind
== DK_NOTE
&& context
->inhibit_notes_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
;
375 if (diagnostic
->option_index
)
377 /* This tests if the user provided the appropriate -Wfoo or
379 if (! context
->option_enabled (diagnostic
->option_index
))
381 /* This tests if the user provided the appropriate -Werror=foo
383 if (context
->classify_diagnostic
[diagnostic
->option_index
] != DK_UNSPECIFIED
)
385 diagnostic
->kind
= context
->classify_diagnostic
[diagnostic
->option_index
];
387 /* This allows for future extensions, like temporarily disabling
388 warnings for ranges of source code. */
389 if (diagnostic
->kind
== DK_IGNORED
)
393 if (orig_diag_kind
== DK_WARNING
&& diagnostic
->kind
== DK_ERROR
)
394 context
->some_warnings_are_errors
= true;
398 if (diagnostic
->kind
== DK_ICE
)
400 #ifndef ENABLE_CHECKING
401 /* When not checking, ICEs are converted to fatal errors when an
402 error has already occurred. This is counteracted by
404 if ((diagnostic_kind_count (context
, DK_ERROR
) > 0
405 || diagnostic_kind_count (context
, DK_SORRY
) > 0)
406 && !context
->abort_on_error
)
408 expanded_location s
= expand_location (diagnostic
->location
);
409 fnotice (stderr
, "%s:%d: confused by earlier errors, bailing out\n",
411 exit (ICE_EXIT_CODE
);
414 if (context
->internal_error
)
415 (*context
->internal_error
) (context
,
416 diagnostic
->message
.format_spec
,
417 diagnostic
->message
.args_ptr
);
419 ++diagnostic_kind_count (context
, diagnostic
->kind
);
421 saved_format_spec
= diagnostic
->message
.format_spec
;
422 if (context
->show_option_requested
)
426 option_text
= context
->option_name (context
, diagnostic
->option_index
,
427 orig_diag_kind
, diagnostic
->kind
);
431 diagnostic
->message
.format_spec
432 = ACONCAT ((diagnostic
->message
.format_spec
,
434 "[", option_text
, "]",
439 diagnostic
->message
.locus
= &diagnostic
->location
;
440 diagnostic
->message
.x_data
= &diagnostic
->x_data
;
441 diagnostic
->x_data
= NULL
;
442 pp_format (context
->printer
, &diagnostic
->message
);
443 (*diagnostic_starter (context
)) (context
, diagnostic
);
444 pp_output_formatted_text (context
->printer
);
445 (*diagnostic_finalizer (context
)) (context
, diagnostic
);
446 pp_flush (context
->printer
);
447 diagnostic_action_after_output (context
, diagnostic
);
448 diagnostic
->message
.format_spec
= saved_format_spec
;
449 diagnostic
->x_data
= NULL
;
456 /* Given a partial pathname as input, return another pathname that
457 shares no directory elements with the pathname of __FILE__. This
458 is used by fancy_abort() to print `Internal compiler error in expr.c'
459 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
462 trim_filename (const char *name
)
464 static const char this_file
[] = __FILE__
;
465 const char *p
= name
, *q
= this_file
;
467 /* First skip any "../" in each filename. This allows us to give a proper
468 reference to a file in a subdirectory. */
469 while (p
[0] == '.' && p
[1] == '.' && IS_DIR_SEPARATOR (p
[2]))
472 while (q
[0] == '.' && q
[1] == '.' && IS_DIR_SEPARATOR (q
[2]))
475 /* Now skip any parts the two filenames have in common. */
476 while (*p
== *q
&& *p
!= 0 && *q
!= 0)
479 /* Now go backwards until the previous directory separator. */
480 while (p
> name
&& !IS_DIR_SEPARATOR (p
[-1]))
486 /* Standard error reporting routines in increasing order of severity.
487 All of these take arguments like printf. */
489 /* Text to be emitted verbatim to the error message stream; this
490 produces no prefix and disables line-wrapping. Use rarely. */
492 verbatim (const char *gmsgid
, ...)
497 va_start (ap
, gmsgid
);
500 text
.format_spec
= _(gmsgid
);
503 pp_format_verbatim (global_dc
->printer
, &text
);
504 pp_flush (global_dc
->printer
);
509 emit_diagnostic (diagnostic_t kind
, location_t location
, int opt
,
510 const char *gmsgid
, ...)
512 diagnostic_info diagnostic
;
515 va_start (ap
, gmsgid
);
516 if (kind
== DK_PERMERROR
)
518 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
,
519 permissive_error_kind (global_dc
));
520 diagnostic
.option_index
= permissive_error_option (global_dc
);
523 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
, kind
);
524 if (kind
== DK_WARNING
|| kind
== DK_PEDWARN
)
525 diagnostic
.option_index
= opt
;
529 return report_diagnostic (&diagnostic
);
532 /* An informative note at LOCATION. Use this for additional details on an error
535 inform (location_t location
, const char *gmsgid
, ...)
537 diagnostic_info diagnostic
;
540 va_start (ap
, gmsgid
);
541 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
, DK_NOTE
);
542 report_diagnostic (&diagnostic
);
546 /* An informative note at LOCATION. Use this for additional details on an
549 inform_n (location_t location
, int n
, const char *singular_gmsgid
,
550 const char *plural_gmsgid
, ...)
552 diagnostic_info diagnostic
;
555 va_start (ap
, plural_gmsgid
);
556 diagnostic_set_info_translated (&diagnostic
,
557 ngettext (singular_gmsgid
, plural_gmsgid
, n
),
558 &ap
, location
, DK_NOTE
);
559 report_diagnostic (&diagnostic
);
563 /* A warning at INPUT_LOCATION. Use this for code which is correct according
564 to the relevant language specification but is likely to be buggy anyway.
565 Returns true if the warning was printed, false if it was inhibited. */
567 warning (int opt
, const char *gmsgid
, ...)
569 diagnostic_info diagnostic
;
572 va_start (ap
, gmsgid
);
573 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_WARNING
);
574 diagnostic
.option_index
= opt
;
577 return report_diagnostic (&diagnostic
);
580 /* A warning at LOCATION. Use this for code which is correct according to the
581 relevant language specification but is likely to be buggy anyway.
582 Returns true if the warning was printed, false if it was inhibited. */
585 warning_at (location_t location
, int opt
, const char *gmsgid
, ...)
587 diagnostic_info diagnostic
;
590 va_start (ap
, gmsgid
);
591 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
, DK_WARNING
);
592 diagnostic
.option_index
= opt
;
594 return report_diagnostic (&diagnostic
);
597 /* A "pedantic" warning at LOCATION: issues a warning unless
598 -pedantic-errors was given on the command line, in which case it
599 issues an error. Use this for diagnostics required by the relevant
600 language standard, if you have chosen not to make them errors.
602 Note that these diagnostics are issued independent of the setting
603 of the -pedantic command-line switch. To get a warning enabled
604 only with that switch, use either "if (pedantic) pedwarn
605 (OPT_pedantic,...)" or just "pedwarn (OPT_pedantic,..)". To get a
606 pedwarn independently of the -pedantic switch use "pedwarn (0,...)".
608 Returns true if the warning was printed, false if it was inhibited. */
611 pedwarn (location_t location
, int opt
, const char *gmsgid
, ...)
613 diagnostic_info diagnostic
;
616 va_start (ap
, gmsgid
);
617 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
, DK_PEDWARN
);
618 diagnostic
.option_index
= opt
;
620 return report_diagnostic (&diagnostic
);
623 /* A "permissive" error at LOCATION: issues an error unless
624 -fpermissive was given on the command line, in which case it issues
625 a warning. Use this for things that really should be errors but we
626 want to support legacy code.
628 Returns true if the warning was printed, false if it was inhibited. */
631 permerror (location_t location
, const char *gmsgid
, ...)
633 diagnostic_info diagnostic
;
636 va_start (ap
, gmsgid
);
637 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
,
638 permissive_error_kind (global_dc
));
639 diagnostic
.option_index
= permissive_error_option (global_dc
);
641 return report_diagnostic (&diagnostic
);
644 /* A hard error: the code is definitely ill-formed, and an object file
645 will not be produced. */
647 error (const char *gmsgid
, ...)
649 diagnostic_info diagnostic
;
652 va_start (ap
, gmsgid
);
653 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_ERROR
);
654 report_diagnostic (&diagnostic
);
658 /* A hard error: the code is definitely ill-formed, and an object file
659 will not be produced. */
661 error_n (location_t location
, int n
, const char *singular_gmsgid
,
662 const char *plural_gmsgid
, ...)
664 diagnostic_info diagnostic
;
667 va_start (ap
, plural_gmsgid
);
668 diagnostic_set_info_translated (&diagnostic
,
669 ngettext (singular_gmsgid
, plural_gmsgid
, n
),
670 &ap
, location
, DK_ERROR
);
671 report_diagnostic (&diagnostic
);
675 /* Same as ebove, but use location LOC instead of input_location. */
677 error_at (location_t loc
, const char *gmsgid
, ...)
679 diagnostic_info diagnostic
;
682 va_start (ap
, gmsgid
);
683 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, loc
, DK_ERROR
);
684 report_diagnostic (&diagnostic
);
688 /* "Sorry, not implemented." Use for a language feature which is
689 required by the relevant specification but not implemented by GCC.
690 An object file will not be produced. */
692 sorry (const char *gmsgid
, ...)
694 diagnostic_info diagnostic
;
697 va_start (ap
, gmsgid
);
698 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_SORRY
);
699 report_diagnostic (&diagnostic
);
703 /* Return true if an error or a "sorry" has been seen. Various
704 processing is disabled after errors. */
708 return errorcount
|| sorrycount
;
711 /* An error which is severe enough that we make no attempt to
712 continue. Do not use this for internal consistency checks; that's
713 internal_error. Use of this function should be rare. */
715 fatal_error (const char *gmsgid
, ...)
717 diagnostic_info diagnostic
;
720 va_start (ap
, gmsgid
);
721 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_FATAL
);
722 report_diagnostic (&diagnostic
);
728 /* An internal consistency check has failed. We make no attempt to
729 continue. Note that unless there is debugging value to be had from
730 a more specific message, or some other good reason, you should use
731 abort () instead of calling this function directly. */
733 internal_error (const char *gmsgid
, ...)
735 diagnostic_info diagnostic
;
738 va_start (ap
, gmsgid
);
739 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_ICE
);
740 report_diagnostic (&diagnostic
);
746 /* Special case error functions. Most are implemented in terms of the
747 above, or should be. */
749 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
750 runs its second argument through gettext. */
752 fnotice (FILE *file
, const char *cmsgid
, ...)
756 va_start (ap
, cmsgid
);
757 vfprintf (file
, _(cmsgid
), ap
);
761 /* Inform the user that an error occurred while trying to report some
762 other error. This indicates catastrophic internal inconsistencies,
763 so give up now. But do try to flush out the previous error.
764 This mustn't use internal_error, that will cause infinite recursion. */
767 error_recursion (diagnostic_context
*context
)
769 diagnostic_info diagnostic
;
771 if (context
->lock
< 3)
772 pp_flush (context
->printer
);
775 "Internal compiler error: Error reporting routines re-entered.\n");
777 /* Call diagnostic_action_after_output to get the "please submit a bug
778 report" message. It only looks at the kind field of diagnostic_info. */
779 diagnostic
.kind
= DK_ICE
;
780 diagnostic_action_after_output (context
, &diagnostic
);
782 /* Do not use gcc_unreachable here; that goes through internal_error
783 and therefore would cause infinite recursion. */
787 /* Report an internal compiler error in a friendly manner. This is
788 the function that gets called upon use of abort() in the source
789 code generally, thanks to a special macro. */
792 fancy_abort (const char *file
, int line
, const char *function
)
794 internal_error ("in %s, at %s:%d", function
, trim_filename (file
), line
);
797 /* Really call the system 'abort'. This has to go right at the end of
798 this file, so that there are no functions after it that call abort
799 and get the system abort instead of our macro. */