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)
55 terminal_width
= gfc_terminal_width ();
62 /* Set the flag for buffering errors or not. */
65 gfc_buffer_error (int flag
)
71 /* Add a single character to the error buffer or output depending on
79 if (use_warning_buffer
)
82 if (warning_ptr
- warning_buffer
.message
>= MAX_ERROR_MESSAGE
)
83 gfc_internal_error ("error_char(): Warning buffer overflow");
88 if (error_ptr
- error_buffer
.message
>= MAX_ERROR_MESSAGE
)
89 gfc_internal_error ("error_char(): Error buffer overflow");
100 /* Copy a string to wherever it needs to go. */
103 error_string (const char *p
)
110 /* Show the file, where it was included and the source line, give a
111 locus. Calls error_printf() recursively, but the recursion is at
112 most one level deep. */
114 static void error_printf (const char *, ...) ATTRIBUTE_PRINTF_1
;
117 show_locus (int offset
, locus
* loc
)
124 /* TODO: Either limit the total length and number of included files
125 displayed or add buffering of arbitrary number of characters in
130 error_printf ("In file %s:%d\n", f
->filename
,
131 #ifdef USE_MAPPED_LOCATION
132 LOCATION_LINE (lb
->location
)
140 i
= f
->inclusion_line
;
143 if (f
== NULL
) break;
145 error_printf (" Included at %s:%d\n", f
->filename
, i
);
148 /* Show the line itself, taking care not to print more than what can
149 show up on the terminal. Tabs are converted to spaces. */
151 p
= lb
->line
+ offset
;
153 if (i
> terminal_width
)
154 i
= terminal_width
- 1;
169 m
= ((c
>> 4) & 0x0F) + '0';
174 m
= (c
& 0x0F) + '0';
185 /* As part of printing an error, we show the source lines that caused
186 the problem. We show at least one, possibly two loci. If we're
187 showing two loci and they both refer to the same file and line, we
188 only print the line once. */
191 show_loci (locus
* l1
, locus
* l2
)
193 int offset
, flag
, i
, m
, c1
, c2
, cmax
;
197 error_printf ("<During initialization>\n");
201 c1
= l1
->nextc
- l1
->lb
->line
;
206 c2
= l2
->nextc
- l2
->lb
->line
;
214 if (l1
->lb
!= l2
->lb
|| m
> terminal_width
- 10)
218 cmax
= (c1
< c2
) ? c2
: c1
;
219 if (cmax
> terminal_width
- 5)
220 offset
= cmax
- terminal_width
+ 5;
228 show_locus (offset
, l1
);
230 /* Arrange that '1' and '2' will show up even if the two columns are equal. */
231 for (i
= 1; i
<= cmax
; i
++)
255 if (c1
> terminal_width
- 5)
263 show_locus (offset
, l1
);
264 for (i
= 1; i
< c1
; i
++)
274 if (c2
> terminal_width
- 20)
282 show_locus (offset
, l2
);
284 for (i
= 1; i
< c2
; i
++)
293 /* Workhorse for the error printing subroutines. This subroutine is
294 inspired by g77's error handling and is similar to printf() with
295 the following %-codes:
297 %c Character, %d Integer, %s String, %% Percent
298 %L Takes locus argument
299 %C Current locus (no argument)
301 If a locus pointer is given, the actual source line is printed out
302 and the column is indicated. Since we want the error message at
303 the bottom of any source file information, we must scan the
304 argument list twice. A maximum of two locus arguments are
311 error_print (const char *type
, const char *format0
, va_list argp
)
313 char c
, *p
, int_buf
[IBUF_LEN
], c_arg
[MAX_ARGS
], *cp_arg
[MAX_ARGS
];
314 int i
, n
, have_l1
, i_arg
[MAX_ARGS
];
315 locus
*l1
, *l2
, *loc
;
318 l1
= l2
= loc
= NULL
;
338 loc
= va_arg (argp
, locus
*);
343 loc
= &gfc_current_locus
;
358 i_arg
[n
++] = va_arg (argp
, int);
362 c_arg
[n
++] = va_arg (argp
, int);
366 cp_arg
[n
++] = va_arg (argp
, char *);
372 /* Show the current loci if we have to. */
382 for (; *format
; format
++)
386 error_char (*format
);
398 error_char (c_arg
[n
++]);
402 error_string (cp_arg
[n
++]);
415 p
= int_buf
+ IBUF_LEN
- 1;
427 error_string (p
+ 1);
430 case 'C': /* Current locus */
431 case 'L': /* Specified locus */
432 error_string (have_l1
? "(2)" : "(1)");
442 /* Wrapper for error_print(). */
445 error_printf (const char *format
, ...)
449 va_start (argp
, format
);
450 error_print ("", format
, argp
);
455 /* Issue a warning. */
458 gfc_warning (const char *format
, ...)
462 if (inhibit_warnings
)
465 warning_buffer
.flag
= 1;
466 warning_ptr
= warning_buffer
.message
;
467 use_warning_buffer
= 1;
469 va_start (argp
, format
);
470 if (buffer_flag
== 0)
472 error_print ("Warning:", format
, argp
);
479 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
480 feature. An error/warning will be issued if the currently selected
481 standard does not contain the requested bits. Return FAILURE if
482 and error is generated. */
485 gfc_notify_std (int std
, const char *format
, ...)
490 warning
= ((gfc_option
.warn_std
& std
) != 0)
491 && !inhibit_warnings
;
492 if ((gfc_option
.allow_std
& std
) != 0
496 if (gfc_suppress_error
)
497 return warning
? SUCCESS
: FAILURE
;
501 warning_buffer
.flag
= 1;
502 warning_ptr
= warning_buffer
.message
;
503 use_warning_buffer
= 1;
507 error_buffer
.flag
= 1;
508 error_ptr
= error_buffer
.message
;
509 use_warning_buffer
= 0;
512 if (buffer_flag
== 0)
519 va_start (argp
, format
);
521 error_print ("Warning:", format
, argp
);
523 error_print ("Error:", format
, argp
);
527 return warning
? SUCCESS
: FAILURE
;
531 /* Immediate warning (i.e. do not buffer the warning). */
534 gfc_warning_now (const char *format
, ...)
539 if (inhibit_warnings
)
546 va_start (argp
, format
);
547 error_print ("Warning:", format
, argp
);
555 /* Clear the warning flag. */
558 gfc_clear_warning (void)
560 warning_buffer
.flag
= 0;
564 /* Check to see if any warnings have been saved.
565 If so, print the warning. */
568 gfc_warning_check (void)
570 if (warning_buffer
.flag
)
573 fputs (warning_buffer
.message
, stderr
);
574 warning_buffer
.flag
= 0;
579 /* Issue an error. */
582 gfc_error (const char *format
, ...)
586 if (gfc_suppress_error
)
589 error_buffer
.flag
= 1;
590 error_ptr
= error_buffer
.message
;
591 use_warning_buffer
= 0;
593 va_start (argp
, format
);
594 if (buffer_flag
== 0)
596 error_print ("Error:", format
, argp
);
603 /* Immediate error. */
606 gfc_error_now (const char *format
, ...)
611 error_buffer
.flag
= 1;
612 error_ptr
= error_buffer
.message
;
618 va_start (argp
, format
);
619 error_print ("Error:", format
, argp
);
627 /* Fatal error, never returns. */
630 gfc_fatal_error (const char *format
, ...)
636 va_start (argp
, format
);
637 error_print ("Fatal Error:", format
, argp
);
644 /* This shouldn't happen... but sometimes does. */
647 gfc_internal_error (const char *format
, ...)
653 va_start (argp
, format
);
655 show_loci (&gfc_current_locus
, NULL
);
656 error_printf ("Internal Error at (1):");
658 error_print ("", format
, argp
);
665 /* Clear the error flag when we start to compile a source line. */
668 gfc_clear_error (void)
670 error_buffer
.flag
= 0;
674 /* Check to see if any errors have been saved.
675 If so, print the error. Returns the state of error_flag. */
678 gfc_error_check (void)
682 rc
= error_buffer
.flag
;
684 if (error_buffer
.flag
)
687 fputs (error_buffer
.message
, stderr
);
688 error_buffer
.flag
= 0;
695 /* Save the existing error state. */
698 gfc_push_error (gfc_error_buf
* err
)
700 err
->flag
= error_buffer
.flag
;
701 if (error_buffer
.flag
)
702 strcpy (err
->message
, error_buffer
.message
);
704 error_buffer
.flag
= 0;
708 /* Restore a previous pushed error state. */
711 gfc_pop_error (gfc_error_buf
* err
)
713 error_buffer
.flag
= err
->flag
;
714 if (error_buffer
.flag
)
715 strcpy (error_buffer
.message
, err
->message
);
719 /* Debug wrapper for printf. */
722 gfc_status (const char *format
, ...)
726 va_start (argp
, format
);
728 vprintf (format
, argp
);
734 /* Subroutine for outputting a single char so that we don't have to go
735 around creating a lot of 1-character strings. */
738 gfc_status_char (char c
)
744 /* Report the number of warnings and errors that occurred to the caller. */
747 gfc_get_errors (int *w
, int *e
)