]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/diagnostic.c
preprocessor/58580 - preprocessor goes OOM with warning for zero literals
[thirdparty/gcc.git] / gcc / diagnostic.c
1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999-2013 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 #include <new> // For placement new.
37
38 #define pedantic_warning_kind(DC) \
39 ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
40 #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR)
41 #define permissive_error_option(DC) ((DC)->opt_permissive)
42
43 /* Prototypes. */
44 static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
45
46 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
47
48 static void diagnostic_action_after_output (diagnostic_context *,
49 diagnostic_info *);
50 static void real_abort (void) ATTRIBUTE_NORETURN;
51
52 /* Name of program invoked, sans directories. */
53
54 const char *progname;
55
56 /* A diagnostic_context surrogate for stderr. */
57 static diagnostic_context global_diagnostic_context;
58 diagnostic_context *global_dc = &global_diagnostic_context;
59 \f
60 /* Return a malloc'd string containing MSG formatted a la printf. The
61 caller is responsible for freeing the memory. */
62 static char *
63 build_message_string (const char *msg, ...)
64 {
65 char *str;
66 va_list ap;
67
68 va_start (ap, msg);
69 vasprintf (&str, msg, ap);
70 va_end (ap);
71
72 return str;
73 }
74
75 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
76 char *
77 file_name_as_prefix (diagnostic_context *context, const char *f)
78 {
79 const char *locus_cs
80 = colorize_start (pp_show_color (context->printer), "locus");
81 const char *locus_ce = colorize_stop (pp_show_color (context->printer));
82 return build_message_string ("%s%s:%s ", locus_cs, f, locus_ce);
83 }
84
85
86 \f
87 /* Return the value of the getenv("COLUMNS") as an integer. If the
88 value is not set to a positive integer, then return INT_MAX. */
89 static int
90 getenv_columns (void)
91 {
92 const char * s = getenv ("COLUMNS");
93 if (s != NULL) {
94 int n = atoi (s);
95 if (n > 0)
96 return n;
97 }
98 return INT_MAX;
99 }
100
101 /* Set caret_max_width to value. */
102 void
103 diagnostic_set_caret_max_width (diagnostic_context *context, int value)
104 {
105 /* One minus to account for the leading empty space. */
106 value = value ? value - 1
107 : (isatty (fileno (pp_buffer (context->printer)->stream))
108 ? getenv_columns () - 1: INT_MAX);
109
110 if (value <= 0)
111 value = INT_MAX;
112
113 context->caret_max_width = value;
114 }
115
116 /* Initialize the diagnostic message outputting machinery. */
117 void
118 diagnostic_initialize (diagnostic_context *context, int n_opts)
119 {
120 int i;
121
122 /* Allocate a basic pretty-printer. Clients will replace this a
123 much more elaborated pretty-printer if they wish. */
124 context->printer = XNEW (pretty_printer);
125 new (context->printer) pretty_printer ();
126
127 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
128 context->some_warnings_are_errors = false;
129 context->warning_as_error_requested = false;
130 context->n_opts = n_opts;
131 context->classify_diagnostic = XNEWVEC (diagnostic_t, n_opts);
132 for (i = 0; i < n_opts; i++)
133 context->classify_diagnostic[i] = DK_UNSPECIFIED;
134 context->show_caret = false;
135 diagnostic_set_caret_max_width (context, pp_line_cutoff (context->printer));
136 context->show_option_requested = false;
137 context->abort_on_error = false;
138 context->show_column = false;
139 context->pedantic_errors = false;
140 context->permissive = false;
141 context->opt_permissive = 0;
142 context->fatal_errors = false;
143 context->dc_inhibit_warnings = false;
144 context->dc_warn_system_headers = false;
145 context->max_errors = 0;
146 context->internal_error = NULL;
147 diagnostic_starter (context) = default_diagnostic_starter;
148 diagnostic_finalizer (context) = default_diagnostic_finalizer;
149 context->option_enabled = NULL;
150 context->option_state = NULL;
151 context->option_name = NULL;
152 context->last_location = UNKNOWN_LOCATION;
153 context->last_module = 0;
154 context->x_data = NULL;
155 context->lock = 0;
156 context->inhibit_notes_p = false;
157 }
158
159 /* Do any cleaning up required after the last diagnostic is emitted. */
160
161 void
162 diagnostic_finish (diagnostic_context *context)
163 {
164 /* Some of the errors may actually have been warnings. */
165 if (context->some_warnings_are_errors)
166 {
167 /* -Werror was given. */
168 if (context->warning_as_error_requested)
169 pp_verbatim (context->printer,
170 _("%s: all warnings being treated as errors"),
171 progname);
172 /* At least one -Werror= was given. */
173 else
174 pp_verbatim (context->printer,
175 _("%s: some warnings being treated as errors"),
176 progname);
177 pp_newline_and_flush (context->printer);
178 }
179 }
180
181 /* Initialize DIAGNOSTIC, where the message MSG has already been
182 translated. */
183 void
184 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
185 va_list *args, location_t location,
186 diagnostic_t kind)
187 {
188 diagnostic->message.err_no = errno;
189 diagnostic->message.args_ptr = args;
190 diagnostic->message.format_spec = msg;
191 diagnostic->location = location;
192 diagnostic->override_column = 0;
193 diagnostic->kind = kind;
194 diagnostic->option_index = 0;
195 }
196
197 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
198 translated. */
199 void
200 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
201 va_list *args, location_t location,
202 diagnostic_t kind)
203 {
204 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
205 }
206
207 /* Return a malloc'd string describing a location. The caller is
208 responsible for freeing the memory. */
209 char *
210 diagnostic_build_prefix (diagnostic_context *context,
211 const diagnostic_info *diagnostic)
212 {
213 static const char *const diagnostic_kind_text[] = {
214 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
215 #include "diagnostic.def"
216 #undef DEFINE_DIAGNOSTIC_KIND
217 "must-not-happen"
218 };
219 static const char *const diagnostic_kind_color[] = {
220 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
221 #include "diagnostic.def"
222 #undef DEFINE_DIAGNOSTIC_KIND
223 NULL
224 };
225 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
226 const char *text_cs = "", *text_ce = "";
227 const char *locus_cs, *locus_ce;
228 pretty_printer *pp = context->printer;
229
230 if (diagnostic_kind_color[diagnostic->kind])
231 {
232 text_cs = colorize_start (pp_show_color (pp),
233 diagnostic_kind_color[diagnostic->kind]);
234 text_ce = colorize_stop (pp_show_color (pp));
235 }
236 locus_cs = colorize_start (pp_show_color (pp), "locus");
237 locus_ce = colorize_stop (pp_show_color (pp));
238
239 expanded_location s = expand_location_to_spelling_point (diagnostic->location);
240 if (diagnostic->override_column)
241 s.column = diagnostic->override_column;
242 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
243
244 return
245 (s.file == NULL
246 ? build_message_string ("%s%s:%s %s%s%s", locus_cs, progname, locus_ce,
247 text_cs, text, text_ce)
248 : !strcmp (s.file, N_("<built-in>"))
249 ? build_message_string ("%s%s:%s %s%s%s", locus_cs, s.file, locus_ce,
250 text_cs, text, text_ce)
251 : context->show_column
252 ? build_message_string ("%s%s:%d:%d:%s %s%s%s", locus_cs, s.file, s.line,
253 s.column, locus_ce, text_cs, text, text_ce)
254 : build_message_string ("%s%s:%d:%s %s%s%s", locus_cs, s.file, s.line, locus_ce,
255 text_cs, text, text_ce));
256 }
257
258 /* If LINE is longer than MAX_WIDTH, and COLUMN is not smaller than
259 MAX_WIDTH by some margin, then adjust the start of the line such
260 that the COLUMN is smaller than MAX_WIDTH minus the margin. The
261 margin is either 10 characters or the difference between the column
262 and the length of the line, whatever is smaller. The length of
263 LINE is given by LINE_WIDTH. */
264 static const char *
265 adjust_line (const char *line, int line_width,
266 int max_width, int *column_p)
267 {
268 int right_margin = 10;
269 int column = *column_p;
270
271 right_margin = MIN (line_width - column, right_margin);
272 right_margin = max_width - right_margin;
273 if (line_width >= max_width && column > right_margin)
274 {
275 line += column - right_margin;
276 *column_p = right_margin;
277 }
278 return line;
279 }
280
281 /* Print the physical source line corresponding to the location of
282 this diagnostics, and a caret indicating the precise column. */
283 void
284 diagnostic_show_locus (diagnostic_context * context,
285 const diagnostic_info *diagnostic)
286 {
287 const char *line;
288 int line_width;
289 char *buffer;
290 expanded_location s;
291 int max_width;
292 const char *saved_prefix;
293 const char *caret_cs, *caret_ce;
294
295 if (!context->show_caret
296 || diagnostic->location <= BUILTINS_LOCATION
297 || diagnostic->location == context->last_location)
298 return;
299
300 context->last_location = diagnostic->location;
301 s = expand_location_to_spelling_point (diagnostic->location);
302 line = location_get_source_line (s, &line_width);
303 if (line == NULL)
304 return;
305
306 max_width = context->caret_max_width;
307 line = adjust_line (line, line_width, max_width, &(s.column));
308
309 pp_newline (context->printer);
310 saved_prefix = pp_get_prefix (context->printer);
311 pp_set_prefix (context->printer, NULL);
312 pp_space (context->printer);
313 while (max_width > 0 && line_width > 0)
314 {
315 char c = *line == '\t' ? ' ' : *line;
316 if (c == '\0')
317 c = ' ';
318 pp_character (context->printer, c);
319 max_width--;
320 line_width--;
321 line++;
322 }
323 pp_newline (context->printer);
324 caret_cs = colorize_start (pp_show_color (context->printer), "caret");
325 caret_ce = colorize_stop (pp_show_color (context->printer));
326
327 /* pp_printf does not implement %*c. */
328 size_t len = s.column + 3 + strlen (caret_cs) + strlen (caret_ce);
329 buffer = XALLOCAVEC (char, len);
330 snprintf (buffer, len, "%s %*c%s", caret_cs, s.column, '^', caret_ce);
331 pp_string (context->printer, buffer);
332 pp_set_prefix (context->printer, saved_prefix);
333 }
334
335 /* Functions at which to stop the backtrace print. It's not
336 particularly helpful to print the callers of these functions. */
337
338 static const char * const bt_stop[] =
339 {
340 "main",
341 "toplev_main",
342 "execute_one_pass",
343 "compile_file",
344 };
345
346 /* A callback function passed to the backtrace_full function. */
347
348 static int
349 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
350 const char *function)
351 {
352 int *pcount = (int *) data;
353
354 /* If we don't have any useful information, don't print
355 anything. */
356 if (filename == NULL && function == NULL)
357 return 0;
358
359 /* Skip functions in diagnostic.c. */
360 if (*pcount == 0
361 && filename != NULL
362 && strcmp (lbasename (filename), "diagnostic.c") == 0)
363 return 0;
364
365 /* Print up to 20 functions. We could make this a --param, but
366 since this is only for debugging just use a constant for now. */
367 if (*pcount >= 20)
368 {
369 /* Returning a non-zero value stops the backtrace. */
370 return 1;
371 }
372 ++*pcount;
373
374 char *alc = NULL;
375 if (function != NULL)
376 {
377 char *str = cplus_demangle_v3 (function,
378 (DMGL_VERBOSE | DMGL_ANSI
379 | DMGL_GNU_V3 | DMGL_PARAMS));
380 if (str != NULL)
381 {
382 alc = str;
383 function = str;
384 }
385
386 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
387 {
388 size_t len = strlen (bt_stop[i]);
389 if (strncmp (function, bt_stop[i], len) == 0
390 && (function[len] == '\0' || function[len] == '('))
391 {
392 if (alc != NULL)
393 free (alc);
394 /* Returning a non-zero value stops the backtrace. */
395 return 1;
396 }
397 }
398 }
399
400 fprintf (stderr, "0x%lx %s\n\t%s:%d\n",
401 (unsigned long) pc,
402 function == NULL ? "???" : function,
403 filename == NULL ? "???" : filename,
404 lineno);
405
406 if (alc != NULL)
407 free (alc);
408
409 return 0;
410 }
411
412 /* A callback function passed to the backtrace_full function. This is
413 called if backtrace_full has an error. */
414
415 static void
416 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
417 {
418 if (errnum < 0)
419 {
420 /* This means that no debug info was available. Just quietly
421 skip printing backtrace info. */
422 return;
423 }
424 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
425 errnum == 0 ? "" : xstrerror (errnum));
426 }
427
428 /* Take any action which is expected to happen after the diagnostic
429 is written out. This function does not always return. */
430 static void
431 diagnostic_action_after_output (diagnostic_context *context,
432 diagnostic_info *diagnostic)
433 {
434 switch (diagnostic->kind)
435 {
436 case DK_DEBUG:
437 case DK_NOTE:
438 case DK_ANACHRONISM:
439 case DK_WARNING:
440 break;
441
442 case DK_ERROR:
443 case DK_SORRY:
444 if (context->abort_on_error)
445 real_abort ();
446 if (context->fatal_errors)
447 {
448 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
449 diagnostic_finish (context);
450 exit (FATAL_EXIT_CODE);
451 }
452 if (context->max_errors != 0
453 && ((unsigned) (diagnostic_kind_count (context, DK_ERROR)
454 + diagnostic_kind_count (context, DK_SORRY))
455 >= context->max_errors))
456 {
457 fnotice (stderr,
458 "compilation terminated due to -fmax-errors=%u.\n",
459 context->max_errors);
460 diagnostic_finish (context);
461 exit (FATAL_EXIT_CODE);
462 }
463 break;
464
465 case DK_ICE:
466 {
467 struct backtrace_state *state =
468 backtrace_create_state (NULL, 0, bt_err_callback, NULL);
469 int count = 0;
470 if (state != NULL)
471 backtrace_full (state, 2, bt_callback, bt_err_callback,
472 (void *) &count);
473
474 if (context->abort_on_error)
475 real_abort ();
476
477 fnotice (stderr, "Please submit a full bug report,\n"
478 "with preprocessed source if appropriate.\n");
479 if (count > 0)
480 fnotice (stderr,
481 ("Please include the complete backtrace "
482 "with any bug report.\n"));
483 fnotice (stderr, "See %s for instructions.\n", bug_report_url);
484
485 exit (ICE_EXIT_CODE);
486 }
487
488 case DK_FATAL:
489 if (context->abort_on_error)
490 real_abort ();
491 diagnostic_finish (context);
492 fnotice (stderr, "compilation terminated.\n");
493 exit (FATAL_EXIT_CODE);
494
495 default:
496 gcc_unreachable ();
497 }
498 }
499
500 void
501 diagnostic_report_current_module (diagnostic_context *context, location_t where)
502 {
503 const struct line_map *map = NULL;
504
505 if (pp_needs_newline (context->printer))
506 {
507 pp_newline (context->printer);
508 pp_needs_newline (context->printer) = false;
509 }
510
511 if (where <= BUILTINS_LOCATION)
512 return;
513
514 linemap_resolve_location (line_table, where,
515 LRK_MACRO_DEFINITION_LOCATION,
516 &map);
517
518 if (map && diagnostic_last_module_changed (context, map))
519 {
520 diagnostic_set_last_module (context, map);
521 if (! MAIN_FILE_P (map))
522 {
523 map = INCLUDED_FROM (line_table, map);
524 if (context->show_column)
525 pp_verbatim (context->printer,
526 "In file included from %r%s:%d:%d%R", "locus",
527 LINEMAP_FILE (map),
528 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
529 else
530 pp_verbatim (context->printer,
531 "In file included from %r%s:%d%R", "locus",
532 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
533 while (! MAIN_FILE_P (map))
534 {
535 map = INCLUDED_FROM (line_table, map);
536 pp_verbatim (context->printer,
537 ",\n from %r%s:%d%R", "locus",
538 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
539 }
540 pp_verbatim (context->printer, ":");
541 pp_newline (context->printer);
542 }
543 }
544 }
545
546 void
547 default_diagnostic_starter (diagnostic_context *context,
548 diagnostic_info *diagnostic)
549 {
550 diagnostic_report_current_module (context, diagnostic->location);
551 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
552 diagnostic));
553 }
554
555 void
556 default_diagnostic_finalizer (diagnostic_context *context ATTRIBUTE_UNUSED,
557 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
558 {
559 }
560
561 /* Interface to specify diagnostic kind overrides. Returns the
562 previous setting, or DK_UNSPECIFIED if the parameters are out of
563 range. If OPTION_INDEX is zero, the new setting is for all the
564 diagnostics. */
565 diagnostic_t
566 diagnostic_classify_diagnostic (diagnostic_context *context,
567 int option_index,
568 diagnostic_t new_kind,
569 location_t where)
570 {
571 diagnostic_t old_kind;
572
573 if (option_index < 0
574 || option_index >= context->n_opts
575 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
576 return DK_UNSPECIFIED;
577
578 old_kind = context->classify_diagnostic[option_index];
579
580 /* Handle pragmas separately, since we need to keep track of *where*
581 the pragmas were. */
582 if (where != UNKNOWN_LOCATION)
583 {
584 int i;
585
586 for (i = context->n_classification_history - 1; i >= 0; i --)
587 if (context->classification_history[i].option == option_index)
588 {
589 old_kind = context->classification_history[i].kind;
590 break;
591 }
592
593 i = context->n_classification_history;
594 context->classification_history =
595 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
596 * sizeof (diagnostic_classification_change_t));
597 context->classification_history[i].location = where;
598 context->classification_history[i].option = option_index;
599 context->classification_history[i].kind = new_kind;
600 context->n_classification_history ++;
601 }
602 else
603 context->classify_diagnostic[option_index] = new_kind;
604
605 return old_kind;
606 }
607
608 /* Save all diagnostic classifications in a stack. */
609 void
610 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
611 {
612 context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
613 context->push_list[context->n_push ++] = context->n_classification_history;
614 }
615
616 /* Restore the topmost classification set off the stack. If the stack
617 is empty, revert to the state based on command line parameters. */
618 void
619 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
620 {
621 int jump_to;
622 int i;
623
624 if (context->n_push)
625 jump_to = context->push_list [-- context->n_push];
626 else
627 jump_to = 0;
628
629 i = context->n_classification_history;
630 context->classification_history =
631 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
632 * sizeof (diagnostic_classification_change_t));
633 context->classification_history[i].location = where;
634 context->classification_history[i].option = jump_to;
635 context->classification_history[i].kind = DK_POP;
636 context->n_classification_history ++;
637 }
638
639 /* Report a diagnostic message (an error or a warning) as specified by
640 DC. This function is *the* subroutine in terms of which front-ends
641 should implement their specific diagnostic handling modules. The
642 front-end independent format specifiers are exactly those described
643 in the documentation of output_format.
644 Return true if a diagnostic was printed, false otherwise. */
645
646 bool
647 diagnostic_report_diagnostic (diagnostic_context *context,
648 diagnostic_info *diagnostic)
649 {
650 location_t location = diagnostic->location;
651 diagnostic_t orig_diag_kind = diagnostic->kind;
652 const char *saved_format_spec;
653
654 /* Give preference to being able to inhibit warnings, before they
655 get reclassified to something else. */
656 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
657 && !diagnostic_report_warnings_p (context, location))
658 return false;
659
660 if (diagnostic->kind == DK_PEDWARN)
661 {
662 diagnostic->kind = pedantic_warning_kind (context);
663 /* We do this to avoid giving the message for -pedantic-errors. */
664 orig_diag_kind = diagnostic->kind;
665 }
666
667 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
668 return false;
669
670 if (context->lock > 0)
671 {
672 /* If we're reporting an ICE in the middle of some other error,
673 try to flush out the previous error, then let this one
674 through. Don't do this more than once. */
675 if (diagnostic->kind == DK_ICE && context->lock == 1)
676 pp_newline_and_flush (context->printer);
677 else
678 error_recursion (context);
679 }
680
681 /* If the user requested that warnings be treated as errors, so be
682 it. Note that we do this before the next block so that
683 individual warnings can be overridden back to warnings with
684 -Wno-error=*. */
685 if (context->warning_as_error_requested
686 && diagnostic->kind == DK_WARNING)
687 {
688 diagnostic->kind = DK_ERROR;
689 }
690
691 if (diagnostic->option_index
692 && diagnostic->option_index != permissive_error_option (context))
693 {
694 diagnostic_t diag_class = DK_UNSPECIFIED;
695
696 /* This tests if the user provided the appropriate -Wfoo or
697 -Wno-foo option. */
698 if (! context->option_enabled (diagnostic->option_index,
699 context->option_state))
700 return false;
701
702 /* This tests for #pragma diagnostic changes. */
703 if (context->n_classification_history > 0)
704 {
705 /* FIXME: Stupid search. Optimize later. */
706 for (int i = context->n_classification_history - 1; i >= 0; i --)
707 {
708 if (linemap_location_before_p
709 (line_table,
710 context->classification_history[i].location,
711 location))
712 {
713 if (context->classification_history[i].kind == (int) DK_POP)
714 {
715 i = context->classification_history[i].option;
716 continue;
717 }
718 int option = context->classification_history[i].option;
719 /* The option 0 is for all the diagnostics. */
720 if (option == 0 || option == diagnostic->option_index)
721 {
722 diag_class = context->classification_history[i].kind;
723 if (diag_class != DK_UNSPECIFIED)
724 diagnostic->kind = diag_class;
725 break;
726 }
727 }
728 }
729 }
730 /* This tests if the user provided the appropriate -Werror=foo
731 option. */
732 if (diag_class == DK_UNSPECIFIED
733 && context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
734 {
735 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
736 }
737 /* This allows for future extensions, like temporarily disabling
738 warnings for ranges of source code. */
739 if (diagnostic->kind == DK_IGNORED)
740 return false;
741 }
742
743 if (orig_diag_kind == DK_WARNING && diagnostic->kind == DK_ERROR)
744 context->some_warnings_are_errors = true;
745
746 context->lock++;
747
748 if (diagnostic->kind == DK_ICE)
749 {
750 #ifndef ENABLE_CHECKING
751 /* When not checking, ICEs are converted to fatal errors when an
752 error has already occurred. This is counteracted by
753 abort_on_error. */
754 if ((diagnostic_kind_count (context, DK_ERROR) > 0
755 || diagnostic_kind_count (context, DK_SORRY) > 0)
756 && !context->abort_on_error)
757 {
758 expanded_location s = expand_location (diagnostic->location);
759 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
760 s.file, s.line);
761 exit (ICE_EXIT_CODE);
762 }
763 #endif
764 if (context->internal_error)
765 (*context->internal_error) (context,
766 diagnostic->message.format_spec,
767 diagnostic->message.args_ptr);
768 }
769 if (diagnostic->kind == DK_ERROR && orig_diag_kind == DK_WARNING)
770 ++diagnostic_kind_count (context, DK_WERROR);
771 else
772 ++diagnostic_kind_count (context, diagnostic->kind);
773
774 saved_format_spec = diagnostic->message.format_spec;
775 if (context->show_option_requested)
776 {
777 char *option_text;
778
779 option_text = context->option_name (context, diagnostic->option_index,
780 orig_diag_kind, diagnostic->kind);
781
782 if (option_text)
783 {
784 diagnostic->message.format_spec
785 = ACONCAT ((diagnostic->message.format_spec,
786 " ",
787 "[", option_text, "]",
788 NULL));
789 free (option_text);
790 }
791 }
792 diagnostic->message.locus = &diagnostic->location;
793 diagnostic->message.x_data = &diagnostic->x_data;
794 diagnostic->x_data = NULL;
795 pp_format (context->printer, &diagnostic->message);
796 (*diagnostic_starter (context)) (context, diagnostic);
797 pp_output_formatted_text (context->printer);
798 diagnostic_show_locus (context, diagnostic);
799 (*diagnostic_finalizer (context)) (context, diagnostic);
800 pp_destroy_prefix (context->printer);
801 pp_newline_and_flush (context->printer);
802 diagnostic_action_after_output (context, diagnostic);
803 diagnostic->message.format_spec = saved_format_spec;
804 diagnostic->x_data = NULL;
805
806 context->lock--;
807
808 return true;
809 }
810
811 /* Given a partial pathname as input, return another pathname that
812 shares no directory elements with the pathname of __FILE__. This
813 is used by fancy_abort() to print `Internal compiler error in expr.c'
814 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
815
816 const char *
817 trim_filename (const char *name)
818 {
819 static const char this_file[] = __FILE__;
820 const char *p = name, *q = this_file;
821
822 /* First skip any "../" in each filename. This allows us to give a proper
823 reference to a file in a subdirectory. */
824 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
825 p += 3;
826
827 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
828 q += 3;
829
830 /* Now skip any parts the two filenames have in common. */
831 while (*p == *q && *p != 0 && *q != 0)
832 p++, q++;
833
834 /* Now go backwards until the previous directory separator. */
835 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
836 p--;
837
838 return p;
839 }
840 \f
841 /* Standard error reporting routines in increasing order of severity.
842 All of these take arguments like printf. */
843
844 /* Text to be emitted verbatim to the error message stream; this
845 produces no prefix and disables line-wrapping. Use rarely. */
846 void
847 verbatim (const char *gmsgid, ...)
848 {
849 text_info text;
850 va_list ap;
851
852 va_start (ap, gmsgid);
853 text.err_no = errno;
854 text.args_ptr = &ap;
855 text.format_spec = _(gmsgid);
856 text.locus = NULL;
857 text.x_data = NULL;
858 pp_format_verbatim (global_dc->printer, &text);
859 pp_newline_and_flush (global_dc->printer);
860 va_end (ap);
861 }
862
863 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
864 void
865 diagnostic_append_note (diagnostic_context *context,
866 location_t location,
867 const char * gmsgid, ...)
868 {
869 diagnostic_info diagnostic;
870 va_list ap;
871 const char *saved_prefix;
872
873 va_start (ap, gmsgid);
874 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
875 if (context->inhibit_notes_p)
876 {
877 va_end (ap);
878 return;
879 }
880 saved_prefix = pp_get_prefix (context->printer);
881 pp_set_prefix (context->printer,
882 diagnostic_build_prefix (context, &diagnostic));
883 pp_newline (context->printer);
884 pp_format (context->printer, &diagnostic.message);
885 pp_output_formatted_text (context->printer);
886 pp_destroy_prefix (context->printer);
887 pp_set_prefix (context->printer, saved_prefix);
888 diagnostic_show_locus (context, &diagnostic);
889 va_end (ap);
890 }
891
892 bool
893 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
894 const char *gmsgid, ...)
895 {
896 diagnostic_info diagnostic;
897 va_list ap;
898 bool ret;
899
900 va_start (ap, gmsgid);
901 if (kind == DK_PERMERROR)
902 {
903 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
904 permissive_error_kind (global_dc));
905 diagnostic.option_index = permissive_error_option (global_dc);
906 }
907 else {
908 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, kind);
909 if (kind == DK_WARNING || kind == DK_PEDWARN)
910 diagnostic.option_index = opt;
911 }
912
913 ret = report_diagnostic (&diagnostic);
914 va_end (ap);
915 return ret;
916 }
917
918 /* An informative note at LOCATION. Use this for additional details on an error
919 message. */
920 void
921 inform (location_t location, const char *gmsgid, ...)
922 {
923 diagnostic_info diagnostic;
924 va_list ap;
925
926 va_start (ap, gmsgid);
927 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
928 report_diagnostic (&diagnostic);
929 va_end (ap);
930 }
931
932 /* An informative note at LOCATION. Use this for additional details on an
933 error message. */
934 void
935 inform_n (location_t location, int n, const char *singular_gmsgid,
936 const char *plural_gmsgid, ...)
937 {
938 diagnostic_info diagnostic;
939 va_list ap;
940
941 va_start (ap, plural_gmsgid);
942 diagnostic_set_info_translated (&diagnostic,
943 ngettext (singular_gmsgid, plural_gmsgid, n),
944 &ap, location, DK_NOTE);
945 report_diagnostic (&diagnostic);
946 va_end (ap);
947 }
948
949 /* A warning at INPUT_LOCATION. Use this for code which is correct according
950 to the relevant language specification but is likely to be buggy anyway.
951 Returns true if the warning was printed, false if it was inhibited. */
952 bool
953 warning (int opt, const char *gmsgid, ...)
954 {
955 diagnostic_info diagnostic;
956 va_list ap;
957 bool ret;
958
959 va_start (ap, gmsgid);
960 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
961 diagnostic.option_index = opt;
962
963 ret = report_diagnostic (&diagnostic);
964 va_end (ap);
965 return ret;
966 }
967
968 /* A warning at LOCATION. Use this for code which is correct according to the
969 relevant language specification but is likely to be buggy anyway.
970 Returns true if the warning was printed, false if it was inhibited. */
971
972 bool
973 warning_at (location_t location, int opt, const char *gmsgid, ...)
974 {
975 diagnostic_info diagnostic;
976 va_list ap;
977 bool ret;
978
979 va_start (ap, gmsgid);
980 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_WARNING);
981 diagnostic.option_index = opt;
982 ret = report_diagnostic (&diagnostic);
983 va_end (ap);
984 return ret;
985 }
986
987 /* A "pedantic" warning at LOCATION: issues a warning unless
988 -pedantic-errors was given on the command line, in which case it
989 issues an error. Use this for diagnostics required by the relevant
990 language standard, if you have chosen not to make them errors.
991
992 Note that these diagnostics are issued independent of the setting
993 of the -Wpedantic command-line switch. To get a warning enabled
994 only with that switch, use either "if (pedantic) pedwarn
995 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
996 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
997
998 Returns true if the warning was printed, false if it was inhibited. */
999
1000 bool
1001 pedwarn (location_t location, int opt, const char *gmsgid, ...)
1002 {
1003 diagnostic_info diagnostic;
1004 va_list ap;
1005 bool ret;
1006
1007 va_start (ap, gmsgid);
1008 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_PEDWARN);
1009 diagnostic.option_index = opt;
1010 ret = report_diagnostic (&diagnostic);
1011 va_end (ap);
1012 return ret;
1013 }
1014
1015 /* A "permissive" error at LOCATION: issues an error unless
1016 -fpermissive was given on the command line, in which case it issues
1017 a warning. Use this for things that really should be errors but we
1018 want to support legacy code.
1019
1020 Returns true if the warning was printed, false if it was inhibited. */
1021
1022 bool
1023 permerror (location_t location, const char *gmsgid, ...)
1024 {
1025 diagnostic_info diagnostic;
1026 va_list ap;
1027 bool ret;
1028
1029 va_start (ap, gmsgid);
1030 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
1031 permissive_error_kind (global_dc));
1032 diagnostic.option_index = permissive_error_option (global_dc);
1033 ret = report_diagnostic (&diagnostic);
1034 va_end (ap);
1035 return ret;
1036 }
1037
1038 /* A hard error: the code is definitely ill-formed, and an object file
1039 will not be produced. */
1040 void
1041 error (const char *gmsgid, ...)
1042 {
1043 diagnostic_info diagnostic;
1044 va_list ap;
1045
1046 va_start (ap, gmsgid);
1047 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
1048 report_diagnostic (&diagnostic);
1049 va_end (ap);
1050 }
1051
1052 /* A hard error: the code is definitely ill-formed, and an object file
1053 will not be produced. */
1054 void
1055 error_n (location_t location, int n, const char *singular_gmsgid,
1056 const char *plural_gmsgid, ...)
1057 {
1058 diagnostic_info diagnostic;
1059 va_list ap;
1060
1061 va_start (ap, plural_gmsgid);
1062 diagnostic_set_info_translated (&diagnostic,
1063 ngettext (singular_gmsgid, plural_gmsgid, n),
1064 &ap, location, DK_ERROR);
1065 report_diagnostic (&diagnostic);
1066 va_end (ap);
1067 }
1068
1069 /* Same as ebove, but use location LOC instead of input_location. */
1070 void
1071 error_at (location_t loc, const char *gmsgid, ...)
1072 {
1073 diagnostic_info diagnostic;
1074 va_list ap;
1075
1076 va_start (ap, gmsgid);
1077 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_ERROR);
1078 report_diagnostic (&diagnostic);
1079 va_end (ap);
1080 }
1081
1082 /* "Sorry, not implemented." Use for a language feature which is
1083 required by the relevant specification but not implemented by GCC.
1084 An object file will not be produced. */
1085 void
1086 sorry (const char *gmsgid, ...)
1087 {
1088 diagnostic_info diagnostic;
1089 va_list ap;
1090
1091 va_start (ap, gmsgid);
1092 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
1093 report_diagnostic (&diagnostic);
1094 va_end (ap);
1095 }
1096
1097 /* Return true if an error or a "sorry" has been seen. Various
1098 processing is disabled after errors. */
1099 bool
1100 seen_error (void)
1101 {
1102 return errorcount || sorrycount;
1103 }
1104
1105 /* An error which is severe enough that we make no attempt to
1106 continue. Do not use this for internal consistency checks; that's
1107 internal_error. Use of this function should be rare. */
1108 void
1109 fatal_error (const char *gmsgid, ...)
1110 {
1111 diagnostic_info diagnostic;
1112 va_list ap;
1113
1114 va_start (ap, gmsgid);
1115 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
1116 report_diagnostic (&diagnostic);
1117 va_end (ap);
1118
1119 gcc_unreachable ();
1120 }
1121
1122 /* An internal consistency check has failed. We make no attempt to
1123 continue. Note that unless there is debugging value to be had from
1124 a more specific message, or some other good reason, you should use
1125 abort () instead of calling this function directly. */
1126 void
1127 internal_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_ICE);
1134 report_diagnostic (&diagnostic);
1135 va_end (ap);
1136
1137 gcc_unreachable ();
1138 }
1139 \f
1140 /* Special case error functions. Most are implemented in terms of the
1141 above, or should be. */
1142
1143 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1144 runs its second argument through gettext. */
1145 void
1146 fnotice (FILE *file, const char *cmsgid, ...)
1147 {
1148 va_list ap;
1149
1150 va_start (ap, cmsgid);
1151 vfprintf (file, _(cmsgid), ap);
1152 va_end (ap);
1153 }
1154
1155 /* Inform the user that an error occurred while trying to report some
1156 other error. This indicates catastrophic internal inconsistencies,
1157 so give up now. But do try to flush out the previous error.
1158 This mustn't use internal_error, that will cause infinite recursion. */
1159
1160 static void
1161 error_recursion (diagnostic_context *context)
1162 {
1163 diagnostic_info diagnostic;
1164
1165 if (context->lock < 3)
1166 pp_newline_and_flush (context->printer);
1167
1168 fnotice (stderr,
1169 "Internal compiler error: Error reporting routines re-entered.\n");
1170
1171 /* Call diagnostic_action_after_output to get the "please submit a bug
1172 report" message. It only looks at the kind field of diagnostic_info. */
1173 diagnostic.kind = DK_ICE;
1174 diagnostic_action_after_output (context, &diagnostic);
1175
1176 /* Do not use gcc_unreachable here; that goes through internal_error
1177 and therefore would cause infinite recursion. */
1178 real_abort ();
1179 }
1180
1181 /* Report an internal compiler error in a friendly manner. This is
1182 the function that gets called upon use of abort() in the source
1183 code generally, thanks to a special macro. */
1184
1185 void
1186 fancy_abort (const char *file, int line, const char *function)
1187 {
1188 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1189 }
1190
1191 /* Really call the system 'abort'. This has to go right at the end of
1192 this file, so that there are no functions after it that call abort
1193 and get the system abort instead of our macro. */
1194 #undef abort
1195 static void
1196 real_abort (void)
1197 {
1198 abort ();
1199 }