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_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
;
155 /* Show the file, where it was included, and the source line, give a
156 locus. Calls error_printf() recursively, but the recursion is at
157 most one level deep. */
159 static void error_printf (const char *, ...) ATTRIBUTE_GCC_GFC(1,2);
162 show_locus (locus
*loc
, int c1
, int c2
)
167 int i
, m
, offset
, cmax
;
169 /* TODO: Either limit the total length and number of included files
170 displayed or add buffering of arbitrary number of characters in
173 /* Write out the error header line, giving the source file and error
174 location (in GNU standard "[file]:[line].[column]:" format),
175 followed by an "included by" stack and a blank line. This header
176 format is matched by a testsuite parser defined in
177 lib/gfortran-dg.exp. */
182 error_string (f
->filename
);
185 #ifdef USE_MAPPED_LOCATION
186 error_integer (LOCATION_LINE (lb
->location
));
188 error_integer (lb
->linenum
);
191 if ((c1
> 0) || (c2
> 0))
197 if ((c1
> 0) && (c2
> 0))
208 i
= f
->inclusion_line
;
211 if (f
== NULL
) break;
213 error_printf (" Included at %s:%d:", f
->filename
, i
);
218 /* Calculate an appropriate horizontal offset of the source line in
219 order to get the error locus within the visible portion of the
220 line. Note that if the margin of 5 here is changed, the
221 corresponding margin of 10 in show_loci should be changed. */
225 /* When the loci is not associated with a column, it will have a
226 value of zero. We adjust this to 1 so that it will appear. */
233 /* If the two loci would appear in the same column, we shift
234 '2' one column to the right, so as to print '12' rather than
235 just '1'. We do this here so it will be accounted for in the
236 margin calculations. */
241 cmax
= (c1
< c2
) ? c2
: c1
;
242 if (cmax
> terminal_width
- 5)
243 offset
= cmax
- terminal_width
+ 5;
245 /* Show the line itself, taking care not to print more than what can
246 show up on the terminal. Tabs are converted to spaces, and
247 nonprintable characters are converted to a "\xNN" sequence. */
249 /* TODO: Although setting i to the terminal width is clever, it fails
250 to work correctly when nonprintable characters exist. A better
251 solution should be found. */
253 p
= lb
->line
+ offset
;
255 if (i
> terminal_width
)
256 i
= terminal_width
- 1;
271 m
= ((c
>> 4) & 0x0F) + '0';
276 m
= (c
& 0x0F) + '0';
285 /* Show the '1' and/or '2' corresponding to the column of the error
286 locus. Note that a value of -1 for c1 or c2 will simply cause
287 the relevant number not to be printed. */
292 for (i
= 1; i
<= cmax
; i
++)
307 /* As part of printing an error, we show the source lines that caused
308 the problem. We show at least one, and possibly two loci; the two
309 loci may or may not be on the same source line. */
312 show_loci (locus
*l1
, locus
*l2
)
316 if (l1
== NULL
|| l1
->lb
== NULL
)
318 error_printf ("<During initialization>\n");
322 /* While calculating parameters for printing the loci, we consider possible
323 reasons for printing one per line. If appropriate, print the loci
324 individually; otherwise we print them both on the same line. */
326 c1
= l1
->nextc
- l1
->lb
->line
;
329 show_locus (l1
, c1
, -1);
333 c2
= l2
->nextc
- l2
->lb
->line
;
340 /* Note that the margin value of 10 here needs to be less than the
341 margin of 5 used in the calculation of offset in show_locus. */
343 if (l1
->lb
!= l2
->lb
|| m
> terminal_width
- 10)
345 show_locus (l1
, c1
, -1);
346 show_locus (l2
, -1, c2
);
350 show_locus (l1
, c1
, c2
);
356 /* Workhorse for the error printing subroutines. This subroutine is
357 inspired by g77's error handling and is similar to printf() with
358 the following %-codes:
360 %c Character, %d or %i Integer, %s String, %% Percent
361 %L Takes locus argument
362 %C Current locus (no argument)
364 If a locus pointer is given, the actual source line is printed out
365 and the column is indicated. Since we want the error message at
366 the bottom of any source file information, we must scan the
367 argument list twice -- once to determine whether the loci are
368 present and record this for printing, and once to print the error
369 message after and loci have been printed. A maximum of two locus
370 arguments are permitted.
372 This function is also called (recursively) by show_locus in the
373 case of included files; however, as show_locus does not resupply
374 any loci, the recursion is at most one level deep. */
378 static void ATTRIBUTE_GCC_GFC(2,0)
379 error_print (const char *type
, const char *format0
, va_list argp
)
381 enum { TYPE_CURRENTLOC
, TYPE_LOCUS
, TYPE_INTEGER
, TYPE_UINTEGER
,
382 TYPE_LONGINT
, TYPE_ULONGINT
, TYPE_CHAR
, TYPE_STRING
,
391 unsigned int uintval
;
393 unsigned long int ulongintval
;
395 const char * stringval
;
397 } arg
[MAX_ARGS
], spec
[MAX_ARGS
];
398 /* spec is the array of specifiers, in the same order as they
399 appear in the format string. arg is the array of arguments,
400 in the same order as they appear in the va_list. */
403 int i
, n
, have_l1
, pos
, maxpos
;
404 locus
*l1
, *l2
, *loc
;
416 for (i
= 0; i
< MAX_ARGS
; i
++)
418 arg
[i
].type
= NOTYPE
;
422 /* First parse the format string for position specifiers. */
435 if (ISDIGIT (*format
))
437 /* This is a position specifier. For example, the number
438 12 in the format string "%12$d", which specifies the third
439 argument of the va_list, formatted in %d format.
440 For details, see "man 3 printf". */
441 pos
= atoi(format
) - 1;
442 gcc_assert (pos
>= 0);
443 while (ISDIGIT(*format
))
445 gcc_assert (*format
++ == '$');
458 arg
[pos
].type
= TYPE_CURRENTLOC
;
462 arg
[pos
].type
= TYPE_LOCUS
;
467 arg
[pos
].type
= TYPE_INTEGER
;
471 arg
[pos
].type
= TYPE_UINTEGER
;
476 arg
[pos
].type
= TYPE_ULONGINT
;
477 else if (c
== 'i' || c
== 'd')
478 arg
[pos
].type
= TYPE_LONGINT
;
484 arg
[pos
].type
= TYPE_CHAR
;
488 arg
[pos
].type
= TYPE_STRING
;
498 /* Then convert the values for each %-style argument. */
499 for (pos
= 0; pos
<= maxpos
; pos
++)
501 gcc_assert (arg
[pos
].type
!= NOTYPE
);
502 switch (arg
[pos
].type
)
504 case TYPE_CURRENTLOC
:
505 loc
= &gfc_current_locus
;
509 if (arg
[pos
].type
== TYPE_LOCUS
)
510 loc
= va_arg (argp
, locus
*);
515 arg
[pos
].u
.stringval
= "(2)";
521 arg
[pos
].u
.stringval
= "(1)";
526 arg
[pos
].u
.intval
= va_arg (argp
, int);
530 arg
[pos
].u
.uintval
= va_arg (argp
, unsigned int);
534 arg
[pos
].u
.longintval
= va_arg (argp
, long int);
538 arg
[pos
].u
.ulongintval
= va_arg (argp
, unsigned long int);
542 arg
[pos
].u
.charval
= (char) va_arg (argp
, int);
546 arg
[pos
].u
.stringval
= (const char *) va_arg (argp
, char *);
554 for (n
= 0; spec
[n
].pos
>= 0; n
++)
555 spec
[n
].u
= arg
[spec
[n
].pos
].u
;
557 /* Show the current loci if we have to. */
571 for (; *format
; format
++)
575 error_char (*format
);
580 if (ISDIGIT (*format
))
582 /* This is a position specifier. See comment above. */
583 while (ISDIGIT (*format
))
586 /* Skip over the dollar sign. */
597 error_char (spec
[n
++].u
.charval
);
601 case 'C': /* Current locus */
602 case 'L': /* Specified locus */
603 error_string (spec
[n
++].u
.stringval
);
608 error_integer (spec
[n
++].u
.intval
);
612 error_uinteger (spec
[n
++].u
.uintval
);
618 error_uinteger (spec
[n
++].u
.ulongintval
);
620 error_integer (spec
[n
++].u
.longintval
);
630 /* Wrapper for error_print(). */
633 error_printf (const char *nocmsgid
, ...)
637 va_start (argp
, nocmsgid
);
638 error_print ("", _(nocmsgid
), argp
);
643 /* Increment the number of errors, and check whether too many have
647 gfc_increment_error_count (void)
650 if ((gfc_option
.max_errors
!= 0) && (errors
>= gfc_option
.max_errors
))
651 gfc_fatal_error ("Error count reached limit of %d.", gfc_option
.max_errors
);
655 /* Issue a warning. */
658 gfc_warning (const char *nocmsgid
, ...)
662 if (inhibit_warnings
)
665 warning_buffer
.flag
= 1;
666 warning_buffer
.index
= 0;
667 cur_error_buffer
= &warning_buffer
;
669 va_start (argp
, nocmsgid
);
670 error_print (_("Warning:"), _(nocmsgid
), argp
);
675 if (buffer_flag
== 0)
678 if (warnings_are_errors
)
679 gfc_increment_error_count();
684 /* Whether, for a feature included in a given standard set (GFC_STD_*),
685 we should issue an error or a warning, or be quiet. */
688 gfc_notification_std (int std
)
692 warning
= ((gfc_option
.warn_std
& std
) != 0) && !inhibit_warnings
;
693 if ((gfc_option
.allow_std
& std
) != 0 && !warning
)
696 return warning
? WARNING
: ERROR
;
700 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
701 feature. An error/warning will be issued if the currently selected
702 standard does not contain the requested bits. Return FAILURE if
703 an error is generated. */
706 gfc_notify_std (int std
, const char *nocmsgid
, ...)
711 warning
= ((gfc_option
.warn_std
& std
) != 0) && !inhibit_warnings
;
712 if ((gfc_option
.allow_std
& std
) != 0 && !warning
)
715 if (gfc_suppress_error
)
716 return warning
? SUCCESS
: FAILURE
;
718 cur_error_buffer
= (warning
&& !warnings_are_errors
)
719 ? &warning_buffer
: &error_buffer
;
720 cur_error_buffer
->flag
= 1;
721 cur_error_buffer
->index
= 0;
723 va_start (argp
, nocmsgid
);
725 error_print (_("Warning:"), _(nocmsgid
), argp
);
727 error_print (_("Error:"), _(nocmsgid
), argp
);
732 if (buffer_flag
== 0)
734 if (warning
&& !warnings_are_errors
)
737 gfc_increment_error_count();
740 return (warning
&& !warnings_are_errors
) ? SUCCESS
: FAILURE
;
744 /* Immediate warning (i.e. do not buffer the warning). */
747 gfc_warning_now (const char *nocmsgid
, ...)
752 if (inhibit_warnings
)
758 if (warnings_are_errors
)
759 gfc_increment_error_count();
761 va_start (argp
, nocmsgid
);
762 error_print (_("Warning:"), _(nocmsgid
), argp
);
770 /* Clear the warning flag. */
773 gfc_clear_warning (void)
775 warning_buffer
.flag
= 0;
779 /* Check to see if any warnings have been saved.
780 If so, print the warning. */
783 gfc_warning_check (void)
785 if (warning_buffer
.flag
)
788 if (warning_buffer
.message
!= NULL
)
789 fputs (warning_buffer
.message
, stderr
);
790 warning_buffer
.flag
= 0;
795 /* Issue an error. */
798 gfc_error (const char *nocmsgid
, ...)
802 if (gfc_suppress_error
)
805 error_buffer
.flag
= 1;
806 error_buffer
.index
= 0;
807 cur_error_buffer
= &error_buffer
;
809 va_start (argp
, nocmsgid
);
810 error_print (_("Error:"), _(nocmsgid
), argp
);
815 if (buffer_flag
== 0)
816 gfc_increment_error_count();
820 /* Immediate error. */
823 gfc_error_now (const char *nocmsgid
, ...)
828 error_buffer
.flag
= 1;
829 error_buffer
.index
= 0;
830 cur_error_buffer
= &error_buffer
;
835 va_start (argp
, nocmsgid
);
836 error_print (_("Error:"), _(nocmsgid
), argp
);
841 gfc_increment_error_count();
845 if (flag_fatal_errors
)
850 /* Fatal error, never returns. */
853 gfc_fatal_error (const char *nocmsgid
, ...)
859 va_start (argp
, nocmsgid
);
860 error_print (_("Fatal Error:"), _(nocmsgid
), argp
);
867 /* This shouldn't happen... but sometimes does. */
870 gfc_internal_error (const char *format
, ...)
876 va_start (argp
, format
);
878 show_loci (&gfc_current_locus
, NULL
);
879 error_printf ("Internal Error at (1):");
881 error_print ("", format
, argp
);
884 exit (ICE_EXIT_CODE
);
888 /* Clear the error flag when we start to compile a source line. */
891 gfc_clear_error (void)
893 error_buffer
.flag
= 0;
897 /* Tests the state of error_flag. */
900 gfc_error_flag_test (void)
902 return error_buffer
.flag
;
906 /* Check to see if any errors have been saved.
907 If so, print the error. Returns the state of error_flag. */
910 gfc_error_check (void)
914 rc
= error_buffer
.flag
;
916 if (error_buffer
.flag
)
918 if (error_buffer
.message
!= NULL
)
919 fputs (error_buffer
.message
, stderr
);
920 error_buffer
.flag
= 0;
922 gfc_increment_error_count();
924 if (flag_fatal_errors
)
932 /* Save the existing error state. */
935 gfc_push_error (gfc_error_buf
*err
)
937 err
->flag
= error_buffer
.flag
;
938 if (error_buffer
.flag
)
939 err
->message
= xstrdup (error_buffer
.message
);
941 error_buffer
.flag
= 0;
945 /* Restore a previous pushed error state. */
948 gfc_pop_error (gfc_error_buf
*err
)
950 error_buffer
.flag
= err
->flag
;
951 if (error_buffer
.flag
)
953 size_t len
= strlen (err
->message
) + 1;
954 gcc_assert (len
<= error_buffer
.allocated
);
955 memcpy (error_buffer
.message
, err
->message
, len
);
956 gfc_free (err
->message
);
961 /* Free a pushed error state, but keep the current error state. */
964 gfc_free_error (gfc_error_buf
*err
)
967 gfc_free (err
->message
);
971 /* Debug wrapper for printf. */
974 gfc_status (const char *cmsgid
, ...)
978 va_start (argp
, cmsgid
);
980 vprintf (_(cmsgid
), argp
);
986 /* Subroutine for outputting a single char so that we don't have to go
987 around creating a lot of 1-character strings. */
990 gfc_status_char (char c
)
996 /* Report the number of warnings and errors that occurred to the caller. */
999 gfc_get_errors (int *w
, int *e
)