]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/diagnostic.c
Update copyright years.
[thirdparty/gcc.git] / gcc / diagnostic.c
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>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21
22 /* This file implements the language independent aspect of diagnostic
23 message module. */
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "version.h"
29 #include "demangle.h"
30 #include "input.h"
31 #include "intl.h"
32 #include "backtrace.h"
33 #include "diagnostic.h"
34 #include "diagnostic-color.h"
35
36 #ifdef HAVE_TERMIOS_H
37 # include <termios.h>
38 #endif
39
40 #ifdef GWINSZ_IN_SYS_IOCTL
41 # include <sys/ioctl.h>
42 #endif
43
44 #include <new> // For placement new.
45
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)
50
51 /* Prototypes. */
52 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
53
54 static void real_abort (void) ATTRIBUTE_NORETURN;
55
56 /* Name of program invoked, sans directories. */
57
58 const char *progname;
59
60 /* A diagnostic_context surrogate for stderr. */
61 static diagnostic_context global_diagnostic_context;
62 diagnostic_context *global_dc = &global_diagnostic_context;
63 \f
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, ...)
68 {
69 char *str;
70 va_list ap;
71
72 va_start (ap, msg);
73 str = xvasprintf (msg, ap);
74 va_end (ap);
75
76 return str;
77 }
78
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)
82 {
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);
87 }
88
89
90 \f
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)
96 {
97 const char * s = getenv ("COLUMNS");
98 if (s != NULL) {
99 int n = atoi (s);
100 if (n > 0)
101 return n;
102 }
103
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
110
111 return INT_MAX;
112 }
113
114 /* Set caret_max_width to value. */
115 void
116 diagnostic_set_caret_max_width (diagnostic_context *context, int value)
117 {
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);
122
123 if (value <= 0)
124 value = INT_MAX;
125
126 context->caret_max_width = value;
127 }
128
129 /* Initialize the diagnostic message outputting machinery. */
130 void
131 diagnostic_initialize (diagnostic_context *context, int n_opts)
132 {
133 int i;
134
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 ();
139
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;
171 }
172
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. */
176
177 void
178 diagnostic_color_init (diagnostic_context *context, int value /*= -1 */)
179 {
180 /* value == -1 is the default value. */
181 if (value < 0)
182 {
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)
189 {
190 if (!getenv ("GCC_COLORS"))
191 return;
192 value = DIAGNOSTICS_COLOR_AUTO;
193 }
194 else
195 value = DIAGNOSTICS_COLOR_DEFAULT;
196 }
197 pp_show_color (context->printer)
198 = colorize_init ((diagnostic_color_rule_t) value);
199 }
200
201 /* Do any cleaning up required after the last diagnostic is emitted. */
202
203 void
204 diagnostic_finish (diagnostic_context *context)
205 {
206 /* Some of the errors may actually have been warnings. */
207 if (context->some_warnings_are_errors)
208 {
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);
220 }
221
222 diagnostic_file_cache_fini ();
223
224 XDELETEVEC (context->classify_diagnostic);
225 context->classify_diagnostic = NULL;
226
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;
232 }
233
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)
240 {
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;
248 }
249
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)
256 {
257 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
258 }
259
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)
265 {
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"
271 };
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
277 };
278 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
279
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;
284
285 if (diagnostic_kind_color[diagnostic->kind])
286 {
287 text_cs = colorize_start (pp_show_color (pp),
288 diagnostic_kind_color[diagnostic->kind]);
289 text_ce = colorize_stop (pp_show_color (pp));
290 }
291 locus_cs = colorize_start (pp_show_color (pp), "locus");
292 locus_ce = colorize_stop (pp_show_color (pp));
293
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));
307 }
308
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)
318 {
319 int right_margin = 10;
320 int column = *column_p;
321
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)
326 {
327 line += column - right_margin;
328 *column_p = right_margin;
329 }
330 return line;
331 }
332
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)
338 {
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;
346
347 if (!context->show_caret
348 || diagnostic->location <= BUILTINS_LOCATION
349 || diagnostic->location == context->last_location)
350 return;
351
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;
357
358 max_width = context->caret_max_width;
359 line = adjust_line (line, line_width, max_width, &(s.column));
360
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)
366 {
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++;
374 }
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));
378
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;
387 }
388
389 /* Functions at which to stop the backtrace print. It's not
390 particularly helpful to print the callers of these functions. */
391
392 static const char * const bt_stop[] =
393 {
394 "main",
395 "toplev::main",
396 "execute_one_pass",
397 "compile_file",
398 };
399
400 /* A callback function passed to the backtrace_full function. */
401
402 static int
403 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
404 const char *function)
405 {
406 int *pcount = (int *) data;
407
408 /* If we don't have any useful information, don't print
409 anything. */
410 if (filename == NULL && function == NULL)
411 return 0;
412
413 /* Skip functions in diagnostic.c. */
414 if (*pcount == 0
415 && filename != NULL
416 && strcmp (lbasename (filename), "diagnostic.c") == 0)
417 return 0;
418
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)
422 {
423 /* Returning a non-zero value stops the backtrace. */
424 return 1;
425 }
426 ++*pcount;
427
428 char *alc = NULL;
429 if (function != NULL)
430 {
431 char *str = cplus_demangle_v3 (function,
432 (DMGL_VERBOSE | DMGL_ANSI
433 | DMGL_GNU_V3 | DMGL_PARAMS));
434 if (str != NULL)
435 {
436 alc = str;
437 function = str;
438 }
439
440 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
441 {
442 size_t len = strlen (bt_stop[i]);
443 if (strncmp (function, bt_stop[i], len) == 0
444 && (function[len] == '\0' || function[len] == '('))
445 {
446 if (alc != NULL)
447 free (alc);
448 /* Returning a non-zero value stops the backtrace. */
449 return 1;
450 }
451 }
452 }
453
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);
459
460 if (alc != NULL)
461 free (alc);
462
463 return 0;
464 }
465
466 /* A callback function passed to the backtrace_full function. This is
467 called if backtrace_full has an error. */
468
469 static void
470 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
471 {
472 if (errnum < 0)
473 {
474 /* This means that no debug info was available. Just quietly
475 skip printing backtrace info. */
476 return;
477 }
478 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
479 errnum == 0 ? "" : xstrerror (errnum));
480 }
481
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)
487 {
488 switch (diag_kind)
489 {
490 case DK_DEBUG:
491 case DK_NOTE:
492 case DK_ANACHRONISM:
493 case DK_WARNING:
494 break;
495
496 case DK_ERROR:
497 case DK_SORRY:
498 if (context->abort_on_error)
499 real_abort ();
500 if (context->fatal_errors)
501 {
502 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
503 diagnostic_finish (context);
504 exit (FATAL_EXIT_CODE);
505 }
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))
511 {
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);
517 }
518 break;
519
520 case DK_ICE:
521 {
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);
528
529 if (context->abort_on_error)
530 real_abort ();
531
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);
539
540 exit (ICE_EXIT_CODE);
541 }
542
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);
549
550 default:
551 gcc_unreachable ();
552 }
553 }
554
555 void
556 diagnostic_report_current_module (diagnostic_context *context, location_t where)
557 {
558 const struct line_map *map = NULL;
559
560 if (pp_needs_newline (context->printer))
561 {
562 pp_newline (context->printer);
563 pp_needs_newline (context->printer) = false;
564 }
565
566 if (where <= BUILTINS_LOCATION)
567 return;
568
569 linemap_resolve_location (line_table, where,
570 LRK_MACRO_DEFINITION_LOCATION,
571 &map);
572
573 if (map && diagnostic_last_module_changed (context, map))
574 {
575 diagnostic_set_last_module (context, map);
576 if (! MAIN_FILE_P (map))
577 {
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))
589 {
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));
594 }
595 pp_verbatim (context->printer, ":");
596 pp_newline (context->printer);
597 }
598 }
599 }
600
601 void
602 default_diagnostic_starter (diagnostic_context *context,
603 diagnostic_info *diagnostic)
604 {
605 diagnostic_report_current_module (context, diagnostic->location);
606 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
607 diagnostic));
608 }
609
610 void
611 default_diagnostic_finalizer (diagnostic_context *context,
612 diagnostic_info *diagnostic)
613 {
614 diagnostic_show_locus (context, diagnostic);
615 pp_destroy_prefix (context->printer);
616 pp_newline_and_flush (context->printer);
617 }
618
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)
628 {
629 diagnostic_t old_kind;
630
631 if (option_index < 0
632 || option_index >= context->n_opts
633 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
634 return DK_UNSPECIFIED;
635
636 old_kind = context->classify_diagnostic[option_index];
637
638 /* Handle pragmas separately, since we need to keep track of *where*
639 the pragmas were. */
640 if (where != UNKNOWN_LOCATION)
641 {
642 int i;
643
644 /* Record the command-line status, so we can reset it back on DK_POP. */
645 if (old_kind == DK_UNSPECIFIED)
646 {
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;
651 }
652
653 for (i = context->n_classification_history - 1; i >= 0; i --)
654 if (context->classification_history[i].option == option_index)
655 {
656 old_kind = context->classification_history[i].kind;
657 break;
658 }
659
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 ++;
668 }
669 else
670 context->classify_diagnostic[option_index] = new_kind;
671
672 return old_kind;
673 }
674
675 /* Save all diagnostic classifications in a stack. */
676 void
677 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
678 {
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;
681 }
682
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)
687 {
688 int jump_to;
689 int i;
690
691 if (context->n_push)
692 jump_to = context->push_list [-- context->n_push];
693 else
694 jump_to = 0;
695
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 ++;
704 }
705
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. */
712
713 bool
714 diagnostic_report_diagnostic (diagnostic_context *context,
715 diagnostic_info *diagnostic)
716 {
717 location_t location = diagnostic->location;
718 diagnostic_t orig_diag_kind = diagnostic->kind;
719 const char *saved_format_spec;
720
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;
726
727 if (diagnostic->kind == DK_PEDWARN)
728 {
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;
732 }
733
734 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
735 return false;
736
737 if (context->lock > 0)
738 {
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);
746 }
747
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)
754 {
755 diagnostic->kind = DK_ERROR;
756 }
757
758 if (diagnostic->option_index
759 && diagnostic->option_index != permissive_error_option (context))
760 {
761 diagnostic_t diag_class = DK_UNSPECIFIED;
762
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;
768
769 /* This tests for #pragma diagnostic changes. */
770 if (context->n_classification_history > 0)
771 {
772 /* FIXME: Stupid search. Optimize later. */
773 for (int i = context->n_classification_history - 1; i >= 0; i --)
774 {
775 if (linemap_location_before_p
776 (line_table,
777 context->classification_history[i].location,
778 location))
779 {
780 if (context->classification_history[i].kind == (int) DK_POP)
781 {
782 i = context->classification_history[i].option;
783 continue;
784 }
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)
788 {
789 diag_class = context->classification_history[i].kind;
790 if (diag_class != DK_UNSPECIFIED)
791 diagnostic->kind = diag_class;
792 break;
793 }
794 }
795 }
796 }
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)
801 {
802 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
803 }
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;
808 }
809
810 if (orig_diag_kind == DK_WARNING && diagnostic->kind == DK_ERROR)
811 context->some_warnings_are_errors = true;
812
813 context->lock++;
814
815 if (diagnostic->kind == DK_ICE)
816 {
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)
824 {
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);
829 }
830 #endif
831 if (context->internal_error)
832 (*context->internal_error) (context,
833 diagnostic->message.format_spec,
834 diagnostic->message.args_ptr);
835 }
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);
840
841 saved_format_spec = diagnostic->message.format_spec;
842 if (context->show_option_requested)
843 {
844 char *option_text;
845
846 option_text = context->option_name (context, diagnostic->option_index,
847 orig_diag_kind, diagnostic->kind);
848
849 if (option_text)
850 {
851 diagnostic->message.format_spec
852 = ACONCAT ((diagnostic->message.format_spec,
853 " ",
854 "[", option_text, "]",
855 NULL));
856 free (option_text);
857 }
858 }
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;
869
870 context->lock--;
871
872 return true;
873 }
874
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'. */
879
880 const char *
881 trim_filename (const char *name)
882 {
883 static const char this_file[] = __FILE__;
884 const char *p = name, *q = this_file;
885
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;
890
891 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
892 q += 3;
893
894 /* Now skip any parts the two filenames have in common. */
895 while (*p == *q && *p != 0 && *q != 0)
896 p++, q++;
897
898 /* Now go backwards until the previous directory separator. */
899 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
900 p--;
901
902 return p;
903 }
904 \f
905 /* Standard error reporting routines in increasing order of severity.
906 All of these take arguments like printf. */
907
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, ...)
912 {
913 text_info text;
914 va_list ap;
915
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);
925 }
926
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, ...)
932 {
933 diagnostic_info diagnostic;
934 va_list ap;
935 const char *saved_prefix;
936
937 va_start (ap, gmsgid);
938 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
939 if (context->inhibit_notes_p)
940 {
941 va_end (ap);
942 return;
943 }
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);
954 }
955
956 bool
957 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
958 const char *gmsgid, ...)
959 {
960 diagnostic_info diagnostic;
961 va_list ap;
962 bool ret;
963
964 va_start (ap, gmsgid);
965 if (kind == DK_PERMERROR)
966 {
967 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
968 permissive_error_kind (global_dc));
969 diagnostic.option_index = permissive_error_option (global_dc);
970 }
971 else {
972 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, kind);
973 if (kind == DK_WARNING || kind == DK_PEDWARN)
974 diagnostic.option_index = opt;
975 }
976
977 ret = report_diagnostic (&diagnostic);
978 va_end (ap);
979 return ret;
980 }
981
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, ...)
986 {
987 diagnostic_info diagnostic;
988 va_list ap;
989
990 va_start (ap, gmsgid);
991 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
992 report_diagnostic (&diagnostic);
993 va_end (ap);
994 }
995
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, ...)
1001 {
1002 diagnostic_info diagnostic;
1003 va_list ap;
1004
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);
1011 }
1012
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, ...)
1018 {
1019 diagnostic_info diagnostic;
1020 va_list ap;
1021 bool ret;
1022
1023 va_start (ap, gmsgid);
1024 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
1025 diagnostic.option_index = opt;
1026
1027 ret = report_diagnostic (&diagnostic);
1028 va_end (ap);
1029 return ret;
1030 }
1031
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. */
1035
1036 bool
1037 warning_at (location_t location, int opt, const char *gmsgid, ...)
1038 {
1039 diagnostic_info diagnostic;
1040 va_list ap;
1041 bool ret;
1042
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;
1049 }
1050
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. */
1054
1055 bool
1056 warning_n (location_t location, int opt, int n, const char *singular_gmsgid,
1057 const char *plural_gmsgid, ...)
1058 {
1059 diagnostic_info diagnostic;
1060 va_list ap;
1061 bool ret;
1062
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;
1071 }
1072
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.
1077
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,...)".
1083
1084 Returns true if the warning was printed, false if it was inhibited. */
1085
1086 bool
1087 pedwarn (location_t location, int opt, const char *gmsgid, ...)
1088 {
1089 diagnostic_info diagnostic;
1090 va_list ap;
1091 bool ret;
1092
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;
1099 }
1100
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.
1105
1106 Returns true if the warning was printed, false if it was inhibited. */
1107
1108 bool
1109 permerror (location_t location, const char *gmsgid, ...)
1110 {
1111 diagnostic_info diagnostic;
1112 va_list ap;
1113 bool ret;
1114
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;
1122 }
1123
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, ...)
1128 {
1129 diagnostic_info diagnostic;
1130 va_list ap;
1131
1132 va_start (ap, gmsgid);
1133 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
1134 report_diagnostic (&diagnostic);
1135 va_end (ap);
1136 }
1137
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, ...)
1143 {
1144 diagnostic_info diagnostic;
1145 va_list ap;
1146
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);
1153 }
1154
1155 /* Same as ebove, but use location LOC instead of input_location. */
1156 void
1157 error_at (location_t loc, const char *gmsgid, ...)
1158 {
1159 diagnostic_info diagnostic;
1160 va_list ap;
1161
1162 va_start (ap, gmsgid);
1163 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_ERROR);
1164 report_diagnostic (&diagnostic);
1165 va_end (ap);
1166 }
1167
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, ...)
1173 {
1174 diagnostic_info diagnostic;
1175 va_list ap;
1176
1177 va_start (ap, gmsgid);
1178 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
1179 report_diagnostic (&diagnostic);
1180 va_end (ap);
1181 }
1182
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)
1187 {
1188 return errorcount || sorrycount;
1189 }
1190
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, ...)
1196 {
1197 diagnostic_info diagnostic;
1198 va_list ap;
1199
1200 va_start (ap, gmsgid);
1201 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
1202 report_diagnostic (&diagnostic);
1203 va_end (ap);
1204
1205 gcc_unreachable ();
1206 }
1207
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, ...)
1213 {
1214 diagnostic_info diagnostic;
1215 va_list ap;
1216
1217 va_start (ap, gmsgid);
1218 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_FATAL);
1219 report_diagnostic (&diagnostic);
1220 va_end (ap);
1221
1222 gcc_unreachable ();
1223 }
1224
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, ...)
1231 {
1232 diagnostic_info diagnostic;
1233 va_list ap;
1234
1235 va_start (ap, gmsgid);
1236 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
1237 report_diagnostic (&diagnostic);
1238 va_end (ap);
1239
1240 gcc_unreachable ();
1241 }
1242 \f
1243 /* Special case error functions. Most are implemented in terms of the
1244 above, or should be. */
1245
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, ...)
1250 {
1251 va_list ap;
1252
1253 va_start (ap, cmsgid);
1254 vfprintf (file, _(cmsgid), ap);
1255 va_end (ap);
1256 }
1257
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. */
1262
1263 static void
1264 error_recursion (diagnostic_context *context)
1265 {
1266 if (context->lock < 3)
1267 pp_newline_and_flush (context->printer);
1268
1269 fnotice (stderr,
1270 "Internal compiler error: Error reporting routines re-entered.\n");
1271
1272 /* Call diagnostic_action_after_output to get the "please submit a bug
1273 report" message. */
1274 diagnostic_action_after_output (context, DK_ICE);
1275
1276 /* Do not use gcc_unreachable here; that goes through internal_error
1277 and therefore would cause infinite recursion. */
1278 real_abort ();
1279 }
1280
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. */
1284
1285 void
1286 fancy_abort (const char *file, int line, const char *function)
1287 {
1288 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1289 }
1290
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)
1297 {
1298 abort ();
1299 }