[Patch obvious testsuite] Remove duplicate body from pr70903.c
[official-gcc.git] / gcc / diagnostic.c
blobbb41011afad68cca29af356d591d293fba75224f
1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999-2016 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
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
15 for more details.
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
23 message module. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "version.h"
29 #include "demangle.h"
30 #include "intl.h"
31 #include "backtrace.h"
32 #include "diagnostic.h"
33 #include "diagnostic-color.h"
34 #include "selftest.h"
36 #ifdef HAVE_TERMIOS_H
37 # include <termios.h>
38 #endif
40 #ifdef GWINSZ_IN_SYS_IOCTL
41 # include <sys/ioctl.h>
42 #endif
44 #define pedantic_warning_kind(DC) \
45 ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
46 #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR)
47 #define permissive_error_option(DC) ((DC)->opt_permissive)
49 /* Prototypes. */
50 static bool diagnostic_impl (rich_location *, int, const char *,
51 va_list *, diagnostic_t) ATTRIBUTE_GCC_DIAG(3,0);
52 static bool diagnostic_n_impl (location_t, int, int, const char *,
53 const char *, va_list *,
54 diagnostic_t) ATTRIBUTE_GCC_DIAG(5,0);
55 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
56 static void real_abort (void) ATTRIBUTE_NORETURN;
58 /* Name of program invoked, sans directories. */
60 const char *progname;
62 /* A diagnostic_context surrogate for stderr. */
63 static diagnostic_context global_diagnostic_context;
64 diagnostic_context *global_dc = &global_diagnostic_context;
66 /* Return a malloc'd string containing MSG formatted a la printf. The
67 caller is responsible for freeing the memory. */
68 char *
69 build_message_string (const char *msg, ...)
71 char *str;
72 va_list ap;
74 va_start (ap, msg);
75 str = xvasprintf (msg, ap);
76 va_end (ap);
78 return str;
81 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
82 char *
83 file_name_as_prefix (diagnostic_context *context, const char *f)
85 const char *locus_cs
86 = colorize_start (pp_show_color (context->printer), "locus");
87 const char *locus_ce = colorize_stop (pp_show_color (context->printer));
88 return build_message_string ("%s%s:%s ", locus_cs, f, locus_ce);
93 /* Return the value of the getenv("COLUMNS") as an integer. If the
94 value is not set to a positive integer, use ioctl to get the
95 terminal width. If it fails, return INT_MAX. */
96 int
97 get_terminal_width (void)
99 const char * s = getenv ("COLUMNS");
100 if (s != NULL) {
101 int n = atoi (s);
102 if (n > 0)
103 return n;
106 #ifdef TIOCGWINSZ
107 struct winsize w;
108 w.ws_col = 0;
109 if (ioctl (0, TIOCGWINSZ, &w) == 0 && w.ws_col > 0)
110 return w.ws_col;
111 #endif
113 return INT_MAX;
116 /* Set caret_max_width to value. */
117 void
118 diagnostic_set_caret_max_width (diagnostic_context *context, int value)
120 /* One minus to account for the leading empty space. */
121 value = value ? value - 1
122 : (isatty (fileno (pp_buffer (context->printer)->stream))
123 ? get_terminal_width () - 1: INT_MAX);
125 if (value <= 0)
126 value = INT_MAX;
128 context->caret_max_width = value;
131 /* Initialize the diagnostic message outputting machinery. */
132 void
133 diagnostic_initialize (diagnostic_context *context, int n_opts)
135 int i;
137 /* Allocate a basic pretty-printer. Clients will replace this a
138 much more elaborated pretty-printer if they wish. */
139 context->printer = XNEW (pretty_printer);
140 new (context->printer) pretty_printer ();
142 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
143 context->warning_as_error_requested = false;
144 context->n_opts = n_opts;
145 context->classify_diagnostic = XNEWVEC (diagnostic_t, n_opts);
146 for (i = 0; i < n_opts; i++)
147 context->classify_diagnostic[i] = DK_UNSPECIFIED;
148 context->show_caret = false;
149 diagnostic_set_caret_max_width (context, pp_line_cutoff (context->printer));
150 for (i = 0; i < rich_location::MAX_RANGES; i++)
151 context->caret_chars[i] = '^';
152 context->show_option_requested = false;
153 context->abort_on_error = false;
154 context->show_column = false;
155 context->pedantic_errors = false;
156 context->permissive = false;
157 context->opt_permissive = 0;
158 context->fatal_errors = false;
159 context->dc_inhibit_warnings = false;
160 context->dc_warn_system_headers = false;
161 context->max_errors = 0;
162 context->internal_error = NULL;
163 diagnostic_starter (context) = default_diagnostic_starter;
164 context->start_span = default_diagnostic_start_span_fn;
165 diagnostic_finalizer (context) = default_diagnostic_finalizer;
166 context->option_enabled = NULL;
167 context->option_state = NULL;
168 context->option_name = NULL;
169 context->last_location = UNKNOWN_LOCATION;
170 context->last_module = 0;
171 context->x_data = NULL;
172 context->lock = 0;
173 context->inhibit_notes_p = false;
176 /* Maybe initialize the color support. We require clients to do this
177 explicitly, since most clients don't want color. When called
178 without a VALUE, it initializes with DIAGNOSTICS_COLOR_DEFAULT. */
180 void
181 diagnostic_color_init (diagnostic_context *context, int value /*= -1 */)
183 /* value == -1 is the default value. */
184 if (value < 0)
186 /* If DIAGNOSTICS_COLOR_DEFAULT is -1, default to
187 -fdiagnostics-color=auto if GCC_COLORS is in the environment,
188 otherwise default to -fdiagnostics-color=never, for other
189 values default to that
190 -fdiagnostics-color={never,auto,always}. */
191 if (DIAGNOSTICS_COLOR_DEFAULT == -1)
193 if (!getenv ("GCC_COLORS"))
194 return;
195 value = DIAGNOSTICS_COLOR_AUTO;
197 else
198 value = DIAGNOSTICS_COLOR_DEFAULT;
200 pp_show_color (context->printer)
201 = colorize_init ((diagnostic_color_rule_t) value);
204 /* Do any cleaning up required after the last diagnostic is emitted. */
206 void
207 diagnostic_finish (diagnostic_context *context)
209 /* Some of the errors may actually have been warnings. */
210 if (diagnostic_kind_count (context, DK_WERROR))
212 /* -Werror was given. */
213 if (context->warning_as_error_requested)
214 pp_verbatim (context->printer,
215 _("%s: all warnings being treated as errors"),
216 progname);
217 /* At least one -Werror= was given. */
218 else
219 pp_verbatim (context->printer,
220 _("%s: some warnings being treated as errors"),
221 progname);
222 pp_newline_and_flush (context->printer);
225 diagnostic_file_cache_fini ();
227 XDELETEVEC (context->classify_diagnostic);
228 context->classify_diagnostic = NULL;
230 /* diagnostic_initialize allocates context->printer using XNEW
231 and placement-new. */
232 context->printer->~pretty_printer ();
233 XDELETE (context->printer);
234 context->printer = NULL;
237 /* Initialize DIAGNOSTIC, where the message MSG has already been
238 translated. */
239 void
240 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
241 va_list *args, rich_location *richloc,
242 diagnostic_t kind)
244 gcc_assert (richloc);
245 diagnostic->message.err_no = errno;
246 diagnostic->message.args_ptr = args;
247 diagnostic->message.format_spec = msg;
248 diagnostic->message.m_richloc = richloc;
249 diagnostic->richloc = richloc;
250 diagnostic->kind = kind;
251 diagnostic->option_index = 0;
254 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
255 translated. */
256 void
257 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
258 va_list *args, rich_location *richloc,
259 diagnostic_t kind)
261 gcc_assert (richloc);
262 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, richloc, kind);
265 static const char *const diagnostic_kind_color[] = {
266 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
267 #include "diagnostic.def"
268 #undef DEFINE_DIAGNOSTIC_KIND
269 NULL
272 /* Get a color name for diagnostics of type KIND
273 Result could be NULL. */
275 const char *
276 diagnostic_get_color_for_kind (diagnostic_t kind)
278 return diagnostic_kind_color[kind];
281 /* Return a malloc'd string describing a location e.g. "foo.c:42:10".
282 The caller is responsible for freeing the memory. */
284 static char *
285 diagnostic_get_location_text (diagnostic_context *context,
286 expanded_location s)
288 pretty_printer *pp = context->printer;
289 const char *locus_cs = colorize_start (pp_show_color (pp), "locus");
290 const char *locus_ce = colorize_stop (pp_show_color (pp));
292 if (s.file == NULL)
293 return build_message_string ("%s%s:%s", locus_cs, progname, locus_ce);
295 if (!strcmp (s.file, N_("<built-in>")))
296 return build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce);
298 if (context->show_column)
299 return build_message_string ("%s%s:%d:%d:%s", locus_cs, s.file, s.line,
300 s.column, locus_ce);
301 else
302 return build_message_string ("%s%s:%d:%s", locus_cs, s.file, s.line,
303 locus_ce);
306 /* Return a malloc'd string describing a location and the severity of the
307 diagnostic, e.g. "foo.c:42:10: error: ". The caller is responsible for
308 freeing the memory. */
309 char *
310 diagnostic_build_prefix (diagnostic_context *context,
311 const diagnostic_info *diagnostic)
313 static const char *const diagnostic_kind_text[] = {
314 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
315 #include "diagnostic.def"
316 #undef DEFINE_DIAGNOSTIC_KIND
317 "must-not-happen"
319 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
321 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
322 const char *text_cs = "", *text_ce = "";
323 pretty_printer *pp = context->printer;
325 if (diagnostic_kind_color[diagnostic->kind])
327 text_cs = colorize_start (pp_show_color (pp),
328 diagnostic_kind_color[diagnostic->kind]);
329 text_ce = colorize_stop (pp_show_color (pp));
332 expanded_location s = diagnostic_expand_location (diagnostic);
333 char *location_text = diagnostic_get_location_text (context, s);
335 char *result = build_message_string ("%s %s%s%s", location_text,
336 text_cs, text, text_ce);
337 free (location_text);
338 return result;
341 /* Functions at which to stop the backtrace print. It's not
342 particularly helpful to print the callers of these functions. */
344 static const char * const bt_stop[] =
346 "main",
347 "toplev::main",
348 "execute_one_pass",
349 "compile_file",
352 /* A callback function passed to the backtrace_full function. */
354 static int
355 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
356 const char *function)
358 int *pcount = (int *) data;
360 /* If we don't have any useful information, don't print
361 anything. */
362 if (filename == NULL && function == NULL)
363 return 0;
365 /* Skip functions in diagnostic.c. */
366 if (*pcount == 0
367 && filename != NULL
368 && strcmp (lbasename (filename), "diagnostic.c") == 0)
369 return 0;
371 /* Print up to 20 functions. We could make this a --param, but
372 since this is only for debugging just use a constant for now. */
373 if (*pcount >= 20)
375 /* Returning a non-zero value stops the backtrace. */
376 return 1;
378 ++*pcount;
380 char *alc = NULL;
381 if (function != NULL)
383 char *str = cplus_demangle_v3 (function,
384 (DMGL_VERBOSE | DMGL_ANSI
385 | DMGL_GNU_V3 | DMGL_PARAMS));
386 if (str != NULL)
388 alc = str;
389 function = str;
392 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
394 size_t len = strlen (bt_stop[i]);
395 if (strncmp (function, bt_stop[i], len) == 0
396 && (function[len] == '\0' || function[len] == '('))
398 if (alc != NULL)
399 free (alc);
400 /* Returning a non-zero value stops the backtrace. */
401 return 1;
406 fprintf (stderr, "0x%lx %s\n\t%s:%d\n",
407 (unsigned long) pc,
408 function == NULL ? "???" : function,
409 filename == NULL ? "???" : filename,
410 lineno);
412 if (alc != NULL)
413 free (alc);
415 return 0;
418 /* A callback function passed to the backtrace_full function. This is
419 called if backtrace_full has an error. */
421 static void
422 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
424 if (errnum < 0)
426 /* This means that no debug info was available. Just quietly
427 skip printing backtrace info. */
428 return;
430 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
431 errnum == 0 ? "" : xstrerror (errnum));
434 /* Take any action which is expected to happen after the diagnostic
435 is written out. This function does not always return. */
436 void
437 diagnostic_action_after_output (diagnostic_context *context,
438 diagnostic_t diag_kind)
440 switch (diag_kind)
442 case DK_DEBUG:
443 case DK_NOTE:
444 case DK_ANACHRONISM:
445 case DK_WARNING:
446 break;
448 case DK_ERROR:
449 case DK_SORRY:
450 if (context->abort_on_error)
451 real_abort ();
452 if (context->fatal_errors)
454 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
455 diagnostic_finish (context);
456 exit (FATAL_EXIT_CODE);
458 if (context->max_errors != 0
459 && ((unsigned) (diagnostic_kind_count (context, DK_ERROR)
460 + diagnostic_kind_count (context, DK_SORRY)
461 + diagnostic_kind_count (context, DK_WERROR))
462 >= context->max_errors))
464 fnotice (stderr,
465 "compilation terminated due to -fmax-errors=%u.\n",
466 context->max_errors);
467 diagnostic_finish (context);
468 exit (FATAL_EXIT_CODE);
470 break;
472 case DK_ICE:
473 case DK_ICE_NOBT:
475 struct backtrace_state *state = NULL;
476 if (diag_kind == DK_ICE)
477 state = backtrace_create_state (NULL, 0, bt_err_callback, NULL);
478 int count = 0;
479 if (state != NULL)
480 backtrace_full (state, 2, bt_callback, bt_err_callback,
481 (void *) &count);
483 if (context->abort_on_error)
484 real_abort ();
486 fnotice (stderr, "Please submit a full bug report,\n"
487 "with preprocessed source if appropriate.\n");
488 if (count > 0)
489 fnotice (stderr,
490 ("Please include the complete backtrace "
491 "with any bug report.\n"));
492 fnotice (stderr, "See %s for instructions.\n", bug_report_url);
494 exit (ICE_EXIT_CODE);
497 case DK_FATAL:
498 if (context->abort_on_error)
499 real_abort ();
500 diagnostic_finish (context);
501 fnotice (stderr, "compilation terminated.\n");
502 exit (FATAL_EXIT_CODE);
504 default:
505 gcc_unreachable ();
509 void
510 diagnostic_report_current_module (diagnostic_context *context, location_t where)
512 const line_map_ordinary *map = NULL;
514 if (pp_needs_newline (context->printer))
516 pp_newline (context->printer);
517 pp_needs_newline (context->printer) = false;
520 if (where <= BUILTINS_LOCATION)
521 return;
523 linemap_resolve_location (line_table, where,
524 LRK_MACRO_DEFINITION_LOCATION,
525 &map);
527 if (map && diagnostic_last_module_changed (context, map))
529 diagnostic_set_last_module (context, map);
530 if (! MAIN_FILE_P (map))
532 map = INCLUDED_FROM (line_table, map);
533 if (context->show_column)
534 pp_verbatim (context->printer,
535 "In file included from %r%s:%d:%d%R", "locus",
536 LINEMAP_FILE (map),
537 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
538 else
539 pp_verbatim (context->printer,
540 "In file included from %r%s:%d%R", "locus",
541 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
542 while (! MAIN_FILE_P (map))
544 map = INCLUDED_FROM (line_table, map);
545 pp_verbatim (context->printer,
546 ",\n from %r%s:%d%R", "locus",
547 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
549 pp_verbatim (context->printer, ":");
550 pp_newline (context->printer);
555 void
556 default_diagnostic_starter (diagnostic_context *context,
557 diagnostic_info *diagnostic)
559 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
560 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
561 diagnostic));
564 void
565 default_diagnostic_start_span_fn (diagnostic_context *context,
566 expanded_location exploc)
568 pp_set_prefix (context->printer,
569 diagnostic_get_location_text (context, exploc));
570 pp_string (context->printer, "");
571 pp_newline (context->printer);
574 void
575 default_diagnostic_finalizer (diagnostic_context *context,
576 diagnostic_info *diagnostic)
578 diagnostic_show_locus (context, diagnostic);
579 pp_destroy_prefix (context->printer);
580 pp_flush (context->printer);
583 /* Interface to specify diagnostic kind overrides. Returns the
584 previous setting, or DK_UNSPECIFIED if the parameters are out of
585 range. If OPTION_INDEX is zero, the new setting is for all the
586 diagnostics. */
587 diagnostic_t
588 diagnostic_classify_diagnostic (diagnostic_context *context,
589 int option_index,
590 diagnostic_t new_kind,
591 location_t where)
593 diagnostic_t old_kind;
595 if (option_index < 0
596 || option_index >= context->n_opts
597 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
598 return DK_UNSPECIFIED;
600 old_kind = context->classify_diagnostic[option_index];
602 /* Handle pragmas separately, since we need to keep track of *where*
603 the pragmas were. */
604 if (where != UNKNOWN_LOCATION)
606 int i;
608 /* Record the command-line status, so we can reset it back on DK_POP. */
609 if (old_kind == DK_UNSPECIFIED)
611 old_kind = !context->option_enabled (option_index,
612 context->option_state)
613 ? DK_IGNORED : (context->warning_as_error_requested
614 ? DK_ERROR : DK_WARNING);
615 context->classify_diagnostic[option_index] = old_kind;
618 for (i = context->n_classification_history - 1; i >= 0; i --)
619 if (context->classification_history[i].option == option_index)
621 old_kind = context->classification_history[i].kind;
622 break;
625 i = context->n_classification_history;
626 context->classification_history =
627 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
628 * sizeof (diagnostic_classification_change_t));
629 context->classification_history[i].location = where;
630 context->classification_history[i].option = option_index;
631 context->classification_history[i].kind = new_kind;
632 context->n_classification_history ++;
634 else
635 context->classify_diagnostic[option_index] = new_kind;
637 return old_kind;
640 /* Save all diagnostic classifications in a stack. */
641 void
642 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
644 context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
645 context->push_list[context->n_push ++] = context->n_classification_history;
648 /* Restore the topmost classification set off the stack. If the stack
649 is empty, revert to the state based on command line parameters. */
650 void
651 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
653 int jump_to;
654 int i;
656 if (context->n_push)
657 jump_to = context->push_list [-- context->n_push];
658 else
659 jump_to = 0;
661 i = context->n_classification_history;
662 context->classification_history =
663 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
664 * sizeof (diagnostic_classification_change_t));
665 context->classification_history[i].location = where;
666 context->classification_history[i].option = jump_to;
667 context->classification_history[i].kind = DK_POP;
668 context->n_classification_history ++;
671 /* Helper function for print_parseable_fixits. Print TEXT to PP, obeying the
672 escaping rules for -fdiagnostics-parseable-fixits. */
674 static void
675 print_escaped_string (pretty_printer *pp, const char *text)
677 gcc_assert (pp);
678 gcc_assert (text);
680 pp_character (pp, '"');
681 for (const char *ch = text; *ch; ch++)
683 switch (*ch)
685 case '\\':
686 /* Escape backslash as two backslashes. */
687 pp_string (pp, "\\\\");
688 break;
689 case '\t':
690 /* Escape tab as "\t". */
691 pp_string (pp, "\\t");
692 break;
693 case '\n':
694 /* Escape newline as "\n". */
695 pp_string (pp, "\\n");
696 break;
697 case '"':
698 /* Escape doublequotes as \". */
699 pp_string (pp, "\\\"");
700 break;
701 default:
702 if (ISPRINT (*ch))
703 pp_character (pp, *ch);
704 else
705 /* Use octal for non-printable chars. */
707 unsigned char c = (*ch & 0xff);
708 pp_printf (pp, "\\%o%o%o", (c / 64), (c / 8) & 007, c & 007);
710 break;
713 pp_character (pp, '"');
716 /* Implementation of -fdiagnostics-parseable-fixits. Print a
717 machine-parseable version of all fixits in RICHLOC to PP. */
719 static void
720 print_parseable_fixits (pretty_printer *pp, rich_location *richloc)
722 gcc_assert (pp);
723 gcc_assert (richloc);
725 for (unsigned i = 0; i < richloc->get_num_fixit_hints (); i++)
727 const fixit_hint *hint = richloc->get_fixit_hint (i);
728 source_location start_loc = hint->get_start_loc ();
729 expanded_location start_exploc = expand_location (start_loc);
730 pp_string (pp, "fix-it:");
731 print_escaped_string (pp, start_exploc.file);
732 source_location end_loc;
734 /* For compatibility with clang, print as a half-open range. */
735 if (hint->maybe_get_end_loc (&end_loc))
737 expanded_location end_exploc = expand_location (end_loc);
738 pp_printf (pp, ":{%i:%i-%i:%i}:",
739 start_exploc.line, start_exploc.column,
740 end_exploc.line, end_exploc.column + 1);
742 else
744 pp_printf (pp, ":{%i:%i-%i:%i}:",
745 start_exploc.line, start_exploc.column,
746 start_exploc.line, start_exploc.column);
748 switch (hint->get_kind ())
750 case fixit_hint::INSERT:
752 const fixit_insert *insert
753 = static_cast <const fixit_insert *> (hint);
754 print_escaped_string (pp, insert->get_string ());
756 break;
758 case fixit_hint::REMOVE:
759 print_escaped_string (pp, "");
760 break;
762 case fixit_hint::REPLACE:
764 const fixit_replace *replace
765 = static_cast <const fixit_replace *> (hint);
766 print_escaped_string (pp, replace->get_string ());
768 break;
770 default:
771 gcc_unreachable ();
773 pp_newline (pp);
777 /* Report a diagnostic message (an error or a warning) as specified by
778 DC. This function is *the* subroutine in terms of which front-ends
779 should implement their specific diagnostic handling modules. The
780 front-end independent format specifiers are exactly those described
781 in the documentation of output_format.
782 Return true if a diagnostic was printed, false otherwise. */
784 bool
785 diagnostic_report_diagnostic (diagnostic_context *context,
786 diagnostic_info *diagnostic)
788 location_t location = diagnostic_location (diagnostic);
789 diagnostic_t orig_diag_kind = diagnostic->kind;
790 const char *saved_format_spec;
792 /* Give preference to being able to inhibit warnings, before they
793 get reclassified to something else. */
794 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
795 && !diagnostic_report_warnings_p (context, location))
796 return false;
798 if (diagnostic->kind == DK_PEDWARN)
800 diagnostic->kind = pedantic_warning_kind (context);
801 /* We do this to avoid giving the message for -pedantic-errors. */
802 orig_diag_kind = diagnostic->kind;
805 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
806 return false;
808 if (context->lock > 0)
810 /* If we're reporting an ICE in the middle of some other error,
811 try to flush out the previous error, then let this one
812 through. Don't do this more than once. */
813 if ((diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
814 && context->lock == 1)
815 pp_newline_and_flush (context->printer);
816 else
817 error_recursion (context);
820 /* If the user requested that warnings be treated as errors, so be
821 it. Note that we do this before the next block so that
822 individual warnings can be overridden back to warnings with
823 -Wno-error=*. */
824 if (context->warning_as_error_requested
825 && diagnostic->kind == DK_WARNING)
827 diagnostic->kind = DK_ERROR;
830 if (diagnostic->option_index
831 && diagnostic->option_index != permissive_error_option (context))
833 diagnostic_t diag_class = DK_UNSPECIFIED;
835 /* This tests if the user provided the appropriate -Wfoo or
836 -Wno-foo option. */
837 if (! context->option_enabled (diagnostic->option_index,
838 context->option_state))
839 return false;
841 /* This tests for #pragma diagnostic changes. */
842 if (context->n_classification_history > 0)
844 /* FIXME: Stupid search. Optimize later. */
845 for (int i = context->n_classification_history - 1; i >= 0; i --)
847 if (linemap_location_before_p
848 (line_table,
849 context->classification_history[i].location,
850 location))
852 if (context->classification_history[i].kind == (int) DK_POP)
854 i = context->classification_history[i].option;
855 continue;
857 int option = context->classification_history[i].option;
858 /* The option 0 is for all the diagnostics. */
859 if (option == 0 || option == diagnostic->option_index)
861 diag_class = context->classification_history[i].kind;
862 if (diag_class != DK_UNSPECIFIED)
863 diagnostic->kind = diag_class;
864 break;
869 /* This tests if the user provided the appropriate -Werror=foo
870 option. */
871 if (diag_class == DK_UNSPECIFIED
872 && context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
874 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
876 /* This allows for future extensions, like temporarily disabling
877 warnings for ranges of source code. */
878 if (diagnostic->kind == DK_IGNORED)
879 return false;
882 context->lock++;
884 if (diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
886 /* When not checking, ICEs are converted to fatal errors when an
887 error has already occurred. This is counteracted by
888 abort_on_error. */
889 if (!CHECKING_P
890 && (diagnostic_kind_count (context, DK_ERROR) > 0
891 || diagnostic_kind_count (context, DK_SORRY) > 0)
892 && !context->abort_on_error)
894 expanded_location s
895 = expand_location (diagnostic_location (diagnostic));
896 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
897 s.file, s.line);
898 exit (ICE_EXIT_CODE);
900 if (context->internal_error)
901 (*context->internal_error) (context,
902 diagnostic->message.format_spec,
903 diagnostic->message.args_ptr);
905 if (diagnostic->kind == DK_ERROR && orig_diag_kind == DK_WARNING)
906 ++diagnostic_kind_count (context, DK_WERROR);
907 else
908 ++diagnostic_kind_count (context, diagnostic->kind);
910 saved_format_spec = diagnostic->message.format_spec;
911 if (context->show_option_requested)
913 char *option_text;
915 option_text = context->option_name (context, diagnostic->option_index,
916 orig_diag_kind, diagnostic->kind);
918 if (option_text)
920 const char *cs
921 = colorize_start (pp_show_color (context->printer),
922 diagnostic_kind_color[diagnostic->kind]);
923 const char *ce = colorize_stop (pp_show_color (context->printer));
924 diagnostic->message.format_spec
925 = ACONCAT ((diagnostic->message.format_spec,
926 " ",
927 "[", cs, option_text, ce, "]",
928 NULL));
929 free (option_text);
932 diagnostic->message.x_data = &diagnostic->x_data;
933 diagnostic->x_data = NULL;
934 pp_format (context->printer, &diagnostic->message);
935 (*diagnostic_starter (context)) (context, diagnostic);
936 pp_output_formatted_text (context->printer);
937 (*diagnostic_finalizer (context)) (context, diagnostic);
938 if (context->parseable_fixits_p)
940 print_parseable_fixits (context->printer, diagnostic->richloc);
941 pp_flush (context->printer);
943 diagnostic_action_after_output (context, diagnostic->kind);
944 diagnostic->message.format_spec = saved_format_spec;
945 diagnostic->x_data = NULL;
947 context->lock--;
949 return true;
952 /* Given a partial pathname as input, return another pathname that
953 shares no directory elements with the pathname of __FILE__. This
954 is used by fancy_abort() to print `Internal compiler error in expr.c'
955 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
957 const char *
958 trim_filename (const char *name)
960 static const char this_file[] = __FILE__;
961 const char *p = name, *q = this_file;
963 /* First skip any "../" in each filename. This allows us to give a proper
964 reference to a file in a subdirectory. */
965 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
966 p += 3;
968 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
969 q += 3;
971 /* Now skip any parts the two filenames have in common. */
972 while (*p == *q && *p != 0 && *q != 0)
973 p++, q++;
975 /* Now go backwards until the previous directory separator. */
976 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
977 p--;
979 return p;
982 /* Standard error reporting routines in increasing order of severity.
983 All of these take arguments like printf. */
985 /* Text to be emitted verbatim to the error message stream; this
986 produces no prefix and disables line-wrapping. Use rarely. */
987 void
988 verbatim (const char *gmsgid, ...)
990 text_info text;
991 va_list ap;
993 va_start (ap, gmsgid);
994 text.err_no = errno;
995 text.args_ptr = &ap;
996 text.format_spec = _(gmsgid);
997 text.x_data = NULL;
998 pp_format_verbatim (global_dc->printer, &text);
999 pp_newline_and_flush (global_dc->printer);
1000 va_end (ap);
1003 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
1004 void
1005 diagnostic_append_note (diagnostic_context *context,
1006 location_t location,
1007 const char * gmsgid, ...)
1009 diagnostic_info diagnostic;
1010 va_list ap;
1011 const char *saved_prefix;
1012 rich_location richloc (line_table, location);
1014 va_start (ap, gmsgid);
1015 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_NOTE);
1016 if (context->inhibit_notes_p)
1018 va_end (ap);
1019 return;
1021 saved_prefix = pp_get_prefix (context->printer);
1022 pp_set_prefix (context->printer,
1023 diagnostic_build_prefix (context, &diagnostic));
1024 pp_format (context->printer, &diagnostic.message);
1025 pp_output_formatted_text (context->printer);
1026 pp_destroy_prefix (context->printer);
1027 pp_set_prefix (context->printer, saved_prefix);
1028 diagnostic_show_locus (context, &diagnostic);
1029 va_end (ap);
1032 /* Implement emit_diagnostic, inform, inform_at_rich_loc, warning, warning_at,
1033 warning_at_rich_loc, pedwarn, permerror, permerror_at_rich_loc, error,
1034 error_at, error_at_rich_loc, sorry, fatal_error, internal_error, and
1035 internal_error_no_backtrace, as documented and defined below. */
1036 static bool
1037 diagnostic_impl (rich_location *richloc, int opt,
1038 const char *gmsgid,
1039 va_list *ap, diagnostic_t kind)
1041 diagnostic_info diagnostic;
1042 if (kind == DK_PERMERROR)
1044 diagnostic_set_info (&diagnostic, gmsgid, ap, richloc,
1045 permissive_error_kind (global_dc));
1046 diagnostic.option_index = permissive_error_option (global_dc);
1048 else
1050 diagnostic_set_info (&diagnostic, gmsgid, ap, richloc, kind);
1051 if (kind == DK_WARNING || kind == DK_PEDWARN)
1052 diagnostic.option_index = opt;
1054 return report_diagnostic (&diagnostic);
1057 /* Implement inform_n, warning_n, and error_n, as documented and
1058 defined below. */
1059 static bool
1060 diagnostic_n_impl (location_t location, int opt, int n,
1061 const char *singular_gmsgid,
1062 const char *plural_gmsgid,
1063 va_list *ap, diagnostic_t kind)
1065 diagnostic_info diagnostic;
1066 rich_location richloc (line_table, location);
1067 diagnostic_set_info_translated (&diagnostic,
1068 ngettext (singular_gmsgid, plural_gmsgid, n),
1069 ap, &richloc, kind);
1070 if (kind == DK_WARNING)
1071 diagnostic.option_index = opt;
1072 return report_diagnostic (&diagnostic);
1075 bool
1076 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
1077 const char *gmsgid, ...)
1079 va_list ap;
1080 va_start (ap, gmsgid);
1081 rich_location richloc (line_table, location);
1082 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, kind);
1083 va_end (ap);
1084 return ret;
1087 /* An informative note at LOCATION. Use this for additional details on an error
1088 message. */
1089 void
1090 inform (location_t location, const char *gmsgid, ...)
1092 va_list ap;
1093 va_start (ap, gmsgid);
1094 rich_location richloc (line_table, location);
1095 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_NOTE);
1096 va_end (ap);
1099 /* Same as "inform", but at RICHLOC. */
1100 void
1101 inform_at_rich_loc (rich_location *richloc, const char *gmsgid, ...)
1103 va_list ap;
1104 va_start (ap, gmsgid);
1105 diagnostic_impl (richloc, -1, gmsgid, &ap, DK_NOTE);
1106 va_end (ap);
1109 /* An informative note at LOCATION. Use this for additional details on an
1110 error message. */
1111 void
1112 inform_n (location_t location, int n, const char *singular_gmsgid,
1113 const char *plural_gmsgid, ...)
1115 va_list ap;
1116 va_start (ap, plural_gmsgid);
1117 diagnostic_n_impl (location, -1, n, singular_gmsgid, plural_gmsgid,
1118 &ap, DK_NOTE);
1119 va_end (ap);
1122 /* A warning at INPUT_LOCATION. Use this for code which is correct according
1123 to the relevant language specification but is likely to be buggy anyway.
1124 Returns true if the warning was printed, false if it was inhibited. */
1125 bool
1126 warning (int opt, const char *gmsgid, ...)
1128 va_list ap;
1129 va_start (ap, gmsgid);
1130 rich_location richloc (line_table, input_location);
1131 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_WARNING);
1132 va_end (ap);
1133 return ret;
1136 /* A warning at LOCATION. Use this for code which is correct according to the
1137 relevant language specification but is likely to be buggy anyway.
1138 Returns true if the warning was printed, false if it was inhibited. */
1140 bool
1141 warning_at (location_t location, int opt, const char *gmsgid, ...)
1143 va_list ap;
1144 va_start (ap, gmsgid);
1145 rich_location richloc (line_table, location);
1146 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_WARNING);
1147 va_end (ap);
1148 return ret;
1151 /* Same as warning at, but using RICHLOC. */
1153 bool
1154 warning_at_rich_loc (rich_location *richloc, int opt, const char *gmsgid, ...)
1156 va_list ap;
1157 va_start (ap, gmsgid);
1158 bool ret = diagnostic_impl (richloc, opt, gmsgid, &ap, DK_WARNING);
1159 va_end (ap);
1160 return ret;
1163 /* A warning at LOCATION. Use this for code which is correct according to the
1164 relevant language specification but is likely to be buggy anyway.
1165 Returns true if the warning was printed, false if it was inhibited. */
1167 bool
1168 warning_n (location_t location, int opt, int n, const char *singular_gmsgid,
1169 const char *plural_gmsgid, ...)
1171 va_list ap;
1172 va_start (ap, plural_gmsgid);
1173 bool ret = diagnostic_n_impl (location, opt, n,
1174 singular_gmsgid, plural_gmsgid,
1175 &ap, DK_WARNING);
1176 va_end (ap);
1177 return ret;
1180 /* A "pedantic" warning at LOCATION: issues a warning unless
1181 -pedantic-errors was given on the command line, in which case it
1182 issues an error. Use this for diagnostics required by the relevant
1183 language standard, if you have chosen not to make them errors.
1185 Note that these diagnostics are issued independent of the setting
1186 of the -Wpedantic command-line switch. To get a warning enabled
1187 only with that switch, use either "if (pedantic) pedwarn
1188 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
1189 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
1191 Returns true if the warning was printed, false if it was inhibited. */
1193 bool
1194 pedwarn (location_t location, int opt, const char *gmsgid, ...)
1196 va_list ap;
1197 va_start (ap, gmsgid);
1198 rich_location richloc (line_table, location);
1199 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_PEDWARN);
1200 va_end (ap);
1201 return ret;
1204 /* Same as pedwarn, but using RICHLOC. */
1206 bool
1207 pedwarn_at_rich_loc (rich_location *richloc, int opt, const char *gmsgid, ...)
1209 va_list ap;
1210 va_start (ap, gmsgid);
1211 bool ret = diagnostic_impl (richloc, opt, gmsgid, &ap, DK_PEDWARN);
1212 va_end (ap);
1213 return ret;
1216 /* A "permissive" error at LOCATION: issues an error unless
1217 -fpermissive was given on the command line, in which case it issues
1218 a warning. Use this for things that really should be errors but we
1219 want to support legacy code.
1221 Returns true if the warning was printed, false if it was inhibited. */
1223 bool
1224 permerror (location_t location, const char *gmsgid, ...)
1226 va_list ap;
1227 va_start (ap, gmsgid);
1228 rich_location richloc (line_table, location);
1229 bool ret = diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_PERMERROR);
1230 va_end (ap);
1231 return ret;
1234 /* Same as "permerror", but at RICHLOC. */
1236 bool
1237 permerror_at_rich_loc (rich_location *richloc, const char *gmsgid, ...)
1239 va_list ap;
1240 va_start (ap, gmsgid);
1241 bool ret = diagnostic_impl (richloc, -1, gmsgid, &ap, DK_PERMERROR);
1242 va_end (ap);
1243 return ret;
1246 /* A hard error: the code is definitely ill-formed, and an object file
1247 will not be produced. */
1248 void
1249 error (const char *gmsgid, ...)
1251 va_list ap;
1252 va_start (ap, gmsgid);
1253 rich_location richloc (line_table, input_location);
1254 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ERROR);
1255 va_end (ap);
1258 /* A hard error: the code is definitely ill-formed, and an object file
1259 will not be produced. */
1260 void
1261 error_n (location_t location, int n, const char *singular_gmsgid,
1262 const char *plural_gmsgid, ...)
1264 va_list ap;
1265 va_start (ap, plural_gmsgid);
1266 diagnostic_n_impl (location, -1, n, singular_gmsgid, plural_gmsgid,
1267 &ap, DK_ERROR);
1268 va_end (ap);
1271 /* Same as above, but use location LOC instead of input_location. */
1272 void
1273 error_at (location_t loc, const char *gmsgid, ...)
1275 va_list ap;
1276 va_start (ap, gmsgid);
1277 rich_location richloc (line_table, loc);
1278 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ERROR);
1279 va_end (ap);
1282 /* Same as above, but use RICH_LOC. */
1284 void
1285 error_at_rich_loc (rich_location *richloc, const char *gmsgid, ...)
1287 va_list ap;
1288 va_start (ap, gmsgid);
1289 diagnostic_impl (richloc, -1, gmsgid, &ap, DK_ERROR);
1290 va_end (ap);
1293 /* "Sorry, not implemented." Use for a language feature which is
1294 required by the relevant specification but not implemented by GCC.
1295 An object file will not be produced. */
1296 void
1297 sorry (const char *gmsgid, ...)
1299 va_list ap;
1300 va_start (ap, gmsgid);
1301 rich_location richloc (line_table, input_location);
1302 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_SORRY);
1303 va_end (ap);
1306 /* Return true if an error or a "sorry" has been seen. Various
1307 processing is disabled after errors. */
1308 bool
1309 seen_error (void)
1311 return errorcount || sorrycount;
1314 /* An error which is severe enough that we make no attempt to
1315 continue. Do not use this for internal consistency checks; that's
1316 internal_error. Use of this function should be rare. */
1317 void
1318 fatal_error (location_t loc, const char *gmsgid, ...)
1320 va_list ap;
1321 va_start (ap, gmsgid);
1322 rich_location richloc (line_table, loc);
1323 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_FATAL);
1324 va_end (ap);
1326 gcc_unreachable ();
1329 /* An internal consistency check has failed. We make no attempt to
1330 continue. Note that unless there is debugging value to be had from
1331 a more specific message, or some other good reason, you should use
1332 abort () instead of calling this function directly. */
1333 void
1334 internal_error (const char *gmsgid, ...)
1336 va_list ap;
1337 va_start (ap, gmsgid);
1338 rich_location richloc (line_table, input_location);
1339 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ICE);
1340 va_end (ap);
1342 gcc_unreachable ();
1345 /* Like internal_error, but no backtrace will be printed. Used when
1346 the internal error does not happen at the current location, but happened
1347 somewhere else. */
1348 void
1349 internal_error_no_backtrace (const char *gmsgid, ...)
1351 va_list ap;
1352 va_start (ap, gmsgid);
1353 rich_location richloc (line_table, input_location);
1354 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ICE_NOBT);
1355 va_end (ap);
1357 gcc_unreachable ();
1360 /* Special case error functions. Most are implemented in terms of the
1361 above, or should be. */
1363 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1364 runs its second argument through gettext. */
1365 void
1366 fnotice (FILE *file, const char *cmsgid, ...)
1368 va_list ap;
1370 va_start (ap, cmsgid);
1371 vfprintf (file, _(cmsgid), ap);
1372 va_end (ap);
1375 /* Inform the user that an error occurred while trying to report some
1376 other error. This indicates catastrophic internal inconsistencies,
1377 so give up now. But do try to flush out the previous error.
1378 This mustn't use internal_error, that will cause infinite recursion. */
1380 static void
1381 error_recursion (diagnostic_context *context)
1383 if (context->lock < 3)
1384 pp_newline_and_flush (context->printer);
1386 fnotice (stderr,
1387 "Internal compiler error: Error reporting routines re-entered.\n");
1389 /* Call diagnostic_action_after_output to get the "please submit a bug
1390 report" message. */
1391 diagnostic_action_after_output (context, DK_ICE);
1393 /* Do not use gcc_unreachable here; that goes through internal_error
1394 and therefore would cause infinite recursion. */
1395 real_abort ();
1398 /* Report an internal compiler error in a friendly manner. This is
1399 the function that gets called upon use of abort() in the source
1400 code generally, thanks to a special macro. */
1402 void
1403 fancy_abort (const char *file, int line, const char *function)
1405 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1408 /* Really call the system 'abort'. This has to go right at the end of
1409 this file, so that there are no functions after it that call abort
1410 and get the system abort instead of our macro. */
1411 #undef abort
1412 static void
1413 real_abort (void)
1415 abort ();
1418 #if CHECKING_P
1420 namespace selftest {
1422 /* Helper function for test_print_escaped_string. */
1424 static void
1425 assert_print_escaped_string (const location &loc, const char *expected_output,
1426 const char *input)
1428 pretty_printer pp;
1429 print_escaped_string (&pp, input);
1430 ASSERT_STREQ_AT (loc, expected_output, pp_formatted_text (&pp));
1433 #define ASSERT_PRINT_ESCAPED_STRING_STREQ(EXPECTED_OUTPUT, INPUT) \
1434 assert_print_escaped_string (SELFTEST_LOCATION, EXPECTED_OUTPUT, INPUT)
1436 /* Tests of print_escaped_string. */
1438 static void
1439 test_print_escaped_string ()
1441 /* Empty string. */
1442 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"\"", "");
1444 /* Non-empty string. */
1445 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"hello world\"", "hello world");
1447 /* Various things that need to be escaped: */
1448 /* Backslash. */
1449 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\\after\"",
1450 "before\\after");
1451 /* Tab. */
1452 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\tafter\"",
1453 "before\tafter");
1454 /* Newline. */
1455 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\nafter\"",
1456 "before\nafter");
1457 /* Double quote. */
1458 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\"after\"",
1459 "before\"after");
1461 /* Non-printable characters: BEL: '\a': 0x07 */
1462 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\007after\"",
1463 "before\aafter");
1464 /* Non-printable characters: vertical tab: '\v': 0x0b */
1465 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\013after\"",
1466 "before\vafter");
1469 /* Tests of print_parseable_fixits. */
1471 /* Verify that print_parseable_fixits emits the empty string if there
1472 are no fixits. */
1474 static void
1475 test_print_parseable_fixits_none ()
1477 pretty_printer pp;
1478 rich_location richloc (line_table, UNKNOWN_LOCATION);
1480 print_parseable_fixits (&pp, &richloc);
1481 ASSERT_STREQ ("", pp_formatted_text (&pp));
1484 /* Verify that print_parseable_fixits does the right thing if there
1485 is an insertion fixit hint. */
1487 static void
1488 test_print_parseable_fixits_insert ()
1490 pretty_printer pp;
1491 rich_location richloc (line_table, UNKNOWN_LOCATION);
1493 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
1494 linemap_line_start (line_table, 5, 100);
1495 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1496 location_t where = linemap_position_for_column (line_table, 10);
1497 richloc.add_fixit_insert (where, "added content");
1499 print_parseable_fixits (&pp, &richloc);
1500 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:10}:\"added content\"\n",
1501 pp_formatted_text (&pp));
1504 /* Verify that print_parseable_fixits does the right thing if there
1505 is an removal fixit hint. */
1507 static void
1508 test_print_parseable_fixits_remove ()
1510 pretty_printer pp;
1511 rich_location richloc (line_table, UNKNOWN_LOCATION);
1513 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
1514 linemap_line_start (line_table, 5, 100);
1515 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1516 source_range where;
1517 where.m_start = linemap_position_for_column (line_table, 10);
1518 where.m_finish = linemap_position_for_column (line_table, 20);
1519 richloc.add_fixit_remove (where);
1521 print_parseable_fixits (&pp, &richloc);
1522 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"\"\n",
1523 pp_formatted_text (&pp));
1526 /* Verify that print_parseable_fixits does the right thing if there
1527 is an replacement fixit hint. */
1529 static void
1530 test_print_parseable_fixits_replace ()
1532 pretty_printer pp;
1533 rich_location richloc (line_table, UNKNOWN_LOCATION);
1535 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
1536 linemap_line_start (line_table, 5, 100);
1537 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1538 source_range where;
1539 where.m_start = linemap_position_for_column (line_table, 10);
1540 where.m_finish = linemap_position_for_column (line_table, 20);
1541 richloc.add_fixit_replace (where, "replacement");
1543 print_parseable_fixits (&pp, &richloc);
1544 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"replacement\"\n",
1545 pp_formatted_text (&pp));
1548 /* Run all of the selftests within this file. */
1550 void
1551 diagnostic_c_tests ()
1553 test_print_escaped_string ();
1554 test_print_parseable_fixits_none ();
1555 test_print_parseable_fixits_insert ();
1556 test_print_parseable_fixits_remove ();
1557 test_print_parseable_fixits_replace ();
1560 } // namespace selftest
1562 #endif /* #if CHECKING_P */