2 Copyright (C) 2000-2014 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>
41 #include "diagnostic.h"
42 #include "diagnostic-color.h"
43 #include "tree-diagnostic.h" /* tree_diagnostics_defaults */
45 static int suppress_errors
= 0;
47 static bool warnings_not_errors
= false;
49 static int terminal_width
, errors
, warnings
;
51 static gfc_error_buf error_buffer
, warning_buffer
, *cur_error_buffer
;
53 /* True if the error/warnings should be buffered. */
54 static bool buffered_p
;
56 /* These are always buffered buffers (.flush_p == false) to be used by
57 the pretty-printer. */
58 static output_buffer pp_warning_buffer
;
59 static int warningcount_buffered
, werrorcount_buffered
;
61 #include <new> /* For placement-new */
63 /* Go one level deeper suppressing errors. */
66 gfc_push_suppress_errors (void)
68 gcc_assert (suppress_errors
>= 0);
73 /* Leave one level of error suppressing. */
76 gfc_pop_suppress_errors (void)
78 gcc_assert (suppress_errors
> 0);
83 /* Determine terminal width (for trimming source lines in output). */
86 get_terminal_width (void)
88 /* Only limit the width if we're outputting to a terminal. */
90 if (!isatty (STDERR_FILENO
))
94 /* Method #1: Use ioctl (not available on all systems). */
98 if (ioctl (0, TIOCGWINSZ
, &w
) == 0 && w
.ws_col
> 0)
102 /* Method #2: Query environment variable $COLUMNS. */
103 const char *p
= getenv ("COLUMNS");
106 int value
= atoi (p
);
111 /* If both fail, use reasonable default. */
116 /* Per-file error initialization. */
119 gfc_error_init_1 (void)
121 terminal_width
= get_terminal_width ();
124 gfc_buffer_error (false);
128 /* Set the flag for buffering errors or not. */
131 gfc_buffer_error (bool flag
)
134 pp_warning_buffer
.flush_p
= !flag
;
138 /* Add a single character to the error buffer or output depending on
146 if (cur_error_buffer
->index
>= cur_error_buffer
->allocated
)
148 cur_error_buffer
->allocated
= cur_error_buffer
->allocated
149 ? cur_error_buffer
->allocated
* 2 : 1000;
150 cur_error_buffer
->message
= XRESIZEVEC (char, cur_error_buffer
->message
,
151 cur_error_buffer
->allocated
);
153 cur_error_buffer
->message
[cur_error_buffer
->index
++] = c
;
159 /* We build up complete lines before handing things
160 over to the library in order to speed up error printing. */
162 static size_t allocated
= 0, index
= 0;
164 if (index
+ 1 >= allocated
)
166 allocated
= allocated
? allocated
* 2 : 1000;
167 line
= XRESIZEVEC (char, line
, allocated
);
173 fputs (line
, stderr
);
181 /* Copy a string to wherever it needs to go. */
184 error_string (const char *p
)
191 /* Print a formatted integer to the error buffer or output. */
196 error_uinteger (unsigned long int i
)
198 char *p
, int_buf
[IBUF_LEN
];
200 p
= int_buf
+ IBUF_LEN
- 1;
212 error_string (p
+ 1);
216 error_integer (long int i
)
222 u
= (unsigned long int) -i
;
233 gfc_widechar_display_length (gfc_char_t c
)
235 if (gfc_wide_is_printable (c
) || c
== '\t')
236 /* Printable ASCII character, or tabulation (output as a space). */
238 else if (c
< ((gfc_char_t
) 1 << 8))
239 /* Displayed as \x?? */
241 else if (c
< ((gfc_char_t
) 1 << 16))
242 /* Displayed as \u???? */
245 /* Displayed as \U???????? */
250 /* Length of the ASCII representation of the wide string, escaping wide
251 characters as print_wide_char_into_buffer() does. */
254 gfc_wide_display_length (const gfc_char_t
*str
)
258 for (i
= 0, len
= 0; str
[i
]; i
++)
259 len
+= gfc_widechar_display_length (str
[i
]);
265 print_wide_char_into_buffer (gfc_char_t c
, char *buf
)
267 static const char xdigit
[16] = { '0', '1', '2', '3', '4', '5', '6',
268 '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
270 if (gfc_wide_is_printable (c
) || c
== '\t')
273 /* Tabulation is output as a space. */
274 buf
[0] = (unsigned char) (c
== '\t' ? ' ' : c
);
277 else if (c
< ((gfc_char_t
) 1 << 8))
280 buf
[3] = xdigit
[c
& 0x0F];
282 buf
[2] = xdigit
[c
& 0x0F];
288 else if (c
< ((gfc_char_t
) 1 << 16))
291 buf
[5] = xdigit
[c
& 0x0F];
293 buf
[4] = xdigit
[c
& 0x0F];
295 buf
[3] = xdigit
[c
& 0x0F];
297 buf
[2] = xdigit
[c
& 0x0F];
306 buf
[9] = xdigit
[c
& 0x0F];
308 buf
[8] = xdigit
[c
& 0x0F];
310 buf
[7] = xdigit
[c
& 0x0F];
312 buf
[6] = xdigit
[c
& 0x0F];
314 buf
[5] = xdigit
[c
& 0x0F];
316 buf
[4] = xdigit
[c
& 0x0F];
318 buf
[3] = xdigit
[c
& 0x0F];
320 buf
[2] = xdigit
[c
& 0x0F];
328 static char wide_char_print_buffer
[11];
331 gfc_print_wide_char (gfc_char_t c
)
333 print_wide_char_into_buffer (c
, wide_char_print_buffer
);
334 return wide_char_print_buffer
;
338 /* Show the file, where it was included, and the source line, give a
339 locus. Calls error_printf() recursively, but the recursion is at
340 most one level deep. */
342 static void error_printf (const char *, ...) ATTRIBUTE_GCC_GFC(1,2);
345 show_locus (locus
*loc
, int c1
, int c2
)
352 /* TODO: Either limit the total length and number of included files
353 displayed or add buffering of arbitrary number of characters in
356 /* Write out the error header line, giving the source file and error
357 location (in GNU standard "[file]:[line].[column]:" format),
358 followed by an "included by" stack and a blank line. This header
359 format is matched by a testsuite parser defined in
360 lib/gfortran-dg.exp. */
365 error_string (f
->filename
);
368 error_integer (LOCATION_LINE (lb
->location
));
370 if ((c1
> 0) || (c2
> 0))
376 if ((c1
> 0) && (c2
> 0))
387 i
= f
->inclusion_line
;
390 if (f
== NULL
) break;
392 error_printf (" Included at %s:%d:", f
->filename
, i
);
397 /* Calculate an appropriate horizontal offset of the source line in
398 order to get the error locus within the visible portion of the
399 line. Note that if the margin of 5 here is changed, the
400 corresponding margin of 10 in show_loci should be changed. */
404 /* If the two loci would appear in the same column, we shift
405 '2' one column to the right, so as to print '12' rather than
406 just '1'. We do this here so it will be accounted for in the
407 margin calculations. */
412 cmax
= (c1
< c2
) ? c2
: c1
;
413 if (cmax
> terminal_width
- 5)
414 offset
= cmax
- terminal_width
+ 5;
416 /* Show the line itself, taking care not to print more than what can
417 show up on the terminal. Tabs are converted to spaces, and
418 nonprintable characters are converted to a "\xNN" sequence. */
420 p
= &(lb
->line
[offset
]);
421 i
= gfc_wide_display_length (p
);
422 if (i
> terminal_width
)
423 i
= terminal_width
- 1;
427 static char buffer
[11];
428 i
-= print_wide_char_into_buffer (*p
++, buffer
);
429 error_string (buffer
);
434 /* Show the '1' and/or '2' corresponding to the column of the error
435 locus. Note that a value of -1 for c1 or c2 will simply cause
436 the relevant number not to be printed. */
442 p
= &(lb
->line
[offset
]);
443 for (i
= 0; i
< cmax
; i
++)
446 spaces
= gfc_widechar_display_length (*p
++);
449 error_char ('1'), spaces
--;
451 error_char ('2'), spaces
--;
453 for (j
= 0; j
< spaces
; j
++)
467 /* As part of printing an error, we show the source lines that caused
468 the problem. We show at least one, and possibly two loci; the two
469 loci may or may not be on the same source line. */
472 show_loci (locus
*l1
, locus
*l2
)
476 if (l1
== NULL
|| l1
->lb
== NULL
)
478 error_printf ("<During initialization>\n");
482 /* While calculating parameters for printing the loci, we consider possible
483 reasons for printing one per line. If appropriate, print the loci
484 individually; otherwise we print them both on the same line. */
486 c1
= l1
->nextc
- l1
->lb
->line
;
489 show_locus (l1
, c1
, -1);
493 c2
= l2
->nextc
- l2
->lb
->line
;
500 /* Note that the margin value of 10 here needs to be less than the
501 margin of 5 used in the calculation of offset in show_locus. */
503 if (l1
->lb
!= l2
->lb
|| m
> terminal_width
- 10)
505 show_locus (l1
, c1
, -1);
506 show_locus (l2
, -1, c2
);
510 show_locus (l1
, c1
, c2
);
516 /* Workhorse for the error printing subroutines. This subroutine is
517 inspired by g77's error handling and is similar to printf() with
518 the following %-codes:
520 %c Character, %d or %i Integer, %s String, %% Percent
521 %L Takes locus argument
522 %C Current locus (no argument)
524 If a locus pointer is given, the actual source line is printed out
525 and the column is indicated. Since we want the error message at
526 the bottom of any source file information, we must scan the
527 argument list twice -- once to determine whether the loci are
528 present and record this for printing, and once to print the error
529 message after and loci have been printed. A maximum of two locus
530 arguments are permitted.
532 This function is also called (recursively) by show_locus in the
533 case of included files; however, as show_locus does not resupply
534 any loci, the recursion is at most one level deep. */
538 static void ATTRIBUTE_GCC_GFC(2,0)
539 error_print (const char *type
, const char *format0
, va_list argp
)
541 enum { TYPE_CURRENTLOC
, TYPE_LOCUS
, TYPE_INTEGER
, TYPE_UINTEGER
,
542 TYPE_LONGINT
, TYPE_ULONGINT
, TYPE_CHAR
, TYPE_STRING
,
551 unsigned int uintval
;
553 unsigned long int ulongintval
;
555 const char * stringval
;
557 } arg
[MAX_ARGS
], spec
[MAX_ARGS
];
558 /* spec is the array of specifiers, in the same order as they
559 appear in the format string. arg is the array of arguments,
560 in the same order as they appear in the va_list. */
563 int i
, n
, have_l1
, pos
, maxpos
;
564 locus
*l1
, *l2
, *loc
;
567 loc
= l1
= l2
= NULL
;
576 for (i
= 0; i
< MAX_ARGS
; i
++)
578 arg
[i
].type
= NOTYPE
;
582 /* First parse the format string for position specifiers. */
595 if (ISDIGIT (*format
))
597 /* This is a position specifier. For example, the number
598 12 in the format string "%12$d", which specifies the third
599 argument of the va_list, formatted in %d format.
600 For details, see "man 3 printf". */
601 pos
= atoi(format
) - 1;
602 gcc_assert (pos
>= 0);
603 while (ISDIGIT(*format
))
605 gcc_assert (*format
== '$');
619 arg
[pos
].type
= TYPE_CURRENTLOC
;
623 arg
[pos
].type
= TYPE_LOCUS
;
628 arg
[pos
].type
= TYPE_INTEGER
;
632 arg
[pos
].type
= TYPE_UINTEGER
;
638 arg
[pos
].type
= TYPE_ULONGINT
;
639 else if (c
== 'i' || c
== 'd')
640 arg
[pos
].type
= TYPE_LONGINT
;
646 arg
[pos
].type
= TYPE_CHAR
;
650 arg
[pos
].type
= TYPE_STRING
;
660 /* Then convert the values for each %-style argument. */
661 for (pos
= 0; pos
<= maxpos
; pos
++)
663 gcc_assert (arg
[pos
].type
!= NOTYPE
);
664 switch (arg
[pos
].type
)
666 case TYPE_CURRENTLOC
:
667 loc
= &gfc_current_locus
;
671 if (arg
[pos
].type
== TYPE_LOCUS
)
672 loc
= va_arg (argp
, locus
*);
677 arg
[pos
].u
.stringval
= "(2)";
683 arg
[pos
].u
.stringval
= "(1)";
688 arg
[pos
].u
.intval
= va_arg (argp
, int);
692 arg
[pos
].u
.uintval
= va_arg (argp
, unsigned int);
696 arg
[pos
].u
.longintval
= va_arg (argp
, long int);
700 arg
[pos
].u
.ulongintval
= va_arg (argp
, unsigned long int);
704 arg
[pos
].u
.charval
= (char) va_arg (argp
, int);
708 arg
[pos
].u
.stringval
= (const char *) va_arg (argp
, char *);
716 for (n
= 0; spec
[n
].pos
>= 0; n
++)
717 spec
[n
].u
= arg
[spec
[n
].pos
].u
;
719 /* Show the current loci if we have to. */
733 for (; *format
; format
++)
737 error_char (*format
);
742 if (ISDIGIT (*format
))
744 /* This is a position specifier. See comment above. */
745 while (ISDIGIT (*format
))
748 /* Skip over the dollar sign. */
759 error_char (spec
[n
++].u
.charval
);
763 case 'C': /* Current locus */
764 case 'L': /* Specified locus */
765 error_string (spec
[n
++].u
.stringval
);
770 error_integer (spec
[n
++].u
.intval
);
774 error_uinteger (spec
[n
++].u
.uintval
);
780 error_uinteger (spec
[n
++].u
.ulongintval
);
782 error_integer (spec
[n
++].u
.longintval
);
792 /* Wrapper for error_print(). */
795 error_printf (const char *gmsgid
, ...)
799 va_start (argp
, gmsgid
);
800 error_print ("", _(gmsgid
), argp
);
805 /* Increment the number of errors, and check whether too many have
809 gfc_increment_error_count (void)
812 if ((gfc_option
.max_errors
!= 0) && (errors
>= gfc_option
.max_errors
))
813 gfc_fatal_error ("Error count reached limit of %d.", gfc_option
.max_errors
);
817 /* Clear any output buffered in a pretty-print output_buffer. */
820 gfc_clear_pp_buffer (output_buffer
*this_buffer
)
822 pretty_printer
*pp
= global_dc
->printer
;
823 output_buffer
*tmp_buffer
= pp
->buffer
;
824 pp
->buffer
= this_buffer
;
825 pp_clear_output_area (pp
);
826 pp
->buffer
= tmp_buffer
;
830 /* Issue a warning. */
831 /* Use gfc_warning instead, unless two locations are used in the same
832 warning or for scanner.c, if the location is not properly set up. */
835 gfc_warning_1 (const char *gmsgid
, ...)
839 if (inhibit_warnings
)
842 warning_buffer
.flag
= 1;
843 warning_buffer
.index
= 0;
844 cur_error_buffer
= &warning_buffer
;
846 va_start (argp
, gmsgid
);
847 error_print (_("Warning:"), _(gmsgid
), argp
);
855 if (warnings_are_errors
)
856 gfc_increment_error_count();
861 /* This is just a helper function to avoid duplicating the logic of
865 gfc_warning (int opt
, const char *gmsgid
, va_list ap
) ATTRIBUTE_GCC_GFC(2,0);
868 gfc_warning (int opt
, const char *gmsgid
, va_list ap
)
873 diagnostic_info diagnostic
;
874 bool fatal_errors
= global_dc
->fatal_errors
;
875 pretty_printer
*pp
= global_dc
->printer
;
876 output_buffer
*tmp_buffer
= pp
->buffer
;
878 gfc_clear_pp_buffer (&pp_warning_buffer
);
882 pp
->buffer
= &pp_warning_buffer
;
883 global_dc
->fatal_errors
= false;
884 /* To prevent -fmax-errors= triggering. */
888 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, UNKNOWN_LOCATION
,
890 diagnostic
.option_index
= opt
;
891 bool ret
= report_diagnostic (&diagnostic
);
895 pp
->buffer
= tmp_buffer
;
896 global_dc
->fatal_errors
= fatal_errors
;
898 warningcount_buffered
= 0;
899 werrorcount_buffered
= 0;
900 /* Undo the above --werrorcount if not Werror, otherwise
901 werrorcount is correct already. */
904 else if (diagnostic
.kind
== DK_ERROR
)
905 ++werrorcount_buffered
;
907 ++werrorcount
, --warningcount
, ++warningcount_buffered
;
914 /* Issue a warning. */
915 /* This function uses the common diagnostics, but does not support
916 two locations; when being used in scanner.c, ensure that the location
917 is properly setup. Otherwise, use gfc_warning_1. */
920 gfc_warning (int opt
, const char *gmsgid
, ...)
924 va_start (argp
, gmsgid
);
925 bool ret
= gfc_warning (opt
, gmsgid
, argp
);
931 gfc_warning (const char *gmsgid
, ...)
935 va_start (argp
, gmsgid
);
936 bool ret
= gfc_warning (0, gmsgid
, argp
);
942 /* Whether, for a feature included in a given standard set (GFC_STD_*),
943 we should issue an error or a warning, or be quiet. */
946 gfc_notification_std (int std
)
950 warning
= ((gfc_option
.warn_std
& std
) != 0) && !inhibit_warnings
;
951 if ((gfc_option
.allow_std
& std
) != 0 && !warning
)
954 return warning
? WARNING
: ERROR
;
958 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
959 feature. An error/warning will be issued if the currently selected
960 standard does not contain the requested bits. Return false if
961 an error is generated. */
964 gfc_notify_std (int std
, const char *gmsgid
, ...)
968 const char *msg1
, *msg2
;
971 warning
= ((gfc_option
.warn_std
& std
) != 0) && !inhibit_warnings
;
972 if ((gfc_option
.allow_std
& std
) != 0 && !warning
)
976 return warning
? true : false;
978 cur_error_buffer
= warning
? &warning_buffer
: &error_buffer
;
979 cur_error_buffer
->flag
= 1;
980 cur_error_buffer
->index
= 0;
983 msg1
= _("Warning:");
989 case GFC_STD_F2008_TS
:
990 msg2
= "TS 29113/TS 18508:";
992 case GFC_STD_F2008_OBS
:
993 msg2
= _("Fortran 2008 obsolescent feature:");
996 msg2
= "Fortran 2008:";
999 msg2
= "Fortran 2003:";
1002 msg2
= _("GNU Extension:");
1004 case GFC_STD_LEGACY
:
1005 msg2
= _("Legacy Extension:");
1007 case GFC_STD_F95_OBS
:
1008 msg2
= _("Obsolescent feature:");
1010 case GFC_STD_F95_DEL
:
1011 msg2
= _("Deleted feature:");
1017 buffer
= (char *) alloca (strlen (msg1
) + strlen (msg2
) + 2);
1018 strcpy (buffer
, msg1
);
1019 strcat (buffer
, " ");
1020 strcat (buffer
, msg2
);
1022 va_start (argp
, gmsgid
);
1023 error_print (buffer
, _(gmsgid
), argp
);
1030 if (warning
&& !warnings_are_errors
)
1033 gfc_increment_error_count();
1034 cur_error_buffer
->flag
= 0;
1037 return (warning
&& !warnings_are_errors
) ? true : false;
1041 /* Immediate warning (i.e. do not buffer the warning). */
1042 /* Use gfc_warning_now instead, unless two locations are used in the same
1043 warning or for scanner.c, if the location is not properly set up. */
1046 gfc_warning_now_1 (const char *gmsgid
, ...)
1049 bool buffered_p_saved
;
1051 if (inhibit_warnings
)
1054 buffered_p_saved
= buffered_p
;
1058 va_start (argp
, gmsgid
);
1059 error_print (_("Warning:"), _(gmsgid
), argp
);
1064 if (warnings_are_errors
)
1065 gfc_increment_error_count();
1067 buffered_p
= buffered_p_saved
;
1070 /* Called from output_format -- during diagnostic message processing
1071 to handle Fortran specific format specifiers with the following meanings:
1073 %C Current locus (no argument)
1074 %L Takes locus argument
1077 gfc_format_decoder (pretty_printer
*pp
,
1078 text_info
*text
, const char *spec
,
1079 int precision ATTRIBUTE_UNUSED
, bool wide ATTRIBUTE_UNUSED
,
1080 bool plus ATTRIBUTE_UNUSED
, bool hash ATTRIBUTE_UNUSED
)
1087 static const char *result
= "(1)";
1090 loc
= &gfc_current_locus
;
1092 loc
= va_arg (*text
->args_ptr
, locus
*);
1093 gcc_assert (loc
->nextc
- loc
->lb
->line
>= 0);
1094 unsigned int offset
= loc
->nextc
- loc
->lb
->line
;
1095 gcc_assert (text
->locus
);
1097 = linemap_position_for_loc_and_offset (line_table
,
1100 global_dc
->caret_char
= '1';
1101 pp_string (pp
, result
);
1109 /* Return a malloc'd string describing a location. The caller is
1110 responsible for freeing the memory. */
1112 gfc_diagnostic_build_prefix (diagnostic_context
*context
,
1113 const diagnostic_info
*diagnostic
)
1115 static const char *const diagnostic_kind_text
[] = {
1116 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
1117 #include "gfc-diagnostic.def"
1118 #undef DEFINE_DIAGNOSTIC_KIND
1121 static const char *const diagnostic_kind_color
[] = {
1122 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
1123 #include "gfc-diagnostic.def"
1124 #undef DEFINE_DIAGNOSTIC_KIND
1127 gcc_assert (diagnostic
->kind
< DK_LAST_DIAGNOSTIC_KIND
);
1128 const char *text
= _(diagnostic_kind_text
[diagnostic
->kind
]);
1129 const char *text_cs
= "", *text_ce
= "";
1130 pretty_printer
*pp
= context
->printer
;
1132 if (diagnostic_kind_color
[diagnostic
->kind
])
1134 text_cs
= colorize_start (pp_show_color (pp
),
1135 diagnostic_kind_color
[diagnostic
->kind
]);
1136 text_ce
= colorize_stop (pp_show_color (pp
));
1138 return build_message_string ("%s%s:%s ", text_cs
, text
, text_ce
);
1141 /* Return a malloc'd string describing a location. The caller is
1142 responsible for freeing the memory. */
1144 gfc_diagnostic_build_locus_prefix (diagnostic_context
*context
,
1145 const diagnostic_info
*diagnostic
)
1147 pretty_printer
*pp
= context
->printer
;
1148 const char *locus_cs
= colorize_start (pp_show_color (pp
), "locus");
1149 const char *locus_ce
= colorize_stop (pp_show_color (pp
));
1150 expanded_location s
= diagnostic_expand_location (diagnostic
);
1151 return (s
.file
== NULL
1152 ? build_message_string ("%s%s:%s", locus_cs
, progname
, locus_ce
)
1153 : !strcmp (s
.file
, N_("<built-in>"))
1154 ? build_message_string ("%s%s:%s", locus_cs
, s
.file
, locus_ce
)
1155 : context
->show_column
1156 ? build_message_string ("%s%s:%d:%d:%s", locus_cs
, s
.file
, s
.line
,
1158 : build_message_string ("%s%s:%d:%s", locus_cs
, s
.file
, s
.line
, locus_ce
));
1162 gfc_diagnostic_starter (diagnostic_context
*context
,
1163 diagnostic_info
*diagnostic
)
1165 char * locus_prefix
= gfc_diagnostic_build_locus_prefix (context
, diagnostic
);
1166 char * prefix
= gfc_diagnostic_build_prefix (context
, diagnostic
);
1167 /* First we assume there is a caret line. */
1168 pp_set_prefix (context
->printer
, NULL
);
1169 if (pp_needs_newline (context
->printer
))
1170 pp_newline (context
->printer
);
1171 pp_verbatim (context
->printer
, locus_prefix
);
1172 /* Fortran uses an empty line between locus and caret line. */
1173 pp_newline (context
->printer
);
1174 diagnostic_show_locus (context
, diagnostic
);
1175 if (pp_needs_newline (context
->printer
))
1177 pp_newline (context
->printer
);
1178 /* If the caret line was shown, the prefix does not contain the
1180 pp_set_prefix (context
->printer
, prefix
);
1184 /* Otherwise, start again. */
1185 pp_clear_output_area(context
->printer
);
1186 pp_set_prefix (context
->printer
, concat (locus_prefix
, " ", prefix
, NULL
));
1189 free (locus_prefix
);
1193 gfc_diagnostic_finalizer (diagnostic_context
*context
,
1194 diagnostic_info
*diagnostic ATTRIBUTE_UNUSED
)
1196 pp_destroy_prefix (context
->printer
);
1197 pp_newline_and_flush (context
->printer
);
1200 /* Immediate warning (i.e. do not buffer the warning). */
1201 /* This function uses the common diagnostics, but does not support
1202 two locations; when being used in scanner.c, ensure that the location
1203 is properly setup. Otherwise, use gfc_warning_now_1. */
1206 gfc_warning_now (int opt
, const char *gmsgid
, ...)
1209 diagnostic_info diagnostic
;
1212 va_start (argp
, gmsgid
);
1213 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, UNKNOWN_LOCATION
,
1215 diagnostic
.option_index
= opt
;
1216 ret
= report_diagnostic (&diagnostic
);
1221 /* Immediate warning (i.e. do not buffer the warning). */
1222 /* This function uses the common diagnostics, but does not support
1223 two locations; when being used in scanner.c, ensure that the location
1224 is properly setup. Otherwise, use gfc_warning_now_1. */
1227 gfc_warning_now (const char *gmsgid
, ...)
1230 diagnostic_info diagnostic
;
1233 va_start (argp
, gmsgid
);
1234 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, UNKNOWN_LOCATION
,
1236 ret
= report_diagnostic (&diagnostic
);
1242 /* Immediate error (i.e. do not buffer). */
1243 /* This function uses the common diagnostics, but does not support
1244 two locations; when being used in scanner.c, ensure that the location
1245 is properly setup. Otherwise, use gfc_error_now_1. */
1248 gfc_error_now (const char *gmsgid
, ...)
1251 diagnostic_info diagnostic
;
1253 va_start (argp
, gmsgid
);
1254 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, UNKNOWN_LOCATION
, DK_ERROR
);
1255 report_diagnostic (&diagnostic
);
1260 /* Fatal error, never returns. */
1263 gfc_fatal_error (const char *gmsgid
, ...)
1266 diagnostic_info diagnostic
;
1268 va_start (argp
, gmsgid
);
1269 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, UNKNOWN_LOCATION
, DK_FATAL
);
1270 report_diagnostic (&diagnostic
);
1276 /* Clear the warning flag. */
1279 gfc_clear_warning (void)
1281 warning_buffer
.flag
= 0;
1283 gfc_clear_pp_buffer (&pp_warning_buffer
);
1284 warningcount_buffered
= 0;
1285 werrorcount_buffered
= 0;
1286 pp_warning_buffer
.flush_p
= false;
1290 /* Check to see if any warnings have been saved.
1291 If so, print the warning. */
1294 gfc_warning_check (void)
1296 if (warning_buffer
.flag
)
1299 if (warning_buffer
.message
!= NULL
)
1300 fputs (warning_buffer
.message
, stderr
);
1301 warning_buffer
.flag
= 0;
1304 /* This is for the new diagnostics machinery. */
1305 pretty_printer
*pp
= global_dc
->printer
;
1306 output_buffer
*tmp_buffer
= pp
->buffer
;
1307 pp
->buffer
= &pp_warning_buffer
;
1308 if (pp_last_position_in_text (pp
) != NULL
)
1310 pp_really_flush (pp
);
1311 pp_warning_buffer
.flush_p
= true;
1312 warningcount
+= warningcount_buffered
;
1313 werrorcount
+= werrorcount_buffered
;
1316 pp
->buffer
= tmp_buffer
;
1320 /* Issue an error. */
1323 gfc_error (const char *gmsgid
, ...)
1327 if (warnings_not_errors
)
1330 if (suppress_errors
)
1333 error_buffer
.flag
= 1;
1334 error_buffer
.index
= 0;
1335 cur_error_buffer
= &error_buffer
;
1337 va_start (argp
, gmsgid
);
1338 error_print (_("Error:"), _(gmsgid
), argp
);
1344 gfc_increment_error_count();
1350 if (inhibit_warnings
)
1353 warning_buffer
.flag
= 1;
1354 warning_buffer
.index
= 0;
1355 cur_error_buffer
= &warning_buffer
;
1357 va_start (argp
, gmsgid
);
1358 error_print (_("Warning:"), _(gmsgid
), argp
);
1366 if (warnings_are_errors
)
1367 gfc_increment_error_count();
1372 /* Immediate error. */
1373 /* Use gfc_error_now instead, unless two locations are used in the same
1374 warning or for scanner.c, if the location is not properly set up. */
1377 gfc_error_now_1 (const char *gmsgid
, ...)
1380 bool buffered_p_saved
;
1382 error_buffer
.flag
= 1;
1383 error_buffer
.index
= 0;
1384 cur_error_buffer
= &error_buffer
;
1386 buffered_p_saved
= buffered_p
;
1389 va_start (argp
, gmsgid
);
1390 error_print (_("Error:"), _(gmsgid
), argp
);
1395 gfc_increment_error_count();
1397 buffered_p
= buffered_p_saved
;
1399 if (flag_fatal_errors
)
1400 exit (FATAL_EXIT_CODE
);
1404 /* This shouldn't happen... but sometimes does. */
1407 gfc_internal_error (const char *gmsgid
, ...)
1410 diagnostic_info diagnostic
;
1412 va_start (argp
, gmsgid
);
1413 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, UNKNOWN_LOCATION
, DK_ICE
);
1414 report_diagnostic (&diagnostic
);
1421 /* Clear the error flag when we start to compile a source line. */
1424 gfc_clear_error (void)
1426 error_buffer
.flag
= 0;
1427 warnings_not_errors
= false;
1431 /* Tests the state of error_flag. */
1434 gfc_error_flag_test (void)
1436 return error_buffer
.flag
;
1440 /* Check to see if any errors have been saved.
1441 If so, print the error. Returns the state of error_flag. */
1444 gfc_error_check (void)
1446 bool error_raised
= (bool) error_buffer
.flag
;
1450 if (error_buffer
.message
!= NULL
)
1451 fputs (error_buffer
.message
, stderr
);
1452 error_buffer
.flag
= 0;
1454 gfc_increment_error_count();
1456 if (flag_fatal_errors
)
1457 exit (FATAL_EXIT_CODE
);
1460 return error_raised
;
1464 /* Save the existing error state. */
1467 gfc_push_error (gfc_error_buf
*err
)
1469 err
->flag
= error_buffer
.flag
;
1470 if (error_buffer
.flag
)
1471 err
->message
= xstrdup (error_buffer
.message
);
1473 error_buffer
.flag
= 0;
1477 /* Restore a previous pushed error state. */
1480 gfc_pop_error (gfc_error_buf
*err
)
1482 error_buffer
.flag
= err
->flag
;
1483 if (error_buffer
.flag
)
1485 size_t len
= strlen (err
->message
) + 1;
1486 gcc_assert (len
<= error_buffer
.allocated
);
1487 memcpy (error_buffer
.message
, err
->message
, len
);
1488 free (err
->message
);
1493 /* Free a pushed error state, but keep the current error state. */
1496 gfc_free_error (gfc_error_buf
*err
)
1499 free (err
->message
);
1503 /* Report the number of warnings and errors that occurred to the caller. */
1506 gfc_get_errors (int *w
, int *e
)
1509 *w
= warnings
+ warningcount
+ werrorcount
;
1511 *e
= errors
+ errorcount
+ sorrycount
+ werrorcount
;
1515 /* Switch errors into warnings. */
1518 gfc_errors_to_warnings (bool f
)
1520 warnings_not_errors
= f
;
1524 gfc_diagnostics_init (void)
1526 diagnostic_starter (global_dc
) = gfc_diagnostic_starter
;
1527 diagnostic_finalizer (global_dc
) = gfc_diagnostic_finalizer
;
1528 diagnostic_format_decoder (global_dc
) = gfc_format_decoder
;
1529 global_dc
->caret_char
= '^';
1530 new (&pp_warning_buffer
) output_buffer ();
1534 gfc_diagnostics_finish (void)
1536 tree_diagnostics_defaults (global_dc
);
1537 /* We still want to use the gfc starter and finalizer, not the tree
1539 diagnostic_starter (global_dc
) = gfc_diagnostic_starter
;
1540 diagnostic_finalizer (global_dc
) = gfc_diagnostic_finalizer
;
1541 global_dc
->caret_char
= '^';