1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999-2018 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file implements the language independent aspect of diagnostic
27 #include "coretypes.h"
31 #include "backtrace.h"
32 #include "diagnostic.h"
33 #include "diagnostic-color.h"
34 #include "edit-context.h"
36 #include "selftest-diagnostic.h"
42 #ifdef GWINSZ_IN_SYS_IOCTL
43 # include <sys/ioctl.h>
46 #define pedantic_warning_kind(DC) \
47 ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
48 #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR)
49 #define permissive_error_option(DC) ((DC)->opt_permissive)
52 static bool diagnostic_impl (rich_location
*, int, const char *,
53 va_list *, diagnostic_t
) ATTRIBUTE_GCC_DIAG(3,0);
54 static bool diagnostic_n_impl (rich_location
*, int, unsigned HOST_WIDE_INT
,
55 const char *, const char *, va_list *,
56 diagnostic_t
) ATTRIBUTE_GCC_DIAG(5,0);
58 static void error_recursion (diagnostic_context
*) ATTRIBUTE_NORETURN
;
59 static void real_abort (void) ATTRIBUTE_NORETURN
;
61 /* Name of program invoked, sans directories. */
65 /* A diagnostic_context surrogate for stderr. */
66 static diagnostic_context global_diagnostic_context
;
67 diagnostic_context
*global_dc
= &global_diagnostic_context
;
69 /* Return a malloc'd string containing MSG formatted a la printf. The
70 caller is responsible for freeing the memory. */
72 build_message_string (const char *msg
, ...)
78 str
= xvasprintf (msg
, ap
);
84 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
86 file_name_as_prefix (diagnostic_context
*context
, const char *f
)
89 = colorize_start (pp_show_color (context
->printer
), "locus");
90 const char *locus_ce
= colorize_stop (pp_show_color (context
->printer
));
91 return build_message_string ("%s%s:%s ", locus_cs
, f
, locus_ce
);
96 /* Return the value of the getenv("COLUMNS") as an integer. If the
97 value is not set to a positive integer, use ioctl to get the
98 terminal width. If it fails, return INT_MAX. */
100 get_terminal_width (void)
102 const char * s
= getenv ("COLUMNS");
112 if (ioctl (0, TIOCGWINSZ
, &w
) == 0 && w
.ws_col
> 0)
119 /* Set caret_max_width to value. */
121 diagnostic_set_caret_max_width (diagnostic_context
*context
, int value
)
123 /* One minus to account for the leading empty space. */
124 value
= value
? value
- 1
125 : (isatty (fileno (pp_buffer (context
->printer
)->stream
))
126 ? get_terminal_width () - 1: INT_MAX
);
131 context
->caret_max_width
= value
;
134 /* Default implementation of final_cb. */
137 default_diagnostic_final_cb (diagnostic_context
*context
)
139 /* Some of the errors may actually have been warnings. */
140 if (diagnostic_kind_count (context
, DK_WERROR
))
142 /* -Werror was given. */
143 if (context
->warning_as_error_requested
)
144 pp_verbatim (context
->printer
,
145 _("%s: all warnings being treated as errors"),
147 /* At least one -Werror= was given. */
149 pp_verbatim (context
->printer
,
150 _("%s: some warnings being treated as errors"),
152 pp_newline_and_flush (context
->printer
);
156 /* Initialize the diagnostic message outputting machinery. */
158 diagnostic_initialize (diagnostic_context
*context
, int n_opts
)
162 /* Allocate a basic pretty-printer. Clients will replace this a
163 much more elaborated pretty-printer if they wish. */
164 context
->printer
= XNEW (pretty_printer
);
165 new (context
->printer
) pretty_printer ();
167 memset (context
->diagnostic_count
, 0, sizeof context
->diagnostic_count
);
168 context
->warning_as_error_requested
= false;
169 context
->n_opts
= n_opts
;
170 context
->classify_diagnostic
= XNEWVEC (diagnostic_t
, n_opts
);
171 for (i
= 0; i
< n_opts
; i
++)
172 context
->classify_diagnostic
[i
] = DK_UNSPECIFIED
;
173 context
->show_caret
= false;
174 diagnostic_set_caret_max_width (context
, pp_line_cutoff (context
->printer
));
175 for (i
= 0; i
< rich_location::STATICALLY_ALLOCATED_RANGES
; i
++)
176 context
->caret_chars
[i
] = '^';
177 context
->show_option_requested
= false;
178 context
->abort_on_error
= false;
179 context
->show_column
= false;
180 context
->pedantic_errors
= false;
181 context
->permissive
= false;
182 context
->opt_permissive
= 0;
183 context
->fatal_errors
= false;
184 context
->dc_inhibit_warnings
= false;
185 context
->dc_warn_system_headers
= false;
186 context
->max_errors
= 0;
187 context
->internal_error
= NULL
;
188 diagnostic_starter (context
) = default_diagnostic_starter
;
189 context
->start_span
= default_diagnostic_start_span_fn
;
190 diagnostic_finalizer (context
) = default_diagnostic_finalizer
;
191 context
->option_enabled
= NULL
;
192 context
->option_state
= NULL
;
193 context
->option_name
= NULL
;
194 context
->last_location
= UNKNOWN_LOCATION
;
195 context
->last_module
= 0;
196 context
->x_data
= NULL
;
198 context
->inhibit_notes_p
= false;
199 context
->colorize_source_p
= false;
200 context
->show_labels_p
= false;
201 context
->show_line_numbers_p
= false;
202 context
->min_margin_width
= 0;
203 context
->show_ruler_p
= false;
204 context
->parseable_fixits_p
= false;
205 context
->edit_context_ptr
= NULL
;
206 context
->diagnostic_group_nesting_depth
= 0;
207 context
->diagnostic_group_emission_count
= 0;
208 context
->begin_group_cb
= NULL
;
209 context
->end_group_cb
= NULL
;
210 context
->final_cb
= default_diagnostic_final_cb
;
213 /* Maybe initialize the color support. We require clients to do this
214 explicitly, since most clients don't want color. When called
215 without a VALUE, it initializes with DIAGNOSTICS_COLOR_DEFAULT. */
218 diagnostic_color_init (diagnostic_context
*context
, int value
/*= -1 */)
220 /* value == -1 is the default value. */
223 /* If DIAGNOSTICS_COLOR_DEFAULT is -1, default to
224 -fdiagnostics-color=auto if GCC_COLORS is in the environment,
225 otherwise default to -fdiagnostics-color=never, for other
226 values default to that
227 -fdiagnostics-color={never,auto,always}. */
228 if (DIAGNOSTICS_COLOR_DEFAULT
== -1)
230 if (!getenv ("GCC_COLORS"))
232 value
= DIAGNOSTICS_COLOR_AUTO
;
235 value
= DIAGNOSTICS_COLOR_DEFAULT
;
237 pp_show_color (context
->printer
)
238 = colorize_init ((diagnostic_color_rule_t
) value
);
241 /* Do any cleaning up required after the last diagnostic is emitted. */
244 diagnostic_finish (diagnostic_context
*context
)
246 if (context
->final_cb
)
247 context
->final_cb (context
);
249 diagnostic_file_cache_fini ();
251 XDELETEVEC (context
->classify_diagnostic
);
252 context
->classify_diagnostic
= NULL
;
254 /* diagnostic_initialize allocates context->printer using XNEW
255 and placement-new. */
256 context
->printer
->~pretty_printer ();
257 XDELETE (context
->printer
);
258 context
->printer
= NULL
;
260 if (context
->edit_context_ptr
)
262 delete context
->edit_context_ptr
;
263 context
->edit_context_ptr
= NULL
;
267 /* Initialize DIAGNOSTIC, where the message MSG has already been
270 diagnostic_set_info_translated (diagnostic_info
*diagnostic
, const char *msg
,
271 va_list *args
, rich_location
*richloc
,
274 gcc_assert (richloc
);
275 diagnostic
->message
.err_no
= errno
;
276 diagnostic
->message
.args_ptr
= args
;
277 diagnostic
->message
.format_spec
= msg
;
278 diagnostic
->message
.m_richloc
= richloc
;
279 diagnostic
->richloc
= richloc
;
280 diagnostic
->kind
= kind
;
281 diagnostic
->option_index
= 0;
284 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
287 diagnostic_set_info (diagnostic_info
*diagnostic
, const char *gmsgid
,
288 va_list *args
, rich_location
*richloc
,
291 gcc_assert (richloc
);
292 diagnostic_set_info_translated (diagnostic
, _(gmsgid
), args
, richloc
, kind
);
295 static const char *const diagnostic_kind_color
[] = {
296 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
297 #include "diagnostic.def"
298 #undef DEFINE_DIAGNOSTIC_KIND
302 /* Get a color name for diagnostics of type KIND
303 Result could be NULL. */
306 diagnostic_get_color_for_kind (diagnostic_t kind
)
308 return diagnostic_kind_color
[kind
];
311 /* Return a formatted line and column ':%line:%column'. Elided if
312 zero. The result is a statically allocated buffer. */
315 maybe_line_and_column (int line
, int col
)
317 static char result
[32];
321 size_t l
= snprintf (result
, sizeof (result
),
322 col
? ":%d:%d" : ":%d", line
, col
);
323 gcc_checking_assert (l
< sizeof (result
));
330 /* Return a malloc'd string describing a location e.g. "foo.c:42:10".
331 The caller is responsible for freeing the memory. */
334 diagnostic_get_location_text (diagnostic_context
*context
,
337 pretty_printer
*pp
= context
->printer
;
338 const char *locus_cs
= colorize_start (pp_show_color (pp
), "locus");
339 const char *locus_ce
= colorize_stop (pp_show_color (pp
));
340 const char *file
= s
.file
? s
.file
: progname
;
341 int line
= strcmp (file
, N_("<built-in>")) ? s
.line
: 0;
342 int col
= context
->show_column
? s
.column
: 0;
344 const char *line_col
= maybe_line_and_column (line
, col
);
345 return build_message_string ("%s%s%s:%s", locus_cs
, file
,
349 /* Return a malloc'd string describing a location and the severity of the
350 diagnostic, e.g. "foo.c:42:10: error: ". The caller is responsible for
351 freeing the memory. */
353 diagnostic_build_prefix (diagnostic_context
*context
,
354 const diagnostic_info
*diagnostic
)
356 static const char *const diagnostic_kind_text
[] = {
357 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
358 #include "diagnostic.def"
359 #undef DEFINE_DIAGNOSTIC_KIND
362 gcc_assert (diagnostic
->kind
< DK_LAST_DIAGNOSTIC_KIND
);
364 const char *text
= _(diagnostic_kind_text
[diagnostic
->kind
]);
365 const char *text_cs
= "", *text_ce
= "";
366 pretty_printer
*pp
= context
->printer
;
368 if (diagnostic_kind_color
[diagnostic
->kind
])
370 text_cs
= colorize_start (pp_show_color (pp
),
371 diagnostic_kind_color
[diagnostic
->kind
]);
372 text_ce
= colorize_stop (pp_show_color (pp
));
375 expanded_location s
= diagnostic_expand_location (diagnostic
);
376 char *location_text
= diagnostic_get_location_text (context
, s
);
378 char *result
= build_message_string ("%s %s%s%s", location_text
,
379 text_cs
, text
, text_ce
);
380 free (location_text
);
384 /* Functions at which to stop the backtrace print. It's not
385 particularly helpful to print the callers of these functions. */
387 static const char * const bt_stop
[] =
395 /* A callback function passed to the backtrace_full function. */
398 bt_callback (void *data
, uintptr_t pc
, const char *filename
, int lineno
,
399 const char *function
)
401 int *pcount
= (int *) data
;
403 /* If we don't have any useful information, don't print
405 if (filename
== NULL
&& function
== NULL
)
408 /* Skip functions in diagnostic.c. */
411 && strcmp (lbasename (filename
), "diagnostic.c") == 0)
414 /* Print up to 20 functions. We could make this a --param, but
415 since this is only for debugging just use a constant for now. */
418 /* Returning a non-zero value stops the backtrace. */
424 if (function
!= NULL
)
426 char *str
= cplus_demangle_v3 (function
,
427 (DMGL_VERBOSE
| DMGL_ANSI
428 | DMGL_GNU_V3
| DMGL_PARAMS
));
435 for (size_t i
= 0; i
< ARRAY_SIZE (bt_stop
); ++i
)
437 size_t len
= strlen (bt_stop
[i
]);
438 if (strncmp (function
, bt_stop
[i
], len
) == 0
439 && (function
[len
] == '\0' || function
[len
] == '('))
443 /* Returning a non-zero value stops the backtrace. */
449 fprintf (stderr
, "0x%lx %s\n\t%s:%d\n",
451 function
== NULL
? "???" : function
,
452 filename
== NULL
? "???" : filename
,
461 /* A callback function passed to the backtrace_full function. This is
462 called if backtrace_full has an error. */
465 bt_err_callback (void *data ATTRIBUTE_UNUSED
, const char *msg
, int errnum
)
469 /* This means that no debug info was available. Just quietly
470 skip printing backtrace info. */
473 fprintf (stderr
, "%s%s%s\n", msg
, errnum
== 0 ? "" : ": ",
474 errnum
== 0 ? "" : xstrerror (errnum
));
477 /* Check if we've met the maximum error limit, and if so fatally exit
478 with a message. CONTEXT is the context to check, and FLUSH
479 indicates whether a diagnostic_finish call is needed. */
482 diagnostic_check_max_errors (diagnostic_context
*context
, bool flush
)
484 if (!context
->max_errors
)
487 int count
= (diagnostic_kind_count (context
, DK_ERROR
)
488 + diagnostic_kind_count (context
, DK_SORRY
)
489 + diagnostic_kind_count (context
, DK_WERROR
));
491 if (count
>= context
->max_errors
)
494 "compilation terminated due to -fmax-errors=%u.\n",
495 context
->max_errors
);
497 diagnostic_finish (context
);
498 exit (FATAL_EXIT_CODE
);
502 /* Take any action which is expected to happen after the diagnostic
503 is written out. This function does not always return. */
505 diagnostic_action_after_output (diagnostic_context
*context
,
506 diagnostic_t diag_kind
)
518 if (context
->abort_on_error
)
520 if (context
->fatal_errors
)
522 fnotice (stderr
, "compilation terminated due to -Wfatal-errors.\n");
523 diagnostic_finish (context
);
524 exit (FATAL_EXIT_CODE
);
531 struct backtrace_state
*state
= NULL
;
532 if (diag_kind
== DK_ICE
)
533 state
= backtrace_create_state (NULL
, 0, bt_err_callback
, NULL
);
536 backtrace_full (state
, 2, bt_callback
, bt_err_callback
,
539 if (context
->abort_on_error
)
542 fnotice (stderr
, "Please submit a full bug report,\n"
543 "with preprocessed source if appropriate.\n");
546 ("Please include the complete backtrace "
547 "with any bug report.\n"));
548 fnotice (stderr
, "See %s for instructions.\n", bug_report_url
);
550 exit (ICE_EXIT_CODE
);
554 if (context
->abort_on_error
)
556 diagnostic_finish (context
);
557 fnotice (stderr
, "compilation terminated.\n");
558 exit (FATAL_EXIT_CODE
);
565 /* True if the last module or file in which a diagnostic was reported is
566 different from the current one. */
569 last_module_changed_p (diagnostic_context
*context
,
570 const line_map_ordinary
*map
)
572 return context
->last_module
!= map
;
575 /* Remember the current module or file as being the last one in which we
576 report a diagnostic. */
579 set_last_module (diagnostic_context
*context
, const line_map_ordinary
*map
)
581 context
->last_module
= map
;
585 diagnostic_report_current_module (diagnostic_context
*context
, location_t where
)
587 const line_map_ordinary
*map
= NULL
;
589 if (pp_needs_newline (context
->printer
))
591 pp_newline (context
->printer
);
592 pp_needs_newline (context
->printer
) = false;
595 if (where
<= BUILTINS_LOCATION
)
598 linemap_resolve_location (line_table
, where
,
599 LRK_MACRO_DEFINITION_LOCATION
,
602 if (map
&& last_module_changed_p (context
, map
))
604 set_last_module (context
, map
);
605 if (! MAIN_FILE_P (map
))
610 where
= linemap_included_from (map
);
611 map
= linemap_included_from_linemap (line_table
, map
);
613 = maybe_line_and_column (SOURCE_LINE (map
, where
),
614 first
&& context
->show_column
615 ? SOURCE_COLUMN (map
, where
) : 0);
616 static const char *const msgs
[] =
618 N_("In file included from"),
621 unsigned index
= !first
;
622 pp_verbatim (context
->printer
, "%s%s %r%s%s%R",
623 first
? "" : ",\n", _(msgs
[index
]),
624 "locus", LINEMAP_FILE (map
), line_col
);
627 while (! MAIN_FILE_P (map
));
628 pp_verbatim (context
->printer
, ":");
629 pp_newline (context
->printer
);
635 default_diagnostic_starter (diagnostic_context
*context
,
636 diagnostic_info
*diagnostic
)
638 diagnostic_report_current_module (context
, diagnostic_location (diagnostic
));
639 pp_set_prefix (context
->printer
, diagnostic_build_prefix (context
,
644 default_diagnostic_start_span_fn (diagnostic_context
*context
,
645 expanded_location exploc
)
647 char *text
= diagnostic_get_location_text (context
, exploc
);
648 pp_string (context
->printer
, text
);
650 pp_newline (context
->printer
);
654 default_diagnostic_finalizer (diagnostic_context
*context
,
655 diagnostic_info
*diagnostic
,
658 diagnostic_show_locus (context
, diagnostic
->richloc
, diagnostic
->kind
);
659 pp_destroy_prefix (context
->printer
);
660 pp_flush (context
->printer
);
663 /* Interface to specify diagnostic kind overrides. Returns the
664 previous setting, or DK_UNSPECIFIED if the parameters are out of
665 range. If OPTION_INDEX is zero, the new setting is for all the
668 diagnostic_classify_diagnostic (diagnostic_context
*context
,
670 diagnostic_t new_kind
,
673 diagnostic_t old_kind
;
676 || option_index
>= context
->n_opts
677 || new_kind
>= DK_LAST_DIAGNOSTIC_KIND
)
678 return DK_UNSPECIFIED
;
680 old_kind
= context
->classify_diagnostic
[option_index
];
682 /* Handle pragmas separately, since we need to keep track of *where*
684 if (where
!= UNKNOWN_LOCATION
)
688 /* Record the command-line status, so we can reset it back on DK_POP. */
689 if (old_kind
== DK_UNSPECIFIED
)
691 old_kind
= !context
->option_enabled (option_index
,
692 context
->option_state
)
693 ? DK_IGNORED
: (context
->warning_as_error_requested
694 ? DK_ERROR
: DK_WARNING
);
695 context
->classify_diagnostic
[option_index
] = old_kind
;
698 for (i
= context
->n_classification_history
- 1; i
>= 0; i
--)
699 if (context
->classification_history
[i
].option
== option_index
)
701 old_kind
= context
->classification_history
[i
].kind
;
705 i
= context
->n_classification_history
;
706 context
->classification_history
=
707 (diagnostic_classification_change_t
*) xrealloc (context
->classification_history
, (i
+ 1)
708 * sizeof (diagnostic_classification_change_t
));
709 context
->classification_history
[i
].location
= where
;
710 context
->classification_history
[i
].option
= option_index
;
711 context
->classification_history
[i
].kind
= new_kind
;
712 context
->n_classification_history
++;
715 context
->classify_diagnostic
[option_index
] = new_kind
;
720 /* Save all diagnostic classifications in a stack. */
722 diagnostic_push_diagnostics (diagnostic_context
*context
, location_t where ATTRIBUTE_UNUSED
)
724 context
->push_list
= (int *) xrealloc (context
->push_list
, (context
->n_push
+ 1) * sizeof (int));
725 context
->push_list
[context
->n_push
++] = context
->n_classification_history
;
728 /* Restore the topmost classification set off the stack. If the stack
729 is empty, revert to the state based on command line parameters. */
731 diagnostic_pop_diagnostics (diagnostic_context
*context
, location_t where
)
737 jump_to
= context
->push_list
[-- context
->n_push
];
741 i
= context
->n_classification_history
;
742 context
->classification_history
=
743 (diagnostic_classification_change_t
*) xrealloc (context
->classification_history
, (i
+ 1)
744 * sizeof (diagnostic_classification_change_t
));
745 context
->classification_history
[i
].location
= where
;
746 context
->classification_history
[i
].option
= jump_to
;
747 context
->classification_history
[i
].kind
= DK_POP
;
748 context
->n_classification_history
++;
751 /* Helper function for print_parseable_fixits. Print TEXT to PP, obeying the
752 escaping rules for -fdiagnostics-parseable-fixits. */
755 print_escaped_string (pretty_printer
*pp
, const char *text
)
760 pp_character (pp
, '"');
761 for (const char *ch
= text
; *ch
; ch
++)
766 /* Escape backslash as two backslashes. */
767 pp_string (pp
, "\\\\");
770 /* Escape tab as "\t". */
771 pp_string (pp
, "\\t");
774 /* Escape newline as "\n". */
775 pp_string (pp
, "\\n");
778 /* Escape doublequotes as \". */
779 pp_string (pp
, "\\\"");
783 pp_character (pp
, *ch
);
785 /* Use octal for non-printable chars. */
787 unsigned char c
= (*ch
& 0xff);
788 pp_printf (pp
, "\\%o%o%o", (c
/ 64), (c
/ 8) & 007, c
& 007);
793 pp_character (pp
, '"');
796 /* Implementation of -fdiagnostics-parseable-fixits. Print a
797 machine-parseable version of all fixits in RICHLOC to PP. */
800 print_parseable_fixits (pretty_printer
*pp
, rich_location
*richloc
)
803 gcc_assert (richloc
);
805 for (unsigned i
= 0; i
< richloc
->get_num_fixit_hints (); i
++)
807 const fixit_hint
*hint
= richloc
->get_fixit_hint (i
);
808 location_t start_loc
= hint
->get_start_loc ();
809 expanded_location start_exploc
= expand_location (start_loc
);
810 pp_string (pp
, "fix-it:");
811 print_escaped_string (pp
, start_exploc
.file
);
812 /* For compatibility with clang, print as a half-open range. */
813 location_t next_loc
= hint
->get_next_loc ();
814 expanded_location next_exploc
= expand_location (next_loc
);
815 pp_printf (pp
, ":{%i:%i-%i:%i}:",
816 start_exploc
.line
, start_exploc
.column
,
817 next_exploc
.line
, next_exploc
.column
);
818 print_escaped_string (pp
, hint
->get_string ());
823 /* Update the diag_class of DIAGNOSTIC based on its location
825 #pragma GCC diagnostic
826 directives recorded within CONTEXT.
828 Return the new diag_class of DIAGNOSTIC if it was updated, or
829 DK_UNSPECIFIED otherwise. */
832 update_effective_level_from_pragmas (diagnostic_context
*context
,
833 diagnostic_info
*diagnostic
)
835 diagnostic_t diag_class
= DK_UNSPECIFIED
;
837 if (context
->n_classification_history
> 0)
839 location_t location
= diagnostic_location (diagnostic
);
841 /* FIXME: Stupid search. Optimize later. */
842 for (int i
= context
->n_classification_history
- 1; i
>= 0; i
--)
844 if (linemap_location_before_p
846 context
->classification_history
[i
].location
,
849 if (context
->classification_history
[i
].kind
== (int) DK_POP
)
851 i
= context
->classification_history
[i
].option
;
854 int option
= context
->classification_history
[i
].option
;
855 /* The option 0 is for all the diagnostics. */
856 if (option
== 0 || option
== diagnostic
->option_index
)
858 diag_class
= context
->classification_history
[i
].kind
;
859 if (diag_class
!= DK_UNSPECIFIED
)
860 diagnostic
->kind
= diag_class
;
870 /* Print any metadata about the option used to control DIAGNOSTIC to CONTEXT's
871 printer, e.g. " [-Werror=uninitialized]".
872 Subroutine of diagnostic_report_diagnostic. */
875 print_option_information (diagnostic_context
*context
,
876 const diagnostic_info
*diagnostic
,
877 diagnostic_t orig_diag_kind
)
881 option_text
= context
->option_name (context
, diagnostic
->option_index
,
882 orig_diag_kind
, diagnostic
->kind
);
886 pretty_printer
*pp
= context
->printer
;
887 pp_string (pp
, " [");
888 pp_string (pp
, colorize_start (pp_show_color (pp
),
889 diagnostic_kind_color
[diagnostic
->kind
]));
890 pp_string (pp
, option_text
);
891 pp_string (pp
, colorize_stop (pp_show_color (pp
)));
892 pp_character (pp
, ']');
897 /* Report a diagnostic message (an error or a warning) as specified by
898 DC. This function is *the* subroutine in terms of which front-ends
899 should implement their specific diagnostic handling modules. The
900 front-end independent format specifiers are exactly those described
901 in the documentation of output_format.
902 Return true if a diagnostic was printed, false otherwise. */
905 diagnostic_report_diagnostic (diagnostic_context
*context
,
906 diagnostic_info
*diagnostic
)
908 location_t location
= diagnostic_location (diagnostic
);
909 diagnostic_t orig_diag_kind
= diagnostic
->kind
;
911 /* Give preference to being able to inhibit warnings, before they
912 get reclassified to something else. */
913 if ((diagnostic
->kind
== DK_WARNING
|| diagnostic
->kind
== DK_PEDWARN
)
914 && !diagnostic_report_warnings_p (context
, location
))
917 if (diagnostic
->kind
== DK_PEDWARN
)
919 diagnostic
->kind
= pedantic_warning_kind (context
);
920 /* We do this to avoid giving the message for -pedantic-errors. */
921 orig_diag_kind
= diagnostic
->kind
;
924 if (diagnostic
->kind
== DK_NOTE
&& context
->inhibit_notes_p
)
927 if (context
->lock
> 0)
929 /* If we're reporting an ICE in the middle of some other error,
930 try to flush out the previous error, then let this one
931 through. Don't do this more than once. */
932 if ((diagnostic
->kind
== DK_ICE
|| diagnostic
->kind
== DK_ICE_NOBT
)
933 && context
->lock
== 1)
934 pp_newline_and_flush (context
->printer
);
936 error_recursion (context
);
939 /* If the user requested that warnings be treated as errors, so be
940 it. Note that we do this before the next block so that
941 individual warnings can be overridden back to warnings with
943 if (context
->warning_as_error_requested
944 && diagnostic
->kind
== DK_WARNING
)
945 diagnostic
->kind
= DK_ERROR
;
947 if (diagnostic
->option_index
948 && diagnostic
->option_index
!= permissive_error_option (context
))
950 /* This tests if the user provided the appropriate -Wfoo or
952 if (! context
->option_enabled (diagnostic
->option_index
,
953 context
->option_state
))
956 /* This tests for #pragma diagnostic changes. */
957 diagnostic_t diag_class
958 = update_effective_level_from_pragmas (context
, diagnostic
);
960 /* This tests if the user provided the appropriate -Werror=foo
962 if (diag_class
== DK_UNSPECIFIED
963 && (context
->classify_diagnostic
[diagnostic
->option_index
]
966 = context
->classify_diagnostic
[diagnostic
->option_index
];
968 /* This allows for future extensions, like temporarily disabling
969 warnings for ranges of source code. */
970 if (diagnostic
->kind
== DK_IGNORED
)
974 if (diagnostic
->kind
!= DK_NOTE
)
975 diagnostic_check_max_errors (context
);
979 if (diagnostic
->kind
== DK_ICE
|| diagnostic
->kind
== DK_ICE_NOBT
)
981 /* When not checking, ICEs are converted to fatal errors when an
982 error has already occurred. This is counteracted by
985 && (diagnostic_kind_count (context
, DK_ERROR
) > 0
986 || diagnostic_kind_count (context
, DK_SORRY
) > 0)
987 && !context
->abort_on_error
)
990 = expand_location (diagnostic_location (diagnostic
));
991 fnotice (stderr
, "%s:%d: confused by earlier errors, bailing out\n",
993 exit (ICE_EXIT_CODE
);
995 if (context
->internal_error
)
996 (*context
->internal_error
) (context
,
997 diagnostic
->message
.format_spec
,
998 diagnostic
->message
.args_ptr
);
1000 if (diagnostic
->kind
== DK_ERROR
&& orig_diag_kind
== DK_WARNING
)
1001 ++diagnostic_kind_count (context
, DK_WERROR
);
1003 ++diagnostic_kind_count (context
, diagnostic
->kind
);
1005 /* Is this the initial diagnostic within the stack of groups? */
1006 if (context
->diagnostic_group_emission_count
== 0)
1008 if (context
->begin_group_cb
)
1009 context
->begin_group_cb (context
);
1011 context
->diagnostic_group_emission_count
++;
1013 diagnostic
->message
.x_data
= &diagnostic
->x_data
;
1014 diagnostic
->x_data
= NULL
;
1015 pp_format (context
->printer
, &diagnostic
->message
);
1016 (*diagnostic_starter (context
)) (context
, diagnostic
);
1017 pp_output_formatted_text (context
->printer
);
1018 if (context
->show_option_requested
)
1019 print_option_information (context
, diagnostic
, orig_diag_kind
);
1020 (*diagnostic_finalizer (context
)) (context
, diagnostic
, orig_diag_kind
);
1021 if (context
->parseable_fixits_p
)
1023 print_parseable_fixits (context
->printer
, diagnostic
->richloc
);
1024 pp_flush (context
->printer
);
1026 diagnostic_action_after_output (context
, diagnostic
->kind
);
1027 diagnostic
->x_data
= NULL
;
1029 if (context
->edit_context_ptr
)
1030 if (diagnostic
->richloc
->fixits_can_be_auto_applied_p ())
1031 context
->edit_context_ptr
->add_fixits (diagnostic
->richloc
);
1038 /* Given a partial pathname as input, return another pathname that
1039 shares no directory elements with the pathname of __FILE__. This
1040 is used by fancy_abort() to print `Internal compiler error in expr.c'
1041 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1044 trim_filename (const char *name
)
1046 static const char this_file
[] = __FILE__
;
1047 const char *p
= name
, *q
= this_file
;
1049 /* First skip any "../" in each filename. This allows us to give a proper
1050 reference to a file in a subdirectory. */
1051 while (p
[0] == '.' && p
[1] == '.' && IS_DIR_SEPARATOR (p
[2]))
1054 while (q
[0] == '.' && q
[1] == '.' && IS_DIR_SEPARATOR (q
[2]))
1057 /* Now skip any parts the two filenames have in common. */
1058 while (*p
== *q
&& *p
!= 0 && *q
!= 0)
1061 /* Now go backwards until the previous directory separator. */
1062 while (p
> name
&& !IS_DIR_SEPARATOR (p
[-1]))
1068 /* Standard error reporting routines in increasing order of severity.
1069 All of these take arguments like printf. */
1071 /* Text to be emitted verbatim to the error message stream; this
1072 produces no prefix and disables line-wrapping. Use rarely. */
1074 verbatim (const char *gmsgid
, ...)
1079 va_start (ap
, gmsgid
);
1080 text
.err_no
= errno
;
1081 text
.args_ptr
= &ap
;
1082 text
.format_spec
= _(gmsgid
);
1084 pp_format_verbatim (global_dc
->printer
, &text
);
1085 pp_newline_and_flush (global_dc
->printer
);
1089 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
1091 diagnostic_append_note (diagnostic_context
*context
,
1092 location_t location
,
1093 const char * gmsgid
, ...)
1095 diagnostic_info diagnostic
;
1097 rich_location
richloc (line_table
, location
);
1099 va_start (ap
, gmsgid
);
1100 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_NOTE
);
1101 if (context
->inhibit_notes_p
)
1106 char *saved_prefix
= pp_take_prefix (context
->printer
);
1107 pp_set_prefix (context
->printer
,
1108 diagnostic_build_prefix (context
, &diagnostic
));
1109 pp_format (context
->printer
, &diagnostic
.message
);
1110 pp_output_formatted_text (context
->printer
);
1111 pp_destroy_prefix (context
->printer
);
1112 pp_set_prefix (context
->printer
, saved_prefix
);
1113 diagnostic_show_locus (context
, &richloc
, DK_NOTE
);
1117 /* Implement emit_diagnostic, inform, warning, warning_at, pedwarn,
1118 permerror, error, error_at, error_at, sorry, fatal_error, internal_error,
1119 and internal_error_no_backtrace, as documented and defined below. */
1121 diagnostic_impl (rich_location
*richloc
, int opt
,
1123 va_list *ap
, diagnostic_t kind
)
1125 diagnostic_info diagnostic
;
1126 if (kind
== DK_PERMERROR
)
1128 diagnostic_set_info (&diagnostic
, gmsgid
, ap
, richloc
,
1129 permissive_error_kind (global_dc
));
1130 diagnostic
.option_index
= permissive_error_option (global_dc
);
1134 diagnostic_set_info (&diagnostic
, gmsgid
, ap
, richloc
, kind
);
1135 if (kind
== DK_WARNING
|| kind
== DK_PEDWARN
)
1136 diagnostic
.option_index
= opt
;
1138 return diagnostic_report_diagnostic (global_dc
, &diagnostic
);
1141 /* Implement inform_n, warning_n, and error_n, as documented and
1144 diagnostic_n_impl (rich_location
*richloc
, int opt
, unsigned HOST_WIDE_INT n
,
1145 const char *singular_gmsgid
,
1146 const char *plural_gmsgid
,
1147 va_list *ap
, diagnostic_t kind
)
1149 diagnostic_info diagnostic
;
1152 if (sizeof n
<= sizeof gtn
)
1155 /* Use the largest number ngettext can handle, otherwise
1156 preserve the six least significant decimal digits for
1157 languages where the plural form depends on them. */
1158 gtn
= n
<= ULONG_MAX
? n
: n
% 1000000LU + 1000000LU;
1160 const char *text
= ngettext (singular_gmsgid
, plural_gmsgid
, gtn
);
1161 diagnostic_set_info_translated (&diagnostic
, text
, ap
, richloc
, kind
);
1162 if (kind
== DK_WARNING
)
1163 diagnostic
.option_index
= opt
;
1164 return diagnostic_report_diagnostic (global_dc
, &diagnostic
);
1167 /* Wrapper around diagnostic_impl taking a variable argument list. */
1170 emit_diagnostic (diagnostic_t kind
, location_t location
, int opt
,
1171 const char *gmsgid
, ...)
1173 auto_diagnostic_group d
;
1175 va_start (ap
, gmsgid
);
1176 rich_location
richloc (line_table
, location
);
1177 bool ret
= diagnostic_impl (&richloc
, opt
, gmsgid
, &ap
, kind
);
1182 /* Wrapper around diagnostic_impl taking a va_list parameter. */
1185 emit_diagnostic_valist (diagnostic_t kind
, location_t location
, int opt
,
1186 const char *gmsgid
, va_list *ap
)
1188 rich_location
richloc (line_table
, location
);
1189 return diagnostic_impl (&richloc
, opt
, gmsgid
, ap
, kind
);
1192 /* An informative note at LOCATION. Use this for additional details on an error
1195 inform (location_t location
, const char *gmsgid
, ...)
1197 auto_diagnostic_group d
;
1199 va_start (ap
, gmsgid
);
1200 rich_location
richloc (line_table
, location
);
1201 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_NOTE
);
1205 /* Same as "inform" above, but at RICHLOC. */
1207 inform (rich_location
*richloc
, const char *gmsgid
, ...)
1209 gcc_assert (richloc
);
1211 auto_diagnostic_group d
;
1213 va_start (ap
, gmsgid
);
1214 diagnostic_impl (richloc
, -1, gmsgid
, &ap
, DK_NOTE
);
1218 /* An informative note at LOCATION. Use this for additional details on an
1221 inform_n (location_t location
, unsigned HOST_WIDE_INT n
,
1222 const char *singular_gmsgid
, const char *plural_gmsgid
, ...)
1225 va_start (ap
, plural_gmsgid
);
1226 auto_diagnostic_group d
;
1227 rich_location
richloc (line_table
, location
);
1228 diagnostic_n_impl (&richloc
, -1, n
, singular_gmsgid
, plural_gmsgid
,
1233 /* A warning at INPUT_LOCATION. Use this for code which is correct according
1234 to the relevant language specification but is likely to be buggy anyway.
1235 Returns true if the warning was printed, false if it was inhibited. */
1237 warning (int opt
, const char *gmsgid
, ...)
1239 auto_diagnostic_group d
;
1241 va_start (ap
, gmsgid
);
1242 rich_location
richloc (line_table
, input_location
);
1243 bool ret
= diagnostic_impl (&richloc
, opt
, gmsgid
, &ap
, DK_WARNING
);
1248 /* A warning at LOCATION. Use this for code which is correct according to the
1249 relevant language specification but is likely to be buggy anyway.
1250 Returns true if the warning was printed, false if it was inhibited. */
1253 warning_at (location_t location
, int opt
, const char *gmsgid
, ...)
1255 auto_diagnostic_group d
;
1257 va_start (ap
, gmsgid
);
1258 rich_location
richloc (line_table
, location
);
1259 bool ret
= diagnostic_impl (&richloc
, opt
, gmsgid
, &ap
, DK_WARNING
);
1264 /* Same as "warning at" above, but using RICHLOC. */
1267 warning_at (rich_location
*richloc
, int opt
, const char *gmsgid
, ...)
1269 gcc_assert (richloc
);
1271 auto_diagnostic_group d
;
1273 va_start (ap
, gmsgid
);
1274 bool ret
= diagnostic_impl (richloc
, opt
, gmsgid
, &ap
, DK_WARNING
);
1279 /* Same as warning_n plural variant below, but using RICHLOC. */
1282 warning_n (rich_location
*richloc
, int opt
, unsigned HOST_WIDE_INT n
,
1283 const char *singular_gmsgid
, const char *plural_gmsgid
, ...)
1285 gcc_assert (richloc
);
1287 auto_diagnostic_group d
;
1289 va_start (ap
, plural_gmsgid
);
1290 bool ret
= diagnostic_n_impl (richloc
, opt
, n
,
1291 singular_gmsgid
, plural_gmsgid
,
1297 /* A warning at LOCATION. Use this for code which is correct according to the
1298 relevant language specification but is likely to be buggy anyway.
1299 Returns true if the warning was printed, false if it was inhibited. */
1302 warning_n (location_t location
, int opt
, unsigned HOST_WIDE_INT n
,
1303 const char *singular_gmsgid
, const char *plural_gmsgid
, ...)
1305 auto_diagnostic_group d
;
1307 va_start (ap
, plural_gmsgid
);
1308 rich_location
richloc (line_table
, location
);
1309 bool ret
= diagnostic_n_impl (&richloc
, opt
, n
,
1310 singular_gmsgid
, plural_gmsgid
,
1316 /* A "pedantic" warning at LOCATION: issues a warning unless
1317 -pedantic-errors was given on the command line, in which case it
1318 issues an error. Use this for diagnostics required by the relevant
1319 language standard, if you have chosen not to make them errors.
1321 Note that these diagnostics are issued independent of the setting
1322 of the -Wpedantic command-line switch. To get a warning enabled
1323 only with that switch, use either "if (pedantic) pedwarn
1324 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
1325 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
1327 Returns true if the warning was printed, false if it was inhibited. */
1330 pedwarn (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_PEDWARN
);
1341 /* Same as pedwarn above, but using RICHLOC. */
1344 pedwarn (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_PEDWARN
);
1356 /* A "permissive" error at LOCATION: issues an error unless
1357 -fpermissive was given on the command line, in which case it issues
1358 a warning. Use this for things that really should be errors but we
1359 want to support legacy code.
1361 Returns true if the warning was printed, false if it was inhibited. */
1364 permerror (location_t location
, const char *gmsgid
, ...)
1366 auto_diagnostic_group d
;
1368 va_start (ap
, gmsgid
);
1369 rich_location
richloc (line_table
, location
);
1370 bool ret
= diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_PERMERROR
);
1375 /* Same as "permerror" above, but at RICHLOC. */
1378 permerror (rich_location
*richloc
, const char *gmsgid
, ...)
1380 gcc_assert (richloc
);
1382 auto_diagnostic_group d
;
1384 va_start (ap
, gmsgid
);
1385 bool ret
= diagnostic_impl (richloc
, -1, gmsgid
, &ap
, DK_PERMERROR
);
1390 /* A hard error: the code is definitely ill-formed, and an object file
1391 will not be produced. */
1393 error (const char *gmsgid
, ...)
1395 auto_diagnostic_group d
;
1397 va_start (ap
, gmsgid
);
1398 rich_location
richloc (line_table
, input_location
);
1399 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_ERROR
);
1403 /* A hard error: the code is definitely ill-formed, and an object file
1404 will not be produced. */
1406 error_n (location_t location
, unsigned HOST_WIDE_INT n
,
1407 const char *singular_gmsgid
, const char *plural_gmsgid
, ...)
1409 auto_diagnostic_group d
;
1411 va_start (ap
, plural_gmsgid
);
1412 rich_location
richloc (line_table
, location
);
1413 diagnostic_n_impl (&richloc
, -1, n
, singular_gmsgid
, plural_gmsgid
,
1418 /* Same as above, but use location LOC instead of input_location. */
1420 error_at (location_t loc
, const char *gmsgid
, ...)
1422 auto_diagnostic_group d
;
1424 va_start (ap
, gmsgid
);
1425 rich_location
richloc (line_table
, loc
);
1426 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_ERROR
);
1430 /* Same as above, but use RICH_LOC. */
1433 error_at (rich_location
*richloc
, const char *gmsgid
, ...)
1435 gcc_assert (richloc
);
1437 auto_diagnostic_group d
;
1439 va_start (ap
, gmsgid
);
1440 diagnostic_impl (richloc
, -1, gmsgid
, &ap
, DK_ERROR
);
1444 /* "Sorry, not implemented." Use for a language feature which is
1445 required by the relevant specification but not implemented by GCC.
1446 An object file will not be produced. */
1448 sorry (const char *gmsgid
, ...)
1450 auto_diagnostic_group d
;
1452 va_start (ap
, gmsgid
);
1453 rich_location
richloc (line_table
, input_location
);
1454 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_SORRY
);
1458 /* Same as above, but use location LOC instead of input_location. */
1460 sorry_at (location_t loc
, const char *gmsgid
, ...)
1462 auto_diagnostic_group d
;
1464 va_start (ap
, gmsgid
);
1465 rich_location
richloc (line_table
, loc
);
1466 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_SORRY
);
1470 /* Return true if an error or a "sorry" has been seen. Various
1471 processing is disabled after errors. */
1475 return errorcount
|| sorrycount
;
1478 /* An error which is severe enough that we make no attempt to
1479 continue. Do not use this for internal consistency checks; that's
1480 internal_error. Use of this function should be rare. */
1482 fatal_error (location_t loc
, const char *gmsgid
, ...)
1484 auto_diagnostic_group d
;
1486 va_start (ap
, gmsgid
);
1487 rich_location
richloc (line_table
, loc
);
1488 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_FATAL
);
1494 /* An internal consistency check has failed. We make no attempt to
1495 continue. Note that unless there is debugging value to be had from
1496 a more specific message, or some other good reason, you should use
1497 abort () instead of calling this function directly. */
1499 internal_error (const char *gmsgid
, ...)
1501 auto_diagnostic_group d
;
1503 va_start (ap
, gmsgid
);
1504 rich_location
richloc (line_table
, input_location
);
1505 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_ICE
);
1511 /* Like internal_error, but no backtrace will be printed. Used when
1512 the internal error does not happen at the current location, but happened
1515 internal_error_no_backtrace (const char *gmsgid
, ...)
1517 auto_diagnostic_group d
;
1519 va_start (ap
, gmsgid
);
1520 rich_location
richloc (line_table
, input_location
);
1521 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_ICE_NOBT
);
1527 /* Special case error functions. Most are implemented in terms of the
1528 above, or should be. */
1530 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1531 runs its second argument through gettext. */
1533 fnotice (FILE *file
, const char *cmsgid
, ...)
1537 va_start (ap
, cmsgid
);
1538 vfprintf (file
, _(cmsgid
), ap
);
1542 /* Inform the user that an error occurred while trying to report some
1543 other error. This indicates catastrophic internal inconsistencies,
1544 so give up now. But do try to flush out the previous error.
1545 This mustn't use internal_error, that will cause infinite recursion. */
1548 error_recursion (diagnostic_context
*context
)
1550 if (context
->lock
< 3)
1551 pp_newline_and_flush (context
->printer
);
1554 "Internal compiler error: Error reporting routines re-entered.\n");
1556 /* Call diagnostic_action_after_output to get the "please submit a bug
1558 diagnostic_action_after_output (context
, DK_ICE
);
1560 /* Do not use gcc_unreachable here; that goes through internal_error
1561 and therefore would cause infinite recursion. */
1565 /* Report an internal compiler error in a friendly manner. This is
1566 the function that gets called upon use of abort() in the source
1567 code generally, thanks to a special macro. */
1570 fancy_abort (const char *file
, int line
, const char *function
)
1572 internal_error ("in %s, at %s:%d", function
, trim_filename (file
), line
);
1575 /* class auto_diagnostic_group. */
1577 /* Constructor: "push" this group into global_dc. */
1579 auto_diagnostic_group::auto_diagnostic_group ()
1581 global_dc
->diagnostic_group_nesting_depth
++;
1584 /* Destructor: "pop" this group from global_dc. */
1586 auto_diagnostic_group::~auto_diagnostic_group ()
1588 if (--global_dc
->diagnostic_group_nesting_depth
== 0)
1590 /* Handle the case where we've popped the final diagnostic group.
1591 If any diagnostics were emitted, give the context a chance
1593 if (global_dc
->diagnostic_group_emission_count
> 0)
1595 if (global_dc
->end_group_cb
)
1596 global_dc
->end_group_cb (global_dc
);
1598 global_dc
->diagnostic_group_emission_count
= 0;
1602 /* Really call the system 'abort'. This has to go right at the end of
1603 this file, so that there are no functions after it that call abort
1604 and get the system abort instead of our macro. */
1614 namespace selftest
{
1616 /* Helper function for test_print_escaped_string. */
1619 assert_print_escaped_string (const location
&loc
, const char *expected_output
,
1623 print_escaped_string (&pp
, input
);
1624 ASSERT_STREQ_AT (loc
, expected_output
, pp_formatted_text (&pp
));
1627 #define ASSERT_PRINT_ESCAPED_STRING_STREQ(EXPECTED_OUTPUT, INPUT) \
1628 assert_print_escaped_string (SELFTEST_LOCATION, EXPECTED_OUTPUT, INPUT)
1630 /* Tests of print_escaped_string. */
1633 test_print_escaped_string ()
1636 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"\"", "");
1638 /* Non-empty string. */
1639 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"hello world\"", "hello world");
1641 /* Various things that need to be escaped: */
1643 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\\after\"",
1646 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\tafter\"",
1649 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\nafter\"",
1652 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\"after\"",
1655 /* Non-printable characters: BEL: '\a': 0x07 */
1656 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\007after\"",
1658 /* Non-printable characters: vertical tab: '\v': 0x0b */
1659 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\013after\"",
1663 /* Tests of print_parseable_fixits. */
1665 /* Verify that print_parseable_fixits emits the empty string if there
1669 test_print_parseable_fixits_none ()
1672 rich_location
richloc (line_table
, UNKNOWN_LOCATION
);
1674 print_parseable_fixits (&pp
, &richloc
);
1675 ASSERT_STREQ ("", pp_formatted_text (&pp
));
1678 /* Verify that print_parseable_fixits does the right thing if there
1679 is an insertion fixit hint. */
1682 test_print_parseable_fixits_insert ()
1685 rich_location
richloc (line_table
, UNKNOWN_LOCATION
);
1687 linemap_add (line_table
, LC_ENTER
, false, "test.c", 0);
1688 linemap_line_start (line_table
, 5, 100);
1689 linemap_add (line_table
, LC_LEAVE
, false, NULL
, 0);
1690 location_t where
= linemap_position_for_column (line_table
, 10);
1691 richloc
.add_fixit_insert_before (where
, "added content");
1693 print_parseable_fixits (&pp
, &richloc
);
1694 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:10}:\"added content\"\n",
1695 pp_formatted_text (&pp
));
1698 /* Verify that print_parseable_fixits does the right thing if there
1699 is an removal fixit hint. */
1702 test_print_parseable_fixits_remove ()
1705 rich_location
richloc (line_table
, UNKNOWN_LOCATION
);
1707 linemap_add (line_table
, LC_ENTER
, false, "test.c", 0);
1708 linemap_line_start (line_table
, 5, 100);
1709 linemap_add (line_table
, LC_LEAVE
, false, NULL
, 0);
1711 where
.m_start
= linemap_position_for_column (line_table
, 10);
1712 where
.m_finish
= linemap_position_for_column (line_table
, 20);
1713 richloc
.add_fixit_remove (where
);
1715 print_parseable_fixits (&pp
, &richloc
);
1716 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"\"\n",
1717 pp_formatted_text (&pp
));
1720 /* Verify that print_parseable_fixits does the right thing if there
1721 is an replacement fixit hint. */
1724 test_print_parseable_fixits_replace ()
1727 rich_location
richloc (line_table
, UNKNOWN_LOCATION
);
1729 linemap_add (line_table
, LC_ENTER
, false, "test.c", 0);
1730 linemap_line_start (line_table
, 5, 100);
1731 linemap_add (line_table
, LC_LEAVE
, false, NULL
, 0);
1733 where
.m_start
= linemap_position_for_column (line_table
, 10);
1734 where
.m_finish
= linemap_position_for_column (line_table
, 20);
1735 richloc
.add_fixit_replace (where
, "replacement");
1737 print_parseable_fixits (&pp
, &richloc
);
1738 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"replacement\"\n",
1739 pp_formatted_text (&pp
));
1743 diagnostic_get_location_text (..., SHOW_COLUMN)
1744 generates EXPECTED_LOC_TEXT, given FILENAME, LINE, COLUMN, with
1745 colorization disabled. */
1748 assert_location_text (const char *expected_loc_text
,
1749 const char *filename
, int line
, int column
,
1752 test_diagnostic_context dc
;
1753 dc
.show_column
= show_column
;
1755 expanded_location xloc
;
1756 xloc
.file
= filename
;
1758 xloc
.column
= column
;
1762 char *actual_loc_text
= diagnostic_get_location_text (&dc
, xloc
);
1763 ASSERT_STREQ (expected_loc_text
, actual_loc_text
);
1764 free (actual_loc_text
);
1767 /* Verify that diagnostic_get_location_text works as expected. */
1770 test_diagnostic_get_location_text ()
1772 const char *old_progname
= progname
;
1773 progname
= "PROGNAME";
1774 assert_location_text ("PROGNAME:", NULL
, 0, 0, true);
1775 assert_location_text ("<built-in>:", "<built-in>", 42, 10, true);
1776 assert_location_text ("foo.c:42:10:", "foo.c", 42, 10, true);
1777 assert_location_text ("foo.c:42:", "foo.c", 42, 0, true);
1778 assert_location_text ("foo.c:", "foo.c", 0, 10, true);
1779 assert_location_text ("foo.c:42:", "foo.c", 42, 10, false);
1780 assert_location_text ("foo.c:", "foo.c", 0, 10, false);
1782 maybe_line_and_column (INT_MAX
, INT_MAX
);
1783 maybe_line_and_column (INT_MIN
, INT_MIN
);
1785 progname
= old_progname
;
1788 /* Run all of the selftests within this file. */
1791 diagnostic_c_tests ()
1793 test_print_escaped_string ();
1794 test_print_parseable_fixits_none ();
1795 test_print_parseable_fixits_insert ();
1796 test_print_parseable_fixits_remove ();
1797 test_print_parseable_fixits_replace ();
1798 test_diagnostic_get_location_text ();
1801 } // namespace selftest
1803 #endif /* #if CHECKING_P */