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