2 Copyright (C) 2000-2013 Free Software Foundation, Inc.
3 Contributed by Andy Vaught & Niels Kristian Bech Jensen
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* Handle the inevitable errors. A major catch here is that things
22 flagged as errors in one match subroutine can conceivably be legal
23 elsewhere. This means that error messages are recorded and saved
24 for possible use later. If a line does not match a legal
25 construction, then the saved error message is reported. */
29 #include "coretypes.h"
37 #ifdef GWINSZ_IN_SYS_IOCTL
38 # include <sys/ioctl.h>
42 static int suppress_errors
= 0;
44 static int warnings_not_errors
= 0;
46 static int terminal_width
, buffer_flag
, errors
, warnings
;
48 static gfc_error_buf error_buffer
, warning_buffer
, *cur_error_buffer
;
51 /* Go one level deeper suppressing errors. */
54 gfc_push_suppress_errors (void)
56 gcc_assert (suppress_errors
>= 0);
61 /* Leave one level of error suppressing. */
64 gfc_pop_suppress_errors (void)
66 gcc_assert (suppress_errors
> 0);
71 /* Determine terminal width (for trimming source lines in output). */
74 get_terminal_width (void)
76 /* Only limit the width if we're outputting to a terminal. */
78 if (!isatty (STDERR_FILENO
))
82 /* Method #1: Use ioctl (not available on all systems). */
86 if (ioctl (0, TIOCGWINSZ
, &w
) == 0 && w
.ws_col
> 0)
90 /* Method #2: Query environment variable $COLUMNS. */
91 const char *p
= getenv ("COLUMNS");
99 /* If both fail, use reasonable default. */
104 /* Per-file error initialization. */
107 gfc_error_init_1 (void)
109 terminal_width
= get_terminal_width ();
116 /* Set the flag for buffering errors or not. */
119 gfc_buffer_error (int flag
)
125 /* Add a single character to the error buffer or output depending on
133 if (cur_error_buffer
->index
>= cur_error_buffer
->allocated
)
135 cur_error_buffer
->allocated
= cur_error_buffer
->allocated
136 ? cur_error_buffer
->allocated
* 2 : 1000;
137 cur_error_buffer
->message
= XRESIZEVEC (char, cur_error_buffer
->message
,
138 cur_error_buffer
->allocated
);
140 cur_error_buffer
->message
[cur_error_buffer
->index
++] = c
;
146 /* We build up complete lines before handing things
147 over to the library in order to speed up error printing. */
149 static size_t allocated
= 0, index
= 0;
151 if (index
+ 1 >= allocated
)
153 allocated
= allocated
? allocated
* 2 : 1000;
154 line
= XRESIZEVEC (char, line
, allocated
);
160 fputs (line
, stderr
);
168 /* Copy a string to wherever it needs to go. */
171 error_string (const char *p
)
178 /* Print a formatted integer to the error buffer or output. */
183 error_uinteger (unsigned long int i
)
185 char *p
, int_buf
[IBUF_LEN
];
187 p
= int_buf
+ IBUF_LEN
- 1;
199 error_string (p
+ 1);
203 error_integer (long int i
)
209 u
= (unsigned long int) -i
;
220 gfc_widechar_display_length (gfc_char_t c
)
222 if (gfc_wide_is_printable (c
) || c
== '\t')
223 /* Printable ASCII character, or tabulation (output as a space). */
225 else if (c
< ((gfc_char_t
) 1 << 8))
226 /* Displayed as \x?? */
228 else if (c
< ((gfc_char_t
) 1 << 16))
229 /* Displayed as \u???? */
232 /* Displayed as \U???????? */
237 /* Length of the ASCII representation of the wide string, escaping wide
238 characters as print_wide_char_into_buffer() does. */
241 gfc_wide_display_length (const gfc_char_t
*str
)
245 for (i
= 0, len
= 0; str
[i
]; i
++)
246 len
+= gfc_widechar_display_length (str
[i
]);
252 print_wide_char_into_buffer (gfc_char_t c
, char *buf
)
254 static const char xdigit
[16] = { '0', '1', '2', '3', '4', '5', '6',
255 '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
257 if (gfc_wide_is_printable (c
) || c
== '\t')
260 /* Tabulation is output as a space. */
261 buf
[0] = (unsigned char) (c
== '\t' ? ' ' : c
);
264 else if (c
< ((gfc_char_t
) 1 << 8))
267 buf
[3] = xdigit
[c
& 0x0F];
269 buf
[2] = xdigit
[c
& 0x0F];
275 else if (c
< ((gfc_char_t
) 1 << 16))
278 buf
[5] = xdigit
[c
& 0x0F];
280 buf
[4] = xdigit
[c
& 0x0F];
282 buf
[3] = xdigit
[c
& 0x0F];
284 buf
[2] = xdigit
[c
& 0x0F];
293 buf
[9] = xdigit
[c
& 0x0F];
295 buf
[8] = xdigit
[c
& 0x0F];
297 buf
[7] = xdigit
[c
& 0x0F];
299 buf
[6] = xdigit
[c
& 0x0F];
301 buf
[5] = xdigit
[c
& 0x0F];
303 buf
[4] = xdigit
[c
& 0x0F];
305 buf
[3] = xdigit
[c
& 0x0F];
307 buf
[2] = xdigit
[c
& 0x0F];
315 static char wide_char_print_buffer
[11];
318 gfc_print_wide_char (gfc_char_t c
)
320 print_wide_char_into_buffer (c
, wide_char_print_buffer
);
321 return wide_char_print_buffer
;
325 /* Show the file, where it was included, and the source line, give a
326 locus. Calls error_printf() recursively, but the recursion is at
327 most one level deep. */
329 static void error_printf (const char *, ...) ATTRIBUTE_GCC_GFC(1,2);
332 show_locus (locus
*loc
, int c1
, int c2
)
339 /* TODO: Either limit the total length and number of included files
340 displayed or add buffering of arbitrary number of characters in
343 /* Write out the error header line, giving the source file and error
344 location (in GNU standard "[file]:[line].[column]:" format),
345 followed by an "included by" stack and a blank line. This header
346 format is matched by a testsuite parser defined in
347 lib/gfortran-dg.exp. */
352 error_string (f
->filename
);
355 error_integer (LOCATION_LINE (lb
->location
));
357 if ((c1
> 0) || (c2
> 0))
363 if ((c1
> 0) && (c2
> 0))
374 i
= f
->inclusion_line
;
377 if (f
== NULL
) break;
379 error_printf (" Included at %s:%d:", f
->filename
, i
);
384 /* Calculate an appropriate horizontal offset of the source line in
385 order to get the error locus within the visible portion of the
386 line. Note that if the margin of 5 here is changed, the
387 corresponding margin of 10 in show_loci should be changed. */
391 /* If the two loci would appear in the same column, we shift
392 '2' one column to the right, so as to print '12' rather than
393 just '1'. We do this here so it will be accounted for in the
394 margin calculations. */
399 cmax
= (c1
< c2
) ? c2
: c1
;
400 if (cmax
> terminal_width
- 5)
401 offset
= cmax
- terminal_width
+ 5;
403 /* Show the line itself, taking care not to print more than what can
404 show up on the terminal. Tabs are converted to spaces, and
405 nonprintable characters are converted to a "\xNN" sequence. */
407 p
= &(lb
->line
[offset
]);
408 i
= gfc_wide_display_length (p
);
409 if (i
> terminal_width
)
410 i
= terminal_width
- 1;
414 static char buffer
[11];
415 i
-= print_wide_char_into_buffer (*p
++, buffer
);
416 error_string (buffer
);
421 /* Show the '1' and/or '2' corresponding to the column of the error
422 locus. Note that a value of -1 for c1 or c2 will simply cause
423 the relevant number not to be printed. */
429 p
= &(lb
->line
[offset
]);
430 for (i
= 0; i
< cmax
; i
++)
433 spaces
= gfc_widechar_display_length (*p
++);
436 error_char ('1'), spaces
--;
438 error_char ('2'), spaces
--;
440 for (j
= 0; j
< spaces
; j
++)
454 /* As part of printing an error, we show the source lines that caused
455 the problem. We show at least one, and possibly two loci; the two
456 loci may or may not be on the same source line. */
459 show_loci (locus
*l1
, locus
*l2
)
463 if (l1
== NULL
|| l1
->lb
== NULL
)
465 error_printf ("<During initialization>\n");
469 /* While calculating parameters for printing the loci, we consider possible
470 reasons for printing one per line. If appropriate, print the loci
471 individually; otherwise we print them both on the same line. */
473 c1
= l1
->nextc
- l1
->lb
->line
;
476 show_locus (l1
, c1
, -1);
480 c2
= l2
->nextc
- l2
->lb
->line
;
487 /* Note that the margin value of 10 here needs to be less than the
488 margin of 5 used in the calculation of offset in show_locus. */
490 if (l1
->lb
!= l2
->lb
|| m
> terminal_width
- 10)
492 show_locus (l1
, c1
, -1);
493 show_locus (l2
, -1, c2
);
497 show_locus (l1
, c1
, c2
);
503 /* Workhorse for the error printing subroutines. This subroutine is
504 inspired by g77's error handling and is similar to printf() with
505 the following %-codes:
507 %c Character, %d or %i Integer, %s String, %% Percent
508 %L Takes locus argument
509 %C Current locus (no argument)
511 If a locus pointer is given, the actual source line is printed out
512 and the column is indicated. Since we want the error message at
513 the bottom of any source file information, we must scan the
514 argument list twice -- once to determine whether the loci are
515 present and record this for printing, and once to print the error
516 message after and loci have been printed. A maximum of two locus
517 arguments are permitted.
519 This function is also called (recursively) by show_locus in the
520 case of included files; however, as show_locus does not resupply
521 any loci, the recursion is at most one level deep. */
525 static void ATTRIBUTE_GCC_GFC(2,0)
526 error_print (const char *type
, const char *format0
, va_list argp
)
528 enum { TYPE_CURRENTLOC
, TYPE_LOCUS
, TYPE_INTEGER
, TYPE_UINTEGER
,
529 TYPE_LONGINT
, TYPE_ULONGINT
, TYPE_CHAR
, TYPE_STRING
,
538 unsigned int uintval
;
540 unsigned long int ulongintval
;
542 const char * stringval
;
544 } arg
[MAX_ARGS
], spec
[MAX_ARGS
];
545 /* spec is the array of specifiers, in the same order as they
546 appear in the format string. arg is the array of arguments,
547 in the same order as they appear in the va_list. */
550 int i
, n
, have_l1
, pos
, maxpos
;
551 locus
*l1
, *l2
, *loc
;
554 loc
= l1
= l2
= NULL
;
563 for (i
= 0; i
< MAX_ARGS
; i
++)
565 arg
[i
].type
= NOTYPE
;
569 /* First parse the format string for position specifiers. */
582 if (ISDIGIT (*format
))
584 /* This is a position specifier. For example, the number
585 12 in the format string "%12$d", which specifies the third
586 argument of the va_list, formatted in %d format.
587 For details, see "man 3 printf". */
588 pos
= atoi(format
) - 1;
589 gcc_assert (pos
>= 0);
590 while (ISDIGIT(*format
))
592 gcc_assert (*format
== '$');
606 arg
[pos
].type
= TYPE_CURRENTLOC
;
610 arg
[pos
].type
= TYPE_LOCUS
;
615 arg
[pos
].type
= TYPE_INTEGER
;
619 arg
[pos
].type
= TYPE_UINTEGER
;
625 arg
[pos
].type
= TYPE_ULONGINT
;
626 else if (c
== 'i' || c
== 'd')
627 arg
[pos
].type
= TYPE_LONGINT
;
633 arg
[pos
].type
= TYPE_CHAR
;
637 arg
[pos
].type
= TYPE_STRING
;
647 /* Then convert the values for each %-style argument. */
648 for (pos
= 0; pos
<= maxpos
; pos
++)
650 gcc_assert (arg
[pos
].type
!= NOTYPE
);
651 switch (arg
[pos
].type
)
653 case TYPE_CURRENTLOC
:
654 loc
= &gfc_current_locus
;
658 if (arg
[pos
].type
== TYPE_LOCUS
)
659 loc
= va_arg (argp
, locus
*);
664 arg
[pos
].u
.stringval
= "(2)";
670 arg
[pos
].u
.stringval
= "(1)";
675 arg
[pos
].u
.intval
= va_arg (argp
, int);
679 arg
[pos
].u
.uintval
= va_arg (argp
, unsigned int);
683 arg
[pos
].u
.longintval
= va_arg (argp
, long int);
687 arg
[pos
].u
.ulongintval
= va_arg (argp
, unsigned long int);
691 arg
[pos
].u
.charval
= (char) va_arg (argp
, int);
695 arg
[pos
].u
.stringval
= (const char *) va_arg (argp
, char *);
703 for (n
= 0; spec
[n
].pos
>= 0; n
++)
704 spec
[n
].u
= arg
[spec
[n
].pos
].u
;
706 /* Show the current loci if we have to. */
720 for (; *format
; format
++)
724 error_char (*format
);
729 if (ISDIGIT (*format
))
731 /* This is a position specifier. See comment above. */
732 while (ISDIGIT (*format
))
735 /* Skip over the dollar sign. */
746 error_char (spec
[n
++].u
.charval
);
750 case 'C': /* Current locus */
751 case 'L': /* Specified locus */
752 error_string (spec
[n
++].u
.stringval
);
757 error_integer (spec
[n
++].u
.intval
);
761 error_uinteger (spec
[n
++].u
.uintval
);
767 error_uinteger (spec
[n
++].u
.ulongintval
);
769 error_integer (spec
[n
++].u
.longintval
);
779 /* Wrapper for error_print(). */
782 error_printf (const char *gmsgid
, ...)
786 va_start (argp
, gmsgid
);
787 error_print ("", _(gmsgid
), argp
);
792 /* Increment the number of errors, and check whether too many have
796 gfc_increment_error_count (void)
799 if ((gfc_option
.max_errors
!= 0) && (errors
>= gfc_option
.max_errors
))
800 gfc_fatal_error ("Error count reached limit of %d.", gfc_option
.max_errors
);
804 /* Issue a warning. */
807 gfc_warning (const char *gmsgid
, ...)
811 if (inhibit_warnings
)
814 warning_buffer
.flag
= 1;
815 warning_buffer
.index
= 0;
816 cur_error_buffer
= &warning_buffer
;
818 va_start (argp
, gmsgid
);
819 error_print (_("Warning:"), _(gmsgid
), argp
);
824 if (buffer_flag
== 0)
827 if (warnings_are_errors
)
828 gfc_increment_error_count();
833 /* Whether, for a feature included in a given standard set (GFC_STD_*),
834 we should issue an error or a warning, or be quiet. */
837 gfc_notification_std (int std
)
841 warning
= ((gfc_option
.warn_std
& std
) != 0) && !inhibit_warnings
;
842 if ((gfc_option
.allow_std
& std
) != 0 && !warning
)
845 return warning
? WARNING
: ERROR
;
849 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
850 feature. An error/warning will be issued if the currently selected
851 standard does not contain the requested bits. Return false if
852 an error is generated. */
855 gfc_notify_std (int std
, const char *gmsgid
, ...)
859 const char *msg1
, *msg2
;
862 warning
= ((gfc_option
.warn_std
& std
) != 0) && !inhibit_warnings
;
863 if ((gfc_option
.allow_std
& std
) != 0 && !warning
)
867 return warning
? true : false;
869 cur_error_buffer
= warning
? &warning_buffer
: &error_buffer
;
870 cur_error_buffer
->flag
= 1;
871 cur_error_buffer
->index
= 0;
874 msg1
= _("Warning:");
880 case GFC_STD_F2008_TS
:
883 case GFC_STD_F2008_OBS
:
884 msg2
= _("Fortran 2008 obsolescent feature:");
887 msg2
= "Fortran 2008:";
890 msg2
= "Fortran 2003:";
893 msg2
= _("GNU Extension:");
896 msg2
= _("Legacy Extension:");
898 case GFC_STD_F95_OBS
:
899 msg2
= _("Obsolescent feature:");
901 case GFC_STD_F95_DEL
:
902 msg2
= _("Deleted feature:");
908 buffer
= (char *) alloca (strlen (msg1
) + strlen (msg2
) + 2);
909 strcpy (buffer
, msg1
);
910 strcat (buffer
, " ");
911 strcat (buffer
, msg2
);
913 va_start (argp
, gmsgid
);
914 error_print (buffer
, _(gmsgid
), argp
);
919 if (buffer_flag
== 0)
921 if (warning
&& !warnings_are_errors
)
924 gfc_increment_error_count();
925 cur_error_buffer
->flag
= 0;
928 return (warning
&& !warnings_are_errors
) ? true : false;
932 /* Immediate warning (i.e. do not buffer the warning). */
935 gfc_warning_now (const char *gmsgid
, ...)
940 if (inhibit_warnings
)
947 va_start (argp
, gmsgid
);
948 error_print (_("Warning:"), _(gmsgid
), argp
);
953 if (warnings_are_errors
)
954 gfc_increment_error_count();
960 /* Clear the warning flag. */
963 gfc_clear_warning (void)
965 warning_buffer
.flag
= 0;
969 /* Check to see if any warnings have been saved.
970 If so, print the warning. */
973 gfc_warning_check (void)
975 if (warning_buffer
.flag
)
978 if (warning_buffer
.message
!= NULL
)
979 fputs (warning_buffer
.message
, stderr
);
980 warning_buffer
.flag
= 0;
985 /* Issue an error. */
988 gfc_error (const char *gmsgid
, ...)
992 if (warnings_not_errors
)
998 error_buffer
.flag
= 1;
999 error_buffer
.index
= 0;
1000 cur_error_buffer
= &error_buffer
;
1002 va_start (argp
, gmsgid
);
1003 error_print (_("Error:"), _(gmsgid
), argp
);
1008 if (buffer_flag
== 0)
1009 gfc_increment_error_count();
1015 if (inhibit_warnings
)
1018 warning_buffer
.flag
= 1;
1019 warning_buffer
.index
= 0;
1020 cur_error_buffer
= &warning_buffer
;
1022 va_start (argp
, gmsgid
);
1023 error_print (_("Warning:"), _(gmsgid
), argp
);
1028 if (buffer_flag
== 0)
1031 if (warnings_are_errors
)
1032 gfc_increment_error_count();
1037 /* Immediate error. */
1040 gfc_error_now (const char *gmsgid
, ...)
1045 error_buffer
.flag
= 1;
1046 error_buffer
.index
= 0;
1047 cur_error_buffer
= &error_buffer
;
1052 va_start (argp
, gmsgid
);
1053 error_print (_("Error:"), _(gmsgid
), argp
);
1058 gfc_increment_error_count();
1062 if (flag_fatal_errors
)
1063 exit (FATAL_EXIT_CODE
);
1067 /* Fatal error, never returns. */
1070 gfc_fatal_error (const char *gmsgid
, ...)
1076 va_start (argp
, gmsgid
);
1077 error_print (_("Fatal Error:"), _(gmsgid
), argp
);
1080 exit (FATAL_EXIT_CODE
);
1084 /* This shouldn't happen... but sometimes does. */
1087 gfc_internal_error (const char *format
, ...)
1093 va_start (argp
, format
);
1095 show_loci (&gfc_current_locus
, NULL
);
1096 error_printf ("Internal Error at (1):");
1098 error_print ("", format
, argp
);
1101 exit (ICE_EXIT_CODE
);
1105 /* Clear the error flag when we start to compile a source line. */
1108 gfc_clear_error (void)
1110 error_buffer
.flag
= 0;
1111 warnings_not_errors
= 0;
1115 /* Tests the state of error_flag. */
1118 gfc_error_flag_test (void)
1120 return error_buffer
.flag
;
1124 /* Check to see if any errors have been saved.
1125 If so, print the error. Returns the state of error_flag. */
1128 gfc_error_check (void)
1132 rc
= error_buffer
.flag
;
1134 if (error_buffer
.flag
)
1136 if (error_buffer
.message
!= NULL
)
1137 fputs (error_buffer
.message
, stderr
);
1138 error_buffer
.flag
= 0;
1140 gfc_increment_error_count();
1142 if (flag_fatal_errors
)
1143 exit (FATAL_EXIT_CODE
);
1150 /* Save the existing error state. */
1153 gfc_push_error (gfc_error_buf
*err
)
1155 err
->flag
= error_buffer
.flag
;
1156 if (error_buffer
.flag
)
1157 err
->message
= xstrdup (error_buffer
.message
);
1159 error_buffer
.flag
= 0;
1163 /* Restore a previous pushed error state. */
1166 gfc_pop_error (gfc_error_buf
*err
)
1168 error_buffer
.flag
= err
->flag
;
1169 if (error_buffer
.flag
)
1171 size_t len
= strlen (err
->message
) + 1;
1172 gcc_assert (len
<= error_buffer
.allocated
);
1173 memcpy (error_buffer
.message
, err
->message
, len
);
1174 free (err
->message
);
1179 /* Free a pushed error state, but keep the current error state. */
1182 gfc_free_error (gfc_error_buf
*err
)
1185 free (err
->message
);
1189 /* Report the number of warnings and errors that occurred to the caller. */
1192 gfc_get_errors (int *w
, int *e
)
1201 /* Switch errors into warnings. */
1204 gfc_errors_to_warnings (int f
)
1206 warnings_not_errors
= (f
== 1) ? 1 : 0;