target-supports.exp (check_effective_target_mips_soft_float): Return true for MIPS16...
[official-gcc.git] / gcc / diagnostic.c
blob6bbfe9a36e7b350030f82aa3aa27249d1c356652
1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
6 This file is part of GCC.
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.
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.
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/>. */
23 /* This file implements the language independent aspect of diagnostic
24 message module. */
26 #include "config.h"
27 #undef FLOAT /* This is for hpux. They should change hpux. */
28 #undef FFS /* Some systems define this in param.h. */
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "version.h"
34 #include "tm_p.h"
35 #include "flags.h"
36 #include "input.h"
37 #include "toplev.h"
38 #include "intl.h"
39 #include "diagnostic.h"
40 #include "langhooks.h"
41 #include "langhooks-def.h"
42 #include "opts.h"
45 /* Prototypes. */
46 static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
48 static void default_diagnostic_starter (diagnostic_context *,
49 diagnostic_info *);
50 static void default_diagnostic_finalizer (diagnostic_context *,
51 diagnostic_info *);
53 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
54 static bool diagnostic_count_diagnostic (diagnostic_context *,
55 diagnostic_info *);
56 static void diagnostic_action_after_output (diagnostic_context *,
57 diagnostic_info *);
58 static void real_abort (void) ATTRIBUTE_NORETURN;
60 /* A diagnostic_context surrogate for stderr. */
61 static diagnostic_context global_diagnostic_context;
62 diagnostic_context *global_dc = &global_diagnostic_context;
65 /* Return a malloc'd string containing MSG formatted a la printf. The
66 caller is responsible for freeing the memory. */
67 static char *
68 build_message_string (const char *msg, ...)
70 char *str;
71 va_list ap;
73 va_start (ap, msg);
74 vasprintf (&str, msg, ap);
75 va_end (ap);
77 return str;
80 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
81 char *
82 file_name_as_prefix (const char *f)
84 return build_message_string ("%s: ", f);
89 /* Initialize the diagnostic message outputting machinery. */
90 void
91 diagnostic_initialize (diagnostic_context *context)
93 /* Allocate a basic pretty-printer. Clients will replace this a
94 much more elaborated pretty-printer if they wish. */
95 context->printer = XNEW (pretty_printer);
96 pp_construct (context->printer, NULL, 0);
97 /* By default, diagnostics are sent to stderr. */
98 context->printer->buffer->stream = stderr;
99 /* By default, we emit prefixes once per message. */
100 context->printer->wrapping.rule = DIAGNOSTICS_SHOW_PREFIX_ONCE;
102 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
103 context->issue_warnings_are_errors_message = true;
104 context->warning_as_error_requested = false;
105 memset (context->classify_diagnostic, DK_UNSPECIFIED,
106 sizeof context->classify_diagnostic);
107 context->show_option_requested = false;
108 context->abort_on_error = false;
109 context->internal_error = NULL;
110 diagnostic_starter (context) = default_diagnostic_starter;
111 diagnostic_finalizer (context) = default_diagnostic_finalizer;
112 context->last_module = 0;
113 context->last_function = NULL;
114 context->lock = 0;
117 /* Initialize DIAGNOSTIC, where the message MSG has already been
118 translated. */
119 void
120 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
121 va_list *args, location_t location,
122 diagnostic_t kind)
124 diagnostic->message.err_no = errno;
125 diagnostic->message.args_ptr = args;
126 diagnostic->message.format_spec = msg;
127 diagnostic->location = location;
128 diagnostic->kind = kind;
129 diagnostic->option_index = 0;
132 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
133 translated. */
134 void
135 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
136 va_list *args, location_t location,
137 diagnostic_t kind)
139 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
142 /* Return a malloc'd string describing a location. The caller is
143 responsible for freeing the memory. */
144 char *
145 diagnostic_build_prefix (diagnostic_info *diagnostic)
147 static const char *const diagnostic_kind_text[] = {
148 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
149 #include "diagnostic.def"
150 #undef DEFINE_DIAGNOSTIC_KIND
151 "must-not-happen"
153 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
154 expanded_location s = expand_location (diagnostic->location);
155 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
157 return
158 (s.file == NULL
159 ? build_message_string ("%s: %s", progname, text)
160 #ifdef USE_MAPPED_LOCATION
161 : flag_show_column && s.column != 0
162 ? build_message_string ("%s:%d:%d: %s", s.file, s.line, s.column, text)
163 #endif
164 : build_message_string ("%s:%d: %s", s.file, s.line, text));
167 /* Count a diagnostic. Return true if the message should be printed. */
168 static bool
169 diagnostic_count_diagnostic (diagnostic_context *context,
170 diagnostic_info *diagnostic)
172 diagnostic_t kind = diagnostic->kind;
173 switch (kind)
175 default:
176 gcc_unreachable ();
178 case DK_ICE:
179 #ifndef ENABLE_CHECKING
180 /* When not checking, ICEs are converted to fatal errors when an
181 error has already occurred. This is counteracted by
182 abort_on_error. */
183 if ((diagnostic_kind_count (context, DK_ERROR) > 0
184 || diagnostic_kind_count (context, DK_SORRY) > 0)
185 && !context->abort_on_error)
187 expanded_location s = expand_location (diagnostic->location);
188 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
189 s.file, s.line);
190 exit (ICE_EXIT_CODE);
192 #endif
193 if (context->internal_error)
194 (*context->internal_error) (diagnostic->message.format_spec,
195 diagnostic->message.args_ptr);
196 /* Fall through. */
198 case DK_FATAL: case DK_SORRY:
199 case DK_ANACHRONISM: case DK_NOTE:
200 ++diagnostic_kind_count (context, kind);
201 break;
203 case DK_WARNING:
204 ++diagnostic_kind_count (context, DK_WARNING);
205 break;
207 case DK_ERROR:
208 ++diagnostic_kind_count (context, DK_ERROR);
209 break;
212 return true;
215 /* Take any action which is expected to happen after the diagnostic
216 is written out. This function does not always return. */
217 static void
218 diagnostic_action_after_output (diagnostic_context *context,
219 diagnostic_info *diagnostic)
221 switch (diagnostic->kind)
223 case DK_DEBUG:
224 case DK_NOTE:
225 case DK_ANACHRONISM:
226 case DK_WARNING:
227 break;
229 case DK_ERROR:
230 case DK_SORRY:
231 if (context->abort_on_error)
232 real_abort ();
233 if (flag_fatal_errors)
235 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
236 exit (FATAL_EXIT_CODE);
238 break;
240 case DK_ICE:
241 if (context->abort_on_error)
242 real_abort ();
244 fnotice (stderr, "Please submit a full bug report,\n"
245 "with preprocessed source if appropriate.\n"
246 "See %s for instructions.\n", bug_report_url);
247 exit (ICE_EXIT_CODE);
249 case DK_FATAL:
250 if (context->abort_on_error)
251 real_abort ();
253 fnotice (stderr, "compilation terminated.\n");
254 exit (FATAL_EXIT_CODE);
256 default:
257 gcc_unreachable ();
261 /* Prints out, if necessary, the name of the current function
262 that caused an error. Called from all error and warning functions. */
263 void
264 diagnostic_report_current_function (diagnostic_context *context,
265 diagnostic_info *diagnostic)
267 diagnostic_report_current_module (context);
268 lang_hooks.print_error_function (context, input_filename, diagnostic);
271 void
272 diagnostic_report_current_module (diagnostic_context *context)
274 struct file_stack *p;
276 if (pp_needs_newline (context->printer))
278 pp_newline (context->printer);
279 pp_needs_newline (context->printer) = false;
282 p = input_file_stack;
283 if (p && diagnostic_last_module_changed (context))
285 expanded_location xloc = expand_location (p->location);
286 pp_verbatim (context->printer,
287 "In file included from %s:%d",
288 xloc.file, xloc.line);
289 while ((p = p->next) != NULL)
291 xloc = expand_location (p->location);
292 pp_verbatim (context->printer,
293 ",\n from %s:%d",
294 xloc.file, xloc.line);
296 pp_verbatim (context->printer, ":");
297 diagnostic_set_last_module (context);
298 pp_newline (context->printer);
302 static void
303 default_diagnostic_starter (diagnostic_context *context,
304 diagnostic_info *diagnostic)
306 diagnostic_report_current_function (context, diagnostic);
307 pp_set_prefix (context->printer, diagnostic_build_prefix (diagnostic));
310 static void
311 default_diagnostic_finalizer (diagnostic_context *context,
312 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
314 pp_destroy_prefix (context->printer);
317 /* Interface to specify diagnostic kind overrides. Returns the
318 previous setting, or DK_UNSPECIFIED if the parameters are out of
319 range. */
320 diagnostic_t
321 diagnostic_classify_diagnostic (diagnostic_context *context,
322 int option_index,
323 diagnostic_t new_kind)
325 diagnostic_t old_kind;
327 if (option_index <= 0
328 || option_index >= N_OPTS
329 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
330 return DK_UNSPECIFIED;
332 old_kind = context->classify_diagnostic[option_index];
333 context->classify_diagnostic[option_index] = new_kind;
334 return old_kind;
337 /* Report a diagnostic message (an error or a warning) as specified by
338 DC. This function is *the* subroutine in terms of which front-ends
339 should implement their specific diagnostic handling modules. The
340 front-end independent format specifiers are exactly those described
341 in the documentation of output_format. */
343 void
344 diagnostic_report_diagnostic (diagnostic_context *context,
345 diagnostic_info *diagnostic)
347 bool maybe_print_warnings_as_errors_message = false;
349 /* Give preference to being able to inhibit warnings, before they
350 get reclassified to something else. */
351 if (diagnostic->kind == DK_WARNING
352 && !diagnostic_report_warnings_p ())
353 return;
355 if (context->lock > 0)
357 /* If we're reporting an ICE in the middle of some other error,
358 try to flush out the previous error, then let this one
359 through. Don't do this more than once. */
360 if (diagnostic->kind == DK_ICE && context->lock == 1)
361 pp_flush (context->printer);
362 else
363 error_recursion (context);
366 /* If the user requested that warnings be treated as errors, so be
367 it. Note that we do this before the next block so that
368 individual warnings can be overridden back to warnings with
369 -Wno-error=*. */
370 if (context->warning_as_error_requested
371 && diagnostic->kind == DK_WARNING)
373 diagnostic->kind = DK_ERROR;
374 maybe_print_warnings_as_errors_message = true;
377 if (diagnostic->option_index)
379 /* This tests if the user provided the appropriate -Wfoo or
380 -Wno-foo option. */
381 if (! option_enabled (diagnostic->option_index))
382 return;
383 /* This tests if the user provided the appropriate -Werror=foo
384 option. */
385 if (context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
387 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
388 maybe_print_warnings_as_errors_message = false;
390 /* This allows for future extensions, like temporarily disabling
391 warnings for ranges of source code. */
392 if (diagnostic->kind == DK_IGNORED)
393 return;
396 /* If we changed the kind due to -Werror, and didn't override it, we
397 need to print this message. */
398 if (context->issue_warnings_are_errors_message
399 && maybe_print_warnings_as_errors_message)
401 pp_verbatim (context->printer,
402 "%s: warnings being treated as errors\n", progname);
403 context->issue_warnings_are_errors_message = false;
406 context->lock++;
408 if (diagnostic_count_diagnostic (context, diagnostic))
410 const char *saved_format_spec = diagnostic->message.format_spec;
412 if (context->show_option_requested && diagnostic->option_index)
413 diagnostic->message.format_spec
414 = ACONCAT ((diagnostic->message.format_spec,
415 " [", cl_options[diagnostic->option_index].opt_text, "]", NULL));
417 diagnostic->message.locus = &diagnostic->location;
418 diagnostic->message.abstract_origin = &diagnostic->abstract_origin;
419 diagnostic->abstract_origin = NULL;
420 pp_format (context->printer, &diagnostic->message);
421 (*diagnostic_starter (context)) (context, diagnostic);
422 pp_output_formatted_text (context->printer);
423 (*diagnostic_finalizer (context)) (context, diagnostic);
424 pp_flush (context->printer);
425 diagnostic_action_after_output (context, diagnostic);
426 diagnostic->message.format_spec = saved_format_spec;
427 diagnostic->abstract_origin = NULL;
430 context->lock--;
433 /* Given a partial pathname as input, return another pathname that
434 shares no directory elements with the pathname of __FILE__. This
435 is used by fancy_abort() to print `Internal compiler error in expr.c'
436 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
438 const char *
439 trim_filename (const char *name)
441 static const char this_file[] = __FILE__;
442 const char *p = name, *q = this_file;
444 /* First skip any "../" in each filename. This allows us to give a proper
445 reference to a file in a subdirectory. */
446 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
447 p += 3;
449 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
450 q += 3;
452 /* Now skip any parts the two filenames have in common. */
453 while (*p == *q && *p != 0 && *q != 0)
454 p++, q++;
456 /* Now go backwards until the previous directory separator. */
457 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
458 p--;
460 return p;
463 /* Standard error reporting routines in increasing order of severity.
464 All of these take arguments like printf. */
466 /* Text to be emitted verbatim to the error message stream; this
467 produces no prefix and disables line-wrapping. Use rarely. */
468 void
469 verbatim (const char *gmsgid, ...)
471 text_info text;
472 va_list ap;
474 va_start (ap, gmsgid);
475 text.err_no = errno;
476 text.args_ptr = &ap;
477 text.format_spec = _(gmsgid);
478 text.locus = NULL;
479 text.abstract_origin = NULL;
480 pp_format_verbatim (global_dc->printer, &text);
481 pp_flush (global_dc->printer);
482 va_end (ap);
485 /* An informative note. Use this for additional details on an error
486 message. */
487 void
488 inform (const char *gmsgid, ...)
490 diagnostic_info diagnostic;
491 va_list ap;
493 va_start (ap, gmsgid);
494 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_NOTE);
495 report_diagnostic (&diagnostic);
496 va_end (ap);
499 /* A warning. Use this for code which is correct according to the
500 relevant language specification but is likely to be buggy anyway. */
501 void
502 warning (int opt, const char *gmsgid, ...)
504 diagnostic_info diagnostic;
505 va_list ap;
507 va_start (ap, gmsgid);
508 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
509 diagnostic.option_index = opt;
511 report_diagnostic (&diagnostic);
512 va_end (ap);
515 void
516 warning0 (const char *gmsgid, ...)
518 diagnostic_info diagnostic;
519 va_list ap;
521 va_start (ap, gmsgid);
522 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
523 report_diagnostic (&diagnostic);
524 va_end (ap);
527 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
528 given on the command line, in which case it issues an error. Use
529 this for diagnostics required by the relevant language standard,
530 if you have chosen not to make them errors.
532 Note that these diagnostics are issued independent of the setting
533 of the -pedantic command-line switch. To get a warning enabled
534 only with that switch, write "if (pedantic) pedwarn (...);" */
535 void
536 pedwarn (const char *gmsgid, ...)
538 diagnostic_info diagnostic;
539 va_list ap;
541 va_start (ap, gmsgid);
542 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location,
543 pedantic_error_kind ());
544 report_diagnostic (&diagnostic);
545 va_end (ap);
548 /* A hard error: the code is definitely ill-formed, and an object file
549 will not be produced. */
550 void
551 error (const char *gmsgid, ...)
553 diagnostic_info diagnostic;
554 va_list ap;
556 va_start (ap, gmsgid);
557 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
558 report_diagnostic (&diagnostic);
559 va_end (ap);
562 /* "Sorry, not implemented." Use for a language feature which is
563 required by the relevant specification but not implemented by GCC.
564 An object file will not be produced. */
565 void
566 sorry (const char *gmsgid, ...)
568 diagnostic_info diagnostic;
569 va_list ap;
571 va_start (ap, gmsgid);
572 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
573 report_diagnostic (&diagnostic);
574 va_end (ap);
577 /* An error which is severe enough that we make no attempt to
578 continue. Do not use this for internal consistency checks; that's
579 internal_error. Use of this function should be rare. */
580 void
581 fatal_error (const char *gmsgid, ...)
583 diagnostic_info diagnostic;
584 va_list ap;
586 va_start (ap, gmsgid);
587 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
588 report_diagnostic (&diagnostic);
589 va_end (ap);
591 gcc_unreachable ();
594 /* An internal consistency check has failed. We make no attempt to
595 continue. Note that unless there is debugging value to be had from
596 a more specific message, or some other good reason, you should use
597 abort () instead of calling this function directly. */
598 void
599 internal_error (const char *gmsgid, ...)
601 diagnostic_info diagnostic;
602 va_list ap;
604 va_start (ap, gmsgid);
605 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
606 report_diagnostic (&diagnostic);
607 va_end (ap);
609 gcc_unreachable ();
612 /* Special case error functions. Most are implemented in terms of the
613 above, or should be. */
615 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
616 runs its second argument through gettext. */
617 void
618 fnotice (FILE *file, const char *cmsgid, ...)
620 va_list ap;
622 va_start (ap, cmsgid);
623 vfprintf (file, _(cmsgid), ap);
624 va_end (ap);
627 /* Inform the user that an error occurred while trying to report some
628 other error. This indicates catastrophic internal inconsistencies,
629 so give up now. But do try to flush out the previous error.
630 This mustn't use internal_error, that will cause infinite recursion. */
632 static void
633 error_recursion (diagnostic_context *context)
635 diagnostic_info diagnostic;
637 if (context->lock < 3)
638 pp_flush (context->printer);
640 fnotice (stderr,
641 "Internal compiler error: Error reporting routines re-entered.\n");
643 /* Call diagnostic_action_after_output to get the "please submit a bug
644 report" message. It only looks at the kind field of diagnostic_info. */
645 diagnostic.kind = DK_ICE;
646 diagnostic_action_after_output (context, &diagnostic);
648 /* Do not use gcc_unreachable here; that goes through internal_error
649 and therefore would cause infinite recursion. */
650 real_abort ();
653 /* Report an internal compiler error in a friendly manner. This is
654 the function that gets called upon use of abort() in the source
655 code generally, thanks to a special macro. */
657 void
658 fancy_abort (const char *file, int line, const char *function)
660 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
663 /* Really call the system 'abort'. This has to go right at the end of
664 this file, so that there are no functions after it that call abort
665 and get the system abort instead of our macro. */
666 #undef abort
667 static void
668 real_abort (void)
670 abort ();