2 Copyright (C) 2000-2015 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"
33 #include "diagnostic.h"
34 #include "diagnostic-color.h"
35 #include "tree-diagnostic.h" /* tree_diagnostics_defaults */
37 #include <new> /* For placement-new */
39 static int suppress_errors
= 0;
41 static bool warnings_not_errors
= false;
43 static int terminal_width
, errors
, warnings
;
45 static gfc_error_buf error_buffer
, warning_buffer
, *cur_error_buffer
;
47 /* True if the error/warnings should be buffered. */
48 static bool buffered_p
;
49 /* These are always buffered buffers (.flush_p == false) to be used by
50 the pretty-printer. */
51 static output_buffer
*pp_error_buffer
, *pp_warning_buffer
;
52 static int warningcount_buffered
, werrorcount_buffered
;
54 /* Return true if there output_buffer is empty. */
57 gfc_output_buffer_empty_p (const output_buffer
* buf
)
59 return output_buffer_last_position_in_text (buf
) == NULL
;
62 /* Go one level deeper suppressing errors. */
65 gfc_push_suppress_errors (void)
67 gcc_assert (suppress_errors
>= 0);
72 gfc_error (const char *gmsgid
, va_list ap
) ATTRIBUTE_GCC_GFC(1,0);
75 gfc_warning (int opt
, const char *gmsgid
, va_list ap
) ATTRIBUTE_GCC_GFC(2,0);
78 /* Leave one level of error suppressing. */
81 gfc_pop_suppress_errors (void)
83 gcc_assert (suppress_errors
> 0);
88 /* Determine terminal width (for trimming source lines in output). */
91 gfc_get_terminal_width (void)
93 return isatty (STDERR_FILENO
) ? get_terminal_width () : INT_MAX
;
97 /* Per-file error initialization. */
100 gfc_error_init_1 (void)
102 terminal_width
= gfc_get_terminal_width ();
105 gfc_buffer_error (false);
109 /* Set the flag for buffering errors or not. */
112 gfc_buffer_error (bool flag
)
118 /* Add a single character to the error buffer or output depending on
126 if (cur_error_buffer
->index
>= cur_error_buffer
->allocated
)
128 cur_error_buffer
->allocated
= cur_error_buffer
->allocated
129 ? cur_error_buffer
->allocated
* 2 : 1000;
130 cur_error_buffer
->message
= XRESIZEVEC (char, cur_error_buffer
->message
,
131 cur_error_buffer
->allocated
);
133 cur_error_buffer
->message
[cur_error_buffer
->index
++] = c
;
139 /* We build up complete lines before handing things
140 over to the library in order to speed up error printing. */
142 static size_t allocated
= 0, index
= 0;
144 if (index
+ 1 >= allocated
)
146 allocated
= allocated
? allocated
* 2 : 1000;
147 line
= XRESIZEVEC (char, line
, allocated
);
153 fputs (line
, stderr
);
161 /* Copy a string to wherever it needs to go. */
164 error_string (const char *p
)
171 /* Print a formatted integer to the error buffer or output. */
176 error_uinteger (unsigned long int i
)
178 char *p
, int_buf
[IBUF_LEN
];
180 p
= int_buf
+ IBUF_LEN
- 1;
192 error_string (p
+ 1);
196 error_integer (long int i
)
202 u
= (unsigned long int) -i
;
213 gfc_widechar_display_length (gfc_char_t c
)
215 if (gfc_wide_is_printable (c
) || c
== '\t')
216 /* Printable ASCII character, or tabulation (output as a space). */
218 else if (c
< ((gfc_char_t
) 1 << 8))
219 /* Displayed as \x?? */
221 else if (c
< ((gfc_char_t
) 1 << 16))
222 /* Displayed as \u???? */
225 /* Displayed as \U???????? */
230 /* Length of the ASCII representation of the wide string, escaping wide
231 characters as print_wide_char_into_buffer() does. */
234 gfc_wide_display_length (const gfc_char_t
*str
)
238 for (i
= 0, len
= 0; str
[i
]; i
++)
239 len
+= gfc_widechar_display_length (str
[i
]);
245 print_wide_char_into_buffer (gfc_char_t c
, char *buf
)
247 static const char xdigit
[16] = { '0', '1', '2', '3', '4', '5', '6',
248 '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
250 if (gfc_wide_is_printable (c
) || c
== '\t')
253 /* Tabulation is output as a space. */
254 buf
[0] = (unsigned char) (c
== '\t' ? ' ' : c
);
257 else if (c
< ((gfc_char_t
) 1 << 8))
260 buf
[3] = xdigit
[c
& 0x0F];
262 buf
[2] = xdigit
[c
& 0x0F];
268 else if (c
< ((gfc_char_t
) 1 << 16))
271 buf
[5] = xdigit
[c
& 0x0F];
273 buf
[4] = xdigit
[c
& 0x0F];
275 buf
[3] = xdigit
[c
& 0x0F];
277 buf
[2] = xdigit
[c
& 0x0F];
286 buf
[9] = xdigit
[c
& 0x0F];
288 buf
[8] = xdigit
[c
& 0x0F];
290 buf
[7] = xdigit
[c
& 0x0F];
292 buf
[6] = xdigit
[c
& 0x0F];
294 buf
[5] = xdigit
[c
& 0x0F];
296 buf
[4] = xdigit
[c
& 0x0F];
298 buf
[3] = xdigit
[c
& 0x0F];
300 buf
[2] = xdigit
[c
& 0x0F];
308 static char wide_char_print_buffer
[11];
311 gfc_print_wide_char (gfc_char_t c
)
313 print_wide_char_into_buffer (c
, wide_char_print_buffer
);
314 return wide_char_print_buffer
;
318 /* Show the file, where it was included, and the source line, give a
319 locus. Calls error_printf() recursively, but the recursion is at
320 most one level deep. */
322 static void error_printf (const char *, ...) ATTRIBUTE_GCC_GFC(1,2);
325 show_locus (locus
*loc
, int c1
, int c2
)
332 /* TODO: Either limit the total length and number of included files
333 displayed or add buffering of arbitrary number of characters in
336 /* Write out the error header line, giving the source file and error
337 location (in GNU standard "[file]:[line].[column]:" format),
338 followed by an "included by" stack and a blank line. This header
339 format is matched by a testsuite parser defined in
340 lib/gfortran-dg.exp. */
345 error_string (f
->filename
);
348 error_integer (LOCATION_LINE (lb
->location
));
350 if ((c1
> 0) || (c2
> 0))
356 if ((c1
> 0) && (c2
> 0))
367 i
= f
->inclusion_line
;
370 if (f
== NULL
) break;
372 error_printf (" Included at %s:%d:", f
->filename
, i
);
377 /* Calculate an appropriate horizontal offset of the source line in
378 order to get the error locus within the visible portion of the
379 line. Note that if the margin of 5 here is changed, the
380 corresponding margin of 10 in show_loci should be changed. */
384 /* If the two loci would appear in the same column, we shift
385 '2' one column to the right, so as to print '12' rather than
386 just '1'. We do this here so it will be accounted for in the
387 margin calculations. */
392 cmax
= (c1
< c2
) ? c2
: c1
;
393 if (cmax
> terminal_width
- 5)
394 offset
= cmax
- terminal_width
+ 5;
396 /* Show the line itself, taking care not to print more than what can
397 show up on the terminal. Tabs are converted to spaces, and
398 nonprintable characters are converted to a "\xNN" sequence. */
400 p
= &(lb
->line
[offset
]);
401 i
= gfc_wide_display_length (p
);
402 if (i
> terminal_width
)
403 i
= terminal_width
- 1;
407 static char buffer
[11];
408 i
-= print_wide_char_into_buffer (*p
++, buffer
);
409 error_string (buffer
);
414 /* Show the '1' and/or '2' corresponding to the column of the error
415 locus. Note that a value of -1 for c1 or c2 will simply cause
416 the relevant number not to be printed. */
422 p
= &(lb
->line
[offset
]);
423 for (i
= 0; i
< cmax
; i
++)
426 spaces
= gfc_widechar_display_length (*p
++);
429 error_char ('1'), spaces
--;
431 error_char ('2'), spaces
--;
433 for (j
= 0; j
< spaces
; j
++)
447 /* As part of printing an error, we show the source lines that caused
448 the problem. We show at least one, and possibly two loci; the two
449 loci may or may not be on the same source line. */
452 show_loci (locus
*l1
, locus
*l2
)
456 if (l1
== NULL
|| l1
->lb
== NULL
)
458 error_printf ("<During initialization>\n");
462 /* While calculating parameters for printing the loci, we consider possible
463 reasons for printing one per line. If appropriate, print the loci
464 individually; otherwise we print them both on the same line. */
466 c1
= l1
->nextc
- l1
->lb
->line
;
469 show_locus (l1
, c1
, -1);
473 c2
= l2
->nextc
- l2
->lb
->line
;
480 /* Note that the margin value of 10 here needs to be less than the
481 margin of 5 used in the calculation of offset in show_locus. */
483 if (l1
->lb
!= l2
->lb
|| m
> terminal_width
- 10)
485 show_locus (l1
, c1
, -1);
486 show_locus (l2
, -1, c2
);
490 show_locus (l1
, c1
, c2
);
496 /* Workhorse for the error printing subroutines. This subroutine is
497 inspired by g77's error handling and is similar to printf() with
498 the following %-codes:
500 %c Character, %d or %i Integer, %s String, %% Percent
501 %L Takes locus argument
502 %C Current locus (no argument)
504 If a locus pointer is given, the actual source line is printed out
505 and the column is indicated. Since we want the error message at
506 the bottom of any source file information, we must scan the
507 argument list twice -- once to determine whether the loci are
508 present and record this for printing, and once to print the error
509 message after and loci have been printed. A maximum of two locus
510 arguments are permitted.
512 This function is also called (recursively) by show_locus in the
513 case of included files; however, as show_locus does not resupply
514 any loci, the recursion is at most one level deep. */
518 static void ATTRIBUTE_GCC_GFC(2,0)
519 error_print (const char *type
, const char *format0
, va_list argp
)
521 enum { TYPE_CURRENTLOC
, TYPE_LOCUS
, TYPE_INTEGER
, TYPE_UINTEGER
,
522 TYPE_LONGINT
, TYPE_ULONGINT
, TYPE_CHAR
, TYPE_STRING
,
531 unsigned int uintval
;
533 unsigned long int ulongintval
;
535 const char * stringval
;
537 } arg
[MAX_ARGS
], spec
[MAX_ARGS
];
538 /* spec is the array of specifiers, in the same order as they
539 appear in the format string. arg is the array of arguments,
540 in the same order as they appear in the va_list. */
543 int i
, n
, have_l1
, pos
, maxpos
;
544 locus
*l1
, *l2
, *loc
;
547 loc
= l1
= l2
= NULL
;
556 for (i
= 0; i
< MAX_ARGS
; i
++)
558 arg
[i
].type
= NOTYPE
;
562 /* First parse the format string for position specifiers. */
575 if (ISDIGIT (*format
))
577 /* This is a position specifier. For example, the number
578 12 in the format string "%12$d", which specifies the third
579 argument of the va_list, formatted in %d format.
580 For details, see "man 3 printf". */
581 pos
= atoi(format
) - 1;
582 gcc_assert (pos
>= 0);
583 while (ISDIGIT(*format
))
585 gcc_assert (*format
== '$');
599 arg
[pos
].type
= TYPE_CURRENTLOC
;
603 arg
[pos
].type
= TYPE_LOCUS
;
608 arg
[pos
].type
= TYPE_INTEGER
;
612 arg
[pos
].type
= TYPE_UINTEGER
;
618 arg
[pos
].type
= TYPE_ULONGINT
;
619 else if (c
== 'i' || c
== 'd')
620 arg
[pos
].type
= TYPE_LONGINT
;
626 arg
[pos
].type
= TYPE_CHAR
;
630 arg
[pos
].type
= TYPE_STRING
;
640 /* Then convert the values for each %-style argument. */
641 for (pos
= 0; pos
<= maxpos
; pos
++)
643 gcc_assert (arg
[pos
].type
!= NOTYPE
);
644 switch (arg
[pos
].type
)
646 case TYPE_CURRENTLOC
:
647 loc
= &gfc_current_locus
;
651 if (arg
[pos
].type
== TYPE_LOCUS
)
652 loc
= va_arg (argp
, locus
*);
657 arg
[pos
].u
.stringval
= "(2)";
663 arg
[pos
].u
.stringval
= "(1)";
668 arg
[pos
].u
.intval
= va_arg (argp
, int);
672 arg
[pos
].u
.uintval
= va_arg (argp
, unsigned int);
676 arg
[pos
].u
.longintval
= va_arg (argp
, long int);
680 arg
[pos
].u
.ulongintval
= va_arg (argp
, unsigned long int);
684 arg
[pos
].u
.charval
= (char) va_arg (argp
, int);
688 arg
[pos
].u
.stringval
= (const char *) va_arg (argp
, char *);
696 for (n
= 0; spec
[n
].pos
>= 0; n
++)
697 spec
[n
].u
= arg
[spec
[n
].pos
].u
;
699 /* Show the current loci if we have to. */
713 for (; *format
; format
++)
717 error_char (*format
);
722 if (ISDIGIT (*format
))
724 /* This is a position specifier. See comment above. */
725 while (ISDIGIT (*format
))
728 /* Skip over the dollar sign. */
739 error_char (spec
[n
++].u
.charval
);
743 case 'C': /* Current locus */
744 case 'L': /* Specified locus */
745 error_string (spec
[n
++].u
.stringval
);
750 error_integer (spec
[n
++].u
.intval
);
754 error_uinteger (spec
[n
++].u
.uintval
);
760 error_uinteger (spec
[n
++].u
.ulongintval
);
762 error_integer (spec
[n
++].u
.longintval
);
772 /* Wrapper for error_print(). */
775 error_printf (const char *gmsgid
, ...)
779 va_start (argp
, gmsgid
);
780 error_print ("", _(gmsgid
), argp
);
785 /* Increment the number of errors, and check whether too many have
789 gfc_increment_error_count (void)
792 if ((gfc_option
.max_errors
!= 0) && (errors
>= gfc_option
.max_errors
))
793 gfc_fatal_error ("Error count reached limit of %d.", gfc_option
.max_errors
);
797 /* Clear any output buffered in a pretty-print output_buffer. */
800 gfc_clear_pp_buffer (output_buffer
*this_buffer
)
802 pretty_printer
*pp
= global_dc
->printer
;
803 output_buffer
*tmp_buffer
= pp
->buffer
;
804 pp
->buffer
= this_buffer
;
805 pp_clear_output_area (pp
);
806 pp
->buffer
= tmp_buffer
;
810 /* Issue a warning. */
811 /* Use gfc_warning instead, unless two locations are used in the same
812 warning or for scanner.c, if the location is not properly set up. */
815 gfc_warning_1 (const char *gmsgid
, ...)
819 if (inhibit_warnings
)
822 warning_buffer
.flag
= 1;
823 warning_buffer
.index
= 0;
824 cur_error_buffer
= &warning_buffer
;
826 va_start (argp
, gmsgid
);
827 error_print (_("Warning:"), _(gmsgid
), argp
);
835 if (warnings_are_errors
)
836 gfc_increment_error_count();
841 /* This is just a helper function to avoid duplicating the logic of
845 gfc_warning (int opt
, const char *gmsgid
, va_list ap
)
850 diagnostic_info diagnostic
;
851 bool fatal_errors
= global_dc
->fatal_errors
;
852 pretty_printer
*pp
= global_dc
->printer
;
853 output_buffer
*tmp_buffer
= pp
->buffer
;
855 gfc_clear_pp_buffer (pp_warning_buffer
);
859 pp
->buffer
= pp_warning_buffer
;
860 global_dc
->fatal_errors
= false;
861 /* To prevent -fmax-errors= triggering. */
865 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, UNKNOWN_LOCATION
,
867 diagnostic
.option_index
= opt
;
868 bool ret
= report_diagnostic (&diagnostic
);
872 pp
->buffer
= tmp_buffer
;
873 global_dc
->fatal_errors
= fatal_errors
;
875 warningcount_buffered
= 0;
876 werrorcount_buffered
= 0;
877 /* Undo the above --werrorcount if not Werror, otherwise
878 werrorcount is correct already. */
881 else if (diagnostic
.kind
== DK_ERROR
)
882 ++werrorcount_buffered
;
884 ++werrorcount
, --warningcount
, ++warningcount_buffered
;
891 /* Issue a warning. */
892 /* This function uses the common diagnostics, but does not support
893 two locations; when being used in scanner.c, ensure that the location
894 is properly setup. Otherwise, use gfc_warning_1. */
897 gfc_warning (int opt
, const char *gmsgid
, ...)
901 va_start (argp
, gmsgid
);
902 bool ret
= gfc_warning (opt
, gmsgid
, argp
);
908 /* Whether, for a feature included in a given standard set (GFC_STD_*),
909 we should issue an error or a warning, or be quiet. */
912 gfc_notification_std (int std
)
916 warning
= ((gfc_option
.warn_std
& std
) != 0) && !inhibit_warnings
;
917 if ((gfc_option
.allow_std
& std
) != 0 && !warning
)
920 return warning
? WARNING
: ERROR
;
924 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
925 feature. An error/warning will be issued if the currently selected
926 standard does not contain the requested bits. Return false if
927 an error is generated. */
930 gfc_notify_std_1 (int std
, const char *gmsgid
, ...)
934 const char *msg1
, *msg2
;
937 warning
= ((gfc_option
.warn_std
& std
) != 0) && !inhibit_warnings
;
938 if ((gfc_option
.allow_std
& std
) != 0 && !warning
)
942 return warning
? true : false;
944 cur_error_buffer
= warning
? &warning_buffer
: &error_buffer
;
945 cur_error_buffer
->flag
= 1;
946 cur_error_buffer
->index
= 0;
949 msg1
= _("Warning:");
955 case GFC_STD_F2008_TS
:
956 msg2
= "TS 29113/TS 18508:";
958 case GFC_STD_F2008_OBS
:
959 msg2
= _("Fortran 2008 obsolescent feature:");
962 msg2
= "Fortran 2008:";
965 msg2
= "Fortran 2003:";
968 msg2
= _("GNU Extension:");
971 msg2
= _("Legacy Extension:");
973 case GFC_STD_F95_OBS
:
974 msg2
= _("Obsolescent feature:");
976 case GFC_STD_F95_DEL
:
977 msg2
= _("Deleted feature:");
983 buffer
= (char *) alloca (strlen (msg1
) + strlen (msg2
) + 2);
984 strcpy (buffer
, msg1
);
985 strcat (buffer
, " ");
986 strcat (buffer
, msg2
);
988 va_start (argp
, gmsgid
);
989 error_print (buffer
, _(gmsgid
), argp
);
996 if (warning
&& !warnings_are_errors
)
999 gfc_increment_error_count();
1000 cur_error_buffer
->flag
= 0;
1003 return (warning
&& !warnings_are_errors
) ? true : false;
1008 gfc_notify_std (int std
, const char *gmsgid
, ...)
1012 const char *msg
, *msg2
;
1015 warning
= ((gfc_option
.warn_std
& std
) != 0) && !inhibit_warnings
;
1016 if ((gfc_option
.allow_std
& std
) != 0 && !warning
)
1019 if (suppress_errors
)
1020 return warning
? true : false;
1024 case GFC_STD_F2008_TS
:
1025 msg
= "TS 29113/TS 18508:";
1027 case GFC_STD_F2008_OBS
:
1028 msg
= _("Fortran 2008 obsolescent feature:");
1031 msg
= "Fortran 2008:";
1034 msg
= "Fortran 2003:";
1037 msg
= _("GNU Extension:");
1039 case GFC_STD_LEGACY
:
1040 msg
= _("Legacy Extension:");
1042 case GFC_STD_F95_OBS
:
1043 msg
= _("Obsolescent feature:");
1045 case GFC_STD_F95_DEL
:
1046 msg
= _("Deleted feature:");
1053 buffer
= (char *) alloca (strlen (msg
) + strlen (msg2
) + 2);
1054 strcpy (buffer
, msg
);
1055 strcat (buffer
, " ");
1056 strcat (buffer
, msg2
);
1058 va_start (argp
, gmsgid
);
1060 gfc_warning (0, buffer
, argp
);
1062 gfc_error (buffer
, argp
);
1065 return (warning
&& !warnings_are_errors
) ? true : false;
1069 /* Immediate warning (i.e. do not buffer the warning). */
1070 /* Use gfc_warning_now instead, unless two locations are used in the same
1071 warning or for scanner.c, if the location is not properly set up. */
1074 gfc_warning_now_1 (const char *gmsgid
, ...)
1077 bool buffered_p_saved
;
1079 if (inhibit_warnings
)
1082 buffered_p_saved
= buffered_p
;
1086 va_start (argp
, gmsgid
);
1087 error_print (_("Warning:"), _(gmsgid
), argp
);
1092 if (warnings_are_errors
)
1093 gfc_increment_error_count();
1095 buffered_p
= buffered_p_saved
;
1098 /* Called from output_format -- during diagnostic message processing
1099 to handle Fortran specific format specifiers with the following meanings:
1101 %C Current locus (no argument)
1102 %L Takes locus argument
1105 gfc_format_decoder (pretty_printer
*pp
,
1106 text_info
*text
, const char *spec
,
1107 int precision ATTRIBUTE_UNUSED
, bool wide ATTRIBUTE_UNUSED
,
1108 bool plus ATTRIBUTE_UNUSED
, bool hash ATTRIBUTE_UNUSED
)
1115 static const char *result
= "(1)";
1118 loc
= &gfc_current_locus
;
1120 loc
= va_arg (*text
->args_ptr
, locus
*);
1121 gcc_assert (loc
->nextc
- loc
->lb
->line
>= 0);
1122 unsigned int offset
= loc
->nextc
- loc
->lb
->line
;
1123 gcc_assert (text
->locus
);
1125 = linemap_position_for_loc_and_offset (line_table
,
1128 global_dc
->caret_char
= '1';
1129 pp_string (pp
, result
);
1137 /* Return a malloc'd string describing a location. The caller is
1138 responsible for freeing the memory. */
1140 gfc_diagnostic_build_prefix (diagnostic_context
*context
,
1141 const diagnostic_info
*diagnostic
)
1143 static const char *const diagnostic_kind_text
[] = {
1144 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
1145 #include "gfc-diagnostic.def"
1146 #undef DEFINE_DIAGNOSTIC_KIND
1149 static const char *const diagnostic_kind_color
[] = {
1150 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
1151 #include "gfc-diagnostic.def"
1152 #undef DEFINE_DIAGNOSTIC_KIND
1155 gcc_assert (diagnostic
->kind
< DK_LAST_DIAGNOSTIC_KIND
);
1156 const char *text
= _(diagnostic_kind_text
[diagnostic
->kind
]);
1157 const char *text_cs
= "", *text_ce
= "";
1158 pretty_printer
*pp
= context
->printer
;
1160 if (diagnostic_kind_color
[diagnostic
->kind
])
1162 text_cs
= colorize_start (pp_show_color (pp
),
1163 diagnostic_kind_color
[diagnostic
->kind
]);
1164 text_ce
= colorize_stop (pp_show_color (pp
));
1166 return build_message_string ("%s%s:%s ", text_cs
, text
, text_ce
);
1169 /* Return a malloc'd string describing a location. The caller is
1170 responsible for freeing the memory. */
1172 gfc_diagnostic_build_locus_prefix (diagnostic_context
*context
,
1173 const diagnostic_info
*diagnostic
)
1175 pretty_printer
*pp
= context
->printer
;
1176 const char *locus_cs
= colorize_start (pp_show_color (pp
), "locus");
1177 const char *locus_ce
= colorize_stop (pp_show_color (pp
));
1178 expanded_location s
= diagnostic_expand_location (diagnostic
);
1179 return (s
.file
== NULL
1180 ? build_message_string ("%s%s:%s", locus_cs
, progname
, locus_ce
)
1181 : !strcmp (s
.file
, N_("<built-in>"))
1182 ? build_message_string ("%s%s:%s", locus_cs
, s
.file
, locus_ce
)
1183 : context
->show_column
1184 ? build_message_string ("%s%s:%d:%d:%s", locus_cs
, s
.file
, s
.line
,
1186 : build_message_string ("%s%s:%d:%s", locus_cs
, s
.file
, s
.line
, locus_ce
));
1190 gfc_diagnostic_starter (diagnostic_context
*context
,
1191 diagnostic_info
*diagnostic
)
1193 char * locus_prefix
= gfc_diagnostic_build_locus_prefix (context
, diagnostic
);
1194 char * prefix
= gfc_diagnostic_build_prefix (context
, diagnostic
);
1195 /* First we assume there is a caret line. */
1196 pp_set_prefix (context
->printer
, NULL
);
1197 if (pp_needs_newline (context
->printer
))
1198 pp_newline (context
->printer
);
1199 pp_verbatim (context
->printer
, locus_prefix
);
1200 /* Fortran uses an empty line between locus and caret line. */
1201 pp_newline (context
->printer
);
1202 diagnostic_show_locus (context
, diagnostic
);
1203 if (pp_needs_newline (context
->printer
))
1205 pp_newline (context
->printer
);
1206 /* If the caret line was shown, the prefix does not contain the
1208 pp_set_prefix (context
->printer
, prefix
);
1212 /* Otherwise, start again. */
1213 pp_clear_output_area(context
->printer
);
1214 pp_set_prefix (context
->printer
, concat (locus_prefix
, " ", prefix
, NULL
));
1217 free (locus_prefix
);
1221 gfc_diagnostic_finalizer (diagnostic_context
*context
,
1222 diagnostic_info
*diagnostic ATTRIBUTE_UNUSED
)
1224 pp_destroy_prefix (context
->printer
);
1225 pp_newline_and_flush (context
->printer
);
1228 /* Immediate warning (i.e. do not buffer the warning). */
1229 /* This function uses the common diagnostics, but does not support
1230 two locations; when being used in scanner.c, ensure that the location
1231 is properly setup. Otherwise, use gfc_warning_now_1. */
1234 gfc_warning_now (int opt
, const char *gmsgid
, ...)
1237 diagnostic_info diagnostic
;
1240 va_start (argp
, gmsgid
);
1241 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, UNKNOWN_LOCATION
,
1243 diagnostic
.option_index
= opt
;
1244 ret
= report_diagnostic (&diagnostic
);
1250 /* Immediate error (i.e. do not buffer). */
1251 /* This function uses the common diagnostics, but does not support
1252 two locations; when being used in scanner.c, ensure that the location
1253 is properly setup. Otherwise, use gfc_error_now_1. */
1256 gfc_error_now (const char *gmsgid
, ...)
1259 diagnostic_info diagnostic
;
1261 va_start (argp
, gmsgid
);
1262 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, UNKNOWN_LOCATION
, DK_ERROR
);
1263 report_diagnostic (&diagnostic
);
1268 /* Fatal error, never returns. */
1271 gfc_fatal_error (const char *gmsgid
, ...)
1274 diagnostic_info diagnostic
;
1276 va_start (argp
, gmsgid
);
1277 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, UNKNOWN_LOCATION
, DK_FATAL
);
1278 report_diagnostic (&diagnostic
);
1284 /* Clear the warning flag. */
1287 gfc_clear_warning (void)
1289 warning_buffer
.flag
= 0;
1291 gfc_clear_pp_buffer (pp_warning_buffer
);
1292 warningcount_buffered
= 0;
1293 werrorcount_buffered
= 0;
1297 /* Check to see if any warnings have been saved.
1298 If so, print the warning. */
1301 gfc_warning_check (void)
1303 if (warning_buffer
.flag
)
1306 if (warning_buffer
.message
!= NULL
)
1307 fputs (warning_buffer
.message
, stderr
);
1308 gfc_clear_warning ();
1310 /* This is for the new diagnostics machinery. */
1311 else if (! gfc_output_buffer_empty_p (pp_warning_buffer
))
1313 pretty_printer
*pp
= global_dc
->printer
;
1314 output_buffer
*tmp_buffer
= pp
->buffer
;
1315 pp
->buffer
= pp_warning_buffer
;
1316 pp_really_flush (pp
);
1317 warningcount
+= warningcount_buffered
;
1318 werrorcount
+= werrorcount_buffered
;
1319 gcc_assert (warningcount_buffered
+ werrorcount_buffered
== 1);
1320 diagnostic_action_after_output (global_dc
,
1321 warningcount_buffered
1322 ? DK_WARNING
: DK_ERROR
);
1323 pp
->buffer
= tmp_buffer
;
1328 /* Issue an error. */
1329 /* Use gfc_error instead, unless two locations are used in the same
1330 warning or for scanner.c, if the location is not properly set up. */
1333 gfc_error_1 (const char *gmsgid
, ...)
1337 if (warnings_not_errors
)
1340 if (suppress_errors
)
1343 error_buffer
.flag
= 1;
1344 error_buffer
.index
= 0;
1345 cur_error_buffer
= &error_buffer
;
1347 va_start (argp
, gmsgid
);
1348 error_print (_("Error:"), _(gmsgid
), argp
);
1354 gfc_increment_error_count();
1360 if (inhibit_warnings
)
1363 warning_buffer
.flag
= 1;
1364 warning_buffer
.index
= 0;
1365 cur_error_buffer
= &warning_buffer
;
1367 va_start (argp
, gmsgid
);
1368 error_print (_("Warning:"), _(gmsgid
), argp
);
1376 if (warnings_are_errors
)
1377 gfc_increment_error_count();
1381 /* Issue an error. */
1382 /* This function uses the common diagnostics, but does not support
1383 two locations; when being used in scanner.c, ensure that the location
1384 is properly setup. Otherwise, use gfc_error_1. */
1387 gfc_error (const char *gmsgid
, va_list ap
)
1392 if (warnings_not_errors
)
1394 gfc_warning (/*opt=*/0, gmsgid
, argp
);
1399 if (suppress_errors
)
1405 diagnostic_info diagnostic
;
1406 bool fatal_errors
= global_dc
->fatal_errors
;
1407 pretty_printer
*pp
= global_dc
->printer
;
1408 output_buffer
*tmp_buffer
= pp
->buffer
;
1410 gfc_clear_pp_buffer (pp_error_buffer
);
1414 pp
->buffer
= pp_error_buffer
;
1415 global_dc
->fatal_errors
= false;
1416 /* To prevent -fmax-errors= triggering, we decrease it before
1417 report_diagnostic increases it. */
1421 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, UNKNOWN_LOCATION
, DK_ERROR
);
1422 report_diagnostic (&diagnostic
);
1426 pp
->buffer
= tmp_buffer
;
1427 global_dc
->fatal_errors
= fatal_errors
;
1435 gfc_error (const char *gmsgid
, ...)
1438 va_start (argp
, gmsgid
);
1439 gfc_error (gmsgid
, argp
);
1444 /* Immediate error. */
1445 /* Use gfc_error_now instead, unless two locations are used in the same
1446 warning or for scanner.c, if the location is not properly set up. */
1449 gfc_error_now_1 (const char *gmsgid
, ...)
1452 bool buffered_p_saved
;
1454 error_buffer
.flag
= 1;
1455 error_buffer
.index
= 0;
1456 cur_error_buffer
= &error_buffer
;
1458 buffered_p_saved
= buffered_p
;
1461 va_start (argp
, gmsgid
);
1462 error_print (_("Error:"), _(gmsgid
), argp
);
1467 gfc_increment_error_count();
1469 buffered_p
= buffered_p_saved
;
1471 if (flag_fatal_errors
)
1472 exit (FATAL_EXIT_CODE
);
1476 /* This shouldn't happen... but sometimes does. */
1479 gfc_internal_error (const char *gmsgid
, ...)
1482 diagnostic_info diagnostic
;
1484 va_start (argp
, gmsgid
);
1485 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, UNKNOWN_LOCATION
, DK_ICE
);
1486 report_diagnostic (&diagnostic
);
1493 /* Clear the error flag when we start to compile a source line. */
1496 gfc_clear_error (void)
1498 error_buffer
.flag
= 0;
1499 warnings_not_errors
= false;
1500 gfc_clear_pp_buffer (pp_error_buffer
);
1504 /* Tests the state of error_flag. */
1507 gfc_error_flag_test (void)
1509 return error_buffer
.flag
1510 || !gfc_output_buffer_empty_p (pp_error_buffer
);
1514 /* Check to see if any errors have been saved.
1515 If so, print the error. Returns the state of error_flag. */
1518 gfc_error_check (void)
1520 bool error_raised
= (bool) error_buffer
.flag
;
1524 if (error_buffer
.message
!= NULL
)
1525 fputs (error_buffer
.message
, stderr
);
1526 error_buffer
.flag
= 0;
1527 gfc_clear_pp_buffer (pp_error_buffer
);
1529 gfc_increment_error_count();
1531 if (flag_fatal_errors
)
1532 exit (FATAL_EXIT_CODE
);
1534 /* This is for the new diagnostics machinery. */
1535 else if (! gfc_output_buffer_empty_p (pp_error_buffer
))
1537 error_raised
= true;
1538 pretty_printer
*pp
= global_dc
->printer
;
1539 output_buffer
*tmp_buffer
= pp
->buffer
;
1540 pp
->buffer
= pp_error_buffer
;
1541 pp_really_flush (pp
);
1543 gcc_assert (gfc_output_buffer_empty_p (pp_error_buffer
));
1544 diagnostic_action_after_output (global_dc
, DK_ERROR
);
1545 pp
->buffer
= tmp_buffer
;
1548 return error_raised
;
1551 /* Move the text buffered from FROM to TO, then clear
1552 FROM. Independently if there was text in FROM, TO is also
1556 gfc_move_output_buffer_from_to (output_buffer
*from
, output_buffer
*to
)
1558 gfc_clear_pp_buffer (to
);
1559 /* We make sure this is always buffered. */
1560 to
->flush_p
= false;
1562 if (! gfc_output_buffer_empty_p (from
))
1564 const char *str
= output_buffer_formatted_text (from
);
1565 output_buffer_append_r (to
, str
, strlen (str
));
1566 gfc_clear_pp_buffer (from
);
1570 /* Save the existing error state. */
1573 gfc_push_error (output_buffer
*buffer_err
, gfc_error_buf
*err
)
1575 err
->flag
= error_buffer
.flag
;
1576 if (error_buffer
.flag
)
1577 err
->message
= xstrdup (error_buffer
.message
);
1579 error_buffer
.flag
= 0;
1581 /* This part uses the common diagnostics. */
1582 gfc_move_output_buffer_from_to (pp_error_buffer
, buffer_err
);
1586 /* Restore a previous pushed error state. */
1589 gfc_pop_error (output_buffer
*buffer_err
, gfc_error_buf
*err
)
1591 error_buffer
.flag
= err
->flag
;
1592 if (error_buffer
.flag
)
1594 size_t len
= strlen (err
->message
) + 1;
1595 gcc_assert (len
<= error_buffer
.allocated
);
1596 memcpy (error_buffer
.message
, err
->message
, len
);
1597 free (err
->message
);
1599 /* This part uses the common diagnostics. */
1600 gfc_move_output_buffer_from_to (buffer_err
, pp_error_buffer
);
1604 /* Free a pushed error state, but keep the current error state. */
1607 gfc_free_error (output_buffer
*buffer_err
, gfc_error_buf
*err
)
1610 free (err
->message
);
1612 gfc_clear_pp_buffer (buffer_err
);
1616 /* Report the number of warnings and errors that occurred to the caller. */
1619 gfc_get_errors (int *w
, int *e
)
1622 *w
= warnings
+ warningcount
+ werrorcount
;
1624 *e
= errors
+ errorcount
+ sorrycount
+ werrorcount
;
1628 /* Switch errors into warnings. */
1631 gfc_errors_to_warnings (bool f
)
1633 warnings_not_errors
= f
;
1637 gfc_diagnostics_init (void)
1639 diagnostic_starter (global_dc
) = gfc_diagnostic_starter
;
1640 diagnostic_finalizer (global_dc
) = gfc_diagnostic_finalizer
;
1641 diagnostic_format_decoder (global_dc
) = gfc_format_decoder
;
1642 global_dc
->caret_char
= '^';
1643 pp_warning_buffer
= new (XNEW (output_buffer
)) output_buffer ();
1644 pp_warning_buffer
->flush_p
= false;
1645 pp_error_buffer
= new (XNEW (output_buffer
)) output_buffer ();
1646 pp_error_buffer
->flush_p
= false;
1650 gfc_diagnostics_finish (void)
1652 tree_diagnostics_defaults (global_dc
);
1653 /* We still want to use the gfc starter and finalizer, not the tree
1655 diagnostic_starter (global_dc
) = gfc_diagnostic_starter
;
1656 diagnostic_finalizer (global_dc
) = gfc_diagnostic_finalizer
;
1657 global_dc
->caret_char
= '^';