Add files that I missed when importing NaCl changes earlier
[gcc/nacl-gcc.git] / gcc / diagnostic.c
blob1c5df65dc1f8462175f245eef7b902c5f2ec8844
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 if (!diagnostic_report_warnings_p ())
205 return false;
207 /* -Werror can reclassify warnings as errors, but
208 classify_diagnostic can reclassify it back to a warning. The
209 second part of this test detects that case. */
210 if (!context->warning_as_error_requested
211 || (context->classify_diagnostic[diagnostic->option_index]
212 == DK_WARNING))
214 ++diagnostic_kind_count (context, DK_WARNING);
215 break;
217 else if (context->issue_warnings_are_errors_message)
219 pp_verbatim (context->printer,
220 "%s: warnings being treated as errors\n", progname);
221 context->issue_warnings_are_errors_message = false;
224 /* And fall through. */
225 case DK_ERROR:
226 ++diagnostic_kind_count (context, DK_ERROR);
227 break;
230 return true;
233 /* Take any action which is expected to happen after the diagnostic
234 is written out. This function does not always return. */
235 static void
236 diagnostic_action_after_output (diagnostic_context *context,
237 diagnostic_info *diagnostic)
239 switch (diagnostic->kind)
241 case DK_DEBUG:
242 case DK_NOTE:
243 case DK_ANACHRONISM:
244 case DK_WARNING:
245 break;
247 case DK_ERROR:
248 case DK_SORRY:
249 if (context->abort_on_error)
250 real_abort ();
251 if (flag_fatal_errors)
253 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
254 exit (FATAL_EXIT_CODE);
256 break;
258 case DK_ICE:
259 if (context->abort_on_error)
260 real_abort ();
262 fnotice (stderr, "Please submit a full bug report,\n"
263 "with preprocessed source if appropriate.\n"
264 "See %s for instructions.\n", bug_report_url);
265 exit (ICE_EXIT_CODE);
267 case DK_FATAL:
268 if (context->abort_on_error)
269 real_abort ();
271 fnotice (stderr, "compilation terminated.\n");
272 exit (FATAL_EXIT_CODE);
274 default:
275 gcc_unreachable ();
279 /* Prints out, if necessary, the name of the current function
280 that caused an error. Called from all error and warning functions. */
281 void
282 diagnostic_report_current_function (diagnostic_context *context)
284 diagnostic_report_current_module (context);
285 lang_hooks.print_error_function (context, input_filename);
288 void
289 diagnostic_report_current_module (diagnostic_context *context)
291 struct file_stack *p;
293 if (pp_needs_newline (context->printer))
295 pp_newline (context->printer);
296 pp_needs_newline (context->printer) = false;
299 p = input_file_stack;
300 if (p && diagnostic_last_module_changed (context))
302 expanded_location xloc = expand_location (p->location);
303 pp_verbatim (context->printer,
304 "In file included from %s:%d",
305 xloc.file, xloc.line);
306 while ((p = p->next) != NULL)
308 xloc = expand_location (p->location);
309 pp_verbatim (context->printer,
310 ",\n from %s:%d",
311 xloc.file, xloc.line);
313 pp_verbatim (context->printer, ":");
314 diagnostic_set_last_module (context);
315 pp_newline (context->printer);
319 static void
320 default_diagnostic_starter (diagnostic_context *context,
321 diagnostic_info *diagnostic)
323 diagnostic_report_current_function (context);
324 pp_set_prefix (context->printer, diagnostic_build_prefix (diagnostic));
327 static void
328 default_diagnostic_finalizer (diagnostic_context *context,
329 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
331 pp_destroy_prefix (context->printer);
334 /* Interface to specify diagnostic kind overrides. Returns the
335 previous setting, or DK_UNSPECIFIED if the parameters are out of
336 range. */
337 diagnostic_t
338 diagnostic_classify_diagnostic (diagnostic_context *context,
339 int option_index,
340 diagnostic_t new_kind)
342 diagnostic_t old_kind;
344 if (option_index <= 0
345 || option_index >= N_OPTS
346 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
347 return DK_UNSPECIFIED;
349 old_kind = context->classify_diagnostic[option_index];
350 context->classify_diagnostic[option_index] = new_kind;
351 return old_kind;
354 /* Report a diagnostic message (an error or a warning) as specified by
355 DC. This function is *the* subroutine in terms of which front-ends
356 should implement their specific diagnostic handling modules. The
357 front-end independent format specifiers are exactly those described
358 in the documentation of output_format. */
360 void
361 diagnostic_report_diagnostic (diagnostic_context *context,
362 diagnostic_info *diagnostic)
364 if (context->lock > 0)
366 /* If we're reporting an ICE in the middle of some other error,
367 try to flush out the previous error, then let this one
368 through. Don't do this more than once. */
369 if (diagnostic->kind == DK_ICE && context->lock == 1)
370 pp_flush (context->printer);
371 else
372 error_recursion (context);
375 if (diagnostic->option_index)
377 /* This tests if the user provided the appropriate -Wfoo or
378 -Wno-foo option. */
379 if (! option_enabled (diagnostic->option_index))
380 return;
381 /* This tests if the user provided the appropriate -Werror=foo
382 option. */
383 if (context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
384 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
385 /* This allows for future extensions, like temporarily disabling
386 warnings for ranges of source code. */
387 if (diagnostic->kind == DK_IGNORED)
388 return;
391 context->lock++;
393 if (diagnostic_count_diagnostic (context, diagnostic))
395 const char *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 pp_format (context->printer, &diagnostic->message);
404 (*diagnostic_starter (context)) (context, diagnostic);
405 pp_output_formatted_text (context->printer);
406 (*diagnostic_finalizer (context)) (context, diagnostic);
407 pp_flush (context->printer);
408 diagnostic_action_after_output (context, diagnostic);
409 diagnostic->message.format_spec = saved_format_spec;
412 context->lock--;
415 /* Given a partial pathname as input, return another pathname that
416 shares no directory elements with the pathname of __FILE__. This
417 is used by fancy_abort() to print `Internal compiler error in expr.c'
418 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
420 const char *
421 trim_filename (const char *name)
423 static const char this_file[] = __FILE__;
424 const char *p = name, *q = this_file;
426 /* First skip any "../" in each filename. This allows us to give a proper
427 reference to a file in a subdirectory. */
428 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
429 p += 3;
431 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
432 q += 3;
434 /* Now skip any parts the two filenames have in common. */
435 while (*p == *q && *p != 0 && *q != 0)
436 p++, q++;
438 /* Now go backwards until the previous directory separator. */
439 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
440 p--;
442 return p;
445 /* Standard error reporting routines in increasing order of severity.
446 All of these take arguments like printf. */
448 /* Text to be emitted verbatim to the error message stream; this
449 produces no prefix and disables line-wrapping. Use rarely. */
450 void
451 verbatim (const char *gmsgid, ...)
453 text_info text;
454 va_list ap;
456 va_start (ap, gmsgid);
457 text.err_no = errno;
458 text.args_ptr = &ap;
459 text.format_spec = _(gmsgid);
460 text.locus = NULL;
461 pp_format_verbatim (global_dc->printer, &text);
462 pp_flush (global_dc->printer);
463 va_end (ap);
466 /* An informative note. Use this for additional details on an error
467 message. */
468 void
469 inform (const char *gmsgid, ...)
471 diagnostic_info diagnostic;
472 va_list ap;
474 va_start (ap, gmsgid);
475 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_NOTE);
476 report_diagnostic (&diagnostic);
477 va_end (ap);
480 /* A warning. Use this for code which is correct according to the
481 relevant language specification but is likely to be buggy anyway. */
482 void
483 warning (int opt, const char *gmsgid, ...)
485 diagnostic_info diagnostic;
486 va_list ap;
488 va_start (ap, gmsgid);
489 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
490 diagnostic.option_index = opt;
492 report_diagnostic (&diagnostic);
493 va_end (ap);
496 void
497 warning0 (const char *gmsgid, ...)
499 diagnostic_info diagnostic;
500 va_list ap;
502 va_start (ap, gmsgid);
503 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
504 report_diagnostic (&diagnostic);
505 va_end (ap);
508 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
509 given on the command line, in which case it issues an error. Use
510 this for diagnostics required by the relevant language standard,
511 if you have chosen not to make them errors.
513 Note that these diagnostics are issued independent of the setting
514 of the -pedantic command-line switch. To get a warning enabled
515 only with that switch, write "if (pedantic) pedwarn (...);" */
516 void
517 pedwarn (const char *gmsgid, ...)
519 diagnostic_info diagnostic;
520 va_list ap;
522 va_start (ap, gmsgid);
523 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location,
524 pedantic_error_kind ());
525 report_diagnostic (&diagnostic);
526 va_end (ap);
529 /* A hard error: the code is definitely ill-formed, and an object file
530 will not be produced. */
531 void
532 error (const char *gmsgid, ...)
534 diagnostic_info diagnostic;
535 va_list ap;
537 va_start (ap, gmsgid);
538 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
539 report_diagnostic (&diagnostic);
540 va_end (ap);
543 /* "Sorry, not implemented." Use for a language feature which is
544 required by the relevant specification but not implemented by GCC.
545 An object file will not be produced. */
546 void
547 sorry (const char *gmsgid, ...)
549 diagnostic_info diagnostic;
550 va_list ap;
552 va_start (ap, gmsgid);
553 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
554 report_diagnostic (&diagnostic);
555 va_end (ap);
558 /* An error which is severe enough that we make no attempt to
559 continue. Do not use this for internal consistency checks; that's
560 internal_error. Use of this function should be rare. */
561 void
562 fatal_error (const char *gmsgid, ...)
564 diagnostic_info diagnostic;
565 va_list ap;
567 va_start (ap, gmsgid);
568 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
569 report_diagnostic (&diagnostic);
570 va_end (ap);
572 gcc_unreachable ();
575 /* An internal consistency check has failed. We make no attempt to
576 continue. Note that unless there is debugging value to be had from
577 a more specific message, or some other good reason, you should use
578 abort () instead of calling this function directly. */
579 void
580 internal_error (const char *gmsgid, ...)
582 diagnostic_info diagnostic;
583 va_list ap;
585 va_start (ap, gmsgid);
586 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
587 report_diagnostic (&diagnostic);
588 va_end (ap);
590 gcc_unreachable ();
593 /* Special case error functions. Most are implemented in terms of the
594 above, or should be. */
596 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
597 runs its second argument through gettext. */
598 void
599 fnotice (FILE *file, const char *cmsgid, ...)
601 va_list ap;
603 va_start (ap, cmsgid);
604 vfprintf (file, _(cmsgid), ap);
605 va_end (ap);
608 /* Inform the user that an error occurred while trying to report some
609 other error. This indicates catastrophic internal inconsistencies,
610 so give up now. But do try to flush out the previous error.
611 This mustn't use internal_error, that will cause infinite recursion. */
613 static void
614 error_recursion (diagnostic_context *context)
616 diagnostic_info diagnostic;
618 if (context->lock < 3)
619 pp_flush (context->printer);
621 fnotice (stderr,
622 "Internal compiler error: Error reporting routines re-entered.\n");
624 /* Call diagnostic_action_after_output to get the "please submit a bug
625 report" message. It only looks at the kind field of diagnostic_info. */
626 diagnostic.kind = DK_ICE;
627 diagnostic_action_after_output (context, &diagnostic);
629 /* Do not use gcc_unreachable here; that goes through internal_error
630 and therefore would cause infinite recursion. */
631 real_abort ();
634 /* Report an internal compiler error in a friendly manner. This is
635 the function that gets called upon use of abort() in the source
636 code generally, thanks to a special macro. */
638 void
639 fancy_abort (const char *file, int line, const char *function)
641 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
644 /* Really call the system 'abort'. This has to go right at the end of
645 this file, so that there are no functions after it that call abort
646 and get the system abort instead of our macro. */
647 #undef abort
648 static void
649 real_abort (void)
651 abort ();