]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/diagnostic.c
diagnostic.c (diagnostic_report_diagnostic): Remove extraneous braces.
[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 "edit-context.h"
35 #include "selftest.h"
36
37 #ifdef HAVE_TERMIOS_H
38 # include <termios.h>
39 #endif
40
41 #ifdef GWINSZ_IN_SYS_IOCTL
42 # include <sys/ioctl.h>
43 #endif
44
45 #define pedantic_warning_kind(DC) \
46 ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
47 #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR)
48 #define permissive_error_option(DC) ((DC)->opt_permissive)
49
50 /* Prototypes. */
51 static bool diagnostic_impl (rich_location *, int, const char *,
52 va_list *, diagnostic_t) ATTRIBUTE_GCC_DIAG(3,0);
53 static bool diagnostic_n_impl (location_t, int, int, const char *,
54 const char *, va_list *,
55 diagnostic_t) ATTRIBUTE_GCC_DIAG(5,0);
56 static bool diagnostic_n_impl_richloc (rich_location *, int, int, const char *,
57 const char *, va_list *,
58 diagnostic_t) ATTRIBUTE_GCC_DIAG(5,0);
59
60 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
61 static void real_abort (void) ATTRIBUTE_NORETURN;
62
63 /* Name of program invoked, sans directories. */
64
65 const char *progname;
66
67 /* A diagnostic_context surrogate for stderr. */
68 static diagnostic_context global_diagnostic_context;
69 diagnostic_context *global_dc = &global_diagnostic_context;
70 \f
71 /* Return a malloc'd string containing MSG formatted a la printf. The
72 caller is responsible for freeing the memory. */
73 char *
74 build_message_string (const char *msg, ...)
75 {
76 char *str;
77 va_list ap;
78
79 va_start (ap, msg);
80 str = xvasprintf (msg, ap);
81 va_end (ap);
82
83 return str;
84 }
85
86 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
87 char *
88 file_name_as_prefix (diagnostic_context *context, const char *f)
89 {
90 const char *locus_cs
91 = colorize_start (pp_show_color (context->printer), "locus");
92 const char *locus_ce = colorize_stop (pp_show_color (context->printer));
93 return build_message_string ("%s%s:%s ", locus_cs, f, locus_ce);
94 }
95
96
97 \f
98 /* Return the value of the getenv("COLUMNS") as an integer. If the
99 value is not set to a positive integer, use ioctl to get the
100 terminal width. If it fails, return INT_MAX. */
101 int
102 get_terminal_width (void)
103 {
104 const char * s = getenv ("COLUMNS");
105 if (s != NULL) {
106 int n = atoi (s);
107 if (n > 0)
108 return n;
109 }
110
111 #ifdef TIOCGWINSZ
112 struct winsize w;
113 w.ws_col = 0;
114 if (ioctl (0, TIOCGWINSZ, &w) == 0 && w.ws_col > 0)
115 return w.ws_col;
116 #endif
117
118 return INT_MAX;
119 }
120
121 /* Set caret_max_width to value. */
122 void
123 diagnostic_set_caret_max_width (diagnostic_context *context, int value)
124 {
125 /* One minus to account for the leading empty space. */
126 value = value ? value - 1
127 : (isatty (fileno (pp_buffer (context->printer)->stream))
128 ? get_terminal_width () - 1: INT_MAX);
129
130 if (value <= 0)
131 value = INT_MAX;
132
133 context->caret_max_width = value;
134 }
135
136 /* Initialize the diagnostic message outputting machinery. */
137 void
138 diagnostic_initialize (diagnostic_context *context, int n_opts)
139 {
140 int i;
141
142 /* Allocate a basic pretty-printer. Clients will replace this a
143 much more elaborated pretty-printer if they wish. */
144 context->printer = XNEW (pretty_printer);
145 new (context->printer) pretty_printer ();
146
147 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
148 context->warning_as_error_requested = false;
149 context->n_opts = n_opts;
150 context->classify_diagnostic = XNEWVEC (diagnostic_t, n_opts);
151 for (i = 0; i < n_opts; i++)
152 context->classify_diagnostic[i] = DK_UNSPECIFIED;
153 context->show_caret = false;
154 diagnostic_set_caret_max_width (context, pp_line_cutoff (context->printer));
155 for (i = 0; i < rich_location::STATICALLY_ALLOCATED_RANGES; i++)
156 context->caret_chars[i] = '^';
157 context->show_option_requested = false;
158 context->abort_on_error = false;
159 context->show_column = false;
160 context->pedantic_errors = false;
161 context->permissive = false;
162 context->opt_permissive = 0;
163 context->fatal_errors = false;
164 context->dc_inhibit_warnings = false;
165 context->dc_warn_system_headers = false;
166 context->max_errors = 0;
167 context->internal_error = NULL;
168 diagnostic_starter (context) = default_diagnostic_starter;
169 context->start_span = default_diagnostic_start_span_fn;
170 diagnostic_finalizer (context) = default_diagnostic_finalizer;
171 context->option_enabled = NULL;
172 context->option_state = NULL;
173 context->option_name = NULL;
174 context->last_location = UNKNOWN_LOCATION;
175 context->last_module = 0;
176 context->x_data = NULL;
177 context->lock = 0;
178 context->inhibit_notes_p = false;
179 context->colorize_source_p = false;
180 context->show_ruler_p = false;
181 context->parseable_fixits_p = false;
182 context->edit_context_ptr = NULL;
183 }
184
185 /* Maybe initialize the color support. We require clients to do this
186 explicitly, since most clients don't want color. When called
187 without a VALUE, it initializes with DIAGNOSTICS_COLOR_DEFAULT. */
188
189 void
190 diagnostic_color_init (diagnostic_context *context, int value /*= -1 */)
191 {
192 /* value == -1 is the default value. */
193 if (value < 0)
194 {
195 /* If DIAGNOSTICS_COLOR_DEFAULT is -1, default to
196 -fdiagnostics-color=auto if GCC_COLORS is in the environment,
197 otherwise default to -fdiagnostics-color=never, for other
198 values default to that
199 -fdiagnostics-color={never,auto,always}. */
200 if (DIAGNOSTICS_COLOR_DEFAULT == -1)
201 {
202 if (!getenv ("GCC_COLORS"))
203 return;
204 value = DIAGNOSTICS_COLOR_AUTO;
205 }
206 else
207 value = DIAGNOSTICS_COLOR_DEFAULT;
208 }
209 pp_show_color (context->printer)
210 = colorize_init ((diagnostic_color_rule_t) value);
211 }
212
213 /* Do any cleaning up required after the last diagnostic is emitted. */
214
215 void
216 diagnostic_finish (diagnostic_context *context)
217 {
218 /* Some of the errors may actually have been warnings. */
219 if (diagnostic_kind_count (context, DK_WERROR))
220 {
221 /* -Werror was given. */
222 if (context->warning_as_error_requested)
223 pp_verbatim (context->printer,
224 _("%s: all warnings being treated as errors"),
225 progname);
226 /* At least one -Werror= was given. */
227 else
228 pp_verbatim (context->printer,
229 _("%s: some warnings being treated as errors"),
230 progname);
231 pp_newline_and_flush (context->printer);
232 }
233
234 diagnostic_file_cache_fini ();
235
236 XDELETEVEC (context->classify_diagnostic);
237 context->classify_diagnostic = NULL;
238
239 /* diagnostic_initialize allocates context->printer using XNEW
240 and placement-new. */
241 context->printer->~pretty_printer ();
242 XDELETE (context->printer);
243 context->printer = NULL;
244
245 if (context->edit_context_ptr)
246 {
247 delete context->edit_context_ptr;
248 context->edit_context_ptr = NULL;
249 }
250 }
251
252 /* Initialize DIAGNOSTIC, where the message MSG has already been
253 translated. */
254 void
255 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
256 va_list *args, rich_location *richloc,
257 diagnostic_t kind)
258 {
259 gcc_assert (richloc);
260 diagnostic->message.err_no = errno;
261 diagnostic->message.args_ptr = args;
262 diagnostic->message.format_spec = msg;
263 diagnostic->message.m_richloc = richloc;
264 diagnostic->richloc = richloc;
265 diagnostic->kind = kind;
266 diagnostic->option_index = 0;
267 }
268
269 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
270 translated. */
271 void
272 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
273 va_list *args, rich_location *richloc,
274 diagnostic_t kind)
275 {
276 gcc_assert (richloc);
277 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, richloc, kind);
278 }
279
280 static const char *const diagnostic_kind_color[] = {
281 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
282 #include "diagnostic.def"
283 #undef DEFINE_DIAGNOSTIC_KIND
284 NULL
285 };
286
287 /* Get a color name for diagnostics of type KIND
288 Result could be NULL. */
289
290 const char *
291 diagnostic_get_color_for_kind (diagnostic_t kind)
292 {
293 return diagnostic_kind_color[kind];
294 }
295
296 /* Return a malloc'd string describing a location e.g. "foo.c:42:10".
297 The caller is responsible for freeing the memory. */
298
299 static char *
300 diagnostic_get_location_text (diagnostic_context *context,
301 expanded_location s)
302 {
303 pretty_printer *pp = context->printer;
304 const char *locus_cs = colorize_start (pp_show_color (pp), "locus");
305 const char *locus_ce = colorize_stop (pp_show_color (pp));
306
307 if (s.file == NULL)
308 return build_message_string ("%s%s:%s", locus_cs, progname, locus_ce);
309
310 if (!strcmp (s.file, N_("<built-in>")))
311 return build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce);
312
313 if (context->show_column)
314 return build_message_string ("%s%s:%d:%d:%s", locus_cs, s.file, s.line,
315 s.column, locus_ce);
316 else
317 return build_message_string ("%s%s:%d:%s", locus_cs, s.file, s.line,
318 locus_ce);
319 }
320
321 /* Return a malloc'd string describing a location and the severity of the
322 diagnostic, e.g. "foo.c:42:10: error: ". The caller is responsible for
323 freeing the memory. */
324 char *
325 diagnostic_build_prefix (diagnostic_context *context,
326 const diagnostic_info *diagnostic)
327 {
328 static const char *const diagnostic_kind_text[] = {
329 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
330 #include "diagnostic.def"
331 #undef DEFINE_DIAGNOSTIC_KIND
332 "must-not-happen"
333 };
334 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
335
336 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
337 const char *text_cs = "", *text_ce = "";
338 pretty_printer *pp = context->printer;
339
340 if (diagnostic_kind_color[diagnostic->kind])
341 {
342 text_cs = colorize_start (pp_show_color (pp),
343 diagnostic_kind_color[diagnostic->kind]);
344 text_ce = colorize_stop (pp_show_color (pp));
345 }
346
347 expanded_location s = diagnostic_expand_location (diagnostic);
348 char *location_text = diagnostic_get_location_text (context, s);
349
350 char *result = build_message_string ("%s %s%s%s", location_text,
351 text_cs, text, text_ce);
352 free (location_text);
353 return result;
354 }
355
356 /* Functions at which to stop the backtrace print. It's not
357 particularly helpful to print the callers of these functions. */
358
359 static const char * const bt_stop[] =
360 {
361 "main",
362 "toplev::main",
363 "execute_one_pass",
364 "compile_file",
365 };
366
367 /* A callback function passed to the backtrace_full function. */
368
369 static int
370 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
371 const char *function)
372 {
373 int *pcount = (int *) data;
374
375 /* If we don't have any useful information, don't print
376 anything. */
377 if (filename == NULL && function == NULL)
378 return 0;
379
380 /* Skip functions in diagnostic.c. */
381 if (*pcount == 0
382 && filename != NULL
383 && strcmp (lbasename (filename), "diagnostic.c") == 0)
384 return 0;
385
386 /* Print up to 20 functions. We could make this a --param, but
387 since this is only for debugging just use a constant for now. */
388 if (*pcount >= 20)
389 {
390 /* Returning a non-zero value stops the backtrace. */
391 return 1;
392 }
393 ++*pcount;
394
395 char *alc = NULL;
396 if (function != NULL)
397 {
398 char *str = cplus_demangle_v3 (function,
399 (DMGL_VERBOSE | DMGL_ANSI
400 | DMGL_GNU_V3 | DMGL_PARAMS));
401 if (str != NULL)
402 {
403 alc = str;
404 function = str;
405 }
406
407 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
408 {
409 size_t len = strlen (bt_stop[i]);
410 if (strncmp (function, bt_stop[i], len) == 0
411 && (function[len] == '\0' || function[len] == '('))
412 {
413 if (alc != NULL)
414 free (alc);
415 /* Returning a non-zero value stops the backtrace. */
416 return 1;
417 }
418 }
419 }
420
421 fprintf (stderr, "0x%lx %s\n\t%s:%d\n",
422 (unsigned long) pc,
423 function == NULL ? "???" : function,
424 filename == NULL ? "???" : filename,
425 lineno);
426
427 if (alc != NULL)
428 free (alc);
429
430 return 0;
431 }
432
433 /* A callback function passed to the backtrace_full function. This is
434 called if backtrace_full has an error. */
435
436 static void
437 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
438 {
439 if (errnum < 0)
440 {
441 /* This means that no debug info was available. Just quietly
442 skip printing backtrace info. */
443 return;
444 }
445 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
446 errnum == 0 ? "" : xstrerror (errnum));
447 }
448
449 /* Take any action which is expected to happen after the diagnostic
450 is written out. This function does not always return. */
451 void
452 diagnostic_action_after_output (diagnostic_context *context,
453 diagnostic_t diag_kind)
454 {
455 switch (diag_kind)
456 {
457 case DK_DEBUG:
458 case DK_NOTE:
459 case DK_ANACHRONISM:
460 case DK_WARNING:
461 break;
462
463 case DK_ERROR:
464 case DK_SORRY:
465 if (context->abort_on_error)
466 real_abort ();
467 if (context->fatal_errors)
468 {
469 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
470 diagnostic_finish (context);
471 exit (FATAL_EXIT_CODE);
472 }
473 if (context->max_errors != 0
474 && ((unsigned) (diagnostic_kind_count (context, DK_ERROR)
475 + diagnostic_kind_count (context, DK_SORRY)
476 + diagnostic_kind_count (context, DK_WERROR))
477 >= context->max_errors))
478 {
479 fnotice (stderr,
480 "compilation terminated due to -fmax-errors=%u.\n",
481 context->max_errors);
482 diagnostic_finish (context);
483 exit (FATAL_EXIT_CODE);
484 }
485 break;
486
487 case DK_ICE:
488 case DK_ICE_NOBT:
489 {
490 struct backtrace_state *state = NULL;
491 if (diag_kind == DK_ICE)
492 state = backtrace_create_state (NULL, 0, bt_err_callback, NULL);
493 int count = 0;
494 if (state != NULL)
495 backtrace_full (state, 2, bt_callback, bt_err_callback,
496 (void *) &count);
497
498 if (context->abort_on_error)
499 real_abort ();
500
501 fnotice (stderr, "Please submit a full bug report,\n"
502 "with preprocessed source if appropriate.\n");
503 if (count > 0)
504 fnotice (stderr,
505 ("Please include the complete backtrace "
506 "with any bug report.\n"));
507 fnotice (stderr, "See %s for instructions.\n", bug_report_url);
508
509 exit (ICE_EXIT_CODE);
510 }
511
512 case DK_FATAL:
513 if (context->abort_on_error)
514 real_abort ();
515 diagnostic_finish (context);
516 fnotice (stderr, "compilation terminated.\n");
517 exit (FATAL_EXIT_CODE);
518
519 default:
520 gcc_unreachable ();
521 }
522 }
523
524 void
525 diagnostic_report_current_module (diagnostic_context *context, location_t where)
526 {
527 const line_map_ordinary *map = NULL;
528
529 if (pp_needs_newline (context->printer))
530 {
531 pp_newline (context->printer);
532 pp_needs_newline (context->printer) = false;
533 }
534
535 if (where <= BUILTINS_LOCATION)
536 return;
537
538 linemap_resolve_location (line_table, where,
539 LRK_MACRO_DEFINITION_LOCATION,
540 &map);
541
542 if (map && diagnostic_last_module_changed (context, map))
543 {
544 diagnostic_set_last_module (context, map);
545 if (! MAIN_FILE_P (map))
546 {
547 map = INCLUDED_FROM (line_table, map);
548 if (context->show_column)
549 pp_verbatim (context->printer,
550 "In file included from %r%s:%d:%d%R", "locus",
551 LINEMAP_FILE (map),
552 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
553 else
554 pp_verbatim (context->printer,
555 "In file included from %r%s:%d%R", "locus",
556 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
557 while (! MAIN_FILE_P (map))
558 {
559 map = INCLUDED_FROM (line_table, map);
560 pp_verbatim (context->printer,
561 ",\n from %r%s:%d%R", "locus",
562 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
563 }
564 pp_verbatim (context->printer, ":");
565 pp_newline (context->printer);
566 }
567 }
568 }
569
570 void
571 default_diagnostic_starter (diagnostic_context *context,
572 diagnostic_info *diagnostic)
573 {
574 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
575 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
576 diagnostic));
577 }
578
579 void
580 default_diagnostic_start_span_fn (diagnostic_context *context,
581 expanded_location exploc)
582 {
583 pp_set_prefix (context->printer,
584 diagnostic_get_location_text (context, exploc));
585 pp_string (context->printer, "");
586 pp_newline (context->printer);
587 }
588
589 void
590 default_diagnostic_finalizer (diagnostic_context *context,
591 diagnostic_info *diagnostic)
592 {
593 diagnostic_show_locus (context, diagnostic->richloc, diagnostic->kind);
594 pp_destroy_prefix (context->printer);
595 pp_flush (context->printer);
596 }
597
598 /* Interface to specify diagnostic kind overrides. Returns the
599 previous setting, or DK_UNSPECIFIED if the parameters are out of
600 range. If OPTION_INDEX is zero, the new setting is for all the
601 diagnostics. */
602 diagnostic_t
603 diagnostic_classify_diagnostic (diagnostic_context *context,
604 int option_index,
605 diagnostic_t new_kind,
606 location_t where)
607 {
608 diagnostic_t old_kind;
609
610 if (option_index < 0
611 || option_index >= context->n_opts
612 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
613 return DK_UNSPECIFIED;
614
615 old_kind = context->classify_diagnostic[option_index];
616
617 /* Handle pragmas separately, since we need to keep track of *where*
618 the pragmas were. */
619 if (where != UNKNOWN_LOCATION)
620 {
621 int i;
622
623 /* Record the command-line status, so we can reset it back on DK_POP. */
624 if (old_kind == DK_UNSPECIFIED)
625 {
626 old_kind = !context->option_enabled (option_index,
627 context->option_state)
628 ? DK_IGNORED : (context->warning_as_error_requested
629 ? DK_ERROR : DK_WARNING);
630 context->classify_diagnostic[option_index] = old_kind;
631 }
632
633 for (i = context->n_classification_history - 1; i >= 0; i --)
634 if (context->classification_history[i].option == option_index)
635 {
636 old_kind = context->classification_history[i].kind;
637 break;
638 }
639
640 i = context->n_classification_history;
641 context->classification_history =
642 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
643 * sizeof (diagnostic_classification_change_t));
644 context->classification_history[i].location = where;
645 context->classification_history[i].option = option_index;
646 context->classification_history[i].kind = new_kind;
647 context->n_classification_history ++;
648 }
649 else
650 context->classify_diagnostic[option_index] = new_kind;
651
652 return old_kind;
653 }
654
655 /* Save all diagnostic classifications in a stack. */
656 void
657 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
658 {
659 context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
660 context->push_list[context->n_push ++] = context->n_classification_history;
661 }
662
663 /* Restore the topmost classification set off the stack. If the stack
664 is empty, revert to the state based on command line parameters. */
665 void
666 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
667 {
668 int jump_to;
669 int i;
670
671 if (context->n_push)
672 jump_to = context->push_list [-- context->n_push];
673 else
674 jump_to = 0;
675
676 i = context->n_classification_history;
677 context->classification_history =
678 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
679 * sizeof (diagnostic_classification_change_t));
680 context->classification_history[i].location = where;
681 context->classification_history[i].option = jump_to;
682 context->classification_history[i].kind = DK_POP;
683 context->n_classification_history ++;
684 }
685
686 /* Helper function for print_parseable_fixits. Print TEXT to PP, obeying the
687 escaping rules for -fdiagnostics-parseable-fixits. */
688
689 static void
690 print_escaped_string (pretty_printer *pp, const char *text)
691 {
692 gcc_assert (pp);
693 gcc_assert (text);
694
695 pp_character (pp, '"');
696 for (const char *ch = text; *ch; ch++)
697 {
698 switch (*ch)
699 {
700 case '\\':
701 /* Escape backslash as two backslashes. */
702 pp_string (pp, "\\\\");
703 break;
704 case '\t':
705 /* Escape tab as "\t". */
706 pp_string (pp, "\\t");
707 break;
708 case '\n':
709 /* Escape newline as "\n". */
710 pp_string (pp, "\\n");
711 break;
712 case '"':
713 /* Escape doublequotes as \". */
714 pp_string (pp, "\\\"");
715 break;
716 default:
717 if (ISPRINT (*ch))
718 pp_character (pp, *ch);
719 else
720 /* Use octal for non-printable chars. */
721 {
722 unsigned char c = (*ch & 0xff);
723 pp_printf (pp, "\\%o%o%o", (c / 64), (c / 8) & 007, c & 007);
724 }
725 break;
726 }
727 }
728 pp_character (pp, '"');
729 }
730
731 /* Implementation of -fdiagnostics-parseable-fixits. Print a
732 machine-parseable version of all fixits in RICHLOC to PP. */
733
734 static void
735 print_parseable_fixits (pretty_printer *pp, rich_location *richloc)
736 {
737 gcc_assert (pp);
738 gcc_assert (richloc);
739
740 for (unsigned i = 0; i < richloc->get_num_fixit_hints (); i++)
741 {
742 const fixit_hint *hint = richloc->get_fixit_hint (i);
743 source_location start_loc = hint->get_start_loc ();
744 expanded_location start_exploc = expand_location (start_loc);
745 pp_string (pp, "fix-it:");
746 print_escaped_string (pp, start_exploc.file);
747 source_location end_loc;
748
749 /* For compatibility with clang, print as a half-open range. */
750 if (hint->maybe_get_end_loc (&end_loc))
751 {
752 expanded_location end_exploc = expand_location (end_loc);
753 pp_printf (pp, ":{%i:%i-%i:%i}:",
754 start_exploc.line, start_exploc.column,
755 end_exploc.line, end_exploc.column + 1);
756 }
757 else
758 {
759 pp_printf (pp, ":{%i:%i-%i:%i}:",
760 start_exploc.line, start_exploc.column,
761 start_exploc.line, start_exploc.column);
762 }
763 switch (hint->get_kind ())
764 {
765 case fixit_hint::INSERT:
766 {
767 const fixit_insert *insert
768 = static_cast <const fixit_insert *> (hint);
769 print_escaped_string (pp, insert->get_string ());
770 }
771 break;
772
773 case fixit_hint::REPLACE:
774 {
775 const fixit_replace *replace
776 = static_cast <const fixit_replace *> (hint);
777 print_escaped_string (pp, replace->get_string ());
778 }
779 break;
780
781 default:
782 gcc_unreachable ();
783 }
784 pp_newline (pp);
785 }
786 }
787
788 /* Report a diagnostic message (an error or a warning) as specified by
789 DC. This function is *the* subroutine in terms of which front-ends
790 should implement their specific diagnostic handling modules. The
791 front-end independent format specifiers are exactly those described
792 in the documentation of output_format.
793 Return true if a diagnostic was printed, false otherwise. */
794
795 bool
796 diagnostic_report_diagnostic (diagnostic_context *context,
797 diagnostic_info *diagnostic)
798 {
799 location_t location = diagnostic_location (diagnostic);
800 diagnostic_t orig_diag_kind = diagnostic->kind;
801 const char *saved_format_spec;
802
803 /* Give preference to being able to inhibit warnings, before they
804 get reclassified to something else. */
805 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
806 && !diagnostic_report_warnings_p (context, location))
807 return false;
808
809 if (diagnostic->kind == DK_PEDWARN)
810 {
811 diagnostic->kind = pedantic_warning_kind (context);
812 /* We do this to avoid giving the message for -pedantic-errors. */
813 orig_diag_kind = diagnostic->kind;
814 }
815
816 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
817 return false;
818
819 if (context->lock > 0)
820 {
821 /* If we're reporting an ICE in the middle of some other error,
822 try to flush out the previous error, then let this one
823 through. Don't do this more than once. */
824 if ((diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
825 && context->lock == 1)
826 pp_newline_and_flush (context->printer);
827 else
828 error_recursion (context);
829 }
830
831 /* If the user requested that warnings be treated as errors, so be
832 it. Note that we do this before the next block so that
833 individual warnings can be overridden back to warnings with
834 -Wno-error=*. */
835 if (context->warning_as_error_requested
836 && diagnostic->kind == DK_WARNING)
837 diagnostic->kind = DK_ERROR;
838
839 if (diagnostic->option_index
840 && diagnostic->option_index != permissive_error_option (context))
841 {
842 diagnostic_t diag_class = DK_UNSPECIFIED;
843
844 /* This tests if the user provided the appropriate -Wfoo or
845 -Wno-foo option. */
846 if (! context->option_enabled (diagnostic->option_index,
847 context->option_state))
848 return false;
849
850 /* This tests for #pragma diagnostic changes. */
851 if (context->n_classification_history > 0)
852 {
853 /* FIXME: Stupid search. Optimize later. */
854 for (int i = context->n_classification_history - 1; i >= 0; i --)
855 {
856 if (linemap_location_before_p
857 (line_table,
858 context->classification_history[i].location,
859 location))
860 {
861 if (context->classification_history[i].kind == (int) DK_POP)
862 {
863 i = context->classification_history[i].option;
864 continue;
865 }
866 int option = context->classification_history[i].option;
867 /* The option 0 is for all the diagnostics. */
868 if (option == 0 || option == diagnostic->option_index)
869 {
870 diag_class = context->classification_history[i].kind;
871 if (diag_class != DK_UNSPECIFIED)
872 diagnostic->kind = diag_class;
873 break;
874 }
875 }
876 }
877 }
878
879 /* This tests if the user provided the appropriate -Werror=foo
880 option. */
881 if (diag_class == DK_UNSPECIFIED
882 && (context->classify_diagnostic[diagnostic->option_index]
883 != DK_UNSPECIFIED))
884 diagnostic->kind
885 = context->classify_diagnostic[diagnostic->option_index];
886
887 /* This allows for future extensions, like temporarily disabling
888 warnings for ranges of source code. */
889 if (diagnostic->kind == DK_IGNORED)
890 return false;
891 }
892
893 context->lock++;
894
895 if (diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
896 {
897 /* When not checking, ICEs are converted to fatal errors when an
898 error has already occurred. This is counteracted by
899 abort_on_error. */
900 if (!CHECKING_P
901 && (diagnostic_kind_count (context, DK_ERROR) > 0
902 || diagnostic_kind_count (context, DK_SORRY) > 0)
903 && !context->abort_on_error)
904 {
905 expanded_location s
906 = expand_location (diagnostic_location (diagnostic));
907 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
908 s.file, s.line);
909 exit (ICE_EXIT_CODE);
910 }
911 if (context->internal_error)
912 (*context->internal_error) (context,
913 diagnostic->message.format_spec,
914 diagnostic->message.args_ptr);
915 }
916 if (diagnostic->kind == DK_ERROR && orig_diag_kind == DK_WARNING)
917 ++diagnostic_kind_count (context, DK_WERROR);
918 else
919 ++diagnostic_kind_count (context, diagnostic->kind);
920
921 saved_format_spec = diagnostic->message.format_spec;
922 if (context->show_option_requested)
923 {
924 char *option_text;
925
926 option_text = context->option_name (context, diagnostic->option_index,
927 orig_diag_kind, diagnostic->kind);
928
929 if (option_text)
930 {
931 const char *cs
932 = colorize_start (pp_show_color (context->printer),
933 diagnostic_kind_color[diagnostic->kind]);
934 const char *ce = colorize_stop (pp_show_color (context->printer));
935 diagnostic->message.format_spec
936 = ACONCAT ((diagnostic->message.format_spec,
937 " ",
938 "[", cs, option_text, ce, "]",
939 NULL));
940 free (option_text);
941 }
942 }
943 diagnostic->message.x_data = &diagnostic->x_data;
944 diagnostic->x_data = NULL;
945 pp_format (context->printer, &diagnostic->message);
946 (*diagnostic_starter (context)) (context, diagnostic);
947 pp_output_formatted_text (context->printer);
948 (*diagnostic_finalizer (context)) (context, diagnostic);
949 if (context->parseable_fixits_p)
950 {
951 print_parseable_fixits (context->printer, diagnostic->richloc);
952 pp_flush (context->printer);
953 }
954 diagnostic_action_after_output (context, diagnostic->kind);
955 diagnostic->message.format_spec = saved_format_spec;
956 diagnostic->x_data = NULL;
957
958 if (context->edit_context_ptr)
959 context->edit_context_ptr->add_fixits (diagnostic->richloc);
960
961 context->lock--;
962
963 return true;
964 }
965
966 /* Given a partial pathname as input, return another pathname that
967 shares no directory elements with the pathname of __FILE__. This
968 is used by fancy_abort() to print `Internal compiler error in expr.c'
969 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
970
971 const char *
972 trim_filename (const char *name)
973 {
974 static const char this_file[] = __FILE__;
975 const char *p = name, *q = this_file;
976
977 /* First skip any "../" in each filename. This allows us to give a proper
978 reference to a file in a subdirectory. */
979 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
980 p += 3;
981
982 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
983 q += 3;
984
985 /* Now skip any parts the two filenames have in common. */
986 while (*p == *q && *p != 0 && *q != 0)
987 p++, q++;
988
989 /* Now go backwards until the previous directory separator. */
990 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
991 p--;
992
993 return p;
994 }
995 \f
996 /* Standard error reporting routines in increasing order of severity.
997 All of these take arguments like printf. */
998
999 /* Text to be emitted verbatim to the error message stream; this
1000 produces no prefix and disables line-wrapping. Use rarely. */
1001 void
1002 verbatim (const char *gmsgid, ...)
1003 {
1004 text_info text;
1005 va_list ap;
1006
1007 va_start (ap, gmsgid);
1008 text.err_no = errno;
1009 text.args_ptr = &ap;
1010 text.format_spec = _(gmsgid);
1011 text.x_data = NULL;
1012 pp_format_verbatim (global_dc->printer, &text);
1013 pp_newline_and_flush (global_dc->printer);
1014 va_end (ap);
1015 }
1016
1017 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
1018 void
1019 diagnostic_append_note (diagnostic_context *context,
1020 location_t location,
1021 const char * gmsgid, ...)
1022 {
1023 diagnostic_info diagnostic;
1024 va_list ap;
1025 const char *saved_prefix;
1026 rich_location richloc (line_table, location);
1027
1028 va_start (ap, gmsgid);
1029 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_NOTE);
1030 if (context->inhibit_notes_p)
1031 {
1032 va_end (ap);
1033 return;
1034 }
1035 saved_prefix = pp_get_prefix (context->printer);
1036 pp_set_prefix (context->printer,
1037 diagnostic_build_prefix (context, &diagnostic));
1038 pp_format (context->printer, &diagnostic.message);
1039 pp_output_formatted_text (context->printer);
1040 pp_destroy_prefix (context->printer);
1041 pp_set_prefix (context->printer, saved_prefix);
1042 diagnostic_show_locus (context, &richloc, DK_NOTE);
1043 va_end (ap);
1044 }
1045
1046 /* Implement emit_diagnostic, inform, inform_at_rich_loc, warning, warning_at,
1047 warning_at_rich_loc, pedwarn, permerror, permerror_at_rich_loc, error,
1048 error_at, error_at_rich_loc, sorry, fatal_error, internal_error, and
1049 internal_error_no_backtrace, as documented and defined below. */
1050 static bool
1051 diagnostic_impl (rich_location *richloc, int opt,
1052 const char *gmsgid,
1053 va_list *ap, diagnostic_t kind)
1054 {
1055 diagnostic_info diagnostic;
1056 if (kind == DK_PERMERROR)
1057 {
1058 diagnostic_set_info (&diagnostic, gmsgid, ap, richloc,
1059 permissive_error_kind (global_dc));
1060 diagnostic.option_index = permissive_error_option (global_dc);
1061 }
1062 else
1063 {
1064 diagnostic_set_info (&diagnostic, gmsgid, ap, richloc, kind);
1065 if (kind == DK_WARNING || kind == DK_PEDWARN)
1066 diagnostic.option_index = opt;
1067 }
1068 return report_diagnostic (&diagnostic);
1069 }
1070
1071 /* Same as diagonostic_n_impl taking rich_location instead of location_t. */
1072 static bool
1073 diagnostic_n_impl_richloc (rich_location *richloc, int opt, int n,
1074 const char *singular_gmsgid,
1075 const char *plural_gmsgid,
1076 va_list *ap, diagnostic_t kind)
1077 {
1078 diagnostic_info diagnostic;
1079 diagnostic_set_info_translated (&diagnostic,
1080 ngettext (singular_gmsgid, plural_gmsgid, n),
1081 ap, richloc, kind);
1082 if (kind == DK_WARNING)
1083 diagnostic.option_index = opt;
1084 return report_diagnostic (&diagnostic);
1085 }
1086
1087 /* Implement inform_n, warning_n, and error_n, as documented and
1088 defined below. */
1089 static bool
1090 diagnostic_n_impl (location_t location, int opt, int n,
1091 const char *singular_gmsgid,
1092 const char *plural_gmsgid,
1093 va_list *ap, diagnostic_t kind)
1094 {
1095 rich_location richloc (line_table, location);
1096 return diagnostic_n_impl_richloc (&richloc, opt, n,
1097 singular_gmsgid, plural_gmsgid, ap, kind);
1098 }
1099
1100 bool
1101 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
1102 const char *gmsgid, ...)
1103 {
1104 va_list ap;
1105 va_start (ap, gmsgid);
1106 rich_location richloc (line_table, location);
1107 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, kind);
1108 va_end (ap);
1109 return ret;
1110 }
1111
1112 /* An informative note at LOCATION. Use this for additional details on an error
1113 message. */
1114 void
1115 inform (location_t location, const char *gmsgid, ...)
1116 {
1117 va_list ap;
1118 va_start (ap, gmsgid);
1119 rich_location richloc (line_table, location);
1120 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_NOTE);
1121 va_end (ap);
1122 }
1123
1124 /* Same as "inform", but at RICHLOC. */
1125 void
1126 inform_at_rich_loc (rich_location *richloc, const char *gmsgid, ...)
1127 {
1128 va_list ap;
1129 va_start (ap, gmsgid);
1130 diagnostic_impl (richloc, -1, gmsgid, &ap, DK_NOTE);
1131 va_end (ap);
1132 }
1133
1134 /* An informative note at LOCATION. Use this for additional details on an
1135 error message. */
1136 void
1137 inform_n (location_t location, int n, const char *singular_gmsgid,
1138 const char *plural_gmsgid, ...)
1139 {
1140 va_list ap;
1141 va_start (ap, plural_gmsgid);
1142 diagnostic_n_impl (location, -1, n, singular_gmsgid, plural_gmsgid,
1143 &ap, DK_NOTE);
1144 va_end (ap);
1145 }
1146
1147 /* A warning at INPUT_LOCATION. Use this for code which is correct according
1148 to the relevant language specification but is likely to be buggy anyway.
1149 Returns true if the warning was printed, false if it was inhibited. */
1150 bool
1151 warning (int opt, const char *gmsgid, ...)
1152 {
1153 va_list ap;
1154 va_start (ap, gmsgid);
1155 rich_location richloc (line_table, input_location);
1156 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_WARNING);
1157 va_end (ap);
1158 return ret;
1159 }
1160
1161 /* A warning at LOCATION. Use this for code which is correct according to the
1162 relevant language specification but is likely to be buggy anyway.
1163 Returns true if the warning was printed, false if it was inhibited. */
1164
1165 bool
1166 warning_at (location_t location, int opt, const char *gmsgid, ...)
1167 {
1168 va_list ap;
1169 va_start (ap, gmsgid);
1170 rich_location richloc (line_table, location);
1171 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_WARNING);
1172 va_end (ap);
1173 return ret;
1174 }
1175
1176 /* Same as warning at, but using RICHLOC. */
1177
1178 bool
1179 warning_at_rich_loc (rich_location *richloc, int opt, const char *gmsgid, ...)
1180 {
1181 va_list ap;
1182 va_start (ap, gmsgid);
1183 bool ret = diagnostic_impl (richloc, opt, gmsgid, &ap, DK_WARNING);
1184 va_end (ap);
1185 return ret;
1186 }
1187
1188 /* Same as warning_at_rich_loc but for plural variant. */
1189
1190 bool
1191 warning_at_rich_loc_n (rich_location *richloc, int opt, int n,
1192 const char *singular_gmsgid, const char *plural_gmsgid, ...)
1193 {
1194 va_list ap;
1195 va_start (ap, plural_gmsgid);
1196 bool ret = diagnostic_n_impl_richloc (richloc, opt, n,
1197 singular_gmsgid, plural_gmsgid,
1198 &ap, DK_WARNING);
1199 va_end (ap);
1200 return ret;
1201 }
1202
1203 /* A warning at LOCATION. Use this for code which is correct according to the
1204 relevant language specification but is likely to be buggy anyway.
1205 Returns true if the warning was printed, false if it was inhibited. */
1206
1207 bool
1208 warning_n (location_t location, int opt, int n, const char *singular_gmsgid,
1209 const char *plural_gmsgid, ...)
1210 {
1211 va_list ap;
1212 va_start (ap, plural_gmsgid);
1213 bool ret = diagnostic_n_impl (location, opt, n,
1214 singular_gmsgid, plural_gmsgid,
1215 &ap, DK_WARNING);
1216 va_end (ap);
1217 return ret;
1218 }
1219
1220 /* A "pedantic" warning at LOCATION: issues a warning unless
1221 -pedantic-errors was given on the command line, in which case it
1222 issues an error. Use this for diagnostics required by the relevant
1223 language standard, if you have chosen not to make them errors.
1224
1225 Note that these diagnostics are issued independent of the setting
1226 of the -Wpedantic command-line switch. To get a warning enabled
1227 only with that switch, use either "if (pedantic) pedwarn
1228 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
1229 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
1230
1231 Returns true if the warning was printed, false if it was inhibited. */
1232
1233 bool
1234 pedwarn (location_t location, int opt, const char *gmsgid, ...)
1235 {
1236 va_list ap;
1237 va_start (ap, gmsgid);
1238 rich_location richloc (line_table, location);
1239 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_PEDWARN);
1240 va_end (ap);
1241 return ret;
1242 }
1243
1244 /* Same as pedwarn, but using RICHLOC. */
1245
1246 bool
1247 pedwarn_at_rich_loc (rich_location *richloc, int opt, const char *gmsgid, ...)
1248 {
1249 va_list ap;
1250 va_start (ap, gmsgid);
1251 bool ret = diagnostic_impl (richloc, opt, gmsgid, &ap, DK_PEDWARN);
1252 va_end (ap);
1253 return ret;
1254 }
1255
1256 /* A "permissive" error at LOCATION: issues an error unless
1257 -fpermissive was given on the command line, in which case it issues
1258 a warning. Use this for things that really should be errors but we
1259 want to support legacy code.
1260
1261 Returns true if the warning was printed, false if it was inhibited. */
1262
1263 bool
1264 permerror (location_t location, const char *gmsgid, ...)
1265 {
1266 va_list ap;
1267 va_start (ap, gmsgid);
1268 rich_location richloc (line_table, location);
1269 bool ret = diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_PERMERROR);
1270 va_end (ap);
1271 return ret;
1272 }
1273
1274 /* Same as "permerror", but at RICHLOC. */
1275
1276 bool
1277 permerror_at_rich_loc (rich_location *richloc, const char *gmsgid, ...)
1278 {
1279 va_list ap;
1280 va_start (ap, gmsgid);
1281 bool ret = diagnostic_impl (richloc, -1, gmsgid, &ap, DK_PERMERROR);
1282 va_end (ap);
1283 return ret;
1284 }
1285
1286 /* A hard error: the code is definitely ill-formed, and an object file
1287 will not be produced. */
1288 void
1289 error (const char *gmsgid, ...)
1290 {
1291 va_list ap;
1292 va_start (ap, gmsgid);
1293 rich_location richloc (line_table, input_location);
1294 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ERROR);
1295 va_end (ap);
1296 }
1297
1298 /* A hard error: the code is definitely ill-formed, and an object file
1299 will not be produced. */
1300 void
1301 error_n (location_t location, int n, const char *singular_gmsgid,
1302 const char *plural_gmsgid, ...)
1303 {
1304 va_list ap;
1305 va_start (ap, plural_gmsgid);
1306 diagnostic_n_impl (location, -1, n, singular_gmsgid, plural_gmsgid,
1307 &ap, DK_ERROR);
1308 va_end (ap);
1309 }
1310
1311 /* Same as above, but use location LOC instead of input_location. */
1312 void
1313 error_at (location_t loc, const char *gmsgid, ...)
1314 {
1315 va_list ap;
1316 va_start (ap, gmsgid);
1317 rich_location richloc (line_table, loc);
1318 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ERROR);
1319 va_end (ap);
1320 }
1321
1322 /* Same as above, but use RICH_LOC. */
1323
1324 void
1325 error_at_rich_loc (rich_location *richloc, const char *gmsgid, ...)
1326 {
1327 va_list ap;
1328 va_start (ap, gmsgid);
1329 diagnostic_impl (richloc, -1, gmsgid, &ap, DK_ERROR);
1330 va_end (ap);
1331 }
1332
1333 /* "Sorry, not implemented." Use for a language feature which is
1334 required by the relevant specification but not implemented by GCC.
1335 An object file will not be produced. */
1336 void
1337 sorry (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_SORRY);
1343 va_end (ap);
1344 }
1345
1346 /* Return true if an error or a "sorry" has been seen. Various
1347 processing is disabled after errors. */
1348 bool
1349 seen_error (void)
1350 {
1351 return errorcount || sorrycount;
1352 }
1353
1354 /* An error which is severe enough that we make no attempt to
1355 continue. Do not use this for internal consistency checks; that's
1356 internal_error. Use of this function should be rare. */
1357 void
1358 fatal_error (location_t loc, const char *gmsgid, ...)
1359 {
1360 va_list ap;
1361 va_start (ap, gmsgid);
1362 rich_location richloc (line_table, loc);
1363 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_FATAL);
1364 va_end (ap);
1365
1366 gcc_unreachable ();
1367 }
1368
1369 /* An internal consistency check has failed. We make no attempt to
1370 continue. Note that unless there is debugging value to be had from
1371 a more specific message, or some other good reason, you should use
1372 abort () instead of calling this function directly. */
1373 void
1374 internal_error (const char *gmsgid, ...)
1375 {
1376 va_list ap;
1377 va_start (ap, gmsgid);
1378 rich_location richloc (line_table, input_location);
1379 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ICE);
1380 va_end (ap);
1381
1382 gcc_unreachable ();
1383 }
1384
1385 /* Like internal_error, but no backtrace will be printed. Used when
1386 the internal error does not happen at the current location, but happened
1387 somewhere else. */
1388 void
1389 internal_error_no_backtrace (const char *gmsgid, ...)
1390 {
1391 va_list ap;
1392 va_start (ap, gmsgid);
1393 rich_location richloc (line_table, input_location);
1394 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ICE_NOBT);
1395 va_end (ap);
1396
1397 gcc_unreachable ();
1398 }
1399 \f
1400 /* Special case error functions. Most are implemented in terms of the
1401 above, or should be. */
1402
1403 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1404 runs its second argument through gettext. */
1405 void
1406 fnotice (FILE *file, const char *cmsgid, ...)
1407 {
1408 va_list ap;
1409
1410 va_start (ap, cmsgid);
1411 vfprintf (file, _(cmsgid), ap);
1412 va_end (ap);
1413 }
1414
1415 /* Inform the user that an error occurred while trying to report some
1416 other error. This indicates catastrophic internal inconsistencies,
1417 so give up now. But do try to flush out the previous error.
1418 This mustn't use internal_error, that will cause infinite recursion. */
1419
1420 static void
1421 error_recursion (diagnostic_context *context)
1422 {
1423 if (context->lock < 3)
1424 pp_newline_and_flush (context->printer);
1425
1426 fnotice (stderr,
1427 "Internal compiler error: Error reporting routines re-entered.\n");
1428
1429 /* Call diagnostic_action_after_output to get the "please submit a bug
1430 report" message. */
1431 diagnostic_action_after_output (context, DK_ICE);
1432
1433 /* Do not use gcc_unreachable here; that goes through internal_error
1434 and therefore would cause infinite recursion. */
1435 real_abort ();
1436 }
1437
1438 /* Report an internal compiler error in a friendly manner. This is
1439 the function that gets called upon use of abort() in the source
1440 code generally, thanks to a special macro. */
1441
1442 void
1443 fancy_abort (const char *file, int line, const char *function)
1444 {
1445 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1446 }
1447
1448 /* Really call the system 'abort'. This has to go right at the end of
1449 this file, so that there are no functions after it that call abort
1450 and get the system abort instead of our macro. */
1451 #undef abort
1452 static void
1453 real_abort (void)
1454 {
1455 abort ();
1456 }
1457
1458 #if CHECKING_P
1459
1460 namespace selftest {
1461
1462 /* Helper function for test_print_escaped_string. */
1463
1464 static void
1465 assert_print_escaped_string (const location &loc, const char *expected_output,
1466 const char *input)
1467 {
1468 pretty_printer pp;
1469 print_escaped_string (&pp, input);
1470 ASSERT_STREQ_AT (loc, expected_output, pp_formatted_text (&pp));
1471 }
1472
1473 #define ASSERT_PRINT_ESCAPED_STRING_STREQ(EXPECTED_OUTPUT, INPUT) \
1474 assert_print_escaped_string (SELFTEST_LOCATION, EXPECTED_OUTPUT, INPUT)
1475
1476 /* Tests of print_escaped_string. */
1477
1478 static void
1479 test_print_escaped_string ()
1480 {
1481 /* Empty string. */
1482 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"\"", "");
1483
1484 /* Non-empty string. */
1485 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"hello world\"", "hello world");
1486
1487 /* Various things that need to be escaped: */
1488 /* Backslash. */
1489 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\\after\"",
1490 "before\\after");
1491 /* Tab. */
1492 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\tafter\"",
1493 "before\tafter");
1494 /* Newline. */
1495 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\nafter\"",
1496 "before\nafter");
1497 /* Double quote. */
1498 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\"after\"",
1499 "before\"after");
1500
1501 /* Non-printable characters: BEL: '\a': 0x07 */
1502 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\007after\"",
1503 "before\aafter");
1504 /* Non-printable characters: vertical tab: '\v': 0x0b */
1505 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\013after\"",
1506 "before\vafter");
1507 }
1508
1509 /* Tests of print_parseable_fixits. */
1510
1511 /* Verify that print_parseable_fixits emits the empty string if there
1512 are no fixits. */
1513
1514 static void
1515 test_print_parseable_fixits_none ()
1516 {
1517 pretty_printer pp;
1518 rich_location richloc (line_table, UNKNOWN_LOCATION);
1519
1520 print_parseable_fixits (&pp, &richloc);
1521 ASSERT_STREQ ("", pp_formatted_text (&pp));
1522 }
1523
1524 /* Verify that print_parseable_fixits does the right thing if there
1525 is an insertion fixit hint. */
1526
1527 static void
1528 test_print_parseable_fixits_insert ()
1529 {
1530 pretty_printer pp;
1531 rich_location richloc (line_table, UNKNOWN_LOCATION);
1532
1533 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
1534 linemap_line_start (line_table, 5, 100);
1535 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1536 location_t where = linemap_position_for_column (line_table, 10);
1537 richloc.add_fixit_insert_before (where, "added content");
1538
1539 print_parseable_fixits (&pp, &richloc);
1540 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:10}:\"added content\"\n",
1541 pp_formatted_text (&pp));
1542 }
1543
1544 /* Verify that print_parseable_fixits does the right thing if there
1545 is an removal fixit hint. */
1546
1547 static void
1548 test_print_parseable_fixits_remove ()
1549 {
1550 pretty_printer pp;
1551 rich_location richloc (line_table, UNKNOWN_LOCATION);
1552
1553 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
1554 linemap_line_start (line_table, 5, 100);
1555 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1556 source_range where;
1557 where.m_start = linemap_position_for_column (line_table, 10);
1558 where.m_finish = linemap_position_for_column (line_table, 20);
1559 richloc.add_fixit_remove (where);
1560
1561 print_parseable_fixits (&pp, &richloc);
1562 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"\"\n",
1563 pp_formatted_text (&pp));
1564 }
1565
1566 /* Verify that print_parseable_fixits does the right thing if there
1567 is an replacement fixit hint. */
1568
1569 static void
1570 test_print_parseable_fixits_replace ()
1571 {
1572 pretty_printer pp;
1573 rich_location richloc (line_table, UNKNOWN_LOCATION);
1574
1575 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
1576 linemap_line_start (line_table, 5, 100);
1577 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1578 source_range where;
1579 where.m_start = linemap_position_for_column (line_table, 10);
1580 where.m_finish = linemap_position_for_column (line_table, 20);
1581 richloc.add_fixit_replace (where, "replacement");
1582
1583 print_parseable_fixits (&pp, &richloc);
1584 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"replacement\"\n",
1585 pp_formatted_text (&pp));
1586 }
1587
1588 /* Run all of the selftests within this file. */
1589
1590 void
1591 diagnostic_c_tests ()
1592 {
1593 test_print_escaped_string ();
1594 test_print_parseable_fixits_none ();
1595 test_print_parseable_fixits_insert ();
1596 test_print_parseable_fixits_remove ();
1597 test_print_parseable_fixits_replace ();
1598 }
1599
1600 } // namespace selftest
1601
1602 #endif /* #if CHECKING_P */