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"
34 #include "edit-context.h"
41 #ifdef GWINSZ_IN_SYS_IOCTL
42 # include <sys/ioctl.h>
45 #define pedantic_warning_kind(DC) \
46 ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
47 #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR)
48 #define permissive_error_option(DC) ((DC)->opt_permissive)
51 static bool diagnostic_impl (rich_location
*, int, const char *,
52 va_list *, diagnostic_t
) ATTRIBUTE_GCC_DIAG(3,0);
53 static bool diagnostic_n_impl (location_t
, int, int, const char *,
54 const char *, va_list *,
55 diagnostic_t
) ATTRIBUTE_GCC_DIAG(5,0);
56 static void error_recursion (diagnostic_context
*) ATTRIBUTE_NORETURN
;
57 static void real_abort (void) ATTRIBUTE_NORETURN
;
59 /* Name of program invoked, sans directories. */
63 /* A diagnostic_context surrogate for stderr. */
64 static diagnostic_context global_diagnostic_context
;
65 diagnostic_context
*global_dc
= &global_diagnostic_context
;
67 /* Return a malloc'd string containing MSG formatted a la printf. The
68 caller is responsible for freeing the memory. */
70 build_message_string (const char *msg
, ...)
76 str
= xvasprintf (msg
, ap
);
82 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
84 file_name_as_prefix (diagnostic_context
*context
, const char *f
)
87 = colorize_start (pp_show_color (context
->printer
), "locus");
88 const char *locus_ce
= colorize_stop (pp_show_color (context
->printer
));
89 return build_message_string ("%s%s:%s ", locus_cs
, f
, locus_ce
);
94 /* Return the value of the getenv("COLUMNS") as an integer. If the
95 value is not set to a positive integer, use ioctl to get the
96 terminal width. If it fails, return INT_MAX. */
98 get_terminal_width (void)
100 const char * s
= getenv ("COLUMNS");
110 if (ioctl (0, TIOCGWINSZ
, &w
) == 0 && w
.ws_col
> 0)
117 /* Set caret_max_width to value. */
119 diagnostic_set_caret_max_width (diagnostic_context
*context
, int value
)
121 /* One minus to account for the leading empty space. */
122 value
= value
? value
- 1
123 : (isatty (fileno (pp_buffer (context
->printer
)->stream
))
124 ? get_terminal_width () - 1: INT_MAX
);
129 context
->caret_max_width
= value
;
132 /* Initialize the diagnostic message outputting machinery. */
134 diagnostic_initialize (diagnostic_context
*context
, int n_opts
)
138 /* Allocate a basic pretty-printer. Clients will replace this a
139 much more elaborated pretty-printer if they wish. */
140 context
->printer
= XNEW (pretty_printer
);
141 new (context
->printer
) pretty_printer ();
143 memset (context
->diagnostic_count
, 0, sizeof context
->diagnostic_count
);
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 for (i
= 0; i
< rich_location::STATICALLY_ALLOCATED_RANGES
; i
++)
152 context
->caret_chars
[i
] = '^';
153 context
->show_option_requested
= false;
154 context
->abort_on_error
= false;
155 context
->show_column
= false;
156 context
->pedantic_errors
= false;
157 context
->permissive
= false;
158 context
->opt_permissive
= 0;
159 context
->fatal_errors
= false;
160 context
->dc_inhibit_warnings
= false;
161 context
->dc_warn_system_headers
= false;
162 context
->max_errors
= 0;
163 context
->internal_error
= NULL
;
164 diagnostic_starter (context
) = default_diagnostic_starter
;
165 context
->start_span
= default_diagnostic_start_span_fn
;
166 diagnostic_finalizer (context
) = default_diagnostic_finalizer
;
167 context
->option_enabled
= NULL
;
168 context
->option_state
= NULL
;
169 context
->option_name
= NULL
;
170 context
->last_location
= UNKNOWN_LOCATION
;
171 context
->last_module
= 0;
172 context
->x_data
= NULL
;
174 context
->inhibit_notes_p
= false;
175 context
->colorize_source_p
= false;
176 context
->show_ruler_p
= false;
177 context
->parseable_fixits_p
= false;
178 context
->edit_context_ptr
= NULL
;
181 /* Maybe initialize the color support. We require clients to do this
182 explicitly, since most clients don't want color. When called
183 without a VALUE, it initializes with DIAGNOSTICS_COLOR_DEFAULT. */
186 diagnostic_color_init (diagnostic_context
*context
, int value
/*= -1 */)
188 /* value == -1 is the default value. */
191 /* If DIAGNOSTICS_COLOR_DEFAULT is -1, default to
192 -fdiagnostics-color=auto if GCC_COLORS is in the environment,
193 otherwise default to -fdiagnostics-color=never, for other
194 values default to that
195 -fdiagnostics-color={never,auto,always}. */
196 if (DIAGNOSTICS_COLOR_DEFAULT
== -1)
198 if (!getenv ("GCC_COLORS"))
200 value
= DIAGNOSTICS_COLOR_AUTO
;
203 value
= DIAGNOSTICS_COLOR_DEFAULT
;
205 pp_show_color (context
->printer
)
206 = colorize_init ((diagnostic_color_rule_t
) value
);
209 /* Do any cleaning up required after the last diagnostic is emitted. */
212 diagnostic_finish (diagnostic_context
*context
)
214 /* Some of the errors may actually have been warnings. */
215 if (diagnostic_kind_count (context
, DK_WERROR
))
217 /* -Werror was given. */
218 if (context
->warning_as_error_requested
)
219 pp_verbatim (context
->printer
,
220 _("%s: all warnings being treated as errors"),
222 /* At least one -Werror= was given. */
224 pp_verbatim (context
->printer
,
225 _("%s: some warnings being treated as errors"),
227 pp_newline_and_flush (context
->printer
);
230 diagnostic_file_cache_fini ();
232 XDELETEVEC (context
->classify_diagnostic
);
233 context
->classify_diagnostic
= NULL
;
235 /* diagnostic_initialize allocates context->printer using XNEW
236 and placement-new. */
237 context
->printer
->~pretty_printer ();
238 XDELETE (context
->printer
);
239 context
->printer
= NULL
;
241 if (context
->edit_context_ptr
)
243 delete context
->edit_context_ptr
;
244 context
->edit_context_ptr
= NULL
;
248 /* Initialize DIAGNOSTIC, where the message MSG has already been
251 diagnostic_set_info_translated (diagnostic_info
*diagnostic
, const char *msg
,
252 va_list *args
, rich_location
*richloc
,
255 gcc_assert (richloc
);
256 diagnostic
->message
.err_no
= errno
;
257 diagnostic
->message
.args_ptr
= args
;
258 diagnostic
->message
.format_spec
= msg
;
259 diagnostic
->message
.m_richloc
= richloc
;
260 diagnostic
->richloc
= richloc
;
261 diagnostic
->kind
= kind
;
262 diagnostic
->option_index
= 0;
265 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
268 diagnostic_set_info (diagnostic_info
*diagnostic
, const char *gmsgid
,
269 va_list *args
, rich_location
*richloc
,
272 gcc_assert (richloc
);
273 diagnostic_set_info_translated (diagnostic
, _(gmsgid
), args
, richloc
, kind
);
276 static const char *const diagnostic_kind_color
[] = {
277 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
278 #include "diagnostic.def"
279 #undef DEFINE_DIAGNOSTIC_KIND
283 /* Get a color name for diagnostics of type KIND
284 Result could be NULL. */
287 diagnostic_get_color_for_kind (diagnostic_t kind
)
289 return diagnostic_kind_color
[kind
];
292 /* Return a malloc'd string describing a location e.g. "foo.c:42:10".
293 The caller is responsible for freeing the memory. */
296 diagnostic_get_location_text (diagnostic_context
*context
,
299 pretty_printer
*pp
= context
->printer
;
300 const char *locus_cs
= colorize_start (pp_show_color (pp
), "locus");
301 const char *locus_ce
= colorize_stop (pp_show_color (pp
));
304 return build_message_string ("%s%s:%s", locus_cs
, progname
, locus_ce
);
306 if (!strcmp (s
.file
, N_("<built-in>")))
307 return build_message_string ("%s%s:%s", locus_cs
, s
.file
, locus_ce
);
309 if (context
->show_column
)
310 return build_message_string ("%s%s:%d:%d:%s", locus_cs
, s
.file
, s
.line
,
313 return build_message_string ("%s%s:%d:%s", locus_cs
, s
.file
, s
.line
,
317 /* Return a malloc'd string describing a location and the severity of the
318 diagnostic, e.g. "foo.c:42:10: error: ". The caller is responsible for
319 freeing the memory. */
321 diagnostic_build_prefix (diagnostic_context
*context
,
322 const diagnostic_info
*diagnostic
)
324 static const char *const diagnostic_kind_text
[] = {
325 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
326 #include "diagnostic.def"
327 #undef DEFINE_DIAGNOSTIC_KIND
330 gcc_assert (diagnostic
->kind
< DK_LAST_DIAGNOSTIC_KIND
);
332 const char *text
= _(diagnostic_kind_text
[diagnostic
->kind
]);
333 const char *text_cs
= "", *text_ce
= "";
334 pretty_printer
*pp
= context
->printer
;
336 if (diagnostic_kind_color
[diagnostic
->kind
])
338 text_cs
= colorize_start (pp_show_color (pp
),
339 diagnostic_kind_color
[diagnostic
->kind
]);
340 text_ce
= colorize_stop (pp_show_color (pp
));
343 expanded_location s
= diagnostic_expand_location (diagnostic
);
344 char *location_text
= diagnostic_get_location_text (context
, s
);
346 char *result
= build_message_string ("%s %s%s%s", location_text
,
347 text_cs
, text
, text_ce
);
348 free (location_text
);
352 /* Functions at which to stop the backtrace print. It's not
353 particularly helpful to print the callers of these functions. */
355 static const char * const bt_stop
[] =
363 /* A callback function passed to the backtrace_full function. */
366 bt_callback (void *data
, uintptr_t pc
, const char *filename
, int lineno
,
367 const char *function
)
369 int *pcount
= (int *) data
;
371 /* If we don't have any useful information, don't print
373 if (filename
== NULL
&& function
== NULL
)
376 /* Skip functions in diagnostic.c. */
379 && strcmp (lbasename (filename
), "diagnostic.c") == 0)
382 /* Print up to 20 functions. We could make this a --param, but
383 since this is only for debugging just use a constant for now. */
386 /* Returning a non-zero value stops the backtrace. */
392 if (function
!= NULL
)
394 char *str
= cplus_demangle_v3 (function
,
395 (DMGL_VERBOSE
| DMGL_ANSI
396 | DMGL_GNU_V3
| DMGL_PARAMS
));
403 for (size_t i
= 0; i
< ARRAY_SIZE (bt_stop
); ++i
)
405 size_t len
= strlen (bt_stop
[i
]);
406 if (strncmp (function
, bt_stop
[i
], len
) == 0
407 && (function
[len
] == '\0' || function
[len
] == '('))
411 /* Returning a non-zero value stops the backtrace. */
417 fprintf (stderr
, "0x%lx %s\n\t%s:%d\n",
419 function
== NULL
? "???" : function
,
420 filename
== NULL
? "???" : filename
,
429 /* A callback function passed to the backtrace_full function. This is
430 called if backtrace_full has an error. */
433 bt_err_callback (void *data ATTRIBUTE_UNUSED
, const char *msg
, int errnum
)
437 /* This means that no debug info was available. Just quietly
438 skip printing backtrace info. */
441 fprintf (stderr
, "%s%s%s\n", msg
, errnum
== 0 ? "" : ": ",
442 errnum
== 0 ? "" : xstrerror (errnum
));
445 /* Take any action which is expected to happen after the diagnostic
446 is written out. This function does not always return. */
448 diagnostic_action_after_output (diagnostic_context
*context
,
449 diagnostic_t diag_kind
)
461 if (context
->abort_on_error
)
463 if (context
->fatal_errors
)
465 fnotice (stderr
, "compilation terminated due to -Wfatal-errors.\n");
466 diagnostic_finish (context
);
467 exit (FATAL_EXIT_CODE
);
469 if (context
->max_errors
!= 0
470 && ((unsigned) (diagnostic_kind_count (context
, DK_ERROR
)
471 + diagnostic_kind_count (context
, DK_SORRY
)
472 + diagnostic_kind_count (context
, DK_WERROR
))
473 >= context
->max_errors
))
476 "compilation terminated due to -fmax-errors=%u.\n",
477 context
->max_errors
);
478 diagnostic_finish (context
);
479 exit (FATAL_EXIT_CODE
);
486 struct backtrace_state
*state
= NULL
;
487 if (diag_kind
== DK_ICE
)
488 state
= backtrace_create_state (NULL
, 0, bt_err_callback
, NULL
);
491 backtrace_full (state
, 2, bt_callback
, bt_err_callback
,
494 if (context
->abort_on_error
)
497 fnotice (stderr
, "Please submit a full bug report,\n"
498 "with preprocessed source if appropriate.\n");
501 ("Please include the complete backtrace "
502 "with any bug report.\n"));
503 fnotice (stderr
, "See %s for instructions.\n", bug_report_url
);
505 exit (ICE_EXIT_CODE
);
509 if (context
->abort_on_error
)
511 diagnostic_finish (context
);
512 fnotice (stderr
, "compilation terminated.\n");
513 exit (FATAL_EXIT_CODE
);
521 diagnostic_report_current_module (diagnostic_context
*context
, location_t where
)
523 const line_map_ordinary
*map
= NULL
;
525 if (pp_needs_newline (context
->printer
))
527 pp_newline (context
->printer
);
528 pp_needs_newline (context
->printer
) = false;
531 if (where
<= BUILTINS_LOCATION
)
534 linemap_resolve_location (line_table
, where
,
535 LRK_MACRO_DEFINITION_LOCATION
,
538 if (map
&& diagnostic_last_module_changed (context
, map
))
540 diagnostic_set_last_module (context
, map
);
541 if (! MAIN_FILE_P (map
))
543 map
= INCLUDED_FROM (line_table
, map
);
544 if (context
->show_column
)
545 pp_verbatim (context
->printer
,
546 "In file included from %r%s:%d:%d%R", "locus",
548 LAST_SOURCE_LINE (map
), LAST_SOURCE_COLUMN (map
));
550 pp_verbatim (context
->printer
,
551 "In file included from %r%s:%d%R", "locus",
552 LINEMAP_FILE (map
), LAST_SOURCE_LINE (map
));
553 while (! MAIN_FILE_P (map
))
555 map
= INCLUDED_FROM (line_table
, map
);
556 pp_verbatim (context
->printer
,
557 ",\n from %r%s:%d%R", "locus",
558 LINEMAP_FILE (map
), LAST_SOURCE_LINE (map
));
560 pp_verbatim (context
->printer
, ":");
561 pp_newline (context
->printer
);
567 default_diagnostic_starter (diagnostic_context
*context
,
568 diagnostic_info
*diagnostic
)
570 diagnostic_report_current_module (context
, diagnostic_location (diagnostic
));
571 pp_set_prefix (context
->printer
, diagnostic_build_prefix (context
,
576 default_diagnostic_start_span_fn (diagnostic_context
*context
,
577 expanded_location exploc
)
579 pp_set_prefix (context
->printer
,
580 diagnostic_get_location_text (context
, exploc
));
581 pp_string (context
->printer
, "");
582 pp_newline (context
->printer
);
586 default_diagnostic_finalizer (diagnostic_context
*context
,
587 diagnostic_info
*diagnostic
)
589 diagnostic_show_locus (context
, diagnostic
->richloc
, diagnostic
->kind
);
590 pp_destroy_prefix (context
->printer
);
591 pp_flush (context
->printer
);
594 /* Interface to specify diagnostic kind overrides. Returns the
595 previous setting, or DK_UNSPECIFIED if the parameters are out of
596 range. If OPTION_INDEX is zero, the new setting is for all the
599 diagnostic_classify_diagnostic (diagnostic_context
*context
,
601 diagnostic_t new_kind
,
604 diagnostic_t old_kind
;
607 || option_index
>= context
->n_opts
608 || new_kind
>= DK_LAST_DIAGNOSTIC_KIND
)
609 return DK_UNSPECIFIED
;
611 old_kind
= context
->classify_diagnostic
[option_index
];
613 /* Handle pragmas separately, since we need to keep track of *where*
615 if (where
!= UNKNOWN_LOCATION
)
619 /* Record the command-line status, so we can reset it back on DK_POP. */
620 if (old_kind
== DK_UNSPECIFIED
)
622 old_kind
= !context
->option_enabled (option_index
,
623 context
->option_state
)
624 ? DK_IGNORED
: (context
->warning_as_error_requested
625 ? DK_ERROR
: DK_WARNING
);
626 context
->classify_diagnostic
[option_index
] = old_kind
;
629 for (i
= context
->n_classification_history
- 1; i
>= 0; i
--)
630 if (context
->classification_history
[i
].option
== option_index
)
632 old_kind
= context
->classification_history
[i
].kind
;
636 i
= context
->n_classification_history
;
637 context
->classification_history
=
638 (diagnostic_classification_change_t
*) xrealloc (context
->classification_history
, (i
+ 1)
639 * sizeof (diagnostic_classification_change_t
));
640 context
->classification_history
[i
].location
= where
;
641 context
->classification_history
[i
].option
= option_index
;
642 context
->classification_history
[i
].kind
= new_kind
;
643 context
->n_classification_history
++;
646 context
->classify_diagnostic
[option_index
] = new_kind
;
651 /* Save all diagnostic classifications in a stack. */
653 diagnostic_push_diagnostics (diagnostic_context
*context
, location_t where ATTRIBUTE_UNUSED
)
655 context
->push_list
= (int *) xrealloc (context
->push_list
, (context
->n_push
+ 1) * sizeof (int));
656 context
->push_list
[context
->n_push
++] = context
->n_classification_history
;
659 /* Restore the topmost classification set off the stack. If the stack
660 is empty, revert to the state based on command line parameters. */
662 diagnostic_pop_diagnostics (diagnostic_context
*context
, location_t where
)
668 jump_to
= context
->push_list
[-- context
->n_push
];
672 i
= context
->n_classification_history
;
673 context
->classification_history
=
674 (diagnostic_classification_change_t
*) xrealloc (context
->classification_history
, (i
+ 1)
675 * sizeof (diagnostic_classification_change_t
));
676 context
->classification_history
[i
].location
= where
;
677 context
->classification_history
[i
].option
= jump_to
;
678 context
->classification_history
[i
].kind
= DK_POP
;
679 context
->n_classification_history
++;
682 /* Helper function for print_parseable_fixits. Print TEXT to PP, obeying the
683 escaping rules for -fdiagnostics-parseable-fixits. */
686 print_escaped_string (pretty_printer
*pp
, const char *text
)
691 pp_character (pp
, '"');
692 for (const char *ch
= text
; *ch
; ch
++)
697 /* Escape backslash as two backslashes. */
698 pp_string (pp
, "\\\\");
701 /* Escape tab as "\t". */
702 pp_string (pp
, "\\t");
705 /* Escape newline as "\n". */
706 pp_string (pp
, "\\n");
709 /* Escape doublequotes as \". */
710 pp_string (pp
, "\\\"");
714 pp_character (pp
, *ch
);
716 /* Use octal for non-printable chars. */
718 unsigned char c
= (*ch
& 0xff);
719 pp_printf (pp
, "\\%o%o%o", (c
/ 64), (c
/ 8) & 007, c
& 007);
724 pp_character (pp
, '"');
727 /* Implementation of -fdiagnostics-parseable-fixits. Print a
728 machine-parseable version of all fixits in RICHLOC to PP. */
731 print_parseable_fixits (pretty_printer
*pp
, rich_location
*richloc
)
734 gcc_assert (richloc
);
736 for (unsigned i
= 0; i
< richloc
->get_num_fixit_hints (); i
++)
738 const fixit_hint
*hint
= richloc
->get_fixit_hint (i
);
739 source_location start_loc
= hint
->get_start_loc ();
740 expanded_location start_exploc
= expand_location (start_loc
);
741 pp_string (pp
, "fix-it:");
742 print_escaped_string (pp
, start_exploc
.file
);
743 source_location end_loc
;
745 /* For compatibility with clang, print as a half-open range. */
746 if (hint
->maybe_get_end_loc (&end_loc
))
748 expanded_location end_exploc
= expand_location (end_loc
);
749 pp_printf (pp
, ":{%i:%i-%i:%i}:",
750 start_exploc
.line
, start_exploc
.column
,
751 end_exploc
.line
, end_exploc
.column
+ 1);
755 pp_printf (pp
, ":{%i:%i-%i:%i}:",
756 start_exploc
.line
, start_exploc
.column
,
757 start_exploc
.line
, start_exploc
.column
);
759 switch (hint
->get_kind ())
761 case fixit_hint::INSERT
:
763 const fixit_insert
*insert
764 = static_cast <const fixit_insert
*> (hint
);
765 print_escaped_string (pp
, insert
->get_string ());
769 case fixit_hint::REPLACE
:
771 const fixit_replace
*replace
772 = static_cast <const fixit_replace
*> (hint
);
773 print_escaped_string (pp
, replace
->get_string ());
784 /* Report a diagnostic message (an error or a warning) as specified by
785 DC. This function is *the* subroutine in terms of which front-ends
786 should implement their specific diagnostic handling modules. The
787 front-end independent format specifiers are exactly those described
788 in the documentation of output_format.
789 Return true if a diagnostic was printed, false otherwise. */
792 diagnostic_report_diagnostic (diagnostic_context
*context
,
793 diagnostic_info
*diagnostic
)
795 location_t location
= diagnostic_location (diagnostic
);
796 diagnostic_t orig_diag_kind
= diagnostic
->kind
;
797 const char *saved_format_spec
;
799 /* Give preference to being able to inhibit warnings, before they
800 get reclassified to something else. */
801 if ((diagnostic
->kind
== DK_WARNING
|| diagnostic
->kind
== DK_PEDWARN
)
802 && !diagnostic_report_warnings_p (context
, location
))
805 if (diagnostic
->kind
== DK_PEDWARN
)
807 diagnostic
->kind
= pedantic_warning_kind (context
);
808 /* We do this to avoid giving the message for -pedantic-errors. */
809 orig_diag_kind
= diagnostic
->kind
;
812 if (diagnostic
->kind
== DK_NOTE
&& context
->inhibit_notes_p
)
815 if (context
->lock
> 0)
817 /* If we're reporting an ICE in the middle of some other error,
818 try to flush out the previous error, then let this one
819 through. Don't do this more than once. */
820 if ((diagnostic
->kind
== DK_ICE
|| diagnostic
->kind
== DK_ICE_NOBT
)
821 && context
->lock
== 1)
822 pp_newline_and_flush (context
->printer
);
824 error_recursion (context
);
827 /* If the user requested that warnings be treated as errors, so be
828 it. Note that we do this before the next block so that
829 individual warnings can be overridden back to warnings with
831 if (context
->warning_as_error_requested
832 && diagnostic
->kind
== DK_WARNING
)
834 diagnostic
->kind
= DK_ERROR
;
837 if (diagnostic
->option_index
838 && diagnostic
->option_index
!= permissive_error_option (context
))
840 diagnostic_t diag_class
= DK_UNSPECIFIED
;
842 /* This tests if the user provided the appropriate -Wfoo or
844 if (! context
->option_enabled (diagnostic
->option_index
,
845 context
->option_state
))
848 /* This tests for #pragma diagnostic changes. */
849 if (context
->n_classification_history
> 0)
851 /* FIXME: Stupid search. Optimize later. */
852 for (int i
= context
->n_classification_history
- 1; i
>= 0; i
--)
854 if (linemap_location_before_p
856 context
->classification_history
[i
].location
,
859 if (context
->classification_history
[i
].kind
== (int) DK_POP
)
861 i
= context
->classification_history
[i
].option
;
864 int option
= context
->classification_history
[i
].option
;
865 /* The option 0 is for all the diagnostics. */
866 if (option
== 0 || option
== diagnostic
->option_index
)
868 diag_class
= context
->classification_history
[i
].kind
;
869 if (diag_class
!= DK_UNSPECIFIED
)
870 diagnostic
->kind
= diag_class
;
876 /* This tests if the user provided the appropriate -Werror=foo
878 if (diag_class
== DK_UNSPECIFIED
879 && context
->classify_diagnostic
[diagnostic
->option_index
] != DK_UNSPECIFIED
)
881 diagnostic
->kind
= context
->classify_diagnostic
[diagnostic
->option_index
];
883 /* This allows for future extensions, like temporarily disabling
884 warnings for ranges of source code. */
885 if (diagnostic
->kind
== DK_IGNORED
)
891 if (diagnostic
->kind
== DK_ICE
|| diagnostic
->kind
== DK_ICE_NOBT
)
893 /* When not checking, ICEs are converted to fatal errors when an
894 error has already occurred. This is counteracted by
897 && (diagnostic_kind_count (context
, DK_ERROR
) > 0
898 || diagnostic_kind_count (context
, DK_SORRY
) > 0)
899 && !context
->abort_on_error
)
902 = expand_location (diagnostic_location (diagnostic
));
903 fnotice (stderr
, "%s:%d: confused by earlier errors, bailing out\n",
905 exit (ICE_EXIT_CODE
);
907 if (context
->internal_error
)
908 (*context
->internal_error
) (context
,
909 diagnostic
->message
.format_spec
,
910 diagnostic
->message
.args_ptr
);
912 if (diagnostic
->kind
== DK_ERROR
&& orig_diag_kind
== DK_WARNING
)
913 ++diagnostic_kind_count (context
, DK_WERROR
);
915 ++diagnostic_kind_count (context
, diagnostic
->kind
);
917 saved_format_spec
= diagnostic
->message
.format_spec
;
918 if (context
->show_option_requested
)
922 option_text
= context
->option_name (context
, diagnostic
->option_index
,
923 orig_diag_kind
, diagnostic
->kind
);
928 = colorize_start (pp_show_color (context
->printer
),
929 diagnostic_kind_color
[diagnostic
->kind
]);
930 const char *ce
= colorize_stop (pp_show_color (context
->printer
));
931 diagnostic
->message
.format_spec
932 = ACONCAT ((diagnostic
->message
.format_spec
,
934 "[", cs
, option_text
, ce
, "]",
939 diagnostic
->message
.x_data
= &diagnostic
->x_data
;
940 diagnostic
->x_data
= NULL
;
941 pp_format (context
->printer
, &diagnostic
->message
);
942 (*diagnostic_starter (context
)) (context
, diagnostic
);
943 pp_output_formatted_text (context
->printer
);
944 (*diagnostic_finalizer (context
)) (context
, diagnostic
);
945 if (context
->parseable_fixits_p
)
947 print_parseable_fixits (context
->printer
, diagnostic
->richloc
);
948 pp_flush (context
->printer
);
950 diagnostic_action_after_output (context
, diagnostic
->kind
);
951 diagnostic
->message
.format_spec
= saved_format_spec
;
952 diagnostic
->x_data
= NULL
;
954 if (context
->edit_context_ptr
)
955 context
->edit_context_ptr
->add_fixits (diagnostic
->richloc
);
962 /* Given a partial pathname as input, return another pathname that
963 shares no directory elements with the pathname of __FILE__. This
964 is used by fancy_abort() to print `Internal compiler error in expr.c'
965 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
968 trim_filename (const char *name
)
970 static const char this_file
[] = __FILE__
;
971 const char *p
= name
, *q
= this_file
;
973 /* First skip any "../" in each filename. This allows us to give a proper
974 reference to a file in a subdirectory. */
975 while (p
[0] == '.' && p
[1] == '.' && IS_DIR_SEPARATOR (p
[2]))
978 while (q
[0] == '.' && q
[1] == '.' && IS_DIR_SEPARATOR (q
[2]))
981 /* Now skip any parts the two filenames have in common. */
982 while (*p
== *q
&& *p
!= 0 && *q
!= 0)
985 /* Now go backwards until the previous directory separator. */
986 while (p
> name
&& !IS_DIR_SEPARATOR (p
[-1]))
992 /* Standard error reporting routines in increasing order of severity.
993 All of these take arguments like printf. */
995 /* Text to be emitted verbatim to the error message stream; this
996 produces no prefix and disables line-wrapping. Use rarely. */
998 verbatim (const char *gmsgid
, ...)
1003 va_start (ap
, gmsgid
);
1004 text
.err_no
= errno
;
1005 text
.args_ptr
= &ap
;
1006 text
.format_spec
= _(gmsgid
);
1008 pp_format_verbatim (global_dc
->printer
, &text
);
1009 pp_newline_and_flush (global_dc
->printer
);
1013 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
1015 diagnostic_append_note (diagnostic_context
*context
,
1016 location_t location
,
1017 const char * gmsgid
, ...)
1019 diagnostic_info diagnostic
;
1021 const char *saved_prefix
;
1022 rich_location
richloc (line_table
, location
);
1024 va_start (ap
, gmsgid
);
1025 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_NOTE
);
1026 if (context
->inhibit_notes_p
)
1031 saved_prefix
= pp_get_prefix (context
->printer
);
1032 pp_set_prefix (context
->printer
,
1033 diagnostic_build_prefix (context
, &diagnostic
));
1034 pp_format (context
->printer
, &diagnostic
.message
);
1035 pp_output_formatted_text (context
->printer
);
1036 pp_destroy_prefix (context
->printer
);
1037 pp_set_prefix (context
->printer
, saved_prefix
);
1038 diagnostic_show_locus (context
, &richloc
, DK_NOTE
);
1042 /* Implement emit_diagnostic, inform, inform_at_rich_loc, warning, warning_at,
1043 warning_at_rich_loc, pedwarn, permerror, permerror_at_rich_loc, error,
1044 error_at, error_at_rich_loc, sorry, fatal_error, internal_error, and
1045 internal_error_no_backtrace, as documented and defined below. */
1047 diagnostic_impl (rich_location
*richloc
, int opt
,
1049 va_list *ap
, diagnostic_t kind
)
1051 diagnostic_info diagnostic
;
1052 if (kind
== DK_PERMERROR
)
1054 diagnostic_set_info (&diagnostic
, gmsgid
, ap
, richloc
,
1055 permissive_error_kind (global_dc
));
1056 diagnostic
.option_index
= permissive_error_option (global_dc
);
1060 diagnostic_set_info (&diagnostic
, gmsgid
, ap
, richloc
, kind
);
1061 if (kind
== DK_WARNING
|| kind
== DK_PEDWARN
)
1062 diagnostic
.option_index
= opt
;
1064 return report_diagnostic (&diagnostic
);
1067 /* Implement inform_n, warning_n, and error_n, as documented and
1070 diagnostic_n_impl (location_t location
, int opt
, int n
,
1071 const char *singular_gmsgid
,
1072 const char *plural_gmsgid
,
1073 va_list *ap
, diagnostic_t kind
)
1075 diagnostic_info diagnostic
;
1076 rich_location
richloc (line_table
, location
);
1077 diagnostic_set_info_translated (&diagnostic
,
1078 ngettext (singular_gmsgid
, plural_gmsgid
, n
),
1079 ap
, &richloc
, kind
);
1080 if (kind
== DK_WARNING
)
1081 diagnostic
.option_index
= opt
;
1082 return report_diagnostic (&diagnostic
);
1086 emit_diagnostic (diagnostic_t kind
, location_t location
, int opt
,
1087 const char *gmsgid
, ...)
1090 va_start (ap
, gmsgid
);
1091 rich_location
richloc (line_table
, location
);
1092 bool ret
= diagnostic_impl (&richloc
, opt
, gmsgid
, &ap
, kind
);
1097 /* An informative note at LOCATION. Use this for additional details on an error
1100 inform (location_t location
, const char *gmsgid
, ...)
1103 va_start (ap
, gmsgid
);
1104 rich_location
richloc (line_table
, location
);
1105 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_NOTE
);
1109 /* Same as "inform", but at RICHLOC. */
1111 inform_at_rich_loc (rich_location
*richloc
, const char *gmsgid
, ...)
1114 va_start (ap
, gmsgid
);
1115 diagnostic_impl (richloc
, -1, gmsgid
, &ap
, DK_NOTE
);
1119 /* An informative note at LOCATION. Use this for additional details on an
1122 inform_n (location_t location
, int n
, const char *singular_gmsgid
,
1123 const char *plural_gmsgid
, ...)
1126 va_start (ap
, plural_gmsgid
);
1127 diagnostic_n_impl (location
, -1, n
, singular_gmsgid
, plural_gmsgid
,
1132 /* A warning at INPUT_LOCATION. Use this for code which is correct according
1133 to the relevant language specification but is likely to be buggy anyway.
1134 Returns true if the warning was printed, false if it was inhibited. */
1136 warning (int opt
, const char *gmsgid
, ...)
1139 va_start (ap
, gmsgid
);
1140 rich_location
richloc (line_table
, input_location
);
1141 bool ret
= diagnostic_impl (&richloc
, opt
, gmsgid
, &ap
, DK_WARNING
);
1146 /* A warning at LOCATION. Use this for code which is correct according to the
1147 relevant language specification but is likely to be buggy anyway.
1148 Returns true if the warning was printed, false if it was inhibited. */
1151 warning_at (location_t location
, int opt
, const char *gmsgid
, ...)
1154 va_start (ap
, gmsgid
);
1155 rich_location
richloc (line_table
, location
);
1156 bool ret
= diagnostic_impl (&richloc
, opt
, gmsgid
, &ap
, DK_WARNING
);
1161 /* Same as warning at, but using RICHLOC. */
1164 warning_at_rich_loc (rich_location
*richloc
, int opt
, const char *gmsgid
, ...)
1167 va_start (ap
, gmsgid
);
1168 bool ret
= diagnostic_impl (richloc
, opt
, gmsgid
, &ap
, DK_WARNING
);
1173 /* A warning at LOCATION. Use this for code which is correct according to the
1174 relevant language specification but is likely to be buggy anyway.
1175 Returns true if the warning was printed, false if it was inhibited. */
1178 warning_n (location_t location
, int opt
, int n
, const char *singular_gmsgid
,
1179 const char *plural_gmsgid
, ...)
1182 va_start (ap
, plural_gmsgid
);
1183 bool ret
= diagnostic_n_impl (location
, opt
, n
,
1184 singular_gmsgid
, plural_gmsgid
,
1190 /* A "pedantic" warning at LOCATION: issues a warning unless
1191 -pedantic-errors was given on the command line, in which case it
1192 issues an error. Use this for diagnostics required by the relevant
1193 language standard, if you have chosen not to make them errors.
1195 Note that these diagnostics are issued independent of the setting
1196 of the -Wpedantic command-line switch. To get a warning enabled
1197 only with that switch, use either "if (pedantic) pedwarn
1198 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
1199 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
1201 Returns true if the warning was printed, false if it was inhibited. */
1204 pedwarn (location_t location
, int opt
, const char *gmsgid
, ...)
1207 va_start (ap
, gmsgid
);
1208 rich_location
richloc (line_table
, location
);
1209 bool ret
= diagnostic_impl (&richloc
, opt
, gmsgid
, &ap
, DK_PEDWARN
);
1214 /* Same as pedwarn, but using RICHLOC. */
1217 pedwarn_at_rich_loc (rich_location
*richloc
, int opt
, const char *gmsgid
, ...)
1220 va_start (ap
, gmsgid
);
1221 bool ret
= diagnostic_impl (richloc
, opt
, gmsgid
, &ap
, DK_PEDWARN
);
1226 /* A "permissive" error at LOCATION: issues an error unless
1227 -fpermissive was given on the command line, in which case it issues
1228 a warning. Use this for things that really should be errors but we
1229 want to support legacy code.
1231 Returns true if the warning was printed, false if it was inhibited. */
1234 permerror (location_t location
, const char *gmsgid
, ...)
1237 va_start (ap
, gmsgid
);
1238 rich_location
richloc (line_table
, location
);
1239 bool ret
= diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_PERMERROR
);
1244 /* Same as "permerror", but at RICHLOC. */
1247 permerror_at_rich_loc (rich_location
*richloc
, const char *gmsgid
, ...)
1250 va_start (ap
, gmsgid
);
1251 bool ret
= diagnostic_impl (richloc
, -1, gmsgid
, &ap
, DK_PERMERROR
);
1256 /* A hard error: the code is definitely ill-formed, and an object file
1257 will not be produced. */
1259 error (const char *gmsgid
, ...)
1262 va_start (ap
, gmsgid
);
1263 rich_location
richloc (line_table
, input_location
);
1264 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_ERROR
);
1268 /* A hard error: the code is definitely ill-formed, and an object file
1269 will not be produced. */
1271 error_n (location_t location
, int n
, const char *singular_gmsgid
,
1272 const char *plural_gmsgid
, ...)
1275 va_start (ap
, plural_gmsgid
);
1276 diagnostic_n_impl (location
, -1, n
, singular_gmsgid
, plural_gmsgid
,
1281 /* Same as above, but use location LOC instead of input_location. */
1283 error_at (location_t loc
, const char *gmsgid
, ...)
1286 va_start (ap
, gmsgid
);
1287 rich_location
richloc (line_table
, loc
);
1288 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_ERROR
);
1292 /* Same as above, but use RICH_LOC. */
1295 error_at_rich_loc (rich_location
*richloc
, const char *gmsgid
, ...)
1298 va_start (ap
, gmsgid
);
1299 diagnostic_impl (richloc
, -1, gmsgid
, &ap
, DK_ERROR
);
1303 /* "Sorry, not implemented." Use for a language feature which is
1304 required by the relevant specification but not implemented by GCC.
1305 An object file will not be produced. */
1307 sorry (const char *gmsgid
, ...)
1310 va_start (ap
, gmsgid
);
1311 rich_location
richloc (line_table
, input_location
);
1312 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_SORRY
);
1316 /* Return true if an error or a "sorry" has been seen. Various
1317 processing is disabled after errors. */
1321 return errorcount
|| sorrycount
;
1324 /* An error which is severe enough that we make no attempt to
1325 continue. Do not use this for internal consistency checks; that's
1326 internal_error. Use of this function should be rare. */
1328 fatal_error (location_t loc
, const char *gmsgid
, ...)
1331 va_start (ap
, gmsgid
);
1332 rich_location
richloc (line_table
, loc
);
1333 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_FATAL
);
1339 /* An internal consistency check has failed. We make no attempt to
1340 continue. Note that unless there is debugging value to be had from
1341 a more specific message, or some other good reason, you should use
1342 abort () instead of calling this function directly. */
1344 internal_error (const char *gmsgid
, ...)
1347 va_start (ap
, gmsgid
);
1348 rich_location
richloc (line_table
, input_location
);
1349 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_ICE
);
1355 /* Like internal_error, but no backtrace will be printed. Used when
1356 the internal error does not happen at the current location, but happened
1359 internal_error_no_backtrace (const char *gmsgid
, ...)
1362 va_start (ap
, gmsgid
);
1363 rich_location
richloc (line_table
, input_location
);
1364 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_ICE_NOBT
);
1370 /* Special case error functions. Most are implemented in terms of the
1371 above, or should be. */
1373 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1374 runs its second argument through gettext. */
1376 fnotice (FILE *file
, const char *cmsgid
, ...)
1380 va_start (ap
, cmsgid
);
1381 vfprintf (file
, _(cmsgid
), ap
);
1385 /* Inform the user that an error occurred while trying to report some
1386 other error. This indicates catastrophic internal inconsistencies,
1387 so give up now. But do try to flush out the previous error.
1388 This mustn't use internal_error, that will cause infinite recursion. */
1391 error_recursion (diagnostic_context
*context
)
1393 if (context
->lock
< 3)
1394 pp_newline_and_flush (context
->printer
);
1397 "Internal compiler error: Error reporting routines re-entered.\n");
1399 /* Call diagnostic_action_after_output to get the "please submit a bug
1401 diagnostic_action_after_output (context
, DK_ICE
);
1403 /* Do not use gcc_unreachable here; that goes through internal_error
1404 and therefore would cause infinite recursion. */
1408 /* Report an internal compiler error in a friendly manner. This is
1409 the function that gets called upon use of abort() in the source
1410 code generally, thanks to a special macro. */
1413 fancy_abort (const char *file
, int line
, const char *function
)
1415 internal_error ("in %s, at %s:%d", function
, trim_filename (file
), line
);
1418 /* Really call the system 'abort'. This has to go right at the end of
1419 this file, so that there are no functions after it that call abort
1420 and get the system abort instead of our macro. */
1430 namespace selftest
{
1432 /* Helper function for test_print_escaped_string. */
1435 assert_print_escaped_string (const location
&loc
, const char *expected_output
,
1439 print_escaped_string (&pp
, input
);
1440 ASSERT_STREQ_AT (loc
, expected_output
, pp_formatted_text (&pp
));
1443 #define ASSERT_PRINT_ESCAPED_STRING_STREQ(EXPECTED_OUTPUT, INPUT) \
1444 assert_print_escaped_string (SELFTEST_LOCATION, EXPECTED_OUTPUT, INPUT)
1446 /* Tests of print_escaped_string. */
1449 test_print_escaped_string ()
1452 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"\"", "");
1454 /* Non-empty string. */
1455 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"hello world\"", "hello world");
1457 /* Various things that need to be escaped: */
1459 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\\after\"",
1462 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\tafter\"",
1465 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\nafter\"",
1468 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\"after\"",
1471 /* Non-printable characters: BEL: '\a': 0x07 */
1472 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\007after\"",
1474 /* Non-printable characters: vertical tab: '\v': 0x0b */
1475 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\013after\"",
1479 /* Tests of print_parseable_fixits. */
1481 /* Verify that print_parseable_fixits emits the empty string if there
1485 test_print_parseable_fixits_none ()
1488 rich_location
richloc (line_table
, UNKNOWN_LOCATION
);
1490 print_parseable_fixits (&pp
, &richloc
);
1491 ASSERT_STREQ ("", pp_formatted_text (&pp
));
1494 /* Verify that print_parseable_fixits does the right thing if there
1495 is an insertion fixit hint. */
1498 test_print_parseable_fixits_insert ()
1501 rich_location
richloc (line_table
, UNKNOWN_LOCATION
);
1503 linemap_add (line_table
, LC_ENTER
, false, "test.c", 0);
1504 linemap_line_start (line_table
, 5, 100);
1505 linemap_add (line_table
, LC_LEAVE
, false, NULL
, 0);
1506 location_t where
= linemap_position_for_column (line_table
, 10);
1507 richloc
.add_fixit_insert_before (where
, "added content");
1509 print_parseable_fixits (&pp
, &richloc
);
1510 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:10}:\"added content\"\n",
1511 pp_formatted_text (&pp
));
1514 /* Verify that print_parseable_fixits does the right thing if there
1515 is an removal fixit hint. */
1518 test_print_parseable_fixits_remove ()
1521 rich_location
richloc (line_table
, UNKNOWN_LOCATION
);
1523 linemap_add (line_table
, LC_ENTER
, false, "test.c", 0);
1524 linemap_line_start (line_table
, 5, 100);
1525 linemap_add (line_table
, LC_LEAVE
, false, NULL
, 0);
1527 where
.m_start
= linemap_position_for_column (line_table
, 10);
1528 where
.m_finish
= linemap_position_for_column (line_table
, 20);
1529 richloc
.add_fixit_remove (where
);
1531 print_parseable_fixits (&pp
, &richloc
);
1532 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"\"\n",
1533 pp_formatted_text (&pp
));
1536 /* Verify that print_parseable_fixits does the right thing if there
1537 is an replacement fixit hint. */
1540 test_print_parseable_fixits_replace ()
1543 rich_location
richloc (line_table
, UNKNOWN_LOCATION
);
1545 linemap_add (line_table
, LC_ENTER
, false, "test.c", 0);
1546 linemap_line_start (line_table
, 5, 100);
1547 linemap_add (line_table
, LC_LEAVE
, false, NULL
, 0);
1549 where
.m_start
= linemap_position_for_column (line_table
, 10);
1550 where
.m_finish
= linemap_position_for_column (line_table
, 20);
1551 richloc
.add_fixit_replace (where
, "replacement");
1553 print_parseable_fixits (&pp
, &richloc
);
1554 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"replacement\"\n",
1555 pp_formatted_text (&pp
));
1558 /* Run all of the selftests within this file. */
1561 diagnostic_c_tests ()
1563 test_print_escaped_string ();
1564 test_print_parseable_fixits_none ();
1565 test_print_parseable_fixits_insert ();
1566 test_print_parseable_fixits_remove ();
1567 test_print_parseable_fixits_replace ();
1570 } // namespace selftest
1572 #endif /* #if CHECKING_P */