1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999-2016 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
5 This file is part of GCC.
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
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
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/>. */
22 /* This file implements the language independent aspect of diagnostic
27 #include "coretypes.h"
31 #include "backtrace.h"
32 #include "diagnostic.h"
33 #include "diagnostic-color.h"
39 #ifdef GWINSZ_IN_SYS_IOCTL
40 # include <sys/ioctl.h>
43 #include <new> // For placement new.
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)
51 static void error_recursion (diagnostic_context
*) ATTRIBUTE_NORETURN
;
53 static void real_abort (void) ATTRIBUTE_NORETURN
;
55 /* Name of program invoked, sans directories. */
59 /* A diagnostic_context surrogate for stderr. */
60 static diagnostic_context global_diagnostic_context
;
61 diagnostic_context
*global_dc
= &global_diagnostic_context
;
63 /* Return a malloc'd string containing MSG formatted a la printf. The
64 caller is responsible for freeing the memory. */
66 build_message_string (const char *msg
, ...)
72 str
= xvasprintf (msg
, ap
);
78 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
80 file_name_as_prefix (diagnostic_context
*context
, const char *f
)
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
);
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. */
94 get_terminal_width (void)
96 const char * s
= getenv ("COLUMNS");
106 if (ioctl (0, TIOCGWINSZ
, &w
) == 0 && w
.ws_col
> 0)
113 /* Set caret_max_width to value. */
115 diagnostic_set_caret_max_width (diagnostic_context
*context
, int value
)
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
);
125 context
->caret_max_width
= value
;
128 /* Initialize the diagnostic message outputting machinery. */
130 diagnostic_initialize (diagnostic_context
*context
, int n_opts
)
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 ();
139 memset (context
->diagnostic_count
, 0, sizeof context
->diagnostic_count
);
140 context
->warning_as_error_requested
= false;
141 context
->n_opts
= n_opts
;
142 context
->classify_diagnostic
= XNEWVEC (diagnostic_t
, n_opts
);
143 for (i
= 0; i
< n_opts
; i
++)
144 context
->classify_diagnostic
[i
] = DK_UNSPECIFIED
;
145 context
->show_caret
= false;
146 diagnostic_set_caret_max_width (context
, pp_line_cutoff (context
->printer
));
147 for (i
= 0; i
< rich_location::MAX_RANGES
; i
++)
148 context
->caret_chars
[i
] = '^';
149 context
->show_option_requested
= false;
150 context
->abort_on_error
= false;
151 context
->show_column
= false;
152 context
->pedantic_errors
= false;
153 context
->permissive
= false;
154 context
->opt_permissive
= 0;
155 context
->fatal_errors
= false;
156 context
->dc_inhibit_warnings
= false;
157 context
->dc_warn_system_headers
= false;
158 context
->max_errors
= 0;
159 context
->internal_error
= NULL
;
160 diagnostic_starter (context
) = default_diagnostic_starter
;
161 context
->start_span
= default_diagnostic_start_span_fn
;
162 diagnostic_finalizer (context
) = default_diagnostic_finalizer
;
163 context
->option_enabled
= NULL
;
164 context
->option_state
= NULL
;
165 context
->option_name
= NULL
;
166 context
->last_location
= UNKNOWN_LOCATION
;
167 context
->last_module
= 0;
168 context
->x_data
= NULL
;
170 context
->inhibit_notes_p
= false;
173 /* Maybe initialize the color support. We require clients to do this
174 explicitly, since most clients don't want color. When called
175 without a VALUE, it initializes with DIAGNOSTICS_COLOR_DEFAULT. */
178 diagnostic_color_init (diagnostic_context
*context
, int value
/*= -1 */)
180 /* value == -1 is the default value. */
183 /* If DIAGNOSTICS_COLOR_DEFAULT is -1, default to
184 -fdiagnostics-color=auto if GCC_COLORS is in the environment,
185 otherwise default to -fdiagnostics-color=never, for other
186 values default to that
187 -fdiagnostics-color={never,auto,always}. */
188 if (DIAGNOSTICS_COLOR_DEFAULT
== -1)
190 if (!getenv ("GCC_COLORS"))
192 value
= DIAGNOSTICS_COLOR_AUTO
;
195 value
= DIAGNOSTICS_COLOR_DEFAULT
;
197 pp_show_color (context
->printer
)
198 = colorize_init ((diagnostic_color_rule_t
) value
);
201 /* Do any cleaning up required after the last diagnostic is emitted. */
204 diagnostic_finish (diagnostic_context
*context
)
206 /* Some of the errors may actually have been warnings. */
207 if (diagnostic_kind_count (context
, DK_WERROR
))
209 /* -Werror was given. */
210 if (context
->warning_as_error_requested
)
211 pp_verbatim (context
->printer
,
212 _("%s: all warnings being treated as errors"),
214 /* At least one -Werror= was given. */
216 pp_verbatim (context
->printer
,
217 _("%s: some warnings being treated as errors"),
219 pp_newline_and_flush (context
->printer
);
222 diagnostic_file_cache_fini ();
224 XDELETEVEC (context
->classify_diagnostic
);
225 context
->classify_diagnostic
= NULL
;
227 /* diagnostic_initialize allocates context->printer using XNEW
228 and placement-new. */
229 context
->printer
->~pretty_printer ();
230 XDELETE (context
->printer
);
231 context
->printer
= NULL
;
234 /* Initialize DIAGNOSTIC, where the message MSG has already been
237 diagnostic_set_info_translated (diagnostic_info
*diagnostic
, const char *msg
,
238 va_list *args
, rich_location
*richloc
,
241 gcc_assert (richloc
);
242 diagnostic
->message
.err_no
= errno
;
243 diagnostic
->message
.args_ptr
= args
;
244 diagnostic
->message
.format_spec
= msg
;
245 diagnostic
->message
.m_richloc
= richloc
;
246 diagnostic
->richloc
= richloc
;
247 diagnostic
->kind
= kind
;
248 diagnostic
->option_index
= 0;
251 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
254 diagnostic_set_info (diagnostic_info
*diagnostic
, const char *gmsgid
,
255 va_list *args
, rich_location
*richloc
,
258 gcc_assert (richloc
);
259 diagnostic_set_info_translated (diagnostic
, _(gmsgid
), args
, richloc
, kind
);
262 static const char *const diagnostic_kind_color
[] = {
263 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
264 #include "diagnostic.def"
265 #undef DEFINE_DIAGNOSTIC_KIND
269 /* Get a color name for diagnostics of type KIND
270 Result could be NULL. */
273 diagnostic_get_color_for_kind (diagnostic_t kind
)
275 return diagnostic_kind_color
[kind
];
278 /* Return a malloc'd string describing a location e.g. "foo.c:42:10".
279 The caller is responsible for freeing the memory. */
282 diagnostic_get_location_text (diagnostic_context
*context
,
285 pretty_printer
*pp
= context
->printer
;
286 const char *locus_cs
= colorize_start (pp_show_color (pp
), "locus");
287 const char *locus_ce
= colorize_stop (pp_show_color (pp
));
290 return build_message_string ("%s%s:%s", locus_cs
, progname
, locus_ce
);
292 if (!strcmp (s
.file
, N_("<built-in>")))
293 return build_message_string ("%s%s:%s", locus_cs
, s
.file
, locus_ce
);
295 if (context
->show_column
)
296 return build_message_string ("%s%s:%d:%d:%s", locus_cs
, s
.file
, s
.line
,
299 return build_message_string ("%s%s:%d:%s", locus_cs
, s
.file
, s
.line
,
303 /* Return a malloc'd string describing a location and the severity of the
304 diagnostic, e.g. "foo.c:42:10: error: ". The caller is responsible for
305 freeing the memory. */
307 diagnostic_build_prefix (diagnostic_context
*context
,
308 const diagnostic_info
*diagnostic
)
310 static const char *const diagnostic_kind_text
[] = {
311 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
312 #include "diagnostic.def"
313 #undef DEFINE_DIAGNOSTIC_KIND
316 gcc_assert (diagnostic
->kind
< DK_LAST_DIAGNOSTIC_KIND
);
318 const char *text
= _(diagnostic_kind_text
[diagnostic
->kind
]);
319 const char *text_cs
= "", *text_ce
= "";
320 pretty_printer
*pp
= context
->printer
;
322 if (diagnostic_kind_color
[diagnostic
->kind
])
324 text_cs
= colorize_start (pp_show_color (pp
),
325 diagnostic_kind_color
[diagnostic
->kind
]);
326 text_ce
= colorize_stop (pp_show_color (pp
));
329 expanded_location s
= diagnostic_expand_location (diagnostic
);
330 char *location_text
= diagnostic_get_location_text (context
, s
);
332 char *result
= build_message_string ("%s %s%s%s", location_text
,
333 text_cs
, text
, text_ce
);
334 free (location_text
);
338 /* Functions at which to stop the backtrace print. It's not
339 particularly helpful to print the callers of these functions. */
341 static const char * const bt_stop
[] =
349 /* A callback function passed to the backtrace_full function. */
352 bt_callback (void *data
, uintptr_t pc
, const char *filename
, int lineno
,
353 const char *function
)
355 int *pcount
= (int *) data
;
357 /* If we don't have any useful information, don't print
359 if (filename
== NULL
&& function
== NULL
)
362 /* Skip functions in diagnostic.c. */
365 && strcmp (lbasename (filename
), "diagnostic.c") == 0)
368 /* Print up to 20 functions. We could make this a --param, but
369 since this is only for debugging just use a constant for now. */
372 /* Returning a non-zero value stops the backtrace. */
378 if (function
!= NULL
)
380 char *str
= cplus_demangle_v3 (function
,
381 (DMGL_VERBOSE
| DMGL_ANSI
382 | DMGL_GNU_V3
| DMGL_PARAMS
));
389 for (size_t i
= 0; i
< ARRAY_SIZE (bt_stop
); ++i
)
391 size_t len
= strlen (bt_stop
[i
]);
392 if (strncmp (function
, bt_stop
[i
], len
) == 0
393 && (function
[len
] == '\0' || function
[len
] == '('))
397 /* Returning a non-zero value stops the backtrace. */
403 fprintf (stderr
, "0x%lx %s\n\t%s:%d\n",
405 function
== NULL
? "???" : function
,
406 filename
== NULL
? "???" : filename
,
415 /* A callback function passed to the backtrace_full function. This is
416 called if backtrace_full has an error. */
419 bt_err_callback (void *data ATTRIBUTE_UNUSED
, const char *msg
, int errnum
)
423 /* This means that no debug info was available. Just quietly
424 skip printing backtrace info. */
427 fprintf (stderr
, "%s%s%s\n", msg
, errnum
== 0 ? "" : ": ",
428 errnum
== 0 ? "" : xstrerror (errnum
));
431 /* Take any action which is expected to happen after the diagnostic
432 is written out. This function does not always return. */
434 diagnostic_action_after_output (diagnostic_context
*context
,
435 diagnostic_t diag_kind
)
447 if (context
->abort_on_error
)
449 if (context
->fatal_errors
)
451 fnotice (stderr
, "compilation terminated due to -Wfatal-errors.\n");
452 diagnostic_finish (context
);
453 exit (FATAL_EXIT_CODE
);
455 if (context
->max_errors
!= 0
456 && ((unsigned) (diagnostic_kind_count (context
, DK_ERROR
)
457 + diagnostic_kind_count (context
, DK_SORRY
)
458 + diagnostic_kind_count (context
, DK_WERROR
))
459 >= context
->max_errors
))
462 "compilation terminated due to -fmax-errors=%u.\n",
463 context
->max_errors
);
464 diagnostic_finish (context
);
465 exit (FATAL_EXIT_CODE
);
472 struct backtrace_state
*state
= NULL
;
473 if (diag_kind
== DK_ICE
)
474 state
= backtrace_create_state (NULL
, 0, bt_err_callback
, NULL
);
477 backtrace_full (state
, 2, bt_callback
, bt_err_callback
,
480 if (context
->abort_on_error
)
483 fnotice (stderr
, "Please submit a full bug report,\n"
484 "with preprocessed source if appropriate.\n");
487 ("Please include the complete backtrace "
488 "with any bug report.\n"));
489 fnotice (stderr
, "See %s for instructions.\n", bug_report_url
);
491 exit (ICE_EXIT_CODE
);
495 if (context
->abort_on_error
)
497 diagnostic_finish (context
);
498 fnotice (stderr
, "compilation terminated.\n");
499 exit (FATAL_EXIT_CODE
);
507 diagnostic_report_current_module (diagnostic_context
*context
, location_t where
)
509 const line_map_ordinary
*map
= NULL
;
511 if (pp_needs_newline (context
->printer
))
513 pp_newline (context
->printer
);
514 pp_needs_newline (context
->printer
) = false;
517 if (where
<= BUILTINS_LOCATION
)
520 linemap_resolve_location (line_table
, where
,
521 LRK_MACRO_DEFINITION_LOCATION
,
524 if (map
&& diagnostic_last_module_changed (context
, map
))
526 diagnostic_set_last_module (context
, map
);
527 if (! MAIN_FILE_P (map
))
529 map
= INCLUDED_FROM (line_table
, map
);
530 if (context
->show_column
)
531 pp_verbatim (context
->printer
,
532 "In file included from %r%s:%d:%d%R", "locus",
534 LAST_SOURCE_LINE (map
), LAST_SOURCE_COLUMN (map
));
536 pp_verbatim (context
->printer
,
537 "In file included from %r%s:%d%R", "locus",
538 LINEMAP_FILE (map
), LAST_SOURCE_LINE (map
));
539 while (! MAIN_FILE_P (map
))
541 map
= INCLUDED_FROM (line_table
, map
);
542 pp_verbatim (context
->printer
,
543 ",\n from %r%s:%d%R", "locus",
544 LINEMAP_FILE (map
), LAST_SOURCE_LINE (map
));
546 pp_verbatim (context
->printer
, ":");
547 pp_newline (context
->printer
);
553 default_diagnostic_starter (diagnostic_context
*context
,
554 diagnostic_info
*diagnostic
)
556 diagnostic_report_current_module (context
, diagnostic_location (diagnostic
));
557 pp_set_prefix (context
->printer
, diagnostic_build_prefix (context
,
562 default_diagnostic_start_span_fn (diagnostic_context
*context
,
563 expanded_location exploc
)
565 pp_set_prefix (context
->printer
,
566 diagnostic_get_location_text (context
, exploc
));
567 pp_string (context
->printer
, "");
568 pp_newline (context
->printer
);
572 default_diagnostic_finalizer (diagnostic_context
*context
,
573 diagnostic_info
*diagnostic
)
575 diagnostic_show_locus (context
, diagnostic
);
576 pp_destroy_prefix (context
->printer
);
577 pp_flush (context
->printer
);
580 /* Interface to specify diagnostic kind overrides. Returns the
581 previous setting, or DK_UNSPECIFIED if the parameters are out of
582 range. If OPTION_INDEX is zero, the new setting is for all the
585 diagnostic_classify_diagnostic (diagnostic_context
*context
,
587 diagnostic_t new_kind
,
590 diagnostic_t old_kind
;
593 || option_index
>= context
->n_opts
594 || new_kind
>= DK_LAST_DIAGNOSTIC_KIND
)
595 return DK_UNSPECIFIED
;
597 old_kind
= context
->classify_diagnostic
[option_index
];
599 /* Handle pragmas separately, since we need to keep track of *where*
601 if (where
!= UNKNOWN_LOCATION
)
605 /* Record the command-line status, so we can reset it back on DK_POP. */
606 if (old_kind
== DK_UNSPECIFIED
)
608 old_kind
= !context
->option_enabled (option_index
,
609 context
->option_state
)
610 ? DK_IGNORED
: (context
->warning_as_error_requested
611 ? DK_ERROR
: DK_WARNING
);
612 context
->classify_diagnostic
[option_index
] = old_kind
;
615 for (i
= context
->n_classification_history
- 1; i
>= 0; i
--)
616 if (context
->classification_history
[i
].option
== option_index
)
618 old_kind
= context
->classification_history
[i
].kind
;
622 i
= context
->n_classification_history
;
623 context
->classification_history
=
624 (diagnostic_classification_change_t
*) xrealloc (context
->classification_history
, (i
+ 1)
625 * sizeof (diagnostic_classification_change_t
));
626 context
->classification_history
[i
].location
= where
;
627 context
->classification_history
[i
].option
= option_index
;
628 context
->classification_history
[i
].kind
= new_kind
;
629 context
->n_classification_history
++;
632 context
->classify_diagnostic
[option_index
] = new_kind
;
637 /* Save all diagnostic classifications in a stack. */
639 diagnostic_push_diagnostics (diagnostic_context
*context
, location_t where ATTRIBUTE_UNUSED
)
641 context
->push_list
= (int *) xrealloc (context
->push_list
, (context
->n_push
+ 1) * sizeof (int));
642 context
->push_list
[context
->n_push
++] = context
->n_classification_history
;
645 /* Restore the topmost classification set off the stack. If the stack
646 is empty, revert to the state based on command line parameters. */
648 diagnostic_pop_diagnostics (diagnostic_context
*context
, location_t where
)
654 jump_to
= context
->push_list
[-- context
->n_push
];
658 i
= context
->n_classification_history
;
659 context
->classification_history
=
660 (diagnostic_classification_change_t
*) xrealloc (context
->classification_history
, (i
+ 1)
661 * sizeof (diagnostic_classification_change_t
));
662 context
->classification_history
[i
].location
= where
;
663 context
->classification_history
[i
].option
= jump_to
;
664 context
->classification_history
[i
].kind
= DK_POP
;
665 context
->n_classification_history
++;
668 /* Report a diagnostic message (an error or a warning) as specified by
669 DC. This function is *the* subroutine in terms of which front-ends
670 should implement their specific diagnostic handling modules. The
671 front-end independent format specifiers are exactly those described
672 in the documentation of output_format.
673 Return true if a diagnostic was printed, false otherwise. */
676 diagnostic_report_diagnostic (diagnostic_context
*context
,
677 diagnostic_info
*diagnostic
)
679 location_t location
= diagnostic_location (diagnostic
);
680 diagnostic_t orig_diag_kind
= diagnostic
->kind
;
681 const char *saved_format_spec
;
683 /* Give preference to being able to inhibit warnings, before they
684 get reclassified to something else. */
685 if ((diagnostic
->kind
== DK_WARNING
|| diagnostic
->kind
== DK_PEDWARN
)
686 && !diagnostic_report_warnings_p (context
, location
))
689 if (diagnostic
->kind
== DK_PEDWARN
)
691 diagnostic
->kind
= pedantic_warning_kind (context
);
692 /* We do this to avoid giving the message for -pedantic-errors. */
693 orig_diag_kind
= diagnostic
->kind
;
696 if (diagnostic
->kind
== DK_NOTE
&& context
->inhibit_notes_p
)
699 if (context
->lock
> 0)
701 /* If we're reporting an ICE in the middle of some other error,
702 try to flush out the previous error, then let this one
703 through. Don't do this more than once. */
704 if ((diagnostic
->kind
== DK_ICE
|| diagnostic
->kind
== DK_ICE_NOBT
)
705 && context
->lock
== 1)
706 pp_newline_and_flush (context
->printer
);
708 error_recursion (context
);
711 /* If the user requested that warnings be treated as errors, so be
712 it. Note that we do this before the next block so that
713 individual warnings can be overridden back to warnings with
715 if (context
->warning_as_error_requested
716 && diagnostic
->kind
== DK_WARNING
)
718 diagnostic
->kind
= DK_ERROR
;
721 if (diagnostic
->option_index
722 && diagnostic
->option_index
!= permissive_error_option (context
))
724 diagnostic_t diag_class
= DK_UNSPECIFIED
;
726 /* This tests if the user provided the appropriate -Wfoo or
728 if (! context
->option_enabled (diagnostic
->option_index
,
729 context
->option_state
))
732 /* This tests for #pragma diagnostic changes. */
733 if (context
->n_classification_history
> 0)
735 /* FIXME: Stupid search. Optimize later. */
736 for (int i
= context
->n_classification_history
- 1; i
>= 0; i
--)
738 if (linemap_location_before_p
740 context
->classification_history
[i
].location
,
743 if (context
->classification_history
[i
].kind
== (int) DK_POP
)
745 i
= context
->classification_history
[i
].option
;
748 int option
= context
->classification_history
[i
].option
;
749 /* The option 0 is for all the diagnostics. */
750 if (option
== 0 || option
== diagnostic
->option_index
)
752 diag_class
= context
->classification_history
[i
].kind
;
753 if (diag_class
!= DK_UNSPECIFIED
)
754 diagnostic
->kind
= diag_class
;
760 /* This tests if the user provided the appropriate -Werror=foo
762 if (diag_class
== DK_UNSPECIFIED
763 && context
->classify_diagnostic
[diagnostic
->option_index
] != DK_UNSPECIFIED
)
765 diagnostic
->kind
= context
->classify_diagnostic
[diagnostic
->option_index
];
767 /* This allows for future extensions, like temporarily disabling
768 warnings for ranges of source code. */
769 if (diagnostic
->kind
== DK_IGNORED
)
775 if (diagnostic
->kind
== DK_ICE
|| diagnostic
->kind
== DK_ICE_NOBT
)
777 /* When not checking, ICEs are converted to fatal errors when an
778 error has already occurred. This is counteracted by
781 && (diagnostic_kind_count (context
, DK_ERROR
) > 0
782 || diagnostic_kind_count (context
, DK_SORRY
) > 0)
783 && !context
->abort_on_error
)
786 = expand_location (diagnostic_location (diagnostic
));
787 fnotice (stderr
, "%s:%d: confused by earlier errors, bailing out\n",
789 exit (ICE_EXIT_CODE
);
791 if (context
->internal_error
)
792 (*context
->internal_error
) (context
,
793 diagnostic
->message
.format_spec
,
794 diagnostic
->message
.args_ptr
);
796 if (diagnostic
->kind
== DK_ERROR
&& orig_diag_kind
== DK_WARNING
)
797 ++diagnostic_kind_count (context
, DK_WERROR
);
799 ++diagnostic_kind_count (context
, diagnostic
->kind
);
801 saved_format_spec
= diagnostic
->message
.format_spec
;
802 if (context
->show_option_requested
)
806 option_text
= context
->option_name (context
, diagnostic
->option_index
,
807 orig_diag_kind
, diagnostic
->kind
);
812 = colorize_start (pp_show_color (context
->printer
),
813 diagnostic_kind_color
[diagnostic
->kind
]);
814 const char *ce
= colorize_stop (pp_show_color (context
->printer
));
815 diagnostic
->message
.format_spec
816 = ACONCAT ((diagnostic
->message
.format_spec
,
818 "[", cs
, option_text
, ce
, "]",
823 diagnostic
->message
.x_data
= &diagnostic
->x_data
;
824 diagnostic
->x_data
= NULL
;
825 pp_format (context
->printer
, &diagnostic
->message
);
826 (*diagnostic_starter (context
)) (context
, diagnostic
);
827 pp_output_formatted_text (context
->printer
);
828 (*diagnostic_finalizer (context
)) (context
, diagnostic
);
829 diagnostic_action_after_output (context
, diagnostic
->kind
);
830 diagnostic
->message
.format_spec
= saved_format_spec
;
831 diagnostic
->x_data
= NULL
;
838 /* Given a partial pathname as input, return another pathname that
839 shares no directory elements with the pathname of __FILE__. This
840 is used by fancy_abort() to print `Internal compiler error in expr.c'
841 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
844 trim_filename (const char *name
)
846 static const char this_file
[] = __FILE__
;
847 const char *p
= name
, *q
= this_file
;
849 /* First skip any "../" in each filename. This allows us to give a proper
850 reference to a file in a subdirectory. */
851 while (p
[0] == '.' && p
[1] == '.' && IS_DIR_SEPARATOR (p
[2]))
854 while (q
[0] == '.' && q
[1] == '.' && IS_DIR_SEPARATOR (q
[2]))
857 /* Now skip any parts the two filenames have in common. */
858 while (*p
== *q
&& *p
!= 0 && *q
!= 0)
861 /* Now go backwards until the previous directory separator. */
862 while (p
> name
&& !IS_DIR_SEPARATOR (p
[-1]))
868 /* Standard error reporting routines in increasing order of severity.
869 All of these take arguments like printf. */
871 /* Text to be emitted verbatim to the error message stream; this
872 produces no prefix and disables line-wrapping. Use rarely. */
874 verbatim (const char *gmsgid
, ...)
879 va_start (ap
, gmsgid
);
882 text
.format_spec
= _(gmsgid
);
884 pp_format_verbatim (global_dc
->printer
, &text
);
885 pp_newline_and_flush (global_dc
->printer
);
889 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
891 diagnostic_append_note (diagnostic_context
*context
,
893 const char * gmsgid
, ...)
895 diagnostic_info diagnostic
;
897 const char *saved_prefix
;
898 rich_location
richloc (line_table
, location
);
900 va_start (ap
, gmsgid
);
901 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_NOTE
);
902 if (context
->inhibit_notes_p
)
907 saved_prefix
= pp_get_prefix (context
->printer
);
908 pp_set_prefix (context
->printer
,
909 diagnostic_build_prefix (context
, &diagnostic
));
910 pp_format (context
->printer
, &diagnostic
.message
);
911 pp_output_formatted_text (context
->printer
);
912 pp_destroy_prefix (context
->printer
);
913 pp_set_prefix (context
->printer
, saved_prefix
);
914 diagnostic_show_locus (context
, &diagnostic
);
919 emit_diagnostic (diagnostic_t kind
, location_t location
, int opt
,
920 const char *gmsgid
, ...)
922 diagnostic_info diagnostic
;
925 rich_location
richloc (line_table
, location
);
927 va_start (ap
, gmsgid
);
928 if (kind
== DK_PERMERROR
)
930 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
,
931 permissive_error_kind (global_dc
));
932 diagnostic
.option_index
= permissive_error_option (global_dc
);
935 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, kind
);
936 if (kind
== DK_WARNING
|| kind
== DK_PEDWARN
)
937 diagnostic
.option_index
= opt
;
940 ret
= report_diagnostic (&diagnostic
);
945 /* An informative note at LOCATION. Use this for additional details on an error
948 inform (location_t location
, const char *gmsgid
, ...)
950 diagnostic_info diagnostic
;
952 rich_location
richloc (line_table
, location
);
954 va_start (ap
, gmsgid
);
955 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_NOTE
);
956 report_diagnostic (&diagnostic
);
960 /* Same as "inform", but at RICHLOC. */
962 inform_at_rich_loc (rich_location
*richloc
, const char *gmsgid
, ...)
964 diagnostic_info diagnostic
;
967 va_start (ap
, gmsgid
);
968 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, richloc
, DK_NOTE
);
969 report_diagnostic (&diagnostic
);
973 /* An informative note at LOCATION. Use this for additional details on an
976 inform_n (location_t location
, int n
, const char *singular_gmsgid
,
977 const char *plural_gmsgid
, ...)
979 diagnostic_info diagnostic
;
981 rich_location
richloc (line_table
, location
);
983 va_start (ap
, plural_gmsgid
);
984 diagnostic_set_info_translated (&diagnostic
,
985 ngettext (singular_gmsgid
, plural_gmsgid
, n
),
986 &ap
, &richloc
, DK_NOTE
);
987 report_diagnostic (&diagnostic
);
991 /* A warning at INPUT_LOCATION. Use this for code which is correct according
992 to the relevant language specification but is likely to be buggy anyway.
993 Returns true if the warning was printed, false if it was inhibited. */
995 warning (int opt
, const char *gmsgid
, ...)
997 diagnostic_info diagnostic
;
1000 rich_location
richloc (line_table
, input_location
);
1002 va_start (ap
, gmsgid
);
1003 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_WARNING
);
1004 diagnostic
.option_index
= opt
;
1006 ret
= report_diagnostic (&diagnostic
);
1011 /* A warning at LOCATION. Use this for code which is correct according to the
1012 relevant language specification but is likely to be buggy anyway.
1013 Returns true if the warning was printed, false if it was inhibited. */
1016 warning_at (location_t location
, int opt
, const char *gmsgid
, ...)
1018 diagnostic_info diagnostic
;
1021 rich_location
richloc (line_table
, location
);
1023 va_start (ap
, gmsgid
);
1024 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_WARNING
);
1025 diagnostic
.option_index
= opt
;
1026 ret
= report_diagnostic (&diagnostic
);
1031 /* Same as warning at, but using RICHLOC. */
1034 warning_at_rich_loc (rich_location
*richloc
, int opt
, const char *gmsgid
, ...)
1036 diagnostic_info diagnostic
;
1040 va_start (ap
, gmsgid
);
1041 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, richloc
, DK_WARNING
);
1042 diagnostic
.option_index
= opt
;
1043 ret
= report_diagnostic (&diagnostic
);
1048 /* A warning at LOCATION. Use this for code which is correct according to the
1049 relevant language specification but is likely to be buggy anyway.
1050 Returns true if the warning was printed, false if it was inhibited. */
1053 warning_n (location_t location
, int opt
, int n
, const char *singular_gmsgid
,
1054 const char *plural_gmsgid
, ...)
1056 diagnostic_info diagnostic
;
1059 rich_location
richloc (line_table
, location
);
1061 va_start (ap
, plural_gmsgid
);
1062 diagnostic_set_info_translated (&diagnostic
,
1063 ngettext (singular_gmsgid
, plural_gmsgid
, n
),
1064 &ap
, &richloc
, DK_WARNING
1066 diagnostic
.option_index
= opt
;
1067 ret
= report_diagnostic (&diagnostic
);
1072 /* A "pedantic" warning at LOCATION: issues a warning unless
1073 -pedantic-errors was given on the command line, in which case it
1074 issues an error. Use this for diagnostics required by the relevant
1075 language standard, if you have chosen not to make them errors.
1077 Note that these diagnostics are issued independent of the setting
1078 of the -Wpedantic command-line switch. To get a warning enabled
1079 only with that switch, use either "if (pedantic) pedwarn
1080 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
1081 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
1083 Returns true if the warning was printed, false if it was inhibited. */
1086 pedwarn (location_t location
, int opt
, const char *gmsgid
, ...)
1088 diagnostic_info diagnostic
;
1091 rich_location
richloc (line_table
, location
);
1093 va_start (ap
, gmsgid
);
1094 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_PEDWARN
);
1095 diagnostic
.option_index
= opt
;
1096 ret
= report_diagnostic (&diagnostic
);
1101 /* A "permissive" error at LOCATION: issues an error unless
1102 -fpermissive was given on the command line, in which case it issues
1103 a warning. Use this for things that really should be errors but we
1104 want to support legacy code.
1106 Returns true if the warning was printed, false if it was inhibited. */
1109 permerror (location_t location
, const char *gmsgid
, ...)
1111 diagnostic_info diagnostic
;
1114 rich_location
richloc (line_table
, location
);
1116 va_start (ap
, gmsgid
);
1117 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
,
1118 permissive_error_kind (global_dc
));
1119 diagnostic
.option_index
= permissive_error_option (global_dc
);
1120 ret
= report_diagnostic (&diagnostic
);
1125 /* Same as "permerror", but at RICHLOC. */
1128 permerror_at_rich_loc (rich_location
*richloc
, const char *gmsgid
, ...)
1130 diagnostic_info diagnostic
;
1134 va_start (ap
, gmsgid
);
1135 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, richloc
,
1136 permissive_error_kind (global_dc
));
1137 diagnostic
.option_index
= permissive_error_option (global_dc
);
1138 ret
= report_diagnostic (&diagnostic
);
1143 /* A hard error: the code is definitely ill-formed, and an object file
1144 will not be produced. */
1146 error (const char *gmsgid
, ...)
1148 diagnostic_info diagnostic
;
1150 rich_location
richloc (line_table
, input_location
);
1152 va_start (ap
, gmsgid
);
1153 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_ERROR
);
1154 report_diagnostic (&diagnostic
);
1158 /* A hard error: the code is definitely ill-formed, and an object file
1159 will not be produced. */
1161 error_n (location_t location
, int n
, const char *singular_gmsgid
,
1162 const char *plural_gmsgid
, ...)
1164 diagnostic_info diagnostic
;
1166 rich_location
richloc (line_table
, location
);
1168 va_start (ap
, plural_gmsgid
);
1169 diagnostic_set_info_translated (&diagnostic
,
1170 ngettext (singular_gmsgid
, plural_gmsgid
, n
),
1171 &ap
, &richloc
, DK_ERROR
);
1172 report_diagnostic (&diagnostic
);
1176 /* Same as ebove, but use location LOC instead of input_location. */
1178 error_at (location_t loc
, const char *gmsgid
, ...)
1180 diagnostic_info diagnostic
;
1182 rich_location
richloc (line_table
, loc
);
1184 va_start (ap
, gmsgid
);
1185 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_ERROR
);
1186 report_diagnostic (&diagnostic
);
1190 /* Same as above, but use RICH_LOC. */
1193 error_at_rich_loc (rich_location
*rich_loc
, const char *gmsgid
, ...)
1195 diagnostic_info diagnostic
;
1198 va_start (ap
, gmsgid
);
1199 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, rich_loc
,
1201 report_diagnostic (&diagnostic
);
1205 /* "Sorry, not implemented." Use for a language feature which is
1206 required by the relevant specification but not implemented by GCC.
1207 An object file will not be produced. */
1209 sorry (const char *gmsgid
, ...)
1211 diagnostic_info diagnostic
;
1213 rich_location
richloc (line_table
, input_location
);
1215 va_start (ap
, gmsgid
);
1216 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_SORRY
);
1217 report_diagnostic (&diagnostic
);
1221 /* Return true if an error or a "sorry" has been seen. Various
1222 processing is disabled after errors. */
1226 return errorcount
|| sorrycount
;
1229 /* An error which is severe enough that we make no attempt to
1230 continue. Do not use this for internal consistency checks; that's
1231 internal_error. Use of this function should be rare. */
1233 fatal_error (location_t loc
, const char *gmsgid
, ...)
1235 diagnostic_info diagnostic
;
1237 rich_location
richloc (line_table
, loc
);
1239 va_start (ap
, gmsgid
);
1240 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_FATAL
);
1241 report_diagnostic (&diagnostic
);
1247 /* An internal consistency check has failed. We make no attempt to
1248 continue. Note that unless there is debugging value to be had from
1249 a more specific message, or some other good reason, you should use
1250 abort () instead of calling this function directly. */
1252 internal_error (const char *gmsgid
, ...)
1254 diagnostic_info diagnostic
;
1256 rich_location
richloc (line_table
, input_location
);
1258 va_start (ap
, gmsgid
);
1259 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_ICE
);
1260 report_diagnostic (&diagnostic
);
1266 /* Like internal_error, but no backtrace will be printed. Used when
1267 the internal error does not happen at the current location, but happened
1270 internal_error_no_backtrace (const char *gmsgid
, ...)
1272 diagnostic_info diagnostic
;
1274 rich_location
richloc (line_table
, input_location
);
1276 va_start (ap
, gmsgid
);
1277 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_ICE_NOBT
);
1278 report_diagnostic (&diagnostic
);
1284 /* Special case error functions. Most are implemented in terms of the
1285 above, or should be. */
1287 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1288 runs its second argument through gettext. */
1290 fnotice (FILE *file
, const char *cmsgid
, ...)
1294 va_start (ap
, cmsgid
);
1295 vfprintf (file
, _(cmsgid
), ap
);
1299 /* Inform the user that an error occurred while trying to report some
1300 other error. This indicates catastrophic internal inconsistencies,
1301 so give up now. But do try to flush out the previous error.
1302 This mustn't use internal_error, that will cause infinite recursion. */
1305 error_recursion (diagnostic_context
*context
)
1307 if (context
->lock
< 3)
1308 pp_newline_and_flush (context
->printer
);
1311 "Internal compiler error: Error reporting routines re-entered.\n");
1313 /* Call diagnostic_action_after_output to get the "please submit a bug
1315 diagnostic_action_after_output (context
, DK_ICE
);
1317 /* Do not use gcc_unreachable here; that goes through internal_error
1318 and therefore would cause infinite recursion. */
1322 /* Report an internal compiler error in a friendly manner. This is
1323 the function that gets called upon use of abort() in the source
1324 code generally, thanks to a special macro. */
1327 fancy_abort (const char *file
, int line
, const char *function
)
1329 internal_error ("in %s, at %s:%d", function
, trim_filename (file
), line
);
1332 /* Really call the system 'abort'. This has to go right at the end of
1333 this file, so that there are no functions after it that call abort
1334 and get the system abort instead of our macro. */