1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999-2019 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 "diagnostic-url.h"
35 #include "edit-context.h"
37 #include "selftest-diagnostic.h"
44 #ifdef GWINSZ_IN_SYS_IOCTL
45 # include <sys/ioctl.h>
48 /* Disable warnings about quoting issues in the pp_xxx calls below
49 that (intentionally) don't follow GCC diagnostic conventions. */
51 # pragma GCC diagnostic push
52 # pragma GCC diagnostic ignored "-Wformat-diag"
55 #define pedantic_warning_kind(DC) \
56 ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
57 #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR)
58 #define permissive_error_option(DC) ((DC)->opt_permissive)
61 static bool diagnostic_impl (rich_location
*, int, const char *,
62 va_list *, diagnostic_t
) ATTRIBUTE_GCC_DIAG(3,0);
63 static bool diagnostic_n_impl (rich_location
*, int, unsigned HOST_WIDE_INT
,
64 const char *, const char *, va_list *,
65 diagnostic_t
) ATTRIBUTE_GCC_DIAG(5,0);
67 static void error_recursion (diagnostic_context
*) ATTRIBUTE_NORETURN
;
68 static void real_abort (void) ATTRIBUTE_NORETURN
;
70 /* Name of program invoked, sans directories. */
74 /* A diagnostic_context surrogate for stderr. */
75 static diagnostic_context global_diagnostic_context
;
76 diagnostic_context
*global_dc
= &global_diagnostic_context
;
78 /* Return a malloc'd string containing MSG formatted a la printf. The
79 caller is responsible for freeing the memory. */
81 build_message_string (const char *msg
, ...)
87 str
= xvasprintf (msg
, ap
);
93 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
95 file_name_as_prefix (diagnostic_context
*context
, const char *f
)
98 = colorize_start (pp_show_color (context
->printer
), "locus");
99 const char *locus_ce
= colorize_stop (pp_show_color (context
->printer
));
100 return build_message_string ("%s%s:%s ", locus_cs
, f
, locus_ce
);
105 /* Return the value of the getenv("COLUMNS") as an integer. If the
106 value is not set to a positive integer, use ioctl to get the
107 terminal width. If it fails, return INT_MAX. */
109 get_terminal_width (void)
111 const char * s
= getenv ("COLUMNS");
121 if (ioctl (0, TIOCGWINSZ
, &w
) == 0 && w
.ws_col
> 0)
128 /* Set caret_max_width to value. */
130 diagnostic_set_caret_max_width (diagnostic_context
*context
, int value
)
132 /* One minus to account for the leading empty space. */
133 value
= value
? value
- 1
134 : (isatty (fileno (pp_buffer (context
->printer
)->stream
))
135 ? get_terminal_width () - 1: INT_MAX
);
140 context
->caret_max_width
= value
;
143 /* Default implementation of final_cb. */
146 default_diagnostic_final_cb (diagnostic_context
*context
)
148 /* Some of the errors may actually have been warnings. */
149 if (diagnostic_kind_count (context
, DK_WERROR
))
151 /* -Werror was given. */
152 if (context
->warning_as_error_requested
)
153 pp_verbatim (context
->printer
,
154 _("%s: all warnings being treated as errors"),
156 /* At least one -Werror= was given. */
158 pp_verbatim (context
->printer
,
159 _("%s: some warnings being treated as errors"),
161 pp_newline_and_flush (context
->printer
);
165 /* Initialize the diagnostic message outputting machinery. */
167 diagnostic_initialize (diagnostic_context
*context
, int n_opts
)
171 /* Allocate a basic pretty-printer. Clients will replace this a
172 much more elaborated pretty-printer if they wish. */
173 context
->printer
= XNEW (pretty_printer
);
174 new (context
->printer
) pretty_printer ();
176 memset (context
->diagnostic_count
, 0, sizeof context
->diagnostic_count
);
177 context
->warning_as_error_requested
= false;
178 context
->n_opts
= n_opts
;
179 context
->classify_diagnostic
= XNEWVEC (diagnostic_t
, n_opts
);
180 for (i
= 0; i
< n_opts
; i
++)
181 context
->classify_diagnostic
[i
] = DK_UNSPECIFIED
;
182 context
->show_caret
= false;
183 diagnostic_set_caret_max_width (context
, pp_line_cutoff (context
->printer
));
184 for (i
= 0; i
< rich_location::STATICALLY_ALLOCATED_RANGES
; i
++)
185 context
->caret_chars
[i
] = '^';
186 context
->show_option_requested
= false;
187 context
->abort_on_error
= false;
188 context
->show_column
= false;
189 context
->pedantic_errors
= false;
190 context
->permissive
= false;
191 context
->opt_permissive
= 0;
192 context
->fatal_errors
= false;
193 context
->dc_inhibit_warnings
= false;
194 context
->dc_warn_system_headers
= false;
195 context
->max_errors
= 0;
196 context
->internal_error
= NULL
;
197 diagnostic_starter (context
) = default_diagnostic_starter
;
198 context
->start_span
= default_diagnostic_start_span_fn
;
199 diagnostic_finalizer (context
) = default_diagnostic_finalizer
;
200 context
->option_enabled
= NULL
;
201 context
->option_state
= NULL
;
202 context
->option_name
= NULL
;
203 context
->get_option_url
= NULL
;
204 context
->last_location
= UNKNOWN_LOCATION
;
205 context
->last_module
= 0;
206 context
->x_data
= NULL
;
208 context
->inhibit_notes_p
= false;
209 context
->colorize_source_p
= false;
210 context
->show_labels_p
= false;
211 context
->show_line_numbers_p
= false;
212 context
->min_margin_width
= 0;
213 context
->show_ruler_p
= false;
214 context
->parseable_fixits_p
= false;
215 context
->edit_context_ptr
= NULL
;
216 context
->diagnostic_group_nesting_depth
= 0;
217 context
->diagnostic_group_emission_count
= 0;
218 context
->begin_group_cb
= NULL
;
219 context
->end_group_cb
= NULL
;
220 context
->final_cb
= default_diagnostic_final_cb
;
223 /* Maybe initialize the color support. We require clients to do this
224 explicitly, since most clients don't want color. When called
225 without a VALUE, it initializes with DIAGNOSTICS_COLOR_DEFAULT. */
228 diagnostic_color_init (diagnostic_context
*context
, int value
/*= -1 */)
230 /* value == -1 is the default value. */
233 /* If DIAGNOSTICS_COLOR_DEFAULT is -1, default to
234 -fdiagnostics-color=auto if GCC_COLORS is in the environment,
235 otherwise default to -fdiagnostics-color=never, for other
236 values default to that
237 -fdiagnostics-color={never,auto,always}. */
238 if (DIAGNOSTICS_COLOR_DEFAULT
== -1)
240 if (!getenv ("GCC_COLORS"))
242 value
= DIAGNOSTICS_COLOR_AUTO
;
245 value
= DIAGNOSTICS_COLOR_DEFAULT
;
247 pp_show_color (context
->printer
)
248 = colorize_init ((diagnostic_color_rule_t
) value
);
251 /* Initialize URL support within CONTEXT based on VALUE, handling "auto". */
254 diagnostic_urls_init (diagnostic_context
*context
, int value
/*= -1 */)
257 value
= DIAGNOSTICS_COLOR_DEFAULT
;
259 context
->printer
->show_urls
260 = diagnostic_urls_enabled_p ((diagnostic_url_rule_t
) value
);
263 /* Do any cleaning up required after the last diagnostic is emitted. */
266 diagnostic_finish (diagnostic_context
*context
)
268 if (context
->final_cb
)
269 context
->final_cb (context
);
271 diagnostic_file_cache_fini ();
273 XDELETEVEC (context
->classify_diagnostic
);
274 context
->classify_diagnostic
= NULL
;
276 /* diagnostic_initialize allocates context->printer using XNEW
277 and placement-new. */
278 context
->printer
->~pretty_printer ();
279 XDELETE (context
->printer
);
280 context
->printer
= NULL
;
282 if (context
->edit_context_ptr
)
284 delete context
->edit_context_ptr
;
285 context
->edit_context_ptr
= NULL
;
289 /* Initialize DIAGNOSTIC, where the message MSG has already been
292 diagnostic_set_info_translated (diagnostic_info
*diagnostic
, const char *msg
,
293 va_list *args
, rich_location
*richloc
,
296 gcc_assert (richloc
);
297 diagnostic
->message
.err_no
= errno
;
298 diagnostic
->message
.args_ptr
= args
;
299 diagnostic
->message
.format_spec
= msg
;
300 diagnostic
->message
.m_richloc
= richloc
;
301 diagnostic
->richloc
= richloc
;
302 diagnostic
->kind
= kind
;
303 diagnostic
->option_index
= 0;
306 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
309 diagnostic_set_info (diagnostic_info
*diagnostic
, const char *gmsgid
,
310 va_list *args
, rich_location
*richloc
,
313 gcc_assert (richloc
);
314 diagnostic_set_info_translated (diagnostic
, _(gmsgid
), args
, richloc
, kind
);
317 static const char *const diagnostic_kind_color
[] = {
318 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
319 #include "diagnostic.def"
320 #undef DEFINE_DIAGNOSTIC_KIND
324 /* Get a color name for diagnostics of type KIND
325 Result could be NULL. */
328 diagnostic_get_color_for_kind (diagnostic_t kind
)
330 return diagnostic_kind_color
[kind
];
333 /* Return a formatted line and column ':%line:%column'. Elided if
334 zero. The result is a statically allocated buffer. */
337 maybe_line_and_column (int line
, int col
)
339 static char result
[32];
343 size_t l
= snprintf (result
, sizeof (result
),
344 col
? ":%d:%d" : ":%d", line
, col
);
345 gcc_checking_assert (l
< sizeof (result
));
352 /* Return a malloc'd string describing a location e.g. "foo.c:42:10".
353 The caller is responsible for freeing the memory. */
356 diagnostic_get_location_text (diagnostic_context
*context
,
359 pretty_printer
*pp
= context
->printer
;
360 const char *locus_cs
= colorize_start (pp_show_color (pp
), "locus");
361 const char *locus_ce
= colorize_stop (pp_show_color (pp
));
362 const char *file
= s
.file
? s
.file
: progname
;
363 int line
= strcmp (file
, N_("<built-in>")) ? s
.line
: 0;
364 int col
= context
->show_column
? s
.column
: 0;
366 const char *line_col
= maybe_line_and_column (line
, col
);
367 return build_message_string ("%s%s%s:%s", locus_cs
, file
,
371 /* Return a malloc'd string describing a location and the severity of the
372 diagnostic, e.g. "foo.c:42:10: error: ". The caller is responsible for
373 freeing the memory. */
375 diagnostic_build_prefix (diagnostic_context
*context
,
376 const diagnostic_info
*diagnostic
)
378 static const char *const diagnostic_kind_text
[] = {
379 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
380 #include "diagnostic.def"
381 #undef DEFINE_DIAGNOSTIC_KIND
384 gcc_assert (diagnostic
->kind
< DK_LAST_DIAGNOSTIC_KIND
);
386 const char *text
= _(diagnostic_kind_text
[diagnostic
->kind
]);
387 const char *text_cs
= "", *text_ce
= "";
388 pretty_printer
*pp
= context
->printer
;
390 if (diagnostic_kind_color
[diagnostic
->kind
])
392 text_cs
= colorize_start (pp_show_color (pp
),
393 diagnostic_kind_color
[diagnostic
->kind
]);
394 text_ce
= colorize_stop (pp_show_color (pp
));
397 expanded_location s
= diagnostic_expand_location (diagnostic
);
398 char *location_text
= diagnostic_get_location_text (context
, s
);
400 char *result
= build_message_string ("%s %s%s%s", location_text
,
401 text_cs
, text
, text_ce
);
402 free (location_text
);
406 /* Functions at which to stop the backtrace print. It's not
407 particularly helpful to print the callers of these functions. */
409 static const char * const bt_stop
[] =
417 /* A callback function passed to the backtrace_full function. */
420 bt_callback (void *data
, uintptr_t pc
, const char *filename
, int lineno
,
421 const char *function
)
423 int *pcount
= (int *) data
;
425 /* If we don't have any useful information, don't print
427 if (filename
== NULL
&& function
== NULL
)
430 /* Skip functions in diagnostic.c. */
433 && strcmp (lbasename (filename
), "diagnostic.c") == 0)
436 /* Print up to 20 functions. We could make this a --param, but
437 since this is only for debugging just use a constant for now. */
440 /* Returning a non-zero value stops the backtrace. */
446 if (function
!= NULL
)
448 char *str
= cplus_demangle_v3 (function
,
449 (DMGL_VERBOSE
| DMGL_ANSI
450 | DMGL_GNU_V3
| DMGL_PARAMS
));
457 for (size_t i
= 0; i
< ARRAY_SIZE (bt_stop
); ++i
)
459 size_t len
= strlen (bt_stop
[i
]);
460 if (strncmp (function
, bt_stop
[i
], len
) == 0
461 && (function
[len
] == '\0' || function
[len
] == '('))
465 /* Returning a non-zero value stops the backtrace. */
471 fprintf (stderr
, "0x%lx %s\n\t%s:%d\n",
473 function
== NULL
? "???" : function
,
474 filename
== NULL
? "???" : filename
,
483 /* A callback function passed to the backtrace_full function. This is
484 called if backtrace_full has an error. */
487 bt_err_callback (void *data ATTRIBUTE_UNUSED
, const char *msg
, int errnum
)
491 /* This means that no debug info was available. Just quietly
492 skip printing backtrace info. */
495 fprintf (stderr
, "%s%s%s\n", msg
, errnum
== 0 ? "" : ": ",
496 errnum
== 0 ? "" : xstrerror (errnum
));
499 /* Check if we've met the maximum error limit, and if so fatally exit
500 with a message. CONTEXT is the context to check, and FLUSH
501 indicates whether a diagnostic_finish call is needed. */
504 diagnostic_check_max_errors (diagnostic_context
*context
, bool flush
)
506 if (!context
->max_errors
)
509 int count
= (diagnostic_kind_count (context
, DK_ERROR
)
510 + diagnostic_kind_count (context
, DK_SORRY
)
511 + diagnostic_kind_count (context
, DK_WERROR
));
513 if (count
>= context
->max_errors
)
516 "compilation terminated due to -fmax-errors=%u.\n",
517 context
->max_errors
);
519 diagnostic_finish (context
);
520 exit (FATAL_EXIT_CODE
);
524 /* Take any action which is expected to happen after the diagnostic
525 is written out. This function does not always return. */
527 diagnostic_action_after_output (diagnostic_context
*context
,
528 diagnostic_t diag_kind
)
540 if (context
->abort_on_error
)
542 if (context
->fatal_errors
)
544 fnotice (stderr
, "compilation terminated due to -Wfatal-errors.\n");
545 diagnostic_finish (context
);
546 exit (FATAL_EXIT_CODE
);
553 struct backtrace_state
*state
= NULL
;
554 if (diag_kind
== DK_ICE
)
555 state
= backtrace_create_state (NULL
, 0, bt_err_callback
, NULL
);
558 backtrace_full (state
, 2, bt_callback
, bt_err_callback
,
561 if (context
->abort_on_error
)
564 fnotice (stderr
, "Please submit a full bug report,\n"
565 "with preprocessed source if appropriate.\n");
568 ("Please include the complete backtrace "
569 "with any bug report.\n"));
570 fnotice (stderr
, "See %s for instructions.\n", bug_report_url
);
572 exit (ICE_EXIT_CODE
);
576 if (context
->abort_on_error
)
578 diagnostic_finish (context
);
579 fnotice (stderr
, "compilation terminated.\n");
580 exit (FATAL_EXIT_CODE
);
587 /* True if the last module or file in which a diagnostic was reported is
588 different from the current one. */
591 last_module_changed_p (diagnostic_context
*context
,
592 const line_map_ordinary
*map
)
594 return context
->last_module
!= map
;
597 /* Remember the current module or file as being the last one in which we
598 report a diagnostic. */
601 set_last_module (diagnostic_context
*context
, const line_map_ordinary
*map
)
603 context
->last_module
= map
;
607 diagnostic_report_current_module (diagnostic_context
*context
, location_t where
)
609 const line_map_ordinary
*map
= NULL
;
611 if (pp_needs_newline (context
->printer
))
613 pp_newline (context
->printer
);
614 pp_needs_newline (context
->printer
) = false;
617 if (where
<= BUILTINS_LOCATION
)
620 linemap_resolve_location (line_table
, where
,
621 LRK_MACRO_DEFINITION_LOCATION
,
624 if (map
&& last_module_changed_p (context
, map
))
626 set_last_module (context
, map
);
627 if (! MAIN_FILE_P (map
))
632 where
= linemap_included_from (map
);
633 map
= linemap_included_from_linemap (line_table
, map
);
635 = maybe_line_and_column (SOURCE_LINE (map
, where
),
636 first
&& context
->show_column
637 ? SOURCE_COLUMN (map
, where
) : 0);
638 static const char *const msgs
[] =
640 N_("In file included from"),
643 unsigned index
= !first
;
644 pp_verbatim (context
->printer
, "%s%s %r%s%s%R",
645 first
? "" : ",\n", _(msgs
[index
]),
646 "locus", LINEMAP_FILE (map
), line_col
);
649 while (! MAIN_FILE_P (map
));
650 pp_verbatim (context
->printer
, ":");
651 pp_newline (context
->printer
);
657 default_diagnostic_starter (diagnostic_context
*context
,
658 diagnostic_info
*diagnostic
)
660 diagnostic_report_current_module (context
, diagnostic_location (diagnostic
));
661 pp_set_prefix (context
->printer
, diagnostic_build_prefix (context
,
666 default_diagnostic_start_span_fn (diagnostic_context
*context
,
667 expanded_location exploc
)
669 char *text
= diagnostic_get_location_text (context
, exploc
);
670 pp_string (context
->printer
, text
);
672 pp_newline (context
->printer
);
676 default_diagnostic_finalizer (diagnostic_context
*context
,
677 diagnostic_info
*diagnostic
,
680 char *saved_prefix
= pp_take_prefix (context
->printer
);
681 pp_set_prefix (context
->printer
, NULL
);
682 diagnostic_show_locus (context
, diagnostic
->richloc
, diagnostic
->kind
);
683 pp_set_prefix (context
->printer
, saved_prefix
);
684 pp_flush (context
->printer
);
687 /* Interface to specify diagnostic kind overrides. Returns the
688 previous setting, or DK_UNSPECIFIED if the parameters are out of
689 range. If OPTION_INDEX is zero, the new setting is for all the
692 diagnostic_classify_diagnostic (diagnostic_context
*context
,
694 diagnostic_t new_kind
,
697 diagnostic_t old_kind
;
700 || option_index
>= context
->n_opts
701 || new_kind
>= DK_LAST_DIAGNOSTIC_KIND
)
702 return DK_UNSPECIFIED
;
704 old_kind
= context
->classify_diagnostic
[option_index
];
706 /* Handle pragmas separately, since we need to keep track of *where*
708 if (where
!= UNKNOWN_LOCATION
)
712 /* Record the command-line status, so we can reset it back on DK_POP. */
713 if (old_kind
== DK_UNSPECIFIED
)
715 old_kind
= !context
->option_enabled (option_index
,
717 context
->option_state
)
718 ? DK_IGNORED
: (context
->warning_as_error_requested
719 ? DK_ERROR
: DK_WARNING
);
720 context
->classify_diagnostic
[option_index
] = old_kind
;
723 for (i
= context
->n_classification_history
- 1; i
>= 0; i
--)
724 if (context
->classification_history
[i
].option
== option_index
)
726 old_kind
= context
->classification_history
[i
].kind
;
730 i
= context
->n_classification_history
;
731 context
->classification_history
=
732 (diagnostic_classification_change_t
*) xrealloc (context
->classification_history
, (i
+ 1)
733 * sizeof (diagnostic_classification_change_t
));
734 context
->classification_history
[i
].location
= where
;
735 context
->classification_history
[i
].option
= option_index
;
736 context
->classification_history
[i
].kind
= new_kind
;
737 context
->n_classification_history
++;
740 context
->classify_diagnostic
[option_index
] = new_kind
;
745 /* Save all diagnostic classifications in a stack. */
747 diagnostic_push_diagnostics (diagnostic_context
*context
, location_t where ATTRIBUTE_UNUSED
)
749 context
->push_list
= (int *) xrealloc (context
->push_list
, (context
->n_push
+ 1) * sizeof (int));
750 context
->push_list
[context
->n_push
++] = context
->n_classification_history
;
753 /* Restore the topmost classification set off the stack. If the stack
754 is empty, revert to the state based on command line parameters. */
756 diagnostic_pop_diagnostics (diagnostic_context
*context
, location_t where
)
762 jump_to
= context
->push_list
[-- context
->n_push
];
766 i
= context
->n_classification_history
;
767 context
->classification_history
=
768 (diagnostic_classification_change_t
*) xrealloc (context
->classification_history
, (i
+ 1)
769 * sizeof (diagnostic_classification_change_t
));
770 context
->classification_history
[i
].location
= where
;
771 context
->classification_history
[i
].option
= jump_to
;
772 context
->classification_history
[i
].kind
= DK_POP
;
773 context
->n_classification_history
++;
776 /* Helper function for print_parseable_fixits. Print TEXT to PP, obeying the
777 escaping rules for -fdiagnostics-parseable-fixits. */
780 print_escaped_string (pretty_printer
*pp
, const char *text
)
785 pp_character (pp
, '"');
786 for (const char *ch
= text
; *ch
; ch
++)
791 /* Escape backslash as two backslashes. */
792 pp_string (pp
, "\\\\");
795 /* Escape tab as "\t". */
796 pp_string (pp
, "\\t");
799 /* Escape newline as "\n". */
800 pp_string (pp
, "\\n");
803 /* Escape doublequotes as \". */
804 pp_string (pp
, "\\\"");
808 pp_character (pp
, *ch
);
810 /* Use octal for non-printable chars. */
812 unsigned char c
= (*ch
& 0xff);
813 pp_printf (pp
, "\\%o%o%o", (c
/ 64), (c
/ 8) & 007, c
& 007);
818 pp_character (pp
, '"');
821 /* Implementation of -fdiagnostics-parseable-fixits. Print a
822 machine-parseable version of all fixits in RICHLOC to PP. */
825 print_parseable_fixits (pretty_printer
*pp
, rich_location
*richloc
)
828 gcc_assert (richloc
);
830 char *saved_prefix
= pp_take_prefix (pp
);
831 pp_set_prefix (pp
, NULL
);
833 for (unsigned i
= 0; i
< richloc
->get_num_fixit_hints (); i
++)
835 const fixit_hint
*hint
= richloc
->get_fixit_hint (i
);
836 location_t start_loc
= hint
->get_start_loc ();
837 expanded_location start_exploc
= expand_location (start_loc
);
838 pp_string (pp
, "fix-it:");
839 print_escaped_string (pp
, start_exploc
.file
);
840 /* For compatibility with clang, print as a half-open range. */
841 location_t next_loc
= hint
->get_next_loc ();
842 expanded_location next_exploc
= expand_location (next_loc
);
843 pp_printf (pp
, ":{%i:%i-%i:%i}:",
844 start_exploc
.line
, start_exploc
.column
,
845 next_exploc
.line
, next_exploc
.column
);
846 print_escaped_string (pp
, hint
->get_string ());
850 pp_set_prefix (pp
, saved_prefix
);
853 /* Update the diag_class of DIAGNOSTIC based on its location
855 #pragma GCC diagnostic
856 directives recorded within CONTEXT.
858 Return the new diag_class of DIAGNOSTIC if it was updated, or
859 DK_UNSPECIFIED otherwise. */
862 update_effective_level_from_pragmas (diagnostic_context
*context
,
863 diagnostic_info
*diagnostic
)
865 diagnostic_t diag_class
= DK_UNSPECIFIED
;
867 if (context
->n_classification_history
> 0)
869 location_t location
= diagnostic_location (diagnostic
);
871 /* FIXME: Stupid search. Optimize later. */
872 for (int i
= context
->n_classification_history
- 1; i
>= 0; i
--)
874 if (linemap_location_before_p
876 context
->classification_history
[i
].location
,
879 if (context
->classification_history
[i
].kind
== (int) DK_POP
)
881 i
= context
->classification_history
[i
].option
;
884 int option
= context
->classification_history
[i
].option
;
885 /* The option 0 is for all the diagnostics. */
886 if (option
== 0 || option
== diagnostic
->option_index
)
888 diag_class
= context
->classification_history
[i
].kind
;
889 if (diag_class
!= DK_UNSPECIFIED
)
890 diagnostic
->kind
= diag_class
;
900 /* Print any metadata about the option used to control DIAGNOSTIC to CONTEXT's
901 printer, e.g. " [-Werror=uninitialized]".
902 Subroutine of diagnostic_report_diagnostic. */
905 print_option_information (diagnostic_context
*context
,
906 const diagnostic_info
*diagnostic
,
907 diagnostic_t orig_diag_kind
)
911 option_text
= context
->option_name (context
, diagnostic
->option_index
,
912 orig_diag_kind
, diagnostic
->kind
);
916 char *option_url
= NULL
;
917 if (context
->get_option_url
)
918 option_url
= context
->get_option_url (context
,
919 diagnostic
->option_index
);
920 pretty_printer
*pp
= context
->printer
;
921 pp_string (pp
, " [");
922 pp_string (pp
, colorize_start (pp_show_color (pp
),
923 diagnostic_kind_color
[diagnostic
->kind
]));
925 pp_begin_url (pp
, option_url
);
926 pp_string (pp
, option_text
);
932 pp_string (pp
, colorize_stop (pp_show_color (pp
)));
933 pp_character (pp
, ']');
938 /* Report a diagnostic message (an error or a warning) as specified by
939 DC. This function is *the* subroutine in terms of which front-ends
940 should implement their specific diagnostic handling modules. The
941 front-end independent format specifiers are exactly those described
942 in the documentation of output_format.
943 Return true if a diagnostic was printed, false otherwise. */
946 diagnostic_report_diagnostic (diagnostic_context
*context
,
947 diagnostic_info
*diagnostic
)
949 location_t location
= diagnostic_location (diagnostic
);
950 diagnostic_t orig_diag_kind
= diagnostic
->kind
;
952 /* Give preference to being able to inhibit warnings, before they
953 get reclassified to something else. */
954 if ((diagnostic
->kind
== DK_WARNING
|| diagnostic
->kind
== DK_PEDWARN
)
955 && !diagnostic_report_warnings_p (context
, location
))
958 if (diagnostic
->kind
== DK_PEDWARN
)
960 diagnostic
->kind
= pedantic_warning_kind (context
);
961 /* We do this to avoid giving the message for -pedantic-errors. */
962 orig_diag_kind
= diagnostic
->kind
;
965 if (diagnostic
->kind
== DK_NOTE
&& context
->inhibit_notes_p
)
968 if (context
->lock
> 0)
970 /* If we're reporting an ICE in the middle of some other error,
971 try to flush out the previous error, then let this one
972 through. Don't do this more than once. */
973 if ((diagnostic
->kind
== DK_ICE
|| diagnostic
->kind
== DK_ICE_NOBT
)
974 && context
->lock
== 1)
975 pp_newline_and_flush (context
->printer
);
977 error_recursion (context
);
980 /* If the user requested that warnings be treated as errors, so be
981 it. Note that we do this before the next block so that
982 individual warnings can be overridden back to warnings with
984 if (context
->warning_as_error_requested
985 && diagnostic
->kind
== DK_WARNING
)
986 diagnostic
->kind
= DK_ERROR
;
988 if (diagnostic
->option_index
989 && diagnostic
->option_index
!= permissive_error_option (context
))
991 /* This tests if the user provided the appropriate -Wfoo or
993 if (! context
->option_enabled (diagnostic
->option_index
,
995 context
->option_state
))
998 /* This tests for #pragma diagnostic changes. */
999 diagnostic_t diag_class
1000 = update_effective_level_from_pragmas (context
, diagnostic
);
1002 /* This tests if the user provided the appropriate -Werror=foo
1004 if (diag_class
== DK_UNSPECIFIED
1005 && (context
->classify_diagnostic
[diagnostic
->option_index
]
1008 = context
->classify_diagnostic
[diagnostic
->option_index
];
1010 /* This allows for future extensions, like temporarily disabling
1011 warnings for ranges of source code. */
1012 if (diagnostic
->kind
== DK_IGNORED
)
1016 if (diagnostic
->kind
!= DK_NOTE
)
1017 diagnostic_check_max_errors (context
);
1021 if (diagnostic
->kind
== DK_ICE
|| diagnostic
->kind
== DK_ICE_NOBT
)
1023 /* When not checking, ICEs are converted to fatal errors when an
1024 error has already occurred. This is counteracted by
1027 && (diagnostic_kind_count (context
, DK_ERROR
) > 0
1028 || diagnostic_kind_count (context
, DK_SORRY
) > 0)
1029 && !context
->abort_on_error
)
1032 = expand_location (diagnostic_location (diagnostic
));
1033 fnotice (stderr
, "%s:%d: confused by earlier errors, bailing out\n",
1035 exit (ICE_EXIT_CODE
);
1037 if (context
->internal_error
)
1038 (*context
->internal_error
) (context
,
1039 diagnostic
->message
.format_spec
,
1040 diagnostic
->message
.args_ptr
);
1042 if (diagnostic
->kind
== DK_ERROR
&& orig_diag_kind
== DK_WARNING
)
1043 ++diagnostic_kind_count (context
, DK_WERROR
);
1045 ++diagnostic_kind_count (context
, diagnostic
->kind
);
1047 /* Is this the initial diagnostic within the stack of groups? */
1048 if (context
->diagnostic_group_emission_count
== 0)
1050 if (context
->begin_group_cb
)
1051 context
->begin_group_cb (context
);
1053 context
->diagnostic_group_emission_count
++;
1055 diagnostic
->message
.x_data
= &diagnostic
->x_data
;
1056 diagnostic
->x_data
= NULL
;
1057 pp_format (context
->printer
, &diagnostic
->message
);
1058 (*diagnostic_starter (context
)) (context
, diagnostic
);
1059 pp_output_formatted_text (context
->printer
);
1060 if (context
->show_option_requested
)
1061 print_option_information (context
, diagnostic
, orig_diag_kind
);
1062 (*diagnostic_finalizer (context
)) (context
, diagnostic
, orig_diag_kind
);
1063 if (context
->parseable_fixits_p
)
1065 print_parseable_fixits (context
->printer
, diagnostic
->richloc
);
1066 pp_flush (context
->printer
);
1068 diagnostic_action_after_output (context
, diagnostic
->kind
);
1069 diagnostic
->x_data
= NULL
;
1071 if (context
->edit_context_ptr
)
1072 if (diagnostic
->richloc
->fixits_can_be_auto_applied_p ())
1073 context
->edit_context_ptr
->add_fixits (diagnostic
->richloc
);
1080 /* Get the number of digits in the decimal representation of VALUE. */
1083 num_digits (int value
)
1085 /* Perhaps simpler to use log10 for this, but doing it this way avoids
1086 using floating point. */
1087 gcc_assert (value
>= 0);
1101 /* Given a partial pathname as input, return another pathname that
1102 shares no directory elements with the pathname of __FILE__. This
1103 is used by fancy_abort() to print `Internal compiler error in expr.c'
1104 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1107 trim_filename (const char *name
)
1109 static const char this_file
[] = __FILE__
;
1110 const char *p
= name
, *q
= this_file
;
1112 /* First skip any "../" in each filename. This allows us to give a proper
1113 reference to a file in a subdirectory. */
1114 while (p
[0] == '.' && p
[1] == '.' && IS_DIR_SEPARATOR (p
[2]))
1117 while (q
[0] == '.' && q
[1] == '.' && IS_DIR_SEPARATOR (q
[2]))
1120 /* Now skip any parts the two filenames have in common. */
1121 while (*p
== *q
&& *p
!= 0 && *q
!= 0)
1124 /* Now go backwards until the previous directory separator. */
1125 while (p
> name
&& !IS_DIR_SEPARATOR (p
[-1]))
1131 /* Standard error reporting routines in increasing order of severity.
1132 All of these take arguments like printf. */
1134 /* Text to be emitted verbatim to the error message stream; this
1135 produces no prefix and disables line-wrapping. Use rarely. */
1137 verbatim (const char *gmsgid
, ...)
1142 va_start (ap
, gmsgid
);
1143 text
.err_no
= errno
;
1144 text
.args_ptr
= &ap
;
1145 text
.format_spec
= _(gmsgid
);
1147 pp_format_verbatim (global_dc
->printer
, &text
);
1148 pp_newline_and_flush (global_dc
->printer
);
1152 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
1154 diagnostic_append_note (diagnostic_context
*context
,
1155 location_t location
,
1156 const char * gmsgid
, ...)
1158 diagnostic_info diagnostic
;
1160 rich_location
richloc (line_table
, location
);
1162 va_start (ap
, gmsgid
);
1163 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_NOTE
);
1164 if (context
->inhibit_notes_p
)
1169 char *saved_prefix
= pp_take_prefix (context
->printer
);
1170 pp_set_prefix (context
->printer
,
1171 diagnostic_build_prefix (context
, &diagnostic
));
1172 pp_format (context
->printer
, &diagnostic
.message
);
1173 pp_output_formatted_text (context
->printer
);
1174 pp_destroy_prefix (context
->printer
);
1175 pp_set_prefix (context
->printer
, saved_prefix
);
1176 diagnostic_show_locus (context
, &richloc
, DK_NOTE
);
1180 /* Implement emit_diagnostic, inform, warning, warning_at, pedwarn,
1181 permerror, error, error_at, error_at, sorry, fatal_error, internal_error,
1182 and internal_error_no_backtrace, as documented and defined below. */
1184 diagnostic_impl (rich_location
*richloc
, int opt
,
1186 va_list *ap
, diagnostic_t kind
)
1188 diagnostic_info diagnostic
;
1189 if (kind
== DK_PERMERROR
)
1191 diagnostic_set_info (&diagnostic
, gmsgid
, ap
, richloc
,
1192 permissive_error_kind (global_dc
));
1193 diagnostic
.option_index
= permissive_error_option (global_dc
);
1197 diagnostic_set_info (&diagnostic
, gmsgid
, ap
, richloc
, kind
);
1198 if (kind
== DK_WARNING
|| kind
== DK_PEDWARN
)
1199 diagnostic
.option_index
= opt
;
1201 return diagnostic_report_diagnostic (global_dc
, &diagnostic
);
1204 /* Implement inform_n, warning_n, and error_n, as documented and
1207 diagnostic_n_impl (rich_location
*richloc
, int opt
, unsigned HOST_WIDE_INT n
,
1208 const char *singular_gmsgid
,
1209 const char *plural_gmsgid
,
1210 va_list *ap
, diagnostic_t kind
)
1212 diagnostic_info diagnostic
;
1215 if (sizeof n
<= sizeof gtn
)
1218 /* Use the largest number ngettext can handle, otherwise
1219 preserve the six least significant decimal digits for
1220 languages where the plural form depends on them. */
1221 gtn
= n
<= ULONG_MAX
? n
: n
% 1000000LU + 1000000LU;
1223 const char *text
= ngettext (singular_gmsgid
, plural_gmsgid
, gtn
);
1224 diagnostic_set_info_translated (&diagnostic
, text
, ap
, richloc
, kind
);
1225 if (kind
== DK_WARNING
)
1226 diagnostic
.option_index
= opt
;
1227 return diagnostic_report_diagnostic (global_dc
, &diagnostic
);
1230 /* Wrapper around diagnostic_impl taking a variable argument list. */
1233 emit_diagnostic (diagnostic_t kind
, location_t location
, int opt
,
1234 const char *gmsgid
, ...)
1236 auto_diagnostic_group d
;
1238 va_start (ap
, gmsgid
);
1239 rich_location
richloc (line_table
, location
);
1240 bool ret
= diagnostic_impl (&richloc
, opt
, gmsgid
, &ap
, kind
);
1245 /* As above, but for rich_location *. */
1248 emit_diagnostic (diagnostic_t kind
, rich_location
*richloc
, int opt
,
1249 const char *gmsgid
, ...)
1251 auto_diagnostic_group d
;
1253 va_start (ap
, gmsgid
);
1254 bool ret
= diagnostic_impl (richloc
, opt
, gmsgid
, &ap
, kind
);
1259 /* Wrapper around diagnostic_impl taking a va_list parameter. */
1262 emit_diagnostic_valist (diagnostic_t kind
, location_t location
, int opt
,
1263 const char *gmsgid
, va_list *ap
)
1265 rich_location
richloc (line_table
, location
);
1266 return diagnostic_impl (&richloc
, opt
, gmsgid
, ap
, kind
);
1269 /* An informative note at LOCATION. Use this for additional details on an error
1272 inform (location_t location
, const char *gmsgid
, ...)
1274 auto_diagnostic_group d
;
1276 va_start (ap
, gmsgid
);
1277 rich_location
richloc (line_table
, location
);
1278 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_NOTE
);
1282 /* Same as "inform" above, but at RICHLOC. */
1284 inform (rich_location
*richloc
, const char *gmsgid
, ...)
1286 gcc_assert (richloc
);
1288 auto_diagnostic_group d
;
1290 va_start (ap
, gmsgid
);
1291 diagnostic_impl (richloc
, -1, gmsgid
, &ap
, DK_NOTE
);
1295 /* An informative note at LOCATION. Use this for additional details on an
1298 inform_n (location_t location
, unsigned HOST_WIDE_INT n
,
1299 const char *singular_gmsgid
, const char *plural_gmsgid
, ...)
1302 va_start (ap
, plural_gmsgid
);
1303 auto_diagnostic_group d
;
1304 rich_location
richloc (line_table
, location
);
1305 diagnostic_n_impl (&richloc
, -1, n
, singular_gmsgid
, plural_gmsgid
,
1310 /* A warning at INPUT_LOCATION. Use this for code which is correct according
1311 to the relevant language specification but is likely to be buggy anyway.
1312 Returns true if the warning was printed, false if it was inhibited. */
1314 warning (int opt
, const char *gmsgid
, ...)
1316 auto_diagnostic_group d
;
1318 va_start (ap
, gmsgid
);
1319 rich_location
richloc (line_table
, input_location
);
1320 bool ret
= diagnostic_impl (&richloc
, opt
, gmsgid
, &ap
, DK_WARNING
);
1325 /* A warning at LOCATION. Use this for code which is correct according to the
1326 relevant language specification but is likely to be buggy anyway.
1327 Returns true if the warning was printed, false if it was inhibited. */
1330 warning_at (location_t location
, int opt
, const char *gmsgid
, ...)
1332 auto_diagnostic_group d
;
1334 va_start (ap
, gmsgid
);
1335 rich_location
richloc (line_table
, location
);
1336 bool ret
= diagnostic_impl (&richloc
, opt
, gmsgid
, &ap
, DK_WARNING
);
1341 /* Same as "warning at" above, but using RICHLOC. */
1344 warning_at (rich_location
*richloc
, int opt
, const char *gmsgid
, ...)
1346 gcc_assert (richloc
);
1348 auto_diagnostic_group d
;
1350 va_start (ap
, gmsgid
);
1351 bool ret
= diagnostic_impl (richloc
, opt
, gmsgid
, &ap
, DK_WARNING
);
1356 /* Same as warning_n plural variant below, but using RICHLOC. */
1359 warning_n (rich_location
*richloc
, int opt
, unsigned HOST_WIDE_INT n
,
1360 const char *singular_gmsgid
, const char *plural_gmsgid
, ...)
1362 gcc_assert (richloc
);
1364 auto_diagnostic_group d
;
1366 va_start (ap
, plural_gmsgid
);
1367 bool ret
= diagnostic_n_impl (richloc
, opt
, n
,
1368 singular_gmsgid
, plural_gmsgid
,
1374 /* A warning at LOCATION. Use this for code which is correct according to the
1375 relevant language specification but is likely to be buggy anyway.
1376 Returns true if the warning was printed, false if it was inhibited. */
1379 warning_n (location_t location
, int opt
, unsigned HOST_WIDE_INT n
,
1380 const char *singular_gmsgid
, const char *plural_gmsgid
, ...)
1382 auto_diagnostic_group d
;
1384 va_start (ap
, plural_gmsgid
);
1385 rich_location
richloc (line_table
, location
);
1386 bool ret
= diagnostic_n_impl (&richloc
, opt
, n
,
1387 singular_gmsgid
, plural_gmsgid
,
1393 /* A "pedantic" warning at LOCATION: issues a warning unless
1394 -pedantic-errors was given on the command line, in which case it
1395 issues an error. Use this for diagnostics required by the relevant
1396 language standard, if you have chosen not to make them errors.
1398 Note that these diagnostics are issued independent of the setting
1399 of the -Wpedantic command-line switch. To get a warning enabled
1400 only with that switch, use either "if (pedantic) pedwarn
1401 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
1402 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
1404 Returns true if the warning was printed, false if it was inhibited. */
1407 pedwarn (location_t location
, int opt
, const char *gmsgid
, ...)
1409 auto_diagnostic_group d
;
1411 va_start (ap
, gmsgid
);
1412 rich_location
richloc (line_table
, location
);
1413 bool ret
= diagnostic_impl (&richloc
, opt
, gmsgid
, &ap
, DK_PEDWARN
);
1418 /* Same as pedwarn above, but using RICHLOC. */
1421 pedwarn (rich_location
*richloc
, int opt
, const char *gmsgid
, ...)
1423 gcc_assert (richloc
);
1425 auto_diagnostic_group d
;
1427 va_start (ap
, gmsgid
);
1428 bool ret
= diagnostic_impl (richloc
, opt
, gmsgid
, &ap
, DK_PEDWARN
);
1433 /* A "permissive" error at LOCATION: issues an error unless
1434 -fpermissive was given on the command line, in which case it issues
1435 a warning. Use this for things that really should be errors but we
1436 want to support legacy code.
1438 Returns true if the warning was printed, false if it was inhibited. */
1441 permerror (location_t location
, const char *gmsgid
, ...)
1443 auto_diagnostic_group d
;
1445 va_start (ap
, gmsgid
);
1446 rich_location
richloc (line_table
, location
);
1447 bool ret
= diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_PERMERROR
);
1452 /* Same as "permerror" above, but at RICHLOC. */
1455 permerror (rich_location
*richloc
, const char *gmsgid
, ...)
1457 gcc_assert (richloc
);
1459 auto_diagnostic_group d
;
1461 va_start (ap
, gmsgid
);
1462 bool ret
= diagnostic_impl (richloc
, -1, gmsgid
, &ap
, DK_PERMERROR
);
1467 /* A hard error: the code is definitely ill-formed, and an object file
1468 will not be produced. */
1470 error (const char *gmsgid
, ...)
1472 auto_diagnostic_group d
;
1474 va_start (ap
, gmsgid
);
1475 rich_location
richloc (line_table
, input_location
);
1476 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_ERROR
);
1480 /* A hard error: the code is definitely ill-formed, and an object file
1481 will not be produced. */
1483 error_n (location_t location
, unsigned HOST_WIDE_INT n
,
1484 const char *singular_gmsgid
, const char *plural_gmsgid
, ...)
1486 auto_diagnostic_group d
;
1488 va_start (ap
, plural_gmsgid
);
1489 rich_location
richloc (line_table
, location
);
1490 diagnostic_n_impl (&richloc
, -1, n
, singular_gmsgid
, plural_gmsgid
,
1495 /* Same as above, but use location LOC instead of input_location. */
1497 error_at (location_t loc
, const char *gmsgid
, ...)
1499 auto_diagnostic_group d
;
1501 va_start (ap
, gmsgid
);
1502 rich_location
richloc (line_table
, loc
);
1503 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_ERROR
);
1507 /* Same as above, but use RICH_LOC. */
1510 error_at (rich_location
*richloc
, const char *gmsgid
, ...)
1512 gcc_assert (richloc
);
1514 auto_diagnostic_group d
;
1516 va_start (ap
, gmsgid
);
1517 diagnostic_impl (richloc
, -1, gmsgid
, &ap
, DK_ERROR
);
1521 /* "Sorry, not implemented." Use for a language feature which is
1522 required by the relevant specification but not implemented by GCC.
1523 An object file will not be produced. */
1525 sorry (const char *gmsgid
, ...)
1527 auto_diagnostic_group d
;
1529 va_start (ap
, gmsgid
);
1530 rich_location
richloc (line_table
, input_location
);
1531 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_SORRY
);
1535 /* Same as above, but use location LOC instead of input_location. */
1537 sorry_at (location_t loc
, const char *gmsgid
, ...)
1539 auto_diagnostic_group d
;
1541 va_start (ap
, gmsgid
);
1542 rich_location
richloc (line_table
, loc
);
1543 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_SORRY
);
1547 /* Return true if an error or a "sorry" has been seen. Various
1548 processing is disabled after errors. */
1552 return errorcount
|| sorrycount
;
1555 /* An error which is severe enough that we make no attempt to
1556 continue. Do not use this for internal consistency checks; that's
1557 internal_error. Use of this function should be rare. */
1559 fatal_error (location_t loc
, const char *gmsgid
, ...)
1561 auto_diagnostic_group d
;
1563 va_start (ap
, gmsgid
);
1564 rich_location
richloc (line_table
, loc
);
1565 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_FATAL
);
1571 /* An internal consistency check has failed. We make no attempt to
1572 continue. Note that unless there is debugging value to be had from
1573 a more specific message, or some other good reason, you should use
1574 abort () instead of calling this function directly. */
1576 internal_error (const char *gmsgid
, ...)
1578 auto_diagnostic_group d
;
1580 va_start (ap
, gmsgid
);
1581 rich_location
richloc (line_table
, input_location
);
1582 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_ICE
);
1588 /* Like internal_error, but no backtrace will be printed. Used when
1589 the internal error does not happen at the current location, but happened
1592 internal_error_no_backtrace (const char *gmsgid
, ...)
1594 auto_diagnostic_group d
;
1596 va_start (ap
, gmsgid
);
1597 rich_location
richloc (line_table
, input_location
);
1598 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_ICE_NOBT
);
1604 /* Special case error functions. Most are implemented in terms of the
1605 above, or should be. */
1607 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1608 runs its second argument through gettext. */
1610 fnotice (FILE *file
, const char *cmsgid
, ...)
1614 va_start (ap
, cmsgid
);
1615 vfprintf (file
, _(cmsgid
), ap
);
1619 /* Inform the user that an error occurred while trying to report some
1620 other error. This indicates catastrophic internal inconsistencies,
1621 so give up now. But do try to flush out the previous error.
1622 This mustn't use internal_error, that will cause infinite recursion. */
1625 error_recursion (diagnostic_context
*context
)
1627 if (context
->lock
< 3)
1628 pp_newline_and_flush (context
->printer
);
1631 "Internal compiler error: Error reporting routines re-entered.\n");
1633 /* Call diagnostic_action_after_output to get the "please submit a bug
1635 diagnostic_action_after_output (context
, DK_ICE
);
1637 /* Do not use gcc_unreachable here; that goes through internal_error
1638 and therefore would cause infinite recursion. */
1642 /* Report an internal compiler error in a friendly manner. This is
1643 the function that gets called upon use of abort() in the source
1644 code generally, thanks to a special macro. */
1647 fancy_abort (const char *file
, int line
, const char *function
)
1649 internal_error ("in %s, at %s:%d", function
, trim_filename (file
), line
);
1652 /* class auto_diagnostic_group. */
1654 /* Constructor: "push" this group into global_dc. */
1656 auto_diagnostic_group::auto_diagnostic_group ()
1658 global_dc
->diagnostic_group_nesting_depth
++;
1661 /* Destructor: "pop" this group from global_dc. */
1663 auto_diagnostic_group::~auto_diagnostic_group ()
1665 if (--global_dc
->diagnostic_group_nesting_depth
== 0)
1667 /* Handle the case where we've popped the final diagnostic group.
1668 If any diagnostics were emitted, give the context a chance
1670 if (global_dc
->diagnostic_group_emission_count
> 0)
1672 if (global_dc
->end_group_cb
)
1673 global_dc
->end_group_cb (global_dc
);
1675 global_dc
->diagnostic_group_emission_count
= 0;
1679 /* Really call the system 'abort'. This has to go right at the end of
1680 this file, so that there are no functions after it that call abort
1681 and get the system abort instead of our macro. */
1691 namespace selftest
{
1693 /* Helper function for test_print_escaped_string. */
1696 assert_print_escaped_string (const location
&loc
, const char *expected_output
,
1700 print_escaped_string (&pp
, input
);
1701 ASSERT_STREQ_AT (loc
, expected_output
, pp_formatted_text (&pp
));
1704 #define ASSERT_PRINT_ESCAPED_STRING_STREQ(EXPECTED_OUTPUT, INPUT) \
1705 assert_print_escaped_string (SELFTEST_LOCATION, EXPECTED_OUTPUT, INPUT)
1707 /* Tests of print_escaped_string. */
1710 test_print_escaped_string ()
1713 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"\"", "");
1715 /* Non-empty string. */
1716 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"hello world\"", "hello world");
1718 /* Various things that need to be escaped: */
1720 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\\after\"",
1723 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\tafter\"",
1726 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\nafter\"",
1729 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\"after\"",
1732 /* Non-printable characters: BEL: '\a': 0x07 */
1733 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\007after\"",
1735 /* Non-printable characters: vertical tab: '\v': 0x0b */
1736 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\013after\"",
1740 /* Tests of print_parseable_fixits. */
1742 /* Verify that print_parseable_fixits emits the empty string if there
1746 test_print_parseable_fixits_none ()
1749 rich_location
richloc (line_table
, UNKNOWN_LOCATION
);
1751 print_parseable_fixits (&pp
, &richloc
);
1752 ASSERT_STREQ ("", pp_formatted_text (&pp
));
1755 /* Verify that print_parseable_fixits does the right thing if there
1756 is an insertion fixit hint. */
1759 test_print_parseable_fixits_insert ()
1762 rich_location
richloc (line_table
, UNKNOWN_LOCATION
);
1764 linemap_add (line_table
, LC_ENTER
, false, "test.c", 0);
1765 linemap_line_start (line_table
, 5, 100);
1766 linemap_add (line_table
, LC_LEAVE
, false, NULL
, 0);
1767 location_t where
= linemap_position_for_column (line_table
, 10);
1768 richloc
.add_fixit_insert_before (where
, "added content");
1770 print_parseable_fixits (&pp
, &richloc
);
1771 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:10}:\"added content\"\n",
1772 pp_formatted_text (&pp
));
1775 /* Verify that print_parseable_fixits does the right thing if there
1776 is an removal fixit hint. */
1779 test_print_parseable_fixits_remove ()
1782 rich_location
richloc (line_table
, UNKNOWN_LOCATION
);
1784 linemap_add (line_table
, LC_ENTER
, false, "test.c", 0);
1785 linemap_line_start (line_table
, 5, 100);
1786 linemap_add (line_table
, LC_LEAVE
, false, NULL
, 0);
1788 where
.m_start
= linemap_position_for_column (line_table
, 10);
1789 where
.m_finish
= linemap_position_for_column (line_table
, 20);
1790 richloc
.add_fixit_remove (where
);
1792 print_parseable_fixits (&pp
, &richloc
);
1793 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"\"\n",
1794 pp_formatted_text (&pp
));
1797 /* Verify that print_parseable_fixits does the right thing if there
1798 is an replacement fixit hint. */
1801 test_print_parseable_fixits_replace ()
1804 rich_location
richloc (line_table
, UNKNOWN_LOCATION
);
1806 linemap_add (line_table
, LC_ENTER
, false, "test.c", 0);
1807 linemap_line_start (line_table
, 5, 100);
1808 linemap_add (line_table
, LC_LEAVE
, false, NULL
, 0);
1810 where
.m_start
= linemap_position_for_column (line_table
, 10);
1811 where
.m_finish
= linemap_position_for_column (line_table
, 20);
1812 richloc
.add_fixit_replace (where
, "replacement");
1814 print_parseable_fixits (&pp
, &richloc
);
1815 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"replacement\"\n",
1816 pp_formatted_text (&pp
));
1820 diagnostic_get_location_text (..., SHOW_COLUMN)
1821 generates EXPECTED_LOC_TEXT, given FILENAME, LINE, COLUMN, with
1822 colorization disabled. */
1825 assert_location_text (const char *expected_loc_text
,
1826 const char *filename
, int line
, int column
,
1829 test_diagnostic_context dc
;
1830 dc
.show_column
= show_column
;
1832 expanded_location xloc
;
1833 xloc
.file
= filename
;
1835 xloc
.column
= column
;
1839 char *actual_loc_text
= diagnostic_get_location_text (&dc
, xloc
);
1840 ASSERT_STREQ (expected_loc_text
, actual_loc_text
);
1841 free (actual_loc_text
);
1844 /* Verify that diagnostic_get_location_text works as expected. */
1847 test_diagnostic_get_location_text ()
1849 const char *old_progname
= progname
;
1850 progname
= "PROGNAME";
1851 assert_location_text ("PROGNAME:", NULL
, 0, 0, true);
1852 assert_location_text ("<built-in>:", "<built-in>", 42, 10, true);
1853 assert_location_text ("foo.c:42:10:", "foo.c", 42, 10, true);
1854 assert_location_text ("foo.c:42:", "foo.c", 42, 0, true);
1855 assert_location_text ("foo.c:", "foo.c", 0, 10, true);
1856 assert_location_text ("foo.c:42:", "foo.c", 42, 10, false);
1857 assert_location_text ("foo.c:", "foo.c", 0, 10, false);
1859 maybe_line_and_column (INT_MAX
, INT_MAX
);
1860 maybe_line_and_column (INT_MIN
, INT_MIN
);
1862 progname
= old_progname
;
1865 /* Selftest for num_digits. */
1870 ASSERT_EQ (1, num_digits (0));
1871 ASSERT_EQ (1, num_digits (9));
1872 ASSERT_EQ (2, num_digits (10));
1873 ASSERT_EQ (2, num_digits (99));
1874 ASSERT_EQ (3, num_digits (100));
1875 ASSERT_EQ (3, num_digits (999));
1876 ASSERT_EQ (4, num_digits (1000));
1877 ASSERT_EQ (4, num_digits (9999));
1878 ASSERT_EQ (5, num_digits (10000));
1879 ASSERT_EQ (5, num_digits (99999));
1880 ASSERT_EQ (6, num_digits (100000));
1881 ASSERT_EQ (6, num_digits (999999));
1882 ASSERT_EQ (7, num_digits (1000000));
1883 ASSERT_EQ (7, num_digits (9999999));
1884 ASSERT_EQ (8, num_digits (10000000));
1885 ASSERT_EQ (8, num_digits (99999999));
1888 /* Run all of the selftests within this file. */
1891 diagnostic_c_tests ()
1893 test_print_escaped_string ();
1894 test_print_parseable_fixits_none ();
1895 test_print_parseable_fixits_insert ();
1896 test_print_parseable_fixits_remove ();
1897 test_print_parseable_fixits_replace ();
1898 test_diagnostic_get_location_text ();
1903 } // namespace selftest
1905 #endif /* #if CHECKING_P */
1908 # pragma GCC diagnostic pop