Implement C _FloatN, _FloatNx types.
[official-gcc.git] / gcc / diagnostic.c
blobfec48c4e441beaf2d261e67a59ebf4ee74ce9e84
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;
174 context->colorize_source_p = false;
175 context->show_ruler_p = false;
176 context->parseable_fixits_p = false;
179 /* Maybe initialize the color support. We require clients to do this
180 explicitly, since most clients don't want color. When called
181 without a VALUE, it initializes with DIAGNOSTICS_COLOR_DEFAULT. */
183 void
184 diagnostic_color_init (diagnostic_context *context, int value /*= -1 */)
186 /* value == -1 is the default value. */
187 if (value < 0)
189 /* If DIAGNOSTICS_COLOR_DEFAULT is -1, default to
190 -fdiagnostics-color=auto if GCC_COLORS is in the environment,
191 otherwise default to -fdiagnostics-color=never, for other
192 values default to that
193 -fdiagnostics-color={never,auto,always}. */
194 if (DIAGNOSTICS_COLOR_DEFAULT == -1)
196 if (!getenv ("GCC_COLORS"))
197 return;
198 value = DIAGNOSTICS_COLOR_AUTO;
200 else
201 value = DIAGNOSTICS_COLOR_DEFAULT;
203 pp_show_color (context->printer)
204 = colorize_init ((diagnostic_color_rule_t) value);
207 /* Do any cleaning up required after the last diagnostic is emitted. */
209 void
210 diagnostic_finish (diagnostic_context *context)
212 /* Some of the errors may actually have been warnings. */
213 if (diagnostic_kind_count (context, DK_WERROR))
215 /* -Werror was given. */
216 if (context->warning_as_error_requested)
217 pp_verbatim (context->printer,
218 _("%s: all warnings being treated as errors"),
219 progname);
220 /* At least one -Werror= was given. */
221 else
222 pp_verbatim (context->printer,
223 _("%s: some warnings being treated as errors"),
224 progname);
225 pp_newline_and_flush (context->printer);
228 diagnostic_file_cache_fini ();
230 XDELETEVEC (context->classify_diagnostic);
231 context->classify_diagnostic = NULL;
233 /* diagnostic_initialize allocates context->printer using XNEW
234 and placement-new. */
235 context->printer->~pretty_printer ();
236 XDELETE (context->printer);
237 context->printer = NULL;
240 /* Initialize DIAGNOSTIC, where the message MSG has already been
241 translated. */
242 void
243 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
244 va_list *args, rich_location *richloc,
245 diagnostic_t kind)
247 gcc_assert (richloc);
248 diagnostic->message.err_no = errno;
249 diagnostic->message.args_ptr = args;
250 diagnostic->message.format_spec = msg;
251 diagnostic->message.m_richloc = richloc;
252 diagnostic->richloc = richloc;
253 diagnostic->kind = kind;
254 diagnostic->option_index = 0;
257 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
258 translated. */
259 void
260 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
261 va_list *args, rich_location *richloc,
262 diagnostic_t kind)
264 gcc_assert (richloc);
265 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, richloc, kind);
268 static const char *const diagnostic_kind_color[] = {
269 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
270 #include "diagnostic.def"
271 #undef DEFINE_DIAGNOSTIC_KIND
272 NULL
275 /* Get a color name for diagnostics of type KIND
276 Result could be NULL. */
278 const char *
279 diagnostic_get_color_for_kind (diagnostic_t kind)
281 return diagnostic_kind_color[kind];
284 /* Return a malloc'd string describing a location e.g. "foo.c:42:10".
285 The caller is responsible for freeing the memory. */
287 static char *
288 diagnostic_get_location_text (diagnostic_context *context,
289 expanded_location s)
291 pretty_printer *pp = context->printer;
292 const char *locus_cs = colorize_start (pp_show_color (pp), "locus");
293 const char *locus_ce = colorize_stop (pp_show_color (pp));
295 if (s.file == NULL)
296 return build_message_string ("%s%s:%s", locus_cs, progname, locus_ce);
298 if (!strcmp (s.file, N_("<built-in>")))
299 return build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce);
301 if (context->show_column)
302 return build_message_string ("%s%s:%d:%d:%s", locus_cs, s.file, s.line,
303 s.column, locus_ce);
304 else
305 return build_message_string ("%s%s:%d:%s", locus_cs, s.file, s.line,
306 locus_ce);
309 /* Return a malloc'd string describing a location and the severity of the
310 diagnostic, e.g. "foo.c:42:10: error: ". The caller is responsible for
311 freeing the memory. */
312 char *
313 diagnostic_build_prefix (diagnostic_context *context,
314 const diagnostic_info *diagnostic)
316 static const char *const diagnostic_kind_text[] = {
317 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
318 #include "diagnostic.def"
319 #undef DEFINE_DIAGNOSTIC_KIND
320 "must-not-happen"
322 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
324 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
325 const char *text_cs = "", *text_ce = "";
326 pretty_printer *pp = context->printer;
328 if (diagnostic_kind_color[diagnostic->kind])
330 text_cs = colorize_start (pp_show_color (pp),
331 diagnostic_kind_color[diagnostic->kind]);
332 text_ce = colorize_stop (pp_show_color (pp));
335 expanded_location s = diagnostic_expand_location (diagnostic);
336 char *location_text = diagnostic_get_location_text (context, s);
338 char *result = build_message_string ("%s %s%s%s", location_text,
339 text_cs, text, text_ce);
340 free (location_text);
341 return result;
344 /* Functions at which to stop the backtrace print. It's not
345 particularly helpful to print the callers of these functions. */
347 static const char * const bt_stop[] =
349 "main",
350 "toplev::main",
351 "execute_one_pass",
352 "compile_file",
355 /* A callback function passed to the backtrace_full function. */
357 static int
358 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
359 const char *function)
361 int *pcount = (int *) data;
363 /* If we don't have any useful information, don't print
364 anything. */
365 if (filename == NULL && function == NULL)
366 return 0;
368 /* Skip functions in diagnostic.c. */
369 if (*pcount == 0
370 && filename != NULL
371 && strcmp (lbasename (filename), "diagnostic.c") == 0)
372 return 0;
374 /* Print up to 20 functions. We could make this a --param, but
375 since this is only for debugging just use a constant for now. */
376 if (*pcount >= 20)
378 /* Returning a non-zero value stops the backtrace. */
379 return 1;
381 ++*pcount;
383 char *alc = NULL;
384 if (function != NULL)
386 char *str = cplus_demangle_v3 (function,
387 (DMGL_VERBOSE | DMGL_ANSI
388 | DMGL_GNU_V3 | DMGL_PARAMS));
389 if (str != NULL)
391 alc = str;
392 function = str;
395 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
397 size_t len = strlen (bt_stop[i]);
398 if (strncmp (function, bt_stop[i], len) == 0
399 && (function[len] == '\0' || function[len] == '('))
401 if (alc != NULL)
402 free (alc);
403 /* Returning a non-zero value stops the backtrace. */
404 return 1;
409 fprintf (stderr, "0x%lx %s\n\t%s:%d\n",
410 (unsigned long) pc,
411 function == NULL ? "???" : function,
412 filename == NULL ? "???" : filename,
413 lineno);
415 if (alc != NULL)
416 free (alc);
418 return 0;
421 /* A callback function passed to the backtrace_full function. This is
422 called if backtrace_full has an error. */
424 static void
425 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
427 if (errnum < 0)
429 /* This means that no debug info was available. Just quietly
430 skip printing backtrace info. */
431 return;
433 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
434 errnum == 0 ? "" : xstrerror (errnum));
437 /* Take any action which is expected to happen after the diagnostic
438 is written out. This function does not always return. */
439 void
440 diagnostic_action_after_output (diagnostic_context *context,
441 diagnostic_t diag_kind)
443 switch (diag_kind)
445 case DK_DEBUG:
446 case DK_NOTE:
447 case DK_ANACHRONISM:
448 case DK_WARNING:
449 break;
451 case DK_ERROR:
452 case DK_SORRY:
453 if (context->abort_on_error)
454 real_abort ();
455 if (context->fatal_errors)
457 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
458 diagnostic_finish (context);
459 exit (FATAL_EXIT_CODE);
461 if (context->max_errors != 0
462 && ((unsigned) (diagnostic_kind_count (context, DK_ERROR)
463 + diagnostic_kind_count (context, DK_SORRY)
464 + diagnostic_kind_count (context, DK_WERROR))
465 >= context->max_errors))
467 fnotice (stderr,
468 "compilation terminated due to -fmax-errors=%u.\n",
469 context->max_errors);
470 diagnostic_finish (context);
471 exit (FATAL_EXIT_CODE);
473 break;
475 case DK_ICE:
476 case DK_ICE_NOBT:
478 struct backtrace_state *state = NULL;
479 if (diag_kind == DK_ICE)
480 state = backtrace_create_state (NULL, 0, bt_err_callback, NULL);
481 int count = 0;
482 if (state != NULL)
483 backtrace_full (state, 2, bt_callback, bt_err_callback,
484 (void *) &count);
486 if (context->abort_on_error)
487 real_abort ();
489 fnotice (stderr, "Please submit a full bug report,\n"
490 "with preprocessed source if appropriate.\n");
491 if (count > 0)
492 fnotice (stderr,
493 ("Please include the complete backtrace "
494 "with any bug report.\n"));
495 fnotice (stderr, "See %s for instructions.\n", bug_report_url);
497 exit (ICE_EXIT_CODE);
500 case DK_FATAL:
501 if (context->abort_on_error)
502 real_abort ();
503 diagnostic_finish (context);
504 fnotice (stderr, "compilation terminated.\n");
505 exit (FATAL_EXIT_CODE);
507 default:
508 gcc_unreachable ();
512 void
513 diagnostic_report_current_module (diagnostic_context *context, location_t where)
515 const line_map_ordinary *map = NULL;
517 if (pp_needs_newline (context->printer))
519 pp_newline (context->printer);
520 pp_needs_newline (context->printer) = false;
523 if (where <= BUILTINS_LOCATION)
524 return;
526 linemap_resolve_location (line_table, where,
527 LRK_MACRO_DEFINITION_LOCATION,
528 &map);
530 if (map && diagnostic_last_module_changed (context, map))
532 diagnostic_set_last_module (context, map);
533 if (! MAIN_FILE_P (map))
535 map = INCLUDED_FROM (line_table, map);
536 if (context->show_column)
537 pp_verbatim (context->printer,
538 "In file included from %r%s:%d:%d%R", "locus",
539 LINEMAP_FILE (map),
540 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
541 else
542 pp_verbatim (context->printer,
543 "In file included from %r%s:%d%R", "locus",
544 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
545 while (! MAIN_FILE_P (map))
547 map = INCLUDED_FROM (line_table, map);
548 pp_verbatim (context->printer,
549 ",\n from %r%s:%d%R", "locus",
550 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
552 pp_verbatim (context->printer, ":");
553 pp_newline (context->printer);
558 void
559 default_diagnostic_starter (diagnostic_context *context,
560 diagnostic_info *diagnostic)
562 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
563 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
564 diagnostic));
567 void
568 default_diagnostic_start_span_fn (diagnostic_context *context,
569 expanded_location exploc)
571 pp_set_prefix (context->printer,
572 diagnostic_get_location_text (context, exploc));
573 pp_string (context->printer, "");
574 pp_newline (context->printer);
577 void
578 default_diagnostic_finalizer (diagnostic_context *context,
579 diagnostic_info *diagnostic)
581 diagnostic_show_locus (context, diagnostic->richloc, diagnostic->kind);
582 pp_destroy_prefix (context->printer);
583 pp_flush (context->printer);
586 /* Interface to specify diagnostic kind overrides. Returns the
587 previous setting, or DK_UNSPECIFIED if the parameters are out of
588 range. If OPTION_INDEX is zero, the new setting is for all the
589 diagnostics. */
590 diagnostic_t
591 diagnostic_classify_diagnostic (diagnostic_context *context,
592 int option_index,
593 diagnostic_t new_kind,
594 location_t where)
596 diagnostic_t old_kind;
598 if (option_index < 0
599 || option_index >= context->n_opts
600 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
601 return DK_UNSPECIFIED;
603 old_kind = context->classify_diagnostic[option_index];
605 /* Handle pragmas separately, since we need to keep track of *where*
606 the pragmas were. */
607 if (where != UNKNOWN_LOCATION)
609 int i;
611 /* Record the command-line status, so we can reset it back on DK_POP. */
612 if (old_kind == DK_UNSPECIFIED)
614 old_kind = !context->option_enabled (option_index,
615 context->option_state)
616 ? DK_IGNORED : (context->warning_as_error_requested
617 ? DK_ERROR : DK_WARNING);
618 context->classify_diagnostic[option_index] = old_kind;
621 for (i = context->n_classification_history - 1; i >= 0; i --)
622 if (context->classification_history[i].option == option_index)
624 old_kind = context->classification_history[i].kind;
625 break;
628 i = context->n_classification_history;
629 context->classification_history =
630 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
631 * sizeof (diagnostic_classification_change_t));
632 context->classification_history[i].location = where;
633 context->classification_history[i].option = option_index;
634 context->classification_history[i].kind = new_kind;
635 context->n_classification_history ++;
637 else
638 context->classify_diagnostic[option_index] = new_kind;
640 return old_kind;
643 /* Save all diagnostic classifications in a stack. */
644 void
645 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
647 context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
648 context->push_list[context->n_push ++] = context->n_classification_history;
651 /* Restore the topmost classification set off the stack. If the stack
652 is empty, revert to the state based on command line parameters. */
653 void
654 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
656 int jump_to;
657 int i;
659 if (context->n_push)
660 jump_to = context->push_list [-- context->n_push];
661 else
662 jump_to = 0;
664 i = context->n_classification_history;
665 context->classification_history =
666 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
667 * sizeof (diagnostic_classification_change_t));
668 context->classification_history[i].location = where;
669 context->classification_history[i].option = jump_to;
670 context->classification_history[i].kind = DK_POP;
671 context->n_classification_history ++;
674 /* Helper function for print_parseable_fixits. Print TEXT to PP, obeying the
675 escaping rules for -fdiagnostics-parseable-fixits. */
677 static void
678 print_escaped_string (pretty_printer *pp, const char *text)
680 gcc_assert (pp);
681 gcc_assert (text);
683 pp_character (pp, '"');
684 for (const char *ch = text; *ch; ch++)
686 switch (*ch)
688 case '\\':
689 /* Escape backslash as two backslashes. */
690 pp_string (pp, "\\\\");
691 break;
692 case '\t':
693 /* Escape tab as "\t". */
694 pp_string (pp, "\\t");
695 break;
696 case '\n':
697 /* Escape newline as "\n". */
698 pp_string (pp, "\\n");
699 break;
700 case '"':
701 /* Escape doublequotes as \". */
702 pp_string (pp, "\\\"");
703 break;
704 default:
705 if (ISPRINT (*ch))
706 pp_character (pp, *ch);
707 else
708 /* Use octal for non-printable chars. */
710 unsigned char c = (*ch & 0xff);
711 pp_printf (pp, "\\%o%o%o", (c / 64), (c / 8) & 007, c & 007);
713 break;
716 pp_character (pp, '"');
719 /* Implementation of -fdiagnostics-parseable-fixits. Print a
720 machine-parseable version of all fixits in RICHLOC to PP. */
722 static void
723 print_parseable_fixits (pretty_printer *pp, rich_location *richloc)
725 gcc_assert (pp);
726 gcc_assert (richloc);
728 for (unsigned i = 0; i < richloc->get_num_fixit_hints (); i++)
730 const fixit_hint *hint = richloc->get_fixit_hint (i);
731 source_location start_loc = hint->get_start_loc ();
732 expanded_location start_exploc = expand_location (start_loc);
733 pp_string (pp, "fix-it:");
734 print_escaped_string (pp, start_exploc.file);
735 source_location end_loc;
737 /* For compatibility with clang, print as a half-open range. */
738 if (hint->maybe_get_end_loc (&end_loc))
740 expanded_location end_exploc = expand_location (end_loc);
741 pp_printf (pp, ":{%i:%i-%i:%i}:",
742 start_exploc.line, start_exploc.column,
743 end_exploc.line, end_exploc.column + 1);
745 else
747 pp_printf (pp, ":{%i:%i-%i:%i}:",
748 start_exploc.line, start_exploc.column,
749 start_exploc.line, start_exploc.column);
751 switch (hint->get_kind ())
753 case fixit_hint::INSERT:
755 const fixit_insert *insert
756 = static_cast <const fixit_insert *> (hint);
757 print_escaped_string (pp, insert->get_string ());
759 break;
761 case fixit_hint::REMOVE:
762 print_escaped_string (pp, "");
763 break;
765 case fixit_hint::REPLACE:
767 const fixit_replace *replace
768 = static_cast <const fixit_replace *> (hint);
769 print_escaped_string (pp, replace->get_string ());
771 break;
773 default:
774 gcc_unreachable ();
776 pp_newline (pp);
780 /* Report a diagnostic message (an error or a warning) as specified by
781 DC. This function is *the* subroutine in terms of which front-ends
782 should implement their specific diagnostic handling modules. The
783 front-end independent format specifiers are exactly those described
784 in the documentation of output_format.
785 Return true if a diagnostic was printed, false otherwise. */
787 bool
788 diagnostic_report_diagnostic (diagnostic_context *context,
789 diagnostic_info *diagnostic)
791 location_t location = diagnostic_location (diagnostic);
792 diagnostic_t orig_diag_kind = diagnostic->kind;
793 const char *saved_format_spec;
795 /* Give preference to being able to inhibit warnings, before they
796 get reclassified to something else. */
797 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
798 && !diagnostic_report_warnings_p (context, location))
799 return false;
801 if (diagnostic->kind == DK_PEDWARN)
803 diagnostic->kind = pedantic_warning_kind (context);
804 /* We do this to avoid giving the message for -pedantic-errors. */
805 orig_diag_kind = diagnostic->kind;
808 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
809 return false;
811 if (context->lock > 0)
813 /* If we're reporting an ICE in the middle of some other error,
814 try to flush out the previous error, then let this one
815 through. Don't do this more than once. */
816 if ((diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
817 && context->lock == 1)
818 pp_newline_and_flush (context->printer);
819 else
820 error_recursion (context);
823 /* If the user requested that warnings be treated as errors, so be
824 it. Note that we do this before the next block so that
825 individual warnings can be overridden back to warnings with
826 -Wno-error=*. */
827 if (context->warning_as_error_requested
828 && diagnostic->kind == DK_WARNING)
830 diagnostic->kind = DK_ERROR;
833 if (diagnostic->option_index
834 && diagnostic->option_index != permissive_error_option (context))
836 diagnostic_t diag_class = DK_UNSPECIFIED;
838 /* This tests if the user provided the appropriate -Wfoo or
839 -Wno-foo option. */
840 if (! context->option_enabled (diagnostic->option_index,
841 context->option_state))
842 return false;
844 /* This tests for #pragma diagnostic changes. */
845 if (context->n_classification_history > 0)
847 /* FIXME: Stupid search. Optimize later. */
848 for (int i = context->n_classification_history - 1; i >= 0; i --)
850 if (linemap_location_before_p
851 (line_table,
852 context->classification_history[i].location,
853 location))
855 if (context->classification_history[i].kind == (int) DK_POP)
857 i = context->classification_history[i].option;
858 continue;
860 int option = context->classification_history[i].option;
861 /* The option 0 is for all the diagnostics. */
862 if (option == 0 || option == diagnostic->option_index)
864 diag_class = context->classification_history[i].kind;
865 if (diag_class != DK_UNSPECIFIED)
866 diagnostic->kind = diag_class;
867 break;
872 /* This tests if the user provided the appropriate -Werror=foo
873 option. */
874 if (diag_class == DK_UNSPECIFIED
875 && context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
877 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
879 /* This allows for future extensions, like temporarily disabling
880 warnings for ranges of source code. */
881 if (diagnostic->kind == DK_IGNORED)
882 return false;
885 context->lock++;
887 if (diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
889 /* When not checking, ICEs are converted to fatal errors when an
890 error has already occurred. This is counteracted by
891 abort_on_error. */
892 if (!CHECKING_P
893 && (diagnostic_kind_count (context, DK_ERROR) > 0
894 || diagnostic_kind_count (context, DK_SORRY) > 0)
895 && !context->abort_on_error)
897 expanded_location s
898 = expand_location (diagnostic_location (diagnostic));
899 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
900 s.file, s.line);
901 exit (ICE_EXIT_CODE);
903 if (context->internal_error)
904 (*context->internal_error) (context,
905 diagnostic->message.format_spec,
906 diagnostic->message.args_ptr);
908 if (diagnostic->kind == DK_ERROR && orig_diag_kind == DK_WARNING)
909 ++diagnostic_kind_count (context, DK_WERROR);
910 else
911 ++diagnostic_kind_count (context, diagnostic->kind);
913 saved_format_spec = diagnostic->message.format_spec;
914 if (context->show_option_requested)
916 char *option_text;
918 option_text = context->option_name (context, diagnostic->option_index,
919 orig_diag_kind, diagnostic->kind);
921 if (option_text)
923 const char *cs
924 = colorize_start (pp_show_color (context->printer),
925 diagnostic_kind_color[diagnostic->kind]);
926 const char *ce = colorize_stop (pp_show_color (context->printer));
927 diagnostic->message.format_spec
928 = ACONCAT ((diagnostic->message.format_spec,
929 " ",
930 "[", cs, option_text, ce, "]",
931 NULL));
932 free (option_text);
935 diagnostic->message.x_data = &diagnostic->x_data;
936 diagnostic->x_data = NULL;
937 pp_format (context->printer, &diagnostic->message);
938 (*diagnostic_starter (context)) (context, diagnostic);
939 pp_output_formatted_text (context->printer);
940 (*diagnostic_finalizer (context)) (context, diagnostic);
941 if (context->parseable_fixits_p)
943 print_parseable_fixits (context->printer, diagnostic->richloc);
944 pp_flush (context->printer);
946 diagnostic_action_after_output (context, diagnostic->kind);
947 diagnostic->message.format_spec = saved_format_spec;
948 diagnostic->x_data = NULL;
950 context->lock--;
952 return true;
955 /* Given a partial pathname as input, return another pathname that
956 shares no directory elements with the pathname of __FILE__. This
957 is used by fancy_abort() to print `Internal compiler error in expr.c'
958 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
960 const char *
961 trim_filename (const char *name)
963 static const char this_file[] = __FILE__;
964 const char *p = name, *q = this_file;
966 /* First skip any "../" in each filename. This allows us to give a proper
967 reference to a file in a subdirectory. */
968 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
969 p += 3;
971 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
972 q += 3;
974 /* Now skip any parts the two filenames have in common. */
975 while (*p == *q && *p != 0 && *q != 0)
976 p++, q++;
978 /* Now go backwards until the previous directory separator. */
979 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
980 p--;
982 return p;
985 /* Standard error reporting routines in increasing order of severity.
986 All of these take arguments like printf. */
988 /* Text to be emitted verbatim to the error message stream; this
989 produces no prefix and disables line-wrapping. Use rarely. */
990 void
991 verbatim (const char *gmsgid, ...)
993 text_info text;
994 va_list ap;
996 va_start (ap, gmsgid);
997 text.err_no = errno;
998 text.args_ptr = &ap;
999 text.format_spec = _(gmsgid);
1000 text.x_data = NULL;
1001 pp_format_verbatim (global_dc->printer, &text);
1002 pp_newline_and_flush (global_dc->printer);
1003 va_end (ap);
1006 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
1007 void
1008 diagnostic_append_note (diagnostic_context *context,
1009 location_t location,
1010 const char * gmsgid, ...)
1012 diagnostic_info diagnostic;
1013 va_list ap;
1014 const char *saved_prefix;
1015 rich_location richloc (line_table, location);
1017 va_start (ap, gmsgid);
1018 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_NOTE);
1019 if (context->inhibit_notes_p)
1021 va_end (ap);
1022 return;
1024 saved_prefix = pp_get_prefix (context->printer);
1025 pp_set_prefix (context->printer,
1026 diagnostic_build_prefix (context, &diagnostic));
1027 pp_format (context->printer, &diagnostic.message);
1028 pp_output_formatted_text (context->printer);
1029 pp_destroy_prefix (context->printer);
1030 pp_set_prefix (context->printer, saved_prefix);
1031 diagnostic_show_locus (context, &richloc, DK_NOTE);
1032 va_end (ap);
1035 /* Implement emit_diagnostic, inform, inform_at_rich_loc, warning, warning_at,
1036 warning_at_rich_loc, pedwarn, permerror, permerror_at_rich_loc, error,
1037 error_at, error_at_rich_loc, sorry, fatal_error, internal_error, and
1038 internal_error_no_backtrace, as documented and defined below. */
1039 static bool
1040 diagnostic_impl (rich_location *richloc, int opt,
1041 const char *gmsgid,
1042 va_list *ap, diagnostic_t kind)
1044 diagnostic_info diagnostic;
1045 if (kind == DK_PERMERROR)
1047 diagnostic_set_info (&diagnostic, gmsgid, ap, richloc,
1048 permissive_error_kind (global_dc));
1049 diagnostic.option_index = permissive_error_option (global_dc);
1051 else
1053 diagnostic_set_info (&diagnostic, gmsgid, ap, richloc, kind);
1054 if (kind == DK_WARNING || kind == DK_PEDWARN)
1055 diagnostic.option_index = opt;
1057 return report_diagnostic (&diagnostic);
1060 /* Implement inform_n, warning_n, and error_n, as documented and
1061 defined below. */
1062 static bool
1063 diagnostic_n_impl (location_t location, int opt, int n,
1064 const char *singular_gmsgid,
1065 const char *plural_gmsgid,
1066 va_list *ap, diagnostic_t kind)
1068 diagnostic_info diagnostic;
1069 rich_location richloc (line_table, location);
1070 diagnostic_set_info_translated (&diagnostic,
1071 ngettext (singular_gmsgid, plural_gmsgid, n),
1072 ap, &richloc, kind);
1073 if (kind == DK_WARNING)
1074 diagnostic.option_index = opt;
1075 return report_diagnostic (&diagnostic);
1078 bool
1079 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
1080 const char *gmsgid, ...)
1082 va_list ap;
1083 va_start (ap, gmsgid);
1084 rich_location richloc (line_table, location);
1085 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, kind);
1086 va_end (ap);
1087 return ret;
1090 /* An informative note at LOCATION. Use this for additional details on an error
1091 message. */
1092 void
1093 inform (location_t location, const char *gmsgid, ...)
1095 va_list ap;
1096 va_start (ap, gmsgid);
1097 rich_location richloc (line_table, location);
1098 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_NOTE);
1099 va_end (ap);
1102 /* Same as "inform", but at RICHLOC. */
1103 void
1104 inform_at_rich_loc (rich_location *richloc, const char *gmsgid, ...)
1106 va_list ap;
1107 va_start (ap, gmsgid);
1108 diagnostic_impl (richloc, -1, gmsgid, &ap, DK_NOTE);
1109 va_end (ap);
1112 /* An informative note at LOCATION. Use this for additional details on an
1113 error message. */
1114 void
1115 inform_n (location_t location, int n, const char *singular_gmsgid,
1116 const char *plural_gmsgid, ...)
1118 va_list ap;
1119 va_start (ap, plural_gmsgid);
1120 diagnostic_n_impl (location, -1, n, singular_gmsgid, plural_gmsgid,
1121 &ap, DK_NOTE);
1122 va_end (ap);
1125 /* A warning at INPUT_LOCATION. Use this for code which is correct according
1126 to the relevant language specification but is likely to be buggy anyway.
1127 Returns true if the warning was printed, false if it was inhibited. */
1128 bool
1129 warning (int opt, const char *gmsgid, ...)
1131 va_list ap;
1132 va_start (ap, gmsgid);
1133 rich_location richloc (line_table, input_location);
1134 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_WARNING);
1135 va_end (ap);
1136 return ret;
1139 /* A warning at LOCATION. Use this for code which is correct according to the
1140 relevant language specification but is likely to be buggy anyway.
1141 Returns true if the warning was printed, false if it was inhibited. */
1143 bool
1144 warning_at (location_t location, int opt, const char *gmsgid, ...)
1146 va_list ap;
1147 va_start (ap, gmsgid);
1148 rich_location richloc (line_table, location);
1149 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_WARNING);
1150 va_end (ap);
1151 return ret;
1154 /* Same as warning at, but using RICHLOC. */
1156 bool
1157 warning_at_rich_loc (rich_location *richloc, int opt, const char *gmsgid, ...)
1159 va_list ap;
1160 va_start (ap, gmsgid);
1161 bool ret = diagnostic_impl (richloc, opt, gmsgid, &ap, DK_WARNING);
1162 va_end (ap);
1163 return ret;
1166 /* A warning at LOCATION. Use this for code which is correct according to the
1167 relevant language specification but is likely to be buggy anyway.
1168 Returns true if the warning was printed, false if it was inhibited. */
1170 bool
1171 warning_n (location_t location, int opt, int n, const char *singular_gmsgid,
1172 const char *plural_gmsgid, ...)
1174 va_list ap;
1175 va_start (ap, plural_gmsgid);
1176 bool ret = diagnostic_n_impl (location, opt, n,
1177 singular_gmsgid, plural_gmsgid,
1178 &ap, DK_WARNING);
1179 va_end (ap);
1180 return ret;
1183 /* A "pedantic" warning at LOCATION: issues a warning unless
1184 -pedantic-errors was given on the command line, in which case it
1185 issues an error. Use this for diagnostics required by the relevant
1186 language standard, if you have chosen not to make them errors.
1188 Note that these diagnostics are issued independent of the setting
1189 of the -Wpedantic command-line switch. To get a warning enabled
1190 only with that switch, use either "if (pedantic) pedwarn
1191 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
1192 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
1194 Returns true if the warning was printed, false if it was inhibited. */
1196 bool
1197 pedwarn (location_t location, int opt, const char *gmsgid, ...)
1199 va_list ap;
1200 va_start (ap, gmsgid);
1201 rich_location richloc (line_table, location);
1202 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_PEDWARN);
1203 va_end (ap);
1204 return ret;
1207 /* Same as pedwarn, but using RICHLOC. */
1209 bool
1210 pedwarn_at_rich_loc (rich_location *richloc, int opt, const char *gmsgid, ...)
1212 va_list ap;
1213 va_start (ap, gmsgid);
1214 bool ret = diagnostic_impl (richloc, opt, gmsgid, &ap, DK_PEDWARN);
1215 va_end (ap);
1216 return ret;
1219 /* A "permissive" error at LOCATION: issues an error unless
1220 -fpermissive was given on the command line, in which case it issues
1221 a warning. Use this for things that really should be errors but we
1222 want to support legacy code.
1224 Returns true if the warning was printed, false if it was inhibited. */
1226 bool
1227 permerror (location_t location, const char *gmsgid, ...)
1229 va_list ap;
1230 va_start (ap, gmsgid);
1231 rich_location richloc (line_table, location);
1232 bool ret = diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_PERMERROR);
1233 va_end (ap);
1234 return ret;
1237 /* Same as "permerror", but at RICHLOC. */
1239 bool
1240 permerror_at_rich_loc (rich_location *richloc, const char *gmsgid, ...)
1242 va_list ap;
1243 va_start (ap, gmsgid);
1244 bool ret = diagnostic_impl (richloc, -1, gmsgid, &ap, DK_PERMERROR);
1245 va_end (ap);
1246 return ret;
1249 /* A hard error: the code is definitely ill-formed, and an object file
1250 will not be produced. */
1251 void
1252 error (const char *gmsgid, ...)
1254 va_list ap;
1255 va_start (ap, gmsgid);
1256 rich_location richloc (line_table, input_location);
1257 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ERROR);
1258 va_end (ap);
1261 /* A hard error: the code is definitely ill-formed, and an object file
1262 will not be produced. */
1263 void
1264 error_n (location_t location, int n, const char *singular_gmsgid,
1265 const char *plural_gmsgid, ...)
1267 va_list ap;
1268 va_start (ap, plural_gmsgid);
1269 diagnostic_n_impl (location, -1, n, singular_gmsgid, plural_gmsgid,
1270 &ap, DK_ERROR);
1271 va_end (ap);
1274 /* Same as above, but use location LOC instead of input_location. */
1275 void
1276 error_at (location_t loc, const char *gmsgid, ...)
1278 va_list ap;
1279 va_start (ap, gmsgid);
1280 rich_location richloc (line_table, loc);
1281 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ERROR);
1282 va_end (ap);
1285 /* Same as above, but use RICH_LOC. */
1287 void
1288 error_at_rich_loc (rich_location *richloc, const char *gmsgid, ...)
1290 va_list ap;
1291 va_start (ap, gmsgid);
1292 diagnostic_impl (richloc, -1, gmsgid, &ap, DK_ERROR);
1293 va_end (ap);
1296 /* "Sorry, not implemented." Use for a language feature which is
1297 required by the relevant specification but not implemented by GCC.
1298 An object file will not be produced. */
1299 void
1300 sorry (const char *gmsgid, ...)
1302 va_list ap;
1303 va_start (ap, gmsgid);
1304 rich_location richloc (line_table, input_location);
1305 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_SORRY);
1306 va_end (ap);
1309 /* Return true if an error or a "sorry" has been seen. Various
1310 processing is disabled after errors. */
1311 bool
1312 seen_error (void)
1314 return errorcount || sorrycount;
1317 /* An error which is severe enough that we make no attempt to
1318 continue. Do not use this for internal consistency checks; that's
1319 internal_error. Use of this function should be rare. */
1320 void
1321 fatal_error (location_t loc, const char *gmsgid, ...)
1323 va_list ap;
1324 va_start (ap, gmsgid);
1325 rich_location richloc (line_table, loc);
1326 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_FATAL);
1327 va_end (ap);
1329 gcc_unreachable ();
1332 /* An internal consistency check has failed. We make no attempt to
1333 continue. Note that unless there is debugging value to be had from
1334 a more specific message, or some other good reason, you should use
1335 abort () instead of calling this function directly. */
1336 void
1337 internal_error (const char *gmsgid, ...)
1339 va_list ap;
1340 va_start (ap, gmsgid);
1341 rich_location richloc (line_table, input_location);
1342 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ICE);
1343 va_end (ap);
1345 gcc_unreachable ();
1348 /* Like internal_error, but no backtrace will be printed. Used when
1349 the internal error does not happen at the current location, but happened
1350 somewhere else. */
1351 void
1352 internal_error_no_backtrace (const char *gmsgid, ...)
1354 va_list ap;
1355 va_start (ap, gmsgid);
1356 rich_location richloc (line_table, input_location);
1357 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ICE_NOBT);
1358 va_end (ap);
1360 gcc_unreachable ();
1363 /* Special case error functions. Most are implemented in terms of the
1364 above, or should be. */
1366 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1367 runs its second argument through gettext. */
1368 void
1369 fnotice (FILE *file, const char *cmsgid, ...)
1371 va_list ap;
1373 va_start (ap, cmsgid);
1374 vfprintf (file, _(cmsgid), ap);
1375 va_end (ap);
1378 /* Inform the user that an error occurred while trying to report some
1379 other error. This indicates catastrophic internal inconsistencies,
1380 so give up now. But do try to flush out the previous error.
1381 This mustn't use internal_error, that will cause infinite recursion. */
1383 static void
1384 error_recursion (diagnostic_context *context)
1386 if (context->lock < 3)
1387 pp_newline_and_flush (context->printer);
1389 fnotice (stderr,
1390 "Internal compiler error: Error reporting routines re-entered.\n");
1392 /* Call diagnostic_action_after_output to get the "please submit a bug
1393 report" message. */
1394 diagnostic_action_after_output (context, DK_ICE);
1396 /* Do not use gcc_unreachable here; that goes through internal_error
1397 and therefore would cause infinite recursion. */
1398 real_abort ();
1401 /* Report an internal compiler error in a friendly manner. This is
1402 the function that gets called upon use of abort() in the source
1403 code generally, thanks to a special macro. */
1405 void
1406 fancy_abort (const char *file, int line, const char *function)
1408 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1411 /* Really call the system 'abort'. This has to go right at the end of
1412 this file, so that there are no functions after it that call abort
1413 and get the system abort instead of our macro. */
1414 #undef abort
1415 static void
1416 real_abort (void)
1418 abort ();
1421 #if CHECKING_P
1423 namespace selftest {
1425 /* Helper function for test_print_escaped_string. */
1427 static void
1428 assert_print_escaped_string (const location &loc, const char *expected_output,
1429 const char *input)
1431 pretty_printer pp;
1432 print_escaped_string (&pp, input);
1433 ASSERT_STREQ_AT (loc, expected_output, pp_formatted_text (&pp));
1436 #define ASSERT_PRINT_ESCAPED_STRING_STREQ(EXPECTED_OUTPUT, INPUT) \
1437 assert_print_escaped_string (SELFTEST_LOCATION, EXPECTED_OUTPUT, INPUT)
1439 /* Tests of print_escaped_string. */
1441 static void
1442 test_print_escaped_string ()
1444 /* Empty string. */
1445 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"\"", "");
1447 /* Non-empty string. */
1448 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"hello world\"", "hello world");
1450 /* Various things that need to be escaped: */
1451 /* Backslash. */
1452 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\\after\"",
1453 "before\\after");
1454 /* Tab. */
1455 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\tafter\"",
1456 "before\tafter");
1457 /* Newline. */
1458 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\nafter\"",
1459 "before\nafter");
1460 /* Double quote. */
1461 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\"after\"",
1462 "before\"after");
1464 /* Non-printable characters: BEL: '\a': 0x07 */
1465 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\007after\"",
1466 "before\aafter");
1467 /* Non-printable characters: vertical tab: '\v': 0x0b */
1468 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\013after\"",
1469 "before\vafter");
1472 /* Tests of print_parseable_fixits. */
1474 /* Verify that print_parseable_fixits emits the empty string if there
1475 are no fixits. */
1477 static void
1478 test_print_parseable_fixits_none ()
1480 pretty_printer pp;
1481 rich_location richloc (line_table, UNKNOWN_LOCATION);
1483 print_parseable_fixits (&pp, &richloc);
1484 ASSERT_STREQ ("", pp_formatted_text (&pp));
1487 /* Verify that print_parseable_fixits does the right thing if there
1488 is an insertion fixit hint. */
1490 static void
1491 test_print_parseable_fixits_insert ()
1493 pretty_printer pp;
1494 rich_location richloc (line_table, UNKNOWN_LOCATION);
1496 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
1497 linemap_line_start (line_table, 5, 100);
1498 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1499 location_t where = linemap_position_for_column (line_table, 10);
1500 richloc.add_fixit_insert (where, "added content");
1502 print_parseable_fixits (&pp, &richloc);
1503 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:10}:\"added content\"\n",
1504 pp_formatted_text (&pp));
1507 /* Verify that print_parseable_fixits does the right thing if there
1508 is an removal fixit hint. */
1510 static void
1511 test_print_parseable_fixits_remove ()
1513 pretty_printer pp;
1514 rich_location richloc (line_table, UNKNOWN_LOCATION);
1516 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
1517 linemap_line_start (line_table, 5, 100);
1518 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1519 source_range where;
1520 where.m_start = linemap_position_for_column (line_table, 10);
1521 where.m_finish = linemap_position_for_column (line_table, 20);
1522 richloc.add_fixit_remove (where);
1524 print_parseable_fixits (&pp, &richloc);
1525 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"\"\n",
1526 pp_formatted_text (&pp));
1529 /* Verify that print_parseable_fixits does the right thing if there
1530 is an replacement fixit hint. */
1532 static void
1533 test_print_parseable_fixits_replace ()
1535 pretty_printer pp;
1536 rich_location richloc (line_table, UNKNOWN_LOCATION);
1538 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
1539 linemap_line_start (line_table, 5, 100);
1540 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1541 source_range where;
1542 where.m_start = linemap_position_for_column (line_table, 10);
1543 where.m_finish = linemap_position_for_column (line_table, 20);
1544 richloc.add_fixit_replace (where, "replacement");
1546 print_parseable_fixits (&pp, &richloc);
1547 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"replacement\"\n",
1548 pp_formatted_text (&pp));
1551 /* Run all of the selftests within this file. */
1553 void
1554 diagnostic_c_tests ()
1556 test_print_escaped_string ();
1557 test_print_parseable_fixits_none ();
1558 test_print_parseable_fixits_insert ();
1559 test_print_parseable_fixits_remove ();
1560 test_print_parseable_fixits_replace ();
1563 } // namespace selftest
1565 #endif /* #if CHECKING_P */