re PR target/30519 (Configuration mismatch -- extra parts)
[official-gcc.git] / gcc / diagnostic.c
blob3691477e07b5bc68de9f8469d2e2270f5241ac4a
1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
24 /* This file implements the language independent aspect of diagnostic
25 message module. */
27 #include "config.h"
28 #undef FLOAT /* This is for hpux. They should change hpux. */
29 #undef FFS /* Some systems define this in param.h. */
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "tree.h"
34 #include "version.h"
35 #include "tm_p.h"
36 #include "flags.h"
37 #include "input.h"
38 #include "toplev.h"
39 #include "intl.h"
40 #include "diagnostic.h"
41 #include "langhooks.h"
42 #include "langhooks-def.h"
43 #include "opts.h"
46 /* Prototypes. */
47 static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
49 static void default_diagnostic_starter (diagnostic_context *,
50 diagnostic_info *);
51 static void default_diagnostic_finalizer (diagnostic_context *,
52 diagnostic_info *);
54 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
55 static bool diagnostic_count_diagnostic (diagnostic_context *,
56 diagnostic_info *);
57 static void diagnostic_action_after_output (diagnostic_context *,
58 diagnostic_info *);
59 static void real_abort (void) ATTRIBUTE_NORETURN;
61 /* A diagnostic_context surrogate for stderr. */
62 static diagnostic_context global_diagnostic_context;
63 diagnostic_context *global_dc = &global_diagnostic_context;
66 /* Return a malloc'd string containing MSG formatted a la printf. The
67 caller is responsible for freeing the memory. */
68 static char *
69 build_message_string (const char *msg, ...)
71 char *str;
72 va_list ap;
74 va_start (ap, msg);
75 vasprintf (&str, msg, ap);
76 va_end (ap);
78 return str;
81 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
82 char *
83 file_name_as_prefix (const char *f)
85 return build_message_string ("%s: ", f);
90 /* Initialize the diagnostic message outputting machinery. */
91 void
92 diagnostic_initialize (diagnostic_context *context)
94 /* Allocate a basic pretty-printer. Clients will replace this a
95 much more elaborated pretty-printer if they wish. */
96 context->printer = XNEW (pretty_printer);
97 pp_construct (context->printer, NULL, 0);
98 /* By default, diagnostics are sent to stderr. */
99 context->printer->buffer->stream = stderr;
100 /* By default, we emit prefixes once per message. */
101 context->printer->wrapping.rule = DIAGNOSTICS_SHOW_PREFIX_ONCE;
103 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
104 context->issue_warnings_are_errors_message = true;
105 context->warning_as_error_requested = false;
106 memset (context->classify_diagnostic, DK_UNSPECIFIED,
107 sizeof context->classify_diagnostic);
108 context->show_option_requested = false;
109 context->abort_on_error = false;
110 context->internal_error = NULL;
111 diagnostic_starter (context) = default_diagnostic_starter;
112 diagnostic_finalizer (context) = default_diagnostic_finalizer;
113 context->last_module = 0;
114 context->last_function = NULL;
115 context->lock = 0;
118 /* Initialize DIAGNOSTIC, where the message MSG has already been
119 translated. */
120 void
121 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
122 va_list *args, location_t location,
123 diagnostic_t kind)
125 diagnostic->message.err_no = errno;
126 diagnostic->message.args_ptr = args;
127 diagnostic->message.format_spec = msg;
128 diagnostic->location = location;
129 diagnostic->kind = kind;
130 diagnostic->option_index = 0;
133 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
134 translated. */
135 void
136 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
137 va_list *args, location_t location,
138 diagnostic_t kind)
140 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
143 /* Return a malloc'd string describing a location. The caller is
144 responsible for freeing the memory. */
145 char *
146 diagnostic_build_prefix (diagnostic_info *diagnostic)
148 static const char *const diagnostic_kind_text[] = {
149 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
150 #include "diagnostic.def"
151 #undef DEFINE_DIAGNOSTIC_KIND
152 "must-not-happen"
154 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
155 expanded_location s = expand_location (diagnostic->location);
156 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
158 return
159 (s.file == NULL
160 ? build_message_string ("%s: %s", progname, text)
161 #ifdef USE_MAPPED_LOCATION
162 : flag_show_column && s.column != 0
163 ? build_message_string ("%s:%d:%d: %s", s.file, s.line, s.column, text)
164 #endif
165 : build_message_string ("%s:%d: %s", s.file, s.line, text));
168 /* Count a diagnostic. Return true if the message should be printed. */
169 static bool
170 diagnostic_count_diagnostic (diagnostic_context *context,
171 diagnostic_info *diagnostic)
173 diagnostic_t kind = diagnostic->kind;
174 switch (kind)
176 default:
177 gcc_unreachable ();
179 case DK_ICE:
180 #ifndef ENABLE_CHECKING
181 /* When not checking, ICEs are converted to fatal errors when an
182 error has already occurred. This is counteracted by
183 abort_on_error. */
184 if ((diagnostic_kind_count (context, DK_ERROR) > 0
185 || diagnostic_kind_count (context, DK_SORRY) > 0)
186 && !context->abort_on_error)
188 expanded_location s = expand_location (diagnostic->location);
189 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
190 s.file, s.line);
191 exit (ICE_EXIT_CODE);
193 #endif
194 if (context->internal_error)
195 (*context->internal_error) (diagnostic->message.format_spec,
196 diagnostic->message.args_ptr);
197 /* Fall through. */
199 case DK_FATAL: case DK_SORRY:
200 case DK_ANACHRONISM: case DK_NOTE:
201 ++diagnostic_kind_count (context, kind);
202 break;
204 case DK_WARNING:
205 if (!diagnostic_report_warnings_p ())
206 return false;
208 /* -Werror can reclassify warnings as errors, but
209 classify_diagnostic can reclassify it back to a warning. The
210 second part of this test detects that case. */
211 if (!context->warning_as_error_requested
212 || (context->classify_diagnostic[diagnostic->option_index]
213 == DK_WARNING))
215 ++diagnostic_kind_count (context, DK_WARNING);
216 break;
218 else if (context->issue_warnings_are_errors_message)
220 pp_verbatim (context->printer,
221 "%s: warnings being treated as errors\n", progname);
222 context->issue_warnings_are_errors_message = false;
225 /* And fall through. */
226 case DK_ERROR:
227 ++diagnostic_kind_count (context, DK_ERROR);
228 break;
231 return true;
234 /* Take any action which is expected to happen after the diagnostic
235 is written out. This function does not always return. */
236 static void
237 diagnostic_action_after_output (diagnostic_context *context,
238 diagnostic_info *diagnostic)
240 switch (diagnostic->kind)
242 case DK_DEBUG:
243 case DK_NOTE:
244 case DK_ANACHRONISM:
245 case DK_WARNING:
246 break;
248 case DK_ERROR:
249 case DK_SORRY:
250 if (context->abort_on_error)
251 real_abort ();
252 if (flag_fatal_errors)
254 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
255 exit (FATAL_EXIT_CODE);
257 break;
259 case DK_ICE:
260 if (context->abort_on_error)
261 real_abort ();
263 fnotice (stderr, "Please submit a full bug report,\n"
264 "with preprocessed source if appropriate.\n"
265 "See %s for instructions.\n", bug_report_url);
266 exit (ICE_EXIT_CODE);
268 case DK_FATAL:
269 if (context->abort_on_error)
270 real_abort ();
272 fnotice (stderr, "compilation terminated.\n");
273 exit (FATAL_EXIT_CODE);
275 default:
276 gcc_unreachable ();
280 /* Prints out, if necessary, the name of the current function
281 that caused an error. Called from all error and warning functions. */
282 void
283 diagnostic_report_current_function (diagnostic_context *context)
285 diagnostic_report_current_module (context);
286 lang_hooks.print_error_function (context, input_filename);
289 void
290 diagnostic_report_current_module (diagnostic_context *context)
292 struct file_stack *p;
294 if (pp_needs_newline (context->printer))
296 pp_newline (context->printer);
297 pp_needs_newline (context->printer) = false;
300 p = input_file_stack;
301 if (p && diagnostic_last_module_changed (context))
303 expanded_location xloc = expand_location (p->location);
304 pp_verbatim (context->printer,
305 "In file included from %s:%d",
306 xloc.file, xloc.line);
307 while ((p = p->next) != NULL)
309 xloc = expand_location (p->location);
310 pp_verbatim (context->printer,
311 ",\n from %s:%d",
312 xloc.file, xloc.line);
314 pp_verbatim (context->printer, ":");
315 diagnostic_set_last_module (context);
316 pp_newline (context->printer);
320 static void
321 default_diagnostic_starter (diagnostic_context *context,
322 diagnostic_info *diagnostic)
324 diagnostic_report_current_function (context);
325 pp_set_prefix (context->printer, diagnostic_build_prefix (diagnostic));
328 static void
329 default_diagnostic_finalizer (diagnostic_context *context,
330 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
332 pp_destroy_prefix (context->printer);
335 /* Interface to specify diagnostic kind overrides. Returns the
336 previous setting, or DK_UNSPECIFIED if the parameters are out of
337 range. */
338 diagnostic_t
339 diagnostic_classify_diagnostic (diagnostic_context *context,
340 int option_index,
341 diagnostic_t new_kind)
343 diagnostic_t old_kind;
345 if (option_index <= 0
346 || option_index >= N_OPTS
347 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
348 return DK_UNSPECIFIED;
350 old_kind = context->classify_diagnostic[option_index];
351 context->classify_diagnostic[option_index] = new_kind;
352 return old_kind;
355 /* Report a diagnostic message (an error or a warning) as specified by
356 DC. This function is *the* subroutine in terms of which front-ends
357 should implement their specific diagnostic handling modules. The
358 front-end independent format specifiers are exactly those described
359 in the documentation of output_format. */
361 void
362 diagnostic_report_diagnostic (diagnostic_context *context,
363 diagnostic_info *diagnostic)
365 if (context->lock > 0)
367 /* If we're reporting an ICE in the middle of some other error,
368 try to flush out the previous error, then let this one
369 through. Don't do this more than once. */
370 if (diagnostic->kind == DK_ICE && context->lock == 1)
371 pp_flush (context->printer);
372 else
373 error_recursion (context);
376 if (diagnostic->option_index)
378 /* This tests if the user provided the appropriate -Wfoo or
379 -Wno-foo option. */
380 if (! option_enabled (diagnostic->option_index))
381 return;
382 /* This tests if the user provided the appropriate -Werror=foo
383 option. */
384 if (context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
385 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
386 /* This allows for future extensions, like temporarily disabling
387 warnings for ranges of source code. */
388 if (diagnostic->kind == DK_IGNORED)
389 return;
392 context->lock++;
394 if (diagnostic_count_diagnostic (context, diagnostic))
396 const char *saved_format_spec = diagnostic->message.format_spec;
398 if (context->show_option_requested && diagnostic->option_index)
399 diagnostic->message.format_spec
400 = ACONCAT ((diagnostic->message.format_spec,
401 " [", cl_options[diagnostic->option_index].opt_text, "]", NULL));
403 diagnostic->message.locus = &diagnostic->location;
404 pp_format (context->printer, &diagnostic->message);
405 (*diagnostic_starter (context)) (context, diagnostic);
406 pp_output_formatted_text (context->printer);
407 (*diagnostic_finalizer (context)) (context, diagnostic);
408 pp_flush (context->printer);
409 diagnostic_action_after_output (context, diagnostic);
410 diagnostic->message.format_spec = saved_format_spec;
413 context->lock--;
416 /* Given a partial pathname as input, return another pathname that
417 shares no directory elements with the pathname of __FILE__. This
418 is used by fancy_abort() to print `Internal compiler error in expr.c'
419 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
421 const char *
422 trim_filename (const char *name)
424 static const char this_file[] = __FILE__;
425 const char *p = name, *q = this_file;
427 /* First skip any "../" in each filename. This allows us to give a proper
428 reference to a file in a subdirectory. */
429 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
430 p += 3;
432 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
433 q += 3;
435 /* Now skip any parts the two filenames have in common. */
436 while (*p == *q && *p != 0 && *q != 0)
437 p++, q++;
439 /* Now go backwards until the previous directory separator. */
440 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
441 p--;
443 return p;
446 /* Standard error reporting routines in increasing order of severity.
447 All of these take arguments like printf. */
449 /* Text to be emitted verbatim to the error message stream; this
450 produces no prefix and disables line-wrapping. Use rarely. */
451 void
452 verbatim (const char *gmsgid, ...)
454 text_info text;
455 va_list ap;
457 va_start (ap, gmsgid);
458 text.err_no = errno;
459 text.args_ptr = &ap;
460 text.format_spec = _(gmsgid);
461 text.locus = NULL;
462 pp_format_verbatim (global_dc->printer, &text);
463 pp_flush (global_dc->printer);
464 va_end (ap);
467 /* An informative note. Use this for additional details on an error
468 message. */
469 void
470 inform (const char *gmsgid, ...)
472 diagnostic_info diagnostic;
473 va_list ap;
475 va_start (ap, gmsgid);
476 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_NOTE);
477 report_diagnostic (&diagnostic);
478 va_end (ap);
481 /* A warning. Use this for code which is correct according to the
482 relevant language specification but is likely to be buggy anyway. */
483 void
484 warning (int opt, const char *gmsgid, ...)
486 diagnostic_info diagnostic;
487 va_list ap;
489 va_start (ap, gmsgid);
490 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
491 diagnostic.option_index = opt;
493 report_diagnostic (&diagnostic);
494 va_end (ap);
497 void
498 warning0 (const char *gmsgid, ...)
500 diagnostic_info diagnostic;
501 va_list ap;
503 va_start (ap, gmsgid);
504 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
505 report_diagnostic (&diagnostic);
506 va_end (ap);
509 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
510 given on the command line, in which case it issues an error. Use
511 this for diagnostics required by the relevant language standard,
512 if you have chosen not to make them errors.
514 Note that these diagnostics are issued independent of the setting
515 of the -pedantic command-line switch. To get a warning enabled
516 only with that switch, write "if (pedantic) pedwarn (...);" */
517 void
518 pedwarn (const char *gmsgid, ...)
520 diagnostic_info diagnostic;
521 va_list ap;
523 va_start (ap, gmsgid);
524 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location,
525 pedantic_error_kind ());
526 report_diagnostic (&diagnostic);
527 va_end (ap);
530 /* A hard error: the code is definitely ill-formed, and an object file
531 will not be produced. */
532 void
533 error (const char *gmsgid, ...)
535 diagnostic_info diagnostic;
536 va_list ap;
538 va_start (ap, gmsgid);
539 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
540 report_diagnostic (&diagnostic);
541 va_end (ap);
544 /* "Sorry, not implemented." Use for a language feature which is
545 required by the relevant specification but not implemented by GCC.
546 An object file will not be produced. */
547 void
548 sorry (const char *gmsgid, ...)
550 diagnostic_info diagnostic;
551 va_list ap;
553 va_start (ap, gmsgid);
554 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
555 report_diagnostic (&diagnostic);
556 va_end (ap);
559 /* An error which is severe enough that we make no attempt to
560 continue. Do not use this for internal consistency checks; that's
561 internal_error. Use of this function should be rare. */
562 void
563 fatal_error (const char *gmsgid, ...)
565 diagnostic_info diagnostic;
566 va_list ap;
568 va_start (ap, gmsgid);
569 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
570 report_diagnostic (&diagnostic);
571 va_end (ap);
573 gcc_unreachable ();
576 /* An internal consistency check has failed. We make no attempt to
577 continue. Note that unless there is debugging value to be had from
578 a more specific message, or some other good reason, you should use
579 abort () instead of calling this function directly. */
580 void
581 internal_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_ICE);
588 report_diagnostic (&diagnostic);
589 va_end (ap);
591 gcc_unreachable ();
594 /* Special case error functions. Most are implemented in terms of the
595 above, or should be. */
597 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
598 runs its second argument through gettext. */
599 void
600 fnotice (FILE *file, const char *cmsgid, ...)
602 va_list ap;
604 va_start (ap, cmsgid);
605 vfprintf (file, _(cmsgid), ap);
606 va_end (ap);
609 /* Inform the user that an error occurred while trying to report some
610 other error. This indicates catastrophic internal inconsistencies,
611 so give up now. But do try to flush out the previous error.
612 This mustn't use internal_error, that will cause infinite recursion. */
614 static void
615 error_recursion (diagnostic_context *context)
617 diagnostic_info diagnostic;
619 if (context->lock < 3)
620 pp_flush (context->printer);
622 fnotice (stderr,
623 "Internal compiler error: Error reporting routines re-entered.\n");
625 /* Call diagnostic_action_after_output to get the "please submit a bug
626 report" message. It only looks at the kind field of diagnostic_info. */
627 diagnostic.kind = DK_ICE;
628 diagnostic_action_after_output (context, &diagnostic);
630 /* Do not use gcc_unreachable here; that goes through internal_error
631 and therefore would cause infinite recursion. */
632 real_abort ();
635 /* Report an internal compiler error in a friendly manner. This is
636 the function that gets called upon use of abort() in the source
637 code generally, thanks to a special macro. */
639 void
640 fancy_abort (const char *file, int line, const char *function)
642 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
645 /* Really call the system 'abort'. This has to go right at the end of
646 this file, so that there are no functions after it that call abort
647 and get the system abort instead of our macro. */
648 #undef abort
649 static void
650 real_abort (void)
652 abort ();