* gcc.dg/vect/slp-9.c: Disable einline.
[official-gcc.git] / gcc / diagnostic.c
blob7cbdb797aed87ff53eeb1359689e2ef2d371051d
1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999-2014 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 vasprintf (&str, 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 context->caret_char = '^';
150 context->show_option_requested = false;
151 context->abort_on_error = false;
152 context->show_column = false;
153 context->pedantic_errors = false;
154 context->permissive = false;
155 context->opt_permissive = 0;
156 context->fatal_errors = false;
157 context->dc_inhibit_warnings = false;
158 context->dc_warn_system_headers = false;
159 context->max_errors = 0;
160 context->internal_error = NULL;
161 diagnostic_starter (context) = default_diagnostic_starter;
162 diagnostic_finalizer (context) = default_diagnostic_finalizer;
163 context->option_enabled = NULL;
164 context->option_state = NULL;
165 context->option_name = NULL;
166 context->last_location = UNKNOWN_LOCATION;
167 context->last_module = 0;
168 context->x_data = NULL;
169 context->lock = 0;
170 context->inhibit_notes_p = false;
173 /* Maybe initialize the color support. We require clients to do this
174 explicitly, since most clients don't want color. When called
175 without a VALUE, it initializes with DIAGNOSTICS_COLOR_DEFAULT. */
177 void
178 diagnostic_color_init (diagnostic_context *context, int value /*= -1 */)
180 /* value == -1 is the default value. */
181 if (value < 0)
183 /* If DIAGNOSTICS_COLOR_DEFAULT is -1, default to
184 -fdiagnostics-color=auto if GCC_COLORS is in the environment,
185 otherwise default to -fdiagnostics-color=never, for other
186 values default to that
187 -fdiagnostics-color={never,auto,always}. */
188 if (DIAGNOSTICS_COLOR_DEFAULT == -1)
190 if (!getenv ("GCC_COLORS"))
191 return;
192 value = DIAGNOSTICS_COLOR_AUTO;
194 else
195 value = DIAGNOSTICS_COLOR_DEFAULT;
197 pp_show_color (context->printer)
198 = colorize_init ((diagnostic_color_rule_t) value);
201 /* Do any cleaning up required after the last diagnostic is emitted. */
203 void
204 diagnostic_finish (diagnostic_context *context)
206 /* Some of the errors may actually have been warnings. */
207 if (context->some_warnings_are_errors)
209 /* -Werror was given. */
210 if (context->warning_as_error_requested)
211 pp_verbatim (context->printer,
212 _("%s: all warnings being treated as errors"),
213 progname);
214 /* At least one -Werror= was given. */
215 else
216 pp_verbatim (context->printer,
217 _("%s: some warnings being treated as errors"),
218 progname);
219 pp_newline_and_flush (context->printer);
222 diagnostic_file_cache_fini ();
224 XDELETEVEC (context->classify_diagnostic);
225 context->classify_diagnostic = NULL;
227 /* diagnostic_initialize allocates context->printer using XNEW
228 and placement-new. */
229 context->printer->~pretty_printer ();
230 XDELETE (context->printer);
231 context->printer = NULL;
234 /* Initialize DIAGNOSTIC, where the message MSG has already been
235 translated. */
236 void
237 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
238 va_list *args, location_t location,
239 diagnostic_t kind)
241 diagnostic->message.err_no = errno;
242 diagnostic->message.args_ptr = args;
243 diagnostic->message.format_spec = msg;
244 diagnostic->location = location;
245 diagnostic->override_column = 0;
246 diagnostic->kind = kind;
247 diagnostic->option_index = 0;
250 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
251 translated. */
252 void
253 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
254 va_list *args, location_t location,
255 diagnostic_t kind)
257 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
260 /* Return a malloc'd string describing a location. The caller is
261 responsible for freeing the memory. */
262 char *
263 diagnostic_build_prefix (diagnostic_context *context,
264 const diagnostic_info *diagnostic)
266 static const char *const diagnostic_kind_text[] = {
267 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
268 #include "diagnostic.def"
269 #undef DEFINE_DIAGNOSTIC_KIND
270 "must-not-happen"
272 static const char *const diagnostic_kind_color[] = {
273 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
274 #include "diagnostic.def"
275 #undef DEFINE_DIAGNOSTIC_KIND
276 NULL
278 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
280 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
281 const char *text_cs = "", *text_ce = "";
282 const char *locus_cs, *locus_ce;
283 pretty_printer *pp = context->printer;
285 if (diagnostic_kind_color[diagnostic->kind])
287 text_cs = colorize_start (pp_show_color (pp),
288 diagnostic_kind_color[diagnostic->kind]);
289 text_ce = colorize_stop (pp_show_color (pp));
291 locus_cs = colorize_start (pp_show_color (pp), "locus");
292 locus_ce = colorize_stop (pp_show_color (pp));
294 expanded_location s = diagnostic_expand_location (diagnostic);
295 return
296 (s.file == NULL
297 ? build_message_string ("%s%s:%s %s%s%s", locus_cs, progname, locus_ce,
298 text_cs, text, text_ce)
299 : !strcmp (s.file, N_("<built-in>"))
300 ? build_message_string ("%s%s:%s %s%s%s", locus_cs, s.file, locus_ce,
301 text_cs, text, text_ce)
302 : context->show_column
303 ? build_message_string ("%s%s:%d:%d:%s %s%s%s", locus_cs, s.file, s.line,
304 s.column, locus_ce, text_cs, text, text_ce)
305 : build_message_string ("%s%s:%d:%s %s%s%s", locus_cs, s.file, s.line,
306 locus_ce, text_cs, text, text_ce));
309 /* If LINE is longer than MAX_WIDTH, and COLUMN is not smaller than
310 MAX_WIDTH by some margin, then adjust the start of the line such
311 that the COLUMN is smaller than MAX_WIDTH minus the margin. The
312 margin is either 10 characters or the difference between the column
313 and the length of the line, whatever is smaller. The length of
314 LINE is given by LINE_WIDTH. */
315 static const char *
316 adjust_line (const char *line, int line_width,
317 int max_width, int *column_p)
319 int right_margin = 10;
320 int column = *column_p;
322 gcc_checking_assert (line_width >= column);
323 right_margin = MIN (line_width - column, right_margin);
324 right_margin = max_width - right_margin;
325 if (line_width >= max_width && column > right_margin)
327 line += column - right_margin;
328 *column_p = right_margin;
330 return line;
333 /* Print the physical source line corresponding to the location of
334 this diagnostic, and a caret indicating the precise column. */
335 void
336 diagnostic_show_locus (diagnostic_context * context,
337 const diagnostic_info *diagnostic)
339 const char *line;
340 int line_width;
341 char *buffer;
342 expanded_location s;
343 int max_width;
344 const char *saved_prefix;
345 const char *caret_cs, *caret_ce;
347 if (!context->show_caret
348 || diagnostic->location <= BUILTINS_LOCATION
349 || diagnostic->location == context->last_location)
350 return;
352 context->last_location = diagnostic->location;
353 s = diagnostic_expand_location (diagnostic);
354 line = location_get_source_line (s, &line_width);
355 if (line == NULL || s.column > line_width)
356 return;
358 max_width = context->caret_max_width;
359 line = adjust_line (line, line_width, max_width, &(s.column));
361 pp_newline (context->printer);
362 saved_prefix = pp_get_prefix (context->printer);
363 pp_set_prefix (context->printer, NULL);
364 pp_space (context->printer);
365 while (max_width > 0 && line_width > 0)
367 char c = *line == '\t' ? ' ' : *line;
368 if (c == '\0')
369 c = ' ';
370 pp_character (context->printer, c);
371 max_width--;
372 line_width--;
373 line++;
375 pp_newline (context->printer);
376 caret_cs = colorize_start (pp_show_color (context->printer), "caret");
377 caret_ce = colorize_stop (pp_show_color (context->printer));
379 /* pp_printf does not implement %*c. */
380 size_t len = s.column + 3 + strlen (caret_cs) + strlen (caret_ce);
381 buffer = XALLOCAVEC (char, len);
382 snprintf (buffer, len, "%s %*c%s", caret_cs, s.column, context->caret_char,
383 caret_ce);
384 pp_string (context->printer, buffer);
385 pp_set_prefix (context->printer, saved_prefix);
386 pp_needs_newline (context->printer) = true;
389 /* Functions at which to stop the backtrace print. It's not
390 particularly helpful to print the callers of these functions. */
392 static const char * const bt_stop[] =
394 "main",
395 "toplev::main",
396 "execute_one_pass",
397 "compile_file",
400 /* A callback function passed to the backtrace_full function. */
402 static int
403 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
404 const char *function)
406 int *pcount = (int *) data;
408 /* If we don't have any useful information, don't print
409 anything. */
410 if (filename == NULL && function == NULL)
411 return 0;
413 /* Skip functions in diagnostic.c. */
414 if (*pcount == 0
415 && filename != NULL
416 && strcmp (lbasename (filename), "diagnostic.c") == 0)
417 return 0;
419 /* Print up to 20 functions. We could make this a --param, but
420 since this is only for debugging just use a constant for now. */
421 if (*pcount >= 20)
423 /* Returning a non-zero value stops the backtrace. */
424 return 1;
426 ++*pcount;
428 char *alc = NULL;
429 if (function != NULL)
431 char *str = cplus_demangle_v3 (function,
432 (DMGL_VERBOSE | DMGL_ANSI
433 | DMGL_GNU_V3 | DMGL_PARAMS));
434 if (str != NULL)
436 alc = str;
437 function = str;
440 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
442 size_t len = strlen (bt_stop[i]);
443 if (strncmp (function, bt_stop[i], len) == 0
444 && (function[len] == '\0' || function[len] == '('))
446 if (alc != NULL)
447 free (alc);
448 /* Returning a non-zero value stops the backtrace. */
449 return 1;
454 fprintf (stderr, "0x%lx %s\n\t%s:%d\n",
455 (unsigned long) pc,
456 function == NULL ? "???" : function,
457 filename == NULL ? "???" : filename,
458 lineno);
460 if (alc != NULL)
461 free (alc);
463 return 0;
466 /* A callback function passed to the backtrace_full function. This is
467 called if backtrace_full has an error. */
469 static void
470 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
472 if (errnum < 0)
474 /* This means that no debug info was available. Just quietly
475 skip printing backtrace info. */
476 return;
478 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
479 errnum == 0 ? "" : xstrerror (errnum));
482 /* Take any action which is expected to happen after the diagnostic
483 is written out. This function does not always return. */
484 void
485 diagnostic_action_after_output (diagnostic_context *context,
486 diagnostic_t diag_kind)
488 switch (diag_kind)
490 case DK_DEBUG:
491 case DK_NOTE:
492 case DK_ANACHRONISM:
493 case DK_WARNING:
494 break;
496 case DK_ERROR:
497 case DK_SORRY:
498 if (context->abort_on_error)
499 real_abort ();
500 if (context->fatal_errors)
502 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
503 diagnostic_finish (context);
504 exit (FATAL_EXIT_CODE);
506 if (context->max_errors != 0
507 && ((unsigned) (diagnostic_kind_count (context, DK_ERROR)
508 + diagnostic_kind_count (context, DK_SORRY)
509 + diagnostic_kind_count (context, DK_WERROR))
510 >= context->max_errors))
512 fnotice (stderr,
513 "compilation terminated due to -fmax-errors=%u.\n",
514 context->max_errors);
515 diagnostic_finish (context);
516 exit (FATAL_EXIT_CODE);
518 break;
520 case DK_ICE:
522 struct backtrace_state *state =
523 backtrace_create_state (NULL, 0, bt_err_callback, NULL);
524 int count = 0;
525 if (state != NULL)
526 backtrace_full (state, 2, bt_callback, bt_err_callback,
527 (void *) &count);
529 if (context->abort_on_error)
530 real_abort ();
532 fnotice (stderr, "Please submit a full bug report,\n"
533 "with preprocessed source if appropriate.\n");
534 if (count > 0)
535 fnotice (stderr,
536 ("Please include the complete backtrace "
537 "with any bug report.\n"));
538 fnotice (stderr, "See %s for instructions.\n", bug_report_url);
540 exit (ICE_EXIT_CODE);
543 case DK_FATAL:
544 if (context->abort_on_error)
545 real_abort ();
546 diagnostic_finish (context);
547 fnotice (stderr, "compilation terminated.\n");
548 exit (FATAL_EXIT_CODE);
550 default:
551 gcc_unreachable ();
555 void
556 diagnostic_report_current_module (diagnostic_context *context, location_t where)
558 const struct line_map *map = NULL;
560 if (pp_needs_newline (context->printer))
562 pp_newline (context->printer);
563 pp_needs_newline (context->printer) = false;
566 if (where <= BUILTINS_LOCATION)
567 return;
569 linemap_resolve_location (line_table, where,
570 LRK_MACRO_DEFINITION_LOCATION,
571 &map);
573 if (map && diagnostic_last_module_changed (context, map))
575 diagnostic_set_last_module (context, map);
576 if (! MAIN_FILE_P (map))
578 map = INCLUDED_FROM (line_table, map);
579 if (context->show_column)
580 pp_verbatim (context->printer,
581 "In file included from %r%s:%d:%d%R", "locus",
582 LINEMAP_FILE (map),
583 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
584 else
585 pp_verbatim (context->printer,
586 "In file included from %r%s:%d%R", "locus",
587 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
588 while (! MAIN_FILE_P (map))
590 map = INCLUDED_FROM (line_table, map);
591 pp_verbatim (context->printer,
592 ",\n from %r%s:%d%R", "locus",
593 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
595 pp_verbatim (context->printer, ":");
596 pp_newline (context->printer);
601 void
602 default_diagnostic_starter (diagnostic_context *context,
603 diagnostic_info *diagnostic)
605 diagnostic_report_current_module (context, diagnostic->location);
606 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
607 diagnostic));
610 void
611 default_diagnostic_finalizer (diagnostic_context *context,
612 diagnostic_info *diagnostic)
614 diagnostic_show_locus (context, diagnostic);
615 pp_destroy_prefix (context->printer);
616 pp_newline_and_flush (context->printer);
619 /* Interface to specify diagnostic kind overrides. Returns the
620 previous setting, or DK_UNSPECIFIED if the parameters are out of
621 range. If OPTION_INDEX is zero, the new setting is for all the
622 diagnostics. */
623 diagnostic_t
624 diagnostic_classify_diagnostic (diagnostic_context *context,
625 int option_index,
626 diagnostic_t new_kind,
627 location_t where)
629 diagnostic_t old_kind;
631 if (option_index < 0
632 || option_index >= context->n_opts
633 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
634 return DK_UNSPECIFIED;
636 old_kind = context->classify_diagnostic[option_index];
638 /* Handle pragmas separately, since we need to keep track of *where*
639 the pragmas were. */
640 if (where != UNKNOWN_LOCATION)
642 int i;
644 /* Record the command-line status, so we can reset it back on DK_POP. */
645 if (old_kind == DK_UNSPECIFIED)
647 old_kind = context->option_enabled (option_index,
648 context->option_state)
649 ? DK_WARNING : DK_IGNORED;
650 context->classify_diagnostic[option_index] = old_kind;
653 for (i = context->n_classification_history - 1; i >= 0; i --)
654 if (context->classification_history[i].option == option_index)
656 old_kind = context->classification_history[i].kind;
657 break;
660 i = context->n_classification_history;
661 context->classification_history =
662 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
663 * sizeof (diagnostic_classification_change_t));
664 context->classification_history[i].location = where;
665 context->classification_history[i].option = option_index;
666 context->classification_history[i].kind = new_kind;
667 context->n_classification_history ++;
669 else
670 context->classify_diagnostic[option_index] = new_kind;
672 return old_kind;
675 /* Save all diagnostic classifications in a stack. */
676 void
677 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
679 context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
680 context->push_list[context->n_push ++] = context->n_classification_history;
683 /* Restore the topmost classification set off the stack. If the stack
684 is empty, revert to the state based on command line parameters. */
685 void
686 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
688 int jump_to;
689 int i;
691 if (context->n_push)
692 jump_to = context->push_list [-- context->n_push];
693 else
694 jump_to = 0;
696 i = context->n_classification_history;
697 context->classification_history =
698 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
699 * sizeof (diagnostic_classification_change_t));
700 context->classification_history[i].location = where;
701 context->classification_history[i].option = jump_to;
702 context->classification_history[i].kind = DK_POP;
703 context->n_classification_history ++;
706 /* Report a diagnostic message (an error or a warning) as specified by
707 DC. This function is *the* subroutine in terms of which front-ends
708 should implement their specific diagnostic handling modules. The
709 front-end independent format specifiers are exactly those described
710 in the documentation of output_format.
711 Return true if a diagnostic was printed, false otherwise. */
713 bool
714 diagnostic_report_diagnostic (diagnostic_context *context,
715 diagnostic_info *diagnostic)
717 location_t location = diagnostic->location;
718 diagnostic_t orig_diag_kind = diagnostic->kind;
719 const char *saved_format_spec;
721 /* Give preference to being able to inhibit warnings, before they
722 get reclassified to something else. */
723 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
724 && !diagnostic_report_warnings_p (context, location))
725 return false;
727 if (diagnostic->kind == DK_PEDWARN)
729 diagnostic->kind = pedantic_warning_kind (context);
730 /* We do this to avoid giving the message for -pedantic-errors. */
731 orig_diag_kind = diagnostic->kind;
734 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
735 return false;
737 if (context->lock > 0)
739 /* If we're reporting an ICE in the middle of some other error,
740 try to flush out the previous error, then let this one
741 through. Don't do this more than once. */
742 if (diagnostic->kind == DK_ICE && context->lock == 1)
743 pp_newline_and_flush (context->printer);
744 else
745 error_recursion (context);
748 /* If the user requested that warnings be treated as errors, so be
749 it. Note that we do this before the next block so that
750 individual warnings can be overridden back to warnings with
751 -Wno-error=*. */
752 if (context->warning_as_error_requested
753 && diagnostic->kind == DK_WARNING)
755 diagnostic->kind = DK_ERROR;
758 if (diagnostic->option_index
759 && diagnostic->option_index != permissive_error_option (context))
761 diagnostic_t diag_class = DK_UNSPECIFIED;
763 /* This tests if the user provided the appropriate -Wfoo or
764 -Wno-foo option. */
765 if (! context->option_enabled (diagnostic->option_index,
766 context->option_state))
767 return false;
769 /* This tests for #pragma diagnostic changes. */
770 if (context->n_classification_history > 0)
772 /* FIXME: Stupid search. Optimize later. */
773 for (int i = context->n_classification_history - 1; i >= 0; i --)
775 if (linemap_location_before_p
776 (line_table,
777 context->classification_history[i].location,
778 location))
780 if (context->classification_history[i].kind == (int) DK_POP)
782 i = context->classification_history[i].option;
783 continue;
785 int option = context->classification_history[i].option;
786 /* The option 0 is for all the diagnostics. */
787 if (option == 0 || option == diagnostic->option_index)
789 diag_class = context->classification_history[i].kind;
790 if (diag_class != DK_UNSPECIFIED)
791 diagnostic->kind = diag_class;
792 break;
797 /* This tests if the user provided the appropriate -Werror=foo
798 option. */
799 if (diag_class == DK_UNSPECIFIED
800 && context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
802 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
804 /* This allows for future extensions, like temporarily disabling
805 warnings for ranges of source code. */
806 if (diagnostic->kind == DK_IGNORED)
807 return false;
810 if (orig_diag_kind == DK_WARNING && diagnostic->kind == DK_ERROR)
811 context->some_warnings_are_errors = true;
813 context->lock++;
815 if (diagnostic->kind == DK_ICE)
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 (const char *gmsgid, ...)
1197 diagnostic_info diagnostic;
1198 va_list ap;
1200 va_start (ap, gmsgid);
1201 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
1202 report_diagnostic (&diagnostic);
1203 va_end (ap);
1205 gcc_unreachable ();
1208 /* An error which is severe enough that we make no attempt to
1209 continue. Do not use this for internal consistency checks; that's
1210 internal_error. Use of this function should be rare. */
1211 void
1212 fatal_error (location_t loc, const char *gmsgid, ...)
1214 diagnostic_info diagnostic;
1215 va_list ap;
1217 va_start (ap, gmsgid);
1218 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_FATAL);
1219 report_diagnostic (&diagnostic);
1220 va_end (ap);
1222 gcc_unreachable ();
1225 /* An internal consistency check has failed. We make no attempt to
1226 continue. Note that unless there is debugging value to be had from
1227 a more specific message, or some other good reason, you should use
1228 abort () instead of calling this function directly. */
1229 void
1230 internal_error (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);
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 ();