Update ChangeLog and version files for release
[official-gcc.git] / gcc / diagnostic.c
blobc43162269ec7bfe0187232cd9efd822491828dba
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->warning_as_error_requested = false;
142 context->n_opts = n_opts;
143 context->classify_diagnostic = XNEWVEC (diagnostic_t, n_opts);
144 for (i = 0; i < n_opts; i++)
145 context->classify_diagnostic[i] = DK_UNSPECIFIED;
146 context->show_caret = false;
147 diagnostic_set_caret_max_width (context, pp_line_cutoff (context->printer));
148 context->caret_char = '^';
149 context->show_option_requested = false;
150 context->abort_on_error = false;
151 context->show_column = false;
152 context->pedantic_errors = false;
153 context->permissive = false;
154 context->opt_permissive = 0;
155 context->fatal_errors = false;
156 context->dc_inhibit_warnings = false;
157 context->dc_warn_system_headers = false;
158 context->max_errors = 0;
159 context->internal_error = NULL;
160 diagnostic_starter (context) = default_diagnostic_starter;
161 diagnostic_finalizer (context) = default_diagnostic_finalizer;
162 context->option_enabled = NULL;
163 context->option_state = NULL;
164 context->option_name = NULL;
165 context->last_location = UNKNOWN_LOCATION;
166 context->last_module = 0;
167 context->x_data = NULL;
168 context->lock = 0;
169 context->inhibit_notes_p = false;
172 /* Maybe initialize the color support. We require clients to do this
173 explicitly, since most clients don't want color. When called
174 without a VALUE, it initializes with DIAGNOSTICS_COLOR_DEFAULT. */
176 void
177 diagnostic_color_init (diagnostic_context *context, int value /*= -1 */)
179 /* value == -1 is the default value. */
180 if (value < 0)
182 /* If DIAGNOSTICS_COLOR_DEFAULT is -1, default to
183 -fdiagnostics-color=auto if GCC_COLORS is in the environment,
184 otherwise default to -fdiagnostics-color=never, for other
185 values default to that
186 -fdiagnostics-color={never,auto,always}. */
187 if (DIAGNOSTICS_COLOR_DEFAULT == -1)
189 if (!getenv ("GCC_COLORS"))
190 return;
191 value = DIAGNOSTICS_COLOR_AUTO;
193 else
194 value = DIAGNOSTICS_COLOR_DEFAULT;
196 pp_show_color (context->printer)
197 = colorize_init ((diagnostic_color_rule_t) value);
200 /* Do any cleaning up required after the last diagnostic is emitted. */
202 void
203 diagnostic_finish (diagnostic_context *context)
205 /* Some of the errors may actually have been warnings. */
206 if (diagnostic_kind_count (context, DK_WERROR))
208 /* -Werror was given. */
209 if (context->warning_as_error_requested)
210 pp_verbatim (context->printer,
211 _("%s: all warnings being treated as errors"),
212 progname);
213 /* At least one -Werror= was given. */
214 else
215 pp_verbatim (context->printer,
216 _("%s: some warnings being treated as errors"),
217 progname);
218 pp_newline_and_flush (context->printer);
221 diagnostic_file_cache_fini ();
223 XDELETEVEC (context->classify_diagnostic);
224 context->classify_diagnostic = NULL;
226 /* diagnostic_initialize allocates context->printer using XNEW
227 and placement-new. */
228 context->printer->~pretty_printer ();
229 XDELETE (context->printer);
230 context->printer = NULL;
233 /* Initialize DIAGNOSTIC, where the message MSG has already been
234 translated. */
235 void
236 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
237 va_list *args, location_t location,
238 diagnostic_t kind)
240 diagnostic->message.err_no = errno;
241 diagnostic->message.args_ptr = args;
242 diagnostic->message.format_spec = msg;
243 diagnostic->location = location;
244 diagnostic->override_column = 0;
245 diagnostic->kind = kind;
246 diagnostic->option_index = 0;
249 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
250 translated. */
251 void
252 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
253 va_list *args, location_t location,
254 diagnostic_t kind)
256 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
259 /* Return a malloc'd string describing a location. The caller is
260 responsible for freeing the memory. */
261 char *
262 diagnostic_build_prefix (diagnostic_context *context,
263 const diagnostic_info *diagnostic)
265 static const char *const diagnostic_kind_text[] = {
266 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
267 #include "diagnostic.def"
268 #undef DEFINE_DIAGNOSTIC_KIND
269 "must-not-happen"
271 static const char *const diagnostic_kind_color[] = {
272 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
273 #include "diagnostic.def"
274 #undef DEFINE_DIAGNOSTIC_KIND
275 NULL
277 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
279 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
280 const char *text_cs = "", *text_ce = "";
281 const char *locus_cs, *locus_ce;
282 pretty_printer *pp = context->printer;
284 if (diagnostic_kind_color[diagnostic->kind])
286 text_cs = colorize_start (pp_show_color (pp),
287 diagnostic_kind_color[diagnostic->kind]);
288 text_ce = colorize_stop (pp_show_color (pp));
290 locus_cs = colorize_start (pp_show_color (pp), "locus");
291 locus_ce = colorize_stop (pp_show_color (pp));
293 expanded_location s = diagnostic_expand_location (diagnostic);
294 return
295 (s.file == NULL
296 ? build_message_string ("%s%s:%s %s%s%s", locus_cs, progname, locus_ce,
297 text_cs, text, text_ce)
298 : !strcmp (s.file, N_("<built-in>"))
299 ? build_message_string ("%s%s:%s %s%s%s", locus_cs, s.file, locus_ce,
300 text_cs, text, text_ce)
301 : context->show_column
302 ? build_message_string ("%s%s:%d:%d:%s %s%s%s", locus_cs, s.file, s.line,
303 s.column, locus_ce, text_cs, text, text_ce)
304 : build_message_string ("%s%s:%d:%s %s%s%s", locus_cs, s.file, s.line,
305 locus_ce, text_cs, text, text_ce));
308 /* If LINE is longer than MAX_WIDTH, and COLUMN is not smaller than
309 MAX_WIDTH by some margin, then adjust the start of the line such
310 that the COLUMN is smaller than MAX_WIDTH minus the margin. The
311 margin is either 10 characters or the difference between the column
312 and the length of the line, whatever is smaller. The length of
313 LINE is given by LINE_WIDTH. */
314 static const char *
315 adjust_line (const char *line, int line_width,
316 int max_width, int *column_p)
318 int right_margin = 10;
319 int column = *column_p;
321 gcc_checking_assert (line_width >= column);
322 right_margin = MIN (line_width - column, right_margin);
323 right_margin = max_width - right_margin;
324 if (line_width >= max_width && column > right_margin)
326 line += column - right_margin;
327 *column_p = right_margin;
329 return line;
332 /* Print the physical source line corresponding to the location of
333 this diagnostic, and a caret indicating the precise column. */
334 void
335 diagnostic_show_locus (diagnostic_context * context,
336 const diagnostic_info *diagnostic)
338 const char *line;
339 int line_width;
340 char *buffer;
341 expanded_location s;
342 int max_width;
343 const char *saved_prefix;
344 const char *caret_cs, *caret_ce;
346 if (!context->show_caret
347 || diagnostic->location <= BUILTINS_LOCATION
348 || diagnostic->location == context->last_location)
349 return;
351 context->last_location = diagnostic->location;
352 s = diagnostic_expand_location (diagnostic);
353 line = location_get_source_line (s, &line_width);
354 if (line == NULL || s.column > line_width)
355 return;
357 max_width = context->caret_max_width;
358 line = adjust_line (line, line_width, max_width, &(s.column));
360 pp_newline (context->printer);
361 saved_prefix = pp_get_prefix (context->printer);
362 pp_set_prefix (context->printer, NULL);
363 pp_space (context->printer);
364 while (max_width > 0 && line_width > 0)
366 char c = *line == '\t' ? ' ' : *line;
367 if (c == '\0')
368 c = ' ';
369 pp_character (context->printer, c);
370 max_width--;
371 line_width--;
372 line++;
374 pp_newline (context->printer);
375 caret_cs = colorize_start (pp_show_color (context->printer), "caret");
376 caret_ce = colorize_stop (pp_show_color (context->printer));
378 /* pp_printf does not implement %*c. */
379 size_t len = s.column + 3 + strlen (caret_cs) + strlen (caret_ce);
380 buffer = XALLOCAVEC (char, len);
381 snprintf (buffer, len, "%s %*c%s", caret_cs, s.column, context->caret_char,
382 caret_ce);
383 pp_string (context->printer, buffer);
384 pp_set_prefix (context->printer, saved_prefix);
385 pp_needs_newline (context->printer) = true;
388 /* Functions at which to stop the backtrace print. It's not
389 particularly helpful to print the callers of these functions. */
391 static const char * const bt_stop[] =
393 "main",
394 "toplev::main",
395 "execute_one_pass",
396 "compile_file",
399 /* A callback function passed to the backtrace_full function. */
401 static int
402 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
403 const char *function)
405 int *pcount = (int *) data;
407 /* If we don't have any useful information, don't print
408 anything. */
409 if (filename == NULL && function == NULL)
410 return 0;
412 /* Skip functions in diagnostic.c. */
413 if (*pcount == 0
414 && filename != NULL
415 && strcmp (lbasename (filename), "diagnostic.c") == 0)
416 return 0;
418 /* Print up to 20 functions. We could make this a --param, but
419 since this is only for debugging just use a constant for now. */
420 if (*pcount >= 20)
422 /* Returning a non-zero value stops the backtrace. */
423 return 1;
425 ++*pcount;
427 char *alc = NULL;
428 if (function != NULL)
430 char *str = cplus_demangle_v3 (function,
431 (DMGL_VERBOSE | DMGL_ANSI
432 | DMGL_GNU_V3 | DMGL_PARAMS));
433 if (str != NULL)
435 alc = str;
436 function = str;
439 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
441 size_t len = strlen (bt_stop[i]);
442 if (strncmp (function, bt_stop[i], len) == 0
443 && (function[len] == '\0' || function[len] == '('))
445 if (alc != NULL)
446 free (alc);
447 /* Returning a non-zero value stops the backtrace. */
448 return 1;
453 fprintf (stderr, "0x%lx %s\n\t%s:%d\n",
454 (unsigned long) pc,
455 function == NULL ? "???" : function,
456 filename == NULL ? "???" : filename,
457 lineno);
459 if (alc != NULL)
460 free (alc);
462 return 0;
465 /* A callback function passed to the backtrace_full function. This is
466 called if backtrace_full has an error. */
468 static void
469 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
471 if (errnum < 0)
473 /* This means that no debug info was available. Just quietly
474 skip printing backtrace info. */
475 return;
477 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
478 errnum == 0 ? "" : xstrerror (errnum));
481 /* Take any action which is expected to happen after the diagnostic
482 is written out. This function does not always return. */
483 void
484 diagnostic_action_after_output (diagnostic_context *context,
485 diagnostic_t diag_kind)
487 switch (diag_kind)
489 case DK_DEBUG:
490 case DK_NOTE:
491 case DK_ANACHRONISM:
492 case DK_WARNING:
493 break;
495 case DK_ERROR:
496 case DK_SORRY:
497 if (context->abort_on_error)
498 real_abort ();
499 if (context->fatal_errors)
501 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
502 diagnostic_finish (context);
503 exit (FATAL_EXIT_CODE);
505 if (context->max_errors != 0
506 && ((unsigned) (diagnostic_kind_count (context, DK_ERROR)
507 + diagnostic_kind_count (context, DK_SORRY)
508 + diagnostic_kind_count (context, DK_WERROR))
509 >= context->max_errors))
511 fnotice (stderr,
512 "compilation terminated due to -fmax-errors=%u.\n",
513 context->max_errors);
514 diagnostic_finish (context);
515 exit (FATAL_EXIT_CODE);
517 break;
519 case DK_ICE:
520 case DK_ICE_NOBT:
522 struct backtrace_state *state = NULL;
523 if (diag_kind == DK_ICE)
524 state = backtrace_create_state (NULL, 0, bt_err_callback, NULL);
525 int count = 0;
526 if (state != NULL)
527 backtrace_full (state, 2, bt_callback, bt_err_callback,
528 (void *) &count);
530 if (context->abort_on_error)
531 real_abort ();
533 fnotice (stderr, "Please submit a full bug report,\n"
534 "with preprocessed source if appropriate.\n");
535 if (count > 0)
536 fnotice (stderr,
537 ("Please include the complete backtrace "
538 "with any bug report.\n"));
539 fnotice (stderr, "See %s for instructions.\n", bug_report_url);
541 exit (ICE_EXIT_CODE);
544 case DK_FATAL:
545 if (context->abort_on_error)
546 real_abort ();
547 diagnostic_finish (context);
548 fnotice (stderr, "compilation terminated.\n");
549 exit (FATAL_EXIT_CODE);
551 default:
552 gcc_unreachable ();
556 void
557 diagnostic_report_current_module (diagnostic_context *context, location_t where)
559 const struct line_map *map = NULL;
561 if (pp_needs_newline (context->printer))
563 pp_newline (context->printer);
564 pp_needs_newline (context->printer) = false;
567 if (where <= BUILTINS_LOCATION)
568 return;
570 linemap_resolve_location (line_table, where,
571 LRK_MACRO_DEFINITION_LOCATION,
572 &map);
574 if (map && diagnostic_last_module_changed (context, map))
576 diagnostic_set_last_module (context, map);
577 if (! MAIN_FILE_P (map))
579 map = INCLUDED_FROM (line_table, map);
580 if (context->show_column)
581 pp_verbatim (context->printer,
582 "In file included from %r%s:%d:%d%R", "locus",
583 LINEMAP_FILE (map),
584 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
585 else
586 pp_verbatim (context->printer,
587 "In file included from %r%s:%d%R", "locus",
588 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
589 while (! MAIN_FILE_P (map))
591 map = INCLUDED_FROM (line_table, map);
592 pp_verbatim (context->printer,
593 ",\n from %r%s:%d%R", "locus",
594 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
596 pp_verbatim (context->printer, ":");
597 pp_newline (context->printer);
602 void
603 default_diagnostic_starter (diagnostic_context *context,
604 diagnostic_info *diagnostic)
606 diagnostic_report_current_module (context, diagnostic->location);
607 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
608 diagnostic));
611 void
612 default_diagnostic_finalizer (diagnostic_context *context,
613 diagnostic_info *diagnostic)
615 diagnostic_show_locus (context, diagnostic);
616 pp_destroy_prefix (context->printer);
617 pp_newline_and_flush (context->printer);
620 /* Interface to specify diagnostic kind overrides. Returns the
621 previous setting, or DK_UNSPECIFIED if the parameters are out of
622 range. If OPTION_INDEX is zero, the new setting is for all the
623 diagnostics. */
624 diagnostic_t
625 diagnostic_classify_diagnostic (diagnostic_context *context,
626 int option_index,
627 diagnostic_t new_kind,
628 location_t where)
630 diagnostic_t old_kind;
632 if (option_index < 0
633 || option_index >= context->n_opts
634 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
635 return DK_UNSPECIFIED;
637 old_kind = context->classify_diagnostic[option_index];
639 /* Handle pragmas separately, since we need to keep track of *where*
640 the pragmas were. */
641 if (where != UNKNOWN_LOCATION)
643 int i;
645 /* Record the command-line status, so we can reset it back on DK_POP. */
646 if (old_kind == DK_UNSPECIFIED)
648 old_kind = !context->option_enabled (option_index,
649 context->option_state)
650 ? DK_IGNORED : (context->warning_as_error_requested
651 ? DK_ERROR : DK_WARNING);
652 context->classify_diagnostic[option_index] = old_kind;
655 for (i = context->n_classification_history - 1; i >= 0; i --)
656 if (context->classification_history[i].option == option_index)
658 old_kind = context->classification_history[i].kind;
659 break;
662 i = context->n_classification_history;
663 context->classification_history =
664 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
665 * sizeof (diagnostic_classification_change_t));
666 context->classification_history[i].location = where;
667 context->classification_history[i].option = option_index;
668 context->classification_history[i].kind = new_kind;
669 context->n_classification_history ++;
671 else
672 context->classify_diagnostic[option_index] = new_kind;
674 return old_kind;
677 /* Save all diagnostic classifications in a stack. */
678 void
679 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
681 context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
682 context->push_list[context->n_push ++] = context->n_classification_history;
685 /* Restore the topmost classification set off the stack. If the stack
686 is empty, revert to the state based on command line parameters. */
687 void
688 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
690 int jump_to;
691 int i;
693 if (context->n_push)
694 jump_to = context->push_list [-- context->n_push];
695 else
696 jump_to = 0;
698 i = context->n_classification_history;
699 context->classification_history =
700 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
701 * sizeof (diagnostic_classification_change_t));
702 context->classification_history[i].location = where;
703 context->classification_history[i].option = jump_to;
704 context->classification_history[i].kind = DK_POP;
705 context->n_classification_history ++;
708 /* Report a diagnostic message (an error or a warning) as specified by
709 DC. This function is *the* subroutine in terms of which front-ends
710 should implement their specific diagnostic handling modules. The
711 front-end independent format specifiers are exactly those described
712 in the documentation of output_format.
713 Return true if a diagnostic was printed, false otherwise. */
715 bool
716 diagnostic_report_diagnostic (diagnostic_context *context,
717 diagnostic_info *diagnostic)
719 location_t location = diagnostic->location;
720 diagnostic_t orig_diag_kind = diagnostic->kind;
721 const char *saved_format_spec;
723 /* Give preference to being able to inhibit warnings, before they
724 get reclassified to something else. */
725 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
726 && !diagnostic_report_warnings_p (context, location))
727 return false;
729 if (diagnostic->kind == DK_PEDWARN)
731 diagnostic->kind = pedantic_warning_kind (context);
732 /* We do this to avoid giving the message for -pedantic-errors. */
733 orig_diag_kind = diagnostic->kind;
736 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
737 return false;
739 if (context->lock > 0)
741 /* If we're reporting an ICE in the middle of some other error,
742 try to flush out the previous error, then let this one
743 through. Don't do this more than once. */
744 if ((diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
745 && context->lock == 1)
746 pp_newline_and_flush (context->printer);
747 else
748 error_recursion (context);
751 /* If the user requested that warnings be treated as errors, so be
752 it. Note that we do this before the next block so that
753 individual warnings can be overridden back to warnings with
754 -Wno-error=*. */
755 if (context->warning_as_error_requested
756 && diagnostic->kind == DK_WARNING)
758 diagnostic->kind = DK_ERROR;
761 if (diagnostic->option_index
762 && diagnostic->option_index != permissive_error_option (context))
764 diagnostic_t diag_class = DK_UNSPECIFIED;
766 /* This tests if the user provided the appropriate -Wfoo or
767 -Wno-foo option. */
768 if (! context->option_enabled (diagnostic->option_index,
769 context->option_state))
770 return false;
772 /* This tests for #pragma diagnostic changes. */
773 if (context->n_classification_history > 0)
775 /* FIXME: Stupid search. Optimize later. */
776 for (int i = context->n_classification_history - 1; i >= 0; i --)
778 if (linemap_location_before_p
779 (line_table,
780 context->classification_history[i].location,
781 location))
783 if (context->classification_history[i].kind == (int) DK_POP)
785 i = context->classification_history[i].option;
786 continue;
788 int option = context->classification_history[i].option;
789 /* The option 0 is for all the diagnostics. */
790 if (option == 0 || option == diagnostic->option_index)
792 diag_class = context->classification_history[i].kind;
793 if (diag_class != DK_UNSPECIFIED)
794 diagnostic->kind = diag_class;
795 break;
800 /* This tests if the user provided the appropriate -Werror=foo
801 option. */
802 if (diag_class == DK_UNSPECIFIED
803 && context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
805 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
807 /* This allows for future extensions, like temporarily disabling
808 warnings for ranges of source code. */
809 if (diagnostic->kind == DK_IGNORED)
810 return false;
813 context->lock++;
815 if (diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
817 #ifndef ENABLE_CHECKING
818 /* When not checking, ICEs are converted to fatal errors when an
819 error has already occurred. This is counteracted by
820 abort_on_error. */
821 if ((diagnostic_kind_count (context, DK_ERROR) > 0
822 || diagnostic_kind_count (context, DK_SORRY) > 0)
823 && !context->abort_on_error)
825 expanded_location s = expand_location (diagnostic->location);
826 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
827 s.file, s.line);
828 exit (ICE_EXIT_CODE);
830 #endif
831 if (context->internal_error)
832 (*context->internal_error) (context,
833 diagnostic->message.format_spec,
834 diagnostic->message.args_ptr);
836 if (diagnostic->kind == DK_ERROR && orig_diag_kind == DK_WARNING)
837 ++diagnostic_kind_count (context, DK_WERROR);
838 else
839 ++diagnostic_kind_count (context, diagnostic->kind);
841 saved_format_spec = diagnostic->message.format_spec;
842 if (context->show_option_requested)
844 char *option_text;
846 option_text = context->option_name (context, diagnostic->option_index,
847 orig_diag_kind, diagnostic->kind);
849 if (option_text)
851 diagnostic->message.format_spec
852 = ACONCAT ((diagnostic->message.format_spec,
853 " ",
854 "[", option_text, "]",
855 NULL));
856 free (option_text);
859 diagnostic->message.locus = &diagnostic->location;
860 diagnostic->message.x_data = &diagnostic->x_data;
861 diagnostic->x_data = NULL;
862 pp_format (context->printer, &diagnostic->message);
863 (*diagnostic_starter (context)) (context, diagnostic);
864 pp_output_formatted_text (context->printer);
865 (*diagnostic_finalizer (context)) (context, diagnostic);
866 diagnostic_action_after_output (context, diagnostic->kind);
867 diagnostic->message.format_spec = saved_format_spec;
868 diagnostic->x_data = NULL;
870 context->lock--;
872 return true;
875 /* Given a partial pathname as input, return another pathname that
876 shares no directory elements with the pathname of __FILE__. This
877 is used by fancy_abort() to print `Internal compiler error in expr.c'
878 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
880 const char *
881 trim_filename (const char *name)
883 static const char this_file[] = __FILE__;
884 const char *p = name, *q = this_file;
886 /* First skip any "../" in each filename. This allows us to give a proper
887 reference to a file in a subdirectory. */
888 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
889 p += 3;
891 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
892 q += 3;
894 /* Now skip any parts the two filenames have in common. */
895 while (*p == *q && *p != 0 && *q != 0)
896 p++, q++;
898 /* Now go backwards until the previous directory separator. */
899 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
900 p--;
902 return p;
905 /* Standard error reporting routines in increasing order of severity.
906 All of these take arguments like printf. */
908 /* Text to be emitted verbatim to the error message stream; this
909 produces no prefix and disables line-wrapping. Use rarely. */
910 void
911 verbatim (const char *gmsgid, ...)
913 text_info text;
914 va_list ap;
916 va_start (ap, gmsgid);
917 text.err_no = errno;
918 text.args_ptr = &ap;
919 text.format_spec = _(gmsgid);
920 text.locus = NULL;
921 text.x_data = NULL;
922 pp_format_verbatim (global_dc->printer, &text);
923 pp_newline_and_flush (global_dc->printer);
924 va_end (ap);
927 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
928 void
929 diagnostic_append_note (diagnostic_context *context,
930 location_t location,
931 const char * gmsgid, ...)
933 diagnostic_info diagnostic;
934 va_list ap;
935 const char *saved_prefix;
937 va_start (ap, gmsgid);
938 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
939 if (context->inhibit_notes_p)
941 va_end (ap);
942 return;
944 saved_prefix = pp_get_prefix (context->printer);
945 pp_set_prefix (context->printer,
946 diagnostic_build_prefix (context, &diagnostic));
947 pp_newline (context->printer);
948 pp_format (context->printer, &diagnostic.message);
949 pp_output_formatted_text (context->printer);
950 pp_destroy_prefix (context->printer);
951 pp_set_prefix (context->printer, saved_prefix);
952 diagnostic_show_locus (context, &diagnostic);
953 va_end (ap);
956 bool
957 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
958 const char *gmsgid, ...)
960 diagnostic_info diagnostic;
961 va_list ap;
962 bool ret;
964 va_start (ap, gmsgid);
965 if (kind == DK_PERMERROR)
967 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
968 permissive_error_kind (global_dc));
969 diagnostic.option_index = permissive_error_option (global_dc);
971 else {
972 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, kind);
973 if (kind == DK_WARNING || kind == DK_PEDWARN)
974 diagnostic.option_index = opt;
977 ret = report_diagnostic (&diagnostic);
978 va_end (ap);
979 return ret;
982 /* An informative note at LOCATION. Use this for additional details on an error
983 message. */
984 void
985 inform (location_t location, const char *gmsgid, ...)
987 diagnostic_info diagnostic;
988 va_list ap;
990 va_start (ap, gmsgid);
991 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
992 report_diagnostic (&diagnostic);
993 va_end (ap);
996 /* An informative note at LOCATION. Use this for additional details on an
997 error message. */
998 void
999 inform_n (location_t location, int n, const char *singular_gmsgid,
1000 const char *plural_gmsgid, ...)
1002 diagnostic_info diagnostic;
1003 va_list ap;
1005 va_start (ap, plural_gmsgid);
1006 diagnostic_set_info_translated (&diagnostic,
1007 ngettext (singular_gmsgid, plural_gmsgid, n),
1008 &ap, location, DK_NOTE);
1009 report_diagnostic (&diagnostic);
1010 va_end (ap);
1013 /* A warning at INPUT_LOCATION. Use this for code which is correct according
1014 to the relevant language specification but is likely to be buggy anyway.
1015 Returns true if the warning was printed, false if it was inhibited. */
1016 bool
1017 warning (int opt, const char *gmsgid, ...)
1019 diagnostic_info diagnostic;
1020 va_list ap;
1021 bool ret;
1023 va_start (ap, gmsgid);
1024 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
1025 diagnostic.option_index = opt;
1027 ret = report_diagnostic (&diagnostic);
1028 va_end (ap);
1029 return ret;
1032 /* A warning at LOCATION. Use this for code which is correct according to the
1033 relevant language specification but is likely to be buggy anyway.
1034 Returns true if the warning was printed, false if it was inhibited. */
1036 bool
1037 warning_at (location_t location, int opt, const char *gmsgid, ...)
1039 diagnostic_info diagnostic;
1040 va_list ap;
1041 bool ret;
1043 va_start (ap, gmsgid);
1044 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_WARNING);
1045 diagnostic.option_index = opt;
1046 ret = report_diagnostic (&diagnostic);
1047 va_end (ap);
1048 return ret;
1051 /* A warning at LOCATION. Use this for code which is correct according to the
1052 relevant language specification but is likely to be buggy anyway.
1053 Returns true if the warning was printed, false if it was inhibited. */
1055 bool
1056 warning_n (location_t location, int opt, int n, const char *singular_gmsgid,
1057 const char *plural_gmsgid, ...)
1059 diagnostic_info diagnostic;
1060 va_list ap;
1061 bool ret;
1063 va_start (ap, plural_gmsgid);
1064 diagnostic_set_info_translated (&diagnostic,
1065 ngettext (singular_gmsgid, plural_gmsgid, n),
1066 &ap, location, DK_WARNING);
1067 diagnostic.option_index = opt;
1068 ret = report_diagnostic (&diagnostic);
1069 va_end (ap);
1070 return ret;
1073 /* A "pedantic" warning at LOCATION: issues a warning unless
1074 -pedantic-errors was given on the command line, in which case it
1075 issues an error. Use this for diagnostics required by the relevant
1076 language standard, if you have chosen not to make them errors.
1078 Note that these diagnostics are issued independent of the setting
1079 of the -Wpedantic command-line switch. To get a warning enabled
1080 only with that switch, use either "if (pedantic) pedwarn
1081 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
1082 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
1084 Returns true if the warning was printed, false if it was inhibited. */
1086 bool
1087 pedwarn (location_t location, int opt, const char *gmsgid, ...)
1089 diagnostic_info diagnostic;
1090 va_list ap;
1091 bool ret;
1093 va_start (ap, gmsgid);
1094 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_PEDWARN);
1095 diagnostic.option_index = opt;
1096 ret = report_diagnostic (&diagnostic);
1097 va_end (ap);
1098 return ret;
1101 /* A "permissive" error at LOCATION: issues an error unless
1102 -fpermissive was given on the command line, in which case it issues
1103 a warning. Use this for things that really should be errors but we
1104 want to support legacy code.
1106 Returns true if the warning was printed, false if it was inhibited. */
1108 bool
1109 permerror (location_t location, const char *gmsgid, ...)
1111 diagnostic_info diagnostic;
1112 va_list ap;
1113 bool ret;
1115 va_start (ap, gmsgid);
1116 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
1117 permissive_error_kind (global_dc));
1118 diagnostic.option_index = permissive_error_option (global_dc);
1119 ret = report_diagnostic (&diagnostic);
1120 va_end (ap);
1121 return ret;
1124 /* A hard error: the code is definitely ill-formed, and an object file
1125 will not be produced. */
1126 void
1127 error (const char *gmsgid, ...)
1129 diagnostic_info diagnostic;
1130 va_list ap;
1132 va_start (ap, gmsgid);
1133 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
1134 report_diagnostic (&diagnostic);
1135 va_end (ap);
1138 /* A hard error: the code is definitely ill-formed, and an object file
1139 will not be produced. */
1140 void
1141 error_n (location_t location, int n, const char *singular_gmsgid,
1142 const char *plural_gmsgid, ...)
1144 diagnostic_info diagnostic;
1145 va_list ap;
1147 va_start (ap, plural_gmsgid);
1148 diagnostic_set_info_translated (&diagnostic,
1149 ngettext (singular_gmsgid, plural_gmsgid, n),
1150 &ap, location, DK_ERROR);
1151 report_diagnostic (&diagnostic);
1152 va_end (ap);
1155 /* Same as ebove, but use location LOC instead of input_location. */
1156 void
1157 error_at (location_t loc, const char *gmsgid, ...)
1159 diagnostic_info diagnostic;
1160 va_list ap;
1162 va_start (ap, gmsgid);
1163 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_ERROR);
1164 report_diagnostic (&diagnostic);
1165 va_end (ap);
1168 /* "Sorry, not implemented." Use for a language feature which is
1169 required by the relevant specification but not implemented by GCC.
1170 An object file will not be produced. */
1171 void
1172 sorry (const char *gmsgid, ...)
1174 diagnostic_info diagnostic;
1175 va_list ap;
1177 va_start (ap, gmsgid);
1178 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
1179 report_diagnostic (&diagnostic);
1180 va_end (ap);
1183 /* Return true if an error or a "sorry" has been seen. Various
1184 processing is disabled after errors. */
1185 bool
1186 seen_error (void)
1188 return errorcount || sorrycount;
1191 /* An error which is severe enough that we make no attempt to
1192 continue. Do not use this for internal consistency checks; that's
1193 internal_error. Use of this function should be rare. */
1194 void
1195 fatal_error (location_t loc, const char *gmsgid, ...)
1197 diagnostic_info diagnostic;
1198 va_list ap;
1200 va_start (ap, gmsgid);
1201 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_FATAL);
1202 report_diagnostic (&diagnostic);
1203 va_end (ap);
1205 gcc_unreachable ();
1208 /* An internal consistency check has failed. We make no attempt to
1209 continue. Note that unless there is debugging value to be had from
1210 a more specific message, or some other good reason, you should use
1211 abort () instead of calling this function directly. */
1212 void
1213 internal_error (const char *gmsgid, ...)
1215 diagnostic_info diagnostic;
1216 va_list ap;
1218 va_start (ap, gmsgid);
1219 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
1220 report_diagnostic (&diagnostic);
1221 va_end (ap);
1223 gcc_unreachable ();
1226 /* Like internal_error, but no backtrace will be printed. Used when
1227 the internal error does not happen at the current location, but happened
1228 somewhere else. */
1229 void
1230 internal_error_no_backtrace (const char *gmsgid, ...)
1232 diagnostic_info diagnostic;
1233 va_list ap;
1235 va_start (ap, gmsgid);
1236 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE_NOBT);
1237 report_diagnostic (&diagnostic);
1238 va_end (ap);
1240 gcc_unreachable ();
1243 /* Special case error functions. Most are implemented in terms of the
1244 above, or should be. */
1246 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1247 runs its second argument through gettext. */
1248 void
1249 fnotice (FILE *file, const char *cmsgid, ...)
1251 va_list ap;
1253 va_start (ap, cmsgid);
1254 vfprintf (file, _(cmsgid), ap);
1255 va_end (ap);
1258 /* Inform the user that an error occurred while trying to report some
1259 other error. This indicates catastrophic internal inconsistencies,
1260 so give up now. But do try to flush out the previous error.
1261 This mustn't use internal_error, that will cause infinite recursion. */
1263 static void
1264 error_recursion (diagnostic_context *context)
1266 if (context->lock < 3)
1267 pp_newline_and_flush (context->printer);
1269 fnotice (stderr,
1270 "Internal compiler error: Error reporting routines re-entered.\n");
1272 /* Call diagnostic_action_after_output to get the "please submit a bug
1273 report" message. */
1274 diagnostic_action_after_output (context, DK_ICE);
1276 /* Do not use gcc_unreachable here; that goes through internal_error
1277 and therefore would cause infinite recursion. */
1278 real_abort ();
1281 /* Report an internal compiler error in a friendly manner. This is
1282 the function that gets called upon use of abort() in the source
1283 code generally, thanks to a special macro. */
1285 void
1286 fancy_abort (const char *file, int line, const char *function)
1288 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1291 /* Really call the system 'abort'. This has to go right at the end of
1292 this file, so that there are no functions after it that call abort
1293 and get the system abort instead of our macro. */
1294 #undef abort
1295 static void
1296 real_abort (void)
1298 abort ();