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, 2011, 2012 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"
33 #include "backtrace.h"
34 #include "diagnostic.h"
36 #define pedantic_warning_kind(DC) \
37 ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
38 #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR)
39 #define permissive_error_option(DC) ((DC)->opt_permissive)
42 static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1
;
44 static void error_recursion (diagnostic_context
*) ATTRIBUTE_NORETURN
;
46 static void diagnostic_action_after_output (diagnostic_context
*,
48 static void real_abort (void) ATTRIBUTE_NORETURN
;
50 /* Name of program invoked, sans directories. */
54 /* A diagnostic_context surrogate for stderr. */
55 static diagnostic_context global_diagnostic_context
;
56 diagnostic_context
*global_dc
= &global_diagnostic_context
;
59 /* Return a malloc'd string containing MSG formatted a la printf. The
60 caller is responsible for freeing the memory. */
62 build_message_string (const char *msg
, ...)
68 vasprintf (&str
, msg
, ap
);
74 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
76 file_name_as_prefix (const char *f
)
78 return build_message_string ("%s: ", f
);
83 /* Return the value of the getenv("COLUMNS") as an integer. If the
84 value is not set to a positive integer, then return INT_MAX. */
88 const char * s
= getenv ("COLUMNS");
97 /* Set caret_max_width to value. */
99 diagnostic_set_caret_max_width (diagnostic_context
*context
, int value
)
101 /* One minus to account for the leading empty space. */
102 value
= value
? value
- 1
103 : (isatty (fileno (context
->printer
->buffer
->stream
))
104 ? getenv_columns () - 1: INT_MAX
);
109 context
->caret_max_width
= value
;
112 /* Initialize the diagnostic message outputting machinery. */
114 diagnostic_initialize (diagnostic_context
*context
, int n_opts
)
118 /* Allocate a basic pretty-printer. Clients will replace this a
119 much more elaborated pretty-printer if they wish. */
120 context
->printer
= XNEW (pretty_printer
);
121 pp_construct (context
->printer
, NULL
, 0);
122 /* By default, diagnostics are sent to stderr. */
123 context
->printer
->buffer
->stream
= stderr
;
124 /* By default, we emit prefixes once per message. */
125 context
->printer
->wrapping
.rule
= DIAGNOSTICS_SHOW_PREFIX_ONCE
;
127 memset (context
->diagnostic_count
, 0, sizeof context
->diagnostic_count
);
128 context
->some_warnings_are_errors
= false;
129 context
->warning_as_error_requested
= false;
130 context
->n_opts
= n_opts
;
131 context
->classify_diagnostic
= XNEWVEC (diagnostic_t
, n_opts
);
132 for (i
= 0; i
< n_opts
; i
++)
133 context
->classify_diagnostic
[i
] = DK_UNSPECIFIED
;
134 context
->show_caret
= false;
135 diagnostic_set_caret_max_width (context
, pp_line_cutoff (context
->printer
));
136 context
->show_option_requested
= false;
137 context
->abort_on_error
= false;
138 context
->show_column
= false;
139 context
->pedantic_errors
= false;
140 context
->permissive
= false;
141 context
->opt_permissive
= 0;
142 context
->fatal_errors
= false;
143 context
->dc_inhibit_warnings
= false;
144 context
->dc_warn_system_headers
= false;
145 context
->max_errors
= 0;
146 context
->internal_error
= NULL
;
147 diagnostic_starter (context
) = default_diagnostic_starter
;
148 diagnostic_finalizer (context
) = default_diagnostic_finalizer
;
149 context
->option_enabled
= NULL
;
150 context
->option_state
= NULL
;
151 context
->option_name
= NULL
;
152 context
->last_location
= UNKNOWN_LOCATION
;
153 context
->last_module
= 0;
154 context
->x_data
= NULL
;
156 context
->inhibit_notes_p
= false;
159 /* Do any cleaning up required after the last diagnostic is emitted. */
162 diagnostic_finish (diagnostic_context
*context
)
164 /* Some of the errors may actually have been warnings. */
165 if (context
->some_warnings_are_errors
)
167 /* -Werror was given. */
168 if (context
->warning_as_error_requested
)
169 pp_verbatim (context
->printer
,
170 _("%s: all warnings being treated as errors"),
172 /* At least one -Werror= was given. */
174 pp_verbatim (context
->printer
,
175 _("%s: some warnings being treated as errors"),
177 pp_newline_and_flush (context
->printer
);
181 /* Initialize DIAGNOSTIC, where the message MSG has already been
184 diagnostic_set_info_translated (diagnostic_info
*diagnostic
, const char *msg
,
185 va_list *args
, location_t location
,
188 diagnostic
->message
.err_no
= errno
;
189 diagnostic
->message
.args_ptr
= args
;
190 diagnostic
->message
.format_spec
= msg
;
191 diagnostic
->location
= location
;
192 diagnostic
->override_column
= 0;
193 diagnostic
->kind
= kind
;
194 diagnostic
->option_index
= 0;
197 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
200 diagnostic_set_info (diagnostic_info
*diagnostic
, const char *gmsgid
,
201 va_list *args
, location_t location
,
204 diagnostic_set_info_translated (diagnostic
, _(gmsgid
), args
, location
, kind
);
207 /* Return a malloc'd string describing a location. The caller is
208 responsible for freeing the memory. */
210 diagnostic_build_prefix (diagnostic_context
*context
,
211 const diagnostic_info
*diagnostic
)
213 static const char *const diagnostic_kind_text
[] = {
214 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
215 #include "diagnostic.def"
216 #undef DEFINE_DIAGNOSTIC_KIND
219 const char *text
= _(diagnostic_kind_text
[diagnostic
->kind
]);
220 expanded_location s
= expand_location_to_spelling_point (diagnostic
->location
);
221 if (diagnostic
->override_column
)
222 s
.column
= diagnostic
->override_column
;
223 gcc_assert (diagnostic
->kind
< DK_LAST_DIAGNOSTIC_KIND
);
227 ? build_message_string ("%s: %s", progname
, text
)
228 : context
->show_column
229 ? build_message_string ("%s:%d:%d: %s", s
.file
, s
.line
, s
.column
, text
)
230 : build_message_string ("%s:%d: %s", s
.file
, s
.line
, text
));
233 /* If LINE is longer than MAX_WIDTH, and COLUMN is not smaller than
234 MAX_WIDTH by some margin, then adjust the start of the line such
235 that the COLUMN is smaller than MAX_WIDTH minus the margin. The
236 margin is either 10 characters or the difference between the column
237 and the length of the line, whatever is smaller. */
239 adjust_line (const char *line
, int max_width
, int *column_p
)
241 int right_margin
= 10;
242 int line_width
= strlen (line
);
243 int column
= *column_p
;
245 right_margin
= MIN(line_width
- column
, right_margin
);
246 right_margin
= max_width
- right_margin
;
247 if (line_width
>= max_width
&& column
> right_margin
)
249 line
+= column
- right_margin
;
250 *column_p
= right_margin
;
255 /* Print the physical source line corresponding to the location of
256 this diagnostics, and a caret indicating the precise column. */
258 diagnostic_show_locus (diagnostic_context
* context
,
259 const diagnostic_info
*diagnostic
)
265 const char *saved_prefix
;
268 if (!context
->show_caret
269 || diagnostic
->location
<= BUILTINS_LOCATION
270 || diagnostic
->location
== context
->last_location
)
273 context
->last_location
= diagnostic
->location
;
274 s
= expand_location_to_spelling_point (diagnostic
->location
);
275 line
= location_get_source_line (s
);
279 max_width
= context
->caret_max_width
;
280 line
= adjust_line (line
, max_width
, &(s
.column
));
282 pp_newline (context
->printer
);
283 saved_prefix
= pp_get_prefix (context
->printer
);
284 pp_set_prefix (context
->printer
, NULL
);
285 pp_character (context
->printer
, ' ');
286 while (max_width
> 0 && *line
!= '\0')
288 char c
= *line
== '\t' ? ' ' : *line
;
289 pp_character (context
->printer
, c
);
293 pp_newline (context
->printer
);
294 /* pp_printf does not implement %*c. */
295 buffer
= XALLOCAVEC (char, s
.column
+ 3);
296 snprintf (buffer
, s
.column
+ 3, " %*c", s
.column
, '^');
297 pp_string (context
->printer
, buffer
);
298 pp_set_prefix (context
->printer
, saved_prefix
);
301 /* Functions at which to stop the backtrace print. It's not
302 particularly helpful to print the callers of these functions. */
304 static const char * const bt_stop
[] =
312 /* A callback function passed to the backtrace_full function. */
315 bt_callback (void *data
, uintptr_t pc
, const char *filename
, int lineno
,
316 const char *function
)
318 int *pcount
= (int *) data
;
320 /* If we don't have any useful information, don't print
322 if (filename
== NULL
&& function
== NULL
)
325 /* Skip functions in diagnostic.c. */
328 && strcmp (lbasename(filename
), "diagnostic.c") == 0)
331 /* Print up to 20 functions. We could make this a --param, but
332 since this is only for debugging just use a constant for now. */
335 /* Returning a non-zero value stops the backtrace. */
341 if (function
!= NULL
)
343 char *str
= cplus_demangle_v3 (function
,
344 (DMGL_VERBOSE
| DMGL_ANSI
345 | DMGL_GNU_V3
| DMGL_PARAMS
));
352 for (size_t i
= 0; i
< ARRAY_SIZE (bt_stop
); ++i
)
354 size_t len
= strlen (bt_stop
[i
]);
355 if (strncmp (function
, bt_stop
[i
], len
) == 0
356 && (function
[len
] == '\0' || function
[len
] == '('))
360 /* Returning a non-zero value stops the backtrace. */
366 fprintf (stderr
, "0x%lx %s\n\t%s:%d\n",
368 function
== NULL
? "???" : function
,
369 filename
== NULL
? "???" : filename
,
378 /* A callback function passed to the backtrace_full function. This is
379 called if backtrace_full has an error. */
382 bt_err_callback (void *data ATTRIBUTE_UNUSED
, const char *msg
, int errnum
)
386 /* This means that no debug info was available. Just quietly
387 skip printing backtrace info. */
390 fprintf (stderr
, "%s%s%s\n", msg
, errnum
== 0 ? "" : ": ",
391 errnum
== 0 ? "" : xstrerror (errnum
));
394 /* Take any action which is expected to happen after the diagnostic
395 is written out. This function does not always return. */
397 diagnostic_action_after_output (diagnostic_context
*context
,
398 diagnostic_info
*diagnostic
)
400 switch (diagnostic
->kind
)
410 if (context
->abort_on_error
)
412 if (context
->fatal_errors
)
414 fnotice (stderr
, "compilation terminated due to -Wfatal-errors.\n");
415 diagnostic_finish (context
);
416 exit (FATAL_EXIT_CODE
);
418 if (context
->max_errors
!= 0
419 && ((unsigned) (diagnostic_kind_count (context
, DK_ERROR
)
420 + diagnostic_kind_count (context
, DK_SORRY
))
421 >= context
->max_errors
))
424 "compilation terminated due to -fmax-errors=%u.\n",
425 context
->max_errors
);
426 diagnostic_finish (context
);
427 exit (FATAL_EXIT_CODE
);
433 struct backtrace_state
*state
=
434 backtrace_create_state (NULL
, 0, bt_err_callback
, NULL
);
437 backtrace_full (state
, 2, bt_callback
, bt_err_callback
,
440 if (context
->abort_on_error
)
443 fnotice (stderr
, "Please submit a full bug report,\n"
444 "with preprocessed source if appropriate.\n");
447 ("Please include the complete backtrace "
448 "with any bug report.\n"));
449 fnotice (stderr
, "See %s for instructions.\n", bug_report_url
);
451 exit (ICE_EXIT_CODE
);
455 if (context
->abort_on_error
)
457 diagnostic_finish (context
);
458 fnotice (stderr
, "compilation terminated.\n");
459 exit (FATAL_EXIT_CODE
);
467 diagnostic_report_current_module (diagnostic_context
*context
, location_t where
)
469 const struct line_map
*map
= NULL
;
471 if (pp_needs_newline (context
->printer
))
473 pp_newline (context
->printer
);
474 pp_needs_newline (context
->printer
) = false;
477 if (where
<= BUILTINS_LOCATION
)
480 linemap_resolve_location (line_table
, where
,
481 LRK_MACRO_DEFINITION_LOCATION
,
484 if (map
&& diagnostic_last_module_changed (context
, map
))
486 diagnostic_set_last_module (context
, map
);
487 if (! MAIN_FILE_P (map
))
489 map
= INCLUDED_FROM (line_table
, map
);
490 if (context
->show_column
)
491 pp_verbatim (context
->printer
,
492 "In file included from %s:%d:%d",
494 LAST_SOURCE_LINE (map
), LAST_SOURCE_COLUMN (map
));
496 pp_verbatim (context
->printer
,
497 "In file included from %s:%d",
498 LINEMAP_FILE (map
), LAST_SOURCE_LINE (map
));
499 while (! MAIN_FILE_P (map
))
501 map
= INCLUDED_FROM (line_table
, map
);
502 pp_verbatim (context
->printer
,
504 LINEMAP_FILE (map
), LAST_SOURCE_LINE (map
));
506 pp_verbatim (context
->printer
, ":");
507 pp_newline (context
->printer
);
513 default_diagnostic_starter (diagnostic_context
*context
,
514 diagnostic_info
*diagnostic
)
516 diagnostic_report_current_module (context
, diagnostic
->location
);
517 pp_set_prefix (context
->printer
, diagnostic_build_prefix (context
,
522 default_diagnostic_finalizer (diagnostic_context
*context ATTRIBUTE_UNUSED
,
523 diagnostic_info
*diagnostic ATTRIBUTE_UNUSED
)
527 /* Interface to specify diagnostic kind overrides. Returns the
528 previous setting, or DK_UNSPECIFIED if the parameters are out of
531 diagnostic_classify_diagnostic (diagnostic_context
*context
,
533 diagnostic_t new_kind
,
536 diagnostic_t old_kind
;
538 if (option_index
<= 0
539 || option_index
>= context
->n_opts
540 || new_kind
>= DK_LAST_DIAGNOSTIC_KIND
)
541 return DK_UNSPECIFIED
;
543 old_kind
= context
->classify_diagnostic
[option_index
];
545 /* Handle pragmas separately, since we need to keep track of *where*
547 if (where
!= UNKNOWN_LOCATION
)
551 for (i
= context
->n_classification_history
- 1; i
>= 0; i
--)
552 if (context
->classification_history
[i
].option
== option_index
)
554 old_kind
= context
->classification_history
[i
].kind
;
558 i
= context
->n_classification_history
;
559 context
->classification_history
=
560 (diagnostic_classification_change_t
*) xrealloc (context
->classification_history
, (i
+ 1)
561 * sizeof (diagnostic_classification_change_t
));
562 context
->classification_history
[i
].location
= where
;
563 context
->classification_history
[i
].option
= option_index
;
564 context
->classification_history
[i
].kind
= new_kind
;
565 context
->n_classification_history
++;
568 context
->classify_diagnostic
[option_index
] = new_kind
;
573 /* Save all diagnostic classifications in a stack. */
575 diagnostic_push_diagnostics (diagnostic_context
*context
, location_t where ATTRIBUTE_UNUSED
)
577 context
->push_list
= (int *) xrealloc (context
->push_list
, (context
->n_push
+ 1) * sizeof (int));
578 context
->push_list
[context
->n_push
++] = context
->n_classification_history
;
581 /* Restore the topmost classification set off the stack. If the stack
582 is empty, revert to the state based on command line parameters. */
584 diagnostic_pop_diagnostics (diagnostic_context
*context
, location_t where
)
590 jump_to
= context
->push_list
[-- context
->n_push
];
594 i
= context
->n_classification_history
;
595 context
->classification_history
=
596 (diagnostic_classification_change_t
*) xrealloc (context
->classification_history
, (i
+ 1)
597 * sizeof (diagnostic_classification_change_t
));
598 context
->classification_history
[i
].location
= where
;
599 context
->classification_history
[i
].option
= jump_to
;
600 context
->classification_history
[i
].kind
= DK_POP
;
601 context
->n_classification_history
++;
604 /* Report a diagnostic message (an error or a warning) as specified by
605 DC. This function is *the* subroutine in terms of which front-ends
606 should implement their specific diagnostic handling modules. The
607 front-end independent format specifiers are exactly those described
608 in the documentation of output_format.
609 Return true if a diagnostic was printed, false otherwise. */
612 diagnostic_report_diagnostic (diagnostic_context
*context
,
613 diagnostic_info
*diagnostic
)
615 location_t location
= diagnostic
->location
;
616 diagnostic_t orig_diag_kind
= diagnostic
->kind
;
617 const char *saved_format_spec
;
619 /* Give preference to being able to inhibit warnings, before they
620 get reclassified to something else. */
621 if ((diagnostic
->kind
== DK_WARNING
|| diagnostic
->kind
== DK_PEDWARN
)
622 && !diagnostic_report_warnings_p (context
, location
))
625 if (diagnostic
->kind
== DK_PEDWARN
)
627 diagnostic
->kind
= pedantic_warning_kind (context
);
628 /* We do this to avoid giving the message for -pedantic-errors. */
629 orig_diag_kind
= diagnostic
->kind
;
632 if (diagnostic
->kind
== DK_NOTE
&& context
->inhibit_notes_p
)
635 if (context
->lock
> 0)
637 /* If we're reporting an ICE in the middle of some other error,
638 try to flush out the previous error, then let this one
639 through. Don't do this more than once. */
640 if (diagnostic
->kind
== DK_ICE
&& context
->lock
== 1)
641 pp_newline_and_flush (context
->printer
);
643 error_recursion (context
);
646 /* If the user requested that warnings be treated as errors, so be
647 it. Note that we do this before the next block so that
648 individual warnings can be overridden back to warnings with
650 if (context
->warning_as_error_requested
651 && diagnostic
->kind
== DK_WARNING
)
653 diagnostic
->kind
= DK_ERROR
;
656 if (diagnostic
->option_index
657 && diagnostic
->option_index
!= permissive_error_option (context
))
659 diagnostic_t diag_class
= DK_UNSPECIFIED
;
661 /* This tests if the user provided the appropriate -Wfoo or
663 if (! context
->option_enabled (diagnostic
->option_index
,
664 context
->option_state
))
667 /* This tests for #pragma diagnostic changes. */
668 if (context
->n_classification_history
> 0)
671 /* FIXME: Stupid search. Optimize later. */
672 for (i
= context
->n_classification_history
- 1; i
>= 0; i
--)
674 if (linemap_location_before_p
676 context
->classification_history
[i
].location
,
679 if (context
->classification_history
[i
].kind
== (int) DK_POP
)
681 i
= context
->classification_history
[i
].option
;
684 if (context
->classification_history
[i
].option
== diagnostic
->option_index
)
686 diag_class
= context
->classification_history
[i
].kind
;
687 if (diag_class
!= DK_UNSPECIFIED
)
688 diagnostic
->kind
= diag_class
;
694 /* This tests if the user provided the appropriate -Werror=foo
696 if (diag_class
== DK_UNSPECIFIED
697 && context
->classify_diagnostic
[diagnostic
->option_index
] != DK_UNSPECIFIED
)
699 diagnostic
->kind
= context
->classify_diagnostic
[diagnostic
->option_index
];
701 /* This allows for future extensions, like temporarily disabling
702 warnings for ranges of source code. */
703 if (diagnostic
->kind
== DK_IGNORED
)
707 if (orig_diag_kind
== DK_WARNING
&& diagnostic
->kind
== DK_ERROR
)
708 context
->some_warnings_are_errors
= true;
712 if (diagnostic
->kind
== DK_ICE
)
714 #ifndef ENABLE_CHECKING
715 /* When not checking, ICEs are converted to fatal errors when an
716 error has already occurred. This is counteracted by
718 if ((diagnostic_kind_count (context
, DK_ERROR
) > 0
719 || diagnostic_kind_count (context
, DK_SORRY
) > 0)
720 && !context
->abort_on_error
)
722 expanded_location s
= expand_location (diagnostic
->location
);
723 fnotice (stderr
, "%s:%d: confused by earlier errors, bailing out\n",
725 exit (ICE_EXIT_CODE
);
728 if (context
->internal_error
)
729 (*context
->internal_error
) (context
,
730 diagnostic
->message
.format_spec
,
731 diagnostic
->message
.args_ptr
);
733 ++diagnostic_kind_count (context
, diagnostic
->kind
);
735 saved_format_spec
= diagnostic
->message
.format_spec
;
736 if (context
->show_option_requested
)
740 option_text
= context
->option_name (context
, diagnostic
->option_index
,
741 orig_diag_kind
, diagnostic
->kind
);
745 diagnostic
->message
.format_spec
746 = ACONCAT ((diagnostic
->message
.format_spec
,
748 "[", option_text
, "]",
753 diagnostic
->message
.locus
= &diagnostic
->location
;
754 diagnostic
->message
.x_data
= &diagnostic
->x_data
;
755 diagnostic
->x_data
= NULL
;
756 pp_format (context
->printer
, &diagnostic
->message
);
757 (*diagnostic_starter (context
)) (context
, diagnostic
);
758 pp_output_formatted_text (context
->printer
);
759 diagnostic_show_locus (context
, diagnostic
);
760 (*diagnostic_finalizer (context
)) (context
, diagnostic
);
761 pp_destroy_prefix (context
->printer
);
762 pp_newline_and_flush (context
->printer
);
763 diagnostic_action_after_output (context
, diagnostic
);
764 diagnostic
->message
.format_spec
= saved_format_spec
;
765 diagnostic
->x_data
= NULL
;
772 /* Given a partial pathname as input, return another pathname that
773 shares no directory elements with the pathname of __FILE__. This
774 is used by fancy_abort() to print `Internal compiler error in expr.c'
775 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
778 trim_filename (const char *name
)
780 static const char this_file
[] = __FILE__
;
781 const char *p
= name
, *q
= this_file
;
783 /* First skip any "../" in each filename. This allows us to give a proper
784 reference to a file in a subdirectory. */
785 while (p
[0] == '.' && p
[1] == '.' && IS_DIR_SEPARATOR (p
[2]))
788 while (q
[0] == '.' && q
[1] == '.' && IS_DIR_SEPARATOR (q
[2]))
791 /* Now skip any parts the two filenames have in common. */
792 while (*p
== *q
&& *p
!= 0 && *q
!= 0)
795 /* Now go backwards until the previous directory separator. */
796 while (p
> name
&& !IS_DIR_SEPARATOR (p
[-1]))
802 /* Standard error reporting routines in increasing order of severity.
803 All of these take arguments like printf. */
805 /* Text to be emitted verbatim to the error message stream; this
806 produces no prefix and disables line-wrapping. Use rarely. */
808 verbatim (const char *gmsgid
, ...)
813 va_start (ap
, gmsgid
);
816 text
.format_spec
= _(gmsgid
);
819 pp_format_verbatim (global_dc
->printer
, &text
);
820 pp_newline_and_flush (global_dc
->printer
);
824 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
826 diagnostic_append_note (diagnostic_context
*context
,
828 const char * gmsgid
, ...)
830 diagnostic_info diagnostic
;
833 va_start (ap
, gmsgid
);
834 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
, DK_NOTE
);
835 if (context
->inhibit_notes_p
)
840 pp_set_prefix (context
->printer
,
841 diagnostic_build_prefix (context
, &diagnostic
));
842 pp_newline (context
->printer
);
843 pp_format (context
->printer
, &diagnostic
.message
);
844 pp_output_formatted_text (context
->printer
);
845 pp_destroy_prefix (context
->printer
);
846 diagnostic_show_locus (context
, &diagnostic
);
851 emit_diagnostic (diagnostic_t kind
, location_t location
, int opt
,
852 const char *gmsgid
, ...)
854 diagnostic_info diagnostic
;
858 va_start (ap
, gmsgid
);
859 if (kind
== DK_PERMERROR
)
861 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
,
862 permissive_error_kind (global_dc
));
863 diagnostic
.option_index
= permissive_error_option (global_dc
);
866 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
, kind
);
867 if (kind
== DK_WARNING
|| kind
== DK_PEDWARN
)
868 diagnostic
.option_index
= opt
;
871 ret
= report_diagnostic (&diagnostic
);
876 /* An informative note at LOCATION. Use this for additional details on an error
879 inform (location_t location
, const char *gmsgid
, ...)
881 diagnostic_info diagnostic
;
884 va_start (ap
, gmsgid
);
885 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
, DK_NOTE
);
886 report_diagnostic (&diagnostic
);
890 /* An informative note at LOCATION. Use this for additional details on an
893 inform_n (location_t location
, int n
, const char *singular_gmsgid
,
894 const char *plural_gmsgid
, ...)
896 diagnostic_info diagnostic
;
899 va_start (ap
, plural_gmsgid
);
900 diagnostic_set_info_translated (&diagnostic
,
901 ngettext (singular_gmsgid
, plural_gmsgid
, n
),
902 &ap
, location
, DK_NOTE
);
903 report_diagnostic (&diagnostic
);
907 /* A warning at INPUT_LOCATION. Use this for code which is correct according
908 to the relevant language specification but is likely to be buggy anyway.
909 Returns true if the warning was printed, false if it was inhibited. */
911 warning (int opt
, const char *gmsgid
, ...)
913 diagnostic_info diagnostic
;
917 va_start (ap
, gmsgid
);
918 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_WARNING
);
919 diagnostic
.option_index
= opt
;
921 ret
= report_diagnostic (&diagnostic
);
926 /* A warning at LOCATION. Use this for code which is correct according to the
927 relevant language specification but is likely to be buggy anyway.
928 Returns true if the warning was printed, false if it was inhibited. */
931 warning_at (location_t location
, int opt
, const char *gmsgid
, ...)
933 diagnostic_info diagnostic
;
937 va_start (ap
, gmsgid
);
938 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
, DK_WARNING
);
939 diagnostic
.option_index
= opt
;
940 ret
= report_diagnostic (&diagnostic
);
945 /* A "pedantic" warning at LOCATION: issues a warning unless
946 -pedantic-errors was given on the command line, in which case it
947 issues an error. Use this for diagnostics required by the relevant
948 language standard, if you have chosen not to make them errors.
950 Note that these diagnostics are issued independent of the setting
951 of the -Wpedantic command-line switch. To get a warning enabled
952 only with that switch, use either "if (pedantic) pedwarn
953 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
954 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
956 Returns true if the warning was printed, false if it was inhibited. */
959 pedwarn (location_t location
, int opt
, const char *gmsgid
, ...)
961 diagnostic_info diagnostic
;
965 va_start (ap
, gmsgid
);
966 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
, DK_PEDWARN
);
967 diagnostic
.option_index
= opt
;
968 ret
= report_diagnostic (&diagnostic
);
973 /* A "permissive" error at LOCATION: issues an error unless
974 -fpermissive was given on the command line, in which case it issues
975 a warning. Use this for things that really should be errors but we
976 want to support legacy code.
978 Returns true if the warning was printed, false if it was inhibited. */
981 permerror (location_t location
, const char *gmsgid
, ...)
983 diagnostic_info diagnostic
;
987 va_start (ap
, gmsgid
);
988 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
,
989 permissive_error_kind (global_dc
));
990 diagnostic
.option_index
= permissive_error_option (global_dc
);
991 ret
= report_diagnostic (&diagnostic
);
996 /* A hard error: the code is definitely ill-formed, and an object file
997 will not be produced. */
999 error (const char *gmsgid
, ...)
1001 diagnostic_info diagnostic
;
1004 va_start (ap
, gmsgid
);
1005 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_ERROR
);
1006 report_diagnostic (&diagnostic
);
1010 /* A hard error: the code is definitely ill-formed, and an object file
1011 will not be produced. */
1013 error_n (location_t location
, int n
, const char *singular_gmsgid
,
1014 const char *plural_gmsgid
, ...)
1016 diagnostic_info diagnostic
;
1019 va_start (ap
, plural_gmsgid
);
1020 diagnostic_set_info_translated (&diagnostic
,
1021 ngettext (singular_gmsgid
, plural_gmsgid
, n
),
1022 &ap
, location
, DK_ERROR
);
1023 report_diagnostic (&diagnostic
);
1027 /* Same as ebove, but use location LOC instead of input_location. */
1029 error_at (location_t loc
, const char *gmsgid
, ...)
1031 diagnostic_info diagnostic
;
1034 va_start (ap
, gmsgid
);
1035 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, loc
, DK_ERROR
);
1036 report_diagnostic (&diagnostic
);
1040 /* "Sorry, not implemented." Use for a language feature which is
1041 required by the relevant specification but not implemented by GCC.
1042 An object file will not be produced. */
1044 sorry (const char *gmsgid
, ...)
1046 diagnostic_info diagnostic
;
1049 va_start (ap
, gmsgid
);
1050 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_SORRY
);
1051 report_diagnostic (&diagnostic
);
1055 /* Return true if an error or a "sorry" has been seen. Various
1056 processing is disabled after errors. */
1060 return errorcount
|| sorrycount
;
1063 /* An error which is severe enough that we make no attempt to
1064 continue. Do not use this for internal consistency checks; that's
1065 internal_error. Use of this function should be rare. */
1067 fatal_error (const char *gmsgid
, ...)
1069 diagnostic_info diagnostic
;
1072 va_start (ap
, gmsgid
);
1073 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_FATAL
);
1074 report_diagnostic (&diagnostic
);
1080 /* An internal consistency check has failed. We make no attempt to
1081 continue. Note that unless there is debugging value to be had from
1082 a more specific message, or some other good reason, you should use
1083 abort () instead of calling this function directly. */
1085 internal_error (const char *gmsgid
, ...)
1087 diagnostic_info diagnostic
;
1090 va_start (ap
, gmsgid
);
1091 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_ICE
);
1092 report_diagnostic (&diagnostic
);
1098 /* Special case error functions. Most are implemented in terms of the
1099 above, or should be. */
1101 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1102 runs its second argument through gettext. */
1104 fnotice (FILE *file
, const char *cmsgid
, ...)
1108 va_start (ap
, cmsgid
);
1109 vfprintf (file
, _(cmsgid
), ap
);
1113 /* Inform the user that an error occurred while trying to report some
1114 other error. This indicates catastrophic internal inconsistencies,
1115 so give up now. But do try to flush out the previous error.
1116 This mustn't use internal_error, that will cause infinite recursion. */
1119 error_recursion (diagnostic_context
*context
)
1121 diagnostic_info diagnostic
;
1123 if (context
->lock
< 3)
1124 pp_newline_and_flush (context
->printer
);
1127 "Internal compiler error: Error reporting routines re-entered.\n");
1129 /* Call diagnostic_action_after_output to get the "please submit a bug
1130 report" message. It only looks at the kind field of diagnostic_info. */
1131 diagnostic
.kind
= DK_ICE
;
1132 diagnostic_action_after_output (context
, &diagnostic
);
1134 /* Do not use gcc_unreachable here; that goes through internal_error
1135 and therefore would cause infinite recursion. */
1139 /* Report an internal compiler error in a friendly manner. This is
1140 the function that gets called upon use of abort() in the source
1141 code generally, thanks to a special macro. */
1144 fancy_abort (const char *file
, int line
, const char *function
)
1146 internal_error ("in %s, at %s:%d", function
, trim_filename (file
), line
);
1149 /* Really call the system 'abort'. This has to go right at the end of
1150 this file, so that there are no functions after it that call abort
1151 and get the system abort instead of our macro. */