2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation,
4 Contributed by Andy Vaught & Niels Kristian Bech Jensen
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
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
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
23 /* Handle the inevitable errors. A major catch here is that things
24 flagged as errors in one match subroutine can conceivably be legal
25 elsewhere. This means that error messages are recorded and saved
26 for possible use later. If a line does not match a legal
27 construction, then the saved error message is reported. */
34 int gfc_suppress_error = 0;
36 static int terminal_width, buffer_flag, errors,
37 use_warning_buffer, warnings;
39 static char *error_ptr, *warning_ptr;
41 static gfc_error_buf error_buffer, warning_buffer;
44 /* Per-file error initialization. */
47 gfc_error_init_1 (void)
49 terminal_width = gfc_terminal_width ();
56 /* Set the flag for buffering errors or not. */
59 gfc_buffer_error (int flag)
65 /* Add a single character to the error buffer or output depending on
73 if (use_warning_buffer)
76 if (warning_ptr - warning_buffer.message >= MAX_ERROR_MESSAGE)
77 gfc_internal_error ("error_char(): Warning buffer overflow");
82 if (error_ptr - error_buffer.message >= MAX_ERROR_MESSAGE)
83 gfc_internal_error ("error_char(): Error buffer overflow");
94 /* Copy a string to wherever it needs to go. */
97 error_string (const char *p)
104 /* Show the file, where it was included and the source line, give a
105 locus. Calls error_printf() recursively, but the recursion is at
106 most one level deep. */
108 static void error_printf (const char *, ...) ATTRIBUTE_PRINTF_1;
111 show_locus (int offset, locus * loc)
118 /* TODO: Either limit the total length and number of included files
119 displayed or add buffering of arbitrary number of characters in
124 error_printf ("In file %s:%d\n", f->filename,
125 #ifdef USE_MAPPED_LOCATION
126 LOCATION_LINE (lb->location)
134 i = f->inclusion_line;
137 if (f == NULL) break;
139 error_printf (" Included at %s:%d\n", f->filename, i);
142 /* Show the line itself, taking care not to print more than what can
143 show up on the terminal. Tabs are converted to spaces. */
145 p = lb->line + offset;
147 if (i > terminal_width)
148 i = terminal_width - 1;
163 m = ((c >> 4) & 0x0F) + '0';
168 m = (c & 0x0F) + '0';
179 /* As part of printing an error, we show the source lines that caused
180 the problem. We show at least one, possibly two loci. If we're
181 showing two loci and they both refer to the same file and line, we
182 only print the line once. */
185 show_loci (locus * l1, locus * l2)
187 int offset, flag, i, m, c1, c2, cmax;
191 error_printf ("<During initialization>\n");
195 c1 = l1->nextc - l1->lb->line;
200 c2 = l2->nextc - l2->lb->line;
208 if (l1->lb != l2->lb || m > terminal_width - 10)
212 cmax = (c1 < c2) ? c2 : c1;
213 if (cmax > terminal_width - 5)
214 offset = cmax - terminal_width + 5;
222 show_locus (offset, l1);
224 /* Arrange that '1' and '2' will show up even if the two columns are equal. */
225 for (i = 1; i <= cmax; i++)
249 if (c1 > terminal_width - 5)
257 show_locus (offset, l1);
258 for (i = 1; i < c1; i++)
268 if (c2 > terminal_width - 20)
276 show_locus (offset, l2);
278 for (i = 1; i < c2; i++)
287 /* Workhorse for the error printing subroutines. This subroutine is
288 inspired by g77's error handling and is similar to printf() with
289 the following %-codes:
291 %c Character, %d Integer, %s String, %% Percent
292 %L Takes locus argument
293 %C Current locus (no argument)
295 If a locus pointer is given, the actual source line is printed out
296 and the column is indicated. Since we want the error message at
297 the bottom of any source file information, we must scan the
298 argument list twice. A maximum of two locus arguments are
305 error_print (const char *type, const char *format0, va_list argp)
307 char c, *p, int_buf[IBUF_LEN], c_arg[MAX_ARGS], *cp_arg[MAX_ARGS];
308 int i, n, have_l1, i_arg[MAX_ARGS];
309 locus *l1, *l2, *loc;
312 l1 = l2 = loc = NULL;
332 loc = va_arg (argp, locus *);
337 loc = &gfc_current_locus;
352 i_arg[n++] = va_arg (argp, int);
356 c_arg[n++] = va_arg (argp, int);
360 cp_arg[n++] = va_arg (argp, char *);
366 /* Show the current loci if we have to. */
376 for (; *format; format++)
380 error_char (*format);
392 error_char (c_arg[n++]);
396 error_string (cp_arg[n++]);
409 p = int_buf + IBUF_LEN - 1;
421 error_string (p + 1);
424 case 'C': /* Current locus */
425 case 'L': /* Specified locus */
426 error_string (have_l1 ? "(2)" : "(1)");
436 /* Wrapper for error_print(). */
439 error_printf (const char *format, ...)
443 va_start (argp, format);
444 error_print ("", format, argp);
449 /* Issue a warning. */
452 gfc_warning (const char *format, ...)
456 if (inhibit_warnings)
459 warning_buffer.flag = 1;
460 warning_ptr = warning_buffer.message;
461 use_warning_buffer = 1;
463 va_start (argp, format);
464 if (buffer_flag == 0)
466 error_print ("Warning:", format, argp);
473 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
474 feature. An error/warning will be issued if the currently selected
475 standard does not contain the requested bits. Return FAILURE if
476 and error is generated. */
479 gfc_notify_std (int std, const char *format, ...)
484 warning = ((gfc_option.warn_std & std) != 0)
485 && !inhibit_warnings;
486 if ((gfc_option.allow_std & std) != 0
490 if (gfc_suppress_error)
491 return warning ? SUCCESS : FAILURE;
495 warning_buffer.flag = 1;
496 warning_ptr = warning_buffer.message;
497 use_warning_buffer = 1;
501 error_buffer.flag = 1;
502 error_ptr = error_buffer.message;
503 use_warning_buffer = 0;
506 if (buffer_flag == 0)
513 va_start (argp, format);
515 error_print ("Warning:", format, argp);
517 error_print ("Error:", format, argp);
521 return warning ? SUCCESS : FAILURE;
525 /* Immediate warning (i.e. do not buffer the warning). */
528 gfc_warning_now (const char *format, ...)
533 if (inhibit_warnings)
540 va_start (argp, format);
541 error_print ("Warning:", format, argp);
549 /* Clear the warning flag. */
552 gfc_clear_warning (void)
554 warning_buffer.flag = 0;
558 /* Check to see if any warnings have been saved.
559 If so, print the warning. */
562 gfc_warning_check (void)
564 if (warning_buffer.flag)
567 fputs (warning_buffer.message, stderr);
568 warning_buffer.flag = 0;
573 /* Issue an error. */
576 gfc_error (const char *format, ...)
580 if (gfc_suppress_error)
583 error_buffer.flag = 1;
584 error_ptr = error_buffer.message;
585 use_warning_buffer = 0;
587 va_start (argp, format);
588 if (buffer_flag == 0)
590 error_print ("Error:", format, argp);
597 /* Immediate error. */
600 gfc_error_now (const char *format, ...)
605 error_buffer.flag = 1;
606 error_ptr = error_buffer.message;
612 va_start (argp, format);
613 error_print ("Error:", format, argp);
621 /* Fatal error, never returns. */
624 gfc_fatal_error (const char *format, ...)
630 va_start (argp, format);
631 error_print ("Fatal Error:", format, argp);
638 /* This shouldn't happen... but sometimes does. */
641 gfc_internal_error (const char *format, ...)
647 va_start (argp, format);
649 show_loci (&gfc_current_locus, NULL);
650 error_printf ("Internal Error at (1):");
652 error_print ("", format, argp);
659 /* Clear the error flag when we start to compile a source line. */
662 gfc_clear_error (void)
664 error_buffer.flag = 0;
668 /* Check to see if any errors have been saved.
669 If so, print the error. Returns the state of error_flag. */
672 gfc_error_check (void)
676 rc = error_buffer.flag;
678 if (error_buffer.flag)
681 fputs (error_buffer.message, stderr);
682 error_buffer.flag = 0;
689 /* Save the existing error state. */
692 gfc_push_error (gfc_error_buf * err)
694 err->flag = error_buffer.flag;
695 if (error_buffer.flag)
696 strcpy (err->message, error_buffer.message);
698 error_buffer.flag = 0;
702 /* Restore a previous pushed error state. */
705 gfc_pop_error (gfc_error_buf * err)
707 error_buffer.flag = err->flag;
708 if (error_buffer.flag)
709 strcpy (error_buffer.message, err->message);
713 /* Debug wrapper for printf. */
716 gfc_status (const char *format, ...)
720 va_start (argp, format);
722 vprintf (format, argp);
728 /* Subroutine for outputting a single char so that we don't have to go
729 around creating a lot of 1-character strings. */
732 gfc_status_char (char c)
738 /* Report the number of warnings and errors that occurred to the caller. */
741 gfc_get_errors (int *w, int *e)