* config/rs6000/rs6000.md (define_attr "type"): Add two and three.
[official-gcc.git] / gcc / diagnostic.c
blobc25c0ec547279c38192b08d034845833d9024d66
1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004
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, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, 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"
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;
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 = xmalloc (sizeof (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->prefixing_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 context->abort_on_error = false;
105 context->internal_error = NULL;
106 diagnostic_starter (context) = default_diagnostic_starter;
107 diagnostic_finalizer (context) = default_diagnostic_finalizer;
108 context->last_module = 0;
109 context->last_function = NULL;
110 context->lock = 0;
113 void
114 diagnostic_set_info (diagnostic_info *diagnostic, const char *msgid,
115 va_list *args, location_t location,
116 diagnostic_t kind)
118 diagnostic->message.err_no = errno;
119 diagnostic->message.args_ptr = args;
120 diagnostic->message.format_spec = _(msgid);
121 diagnostic->location = location;
122 diagnostic->kind = kind;
125 /* Return a malloc'd string describing a location. The caller is
126 responsible for freeing the memory. */
127 char *
128 diagnostic_build_prefix (diagnostic_info *diagnostic)
130 static const char *const diagnostic_kind_text[] = {
131 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
132 #include "diagnostic.def"
133 #undef DEFINE_DIAGNOSTIC_KIND
134 "must-not-happen"
136 expanded_location s = expand_location (diagnostic->location);
137 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
139 return s.file
140 ? build_message_string ("%s:%d: %s",
141 s.file, s.line,
142 _(diagnostic_kind_text[diagnostic->kind]))
143 : build_message_string ("%s: %s", progname,
144 _(diagnostic_kind_text[diagnostic->kind]));
147 /* Count a diagnostic. Return true if the message should be printed. */
148 static bool
149 diagnostic_count_diagnostic (diagnostic_context *context,
150 diagnostic_info *diagnostic)
152 diagnostic_t kind = diagnostic->kind;
153 switch (kind)
155 default:
156 gcc_unreachable ();
158 case DK_ICE:
159 #ifndef ENABLE_CHECKING
160 /* When not checking, ICEs are converted to fatal errors when an
161 error has already occurred. This is counteracted by
162 abort_on_error. */
163 if ((diagnostic_kind_count (context, DK_ERROR) > 0
164 || diagnostic_kind_count (context, DK_SORRY) > 0)
165 && !context->abort_on_error)
167 expanded_location s = expand_location (diagnostic->location);
168 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
169 s.file, s.line);
170 exit (FATAL_EXIT_CODE);
172 #endif
173 if (context->internal_error)
174 (*context->internal_error) (diagnostic->message.format_spec,
175 diagnostic->message.args_ptr);
176 /* Fall through. */
178 case DK_FATAL: case DK_SORRY:
179 case DK_ANACHRONISM: case DK_NOTE:
180 ++diagnostic_kind_count (context, kind);
181 break;
183 case DK_WARNING:
184 if (!diagnostic_report_warnings_p ())
185 return false;
187 if (!context->warning_as_error_requested)
189 ++diagnostic_kind_count (context, DK_WARNING);
190 break;
192 else if (context->issue_warnings_are_errors_message)
194 pp_verbatim (context->printer,
195 "%s: warnings being treated as errors\n", progname);
196 context->issue_warnings_are_errors_message = false;
199 /* And fall through. */
200 case DK_ERROR:
201 ++diagnostic_kind_count (context, DK_ERROR);
202 break;
205 return true;
208 /* Take any action which is expected to happen after the diagnostic
209 is written out. This function does not always return. */
210 static void
211 diagnostic_action_after_output (diagnostic_context *context,
212 diagnostic_info *diagnostic)
214 switch (diagnostic->kind)
216 case DK_DEBUG:
217 case DK_NOTE:
218 case DK_ANACHRONISM:
219 case DK_WARNING:
220 break;
222 case DK_ERROR:
223 case DK_SORRY:
224 if (context->abort_on_error)
225 real_abort ();
226 if (flag_fatal_errors)
228 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
229 exit (FATAL_EXIT_CODE);
231 break;
233 case DK_ICE:
234 if (context->abort_on_error)
235 real_abort ();
237 fnotice (stderr, "Please submit a full bug report,\n"
238 "with preprocessed source if appropriate.\n"
239 "See %s for instructions.\n", bug_report_url);
240 exit (FATAL_EXIT_CODE);
242 case DK_FATAL:
243 if (context->abort_on_error)
244 real_abort ();
246 fnotice (stderr, "compilation terminated.\n");
247 exit (FATAL_EXIT_CODE);
249 default:
250 gcc_unreachable ();
254 /* Prints out, if necessary, the name of the current function
255 that caused an error. Called from all error and warning functions. */
256 void
257 diagnostic_report_current_function (diagnostic_context *context)
259 diagnostic_report_current_module (context);
260 lang_hooks.print_error_function (context, input_filename);
263 void
264 diagnostic_report_current_module (diagnostic_context *context)
266 struct file_stack *p;
268 if (pp_needs_newline (context->printer))
270 pp_newline (context->printer);
271 pp_needs_newline (context->printer) = false;
274 p = input_file_stack;
275 if (p && diagnostic_last_module_changed (context))
277 expanded_location xloc = expand_location (p->location);
278 pp_verbatim (context->printer,
279 "In file included from %s:%d",
280 xloc.file, xloc.line);
281 while ((p = p->next) != NULL)
283 xloc = expand_location (p->location);
284 pp_verbatim (context->printer,
285 ",\n from %s:%d",
286 xloc.file, xloc.line);
288 pp_verbatim (context->printer, ":\n");
289 diagnostic_set_last_module (context);
293 static void
294 default_diagnostic_starter (diagnostic_context *context,
295 diagnostic_info *diagnostic)
297 diagnostic_report_current_function (context);
298 pp_set_prefix (context->printer, diagnostic_build_prefix (diagnostic));
301 static void
302 default_diagnostic_finalizer (diagnostic_context *context,
303 diagnostic_info *diagnostic __attribute__((unused)))
305 pp_destroy_prefix (context->printer);
308 /* Report a diagnostic message (an error or a warning) as specified by
309 DC. This function is *the* subroutine in terms of which front-ends
310 should implement their specific diagnostic handling modules. The
311 front-end independent format specifiers are exactly those described
312 in the documentation of output_format. */
314 void
315 diagnostic_report_diagnostic (diagnostic_context *context,
316 diagnostic_info *diagnostic)
318 if (context->lock > 0)
320 /* If we're reporting an ICE in the middle of some other error,
321 try to flush out the previous error, then let this one
322 through. Don't do this more than once. */
323 if (diagnostic->kind == DK_ICE && context->lock == 1)
324 pp_flush (context->printer);
325 else
326 error_recursion (context);
329 context->lock++;
331 if (diagnostic_count_diagnostic (context, diagnostic))
333 pp_prepare_to_format (context->printer, &diagnostic->message,
334 &diagnostic->location);
335 (*diagnostic_starter (context)) (context, diagnostic);
336 pp_format_text (context->printer, &diagnostic->message);
337 (*diagnostic_finalizer (context)) (context, diagnostic);
338 pp_flush (context->printer);
339 diagnostic_action_after_output (context, diagnostic);
342 context->lock--;
345 /* Given a partial pathname as input, return another pathname that
346 shares no directory elements with the pathname of __FILE__. This
347 is used by fancy_abort() to print `Internal compiler error in expr.c'
348 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
350 const char *
351 trim_filename (const char *name)
353 static const char this_file[] = __FILE__;
354 const char *p = name, *q = this_file;
356 /* First skip any "../" in each filename. This allows us to give a proper
357 reference to a file in a subdirectory. */
358 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
359 p += 3;
361 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
362 q += 3;
364 /* Now skip any parts the two filenames have in common. */
365 while (*p == *q && *p != 0 && *q != 0)
366 p++, q++;
368 /* Now go backwards until the previous directory separator. */
369 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
370 p--;
372 return p;
375 /* Standard error reporting routines in increasing order of severity.
376 All of these take arguments like printf. */
378 /* Text to be emitted verbatim to the error message stream; this
379 produces no prefix and disables line-wrapping. Use rarely. */
380 void
381 verbatim (const char *msgid, ...)
383 text_info text;
384 va_list ap;
386 va_start (ap, msgid);
387 text.err_no = errno;
388 text.args_ptr = &ap;
389 text.format_spec = _(msgid);
390 pp_format_verbatim (global_dc->printer, &text);
391 pp_flush (global_dc->printer);
392 va_end (ap);
395 /* An informative note. Use this for additional details on an error
396 message. */
397 void
398 inform (const char *msgid, ...)
400 diagnostic_info diagnostic;
401 va_list ap;
403 va_start (ap, msgid);
404 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_NOTE);
405 report_diagnostic (&diagnostic);
406 va_end (ap);
409 /* A warning. Use this for code which is correct according to the
410 relevant language specification but is likely to be buggy anyway. */
411 void
412 warning (const char *msgid, ...)
414 diagnostic_info diagnostic;
415 va_list ap;
417 va_start (ap, msgid);
418 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_WARNING);
419 report_diagnostic (&diagnostic);
420 va_end (ap);
423 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
424 given on the command line, in which case it issues an error. Use
425 this for diagnostics required by the relevant language standard,
426 if you have chosen not to make them errors.
428 Note that these diagnostics are issued independent of the setting
429 of the -pedantic command-line switch. To get a warning enabled
430 only with that switch, write "if (pedantic) pedwarn (...);" */
431 void
432 pedwarn (const char *msgid, ...)
434 diagnostic_info diagnostic;
435 va_list ap;
437 va_start (ap, msgid);
438 diagnostic_set_info (&diagnostic, msgid, &ap, input_location,
439 pedantic_error_kind ());
440 report_diagnostic (&diagnostic);
441 va_end (ap);
444 /* A hard error: the code is definitely ill-formed, and an object file
445 will not be produced. */
446 void
447 error (const char *msgid, ...)
449 diagnostic_info diagnostic;
450 va_list ap;
452 va_start (ap, msgid);
453 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_ERROR);
454 report_diagnostic (&diagnostic);
455 va_end (ap);
458 /* "Sorry, not implemented." Use for a language feature which is
459 required by the relevant specification but not implemented by GCC.
460 An object file will not be produced. */
461 void
462 sorry (const char *msgid, ...)
464 diagnostic_info diagnostic;
465 va_list ap;
467 va_start (ap, msgid);
468 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_SORRY);
469 report_diagnostic (&diagnostic);
470 va_end (ap);
473 /* An error which is severe enough that we make no attempt to
474 continue. Do not use this for internal consistency checks; that's
475 internal_error. Use of this function should be rare. */
476 void
477 fatal_error (const char *msgid, ...)
479 diagnostic_info diagnostic;
480 va_list ap;
482 va_start (ap, msgid);
483 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_FATAL);
484 report_diagnostic (&diagnostic);
485 va_end (ap);
487 gcc_unreachable ();
490 /* An internal consistency check has failed. We make no attempt to
491 continue. Note that unless there is debugging value to be had from
492 a more specific message, or some other good reason, you should use
493 abort () instead of calling this function directly. */
494 void
495 internal_error (const char *msgid, ...)
497 diagnostic_info diagnostic;
498 va_list ap;
500 va_start (ap, msgid);
501 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_ICE);
502 report_diagnostic (&diagnostic);
503 va_end (ap);
505 gcc_unreachable ();
508 /* Special case error functions. Most are implemented in terms of the
509 above, or should be. */
511 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
512 runs its second argument through gettext. */
513 void
514 fnotice (FILE *file, const char *msgid, ...)
516 va_list ap;
518 va_start (ap, msgid);
519 vfprintf (file, _(msgid), ap);
520 va_end (ap);
523 /* Inform the user that an error occurred while trying to report some
524 other error. This indicates catastrophic internal inconsistencies,
525 so give up now. But do try to flush out the previous error.
526 This mustn't use internal_error, that will cause infinite recursion. */
528 static void
529 error_recursion (diagnostic_context *context)
531 diagnostic_info diagnostic;
533 if (context->lock < 3)
534 pp_flush (context->printer);
536 fnotice (stderr,
537 "Internal compiler error: Error reporting routines re-entered.\n");
539 /* Call diagnostic_action_after_output to get the "please submit a bug
540 report" message. It only looks at the kind field of diagnostic_info. */
541 diagnostic.kind = DK_ICE;
542 diagnostic_action_after_output (context, &diagnostic);
544 /* Do not use gcc_unreachable here; that goes through internal_error
545 and therefore would cause infinite recursion. */
546 real_abort ();
549 /* Report an internal compiler error in a friendly manner. This is
550 the function that gets called upon use of abort() in the source
551 code generally, thanks to a special macro. */
553 void
554 fancy_abort (const char *file, int line, const char *function)
556 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
559 /* Really call the system 'abort'. This has to go right at the end of
560 this file, so that there are no functions after it that call abort
561 and get the system abort instead of our macro. */
562 #undef abort
563 static void
564 real_abort (void)
566 abort ();