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. */
40 int gfc_suppress_error = 0;
42 static int terminal_width, buffer_flag, errors,
43 use_warning_buffer, warnings;
45 static char *error_ptr, *warning_ptr;
47 static gfc_error_buf error_buffer, warning_buffer;
50 /* Per-file error initialization. */
53 gfc_error_init_1 (void)
56 terminal_width = gfc_terminal_width();
63 /* Set the flag for buffering errors or not. */
66 gfc_buffer_error (int flag)
73 /* Add a single character to the error buffer or output depending on
82 if (use_warning_buffer)
85 if (warning_ptr - warning_buffer.message >= MAX_ERROR_MESSAGE)
86 gfc_internal_error ("error_char(): Warning buffer overflow");
91 if (error_ptr - error_buffer.message >= MAX_ERROR_MESSAGE)
92 gfc_internal_error ("error_char(): Error buffer overflow");
103 /* Copy a string to wherever it needs to go. */
106 error_string (const char *p)
114 /* Show the file, where it was included and the source line give a
115 locus. Calls error_printf() recursively, but the recursion is at
116 most one level deep. */
118 static void error_printf (const char *, ...) ATTRIBUTE_PRINTF_1;
121 show_locus (int offset, locus * l)
127 /* TODO: Either limit the total length and number of included files
128 displayed or add buffering of arbitrary number of characters in
131 error_printf ("In file %s:%d\n", f->filename, l->lp->start_line + l->line);
136 error_printf (" Included at %s:%d\n", f->filename,
137 f->loc.lp->start_line + f->loc.line);
141 /* Show the line itself, taking care not to print more than what can
142 show up on the terminal. Tabs are converted to spaces. */
143 p = l->lp->line[l->line] + offset;
145 if (i > terminal_width)
146 i = terminal_width - 1;
161 m = ((c >> 4) & 0x0F) + '0';
166 m = (c & 0x0F) + '0';
177 /* As part of printing an error, we show the source lines that caused
178 the problem. We show at least one, possibly two loci. If we're
179 showing two loci and they both refer to the same file and line, we
180 only print the line once. */
183 show_loci (locus * l1, locus * l2)
185 int offset, flag, i, m, c1, c2, cmax;
189 error_printf ("<During initialization>\n");
193 c1 = l1->nextc - l1->lp->line[l1->line];
198 c2 = l2->nextc - l2->lp->line[l2->line];
206 if (l1->lp != l2->lp || l1->line != l2->line || m > terminal_width - 10)
210 cmax = (c1 < c2) ? c2 : c1;
211 if (cmax > terminal_width - 5)
212 offset = cmax - terminal_width + 5;
220 show_locus (offset, l1);
222 /* Arrange that '1' and '2' will show up even if the two columns are equal. */
223 for (i = 1; i <= cmax; i++)
247 if (c1 > terminal_width - 5)
255 show_locus (offset, l1);
256 for (i = 1; i < c1; i++)
266 if (c2 > terminal_width - 20)
274 show_locus (offset, l2);
276 for (i = 1; i < c2; i++)
285 /* Workhorse for the error printing subroutines. This subroutine is
286 inspired by g77's error handling and is similar to printf() with
287 the following %-codes:
289 %c Character, %d Integer, %s String, %% Percent
290 %L Takes locus argument
291 %C Current locus (no argument)
293 If a locus pointer is given, the actual source line is printed out
294 and the column is indicated. Since we want the error message at
295 the bottom of any source file information, we must scan the
296 argument list twice. A maximum of two locus arguments are
303 error_print (const char *type, const char *format0, va_list argp)
305 char c, *p, int_buf[IBUF_LEN], c_arg[MAX_ARGS], *cp_arg[MAX_ARGS];
306 int i, n, have_l1, i_arg[MAX_ARGS];
307 locus *l1, *l2, *loc;
310 l1 = l2 = loc = NULL;
330 loc = va_arg (argp, locus *);
335 loc = gfc_current_locus ();
350 i_arg[n++] = va_arg (argp, int);
354 c_arg[n++] = va_arg (argp, int);
358 cp_arg[n++] = va_arg (argp, char *);
364 /* Show the current loci if we have to. */
374 for (; *format; format++)
378 error_char (*format);
390 error_char (c_arg[n++]);
394 error_string (cp_arg[n++]);
407 p = int_buf + IBUF_LEN - 1;
419 error_string (p + 1);
422 case 'C': /* Current locus */
423 case 'L': /* Specified locus */
424 error_string (have_l1 ? "(2)" : "(1)");
434 /* Wrapper for error_print(). */
437 error_printf (const char *format, ...)
441 va_start (argp, format);
442 error_print ("", format, argp);
447 /* Issue a warning. */
450 gfc_warning (const char *format, ...)
454 if (inhibit_warnings)
457 warning_buffer.flag = 1;
458 warning_ptr = warning_buffer.message;
459 use_warning_buffer = 1;
461 va_start (argp, format);
462 if (buffer_flag == 0)
464 error_print ("Warning:", format, argp);
471 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
472 feature. An error/warning will be issued if the currently selected
473 standard does not contain the requested bits. Return FAILURE if
474 and error is generated. */
477 gfc_notify_std (int std, const char *format, ...)
482 warning = ((gfc_option.warn_std & std) != 0)
483 && !inhibit_warnings;
484 if ((gfc_option.allow_std & std) != 0
488 if (gfc_suppress_error)
489 return warning ? SUCCESS : FAILURE;
493 warning_buffer.flag = 1;
494 warning_ptr = warning_buffer.message;
495 use_warning_buffer = 1;
499 error_buffer.flag = 1;
500 error_ptr = error_buffer.message;
501 use_warning_buffer = 0;
504 if (buffer_flag == 0)
511 va_start (argp, format);
513 error_print ("Warning:", format, argp);
515 error_print ("Error:", format, argp);
519 return warning ? SUCCESS : FAILURE;
523 /* Immediate warning (i.e. do not buffer the warning). */
526 gfc_warning_now (const char *format, ...)
531 if (inhibit_warnings)
538 va_start (argp, format);
539 error_print ("Warning:", format, argp);
547 /* Clear the warning flag. */
550 gfc_clear_warning (void)
553 warning_buffer.flag = 0;
557 /* Check to see if any warnings have been saved.
558 If so, print the warning. */
561 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)
665 error_buffer.flag = 0;
669 /* Check to see if any errors have been saved.
670 If so, print the error. Returns the state of error_flag. */
673 gfc_error_check (void)
677 rc = error_buffer.flag;
679 if (error_buffer.flag)
682 fputs (error_buffer.message, stderr);
683 error_buffer.flag = 0;
690 /* Save the existing error state. */
693 gfc_push_error (gfc_error_buf * err)
696 err->flag = error_buffer.flag;
697 if (error_buffer.flag)
698 strcpy (err->message, error_buffer.message);
700 error_buffer.flag = 0;
704 /* Restore a previous pushed error state. */
707 gfc_pop_error (gfc_error_buf * err)
710 error_buffer.flag = err->flag;
711 if (error_buffer.flag)
712 strcpy (error_buffer.message, err->message);
716 /* Debug wrapper for printf. */
719 gfc_status (const char *format, ...)
723 va_start (argp, format);
725 vprintf (format, argp);
731 /* Subroutine for outputting a single char so that we don't have to go
732 around creating a lot of 1-character strings. */
735 gfc_status_char (char c)
741 /* Report the number of warnings and errors that occored to the caller. */
744 gfc_get_errors (int *w, int *e)