PR rtl-optimization/82913
[official-gcc.git] / gcc / diagnostic.c
blob813bca6f65d3ff0611f4a961056e85f3bdb92a7a
1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999-2017 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 "edit-context.h"
35 #include "selftest.h"
36 #include "selftest-diagnostic.h"
38 #ifdef HAVE_TERMIOS_H
39 # include <termios.h>
40 #endif
42 #ifdef GWINSZ_IN_SYS_IOCTL
43 # include <sys/ioctl.h>
44 #endif
46 #define pedantic_warning_kind(DC) \
47 ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
48 #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR)
49 #define permissive_error_option(DC) ((DC)->opt_permissive)
51 /* Prototypes. */
52 static bool diagnostic_impl (rich_location *, int, const char *,
53 va_list *, diagnostic_t) ATTRIBUTE_GCC_DIAG(3,0);
54 static bool diagnostic_n_impl (rich_location *, int, int, const char *,
55 const char *, va_list *,
56 diagnostic_t) ATTRIBUTE_GCC_DIAG(5,0);
58 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
59 static void real_abort (void) ATTRIBUTE_NORETURN;
61 /* Name of program invoked, sans directories. */
63 const char *progname;
65 /* A diagnostic_context surrogate for stderr. */
66 static diagnostic_context global_diagnostic_context;
67 diagnostic_context *global_dc = &global_diagnostic_context;
69 /* Return a malloc'd string containing MSG formatted a la printf. The
70 caller is responsible for freeing the memory. */
71 char *
72 build_message_string (const char *msg, ...)
74 char *str;
75 va_list ap;
77 va_start (ap, msg);
78 str = xvasprintf (msg, ap);
79 va_end (ap);
81 return str;
84 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
85 char *
86 file_name_as_prefix (diagnostic_context *context, const char *f)
88 const char *locus_cs
89 = colorize_start (pp_show_color (context->printer), "locus");
90 const char *locus_ce = colorize_stop (pp_show_color (context->printer));
91 return build_message_string ("%s%s:%s ", locus_cs, f, locus_ce);
96 /* Return the value of the getenv("COLUMNS") as an integer. If the
97 value is not set to a positive integer, use ioctl to get the
98 terminal width. If it fails, return INT_MAX. */
99 int
100 get_terminal_width (void)
102 const char * s = getenv ("COLUMNS");
103 if (s != NULL) {
104 int n = atoi (s);
105 if (n > 0)
106 return n;
109 #ifdef TIOCGWINSZ
110 struct winsize w;
111 w.ws_col = 0;
112 if (ioctl (0, TIOCGWINSZ, &w) == 0 && w.ws_col > 0)
113 return w.ws_col;
114 #endif
116 return INT_MAX;
119 /* Set caret_max_width to value. */
120 void
121 diagnostic_set_caret_max_width (diagnostic_context *context, int value)
123 /* One minus to account for the leading empty space. */
124 value = value ? value - 1
125 : (isatty (fileno (pp_buffer (context->printer)->stream))
126 ? get_terminal_width () - 1: INT_MAX);
128 if (value <= 0)
129 value = INT_MAX;
131 context->caret_max_width = value;
134 /* Initialize the diagnostic message outputting machinery. */
135 void
136 diagnostic_initialize (diagnostic_context *context, int n_opts)
138 int i;
140 /* Allocate a basic pretty-printer. Clients will replace this a
141 much more elaborated pretty-printer if they wish. */
142 context->printer = XNEW (pretty_printer);
143 new (context->printer) pretty_printer ();
145 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
146 context->warning_as_error_requested = false;
147 context->n_opts = n_opts;
148 context->classify_diagnostic = XNEWVEC (diagnostic_t, n_opts);
149 for (i = 0; i < n_opts; i++)
150 context->classify_diagnostic[i] = DK_UNSPECIFIED;
151 context->show_caret = false;
152 diagnostic_set_caret_max_width (context, pp_line_cutoff (context->printer));
153 for (i = 0; i < rich_location::STATICALLY_ALLOCATED_RANGES; i++)
154 context->caret_chars[i] = '^';
155 context->show_option_requested = false;
156 context->abort_on_error = false;
157 context->show_column = false;
158 context->pedantic_errors = false;
159 context->permissive = false;
160 context->opt_permissive = 0;
161 context->fatal_errors = false;
162 context->dc_inhibit_warnings = false;
163 context->dc_warn_system_headers = false;
164 context->max_errors = 0;
165 context->internal_error = NULL;
166 diagnostic_starter (context) = default_diagnostic_starter;
167 context->start_span = default_diagnostic_start_span_fn;
168 diagnostic_finalizer (context) = default_diagnostic_finalizer;
169 context->option_enabled = NULL;
170 context->option_state = NULL;
171 context->option_name = NULL;
172 context->last_location = UNKNOWN_LOCATION;
173 context->last_module = 0;
174 context->x_data = NULL;
175 context->lock = 0;
176 context->inhibit_notes_p = false;
177 context->colorize_source_p = false;
178 context->show_ruler_p = false;
179 context->parseable_fixits_p = false;
180 context->edit_context_ptr = NULL;
183 /* Maybe initialize the color support. We require clients to do this
184 explicitly, since most clients don't want color. When called
185 without a VALUE, it initializes with DIAGNOSTICS_COLOR_DEFAULT. */
187 void
188 diagnostic_color_init (diagnostic_context *context, int value /*= -1 */)
190 /* value == -1 is the default value. */
191 if (value < 0)
193 /* If DIAGNOSTICS_COLOR_DEFAULT is -1, default to
194 -fdiagnostics-color=auto if GCC_COLORS is in the environment,
195 otherwise default to -fdiagnostics-color=never, for other
196 values default to that
197 -fdiagnostics-color={never,auto,always}. */
198 if (DIAGNOSTICS_COLOR_DEFAULT == -1)
200 if (!getenv ("GCC_COLORS"))
201 return;
202 value = DIAGNOSTICS_COLOR_AUTO;
204 else
205 value = DIAGNOSTICS_COLOR_DEFAULT;
207 pp_show_color (context->printer)
208 = colorize_init ((diagnostic_color_rule_t) value);
211 /* Do any cleaning up required after the last diagnostic is emitted. */
213 void
214 diagnostic_finish (diagnostic_context *context)
216 /* Some of the errors may actually have been warnings. */
217 if (diagnostic_kind_count (context, DK_WERROR))
219 /* -Werror was given. */
220 if (context->warning_as_error_requested)
221 pp_verbatim (context->printer,
222 _("%s: all warnings being treated as errors"),
223 progname);
224 /* At least one -Werror= was given. */
225 else
226 pp_verbatim (context->printer,
227 _("%s: some warnings being treated as errors"),
228 progname);
229 pp_newline_and_flush (context->printer);
232 diagnostic_file_cache_fini ();
234 XDELETEVEC (context->classify_diagnostic);
235 context->classify_diagnostic = NULL;
237 /* diagnostic_initialize allocates context->printer using XNEW
238 and placement-new. */
239 context->printer->~pretty_printer ();
240 XDELETE (context->printer);
241 context->printer = NULL;
243 if (context->edit_context_ptr)
245 delete context->edit_context_ptr;
246 context->edit_context_ptr = NULL;
250 /* Initialize DIAGNOSTIC, where the message MSG has already been
251 translated. */
252 void
253 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
254 va_list *args, rich_location *richloc,
255 diagnostic_t kind)
257 gcc_assert (richloc);
258 diagnostic->message.err_no = errno;
259 diagnostic->message.args_ptr = args;
260 diagnostic->message.format_spec = msg;
261 diagnostic->message.m_richloc = richloc;
262 diagnostic->richloc = richloc;
263 diagnostic->kind = kind;
264 diagnostic->option_index = 0;
267 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
268 translated. */
269 void
270 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
271 va_list *args, rich_location *richloc,
272 diagnostic_t kind)
274 gcc_assert (richloc);
275 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, richloc, kind);
278 static const char *const diagnostic_kind_color[] = {
279 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
280 #include "diagnostic.def"
281 #undef DEFINE_DIAGNOSTIC_KIND
282 NULL
285 /* Get a color name for diagnostics of type KIND
286 Result could be NULL. */
288 const char *
289 diagnostic_get_color_for_kind (diagnostic_t kind)
291 return diagnostic_kind_color[kind];
294 /* Return a malloc'd string describing a location e.g. "foo.c:42:10".
295 The caller is responsible for freeing the memory. */
297 static char *
298 diagnostic_get_location_text (diagnostic_context *context,
299 expanded_location s)
301 pretty_printer *pp = context->printer;
302 const char *locus_cs = colorize_start (pp_show_color (pp), "locus");
303 const char *locus_ce = colorize_stop (pp_show_color (pp));
305 if (s.file == NULL)
306 return build_message_string ("%s%s:%s", locus_cs, progname, locus_ce);
308 if (!strcmp (s.file, N_("<built-in>")))
309 return build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce);
311 if (context->show_column)
312 return build_message_string ("%s%s:%d:%d:%s", locus_cs, s.file, s.line,
313 s.column, locus_ce);
314 else
315 return build_message_string ("%s%s:%d:%s", locus_cs, s.file, s.line,
316 locus_ce);
319 /* Return a malloc'd string describing a location and the severity of the
320 diagnostic, e.g. "foo.c:42:10: error: ". The caller is responsible for
321 freeing the memory. */
322 char *
323 diagnostic_build_prefix (diagnostic_context *context,
324 const diagnostic_info *diagnostic)
326 static const char *const diagnostic_kind_text[] = {
327 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
328 #include "diagnostic.def"
329 #undef DEFINE_DIAGNOSTIC_KIND
330 "must-not-happen"
332 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
334 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
335 const char *text_cs = "", *text_ce = "";
336 pretty_printer *pp = context->printer;
338 if (diagnostic_kind_color[diagnostic->kind])
340 text_cs = colorize_start (pp_show_color (pp),
341 diagnostic_kind_color[diagnostic->kind]);
342 text_ce = colorize_stop (pp_show_color (pp));
345 expanded_location s = diagnostic_expand_location (diagnostic);
346 char *location_text = diagnostic_get_location_text (context, s);
348 char *result = build_message_string ("%s %s%s%s", location_text,
349 text_cs, text, text_ce);
350 free (location_text);
351 return result;
354 /* Functions at which to stop the backtrace print. It's not
355 particularly helpful to print the callers of these functions. */
357 static const char * const bt_stop[] =
359 "main",
360 "toplev::main",
361 "execute_one_pass",
362 "compile_file",
365 /* A callback function passed to the backtrace_full function. */
367 static int
368 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
369 const char *function)
371 int *pcount = (int *) data;
373 /* If we don't have any useful information, don't print
374 anything. */
375 if (filename == NULL && function == NULL)
376 return 0;
378 /* Skip functions in diagnostic.c. */
379 if (*pcount == 0
380 && filename != NULL
381 && strcmp (lbasename (filename), "diagnostic.c") == 0)
382 return 0;
384 /* Print up to 20 functions. We could make this a --param, but
385 since this is only for debugging just use a constant for now. */
386 if (*pcount >= 20)
388 /* Returning a non-zero value stops the backtrace. */
389 return 1;
391 ++*pcount;
393 char *alc = NULL;
394 if (function != NULL)
396 char *str = cplus_demangle_v3 (function,
397 (DMGL_VERBOSE | DMGL_ANSI
398 | DMGL_GNU_V3 | DMGL_PARAMS));
399 if (str != NULL)
401 alc = str;
402 function = str;
405 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
407 size_t len = strlen (bt_stop[i]);
408 if (strncmp (function, bt_stop[i], len) == 0
409 && (function[len] == '\0' || function[len] == '('))
411 if (alc != NULL)
412 free (alc);
413 /* Returning a non-zero value stops the backtrace. */
414 return 1;
419 fprintf (stderr, "0x%lx %s\n\t%s:%d\n",
420 (unsigned long) pc,
421 function == NULL ? "???" : function,
422 filename == NULL ? "???" : filename,
423 lineno);
425 if (alc != NULL)
426 free (alc);
428 return 0;
431 /* A callback function passed to the backtrace_full function. This is
432 called if backtrace_full has an error. */
434 static void
435 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
437 if (errnum < 0)
439 /* This means that no debug info was available. Just quietly
440 skip printing backtrace info. */
441 return;
443 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
444 errnum == 0 ? "" : xstrerror (errnum));
447 /* Check if we've met the maximum error limit, and if so fatally exit
448 with a message. CONTEXT is the context to check, and FLUSH
449 indicates whether a diagnostic_finish call is needed. */
451 void
452 diagnostic_check_max_errors (diagnostic_context *context, bool flush)
454 if (!context->max_errors)
455 return;
457 int count = (diagnostic_kind_count (context, DK_ERROR)
458 + diagnostic_kind_count (context, DK_SORRY)
459 + diagnostic_kind_count (context, DK_WERROR));
461 if (count >= context->max_errors)
463 fnotice (stderr,
464 "compilation terminated due to -fmax-errors=%u.\n",
465 context->max_errors);
466 if (flush)
467 diagnostic_finish (context);
468 exit (FATAL_EXIT_CODE);
472 /* Take any action which is expected to happen after the diagnostic
473 is written out. This function does not always return. */
474 void
475 diagnostic_action_after_output (diagnostic_context *context,
476 diagnostic_t diag_kind)
478 switch (diag_kind)
480 case DK_DEBUG:
481 case DK_NOTE:
482 case DK_ANACHRONISM:
483 case DK_WARNING:
484 break;
486 case DK_ERROR:
487 case DK_SORRY:
488 if (context->abort_on_error)
489 real_abort ();
490 if (context->fatal_errors)
492 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
493 diagnostic_finish (context);
494 exit (FATAL_EXIT_CODE);
496 break;
498 case DK_ICE:
499 case DK_ICE_NOBT:
501 struct backtrace_state *state = NULL;
502 if (diag_kind == DK_ICE)
503 state = backtrace_create_state (NULL, 0, bt_err_callback, NULL);
504 int count = 0;
505 if (state != NULL)
506 backtrace_full (state, 2, bt_callback, bt_err_callback,
507 (void *) &count);
509 if (context->abort_on_error)
510 real_abort ();
512 fnotice (stderr, "Please submit a full bug report,\n"
513 "with preprocessed source if appropriate.\n");
514 if (count > 0)
515 fnotice (stderr,
516 ("Please include the complete backtrace "
517 "with any bug report.\n"));
518 fnotice (stderr, "See %s for instructions.\n", bug_report_url);
520 exit (ICE_EXIT_CODE);
523 case DK_FATAL:
524 if (context->abort_on_error)
525 real_abort ();
526 diagnostic_finish (context);
527 fnotice (stderr, "compilation terminated.\n");
528 exit (FATAL_EXIT_CODE);
530 default:
531 gcc_unreachable ();
535 /* True if the last module or file in which a diagnostic was reported is
536 different from the current one. */
538 static bool
539 last_module_changed_p (diagnostic_context *context,
540 const line_map_ordinary *map)
542 return context->last_module != map;
545 /* Remember the current module or file as being the last one in which we
546 report a diagnostic. */
548 static void
549 set_last_module (diagnostic_context *context, const line_map_ordinary *map)
551 context->last_module = map;
554 void
555 diagnostic_report_current_module (diagnostic_context *context, location_t where)
557 const line_map_ordinary *map = NULL;
559 if (pp_needs_newline (context->printer))
561 pp_newline (context->printer);
562 pp_needs_newline (context->printer) = false;
565 if (where <= BUILTINS_LOCATION)
566 return;
568 linemap_resolve_location (line_table, where,
569 LRK_MACRO_DEFINITION_LOCATION,
570 &map);
572 if (map && last_module_changed_p (context, map))
574 set_last_module (context, map);
575 if (! MAIN_FILE_P (map))
577 map = INCLUDED_FROM (line_table, map);
578 if (context->show_column)
579 pp_verbatim (context->printer,
580 "In file included from %r%s:%d:%d%R", "locus",
581 LINEMAP_FILE (map),
582 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
583 else
584 pp_verbatim (context->printer,
585 "In file included from %r%s:%d%R", "locus",
586 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
587 while (! MAIN_FILE_P (map))
589 map = INCLUDED_FROM (line_table, map);
590 pp_verbatim (context->printer,
591 ",\n from %r%s:%d%R", "locus",
592 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
594 pp_verbatim (context->printer, ":");
595 pp_newline (context->printer);
600 void
601 default_diagnostic_starter (diagnostic_context *context,
602 diagnostic_info *diagnostic)
604 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
605 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
606 diagnostic));
609 void
610 default_diagnostic_start_span_fn (diagnostic_context *context,
611 expanded_location exploc)
613 pp_set_prefix (context->printer,
614 diagnostic_get_location_text (context, exploc));
615 pp_string (context->printer, "");
616 pp_newline (context->printer);
619 void
620 default_diagnostic_finalizer (diagnostic_context *context,
621 diagnostic_info *diagnostic)
623 diagnostic_show_locus (context, diagnostic->richloc, diagnostic->kind);
624 pp_destroy_prefix (context->printer);
625 pp_flush (context->printer);
628 /* Interface to specify diagnostic kind overrides. Returns the
629 previous setting, or DK_UNSPECIFIED if the parameters are out of
630 range. If OPTION_INDEX is zero, the new setting is for all the
631 diagnostics. */
632 diagnostic_t
633 diagnostic_classify_diagnostic (diagnostic_context *context,
634 int option_index,
635 diagnostic_t new_kind,
636 location_t where)
638 diagnostic_t old_kind;
640 if (option_index < 0
641 || option_index >= context->n_opts
642 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
643 return DK_UNSPECIFIED;
645 old_kind = context->classify_diagnostic[option_index];
647 /* Handle pragmas separately, since we need to keep track of *where*
648 the pragmas were. */
649 if (where != UNKNOWN_LOCATION)
651 int i;
653 /* Record the command-line status, so we can reset it back on DK_POP. */
654 if (old_kind == DK_UNSPECIFIED)
656 old_kind = !context->option_enabled (option_index,
657 context->option_state)
658 ? DK_IGNORED : (context->warning_as_error_requested
659 ? DK_ERROR : DK_WARNING);
660 context->classify_diagnostic[option_index] = old_kind;
663 for (i = context->n_classification_history - 1; i >= 0; i --)
664 if (context->classification_history[i].option == option_index)
666 old_kind = context->classification_history[i].kind;
667 break;
670 i = context->n_classification_history;
671 context->classification_history =
672 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
673 * sizeof (diagnostic_classification_change_t));
674 context->classification_history[i].location = where;
675 context->classification_history[i].option = option_index;
676 context->classification_history[i].kind = new_kind;
677 context->n_classification_history ++;
679 else
680 context->classify_diagnostic[option_index] = new_kind;
682 return old_kind;
685 /* Save all diagnostic classifications in a stack. */
686 void
687 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
689 context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
690 context->push_list[context->n_push ++] = context->n_classification_history;
693 /* Restore the topmost classification set off the stack. If the stack
694 is empty, revert to the state based on command line parameters. */
695 void
696 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
698 int jump_to;
699 int i;
701 if (context->n_push)
702 jump_to = context->push_list [-- context->n_push];
703 else
704 jump_to = 0;
706 i = context->n_classification_history;
707 context->classification_history =
708 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
709 * sizeof (diagnostic_classification_change_t));
710 context->classification_history[i].location = where;
711 context->classification_history[i].option = jump_to;
712 context->classification_history[i].kind = DK_POP;
713 context->n_classification_history ++;
716 /* Helper function for print_parseable_fixits. Print TEXT to PP, obeying the
717 escaping rules for -fdiagnostics-parseable-fixits. */
719 static void
720 print_escaped_string (pretty_printer *pp, const char *text)
722 gcc_assert (pp);
723 gcc_assert (text);
725 pp_character (pp, '"');
726 for (const char *ch = text; *ch; ch++)
728 switch (*ch)
730 case '\\':
731 /* Escape backslash as two backslashes. */
732 pp_string (pp, "\\\\");
733 break;
734 case '\t':
735 /* Escape tab as "\t". */
736 pp_string (pp, "\\t");
737 break;
738 case '\n':
739 /* Escape newline as "\n". */
740 pp_string (pp, "\\n");
741 break;
742 case '"':
743 /* Escape doublequotes as \". */
744 pp_string (pp, "\\\"");
745 break;
746 default:
747 if (ISPRINT (*ch))
748 pp_character (pp, *ch);
749 else
750 /* Use octal for non-printable chars. */
752 unsigned char c = (*ch & 0xff);
753 pp_printf (pp, "\\%o%o%o", (c / 64), (c / 8) & 007, c & 007);
755 break;
758 pp_character (pp, '"');
761 /* Implementation of -fdiagnostics-parseable-fixits. Print a
762 machine-parseable version of all fixits in RICHLOC to PP. */
764 static void
765 print_parseable_fixits (pretty_printer *pp, rich_location *richloc)
767 gcc_assert (pp);
768 gcc_assert (richloc);
770 for (unsigned i = 0; i < richloc->get_num_fixit_hints (); i++)
772 const fixit_hint *hint = richloc->get_fixit_hint (i);
773 source_location start_loc = hint->get_start_loc ();
774 expanded_location start_exploc = expand_location (start_loc);
775 pp_string (pp, "fix-it:");
776 print_escaped_string (pp, start_exploc.file);
777 /* For compatibility with clang, print as a half-open range. */
778 source_location next_loc = hint->get_next_loc ();
779 expanded_location next_exploc = expand_location (next_loc);
780 pp_printf (pp, ":{%i:%i-%i:%i}:",
781 start_exploc.line, start_exploc.column,
782 next_exploc.line, next_exploc.column);
783 print_escaped_string (pp, hint->get_string ());
784 pp_newline (pp);
788 /* Update the diag_class of DIAGNOSTIC based on its location
789 relative to any
790 #pragma GCC diagnostic
791 directives recorded within CONTEXT.
793 Return the new diag_class of DIAGNOSTIC if it was updated, or
794 DK_UNSPECIFIED otherwise. */
796 static diagnostic_t
797 update_effective_level_from_pragmas (diagnostic_context *context,
798 diagnostic_info *diagnostic)
800 diagnostic_t diag_class = DK_UNSPECIFIED;
802 if (context->n_classification_history > 0)
804 location_t location = diagnostic_location (diagnostic);
806 /* FIXME: Stupid search. Optimize later. */
807 for (int i = context->n_classification_history - 1; i >= 0; i --)
809 if (linemap_location_before_p
810 (line_table,
811 context->classification_history[i].location,
812 location))
814 if (context->classification_history[i].kind == (int) DK_POP)
816 i = context->classification_history[i].option;
817 continue;
819 int option = context->classification_history[i].option;
820 /* The option 0 is for all the diagnostics. */
821 if (option == 0 || option == diagnostic->option_index)
823 diag_class = context->classification_history[i].kind;
824 if (diag_class != DK_UNSPECIFIED)
825 diagnostic->kind = diag_class;
826 break;
832 return diag_class;
835 /* Print any metadata about the option used to control DIAGNOSTIC to CONTEXT's
836 printer, e.g. " [-Werror=uninitialized]".
837 Subroutine of diagnostic_report_diagnostic. */
839 static void
840 print_option_information (diagnostic_context *context,
841 const diagnostic_info *diagnostic,
842 diagnostic_t orig_diag_kind)
844 char *option_text;
846 option_text = context->option_name (context, diagnostic->option_index,
847 orig_diag_kind, diagnostic->kind);
849 if (option_text)
851 pretty_printer *pp = context->printer;
852 pp_string (pp, " [");
853 pp_string (pp, colorize_start (pp_show_color (pp),
854 diagnostic_kind_color[diagnostic->kind]));
855 pp_string (pp, option_text);
856 pp_string (pp, colorize_stop (pp_show_color (pp)));
857 pp_character (pp, ']');
858 free (option_text);
862 /* Report a diagnostic message (an error or a warning) as specified by
863 DC. This function is *the* subroutine in terms of which front-ends
864 should implement their specific diagnostic handling modules. The
865 front-end independent format specifiers are exactly those described
866 in the documentation of output_format.
867 Return true if a diagnostic was printed, false otherwise. */
869 bool
870 diagnostic_report_diagnostic (diagnostic_context *context,
871 diagnostic_info *diagnostic)
873 location_t location = diagnostic_location (diagnostic);
874 diagnostic_t orig_diag_kind = diagnostic->kind;
876 /* Give preference to being able to inhibit warnings, before they
877 get reclassified to something else. */
878 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
879 && !diagnostic_report_warnings_p (context, location))
880 return false;
882 if (diagnostic->kind == DK_PEDWARN)
884 diagnostic->kind = pedantic_warning_kind (context);
885 /* We do this to avoid giving the message for -pedantic-errors. */
886 orig_diag_kind = diagnostic->kind;
889 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
890 return false;
892 if (context->lock > 0)
894 /* If we're reporting an ICE in the middle of some other error,
895 try to flush out the previous error, then let this one
896 through. Don't do this more than once. */
897 if ((diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
898 && context->lock == 1)
899 pp_newline_and_flush (context->printer);
900 else
901 error_recursion (context);
904 /* If the user requested that warnings be treated as errors, so be
905 it. Note that we do this before the next block so that
906 individual warnings can be overridden back to warnings with
907 -Wno-error=*. */
908 if (context->warning_as_error_requested
909 && diagnostic->kind == DK_WARNING)
910 diagnostic->kind = DK_ERROR;
912 if (diagnostic->option_index
913 && diagnostic->option_index != permissive_error_option (context))
915 /* This tests if the user provided the appropriate -Wfoo or
916 -Wno-foo option. */
917 if (! context->option_enabled (diagnostic->option_index,
918 context->option_state))
919 return false;
921 /* This tests for #pragma diagnostic changes. */
922 diagnostic_t diag_class
923 = update_effective_level_from_pragmas (context, diagnostic);
925 /* This tests if the user provided the appropriate -Werror=foo
926 option. */
927 if (diag_class == DK_UNSPECIFIED
928 && (context->classify_diagnostic[diagnostic->option_index]
929 != DK_UNSPECIFIED))
930 diagnostic->kind
931 = context->classify_diagnostic[diagnostic->option_index];
933 /* This allows for future extensions, like temporarily disabling
934 warnings for ranges of source code. */
935 if (diagnostic->kind == DK_IGNORED)
936 return false;
939 if (diagnostic->kind != DK_NOTE)
940 diagnostic_check_max_errors (context);
942 context->lock++;
944 if (diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
946 /* When not checking, ICEs are converted to fatal errors when an
947 error has already occurred. This is counteracted by
948 abort_on_error. */
949 if (!CHECKING_P
950 && (diagnostic_kind_count (context, DK_ERROR) > 0
951 || diagnostic_kind_count (context, DK_SORRY) > 0)
952 && !context->abort_on_error)
954 expanded_location s
955 = expand_location (diagnostic_location (diagnostic));
956 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
957 s.file, s.line);
958 exit (ICE_EXIT_CODE);
960 if (context->internal_error)
961 (*context->internal_error) (context,
962 diagnostic->message.format_spec,
963 diagnostic->message.args_ptr);
965 if (diagnostic->kind == DK_ERROR && orig_diag_kind == DK_WARNING)
966 ++diagnostic_kind_count (context, DK_WERROR);
967 else
968 ++diagnostic_kind_count (context, diagnostic->kind);
970 diagnostic->message.x_data = &diagnostic->x_data;
971 diagnostic->x_data = NULL;
972 pp_format (context->printer, &diagnostic->message);
973 (*diagnostic_starter (context)) (context, diagnostic);
974 pp_output_formatted_text (context->printer);
975 if (context->show_option_requested)
976 print_option_information (context, diagnostic, orig_diag_kind);
977 (*diagnostic_finalizer (context)) (context, diagnostic);
978 if (context->parseable_fixits_p)
980 print_parseable_fixits (context->printer, diagnostic->richloc);
981 pp_flush (context->printer);
983 diagnostic_action_after_output (context, diagnostic->kind);
984 diagnostic->x_data = NULL;
986 if (context->edit_context_ptr)
987 if (diagnostic->richloc->fixits_can_be_auto_applied_p ())
988 context->edit_context_ptr->add_fixits (diagnostic->richloc);
990 context->lock--;
992 return true;
995 /* Given a partial pathname as input, return another pathname that
996 shares no directory elements with the pathname of __FILE__. This
997 is used by fancy_abort() to print `Internal compiler error in expr.c'
998 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1000 const char *
1001 trim_filename (const char *name)
1003 static const char this_file[] = __FILE__;
1004 const char *p = name, *q = this_file;
1006 /* First skip any "../" in each filename. This allows us to give a proper
1007 reference to a file in a subdirectory. */
1008 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
1009 p += 3;
1011 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
1012 q += 3;
1014 /* Now skip any parts the two filenames have in common. */
1015 while (*p == *q && *p != 0 && *q != 0)
1016 p++, q++;
1018 /* Now go backwards until the previous directory separator. */
1019 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
1020 p--;
1022 return p;
1025 /* Standard error reporting routines in increasing order of severity.
1026 All of these take arguments like printf. */
1028 /* Text to be emitted verbatim to the error message stream; this
1029 produces no prefix and disables line-wrapping. Use rarely. */
1030 void
1031 verbatim (const char *gmsgid, ...)
1033 text_info text;
1034 va_list ap;
1036 va_start (ap, gmsgid);
1037 text.err_no = errno;
1038 text.args_ptr = &ap;
1039 text.format_spec = _(gmsgid);
1040 text.x_data = NULL;
1041 pp_format_verbatim (global_dc->printer, &text);
1042 pp_newline_and_flush (global_dc->printer);
1043 va_end (ap);
1046 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
1047 void
1048 diagnostic_append_note (diagnostic_context *context,
1049 location_t location,
1050 const char * gmsgid, ...)
1052 diagnostic_info diagnostic;
1053 va_list ap;
1054 const char *saved_prefix;
1055 rich_location richloc (line_table, location);
1057 va_start (ap, gmsgid);
1058 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_NOTE);
1059 if (context->inhibit_notes_p)
1061 va_end (ap);
1062 return;
1064 saved_prefix = pp_get_prefix (context->printer);
1065 pp_set_prefix (context->printer,
1066 diagnostic_build_prefix (context, &diagnostic));
1067 pp_format (context->printer, &diagnostic.message);
1068 pp_output_formatted_text (context->printer);
1069 pp_destroy_prefix (context->printer);
1070 pp_set_prefix (context->printer, saved_prefix);
1071 diagnostic_show_locus (context, &richloc, DK_NOTE);
1072 va_end (ap);
1075 /* Implement emit_diagnostic, inform, warning, warning_at, pedwarn,
1076 permerror, error, error_at, error_at, sorry, fatal_error, internal_error,
1077 and internal_error_no_backtrace, as documented and defined below. */
1078 static bool
1079 diagnostic_impl (rich_location *richloc, int opt,
1080 const char *gmsgid,
1081 va_list *ap, diagnostic_t kind)
1083 diagnostic_info diagnostic;
1084 if (kind == DK_PERMERROR)
1086 diagnostic_set_info (&diagnostic, gmsgid, ap, richloc,
1087 permissive_error_kind (global_dc));
1088 diagnostic.option_index = permissive_error_option (global_dc);
1090 else
1092 diagnostic_set_info (&diagnostic, gmsgid, ap, richloc, kind);
1093 if (kind == DK_WARNING || kind == DK_PEDWARN)
1094 diagnostic.option_index = opt;
1096 return diagnostic_report_diagnostic (global_dc, &diagnostic);
1099 /* Implement inform_n, warning_n, and error_n, as documented and
1100 defined below. */
1101 static bool
1102 diagnostic_n_impl (rich_location *richloc, int opt, int n,
1103 const char *singular_gmsgid,
1104 const char *plural_gmsgid,
1105 va_list *ap, diagnostic_t kind)
1107 diagnostic_info diagnostic;
1108 diagnostic_set_info_translated (&diagnostic,
1109 ngettext (singular_gmsgid, plural_gmsgid, n),
1110 ap, richloc, kind);
1111 if (kind == DK_WARNING)
1112 diagnostic.option_index = opt;
1113 return diagnostic_report_diagnostic (global_dc, &diagnostic);
1116 /* Wrapper around diagnostic_impl taking a variable argument list. */
1118 bool
1119 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
1120 const char *gmsgid, ...)
1122 va_list ap;
1123 va_start (ap, gmsgid);
1124 rich_location richloc (line_table, location);
1125 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, kind);
1126 va_end (ap);
1127 return ret;
1130 /* Wrapper around diagnostic_impl taking a va_list parameter. */
1132 bool
1133 emit_diagnostic_valist (diagnostic_t kind, location_t location, int opt,
1134 const char *gmsgid, va_list *ap)
1136 rich_location richloc (line_table, location);
1137 return diagnostic_impl (&richloc, opt, gmsgid, ap, kind);
1140 /* An informative note at LOCATION. Use this for additional details on an error
1141 message. */
1142 void
1143 inform (location_t location, const char *gmsgid, ...)
1145 va_list ap;
1146 va_start (ap, gmsgid);
1147 rich_location richloc (line_table, location);
1148 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_NOTE);
1149 va_end (ap);
1152 /* Same as "inform" above, but at RICHLOC. */
1153 void
1154 inform (rich_location *richloc, const char *gmsgid, ...)
1156 gcc_assert (richloc);
1158 va_list ap;
1159 va_start (ap, gmsgid);
1160 diagnostic_impl (richloc, -1, gmsgid, &ap, DK_NOTE);
1161 va_end (ap);
1164 /* An informative note at LOCATION. Use this for additional details on an
1165 error message. */
1166 void
1167 inform_n (location_t location, int n, const char *singular_gmsgid,
1168 const char *plural_gmsgid, ...)
1170 va_list ap;
1171 va_start (ap, plural_gmsgid);
1172 rich_location richloc (line_table, location);
1173 diagnostic_n_impl (&richloc, -1, n, singular_gmsgid, plural_gmsgid,
1174 &ap, DK_NOTE);
1175 va_end (ap);
1178 /* A warning at INPUT_LOCATION. Use this for code which is correct according
1179 to the relevant language specification but is likely to be buggy anyway.
1180 Returns true if the warning was printed, false if it was inhibited. */
1181 bool
1182 warning (int opt, const char *gmsgid, ...)
1184 va_list ap;
1185 va_start (ap, gmsgid);
1186 rich_location richloc (line_table, input_location);
1187 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_WARNING);
1188 va_end (ap);
1189 return ret;
1192 /* A warning at LOCATION. Use this for code which is correct according to the
1193 relevant language specification but is likely to be buggy anyway.
1194 Returns true if the warning was printed, false if it was inhibited. */
1196 bool
1197 warning_at (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_WARNING);
1203 va_end (ap);
1204 return ret;
1207 /* Same as "warning at" above, but using RICHLOC. */
1209 bool
1210 warning_at (rich_location *richloc, int opt, const char *gmsgid, ...)
1212 gcc_assert (richloc);
1214 va_list ap;
1215 va_start (ap, gmsgid);
1216 bool ret = diagnostic_impl (richloc, opt, gmsgid, &ap, DK_WARNING);
1217 va_end (ap);
1218 return ret;
1221 /* Same as warning_n plural variant below, but using RICHLOC. */
1223 bool
1224 warning_n (rich_location *richloc, int opt, int n,
1225 const char *singular_gmsgid, const char *plural_gmsgid, ...)
1227 gcc_assert (richloc);
1229 va_list ap;
1230 va_start (ap, plural_gmsgid);
1231 bool ret = diagnostic_n_impl (richloc, opt, n,
1232 singular_gmsgid, plural_gmsgid,
1233 &ap, DK_WARNING);
1234 va_end (ap);
1235 return ret;
1238 /* A warning at LOCATION. Use this for code which is correct according to the
1239 relevant language specification but is likely to be buggy anyway.
1240 Returns true if the warning was printed, false if it was inhibited. */
1242 bool
1243 warning_n (location_t location, int opt, int n, const char *singular_gmsgid,
1244 const char *plural_gmsgid, ...)
1246 va_list ap;
1247 va_start (ap, plural_gmsgid);
1248 rich_location richloc (line_table, location);
1249 bool ret = diagnostic_n_impl (&richloc, opt, n,
1250 singular_gmsgid, plural_gmsgid,
1251 &ap, DK_WARNING);
1252 va_end (ap);
1253 return ret;
1256 /* A "pedantic" warning at LOCATION: issues a warning unless
1257 -pedantic-errors was given on the command line, in which case it
1258 issues an error. Use this for diagnostics required by the relevant
1259 language standard, if you have chosen not to make them errors.
1261 Note that these diagnostics are issued independent of the setting
1262 of the -Wpedantic command-line switch. To get a warning enabled
1263 only with that switch, use either "if (pedantic) pedwarn
1264 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
1265 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
1267 Returns true if the warning was printed, false if it was inhibited. */
1269 bool
1270 pedwarn (location_t location, int opt, const char *gmsgid, ...)
1272 va_list ap;
1273 va_start (ap, gmsgid);
1274 rich_location richloc (line_table, location);
1275 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_PEDWARN);
1276 va_end (ap);
1277 return ret;
1280 /* Same as pedwarn above, but using RICHLOC. */
1282 bool
1283 pedwarn (rich_location *richloc, int opt, const char *gmsgid, ...)
1285 gcc_assert (richloc);
1287 va_list ap;
1288 va_start (ap, gmsgid);
1289 bool ret = diagnostic_impl (richloc, opt, gmsgid, &ap, DK_PEDWARN);
1290 va_end (ap);
1291 return ret;
1294 /* A "permissive" error at LOCATION: issues an error unless
1295 -fpermissive was given on the command line, in which case it issues
1296 a warning. Use this for things that really should be errors but we
1297 want to support legacy code.
1299 Returns true if the warning was printed, false if it was inhibited. */
1301 bool
1302 permerror (location_t location, const char *gmsgid, ...)
1304 va_list ap;
1305 va_start (ap, gmsgid);
1306 rich_location richloc (line_table, location);
1307 bool ret = diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_PERMERROR);
1308 va_end (ap);
1309 return ret;
1312 /* Same as "permerror" above, but at RICHLOC. */
1314 bool
1315 permerror (rich_location *richloc, const char *gmsgid, ...)
1317 gcc_assert (richloc);
1319 va_list ap;
1320 va_start (ap, gmsgid);
1321 bool ret = diagnostic_impl (richloc, -1, gmsgid, &ap, DK_PERMERROR);
1322 va_end (ap);
1323 return ret;
1326 /* A hard error: the code is definitely ill-formed, and an object file
1327 will not be produced. */
1328 void
1329 error (const char *gmsgid, ...)
1331 va_list ap;
1332 va_start (ap, gmsgid);
1333 rich_location richloc (line_table, input_location);
1334 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ERROR);
1335 va_end (ap);
1338 /* A hard error: the code is definitely ill-formed, and an object file
1339 will not be produced. */
1340 void
1341 error_n (location_t location, int n, const char *singular_gmsgid,
1342 const char *plural_gmsgid, ...)
1344 va_list ap;
1345 va_start (ap, plural_gmsgid);
1346 rich_location richloc (line_table, location);
1347 diagnostic_n_impl (&richloc, -1, n, singular_gmsgid, plural_gmsgid,
1348 &ap, DK_ERROR);
1349 va_end (ap);
1352 /* Same as above, but use location LOC instead of input_location. */
1353 void
1354 error_at (location_t loc, const char *gmsgid, ...)
1356 va_list ap;
1357 va_start (ap, gmsgid);
1358 rich_location richloc (line_table, loc);
1359 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ERROR);
1360 va_end (ap);
1363 /* Same as above, but use RICH_LOC. */
1365 void
1366 error_at (rich_location *richloc, const char *gmsgid, ...)
1368 gcc_assert (richloc);
1370 va_list ap;
1371 va_start (ap, gmsgid);
1372 diagnostic_impl (richloc, -1, gmsgid, &ap, DK_ERROR);
1373 va_end (ap);
1376 /* "Sorry, not implemented." Use for a language feature which is
1377 required by the relevant specification but not implemented by GCC.
1378 An object file will not be produced. */
1379 void
1380 sorry (const char *gmsgid, ...)
1382 va_list ap;
1383 va_start (ap, gmsgid);
1384 rich_location richloc (line_table, input_location);
1385 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_SORRY);
1386 va_end (ap);
1389 /* Return true if an error or a "sorry" has been seen. Various
1390 processing is disabled after errors. */
1391 bool
1392 seen_error (void)
1394 return errorcount || sorrycount;
1397 /* An error which is severe enough that we make no attempt to
1398 continue. Do not use this for internal consistency checks; that's
1399 internal_error. Use of this function should be rare. */
1400 void
1401 fatal_error (location_t loc, const char *gmsgid, ...)
1403 va_list ap;
1404 va_start (ap, gmsgid);
1405 rich_location richloc (line_table, loc);
1406 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_FATAL);
1407 va_end (ap);
1409 gcc_unreachable ();
1412 /* An internal consistency check has failed. We make no attempt to
1413 continue. Note that unless there is debugging value to be had from
1414 a more specific message, or some other good reason, you should use
1415 abort () instead of calling this function directly. */
1416 void
1417 internal_error (const char *gmsgid, ...)
1419 va_list ap;
1420 va_start (ap, gmsgid);
1421 rich_location richloc (line_table, input_location);
1422 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ICE);
1423 va_end (ap);
1425 gcc_unreachable ();
1428 /* Like internal_error, but no backtrace will be printed. Used when
1429 the internal error does not happen at the current location, but happened
1430 somewhere else. */
1431 void
1432 internal_error_no_backtrace (const char *gmsgid, ...)
1434 va_list ap;
1435 va_start (ap, gmsgid);
1436 rich_location richloc (line_table, input_location);
1437 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ICE_NOBT);
1438 va_end (ap);
1440 gcc_unreachable ();
1443 /* Special case error functions. Most are implemented in terms of the
1444 above, or should be. */
1446 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1447 runs its second argument through gettext. */
1448 void
1449 fnotice (FILE *file, const char *cmsgid, ...)
1451 va_list ap;
1453 va_start (ap, cmsgid);
1454 vfprintf (file, _(cmsgid), ap);
1455 va_end (ap);
1458 /* Inform the user that an error occurred while trying to report some
1459 other error. This indicates catastrophic internal inconsistencies,
1460 so give up now. But do try to flush out the previous error.
1461 This mustn't use internal_error, that will cause infinite recursion. */
1463 static void
1464 error_recursion (diagnostic_context *context)
1466 if (context->lock < 3)
1467 pp_newline_and_flush (context->printer);
1469 fnotice (stderr,
1470 "Internal compiler error: Error reporting routines re-entered.\n");
1472 /* Call diagnostic_action_after_output to get the "please submit a bug
1473 report" message. */
1474 diagnostic_action_after_output (context, DK_ICE);
1476 /* Do not use gcc_unreachable here; that goes through internal_error
1477 and therefore would cause infinite recursion. */
1478 real_abort ();
1481 /* Report an internal compiler error in a friendly manner. This is
1482 the function that gets called upon use of abort() in the source
1483 code generally, thanks to a special macro. */
1485 void
1486 fancy_abort (const char *file, int line, const char *function)
1488 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1491 /* Really call the system 'abort'. This has to go right at the end of
1492 this file, so that there are no functions after it that call abort
1493 and get the system abort instead of our macro. */
1494 #undef abort
1495 static void
1496 real_abort (void)
1498 abort ();
1501 #if CHECKING_P
1503 namespace selftest {
1505 /* Helper function for test_print_escaped_string. */
1507 static void
1508 assert_print_escaped_string (const location &loc, const char *expected_output,
1509 const char *input)
1511 pretty_printer pp;
1512 print_escaped_string (&pp, input);
1513 ASSERT_STREQ_AT (loc, expected_output, pp_formatted_text (&pp));
1516 #define ASSERT_PRINT_ESCAPED_STRING_STREQ(EXPECTED_OUTPUT, INPUT) \
1517 assert_print_escaped_string (SELFTEST_LOCATION, EXPECTED_OUTPUT, INPUT)
1519 /* Tests of print_escaped_string. */
1521 static void
1522 test_print_escaped_string ()
1524 /* Empty string. */
1525 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"\"", "");
1527 /* Non-empty string. */
1528 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"hello world\"", "hello world");
1530 /* Various things that need to be escaped: */
1531 /* Backslash. */
1532 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\\after\"",
1533 "before\\after");
1534 /* Tab. */
1535 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\tafter\"",
1536 "before\tafter");
1537 /* Newline. */
1538 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\nafter\"",
1539 "before\nafter");
1540 /* Double quote. */
1541 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\"after\"",
1542 "before\"after");
1544 /* Non-printable characters: BEL: '\a': 0x07 */
1545 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\007after\"",
1546 "before\aafter");
1547 /* Non-printable characters: vertical tab: '\v': 0x0b */
1548 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\013after\"",
1549 "before\vafter");
1552 /* Tests of print_parseable_fixits. */
1554 /* Verify that print_parseable_fixits emits the empty string if there
1555 are no fixits. */
1557 static void
1558 test_print_parseable_fixits_none ()
1560 pretty_printer pp;
1561 rich_location richloc (line_table, UNKNOWN_LOCATION);
1563 print_parseable_fixits (&pp, &richloc);
1564 ASSERT_STREQ ("", pp_formatted_text (&pp));
1567 /* Verify that print_parseable_fixits does the right thing if there
1568 is an insertion fixit hint. */
1570 static void
1571 test_print_parseable_fixits_insert ()
1573 pretty_printer pp;
1574 rich_location richloc (line_table, UNKNOWN_LOCATION);
1576 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
1577 linemap_line_start (line_table, 5, 100);
1578 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1579 location_t where = linemap_position_for_column (line_table, 10);
1580 richloc.add_fixit_insert_before (where, "added content");
1582 print_parseable_fixits (&pp, &richloc);
1583 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:10}:\"added content\"\n",
1584 pp_formatted_text (&pp));
1587 /* Verify that print_parseable_fixits does the right thing if there
1588 is an removal fixit hint. */
1590 static void
1591 test_print_parseable_fixits_remove ()
1593 pretty_printer pp;
1594 rich_location richloc (line_table, UNKNOWN_LOCATION);
1596 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
1597 linemap_line_start (line_table, 5, 100);
1598 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1599 source_range where;
1600 where.m_start = linemap_position_for_column (line_table, 10);
1601 where.m_finish = linemap_position_for_column (line_table, 20);
1602 richloc.add_fixit_remove (where);
1604 print_parseable_fixits (&pp, &richloc);
1605 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"\"\n",
1606 pp_formatted_text (&pp));
1609 /* Verify that print_parseable_fixits does the right thing if there
1610 is an replacement fixit hint. */
1612 static void
1613 test_print_parseable_fixits_replace ()
1615 pretty_printer pp;
1616 rich_location richloc (line_table, UNKNOWN_LOCATION);
1618 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
1619 linemap_line_start (line_table, 5, 100);
1620 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1621 source_range where;
1622 where.m_start = linemap_position_for_column (line_table, 10);
1623 where.m_finish = linemap_position_for_column (line_table, 20);
1624 richloc.add_fixit_replace (where, "replacement");
1626 print_parseable_fixits (&pp, &richloc);
1627 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"replacement\"\n",
1628 pp_formatted_text (&pp));
1631 /* Verify that
1632 diagnostic_get_location_text (..., SHOW_COLUMN)
1633 generates EXPECTED_LOC_TEXT, given FILENAME, LINE, COLUMN, with
1634 colorization disabled. */
1636 static void
1637 assert_location_text (const char *expected_loc_text,
1638 const char *filename, int line, int column,
1639 bool show_column)
1641 test_diagnostic_context dc;
1642 dc.show_column = show_column;
1644 expanded_location xloc;
1645 xloc.file = filename;
1646 xloc.line = line;
1647 xloc.column = column;
1648 xloc.data = NULL;
1649 xloc.sysp = false;
1651 char *actual_loc_text = diagnostic_get_location_text (&dc, xloc);
1652 ASSERT_STREQ (expected_loc_text, actual_loc_text);
1653 free (actual_loc_text);
1656 /* Verify that diagnostic_get_location_text works as expected. */
1658 static void
1659 test_diagnostic_get_location_text ()
1661 const char *old_progname = progname;
1662 progname = "PROGNAME";
1663 assert_location_text ("PROGNAME:", NULL, 0, 0, true);
1664 assert_location_text ("<built-in>:", "<built-in>", 42, 10, true);
1665 assert_location_text ("foo.c:42:10:", "foo.c", 42, 10, true);
1666 assert_location_text ("foo.c:42:", "foo.c", 42, 10, false);
1667 progname = old_progname;
1670 /* Run all of the selftests within this file. */
1672 void
1673 diagnostic_c_tests ()
1675 test_print_escaped_string ();
1676 test_print_parseable_fixits_none ();
1677 test_print_parseable_fixits_insert ();
1678 test_print_parseable_fixits_remove ();
1679 test_print_parseable_fixits_replace ();
1680 test_diagnostic_get_location_text ();
1683 } // namespace selftest
1685 #endif /* #if CHECKING_P */