]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/diagnostic.c
PR c++/70105: Defer location expansion until diagnostic_show_locus
[thirdparty/gcc.git] / gcc / diagnostic.c
1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999-2016 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21
22 /* This file implements the language independent aspect of diagnostic
23 message module. */
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "version.h"
29 #include "demangle.h"
30 #include "intl.h"
31 #include "backtrace.h"
32 #include "diagnostic.h"
33 #include "diagnostic-color.h"
34
35 #ifdef HAVE_TERMIOS_H
36 # include <termios.h>
37 #endif
38
39 #ifdef GWINSZ_IN_SYS_IOCTL
40 # include <sys/ioctl.h>
41 #endif
42
43 #include <new> // For placement new.
44
45 #define pedantic_warning_kind(DC) \
46 ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
47 #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR)
48 #define permissive_error_option(DC) ((DC)->opt_permissive)
49
50 /* Prototypes. */
51 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
52
53 static void real_abort (void) ATTRIBUTE_NORETURN;
54
55 /* Name of program invoked, sans directories. */
56
57 const char *progname;
58
59 /* A diagnostic_context surrogate for stderr. */
60 static diagnostic_context global_diagnostic_context;
61 diagnostic_context *global_dc = &global_diagnostic_context;
62 \f
63 /* Return a malloc'd string containing MSG formatted a la printf. The
64 caller is responsible for freeing the memory. */
65 char *
66 build_message_string (const char *msg, ...)
67 {
68 char *str;
69 va_list ap;
70
71 va_start (ap, msg);
72 str = xvasprintf (msg, ap);
73 va_end (ap);
74
75 return str;
76 }
77
78 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
79 char *
80 file_name_as_prefix (diagnostic_context *context, const char *f)
81 {
82 const char *locus_cs
83 = colorize_start (pp_show_color (context->printer), "locus");
84 const char *locus_ce = colorize_stop (pp_show_color (context->printer));
85 return build_message_string ("%s%s:%s ", locus_cs, f, locus_ce);
86 }
87
88
89 \f
90 /* Return the value of the getenv("COLUMNS") as an integer. If the
91 value is not set to a positive integer, use ioctl to get the
92 terminal width. If it fails, return INT_MAX. */
93 int
94 get_terminal_width (void)
95 {
96 const char * s = getenv ("COLUMNS");
97 if (s != NULL) {
98 int n = atoi (s);
99 if (n > 0)
100 return n;
101 }
102
103 #ifdef TIOCGWINSZ
104 struct winsize w;
105 w.ws_col = 0;
106 if (ioctl (0, TIOCGWINSZ, &w) == 0 && w.ws_col > 0)
107 return w.ws_col;
108 #endif
109
110 return INT_MAX;
111 }
112
113 /* Set caret_max_width to value. */
114 void
115 diagnostic_set_caret_max_width (diagnostic_context *context, int value)
116 {
117 /* One minus to account for the leading empty space. */
118 value = value ? value - 1
119 : (isatty (fileno (pp_buffer (context->printer)->stream))
120 ? get_terminal_width () - 1: INT_MAX);
121
122 if (value <= 0)
123 value = INT_MAX;
124
125 context->caret_max_width = value;
126 }
127
128 /* Initialize the diagnostic message outputting machinery. */
129 void
130 diagnostic_initialize (diagnostic_context *context, int n_opts)
131 {
132 int i;
133
134 /* Allocate a basic pretty-printer. Clients will replace this a
135 much more elaborated pretty-printer if they wish. */
136 context->printer = XNEW (pretty_printer);
137 new (context->printer) pretty_printer ();
138
139 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
140 context->warning_as_error_requested = false;
141 context->n_opts = n_opts;
142 context->classify_diagnostic = XNEWVEC (diagnostic_t, n_opts);
143 for (i = 0; i < n_opts; i++)
144 context->classify_diagnostic[i] = DK_UNSPECIFIED;
145 context->show_caret = false;
146 diagnostic_set_caret_max_width (context, pp_line_cutoff (context->printer));
147 for (i = 0; i < rich_location::MAX_RANGES; i++)
148 context->caret_chars[i] = '^';
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 context->start_span = default_diagnostic_start_span_fn;
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 (diagnostic_kind_count (context, DK_WERROR))
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, rich_location *richloc,
239 diagnostic_t kind)
240 {
241 gcc_assert (richloc);
242 diagnostic->message.err_no = errno;
243 diagnostic->message.args_ptr = args;
244 diagnostic->message.format_spec = msg;
245 diagnostic->message.m_richloc = richloc;
246 diagnostic->richloc = richloc;
247 diagnostic->kind = kind;
248 diagnostic->option_index = 0;
249 }
250
251 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
252 translated. */
253 void
254 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
255 va_list *args, rich_location *richloc,
256 diagnostic_t kind)
257 {
258 gcc_assert (richloc);
259 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, richloc, kind);
260 }
261
262 static const char *const diagnostic_kind_color[] = {
263 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
264 #include "diagnostic.def"
265 #undef DEFINE_DIAGNOSTIC_KIND
266 NULL
267 };
268
269 /* Get a color name for diagnostics of type KIND
270 Result could be NULL. */
271
272 const char *
273 diagnostic_get_color_for_kind (diagnostic_t kind)
274 {
275 return diagnostic_kind_color[kind];
276 }
277
278 /* Return a malloc'd string describing a location e.g. "foo.c:42:10".
279 The caller is responsible for freeing the memory. */
280
281 static char *
282 diagnostic_get_location_text (diagnostic_context *context,
283 expanded_location s)
284 {
285 pretty_printer *pp = context->printer;
286 const char *locus_cs = colorize_start (pp_show_color (pp), "locus");
287 const char *locus_ce = colorize_stop (pp_show_color (pp));
288
289 if (s.file == NULL)
290 return build_message_string ("%s%s:%s", locus_cs, progname, locus_ce);
291
292 if (!strcmp (s.file, N_("<built-in>")))
293 return build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce);
294
295 if (context->show_column)
296 return build_message_string ("%s%s:%d:%d:%s", locus_cs, s.file, s.line,
297 s.column, locus_ce);
298 else
299 return build_message_string ("%s%s:%d:%s", locus_cs, s.file, s.line,
300 locus_ce);
301 }
302
303 /* Return a malloc'd string describing a location and the severity of the
304 diagnostic, e.g. "foo.c:42:10: error: ". The caller is responsible for
305 freeing the memory. */
306 char *
307 diagnostic_build_prefix (diagnostic_context *context,
308 const diagnostic_info *diagnostic)
309 {
310 static const char *const diagnostic_kind_text[] = {
311 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
312 #include "diagnostic.def"
313 #undef DEFINE_DIAGNOSTIC_KIND
314 "must-not-happen"
315 };
316 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
317
318 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
319 const char *text_cs = "", *text_ce = "";
320 pretty_printer *pp = context->printer;
321
322 if (diagnostic_kind_color[diagnostic->kind])
323 {
324 text_cs = colorize_start (pp_show_color (pp),
325 diagnostic_kind_color[diagnostic->kind]);
326 text_ce = colorize_stop (pp_show_color (pp));
327 }
328
329 expanded_location s = diagnostic_expand_location (diagnostic);
330 char *location_text = diagnostic_get_location_text (context, s);
331
332 char *result = build_message_string ("%s %s%s%s", location_text,
333 text_cs, text, text_ce);
334 free (location_text);
335 return result;
336 }
337
338 /* Functions at which to stop the backtrace print. It's not
339 particularly helpful to print the callers of these functions. */
340
341 static const char * const bt_stop[] =
342 {
343 "main",
344 "toplev::main",
345 "execute_one_pass",
346 "compile_file",
347 };
348
349 /* A callback function passed to the backtrace_full function. */
350
351 static int
352 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
353 const char *function)
354 {
355 int *pcount = (int *) data;
356
357 /* If we don't have any useful information, don't print
358 anything. */
359 if (filename == NULL && function == NULL)
360 return 0;
361
362 /* Skip functions in diagnostic.c. */
363 if (*pcount == 0
364 && filename != NULL
365 && strcmp (lbasename (filename), "diagnostic.c") == 0)
366 return 0;
367
368 /* Print up to 20 functions. We could make this a --param, but
369 since this is only for debugging just use a constant for now. */
370 if (*pcount >= 20)
371 {
372 /* Returning a non-zero value stops the backtrace. */
373 return 1;
374 }
375 ++*pcount;
376
377 char *alc = NULL;
378 if (function != NULL)
379 {
380 char *str = cplus_demangle_v3 (function,
381 (DMGL_VERBOSE | DMGL_ANSI
382 | DMGL_GNU_V3 | DMGL_PARAMS));
383 if (str != NULL)
384 {
385 alc = str;
386 function = str;
387 }
388
389 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
390 {
391 size_t len = strlen (bt_stop[i]);
392 if (strncmp (function, bt_stop[i], len) == 0
393 && (function[len] == '\0' || function[len] == '('))
394 {
395 if (alc != NULL)
396 free (alc);
397 /* Returning a non-zero value stops the backtrace. */
398 return 1;
399 }
400 }
401 }
402
403 fprintf (stderr, "0x%lx %s\n\t%s:%d\n",
404 (unsigned long) pc,
405 function == NULL ? "???" : function,
406 filename == NULL ? "???" : filename,
407 lineno);
408
409 if (alc != NULL)
410 free (alc);
411
412 return 0;
413 }
414
415 /* A callback function passed to the backtrace_full function. This is
416 called if backtrace_full has an error. */
417
418 static void
419 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
420 {
421 if (errnum < 0)
422 {
423 /* This means that no debug info was available. Just quietly
424 skip printing backtrace info. */
425 return;
426 }
427 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
428 errnum == 0 ? "" : xstrerror (errnum));
429 }
430
431 /* Take any action which is expected to happen after the diagnostic
432 is written out. This function does not always return. */
433 void
434 diagnostic_action_after_output (diagnostic_context *context,
435 diagnostic_t diag_kind)
436 {
437 switch (diag_kind)
438 {
439 case DK_DEBUG:
440 case DK_NOTE:
441 case DK_ANACHRONISM:
442 case DK_WARNING:
443 break;
444
445 case DK_ERROR:
446 case DK_SORRY:
447 if (context->abort_on_error)
448 real_abort ();
449 if (context->fatal_errors)
450 {
451 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
452 diagnostic_finish (context);
453 exit (FATAL_EXIT_CODE);
454 }
455 if (context->max_errors != 0
456 && ((unsigned) (diagnostic_kind_count (context, DK_ERROR)
457 + diagnostic_kind_count (context, DK_SORRY)
458 + diagnostic_kind_count (context, DK_WERROR))
459 >= context->max_errors))
460 {
461 fnotice (stderr,
462 "compilation terminated due to -fmax-errors=%u.\n",
463 context->max_errors);
464 diagnostic_finish (context);
465 exit (FATAL_EXIT_CODE);
466 }
467 break;
468
469 case DK_ICE:
470 case DK_ICE_NOBT:
471 {
472 struct backtrace_state *state = NULL;
473 if (diag_kind == DK_ICE)
474 state = backtrace_create_state (NULL, 0, bt_err_callback, NULL);
475 int count = 0;
476 if (state != NULL)
477 backtrace_full (state, 2, bt_callback, bt_err_callback,
478 (void *) &count);
479
480 if (context->abort_on_error)
481 real_abort ();
482
483 fnotice (stderr, "Please submit a full bug report,\n"
484 "with preprocessed source if appropriate.\n");
485 if (count > 0)
486 fnotice (stderr,
487 ("Please include the complete backtrace "
488 "with any bug report.\n"));
489 fnotice (stderr, "See %s for instructions.\n", bug_report_url);
490
491 exit (ICE_EXIT_CODE);
492 }
493
494 case DK_FATAL:
495 if (context->abort_on_error)
496 real_abort ();
497 diagnostic_finish (context);
498 fnotice (stderr, "compilation terminated.\n");
499 exit (FATAL_EXIT_CODE);
500
501 default:
502 gcc_unreachable ();
503 }
504 }
505
506 void
507 diagnostic_report_current_module (diagnostic_context *context, location_t where)
508 {
509 const line_map_ordinary *map = NULL;
510
511 if (pp_needs_newline (context->printer))
512 {
513 pp_newline (context->printer);
514 pp_needs_newline (context->printer) = false;
515 }
516
517 if (where <= BUILTINS_LOCATION)
518 return;
519
520 linemap_resolve_location (line_table, where,
521 LRK_MACRO_DEFINITION_LOCATION,
522 &map);
523
524 if (map && diagnostic_last_module_changed (context, map))
525 {
526 diagnostic_set_last_module (context, map);
527 if (! MAIN_FILE_P (map))
528 {
529 map = INCLUDED_FROM (line_table, map);
530 if (context->show_column)
531 pp_verbatim (context->printer,
532 "In file included from %r%s:%d:%d%R", "locus",
533 LINEMAP_FILE (map),
534 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
535 else
536 pp_verbatim (context->printer,
537 "In file included from %r%s:%d%R", "locus",
538 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
539 while (! MAIN_FILE_P (map))
540 {
541 map = INCLUDED_FROM (line_table, map);
542 pp_verbatim (context->printer,
543 ",\n from %r%s:%d%R", "locus",
544 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
545 }
546 pp_verbatim (context->printer, ":");
547 pp_newline (context->printer);
548 }
549 }
550 }
551
552 void
553 default_diagnostic_starter (diagnostic_context *context,
554 diagnostic_info *diagnostic)
555 {
556 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
557 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
558 diagnostic));
559 }
560
561 void
562 default_diagnostic_start_span_fn (diagnostic_context *context,
563 expanded_location exploc)
564 {
565 pp_set_prefix (context->printer,
566 diagnostic_get_location_text (context, exploc));
567 pp_string (context->printer, "");
568 pp_newline (context->printer);
569 }
570
571 void
572 default_diagnostic_finalizer (diagnostic_context *context,
573 diagnostic_info *diagnostic)
574 {
575 diagnostic_show_locus (context, diagnostic);
576 pp_destroy_prefix (context->printer);
577 pp_flush (context->printer);
578 }
579
580 /* Interface to specify diagnostic kind overrides. Returns the
581 previous setting, or DK_UNSPECIFIED if the parameters are out of
582 range. If OPTION_INDEX is zero, the new setting is for all the
583 diagnostics. */
584 diagnostic_t
585 diagnostic_classify_diagnostic (diagnostic_context *context,
586 int option_index,
587 diagnostic_t new_kind,
588 location_t where)
589 {
590 diagnostic_t old_kind;
591
592 if (option_index < 0
593 || option_index >= context->n_opts
594 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
595 return DK_UNSPECIFIED;
596
597 old_kind = context->classify_diagnostic[option_index];
598
599 /* Handle pragmas separately, since we need to keep track of *where*
600 the pragmas were. */
601 if (where != UNKNOWN_LOCATION)
602 {
603 int i;
604
605 /* Record the command-line status, so we can reset it back on DK_POP. */
606 if (old_kind == DK_UNSPECIFIED)
607 {
608 old_kind = !context->option_enabled (option_index,
609 context->option_state)
610 ? DK_IGNORED : (context->warning_as_error_requested
611 ? DK_ERROR : DK_WARNING);
612 context->classify_diagnostic[option_index] = old_kind;
613 }
614
615 for (i = context->n_classification_history - 1; i >= 0; i --)
616 if (context->classification_history[i].option == option_index)
617 {
618 old_kind = context->classification_history[i].kind;
619 break;
620 }
621
622 i = context->n_classification_history;
623 context->classification_history =
624 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
625 * sizeof (diagnostic_classification_change_t));
626 context->classification_history[i].location = where;
627 context->classification_history[i].option = option_index;
628 context->classification_history[i].kind = new_kind;
629 context->n_classification_history ++;
630 }
631 else
632 context->classify_diagnostic[option_index] = new_kind;
633
634 return old_kind;
635 }
636
637 /* Save all diagnostic classifications in a stack. */
638 void
639 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
640 {
641 context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
642 context->push_list[context->n_push ++] = context->n_classification_history;
643 }
644
645 /* Restore the topmost classification set off the stack. If the stack
646 is empty, revert to the state based on command line parameters. */
647 void
648 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
649 {
650 int jump_to;
651 int i;
652
653 if (context->n_push)
654 jump_to = context->push_list [-- context->n_push];
655 else
656 jump_to = 0;
657
658 i = context->n_classification_history;
659 context->classification_history =
660 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
661 * sizeof (diagnostic_classification_change_t));
662 context->classification_history[i].location = where;
663 context->classification_history[i].option = jump_to;
664 context->classification_history[i].kind = DK_POP;
665 context->n_classification_history ++;
666 }
667
668 /* Report a diagnostic message (an error or a warning) as specified by
669 DC. This function is *the* subroutine in terms of which front-ends
670 should implement their specific diagnostic handling modules. The
671 front-end independent format specifiers are exactly those described
672 in the documentation of output_format.
673 Return true if a diagnostic was printed, false otherwise. */
674
675 bool
676 diagnostic_report_diagnostic (diagnostic_context *context,
677 diagnostic_info *diagnostic)
678 {
679 location_t location = diagnostic_location (diagnostic);
680 diagnostic_t orig_diag_kind = diagnostic->kind;
681 const char *saved_format_spec;
682
683 /* Give preference to being able to inhibit warnings, before they
684 get reclassified to something else. */
685 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
686 && !diagnostic_report_warnings_p (context, location))
687 return false;
688
689 if (diagnostic->kind == DK_PEDWARN)
690 {
691 diagnostic->kind = pedantic_warning_kind (context);
692 /* We do this to avoid giving the message for -pedantic-errors. */
693 orig_diag_kind = diagnostic->kind;
694 }
695
696 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
697 return false;
698
699 if (context->lock > 0)
700 {
701 /* If we're reporting an ICE in the middle of some other error,
702 try to flush out the previous error, then let this one
703 through. Don't do this more than once. */
704 if ((diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
705 && context->lock == 1)
706 pp_newline_and_flush (context->printer);
707 else
708 error_recursion (context);
709 }
710
711 /* If the user requested that warnings be treated as errors, so be
712 it. Note that we do this before the next block so that
713 individual warnings can be overridden back to warnings with
714 -Wno-error=*. */
715 if (context->warning_as_error_requested
716 && diagnostic->kind == DK_WARNING)
717 {
718 diagnostic->kind = DK_ERROR;
719 }
720
721 if (diagnostic->option_index
722 && diagnostic->option_index != permissive_error_option (context))
723 {
724 diagnostic_t diag_class = DK_UNSPECIFIED;
725
726 /* This tests if the user provided the appropriate -Wfoo or
727 -Wno-foo option. */
728 if (! context->option_enabled (diagnostic->option_index,
729 context->option_state))
730 return false;
731
732 /* This tests for #pragma diagnostic changes. */
733 if (context->n_classification_history > 0)
734 {
735 /* FIXME: Stupid search. Optimize later. */
736 for (int i = context->n_classification_history - 1; i >= 0; i --)
737 {
738 if (linemap_location_before_p
739 (line_table,
740 context->classification_history[i].location,
741 location))
742 {
743 if (context->classification_history[i].kind == (int) DK_POP)
744 {
745 i = context->classification_history[i].option;
746 continue;
747 }
748 int option = context->classification_history[i].option;
749 /* The option 0 is for all the diagnostics. */
750 if (option == 0 || option == diagnostic->option_index)
751 {
752 diag_class = context->classification_history[i].kind;
753 if (diag_class != DK_UNSPECIFIED)
754 diagnostic->kind = diag_class;
755 break;
756 }
757 }
758 }
759 }
760 /* This tests if the user provided the appropriate -Werror=foo
761 option. */
762 if (diag_class == DK_UNSPECIFIED
763 && context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
764 {
765 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
766 }
767 /* This allows for future extensions, like temporarily disabling
768 warnings for ranges of source code. */
769 if (diagnostic->kind == DK_IGNORED)
770 return false;
771 }
772
773 context->lock++;
774
775 if (diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
776 {
777 /* When not checking, ICEs are converted to fatal errors when an
778 error has already occurred. This is counteracted by
779 abort_on_error. */
780 if (!CHECKING_P
781 && (diagnostic_kind_count (context, DK_ERROR) > 0
782 || diagnostic_kind_count (context, DK_SORRY) > 0)
783 && !context->abort_on_error)
784 {
785 expanded_location s
786 = expand_location (diagnostic_location (diagnostic));
787 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
788 s.file, s.line);
789 exit (ICE_EXIT_CODE);
790 }
791 if (context->internal_error)
792 (*context->internal_error) (context,
793 diagnostic->message.format_spec,
794 diagnostic->message.args_ptr);
795 }
796 if (diagnostic->kind == DK_ERROR && orig_diag_kind == DK_WARNING)
797 ++diagnostic_kind_count (context, DK_WERROR);
798 else
799 ++diagnostic_kind_count (context, diagnostic->kind);
800
801 saved_format_spec = diagnostic->message.format_spec;
802 if (context->show_option_requested)
803 {
804 char *option_text;
805
806 option_text = context->option_name (context, diagnostic->option_index,
807 orig_diag_kind, diagnostic->kind);
808
809 if (option_text)
810 {
811 const char *cs
812 = colorize_start (pp_show_color (context->printer),
813 diagnostic_kind_color[diagnostic->kind]);
814 const char *ce = colorize_stop (pp_show_color (context->printer));
815 diagnostic->message.format_spec
816 = ACONCAT ((diagnostic->message.format_spec,
817 " ",
818 "[", cs, option_text, ce, "]",
819 NULL));
820 free (option_text);
821 }
822 }
823 diagnostic->message.x_data = &diagnostic->x_data;
824 diagnostic->x_data = NULL;
825 pp_format (context->printer, &diagnostic->message);
826 (*diagnostic_starter (context)) (context, diagnostic);
827 pp_output_formatted_text (context->printer);
828 (*diagnostic_finalizer (context)) (context, diagnostic);
829 diagnostic_action_after_output (context, diagnostic->kind);
830 diagnostic->message.format_spec = saved_format_spec;
831 diagnostic->x_data = NULL;
832
833 context->lock--;
834
835 return true;
836 }
837
838 /* Given a partial pathname as input, return another pathname that
839 shares no directory elements with the pathname of __FILE__. This
840 is used by fancy_abort() to print `Internal compiler error in expr.c'
841 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
842
843 const char *
844 trim_filename (const char *name)
845 {
846 static const char this_file[] = __FILE__;
847 const char *p = name, *q = this_file;
848
849 /* First skip any "../" in each filename. This allows us to give a proper
850 reference to a file in a subdirectory. */
851 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
852 p += 3;
853
854 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
855 q += 3;
856
857 /* Now skip any parts the two filenames have in common. */
858 while (*p == *q && *p != 0 && *q != 0)
859 p++, q++;
860
861 /* Now go backwards until the previous directory separator. */
862 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
863 p--;
864
865 return p;
866 }
867 \f
868 /* Standard error reporting routines in increasing order of severity.
869 All of these take arguments like printf. */
870
871 /* Text to be emitted verbatim to the error message stream; this
872 produces no prefix and disables line-wrapping. Use rarely. */
873 void
874 verbatim (const char *gmsgid, ...)
875 {
876 text_info text;
877 va_list ap;
878
879 va_start (ap, gmsgid);
880 text.err_no = errno;
881 text.args_ptr = &ap;
882 text.format_spec = _(gmsgid);
883 text.x_data = NULL;
884 pp_format_verbatim (global_dc->printer, &text);
885 pp_newline_and_flush (global_dc->printer);
886 va_end (ap);
887 }
888
889 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
890 void
891 diagnostic_append_note (diagnostic_context *context,
892 location_t location,
893 const char * gmsgid, ...)
894 {
895 diagnostic_info diagnostic;
896 va_list ap;
897 const char *saved_prefix;
898 rich_location richloc (line_table, location);
899
900 va_start (ap, gmsgid);
901 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_NOTE);
902 if (context->inhibit_notes_p)
903 {
904 va_end (ap);
905 return;
906 }
907 saved_prefix = pp_get_prefix (context->printer);
908 pp_set_prefix (context->printer,
909 diagnostic_build_prefix (context, &diagnostic));
910 pp_format (context->printer, &diagnostic.message);
911 pp_output_formatted_text (context->printer);
912 pp_destroy_prefix (context->printer);
913 pp_set_prefix (context->printer, saved_prefix);
914 diagnostic_show_locus (context, &diagnostic);
915 va_end (ap);
916 }
917
918 bool
919 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
920 const char *gmsgid, ...)
921 {
922 diagnostic_info diagnostic;
923 va_list ap;
924 bool ret;
925 rich_location richloc (line_table, location);
926
927 va_start (ap, gmsgid);
928 if (kind == DK_PERMERROR)
929 {
930 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
931 permissive_error_kind (global_dc));
932 diagnostic.option_index = permissive_error_option (global_dc);
933 }
934 else {
935 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, kind);
936 if (kind == DK_WARNING || kind == DK_PEDWARN)
937 diagnostic.option_index = opt;
938 }
939
940 ret = report_diagnostic (&diagnostic);
941 va_end (ap);
942 return ret;
943 }
944
945 /* An informative note at LOCATION. Use this for additional details on an error
946 message. */
947 void
948 inform (location_t location, const char *gmsgid, ...)
949 {
950 diagnostic_info diagnostic;
951 va_list ap;
952 rich_location richloc (line_table, location);
953
954 va_start (ap, gmsgid);
955 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_NOTE);
956 report_diagnostic (&diagnostic);
957 va_end (ap);
958 }
959
960 /* Same as "inform", but at RICHLOC. */
961 void
962 inform_at_rich_loc (rich_location *richloc, const char *gmsgid, ...)
963 {
964 diagnostic_info diagnostic;
965 va_list ap;
966
967 va_start (ap, gmsgid);
968 diagnostic_set_info (&diagnostic, gmsgid, &ap, richloc, DK_NOTE);
969 report_diagnostic (&diagnostic);
970 va_end (ap);
971 }
972
973 /* An informative note at LOCATION. Use this for additional details on an
974 error message. */
975 void
976 inform_n (location_t location, int n, const char *singular_gmsgid,
977 const char *plural_gmsgid, ...)
978 {
979 diagnostic_info diagnostic;
980 va_list ap;
981 rich_location richloc (line_table, location);
982
983 va_start (ap, plural_gmsgid);
984 diagnostic_set_info_translated (&diagnostic,
985 ngettext (singular_gmsgid, plural_gmsgid, n),
986 &ap, &richloc, DK_NOTE);
987 report_diagnostic (&diagnostic);
988 va_end (ap);
989 }
990
991 /* A warning at INPUT_LOCATION. Use this for code which is correct according
992 to the relevant language specification but is likely to be buggy anyway.
993 Returns true if the warning was printed, false if it was inhibited. */
994 bool
995 warning (int opt, const char *gmsgid, ...)
996 {
997 diagnostic_info diagnostic;
998 va_list ap;
999 bool ret;
1000 rich_location richloc (line_table, input_location);
1001
1002 va_start (ap, gmsgid);
1003 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_WARNING);
1004 diagnostic.option_index = opt;
1005
1006 ret = report_diagnostic (&diagnostic);
1007 va_end (ap);
1008 return ret;
1009 }
1010
1011 /* A warning at LOCATION. Use this for code which is correct according to the
1012 relevant language specification but is likely to be buggy anyway.
1013 Returns true if the warning was printed, false if it was inhibited. */
1014
1015 bool
1016 warning_at (location_t location, int opt, const char *gmsgid, ...)
1017 {
1018 diagnostic_info diagnostic;
1019 va_list ap;
1020 bool ret;
1021 rich_location richloc (line_table, location);
1022
1023 va_start (ap, gmsgid);
1024 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_WARNING);
1025 diagnostic.option_index = opt;
1026 ret = report_diagnostic (&diagnostic);
1027 va_end (ap);
1028 return ret;
1029 }
1030
1031 /* Same as warning at, but using RICHLOC. */
1032
1033 bool
1034 warning_at_rich_loc (rich_location *richloc, int opt, const char *gmsgid, ...)
1035 {
1036 diagnostic_info diagnostic;
1037 va_list ap;
1038 bool ret;
1039
1040 va_start (ap, gmsgid);
1041 diagnostic_set_info (&diagnostic, gmsgid, &ap, richloc, DK_WARNING);
1042 diagnostic.option_index = opt;
1043 ret = report_diagnostic (&diagnostic);
1044 va_end (ap);
1045 return ret;
1046 }
1047
1048 /* A warning at LOCATION. Use this for code which is correct according to the
1049 relevant language specification but is likely to be buggy anyway.
1050 Returns true if the warning was printed, false if it was inhibited. */
1051
1052 bool
1053 warning_n (location_t location, int opt, int n, const char *singular_gmsgid,
1054 const char *plural_gmsgid, ...)
1055 {
1056 diagnostic_info diagnostic;
1057 va_list ap;
1058 bool ret;
1059 rich_location richloc (line_table, location);
1060
1061 va_start (ap, plural_gmsgid);
1062 diagnostic_set_info_translated (&diagnostic,
1063 ngettext (singular_gmsgid, plural_gmsgid, n),
1064 &ap, &richloc, DK_WARNING
1065 );
1066 diagnostic.option_index = opt;
1067 ret = report_diagnostic (&diagnostic);
1068 va_end (ap);
1069 return ret;
1070 }
1071
1072 /* A "pedantic" warning at LOCATION: issues a warning unless
1073 -pedantic-errors was given on the command line, in which case it
1074 issues an error. Use this for diagnostics required by the relevant
1075 language standard, if you have chosen not to make them errors.
1076
1077 Note that these diagnostics are issued independent of the setting
1078 of the -Wpedantic command-line switch. To get a warning enabled
1079 only with that switch, use either "if (pedantic) pedwarn
1080 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
1081 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
1082
1083 Returns true if the warning was printed, false if it was inhibited. */
1084
1085 bool
1086 pedwarn (location_t location, int opt, const char *gmsgid, ...)
1087 {
1088 diagnostic_info diagnostic;
1089 va_list ap;
1090 bool ret;
1091 rich_location richloc (line_table, location);
1092
1093 va_start (ap, gmsgid);
1094 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, 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 rich_location richloc (line_table, location);
1115
1116 va_start (ap, gmsgid);
1117 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
1118 permissive_error_kind (global_dc));
1119 diagnostic.option_index = permissive_error_option (global_dc);
1120 ret = report_diagnostic (&diagnostic);
1121 va_end (ap);
1122 return ret;
1123 }
1124
1125 /* Same as "permerror", but at RICHLOC. */
1126
1127 bool
1128 permerror_at_rich_loc (rich_location *richloc, const char *gmsgid, ...)
1129 {
1130 diagnostic_info diagnostic;
1131 va_list ap;
1132 bool ret;
1133
1134 va_start (ap, gmsgid);
1135 diagnostic_set_info (&diagnostic, gmsgid, &ap, richloc,
1136 permissive_error_kind (global_dc));
1137 diagnostic.option_index = permissive_error_option (global_dc);
1138 ret = report_diagnostic (&diagnostic);
1139 va_end (ap);
1140 return ret;
1141 }
1142
1143 /* A hard error: the code is definitely ill-formed, and an object file
1144 will not be produced. */
1145 void
1146 error (const char *gmsgid, ...)
1147 {
1148 diagnostic_info diagnostic;
1149 va_list ap;
1150 rich_location richloc (line_table, input_location);
1151
1152 va_start (ap, gmsgid);
1153 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_ERROR);
1154 report_diagnostic (&diagnostic);
1155 va_end (ap);
1156 }
1157
1158 /* A hard error: the code is definitely ill-formed, and an object file
1159 will not be produced. */
1160 void
1161 error_n (location_t location, int n, const char *singular_gmsgid,
1162 const char *plural_gmsgid, ...)
1163 {
1164 diagnostic_info diagnostic;
1165 va_list ap;
1166 rich_location richloc (line_table, location);
1167
1168 va_start (ap, plural_gmsgid);
1169 diagnostic_set_info_translated (&diagnostic,
1170 ngettext (singular_gmsgid, plural_gmsgid, n),
1171 &ap, &richloc, DK_ERROR);
1172 report_diagnostic (&diagnostic);
1173 va_end (ap);
1174 }
1175
1176 /* Same as ebove, but use location LOC instead of input_location. */
1177 void
1178 error_at (location_t loc, const char *gmsgid, ...)
1179 {
1180 diagnostic_info diagnostic;
1181 va_list ap;
1182 rich_location richloc (line_table, loc);
1183
1184 va_start (ap, gmsgid);
1185 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_ERROR);
1186 report_diagnostic (&diagnostic);
1187 va_end (ap);
1188 }
1189
1190 /* Same as above, but use RICH_LOC. */
1191
1192 void
1193 error_at_rich_loc (rich_location *rich_loc, const char *gmsgid, ...)
1194 {
1195 diagnostic_info diagnostic;
1196 va_list ap;
1197
1198 va_start (ap, gmsgid);
1199 diagnostic_set_info (&diagnostic, gmsgid, &ap, rich_loc,
1200 DK_ERROR);
1201 report_diagnostic (&diagnostic);
1202 va_end (ap);
1203 }
1204
1205 /* "Sorry, not implemented." Use for a language feature which is
1206 required by the relevant specification but not implemented by GCC.
1207 An object file will not be produced. */
1208 void
1209 sorry (const char *gmsgid, ...)
1210 {
1211 diagnostic_info diagnostic;
1212 va_list ap;
1213 rich_location richloc (line_table, input_location);
1214
1215 va_start (ap, gmsgid);
1216 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_SORRY);
1217 report_diagnostic (&diagnostic);
1218 va_end (ap);
1219 }
1220
1221 /* Return true if an error or a "sorry" has been seen. Various
1222 processing is disabled after errors. */
1223 bool
1224 seen_error (void)
1225 {
1226 return errorcount || sorrycount;
1227 }
1228
1229 /* An error which is severe enough that we make no attempt to
1230 continue. Do not use this for internal consistency checks; that's
1231 internal_error. Use of this function should be rare. */
1232 void
1233 fatal_error (location_t loc, const char *gmsgid, ...)
1234 {
1235 diagnostic_info diagnostic;
1236 va_list ap;
1237 rich_location richloc (line_table, loc);
1238
1239 va_start (ap, gmsgid);
1240 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_FATAL);
1241 report_diagnostic (&diagnostic);
1242 va_end (ap);
1243
1244 gcc_unreachable ();
1245 }
1246
1247 /* An internal consistency check has failed. We make no attempt to
1248 continue. Note that unless there is debugging value to be had from
1249 a more specific message, or some other good reason, you should use
1250 abort () instead of calling this function directly. */
1251 void
1252 internal_error (const char *gmsgid, ...)
1253 {
1254 diagnostic_info diagnostic;
1255 va_list ap;
1256 rich_location richloc (line_table, input_location);
1257
1258 va_start (ap, gmsgid);
1259 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_ICE);
1260 report_diagnostic (&diagnostic);
1261 va_end (ap);
1262
1263 gcc_unreachable ();
1264 }
1265
1266 /* Like internal_error, but no backtrace will be printed. Used when
1267 the internal error does not happen at the current location, but happened
1268 somewhere else. */
1269 void
1270 internal_error_no_backtrace (const char *gmsgid, ...)
1271 {
1272 diagnostic_info diagnostic;
1273 va_list ap;
1274 rich_location richloc (line_table, input_location);
1275
1276 va_start (ap, gmsgid);
1277 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_ICE_NOBT);
1278 report_diagnostic (&diagnostic);
1279 va_end (ap);
1280
1281 gcc_unreachable ();
1282 }
1283 \f
1284 /* Special case error functions. Most are implemented in terms of the
1285 above, or should be. */
1286
1287 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1288 runs its second argument through gettext. */
1289 void
1290 fnotice (FILE *file, const char *cmsgid, ...)
1291 {
1292 va_list ap;
1293
1294 va_start (ap, cmsgid);
1295 vfprintf (file, _(cmsgid), ap);
1296 va_end (ap);
1297 }
1298
1299 /* Inform the user that an error occurred while trying to report some
1300 other error. This indicates catastrophic internal inconsistencies,
1301 so give up now. But do try to flush out the previous error.
1302 This mustn't use internal_error, that will cause infinite recursion. */
1303
1304 static void
1305 error_recursion (diagnostic_context *context)
1306 {
1307 if (context->lock < 3)
1308 pp_newline_and_flush (context->printer);
1309
1310 fnotice (stderr,
1311 "Internal compiler error: Error reporting routines re-entered.\n");
1312
1313 /* Call diagnostic_action_after_output to get the "please submit a bug
1314 report" message. */
1315 diagnostic_action_after_output (context, DK_ICE);
1316
1317 /* Do not use gcc_unreachable here; that goes through internal_error
1318 and therefore would cause infinite recursion. */
1319 real_abort ();
1320 }
1321
1322 /* Report an internal compiler error in a friendly manner. This is
1323 the function that gets called upon use of abort() in the source
1324 code generally, thanks to a special macro. */
1325
1326 void
1327 fancy_abort (const char *file, int line, const char *function)
1328 {
1329 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1330 }
1331
1332 /* Really call the system 'abort'. This has to go right at the end of
1333 this file, so that there are no functions after it that call abort
1334 and get the system abort instead of our macro. */
1335 #undef abort
1336 static void
1337 real_abort (void)
1338 {
1339 abort ();
1340 }