Check in tree-dce enh to trunk
[official-gcc.git] / gcc / diagnostic.c
blob5a6aaee914d2b532aa0bef2bee6ea2b7da3635e8
1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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;
55 static void diagnostic_action_after_output (diagnostic_context *,
56 diagnostic_info *);
57 static void real_abort (void) ATTRIBUTE_NORETURN;
59 /* A diagnostic_context surrogate for stderr. */
60 static diagnostic_context global_diagnostic_context;
61 diagnostic_context *global_dc = &global_diagnostic_context;
64 /* Return a malloc'd string containing MSG formatted a la printf. The
65 caller is responsible for freeing the memory. */
66 static char *
67 build_message_string (const char *msg, ...)
69 char *str;
70 va_list ap;
72 va_start (ap, msg);
73 vasprintf (&str, msg, ap);
74 va_end (ap);
76 return str;
79 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
80 char *
81 file_name_as_prefix (const char *f)
83 return build_message_string ("%s: ", f);
88 /* Initialize the diagnostic message outputting machinery. */
89 void
90 diagnostic_initialize (diagnostic_context *context)
92 /* Allocate a basic pretty-printer. Clients will replace this a
93 much more elaborated pretty-printer if they wish. */
94 context->printer = XNEW (pretty_printer);
95 pp_construct (context->printer, NULL, 0);
96 /* By default, diagnostics are sent to stderr. */
97 context->printer->buffer->stream = stderr;
98 /* By default, we emit prefixes once per message. */
99 context->printer->wrapping.rule = DIAGNOSTICS_SHOW_PREFIX_ONCE;
101 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
102 context->issue_warnings_are_errors_message = true;
103 context->warning_as_error_requested = false;
104 memset (context->classify_diagnostic, DK_UNSPECIFIED,
105 sizeof context->classify_diagnostic);
106 context->show_option_requested = false;
107 context->abort_on_error = false;
108 context->internal_error = NULL;
109 diagnostic_starter (context) = default_diagnostic_starter;
110 diagnostic_finalizer (context) = default_diagnostic_finalizer;
111 context->last_module = 0;
112 context->last_function = NULL;
113 context->lock = 0;
116 /* Initialize DIAGNOSTIC, where the message MSG has already been
117 translated. */
118 void
119 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
120 va_list *args, location_t location,
121 diagnostic_t kind)
123 diagnostic->message.err_no = errno;
124 diagnostic->message.args_ptr = args;
125 diagnostic->message.format_spec = msg;
126 diagnostic->location = location;
127 diagnostic->kind = kind;
128 diagnostic->option_index = 0;
131 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
132 translated. */
133 void
134 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
135 va_list *args, location_t location,
136 diagnostic_t kind)
138 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
141 /* Return a malloc'd string describing a location. The caller is
142 responsible for freeing the memory. */
143 char *
144 diagnostic_build_prefix (diagnostic_info *diagnostic)
146 static const char *const diagnostic_kind_text[] = {
147 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
148 #include "diagnostic.def"
149 #undef DEFINE_DIAGNOSTIC_KIND
150 "must-not-happen"
152 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
153 expanded_location s = expand_location (diagnostic->location);
154 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
156 return
157 (s.file == NULL
158 ? build_message_string ("%s: %s", progname, text)
159 : flag_show_column && s.column != 0
160 ? build_message_string ("%s:%d:%d: %s", s.file, s.line, s.column, text)
161 : build_message_string ("%s:%d: %s", s.file, s.line, text));
164 /* Take any action which is expected to happen after the diagnostic
165 is written out. This function does not always return. */
166 static void
167 diagnostic_action_after_output (diagnostic_context *context,
168 diagnostic_info *diagnostic)
170 switch (diagnostic->kind)
172 case DK_DEBUG:
173 case DK_NOTE:
174 case DK_ANACHRONISM:
175 case DK_WARNING:
176 break;
178 case DK_ERROR:
179 case DK_SORRY:
180 if (context->abort_on_error)
181 real_abort ();
182 if (flag_fatal_errors)
184 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
185 exit (FATAL_EXIT_CODE);
187 break;
189 case DK_ICE:
190 if (context->abort_on_error)
191 real_abort ();
193 fnotice (stderr, "Please submit a full bug report,\n"
194 "with preprocessed source if appropriate.\n"
195 "See %s for instructions.\n", bug_report_url);
196 exit (ICE_EXIT_CODE);
198 case DK_FATAL:
199 if (context->abort_on_error)
200 real_abort ();
202 fnotice (stderr, "compilation terminated.\n");
203 exit (FATAL_EXIT_CODE);
205 default:
206 gcc_unreachable ();
210 /* Prints out, if necessary, the name of the current function
211 that caused an error. Called from all error and warning functions. */
212 void
213 diagnostic_report_current_function (diagnostic_context *context,
214 diagnostic_info *diagnostic)
216 diagnostic_report_current_module (context);
217 lang_hooks.print_error_function (context, input_filename, diagnostic);
220 void
221 diagnostic_report_current_module (diagnostic_context *context)
223 const struct line_map *map;
225 if (pp_needs_newline (context->printer))
227 pp_newline (context->printer);
228 pp_needs_newline (context->printer) = false;
231 if (input_location <= BUILTINS_LOCATION)
232 return;
234 map = linemap_lookup (line_table, input_location);
235 if (map && diagnostic_last_module_changed (context, map))
237 diagnostic_set_last_module (context, map);
238 if (! MAIN_FILE_P (map))
240 map = INCLUDED_FROM (line_table, map);
241 pp_verbatim (context->printer,
242 "In file included from %s:%d",
243 map->to_file, LAST_SOURCE_LINE (map));
244 while (! MAIN_FILE_P (map))
246 map = INCLUDED_FROM (line_table, map);
247 pp_verbatim (context->printer,
248 ",\n from %s:%d",
249 map->to_file, LAST_SOURCE_LINE (map));
251 pp_verbatim (context->printer, ":");
252 pp_newline (context->printer);
257 static void
258 default_diagnostic_starter (diagnostic_context *context,
259 diagnostic_info *diagnostic)
261 diagnostic_report_current_function (context, diagnostic);
262 pp_set_prefix (context->printer, diagnostic_build_prefix (diagnostic));
265 static void
266 default_diagnostic_finalizer (diagnostic_context *context,
267 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
269 pp_destroy_prefix (context->printer);
272 /* Interface to specify diagnostic kind overrides. Returns the
273 previous setting, or DK_UNSPECIFIED if the parameters are out of
274 range. */
275 diagnostic_t
276 diagnostic_classify_diagnostic (diagnostic_context *context,
277 int option_index,
278 diagnostic_t new_kind)
280 diagnostic_t old_kind;
282 if (option_index <= 0
283 || option_index >= N_OPTS
284 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
285 return DK_UNSPECIFIED;
287 old_kind = context->classify_diagnostic[option_index];
288 context->classify_diagnostic[option_index] = new_kind;
289 return old_kind;
292 /* Report a diagnostic message (an error or a warning) as specified by
293 DC. This function is *the* subroutine in terms of which front-ends
294 should implement their specific diagnostic handling modules. The
295 front-end independent format specifiers are exactly those described
296 in the documentation of output_format. */
298 void
299 diagnostic_report_diagnostic (diagnostic_context *context,
300 diagnostic_info *diagnostic)
302 bool maybe_print_warnings_as_errors_message = false;
303 const char *saved_format_spec;
305 /* Give preference to being able to inhibit warnings, before they
306 get reclassified to something else. */
307 if (diagnostic->kind == DK_WARNING
308 && !diagnostic_report_warnings_p ())
309 return;
311 if (context->lock > 0)
313 /* If we're reporting an ICE in the middle of some other error,
314 try to flush out the previous error, then let this one
315 through. Don't do this more than once. */
316 if (diagnostic->kind == DK_ICE && context->lock == 1)
317 pp_flush (context->printer);
318 else
319 error_recursion (context);
322 /* If the user requested that warnings be treated as errors, so be
323 it. Note that we do this before the next block so that
324 individual warnings can be overridden back to warnings with
325 -Wno-error=*. */
326 if (context->warning_as_error_requested
327 && diagnostic->kind == DK_WARNING)
329 diagnostic->kind = DK_ERROR;
330 maybe_print_warnings_as_errors_message = true;
333 if (diagnostic->option_index)
335 /* This tests if the user provided the appropriate -Wfoo or
336 -Wno-foo option. */
337 if (! option_enabled (diagnostic->option_index))
338 return;
339 /* This tests if the user provided the appropriate -Werror=foo
340 option. */
341 if (context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
343 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
344 maybe_print_warnings_as_errors_message = false;
346 /* This allows for future extensions, like temporarily disabling
347 warnings for ranges of source code. */
348 if (diagnostic->kind == DK_IGNORED)
349 return;
352 /* If we changed the kind due to -Werror, and didn't override it, we
353 need to print this message. */
354 if (context->issue_warnings_are_errors_message
355 && maybe_print_warnings_as_errors_message)
357 pp_verbatim (context->printer,
358 "%s: warnings being treated as errors\n", progname);
359 context->issue_warnings_are_errors_message = false;
362 context->lock++;
364 if (diagnostic->kind == DK_ICE)
366 #ifndef ENABLE_CHECKING
367 /* When not checking, ICEs are converted to fatal errors when an
368 error has already occurred. This is counteracted by
369 abort_on_error. */
370 if ((diagnostic_kind_count (context, DK_ERROR) > 0
371 || diagnostic_kind_count (context, DK_SORRY) > 0)
372 && !context->abort_on_error)
374 expanded_location s = expand_location (diagnostic->location);
375 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
376 s.file, s.line);
377 exit (ICE_EXIT_CODE);
379 #endif
380 if (context->internal_error)
381 (*context->internal_error) (diagnostic->message.format_spec,
382 diagnostic->message.args_ptr);
384 ++diagnostic_kind_count (context, diagnostic->kind);
386 saved_format_spec = diagnostic->message.format_spec;
387 if (context->show_option_requested && diagnostic->option_index)
388 diagnostic->message.format_spec
389 = ACONCAT ((diagnostic->message.format_spec,
390 " [", cl_options[diagnostic->option_index].opt_text, "]", NULL));
392 diagnostic->message.locus = &diagnostic->location;
393 diagnostic->message.abstract_origin = &diagnostic->abstract_origin;
394 diagnostic->abstract_origin = NULL;
395 pp_format (context->printer, &diagnostic->message);
396 (*diagnostic_starter (context)) (context, diagnostic);
397 pp_output_formatted_text (context->printer);
398 (*diagnostic_finalizer (context)) (context, diagnostic);
399 pp_flush (context->printer);
400 diagnostic_action_after_output (context, diagnostic);
401 diagnostic->message.format_spec = saved_format_spec;
402 diagnostic->abstract_origin = NULL;
404 context->lock--;
407 /* Given a partial pathname as input, return another pathname that
408 shares no directory elements with the pathname of __FILE__. This
409 is used by fancy_abort() to print `Internal compiler error in expr.c'
410 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
412 const char *
413 trim_filename (const char *name)
415 static const char this_file[] = __FILE__;
416 const char *p = name, *q = this_file;
418 /* First skip any "../" in each filename. This allows us to give a proper
419 reference to a file in a subdirectory. */
420 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
421 p += 3;
423 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
424 q += 3;
426 /* Now skip any parts the two filenames have in common. */
427 while (*p == *q && *p != 0 && *q != 0)
428 p++, q++;
430 /* Now go backwards until the previous directory separator. */
431 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
432 p--;
434 return p;
437 /* Standard error reporting routines in increasing order of severity.
438 All of these take arguments like printf. */
440 /* Text to be emitted verbatim to the error message stream; this
441 produces no prefix and disables line-wrapping. Use rarely. */
442 void
443 verbatim (const char *gmsgid, ...)
445 text_info text;
446 va_list ap;
448 va_start (ap, gmsgid);
449 text.err_no = errno;
450 text.args_ptr = &ap;
451 text.format_spec = _(gmsgid);
452 text.locus = NULL;
453 text.abstract_origin = NULL;
454 pp_format_verbatim (global_dc->printer, &text);
455 pp_flush (global_dc->printer);
456 va_end (ap);
459 /* An informative note. Use this for additional details on an error
460 message. */
461 void
462 inform (const char *gmsgid, ...)
464 diagnostic_info diagnostic;
465 va_list ap;
467 va_start (ap, gmsgid);
468 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_NOTE);
469 report_diagnostic (&diagnostic);
470 va_end (ap);
473 /* A warning. Use this for code which is correct according to the
474 relevant language specification but is likely to be buggy anyway. */
475 void
476 warning (int opt, const char *gmsgid, ...)
478 diagnostic_info diagnostic;
479 va_list ap;
481 va_start (ap, gmsgid);
482 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
483 diagnostic.option_index = opt;
485 report_diagnostic (&diagnostic);
486 va_end (ap);
489 void
490 warning0 (const char *gmsgid, ...)
492 diagnostic_info diagnostic;
493 va_list ap;
495 va_start (ap, gmsgid);
496 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
497 report_diagnostic (&diagnostic);
498 va_end (ap);
501 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
502 given on the command line, in which case it issues an error. Use
503 this for diagnostics required by the relevant language standard,
504 if you have chosen not to make them errors.
506 Note that these diagnostics are issued independent of the setting
507 of the -pedantic command-line switch. To get a warning enabled
508 only with that switch, write "if (pedantic) pedwarn (...);" */
509 void
510 pedwarn (const char *gmsgid, ...)
512 diagnostic_info diagnostic;
513 va_list ap;
515 va_start (ap, gmsgid);
516 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location,
517 pedantic_warning_kind ());
518 report_diagnostic (&diagnostic);
519 va_end (ap);
522 /* A "permissive" error: issues an error unless -fpermissive was given
523 on the command line, in which case it issues a warning. Use this
524 for things that really should be errors but we want to support
525 legacy code. */
527 void
528 permerror (const char *gmsgid, ...)
530 diagnostic_info diagnostic;
531 va_list ap;
533 va_start (ap, gmsgid);
534 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location,
535 permissive_error_kind ());
536 diagnostic.option_index = OPT_fpermissive;
537 report_diagnostic (&diagnostic);
538 va_end (ap);
542 /* A hard error: the code is definitely ill-formed, and an object file
543 will not be produced. */
544 void
545 error (const char *gmsgid, ...)
547 diagnostic_info diagnostic;
548 va_list ap;
550 va_start (ap, gmsgid);
551 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
552 report_diagnostic (&diagnostic);
553 va_end (ap);
556 /* "Sorry, not implemented." Use for a language feature which is
557 required by the relevant specification but not implemented by GCC.
558 An object file will not be produced. */
559 void
560 sorry (const char *gmsgid, ...)
562 diagnostic_info diagnostic;
563 va_list ap;
565 va_start (ap, gmsgid);
566 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
567 report_diagnostic (&diagnostic);
568 va_end (ap);
571 /* An error which is severe enough that we make no attempt to
572 continue. Do not use this for internal consistency checks; that's
573 internal_error. Use of this function should be rare. */
574 void
575 fatal_error (const char *gmsgid, ...)
577 diagnostic_info diagnostic;
578 va_list ap;
580 va_start (ap, gmsgid);
581 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
582 report_diagnostic (&diagnostic);
583 va_end (ap);
585 gcc_unreachable ();
588 /* An internal consistency check has failed. We make no attempt to
589 continue. Note that unless there is debugging value to be had from
590 a more specific message, or some other good reason, you should use
591 abort () instead of calling this function directly. */
592 void
593 internal_error (const char *gmsgid, ...)
595 diagnostic_info diagnostic;
596 va_list ap;
598 va_start (ap, gmsgid);
599 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
600 report_diagnostic (&diagnostic);
601 va_end (ap);
603 gcc_unreachable ();
606 /* Special case error functions. Most are implemented in terms of the
607 above, or should be. */
609 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
610 runs its second argument through gettext. */
611 void
612 fnotice (FILE *file, const char *cmsgid, ...)
614 va_list ap;
616 va_start (ap, cmsgid);
617 vfprintf (file, _(cmsgid), ap);
618 va_end (ap);
621 /* Inform the user that an error occurred while trying to report some
622 other error. This indicates catastrophic internal inconsistencies,
623 so give up now. But do try to flush out the previous error.
624 This mustn't use internal_error, that will cause infinite recursion. */
626 static void
627 error_recursion (diagnostic_context *context)
629 diagnostic_info diagnostic;
631 if (context->lock < 3)
632 pp_flush (context->printer);
634 fnotice (stderr,
635 "Internal compiler error: Error reporting routines re-entered.\n");
637 /* Call diagnostic_action_after_output to get the "please submit a bug
638 report" message. It only looks at the kind field of diagnostic_info. */
639 diagnostic.kind = DK_ICE;
640 diagnostic_action_after_output (context, &diagnostic);
642 /* Do not use gcc_unreachable here; that goes through internal_error
643 and therefore would cause infinite recursion. */
644 real_abort ();
647 /* Report an internal compiler error in a friendly manner. This is
648 the function that gets called upon use of abort() in the source
649 code generally, thanks to a special macro. */
651 void
652 fancy_abort (const char *file, int line, const char *function)
654 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
657 /* Really call the system 'abort'. This has to go right at the end of
658 this file, so that there are no functions after it that call abort
659 and get the system abort instead of our macro. */
660 #undef abort
661 static void
662 real_abort (void)
664 abort ();