* c-ubsan.c (ubsan_instrument_shift): Use type0.
[official-gcc.git] / gcc / diagnostic.c
blob8b70a16ad098e5a58705fd8a827ba95283652fb5
1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999-2015 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file implements the language independent aspect of diagnostic
23 message module. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "version.h"
29 #include "demangle.h"
30 #include "input.h"
31 #include "intl.h"
32 #include "backtrace.h"
33 #include "diagnostic.h"
34 #include "diagnostic-color.h"
36 #ifdef HAVE_TERMIOS_H
37 # include <termios.h>
38 #endif
40 #ifdef GWINSZ_IN_SYS_IOCTL
41 # include <sys/ioctl.h>
42 #endif
44 #include <new> // For placement new.
46 #define pedantic_warning_kind(DC) \
47 ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
48 #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR)
49 #define permissive_error_option(DC) ((DC)->opt_permissive)
51 /* Prototypes. */
52 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
54 static void real_abort (void) ATTRIBUTE_NORETURN;
56 /* Name of program invoked, sans directories. */
58 const char *progname;
60 /* A diagnostic_context surrogate for stderr. */
61 static diagnostic_context global_diagnostic_context;
62 diagnostic_context *global_dc = &global_diagnostic_context;
64 /* Return a malloc'd string containing MSG formatted a la printf. The
65 caller is responsible for freeing the memory. */
66 char *
67 build_message_string (const char *msg, ...)
69 char *str;
70 va_list ap;
72 va_start (ap, msg);
73 str = xvasprintf (msg, ap);
74 va_end (ap);
76 return str;
79 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
80 char *
81 file_name_as_prefix (diagnostic_context *context, const char *f)
83 const char *locus_cs
84 = colorize_start (pp_show_color (context->printer), "locus");
85 const char *locus_ce = colorize_stop (pp_show_color (context->printer));
86 return build_message_string ("%s%s:%s ", locus_cs, f, locus_ce);
91 /* Return the value of the getenv("COLUMNS") as an integer. If the
92 value is not set to a positive integer, use ioctl to get the
93 terminal width. If it fails, return INT_MAX. */
94 int
95 get_terminal_width (void)
97 const char * s = getenv ("COLUMNS");
98 if (s != NULL) {
99 int n = atoi (s);
100 if (n > 0)
101 return n;
104 #ifdef TIOCGWINSZ
105 struct winsize w;
106 w.ws_col = 0;
107 if (ioctl (0, TIOCGWINSZ, &w) == 0 && w.ws_col > 0)
108 return w.ws_col;
109 #endif
111 return INT_MAX;
114 /* Set caret_max_width to value. */
115 void
116 diagnostic_set_caret_max_width (diagnostic_context *context, int value)
118 /* One minus to account for the leading empty space. */
119 value = value ? value - 1
120 : (isatty (fileno (pp_buffer (context->printer)->stream))
121 ? get_terminal_width () - 1: INT_MAX);
123 if (value <= 0)
124 value = INT_MAX;
126 context->caret_max_width = value;
129 /* Initialize the diagnostic message outputting machinery. */
130 void
131 diagnostic_initialize (diagnostic_context *context, int n_opts)
133 int i;
135 /* Allocate a basic pretty-printer. Clients will replace this a
136 much more elaborated pretty-printer if they wish. */
137 context->printer = XNEW (pretty_printer);
138 new (context->printer) pretty_printer ();
140 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
141 context->some_warnings_are_errors = false;
142 context->warning_as_error_requested = false;
143 context->n_opts = n_opts;
144 context->classify_diagnostic = XNEWVEC (diagnostic_t, n_opts);
145 for (i = 0; i < n_opts; i++)
146 context->classify_diagnostic[i] = DK_UNSPECIFIED;
147 context->show_caret = false;
148 diagnostic_set_caret_max_width (context, pp_line_cutoff (context->printer));
149 for (i = 0; i < MAX_LOCATIONS_PER_MESSAGE; i++)
150 context->caret_chars[i] = '^';
151 context->show_option_requested = false;
152 context->abort_on_error = false;
153 context->show_column = false;
154 context->pedantic_errors = false;
155 context->permissive = false;
156 context->opt_permissive = 0;
157 context->fatal_errors = false;
158 context->dc_inhibit_warnings = false;
159 context->dc_warn_system_headers = false;
160 context->max_errors = 0;
161 context->internal_error = NULL;
162 diagnostic_starter (context) = default_diagnostic_starter;
163 diagnostic_finalizer (context) = default_diagnostic_finalizer;
164 context->option_enabled = NULL;
165 context->option_state = NULL;
166 context->option_name = NULL;
167 context->last_location = UNKNOWN_LOCATION;
168 context->last_module = 0;
169 context->x_data = NULL;
170 context->lock = 0;
171 context->inhibit_notes_p = false;
174 /* Maybe initialize the color support. We require clients to do this
175 explicitly, since most clients don't want color. When called
176 without a VALUE, it initializes with DIAGNOSTICS_COLOR_DEFAULT. */
178 void
179 diagnostic_color_init (diagnostic_context *context, int value /*= -1 */)
181 /* value == -1 is the default value. */
182 if (value < 0)
184 /* If DIAGNOSTICS_COLOR_DEFAULT is -1, default to
185 -fdiagnostics-color=auto if GCC_COLORS is in the environment,
186 otherwise default to -fdiagnostics-color=never, for other
187 values default to that
188 -fdiagnostics-color={never,auto,always}. */
189 if (DIAGNOSTICS_COLOR_DEFAULT == -1)
191 if (!getenv ("GCC_COLORS"))
192 return;
193 value = DIAGNOSTICS_COLOR_AUTO;
195 else
196 value = DIAGNOSTICS_COLOR_DEFAULT;
198 pp_show_color (context->printer)
199 = colorize_init ((diagnostic_color_rule_t) value);
202 /* Do any cleaning up required after the last diagnostic is emitted. */
204 void
205 diagnostic_finish (diagnostic_context *context)
207 /* Some of the errors may actually have been warnings. */
208 if (context->some_warnings_are_errors)
210 /* -Werror was given. */
211 if (context->warning_as_error_requested)
212 pp_verbatim (context->printer,
213 _("%s: all warnings being treated as errors"),
214 progname);
215 /* At least one -Werror= was given. */
216 else
217 pp_verbatim (context->printer,
218 _("%s: some warnings being treated as errors"),
219 progname);
220 pp_newline_and_flush (context->printer);
223 diagnostic_file_cache_fini ();
225 XDELETEVEC (context->classify_diagnostic);
226 context->classify_diagnostic = NULL;
228 /* diagnostic_initialize allocates context->printer using XNEW
229 and placement-new. */
230 context->printer->~pretty_printer ();
231 XDELETE (context->printer);
232 context->printer = NULL;
235 /* Initialize DIAGNOSTIC, where the message MSG has already been
236 translated. */
237 void
238 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
239 va_list *args, location_t location,
240 diagnostic_t kind)
242 diagnostic->message.err_no = errno;
243 diagnostic->message.args_ptr = args;
244 diagnostic->message.format_spec = msg;
245 diagnostic->message.set_location (0, location);
246 for (int i = 1; i < MAX_LOCATIONS_PER_MESSAGE; i++)
247 diagnostic->message.set_location (i, UNKNOWN_LOCATION);
248 diagnostic->override_column = 0;
249 diagnostic->kind = kind;
250 diagnostic->option_index = 0;
253 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
254 translated. */
255 void
256 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
257 va_list *args, location_t location,
258 diagnostic_t kind)
260 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
263 /* Return a malloc'd string describing a location. The caller is
264 responsible for freeing the memory. */
265 char *
266 diagnostic_build_prefix (diagnostic_context *context,
267 const diagnostic_info *diagnostic)
269 static const char *const diagnostic_kind_text[] = {
270 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
271 #include "diagnostic.def"
272 #undef DEFINE_DIAGNOSTIC_KIND
273 "must-not-happen"
275 static const char *const diagnostic_kind_color[] = {
276 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
277 #include "diagnostic.def"
278 #undef DEFINE_DIAGNOSTIC_KIND
279 NULL
281 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
283 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
284 const char *text_cs = "", *text_ce = "";
285 const char *locus_cs, *locus_ce;
286 pretty_printer *pp = context->printer;
288 if (diagnostic_kind_color[diagnostic->kind])
290 text_cs = colorize_start (pp_show_color (pp),
291 diagnostic_kind_color[diagnostic->kind]);
292 text_ce = colorize_stop (pp_show_color (pp));
294 locus_cs = colorize_start (pp_show_color (pp), "locus");
295 locus_ce = colorize_stop (pp_show_color (pp));
297 expanded_location s = diagnostic_expand_location (diagnostic);
298 return
299 (s.file == NULL
300 ? build_message_string ("%s%s:%s %s%s%s", locus_cs, progname, locus_ce,
301 text_cs, text, text_ce)
302 : !strcmp (s.file, N_("<built-in>"))
303 ? build_message_string ("%s%s:%s %s%s%s", locus_cs, s.file, locus_ce,
304 text_cs, text, text_ce)
305 : context->show_column
306 ? build_message_string ("%s%s:%d:%d:%s %s%s%s", locus_cs, s.file, s.line,
307 s.column, locus_ce, text_cs, text, text_ce)
308 : build_message_string ("%s%s:%d:%s %s%s%s", locus_cs, s.file, s.line,
309 locus_ce, text_cs, text, text_ce));
312 /* If LINE is longer than MAX_WIDTH, and COLUMN is not smaller than
313 MAX_WIDTH by some margin, then adjust the start of the line such
314 that the COLUMN is smaller than MAX_WIDTH minus the margin. The
315 margin is either CARET_LINE_MARGIN characters or the difference
316 between the column and the length of the line, whatever is smaller.
317 The length of LINE is given by LINE_WIDTH. */
318 static const char *
319 adjust_line (const char *line, int line_width,
320 int max_width, int *column_p)
322 int right_margin = CARET_LINE_MARGIN;
323 int column = *column_p;
325 gcc_checking_assert (line_width >= column);
326 right_margin = MIN (line_width - column, right_margin);
327 right_margin = max_width - right_margin;
328 if (line_width >= max_width && column > right_margin)
330 line += column - right_margin;
331 *column_p = right_margin;
333 return line;
336 /* Print the physical source line corresponding to the location of
337 this diagnostic, and a caret indicating the precise column. This
338 function only prints two caret characters if the two locations
339 given by DIAGNOSTIC are on the same line according to
340 diagnostic_same_line(). */
341 void
342 diagnostic_show_locus (diagnostic_context * context,
343 const diagnostic_info *diagnostic)
345 if (!context->show_caret
346 || diagnostic_location (diagnostic, 0) <= BUILTINS_LOCATION
347 || diagnostic_location (diagnostic, 0) == context->last_location)
348 return;
350 context->last_location = diagnostic_location (diagnostic, 0);
351 expanded_location s0 = diagnostic_expand_location (diagnostic, 0);
352 expanded_location s1 = { };
353 /* Zero-initialized. This is checked later by diagnostic_print_caret_line. */
355 if (diagnostic_location (diagnostic, 1) > BUILTINS_LOCATION)
356 s1 = diagnostic_expand_location (diagnostic, 1);
358 diagnostic_print_caret_line (context, s0, s1,
359 context->caret_chars[0],
360 context->caret_chars[1]);
363 /* Print (part) of the source line given by xloc1 with caret1 pointing
364 at the column. If xloc2.column != 0 and it fits within the same
365 line as xloc1 according to diagnostic_same_line (), then caret2 is
366 printed at xloc2.colum. Otherwise, the caller has to set up things
367 to print a second caret line for xloc2. */
368 void
369 diagnostic_print_caret_line (diagnostic_context * context,
370 expanded_location xloc1,
371 expanded_location xloc2,
372 char caret1, char caret2)
374 if (!diagnostic_same_line (context, xloc1, xloc2))
375 /* This will mean ignore xloc2. */
376 xloc2.column = 0;
377 else if (xloc1.column == xloc2.column)
378 xloc2.column++;
380 int cmax = MAX (xloc1.column, xloc2.column);
381 int line_width;
382 const char *line = location_get_source_line (xloc1, &line_width);
383 if (line == NULL || cmax > line_width)
384 return;
386 /* Center the interesting part of the source line to fit in
387 max_width, and adjust all columns accordingly. */
388 int max_width = context->caret_max_width;
389 int offset = (int) cmax;
390 line = adjust_line (line, line_width, max_width, &offset);
391 offset -= cmax;
392 cmax += offset;
393 xloc1.column += offset;
394 if (xloc2.column)
395 xloc2.column += offset;
397 /* Print the source line. */
398 pp_newline (context->printer);
399 const char *saved_prefix = pp_get_prefix (context->printer);
400 pp_set_prefix (context->printer, NULL);
401 pp_space (context->printer);
402 while (max_width > 0 && line_width > 0)
404 char c = *line == '\t' ? ' ' : *line;
405 if (c == '\0')
406 c = ' ';
407 pp_character (context->printer, c);
408 max_width--;
409 line_width--;
410 line++;
412 pp_newline (context->printer);
414 /* Print the caret under the line. */
415 const char *caret_cs, *caret_ce;
416 caret_cs = colorize_start (pp_show_color (context->printer), "caret");
417 caret_ce = colorize_stop (pp_show_color (context->printer));
418 int cmin = xloc2.column
419 ? MIN (xloc1.column, xloc2.column) : xloc1.column;
420 int caret_min = cmin == xloc1.column ? caret1 : caret2;
421 int caret_max = cmin == xloc1.column ? caret2 : caret1;
423 /* cmin is >= 1, but we indent with an extra space at the start like
424 we did above. */
425 int i;
426 for (i = 0; i < cmin; i++)
427 pp_space (context->printer);
428 pp_printf (context->printer, "%s%c%s", caret_cs, caret_min, caret_ce);
430 if (xloc2.column)
432 for (i++; i < cmax; i++)
433 pp_space (context->printer);
434 pp_printf (context->printer, "%s%c%s", caret_cs, caret_max, caret_ce);
436 pp_set_prefix (context->printer, saved_prefix);
437 pp_needs_newline (context->printer) = true;
440 /* Functions at which to stop the backtrace print. It's not
441 particularly helpful to print the callers of these functions. */
443 static const char * const bt_stop[] =
445 "main",
446 "toplev::main",
447 "execute_one_pass",
448 "compile_file",
451 /* A callback function passed to the backtrace_full function. */
453 static int
454 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
455 const char *function)
457 int *pcount = (int *) data;
459 /* If we don't have any useful information, don't print
460 anything. */
461 if (filename == NULL && function == NULL)
462 return 0;
464 /* Skip functions in diagnostic.c. */
465 if (*pcount == 0
466 && filename != NULL
467 && strcmp (lbasename (filename), "diagnostic.c") == 0)
468 return 0;
470 /* Print up to 20 functions. We could make this a --param, but
471 since this is only for debugging just use a constant for now. */
472 if (*pcount >= 20)
474 /* Returning a non-zero value stops the backtrace. */
475 return 1;
477 ++*pcount;
479 char *alc = NULL;
480 if (function != NULL)
482 char *str = cplus_demangle_v3 (function,
483 (DMGL_VERBOSE | DMGL_ANSI
484 | DMGL_GNU_V3 | DMGL_PARAMS));
485 if (str != NULL)
487 alc = str;
488 function = str;
491 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
493 size_t len = strlen (bt_stop[i]);
494 if (strncmp (function, bt_stop[i], len) == 0
495 && (function[len] == '\0' || function[len] == '('))
497 if (alc != NULL)
498 free (alc);
499 /* Returning a non-zero value stops the backtrace. */
500 return 1;
505 fprintf (stderr, "0x%lx %s\n\t%s:%d\n",
506 (unsigned long) pc,
507 function == NULL ? "???" : function,
508 filename == NULL ? "???" : filename,
509 lineno);
511 if (alc != NULL)
512 free (alc);
514 return 0;
517 /* A callback function passed to the backtrace_full function. This is
518 called if backtrace_full has an error. */
520 static void
521 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
523 if (errnum < 0)
525 /* This means that no debug info was available. Just quietly
526 skip printing backtrace info. */
527 return;
529 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
530 errnum == 0 ? "" : xstrerror (errnum));
533 /* Take any action which is expected to happen after the diagnostic
534 is written out. This function does not always return. */
535 void
536 diagnostic_action_after_output (diagnostic_context *context,
537 diagnostic_t diag_kind)
539 switch (diag_kind)
541 case DK_DEBUG:
542 case DK_NOTE:
543 case DK_ANACHRONISM:
544 case DK_WARNING:
545 break;
547 case DK_ERROR:
548 case DK_SORRY:
549 if (context->abort_on_error)
550 real_abort ();
551 if (context->fatal_errors)
553 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
554 diagnostic_finish (context);
555 exit (FATAL_EXIT_CODE);
557 if (context->max_errors != 0
558 && ((unsigned) (diagnostic_kind_count (context, DK_ERROR)
559 + diagnostic_kind_count (context, DK_SORRY)
560 + diagnostic_kind_count (context, DK_WERROR))
561 >= context->max_errors))
563 fnotice (stderr,
564 "compilation terminated due to -fmax-errors=%u.\n",
565 context->max_errors);
566 diagnostic_finish (context);
567 exit (FATAL_EXIT_CODE);
569 break;
571 case DK_ICE:
572 case DK_ICE_NOBT:
574 struct backtrace_state *state = NULL;
575 if (diag_kind == DK_ICE)
576 state = backtrace_create_state (NULL, 0, bt_err_callback, NULL);
577 int count = 0;
578 if (state != NULL)
579 backtrace_full (state, 2, bt_callback, bt_err_callback,
580 (void *) &count);
582 if (context->abort_on_error)
583 real_abort ();
585 fnotice (stderr, "Please submit a full bug report,\n"
586 "with preprocessed source if appropriate.\n");
587 if (count > 0)
588 fnotice (stderr,
589 ("Please include the complete backtrace "
590 "with any bug report.\n"));
591 fnotice (stderr, "See %s for instructions.\n", bug_report_url);
593 exit (ICE_EXIT_CODE);
596 case DK_FATAL:
597 if (context->abort_on_error)
598 real_abort ();
599 diagnostic_finish (context);
600 fnotice (stderr, "compilation terminated.\n");
601 exit (FATAL_EXIT_CODE);
603 default:
604 gcc_unreachable ();
608 void
609 diagnostic_report_current_module (diagnostic_context *context, location_t where)
611 const line_map_ordinary *map = NULL;
613 if (pp_needs_newline (context->printer))
615 pp_newline (context->printer);
616 pp_needs_newline (context->printer) = false;
619 if (where <= BUILTINS_LOCATION)
620 return;
622 linemap_resolve_location (line_table, where,
623 LRK_MACRO_DEFINITION_LOCATION,
624 &map);
626 if (map && diagnostic_last_module_changed (context, map))
628 diagnostic_set_last_module (context, map);
629 if (! MAIN_FILE_P (map))
631 map = INCLUDED_FROM (line_table, map);
632 if (context->show_column)
633 pp_verbatim (context->printer,
634 "In file included from %r%s:%d:%d%R", "locus",
635 LINEMAP_FILE (map),
636 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
637 else
638 pp_verbatim (context->printer,
639 "In file included from %r%s:%d%R", "locus",
640 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
641 while (! MAIN_FILE_P (map))
643 map = INCLUDED_FROM (line_table, map);
644 pp_verbatim (context->printer,
645 ",\n from %r%s:%d%R", "locus",
646 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
648 pp_verbatim (context->printer, ":");
649 pp_newline (context->printer);
654 void
655 default_diagnostic_starter (diagnostic_context *context,
656 diagnostic_info *diagnostic)
658 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
659 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
660 diagnostic));
663 void
664 default_diagnostic_finalizer (diagnostic_context *context,
665 diagnostic_info *diagnostic)
667 diagnostic_show_locus (context, diagnostic);
668 pp_destroy_prefix (context->printer);
669 pp_newline_and_flush (context->printer);
672 /* Interface to specify diagnostic kind overrides. Returns the
673 previous setting, or DK_UNSPECIFIED if the parameters are out of
674 range. If OPTION_INDEX is zero, the new setting is for all the
675 diagnostics. */
676 diagnostic_t
677 diagnostic_classify_diagnostic (diagnostic_context *context,
678 int option_index,
679 diagnostic_t new_kind,
680 location_t where)
682 diagnostic_t old_kind;
684 if (option_index < 0
685 || option_index >= context->n_opts
686 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
687 return DK_UNSPECIFIED;
689 old_kind = context->classify_diagnostic[option_index];
691 /* Handle pragmas separately, since we need to keep track of *where*
692 the pragmas were. */
693 if (where != UNKNOWN_LOCATION)
695 int i;
697 /* Record the command-line status, so we can reset it back on DK_POP. */
698 if (old_kind == DK_UNSPECIFIED)
700 old_kind = context->option_enabled (option_index,
701 context->option_state)
702 ? DK_WARNING : DK_IGNORED;
703 context->classify_diagnostic[option_index] = old_kind;
706 for (i = context->n_classification_history - 1; i >= 0; i --)
707 if (context->classification_history[i].option == option_index)
709 old_kind = context->classification_history[i].kind;
710 break;
713 i = context->n_classification_history;
714 context->classification_history =
715 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
716 * sizeof (diagnostic_classification_change_t));
717 context->classification_history[i].location = where;
718 context->classification_history[i].option = option_index;
719 context->classification_history[i].kind = new_kind;
720 context->n_classification_history ++;
722 else
723 context->classify_diagnostic[option_index] = new_kind;
725 return old_kind;
728 /* Save all diagnostic classifications in a stack. */
729 void
730 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
732 context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
733 context->push_list[context->n_push ++] = context->n_classification_history;
736 /* Restore the topmost classification set off the stack. If the stack
737 is empty, revert to the state based on command line parameters. */
738 void
739 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
741 int jump_to;
742 int i;
744 if (context->n_push)
745 jump_to = context->push_list [-- context->n_push];
746 else
747 jump_to = 0;
749 i = context->n_classification_history;
750 context->classification_history =
751 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
752 * sizeof (diagnostic_classification_change_t));
753 context->classification_history[i].location = where;
754 context->classification_history[i].option = jump_to;
755 context->classification_history[i].kind = DK_POP;
756 context->n_classification_history ++;
759 /* Report a diagnostic message (an error or a warning) as specified by
760 DC. This function is *the* subroutine in terms of which front-ends
761 should implement their specific diagnostic handling modules. The
762 front-end independent format specifiers are exactly those described
763 in the documentation of output_format.
764 Return true if a diagnostic was printed, false otherwise. */
766 bool
767 diagnostic_report_diagnostic (diagnostic_context *context,
768 diagnostic_info *diagnostic)
770 location_t location = diagnostic_location (diagnostic);
771 diagnostic_t orig_diag_kind = diagnostic->kind;
772 const char *saved_format_spec;
774 /* Give preference to being able to inhibit warnings, before they
775 get reclassified to something else. */
776 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
777 && !diagnostic_report_warnings_p (context, location))
778 return false;
780 if (diagnostic->kind == DK_PEDWARN)
782 diagnostic->kind = pedantic_warning_kind (context);
783 /* We do this to avoid giving the message for -pedantic-errors. */
784 orig_diag_kind = diagnostic->kind;
787 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
788 return false;
790 if (context->lock > 0)
792 /* If we're reporting an ICE in the middle of some other error,
793 try to flush out the previous error, then let this one
794 through. Don't do this more than once. */
795 if ((diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
796 && context->lock == 1)
797 pp_newline_and_flush (context->printer);
798 else
799 error_recursion (context);
802 /* If the user requested that warnings be treated as errors, so be
803 it. Note that we do this before the next block so that
804 individual warnings can be overridden back to warnings with
805 -Wno-error=*. */
806 if (context->warning_as_error_requested
807 && diagnostic->kind == DK_WARNING)
809 diagnostic->kind = DK_ERROR;
812 if (diagnostic->option_index
813 && diagnostic->option_index != permissive_error_option (context))
815 diagnostic_t diag_class = DK_UNSPECIFIED;
817 /* This tests if the user provided the appropriate -Wfoo or
818 -Wno-foo option. */
819 if (! context->option_enabled (diagnostic->option_index,
820 context->option_state))
821 return false;
823 /* This tests for #pragma diagnostic changes. */
824 if (context->n_classification_history > 0)
826 /* FIXME: Stupid search. Optimize later. */
827 for (int i = context->n_classification_history - 1; i >= 0; i --)
829 if (linemap_location_before_p
830 (line_table,
831 context->classification_history[i].location,
832 location))
834 if (context->classification_history[i].kind == (int) DK_POP)
836 i = context->classification_history[i].option;
837 continue;
839 int option = context->classification_history[i].option;
840 /* The option 0 is for all the diagnostics. */
841 if (option == 0 || option == diagnostic->option_index)
843 diag_class = context->classification_history[i].kind;
844 if (diag_class != DK_UNSPECIFIED)
845 diagnostic->kind = diag_class;
846 break;
851 /* This tests if the user provided the appropriate -Werror=foo
852 option. */
853 if (diag_class == DK_UNSPECIFIED
854 && context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
856 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
858 /* This allows for future extensions, like temporarily disabling
859 warnings for ranges of source code. */
860 if (diagnostic->kind == DK_IGNORED)
861 return false;
864 if (orig_diag_kind == DK_WARNING && diagnostic->kind == DK_ERROR)
865 context->some_warnings_are_errors = true;
867 context->lock++;
869 if (diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
871 #ifndef ENABLE_CHECKING
872 /* When not checking, ICEs are converted to fatal errors when an
873 error has already occurred. This is counteracted by
874 abort_on_error. */
875 if ((diagnostic_kind_count (context, DK_ERROR) > 0
876 || diagnostic_kind_count (context, DK_SORRY) > 0)
877 && !context->abort_on_error)
879 expanded_location s
880 = expand_location (diagnostic_location (diagnostic));
881 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
882 s.file, s.line);
883 exit (ICE_EXIT_CODE);
885 #endif
886 if (context->internal_error)
887 (*context->internal_error) (context,
888 diagnostic->message.format_spec,
889 diagnostic->message.args_ptr);
891 if (diagnostic->kind == DK_ERROR && orig_diag_kind == DK_WARNING)
892 ++diagnostic_kind_count (context, DK_WERROR);
893 else
894 ++diagnostic_kind_count (context, diagnostic->kind);
896 saved_format_spec = diagnostic->message.format_spec;
897 if (context->show_option_requested)
899 char *option_text;
901 option_text = context->option_name (context, diagnostic->option_index,
902 orig_diag_kind, diagnostic->kind);
904 if (option_text)
906 diagnostic->message.format_spec
907 = ACONCAT ((diagnostic->message.format_spec,
908 " ",
909 "[", option_text, "]",
910 NULL));
911 free (option_text);
914 diagnostic->message.x_data = &diagnostic->x_data;
915 diagnostic->x_data = NULL;
916 pp_format (context->printer, &diagnostic->message);
917 (*diagnostic_starter (context)) (context, diagnostic);
918 pp_output_formatted_text (context->printer);
919 (*diagnostic_finalizer (context)) (context, diagnostic);
920 diagnostic_action_after_output (context, diagnostic->kind);
921 diagnostic->message.format_spec = saved_format_spec;
922 diagnostic->x_data = NULL;
924 context->lock--;
926 return true;
929 /* Given a partial pathname as input, return another pathname that
930 shares no directory elements with the pathname of __FILE__. This
931 is used by fancy_abort() to print `Internal compiler error in expr.c'
932 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
934 const char *
935 trim_filename (const char *name)
937 static const char this_file[] = __FILE__;
938 const char *p = name, *q = this_file;
940 /* First skip any "../" in each filename. This allows us to give a proper
941 reference to a file in a subdirectory. */
942 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
943 p += 3;
945 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
946 q += 3;
948 /* Now skip any parts the two filenames have in common. */
949 while (*p == *q && *p != 0 && *q != 0)
950 p++, q++;
952 /* Now go backwards until the previous directory separator. */
953 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
954 p--;
956 return p;
959 /* Standard error reporting routines in increasing order of severity.
960 All of these take arguments like printf. */
962 /* Text to be emitted verbatim to the error message stream; this
963 produces no prefix and disables line-wrapping. Use rarely. */
964 void
965 verbatim (const char *gmsgid, ...)
967 text_info text;
968 va_list ap;
970 va_start (ap, gmsgid);
971 text.err_no = errno;
972 text.args_ptr = &ap;
973 text.format_spec = _(gmsgid);
974 text.x_data = NULL;
975 pp_format_verbatim (global_dc->printer, &text);
976 pp_newline_and_flush (global_dc->printer);
977 va_end (ap);
980 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
981 void
982 diagnostic_append_note (diagnostic_context *context,
983 location_t location,
984 const char * gmsgid, ...)
986 diagnostic_info diagnostic;
987 va_list ap;
988 const char *saved_prefix;
990 va_start (ap, gmsgid);
991 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
992 if (context->inhibit_notes_p)
994 va_end (ap);
995 return;
997 saved_prefix = pp_get_prefix (context->printer);
998 pp_set_prefix (context->printer,
999 diagnostic_build_prefix (context, &diagnostic));
1000 pp_newline (context->printer);
1001 pp_format (context->printer, &diagnostic.message);
1002 pp_output_formatted_text (context->printer);
1003 pp_destroy_prefix (context->printer);
1004 pp_set_prefix (context->printer, saved_prefix);
1005 diagnostic_show_locus (context, &diagnostic);
1006 va_end (ap);
1009 bool
1010 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
1011 const char *gmsgid, ...)
1013 diagnostic_info diagnostic;
1014 va_list ap;
1015 bool ret;
1017 va_start (ap, gmsgid);
1018 if (kind == DK_PERMERROR)
1020 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
1021 permissive_error_kind (global_dc));
1022 diagnostic.option_index = permissive_error_option (global_dc);
1024 else {
1025 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, kind);
1026 if (kind == DK_WARNING || kind == DK_PEDWARN)
1027 diagnostic.option_index = opt;
1030 ret = report_diagnostic (&diagnostic);
1031 va_end (ap);
1032 return ret;
1035 /* An informative note at LOCATION. Use this for additional details on an error
1036 message. */
1037 void
1038 inform (location_t location, const char *gmsgid, ...)
1040 diagnostic_info diagnostic;
1041 va_list ap;
1043 va_start (ap, gmsgid);
1044 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
1045 report_diagnostic (&diagnostic);
1046 va_end (ap);
1049 /* An informative note at LOCATION. Use this for additional details on an
1050 error message. */
1051 void
1052 inform_n (location_t location, int n, const char *singular_gmsgid,
1053 const char *plural_gmsgid, ...)
1055 diagnostic_info diagnostic;
1056 va_list ap;
1058 va_start (ap, plural_gmsgid);
1059 diagnostic_set_info_translated (&diagnostic,
1060 ngettext (singular_gmsgid, plural_gmsgid, n),
1061 &ap, location, DK_NOTE);
1062 report_diagnostic (&diagnostic);
1063 va_end (ap);
1066 /* A warning at INPUT_LOCATION. Use this for code which is correct according
1067 to the relevant language specification but is likely to be buggy anyway.
1068 Returns true if the warning was printed, false if it was inhibited. */
1069 bool
1070 warning (int opt, const char *gmsgid, ...)
1072 diagnostic_info diagnostic;
1073 va_list ap;
1074 bool ret;
1076 va_start (ap, gmsgid);
1077 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
1078 diagnostic.option_index = opt;
1080 ret = report_diagnostic (&diagnostic);
1081 va_end (ap);
1082 return ret;
1085 /* A warning at LOCATION. Use this for code which is correct according to the
1086 relevant language specification but is likely to be buggy anyway.
1087 Returns true if the warning was printed, false if it was inhibited. */
1089 bool
1090 warning_at (location_t location, int opt, const char *gmsgid, ...)
1092 diagnostic_info diagnostic;
1093 va_list ap;
1094 bool ret;
1096 va_start (ap, gmsgid);
1097 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_WARNING);
1098 diagnostic.option_index = opt;
1099 ret = report_diagnostic (&diagnostic);
1100 va_end (ap);
1101 return ret;
1104 /* A warning at LOCATION. Use this for code which is correct according to the
1105 relevant language specification but is likely to be buggy anyway.
1106 Returns true if the warning was printed, false if it was inhibited. */
1108 bool
1109 warning_n (location_t location, int opt, int n, const char *singular_gmsgid,
1110 const char *plural_gmsgid, ...)
1112 diagnostic_info diagnostic;
1113 va_list ap;
1114 bool ret;
1116 va_start (ap, plural_gmsgid);
1117 diagnostic_set_info_translated (&diagnostic,
1118 ngettext (singular_gmsgid, plural_gmsgid, n),
1119 &ap, location, DK_WARNING);
1120 diagnostic.option_index = opt;
1121 ret = report_diagnostic (&diagnostic);
1122 va_end (ap);
1123 return ret;
1126 /* A "pedantic" warning at LOCATION: issues a warning unless
1127 -pedantic-errors was given on the command line, in which case it
1128 issues an error. Use this for diagnostics required by the relevant
1129 language standard, if you have chosen not to make them errors.
1131 Note that these diagnostics are issued independent of the setting
1132 of the -Wpedantic command-line switch. To get a warning enabled
1133 only with that switch, use either "if (pedantic) pedwarn
1134 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
1135 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
1137 Returns true if the warning was printed, false if it was inhibited. */
1139 bool
1140 pedwarn (location_t location, int opt, const char *gmsgid, ...)
1142 diagnostic_info diagnostic;
1143 va_list ap;
1144 bool ret;
1146 va_start (ap, gmsgid);
1147 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_PEDWARN);
1148 diagnostic.option_index = opt;
1149 ret = report_diagnostic (&diagnostic);
1150 va_end (ap);
1151 return ret;
1154 /* A "permissive" error at LOCATION: issues an error unless
1155 -fpermissive was given on the command line, in which case it issues
1156 a warning. Use this for things that really should be errors but we
1157 want to support legacy code.
1159 Returns true if the warning was printed, false if it was inhibited. */
1161 bool
1162 permerror (location_t location, const char *gmsgid, ...)
1164 diagnostic_info diagnostic;
1165 va_list ap;
1166 bool ret;
1168 va_start (ap, gmsgid);
1169 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
1170 permissive_error_kind (global_dc));
1171 diagnostic.option_index = permissive_error_option (global_dc);
1172 ret = report_diagnostic (&diagnostic);
1173 va_end (ap);
1174 return ret;
1177 /* A hard error: the code is definitely ill-formed, and an object file
1178 will not be produced. */
1179 void
1180 error (const char *gmsgid, ...)
1182 diagnostic_info diagnostic;
1183 va_list ap;
1185 va_start (ap, gmsgid);
1186 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
1187 report_diagnostic (&diagnostic);
1188 va_end (ap);
1191 /* A hard error: the code is definitely ill-formed, and an object file
1192 will not be produced. */
1193 void
1194 error_n (location_t location, int n, const char *singular_gmsgid,
1195 const char *plural_gmsgid, ...)
1197 diagnostic_info diagnostic;
1198 va_list ap;
1200 va_start (ap, plural_gmsgid);
1201 diagnostic_set_info_translated (&diagnostic,
1202 ngettext (singular_gmsgid, plural_gmsgid, n),
1203 &ap, location, DK_ERROR);
1204 report_diagnostic (&diagnostic);
1205 va_end (ap);
1208 /* Same as ebove, but use location LOC instead of input_location. */
1209 void
1210 error_at (location_t loc, const char *gmsgid, ...)
1212 diagnostic_info diagnostic;
1213 va_list ap;
1215 va_start (ap, gmsgid);
1216 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_ERROR);
1217 report_diagnostic (&diagnostic);
1218 va_end (ap);
1221 /* "Sorry, not implemented." Use for a language feature which is
1222 required by the relevant specification but not implemented by GCC.
1223 An object file will not be produced. */
1224 void
1225 sorry (const char *gmsgid, ...)
1227 diagnostic_info diagnostic;
1228 va_list ap;
1230 va_start (ap, gmsgid);
1231 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
1232 report_diagnostic (&diagnostic);
1233 va_end (ap);
1236 /* Return true if an error or a "sorry" has been seen. Various
1237 processing is disabled after errors. */
1238 bool
1239 seen_error (void)
1241 return errorcount || sorrycount;
1244 /* An error which is severe enough that we make no attempt to
1245 continue. Do not use this for internal consistency checks; that's
1246 internal_error. Use of this function should be rare. */
1247 void
1248 fatal_error (location_t loc, const char *gmsgid, ...)
1250 diagnostic_info diagnostic;
1251 va_list ap;
1253 va_start (ap, gmsgid);
1254 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_FATAL);
1255 report_diagnostic (&diagnostic);
1256 va_end (ap);
1258 gcc_unreachable ();
1261 /* An internal consistency check has failed. We make no attempt to
1262 continue. Note that unless there is debugging value to be had from
1263 a more specific message, or some other good reason, you should use
1264 abort () instead of calling this function directly. */
1265 void
1266 internal_error (const char *gmsgid, ...)
1268 diagnostic_info diagnostic;
1269 va_list ap;
1271 va_start (ap, gmsgid);
1272 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
1273 report_diagnostic (&diagnostic);
1274 va_end (ap);
1276 gcc_unreachable ();
1279 /* Like internal_error, but no backtrace will be printed. Used when
1280 the internal error does not happen at the current location, but happened
1281 somewhere else. */
1282 void
1283 internal_error_no_backtrace (const char *gmsgid, ...)
1285 diagnostic_info diagnostic;
1286 va_list ap;
1288 va_start (ap, gmsgid);
1289 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE_NOBT);
1290 report_diagnostic (&diagnostic);
1291 va_end (ap);
1293 gcc_unreachable ();
1296 /* Special case error functions. Most are implemented in terms of the
1297 above, or should be. */
1299 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1300 runs its second argument through gettext. */
1301 void
1302 fnotice (FILE *file, const char *cmsgid, ...)
1304 va_list ap;
1306 va_start (ap, cmsgid);
1307 vfprintf (file, _(cmsgid), ap);
1308 va_end (ap);
1311 /* Inform the user that an error occurred while trying to report some
1312 other error. This indicates catastrophic internal inconsistencies,
1313 so give up now. But do try to flush out the previous error.
1314 This mustn't use internal_error, that will cause infinite recursion. */
1316 static void
1317 error_recursion (diagnostic_context *context)
1319 if (context->lock < 3)
1320 pp_newline_and_flush (context->printer);
1322 fnotice (stderr,
1323 "Internal compiler error: Error reporting routines re-entered.\n");
1325 /* Call diagnostic_action_after_output to get the "please submit a bug
1326 report" message. */
1327 diagnostic_action_after_output (context, DK_ICE);
1329 /* Do not use gcc_unreachable here; that goes through internal_error
1330 and therefore would cause infinite recursion. */
1331 real_abort ();
1334 /* Report an internal compiler error in a friendly manner. This is
1335 the function that gets called upon use of abort() in the source
1336 code generally, thanks to a special macro. */
1338 void
1339 fancy_abort (const char *file, int line, const char *function)
1341 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1344 /* Really call the system 'abort'. This has to go right at the end of
1345 this file, so that there are no functions after it that call abort
1346 and get the system abort instead of our macro. */
1347 #undef abort
1348 static void
1349 real_abort (void)
1351 abort ();