]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/diagnostic.c
The flag diagnostic_context::some_warnings_are_errors controls whether to give...
[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 "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 < MAX_LOCATIONS_PER_MESSAGE; 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 diagnostic_finalizer (context) = default_diagnostic_finalizer;
162 context->option_enabled = NULL;
163 context->option_state = NULL;
164 context->option_name = NULL;
165 context->last_location = UNKNOWN_LOCATION;
166 context->last_module = 0;
167 context->x_data = NULL;
168 context->lock = 0;
169 context->inhibit_notes_p = false;
170 }
171
172 /* Maybe initialize the color support. We require clients to do this
173 explicitly, since most clients don't want color. When called
174 without a VALUE, it initializes with DIAGNOSTICS_COLOR_DEFAULT. */
175
176 void
177 diagnostic_color_init (diagnostic_context *context, int value /*= -1 */)
178 {
179 /* value == -1 is the default value. */
180 if (value < 0)
181 {
182 /* If DIAGNOSTICS_COLOR_DEFAULT is -1, default to
183 -fdiagnostics-color=auto if GCC_COLORS is in the environment,
184 otherwise default to -fdiagnostics-color=never, for other
185 values default to that
186 -fdiagnostics-color={never,auto,always}. */
187 if (DIAGNOSTICS_COLOR_DEFAULT == -1)
188 {
189 if (!getenv ("GCC_COLORS"))
190 return;
191 value = DIAGNOSTICS_COLOR_AUTO;
192 }
193 else
194 value = DIAGNOSTICS_COLOR_DEFAULT;
195 }
196 pp_show_color (context->printer)
197 = colorize_init ((diagnostic_color_rule_t) value);
198 }
199
200 /* Do any cleaning up required after the last diagnostic is emitted. */
201
202 void
203 diagnostic_finish (diagnostic_context *context)
204 {
205 /* Some of the errors may actually have been warnings. */
206 if (diagnostic_kind_count (context, DK_WERROR))
207 {
208 /* -Werror was given. */
209 if (context->warning_as_error_requested)
210 pp_verbatim (context->printer,
211 _("%s: all warnings being treated as errors"),
212 progname);
213 /* At least one -Werror= was given. */
214 else
215 pp_verbatim (context->printer,
216 _("%s: some warnings being treated as errors"),
217 progname);
218 pp_newline_and_flush (context->printer);
219 }
220
221 diagnostic_file_cache_fini ();
222
223 XDELETEVEC (context->classify_diagnostic);
224 context->classify_diagnostic = NULL;
225
226 /* diagnostic_initialize allocates context->printer using XNEW
227 and placement-new. */
228 context->printer->~pretty_printer ();
229 XDELETE (context->printer);
230 context->printer = NULL;
231 }
232
233 /* Initialize DIAGNOSTIC, where the message MSG has already been
234 translated. */
235 void
236 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
237 va_list *args, location_t location,
238 diagnostic_t kind)
239 {
240 diagnostic->message.err_no = errno;
241 diagnostic->message.args_ptr = args;
242 diagnostic->message.format_spec = msg;
243 diagnostic->message.set_location (0, location);
244 for (int i = 1; i < MAX_LOCATIONS_PER_MESSAGE; i++)
245 diagnostic->message.set_location (i, UNKNOWN_LOCATION);
246 diagnostic->override_column = 0;
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, location_t location,
256 diagnostic_t kind)
257 {
258 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
259 }
260
261 /* Return a malloc'd string describing a location. The caller is
262 responsible for freeing the memory. */
263 char *
264 diagnostic_build_prefix (diagnostic_context *context,
265 const diagnostic_info *diagnostic)
266 {
267 static const char *const diagnostic_kind_text[] = {
268 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
269 #include "diagnostic.def"
270 #undef DEFINE_DIAGNOSTIC_KIND
271 "must-not-happen"
272 };
273 static const char *const diagnostic_kind_color[] = {
274 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
275 #include "diagnostic.def"
276 #undef DEFINE_DIAGNOSTIC_KIND
277 NULL
278 };
279 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
280
281 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
282 const char *text_cs = "", *text_ce = "";
283 const char *locus_cs, *locus_ce;
284 pretty_printer *pp = context->printer;
285
286 if (diagnostic_kind_color[diagnostic->kind])
287 {
288 text_cs = colorize_start (pp_show_color (pp),
289 diagnostic_kind_color[diagnostic->kind]);
290 text_ce = colorize_stop (pp_show_color (pp));
291 }
292 locus_cs = colorize_start (pp_show_color (pp), "locus");
293 locus_ce = colorize_stop (pp_show_color (pp));
294
295 expanded_location s = diagnostic_expand_location (diagnostic);
296 return
297 (s.file == NULL
298 ? build_message_string ("%s%s:%s %s%s%s", locus_cs, progname, locus_ce,
299 text_cs, text, text_ce)
300 : !strcmp (s.file, N_("<built-in>"))
301 ? build_message_string ("%s%s:%s %s%s%s", locus_cs, s.file, locus_ce,
302 text_cs, text, text_ce)
303 : context->show_column
304 ? build_message_string ("%s%s:%d:%d:%s %s%s%s", locus_cs, s.file, s.line,
305 s.column, locus_ce, text_cs, text, text_ce)
306 : build_message_string ("%s%s:%d:%s %s%s%s", locus_cs, s.file, s.line,
307 locus_ce, text_cs, text, text_ce));
308 }
309
310 /* If LINE is longer than MAX_WIDTH, and COLUMN is not smaller than
311 MAX_WIDTH by some margin, then adjust the start of the line such
312 that the COLUMN is smaller than MAX_WIDTH minus the margin. The
313 margin is either CARET_LINE_MARGIN characters or the difference
314 between the column and the length of the line, whatever is smaller.
315 The length of LINE is given by LINE_WIDTH. */
316 static const char *
317 adjust_line (const char *line, int line_width,
318 int max_width, int *column_p)
319 {
320 int right_margin = CARET_LINE_MARGIN;
321 int column = *column_p;
322
323 gcc_checking_assert (line_width >= column);
324 right_margin = MIN (line_width - column, right_margin);
325 right_margin = max_width - right_margin;
326 if (line_width >= max_width && column > right_margin)
327 {
328 line += column - right_margin;
329 *column_p = right_margin;
330 }
331 return line;
332 }
333
334 /* Print the physical source line corresponding to the location of
335 this diagnostic, and a caret indicating the precise column. This
336 function only prints two caret characters if the two locations
337 given by DIAGNOSTIC are on the same line according to
338 diagnostic_same_line(). */
339 void
340 diagnostic_show_locus (diagnostic_context * context,
341 const diagnostic_info *diagnostic)
342 {
343 if (!context->show_caret
344 || diagnostic_location (diagnostic, 0) <= BUILTINS_LOCATION
345 || diagnostic_location (diagnostic, 0) == context->last_location)
346 return;
347
348 context->last_location = diagnostic_location (diagnostic, 0);
349 expanded_location s0 = diagnostic_expand_location (diagnostic, 0);
350 expanded_location s1 = { };
351 /* Zero-initialized. This is checked later by diagnostic_print_caret_line. */
352
353 if (diagnostic_location (diagnostic, 1) > BUILTINS_LOCATION)
354 s1 = diagnostic_expand_location (diagnostic, 1);
355
356 diagnostic_print_caret_line (context, s0, s1,
357 context->caret_chars[0],
358 context->caret_chars[1]);
359 }
360
361 /* Print (part) of the source line given by xloc1 with caret1 pointing
362 at the column. If xloc2.column != 0 and it fits within the same
363 line as xloc1 according to diagnostic_same_line (), then caret2 is
364 printed at xloc2.colum. Otherwise, the caller has to set up things
365 to print a second caret line for xloc2. */
366 void
367 diagnostic_print_caret_line (diagnostic_context * context,
368 expanded_location xloc1,
369 expanded_location xloc2,
370 char caret1, char caret2)
371 {
372 if (!diagnostic_same_line (context, xloc1, xloc2))
373 /* This will mean ignore xloc2. */
374 xloc2.column = 0;
375 else if (xloc1.column == xloc2.column)
376 xloc2.column++;
377
378 int cmax = MAX (xloc1.column, xloc2.column);
379 int line_width;
380 const char *line = location_get_source_line (xloc1, &line_width);
381 if (line == NULL || cmax > line_width)
382 return;
383
384 /* Center the interesting part of the source line to fit in
385 max_width, and adjust all columns accordingly. */
386 int max_width = context->caret_max_width;
387 int offset = (int) cmax;
388 line = adjust_line (line, line_width, max_width, &offset);
389 offset -= cmax;
390 cmax += offset;
391 xloc1.column += offset;
392 if (xloc2.column)
393 xloc2.column += offset;
394
395 /* Print the source line. */
396 pp_newline (context->printer);
397 const char *saved_prefix = pp_get_prefix (context->printer);
398 pp_set_prefix (context->printer, NULL);
399 pp_space (context->printer);
400 while (max_width > 0 && line_width > 0)
401 {
402 char c = *line == '\t' ? ' ' : *line;
403 if (c == '\0')
404 c = ' ';
405 pp_character (context->printer, c);
406 max_width--;
407 line_width--;
408 line++;
409 }
410 pp_newline (context->printer);
411
412 /* Print the caret under the line. */
413 const char *caret_cs, *caret_ce;
414 caret_cs = colorize_start (pp_show_color (context->printer), "caret");
415 caret_ce = colorize_stop (pp_show_color (context->printer));
416 int cmin = xloc2.column
417 ? MIN (xloc1.column, xloc2.column) : xloc1.column;
418 int caret_min = cmin == xloc1.column ? caret1 : caret2;
419 int caret_max = cmin == xloc1.column ? caret2 : caret1;
420
421 /* cmin is >= 1, but we indent with an extra space at the start like
422 we did above. */
423 int i;
424 for (i = 0; i < cmin; i++)
425 pp_space (context->printer);
426 pp_printf (context->printer, "%s%c%s", caret_cs, caret_min, caret_ce);
427
428 if (xloc2.column)
429 {
430 for (i++; i < cmax; i++)
431 pp_space (context->printer);
432 pp_printf (context->printer, "%s%c%s", caret_cs, caret_max, caret_ce);
433 }
434 pp_set_prefix (context->printer, saved_prefix);
435 pp_needs_newline (context->printer) = true;
436 }
437
438 /* Functions at which to stop the backtrace print. It's not
439 particularly helpful to print the callers of these functions. */
440
441 static const char * const bt_stop[] =
442 {
443 "main",
444 "toplev::main",
445 "execute_one_pass",
446 "compile_file",
447 };
448
449 /* A callback function passed to the backtrace_full function. */
450
451 static int
452 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
453 const char *function)
454 {
455 int *pcount = (int *) data;
456
457 /* If we don't have any useful information, don't print
458 anything. */
459 if (filename == NULL && function == NULL)
460 return 0;
461
462 /* Skip functions in diagnostic.c. */
463 if (*pcount == 0
464 && filename != NULL
465 && strcmp (lbasename (filename), "diagnostic.c") == 0)
466 return 0;
467
468 /* Print up to 20 functions. We could make this a --param, but
469 since this is only for debugging just use a constant for now. */
470 if (*pcount >= 20)
471 {
472 /* Returning a non-zero value stops the backtrace. */
473 return 1;
474 }
475 ++*pcount;
476
477 char *alc = NULL;
478 if (function != NULL)
479 {
480 char *str = cplus_demangle_v3 (function,
481 (DMGL_VERBOSE | DMGL_ANSI
482 | DMGL_GNU_V3 | DMGL_PARAMS));
483 if (str != NULL)
484 {
485 alc = str;
486 function = str;
487 }
488
489 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
490 {
491 size_t len = strlen (bt_stop[i]);
492 if (strncmp (function, bt_stop[i], len) == 0
493 && (function[len] == '\0' || function[len] == '('))
494 {
495 if (alc != NULL)
496 free (alc);
497 /* Returning a non-zero value stops the backtrace. */
498 return 1;
499 }
500 }
501 }
502
503 fprintf (stderr, "0x%lx %s\n\t%s:%d\n",
504 (unsigned long) pc,
505 function == NULL ? "???" : function,
506 filename == NULL ? "???" : filename,
507 lineno);
508
509 if (alc != NULL)
510 free (alc);
511
512 return 0;
513 }
514
515 /* A callback function passed to the backtrace_full function. This is
516 called if backtrace_full has an error. */
517
518 static void
519 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
520 {
521 if (errnum < 0)
522 {
523 /* This means that no debug info was available. Just quietly
524 skip printing backtrace info. */
525 return;
526 }
527 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
528 errnum == 0 ? "" : xstrerror (errnum));
529 }
530
531 /* Take any action which is expected to happen after the diagnostic
532 is written out. This function does not always return. */
533 void
534 diagnostic_action_after_output (diagnostic_context *context,
535 diagnostic_t diag_kind)
536 {
537 switch (diag_kind)
538 {
539 case DK_DEBUG:
540 case DK_NOTE:
541 case DK_ANACHRONISM:
542 case DK_WARNING:
543 break;
544
545 case DK_ERROR:
546 case DK_SORRY:
547 if (context->abort_on_error)
548 real_abort ();
549 if (context->fatal_errors)
550 {
551 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
552 diagnostic_finish (context);
553 exit (FATAL_EXIT_CODE);
554 }
555 if (context->max_errors != 0
556 && ((unsigned) (diagnostic_kind_count (context, DK_ERROR)
557 + diagnostic_kind_count (context, DK_SORRY)
558 + diagnostic_kind_count (context, DK_WERROR))
559 >= context->max_errors))
560 {
561 fnotice (stderr,
562 "compilation terminated due to -fmax-errors=%u.\n",
563 context->max_errors);
564 diagnostic_finish (context);
565 exit (FATAL_EXIT_CODE);
566 }
567 break;
568
569 case DK_ICE:
570 case DK_ICE_NOBT:
571 {
572 struct backtrace_state *state = NULL;
573 if (diag_kind == DK_ICE)
574 state = backtrace_create_state (NULL, 0, bt_err_callback, NULL);
575 int count = 0;
576 if (state != NULL)
577 backtrace_full (state, 2, bt_callback, bt_err_callback,
578 (void *) &count);
579
580 if (context->abort_on_error)
581 real_abort ();
582
583 fnotice (stderr, "Please submit a full bug report,\n"
584 "with preprocessed source if appropriate.\n");
585 if (count > 0)
586 fnotice (stderr,
587 ("Please include the complete backtrace "
588 "with any bug report.\n"));
589 fnotice (stderr, "See %s for instructions.\n", bug_report_url);
590
591 exit (ICE_EXIT_CODE);
592 }
593
594 case DK_FATAL:
595 if (context->abort_on_error)
596 real_abort ();
597 diagnostic_finish (context);
598 fnotice (stderr, "compilation terminated.\n");
599 exit (FATAL_EXIT_CODE);
600
601 default:
602 gcc_unreachable ();
603 }
604 }
605
606 void
607 diagnostic_report_current_module (diagnostic_context *context, location_t where)
608 {
609 const line_map_ordinary *map = NULL;
610
611 if (pp_needs_newline (context->printer))
612 {
613 pp_newline (context->printer);
614 pp_needs_newline (context->printer) = false;
615 }
616
617 if (where <= BUILTINS_LOCATION)
618 return;
619
620 linemap_resolve_location (line_table, where,
621 LRK_MACRO_DEFINITION_LOCATION,
622 &map);
623
624 if (map && diagnostic_last_module_changed (context, map))
625 {
626 diagnostic_set_last_module (context, map);
627 if (! MAIN_FILE_P (map))
628 {
629 map = INCLUDED_FROM (line_table, map);
630 if (context->show_column)
631 pp_verbatim (context->printer,
632 "In file included from %r%s:%d:%d%R", "locus",
633 LINEMAP_FILE (map),
634 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
635 else
636 pp_verbatim (context->printer,
637 "In file included from %r%s:%d%R", "locus",
638 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
639 while (! MAIN_FILE_P (map))
640 {
641 map = INCLUDED_FROM (line_table, map);
642 pp_verbatim (context->printer,
643 ",\n from %r%s:%d%R", "locus",
644 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
645 }
646 pp_verbatim (context->printer, ":");
647 pp_newline (context->printer);
648 }
649 }
650 }
651
652 void
653 default_diagnostic_starter (diagnostic_context *context,
654 diagnostic_info *diagnostic)
655 {
656 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
657 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
658 diagnostic));
659 }
660
661 void
662 default_diagnostic_finalizer (diagnostic_context *context,
663 diagnostic_info *diagnostic)
664 {
665 diagnostic_show_locus (context, diagnostic);
666 pp_destroy_prefix (context->printer);
667 pp_newline_and_flush (context->printer);
668 }
669
670 /* Interface to specify diagnostic kind overrides. Returns the
671 previous setting, or DK_UNSPECIFIED if the parameters are out of
672 range. If OPTION_INDEX is zero, the new setting is for all the
673 diagnostics. */
674 diagnostic_t
675 diagnostic_classify_diagnostic (diagnostic_context *context,
676 int option_index,
677 diagnostic_t new_kind,
678 location_t where)
679 {
680 diagnostic_t old_kind;
681
682 if (option_index < 0
683 || option_index >= context->n_opts
684 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
685 return DK_UNSPECIFIED;
686
687 old_kind = context->classify_diagnostic[option_index];
688
689 /* Handle pragmas separately, since we need to keep track of *where*
690 the pragmas were. */
691 if (where != UNKNOWN_LOCATION)
692 {
693 int i;
694
695 /* Record the command-line status, so we can reset it back on DK_POP. */
696 if (old_kind == DK_UNSPECIFIED)
697 {
698 old_kind = !context->option_enabled (option_index,
699 context->option_state)
700 ? DK_IGNORED : (context->warning_as_error_requested
701 ? DK_ERROR : DK_WARNING);
702 context->classify_diagnostic[option_index] = old_kind;
703 }
704
705 for (i = context->n_classification_history - 1; i >= 0; i --)
706 if (context->classification_history[i].option == option_index)
707 {
708 old_kind = context->classification_history[i].kind;
709 break;
710 }
711
712 i = context->n_classification_history;
713 context->classification_history =
714 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
715 * sizeof (diagnostic_classification_change_t));
716 context->classification_history[i].location = where;
717 context->classification_history[i].option = option_index;
718 context->classification_history[i].kind = new_kind;
719 context->n_classification_history ++;
720 }
721 else
722 context->classify_diagnostic[option_index] = new_kind;
723
724 return old_kind;
725 }
726
727 /* Save all diagnostic classifications in a stack. */
728 void
729 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
730 {
731 context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
732 context->push_list[context->n_push ++] = context->n_classification_history;
733 }
734
735 /* Restore the topmost classification set off the stack. If the stack
736 is empty, revert to the state based on command line parameters. */
737 void
738 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
739 {
740 int jump_to;
741 int i;
742
743 if (context->n_push)
744 jump_to = context->push_list [-- context->n_push];
745 else
746 jump_to = 0;
747
748 i = context->n_classification_history;
749 context->classification_history =
750 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
751 * sizeof (diagnostic_classification_change_t));
752 context->classification_history[i].location = where;
753 context->classification_history[i].option = jump_to;
754 context->classification_history[i].kind = DK_POP;
755 context->n_classification_history ++;
756 }
757
758 /* Report a diagnostic message (an error or a warning) as specified by
759 DC. This function is *the* subroutine in terms of which front-ends
760 should implement their specific diagnostic handling modules. The
761 front-end independent format specifiers are exactly those described
762 in the documentation of output_format.
763 Return true if a diagnostic was printed, false otherwise. */
764
765 bool
766 diagnostic_report_diagnostic (diagnostic_context *context,
767 diagnostic_info *diagnostic)
768 {
769 location_t location = diagnostic_location (diagnostic);
770 diagnostic_t orig_diag_kind = diagnostic->kind;
771 const char *saved_format_spec;
772
773 /* Give preference to being able to inhibit warnings, before they
774 get reclassified to something else. */
775 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
776 && !diagnostic_report_warnings_p (context, location))
777 return false;
778
779 if (diagnostic->kind == DK_PEDWARN)
780 {
781 diagnostic->kind = pedantic_warning_kind (context);
782 /* We do this to avoid giving the message for -pedantic-errors. */
783 orig_diag_kind = diagnostic->kind;
784 }
785
786 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
787 return false;
788
789 if (context->lock > 0)
790 {
791 /* If we're reporting an ICE in the middle of some other error,
792 try to flush out the previous error, then let this one
793 through. Don't do this more than once. */
794 if ((diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
795 && context->lock == 1)
796 pp_newline_and_flush (context->printer);
797 else
798 error_recursion (context);
799 }
800
801 /* If the user requested that warnings be treated as errors, so be
802 it. Note that we do this before the next block so that
803 individual warnings can be overridden back to warnings with
804 -Wno-error=*. */
805 if (context->warning_as_error_requested
806 && diagnostic->kind == DK_WARNING)
807 {
808 diagnostic->kind = DK_ERROR;
809 }
810
811 if (diagnostic->option_index
812 && diagnostic->option_index != permissive_error_option (context))
813 {
814 diagnostic_t diag_class = DK_UNSPECIFIED;
815
816 /* This tests if the user provided the appropriate -Wfoo or
817 -Wno-foo option. */
818 if (! context->option_enabled (diagnostic->option_index,
819 context->option_state))
820 return false;
821
822 /* This tests for #pragma diagnostic changes. */
823 if (context->n_classification_history > 0)
824 {
825 /* FIXME: Stupid search. Optimize later. */
826 for (int i = context->n_classification_history - 1; i >= 0; i --)
827 {
828 if (linemap_location_before_p
829 (line_table,
830 context->classification_history[i].location,
831 location))
832 {
833 if (context->classification_history[i].kind == (int) DK_POP)
834 {
835 i = context->classification_history[i].option;
836 continue;
837 }
838 int option = context->classification_history[i].option;
839 /* The option 0 is for all the diagnostics. */
840 if (option == 0 || option == diagnostic->option_index)
841 {
842 diag_class = context->classification_history[i].kind;
843 if (diag_class != DK_UNSPECIFIED)
844 diagnostic->kind = diag_class;
845 break;
846 }
847 }
848 }
849 }
850 /* This tests if the user provided the appropriate -Werror=foo
851 option. */
852 if (diag_class == DK_UNSPECIFIED
853 && context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
854 {
855 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
856 }
857 /* This allows for future extensions, like temporarily disabling
858 warnings for ranges of source code. */
859 if (diagnostic->kind == DK_IGNORED)
860 return false;
861 }
862
863 context->lock++;
864
865 if (diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
866 {
867 #ifndef ENABLE_CHECKING
868 /* When not checking, ICEs are converted to fatal errors when an
869 error has already occurred. This is counteracted by
870 abort_on_error. */
871 if ((diagnostic_kind_count (context, DK_ERROR) > 0
872 || diagnostic_kind_count (context, DK_SORRY) > 0)
873 && !context->abort_on_error)
874 {
875 expanded_location s
876 = expand_location (diagnostic_location (diagnostic));
877 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
878 s.file, s.line);
879 exit (ICE_EXIT_CODE);
880 }
881 #endif
882 if (context->internal_error)
883 (*context->internal_error) (context,
884 diagnostic->message.format_spec,
885 diagnostic->message.args_ptr);
886 }
887 if (diagnostic->kind == DK_ERROR && orig_diag_kind == DK_WARNING)
888 ++diagnostic_kind_count (context, DK_WERROR);
889 else
890 ++diagnostic_kind_count (context, diagnostic->kind);
891
892 saved_format_spec = diagnostic->message.format_spec;
893 if (context->show_option_requested)
894 {
895 char *option_text;
896
897 option_text = context->option_name (context, diagnostic->option_index,
898 orig_diag_kind, diagnostic->kind);
899
900 if (option_text)
901 {
902 diagnostic->message.format_spec
903 = ACONCAT ((diagnostic->message.format_spec,
904 " ",
905 "[", option_text, "]",
906 NULL));
907 free (option_text);
908 }
909 }
910 diagnostic->message.x_data = &diagnostic->x_data;
911 diagnostic->x_data = NULL;
912 pp_format (context->printer, &diagnostic->message);
913 (*diagnostic_starter (context)) (context, diagnostic);
914 pp_output_formatted_text (context->printer);
915 (*diagnostic_finalizer (context)) (context, diagnostic);
916 diagnostic_action_after_output (context, diagnostic->kind);
917 diagnostic->message.format_spec = saved_format_spec;
918 diagnostic->x_data = NULL;
919
920 context->lock--;
921
922 return true;
923 }
924
925 /* Given a partial pathname as input, return another pathname that
926 shares no directory elements with the pathname of __FILE__. This
927 is used by fancy_abort() to print `Internal compiler error in expr.c'
928 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
929
930 const char *
931 trim_filename (const char *name)
932 {
933 static const char this_file[] = __FILE__;
934 const char *p = name, *q = this_file;
935
936 /* First skip any "../" in each filename. This allows us to give a proper
937 reference to a file in a subdirectory. */
938 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
939 p += 3;
940
941 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
942 q += 3;
943
944 /* Now skip any parts the two filenames have in common. */
945 while (*p == *q && *p != 0 && *q != 0)
946 p++, q++;
947
948 /* Now go backwards until the previous directory separator. */
949 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
950 p--;
951
952 return p;
953 }
954 \f
955 /* Standard error reporting routines in increasing order of severity.
956 All of these take arguments like printf. */
957
958 /* Text to be emitted verbatim to the error message stream; this
959 produces no prefix and disables line-wrapping. Use rarely. */
960 void
961 verbatim (const char *gmsgid, ...)
962 {
963 text_info text;
964 va_list ap;
965
966 va_start (ap, gmsgid);
967 text.err_no = errno;
968 text.args_ptr = &ap;
969 text.format_spec = _(gmsgid);
970 text.x_data = NULL;
971 pp_format_verbatim (global_dc->printer, &text);
972 pp_newline_and_flush (global_dc->printer);
973 va_end (ap);
974 }
975
976 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
977 void
978 diagnostic_append_note (diagnostic_context *context,
979 location_t location,
980 const char * gmsgid, ...)
981 {
982 diagnostic_info diagnostic;
983 va_list ap;
984 const char *saved_prefix;
985
986 va_start (ap, gmsgid);
987 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
988 if (context->inhibit_notes_p)
989 {
990 va_end (ap);
991 return;
992 }
993 saved_prefix = pp_get_prefix (context->printer);
994 pp_set_prefix (context->printer,
995 diagnostic_build_prefix (context, &diagnostic));
996 pp_newline (context->printer);
997 pp_format (context->printer, &diagnostic.message);
998 pp_output_formatted_text (context->printer);
999 pp_destroy_prefix (context->printer);
1000 pp_set_prefix (context->printer, saved_prefix);
1001 diagnostic_show_locus (context, &diagnostic);
1002 va_end (ap);
1003 }
1004
1005 bool
1006 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
1007 const char *gmsgid, ...)
1008 {
1009 diagnostic_info diagnostic;
1010 va_list ap;
1011 bool ret;
1012
1013 va_start (ap, gmsgid);
1014 if (kind == DK_PERMERROR)
1015 {
1016 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
1017 permissive_error_kind (global_dc));
1018 diagnostic.option_index = permissive_error_option (global_dc);
1019 }
1020 else {
1021 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, kind);
1022 if (kind == DK_WARNING || kind == DK_PEDWARN)
1023 diagnostic.option_index = opt;
1024 }
1025
1026 ret = report_diagnostic (&diagnostic);
1027 va_end (ap);
1028 return ret;
1029 }
1030
1031 /* An informative note at LOCATION. Use this for additional details on an error
1032 message. */
1033 void
1034 inform (location_t location, const char *gmsgid, ...)
1035 {
1036 diagnostic_info diagnostic;
1037 va_list ap;
1038
1039 va_start (ap, gmsgid);
1040 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
1041 report_diagnostic (&diagnostic);
1042 va_end (ap);
1043 }
1044
1045 /* An informative note at LOCATION. Use this for additional details on an
1046 error message. */
1047 void
1048 inform_n (location_t location, int n, const char *singular_gmsgid,
1049 const char *plural_gmsgid, ...)
1050 {
1051 diagnostic_info diagnostic;
1052 va_list ap;
1053
1054 va_start (ap, plural_gmsgid);
1055 diagnostic_set_info_translated (&diagnostic,
1056 ngettext (singular_gmsgid, plural_gmsgid, n),
1057 &ap, location, DK_NOTE);
1058 report_diagnostic (&diagnostic);
1059 va_end (ap);
1060 }
1061
1062 /* A warning at INPUT_LOCATION. Use this for code which is correct according
1063 to the relevant language specification but is likely to be buggy anyway.
1064 Returns true if the warning was printed, false if it was inhibited. */
1065 bool
1066 warning (int opt, const char *gmsgid, ...)
1067 {
1068 diagnostic_info diagnostic;
1069 va_list ap;
1070 bool ret;
1071
1072 va_start (ap, gmsgid);
1073 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
1074 diagnostic.option_index = opt;
1075
1076 ret = report_diagnostic (&diagnostic);
1077 va_end (ap);
1078 return ret;
1079 }
1080
1081 /* A warning at LOCATION. Use this for code which is correct according to the
1082 relevant language specification but is likely to be buggy anyway.
1083 Returns true if the warning was printed, false if it was inhibited. */
1084
1085 bool
1086 warning_at (location_t location, int opt, const char *gmsgid, ...)
1087 {
1088 diagnostic_info diagnostic;
1089 va_list ap;
1090 bool ret;
1091
1092 va_start (ap, gmsgid);
1093 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_WARNING);
1094 diagnostic.option_index = opt;
1095 ret = report_diagnostic (&diagnostic);
1096 va_end (ap);
1097 return ret;
1098 }
1099
1100 /* A warning at LOCATION. Use this for code which is correct according to the
1101 relevant language specification but is likely to be buggy anyway.
1102 Returns true if the warning was printed, false if it was inhibited. */
1103
1104 bool
1105 warning_n (location_t location, int opt, int n, const char *singular_gmsgid,
1106 const char *plural_gmsgid, ...)
1107 {
1108 diagnostic_info diagnostic;
1109 va_list ap;
1110 bool ret;
1111
1112 va_start (ap, plural_gmsgid);
1113 diagnostic_set_info_translated (&diagnostic,
1114 ngettext (singular_gmsgid, plural_gmsgid, n),
1115 &ap, location, DK_WARNING);
1116 diagnostic.option_index = opt;
1117 ret = report_diagnostic (&diagnostic);
1118 va_end (ap);
1119 return ret;
1120 }
1121
1122 /* A "pedantic" warning at LOCATION: issues a warning unless
1123 -pedantic-errors was given on the command line, in which case it
1124 issues an error. Use this for diagnostics required by the relevant
1125 language standard, if you have chosen not to make them errors.
1126
1127 Note that these diagnostics are issued independent of the setting
1128 of the -Wpedantic command-line switch. To get a warning enabled
1129 only with that switch, use either "if (pedantic) pedwarn
1130 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
1131 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
1132
1133 Returns true if the warning was printed, false if it was inhibited. */
1134
1135 bool
1136 pedwarn (location_t location, int opt, const char *gmsgid, ...)
1137 {
1138 diagnostic_info diagnostic;
1139 va_list ap;
1140 bool ret;
1141
1142 va_start (ap, gmsgid);
1143 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_PEDWARN);
1144 diagnostic.option_index = opt;
1145 ret = report_diagnostic (&diagnostic);
1146 va_end (ap);
1147 return ret;
1148 }
1149
1150 /* A "permissive" error at LOCATION: issues an error unless
1151 -fpermissive was given on the command line, in which case it issues
1152 a warning. Use this for things that really should be errors but we
1153 want to support legacy code.
1154
1155 Returns true if the warning was printed, false if it was inhibited. */
1156
1157 bool
1158 permerror (location_t location, const char *gmsgid, ...)
1159 {
1160 diagnostic_info diagnostic;
1161 va_list ap;
1162 bool ret;
1163
1164 va_start (ap, gmsgid);
1165 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
1166 permissive_error_kind (global_dc));
1167 diagnostic.option_index = permissive_error_option (global_dc);
1168 ret = report_diagnostic (&diagnostic);
1169 va_end (ap);
1170 return ret;
1171 }
1172
1173 /* A hard error: the code is definitely ill-formed, and an object file
1174 will not be produced. */
1175 void
1176 error (const char *gmsgid, ...)
1177 {
1178 diagnostic_info diagnostic;
1179 va_list ap;
1180
1181 va_start (ap, gmsgid);
1182 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
1183 report_diagnostic (&diagnostic);
1184 va_end (ap);
1185 }
1186
1187 /* A hard error: the code is definitely ill-formed, and an object file
1188 will not be produced. */
1189 void
1190 error_n (location_t location, int n, const char *singular_gmsgid,
1191 const char *plural_gmsgid, ...)
1192 {
1193 diagnostic_info diagnostic;
1194 va_list ap;
1195
1196 va_start (ap, plural_gmsgid);
1197 diagnostic_set_info_translated (&diagnostic,
1198 ngettext (singular_gmsgid, plural_gmsgid, n),
1199 &ap, location, DK_ERROR);
1200 report_diagnostic (&diagnostic);
1201 va_end (ap);
1202 }
1203
1204 /* Same as ebove, but use location LOC instead of input_location. */
1205 void
1206 error_at (location_t loc, const char *gmsgid, ...)
1207 {
1208 diagnostic_info diagnostic;
1209 va_list ap;
1210
1211 va_start (ap, gmsgid);
1212 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_ERROR);
1213 report_diagnostic (&diagnostic);
1214 va_end (ap);
1215 }
1216
1217 /* "Sorry, not implemented." Use for a language feature which is
1218 required by the relevant specification but not implemented by GCC.
1219 An object file will not be produced. */
1220 void
1221 sorry (const char *gmsgid, ...)
1222 {
1223 diagnostic_info diagnostic;
1224 va_list ap;
1225
1226 va_start (ap, gmsgid);
1227 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
1228 report_diagnostic (&diagnostic);
1229 va_end (ap);
1230 }
1231
1232 /* Return true if an error or a "sorry" has been seen. Various
1233 processing is disabled after errors. */
1234 bool
1235 seen_error (void)
1236 {
1237 return errorcount || sorrycount;
1238 }
1239
1240 /* An error which is severe enough that we make no attempt to
1241 continue. Do not use this for internal consistency checks; that's
1242 internal_error. Use of this function should be rare. */
1243 void
1244 fatal_error (location_t loc, const char *gmsgid, ...)
1245 {
1246 diagnostic_info diagnostic;
1247 va_list ap;
1248
1249 va_start (ap, gmsgid);
1250 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_FATAL);
1251 report_diagnostic (&diagnostic);
1252 va_end (ap);
1253
1254 gcc_unreachable ();
1255 }
1256
1257 /* An internal consistency check has failed. We make no attempt to
1258 continue. Note that unless there is debugging value to be had from
1259 a more specific message, or some other good reason, you should use
1260 abort () instead of calling this function directly. */
1261 void
1262 internal_error (const char *gmsgid, ...)
1263 {
1264 diagnostic_info diagnostic;
1265 va_list ap;
1266
1267 va_start (ap, gmsgid);
1268 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
1269 report_diagnostic (&diagnostic);
1270 va_end (ap);
1271
1272 gcc_unreachable ();
1273 }
1274
1275 /* Like internal_error, but no backtrace will be printed. Used when
1276 the internal error does not happen at the current location, but happened
1277 somewhere else. */
1278 void
1279 internal_error_no_backtrace (const char *gmsgid, ...)
1280 {
1281 diagnostic_info diagnostic;
1282 va_list ap;
1283
1284 va_start (ap, gmsgid);
1285 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE_NOBT);
1286 report_diagnostic (&diagnostic);
1287 va_end (ap);
1288
1289 gcc_unreachable ();
1290 }
1291 \f
1292 /* Special case error functions. Most are implemented in terms of the
1293 above, or should be. */
1294
1295 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1296 runs its second argument through gettext. */
1297 void
1298 fnotice (FILE *file, const char *cmsgid, ...)
1299 {
1300 va_list ap;
1301
1302 va_start (ap, cmsgid);
1303 vfprintf (file, _(cmsgid), ap);
1304 va_end (ap);
1305 }
1306
1307 /* Inform the user that an error occurred while trying to report some
1308 other error. This indicates catastrophic internal inconsistencies,
1309 so give up now. But do try to flush out the previous error.
1310 This mustn't use internal_error, that will cause infinite recursion. */
1311
1312 static void
1313 error_recursion (diagnostic_context *context)
1314 {
1315 if (context->lock < 3)
1316 pp_newline_and_flush (context->printer);
1317
1318 fnotice (stderr,
1319 "Internal compiler error: Error reporting routines re-entered.\n");
1320
1321 /* Call diagnostic_action_after_output to get the "please submit a bug
1322 report" message. */
1323 diagnostic_action_after_output (context, DK_ICE);
1324
1325 /* Do not use gcc_unreachable here; that goes through internal_error
1326 and therefore would cause infinite recursion. */
1327 real_abort ();
1328 }
1329
1330 /* Report an internal compiler error in a friendly manner. This is
1331 the function that gets called upon use of abort() in the source
1332 code generally, thanks to a special macro. */
1333
1334 void
1335 fancy_abort (const char *file, int line, const char *function)
1336 {
1337 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1338 }
1339
1340 /* Really call the system 'abort'. This has to go right at the end of
1341 this file, so that there are no functions after it that call abort
1342 and get the system abort instead of our macro. */
1343 #undef abort
1344 static void
1345 real_abort (void)
1346 {
1347 abort ();
1348 }