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 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_uinteger (unsigned long int i
)
121 char *p
, int_buf
[IBUF_LEN
];
123 p
= int_buf
+ IBUF_LEN
- 1;
135 error_string (p
+ 1);
139 error_integer (long int i
)
145 u
= (unsigned long int) -i
;
156 print_wide_char_into_buffer (gfc_char_t c
, char *buf
)
158 static const char xdigit
[16] = { '0', '1', '2', '3', '4', '5', '6',
159 '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
161 if (gfc_wide_is_printable (c
))
164 buf
[0] = (unsigned char) c
;
166 else if (c
< ((gfc_char_t
) 1 << 8))
169 buf
[3] = xdigit
[c
& 0x0F];
171 buf
[2] = xdigit
[c
& 0x0F];
176 else if (c
< ((gfc_char_t
) 1 << 16))
179 buf
[5] = xdigit
[c
& 0x0F];
181 buf
[4] = xdigit
[c
& 0x0F];
183 buf
[3] = xdigit
[c
& 0x0F];
185 buf
[2] = xdigit
[c
& 0x0F];
193 buf
[9] = xdigit
[c
& 0x0F];
195 buf
[8] = xdigit
[c
& 0x0F];
197 buf
[7] = xdigit
[c
& 0x0F];
199 buf
[6] = xdigit
[c
& 0x0F];
201 buf
[5] = xdigit
[c
& 0x0F];
203 buf
[4] = xdigit
[c
& 0x0F];
205 buf
[3] = xdigit
[c
& 0x0F];
207 buf
[2] = xdigit
[c
& 0x0F];
214 static char wide_char_print_buffer
[11];
217 gfc_print_wide_char (gfc_char_t c
)
219 print_wide_char_into_buffer (c
, wide_char_print_buffer
);
220 return wide_char_print_buffer
;
224 /* Show the file, where it was included, and the source line, give a
225 locus. Calls error_printf() recursively, but the recursion is at
226 most one level deep. */
228 static void error_printf (const char *, ...) ATTRIBUTE_GCC_GFC(1,2);
231 show_locus (locus
*loc
, int c1
, int c2
)
238 /* TODO: Either limit the total length and number of included files
239 displayed or add buffering of arbitrary number of characters in
242 /* Write out the error header line, giving the source file and error
243 location (in GNU standard "[file]:[line].[column]:" format),
244 followed by an "included by" stack and a blank line. This header
245 format is matched by a testsuite parser defined in
246 lib/gfortran-dg.exp. */
251 error_string (f
->filename
);
254 error_integer (LOCATION_LINE (lb
->location
));
256 if ((c1
> 0) || (c2
> 0))
262 if ((c1
> 0) && (c2
> 0))
273 i
= f
->inclusion_line
;
276 if (f
== NULL
) break;
278 error_printf (" Included at %s:%d:", f
->filename
, i
);
283 /* Calculate an appropriate horizontal offset of the source line in
284 order to get the error locus within the visible portion of the
285 line. Note that if the margin of 5 here is changed, the
286 corresponding margin of 10 in show_loci should be changed. */
290 /* When the loci is not associated with a column, it will have a
291 value of zero. We adjust this to 1 so that it will appear. */
298 /* If the two loci would appear in the same column, we shift
299 '2' one column to the right, so as to print '12' rather than
300 just '1'. We do this here so it will be accounted for in the
301 margin calculations. */
306 cmax
= (c1
< c2
) ? c2
: c1
;
307 if (cmax
> terminal_width
- 5)
308 offset
= cmax
- terminal_width
+ 5;
310 /* Show the line itself, taking care not to print more than what can
311 show up on the terminal. Tabs are converted to spaces, and
312 nonprintable characters are converted to a "\xNN" sequence. */
314 /* TODO: Although setting i to the terminal width is clever, it fails
315 to work correctly when nonprintable characters exist. A better
316 solution should be found. */
318 p
= &(lb
->line
[offset
]);
319 i
= gfc_wide_strlen (p
);
320 if (i
> terminal_width
)
321 i
= terminal_width
- 1;
325 static char buffer
[11];
331 print_wide_char_into_buffer (c
, buffer
);
332 error_string (buffer
);
337 /* Show the '1' and/or '2' corresponding to the column of the error
338 locus. Note that a value of -1 for c1 or c2 will simply cause
339 the relevant number not to be printed. */
344 for (i
= 1; i
<= cmax
; i
++)
359 /* As part of printing an error, we show the source lines that caused
360 the problem. We show at least one, and possibly two loci; the two
361 loci may or may not be on the same source line. */
364 show_loci (locus
*l1
, locus
*l2
)
368 if (l1
== NULL
|| l1
->lb
== NULL
)
370 error_printf ("<During initialization>\n");
374 /* While calculating parameters for printing the loci, we consider possible
375 reasons for printing one per line. If appropriate, print the loci
376 individually; otherwise we print them both on the same line. */
378 c1
= l1
->nextc
- l1
->lb
->line
;
381 show_locus (l1
, c1
, -1);
385 c2
= l2
->nextc
- l2
->lb
->line
;
392 /* Note that the margin value of 10 here needs to be less than the
393 margin of 5 used in the calculation of offset in show_locus. */
395 if (l1
->lb
!= l2
->lb
|| m
> terminal_width
- 10)
397 show_locus (l1
, c1
, -1);
398 show_locus (l2
, -1, c2
);
402 show_locus (l1
, c1
, c2
);
408 /* Workhorse for the error printing subroutines. This subroutine is
409 inspired by g77's error handling and is similar to printf() with
410 the following %-codes:
412 %c Character, %d or %i Integer, %s String, %% Percent
413 %L Takes locus argument
414 %C Current locus (no argument)
416 If a locus pointer is given, the actual source line is printed out
417 and the column is indicated. Since we want the error message at
418 the bottom of any source file information, we must scan the
419 argument list twice -- once to determine whether the loci are
420 present and record this for printing, and once to print the error
421 message after and loci have been printed. A maximum of two locus
422 arguments are permitted.
424 This function is also called (recursively) by show_locus in the
425 case of included files; however, as show_locus does not resupply
426 any loci, the recursion is at most one level deep. */
430 static void ATTRIBUTE_GCC_GFC(2,0)
431 error_print (const char *type
, const char *format0
, va_list argp
)
433 enum { TYPE_CURRENTLOC
, TYPE_LOCUS
, TYPE_INTEGER
, TYPE_UINTEGER
,
434 TYPE_LONGINT
, TYPE_ULONGINT
, TYPE_CHAR
, TYPE_STRING
,
443 unsigned int uintval
;
445 unsigned long int ulongintval
;
447 const char * stringval
;
449 } arg
[MAX_ARGS
], spec
[MAX_ARGS
];
450 /* spec is the array of specifiers, in the same order as they
451 appear in the format string. arg is the array of arguments,
452 in the same order as they appear in the va_list. */
455 int i
, n
, have_l1
, pos
, maxpos
;
456 locus
*l1
, *l2
, *loc
;
468 for (i
= 0; i
< MAX_ARGS
; i
++)
470 arg
[i
].type
= NOTYPE
;
474 /* First parse the format string for position specifiers. */
487 if (ISDIGIT (*format
))
489 /* This is a position specifier. For example, the number
490 12 in the format string "%12$d", which specifies the third
491 argument of the va_list, formatted in %d format.
492 For details, see "man 3 printf". */
493 pos
= atoi(format
) - 1;
494 gcc_assert (pos
>= 0);
495 while (ISDIGIT(*format
))
497 gcc_assert (*format
++ == '$');
510 arg
[pos
].type
= TYPE_CURRENTLOC
;
514 arg
[pos
].type
= TYPE_LOCUS
;
519 arg
[pos
].type
= TYPE_INTEGER
;
523 arg
[pos
].type
= TYPE_UINTEGER
;
528 arg
[pos
].type
= TYPE_ULONGINT
;
529 else if (c
== 'i' || c
== 'd')
530 arg
[pos
].type
= TYPE_LONGINT
;
536 arg
[pos
].type
= TYPE_CHAR
;
540 arg
[pos
].type
= TYPE_STRING
;
550 /* Then convert the values for each %-style argument. */
551 for (pos
= 0; pos
<= maxpos
; pos
++)
553 gcc_assert (arg
[pos
].type
!= NOTYPE
);
554 switch (arg
[pos
].type
)
556 case TYPE_CURRENTLOC
:
557 loc
= &gfc_current_locus
;
561 if (arg
[pos
].type
== TYPE_LOCUS
)
562 loc
= va_arg (argp
, locus
*);
567 arg
[pos
].u
.stringval
= "(2)";
573 arg
[pos
].u
.stringval
= "(1)";
578 arg
[pos
].u
.intval
= va_arg (argp
, int);
582 arg
[pos
].u
.uintval
= va_arg (argp
, unsigned int);
586 arg
[pos
].u
.longintval
= va_arg (argp
, long int);
590 arg
[pos
].u
.ulongintval
= va_arg (argp
, unsigned long int);
594 arg
[pos
].u
.charval
= (char) va_arg (argp
, int);
598 arg
[pos
].u
.stringval
= (const char *) va_arg (argp
, char *);
606 for (n
= 0; spec
[n
].pos
>= 0; n
++)
607 spec
[n
].u
= arg
[spec
[n
].pos
].u
;
609 /* Show the current loci if we have to. */
623 for (; *format
; format
++)
627 error_char (*format
);
632 if (ISDIGIT (*format
))
634 /* This is a position specifier. See comment above. */
635 while (ISDIGIT (*format
))
638 /* Skip over the dollar sign. */
649 error_char (spec
[n
++].u
.charval
);
653 case 'C': /* Current locus */
654 case 'L': /* Specified locus */
655 error_string (spec
[n
++].u
.stringval
);
660 error_integer (spec
[n
++].u
.intval
);
664 error_uinteger (spec
[n
++].u
.uintval
);
670 error_uinteger (spec
[n
++].u
.ulongintval
);
672 error_integer (spec
[n
++].u
.longintval
);
682 /* Wrapper for error_print(). */
685 error_printf (const char *nocmsgid
, ...)
689 va_start (argp
, nocmsgid
);
690 error_print ("", _(nocmsgid
), argp
);
695 /* Increment the number of errors, and check whether too many have
699 gfc_increment_error_count (void)
702 if ((gfc_option
.max_errors
!= 0) && (errors
>= gfc_option
.max_errors
))
703 gfc_fatal_error ("Error count reached limit of %d.", gfc_option
.max_errors
);
707 /* Issue a warning. */
710 gfc_warning (const char *nocmsgid
, ...)
714 if (inhibit_warnings
)
717 warning_buffer
.flag
= 1;
718 warning_buffer
.index
= 0;
719 cur_error_buffer
= &warning_buffer
;
721 va_start (argp
, nocmsgid
);
722 error_print (_("Warning:"), _(nocmsgid
), argp
);
727 if (buffer_flag
== 0)
730 if (warnings_are_errors
)
731 gfc_increment_error_count();
736 /* Whether, for a feature included in a given standard set (GFC_STD_*),
737 we should issue an error or a warning, or be quiet. */
740 gfc_notification_std (int std
)
744 warning
= ((gfc_option
.warn_std
& std
) != 0) && !inhibit_warnings
;
745 if ((gfc_option
.allow_std
& std
) != 0 && !warning
)
748 return warning
? WARNING
: ERROR
;
752 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
753 feature. An error/warning will be issued if the currently selected
754 standard does not contain the requested bits. Return FAILURE if
755 an error is generated. */
758 gfc_notify_std (int std
, const char *nocmsgid
, ...)
763 warning
= ((gfc_option
.warn_std
& std
) != 0) && !inhibit_warnings
;
764 if ((gfc_option
.allow_std
& std
) != 0 && !warning
)
767 if (gfc_suppress_error
)
768 return warning
? SUCCESS
: FAILURE
;
770 cur_error_buffer
= warning
? &warning_buffer
: &error_buffer
;
771 cur_error_buffer
->flag
= 1;
772 cur_error_buffer
->index
= 0;
774 va_start (argp
, nocmsgid
);
776 error_print (_("Warning:"), _(nocmsgid
), argp
);
778 error_print (_("Error:"), _(nocmsgid
), argp
);
783 if (buffer_flag
== 0)
785 if (warning
&& !warnings_are_errors
)
788 gfc_increment_error_count();
791 return (warning
&& !warnings_are_errors
) ? SUCCESS
: FAILURE
;
795 /* Immediate warning (i.e. do not buffer the warning). */
798 gfc_warning_now (const char *nocmsgid
, ...)
803 if (inhibit_warnings
)
809 if (warnings_are_errors
)
810 gfc_increment_error_count();
812 va_start (argp
, nocmsgid
);
813 error_print (_("Warning:"), _(nocmsgid
), argp
);
821 /* Clear the warning flag. */
824 gfc_clear_warning (void)
826 warning_buffer
.flag
= 0;
830 /* Check to see if any warnings have been saved.
831 If so, print the warning. */
834 gfc_warning_check (void)
836 if (warning_buffer
.flag
)
839 if (warning_buffer
.message
!= NULL
)
840 fputs (warning_buffer
.message
, stderr
);
841 warning_buffer
.flag
= 0;
846 /* Issue an error. */
849 gfc_error (const char *nocmsgid
, ...)
853 if (gfc_suppress_error
)
856 error_buffer
.flag
= 1;
857 error_buffer
.index
= 0;
858 cur_error_buffer
= &error_buffer
;
860 va_start (argp
, nocmsgid
);
861 error_print (_("Error:"), _(nocmsgid
), argp
);
866 if (buffer_flag
== 0)
867 gfc_increment_error_count();
871 /* Immediate error. */
874 gfc_error_now (const char *nocmsgid
, ...)
879 error_buffer
.flag
= 1;
880 error_buffer
.index
= 0;
881 cur_error_buffer
= &error_buffer
;
886 va_start (argp
, nocmsgid
);
887 error_print (_("Error:"), _(nocmsgid
), argp
);
892 gfc_increment_error_count();
896 if (flag_fatal_errors
)
901 /* Fatal error, never returns. */
904 gfc_fatal_error (const char *nocmsgid
, ...)
910 va_start (argp
, nocmsgid
);
911 error_print (_("Fatal Error:"), _(nocmsgid
), argp
);
918 /* This shouldn't happen... but sometimes does. */
921 gfc_internal_error (const char *format
, ...)
927 va_start (argp
, format
);
929 show_loci (&gfc_current_locus
, NULL
);
930 error_printf ("Internal Error at (1):");
932 error_print ("", format
, argp
);
935 exit (ICE_EXIT_CODE
);
939 /* Clear the error flag when we start to compile a source line. */
942 gfc_clear_error (void)
944 error_buffer
.flag
= 0;
948 /* Tests the state of error_flag. */
951 gfc_error_flag_test (void)
953 return error_buffer
.flag
;
957 /* Check to see if any errors have been saved.
958 If so, print the error. Returns the state of error_flag. */
961 gfc_error_check (void)
965 rc
= error_buffer
.flag
;
967 if (error_buffer
.flag
)
969 if (error_buffer
.message
!= NULL
)
970 fputs (error_buffer
.message
, stderr
);
971 error_buffer
.flag
= 0;
973 gfc_increment_error_count();
975 if (flag_fatal_errors
)
983 /* Save the existing error state. */
986 gfc_push_error (gfc_error_buf
*err
)
988 err
->flag
= error_buffer
.flag
;
989 if (error_buffer
.flag
)
990 err
->message
= xstrdup (error_buffer
.message
);
992 error_buffer
.flag
= 0;
996 /* Restore a previous pushed error state. */
999 gfc_pop_error (gfc_error_buf
*err
)
1001 error_buffer
.flag
= err
->flag
;
1002 if (error_buffer
.flag
)
1004 size_t len
= strlen (err
->message
) + 1;
1005 gcc_assert (len
<= error_buffer
.allocated
);
1006 memcpy (error_buffer
.message
, err
->message
, len
);
1007 gfc_free (err
->message
);
1012 /* Free a pushed error state, but keep the current error state. */
1015 gfc_free_error (gfc_error_buf
*err
)
1018 gfc_free (err
->message
);
1022 /* Report the number of warnings and errors that occurred to the caller. */
1025 gfc_get_errors (int *w
, int *e
)