expmed.c (expand_divmod): Always use a comparison for a division by a large unsigned...
[official-gcc.git] / gcc / diagnostic.c
blob8d012491577ebd38c55c4919d9e35e886ae34868
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"
44 #define pedantic_warning_kind() (flag_pedantic_errors ? DK_ERROR : DK_WARNING)
45 #define permissive_error_kind() (flag_permissive ? DK_WARNING : DK_ERROR)
47 /* Prototypes. */
48 static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
50 static void default_diagnostic_starter (diagnostic_context *,
51 diagnostic_info *);
52 static void default_diagnostic_finalizer (diagnostic_context *,
53 diagnostic_info *);
55 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
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->override_column = 0;
130 diagnostic->kind = kind;
131 diagnostic->option_index = 0;
134 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
135 translated. */
136 void
137 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
138 va_list *args, location_t location,
139 diagnostic_t kind)
141 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
144 /* Return a malloc'd string describing a location. The caller is
145 responsible for freeing the memory. */
146 char *
147 diagnostic_build_prefix (diagnostic_info *diagnostic)
149 static const char *const diagnostic_kind_text[] = {
150 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
151 #include "diagnostic.def"
152 #undef DEFINE_DIAGNOSTIC_KIND
153 "must-not-happen"
155 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
156 expanded_location s = expand_location (diagnostic->location);
157 if (diagnostic->override_column)
158 s.column = diagnostic->override_column;
159 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
161 return
162 (s.file == NULL
163 ? build_message_string ("%s: %s", progname, text)
164 : flag_show_column && s.column != 0
165 ? build_message_string ("%s:%d:%d: %s", s.file, s.line, s.column, text)
166 : build_message_string ("%s:%d: %s", s.file, s.line, text));
169 /* Take any action which is expected to happen after the diagnostic
170 is written out. This function does not always return. */
171 static void
172 diagnostic_action_after_output (diagnostic_context *context,
173 diagnostic_info *diagnostic)
175 switch (diagnostic->kind)
177 case DK_DEBUG:
178 case DK_NOTE:
179 case DK_ANACHRONISM:
180 case DK_WARNING:
181 break;
183 case DK_ERROR:
184 case DK_SORRY:
185 if (context->abort_on_error)
186 real_abort ();
187 if (flag_fatal_errors)
189 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
190 exit (FATAL_EXIT_CODE);
192 break;
194 case DK_ICE:
195 if (context->abort_on_error)
196 real_abort ();
198 fnotice (stderr, "Please submit a full bug report,\n"
199 "with preprocessed source if appropriate.\n"
200 "See %s for instructions.\n", bug_report_url);
201 exit (ICE_EXIT_CODE);
203 case DK_FATAL:
204 if (context->abort_on_error)
205 real_abort ();
207 fnotice (stderr, "compilation terminated.\n");
208 exit (FATAL_EXIT_CODE);
210 default:
211 gcc_unreachable ();
215 /* Prints out, if necessary, the name of the current function
216 that caused an error. Called from all error and warning functions. */
217 void
218 diagnostic_report_current_function (diagnostic_context *context,
219 diagnostic_info *diagnostic)
221 diagnostic_report_current_module (context);
222 lang_hooks.print_error_function (context, input_filename, diagnostic);
225 void
226 diagnostic_report_current_module (diagnostic_context *context)
228 const struct line_map *map;
230 if (pp_needs_newline (context->printer))
232 pp_newline (context->printer);
233 pp_needs_newline (context->printer) = false;
236 if (input_location <= BUILTINS_LOCATION)
237 return;
239 map = linemap_lookup (line_table, input_location);
240 if (map && diagnostic_last_module_changed (context, map))
242 diagnostic_set_last_module (context, map);
243 if (! MAIN_FILE_P (map))
245 map = INCLUDED_FROM (line_table, map);
246 pp_verbatim (context->printer,
247 "In file included from %s:%d",
248 map->to_file, LAST_SOURCE_LINE (map));
249 while (! MAIN_FILE_P (map))
251 map = INCLUDED_FROM (line_table, map);
252 pp_verbatim (context->printer,
253 ",\n from %s:%d",
254 map->to_file, LAST_SOURCE_LINE (map));
256 pp_verbatim (context->printer, ":");
257 pp_newline (context->printer);
262 static void
263 default_diagnostic_starter (diagnostic_context *context,
264 diagnostic_info *diagnostic)
266 diagnostic_report_current_function (context, diagnostic);
267 pp_set_prefix (context->printer, diagnostic_build_prefix (diagnostic));
270 static void
271 default_diagnostic_finalizer (diagnostic_context *context,
272 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
274 pp_destroy_prefix (context->printer);
277 /* Interface to specify diagnostic kind overrides. Returns the
278 previous setting, or DK_UNSPECIFIED if the parameters are out of
279 range. */
280 diagnostic_t
281 diagnostic_classify_diagnostic (diagnostic_context *context,
282 int option_index,
283 diagnostic_t new_kind)
285 diagnostic_t old_kind;
287 if (option_index <= 0
288 || option_index >= N_OPTS
289 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
290 return DK_UNSPECIFIED;
292 old_kind = context->classify_diagnostic[option_index];
293 context->classify_diagnostic[option_index] = new_kind;
294 return old_kind;
297 /* Report a diagnostic message (an error or a warning) as specified by
298 DC. This function is *the* subroutine in terms of which front-ends
299 should implement their specific diagnostic handling modules. The
300 front-end independent format specifiers are exactly those described
301 in the documentation of output_format.
302 Return true if a diagnostic was printed, false otherwise. */
304 bool
305 diagnostic_report_diagnostic (diagnostic_context *context,
306 diagnostic_info *diagnostic)
308 location_t location = diagnostic->location;
309 bool maybe_print_warnings_as_errors_message = false;
310 const char *saved_format_spec;
312 /* Give preference to being able to inhibit warnings, before they
313 get reclassified to something else. */
314 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
315 && !diagnostic_report_warnings_p (location))
316 return false;
318 if (diagnostic->kind == DK_PEDWARN)
319 diagnostic->kind = pedantic_warning_kind ();
321 if (context->lock > 0)
323 /* If we're reporting an ICE in the middle of some other error,
324 try to flush out the previous error, then let this one
325 through. Don't do this more than once. */
326 if (diagnostic->kind == DK_ICE && context->lock == 1)
327 pp_flush (context->printer);
328 else
329 error_recursion (context);
332 /* If the user requested that warnings be treated as errors, so be
333 it. Note that we do this before the next block so that
334 individual warnings can be overridden back to warnings with
335 -Wno-error=*. */
336 if (context->warning_as_error_requested
337 && diagnostic->kind == DK_WARNING)
339 diagnostic->kind = DK_ERROR;
340 maybe_print_warnings_as_errors_message = true;
343 if (diagnostic->option_index)
345 /* This tests if the user provided the appropriate -Wfoo or
346 -Wno-foo option. */
347 if (! option_enabled (diagnostic->option_index))
348 return false;
349 /* This tests if the user provided the appropriate -Werror=foo
350 option. */
351 if (context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
353 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
354 maybe_print_warnings_as_errors_message = false;
356 /* This allows for future extensions, like temporarily disabling
357 warnings for ranges of source code. */
358 if (diagnostic->kind == DK_IGNORED)
359 return false;
362 /* If we changed the kind due to -Werror, and didn't override it, we
363 need to print this message. */
364 if (context->issue_warnings_are_errors_message
365 && maybe_print_warnings_as_errors_message)
367 pp_verbatim (context->printer,
368 "%s: warnings being treated as errors\n", progname);
369 context->issue_warnings_are_errors_message = false;
372 context->lock++;
374 if (diagnostic->kind == DK_ICE)
376 #ifndef ENABLE_CHECKING
377 /* When not checking, ICEs are converted to fatal errors when an
378 error has already occurred. This is counteracted by
379 abort_on_error. */
380 if ((diagnostic_kind_count (context, DK_ERROR) > 0
381 || diagnostic_kind_count (context, DK_SORRY) > 0)
382 && !context->abort_on_error)
384 expanded_location s = expand_location (diagnostic->location);
385 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
386 s.file, s.line);
387 exit (ICE_EXIT_CODE);
389 #endif
390 if (context->internal_error)
391 (*context->internal_error) (diagnostic->message.format_spec,
392 diagnostic->message.args_ptr);
394 ++diagnostic_kind_count (context, diagnostic->kind);
396 saved_format_spec = diagnostic->message.format_spec;
397 if (context->show_option_requested && diagnostic->option_index)
398 diagnostic->message.format_spec
399 = ACONCAT ((diagnostic->message.format_spec,
400 " [", cl_options[diagnostic->option_index].opt_text, "]", NULL));
402 diagnostic->message.locus = &diagnostic->location;
403 diagnostic->message.abstract_origin = &diagnostic->abstract_origin;
404 diagnostic->abstract_origin = NULL;
405 pp_format (context->printer, &diagnostic->message);
406 (*diagnostic_starter (context)) (context, diagnostic);
407 pp_output_formatted_text (context->printer);
408 (*diagnostic_finalizer (context)) (context, diagnostic);
409 pp_flush (context->printer);
410 diagnostic_action_after_output (context, diagnostic);
411 diagnostic->message.format_spec = saved_format_spec;
412 diagnostic->abstract_origin = NULL;
414 context->lock--;
416 return true;
419 /* Given a partial pathname as input, return another pathname that
420 shares no directory elements with the pathname of __FILE__. This
421 is used by fancy_abort() to print `Internal compiler error in expr.c'
422 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
424 const char *
425 trim_filename (const char *name)
427 static const char this_file[] = __FILE__;
428 const char *p = name, *q = this_file;
430 /* First skip any "../" in each filename. This allows us to give a proper
431 reference to a file in a subdirectory. */
432 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
433 p += 3;
435 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
436 q += 3;
438 /* Now skip any parts the two filenames have in common. */
439 while (*p == *q && *p != 0 && *q != 0)
440 p++, q++;
442 /* Now go backwards until the previous directory separator. */
443 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
444 p--;
446 return p;
449 /* Standard error reporting routines in increasing order of severity.
450 All of these take arguments like printf. */
452 /* Text to be emitted verbatim to the error message stream; this
453 produces no prefix and disables line-wrapping. Use rarely. */
454 void
455 verbatim (const char *gmsgid, ...)
457 text_info text;
458 va_list ap;
460 va_start (ap, gmsgid);
461 text.err_no = errno;
462 text.args_ptr = &ap;
463 text.format_spec = _(gmsgid);
464 text.locus = NULL;
465 text.abstract_origin = NULL;
466 pp_format_verbatim (global_dc->printer, &text);
467 pp_flush (global_dc->printer);
468 va_end (ap);
471 bool
472 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
473 const char *gmsgid, ...)
475 diagnostic_info diagnostic;
476 va_list ap;
478 va_start (ap, gmsgid);
479 if (kind == DK_PERMERROR)
481 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
482 permissive_error_kind ());
483 diagnostic.option_index = OPT_fpermissive;
485 else {
486 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, kind);
487 if (kind == DK_WARNING || kind == DK_PEDWARN)
488 diagnostic.option_index = opt;
490 va_end (ap);
492 return report_diagnostic (&diagnostic);
495 /* An informative note at LOCATION. Use this for additional details on an error
496 message. */
497 void
498 inform (location_t location, const char *gmsgid, ...)
500 diagnostic_info diagnostic;
501 va_list ap;
503 va_start (ap, gmsgid);
504 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
505 report_diagnostic (&diagnostic);
506 va_end (ap);
509 /* A warning at INPUT_LOCATION. Use this for code which is correct according
510 to the relevant language specification but is likely to be buggy anyway.
511 Returns true if the warning was printed, false if it was inhibited. */
512 bool
513 warning (int opt, const char *gmsgid, ...)
515 diagnostic_info diagnostic;
516 va_list ap;
518 va_start (ap, gmsgid);
519 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
520 diagnostic.option_index = opt;
522 va_end (ap);
523 return report_diagnostic (&diagnostic);
526 /* A warning at LOCATION. Use this for code which is correct according to the
527 relevant language specification but is likely to be buggy anyway.
528 Returns true if the warning was printed, false if it was inhibited. */
530 bool
531 warning_at (location_t location, int opt, const char *gmsgid, ...)
533 diagnostic_info diagnostic;
534 va_list ap;
536 va_start (ap, gmsgid);
537 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_WARNING);
538 diagnostic.option_index = opt;
539 va_end (ap);
540 return report_diagnostic (&diagnostic);
543 /* A "pedantic" warning at LOCATION: issues a warning unless
544 -pedantic-errors was given on the command line, in which case it
545 issues an error. Use this for diagnostics required by the relevant
546 language standard, if you have chosen not to make them errors.
548 Note that these diagnostics are issued independent of the setting
549 of the -pedantic command-line switch. To get a warning enabled
550 only with that switch, use either "if (pedantic) pedwarn
551 (OPT_pedantic,...)" or just "pedwarn (OPT_pedantic,..)". To get a
552 pedwarn independently of the -pedantic switch use "pedwarn (0,...)".
554 Returns true if the warning was printed, false if it was inhibited. */
556 bool
557 pedwarn (location_t location, int opt, const char *gmsgid, ...)
559 diagnostic_info diagnostic;
560 va_list ap;
562 va_start (ap, gmsgid);
563 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_PEDWARN);
564 diagnostic.option_index = opt;
565 va_end (ap);
566 return report_diagnostic (&diagnostic);
569 /* A "permissive" error at LOCATION: issues an error unless
570 -fpermissive was given on the command line, in which case it issues
571 a warning. Use this for things that really should be errors but we
572 want to support legacy code.
574 Returns true if the warning was printed, false if it was inhibited. */
576 bool
577 permerror (location_t location, const char *gmsgid, ...)
579 diagnostic_info diagnostic;
580 va_list ap;
582 va_start (ap, gmsgid);
583 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
584 permissive_error_kind ());
585 diagnostic.option_index = OPT_fpermissive;
586 va_end (ap);
587 return report_diagnostic (&diagnostic);
590 /* A hard error: the code is definitely ill-formed, and an object file
591 will not be produced. */
592 void
593 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_ERROR);
600 report_diagnostic (&diagnostic);
601 va_end (ap);
604 /* Same as ebove, but use location LOC instead of input_location. */
605 void
606 error_at (location_t loc, const char *gmsgid, ...)
608 diagnostic_info diagnostic;
609 va_list ap;
611 va_start (ap, gmsgid);
612 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_ERROR);
613 report_diagnostic (&diagnostic);
614 va_end (ap);
617 /* "Sorry, not implemented." Use for a language feature which is
618 required by the relevant specification but not implemented by GCC.
619 An object file will not be produced. */
620 void
621 sorry (const char *gmsgid, ...)
623 diagnostic_info diagnostic;
624 va_list ap;
626 va_start (ap, gmsgid);
627 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
628 report_diagnostic (&diagnostic);
629 va_end (ap);
632 /* An error which is severe enough that we make no attempt to
633 continue. Do not use this for internal consistency checks; that's
634 internal_error. Use of this function should be rare. */
635 void
636 fatal_error (const char *gmsgid, ...)
638 diagnostic_info diagnostic;
639 va_list ap;
641 va_start (ap, gmsgid);
642 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
643 report_diagnostic (&diagnostic);
644 va_end (ap);
646 gcc_unreachable ();
649 /* An internal consistency check has failed. We make no attempt to
650 continue. Note that unless there is debugging value to be had from
651 a more specific message, or some other good reason, you should use
652 abort () instead of calling this function directly. */
653 void
654 internal_error (const char *gmsgid, ...)
656 diagnostic_info diagnostic;
657 va_list ap;
659 va_start (ap, gmsgid);
660 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
661 report_diagnostic (&diagnostic);
662 va_end (ap);
664 gcc_unreachable ();
667 /* Special case error functions. Most are implemented in terms of the
668 above, or should be. */
670 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
671 runs its second argument through gettext. */
672 void
673 fnotice (FILE *file, const char *cmsgid, ...)
675 va_list ap;
677 va_start (ap, cmsgid);
678 vfprintf (file, _(cmsgid), ap);
679 va_end (ap);
682 /* Inform the user that an error occurred while trying to report some
683 other error. This indicates catastrophic internal inconsistencies,
684 so give up now. But do try to flush out the previous error.
685 This mustn't use internal_error, that will cause infinite recursion. */
687 static void
688 error_recursion (diagnostic_context *context)
690 diagnostic_info diagnostic;
692 if (context->lock < 3)
693 pp_flush (context->printer);
695 fnotice (stderr,
696 "Internal compiler error: Error reporting routines re-entered.\n");
698 /* Call diagnostic_action_after_output to get the "please submit a bug
699 report" message. It only looks at the kind field of diagnostic_info. */
700 diagnostic.kind = DK_ICE;
701 diagnostic_action_after_output (context, &diagnostic);
703 /* Do not use gcc_unreachable here; that goes through internal_error
704 and therefore would cause infinite recursion. */
705 real_abort ();
708 /* Report an internal compiler error in a friendly manner. This is
709 the function that gets called upon use of abort() in the source
710 code generally, thanks to a special macro. */
712 void
713 fancy_abort (const char *file, int line, const char *function)
715 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
718 /* Really call the system 'abort'. This has to go right at the end of
719 this file, so that there are no functions after it that call abort
720 and get the system abort instead of our macro. */
721 #undef abort
722 static void
723 real_abort (void)
725 abort ();