1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999-2016 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"
31 #include "backtrace.h"
32 #include "diagnostic.h"
33 #include "diagnostic-color.h"
40 #ifdef GWINSZ_IN_SYS_IOCTL
41 # include <sys/ioctl.h>
44 #define pedantic_warning_kind(DC) \
45 ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
46 #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR)
47 #define permissive_error_option(DC) ((DC)->opt_permissive)
50 static bool diagnostic_impl (rich_location
*, int, const char *,
51 va_list *, diagnostic_t
) ATTRIBUTE_GCC_DIAG(3,0);
52 static bool diagnostic_n_impl (location_t
, int, int, const char *,
53 const char *, va_list *,
54 diagnostic_t
) ATTRIBUTE_GCC_DIAG(5,0);
55 static void error_recursion (diagnostic_context
*) ATTRIBUTE_NORETURN
;
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 str
= xvasprintf (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
->warning_as_error_requested
= false;
144 context
->n_opts
= n_opts
;
145 context
->classify_diagnostic
= XNEWVEC (diagnostic_t
, n_opts
);
146 for (i
= 0; i
< n_opts
; i
++)
147 context
->classify_diagnostic
[i
] = DK_UNSPECIFIED
;
148 context
->show_caret
= false;
149 diagnostic_set_caret_max_width (context
, pp_line_cutoff (context
->printer
));
150 for (i
= 0; i
< rich_location::MAX_RANGES
; i
++)
151 context
->caret_chars
[i
] = '^';
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 context
->start_span
= default_diagnostic_start_span_fn
;
165 diagnostic_finalizer (context
) = default_diagnostic_finalizer
;
166 context
->option_enabled
= NULL
;
167 context
->option_state
= NULL
;
168 context
->option_name
= NULL
;
169 context
->last_location
= UNKNOWN_LOCATION
;
170 context
->last_module
= 0;
171 context
->x_data
= NULL
;
173 context
->inhibit_notes_p
= false;
176 /* Maybe initialize the color support. We require clients to do this
177 explicitly, since most clients don't want color. When called
178 without a VALUE, it initializes with DIAGNOSTICS_COLOR_DEFAULT. */
181 diagnostic_color_init (diagnostic_context
*context
, int value
/*= -1 */)
183 /* value == -1 is the default value. */
186 /* If DIAGNOSTICS_COLOR_DEFAULT is -1, default to
187 -fdiagnostics-color=auto if GCC_COLORS is in the environment,
188 otherwise default to -fdiagnostics-color=never, for other
189 values default to that
190 -fdiagnostics-color={never,auto,always}. */
191 if (DIAGNOSTICS_COLOR_DEFAULT
== -1)
193 if (!getenv ("GCC_COLORS"))
195 value
= DIAGNOSTICS_COLOR_AUTO
;
198 value
= DIAGNOSTICS_COLOR_DEFAULT
;
200 pp_show_color (context
->printer
)
201 = colorize_init ((diagnostic_color_rule_t
) value
);
204 /* Do any cleaning up required after the last diagnostic is emitted. */
207 diagnostic_finish (diagnostic_context
*context
)
209 /* Some of the errors may actually have been warnings. */
210 if (diagnostic_kind_count (context
, DK_WERROR
))
212 /* -Werror was given. */
213 if (context
->warning_as_error_requested
)
214 pp_verbatim (context
->printer
,
215 _("%s: all warnings being treated as errors"),
217 /* At least one -Werror= was given. */
219 pp_verbatim (context
->printer
,
220 _("%s: some warnings being treated as errors"),
222 pp_newline_and_flush (context
->printer
);
225 diagnostic_file_cache_fini ();
227 XDELETEVEC (context
->classify_diagnostic
);
228 context
->classify_diagnostic
= NULL
;
230 /* diagnostic_initialize allocates context->printer using XNEW
231 and placement-new. */
232 context
->printer
->~pretty_printer ();
233 XDELETE (context
->printer
);
234 context
->printer
= NULL
;
237 /* Initialize DIAGNOSTIC, where the message MSG has already been
240 diagnostic_set_info_translated (diagnostic_info
*diagnostic
, const char *msg
,
241 va_list *args
, rich_location
*richloc
,
244 gcc_assert (richloc
);
245 diagnostic
->message
.err_no
= errno
;
246 diagnostic
->message
.args_ptr
= args
;
247 diagnostic
->message
.format_spec
= msg
;
248 diagnostic
->message
.m_richloc
= richloc
;
249 diagnostic
->richloc
= richloc
;
250 diagnostic
->kind
= kind
;
251 diagnostic
->option_index
= 0;
254 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
257 diagnostic_set_info (diagnostic_info
*diagnostic
, const char *gmsgid
,
258 va_list *args
, rich_location
*richloc
,
261 gcc_assert (richloc
);
262 diagnostic_set_info_translated (diagnostic
, _(gmsgid
), args
, richloc
, kind
);
265 static const char *const diagnostic_kind_color
[] = {
266 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
267 #include "diagnostic.def"
268 #undef DEFINE_DIAGNOSTIC_KIND
272 /* Get a color name for diagnostics of type KIND
273 Result could be NULL. */
276 diagnostic_get_color_for_kind (diagnostic_t kind
)
278 return diagnostic_kind_color
[kind
];
281 /* Return a malloc'd string describing a location e.g. "foo.c:42:10".
282 The caller is responsible for freeing the memory. */
285 diagnostic_get_location_text (diagnostic_context
*context
,
288 pretty_printer
*pp
= context
->printer
;
289 const char *locus_cs
= colorize_start (pp_show_color (pp
), "locus");
290 const char *locus_ce
= colorize_stop (pp_show_color (pp
));
293 return build_message_string ("%s%s:%s", locus_cs
, progname
, locus_ce
);
295 if (!strcmp (s
.file
, N_("<built-in>")))
296 return build_message_string ("%s%s:%s", locus_cs
, s
.file
, locus_ce
);
298 if (context
->show_column
)
299 return build_message_string ("%s%s:%d:%d:%s", locus_cs
, s
.file
, s
.line
,
302 return build_message_string ("%s%s:%d:%s", locus_cs
, s
.file
, s
.line
,
306 /* Return a malloc'd string describing a location and the severity of the
307 diagnostic, e.g. "foo.c:42:10: error: ". The caller is responsible for
308 freeing the memory. */
310 diagnostic_build_prefix (diagnostic_context
*context
,
311 const diagnostic_info
*diagnostic
)
313 static const char *const diagnostic_kind_text
[] = {
314 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
315 #include "diagnostic.def"
316 #undef DEFINE_DIAGNOSTIC_KIND
319 gcc_assert (diagnostic
->kind
< DK_LAST_DIAGNOSTIC_KIND
);
321 const char *text
= _(diagnostic_kind_text
[diagnostic
->kind
]);
322 const char *text_cs
= "", *text_ce
= "";
323 pretty_printer
*pp
= context
->printer
;
325 if (diagnostic_kind_color
[diagnostic
->kind
])
327 text_cs
= colorize_start (pp_show_color (pp
),
328 diagnostic_kind_color
[diagnostic
->kind
]);
329 text_ce
= colorize_stop (pp_show_color (pp
));
332 expanded_location s
= diagnostic_expand_location (diagnostic
);
333 char *location_text
= diagnostic_get_location_text (context
, s
);
335 char *result
= build_message_string ("%s %s%s%s", location_text
,
336 text_cs
, text
, text_ce
);
337 free (location_text
);
341 /* Functions at which to stop the backtrace print. It's not
342 particularly helpful to print the callers of these functions. */
344 static const char * const bt_stop
[] =
352 /* A callback function passed to the backtrace_full function. */
355 bt_callback (void *data
, uintptr_t pc
, const char *filename
, int lineno
,
356 const char *function
)
358 int *pcount
= (int *) data
;
360 /* If we don't have any useful information, don't print
362 if (filename
== NULL
&& function
== NULL
)
365 /* Skip functions in diagnostic.c. */
368 && strcmp (lbasename (filename
), "diagnostic.c") == 0)
371 /* Print up to 20 functions. We could make this a --param, but
372 since this is only for debugging just use a constant for now. */
375 /* Returning a non-zero value stops the backtrace. */
381 if (function
!= NULL
)
383 char *str
= cplus_demangle_v3 (function
,
384 (DMGL_VERBOSE
| DMGL_ANSI
385 | DMGL_GNU_V3
| DMGL_PARAMS
));
392 for (size_t i
= 0; i
< ARRAY_SIZE (bt_stop
); ++i
)
394 size_t len
= strlen (bt_stop
[i
]);
395 if (strncmp (function
, bt_stop
[i
], len
) == 0
396 && (function
[len
] == '\0' || function
[len
] == '('))
400 /* Returning a non-zero value stops the backtrace. */
406 fprintf (stderr
, "0x%lx %s\n\t%s:%d\n",
408 function
== NULL
? "???" : function
,
409 filename
== NULL
? "???" : filename
,
418 /* A callback function passed to the backtrace_full function. This is
419 called if backtrace_full has an error. */
422 bt_err_callback (void *data ATTRIBUTE_UNUSED
, const char *msg
, int errnum
)
426 /* This means that no debug info was available. Just quietly
427 skip printing backtrace info. */
430 fprintf (stderr
, "%s%s%s\n", msg
, errnum
== 0 ? "" : ": ",
431 errnum
== 0 ? "" : xstrerror (errnum
));
434 /* Take any action which is expected to happen after the diagnostic
435 is written out. This function does not always return. */
437 diagnostic_action_after_output (diagnostic_context
*context
,
438 diagnostic_t diag_kind
)
450 if (context
->abort_on_error
)
452 if (context
->fatal_errors
)
454 fnotice (stderr
, "compilation terminated due to -Wfatal-errors.\n");
455 diagnostic_finish (context
);
456 exit (FATAL_EXIT_CODE
);
458 if (context
->max_errors
!= 0
459 && ((unsigned) (diagnostic_kind_count (context
, DK_ERROR
)
460 + diagnostic_kind_count (context
, DK_SORRY
)
461 + diagnostic_kind_count (context
, DK_WERROR
))
462 >= context
->max_errors
))
465 "compilation terminated due to -fmax-errors=%u.\n",
466 context
->max_errors
);
467 diagnostic_finish (context
);
468 exit (FATAL_EXIT_CODE
);
475 struct backtrace_state
*state
= NULL
;
476 if (diag_kind
== DK_ICE
)
477 state
= backtrace_create_state (NULL
, 0, bt_err_callback
, NULL
);
480 backtrace_full (state
, 2, bt_callback
, bt_err_callback
,
483 if (context
->abort_on_error
)
486 fnotice (stderr
, "Please submit a full bug report,\n"
487 "with preprocessed source if appropriate.\n");
490 ("Please include the complete backtrace "
491 "with any bug report.\n"));
492 fnotice (stderr
, "See %s for instructions.\n", bug_report_url
);
494 exit (ICE_EXIT_CODE
);
498 if (context
->abort_on_error
)
500 diagnostic_finish (context
);
501 fnotice (stderr
, "compilation terminated.\n");
502 exit (FATAL_EXIT_CODE
);
510 diagnostic_report_current_module (diagnostic_context
*context
, location_t where
)
512 const line_map_ordinary
*map
= NULL
;
514 if (pp_needs_newline (context
->printer
))
516 pp_newline (context
->printer
);
517 pp_needs_newline (context
->printer
) = false;
520 if (where
<= BUILTINS_LOCATION
)
523 linemap_resolve_location (line_table
, where
,
524 LRK_MACRO_DEFINITION_LOCATION
,
527 if (map
&& diagnostic_last_module_changed (context
, map
))
529 diagnostic_set_last_module (context
, map
);
530 if (! MAIN_FILE_P (map
))
532 map
= INCLUDED_FROM (line_table
, map
);
533 if (context
->show_column
)
534 pp_verbatim (context
->printer
,
535 "In file included from %r%s:%d:%d%R", "locus",
537 LAST_SOURCE_LINE (map
), LAST_SOURCE_COLUMN (map
));
539 pp_verbatim (context
->printer
,
540 "In file included from %r%s:%d%R", "locus",
541 LINEMAP_FILE (map
), LAST_SOURCE_LINE (map
));
542 while (! MAIN_FILE_P (map
))
544 map
= INCLUDED_FROM (line_table
, map
);
545 pp_verbatim (context
->printer
,
546 ",\n from %r%s:%d%R", "locus",
547 LINEMAP_FILE (map
), LAST_SOURCE_LINE (map
));
549 pp_verbatim (context
->printer
, ":");
550 pp_newline (context
->printer
);
556 default_diagnostic_starter (diagnostic_context
*context
,
557 diagnostic_info
*diagnostic
)
559 diagnostic_report_current_module (context
, diagnostic_location (diagnostic
));
560 pp_set_prefix (context
->printer
, diagnostic_build_prefix (context
,
565 default_diagnostic_start_span_fn (diagnostic_context
*context
,
566 expanded_location exploc
)
568 pp_set_prefix (context
->printer
,
569 diagnostic_get_location_text (context
, exploc
));
570 pp_string (context
->printer
, "");
571 pp_newline (context
->printer
);
575 default_diagnostic_finalizer (diagnostic_context
*context
,
576 diagnostic_info
*diagnostic
)
578 diagnostic_show_locus (context
, diagnostic
);
579 pp_destroy_prefix (context
->printer
);
580 pp_flush (context
->printer
);
583 /* Interface to specify diagnostic kind overrides. Returns the
584 previous setting, or DK_UNSPECIFIED if the parameters are out of
585 range. If OPTION_INDEX is zero, the new setting is for all the
588 diagnostic_classify_diagnostic (diagnostic_context
*context
,
590 diagnostic_t new_kind
,
593 diagnostic_t old_kind
;
596 || option_index
>= context
->n_opts
597 || new_kind
>= DK_LAST_DIAGNOSTIC_KIND
)
598 return DK_UNSPECIFIED
;
600 old_kind
= context
->classify_diagnostic
[option_index
];
602 /* Handle pragmas separately, since we need to keep track of *where*
604 if (where
!= UNKNOWN_LOCATION
)
608 /* Record the command-line status, so we can reset it back on DK_POP. */
609 if (old_kind
== DK_UNSPECIFIED
)
611 old_kind
= !context
->option_enabled (option_index
,
612 context
->option_state
)
613 ? DK_IGNORED
: (context
->warning_as_error_requested
614 ? DK_ERROR
: DK_WARNING
);
615 context
->classify_diagnostic
[option_index
] = old_kind
;
618 for (i
= context
->n_classification_history
- 1; i
>= 0; i
--)
619 if (context
->classification_history
[i
].option
== option_index
)
621 old_kind
= context
->classification_history
[i
].kind
;
625 i
= context
->n_classification_history
;
626 context
->classification_history
=
627 (diagnostic_classification_change_t
*) xrealloc (context
->classification_history
, (i
+ 1)
628 * sizeof (diagnostic_classification_change_t
));
629 context
->classification_history
[i
].location
= where
;
630 context
->classification_history
[i
].option
= option_index
;
631 context
->classification_history
[i
].kind
= new_kind
;
632 context
->n_classification_history
++;
635 context
->classify_diagnostic
[option_index
] = new_kind
;
640 /* Save all diagnostic classifications in a stack. */
642 diagnostic_push_diagnostics (diagnostic_context
*context
, location_t where ATTRIBUTE_UNUSED
)
644 context
->push_list
= (int *) xrealloc (context
->push_list
, (context
->n_push
+ 1) * sizeof (int));
645 context
->push_list
[context
->n_push
++] = context
->n_classification_history
;
648 /* Restore the topmost classification set off the stack. If the stack
649 is empty, revert to the state based on command line parameters. */
651 diagnostic_pop_diagnostics (diagnostic_context
*context
, location_t where
)
657 jump_to
= context
->push_list
[-- context
->n_push
];
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
= jump_to
;
667 context
->classification_history
[i
].kind
= DK_POP
;
668 context
->n_classification_history
++;
671 /* Helper function for print_parseable_fixits. Print TEXT to PP, obeying the
672 escaping rules for -fdiagnostics-parseable-fixits. */
675 print_escaped_string (pretty_printer
*pp
, const char *text
)
680 pp_character (pp
, '"');
681 for (const char *ch
= text
; *ch
; ch
++)
686 /* Escape backslash as two backslashes. */
687 pp_string (pp
, "\\\\");
690 /* Escape tab as "\t". */
691 pp_string (pp
, "\\t");
694 /* Escape newline as "\n". */
695 pp_string (pp
, "\\n");
698 /* Escape doublequotes as \". */
699 pp_string (pp
, "\\\"");
703 pp_character (pp
, *ch
);
705 /* Use octal for non-printable chars. */
707 unsigned char c
= (*ch
& 0xff);
708 pp_printf (pp
, "\\%o%o%o", (c
/ 64), (c
/ 8) & 007, c
& 007);
713 pp_character (pp
, '"');
716 /* Implementation of -fdiagnostics-parseable-fixits. Print a
717 machine-parseable version of all fixits in RICHLOC to PP. */
720 print_parseable_fixits (pretty_printer
*pp
, rich_location
*richloc
)
723 gcc_assert (richloc
);
725 for (unsigned i
= 0; i
< richloc
->get_num_fixit_hints (); i
++)
727 const fixit_hint
*hint
= richloc
->get_fixit_hint (i
);
728 source_location start_loc
= hint
->get_start_loc ();
729 expanded_location start_exploc
= expand_location (start_loc
);
730 pp_string (pp
, "fix-it:");
731 print_escaped_string (pp
, start_exploc
.file
);
732 source_location end_loc
;
734 /* For compatibility with clang, print as a half-open range. */
735 if (hint
->maybe_get_end_loc (&end_loc
))
737 expanded_location end_exploc
= expand_location (end_loc
);
738 pp_printf (pp
, ":{%i:%i-%i:%i}:",
739 start_exploc
.line
, start_exploc
.column
,
740 end_exploc
.line
, end_exploc
.column
+ 1);
744 pp_printf (pp
, ":{%i:%i-%i:%i}:",
745 start_exploc
.line
, start_exploc
.column
,
746 start_exploc
.line
, start_exploc
.column
);
748 switch (hint
->get_kind ())
750 case fixit_hint::INSERT
:
752 const fixit_insert
*insert
753 = static_cast <const fixit_insert
*> (hint
);
754 print_escaped_string (pp
, insert
->get_string ());
758 case fixit_hint::REMOVE
:
759 print_escaped_string (pp
, "");
762 case fixit_hint::REPLACE
:
764 const fixit_replace
*replace
765 = static_cast <const fixit_replace
*> (hint
);
766 print_escaped_string (pp
, replace
->get_string ());
777 /* Report a diagnostic message (an error or a warning) as specified by
778 DC. This function is *the* subroutine in terms of which front-ends
779 should implement their specific diagnostic handling modules. The
780 front-end independent format specifiers are exactly those described
781 in the documentation of output_format.
782 Return true if a diagnostic was printed, false otherwise. */
785 diagnostic_report_diagnostic (diagnostic_context
*context
,
786 diagnostic_info
*diagnostic
)
788 location_t location
= diagnostic_location (diagnostic
);
789 diagnostic_t orig_diag_kind
= diagnostic
->kind
;
790 const char *saved_format_spec
;
792 /* Give preference to being able to inhibit warnings, before they
793 get reclassified to something else. */
794 if ((diagnostic
->kind
== DK_WARNING
|| diagnostic
->kind
== DK_PEDWARN
)
795 && !diagnostic_report_warnings_p (context
, location
))
798 if (diagnostic
->kind
== DK_PEDWARN
)
800 diagnostic
->kind
= pedantic_warning_kind (context
);
801 /* We do this to avoid giving the message for -pedantic-errors. */
802 orig_diag_kind
= diagnostic
->kind
;
805 if (diagnostic
->kind
== DK_NOTE
&& context
->inhibit_notes_p
)
808 if (context
->lock
> 0)
810 /* If we're reporting an ICE in the middle of some other error,
811 try to flush out the previous error, then let this one
812 through. Don't do this more than once. */
813 if ((diagnostic
->kind
== DK_ICE
|| diagnostic
->kind
== DK_ICE_NOBT
)
814 && context
->lock
== 1)
815 pp_newline_and_flush (context
->printer
);
817 error_recursion (context
);
820 /* If the user requested that warnings be treated as errors, so be
821 it. Note that we do this before the next block so that
822 individual warnings can be overridden back to warnings with
824 if (context
->warning_as_error_requested
825 && diagnostic
->kind
== DK_WARNING
)
827 diagnostic
->kind
= DK_ERROR
;
830 if (diagnostic
->option_index
831 && diagnostic
->option_index
!= permissive_error_option (context
))
833 diagnostic_t diag_class
= DK_UNSPECIFIED
;
835 /* This tests if the user provided the appropriate -Wfoo or
837 if (! context
->option_enabled (diagnostic
->option_index
,
838 context
->option_state
))
841 /* This tests for #pragma diagnostic changes. */
842 if (context
->n_classification_history
> 0)
844 /* FIXME: Stupid search. Optimize later. */
845 for (int i
= context
->n_classification_history
- 1; i
>= 0; i
--)
847 if (linemap_location_before_p
849 context
->classification_history
[i
].location
,
852 if (context
->classification_history
[i
].kind
== (int) DK_POP
)
854 i
= context
->classification_history
[i
].option
;
857 int option
= context
->classification_history
[i
].option
;
858 /* The option 0 is for all the diagnostics. */
859 if (option
== 0 || option
== diagnostic
->option_index
)
861 diag_class
= context
->classification_history
[i
].kind
;
862 if (diag_class
!= DK_UNSPECIFIED
)
863 diagnostic
->kind
= diag_class
;
869 /* This tests if the user provided the appropriate -Werror=foo
871 if (diag_class
== DK_UNSPECIFIED
872 && context
->classify_diagnostic
[diagnostic
->option_index
] != DK_UNSPECIFIED
)
874 diagnostic
->kind
= context
->classify_diagnostic
[diagnostic
->option_index
];
876 /* This allows for future extensions, like temporarily disabling
877 warnings for ranges of source code. */
878 if (diagnostic
->kind
== DK_IGNORED
)
884 if (diagnostic
->kind
== DK_ICE
|| diagnostic
->kind
== DK_ICE_NOBT
)
886 /* When not checking, ICEs are converted to fatal errors when an
887 error has already occurred. This is counteracted by
890 && (diagnostic_kind_count (context
, DK_ERROR
) > 0
891 || diagnostic_kind_count (context
, DK_SORRY
) > 0)
892 && !context
->abort_on_error
)
895 = expand_location (diagnostic_location (diagnostic
));
896 fnotice (stderr
, "%s:%d: confused by earlier errors, bailing out\n",
898 exit (ICE_EXIT_CODE
);
900 if (context
->internal_error
)
901 (*context
->internal_error
) (context
,
902 diagnostic
->message
.format_spec
,
903 diagnostic
->message
.args_ptr
);
905 if (diagnostic
->kind
== DK_ERROR
&& orig_diag_kind
== DK_WARNING
)
906 ++diagnostic_kind_count (context
, DK_WERROR
);
908 ++diagnostic_kind_count (context
, diagnostic
->kind
);
910 saved_format_spec
= diagnostic
->message
.format_spec
;
911 if (context
->show_option_requested
)
915 option_text
= context
->option_name (context
, diagnostic
->option_index
,
916 orig_diag_kind
, diagnostic
->kind
);
921 = colorize_start (pp_show_color (context
->printer
),
922 diagnostic_kind_color
[diagnostic
->kind
]);
923 const char *ce
= colorize_stop (pp_show_color (context
->printer
));
924 diagnostic
->message
.format_spec
925 = ACONCAT ((diagnostic
->message
.format_spec
,
927 "[", cs
, option_text
, ce
, "]",
932 diagnostic
->message
.x_data
= &diagnostic
->x_data
;
933 diagnostic
->x_data
= NULL
;
934 pp_format (context
->printer
, &diagnostic
->message
);
935 (*diagnostic_starter (context
)) (context
, diagnostic
);
936 pp_output_formatted_text (context
->printer
);
937 (*diagnostic_finalizer (context
)) (context
, diagnostic
);
938 if (context
->parseable_fixits_p
)
940 print_parseable_fixits (context
->printer
, diagnostic
->richloc
);
941 pp_flush (context
->printer
);
943 diagnostic_action_after_output (context
, diagnostic
->kind
);
944 diagnostic
->message
.format_spec
= saved_format_spec
;
945 diagnostic
->x_data
= NULL
;
952 /* Given a partial pathname as input, return another pathname that
953 shares no directory elements with the pathname of __FILE__. This
954 is used by fancy_abort() to print `Internal compiler error in expr.c'
955 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
958 trim_filename (const char *name
)
960 static const char this_file
[] = __FILE__
;
961 const char *p
= name
, *q
= this_file
;
963 /* First skip any "../" in each filename. This allows us to give a proper
964 reference to a file in a subdirectory. */
965 while (p
[0] == '.' && p
[1] == '.' && IS_DIR_SEPARATOR (p
[2]))
968 while (q
[0] == '.' && q
[1] == '.' && IS_DIR_SEPARATOR (q
[2]))
971 /* Now skip any parts the two filenames have in common. */
972 while (*p
== *q
&& *p
!= 0 && *q
!= 0)
975 /* Now go backwards until the previous directory separator. */
976 while (p
> name
&& !IS_DIR_SEPARATOR (p
[-1]))
982 /* Standard error reporting routines in increasing order of severity.
983 All of these take arguments like printf. */
985 /* Text to be emitted verbatim to the error message stream; this
986 produces no prefix and disables line-wrapping. Use rarely. */
988 verbatim (const char *gmsgid
, ...)
993 va_start (ap
, gmsgid
);
996 text
.format_spec
= _(gmsgid
);
998 pp_format_verbatim (global_dc
->printer
, &text
);
999 pp_newline_and_flush (global_dc
->printer
);
1003 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
1005 diagnostic_append_note (diagnostic_context
*context
,
1006 location_t location
,
1007 const char * gmsgid
, ...)
1009 diagnostic_info diagnostic
;
1011 const char *saved_prefix
;
1012 rich_location
richloc (line_table
, location
);
1014 va_start (ap
, gmsgid
);
1015 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_NOTE
);
1016 if (context
->inhibit_notes_p
)
1021 saved_prefix
= pp_get_prefix (context
->printer
);
1022 pp_set_prefix (context
->printer
,
1023 diagnostic_build_prefix (context
, &diagnostic
));
1024 pp_format (context
->printer
, &diagnostic
.message
);
1025 pp_output_formatted_text (context
->printer
);
1026 pp_destroy_prefix (context
->printer
);
1027 pp_set_prefix (context
->printer
, saved_prefix
);
1028 diagnostic_show_locus (context
, &diagnostic
);
1032 /* Implement emit_diagnostic, inform, inform_at_rich_loc, warning, warning_at,
1033 warning_at_rich_loc, pedwarn, permerror, permerror_at_rich_loc, error,
1034 error_at, error_at_rich_loc, sorry, fatal_error, internal_error, and
1035 internal_error_no_backtrace, as documented and defined below. */
1037 diagnostic_impl (rich_location
*richloc
, int opt
,
1039 va_list *ap
, diagnostic_t kind
)
1041 diagnostic_info diagnostic
;
1042 if (kind
== DK_PERMERROR
)
1044 diagnostic_set_info (&diagnostic
, gmsgid
, ap
, richloc
,
1045 permissive_error_kind (global_dc
));
1046 diagnostic
.option_index
= permissive_error_option (global_dc
);
1050 diagnostic_set_info (&diagnostic
, gmsgid
, ap
, richloc
, kind
);
1051 if (kind
== DK_WARNING
|| kind
== DK_PEDWARN
)
1052 diagnostic
.option_index
= opt
;
1054 return report_diagnostic (&diagnostic
);
1057 /* Implement inform_n, warning_n, and error_n, as documented and
1060 diagnostic_n_impl (location_t location
, int opt
, int n
,
1061 const char *singular_gmsgid
,
1062 const char *plural_gmsgid
,
1063 va_list *ap
, diagnostic_t kind
)
1065 diagnostic_info diagnostic
;
1066 rich_location
richloc (line_table
, location
);
1067 diagnostic_set_info_translated (&diagnostic
,
1068 ngettext (singular_gmsgid
, plural_gmsgid
, n
),
1069 ap
, &richloc
, kind
);
1070 if (kind
== DK_WARNING
)
1071 diagnostic
.option_index
= opt
;
1072 return report_diagnostic (&diagnostic
);
1076 emit_diagnostic (diagnostic_t kind
, location_t location
, int opt
,
1077 const char *gmsgid
, ...)
1080 va_start (ap
, gmsgid
);
1081 rich_location
richloc (line_table
, location
);
1082 bool ret
= diagnostic_impl (&richloc
, opt
, gmsgid
, &ap
, kind
);
1087 /* An informative note at LOCATION. Use this for additional details on an error
1090 inform (location_t location
, const char *gmsgid
, ...)
1093 va_start (ap
, gmsgid
);
1094 rich_location
richloc (line_table
, location
);
1095 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_NOTE
);
1099 /* Same as "inform", but at RICHLOC. */
1101 inform_at_rich_loc (rich_location
*richloc
, const char *gmsgid
, ...)
1104 va_start (ap
, gmsgid
);
1105 diagnostic_impl (richloc
, -1, gmsgid
, &ap
, DK_NOTE
);
1109 /* An informative note at LOCATION. Use this for additional details on an
1112 inform_n (location_t location
, int n
, const char *singular_gmsgid
,
1113 const char *plural_gmsgid
, ...)
1116 va_start (ap
, plural_gmsgid
);
1117 diagnostic_n_impl (location
, -1, n
, singular_gmsgid
, plural_gmsgid
,
1122 /* A warning at INPUT_LOCATION. Use this for code which is correct according
1123 to the relevant language specification but is likely to be buggy anyway.
1124 Returns true if the warning was printed, false if it was inhibited. */
1126 warning (int opt
, const char *gmsgid
, ...)
1129 va_start (ap
, gmsgid
);
1130 rich_location
richloc (line_table
, input_location
);
1131 bool ret
= diagnostic_impl (&richloc
, opt
, gmsgid
, &ap
, DK_WARNING
);
1136 /* A warning at LOCATION. Use this for code which is correct according to the
1137 relevant language specification but is likely to be buggy anyway.
1138 Returns true if the warning was printed, false if it was inhibited. */
1141 warning_at (location_t location
, int opt
, const char *gmsgid
, ...)
1144 va_start (ap
, gmsgid
);
1145 rich_location
richloc (line_table
, location
);
1146 bool ret
= diagnostic_impl (&richloc
, opt
, gmsgid
, &ap
, DK_WARNING
);
1151 /* Same as warning at, but using RICHLOC. */
1154 warning_at_rich_loc (rich_location
*richloc
, int opt
, const char *gmsgid
, ...)
1157 va_start (ap
, gmsgid
);
1158 bool ret
= diagnostic_impl (richloc
, opt
, gmsgid
, &ap
, DK_WARNING
);
1163 /* A warning at LOCATION. Use this for code which is correct according to the
1164 relevant language specification but is likely to be buggy anyway.
1165 Returns true if the warning was printed, false if it was inhibited. */
1168 warning_n (location_t location
, int opt
, int n
, const char *singular_gmsgid
,
1169 const char *plural_gmsgid
, ...)
1172 va_start (ap
, plural_gmsgid
);
1173 bool ret
= diagnostic_n_impl (location
, opt
, n
,
1174 singular_gmsgid
, plural_gmsgid
,
1180 /* A "pedantic" warning at LOCATION: issues a warning unless
1181 -pedantic-errors was given on the command line, in which case it
1182 issues an error. Use this for diagnostics required by the relevant
1183 language standard, if you have chosen not to make them errors.
1185 Note that these diagnostics are issued independent of the setting
1186 of the -Wpedantic command-line switch. To get a warning enabled
1187 only with that switch, use either "if (pedantic) pedwarn
1188 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
1189 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
1191 Returns true if the warning was printed, false if it was inhibited. */
1194 pedwarn (location_t location
, int opt
, const char *gmsgid
, ...)
1197 va_start (ap
, gmsgid
);
1198 rich_location
richloc (line_table
, location
);
1199 bool ret
= diagnostic_impl (&richloc
, opt
, gmsgid
, &ap
, DK_PEDWARN
);
1204 /* Same as pedwarn, but using RICHLOC. */
1207 pedwarn_at_rich_loc (rich_location
*richloc
, int opt
, const char *gmsgid
, ...)
1210 va_start (ap
, gmsgid
);
1211 bool ret
= diagnostic_impl (richloc
, opt
, gmsgid
, &ap
, DK_PEDWARN
);
1216 /* A "permissive" error at LOCATION: issues an error unless
1217 -fpermissive was given on the command line, in which case it issues
1218 a warning. Use this for things that really should be errors but we
1219 want to support legacy code.
1221 Returns true if the warning was printed, false if it was inhibited. */
1224 permerror (location_t location
, const char *gmsgid
, ...)
1227 va_start (ap
, gmsgid
);
1228 rich_location
richloc (line_table
, location
);
1229 bool ret
= diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_PERMERROR
);
1234 /* Same as "permerror", but at RICHLOC. */
1237 permerror_at_rich_loc (rich_location
*richloc
, const char *gmsgid
, ...)
1240 va_start (ap
, gmsgid
);
1241 bool ret
= diagnostic_impl (richloc
, -1, gmsgid
, &ap
, DK_PERMERROR
);
1246 /* A hard error: the code is definitely ill-formed, and an object file
1247 will not be produced. */
1249 error (const char *gmsgid
, ...)
1252 va_start (ap
, gmsgid
);
1253 rich_location
richloc (line_table
, input_location
);
1254 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_ERROR
);
1258 /* A hard error: the code is definitely ill-formed, and an object file
1259 will not be produced. */
1261 error_n (location_t location
, int n
, const char *singular_gmsgid
,
1262 const char *plural_gmsgid
, ...)
1265 va_start (ap
, plural_gmsgid
);
1266 diagnostic_n_impl (location
, -1, n
, singular_gmsgid
, plural_gmsgid
,
1271 /* Same as above, but use location LOC instead of input_location. */
1273 error_at (location_t loc
, const char *gmsgid
, ...)
1276 va_start (ap
, gmsgid
);
1277 rich_location
richloc (line_table
, loc
);
1278 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_ERROR
);
1282 /* Same as above, but use RICH_LOC. */
1285 error_at_rich_loc (rich_location
*richloc
, const char *gmsgid
, ...)
1288 va_start (ap
, gmsgid
);
1289 diagnostic_impl (richloc
, -1, gmsgid
, &ap
, DK_ERROR
);
1293 /* "Sorry, not implemented." Use for a language feature which is
1294 required by the relevant specification but not implemented by GCC.
1295 An object file will not be produced. */
1297 sorry (const char *gmsgid
, ...)
1300 va_start (ap
, gmsgid
);
1301 rich_location
richloc (line_table
, input_location
);
1302 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_SORRY
);
1306 /* Return true if an error or a "sorry" has been seen. Various
1307 processing is disabled after errors. */
1311 return errorcount
|| sorrycount
;
1314 /* An error which is severe enough that we make no attempt to
1315 continue. Do not use this for internal consistency checks; that's
1316 internal_error. Use of this function should be rare. */
1318 fatal_error (location_t loc
, const char *gmsgid
, ...)
1321 va_start (ap
, gmsgid
);
1322 rich_location
richloc (line_table
, loc
);
1323 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_FATAL
);
1329 /* An internal consistency check has failed. We make no attempt to
1330 continue. Note that unless there is debugging value to be had from
1331 a more specific message, or some other good reason, you should use
1332 abort () instead of calling this function directly. */
1334 internal_error (const char *gmsgid
, ...)
1337 va_start (ap
, gmsgid
);
1338 rich_location
richloc (line_table
, input_location
);
1339 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_ICE
);
1345 /* Like internal_error, but no backtrace will be printed. Used when
1346 the internal error does not happen at the current location, but happened
1349 internal_error_no_backtrace (const char *gmsgid
, ...)
1352 va_start (ap
, gmsgid
);
1353 rich_location
richloc (line_table
, input_location
);
1354 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_ICE_NOBT
);
1360 /* Special case error functions. Most are implemented in terms of the
1361 above, or should be. */
1363 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1364 runs its second argument through gettext. */
1366 fnotice (FILE *file
, const char *cmsgid
, ...)
1370 va_start (ap
, cmsgid
);
1371 vfprintf (file
, _(cmsgid
), ap
);
1375 /* Inform the user that an error occurred while trying to report some
1376 other error. This indicates catastrophic internal inconsistencies,
1377 so give up now. But do try to flush out the previous error.
1378 This mustn't use internal_error, that will cause infinite recursion. */
1381 error_recursion (diagnostic_context
*context
)
1383 if (context
->lock
< 3)
1384 pp_newline_and_flush (context
->printer
);
1387 "Internal compiler error: Error reporting routines re-entered.\n");
1389 /* Call diagnostic_action_after_output to get the "please submit a bug
1391 diagnostic_action_after_output (context
, DK_ICE
);
1393 /* Do not use gcc_unreachable here; that goes through internal_error
1394 and therefore would cause infinite recursion. */
1398 /* Report an internal compiler error in a friendly manner. This is
1399 the function that gets called upon use of abort() in the source
1400 code generally, thanks to a special macro. */
1403 fancy_abort (const char *file
, int line
, const char *function
)
1405 internal_error ("in %s, at %s:%d", function
, trim_filename (file
), line
);
1408 /* Really call the system 'abort'. This has to go right at the end of
1409 this file, so that there are no functions after it that call abort
1410 and get the system abort instead of our macro. */
1420 namespace selftest
{
1422 /* Helper function for test_print_escaped_string. */
1425 assert_print_escaped_string (const location
&loc
, const char *expected_output
,
1429 print_escaped_string (&pp
, input
);
1430 ASSERT_STREQ_AT (loc
, expected_output
, pp_formatted_text (&pp
));
1433 #define ASSERT_PRINT_ESCAPED_STRING_STREQ(EXPECTED_OUTPUT, INPUT) \
1434 assert_print_escaped_string (SELFTEST_LOCATION, EXPECTED_OUTPUT, INPUT)
1436 /* Tests of print_escaped_string. */
1439 test_print_escaped_string ()
1442 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"\"", "");
1444 /* Non-empty string. */
1445 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"hello world\"", "hello world");
1447 /* Various things that need to be escaped: */
1449 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\\after\"",
1452 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\tafter\"",
1455 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\nafter\"",
1458 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\"after\"",
1461 /* Non-printable characters: BEL: '\a': 0x07 */
1462 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\007after\"",
1464 /* Non-printable characters: vertical tab: '\v': 0x0b */
1465 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\013after\"",
1469 /* Tests of print_parseable_fixits. */
1471 /* Verify that print_parseable_fixits emits the empty string if there
1475 test_print_parseable_fixits_none ()
1478 rich_location
richloc (line_table
, UNKNOWN_LOCATION
);
1480 print_parseable_fixits (&pp
, &richloc
);
1481 ASSERT_STREQ ("", pp_formatted_text (&pp
));
1484 /* Verify that print_parseable_fixits does the right thing if there
1485 is an insertion fixit hint. */
1488 test_print_parseable_fixits_insert ()
1491 rich_location
richloc (line_table
, UNKNOWN_LOCATION
);
1493 linemap_add (line_table
, LC_ENTER
, false, "test.c", 0);
1494 linemap_line_start (line_table
, 5, 100);
1495 linemap_add (line_table
, LC_LEAVE
, false, NULL
, 0);
1496 location_t where
= linemap_position_for_column (line_table
, 10);
1497 richloc
.add_fixit_insert (where
, "added content");
1499 print_parseable_fixits (&pp
, &richloc
);
1500 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:10}:\"added content\"\n",
1501 pp_formatted_text (&pp
));
1504 /* Verify that print_parseable_fixits does the right thing if there
1505 is an removal fixit hint. */
1508 test_print_parseable_fixits_remove ()
1511 rich_location
richloc (line_table
, UNKNOWN_LOCATION
);
1513 linemap_add (line_table
, LC_ENTER
, false, "test.c", 0);
1514 linemap_line_start (line_table
, 5, 100);
1515 linemap_add (line_table
, LC_LEAVE
, false, NULL
, 0);
1517 where
.m_start
= linemap_position_for_column (line_table
, 10);
1518 where
.m_finish
= linemap_position_for_column (line_table
, 20);
1519 richloc
.add_fixit_remove (where
);
1521 print_parseable_fixits (&pp
, &richloc
);
1522 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"\"\n",
1523 pp_formatted_text (&pp
));
1526 /* Verify that print_parseable_fixits does the right thing if there
1527 is an replacement fixit hint. */
1530 test_print_parseable_fixits_replace ()
1533 rich_location
richloc (line_table
, UNKNOWN_LOCATION
);
1535 linemap_add (line_table
, LC_ENTER
, false, "test.c", 0);
1536 linemap_line_start (line_table
, 5, 100);
1537 linemap_add (line_table
, LC_LEAVE
, false, NULL
, 0);
1539 where
.m_start
= linemap_position_for_column (line_table
, 10);
1540 where
.m_finish
= linemap_position_for_column (line_table
, 20);
1541 richloc
.add_fixit_replace (where
, "replacement");
1543 print_parseable_fixits (&pp
, &richloc
);
1544 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"replacement\"\n",
1545 pp_formatted_text (&pp
));
1548 /* Run all of the selftests within this file. */
1551 diagnostic_c_tests ()
1553 test_print_escaped_string ();
1554 test_print_parseable_fixits_none ();
1555 test_print_parseable_fixits_insert ();
1556 test_print_parseable_fixits_remove ();
1557 test_print_parseable_fixits_replace ();
1560 } // namespace selftest
1562 #endif /* #if CHECKING_P */