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