]> gcc.gnu.org Git - gcc.git/blame - gcc/diagnostic.c
pretty-print.c: Include tree.h.
[gcc.git] / gcc / diagnostic.c
CommitLineData
47b69537 1/* Language-independent diagnostic subroutines for the GNU Compiler Collection
88462c42
KH
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
406a65d0 4 Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
6ed551b4 5
1322177d 6This file is part of GCC.
6ed551b4 7
1322177d
LB
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 2, or (at your option) any later
11version.
6ed551b4 12
1322177d
LB
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
6ed551b4
GDR
17
18You should have received a copy of the GNU General Public License
1322177d
LB
19along with GCC; see the file COPYING. If not, write to the Free
20Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2102111-1307, USA. */
6ed551b4
GDR
22
23
4eb00163 24/* This file implements the language independent aspect of diagnostic
6ed551b4
GDR
25 message module. */
26
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"
4977bab6
ZW
31#include "coretypes.h"
32#include "tm.h"
6ed551b4 33#include "tree.h"
a757585a 34#include "version.h"
6ed551b4
GDR
35#include "tm_p.h"
36#include "flags.h"
37#include "input.h"
6ed551b4
GDR
38#include "toplev.h"
39#include "intl.h"
345ed1fe 40#include "diagnostic.h"
7afff7cf 41#include "langhooks.h"
b18101c7 42#include "langhooks-def.h"
6ed551b4 43
c707a408 44
30f7a378 45/* Prototypes. */
b6fe0bb8 46static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
79a490a9
AJ
47
48static void default_diagnostic_starter (diagnostic_context *,
49 diagnostic_info *);
50static void default_diagnostic_finalizer (diagnostic_context *,
51 diagnostic_info *);
52
53static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
79a490a9
AJ
54static bool diagnostic_count_diagnostic (diagnostic_context *,
55 diagnostic_info *);
56static void diagnostic_action_after_output (diagnostic_context *,
57 diagnostic_info *);
58static void real_abort (void) ATTRIBUTE_NORETURN;
fbfc1192 59
f68fc4db
GDR
60/* A diagnostic_context surrogate for stderr. */
61static diagnostic_context global_diagnostic_context;
62diagnostic_context *global_dc = &global_diagnostic_context;
6ed551b4 63\f
59650e48
ZW
64/* Return a malloc'd string containing MSG formatted a la printf. The
65 caller is responsible for freeing the memory. */
36244024 66static char *
e34d07f2 67build_message_string (const char *msg, ...)
b903d81e 68{
36244024 69 char *str;
e34d07f2 70 va_list ap;
b903d81e 71
e34d07f2 72 va_start (ap, msg);
59650e48 73 vasprintf (&str, msg, ap);
e34d07f2 74 va_end (ap);
b903d81e
GDR
75
76 return str;
77}
78
95bd1dd7 79/* Same as diagnostic_build_prefix, but only the source FILE is given. */
24805e80 80char *
79a490a9 81file_name_as_prefix (const char *f)
24805e80
GDR
82{
83 return build_message_string ("%s: ", f);
84}
85
47b69537 86
6ed551b4 87\f
47b69537
GDR
88/* Initialize the diagnostic message outputting machinery. */
89void
79a490a9 90diagnostic_initialize (diagnostic_context *context)
47b69537 91{
b6fe0bb8
GDR
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);
47b69537 96 /* By default, diagnostics are sent to stderr. */
b6fe0bb8 97 context->printer->buffer->stream = stderr;
47b69537 98 /* By default, we emit prefixes once per message. */
b6fe0bb8 99 context->printer->prefixing_rule = DIAGNOSTICS_SHOW_PREFIX_ONCE;
47b69537 100
b6fe0bb8 101 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
7783b402
GDR
102 context->issue_warnings_are_errors_message = true;
103 context->warning_as_error_requested = false;
b6fe0bb8
GDR
104 context->abort_on_error = false;
105 context->internal_error = NULL;
47b69537
GDR
106 diagnostic_starter (context) = default_diagnostic_starter;
107 diagnostic_finalizer (context) = default_diagnostic_finalizer;
b6fe0bb8
GDR
108 context->last_module = 0;
109 context->last_function = NULL;
110 context->lock = 0;
47b69537
GDR
111}
112
113void
79a490a9 114diagnostic_set_info (diagnostic_info *diagnostic, const char *msgid,
9a472a42 115 va_list *args, location_t location,
79a490a9 116 diagnostic_t kind)
47b69537 117{
fa6ef813 118 diagnostic->message.err_no = errno;
47b69537 119 diagnostic->message.args_ptr = args;
fa6ef813 120 diagnostic->message.format_spec = _(msgid);
d5706a1e 121 diagnostic->location = location;
47b69537
GDR
122 diagnostic->kind = kind;
123}
124
125/* Return a malloc'd string describing a location. The caller is
126 responsible for freeing the memory. */
127char *
79a490a9 128diagnostic_build_prefix (diagnostic_info *diagnostic)
47b69537 129{
ef9772c8 130 static const char *const diagnostic_kind_text[] = {
caef5b46 131#define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
4b1d52c7
GDR
132#include "diagnostic.def"
133#undef DEFINE_DIAGNOSTIC_KIND
134 "must-not-happen"
135 };
6773e15f 136 expanded_location s = expand_location (diagnostic->location);
ced3f397 137 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
4b1d52c7 138
6773e15f 139 return s.file
4b1d52c7 140 ? build_message_string ("%s:%d: %s",
6773e15f 141 s.file, s.line,
caef5b46 142 _(diagnostic_kind_text[diagnostic->kind]))
4b1d52c7 143 : build_message_string ("%s: %s", progname,
caef5b46 144 _(diagnostic_kind_text[diagnostic->kind]));
47b69537
GDR
145}
146
13f0d49c 147/* Count a diagnostic. Return true if the message should be printed. */
9804f5fb 148static bool
79a490a9
AJ
149diagnostic_count_diagnostic (diagnostic_context *context,
150 diagnostic_info *diagnostic)
47b69537 151{
9804f5fb 152 diagnostic_t kind = diagnostic->kind;
13f0d49c 153 switch (kind)
6ed551b4 154 {
13f0d49c 155 default:
ced3f397 156 gcc_unreachable ();
3aafa0bb 157
9804f5fb
ZW
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)
166 {
6773e15f 167 expanded_location s = expand_location (diagnostic->location);
9804f5fb 168 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
6773e15f 169 s.file, s.line);
9804f5fb
ZW
170 exit (FATAL_EXIT_CODE);
171 }
172#endif
173 if (context->internal_error)
174 (*context->internal_error) (diagnostic->message.format_spec,
175 diagnostic->message.args_ptr);
938d968e 176 /* Fall through. */
9804f5fb
ZW
177
178 case DK_FATAL: case DK_SORRY:
13f0d49c
GDR
179 case DK_ANACHRONISM: case DK_NOTE:
180 ++diagnostic_kind_count (context, kind);
181 break;
182
183 case DK_WARNING:
184 if (!diagnostic_report_warnings_p ())
185 return false;
9804f5fb 186
7783b402 187 if (!context->warning_as_error_requested)
13f0d49c
GDR
188 {
189 ++diagnostic_kind_count (context, DK_WARNING);
190 break;
191 }
7783b402 192 else if (context->issue_warnings_are_errors_message)
13f0d49c 193 {
b6fe0bb8
GDR
194 pp_verbatim (context->printer,
195 "%s: warnings being treated as errors\n", progname);
7783b402 196 context->issue_warnings_are_errors_message = false;
13f0d49c 197 }
9804f5fb 198
938d968e 199 /* And fall through. */
9804f5fb 200 case DK_ERROR:
47b69537 201 ++diagnostic_kind_count (context, DK_ERROR);
13f0d49c 202 break;
6ed551b4
GDR
203 }
204
47b69537 205 return true;
6ed551b4
GDR
206}
207
9804f5fb
ZW
208/* Take any action which is expected to happen after the diagnostic
209 is written out. This function does not always return. */
210static void
79a490a9
AJ
211diagnostic_action_after_output (diagnostic_context *context,
212 diagnostic_info *diagnostic)
9804f5fb
ZW
213{
214 switch (diagnostic->kind)
215 {
216 case DK_DEBUG:
217 case DK_NOTE:
218 case DK_ANACHRONISM:
219 case DK_WARNING:
220 break;
221
222 case DK_ERROR:
223 case DK_SORRY:
224 if (context->abort_on_error)
225 real_abort ();
c65a01af
RG
226 if (flag_fatal_errors)
227 {
228 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
229 exit (FATAL_EXIT_CODE);
230 }
9804f5fb
ZW
231 break;
232
233 case DK_ICE:
234 if (context->abort_on_error)
235 real_abort ();
236
d5706a1e
ZW
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);
9804f5fb
ZW
240 exit (FATAL_EXIT_CODE);
241
242 case DK_FATAL:
243 if (context->abort_on_error)
244 real_abort ();
245
246 fnotice (stderr, "compilation terminated.\n");
247 exit (FATAL_EXIT_CODE);
248
249 default:
d5706a1e 250 gcc_unreachable ();
9804f5fb
ZW
251 }
252}
253
6ed551b4 254/* Prints out, if necessary, the name of the current function
d5706a1e 255 that caused an error. Called from all error and warning functions. */
6ed551b4 256void
79a490a9 257diagnostic_report_current_function (diagnostic_context *context)
6ed551b4 258{
47b69537 259 diagnostic_report_current_module (context);
ae2bcd98 260 lang_hooks.print_error_function (context, input_filename);
6ed551b4
GDR
261}
262
6ed551b4 263void
79a490a9 264diagnostic_report_current_module (diagnostic_context *context)
6ed551b4 265{
59650e48 266 struct file_stack *p;
6ed551b4 267
b6fe0bb8 268 if (pp_needs_newline (context->printer))
59650e48 269 {
b6fe0bb8
GDR
270 pp_newline (context->printer);
271 pp_needs_newline (context->printer) = false;
59650e48 272 }
6ed551b4 273
6773e15f
PB
274 p = input_file_stack;
275 if (p && diagnostic_last_module_changed (context))
59650e48 276 {
6773e15f 277 expanded_location xloc = expand_location (p->location);
b6fe0bb8
GDR
278 pp_verbatim (context->printer,
279 "In file included from %s:%d",
6773e15f 280 xloc.file, xloc.line);
53f72d60 281 while ((p = p->next) != NULL)
6773e15f
PB
282 {
283 xloc = expand_location (p->location);
284 pp_verbatim (context->printer,
285 ",\n from %s:%d",
286 xloc.file, xloc.line);
287 }
b6fe0bb8 288 pp_verbatim (context->printer, ":\n");
59650e48
ZW
289 diagnostic_set_last_module (context);
290 }
291}
400500c4 292
59650e48 293static void
79a490a9
AJ
294default_diagnostic_starter (diagnostic_context *context,
295 diagnostic_info *diagnostic)
6ed551b4 296{
59650e48 297 diagnostic_report_current_function (context);
b6fe0bb8 298 pp_set_prefix (context->printer, diagnostic_build_prefix (diagnostic));
59650e48 299}
6ed551b4 300
59650e48 301static void
5671bf27
AJ
302default_diagnostic_finalizer (diagnostic_context *context,
303 diagnostic_info *diagnostic __attribute__((unused)))
59650e48 304{
b6fe0bb8 305 pp_destroy_prefix (context->printer);
6ed551b4
GDR
306}
307
59650e48
ZW
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. */
400500c4
RK
313
314void
79a490a9
AJ
315diagnostic_report_diagnostic (diagnostic_context *context,
316 diagnostic_info *diagnostic)
400500c4 317{
d5706a1e
ZW
318 if (context->lock > 0)
319 {
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);
327 }
328
329 context->lock++;
400500c4 330
9804f5fb 331 if (diagnostic_count_diagnostic (context, diagnostic))
59650e48 332 {
d5706a1e
ZW
333 pp_prepare_to_format (context->printer, &diagnostic->message,
334 &diagnostic->location);
59650e48 335 (*diagnostic_starter (context)) (context, diagnostic);
b6fe0bb8 336 pp_format_text (context->printer, &diagnostic->message);
9804f5fb 337 (*diagnostic_finalizer (context)) (context, diagnostic);
b6fe0bb8 338 pp_flush (context->printer);
9804f5fb 339 diagnostic_action_after_output (context, diagnostic);
43db5b3c 340 }
9804f5fb
ZW
341
342 context->lock--;
59650e48 343}
43db5b3c 344
59650e48
ZW
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'. */
3a538a66 349
59650e48 350const char *
79a490a9 351trim_filename (const char *name)
59650e48
ZW
352{
353 static const char this_file[] = __FILE__;
354 const char *p = name, *q = this_file;
43db5b3c 355
59650e48
ZW
356 /* First skip any "../" in each filename. This allows us to give a proper
357 reference to a file in a subdirectory. */
d5706a1e 358 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
59650e48
ZW
359 p += 3;
360
d5706a1e 361 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
59650e48
ZW
362 q += 3;
363
364 /* Now skip any parts the two filenames have in common. */
365 while (*p == *q && *p != 0 && *q != 0)
366 p++, q++;
367
368 /* Now go backwards until the previous directory separator. */
d5706a1e 369 while (p > name && IS_DIR_SEPARATOR (p[-1]))
59650e48
ZW
370 p--;
371
372 return p;
6ed551b4 373}
59650e48
ZW
374\f
375/* Standard error reporting routines in increasing order of severity.
376 All of these take arguments like printf. */
6ed551b4 377
59650e48
ZW
378/* Text to be emitted verbatim to the error message stream; this
379 produces no prefix and disables line-wrapping. Use rarely. */
6ed551b4 380void
e34d07f2 381verbatim (const char *msgid, ...)
59650e48
ZW
382{
383 text_info text;
e34d07f2 384 va_list ap;
59650e48 385
e34d07f2 386 va_start (ap, msgid);
fa6ef813 387 text.err_no = errno;
59650e48 388 text.args_ptr = &ap;
fa6ef813 389 text.format_spec = _(msgid);
b6fe0bb8
GDR
390 pp_format_verbatim (global_dc->printer, &text);
391 pp_flush (global_dc->printer);
e34d07f2 392 va_end (ap);
59650e48
ZW
393}
394
395/* An informative note. Use this for additional details on an error
396 message. */
397void
e34d07f2 398inform (const char *msgid, ...)
6ed551b4 399{
47b69537 400 diagnostic_info diagnostic;
e34d07f2 401 va_list ap;
59650e48 402
e34d07f2 403 va_start (ap, msgid);
9a472a42 404 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_NOTE);
59650e48 405 report_diagnostic (&diagnostic);
e34d07f2 406 va_end (ap);
6ed551b4
GDR
407}
408
59650e48
ZW
409/* A warning. Use this for code which is correct according to the
410 relevant language specification but is likely to be buggy anyway. */
6ed551b4 411void
e34d07f2 412warning (const char *msgid, ...)
6ed551b4 413{
47b69537 414 diagnostic_info diagnostic;
e34d07f2 415 va_list ap;
6ed551b4 416
e34d07f2 417 va_start (ap, msgid);
9a472a42 418 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_WARNING);
47b69537 419 report_diagnostic (&diagnostic);
e34d07f2 420 va_end (ap);
6ed551b4
GDR
421}
422
9804f5fb
ZW
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.
427
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 (...);" */
59650e48 431void
e34d07f2 432pedwarn (const char *msgid, ...)
59650e48
ZW
433{
434 diagnostic_info diagnostic;
e34d07f2 435 va_list ap;
8514e318 436
e34d07f2 437 va_start (ap, msgid);
9a472a42
NS
438 diagnostic_set_info (&diagnostic, msgid, &ap, input_location,
439 pedantic_error_kind ());
59650e48 440 report_diagnostic (&diagnostic);
e34d07f2 441 va_end (ap);
59650e48 442}
43db5b3c 443
59650e48
ZW
444/* A hard error: the code is definitely ill-formed, and an object file
445 will not be produced. */
8514e318 446void
e34d07f2 447error (const char *msgid, ...)
8514e318 448{
59650e48 449 diagnostic_info diagnostic;
e34d07f2 450 va_list ap;
59650e48 451
e34d07f2 452 va_start (ap, msgid);
9a472a42 453 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_ERROR);
59650e48 454 report_diagnostic (&diagnostic);
e34d07f2 455 va_end (ap);
93d87cb1
GDR
456}
457
59650e48
ZW
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. */
93d87cb1 461void
e34d07f2 462sorry (const char *msgid, ...)
93d87cb1 463{
59650e48 464 diagnostic_info diagnostic;
e34d07f2 465 va_list ap;
fbfc1192 466
e34d07f2 467 va_start (ap, msgid);
9a472a42 468 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_SORRY);
9804f5fb 469 report_diagnostic (&diagnostic);
e34d07f2 470 va_end (ap);
59650e48 471}
43db5b3c 472
59650e48
ZW
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. */
476void
e34d07f2 477fatal_error (const char *msgid, ...)
fbfc1192 478{
59650e48 479 diagnostic_info diagnostic;
e34d07f2 480 va_list ap;
fbfc1192 481
e34d07f2 482 va_start (ap, msgid);
9a472a42 483 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_FATAL);
59650e48 484 report_diagnostic (&diagnostic);
e34d07f2 485 va_end (ap);
59650e48 486
d5706a1e 487 gcc_unreachable ();
fbfc1192
ZW
488}
489
59650e48
ZW
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. */
494void
e34d07f2 495internal_error (const char *msgid, ...)
fbfc1192 496{
59650e48 497 diagnostic_info diagnostic;
e34d07f2 498 va_list ap;
fbfc1192 499
e34d07f2 500 va_start (ap, msgid);
9a472a42 501 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_ICE);
59650e48 502 report_diagnostic (&diagnostic);
e34d07f2 503 va_end (ap);
fbfc1192 504
d5706a1e 505 gcc_unreachable ();
fbfc1192 506}
59650e48 507\f
59650e48
ZW
508/* Special case error functions. Most are implemented in terms of the
509 above, or should be. */
e23bd218 510
59650e48
ZW
511/* Print a diagnostic MSGID on FILE. This is just fprintf, except it
512 runs its second argument through gettext. */
04c1334c 513void
e34d07f2 514fnotice (FILE *file, const char *msgid, ...)
04c1334c 515{
e34d07f2 516 va_list ap;
04c1334c 517
e34d07f2 518 va_start (ap, msgid);
59650e48 519 vfprintf (file, _(msgid), ap);
e34d07f2 520 va_end (ap);
04c1334c
GDR
521}
522
59650e48
ZW
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. */
527
528static void
79a490a9 529error_recursion (diagnostic_context *context)
59650e48 530{
d5706a1e
ZW
531 diagnostic_info diagnostic;
532
59650e48 533 if (context->lock < 3)
b6fe0bb8 534 pp_flush (context->printer);
59650e48
ZW
535
536 fnotice (stderr,
537 "Internal compiler error: Error reporting routines re-entered.\n");
d5706a1e
ZW
538
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);
543
544 /* Do not use gcc_unreachable here; that goes through internal_error
545 and therefore would cause infinite recursion. */
546 real_abort ();
59650e48
ZW
547}
548
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. */
552
553void
79a490a9 554fancy_abort (const char *file, int line, const char *function)
59650e48
ZW
555{
556 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
557}
558
886e0865
GK
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
79a490a9
AJ
563static void
564real_abort (void)
886e0865
GK
565{
566 abort ();
567}
This page took 1.231592 seconds and 5 git commands to generate.