1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999-2014 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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file implements the language independent aspect of diagnostic
27 #include "coretypes.h"
32 #include "backtrace.h"
33 #include "diagnostic.h"
34 #include "diagnostic-color.h"
40 #ifdef GWINSZ_IN_SYS_IOCTL
41 # include <sys/ioctl.h>
44 #include <new> // For placement new.
46 #define pedantic_warning_kind(DC) \
47 ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
48 #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR)
49 #define permissive_error_option(DC) ((DC)->opt_permissive)
52 static void error_recursion (diagnostic_context
*) ATTRIBUTE_NORETURN
;
54 static void diagnostic_action_after_output (diagnostic_context
*,
56 static void real_abort (void) ATTRIBUTE_NORETURN
;
58 /* Name of program invoked, sans directories. */
62 /* A diagnostic_context surrogate for stderr. */
63 static diagnostic_context global_diagnostic_context
;
64 diagnostic_context
*global_dc
= &global_diagnostic_context
;
66 /* Return a malloc'd string containing MSG formatted a la printf. The
67 caller is responsible for freeing the memory. */
69 build_message_string (const char *msg
, ...)
75 vasprintf (&str
, msg
, ap
);
81 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
83 file_name_as_prefix (diagnostic_context
*context
, const char *f
)
86 = colorize_start (pp_show_color (context
->printer
), "locus");
87 const char *locus_ce
= colorize_stop (pp_show_color (context
->printer
));
88 return build_message_string ("%s%s:%s ", locus_cs
, f
, locus_ce
);
93 /* Return the value of the getenv("COLUMNS") as an integer. If the
94 value is not set to a positive integer, use ioctl to get the
95 terminal width. If it fails, return INT_MAX. */
97 get_terminal_width (void)
99 const char * s
= getenv ("COLUMNS");
109 if (ioctl (0, TIOCGWINSZ
, &w
) == 0 && w
.ws_col
> 0)
116 /* Set caret_max_width to value. */
118 diagnostic_set_caret_max_width (diagnostic_context
*context
, int value
)
120 /* One minus to account for the leading empty space. */
121 value
= value
? value
- 1
122 : (isatty (fileno (pp_buffer (context
->printer
)->stream
))
123 ? get_terminal_width () - 1: INT_MAX
);
128 context
->caret_max_width
= value
;
131 /* Initialize the diagnostic message outputting machinery. */
133 diagnostic_initialize (diagnostic_context
*context
, int n_opts
)
137 /* Allocate a basic pretty-printer. Clients will replace this a
138 much more elaborated pretty-printer if they wish. */
139 context
->printer
= XNEW (pretty_printer
);
140 new (context
->printer
) pretty_printer ();
142 memset (context
->diagnostic_count
, 0, sizeof context
->diagnostic_count
);
143 context
->some_warnings_are_errors
= false;
144 context
->warning_as_error_requested
= false;
145 context
->n_opts
= n_opts
;
146 context
->classify_diagnostic
= XNEWVEC (diagnostic_t
, n_opts
);
147 for (i
= 0; i
< n_opts
; i
++)
148 context
->classify_diagnostic
[i
] = DK_UNSPECIFIED
;
149 context
->show_caret
= false;
150 diagnostic_set_caret_max_width (context
, pp_line_cutoff (context
->printer
));
151 context
->caret_char
= '^';
152 context
->show_option_requested
= false;
153 context
->abort_on_error
= false;
154 context
->show_column
= false;
155 context
->pedantic_errors
= false;
156 context
->permissive
= false;
157 context
->opt_permissive
= 0;
158 context
->fatal_errors
= false;
159 context
->dc_inhibit_warnings
= false;
160 context
->dc_warn_system_headers
= false;
161 context
->max_errors
= 0;
162 context
->internal_error
= NULL
;
163 diagnostic_starter (context
) = default_diagnostic_starter
;
164 diagnostic_finalizer (context
) = default_diagnostic_finalizer
;
165 context
->option_enabled
= NULL
;
166 context
->option_state
= NULL
;
167 context
->option_name
= NULL
;
168 context
->last_location
= UNKNOWN_LOCATION
;
169 context
->last_module
= 0;
170 context
->x_data
= NULL
;
172 context
->inhibit_notes_p
= false;
175 /* Maybe initialize the color support. We require clients to do this
176 explicitly, since most clients don't want color. When called
177 without a VALUE, it initializes with DIAGNOSTICS_COLOR_DEFAULT. */
180 diagnostic_color_init (diagnostic_context
*context
, int value
/*= -1 */)
182 /* value == -1 is the default value. */
185 /* If DIAGNOSTICS_COLOR_DEFAULT is -1, default to
186 -fdiagnostics-color=auto if GCC_COLORS is in the environment,
187 otherwise default to -fdiagnostics-color=never, for other
188 values default to that
189 -fdiagnostics-color={never,auto,always}. */
190 if (DIAGNOSTICS_COLOR_DEFAULT
== -1)
192 if (!getenv ("GCC_COLORS"))
194 value
= DIAGNOSTICS_COLOR_AUTO
;
197 value
= DIAGNOSTICS_COLOR_DEFAULT
;
199 pp_show_color (context
->printer
)
200 = colorize_init ((diagnostic_color_rule_t
) value
);
203 /* Do any cleaning up required after the last diagnostic is emitted. */
206 diagnostic_finish (diagnostic_context
*context
)
208 /* Some of the errors may actually have been warnings. */
209 if (context
->some_warnings_are_errors
)
211 /* -Werror was given. */
212 if (context
->warning_as_error_requested
)
213 pp_verbatim (context
->printer
,
214 _("%s: all warnings being treated as errors"),
216 /* At least one -Werror= was given. */
218 pp_verbatim (context
->printer
,
219 _("%s: some warnings being treated as errors"),
221 pp_newline_and_flush (context
->printer
);
224 diagnostic_file_cache_fini ();
226 XDELETEVEC (context
->classify_diagnostic
);
227 context
->classify_diagnostic
= NULL
;
229 /* diagnostic_initialize allocates context->printer using XNEW
230 and placement-new. */
231 context
->printer
->~pretty_printer ();
232 XDELETE (context
->printer
);
233 context
->printer
= NULL
;
236 /* Initialize DIAGNOSTIC, where the message MSG has already been
239 diagnostic_set_info_translated (diagnostic_info
*diagnostic
, const char *msg
,
240 va_list *args
, location_t location
,
243 diagnostic
->message
.err_no
= errno
;
244 diagnostic
->message
.args_ptr
= args
;
245 diagnostic
->message
.format_spec
= msg
;
246 diagnostic
->location
= location
;
247 diagnostic
->override_column
= 0;
248 diagnostic
->kind
= kind
;
249 diagnostic
->option_index
= 0;
252 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
255 diagnostic_set_info (diagnostic_info
*diagnostic
, const char *gmsgid
,
256 va_list *args
, location_t location
,
259 diagnostic_set_info_translated (diagnostic
, _(gmsgid
), args
, location
, kind
);
262 /* Return a malloc'd string describing a location. The caller is
263 responsible for freeing the memory. */
265 diagnostic_build_prefix (diagnostic_context
*context
,
266 const diagnostic_info
*diagnostic
)
268 static const char *const diagnostic_kind_text
[] = {
269 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
270 #include "diagnostic.def"
271 #undef DEFINE_DIAGNOSTIC_KIND
274 static const char *const diagnostic_kind_color
[] = {
275 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
276 #include "diagnostic.def"
277 #undef DEFINE_DIAGNOSTIC_KIND
280 gcc_assert (diagnostic
->kind
< DK_LAST_DIAGNOSTIC_KIND
);
282 const char *text
= _(diagnostic_kind_text
[diagnostic
->kind
]);
283 const char *text_cs
= "", *text_ce
= "";
284 const char *locus_cs
, *locus_ce
;
285 pretty_printer
*pp
= context
->printer
;
287 if (diagnostic_kind_color
[diagnostic
->kind
])
289 text_cs
= colorize_start (pp_show_color (pp
),
290 diagnostic_kind_color
[diagnostic
->kind
]);
291 text_ce
= colorize_stop (pp_show_color (pp
));
293 locus_cs
= colorize_start (pp_show_color (pp
), "locus");
294 locus_ce
= colorize_stop (pp_show_color (pp
));
296 expanded_location s
= diagnostic_expand_location (diagnostic
);
299 ? build_message_string ("%s%s:%s %s%s%s", locus_cs
, progname
, locus_ce
,
300 text_cs
, text
, text_ce
)
301 : !strcmp (s
.file
, N_("<built-in>"))
302 ? build_message_string ("%s%s:%s %s%s%s", locus_cs
, s
.file
, locus_ce
,
303 text_cs
, text
, text_ce
)
304 : context
->show_column
305 ? build_message_string ("%s%s:%d:%d:%s %s%s%s", locus_cs
, s
.file
, s
.line
,
306 s
.column
, locus_ce
, text_cs
, text
, text_ce
)
307 : build_message_string ("%s%s:%d:%s %s%s%s", locus_cs
, s
.file
, s
.line
,
308 locus_ce
, text_cs
, text
, text_ce
));
311 /* If LINE is longer than MAX_WIDTH, and COLUMN is not smaller than
312 MAX_WIDTH by some margin, then adjust the start of the line such
313 that the COLUMN is smaller than MAX_WIDTH minus the margin. The
314 margin is either 10 characters or the difference between the column
315 and the length of the line, whatever is smaller. The length of
316 LINE is given by LINE_WIDTH. */
318 adjust_line (const char *line
, int line_width
,
319 int max_width
, int *column_p
)
321 int right_margin
= 10;
322 int column
= *column_p
;
324 gcc_checking_assert (line_width
>= column
);
325 right_margin
= MIN (line_width
- column
, right_margin
);
326 right_margin
= max_width
- right_margin
;
327 if (line_width
>= max_width
&& column
> right_margin
)
329 line
+= column
- right_margin
;
330 *column_p
= right_margin
;
335 /* Print the physical source line corresponding to the location of
336 this diagnostic, and a caret indicating the precise column. */
338 diagnostic_show_locus (diagnostic_context
* context
,
339 const diagnostic_info
*diagnostic
)
346 const char *saved_prefix
;
347 const char *caret_cs
, *caret_ce
;
349 if (!context
->show_caret
350 || diagnostic
->location
<= BUILTINS_LOCATION
351 || diagnostic
->location
== context
->last_location
)
354 context
->last_location
= diagnostic
->location
;
355 s
= diagnostic_expand_location (diagnostic
);
356 line
= location_get_source_line (s
, &line_width
);
357 if (line
== NULL
|| s
.column
> line_width
)
360 max_width
= context
->caret_max_width
;
361 line
= adjust_line (line
, line_width
, max_width
, &(s
.column
));
363 pp_newline (context
->printer
);
364 saved_prefix
= pp_get_prefix (context
->printer
);
365 pp_set_prefix (context
->printer
, NULL
);
366 pp_space (context
->printer
);
367 while (max_width
> 0 && line_width
> 0)
369 char c
= *line
== '\t' ? ' ' : *line
;
372 pp_character (context
->printer
, c
);
377 pp_newline (context
->printer
);
378 caret_cs
= colorize_start (pp_show_color (context
->printer
), "caret");
379 caret_ce
= colorize_stop (pp_show_color (context
->printer
));
381 /* pp_printf does not implement %*c. */
382 size_t len
= s
.column
+ 3 + strlen (caret_cs
) + strlen (caret_ce
);
383 buffer
= XALLOCAVEC (char, len
);
384 snprintf (buffer
, len
, "%s %*c%s", caret_cs
, s
.column
, context
->caret_char
,
386 pp_string (context
->printer
, buffer
);
387 pp_set_prefix (context
->printer
, saved_prefix
);
388 pp_needs_newline (context
->printer
) = true;
391 /* Functions at which to stop the backtrace print. It's not
392 particularly helpful to print the callers of these functions. */
394 static const char * const bt_stop
[] =
402 /* A callback function passed to the backtrace_full function. */
405 bt_callback (void *data
, uintptr_t pc
, const char *filename
, int lineno
,
406 const char *function
)
408 int *pcount
= (int *) data
;
410 /* If we don't have any useful information, don't print
412 if (filename
== NULL
&& function
== NULL
)
415 /* Skip functions in diagnostic.c. */
418 && strcmp (lbasename (filename
), "diagnostic.c") == 0)
421 /* Print up to 20 functions. We could make this a --param, but
422 since this is only for debugging just use a constant for now. */
425 /* Returning a non-zero value stops the backtrace. */
431 if (function
!= NULL
)
433 char *str
= cplus_demangle_v3 (function
,
434 (DMGL_VERBOSE
| DMGL_ANSI
435 | DMGL_GNU_V3
| DMGL_PARAMS
));
442 for (size_t i
= 0; i
< ARRAY_SIZE (bt_stop
); ++i
)
444 size_t len
= strlen (bt_stop
[i
]);
445 if (strncmp (function
, bt_stop
[i
], len
) == 0
446 && (function
[len
] == '\0' || function
[len
] == '('))
450 /* Returning a non-zero value stops the backtrace. */
456 fprintf (stderr
, "0x%lx %s\n\t%s:%d\n",
458 function
== NULL
? "???" : function
,
459 filename
== NULL
? "???" : filename
,
468 /* A callback function passed to the backtrace_full function. This is
469 called if backtrace_full has an error. */
472 bt_err_callback (void *data ATTRIBUTE_UNUSED
, const char *msg
, int errnum
)
476 /* This means that no debug info was available. Just quietly
477 skip printing backtrace info. */
480 fprintf (stderr
, "%s%s%s\n", msg
, errnum
== 0 ? "" : ": ",
481 errnum
== 0 ? "" : xstrerror (errnum
));
484 /* Take any action which is expected to happen after the diagnostic
485 is written out. This function does not always return. */
487 diagnostic_action_after_output (diagnostic_context
*context
,
488 diagnostic_info
*diagnostic
)
490 switch (diagnostic
->kind
)
500 if (context
->abort_on_error
)
502 if (context
->fatal_errors
)
504 fnotice (stderr
, "compilation terminated due to -Wfatal-errors.\n");
505 diagnostic_finish (context
);
506 exit (FATAL_EXIT_CODE
);
508 if (context
->max_errors
!= 0
509 && ((unsigned) (diagnostic_kind_count (context
, DK_ERROR
)
510 + diagnostic_kind_count (context
, DK_SORRY
))
511 >= context
->max_errors
))
514 "compilation terminated due to -fmax-errors=%u.\n",
515 context
->max_errors
);
516 diagnostic_finish (context
);
517 exit (FATAL_EXIT_CODE
);
523 struct backtrace_state
*state
=
524 backtrace_create_state (NULL
, 0, bt_err_callback
, NULL
);
527 backtrace_full (state
, 2, bt_callback
, bt_err_callback
,
530 if (context
->abort_on_error
)
533 fnotice (stderr
, "Please submit a full bug report,\n"
534 "with preprocessed source if appropriate.\n");
537 ("Please include the complete backtrace "
538 "with any bug report.\n"));
539 fnotice (stderr
, "See %s for instructions.\n", bug_report_url
);
541 exit (ICE_EXIT_CODE
);
545 if (context
->abort_on_error
)
547 diagnostic_finish (context
);
548 fnotice (stderr
, "compilation terminated.\n");
549 exit (FATAL_EXIT_CODE
);
557 diagnostic_report_current_module (diagnostic_context
*context
, location_t where
)
559 const struct line_map
*map
= NULL
;
561 if (pp_needs_newline (context
->printer
))
563 pp_newline (context
->printer
);
564 pp_needs_newline (context
->printer
) = false;
567 if (where
<= BUILTINS_LOCATION
)
570 linemap_resolve_location (line_table
, where
,
571 LRK_MACRO_DEFINITION_LOCATION
,
574 if (map
&& diagnostic_last_module_changed (context
, map
))
576 diagnostic_set_last_module (context
, map
);
577 if (! MAIN_FILE_P (map
))
579 map
= INCLUDED_FROM (line_table
, map
);
580 if (context
->show_column
)
581 pp_verbatim (context
->printer
,
582 "In file included from %r%s:%d:%d%R", "locus",
584 LAST_SOURCE_LINE (map
), LAST_SOURCE_COLUMN (map
));
586 pp_verbatim (context
->printer
,
587 "In file included from %r%s:%d%R", "locus",
588 LINEMAP_FILE (map
), LAST_SOURCE_LINE (map
));
589 while (! MAIN_FILE_P (map
))
591 map
= INCLUDED_FROM (line_table
, map
);
592 pp_verbatim (context
->printer
,
593 ",\n from %r%s:%d%R", "locus",
594 LINEMAP_FILE (map
), LAST_SOURCE_LINE (map
));
596 pp_verbatim (context
->printer
, ":");
597 pp_newline (context
->printer
);
603 default_diagnostic_starter (diagnostic_context
*context
,
604 diagnostic_info
*diagnostic
)
606 diagnostic_report_current_module (context
, diagnostic
->location
);
607 pp_set_prefix (context
->printer
, diagnostic_build_prefix (context
,
612 default_diagnostic_finalizer (diagnostic_context
*context
,
613 diagnostic_info
*diagnostic
)
615 diagnostic_show_locus (context
, diagnostic
);
616 pp_destroy_prefix (context
->printer
);
617 pp_newline_and_flush (context
->printer
);
620 /* Interface to specify diagnostic kind overrides. Returns the
621 previous setting, or DK_UNSPECIFIED if the parameters are out of
622 range. If OPTION_INDEX is zero, the new setting is for all the
625 diagnostic_classify_diagnostic (diagnostic_context
*context
,
627 diagnostic_t new_kind
,
630 diagnostic_t old_kind
;
633 || option_index
>= context
->n_opts
634 || new_kind
>= DK_LAST_DIAGNOSTIC_KIND
)
635 return DK_UNSPECIFIED
;
637 old_kind
= context
->classify_diagnostic
[option_index
];
639 /* Handle pragmas separately, since we need to keep track of *where*
641 if (where
!= UNKNOWN_LOCATION
)
645 /* Record the command-line status, so we can reset it back on DK_POP. */
646 if (old_kind
== DK_UNSPECIFIED
)
648 old_kind
= context
->option_enabled (option_index
,
649 context
->option_state
)
650 ? DK_WARNING
: DK_IGNORED
;
651 context
->classify_diagnostic
[option_index
] = old_kind
;
654 for (i
= context
->n_classification_history
- 1; i
>= 0; i
--)
655 if (context
->classification_history
[i
].option
== option_index
)
657 old_kind
= context
->classification_history
[i
].kind
;
661 i
= context
->n_classification_history
;
662 context
->classification_history
=
663 (diagnostic_classification_change_t
*) xrealloc (context
->classification_history
, (i
+ 1)
664 * sizeof (diagnostic_classification_change_t
));
665 context
->classification_history
[i
].location
= where
;
666 context
->classification_history
[i
].option
= option_index
;
667 context
->classification_history
[i
].kind
= new_kind
;
668 context
->n_classification_history
++;
671 context
->classify_diagnostic
[option_index
] = new_kind
;
676 /* Save all diagnostic classifications in a stack. */
678 diagnostic_push_diagnostics (diagnostic_context
*context
, location_t where ATTRIBUTE_UNUSED
)
680 context
->push_list
= (int *) xrealloc (context
->push_list
, (context
->n_push
+ 1) * sizeof (int));
681 context
->push_list
[context
->n_push
++] = context
->n_classification_history
;
684 /* Restore the topmost classification set off the stack. If the stack
685 is empty, revert to the state based on command line parameters. */
687 diagnostic_pop_diagnostics (diagnostic_context
*context
, location_t where
)
693 jump_to
= context
->push_list
[-- context
->n_push
];
697 i
= context
->n_classification_history
;
698 context
->classification_history
=
699 (diagnostic_classification_change_t
*) xrealloc (context
->classification_history
, (i
+ 1)
700 * sizeof (diagnostic_classification_change_t
));
701 context
->classification_history
[i
].location
= where
;
702 context
->classification_history
[i
].option
= jump_to
;
703 context
->classification_history
[i
].kind
= DK_POP
;
704 context
->n_classification_history
++;
707 /* Report a diagnostic message (an error or a warning) as specified by
708 DC. This function is *the* subroutine in terms of which front-ends
709 should implement their specific diagnostic handling modules. The
710 front-end independent format specifiers are exactly those described
711 in the documentation of output_format.
712 Return true if a diagnostic was printed, false otherwise. */
715 diagnostic_report_diagnostic (diagnostic_context
*context
,
716 diagnostic_info
*diagnostic
)
718 location_t location
= diagnostic
->location
;
719 diagnostic_t orig_diag_kind
= diagnostic
->kind
;
720 const char *saved_format_spec
;
722 /* Give preference to being able to inhibit warnings, before they
723 get reclassified to something else. */
724 if ((diagnostic
->kind
== DK_WARNING
|| diagnostic
->kind
== DK_PEDWARN
)
725 && !diagnostic_report_warnings_p (context
, location
))
728 if (diagnostic
->kind
== DK_PEDWARN
)
730 diagnostic
->kind
= pedantic_warning_kind (context
);
731 /* We do this to avoid giving the message for -pedantic-errors. */
732 orig_diag_kind
= diagnostic
->kind
;
735 if (diagnostic
->kind
== DK_NOTE
&& context
->inhibit_notes_p
)
738 if (context
->lock
> 0)
740 /* If we're reporting an ICE in the middle of some other error,
741 try to flush out the previous error, then let this one
742 through. Don't do this more than once. */
743 if (diagnostic
->kind
== DK_ICE
&& context
->lock
== 1)
744 pp_newline_and_flush (context
->printer
);
746 error_recursion (context
);
749 /* If the user requested that warnings be treated as errors, so be
750 it. Note that we do this before the next block so that
751 individual warnings can be overridden back to warnings with
753 if (context
->warning_as_error_requested
754 && diagnostic
->kind
== DK_WARNING
)
756 diagnostic
->kind
= DK_ERROR
;
759 if (diagnostic
->option_index
760 && diagnostic
->option_index
!= permissive_error_option (context
))
762 diagnostic_t diag_class
= DK_UNSPECIFIED
;
764 /* This tests if the user provided the appropriate -Wfoo or
766 if (! context
->option_enabled (diagnostic
->option_index
,
767 context
->option_state
))
770 /* This tests for #pragma diagnostic changes. */
771 if (context
->n_classification_history
> 0)
773 /* FIXME: Stupid search. Optimize later. */
774 for (int i
= context
->n_classification_history
- 1; i
>= 0; i
--)
776 if (linemap_location_before_p
778 context
->classification_history
[i
].location
,
781 if (context
->classification_history
[i
].kind
== (int) DK_POP
)
783 i
= context
->classification_history
[i
].option
;
786 int option
= context
->classification_history
[i
].option
;
787 /* The option 0 is for all the diagnostics. */
788 if (option
== 0 || option
== diagnostic
->option_index
)
790 diag_class
= context
->classification_history
[i
].kind
;
791 if (diag_class
!= DK_UNSPECIFIED
)
792 diagnostic
->kind
= diag_class
;
798 /* This tests if the user provided the appropriate -Werror=foo
800 if (diag_class
== DK_UNSPECIFIED
801 && context
->classify_diagnostic
[diagnostic
->option_index
] != DK_UNSPECIFIED
)
803 diagnostic
->kind
= context
->classify_diagnostic
[diagnostic
->option_index
];
805 /* This allows for future extensions, like temporarily disabling
806 warnings for ranges of source code. */
807 if (diagnostic
->kind
== DK_IGNORED
)
811 if (orig_diag_kind
== DK_WARNING
&& diagnostic
->kind
== DK_ERROR
)
812 context
->some_warnings_are_errors
= true;
816 if (diagnostic
->kind
== DK_ICE
)
818 #ifndef ENABLE_CHECKING
819 /* When not checking, ICEs are converted to fatal errors when an
820 error has already occurred. This is counteracted by
822 if ((diagnostic_kind_count (context
, DK_ERROR
) > 0
823 || diagnostic_kind_count (context
, DK_SORRY
) > 0)
824 && !context
->abort_on_error
)
826 expanded_location s
= expand_location (diagnostic
->location
);
827 fnotice (stderr
, "%s:%d: confused by earlier errors, bailing out\n",
829 exit (ICE_EXIT_CODE
);
832 if (context
->internal_error
)
833 (*context
->internal_error
) (context
,
834 diagnostic
->message
.format_spec
,
835 diagnostic
->message
.args_ptr
);
837 if (diagnostic
->kind
== DK_ERROR
&& orig_diag_kind
== DK_WARNING
)
838 ++diagnostic_kind_count (context
, DK_WERROR
);
840 ++diagnostic_kind_count (context
, diagnostic
->kind
);
842 saved_format_spec
= diagnostic
->message
.format_spec
;
843 if (context
->show_option_requested
)
847 option_text
= context
->option_name (context
, diagnostic
->option_index
,
848 orig_diag_kind
, diagnostic
->kind
);
852 diagnostic
->message
.format_spec
853 = ACONCAT ((diagnostic
->message
.format_spec
,
855 "[", option_text
, "]",
860 diagnostic
->message
.locus
= &diagnostic
->location
;
861 diagnostic
->message
.x_data
= &diagnostic
->x_data
;
862 diagnostic
->x_data
= NULL
;
863 pp_format (context
->printer
, &diagnostic
->message
);
864 (*diagnostic_starter (context
)) (context
, diagnostic
);
865 pp_output_formatted_text (context
->printer
);
866 (*diagnostic_finalizer (context
)) (context
, diagnostic
);
867 diagnostic_action_after_output (context
, diagnostic
);
868 diagnostic
->message
.format_spec
= saved_format_spec
;
869 diagnostic
->x_data
= NULL
;
876 /* Given a partial pathname as input, return another pathname that
877 shares no directory elements with the pathname of __FILE__. This
878 is used by fancy_abort() to print `Internal compiler error in expr.c'
879 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
882 trim_filename (const char *name
)
884 static const char this_file
[] = __FILE__
;
885 const char *p
= name
, *q
= this_file
;
887 /* First skip any "../" in each filename. This allows us to give a proper
888 reference to a file in a subdirectory. */
889 while (p
[0] == '.' && p
[1] == '.' && IS_DIR_SEPARATOR (p
[2]))
892 while (q
[0] == '.' && q
[1] == '.' && IS_DIR_SEPARATOR (q
[2]))
895 /* Now skip any parts the two filenames have in common. */
896 while (*p
== *q
&& *p
!= 0 && *q
!= 0)
899 /* Now go backwards until the previous directory separator. */
900 while (p
> name
&& !IS_DIR_SEPARATOR (p
[-1]))
906 /* Standard error reporting routines in increasing order of severity.
907 All of these take arguments like printf. */
909 /* Text to be emitted verbatim to the error message stream; this
910 produces no prefix and disables line-wrapping. Use rarely. */
912 verbatim (const char *gmsgid
, ...)
917 va_start (ap
, gmsgid
);
920 text
.format_spec
= _(gmsgid
);
923 pp_format_verbatim (global_dc
->printer
, &text
);
924 pp_newline_and_flush (global_dc
->printer
);
928 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
930 diagnostic_append_note (diagnostic_context
*context
,
932 const char * gmsgid
, ...)
934 diagnostic_info diagnostic
;
936 const char *saved_prefix
;
938 va_start (ap
, gmsgid
);
939 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
, DK_NOTE
);
940 if (context
->inhibit_notes_p
)
945 saved_prefix
= pp_get_prefix (context
->printer
);
946 pp_set_prefix (context
->printer
,
947 diagnostic_build_prefix (context
, &diagnostic
));
948 pp_newline (context
->printer
);
949 pp_format (context
->printer
, &diagnostic
.message
);
950 pp_output_formatted_text (context
->printer
);
951 pp_destroy_prefix (context
->printer
);
952 pp_set_prefix (context
->printer
, saved_prefix
);
953 diagnostic_show_locus (context
, &diagnostic
);
958 emit_diagnostic (diagnostic_t kind
, location_t location
, int opt
,
959 const char *gmsgid
, ...)
961 diagnostic_info diagnostic
;
965 va_start (ap
, gmsgid
);
966 if (kind
== DK_PERMERROR
)
968 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
,
969 permissive_error_kind (global_dc
));
970 diagnostic
.option_index
= permissive_error_option (global_dc
);
973 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
, kind
);
974 if (kind
== DK_WARNING
|| kind
== DK_PEDWARN
)
975 diagnostic
.option_index
= opt
;
978 ret
= report_diagnostic (&diagnostic
);
983 /* An informative note at LOCATION. Use this for additional details on an error
986 inform (location_t location
, const char *gmsgid
, ...)
988 diagnostic_info diagnostic
;
991 va_start (ap
, gmsgid
);
992 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
, DK_NOTE
);
993 report_diagnostic (&diagnostic
);
997 /* An informative note at LOCATION. Use this for additional details on an
1000 inform_n (location_t location
, int n
, const char *singular_gmsgid
,
1001 const char *plural_gmsgid
, ...)
1003 diagnostic_info diagnostic
;
1006 va_start (ap
, plural_gmsgid
);
1007 diagnostic_set_info_translated (&diagnostic
,
1008 ngettext (singular_gmsgid
, plural_gmsgid
, n
),
1009 &ap
, location
, DK_NOTE
);
1010 report_diagnostic (&diagnostic
);
1014 /* A warning at INPUT_LOCATION. Use this for code which is correct according
1015 to the relevant language specification but is likely to be buggy anyway.
1016 Returns true if the warning was printed, false if it was inhibited. */
1018 warning (int opt
, const char *gmsgid
, ...)
1020 diagnostic_info diagnostic
;
1024 va_start (ap
, gmsgid
);
1025 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_WARNING
);
1026 diagnostic
.option_index
= opt
;
1028 ret
= report_diagnostic (&diagnostic
);
1033 /* A warning at LOCATION. Use this for code which is correct according to the
1034 relevant language specification but is likely to be buggy anyway.
1035 Returns true if the warning was printed, false if it was inhibited. */
1038 warning_at (location_t location
, int opt
, const char *gmsgid
, ...)
1040 diagnostic_info diagnostic
;
1044 va_start (ap
, gmsgid
);
1045 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
, DK_WARNING
);
1046 diagnostic
.option_index
= opt
;
1047 ret
= report_diagnostic (&diagnostic
);
1052 /* A warning at LOCATION. Use this for code which is correct according to the
1053 relevant language specification but is likely to be buggy anyway.
1054 Returns true if the warning was printed, false if it was inhibited. */
1057 warning_n (location_t location
, int opt
, int n
, const char *singular_gmsgid
,
1058 const char *plural_gmsgid
, ...)
1060 diagnostic_info diagnostic
;
1064 va_start (ap
, plural_gmsgid
);
1065 diagnostic_set_info_translated (&diagnostic
,
1066 ngettext (singular_gmsgid
, plural_gmsgid
, n
),
1067 &ap
, location
, DK_WARNING
);
1068 diagnostic
.option_index
= opt
;
1069 ret
= report_diagnostic (&diagnostic
);
1074 /* A "pedantic" warning at LOCATION: issues a warning unless
1075 -pedantic-errors was given on the command line, in which case it
1076 issues an error. Use this for diagnostics required by the relevant
1077 language standard, if you have chosen not to make them errors.
1079 Note that these diagnostics are issued independent of the setting
1080 of the -Wpedantic command-line switch. To get a warning enabled
1081 only with that switch, use either "if (pedantic) pedwarn
1082 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
1083 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
1085 Returns true if the warning was printed, false if it was inhibited. */
1088 pedwarn (location_t location
, int opt
, const char *gmsgid
, ...)
1090 diagnostic_info diagnostic
;
1094 va_start (ap
, gmsgid
);
1095 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
, DK_PEDWARN
);
1096 diagnostic
.option_index
= opt
;
1097 ret
= report_diagnostic (&diagnostic
);
1102 /* A "permissive" error at LOCATION: issues an error unless
1103 -fpermissive was given on the command line, in which case it issues
1104 a warning. Use this for things that really should be errors but we
1105 want to support legacy code.
1107 Returns true if the warning was printed, false if it was inhibited. */
1110 permerror (location_t location
, const char *gmsgid
, ...)
1112 diagnostic_info diagnostic
;
1116 va_start (ap
, gmsgid
);
1117 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
,
1118 permissive_error_kind (global_dc
));
1119 diagnostic
.option_index
= permissive_error_option (global_dc
);
1120 ret
= report_diagnostic (&diagnostic
);
1125 /* A hard error: the code is definitely ill-formed, and an object file
1126 will not be produced. */
1128 error (const char *gmsgid
, ...)
1130 diagnostic_info diagnostic
;
1133 va_start (ap
, gmsgid
);
1134 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_ERROR
);
1135 report_diagnostic (&diagnostic
);
1139 /* A hard error: the code is definitely ill-formed, and an object file
1140 will not be produced. */
1142 error_n (location_t location
, int n
, const char *singular_gmsgid
,
1143 const char *plural_gmsgid
, ...)
1145 diagnostic_info diagnostic
;
1148 va_start (ap
, plural_gmsgid
);
1149 diagnostic_set_info_translated (&diagnostic
,
1150 ngettext (singular_gmsgid
, plural_gmsgid
, n
),
1151 &ap
, location
, DK_ERROR
);
1152 report_diagnostic (&diagnostic
);
1156 /* Same as ebove, but use location LOC instead of input_location. */
1158 error_at (location_t loc
, const char *gmsgid
, ...)
1160 diagnostic_info diagnostic
;
1163 va_start (ap
, gmsgid
);
1164 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, loc
, DK_ERROR
);
1165 report_diagnostic (&diagnostic
);
1169 /* "Sorry, not implemented." Use for a language feature which is
1170 required by the relevant specification but not implemented by GCC.
1171 An object file will not be produced. */
1173 sorry (const char *gmsgid
, ...)
1175 diagnostic_info diagnostic
;
1178 va_start (ap
, gmsgid
);
1179 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_SORRY
);
1180 report_diagnostic (&diagnostic
);
1184 /* Return true if an error or a "sorry" has been seen. Various
1185 processing is disabled after errors. */
1189 return errorcount
|| sorrycount
;
1192 /* An error which is severe enough that we make no attempt to
1193 continue. Do not use this for internal consistency checks; that's
1194 internal_error. Use of this function should be rare. */
1196 fatal_error (const char *gmsgid
, ...)
1198 diagnostic_info diagnostic
;
1201 va_start (ap
, gmsgid
);
1202 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_FATAL
);
1203 report_diagnostic (&diagnostic
);
1209 /* An error which is severe enough that we make no attempt to
1210 continue. Do not use this for internal consistency checks; that's
1211 internal_error. Use of this function should be rare. */
1213 fatal_error (location_t loc
, const char *gmsgid
, ...)
1215 diagnostic_info diagnostic
;
1218 va_start (ap
, gmsgid
);
1219 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, loc
, DK_FATAL
);
1220 report_diagnostic (&diagnostic
);
1226 /* An internal consistency check has failed. We make no attempt to
1227 continue. Note that unless there is debugging value to be had from
1228 a more specific message, or some other good reason, you should use
1229 abort () instead of calling this function directly. */
1231 internal_error (const char *gmsgid
, ...)
1233 diagnostic_info diagnostic
;
1236 va_start (ap
, gmsgid
);
1237 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, input_location
, DK_ICE
);
1238 report_diagnostic (&diagnostic
);
1244 /* Special case error functions. Most are implemented in terms of the
1245 above, or should be. */
1247 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1248 runs its second argument through gettext. */
1250 fnotice (FILE *file
, const char *cmsgid
, ...)
1254 va_start (ap
, cmsgid
);
1255 vfprintf (file
, _(cmsgid
), ap
);
1259 /* Inform the user that an error occurred while trying to report some
1260 other error. This indicates catastrophic internal inconsistencies,
1261 so give up now. But do try to flush out the previous error.
1262 This mustn't use internal_error, that will cause infinite recursion. */
1265 error_recursion (diagnostic_context
*context
)
1267 diagnostic_info diagnostic
;
1269 if (context
->lock
< 3)
1270 pp_newline_and_flush (context
->printer
);
1273 "Internal compiler error: Error reporting routines re-entered.\n");
1275 /* Call diagnostic_action_after_output to get the "please submit a bug
1276 report" message. It only looks at the kind field of diagnostic_info. */
1277 diagnostic
.kind
= DK_ICE
;
1278 diagnostic_action_after_output (context
, &diagnostic
);
1280 /* Do not use gcc_unreachable here; that goes through internal_error
1281 and therefore would cause infinite recursion. */
1285 /* Report an internal compiler error in a friendly manner. This is
1286 the function that gets called upon use of abort() in the source
1287 code generally, thanks to a special macro. */
1290 fancy_abort (const char *file
, int line
, const char *function
)
1292 internal_error ("in %s, at %s:%d", function
, trim_filename (file
), line
);
1295 /* Really call the system 'abort'. This has to go right at the end of
1296 this file, so that there are no functions after it that call abort
1297 and get the system abort instead of our macro. */