1 /* Language-independent diagnostic subroutines that implicitly use global_dc.
2 Copyright (C) 1999-2025 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 parts of the language independent aspect
23 of diagnostic messages that implicitly use global_dc. */
27 #include "coretypes.h"
29 #include "diagnostic.h"
30 #include "diagnostic-format.h"
32 /* A diagnostic_context surrogate for stderr. */
33 static diagnostic_context global_diagnostic_context
;
34 diagnostic_context
*global_dc
= &global_diagnostic_context
;
36 /* Standard error reporting routines in increasing order of severity. */
38 /* Text to be emitted verbatim to the error message stream; this
39 produces no prefix and disables line-wrapping. Use rarely.
40 It is ignored for machine-readable output formats. */
42 verbatim (const char *gmsgid
, ...)
46 va_start (ap
, gmsgid
);
47 text_info
text (_(gmsgid
), &ap
, errno
);
48 global_dc
->report_verbatim (text
);
52 /* Wrapper around diagnostic_context::diagnostic_impl
53 implying global_dc and taking a variable argument list. */
56 emit_diagnostic (diagnostic_t kind
,
58 diagnostic_option_id option_id
,
59 const char *gmsgid
, ...)
61 auto_diagnostic_group d
;
63 va_start (ap
, gmsgid
);
64 rich_location
richloc (line_table
, location
);
65 bool ret
= global_dc
->diagnostic_impl (&richloc
, nullptr, option_id
,
71 /* As above, but for rich_location *. */
74 emit_diagnostic (diagnostic_t kind
,
75 rich_location
*richloc
,
76 diagnostic_option_id option_id
,
77 const char *gmsgid
, ...)
79 auto_diagnostic_group d
;
81 va_start (ap
, gmsgid
);
82 bool ret
= global_dc
->diagnostic_impl (richloc
, nullptr, option_id
,
88 /* As above, but taking a variable argument list. */
91 emit_diagnostic_valist (diagnostic_t kind
,
93 diagnostic_option_id option_id
,
94 const char *gmsgid
, va_list *ap
)
96 rich_location
richloc (line_table
, location
);
97 return global_dc
->diagnostic_impl (&richloc
, nullptr, option_id
,
101 /* As above, but with rich_location and metadata. */
104 emit_diagnostic_valist_meta (diagnostic_t kind
,
105 rich_location
*richloc
,
106 const diagnostic_metadata
*metadata
,
107 diagnostic_option_id option_id
,
108 const char *gmsgid
, va_list *ap
)
110 return global_dc
->diagnostic_impl (richloc
, metadata
, option_id
,
114 /* An informative note at LOCATION. Use this for additional details on an error
117 inform (location_t location
, const char *gmsgid
, ...)
119 auto_diagnostic_group d
;
121 va_start (ap
, gmsgid
);
122 rich_location
richloc (line_table
, location
);
123 global_dc
->diagnostic_impl (&richloc
, nullptr, -1, gmsgid
, &ap
, DK_NOTE
);
127 /* Same as "inform" above, but at RICHLOC. */
129 inform (rich_location
*richloc
, const char *gmsgid
, ...)
131 gcc_assert (richloc
);
133 auto_diagnostic_group d
;
135 va_start (ap
, gmsgid
);
136 global_dc
->diagnostic_impl (richloc
, nullptr, -1, gmsgid
, &ap
, DK_NOTE
);
140 /* An informative note at LOCATION. Use this for additional details on an
143 inform_n (location_t location
, unsigned HOST_WIDE_INT n
,
144 const char *singular_gmsgid
, const char *plural_gmsgid
, ...)
147 va_start (ap
, plural_gmsgid
);
148 auto_diagnostic_group d
;
149 rich_location
richloc (line_table
, location
);
150 global_dc
->diagnostic_n_impl (&richloc
, nullptr, -1, n
,
151 singular_gmsgid
, plural_gmsgid
,
156 /* A warning at INPUT_LOCATION. Use this for code which is correct according
157 to the relevant language specification but is likely to be buggy anyway.
158 Returns true if the warning was printed, false if it was inhibited. */
160 warning (diagnostic_option_id option_id
, const char *gmsgid
, ...)
162 auto_diagnostic_group d
;
164 va_start (ap
, gmsgid
);
165 rich_location
richloc (line_table
, input_location
);
166 bool ret
= global_dc
->diagnostic_impl (&richloc
, nullptr, option_id
,
167 gmsgid
, &ap
, DK_WARNING
);
172 /* A warning at LOCATION. Use this for code which is correct according to the
173 relevant language specification but is likely to be buggy anyway.
174 Returns true if the warning was printed, false if it was inhibited. */
177 warning_at (location_t location
,
178 diagnostic_option_id option_id
,
179 const char *gmsgid
, ...)
181 auto_diagnostic_group d
;
183 va_start (ap
, gmsgid
);
184 rich_location
richloc (line_table
, location
);
185 bool ret
= global_dc
->diagnostic_impl (&richloc
, nullptr, option_id
,
186 gmsgid
, &ap
, DK_WARNING
);
191 /* Same as "warning at" above, but using RICHLOC. */
194 warning_at (rich_location
*richloc
,
195 diagnostic_option_id option_id
,
196 const char *gmsgid
, ...)
198 gcc_assert (richloc
);
200 auto_diagnostic_group d
;
202 va_start (ap
, gmsgid
);
203 bool ret
= global_dc
->diagnostic_impl (richloc
, nullptr, option_id
,
204 gmsgid
, &ap
, DK_WARNING
);
209 /* Same as "warning at" above, but using METADATA. */
212 warning_meta (rich_location
*richloc
,
213 const diagnostic_metadata
&metadata
,
214 diagnostic_option_id option_id
,
215 const char *gmsgid
, ...)
217 gcc_assert (richloc
);
219 auto_diagnostic_group d
;
221 va_start (ap
, gmsgid
);
222 bool ret
= global_dc
->diagnostic_impl (richloc
, &metadata
, option_id
,
223 gmsgid
, &ap
, DK_WARNING
);
228 /* Same as warning_n plural variant below, but using RICHLOC. */
231 warning_n (rich_location
*richloc
,
232 diagnostic_option_id option_id
,
233 unsigned HOST_WIDE_INT n
,
234 const char *singular_gmsgid
, const char *plural_gmsgid
, ...)
236 gcc_assert (richloc
);
238 auto_diagnostic_group d
;
240 va_start (ap
, plural_gmsgid
);
241 bool ret
= global_dc
->diagnostic_n_impl (richloc
, nullptr, option_id
, n
,
242 singular_gmsgid
, plural_gmsgid
,
248 /* A warning at LOCATION. Use this for code which is correct according to the
249 relevant language specification but is likely to be buggy anyway.
250 Returns true if the warning was printed, false if it was inhibited. */
253 warning_n (location_t location
,
254 diagnostic_option_id option_id
,
255 unsigned HOST_WIDE_INT n
,
256 const char *singular_gmsgid
, const char *plural_gmsgid
, ...)
258 auto_diagnostic_group d
;
260 va_start (ap
, plural_gmsgid
);
261 rich_location
richloc (line_table
, location
);
262 bool ret
= global_dc
->diagnostic_n_impl (&richloc
, nullptr, option_id
, n
,
263 singular_gmsgid
, plural_gmsgid
,
269 /* A "pedantic" warning at LOCATION: issues a warning unless
270 -pedantic-errors was given on the command line, in which case it
271 issues an error. Use this for diagnostics required by the relevant
272 language standard, if you have chosen not to make them errors.
274 Note that these diagnostics are issued independent of the setting
275 of the -Wpedantic command-line switch. To get a warning enabled
276 only with that switch, use either "if (pedantic) pedwarn
277 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
278 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
280 Returns true if the warning was printed, false if it was inhibited. */
283 pedwarn (location_t location
,
284 diagnostic_option_id option_id
,
285 const char *gmsgid
, ...)
287 auto_diagnostic_group d
;
289 va_start (ap
, gmsgid
);
290 rich_location
richloc (line_table
, location
);
291 bool ret
= global_dc
->diagnostic_impl (&richloc
, nullptr, option_id
,
292 gmsgid
, &ap
, DK_PEDWARN
);
297 /* Same as pedwarn above, but using RICHLOC. */
300 pedwarn (rich_location
*richloc
,
301 diagnostic_option_id option_id
,
302 const char *gmsgid
, ...)
304 gcc_assert (richloc
);
306 auto_diagnostic_group d
;
308 va_start (ap
, gmsgid
);
309 bool ret
= global_dc
->diagnostic_impl (richloc
, nullptr, option_id
,
310 gmsgid
, &ap
, DK_PEDWARN
);
315 /* A "permissive" error at LOCATION: issues an error unless
316 -fpermissive was given on the command line, in which case it issues
317 a warning. Use this for things that really should be errors but we
318 want to support legacy code.
320 Returns true if the warning was printed, false if it was inhibited. */
323 permerror (location_t location
, const char *gmsgid
, ...)
325 auto_diagnostic_group d
;
327 va_start (ap
, gmsgid
);
328 rich_location
richloc (line_table
, location
);
329 bool ret
= global_dc
->diagnostic_impl (&richloc
, nullptr, -1, gmsgid
, &ap
,
335 /* Same as "permerror" above, but at RICHLOC. */
338 permerror (rich_location
*richloc
, const char *gmsgid
, ...)
340 gcc_assert (richloc
);
342 auto_diagnostic_group d
;
344 va_start (ap
, gmsgid
);
345 bool ret
= global_dc
->diagnostic_impl (richloc
, nullptr, -1, gmsgid
, &ap
,
351 /* Similar to the above, but controlled by a flag other than -fpermissive.
352 As above, an error by default or a warning with -fpermissive, but this
353 diagnostic can also be downgraded by -Wno-error=opt. */
356 permerror_opt (location_t location
,
357 diagnostic_option_id option_id
,
358 const char *gmsgid
, ...)
360 auto_diagnostic_group d
;
362 va_start (ap
, gmsgid
);
363 rich_location
richloc (line_table
, location
);
364 bool ret
= global_dc
->diagnostic_impl (&richloc
, nullptr, option_id
,
365 gmsgid
, &ap
, DK_PERMERROR
);
370 /* Same as "permerror" above, but at RICHLOC. */
373 permerror_opt (rich_location
*richloc
,
374 diagnostic_option_id option_id
,
375 const char *gmsgid
, ...)
377 gcc_assert (richloc
);
379 auto_diagnostic_group d
;
381 va_start (ap
, gmsgid
);
382 bool ret
= global_dc
->diagnostic_impl (richloc
, nullptr, option_id
,
383 gmsgid
, &ap
, DK_PERMERROR
);
388 /* A hard error: the code is definitely ill-formed, and an object file
389 will not be produced. */
391 error (const char *gmsgid
, ...)
393 auto_diagnostic_group d
;
395 va_start (ap
, gmsgid
);
396 rich_location
richloc (line_table
, input_location
);
397 global_dc
->diagnostic_impl (&richloc
, nullptr, -1, gmsgid
, &ap
, DK_ERROR
);
401 /* A hard error: the code is definitely ill-formed, and an object file
402 will not be produced. */
404 error_n (location_t location
, unsigned HOST_WIDE_INT n
,
405 const char *singular_gmsgid
, const char *plural_gmsgid
, ...)
407 auto_diagnostic_group d
;
409 va_start (ap
, plural_gmsgid
);
410 rich_location
richloc (line_table
, location
);
411 global_dc
->diagnostic_n_impl (&richloc
, nullptr, -1, n
,
412 singular_gmsgid
, plural_gmsgid
,
417 /* Same as above, but use location LOC instead of input_location. */
419 error_at (location_t loc
, const char *gmsgid
, ...)
421 auto_diagnostic_group d
;
423 va_start (ap
, gmsgid
);
424 rich_location
richloc (line_table
, loc
);
425 global_dc
->diagnostic_impl (&richloc
, nullptr, -1, gmsgid
, &ap
, DK_ERROR
);
429 /* Same as above, but use RICH_LOC. */
432 error_at (rich_location
*richloc
, const char *gmsgid
, ...)
434 gcc_assert (richloc
);
436 auto_diagnostic_group d
;
438 va_start (ap
, gmsgid
);
439 global_dc
->diagnostic_impl (richloc
, nullptr, -1, gmsgid
, &ap
, DK_ERROR
);
443 /* Same as above, but with metadata. */
446 error_meta (rich_location
*richloc
, const diagnostic_metadata
&metadata
,
447 const char *gmsgid
, ...)
449 gcc_assert (richloc
);
451 auto_diagnostic_group d
;
453 va_start (ap
, gmsgid
);
454 global_dc
->diagnostic_impl (richloc
, &metadata
, -1, gmsgid
, &ap
, DK_ERROR
);
458 /* "Sorry, not implemented." Use for a language feature which is
459 required by the relevant specification but not implemented by GCC.
460 An object file will not be produced. */
462 sorry (const char *gmsgid
, ...)
464 auto_diagnostic_group d
;
466 va_start (ap
, gmsgid
);
467 rich_location
richloc (line_table
, input_location
);
468 global_dc
->diagnostic_impl (&richloc
, nullptr, -1, gmsgid
, &ap
, DK_SORRY
);
472 /* Same as above, but use location LOC instead of input_location. */
474 sorry_at (location_t loc
, const char *gmsgid
, ...)
476 auto_diagnostic_group d
;
478 va_start (ap
, gmsgid
);
479 rich_location
richloc (line_table
, loc
);
480 global_dc
->diagnostic_impl (&richloc
, nullptr, -1, gmsgid
, &ap
, DK_SORRY
);
484 /* Return true if an error or a "sorry" has been seen on global_dc. Various
485 processing is disabled after errors. */
489 return errorcount
|| sorrycount
;
492 /* An error which is severe enough that we make no attempt to
493 continue. Do not use this for internal consistency checks; that's
494 internal_error. Use of this function should be rare. */
496 fatal_error (location_t loc
, const char *gmsgid
, ...)
498 auto_diagnostic_group d
;
500 va_start (ap
, gmsgid
);
501 rich_location
richloc (line_table
, loc
);
502 global_dc
->diagnostic_impl (&richloc
, nullptr, -1, gmsgid
, &ap
, DK_FATAL
);
508 /* An internal consistency check has failed. We make no attempt to
511 internal_error (const char *gmsgid
, ...)
513 auto_diagnostic_group d
;
515 va_start (ap
, gmsgid
);
516 rich_location
richloc (line_table
, input_location
);
517 global_dc
->diagnostic_impl (&richloc
, nullptr, -1, gmsgid
, &ap
, DK_ICE
);
523 /* Like internal_error, but no backtrace will be printed. Used when
524 the internal error does not happen at the current location, but happened
527 internal_error_no_backtrace (const char *gmsgid
, ...)
529 auto_diagnostic_group d
;
531 va_start (ap
, gmsgid
);
532 rich_location
richloc (line_table
, input_location
);
533 global_dc
->diagnostic_impl (&richloc
, nullptr, -1, gmsgid
, &ap
, DK_ICE_NOBT
);
540 /* Special case error functions. Most are implemented in terms of the
541 above, or should be. */
543 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
544 runs its second argument through gettext. */
546 fnotice (FILE *file
, const char *cmsgid
, ...)
548 /* If the user requested one of the machine-readable diagnostic output
549 formats on stderr (e.g. -fdiagnostics-format=sarif-stderr), then
550 emitting free-form text on stderr will lead to corrupt output.
551 Skip the message for such cases. */
552 if (file
== stderr
&& global_dc
)
553 if (!global_dc
->supports_fnotice_on_stderr_p ())
558 va_start (ap
, cmsgid
);
559 vfprintf (file
, _(cmsgid
), ap
);
563 /* class auto_diagnostic_group. */
565 /* Constructor: "push" this group into global_dc. */
567 auto_diagnostic_group::auto_diagnostic_group ()
569 global_dc
->begin_group ();
572 /* Destructor: "pop" this group from global_dc. */
574 auto_diagnostic_group::~auto_diagnostic_group ()
576 global_dc
->end_group ();
579 /* class auto_diagnostic_nesting_level. */
581 auto_diagnostic_nesting_level::auto_diagnostic_nesting_level ()
583 global_dc
->push_nesting_level ();
586 auto_diagnostic_nesting_level::~auto_diagnostic_nesting_level ()
588 global_dc
->pop_nesting_level ();