]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/diagnostic.c
Implement -fdiagnostics-parseable-fixits
[thirdparty/gcc.git] / gcc / diagnostic.c
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>
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
21
22 /* This file implements the language independent aspect of diagnostic
23 message module. */
24
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"
35
36 #ifdef HAVE_TERMIOS_H
37 # include <termios.h>
38 #endif
39
40 #ifdef GWINSZ_IN_SYS_IOCTL
41 # include <sys/ioctl.h>
42 #endif
43
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)
48
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;
57
58 /* Name of program invoked, sans directories. */
59
60 const char *progname;
61
62 /* A diagnostic_context surrogate for stderr. */
63 static diagnostic_context global_diagnostic_context;
64 diagnostic_context *global_dc = &global_diagnostic_context;
65 \f
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, ...)
70 {
71 char *str;
72 va_list ap;
73
74 va_start (ap, msg);
75 str = xvasprintf (msg, ap);
76 va_end (ap);
77
78 return str;
79 }
80
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)
84 {
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);
89 }
90
91
92 \f
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)
98 {
99 const char * s = getenv ("COLUMNS");
100 if (s != NULL) {
101 int n = atoi (s);
102 if (n > 0)
103 return n;
104 }
105
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
112
113 return INT_MAX;
114 }
115
116 /* Set caret_max_width to value. */
117 void
118 diagnostic_set_caret_max_width (diagnostic_context *context, int value)
119 {
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);
124
125 if (value <= 0)
126 value = INT_MAX;
127
128 context->caret_max_width = value;
129 }
130
131 /* Initialize the diagnostic message outputting machinery. */
132 void
133 diagnostic_initialize (diagnostic_context *context, int n_opts)
134 {
135 int i;
136
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 ();
141
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 }
175
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. */
179
180 void
181 diagnostic_color_init (diagnostic_context *context, int value /*= -1 */)
182 {
183 /* value == -1 is the default value. */
184 if (value < 0)
185 {
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)
192 {
193 if (!getenv ("GCC_COLORS"))
194 return;
195 value = DIAGNOSTICS_COLOR_AUTO;
196 }
197 else
198 value = DIAGNOSTICS_COLOR_DEFAULT;
199 }
200 pp_show_color (context->printer)
201 = colorize_init ((diagnostic_color_rule_t) value);
202 }
203
204 /* Do any cleaning up required after the last diagnostic is emitted. */
205
206 void
207 diagnostic_finish (diagnostic_context *context)
208 {
209 /* Some of the errors may actually have been warnings. */
210 if (diagnostic_kind_count (context, DK_WERROR))
211 {
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);
223 }
224
225 diagnostic_file_cache_fini ();
226
227 XDELETEVEC (context->classify_diagnostic);
228 context->classify_diagnostic = NULL;
229
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;
235 }
236
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)
243 {
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;
252 }
253
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)
260 {
261 gcc_assert (richloc);
262 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, richloc, kind);
263 }
264
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
270 };
271
272 /* Get a color name for diagnostics of type KIND
273 Result could be NULL. */
274
275 const char *
276 diagnostic_get_color_for_kind (diagnostic_t kind)
277 {
278 return diagnostic_kind_color[kind];
279 }
280
281 /* Return a malloc'd string describing a location e.g. "foo.c:42:10".
282 The caller is responsible for freeing the memory. */
283
284 static char *
285 diagnostic_get_location_text (diagnostic_context *context,
286 expanded_location s)
287 {
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));
291
292 if (s.file == NULL)
293 return build_message_string ("%s%s:%s", locus_cs, progname, locus_ce);
294
295 if (!strcmp (s.file, N_("<built-in>")))
296 return build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce);
297
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);
304 }
305
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)
312 {
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"
318 };
319 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
320
321 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
322 const char *text_cs = "", *text_ce = "";
323 pretty_printer *pp = context->printer;
324
325 if (diagnostic_kind_color[diagnostic->kind])
326 {
327 text_cs = colorize_start (pp_show_color (pp),
328 diagnostic_kind_color[diagnostic->kind]);
329 text_ce = colorize_stop (pp_show_color (pp));
330 }
331
332 expanded_location s = diagnostic_expand_location (diagnostic);
333 char *location_text = diagnostic_get_location_text (context, s);
334
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;
339 }
340
341 /* Functions at which to stop the backtrace print. It's not
342 particularly helpful to print the callers of these functions. */
343
344 static const char * const bt_stop[] =
345 {
346 "main",
347 "toplev::main",
348 "execute_one_pass",
349 "compile_file",
350 };
351
352 /* A callback function passed to the backtrace_full function. */
353
354 static int
355 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
356 const char *function)
357 {
358 int *pcount = (int *) data;
359
360 /* If we don't have any useful information, don't print
361 anything. */
362 if (filename == NULL && function == NULL)
363 return 0;
364
365 /* Skip functions in diagnostic.c. */
366 if (*pcount == 0
367 && filename != NULL
368 && strcmp (lbasename (filename), "diagnostic.c") == 0)
369 return 0;
370
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)
374 {
375 /* Returning a non-zero value stops the backtrace. */
376 return 1;
377 }
378 ++*pcount;
379
380 char *alc = NULL;
381 if (function != NULL)
382 {
383 char *str = cplus_demangle_v3 (function,
384 (DMGL_VERBOSE | DMGL_ANSI
385 | DMGL_GNU_V3 | DMGL_PARAMS));
386 if (str != NULL)
387 {
388 alc = str;
389 function = str;
390 }
391
392 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
393 {
394 size_t len = strlen (bt_stop[i]);
395 if (strncmp (function, bt_stop[i], len) == 0
396 && (function[len] == '\0' || function[len] == '('))
397 {
398 if (alc != NULL)
399 free (alc);
400 /* Returning a non-zero value stops the backtrace. */
401 return 1;
402 }
403 }
404 }
405
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);
411
412 if (alc != NULL)
413 free (alc);
414
415 return 0;
416 }
417
418 /* A callback function passed to the backtrace_full function. This is
419 called if backtrace_full has an error. */
420
421 static void
422 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
423 {
424 if (errnum < 0)
425 {
426 /* This means that no debug info was available. Just quietly
427 skip printing backtrace info. */
428 return;
429 }
430 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
431 errnum == 0 ? "" : xstrerror (errnum));
432 }
433
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)
439 {
440 switch (diag_kind)
441 {
442 case DK_DEBUG:
443 case DK_NOTE:
444 case DK_ANACHRONISM:
445 case DK_WARNING:
446 break;
447
448 case DK_ERROR:
449 case DK_SORRY:
450 if (context->abort_on_error)
451 real_abort ();
452 if (context->fatal_errors)
453 {
454 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
455 diagnostic_finish (context);
456 exit (FATAL_EXIT_CODE);
457 }
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))
463 {
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);
469 }
470 break;
471
472 case DK_ICE:
473 case DK_ICE_NOBT:
474 {
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);
482
483 if (context->abort_on_error)
484 real_abort ();
485
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);
493
494 exit (ICE_EXIT_CODE);
495 }
496
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);
503
504 default:
505 gcc_unreachable ();
506 }
507 }
508
509 void
510 diagnostic_report_current_module (diagnostic_context *context, location_t where)
511 {
512 const line_map_ordinary *map = NULL;
513
514 if (pp_needs_newline (context->printer))
515 {
516 pp_newline (context->printer);
517 pp_needs_newline (context->printer) = false;
518 }
519
520 if (where <= BUILTINS_LOCATION)
521 return;
522
523 linemap_resolve_location (line_table, where,
524 LRK_MACRO_DEFINITION_LOCATION,
525 &map);
526
527 if (map && diagnostic_last_module_changed (context, map))
528 {
529 diagnostic_set_last_module (context, map);
530 if (! MAIN_FILE_P (map))
531 {
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))
543 {
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));
548 }
549 pp_verbatim (context->printer, ":");
550 pp_newline (context->printer);
551 }
552 }
553 }
554
555 void
556 default_diagnostic_starter (diagnostic_context *context,
557 diagnostic_info *diagnostic)
558 {
559 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
560 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
561 diagnostic));
562 }
563
564 void
565 default_diagnostic_start_span_fn (diagnostic_context *context,
566 expanded_location exploc)
567 {
568 pp_set_prefix (context->printer,
569 diagnostic_get_location_text (context, exploc));
570 pp_string (context->printer, "");
571 pp_newline (context->printer);
572 }
573
574 void
575 default_diagnostic_finalizer (diagnostic_context *context,
576 diagnostic_info *diagnostic)
577 {
578 diagnostic_show_locus (context, diagnostic);
579 pp_destroy_prefix (context->printer);
580 pp_flush (context->printer);
581 }
582
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)
592 {
593 diagnostic_t old_kind;
594
595 if (option_index < 0
596 || option_index >= context->n_opts
597 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
598 return DK_UNSPECIFIED;
599
600 old_kind = context->classify_diagnostic[option_index];
601
602 /* Handle pragmas separately, since we need to keep track of *where*
603 the pragmas were. */
604 if (where != UNKNOWN_LOCATION)
605 {
606 int i;
607
608 /* Record the command-line status, so we can reset it back on DK_POP. */
609 if (old_kind == DK_UNSPECIFIED)
610 {
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;
616 }
617
618 for (i = context->n_classification_history - 1; i >= 0; i --)
619 if (context->classification_history[i].option == option_index)
620 {
621 old_kind = context->classification_history[i].kind;
622 break;
623 }
624
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 ++;
633 }
634 else
635 context->classify_diagnostic[option_index] = new_kind;
636
637 return old_kind;
638 }
639
640 /* Save all diagnostic classifications in a stack. */
641 void
642 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
643 {
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;
646 }
647
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)
652 {
653 int jump_to;
654 int i;
655
656 if (context->n_push)
657 jump_to = context->push_list [-- context->n_push];
658 else
659 jump_to = 0;
660
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 ++;
669 }
670
671 /* Helper function for print_parseable_fixits. Print TEXT to PP, obeying the
672 escaping rules for -fdiagnostics-parseable-fixits. */
673
674 static void
675 print_escaped_string (pretty_printer *pp, const char *text)
676 {
677 gcc_assert (pp);
678 gcc_assert (text);
679
680 pp_character (pp, '"');
681 for (const char *ch = text; *ch; ch++)
682 {
683 switch (*ch)
684 {
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. */
706 {
707 unsigned char c = (*ch & 0xff);
708 pp_printf (pp, "\\%o%o%o", (c / 64), (c / 8) & 007, c & 007);
709 }
710 break;
711 }
712 }
713 pp_character (pp, '"');
714 }
715
716 /* Implementation of -fdiagnostics-parseable-fixits. Print a
717 machine-parseable version of all fixits in RICHLOC to PP. */
718
719 static void
720 print_parseable_fixits (pretty_printer *pp, rich_location *richloc)
721 {
722 gcc_assert (pp);
723 gcc_assert (richloc);
724
725 for (unsigned i = 0; i < richloc->get_num_fixit_hints (); i++)
726 {
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;
733
734 /* For compatibility with clang, print as a half-open range. */
735 if (hint->maybe_get_end_loc (&end_loc))
736 {
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);
741 }
742 else
743 {
744 pp_printf (pp, ":{%i:%i-%i:%i}:",
745 start_exploc.line, start_exploc.column,
746 start_exploc.line, start_exploc.column);
747 }
748 switch (hint->get_kind ())
749 {
750 case fixit_hint::INSERT:
751 {
752 const fixit_insert *insert
753 = static_cast <const fixit_insert *> (hint);
754 print_escaped_string (pp, insert->get_string ());
755 }
756 break;
757
758 case fixit_hint::REMOVE:
759 print_escaped_string (pp, "");
760 break;
761
762 case fixit_hint::REPLACE:
763 {
764 const fixit_replace *replace
765 = static_cast <const fixit_replace *> (hint);
766 print_escaped_string (pp, replace->get_string ());
767 }
768 break;
769
770 default:
771 gcc_unreachable ();
772 }
773 pp_newline (pp);
774 }
775 }
776
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. */
783
784 bool
785 diagnostic_report_diagnostic (diagnostic_context *context,
786 diagnostic_info *diagnostic)
787 {
788 location_t location = diagnostic_location (diagnostic);
789 diagnostic_t orig_diag_kind = diagnostic->kind;
790 const char *saved_format_spec;
791
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;
797
798 if (diagnostic->kind == DK_PEDWARN)
799 {
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;
803 }
804
805 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
806 return false;
807
808 if (context->lock > 0)
809 {
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);
818 }
819
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)
826 {
827 diagnostic->kind = DK_ERROR;
828 }
829
830 if (diagnostic->option_index
831 && diagnostic->option_index != permissive_error_option (context))
832 {
833 diagnostic_t diag_class = DK_UNSPECIFIED;
834
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;
840
841 /* This tests for #pragma diagnostic changes. */
842 if (context->n_classification_history > 0)
843 {
844 /* FIXME: Stupid search. Optimize later. */
845 for (int i = context->n_classification_history - 1; i >= 0; i --)
846 {
847 if (linemap_location_before_p
848 (line_table,
849 context->classification_history[i].location,
850 location))
851 {
852 if (context->classification_history[i].kind == (int) DK_POP)
853 {
854 i = context->classification_history[i].option;
855 continue;
856 }
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)
860 {
861 diag_class = context->classification_history[i].kind;
862 if (diag_class != DK_UNSPECIFIED)
863 diagnostic->kind = diag_class;
864 break;
865 }
866 }
867 }
868 }
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)
873 {
874 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
875 }
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;
880 }
881
882 context->lock++;
883
884 if (diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
885 {
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)
893 {
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);
899 }
900 if (context->internal_error)
901 (*context->internal_error) (context,
902 diagnostic->message.format_spec,
903 diagnostic->message.args_ptr);
904 }
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);
909
910 saved_format_spec = diagnostic->message.format_spec;
911 if (context->show_option_requested)
912 {
913 char *option_text;
914
915 option_text = context->option_name (context, diagnostic->option_index,
916 orig_diag_kind, diagnostic->kind);
917
918 if (option_text)
919 {
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);
930 }
931 }
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)
939 {
940 print_parseable_fixits (context->printer, diagnostic->richloc);
941 pp_flush (context->printer);
942 }
943 diagnostic_action_after_output (context, diagnostic->kind);
944 diagnostic->message.format_spec = saved_format_spec;
945 diagnostic->x_data = NULL;
946
947 context->lock--;
948
949 return true;
950 }
951
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'. */
956
957 const char *
958 trim_filename (const char *name)
959 {
960 static const char this_file[] = __FILE__;
961 const char *p = name, *q = this_file;
962
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;
967
968 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
969 q += 3;
970
971 /* Now skip any parts the two filenames have in common. */
972 while (*p == *q && *p != 0 && *q != 0)
973 p++, q++;
974
975 /* Now go backwards until the previous directory separator. */
976 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
977 p--;
978
979 return p;
980 }
981 \f
982 /* Standard error reporting routines in increasing order of severity.
983 All of these take arguments like printf. */
984
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, ...)
989 {
990 text_info text;
991 va_list ap;
992
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);
1001 }
1002
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, ...)
1008 {
1009 diagnostic_info diagnostic;
1010 va_list ap;
1011 const char *saved_prefix;
1012 rich_location richloc (line_table, location);
1013
1014 va_start (ap, gmsgid);
1015 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_NOTE);
1016 if (context->inhibit_notes_p)
1017 {
1018 va_end (ap);
1019 return;
1020 }
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);
1030 }
1031
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)
1040 {
1041 diagnostic_info diagnostic;
1042 if (kind == DK_PERMERROR)
1043 {
1044 diagnostic_set_info (&diagnostic, gmsgid, ap, richloc,
1045 permissive_error_kind (global_dc));
1046 diagnostic.option_index = permissive_error_option (global_dc);
1047 }
1048 else
1049 {
1050 diagnostic_set_info (&diagnostic, gmsgid, ap, richloc, kind);
1051 if (kind == DK_WARNING || kind == DK_PEDWARN)
1052 diagnostic.option_index = opt;
1053 }
1054 return report_diagnostic (&diagnostic);
1055 }
1056
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)
1064 {
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);
1073 }
1074
1075 bool
1076 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
1077 const char *gmsgid, ...)
1078 {
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;
1085 }
1086
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, ...)
1091 {
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);
1097 }
1098
1099 /* Same as "inform", but at RICHLOC. */
1100 void
1101 inform_at_rich_loc (rich_location *richloc, const char *gmsgid, ...)
1102 {
1103 va_list ap;
1104 va_start (ap, gmsgid);
1105 diagnostic_impl (richloc, -1, gmsgid, &ap, DK_NOTE);
1106 va_end (ap);
1107 }
1108
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, ...)
1114 {
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);
1120 }
1121
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, ...)
1127 {
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;
1134 }
1135
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. */
1139
1140 bool
1141 warning_at (location_t location, int opt, const char *gmsgid, ...)
1142 {
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;
1149 }
1150
1151 /* Same as warning at, but using RICHLOC. */
1152
1153 bool
1154 warning_at_rich_loc (rich_location *richloc, int opt, const char *gmsgid, ...)
1155 {
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;
1161 }
1162
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. */
1166
1167 bool
1168 warning_n (location_t location, int opt, int n, const char *singular_gmsgid,
1169 const char *plural_gmsgid, ...)
1170 {
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;
1178 }
1179
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.
1184
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,...)".
1190
1191 Returns true if the warning was printed, false if it was inhibited. */
1192
1193 bool
1194 pedwarn (location_t location, int opt, const char *gmsgid, ...)
1195 {
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;
1202 }
1203
1204 /* A "permissive" error at LOCATION: issues an error unless
1205 -fpermissive was given on the command line, in which case it issues
1206 a warning. Use this for things that really should be errors but we
1207 want to support legacy code.
1208
1209 Returns true if the warning was printed, false if it was inhibited. */
1210
1211 bool
1212 permerror (location_t location, const char *gmsgid, ...)
1213 {
1214 va_list ap;
1215 va_start (ap, gmsgid);
1216 rich_location richloc (line_table, location);
1217 bool ret = diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_PERMERROR);
1218 va_end (ap);
1219 return ret;
1220 }
1221
1222 /* Same as "permerror", but at RICHLOC. */
1223
1224 bool
1225 permerror_at_rich_loc (rich_location *richloc, const char *gmsgid, ...)
1226 {
1227 va_list ap;
1228 va_start (ap, gmsgid);
1229 bool ret = diagnostic_impl (richloc, -1, gmsgid, &ap, DK_PERMERROR);
1230 va_end (ap);
1231 return ret;
1232 }
1233
1234 /* A hard error: the code is definitely ill-formed, and an object file
1235 will not be produced. */
1236 void
1237 error (const char *gmsgid, ...)
1238 {
1239 va_list ap;
1240 va_start (ap, gmsgid);
1241 rich_location richloc (line_table, input_location);
1242 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ERROR);
1243 va_end (ap);
1244 }
1245
1246 /* A hard error: the code is definitely ill-formed, and an object file
1247 will not be produced. */
1248 void
1249 error_n (location_t location, int n, const char *singular_gmsgid,
1250 const char *plural_gmsgid, ...)
1251 {
1252 va_list ap;
1253 va_start (ap, plural_gmsgid);
1254 diagnostic_n_impl (location, -1, n, singular_gmsgid, plural_gmsgid,
1255 &ap, DK_ERROR);
1256 va_end (ap);
1257 }
1258
1259 /* Same as above, but use location LOC instead of input_location. */
1260 void
1261 error_at (location_t loc, const char *gmsgid, ...)
1262 {
1263 va_list ap;
1264 va_start (ap, gmsgid);
1265 rich_location richloc (line_table, loc);
1266 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ERROR);
1267 va_end (ap);
1268 }
1269
1270 /* Same as above, but use RICH_LOC. */
1271
1272 void
1273 error_at_rich_loc (rich_location *richloc, const char *gmsgid, ...)
1274 {
1275 va_list ap;
1276 va_start (ap, gmsgid);
1277 diagnostic_impl (richloc, -1, gmsgid, &ap, DK_ERROR);
1278 va_end (ap);
1279 }
1280
1281 /* "Sorry, not implemented." Use for a language feature which is
1282 required by the relevant specification but not implemented by GCC.
1283 An object file will not be produced. */
1284 void
1285 sorry (const char *gmsgid, ...)
1286 {
1287 va_list ap;
1288 va_start (ap, gmsgid);
1289 rich_location richloc (line_table, input_location);
1290 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_SORRY);
1291 va_end (ap);
1292 }
1293
1294 /* Return true if an error or a "sorry" has been seen. Various
1295 processing is disabled after errors. */
1296 bool
1297 seen_error (void)
1298 {
1299 return errorcount || sorrycount;
1300 }
1301
1302 /* An error which is severe enough that we make no attempt to
1303 continue. Do not use this for internal consistency checks; that's
1304 internal_error. Use of this function should be rare. */
1305 void
1306 fatal_error (location_t loc, const char *gmsgid, ...)
1307 {
1308 va_list ap;
1309 va_start (ap, gmsgid);
1310 rich_location richloc (line_table, loc);
1311 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_FATAL);
1312 va_end (ap);
1313
1314 gcc_unreachable ();
1315 }
1316
1317 /* An internal consistency check has failed. We make no attempt to
1318 continue. Note that unless there is debugging value to be had from
1319 a more specific message, or some other good reason, you should use
1320 abort () instead of calling this function directly. */
1321 void
1322 internal_error (const char *gmsgid, ...)
1323 {
1324 va_list ap;
1325 va_start (ap, gmsgid);
1326 rich_location richloc (line_table, input_location);
1327 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ICE);
1328 va_end (ap);
1329
1330 gcc_unreachable ();
1331 }
1332
1333 /* Like internal_error, but no backtrace will be printed. Used when
1334 the internal error does not happen at the current location, but happened
1335 somewhere else. */
1336 void
1337 internal_error_no_backtrace (const char *gmsgid, ...)
1338 {
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_NOBT);
1343 va_end (ap);
1344
1345 gcc_unreachable ();
1346 }
1347 \f
1348 /* Special case error functions. Most are implemented in terms of the
1349 above, or should be. */
1350
1351 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1352 runs its second argument through gettext. */
1353 void
1354 fnotice (FILE *file, const char *cmsgid, ...)
1355 {
1356 va_list ap;
1357
1358 va_start (ap, cmsgid);
1359 vfprintf (file, _(cmsgid), ap);
1360 va_end (ap);
1361 }
1362
1363 /* Inform the user that an error occurred while trying to report some
1364 other error. This indicates catastrophic internal inconsistencies,
1365 so give up now. But do try to flush out the previous error.
1366 This mustn't use internal_error, that will cause infinite recursion. */
1367
1368 static void
1369 error_recursion (diagnostic_context *context)
1370 {
1371 if (context->lock < 3)
1372 pp_newline_and_flush (context->printer);
1373
1374 fnotice (stderr,
1375 "Internal compiler error: Error reporting routines re-entered.\n");
1376
1377 /* Call diagnostic_action_after_output to get the "please submit a bug
1378 report" message. */
1379 diagnostic_action_after_output (context, DK_ICE);
1380
1381 /* Do not use gcc_unreachable here; that goes through internal_error
1382 and therefore would cause infinite recursion. */
1383 real_abort ();
1384 }
1385
1386 /* Report an internal compiler error in a friendly manner. This is
1387 the function that gets called upon use of abort() in the source
1388 code generally, thanks to a special macro. */
1389
1390 void
1391 fancy_abort (const char *file, int line, const char *function)
1392 {
1393 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1394 }
1395
1396 /* Really call the system 'abort'. This has to go right at the end of
1397 this file, so that there are no functions after it that call abort
1398 and get the system abort instead of our macro. */
1399 #undef abort
1400 static void
1401 real_abort (void)
1402 {
1403 abort ();
1404 }
1405
1406 #if CHECKING_P
1407
1408 namespace selftest {
1409
1410 /* Helper function for test_print_escaped_string. */
1411
1412 static void
1413 assert_print_escaped_string (const location &loc, const char *expected_output,
1414 const char *input)
1415 {
1416 pretty_printer pp;
1417 print_escaped_string (&pp, input);
1418 ASSERT_STREQ_AT (loc, expected_output, pp_formatted_text (&pp));
1419 }
1420
1421 #define ASSERT_PRINT_ESCAPED_STRING_STREQ(EXPECTED_OUTPUT, INPUT) \
1422 assert_print_escaped_string (SELFTEST_LOCATION, EXPECTED_OUTPUT, INPUT)
1423
1424 /* Tests of print_escaped_string. */
1425
1426 static void
1427 test_print_escaped_string ()
1428 {
1429 /* Empty string. */
1430 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"\"", "");
1431
1432 /* Non-empty string. */
1433 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"hello world\"", "hello world");
1434
1435 /* Various things that need to be escaped: */
1436 /* Backslash. */
1437 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\\after\"",
1438 "before\\after");
1439 /* Tab. */
1440 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\tafter\"",
1441 "before\tafter");
1442 /* Newline. */
1443 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\nafter\"",
1444 "before\nafter");
1445 /* Double quote. */
1446 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\"after\"",
1447 "before\"after");
1448
1449 /* Non-printable characters: BEL: '\a': 0x07 */
1450 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\007after\"",
1451 "before\aafter");
1452 /* Non-printable characters: vertical tab: '\v': 0x0b */
1453 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\013after\"",
1454 "before\vafter");
1455 }
1456
1457 /* Tests of print_parseable_fixits. */
1458
1459 /* Verify that print_parseable_fixits emits the empty string if there
1460 are no fixits. */
1461
1462 static void
1463 test_print_parseable_fixits_none ()
1464 {
1465 pretty_printer pp;
1466 rich_location richloc (line_table, UNKNOWN_LOCATION);
1467
1468 print_parseable_fixits (&pp, &richloc);
1469 ASSERT_STREQ ("", pp_formatted_text (&pp));
1470 }
1471
1472 /* Verify that print_parseable_fixits does the right thing if there
1473 is an insertion fixit hint. */
1474
1475 static void
1476 test_print_parseable_fixits_insert ()
1477 {
1478 pretty_printer pp;
1479 rich_location richloc (line_table, UNKNOWN_LOCATION);
1480
1481 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
1482 linemap_line_start (line_table, 5, 100);
1483 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1484 location_t where = linemap_position_for_column (line_table, 10);
1485 richloc.add_fixit_insert (where, "added content");
1486
1487 print_parseable_fixits (&pp, &richloc);
1488 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:10}:\"added content\"\n",
1489 pp_formatted_text (&pp));
1490 }
1491
1492 /* Verify that print_parseable_fixits does the right thing if there
1493 is an removal fixit hint. */
1494
1495 static void
1496 test_print_parseable_fixits_remove ()
1497 {
1498 pretty_printer pp;
1499 rich_location richloc (line_table, UNKNOWN_LOCATION);
1500
1501 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
1502 linemap_line_start (line_table, 5, 100);
1503 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1504 source_range where;
1505 where.m_start = linemap_position_for_column (line_table, 10);
1506 where.m_finish = linemap_position_for_column (line_table, 20);
1507 richloc.add_fixit_remove (where);
1508
1509 print_parseable_fixits (&pp, &richloc);
1510 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"\"\n",
1511 pp_formatted_text (&pp));
1512 }
1513
1514 /* Verify that print_parseable_fixits does the right thing if there
1515 is an replacement fixit hint. */
1516
1517 static void
1518 test_print_parseable_fixits_replace ()
1519 {
1520 pretty_printer pp;
1521 rich_location richloc (line_table, UNKNOWN_LOCATION);
1522
1523 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
1524 linemap_line_start (line_table, 5, 100);
1525 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1526 source_range where;
1527 where.m_start = linemap_position_for_column (line_table, 10);
1528 where.m_finish = linemap_position_for_column (line_table, 20);
1529 richloc.add_fixit_replace (where, "replacement");
1530
1531 print_parseable_fixits (&pp, &richloc);
1532 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"replacement\"\n",
1533 pp_formatted_text (&pp));
1534 }
1535
1536 /* Run all of the selftests within this file. */
1537
1538 void
1539 diagnostic_c_tests ()
1540 {
1541 test_print_escaped_string ();
1542 test_print_parseable_fixits_none ();
1543 test_print_parseable_fixits_insert ();
1544 test_print_parseable_fixits_remove ();
1545 test_print_parseable_fixits_replace ();
1546 }
1547
1548 } // namespace selftest
1549
1550 #endif /* #if CHECKING_P */