Enable dumping of alias graphs.
[official-gcc/Ramakrishna.git] / gcc / diagnostic.c
blobbca7478329180af2b6c00fd9552ceb37654e00ab
1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 2009 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"
43 #include "plugin.h"
45 #define pedantic_warning_kind() (flag_pedantic_errors ? DK_ERROR : DK_WARNING)
46 #define permissive_error_kind() (flag_permissive ? DK_WARNING : DK_ERROR)
48 /* Prototypes. */
49 static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
51 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
53 static void diagnostic_action_after_output (diagnostic_context *,
54 diagnostic_info *);
55 static void real_abort (void) ATTRIBUTE_NORETURN;
57 /* A diagnostic_context surrogate for stderr. */
58 static diagnostic_context global_diagnostic_context;
59 diagnostic_context *global_dc = &global_diagnostic_context;
62 /* Return a malloc'd string containing MSG formatted a la printf. The
63 caller is responsible for freeing the memory. */
64 static char *
65 build_message_string (const char *msg, ...)
67 char *str;
68 va_list ap;
70 va_start (ap, msg);
71 vasprintf (&str, msg, ap);
72 va_end (ap);
74 return str;
77 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
78 char *
79 file_name_as_prefix (const char *f)
81 return build_message_string ("%s: ", f);
86 /* Initialize the diagnostic message outputting machinery. */
87 void
88 diagnostic_initialize (diagnostic_context *context)
90 /* Allocate a basic pretty-printer. Clients will replace this a
91 much more elaborated pretty-printer if they wish. */
92 context->printer = XNEW (pretty_printer);
93 pp_construct (context->printer, NULL, 0);
94 /* By default, diagnostics are sent to stderr. */
95 context->printer->buffer->stream = stderr;
96 /* By default, we emit prefixes once per message. */
97 context->printer->wrapping.rule = DIAGNOSTICS_SHOW_PREFIX_ONCE;
99 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
100 context->issue_warnings_are_errors_message = true;
101 context->warning_as_error_requested = false;
102 memset (context->classify_diagnostic, DK_UNSPECIFIED,
103 sizeof context->classify_diagnostic);
104 context->show_option_requested = false;
105 context->abort_on_error = false;
106 context->internal_error = NULL;
107 diagnostic_starter (context) = default_diagnostic_starter;
108 diagnostic_finalizer (context) = default_diagnostic_finalizer;
109 context->last_module = 0;
110 context->last_function = NULL;
111 context->lock = 0;
114 /* Initialize DIAGNOSTIC, where the message MSG has already been
115 translated. */
116 void
117 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
118 va_list *args, location_t location,
119 diagnostic_t kind)
121 diagnostic->message.err_no = errno;
122 diagnostic->message.args_ptr = args;
123 diagnostic->message.format_spec = msg;
124 diagnostic->location = location;
125 diagnostic->override_column = 0;
126 diagnostic->kind = kind;
127 diagnostic->option_index = 0;
130 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
131 translated. */
132 void
133 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
134 va_list *args, location_t location,
135 diagnostic_t kind)
137 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
140 /* Return a malloc'd string describing a location. The caller is
141 responsible for freeing the memory. */
142 char *
143 diagnostic_build_prefix (diagnostic_info *diagnostic)
145 static const char *const diagnostic_kind_text[] = {
146 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
147 #include "diagnostic.def"
148 #undef DEFINE_DIAGNOSTIC_KIND
149 "must-not-happen"
151 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
152 expanded_location s = expand_location (diagnostic->location);
153 if (diagnostic->override_column)
154 s.column = diagnostic->override_column;
155 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
157 return
158 (s.file == NULL
159 ? build_message_string ("%s: %s", progname, text)
160 : flag_show_column
161 ? build_message_string ("%s:%d:%d: %s", s.file, s.line, s.column, text)
162 : build_message_string ("%s:%d: %s", s.file, s.line, text));
165 /* Take any action which is expected to happen after the diagnostic
166 is written out. This function does not always return. */
167 static void
168 diagnostic_action_after_output (diagnostic_context *context,
169 diagnostic_info *diagnostic)
171 switch (diagnostic->kind)
173 case DK_DEBUG:
174 case DK_NOTE:
175 case DK_ANACHRONISM:
176 case DK_WARNING:
177 break;
179 case DK_ERROR:
180 case DK_SORRY:
181 if (context->abort_on_error)
182 real_abort ();
183 if (flag_fatal_errors)
185 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
186 exit (FATAL_EXIT_CODE);
188 break;
190 case DK_ICE:
191 if (context->abort_on_error)
192 real_abort ();
194 fnotice (stderr, "Please submit a full bug report,\n"
195 "with preprocessed source if appropriate.\n"
196 "See %s for instructions.\n", bug_report_url);
197 exit (ICE_EXIT_CODE);
199 case DK_FATAL:
200 if (context->abort_on_error)
201 real_abort ();
203 fnotice (stderr, "compilation terminated.\n");
204 exit (FATAL_EXIT_CODE);
206 default:
207 gcc_unreachable ();
211 /* Prints out, if necessary, the name of the current function
212 that caused an error. Called from all error and warning functions. */
213 void
214 diagnostic_report_current_function (diagnostic_context *context,
215 diagnostic_info *diagnostic)
217 diagnostic_report_current_module (context);
218 lang_hooks.print_error_function (context, input_filename, diagnostic);
221 void
222 diagnostic_report_current_module (diagnostic_context *context)
224 const struct line_map *map;
226 if (pp_needs_newline (context->printer))
228 pp_newline (context->printer);
229 pp_needs_newline (context->printer) = false;
232 if (input_location <= BUILTINS_LOCATION)
233 return;
235 map = linemap_lookup (line_table, input_location);
236 if (map && diagnostic_last_module_changed (context, map))
238 diagnostic_set_last_module (context, map);
239 if (! MAIN_FILE_P (map))
241 map = INCLUDED_FROM (line_table, map);
242 if (flag_show_column)
243 pp_verbatim (context->printer,
244 "In file included from %s:%d:%d",
245 map->to_file,
246 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
247 else
248 pp_verbatim (context->printer,
249 "In file included from %s:%d",
250 map->to_file, LAST_SOURCE_LINE (map));
251 while (! MAIN_FILE_P (map))
253 map = INCLUDED_FROM (line_table, map);
254 pp_verbatim (context->printer,
255 ",\n from %s:%d",
256 map->to_file, LAST_SOURCE_LINE (map));
258 pp_verbatim (context->printer, ":");
259 pp_newline (context->printer);
264 void
265 default_diagnostic_starter (diagnostic_context *context,
266 diagnostic_info *diagnostic)
268 diagnostic_report_current_function (context, diagnostic);
269 pp_set_prefix (context->printer, diagnostic_build_prefix (diagnostic));
272 void
273 default_diagnostic_finalizer (diagnostic_context *context,
274 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
276 pp_destroy_prefix (context->printer);
279 /* Interface to specify diagnostic kind overrides. Returns the
280 previous setting, or DK_UNSPECIFIED if the parameters are out of
281 range. */
282 diagnostic_t
283 diagnostic_classify_diagnostic (diagnostic_context *context,
284 int option_index,
285 diagnostic_t new_kind)
287 diagnostic_t old_kind;
289 if (option_index <= 0
290 || option_index >= N_OPTS
291 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
292 return DK_UNSPECIFIED;
294 old_kind = context->classify_diagnostic[option_index];
295 context->classify_diagnostic[option_index] = new_kind;
296 return old_kind;
299 /* Report a diagnostic message (an error or a warning) as specified by
300 DC. This function is *the* subroutine in terms of which front-ends
301 should implement their specific diagnostic handling modules. The
302 front-end independent format specifiers are exactly those described
303 in the documentation of output_format.
304 Return true if a diagnostic was printed, false otherwise. */
306 bool
307 diagnostic_report_diagnostic (diagnostic_context *context,
308 diagnostic_info *diagnostic)
310 location_t location = diagnostic->location;
311 bool maybe_print_warnings_as_errors_message = false;
312 const char *saved_format_spec;
314 /* Give preference to being able to inhibit warnings, before they
315 get reclassified to something else. */
316 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
317 && !diagnostic_report_warnings_p (location))
318 return false;
320 if (diagnostic->kind == DK_NOTE && flag_compare_debug)
321 return false;
323 if (diagnostic->kind == DK_PEDWARN)
324 diagnostic->kind = pedantic_warning_kind ();
326 if (context->lock > 0)
328 /* If we're reporting an ICE in the middle of some other error,
329 try to flush out the previous error, then let this one
330 through. Don't do this more than once. */
331 if (diagnostic->kind == DK_ICE && context->lock == 1)
332 pp_flush (context->printer);
333 else
334 error_recursion (context);
337 /* If the user requested that warnings be treated as errors, so be
338 it. Note that we do this before the next block so that
339 individual warnings can be overridden back to warnings with
340 -Wno-error=*. */
341 if (context->warning_as_error_requested
342 && diagnostic->kind == DK_WARNING)
344 diagnostic->kind = DK_ERROR;
345 maybe_print_warnings_as_errors_message = true;
348 if (diagnostic->option_index)
350 /* This tests if the user provided the appropriate -Wfoo or
351 -Wno-foo option. */
352 if (! option_enabled (diagnostic->option_index))
353 return false;
354 /* This tests if the user provided the appropriate -Werror=foo
355 option. */
356 if (context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
358 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
359 maybe_print_warnings_as_errors_message = false;
361 /* This allows for future extensions, like temporarily disabling
362 warnings for ranges of source code. */
363 if (diagnostic->kind == DK_IGNORED)
364 return false;
367 /* If we changed the kind due to -Werror, and didn't override it, we
368 need to print this message. */
369 if (context->issue_warnings_are_errors_message
370 && maybe_print_warnings_as_errors_message)
372 pp_verbatim (context->printer,
373 "%s: warnings being treated as errors\n", progname);
374 context->issue_warnings_are_errors_message = false;
377 context->lock++;
379 if (diagnostic->kind == DK_ICE && plugins_active_p ())
381 fnotice (stderr, "*** WARNING *** there are active plugins, do not report"
382 " this as a bug unless you can reproduce it without enabling"
383 " any plugins.\n");
384 dump_active_plugins (stderr);
387 if (diagnostic->kind == DK_ICE)
389 #ifndef ENABLE_CHECKING
390 /* When not checking, ICEs are converted to fatal errors when an
391 error has already occurred. This is counteracted by
392 abort_on_error. */
393 if ((diagnostic_kind_count (context, DK_ERROR) > 0
394 || diagnostic_kind_count (context, DK_SORRY) > 0)
395 && !context->abort_on_error)
397 expanded_location s = expand_location (diagnostic->location);
398 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
399 s.file, s.line);
400 exit (ICE_EXIT_CODE);
402 #endif
403 if (context->internal_error)
404 (*context->internal_error) (diagnostic->message.format_spec,
405 diagnostic->message.args_ptr);
407 ++diagnostic_kind_count (context, diagnostic->kind);
409 saved_format_spec = diagnostic->message.format_spec;
410 if (context->show_option_requested && diagnostic->option_index)
411 diagnostic->message.format_spec
412 = ACONCAT ((diagnostic->message.format_spec,
413 " [", cl_options[diagnostic->option_index].opt_text, "]", NULL));
415 diagnostic->message.locus = &diagnostic->location;
416 diagnostic->message.abstract_origin = &diagnostic->abstract_origin;
417 diagnostic->abstract_origin = NULL;
418 pp_format (context->printer, &diagnostic->message);
419 (*diagnostic_starter (context)) (context, diagnostic);
420 pp_output_formatted_text (context->printer);
421 (*diagnostic_finalizer (context)) (context, diagnostic);
422 pp_flush (context->printer);
423 diagnostic_action_after_output (context, diagnostic);
424 diagnostic->message.format_spec = saved_format_spec;
425 diagnostic->abstract_origin = NULL;
427 context->lock--;
429 return true;
432 /* Given a partial pathname as input, return another pathname that
433 shares no directory elements with the pathname of __FILE__. This
434 is used by fancy_abort() to print `Internal compiler error in expr.c'
435 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
437 const char *
438 trim_filename (const char *name)
440 static const char this_file[] = __FILE__;
441 const char *p = name, *q = this_file;
443 /* First skip any "../" in each filename. This allows us to give a proper
444 reference to a file in a subdirectory. */
445 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
446 p += 3;
448 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
449 q += 3;
451 /* Now skip any parts the two filenames have in common. */
452 while (*p == *q && *p != 0 && *q != 0)
453 p++, q++;
455 /* Now go backwards until the previous directory separator. */
456 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
457 p--;
459 return p;
462 /* Standard error reporting routines in increasing order of severity.
463 All of these take arguments like printf. */
465 /* Text to be emitted verbatim to the error message stream; this
466 produces no prefix and disables line-wrapping. Use rarely. */
467 void
468 verbatim (const char *gmsgid, ...)
470 text_info text;
471 va_list ap;
473 va_start (ap, gmsgid);
474 text.err_no = errno;
475 text.args_ptr = &ap;
476 text.format_spec = _(gmsgid);
477 text.locus = NULL;
478 text.abstract_origin = NULL;
479 pp_format_verbatim (global_dc->printer, &text);
480 pp_flush (global_dc->printer);
481 va_end (ap);
484 bool
485 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
486 const char *gmsgid, ...)
488 diagnostic_info diagnostic;
489 va_list ap;
491 va_start (ap, gmsgid);
492 if (kind == DK_PERMERROR)
494 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
495 permissive_error_kind ());
496 diagnostic.option_index = OPT_fpermissive;
498 else {
499 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, kind);
500 if (kind == DK_WARNING || kind == DK_PEDWARN)
501 diagnostic.option_index = opt;
503 va_end (ap);
505 return report_diagnostic (&diagnostic);
508 /* An informative note at LOCATION. Use this for additional details on an error
509 message. */
510 void
511 inform (location_t location, const char *gmsgid, ...)
513 diagnostic_info diagnostic;
514 va_list ap;
516 va_start (ap, gmsgid);
517 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
518 report_diagnostic (&diagnostic);
519 va_end (ap);
522 /* A warning at INPUT_LOCATION. Use this for code which is correct according
523 to the relevant language specification but is likely to be buggy anyway.
524 Returns true if the warning was printed, false if it was inhibited. */
525 bool
526 warning (int opt, const char *gmsgid, ...)
528 diagnostic_info diagnostic;
529 va_list ap;
531 va_start (ap, gmsgid);
532 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
533 diagnostic.option_index = opt;
535 va_end (ap);
536 return report_diagnostic (&diagnostic);
539 /* A warning at LOCATION. Use this for code which is correct according to the
540 relevant language specification but is likely to be buggy anyway.
541 Returns true if the warning was printed, false if it was inhibited. */
543 bool
544 warning_at (location_t location, int opt, const char *gmsgid, ...)
546 diagnostic_info diagnostic;
547 va_list ap;
549 va_start (ap, gmsgid);
550 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_WARNING);
551 diagnostic.option_index = opt;
552 va_end (ap);
553 return report_diagnostic (&diagnostic);
556 /* A "pedantic" warning at LOCATION: issues a warning unless
557 -pedantic-errors was given on the command line, in which case it
558 issues an error. Use this for diagnostics required by the relevant
559 language standard, if you have chosen not to make them errors.
561 Note that these diagnostics are issued independent of the setting
562 of the -pedantic command-line switch. To get a warning enabled
563 only with that switch, use either "if (pedantic) pedwarn
564 (OPT_pedantic,...)" or just "pedwarn (OPT_pedantic,..)". To get a
565 pedwarn independently of the -pedantic switch use "pedwarn (0,...)".
567 Returns true if the warning was printed, false if it was inhibited. */
569 bool
570 pedwarn (location_t location, int opt, const char *gmsgid, ...)
572 diagnostic_info diagnostic;
573 va_list ap;
575 va_start (ap, gmsgid);
576 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_PEDWARN);
577 diagnostic.option_index = opt;
578 va_end (ap);
579 return report_diagnostic (&diagnostic);
582 /* A "permissive" error at LOCATION: issues an error unless
583 -fpermissive was given on the command line, in which case it issues
584 a warning. Use this for things that really should be errors but we
585 want to support legacy code.
587 Returns true if the warning was printed, false if it was inhibited. */
589 bool
590 permerror (location_t location, const char *gmsgid, ...)
592 diagnostic_info diagnostic;
593 va_list ap;
595 va_start (ap, gmsgid);
596 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
597 permissive_error_kind ());
598 diagnostic.option_index = OPT_fpermissive;
599 va_end (ap);
600 return report_diagnostic (&diagnostic);
603 /* A hard error: the code is definitely ill-formed, and an object file
604 will not be produced. */
605 void
606 error (const char *gmsgid, ...)
608 diagnostic_info diagnostic;
609 va_list ap;
611 va_start (ap, gmsgid);
612 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
613 report_diagnostic (&diagnostic);
614 va_end (ap);
617 /* Same as ebove, but use location LOC instead of input_location. */
618 void
619 error_at (location_t loc, const char *gmsgid, ...)
621 diagnostic_info diagnostic;
622 va_list ap;
624 va_start (ap, gmsgid);
625 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_ERROR);
626 report_diagnostic (&diagnostic);
627 va_end (ap);
630 /* "Sorry, not implemented." Use for a language feature which is
631 required by the relevant specification but not implemented by GCC.
632 An object file will not be produced. */
633 void
634 sorry (const char *gmsgid, ...)
636 diagnostic_info diagnostic;
637 va_list ap;
639 va_start (ap, gmsgid);
640 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
641 report_diagnostic (&diagnostic);
642 va_end (ap);
645 /* An error which is severe enough that we make no attempt to
646 continue. Do not use this for internal consistency checks; that's
647 internal_error. Use of this function should be rare. */
648 void
649 fatal_error (const char *gmsgid, ...)
651 diagnostic_info diagnostic;
652 va_list ap;
654 va_start (ap, gmsgid);
655 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
656 report_diagnostic (&diagnostic);
657 va_end (ap);
659 gcc_unreachable ();
662 /* An internal consistency check has failed. We make no attempt to
663 continue. Note that unless there is debugging value to be had from
664 a more specific message, or some other good reason, you should use
665 abort () instead of calling this function directly. */
666 void
667 internal_error (const char *gmsgid, ...)
669 diagnostic_info diagnostic;
670 va_list ap;
672 va_start (ap, gmsgid);
673 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
674 report_diagnostic (&diagnostic);
675 va_end (ap);
677 gcc_unreachable ();
680 /* Special case error functions. Most are implemented in terms of the
681 above, or should be. */
683 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
684 runs its second argument through gettext. */
685 void
686 fnotice (FILE *file, const char *cmsgid, ...)
688 va_list ap;
690 va_start (ap, cmsgid);
691 vfprintf (file, _(cmsgid), ap);
692 va_end (ap);
695 /* Inform the user that an error occurred while trying to report some
696 other error. This indicates catastrophic internal inconsistencies,
697 so give up now. But do try to flush out the previous error.
698 This mustn't use internal_error, that will cause infinite recursion. */
700 static void
701 error_recursion (diagnostic_context *context)
703 diagnostic_info diagnostic;
705 if (context->lock < 3)
706 pp_flush (context->printer);
708 fnotice (stderr,
709 "Internal compiler error: Error reporting routines re-entered.\n");
711 /* Call diagnostic_action_after_output to get the "please submit a bug
712 report" message. It only looks at the kind field of diagnostic_info. */
713 diagnostic.kind = DK_ICE;
714 diagnostic_action_after_output (context, &diagnostic);
716 /* Do not use gcc_unreachable here; that goes through internal_error
717 and therefore would cause infinite recursion. */
718 real_abort ();
721 /* Report an internal compiler error in a friendly manner. This is
722 the function that gets called upon use of abort() in the source
723 code generally, thanks to a special macro. */
725 void
726 fancy_abort (const char *file, int line, const char *function)
728 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
731 /* Really call the system 'abort'. This has to go right at the end of
732 this file, so that there are no functions after it that call abort
733 and get the system abort instead of our macro. */
734 #undef abort
735 static void
736 real_abort (void)
738 abort ();