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
* loc
)
128 /* TODO: Either limit the total length and number of included files
129 displayed or add buffering of arbitrary number of characters in
134 error_printf ("In file %s:%d\n", f
->filename
, lb
->linenum
);
138 i
= f
->inclusion_line
;
141 if (f
== NULL
) break;
143 error_printf (" Included at %s:%d\n", f
->filename
, i
);
146 /* Show the line itself, taking care not to print more than what can
147 show up on the terminal. Tabs are converted to spaces. */
149 p
= lb
->line
+ offset
;
151 if (i
> terminal_width
)
152 i
= terminal_width
- 1;
167 m
= ((c
>> 4) & 0x0F) + '0';
172 m
= (c
& 0x0F) + '0';
183 /* As part of printing an error, we show the source lines that caused
184 the problem. We show at least one, possibly two loci. If we're
185 showing two loci and they both refer to the same file and line, we
186 only print the line once. */
189 show_loci (locus
* l1
, locus
* l2
)
191 int offset
, flag
, i
, m
, c1
, c2
, cmax
;
195 error_printf ("<During initialization>\n");
199 c1
= l1
->nextc
- l1
->lb
->line
;
204 c2
= l2
->nextc
- l2
->lb
->line
;
212 if (l1
->lb
!= l2
->lb
|| m
> terminal_width
- 10)
216 cmax
= (c1
< c2
) ? c2
: c1
;
217 if (cmax
> terminal_width
- 5)
218 offset
= cmax
- terminal_width
+ 5;
226 show_locus (offset
, l1
);
228 /* Arrange that '1' and '2' will show up even if the two columns are equal. */
229 for (i
= 1; i
<= cmax
; i
++)
253 if (c1
> terminal_width
- 5)
261 show_locus (offset
, l1
);
262 for (i
= 1; i
< c1
; i
++)
272 if (c2
> terminal_width
- 20)
280 show_locus (offset
, l2
);
282 for (i
= 1; i
< c2
; i
++)
291 /* Workhorse for the error printing subroutines. This subroutine is
292 inspired by g77's error handling and is similar to printf() with
293 the following %-codes:
295 %c Character, %d Integer, %s String, %% Percent
296 %L Takes locus argument
297 %C Current locus (no argument)
299 If a locus pointer is given, the actual source line is printed out
300 and the column is indicated. Since we want the error message at
301 the bottom of any source file information, we must scan the
302 argument list twice. A maximum of two locus arguments are
309 error_print (const char *type
, const char *format0
, va_list argp
)
311 char c
, *p
, int_buf
[IBUF_LEN
], c_arg
[MAX_ARGS
], *cp_arg
[MAX_ARGS
];
312 int i
, n
, have_l1
, i_arg
[MAX_ARGS
];
313 locus
*l1
, *l2
, *loc
;
316 l1
= l2
= loc
= NULL
;
336 loc
= va_arg (argp
, locus
*);
341 loc
= gfc_current_locus ();
356 i_arg
[n
++] = va_arg (argp
, int);
360 c_arg
[n
++] = va_arg (argp
, int);
364 cp_arg
[n
++] = va_arg (argp
, char *);
370 /* Show the current loci if we have to. */
380 for (; *format
; format
++)
384 error_char (*format
);
396 error_char (c_arg
[n
++]);
400 error_string (cp_arg
[n
++]);
413 p
= int_buf
+ IBUF_LEN
- 1;
425 error_string (p
+ 1);
428 case 'C': /* Current locus */
429 case 'L': /* Specified locus */
430 error_string (have_l1
? "(2)" : "(1)");
440 /* Wrapper for error_print(). */
443 error_printf (const char *format
, ...)
447 va_start (argp
, format
);
448 error_print ("", format
, argp
);
453 /* Issue a warning. */
456 gfc_warning (const char *format
, ...)
460 if (inhibit_warnings
)
463 warning_buffer
.flag
= 1;
464 warning_ptr
= warning_buffer
.message
;
465 use_warning_buffer
= 1;
467 va_start (argp
, format
);
468 if (buffer_flag
== 0)
470 error_print ("Warning:", format
, argp
);
477 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
478 feature. An error/warning will be issued if the currently selected
479 standard does not contain the requested bits. Return FAILURE if
480 and error is generated. */
483 gfc_notify_std (int std
, const char *format
, ...)
488 warning
= ((gfc_option
.warn_std
& std
) != 0)
489 && !inhibit_warnings
;
490 if ((gfc_option
.allow_std
& std
) != 0
494 if (gfc_suppress_error
)
495 return warning
? SUCCESS
: FAILURE
;
499 warning_buffer
.flag
= 1;
500 warning_ptr
= warning_buffer
.message
;
501 use_warning_buffer
= 1;
505 error_buffer
.flag
= 1;
506 error_ptr
= error_buffer
.message
;
507 use_warning_buffer
= 0;
510 if (buffer_flag
== 0)
517 va_start (argp
, format
);
519 error_print ("Warning:", format
, argp
);
521 error_print ("Error:", format
, argp
);
525 return warning
? SUCCESS
: FAILURE
;
529 /* Immediate warning (i.e. do not buffer the warning). */
532 gfc_warning_now (const char *format
, ...)
537 if (inhibit_warnings
)
544 va_start (argp
, format
);
545 error_print ("Warning:", format
, argp
);
553 /* Clear the warning flag. */
556 gfc_clear_warning (void)
559 warning_buffer
.flag
= 0;
563 /* Check to see if any warnings have been saved.
564 If so, print the warning. */
567 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)
671 error_buffer
.flag
= 0;
675 /* Check to see if any errors have been saved.
676 If so, print the error. Returns the state of error_flag. */
679 gfc_error_check (void)
683 rc
= error_buffer
.flag
;
685 if (error_buffer
.flag
)
688 fputs (error_buffer
.message
, stderr
);
689 error_buffer
.flag
= 0;
696 /* Save the existing error state. */
699 gfc_push_error (gfc_error_buf
* err
)
702 err
->flag
= error_buffer
.flag
;
703 if (error_buffer
.flag
)
704 strcpy (err
->message
, error_buffer
.message
);
706 error_buffer
.flag
= 0;
710 /* Restore a previous pushed error state. */
713 gfc_pop_error (gfc_error_buf
* err
)
716 error_buffer
.flag
= err
->flag
;
717 if (error_buffer
.flag
)
718 strcpy (error_buffer
.message
, err
->message
);
722 /* Debug wrapper for printf. */
725 gfc_status (const char *format
, ...)
729 va_start (argp
, format
);
731 vprintf (format
, argp
);
737 /* Subroutine for outputting a single char so that we don't have to go
738 around creating a lot of 1-character strings. */
741 gfc_status_char (char c
)
747 /* Report the number of warnings and errors that occored to the caller. */
750 gfc_get_errors (int *w
, int *e
)