2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 Free Software Foundation, Inc.
5 Contributed by Andy Vaught & Niels Kristian Bech Jensen
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
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. */
34 static int suppress_errors
= 0;
36 static int warnings_not_errors
= 0;
38 static int terminal_width
, buffer_flag
, errors
, warnings
;
40 static gfc_error_buf error_buffer
, warning_buffer
, *cur_error_buffer
;
43 /* Go one level deeper suppressing errors. */
46 gfc_push_suppress_errors (void)
48 gcc_assert (suppress_errors
>= 0);
53 /* Leave one level of error suppressing. */
56 gfc_pop_suppress_errors (void)
58 gcc_assert (suppress_errors
> 0);
63 /* Per-file error initialization. */
66 gfc_error_init_1 (void)
68 terminal_width
= gfc_terminal_width ();
75 /* Set the flag for buffering errors or not. */
78 gfc_buffer_error (int flag
)
84 /* Add a single character to the error buffer or output depending on
92 if (cur_error_buffer
->index
>= cur_error_buffer
->allocated
)
94 cur_error_buffer
->allocated
= cur_error_buffer
->allocated
95 ? cur_error_buffer
->allocated
* 2 : 1000;
96 cur_error_buffer
->message
= XRESIZEVEC (char, cur_error_buffer
->message
,
97 cur_error_buffer
->allocated
);
99 cur_error_buffer
->message
[cur_error_buffer
->index
++] = c
;
105 /* We build up complete lines before handing things
106 over to the library in order to speed up error printing. */
108 static size_t allocated
= 0, index
= 0;
110 if (index
+ 1 >= allocated
)
112 allocated
= allocated
? allocated
* 2 : 1000;
113 line
= XRESIZEVEC (char, line
, allocated
);
119 fputs (line
, stderr
);
127 /* Copy a string to wherever it needs to go. */
130 error_string (const char *p
)
137 /* Print a formatted integer to the error buffer or output. */
142 error_uinteger (unsigned long int i
)
144 char *p
, int_buf
[IBUF_LEN
];
146 p
= int_buf
+ IBUF_LEN
- 1;
158 error_string (p
+ 1);
162 error_integer (long int i
)
168 u
= (unsigned long int) -i
;
179 gfc_widechar_display_length (gfc_char_t c
)
181 if (gfc_wide_is_printable (c
) || c
== '\t')
182 /* Printable ASCII character, or tabulation (output as a space). */
184 else if (c
< ((gfc_char_t
) 1 << 8))
185 /* Displayed as \x?? */
187 else if (c
< ((gfc_char_t
) 1 << 16))
188 /* Displayed as \u???? */
191 /* Displayed as \U???????? */
196 /* Length of the ASCII representation of the wide string, escaping wide
197 characters as print_wide_char_into_buffer() does. */
200 gfc_wide_display_length (const gfc_char_t
*str
)
204 for (i
= 0, len
= 0; str
[i
]; i
++)
205 len
+= gfc_widechar_display_length (str
[i
]);
211 print_wide_char_into_buffer (gfc_char_t c
, char *buf
)
213 static const char xdigit
[16] = { '0', '1', '2', '3', '4', '5', '6',
214 '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
216 if (gfc_wide_is_printable (c
) || c
== '\t')
219 /* Tabulation is output as a space. */
220 buf
[0] = (unsigned char) (c
== '\t' ? ' ' : c
);
223 else if (c
< ((gfc_char_t
) 1 << 8))
226 buf
[3] = xdigit
[c
& 0x0F];
228 buf
[2] = xdigit
[c
& 0x0F];
234 else if (c
< ((gfc_char_t
) 1 << 16))
237 buf
[5] = xdigit
[c
& 0x0F];
239 buf
[4] = xdigit
[c
& 0x0F];
241 buf
[3] = xdigit
[c
& 0x0F];
243 buf
[2] = xdigit
[c
& 0x0F];
252 buf
[9] = xdigit
[c
& 0x0F];
254 buf
[8] = xdigit
[c
& 0x0F];
256 buf
[7] = xdigit
[c
& 0x0F];
258 buf
[6] = xdigit
[c
& 0x0F];
260 buf
[5] = xdigit
[c
& 0x0F];
262 buf
[4] = xdigit
[c
& 0x0F];
264 buf
[3] = xdigit
[c
& 0x0F];
266 buf
[2] = xdigit
[c
& 0x0F];
274 static char wide_char_print_buffer
[11];
277 gfc_print_wide_char (gfc_char_t c
)
279 print_wide_char_into_buffer (c
, wide_char_print_buffer
);
280 return wide_char_print_buffer
;
284 /* Show the file, where it was included, and the source line, give a
285 locus. Calls error_printf() recursively, but the recursion is at
286 most one level deep. */
288 static void error_printf (const char *, ...) ATTRIBUTE_GCC_GFC(1,2);
291 show_locus (locus
*loc
, int c1
, int c2
)
298 /* TODO: Either limit the total length and number of included files
299 displayed or add buffering of arbitrary number of characters in
302 /* Write out the error header line, giving the source file and error
303 location (in GNU standard "[file]:[line].[column]:" format),
304 followed by an "included by" stack and a blank line. This header
305 format is matched by a testsuite parser defined in
306 lib/gfortran-dg.exp. */
311 error_string (f
->filename
);
314 error_integer (LOCATION_LINE (lb
->location
));
316 if ((c1
> 0) || (c2
> 0))
322 if ((c1
> 0) && (c2
> 0))
333 i
= f
->inclusion_line
;
336 if (f
== NULL
) break;
338 error_printf (" Included at %s:%d:", f
->filename
, i
);
343 /* Calculate an appropriate horizontal offset of the source line in
344 order to get the error locus within the visible portion of the
345 line. Note that if the margin of 5 here is changed, the
346 corresponding margin of 10 in show_loci should be changed. */
350 /* If the two loci would appear in the same column, we shift
351 '2' one column to the right, so as to print '12' rather than
352 just '1'. We do this here so it will be accounted for in the
353 margin calculations. */
358 cmax
= (c1
< c2
) ? c2
: c1
;
359 if (cmax
> terminal_width
- 5)
360 offset
= cmax
- terminal_width
+ 5;
362 /* Show the line itself, taking care not to print more than what can
363 show up on the terminal. Tabs are converted to spaces, and
364 nonprintable characters are converted to a "\xNN" sequence. */
366 p
= &(lb
->line
[offset
]);
367 i
= gfc_wide_display_length (p
);
368 if (i
> terminal_width
)
369 i
= terminal_width
- 1;
373 static char buffer
[11];
374 i
-= print_wide_char_into_buffer (*p
++, buffer
);
375 error_string (buffer
);
380 /* Show the '1' and/or '2' corresponding to the column of the error
381 locus. Note that a value of -1 for c1 or c2 will simply cause
382 the relevant number not to be printed. */
387 p
= &(lb
->line
[offset
]);
388 for (i
= 0; i
<= cmax
; i
++)
391 spaces
= gfc_widechar_display_length (*p
++);
394 error_char ('1'), spaces
--;
396 error_char ('2'), spaces
--;
398 for (j
= 0; j
< spaces
; j
++)
407 /* As part of printing an error, we show the source lines that caused
408 the problem. We show at least one, and possibly two loci; the two
409 loci may or may not be on the same source line. */
412 show_loci (locus
*l1
, locus
*l2
)
416 if (l1
== NULL
|| l1
->lb
== NULL
)
418 error_printf ("<During initialization>\n");
422 /* While calculating parameters for printing the loci, we consider possible
423 reasons for printing one per line. If appropriate, print the loci
424 individually; otherwise we print them both on the same line. */
426 c1
= l1
->nextc
- l1
->lb
->line
;
429 show_locus (l1
, c1
, -1);
433 c2
= l2
->nextc
- l2
->lb
->line
;
440 /* Note that the margin value of 10 here needs to be less than the
441 margin of 5 used in the calculation of offset in show_locus. */
443 if (l1
->lb
!= l2
->lb
|| m
> terminal_width
- 10)
445 show_locus (l1
, c1
, -1);
446 show_locus (l2
, -1, c2
);
450 show_locus (l1
, c1
, c2
);
456 /* Workhorse for the error printing subroutines. This subroutine is
457 inspired by g77's error handling and is similar to printf() with
458 the following %-codes:
460 %c Character, %d or %i Integer, %s String, %% Percent
461 %L Takes locus argument
462 %C Current locus (no argument)
464 If a locus pointer is given, the actual source line is printed out
465 and the column is indicated. Since we want the error message at
466 the bottom of any source file information, we must scan the
467 argument list twice -- once to determine whether the loci are
468 present and record this for printing, and once to print the error
469 message after and loci have been printed. A maximum of two locus
470 arguments are permitted.
472 This function is also called (recursively) by show_locus in the
473 case of included files; however, as show_locus does not resupply
474 any loci, the recursion is at most one level deep. */
478 static void ATTRIBUTE_GCC_GFC(2,0)
479 error_print (const char *type
, const char *format0
, va_list argp
)
481 enum { TYPE_CURRENTLOC
, TYPE_LOCUS
, TYPE_INTEGER
, TYPE_UINTEGER
,
482 TYPE_LONGINT
, TYPE_ULONGINT
, TYPE_CHAR
, TYPE_STRING
,
491 unsigned int uintval
;
493 unsigned long int ulongintval
;
495 const char * stringval
;
497 } arg
[MAX_ARGS
], spec
[MAX_ARGS
];
498 /* spec is the array of specifiers, in the same order as they
499 appear in the format string. arg is the array of arguments,
500 in the same order as they appear in the va_list. */
503 int i
, n
, have_l1
, pos
, maxpos
;
504 locus
*l1
, *l2
, *loc
;
507 loc
= l1
= l2
= NULL
;
516 for (i
= 0; i
< MAX_ARGS
; i
++)
518 arg
[i
].type
= NOTYPE
;
522 /* First parse the format string for position specifiers. */
535 if (ISDIGIT (*format
))
537 /* This is a position specifier. For example, the number
538 12 in the format string "%12$d", which specifies the third
539 argument of the va_list, formatted in %d format.
540 For details, see "man 3 printf". */
541 pos
= atoi(format
) - 1;
542 gcc_assert (pos
>= 0);
543 while (ISDIGIT(*format
))
545 gcc_assert (*format
++ == '$');
558 arg
[pos
].type
= TYPE_CURRENTLOC
;
562 arg
[pos
].type
= TYPE_LOCUS
;
567 arg
[pos
].type
= TYPE_INTEGER
;
571 arg
[pos
].type
= TYPE_UINTEGER
;
577 arg
[pos
].type
= TYPE_ULONGINT
;
578 else if (c
== 'i' || c
== 'd')
579 arg
[pos
].type
= TYPE_LONGINT
;
585 arg
[pos
].type
= TYPE_CHAR
;
589 arg
[pos
].type
= TYPE_STRING
;
599 /* Then convert the values for each %-style argument. */
600 for (pos
= 0; pos
<= maxpos
; pos
++)
602 gcc_assert (arg
[pos
].type
!= NOTYPE
);
603 switch (arg
[pos
].type
)
605 case TYPE_CURRENTLOC
:
606 loc
= &gfc_current_locus
;
610 if (arg
[pos
].type
== TYPE_LOCUS
)
611 loc
= va_arg (argp
, locus
*);
616 arg
[pos
].u
.stringval
= "(2)";
622 arg
[pos
].u
.stringval
= "(1)";
627 arg
[pos
].u
.intval
= va_arg (argp
, int);
631 arg
[pos
].u
.uintval
= va_arg (argp
, unsigned int);
635 arg
[pos
].u
.longintval
= va_arg (argp
, long int);
639 arg
[pos
].u
.ulongintval
= va_arg (argp
, unsigned long int);
643 arg
[pos
].u
.charval
= (char) va_arg (argp
, int);
647 arg
[pos
].u
.stringval
= (const char *) va_arg (argp
, char *);
655 for (n
= 0; spec
[n
].pos
>= 0; n
++)
656 spec
[n
].u
= arg
[spec
[n
].pos
].u
;
658 /* Show the current loci if we have to. */
672 for (; *format
; format
++)
676 error_char (*format
);
681 if (ISDIGIT (*format
))
683 /* This is a position specifier. See comment above. */
684 while (ISDIGIT (*format
))
687 /* Skip over the dollar sign. */
698 error_char (spec
[n
++].u
.charval
);
702 case 'C': /* Current locus */
703 case 'L': /* Specified locus */
704 error_string (spec
[n
++].u
.stringval
);
709 error_integer (spec
[n
++].u
.intval
);
713 error_uinteger (spec
[n
++].u
.uintval
);
719 error_uinteger (spec
[n
++].u
.ulongintval
);
721 error_integer (spec
[n
++].u
.longintval
);
731 /* Wrapper for error_print(). */
734 error_printf (const char *gmsgid
, ...)
738 va_start (argp
, gmsgid
);
739 error_print ("", _(gmsgid
), argp
);
744 /* Increment the number of errors, and check whether too many have
748 gfc_increment_error_count (void)
751 if ((gfc_option
.max_errors
!= 0) && (errors
>= gfc_option
.max_errors
))
752 gfc_fatal_error ("Error count reached limit of %d.", gfc_option
.max_errors
);
756 /* Issue a warning. */
759 gfc_warning (const char *gmsgid
, ...)
763 if (inhibit_warnings
)
766 warning_buffer
.flag
= 1;
767 warning_buffer
.index
= 0;
768 cur_error_buffer
= &warning_buffer
;
770 va_start (argp
, gmsgid
);
771 error_print (_("Warning:"), _(gmsgid
), argp
);
776 if (buffer_flag
== 0)
779 if (warnings_are_errors
)
780 gfc_increment_error_count();
785 /* Whether, for a feature included in a given standard set (GFC_STD_*),
786 we should issue an error or a warning, or be quiet. */
789 gfc_notification_std (int std
)
793 warning
= ((gfc_option
.warn_std
& std
) != 0) && !inhibit_warnings
;
794 if ((gfc_option
.allow_std
& std
) != 0 && !warning
)
797 return warning
? WARNING
: ERROR
;
801 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
802 feature. An error/warning will be issued if the currently selected
803 standard does not contain the requested bits. Return FAILURE if
804 an error is generated. */
807 gfc_notify_std (int std
, const char *gmsgid
, ...)
812 warning
= ((gfc_option
.warn_std
& std
) != 0) && !inhibit_warnings
;
813 if ((gfc_option
.allow_std
& std
) != 0 && !warning
)
817 return warning
? SUCCESS
: FAILURE
;
819 cur_error_buffer
= warning
? &warning_buffer
: &error_buffer
;
820 cur_error_buffer
->flag
= 1;
821 cur_error_buffer
->index
= 0;
823 va_start (argp
, gmsgid
);
825 error_print (_("Warning:"), _(gmsgid
), argp
);
827 error_print (_("Error:"), _(gmsgid
), argp
);
832 if (buffer_flag
== 0)
834 if (warning
&& !warnings_are_errors
)
837 gfc_increment_error_count();
840 return (warning
&& !warnings_are_errors
) ? SUCCESS
: FAILURE
;
844 /* Immediate warning (i.e. do not buffer the warning). */
847 gfc_warning_now (const char *gmsgid
, ...)
852 if (inhibit_warnings
)
859 va_start (argp
, gmsgid
);
860 error_print (_("Warning:"), _(gmsgid
), argp
);
865 if (warnings_are_errors
)
866 gfc_increment_error_count();
872 /* Clear the warning flag. */
875 gfc_clear_warning (void)
877 warning_buffer
.flag
= 0;
881 /* Check to see if any warnings have been saved.
882 If so, print the warning. */
885 gfc_warning_check (void)
887 if (warning_buffer
.flag
)
890 if (warning_buffer
.message
!= NULL
)
891 fputs (warning_buffer
.message
, stderr
);
892 warning_buffer
.flag
= 0;
897 /* Issue an error. */
900 gfc_error (const char *gmsgid
, ...)
904 if (warnings_not_errors
)
910 error_buffer
.flag
= 1;
911 error_buffer
.index
= 0;
912 cur_error_buffer
= &error_buffer
;
914 va_start (argp
, gmsgid
);
915 error_print (_("Error:"), _(gmsgid
), argp
);
920 if (buffer_flag
== 0)
921 gfc_increment_error_count();
927 if (inhibit_warnings
)
930 warning_buffer
.flag
= 1;
931 warning_buffer
.index
= 0;
932 cur_error_buffer
= &warning_buffer
;
934 va_start (argp
, gmsgid
);
935 error_print (_("Warning:"), _(gmsgid
), argp
);
940 if (buffer_flag
== 0)
943 if (warnings_are_errors
)
944 gfc_increment_error_count();
949 /* Immediate error. */
952 gfc_error_now (const char *gmsgid
, ...)
957 error_buffer
.flag
= 1;
958 error_buffer
.index
= 0;
959 cur_error_buffer
= &error_buffer
;
964 va_start (argp
, gmsgid
);
965 error_print (_("Error:"), _(gmsgid
), argp
);
970 gfc_increment_error_count();
974 if (flag_fatal_errors
)
975 exit (FATAL_EXIT_CODE
);
979 /* Fatal error, never returns. */
982 gfc_fatal_error (const char *gmsgid
, ...)
988 va_start (argp
, gmsgid
);
989 error_print (_("Fatal Error:"), _(gmsgid
), argp
);
992 exit (FATAL_EXIT_CODE
);
996 /* This shouldn't happen... but sometimes does. */
999 gfc_internal_error (const char *format
, ...)
1005 va_start (argp
, format
);
1007 show_loci (&gfc_current_locus
, NULL
);
1008 error_printf ("Internal Error at (1):");
1010 error_print ("", format
, argp
);
1013 exit (ICE_EXIT_CODE
);
1017 /* Clear the error flag when we start to compile a source line. */
1020 gfc_clear_error (void)
1022 error_buffer
.flag
= 0;
1023 warnings_not_errors
= 0;
1027 /* Tests the state of error_flag. */
1030 gfc_error_flag_test (void)
1032 return error_buffer
.flag
;
1036 /* Check to see if any errors have been saved.
1037 If so, print the error. Returns the state of error_flag. */
1040 gfc_error_check (void)
1044 rc
= error_buffer
.flag
;
1046 if (error_buffer
.flag
)
1048 if (error_buffer
.message
!= NULL
)
1049 fputs (error_buffer
.message
, stderr
);
1050 error_buffer
.flag
= 0;
1052 gfc_increment_error_count();
1054 if (flag_fatal_errors
)
1055 exit (FATAL_EXIT_CODE
);
1062 /* Save the existing error state. */
1065 gfc_push_error (gfc_error_buf
*err
)
1067 err
->flag
= error_buffer
.flag
;
1068 if (error_buffer
.flag
)
1069 err
->message
= xstrdup (error_buffer
.message
);
1071 error_buffer
.flag
= 0;
1075 /* Restore a previous pushed error state. */
1078 gfc_pop_error (gfc_error_buf
*err
)
1080 error_buffer
.flag
= err
->flag
;
1081 if (error_buffer
.flag
)
1083 size_t len
= strlen (err
->message
) + 1;
1084 gcc_assert (len
<= error_buffer
.allocated
);
1085 memcpy (error_buffer
.message
, err
->message
, len
);
1086 free (err
->message
);
1091 /* Free a pushed error state, but keep the current error state. */
1094 gfc_free_error (gfc_error_buf
*err
)
1097 free (err
->message
);
1101 /* Report the number of warnings and errors that occurred to the caller. */
1104 gfc_get_errors (int *w
, int *e
)
1113 /* Switch errors into warnings. */
1116 gfc_errors_to_warnings (int f
)
1118 warnings_not_errors
= (f
== 1) ? 1 : 0;