2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Handle the inevitable errors. A major catch here is that things
23 flagged as errors in one match subroutine can conceivably be legal
24 elsewhere. This means that error messages are recorded and saved
25 for possible use later. If a line does not match a legal
26 construction, then the saved error message is reported. */
33 int gfc_suppress_error
= 0;
35 static int terminal_width
, buffer_flag
, errors
, warnings
;
37 static gfc_error_buf error_buffer
, warning_buffer
, *cur_error_buffer
;
40 /* Per-file error initialization. */
43 gfc_error_init_1 (void)
45 terminal_width
= gfc_terminal_width ();
52 /* Set the flag for buffering errors or not. */
55 gfc_buffer_error (int flag
)
61 /* Add a single character to the error buffer or output depending on
69 if (cur_error_buffer
->index
>= cur_error_buffer
->allocated
)
71 cur_error_buffer
->allocated
= cur_error_buffer
->allocated
72 ? cur_error_buffer
->allocated
* 2 : 1000;
73 cur_error_buffer
->message
= xrealloc (cur_error_buffer
->message
,
74 cur_error_buffer
->allocated
);
76 cur_error_buffer
->message
[cur_error_buffer
->index
++] = c
;
82 /* We build up complete lines before handing things
83 over to the library in order to speed up error printing. */
85 static size_t allocated
= 0, index
= 0;
87 if (index
+ 1 >= allocated
)
89 allocated
= allocated
? allocated
* 2 : 1000;
90 line
= xrealloc (line
, allocated
);
104 /* Copy a string to wherever it needs to go. */
107 error_string (const char *p
)
114 /* Print a formatted integer to the error buffer or output. */
119 error_integer (int i
)
121 char *p
, int_buf
[IBUF_LEN
];
129 p
= int_buf
+ IBUF_LEN
- 1;
141 error_string (p
+ 1);
145 /* Show the file, where it was included, and the source line, give a
146 locus. Calls error_printf() recursively, but the recursion is at
147 most one level deep. */
149 static void error_printf (const char *, ...) ATTRIBUTE_GCC_GFC(1,2);
152 show_locus (locus
*loc
, int c1
, int c2
)
157 int i
, m
, offset
, cmax
;
159 /* TODO: Either limit the total length and number of included files
160 displayed or add buffering of arbitrary number of characters in
163 /* Write out the error header line, giving the source file and error
164 location (in GNU standard "[file]:[line].[column]:" format),
165 followed by an "included by" stack and a blank line. This header
166 format is matched by a testsuite parser defined in
167 lib/gfortran-dg.exp. */
172 error_string (f
->filename
);
175 #ifdef USE_MAPPED_LOCATION
176 error_integer (LOCATION_LINE (lb
->location
));
178 error_integer (lb
->linenum
);
181 if ((c1
> 0) || (c2
> 0))
187 if ((c1
> 0) && (c2
> 0))
198 i
= f
->inclusion_line
;
201 if (f
== NULL
) break;
203 error_printf (" Included at %s:%d:", f
->filename
, i
);
208 /* Calculate an appropriate horizontal offset of the source line in
209 order to get the error locus within the visible portion of the
210 line. Note that if the margin of 5 here is changed, the
211 corresponding margin of 10 in show_loci should be changed. */
215 /* When the loci is not associated with a column, it will have a
216 value of zero. We adjust this to 1 so that it will appear. */
223 /* If the two loci would appear in the same column, we shift
224 '2' one column to the right, so as to print '12' rather than
225 just '1'. We do this here so it will be accounted for in the
226 margin calculations. */
231 cmax
= (c1
< c2
) ? c2
: c1
;
232 if (cmax
> terminal_width
- 5)
233 offset
= cmax
- terminal_width
+ 5;
235 /* Show the line itself, taking care not to print more than what can
236 show up on the terminal. Tabs are converted to spaces, and
237 nonprintable characters are converted to a "\xNN" sequence. */
239 /* TODO: Although setting i to the terminal width is clever, it fails
240 to work correctly when nonprintable characters exist. A better
241 solution should be found. */
243 p
= lb
->line
+ offset
;
245 if (i
> terminal_width
)
246 i
= terminal_width
- 1;
261 m
= ((c
>> 4) & 0x0F) + '0';
266 m
= (c
& 0x0F) + '0';
275 /* Show the '1' and/or '2' corresponding to the column of the error
276 locus. Note that a value of -1 for c1 or c2 will simply cause
277 the relevant number not to be printed. */
282 for (i
= 1; i
<= cmax
; i
++)
297 /* As part of printing an error, we show the source lines that caused
298 the problem. We show at least one, and possibly two loci; the two
299 loci may or may not be on the same source line. */
302 show_loci (locus
*l1
, locus
*l2
)
306 if (l1
== NULL
|| l1
->lb
== NULL
)
308 error_printf ("<During initialization>\n");
312 /* While calculating parameters for printing the loci, we consider possible
313 reasons for printing one per line. If appropriate, print the loci
314 individually; otherwise we print them both on the same line. */
316 c1
= l1
->nextc
- l1
->lb
->line
;
319 show_locus (l1
, c1
, -1);
323 c2
= l2
->nextc
- l2
->lb
->line
;
330 /* Note that the margin value of 10 here needs to be less than the
331 margin of 5 used in the calculation of offset in show_locus. */
333 if (l1
->lb
!= l2
->lb
|| m
> terminal_width
- 10)
335 show_locus (l1
, c1
, -1);
336 show_locus (l2
, -1, c2
);
340 show_locus (l1
, c1
, c2
);
346 /* Workhorse for the error printing subroutines. This subroutine is
347 inspired by g77's error handling and is similar to printf() with
348 the following %-codes:
350 %c Character, %d or %i Integer, %s String, %% Percent
351 %L Takes locus argument
352 %C Current locus (no argument)
354 If a locus pointer is given, the actual source line is printed out
355 and the column is indicated. Since we want the error message at
356 the bottom of any source file information, we must scan the
357 argument list twice -- once to determine whether the loci are
358 present and record this for printing, and once to print the error
359 message after and loci have been printed. A maximum of two locus
360 arguments are permitted.
362 This function is also called (recursively) by show_locus in the
363 case of included files; however, as show_locus does not resupply
364 any loci, the recursion is at most one level deep. */
368 static void ATTRIBUTE_GCC_GFC(2,0)
369 error_print (const char *type
, const char *format0
, va_list argp
)
371 enum { TYPE_CURRENTLOC
, TYPE_LOCUS
, TYPE_INTEGER
, TYPE_CHAR
, TYPE_STRING
,
381 const char * stringval
;
383 } arg
[MAX_ARGS
], spec
[MAX_ARGS
];
384 /* spec is the array of specifiers, in the same order as they
385 appear in the format string. arg is the array of arguments,
386 in the same order as they appear in the va_list. */
389 int i
, n
, have_l1
, pos
, maxpos
;
390 locus
*l1
, *l2
, *loc
;
402 for (i
= 0; i
< MAX_ARGS
; i
++)
404 arg
[i
].type
= NOTYPE
;
408 /* First parse the format string for position specifiers. */
421 if (ISDIGIT (*format
))
423 /* This is a position specifier. For example, the number
424 12 in the format string "%12$d", which specifies the third
425 argument of the va_list, formatted in %d format.
426 For details, see "man 3 printf". */
427 pos
= atoi(format
) - 1;
428 gcc_assert (pos
>= 0);
429 while (ISDIGIT(*format
))
431 gcc_assert (*format
++ == '$');
444 arg
[pos
].type
= TYPE_CURRENTLOC
;
448 arg
[pos
].type
= TYPE_LOCUS
;
453 arg
[pos
].type
= TYPE_INTEGER
;
457 arg
[pos
].type
= TYPE_CHAR
;
461 arg
[pos
].type
= TYPE_STRING
;
471 /* Then convert the values for each %-style argument. */
472 for (pos
= 0; pos
<= maxpos
; pos
++)
474 gcc_assert (arg
[pos
].type
!= NOTYPE
);
475 switch (arg
[pos
].type
)
477 case TYPE_CURRENTLOC
:
478 loc
= &gfc_current_locus
;
482 if (arg
[pos
].type
== TYPE_LOCUS
)
483 loc
= va_arg (argp
, locus
*);
488 arg
[pos
].u
.stringval
= "(2)";
494 arg
[pos
].u
.stringval
= "(1)";
499 arg
[pos
].u
.intval
= va_arg (argp
, int);
503 arg
[pos
].u
.charval
= (char) va_arg (argp
, int);
507 arg
[pos
].u
.stringval
= (const char *) va_arg (argp
, char *);
515 for (n
= 0; spec
[n
].pos
>= 0; n
++)
516 spec
[n
].u
= arg
[spec
[n
].pos
].u
;
518 /* Show the current loci if we have to. */
532 for (; *format
; format
++)
536 error_char (*format
);
541 if (ISDIGIT (*format
))
543 /* This is a position specifier. See comment above. */
544 while (ISDIGIT (*format
))
547 /* Skip over the dollar sign. */
558 error_char (spec
[n
++].u
.charval
);
562 case 'C': /* Current locus */
563 case 'L': /* Specified locus */
564 error_string (spec
[n
++].u
.stringval
);
569 error_integer (spec
[n
++].u
.intval
);
578 /* Wrapper for error_print(). */
581 error_printf (const char *nocmsgid
, ...)
585 va_start (argp
, nocmsgid
);
586 error_print ("", _(nocmsgid
), argp
);
591 /* Increment the number of errors, and check whether too many have
595 gfc_increment_error_count (void)
598 if ((gfc_option
.max_errors
!= 0) && (errors
>= gfc_option
.max_errors
))
599 gfc_fatal_error ("Error count reached limit of %d.", gfc_option
.max_errors
);
603 /* Issue a warning. */
606 gfc_warning (const char *nocmsgid
, ...)
610 if (inhibit_warnings
)
613 warning_buffer
.flag
= 1;
614 warning_buffer
.index
= 0;
615 cur_error_buffer
= &warning_buffer
;
617 va_start (argp
, nocmsgid
);
618 error_print (_("Warning:"), _(nocmsgid
), argp
);
623 if (buffer_flag
== 0)
626 if (warnings_are_errors
)
627 gfc_increment_error_count();
632 /* Whether, for a feature included in a given standard set (GFC_STD_*),
633 we should issue an error or a warning, or be quiet. */
636 gfc_notification_std (int std
)
640 warning
= ((gfc_option
.warn_std
& std
) != 0) && !inhibit_warnings
;
641 if ((gfc_option
.allow_std
& std
) != 0 && !warning
)
644 return warning
? WARNING
: ERROR
;
648 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
649 feature. An error/warning will be issued if the currently selected
650 standard does not contain the requested bits. Return FAILURE if
651 an error is generated. */
654 gfc_notify_std (int std
, const char *nocmsgid
, ...)
659 warning
= ((gfc_option
.warn_std
& std
) != 0) && !inhibit_warnings
;
660 if ((gfc_option
.allow_std
& std
) != 0 && !warning
)
663 if (gfc_suppress_error
)
664 return warning
? SUCCESS
: FAILURE
;
666 cur_error_buffer
= (warning
&& !warnings_are_errors
)
667 ? &warning_buffer
: &error_buffer
;
668 cur_error_buffer
->flag
= 1;
669 cur_error_buffer
->index
= 0;
671 va_start (argp
, nocmsgid
);
673 error_print (_("Warning:"), _(nocmsgid
), argp
);
675 error_print (_("Error:"), _(nocmsgid
), argp
);
680 if (buffer_flag
== 0)
682 if (warning
&& !warnings_are_errors
)
685 gfc_increment_error_count();
688 return (warning
&& !warnings_are_errors
) ? SUCCESS
: FAILURE
;
692 /* Immediate warning (i.e. do not buffer the warning). */
695 gfc_warning_now (const char *nocmsgid
, ...)
700 if (inhibit_warnings
)
706 if (warnings_are_errors
)
707 gfc_increment_error_count();
709 va_start (argp
, nocmsgid
);
710 error_print (_("Warning:"), _(nocmsgid
), argp
);
718 /* Clear the warning flag. */
721 gfc_clear_warning (void)
723 warning_buffer
.flag
= 0;
727 /* Check to see if any warnings have been saved.
728 If so, print the warning. */
731 gfc_warning_check (void)
733 if (warning_buffer
.flag
)
736 if (warning_buffer
.message
!= NULL
)
737 fputs (warning_buffer
.message
, stderr
);
738 warning_buffer
.flag
= 0;
743 /* Issue an error. */
746 gfc_error (const char *nocmsgid
, ...)
750 if (gfc_suppress_error
)
753 error_buffer
.flag
= 1;
754 error_buffer
.index
= 0;
755 cur_error_buffer
= &error_buffer
;
757 va_start (argp
, nocmsgid
);
758 error_print (_("Error:"), _(nocmsgid
), argp
);
763 if (buffer_flag
== 0)
764 gfc_increment_error_count();
768 /* Immediate error. */
771 gfc_error_now (const char *nocmsgid
, ...)
776 error_buffer
.flag
= 1;
777 error_buffer
.index
= 0;
778 cur_error_buffer
= &error_buffer
;
783 va_start (argp
, nocmsgid
);
784 error_print (_("Error:"), _(nocmsgid
), argp
);
789 gfc_increment_error_count();
793 if (flag_fatal_errors
)
798 /* Fatal error, never returns. */
801 gfc_fatal_error (const char *nocmsgid
, ...)
807 va_start (argp
, nocmsgid
);
808 error_print (_("Fatal Error:"), _(nocmsgid
), argp
);
815 /* This shouldn't happen... but sometimes does. */
818 gfc_internal_error (const char *format
, ...)
824 va_start (argp
, format
);
826 show_loci (&gfc_current_locus
, NULL
);
827 error_printf ("Internal Error at (1):");
829 error_print ("", format
, argp
);
832 exit (ICE_EXIT_CODE
);
836 /* Clear the error flag when we start to compile a source line. */
839 gfc_clear_error (void)
841 error_buffer
.flag
= 0;
845 /* Tests the state of error_flag. */
848 gfc_error_flag_test (void)
850 return error_buffer
.flag
;
854 /* Check to see if any errors have been saved.
855 If so, print the error. Returns the state of error_flag. */
858 gfc_error_check (void)
862 rc
= error_buffer
.flag
;
864 if (error_buffer
.flag
)
866 if (error_buffer
.message
!= NULL
)
867 fputs (error_buffer
.message
, stderr
);
868 error_buffer
.flag
= 0;
870 gfc_increment_error_count();
872 if (flag_fatal_errors
)
880 /* Save the existing error state. */
883 gfc_push_error (gfc_error_buf
*err
)
885 err
->flag
= error_buffer
.flag
;
886 if (error_buffer
.flag
)
887 err
->message
= xstrdup (error_buffer
.message
);
889 error_buffer
.flag
= 0;
893 /* Restore a previous pushed error state. */
896 gfc_pop_error (gfc_error_buf
*err
)
898 error_buffer
.flag
= err
->flag
;
899 if (error_buffer
.flag
)
901 size_t len
= strlen (err
->message
) + 1;
902 gcc_assert (len
<= error_buffer
.allocated
);
903 memcpy (error_buffer
.message
, err
->message
, len
);
904 gfc_free (err
->message
);
909 /* Free a pushed error state, but keep the current error state. */
912 gfc_free_error (gfc_error_buf
*err
)
915 gfc_free (err
->message
);
919 /* Debug wrapper for printf. */
922 gfc_status (const char *cmsgid
, ...)
926 va_start (argp
, cmsgid
);
928 vprintf (_(cmsgid
), argp
);
934 /* Subroutine for outputting a single char so that we don't have to go
935 around creating a lot of 1-character strings. */
938 gfc_status_char (char c
)
944 /* Report the number of warnings and errors that occurred to the caller. */
947 gfc_get_errors (int *w
, int *e
)