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
->last_location
= UNKNOWN_LOCATION
;
204 context
->last_module
= 0;
205 context
->x_data
= NULL
;
207 context
->inhibit_notes_p
= false;
208 context
->colorize_source_p
= false;
209 context
->show_labels_p
= false;
210 context
->show_line_numbers_p
= false;
211 context
->min_margin_width
= 0;
212 context
->show_ruler_p
= false;
213 context
->parseable_fixits_p
= false;
214 context
->edit_context_ptr
= NULL
;
215 context
->diagnostic_group_nesting_depth
= 0;
216 context
->diagnostic_group_emission_count
= 0;
217 context
->begin_group_cb
= NULL
;
218 context
->end_group_cb
= NULL
;
219 context
->final_cb
= default_diagnostic_final_cb
;
222 /* Maybe initialize the color support. We require clients to do this
223 explicitly, since most clients don't want color. When called
224 without a VALUE, it initializes with DIAGNOSTICS_COLOR_DEFAULT. */
227 diagnostic_color_init (diagnostic_context
*context
, int value
/*= -1 */)
229 /* value == -1 is the default value. */
232 /* If DIAGNOSTICS_COLOR_DEFAULT is -1, default to
233 -fdiagnostics-color=auto if GCC_COLORS is in the environment,
234 otherwise default to -fdiagnostics-color=never, for other
235 values default to that
236 -fdiagnostics-color={never,auto,always}. */
237 if (DIAGNOSTICS_COLOR_DEFAULT
== -1)
239 if (!getenv ("GCC_COLORS"))
241 value
= DIAGNOSTICS_COLOR_AUTO
;
244 value
= DIAGNOSTICS_COLOR_DEFAULT
;
246 pp_show_color (context
->printer
)
247 = colorize_init ((diagnostic_color_rule_t
) value
);
250 /* Initialize URL support within CONTEXT based on VALUE, handling "auto". */
253 diagnostic_urls_init (diagnostic_context
*context
, int value
/*= -1 */)
256 value
= DIAGNOSTICS_COLOR_DEFAULT
;
258 context
->printer
->show_urls
259 = diagnostic_urls_enabled_p ((diagnostic_url_rule_t
) value
);
262 /* Do any cleaning up required after the last diagnostic is emitted. */
265 diagnostic_finish (diagnostic_context
*context
)
267 if (context
->final_cb
)
268 context
->final_cb (context
);
270 diagnostic_file_cache_fini ();
272 XDELETEVEC (context
->classify_diagnostic
);
273 context
->classify_diagnostic
= NULL
;
275 /* diagnostic_initialize allocates context->printer using XNEW
276 and placement-new. */
277 context
->printer
->~pretty_printer ();
278 XDELETE (context
->printer
);
279 context
->printer
= NULL
;
281 if (context
->edit_context_ptr
)
283 delete context
->edit_context_ptr
;
284 context
->edit_context_ptr
= NULL
;
288 /* Initialize DIAGNOSTIC, where the message MSG has already been
291 diagnostic_set_info_translated (diagnostic_info
*diagnostic
, const char *msg
,
292 va_list *args
, rich_location
*richloc
,
295 gcc_assert (richloc
);
296 diagnostic
->message
.err_no
= errno
;
297 diagnostic
->message
.args_ptr
= args
;
298 diagnostic
->message
.format_spec
= msg
;
299 diagnostic
->message
.m_richloc
= richloc
;
300 diagnostic
->richloc
= richloc
;
301 diagnostic
->kind
= kind
;
302 diagnostic
->option_index
= 0;
305 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
308 diagnostic_set_info (diagnostic_info
*diagnostic
, const char *gmsgid
,
309 va_list *args
, rich_location
*richloc
,
312 gcc_assert (richloc
);
313 diagnostic_set_info_translated (diagnostic
, _(gmsgid
), args
, richloc
, kind
);
316 static const char *const diagnostic_kind_color
[] = {
317 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
318 #include "diagnostic.def"
319 #undef DEFINE_DIAGNOSTIC_KIND
323 /* Get a color name for diagnostics of type KIND
324 Result could be NULL. */
327 diagnostic_get_color_for_kind (diagnostic_t kind
)
329 return diagnostic_kind_color
[kind
];
332 /* Return a formatted line and column ':%line:%column'. Elided if
333 zero. The result is a statically allocated buffer. */
336 maybe_line_and_column (int line
, int col
)
338 static char result
[32];
342 size_t l
= snprintf (result
, sizeof (result
),
343 col
? ":%d:%d" : ":%d", line
, col
);
344 gcc_checking_assert (l
< sizeof (result
));
351 /* Return a malloc'd string describing a location e.g. "foo.c:42:10".
352 The caller is responsible for freeing the memory. */
355 diagnostic_get_location_text (diagnostic_context
*context
,
358 pretty_printer
*pp
= context
->printer
;
359 const char *locus_cs
= colorize_start (pp_show_color (pp
), "locus");
360 const char *locus_ce
= colorize_stop (pp_show_color (pp
));
361 const char *file
= s
.file
? s
.file
: progname
;
362 int line
= strcmp (file
, N_("<built-in>")) ? s
.line
: 0;
363 int col
= context
->show_column
? s
.column
: 0;
365 const char *line_col
= maybe_line_and_column (line
, col
);
366 return build_message_string ("%s%s%s:%s", locus_cs
, file
,
370 /* Return a malloc'd string describing a location and the severity of the
371 diagnostic, e.g. "foo.c:42:10: error: ". The caller is responsible for
372 freeing the memory. */
374 diagnostic_build_prefix (diagnostic_context
*context
,
375 const diagnostic_info
*diagnostic
)
377 static const char *const diagnostic_kind_text
[] = {
378 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
379 #include "diagnostic.def"
380 #undef DEFINE_DIAGNOSTIC_KIND
383 gcc_assert (diagnostic
->kind
< DK_LAST_DIAGNOSTIC_KIND
);
385 const char *text
= _(diagnostic_kind_text
[diagnostic
->kind
]);
386 const char *text_cs
= "", *text_ce
= "";
387 pretty_printer
*pp
= context
->printer
;
389 if (diagnostic_kind_color
[diagnostic
->kind
])
391 text_cs
= colorize_start (pp_show_color (pp
),
392 diagnostic_kind_color
[diagnostic
->kind
]);
393 text_ce
= colorize_stop (pp_show_color (pp
));
396 expanded_location s
= diagnostic_expand_location (diagnostic
);
397 char *location_text
= diagnostic_get_location_text (context
, s
);
399 char *result
= build_message_string ("%s %s%s%s", location_text
,
400 text_cs
, text
, text_ce
);
401 free (location_text
);
405 /* Functions at which to stop the backtrace print. It's not
406 particularly helpful to print the callers of these functions. */
408 static const char * const bt_stop
[] =
416 /* A callback function passed to the backtrace_full function. */
419 bt_callback (void *data
, uintptr_t pc
, const char *filename
, int lineno
,
420 const char *function
)
422 int *pcount
= (int *) data
;
424 /* If we don't have any useful information, don't print
426 if (filename
== NULL
&& function
== NULL
)
429 /* Skip functions in diagnostic.c. */
432 && strcmp (lbasename (filename
), "diagnostic.c") == 0)
435 /* Print up to 20 functions. We could make this a --param, but
436 since this is only for debugging just use a constant for now. */
439 /* Returning a non-zero value stops the backtrace. */
445 if (function
!= NULL
)
447 char *str
= cplus_demangle_v3 (function
,
448 (DMGL_VERBOSE
| DMGL_ANSI
449 | DMGL_GNU_V3
| DMGL_PARAMS
));
456 for (size_t i
= 0; i
< ARRAY_SIZE (bt_stop
); ++i
)
458 size_t len
= strlen (bt_stop
[i
]);
459 if (strncmp (function
, bt_stop
[i
], len
) == 0
460 && (function
[len
] == '\0' || function
[len
] == '('))
464 /* Returning a non-zero value stops the backtrace. */
470 fprintf (stderr
, "0x%lx %s\n\t%s:%d\n",
472 function
== NULL
? "???" : function
,
473 filename
== NULL
? "???" : filename
,
482 /* A callback function passed to the backtrace_full function. This is
483 called if backtrace_full has an error. */
486 bt_err_callback (void *data ATTRIBUTE_UNUSED
, const char *msg
, int errnum
)
490 /* This means that no debug info was available. Just quietly
491 skip printing backtrace info. */
494 fprintf (stderr
, "%s%s%s\n", msg
, errnum
== 0 ? "" : ": ",
495 errnum
== 0 ? "" : xstrerror (errnum
));
498 /* Check if we've met the maximum error limit, and if so fatally exit
499 with a message. CONTEXT is the context to check, and FLUSH
500 indicates whether a diagnostic_finish call is needed. */
503 diagnostic_check_max_errors (diagnostic_context
*context
, bool flush
)
505 if (!context
->max_errors
)
508 int count
= (diagnostic_kind_count (context
, DK_ERROR
)
509 + diagnostic_kind_count (context
, DK_SORRY
)
510 + diagnostic_kind_count (context
, DK_WERROR
));
512 if (count
>= context
->max_errors
)
515 "compilation terminated due to -fmax-errors=%u.\n",
516 context
->max_errors
);
518 diagnostic_finish (context
);
519 exit (FATAL_EXIT_CODE
);
523 /* Take any action which is expected to happen after the diagnostic
524 is written out. This function does not always return. */
526 diagnostic_action_after_output (diagnostic_context
*context
,
527 diagnostic_t diag_kind
)
539 if (context
->abort_on_error
)
541 if (context
->fatal_errors
)
543 fnotice (stderr
, "compilation terminated due to -Wfatal-errors.\n");
544 diagnostic_finish (context
);
545 exit (FATAL_EXIT_CODE
);
552 struct backtrace_state
*state
= NULL
;
553 if (diag_kind
== DK_ICE
)
554 state
= backtrace_create_state (NULL
, 0, bt_err_callback
, NULL
);
557 backtrace_full (state
, 2, bt_callback
, bt_err_callback
,
560 if (context
->abort_on_error
)
563 fnotice (stderr
, "Please submit a full bug report,\n"
564 "with preprocessed source if appropriate.\n");
567 ("Please include the complete backtrace "
568 "with any bug report.\n"));
569 fnotice (stderr
, "See %s for instructions.\n", bug_report_url
);
571 exit (ICE_EXIT_CODE
);
575 if (context
->abort_on_error
)
577 diagnostic_finish (context
);
578 fnotice (stderr
, "compilation terminated.\n");
579 exit (FATAL_EXIT_CODE
);
586 /* True if the last module or file in which a diagnostic was reported is
587 different from the current one. */
590 last_module_changed_p (diagnostic_context
*context
,
591 const line_map_ordinary
*map
)
593 return context
->last_module
!= map
;
596 /* Remember the current module or file as being the last one in which we
597 report a diagnostic. */
600 set_last_module (diagnostic_context
*context
, const line_map_ordinary
*map
)
602 context
->last_module
= map
;
606 diagnostic_report_current_module (diagnostic_context
*context
, location_t where
)
608 const line_map_ordinary
*map
= NULL
;
610 if (pp_needs_newline (context
->printer
))
612 pp_newline (context
->printer
);
613 pp_needs_newline (context
->printer
) = false;
616 if (where
<= BUILTINS_LOCATION
)
619 linemap_resolve_location (line_table
, where
,
620 LRK_MACRO_DEFINITION_LOCATION
,
623 if (map
&& last_module_changed_p (context
, map
))
625 set_last_module (context
, map
);
626 if (! MAIN_FILE_P (map
))
631 where
= linemap_included_from (map
);
632 map
= linemap_included_from_linemap (line_table
, map
);
634 = maybe_line_and_column (SOURCE_LINE (map
, where
),
635 first
&& context
->show_column
636 ? SOURCE_COLUMN (map
, where
) : 0);
637 static const char *const msgs
[] =
639 N_("In file included from"),
642 unsigned index
= !first
;
643 pp_verbatim (context
->printer
, "%s%s %r%s%s%R",
644 first
? "" : ",\n", _(msgs
[index
]),
645 "locus", LINEMAP_FILE (map
), line_col
);
648 while (! MAIN_FILE_P (map
));
649 pp_verbatim (context
->printer
, ":");
650 pp_newline (context
->printer
);
656 default_diagnostic_starter (diagnostic_context
*context
,
657 diagnostic_info
*diagnostic
)
659 diagnostic_report_current_module (context
, diagnostic_location (diagnostic
));
660 pp_set_prefix (context
->printer
, diagnostic_build_prefix (context
,
665 default_diagnostic_start_span_fn (diagnostic_context
*context
,
666 expanded_location exploc
)
668 char *text
= diagnostic_get_location_text (context
, exploc
);
669 pp_string (context
->printer
, text
);
671 pp_newline (context
->printer
);
675 default_diagnostic_finalizer (diagnostic_context
*context
,
676 diagnostic_info
*diagnostic
,
679 char *saved_prefix
= pp_take_prefix (context
->printer
);
680 pp_set_prefix (context
->printer
, NULL
);
681 diagnostic_show_locus (context
, diagnostic
->richloc
, diagnostic
->kind
);
682 pp_set_prefix (context
->printer
, saved_prefix
);
683 pp_flush (context
->printer
);
686 /* Interface to specify diagnostic kind overrides. Returns the
687 previous setting, or DK_UNSPECIFIED if the parameters are out of
688 range. If OPTION_INDEX is zero, the new setting is for all the
691 diagnostic_classify_diagnostic (diagnostic_context
*context
,
693 diagnostic_t new_kind
,
696 diagnostic_t old_kind
;
699 || option_index
>= context
->n_opts
700 || new_kind
>= DK_LAST_DIAGNOSTIC_KIND
)
701 return DK_UNSPECIFIED
;
703 old_kind
= context
->classify_diagnostic
[option_index
];
705 /* Handle pragmas separately, since we need to keep track of *where*
707 if (where
!= UNKNOWN_LOCATION
)
711 /* Record the command-line status, so we can reset it back on DK_POP. */
712 if (old_kind
== DK_UNSPECIFIED
)
714 old_kind
= !context
->option_enabled (option_index
,
716 context
->option_state
)
717 ? DK_IGNORED
: (context
->warning_as_error_requested
718 ? DK_ERROR
: DK_WARNING
);
719 context
->classify_diagnostic
[option_index
] = old_kind
;
722 for (i
= context
->n_classification_history
- 1; i
>= 0; i
--)
723 if (context
->classification_history
[i
].option
== option_index
)
725 old_kind
= context
->classification_history
[i
].kind
;
729 i
= context
->n_classification_history
;
730 context
->classification_history
=
731 (diagnostic_classification_change_t
*) xrealloc (context
->classification_history
, (i
+ 1)
732 * sizeof (diagnostic_classification_change_t
));
733 context
->classification_history
[i
].location
= where
;
734 context
->classification_history
[i
].option
= option_index
;
735 context
->classification_history
[i
].kind
= new_kind
;
736 context
->n_classification_history
++;
739 context
->classify_diagnostic
[option_index
] = new_kind
;
744 /* Save all diagnostic classifications in a stack. */
746 diagnostic_push_diagnostics (diagnostic_context
*context
, location_t where ATTRIBUTE_UNUSED
)
748 context
->push_list
= (int *) xrealloc (context
->push_list
, (context
->n_push
+ 1) * sizeof (int));
749 context
->push_list
[context
->n_push
++] = context
->n_classification_history
;
752 /* Restore the topmost classification set off the stack. If the stack
753 is empty, revert to the state based on command line parameters. */
755 diagnostic_pop_diagnostics (diagnostic_context
*context
, location_t where
)
761 jump_to
= context
->push_list
[-- context
->n_push
];
765 i
= context
->n_classification_history
;
766 context
->classification_history
=
767 (diagnostic_classification_change_t
*) xrealloc (context
->classification_history
, (i
+ 1)
768 * sizeof (diagnostic_classification_change_t
));
769 context
->classification_history
[i
].location
= where
;
770 context
->classification_history
[i
].option
= jump_to
;
771 context
->classification_history
[i
].kind
= DK_POP
;
772 context
->n_classification_history
++;
775 /* Helper function for print_parseable_fixits. Print TEXT to PP, obeying the
776 escaping rules for -fdiagnostics-parseable-fixits. */
779 print_escaped_string (pretty_printer
*pp
, const char *text
)
784 pp_character (pp
, '"');
785 for (const char *ch
= text
; *ch
; ch
++)
790 /* Escape backslash as two backslashes. */
791 pp_string (pp
, "\\\\");
794 /* Escape tab as "\t". */
795 pp_string (pp
, "\\t");
798 /* Escape newline as "\n". */
799 pp_string (pp
, "\\n");
802 /* Escape doublequotes as \". */
803 pp_string (pp
, "\\\"");
807 pp_character (pp
, *ch
);
809 /* Use octal for non-printable chars. */
811 unsigned char c
= (*ch
& 0xff);
812 pp_printf (pp
, "\\%o%o%o", (c
/ 64), (c
/ 8) & 007, c
& 007);
817 pp_character (pp
, '"');
820 /* Implementation of -fdiagnostics-parseable-fixits. Print a
821 machine-parseable version of all fixits in RICHLOC to PP. */
824 print_parseable_fixits (pretty_printer
*pp
, rich_location
*richloc
)
827 gcc_assert (richloc
);
829 char *saved_prefix
= pp_take_prefix (pp
);
830 pp_set_prefix (pp
, NULL
);
832 for (unsigned i
= 0; i
< richloc
->get_num_fixit_hints (); i
++)
834 const fixit_hint
*hint
= richloc
->get_fixit_hint (i
);
835 location_t start_loc
= hint
->get_start_loc ();
836 expanded_location start_exploc
= expand_location (start_loc
);
837 pp_string (pp
, "fix-it:");
838 print_escaped_string (pp
, start_exploc
.file
);
839 /* For compatibility with clang, print as a half-open range. */
840 location_t next_loc
= hint
->get_next_loc ();
841 expanded_location next_exploc
= expand_location (next_loc
);
842 pp_printf (pp
, ":{%i:%i-%i:%i}:",
843 start_exploc
.line
, start_exploc
.column
,
844 next_exploc
.line
, next_exploc
.column
);
845 print_escaped_string (pp
, hint
->get_string ());
849 pp_set_prefix (pp
, saved_prefix
);
852 /* Update the diag_class of DIAGNOSTIC based on its location
854 #pragma GCC diagnostic
855 directives recorded within CONTEXT.
857 Return the new diag_class of DIAGNOSTIC if it was updated, or
858 DK_UNSPECIFIED otherwise. */
861 update_effective_level_from_pragmas (diagnostic_context
*context
,
862 diagnostic_info
*diagnostic
)
864 diagnostic_t diag_class
= DK_UNSPECIFIED
;
866 if (context
->n_classification_history
> 0)
868 location_t location
= diagnostic_location (diagnostic
);
870 /* FIXME: Stupid search. Optimize later. */
871 for (int i
= context
->n_classification_history
- 1; i
>= 0; i
--)
873 if (linemap_location_before_p
875 context
->classification_history
[i
].location
,
878 if (context
->classification_history
[i
].kind
== (int) DK_POP
)
880 i
= context
->classification_history
[i
].option
;
883 int option
= context
->classification_history
[i
].option
;
884 /* The option 0 is for all the diagnostics. */
885 if (option
== 0 || option
== diagnostic
->option_index
)
887 diag_class
= context
->classification_history
[i
].kind
;
888 if (diag_class
!= DK_UNSPECIFIED
)
889 diagnostic
->kind
= diag_class
;
899 /* Print any metadata about the option used to control DIAGNOSTIC to CONTEXT's
900 printer, e.g. " [-Werror=uninitialized]".
901 Subroutine of diagnostic_report_diagnostic. */
904 print_option_information (diagnostic_context
*context
,
905 const diagnostic_info
*diagnostic
,
906 diagnostic_t orig_diag_kind
)
910 option_text
= context
->option_name (context
, diagnostic
->option_index
,
911 orig_diag_kind
, diagnostic
->kind
);
915 pretty_printer
*pp
= context
->printer
;
916 pp_string (pp
, " [");
917 pp_string (pp
, colorize_start (pp_show_color (pp
),
918 diagnostic_kind_color
[diagnostic
->kind
]));
919 pp_string (pp
, option_text
);
920 pp_string (pp
, colorize_stop (pp_show_color (pp
)));
921 pp_character (pp
, ']');
926 /* Report a diagnostic message (an error or a warning) as specified by
927 DC. This function is *the* subroutine in terms of which front-ends
928 should implement their specific diagnostic handling modules. The
929 front-end independent format specifiers are exactly those described
930 in the documentation of output_format.
931 Return true if a diagnostic was printed, false otherwise. */
934 diagnostic_report_diagnostic (diagnostic_context
*context
,
935 diagnostic_info
*diagnostic
)
937 location_t location
= diagnostic_location (diagnostic
);
938 diagnostic_t orig_diag_kind
= diagnostic
->kind
;
940 /* Give preference to being able to inhibit warnings, before they
941 get reclassified to something else. */
942 if ((diagnostic
->kind
== DK_WARNING
|| diagnostic
->kind
== DK_PEDWARN
)
943 && !diagnostic_report_warnings_p (context
, location
))
946 if (diagnostic
->kind
== DK_PEDWARN
)
948 diagnostic
->kind
= pedantic_warning_kind (context
);
949 /* We do this to avoid giving the message for -pedantic-errors. */
950 orig_diag_kind
= diagnostic
->kind
;
953 if (diagnostic
->kind
== DK_NOTE
&& context
->inhibit_notes_p
)
956 if (context
->lock
> 0)
958 /* If we're reporting an ICE in the middle of some other error,
959 try to flush out the previous error, then let this one
960 through. Don't do this more than once. */
961 if ((diagnostic
->kind
== DK_ICE
|| diagnostic
->kind
== DK_ICE_NOBT
)
962 && context
->lock
== 1)
963 pp_newline_and_flush (context
->printer
);
965 error_recursion (context
);
968 /* If the user requested that warnings be treated as errors, so be
969 it. Note that we do this before the next block so that
970 individual warnings can be overridden back to warnings with
972 if (context
->warning_as_error_requested
973 && diagnostic
->kind
== DK_WARNING
)
974 diagnostic
->kind
= DK_ERROR
;
976 if (diagnostic
->option_index
977 && diagnostic
->option_index
!= permissive_error_option (context
))
979 /* This tests if the user provided the appropriate -Wfoo or
981 if (! context
->option_enabled (diagnostic
->option_index
,
983 context
->option_state
))
986 /* This tests for #pragma diagnostic changes. */
987 diagnostic_t diag_class
988 = update_effective_level_from_pragmas (context
, diagnostic
);
990 /* This tests if the user provided the appropriate -Werror=foo
992 if (diag_class
== DK_UNSPECIFIED
993 && (context
->classify_diagnostic
[diagnostic
->option_index
]
996 = context
->classify_diagnostic
[diagnostic
->option_index
];
998 /* This allows for future extensions, like temporarily disabling
999 warnings for ranges of source code. */
1000 if (diagnostic
->kind
== DK_IGNORED
)
1004 if (diagnostic
->kind
!= DK_NOTE
)
1005 diagnostic_check_max_errors (context
);
1009 if (diagnostic
->kind
== DK_ICE
|| diagnostic
->kind
== DK_ICE_NOBT
)
1011 /* When not checking, ICEs are converted to fatal errors when an
1012 error has already occurred. This is counteracted by
1015 && (diagnostic_kind_count (context
, DK_ERROR
) > 0
1016 || diagnostic_kind_count (context
, DK_SORRY
) > 0)
1017 && !context
->abort_on_error
)
1020 = expand_location (diagnostic_location (diagnostic
));
1021 fnotice (stderr
, "%s:%d: confused by earlier errors, bailing out\n",
1023 exit (ICE_EXIT_CODE
);
1025 if (context
->internal_error
)
1026 (*context
->internal_error
) (context
,
1027 diagnostic
->message
.format_spec
,
1028 diagnostic
->message
.args_ptr
);
1030 if (diagnostic
->kind
== DK_ERROR
&& orig_diag_kind
== DK_WARNING
)
1031 ++diagnostic_kind_count (context
, DK_WERROR
);
1033 ++diagnostic_kind_count (context
, diagnostic
->kind
);
1035 /* Is this the initial diagnostic within the stack of groups? */
1036 if (context
->diagnostic_group_emission_count
== 0)
1038 if (context
->begin_group_cb
)
1039 context
->begin_group_cb (context
);
1041 context
->diagnostic_group_emission_count
++;
1043 diagnostic
->message
.x_data
= &diagnostic
->x_data
;
1044 diagnostic
->x_data
= NULL
;
1045 pp_format (context
->printer
, &diagnostic
->message
);
1046 (*diagnostic_starter (context
)) (context
, diagnostic
);
1047 pp_output_formatted_text (context
->printer
);
1048 if (context
->show_option_requested
)
1049 print_option_information (context
, diagnostic
, orig_diag_kind
);
1050 (*diagnostic_finalizer (context
)) (context
, diagnostic
, orig_diag_kind
);
1051 if (context
->parseable_fixits_p
)
1053 print_parseable_fixits (context
->printer
, diagnostic
->richloc
);
1054 pp_flush (context
->printer
);
1056 diagnostic_action_after_output (context
, diagnostic
->kind
);
1057 diagnostic
->x_data
= NULL
;
1059 if (context
->edit_context_ptr
)
1060 if (diagnostic
->richloc
->fixits_can_be_auto_applied_p ())
1061 context
->edit_context_ptr
->add_fixits (diagnostic
->richloc
);
1068 /* Get the number of digits in the decimal representation of VALUE. */
1071 num_digits (int value
)
1073 /* Perhaps simpler to use log10 for this, but doing it this way avoids
1074 using floating point. */
1075 gcc_assert (value
>= 0);
1089 /* Given a partial pathname as input, return another pathname that
1090 shares no directory elements with the pathname of __FILE__. This
1091 is used by fancy_abort() to print `Internal compiler error in expr.c'
1092 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1095 trim_filename (const char *name
)
1097 static const char this_file
[] = __FILE__
;
1098 const char *p
= name
, *q
= this_file
;
1100 /* First skip any "../" in each filename. This allows us to give a proper
1101 reference to a file in a subdirectory. */
1102 while (p
[0] == '.' && p
[1] == '.' && IS_DIR_SEPARATOR (p
[2]))
1105 while (q
[0] == '.' && q
[1] == '.' && IS_DIR_SEPARATOR (q
[2]))
1108 /* Now skip any parts the two filenames have in common. */
1109 while (*p
== *q
&& *p
!= 0 && *q
!= 0)
1112 /* Now go backwards until the previous directory separator. */
1113 while (p
> name
&& !IS_DIR_SEPARATOR (p
[-1]))
1119 /* Standard error reporting routines in increasing order of severity.
1120 All of these take arguments like printf. */
1122 /* Text to be emitted verbatim to the error message stream; this
1123 produces no prefix and disables line-wrapping. Use rarely. */
1125 verbatim (const char *gmsgid
, ...)
1130 va_start (ap
, gmsgid
);
1131 text
.err_no
= errno
;
1132 text
.args_ptr
= &ap
;
1133 text
.format_spec
= _(gmsgid
);
1135 pp_format_verbatim (global_dc
->printer
, &text
);
1136 pp_newline_and_flush (global_dc
->printer
);
1140 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
1142 diagnostic_append_note (diagnostic_context
*context
,
1143 location_t location
,
1144 const char * gmsgid
, ...)
1146 diagnostic_info diagnostic
;
1148 rich_location
richloc (line_table
, location
);
1150 va_start (ap
, gmsgid
);
1151 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_NOTE
);
1152 if (context
->inhibit_notes_p
)
1157 char *saved_prefix
= pp_take_prefix (context
->printer
);
1158 pp_set_prefix (context
->printer
,
1159 diagnostic_build_prefix (context
, &diagnostic
));
1160 pp_format (context
->printer
, &diagnostic
.message
);
1161 pp_output_formatted_text (context
->printer
);
1162 pp_destroy_prefix (context
->printer
);
1163 pp_set_prefix (context
->printer
, saved_prefix
);
1164 diagnostic_show_locus (context
, &richloc
, DK_NOTE
);
1168 /* Implement emit_diagnostic, inform, warning, warning_at, pedwarn,
1169 permerror, error, error_at, error_at, sorry, fatal_error, internal_error,
1170 and internal_error_no_backtrace, as documented and defined below. */
1172 diagnostic_impl (rich_location
*richloc
, int opt
,
1174 va_list *ap
, diagnostic_t kind
)
1176 diagnostic_info diagnostic
;
1177 if (kind
== DK_PERMERROR
)
1179 diagnostic_set_info (&diagnostic
, gmsgid
, ap
, richloc
,
1180 permissive_error_kind (global_dc
));
1181 diagnostic
.option_index
= permissive_error_option (global_dc
);
1185 diagnostic_set_info (&diagnostic
, gmsgid
, ap
, richloc
, kind
);
1186 if (kind
== DK_WARNING
|| kind
== DK_PEDWARN
)
1187 diagnostic
.option_index
= opt
;
1189 return diagnostic_report_diagnostic (global_dc
, &diagnostic
);
1192 /* Implement inform_n, warning_n, and error_n, as documented and
1195 diagnostic_n_impl (rich_location
*richloc
, int opt
, unsigned HOST_WIDE_INT n
,
1196 const char *singular_gmsgid
,
1197 const char *plural_gmsgid
,
1198 va_list *ap
, diagnostic_t kind
)
1200 diagnostic_info diagnostic
;
1203 if (sizeof n
<= sizeof gtn
)
1206 /* Use the largest number ngettext can handle, otherwise
1207 preserve the six least significant decimal digits for
1208 languages where the plural form depends on them. */
1209 gtn
= n
<= ULONG_MAX
? n
: n
% 1000000LU + 1000000LU;
1211 const char *text
= ngettext (singular_gmsgid
, plural_gmsgid
, gtn
);
1212 diagnostic_set_info_translated (&diagnostic
, text
, ap
, richloc
, kind
);
1213 if (kind
== DK_WARNING
)
1214 diagnostic
.option_index
= opt
;
1215 return diagnostic_report_diagnostic (global_dc
, &diagnostic
);
1218 /* Wrapper around diagnostic_impl taking a variable argument list. */
1221 emit_diagnostic (diagnostic_t kind
, location_t location
, int opt
,
1222 const char *gmsgid
, ...)
1224 auto_diagnostic_group d
;
1226 va_start (ap
, gmsgid
);
1227 rich_location
richloc (line_table
, location
);
1228 bool ret
= diagnostic_impl (&richloc
, opt
, gmsgid
, &ap
, kind
);
1233 /* As above, but for rich_location *. */
1236 emit_diagnostic (diagnostic_t kind
, rich_location
*richloc
, int opt
,
1237 const char *gmsgid
, ...)
1239 auto_diagnostic_group d
;
1241 va_start (ap
, gmsgid
);
1242 bool ret
= diagnostic_impl (richloc
, opt
, gmsgid
, &ap
, kind
);
1247 /* Wrapper around diagnostic_impl taking a va_list parameter. */
1250 emit_diagnostic_valist (diagnostic_t kind
, location_t location
, int opt
,
1251 const char *gmsgid
, va_list *ap
)
1253 rich_location
richloc (line_table
, location
);
1254 return diagnostic_impl (&richloc
, opt
, gmsgid
, ap
, kind
);
1257 /* An informative note at LOCATION. Use this for additional details on an error
1260 inform (location_t location
, const char *gmsgid
, ...)
1262 auto_diagnostic_group d
;
1264 va_start (ap
, gmsgid
);
1265 rich_location
richloc (line_table
, location
);
1266 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_NOTE
);
1270 /* Same as "inform" above, but at RICHLOC. */
1272 inform (rich_location
*richloc
, const char *gmsgid
, ...)
1274 gcc_assert (richloc
);
1276 auto_diagnostic_group d
;
1278 va_start (ap
, gmsgid
);
1279 diagnostic_impl (richloc
, -1, gmsgid
, &ap
, DK_NOTE
);
1283 /* An informative note at LOCATION. Use this for additional details on an
1286 inform_n (location_t location
, unsigned HOST_WIDE_INT n
,
1287 const char *singular_gmsgid
, const char *plural_gmsgid
, ...)
1290 va_start (ap
, plural_gmsgid
);
1291 auto_diagnostic_group d
;
1292 rich_location
richloc (line_table
, location
);
1293 diagnostic_n_impl (&richloc
, -1, n
, singular_gmsgid
, plural_gmsgid
,
1298 /* A warning at INPUT_LOCATION. Use this for code which is correct according
1299 to the relevant language specification but is likely to be buggy anyway.
1300 Returns true if the warning was printed, false if it was inhibited. */
1302 warning (int opt
, const char *gmsgid
, ...)
1304 auto_diagnostic_group d
;
1306 va_start (ap
, gmsgid
);
1307 rich_location
richloc (line_table
, input_location
);
1308 bool ret
= diagnostic_impl (&richloc
, opt
, gmsgid
, &ap
, DK_WARNING
);
1313 /* A warning at LOCATION. Use this for code which is correct according to the
1314 relevant language specification but is likely to be buggy anyway.
1315 Returns true if the warning was printed, false if it was inhibited. */
1318 warning_at (location_t location
, int opt
, const char *gmsgid
, ...)
1320 auto_diagnostic_group d
;
1322 va_start (ap
, gmsgid
);
1323 rich_location
richloc (line_table
, location
);
1324 bool ret
= diagnostic_impl (&richloc
, opt
, gmsgid
, &ap
, DK_WARNING
);
1329 /* Same as "warning at" above, but using RICHLOC. */
1332 warning_at (rich_location
*richloc
, int opt
, const char *gmsgid
, ...)
1334 gcc_assert (richloc
);
1336 auto_diagnostic_group d
;
1338 va_start (ap
, gmsgid
);
1339 bool ret
= diagnostic_impl (richloc
, opt
, gmsgid
, &ap
, DK_WARNING
);
1344 /* Same as warning_n plural variant below, but using RICHLOC. */
1347 warning_n (rich_location
*richloc
, int opt
, unsigned HOST_WIDE_INT n
,
1348 const char *singular_gmsgid
, const char *plural_gmsgid
, ...)
1350 gcc_assert (richloc
);
1352 auto_diagnostic_group d
;
1354 va_start (ap
, plural_gmsgid
);
1355 bool ret
= diagnostic_n_impl (richloc
, opt
, n
,
1356 singular_gmsgid
, plural_gmsgid
,
1362 /* A warning at LOCATION. Use this for code which is correct according to the
1363 relevant language specification but is likely to be buggy anyway.
1364 Returns true if the warning was printed, false if it was inhibited. */
1367 warning_n (location_t location
, int opt
, unsigned HOST_WIDE_INT n
,
1368 const char *singular_gmsgid
, const char *plural_gmsgid
, ...)
1370 auto_diagnostic_group d
;
1372 va_start (ap
, plural_gmsgid
);
1373 rich_location
richloc (line_table
, location
);
1374 bool ret
= diagnostic_n_impl (&richloc
, opt
, n
,
1375 singular_gmsgid
, plural_gmsgid
,
1381 /* A "pedantic" warning at LOCATION: issues a warning unless
1382 -pedantic-errors was given on the command line, in which case it
1383 issues an error. Use this for diagnostics required by the relevant
1384 language standard, if you have chosen not to make them errors.
1386 Note that these diagnostics are issued independent of the setting
1387 of the -Wpedantic command-line switch. To get a warning enabled
1388 only with that switch, use either "if (pedantic) pedwarn
1389 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
1390 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
1392 Returns true if the warning was printed, false if it was inhibited. */
1395 pedwarn (location_t location
, int opt
, const char *gmsgid
, ...)
1397 auto_diagnostic_group d
;
1399 va_start (ap
, gmsgid
);
1400 rich_location
richloc (line_table
, location
);
1401 bool ret
= diagnostic_impl (&richloc
, opt
, gmsgid
, &ap
, DK_PEDWARN
);
1406 /* Same as pedwarn above, but using RICHLOC. */
1409 pedwarn (rich_location
*richloc
, int opt
, const char *gmsgid
, ...)
1411 gcc_assert (richloc
);
1413 auto_diagnostic_group d
;
1415 va_start (ap
, gmsgid
);
1416 bool ret
= diagnostic_impl (richloc
, opt
, gmsgid
, &ap
, DK_PEDWARN
);
1421 /* A "permissive" error at LOCATION: issues an error unless
1422 -fpermissive was given on the command line, in which case it issues
1423 a warning. Use this for things that really should be errors but we
1424 want to support legacy code.
1426 Returns true if the warning was printed, false if it was inhibited. */
1429 permerror (location_t location
, const char *gmsgid
, ...)
1431 auto_diagnostic_group d
;
1433 va_start (ap
, gmsgid
);
1434 rich_location
richloc (line_table
, location
);
1435 bool ret
= diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_PERMERROR
);
1440 /* Same as "permerror" above, but at RICHLOC. */
1443 permerror (rich_location
*richloc
, const char *gmsgid
, ...)
1445 gcc_assert (richloc
);
1447 auto_diagnostic_group d
;
1449 va_start (ap
, gmsgid
);
1450 bool ret
= diagnostic_impl (richloc
, -1, gmsgid
, &ap
, DK_PERMERROR
);
1455 /* A hard error: the code is definitely ill-formed, and an object file
1456 will not be produced. */
1458 error (const char *gmsgid
, ...)
1460 auto_diagnostic_group d
;
1462 va_start (ap
, gmsgid
);
1463 rich_location
richloc (line_table
, input_location
);
1464 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_ERROR
);
1468 /* A hard error: the code is definitely ill-formed, and an object file
1469 will not be produced. */
1471 error_n (location_t location
, unsigned HOST_WIDE_INT n
,
1472 const char *singular_gmsgid
, const char *plural_gmsgid
, ...)
1474 auto_diagnostic_group d
;
1476 va_start (ap
, plural_gmsgid
);
1477 rich_location
richloc (line_table
, location
);
1478 diagnostic_n_impl (&richloc
, -1, n
, singular_gmsgid
, plural_gmsgid
,
1483 /* Same as above, but use location LOC instead of input_location. */
1485 error_at (location_t loc
, const char *gmsgid
, ...)
1487 auto_diagnostic_group d
;
1489 va_start (ap
, gmsgid
);
1490 rich_location
richloc (line_table
, loc
);
1491 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_ERROR
);
1495 /* Same as above, but use RICH_LOC. */
1498 error_at (rich_location
*richloc
, const char *gmsgid
, ...)
1500 gcc_assert (richloc
);
1502 auto_diagnostic_group d
;
1504 va_start (ap
, gmsgid
);
1505 diagnostic_impl (richloc
, -1, gmsgid
, &ap
, DK_ERROR
);
1509 /* "Sorry, not implemented." Use for a language feature which is
1510 required by the relevant specification but not implemented by GCC.
1511 An object file will not be produced. */
1513 sorry (const char *gmsgid
, ...)
1515 auto_diagnostic_group d
;
1517 va_start (ap
, gmsgid
);
1518 rich_location
richloc (line_table
, input_location
);
1519 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_SORRY
);
1523 /* Same as above, but use location LOC instead of input_location. */
1525 sorry_at (location_t loc
, const char *gmsgid
, ...)
1527 auto_diagnostic_group d
;
1529 va_start (ap
, gmsgid
);
1530 rich_location
richloc (line_table
, loc
);
1531 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_SORRY
);
1535 /* Return true if an error or a "sorry" has been seen. Various
1536 processing is disabled after errors. */
1540 return errorcount
|| sorrycount
;
1543 /* An error which is severe enough that we make no attempt to
1544 continue. Do not use this for internal consistency checks; that's
1545 internal_error. Use of this function should be rare. */
1547 fatal_error (location_t loc
, const char *gmsgid
, ...)
1549 auto_diagnostic_group d
;
1551 va_start (ap
, gmsgid
);
1552 rich_location
richloc (line_table
, loc
);
1553 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_FATAL
);
1559 /* An internal consistency check has failed. We make no attempt to
1560 continue. Note that unless there is debugging value to be had from
1561 a more specific message, or some other good reason, you should use
1562 abort () instead of calling this function directly. */
1564 internal_error (const char *gmsgid
, ...)
1566 auto_diagnostic_group d
;
1568 va_start (ap
, gmsgid
);
1569 rich_location
richloc (line_table
, input_location
);
1570 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_ICE
);
1576 /* Like internal_error, but no backtrace will be printed. Used when
1577 the internal error does not happen at the current location, but happened
1580 internal_error_no_backtrace (const char *gmsgid
, ...)
1582 auto_diagnostic_group d
;
1584 va_start (ap
, gmsgid
);
1585 rich_location
richloc (line_table
, input_location
);
1586 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_ICE_NOBT
);
1592 /* Special case error functions. Most are implemented in terms of the
1593 above, or should be. */
1595 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1596 runs its second argument through gettext. */
1598 fnotice (FILE *file
, const char *cmsgid
, ...)
1602 va_start (ap
, cmsgid
);
1603 vfprintf (file
, _(cmsgid
), ap
);
1607 /* Inform the user that an error occurred while trying to report some
1608 other error. This indicates catastrophic internal inconsistencies,
1609 so give up now. But do try to flush out the previous error.
1610 This mustn't use internal_error, that will cause infinite recursion. */
1613 error_recursion (diagnostic_context
*context
)
1615 if (context
->lock
< 3)
1616 pp_newline_and_flush (context
->printer
);
1619 "Internal compiler error: Error reporting routines re-entered.\n");
1621 /* Call diagnostic_action_after_output to get the "please submit a bug
1623 diagnostic_action_after_output (context
, DK_ICE
);
1625 /* Do not use gcc_unreachable here; that goes through internal_error
1626 and therefore would cause infinite recursion. */
1630 /* Report an internal compiler error in a friendly manner. This is
1631 the function that gets called upon use of abort() in the source
1632 code generally, thanks to a special macro. */
1635 fancy_abort (const char *file
, int line
, const char *function
)
1637 internal_error ("in %s, at %s:%d", function
, trim_filename (file
), line
);
1640 /* class auto_diagnostic_group. */
1642 /* Constructor: "push" this group into global_dc. */
1644 auto_diagnostic_group::auto_diagnostic_group ()
1646 global_dc
->diagnostic_group_nesting_depth
++;
1649 /* Destructor: "pop" this group from global_dc. */
1651 auto_diagnostic_group::~auto_diagnostic_group ()
1653 if (--global_dc
->diagnostic_group_nesting_depth
== 0)
1655 /* Handle the case where we've popped the final diagnostic group.
1656 If any diagnostics were emitted, give the context a chance
1658 if (global_dc
->diagnostic_group_emission_count
> 0)
1660 if (global_dc
->end_group_cb
)
1661 global_dc
->end_group_cb (global_dc
);
1663 global_dc
->diagnostic_group_emission_count
= 0;
1667 /* Really call the system 'abort'. This has to go right at the end of
1668 this file, so that there are no functions after it that call abort
1669 and get the system abort instead of our macro. */
1679 namespace selftest
{
1681 /* Helper function for test_print_escaped_string. */
1684 assert_print_escaped_string (const location
&loc
, const char *expected_output
,
1688 print_escaped_string (&pp
, input
);
1689 ASSERT_STREQ_AT (loc
, expected_output
, pp_formatted_text (&pp
));
1692 #define ASSERT_PRINT_ESCAPED_STRING_STREQ(EXPECTED_OUTPUT, INPUT) \
1693 assert_print_escaped_string (SELFTEST_LOCATION, EXPECTED_OUTPUT, INPUT)
1695 /* Tests of print_escaped_string. */
1698 test_print_escaped_string ()
1701 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"\"", "");
1703 /* Non-empty string. */
1704 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"hello world\"", "hello world");
1706 /* Various things that need to be escaped: */
1708 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\\after\"",
1711 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\tafter\"",
1714 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\nafter\"",
1717 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\"after\"",
1720 /* Non-printable characters: BEL: '\a': 0x07 */
1721 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\007after\"",
1723 /* Non-printable characters: vertical tab: '\v': 0x0b */
1724 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\013after\"",
1728 /* Tests of print_parseable_fixits. */
1730 /* Verify that print_parseable_fixits emits the empty string if there
1734 test_print_parseable_fixits_none ()
1737 rich_location
richloc (line_table
, UNKNOWN_LOCATION
);
1739 print_parseable_fixits (&pp
, &richloc
);
1740 ASSERT_STREQ ("", pp_formatted_text (&pp
));
1743 /* Verify that print_parseable_fixits does the right thing if there
1744 is an insertion fixit hint. */
1747 test_print_parseable_fixits_insert ()
1750 rich_location
richloc (line_table
, UNKNOWN_LOCATION
);
1752 linemap_add (line_table
, LC_ENTER
, false, "test.c", 0);
1753 linemap_line_start (line_table
, 5, 100);
1754 linemap_add (line_table
, LC_LEAVE
, false, NULL
, 0);
1755 location_t where
= linemap_position_for_column (line_table
, 10);
1756 richloc
.add_fixit_insert_before (where
, "added content");
1758 print_parseable_fixits (&pp
, &richloc
);
1759 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:10}:\"added content\"\n",
1760 pp_formatted_text (&pp
));
1763 /* Verify that print_parseable_fixits does the right thing if there
1764 is an removal fixit hint. */
1767 test_print_parseable_fixits_remove ()
1770 rich_location
richloc (line_table
, UNKNOWN_LOCATION
);
1772 linemap_add (line_table
, LC_ENTER
, false, "test.c", 0);
1773 linemap_line_start (line_table
, 5, 100);
1774 linemap_add (line_table
, LC_LEAVE
, false, NULL
, 0);
1776 where
.m_start
= linemap_position_for_column (line_table
, 10);
1777 where
.m_finish
= linemap_position_for_column (line_table
, 20);
1778 richloc
.add_fixit_remove (where
);
1780 print_parseable_fixits (&pp
, &richloc
);
1781 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"\"\n",
1782 pp_formatted_text (&pp
));
1785 /* Verify that print_parseable_fixits does the right thing if there
1786 is an replacement fixit hint. */
1789 test_print_parseable_fixits_replace ()
1792 rich_location
richloc (line_table
, UNKNOWN_LOCATION
);
1794 linemap_add (line_table
, LC_ENTER
, false, "test.c", 0);
1795 linemap_line_start (line_table
, 5, 100);
1796 linemap_add (line_table
, LC_LEAVE
, false, NULL
, 0);
1798 where
.m_start
= linemap_position_for_column (line_table
, 10);
1799 where
.m_finish
= linemap_position_for_column (line_table
, 20);
1800 richloc
.add_fixit_replace (where
, "replacement");
1802 print_parseable_fixits (&pp
, &richloc
);
1803 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"replacement\"\n",
1804 pp_formatted_text (&pp
));
1808 diagnostic_get_location_text (..., SHOW_COLUMN)
1809 generates EXPECTED_LOC_TEXT, given FILENAME, LINE, COLUMN, with
1810 colorization disabled. */
1813 assert_location_text (const char *expected_loc_text
,
1814 const char *filename
, int line
, int column
,
1817 test_diagnostic_context dc
;
1818 dc
.show_column
= show_column
;
1820 expanded_location xloc
;
1821 xloc
.file
= filename
;
1823 xloc
.column
= column
;
1827 char *actual_loc_text
= diagnostic_get_location_text (&dc
, xloc
);
1828 ASSERT_STREQ (expected_loc_text
, actual_loc_text
);
1829 free (actual_loc_text
);
1832 /* Verify that diagnostic_get_location_text works as expected. */
1835 test_diagnostic_get_location_text ()
1837 const char *old_progname
= progname
;
1838 progname
= "PROGNAME";
1839 assert_location_text ("PROGNAME:", NULL
, 0, 0, true);
1840 assert_location_text ("<built-in>:", "<built-in>", 42, 10, true);
1841 assert_location_text ("foo.c:42:10:", "foo.c", 42, 10, true);
1842 assert_location_text ("foo.c:42:", "foo.c", 42, 0, true);
1843 assert_location_text ("foo.c:", "foo.c", 0, 10, true);
1844 assert_location_text ("foo.c:42:", "foo.c", 42, 10, false);
1845 assert_location_text ("foo.c:", "foo.c", 0, 10, false);
1847 maybe_line_and_column (INT_MAX
, INT_MAX
);
1848 maybe_line_and_column (INT_MIN
, INT_MIN
);
1850 progname
= old_progname
;
1853 /* Selftest for num_digits. */
1858 ASSERT_EQ (1, num_digits (0));
1859 ASSERT_EQ (1, num_digits (9));
1860 ASSERT_EQ (2, num_digits (10));
1861 ASSERT_EQ (2, num_digits (99));
1862 ASSERT_EQ (3, num_digits (100));
1863 ASSERT_EQ (3, num_digits (999));
1864 ASSERT_EQ (4, num_digits (1000));
1865 ASSERT_EQ (4, num_digits (9999));
1866 ASSERT_EQ (5, num_digits (10000));
1867 ASSERT_EQ (5, num_digits (99999));
1868 ASSERT_EQ (6, num_digits (100000));
1869 ASSERT_EQ (6, num_digits (999999));
1870 ASSERT_EQ (7, num_digits (1000000));
1871 ASSERT_EQ (7, num_digits (9999999));
1872 ASSERT_EQ (8, num_digits (10000000));
1873 ASSERT_EQ (8, num_digits (99999999));
1876 /* Run all of the selftests within this file. */
1879 diagnostic_c_tests ()
1881 test_print_escaped_string ();
1882 test_print_parseable_fixits_none ();
1883 test_print_parseable_fixits_insert ();
1884 test_print_parseable_fixits_remove ();
1885 test_print_parseable_fixits_replace ();
1886 test_diagnostic_get_location_text ();
1891 } // namespace selftest
1893 #endif /* #if CHECKING_P */
1896 # pragma GCC diagnostic pop