2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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 static int suppress_errors
= 0;
35 static int terminal_width
, buffer_flag
, errors
, warnings
;
37 static gfc_error_buf error_buffer
, warning_buffer
, *cur_error_buffer
;
40 /* Go one level deeper suppressing errors. */
43 gfc_push_suppress_errors (void)
45 gcc_assert (suppress_errors
>= 0);
50 /* Leave one level of error suppressing. */
53 gfc_pop_suppress_errors (void)
55 gcc_assert (suppress_errors
> 0);
60 /* Per-file error initialization. */
63 gfc_error_init_1 (void)
65 terminal_width
= gfc_terminal_width ();
72 /* Set the flag for buffering errors or not. */
75 gfc_buffer_error (int flag
)
81 /* Add a single character to the error buffer or output depending on
89 if (cur_error_buffer
->index
>= cur_error_buffer
->allocated
)
91 cur_error_buffer
->allocated
= cur_error_buffer
->allocated
92 ? cur_error_buffer
->allocated
* 2 : 1000;
93 cur_error_buffer
->message
= XRESIZEVEC (char, cur_error_buffer
->message
,
94 cur_error_buffer
->allocated
);
96 cur_error_buffer
->message
[cur_error_buffer
->index
++] = c
;
102 /* We build up complete lines before handing things
103 over to the library in order to speed up error printing. */
105 static size_t allocated
= 0, index
= 0;
107 if (index
+ 1 >= allocated
)
109 allocated
= allocated
? allocated
* 2 : 1000;
110 line
= XRESIZEVEC (char, line
, allocated
);
116 fputs (line
, stderr
);
124 /* Copy a string to wherever it needs to go. */
127 error_string (const char *p
)
134 /* Print a formatted integer to the error buffer or output. */
139 error_uinteger (unsigned long int i
)
141 char *p
, int_buf
[IBUF_LEN
];
143 p
= int_buf
+ IBUF_LEN
- 1;
155 error_string (p
+ 1);
159 error_integer (long int i
)
165 u
= (unsigned long int) -i
;
176 print_wide_char_into_buffer (gfc_char_t c
, char *buf
)
178 static const char xdigit
[16] = { '0', '1', '2', '3', '4', '5', '6',
179 '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
181 if (gfc_wide_is_printable (c
))
184 buf
[0] = (unsigned char) c
;
186 else if (c
< ((gfc_char_t
) 1 << 8))
189 buf
[3] = xdigit
[c
& 0x0F];
191 buf
[2] = xdigit
[c
& 0x0F];
196 else if (c
< ((gfc_char_t
) 1 << 16))
199 buf
[5] = xdigit
[c
& 0x0F];
201 buf
[4] = xdigit
[c
& 0x0F];
203 buf
[3] = xdigit
[c
& 0x0F];
205 buf
[2] = xdigit
[c
& 0x0F];
213 buf
[9] = xdigit
[c
& 0x0F];
215 buf
[8] = xdigit
[c
& 0x0F];
217 buf
[7] = xdigit
[c
& 0x0F];
219 buf
[6] = xdigit
[c
& 0x0F];
221 buf
[5] = xdigit
[c
& 0x0F];
223 buf
[4] = xdigit
[c
& 0x0F];
225 buf
[3] = xdigit
[c
& 0x0F];
227 buf
[2] = xdigit
[c
& 0x0F];
234 static char wide_char_print_buffer
[11];
237 gfc_print_wide_char (gfc_char_t c
)
239 print_wide_char_into_buffer (c
, wide_char_print_buffer
);
240 return wide_char_print_buffer
;
244 /* Show the file, where it was included, and the source line, give a
245 locus. Calls error_printf() recursively, but the recursion is at
246 most one level deep. */
248 static void error_printf (const char *, ...) ATTRIBUTE_GCC_GFC(1,2);
251 show_locus (locus
*loc
, int c1
, int c2
)
258 /* TODO: Either limit the total length and number of included files
259 displayed or add buffering of arbitrary number of characters in
262 /* Write out the error header line, giving the source file and error
263 location (in GNU standard "[file]:[line].[column]:" format),
264 followed by an "included by" stack and a blank line. This header
265 format is matched by a testsuite parser defined in
266 lib/gfortran-dg.exp. */
271 error_string (f
->filename
);
274 error_integer (LOCATION_LINE (lb
->location
));
276 if ((c1
> 0) || (c2
> 0))
282 if ((c1
> 0) && (c2
> 0))
293 i
= f
->inclusion_line
;
296 if (f
== NULL
) break;
298 error_printf (" Included at %s:%d:", f
->filename
, i
);
303 /* Calculate an appropriate horizontal offset of the source line in
304 order to get the error locus within the visible portion of the
305 line. Note that if the margin of 5 here is changed, the
306 corresponding margin of 10 in show_loci should be changed. */
310 /* If the two loci would appear in the same column, we shift
311 '2' one column to the right, so as to print '12' rather than
312 just '1'. We do this here so it will be accounted for in the
313 margin calculations. */
318 cmax
= (c1
< c2
) ? c2
: c1
;
319 if (cmax
> terminal_width
- 5)
320 offset
= cmax
- terminal_width
+ 5;
322 /* Show the line itself, taking care not to print more than what can
323 show up on the terminal. Tabs are converted to spaces, and
324 nonprintable characters are converted to a "\xNN" sequence. */
326 /* TODO: Although setting i to the terminal width is clever, it fails
327 to work correctly when nonprintable characters exist. A better
328 solution should be found. */
330 p
= &(lb
->line
[offset
]);
331 i
= gfc_wide_strlen (p
);
332 if (i
> terminal_width
)
333 i
= terminal_width
- 1;
337 static char buffer
[11];
343 print_wide_char_into_buffer (c
, buffer
);
344 error_string (buffer
);
349 /* Show the '1' and/or '2' corresponding to the column of the error
350 locus. Note that a value of -1 for c1 or c2 will simply cause
351 the relevant number not to be printed. */
356 for (i
= 0; i
<= cmax
; i
++)
371 /* As part of printing an error, we show the source lines that caused
372 the problem. We show at least one, and possibly two loci; the two
373 loci may or may not be on the same source line. */
376 show_loci (locus
*l1
, locus
*l2
)
380 if (l1
== NULL
|| l1
->lb
== NULL
)
382 error_printf ("<During initialization>\n");
386 /* While calculating parameters for printing the loci, we consider possible
387 reasons for printing one per line. If appropriate, print the loci
388 individually; otherwise we print them both on the same line. */
390 c1
= l1
->nextc
- l1
->lb
->line
;
393 show_locus (l1
, c1
, -1);
397 c2
= l2
->nextc
- l2
->lb
->line
;
404 /* Note that the margin value of 10 here needs to be less than the
405 margin of 5 used in the calculation of offset in show_locus. */
407 if (l1
->lb
!= l2
->lb
|| m
> terminal_width
- 10)
409 show_locus (l1
, c1
, -1);
410 show_locus (l2
, -1, c2
);
414 show_locus (l1
, c1
, c2
);
420 /* Workhorse for the error printing subroutines. This subroutine is
421 inspired by g77's error handling and is similar to printf() with
422 the following %-codes:
424 %c Character, %d or %i Integer, %s String, %% Percent
425 %L Takes locus argument
426 %C Current locus (no argument)
428 If a locus pointer is given, the actual source line is printed out
429 and the column is indicated. Since we want the error message at
430 the bottom of any source file information, we must scan the
431 argument list twice -- once to determine whether the loci are
432 present and record this for printing, and once to print the error
433 message after and loci have been printed. A maximum of two locus
434 arguments are permitted.
436 This function is also called (recursively) by show_locus in the
437 case of included files; however, as show_locus does not resupply
438 any loci, the recursion is at most one level deep. */
442 static void ATTRIBUTE_GCC_GFC(2,0)
443 error_print (const char *type
, const char *format0
, va_list argp
)
445 enum { TYPE_CURRENTLOC
, TYPE_LOCUS
, TYPE_INTEGER
, TYPE_UINTEGER
,
446 TYPE_LONGINT
, TYPE_ULONGINT
, TYPE_CHAR
, TYPE_STRING
,
455 unsigned int uintval
;
457 unsigned long int ulongintval
;
459 const char * stringval
;
461 } arg
[MAX_ARGS
], spec
[MAX_ARGS
];
462 /* spec is the array of specifiers, in the same order as they
463 appear in the format string. arg is the array of arguments,
464 in the same order as they appear in the va_list. */
467 int i
, n
, have_l1
, pos
, maxpos
;
468 locus
*l1
, *l2
, *loc
;
480 for (i
= 0; i
< MAX_ARGS
; i
++)
482 arg
[i
].type
= NOTYPE
;
486 /* First parse the format string for position specifiers. */
499 if (ISDIGIT (*format
))
501 /* This is a position specifier. For example, the number
502 12 in the format string "%12$d", which specifies the third
503 argument of the va_list, formatted in %d format.
504 For details, see "man 3 printf". */
505 pos
= atoi(format
) - 1;
506 gcc_assert (pos
>= 0);
507 while (ISDIGIT(*format
))
509 gcc_assert (*format
++ == '$');
522 arg
[pos
].type
= TYPE_CURRENTLOC
;
526 arg
[pos
].type
= TYPE_LOCUS
;
531 arg
[pos
].type
= TYPE_INTEGER
;
535 arg
[pos
].type
= TYPE_UINTEGER
;
540 arg
[pos
].type
= TYPE_ULONGINT
;
541 else if (c
== 'i' || c
== 'd')
542 arg
[pos
].type
= TYPE_LONGINT
;
548 arg
[pos
].type
= TYPE_CHAR
;
552 arg
[pos
].type
= TYPE_STRING
;
562 /* Then convert the values for each %-style argument. */
563 for (pos
= 0; pos
<= maxpos
; pos
++)
565 gcc_assert (arg
[pos
].type
!= NOTYPE
);
566 switch (arg
[pos
].type
)
568 case TYPE_CURRENTLOC
:
569 loc
= &gfc_current_locus
;
573 if (arg
[pos
].type
== TYPE_LOCUS
)
574 loc
= va_arg (argp
, locus
*);
579 arg
[pos
].u
.stringval
= "(2)";
585 arg
[pos
].u
.stringval
= "(1)";
590 arg
[pos
].u
.intval
= va_arg (argp
, int);
594 arg
[pos
].u
.uintval
= va_arg (argp
, unsigned int);
598 arg
[pos
].u
.longintval
= va_arg (argp
, long int);
602 arg
[pos
].u
.ulongintval
= va_arg (argp
, unsigned long int);
606 arg
[pos
].u
.charval
= (char) va_arg (argp
, int);
610 arg
[pos
].u
.stringval
= (const char *) va_arg (argp
, char *);
618 for (n
= 0; spec
[n
].pos
>= 0; n
++)
619 spec
[n
].u
= arg
[spec
[n
].pos
].u
;
621 /* Show the current loci if we have to. */
635 for (; *format
; format
++)
639 error_char (*format
);
644 if (ISDIGIT (*format
))
646 /* This is a position specifier. See comment above. */
647 while (ISDIGIT (*format
))
650 /* Skip over the dollar sign. */
661 error_char (spec
[n
++].u
.charval
);
665 case 'C': /* Current locus */
666 case 'L': /* Specified locus */
667 error_string (spec
[n
++].u
.stringval
);
672 error_integer (spec
[n
++].u
.intval
);
676 error_uinteger (spec
[n
++].u
.uintval
);
682 error_uinteger (spec
[n
++].u
.ulongintval
);
684 error_integer (spec
[n
++].u
.longintval
);
694 /* Wrapper for error_print(). */
697 error_printf (const char *nocmsgid
, ...)
701 va_start (argp
, nocmsgid
);
702 error_print ("", _(nocmsgid
), argp
);
707 /* Increment the number of errors, and check whether too many have
711 gfc_increment_error_count (void)
714 if ((gfc_option
.max_errors
!= 0) && (errors
>= gfc_option
.max_errors
))
715 gfc_fatal_error ("Error count reached limit of %d.", gfc_option
.max_errors
);
719 /* Issue a warning. */
722 gfc_warning (const char *nocmsgid
, ...)
726 if (inhibit_warnings
)
729 warning_buffer
.flag
= 1;
730 warning_buffer
.index
= 0;
731 cur_error_buffer
= &warning_buffer
;
733 va_start (argp
, nocmsgid
);
734 error_print (_("Warning:"), _(nocmsgid
), argp
);
739 if (buffer_flag
== 0)
742 if (warnings_are_errors
)
743 gfc_increment_error_count();
748 /* Whether, for a feature included in a given standard set (GFC_STD_*),
749 we should issue an error or a warning, or be quiet. */
752 gfc_notification_std (int std
)
756 warning
= ((gfc_option
.warn_std
& std
) != 0) && !inhibit_warnings
;
757 if ((gfc_option
.allow_std
& std
) != 0 && !warning
)
760 return warning
? WARNING
: ERROR
;
764 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
765 feature. An error/warning will be issued if the currently selected
766 standard does not contain the requested bits. Return FAILURE if
767 an error is generated. */
770 gfc_notify_std (int std
, const char *nocmsgid
, ...)
775 warning
= ((gfc_option
.warn_std
& std
) != 0) && !inhibit_warnings
;
776 if ((gfc_option
.allow_std
& std
) != 0 && !warning
)
780 return warning
? SUCCESS
: FAILURE
;
782 cur_error_buffer
= warning
? &warning_buffer
: &error_buffer
;
783 cur_error_buffer
->flag
= 1;
784 cur_error_buffer
->index
= 0;
786 va_start (argp
, nocmsgid
);
788 error_print (_("Warning:"), _(nocmsgid
), argp
);
790 error_print (_("Error:"), _(nocmsgid
), argp
);
795 if (buffer_flag
== 0)
797 if (warning
&& !warnings_are_errors
)
800 gfc_increment_error_count();
803 return (warning
&& !warnings_are_errors
) ? SUCCESS
: FAILURE
;
807 /* Immediate warning (i.e. do not buffer the warning). */
810 gfc_warning_now (const char *nocmsgid
, ...)
815 if (inhibit_warnings
)
821 if (warnings_are_errors
)
822 gfc_increment_error_count();
824 va_start (argp
, nocmsgid
);
825 error_print (_("Warning:"), _(nocmsgid
), argp
);
833 /* Clear the warning flag. */
836 gfc_clear_warning (void)
838 warning_buffer
.flag
= 0;
842 /* Check to see if any warnings have been saved.
843 If so, print the warning. */
846 gfc_warning_check (void)
848 if (warning_buffer
.flag
)
851 if (warning_buffer
.message
!= NULL
)
852 fputs (warning_buffer
.message
, stderr
);
853 warning_buffer
.flag
= 0;
858 /* Issue an error. */
861 gfc_error (const char *nocmsgid
, ...)
868 error_buffer
.flag
= 1;
869 error_buffer
.index
= 0;
870 cur_error_buffer
= &error_buffer
;
872 va_start (argp
, nocmsgid
);
873 error_print (_("Error:"), _(nocmsgid
), argp
);
878 if (buffer_flag
== 0)
879 gfc_increment_error_count();
883 /* Immediate error. */
886 gfc_error_now (const char *nocmsgid
, ...)
891 error_buffer
.flag
= 1;
892 error_buffer
.index
= 0;
893 cur_error_buffer
= &error_buffer
;
898 va_start (argp
, nocmsgid
);
899 error_print (_("Error:"), _(nocmsgid
), argp
);
904 gfc_increment_error_count();
908 if (flag_fatal_errors
)
913 /* Fatal error, never returns. */
916 gfc_fatal_error (const char *nocmsgid
, ...)
922 va_start (argp
, nocmsgid
);
923 error_print (_("Fatal Error:"), _(nocmsgid
), argp
);
930 /* This shouldn't happen... but sometimes does. */
933 gfc_internal_error (const char *format
, ...)
939 va_start (argp
, format
);
941 show_loci (&gfc_current_locus
, NULL
);
942 error_printf ("Internal Error at (1):");
944 error_print ("", format
, argp
);
947 exit (ICE_EXIT_CODE
);
951 /* Clear the error flag when we start to compile a source line. */
954 gfc_clear_error (void)
956 error_buffer
.flag
= 0;
960 /* Tests the state of error_flag. */
963 gfc_error_flag_test (void)
965 return error_buffer
.flag
;
969 /* Check to see if any errors have been saved.
970 If so, print the error. Returns the state of error_flag. */
973 gfc_error_check (void)
977 rc
= error_buffer
.flag
;
979 if (error_buffer
.flag
)
981 if (error_buffer
.message
!= NULL
)
982 fputs (error_buffer
.message
, stderr
);
983 error_buffer
.flag
= 0;
985 gfc_increment_error_count();
987 if (flag_fatal_errors
)
995 /* Save the existing error state. */
998 gfc_push_error (gfc_error_buf
*err
)
1000 err
->flag
= error_buffer
.flag
;
1001 if (error_buffer
.flag
)
1002 err
->message
= xstrdup (error_buffer
.message
);
1004 error_buffer
.flag
= 0;
1008 /* Restore a previous pushed error state. */
1011 gfc_pop_error (gfc_error_buf
*err
)
1013 error_buffer
.flag
= err
->flag
;
1014 if (error_buffer
.flag
)
1016 size_t len
= strlen (err
->message
) + 1;
1017 gcc_assert (len
<= error_buffer
.allocated
);
1018 memcpy (error_buffer
.message
, err
->message
, len
);
1019 gfc_free (err
->message
);
1024 /* Free a pushed error state, but keep the current error state. */
1027 gfc_free_error (gfc_error_buf
*err
)
1030 gfc_free (err
->message
);
1034 /* Report the number of warnings and errors that occurred to the caller. */
1037 gfc_get_errors (int *w
, int *e
)