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"
33 #include "diagnostic.h"
34 #include "diagnostic-color.h"
35 #include "tree-diagnostic.h" /* tree_diagnostics_defaults */
37 static int suppress_errors
= 0;
39 static bool warnings_not_errors
= false;
41 static int terminal_width
, errors
, warnings
;
43 static gfc_error_buf error_buffer
, warning_buffer
, *cur_error_buffer
;
45 /* True if the error/warnings should be buffered. */
46 static bool buffered_p
;
48 /* These are always buffered buffers (.flush_p == false) to be used by
49 the pretty-printer. */
50 static output_buffer pp_warning_buffer
;
51 static int warningcount_buffered
, werrorcount_buffered
;
53 #include <new> /* For placement-new */
55 /* Go one level deeper suppressing errors. */
58 gfc_push_suppress_errors (void)
60 gcc_assert (suppress_errors
>= 0);
65 /* Leave one level of error suppressing. */
68 gfc_pop_suppress_errors (void)
70 gcc_assert (suppress_errors
> 0);
75 /* Determine terminal width (for trimming source lines in output). */
78 gfc_get_terminal_width (void)
80 return isatty (STDERR_FILENO
) ? get_terminal_width () : INT_MAX
;
84 /* Per-file error initialization. */
87 gfc_error_init_1 (void)
89 terminal_width
= gfc_get_terminal_width ();
92 gfc_buffer_error (false);
96 /* Set the flag for buffering errors or not. */
99 gfc_buffer_error (bool flag
)
102 pp_warning_buffer
.flush_p
= !flag
;
106 /* Add a single character to the error buffer or output depending on
114 if (cur_error_buffer
->index
>= cur_error_buffer
->allocated
)
116 cur_error_buffer
->allocated
= cur_error_buffer
->allocated
117 ? cur_error_buffer
->allocated
* 2 : 1000;
118 cur_error_buffer
->message
= XRESIZEVEC (char, cur_error_buffer
->message
,
119 cur_error_buffer
->allocated
);
121 cur_error_buffer
->message
[cur_error_buffer
->index
++] = c
;
127 /* We build up complete lines before handing things
128 over to the library in order to speed up error printing. */
130 static size_t allocated
= 0, index
= 0;
132 if (index
+ 1 >= allocated
)
134 allocated
= allocated
? allocated
* 2 : 1000;
135 line
= XRESIZEVEC (char, line
, allocated
);
141 fputs (line
, stderr
);
149 /* Copy a string to wherever it needs to go. */
152 error_string (const char *p
)
159 /* Print a formatted integer to the error buffer or output. */
164 error_uinteger (unsigned long int i
)
166 char *p
, int_buf
[IBUF_LEN
];
168 p
= int_buf
+ IBUF_LEN
- 1;
180 error_string (p
+ 1);
184 error_integer (long int i
)
190 u
= (unsigned long int) -i
;
201 gfc_widechar_display_length (gfc_char_t c
)
203 if (gfc_wide_is_printable (c
) || c
== '\t')
204 /* Printable ASCII character, or tabulation (output as a space). */
206 else if (c
< ((gfc_char_t
) 1 << 8))
207 /* Displayed as \x?? */
209 else if (c
< ((gfc_char_t
) 1 << 16))
210 /* Displayed as \u???? */
213 /* Displayed as \U???????? */
218 /* Length of the ASCII representation of the wide string, escaping wide
219 characters as print_wide_char_into_buffer() does. */
222 gfc_wide_display_length (const gfc_char_t
*str
)
226 for (i
= 0, len
= 0; str
[i
]; i
++)
227 len
+= gfc_widechar_display_length (str
[i
]);
233 print_wide_char_into_buffer (gfc_char_t c
, char *buf
)
235 static const char xdigit
[16] = { '0', '1', '2', '3', '4', '5', '6',
236 '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
238 if (gfc_wide_is_printable (c
) || c
== '\t')
241 /* Tabulation is output as a space. */
242 buf
[0] = (unsigned char) (c
== '\t' ? ' ' : c
);
245 else if (c
< ((gfc_char_t
) 1 << 8))
248 buf
[3] = xdigit
[c
& 0x0F];
250 buf
[2] = xdigit
[c
& 0x0F];
256 else if (c
< ((gfc_char_t
) 1 << 16))
259 buf
[5] = xdigit
[c
& 0x0F];
261 buf
[4] = xdigit
[c
& 0x0F];
263 buf
[3] = xdigit
[c
& 0x0F];
265 buf
[2] = xdigit
[c
& 0x0F];
274 buf
[9] = xdigit
[c
& 0x0F];
276 buf
[8] = xdigit
[c
& 0x0F];
278 buf
[7] = xdigit
[c
& 0x0F];
280 buf
[6] = xdigit
[c
& 0x0F];
282 buf
[5] = xdigit
[c
& 0x0F];
284 buf
[4] = xdigit
[c
& 0x0F];
286 buf
[3] = xdigit
[c
& 0x0F];
288 buf
[2] = xdigit
[c
& 0x0F];
296 static char wide_char_print_buffer
[11];
299 gfc_print_wide_char (gfc_char_t c
)
301 print_wide_char_into_buffer (c
, wide_char_print_buffer
);
302 return wide_char_print_buffer
;
306 /* Show the file, where it was included, and the source line, give a
307 locus. Calls error_printf() recursively, but the recursion is at
308 most one level deep. */
310 static void error_printf (const char *, ...) ATTRIBUTE_GCC_GFC(1,2);
313 show_locus (locus
*loc
, int c1
, int c2
)
320 /* TODO: Either limit the total length and number of included files
321 displayed or add buffering of arbitrary number of characters in
324 /* Write out the error header line, giving the source file and error
325 location (in GNU standard "[file]:[line].[column]:" format),
326 followed by an "included by" stack and a blank line. This header
327 format is matched by a testsuite parser defined in
328 lib/gfortran-dg.exp. */
333 error_string (f
->filename
);
336 error_integer (LOCATION_LINE (lb
->location
));
338 if ((c1
> 0) || (c2
> 0))
344 if ((c1
> 0) && (c2
> 0))
355 i
= f
->inclusion_line
;
358 if (f
== NULL
) break;
360 error_printf (" Included at %s:%d:", f
->filename
, i
);
365 /* Calculate an appropriate horizontal offset of the source line in
366 order to get the error locus within the visible portion of the
367 line. Note that if the margin of 5 here is changed, the
368 corresponding margin of 10 in show_loci should be changed. */
372 /* If the two loci would appear in the same column, we shift
373 '2' one column to the right, so as to print '12' rather than
374 just '1'. We do this here so it will be accounted for in the
375 margin calculations. */
380 cmax
= (c1
< c2
) ? c2
: c1
;
381 if (cmax
> terminal_width
- 5)
382 offset
= cmax
- terminal_width
+ 5;
384 /* Show the line itself, taking care not to print more than what can
385 show up on the terminal. Tabs are converted to spaces, and
386 nonprintable characters are converted to a "\xNN" sequence. */
388 p
= &(lb
->line
[offset
]);
389 i
= gfc_wide_display_length (p
);
390 if (i
> terminal_width
)
391 i
= terminal_width
- 1;
395 static char buffer
[11];
396 i
-= print_wide_char_into_buffer (*p
++, buffer
);
397 error_string (buffer
);
402 /* Show the '1' and/or '2' corresponding to the column of the error
403 locus. Note that a value of -1 for c1 or c2 will simply cause
404 the relevant number not to be printed. */
410 p
= &(lb
->line
[offset
]);
411 for (i
= 0; i
< cmax
; i
++)
414 spaces
= gfc_widechar_display_length (*p
++);
417 error_char ('1'), spaces
--;
419 error_char ('2'), spaces
--;
421 for (j
= 0; j
< spaces
; j
++)
435 /* As part of printing an error, we show the source lines that caused
436 the problem. We show at least one, and possibly two loci; the two
437 loci may or may not be on the same source line. */
440 show_loci (locus
*l1
, locus
*l2
)
444 if (l1
== NULL
|| l1
->lb
== NULL
)
446 error_printf ("<During initialization>\n");
450 /* While calculating parameters for printing the loci, we consider possible
451 reasons for printing one per line. If appropriate, print the loci
452 individually; otherwise we print them both on the same line. */
454 c1
= l1
->nextc
- l1
->lb
->line
;
457 show_locus (l1
, c1
, -1);
461 c2
= l2
->nextc
- l2
->lb
->line
;
468 /* Note that the margin value of 10 here needs to be less than the
469 margin of 5 used in the calculation of offset in show_locus. */
471 if (l1
->lb
!= l2
->lb
|| m
> terminal_width
- 10)
473 show_locus (l1
, c1
, -1);
474 show_locus (l2
, -1, c2
);
478 show_locus (l1
, c1
, c2
);
484 /* Workhorse for the error printing subroutines. This subroutine is
485 inspired by g77's error handling and is similar to printf() with
486 the following %-codes:
488 %c Character, %d or %i Integer, %s String, %% Percent
489 %L Takes locus argument
490 %C Current locus (no argument)
492 If a locus pointer is given, the actual source line is printed out
493 and the column is indicated. Since we want the error message at
494 the bottom of any source file information, we must scan the
495 argument list twice -- once to determine whether the loci are
496 present and record this for printing, and once to print the error
497 message after and loci have been printed. A maximum of two locus
498 arguments are permitted.
500 This function is also called (recursively) by show_locus in the
501 case of included files; however, as show_locus does not resupply
502 any loci, the recursion is at most one level deep. */
506 static void ATTRIBUTE_GCC_GFC(2,0)
507 error_print (const char *type
, const char *format0
, va_list argp
)
509 enum { TYPE_CURRENTLOC
, TYPE_LOCUS
, TYPE_INTEGER
, TYPE_UINTEGER
,
510 TYPE_LONGINT
, TYPE_ULONGINT
, TYPE_CHAR
, TYPE_STRING
,
519 unsigned int uintval
;
521 unsigned long int ulongintval
;
523 const char * stringval
;
525 } arg
[MAX_ARGS
], spec
[MAX_ARGS
];
526 /* spec is the array of specifiers, in the same order as they
527 appear in the format string. arg is the array of arguments,
528 in the same order as they appear in the va_list. */
531 int i
, n
, have_l1
, pos
, maxpos
;
532 locus
*l1
, *l2
, *loc
;
535 loc
= l1
= l2
= NULL
;
544 for (i
= 0; i
< MAX_ARGS
; i
++)
546 arg
[i
].type
= NOTYPE
;
550 /* First parse the format string for position specifiers. */
563 if (ISDIGIT (*format
))
565 /* This is a position specifier. For example, the number
566 12 in the format string "%12$d", which specifies the third
567 argument of the va_list, formatted in %d format.
568 For details, see "man 3 printf". */
569 pos
= atoi(format
) - 1;
570 gcc_assert (pos
>= 0);
571 while (ISDIGIT(*format
))
573 gcc_assert (*format
== '$');
587 arg
[pos
].type
= TYPE_CURRENTLOC
;
591 arg
[pos
].type
= TYPE_LOCUS
;
596 arg
[pos
].type
= TYPE_INTEGER
;
600 arg
[pos
].type
= TYPE_UINTEGER
;
606 arg
[pos
].type
= TYPE_ULONGINT
;
607 else if (c
== 'i' || c
== 'd')
608 arg
[pos
].type
= TYPE_LONGINT
;
614 arg
[pos
].type
= TYPE_CHAR
;
618 arg
[pos
].type
= TYPE_STRING
;
628 /* Then convert the values for each %-style argument. */
629 for (pos
= 0; pos
<= maxpos
; pos
++)
631 gcc_assert (arg
[pos
].type
!= NOTYPE
);
632 switch (arg
[pos
].type
)
634 case TYPE_CURRENTLOC
:
635 loc
= &gfc_current_locus
;
639 if (arg
[pos
].type
== TYPE_LOCUS
)
640 loc
= va_arg (argp
, locus
*);
645 arg
[pos
].u
.stringval
= "(2)";
651 arg
[pos
].u
.stringval
= "(1)";
656 arg
[pos
].u
.intval
= va_arg (argp
, int);
660 arg
[pos
].u
.uintval
= va_arg (argp
, unsigned int);
664 arg
[pos
].u
.longintval
= va_arg (argp
, long int);
668 arg
[pos
].u
.ulongintval
= va_arg (argp
, unsigned long int);
672 arg
[pos
].u
.charval
= (char) va_arg (argp
, int);
676 arg
[pos
].u
.stringval
= (const char *) va_arg (argp
, char *);
684 for (n
= 0; spec
[n
].pos
>= 0; n
++)
685 spec
[n
].u
= arg
[spec
[n
].pos
].u
;
687 /* Show the current loci if we have to. */
701 for (; *format
; format
++)
705 error_char (*format
);
710 if (ISDIGIT (*format
))
712 /* This is a position specifier. See comment above. */
713 while (ISDIGIT (*format
))
716 /* Skip over the dollar sign. */
727 error_char (spec
[n
++].u
.charval
);
731 case 'C': /* Current locus */
732 case 'L': /* Specified locus */
733 error_string (spec
[n
++].u
.stringval
);
738 error_integer (spec
[n
++].u
.intval
);
742 error_uinteger (spec
[n
++].u
.uintval
);
748 error_uinteger (spec
[n
++].u
.ulongintval
);
750 error_integer (spec
[n
++].u
.longintval
);
760 /* Wrapper for error_print(). */
763 error_printf (const char *gmsgid
, ...)
767 va_start (argp
, gmsgid
);
768 error_print ("", _(gmsgid
), argp
);
773 /* Increment the number of errors, and check whether too many have
777 gfc_increment_error_count (void)
780 if ((gfc_option
.max_errors
!= 0) && (errors
>= gfc_option
.max_errors
))
781 gfc_fatal_error ("Error count reached limit of %d.", gfc_option
.max_errors
);
785 /* Clear any output buffered in a pretty-print output_buffer. */
788 gfc_clear_pp_buffer (output_buffer
*this_buffer
)
790 pretty_printer
*pp
= global_dc
->printer
;
791 output_buffer
*tmp_buffer
= pp
->buffer
;
792 pp
->buffer
= this_buffer
;
793 pp_clear_output_area (pp
);
794 pp
->buffer
= tmp_buffer
;
798 /* Issue a warning. */
799 /* Use gfc_warning instead, unless two locations are used in the same
800 warning or for scanner.c, if the location is not properly set up. */
803 gfc_warning_1 (const char *gmsgid
, ...)
807 if (inhibit_warnings
)
810 warning_buffer
.flag
= 1;
811 warning_buffer
.index
= 0;
812 cur_error_buffer
= &warning_buffer
;
814 va_start (argp
, gmsgid
);
815 error_print (_("Warning:"), _(gmsgid
), argp
);
823 if (warnings_are_errors
)
824 gfc_increment_error_count();
829 /* This is just a helper function to avoid duplicating the logic of
833 gfc_warning (int opt
, const char *gmsgid
, va_list ap
) ATTRIBUTE_GCC_GFC(2,0);
836 gfc_warning (int opt
, const char *gmsgid
, va_list ap
)
841 diagnostic_info diagnostic
;
842 bool fatal_errors
= global_dc
->fatal_errors
;
843 pretty_printer
*pp
= global_dc
->printer
;
844 output_buffer
*tmp_buffer
= pp
->buffer
;
846 gfc_clear_pp_buffer (&pp_warning_buffer
);
850 pp
->buffer
= &pp_warning_buffer
;
851 global_dc
->fatal_errors
= false;
852 /* To prevent -fmax-errors= triggering. */
856 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, UNKNOWN_LOCATION
,
858 diagnostic
.option_index
= opt
;
859 bool ret
= report_diagnostic (&diagnostic
);
863 pp
->buffer
= tmp_buffer
;
864 global_dc
->fatal_errors
= fatal_errors
;
866 warningcount_buffered
= 0;
867 werrorcount_buffered
= 0;
868 /* Undo the above --werrorcount if not Werror, otherwise
869 werrorcount is correct already. */
872 else if (diagnostic
.kind
== DK_ERROR
)
873 ++werrorcount_buffered
;
875 ++werrorcount
, --warningcount
, ++warningcount_buffered
;
882 /* Issue a warning. */
883 /* This function uses the common diagnostics, but does not support
884 two locations; when being used in scanner.c, ensure that the location
885 is properly setup. Otherwise, use gfc_warning_1. */
888 gfc_warning (int opt
, const char *gmsgid
, ...)
892 va_start (argp
, gmsgid
);
893 bool ret
= gfc_warning (opt
, gmsgid
, argp
);
899 gfc_warning (const char *gmsgid
, ...)
903 va_start (argp
, gmsgid
);
904 bool ret
= gfc_warning (0, gmsgid
, argp
);
910 /* Whether, for a feature included in a given standard set (GFC_STD_*),
911 we should issue an error or a warning, or be quiet. */
914 gfc_notification_std (int std
)
918 warning
= ((gfc_option
.warn_std
& std
) != 0) && !inhibit_warnings
;
919 if ((gfc_option
.allow_std
& std
) != 0 && !warning
)
922 return warning
? WARNING
: ERROR
;
926 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
927 feature. An error/warning will be issued if the currently selected
928 standard does not contain the requested bits. Return false if
929 an error is generated. */
932 gfc_notify_std (int std
, const char *gmsgid
, ...)
936 const char *msg1
, *msg2
;
939 warning
= ((gfc_option
.warn_std
& std
) != 0) && !inhibit_warnings
;
940 if ((gfc_option
.allow_std
& std
) != 0 && !warning
)
944 return warning
? true : false;
946 cur_error_buffer
= warning
? &warning_buffer
: &error_buffer
;
947 cur_error_buffer
->flag
= 1;
948 cur_error_buffer
->index
= 0;
951 msg1
= _("Warning:");
957 case GFC_STD_F2008_TS
:
958 msg2
= "TS 29113/TS 18508:";
960 case GFC_STD_F2008_OBS
:
961 msg2
= _("Fortran 2008 obsolescent feature:");
964 msg2
= "Fortran 2008:";
967 msg2
= "Fortran 2003:";
970 msg2
= _("GNU Extension:");
973 msg2
= _("Legacy Extension:");
975 case GFC_STD_F95_OBS
:
976 msg2
= _("Obsolescent feature:");
978 case GFC_STD_F95_DEL
:
979 msg2
= _("Deleted feature:");
985 buffer
= (char *) alloca (strlen (msg1
) + strlen (msg2
) + 2);
986 strcpy (buffer
, msg1
);
987 strcat (buffer
, " ");
988 strcat (buffer
, msg2
);
990 va_start (argp
, gmsgid
);
991 error_print (buffer
, _(gmsgid
), argp
);
998 if (warning
&& !warnings_are_errors
)
1001 gfc_increment_error_count();
1002 cur_error_buffer
->flag
= 0;
1005 return (warning
&& !warnings_are_errors
) ? true : false;
1009 /* Immediate warning (i.e. do not buffer the warning). */
1010 /* Use gfc_warning_now instead, unless two locations are used in the same
1011 warning or for scanner.c, if the location is not properly set up. */
1014 gfc_warning_now_1 (const char *gmsgid
, ...)
1017 bool buffered_p_saved
;
1019 if (inhibit_warnings
)
1022 buffered_p_saved
= buffered_p
;
1026 va_start (argp
, gmsgid
);
1027 error_print (_("Warning:"), _(gmsgid
), argp
);
1032 if (warnings_are_errors
)
1033 gfc_increment_error_count();
1035 buffered_p
= buffered_p_saved
;
1038 /* Called from output_format -- during diagnostic message processing
1039 to handle Fortran specific format specifiers with the following meanings:
1041 %C Current locus (no argument)
1042 %L Takes locus argument
1045 gfc_format_decoder (pretty_printer
*pp
,
1046 text_info
*text
, const char *spec
,
1047 int precision ATTRIBUTE_UNUSED
, bool wide ATTRIBUTE_UNUSED
,
1048 bool plus ATTRIBUTE_UNUSED
, bool hash ATTRIBUTE_UNUSED
)
1055 static const char *result
= "(1)";
1058 loc
= &gfc_current_locus
;
1060 loc
= va_arg (*text
->args_ptr
, locus
*);
1061 gcc_assert (loc
->nextc
- loc
->lb
->line
>= 0);
1062 unsigned int offset
= loc
->nextc
- loc
->lb
->line
;
1063 gcc_assert (text
->locus
);
1065 = linemap_position_for_loc_and_offset (line_table
,
1068 global_dc
->caret_char
= '1';
1069 pp_string (pp
, result
);
1077 /* Return a malloc'd string describing a location. The caller is
1078 responsible for freeing the memory. */
1080 gfc_diagnostic_build_prefix (diagnostic_context
*context
,
1081 const diagnostic_info
*diagnostic
)
1083 static const char *const diagnostic_kind_text
[] = {
1084 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
1085 #include "gfc-diagnostic.def"
1086 #undef DEFINE_DIAGNOSTIC_KIND
1089 static const char *const diagnostic_kind_color
[] = {
1090 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
1091 #include "gfc-diagnostic.def"
1092 #undef DEFINE_DIAGNOSTIC_KIND
1095 gcc_assert (diagnostic
->kind
< DK_LAST_DIAGNOSTIC_KIND
);
1096 const char *text
= _(diagnostic_kind_text
[diagnostic
->kind
]);
1097 const char *text_cs
= "", *text_ce
= "";
1098 pretty_printer
*pp
= context
->printer
;
1100 if (diagnostic_kind_color
[diagnostic
->kind
])
1102 text_cs
= colorize_start (pp_show_color (pp
),
1103 diagnostic_kind_color
[diagnostic
->kind
]);
1104 text_ce
= colorize_stop (pp_show_color (pp
));
1106 return build_message_string ("%s%s:%s ", text_cs
, text
, text_ce
);
1109 /* Return a malloc'd string describing a location. The caller is
1110 responsible for freeing the memory. */
1112 gfc_diagnostic_build_locus_prefix (diagnostic_context
*context
,
1113 const diagnostic_info
*diagnostic
)
1115 pretty_printer
*pp
= context
->printer
;
1116 const char *locus_cs
= colorize_start (pp_show_color (pp
), "locus");
1117 const char *locus_ce
= colorize_stop (pp_show_color (pp
));
1118 expanded_location s
= diagnostic_expand_location (diagnostic
);
1119 return (s
.file
== NULL
1120 ? build_message_string ("%s%s:%s", locus_cs
, progname
, locus_ce
)
1121 : !strcmp (s
.file
, N_("<built-in>"))
1122 ? build_message_string ("%s%s:%s", locus_cs
, s
.file
, locus_ce
)
1123 : context
->show_column
1124 ? build_message_string ("%s%s:%d:%d:%s", locus_cs
, s
.file
, s
.line
,
1126 : build_message_string ("%s%s:%d:%s", locus_cs
, s
.file
, s
.line
, locus_ce
));
1130 gfc_diagnostic_starter (diagnostic_context
*context
,
1131 diagnostic_info
*diagnostic
)
1133 char * locus_prefix
= gfc_diagnostic_build_locus_prefix (context
, diagnostic
);
1134 char * prefix
= gfc_diagnostic_build_prefix (context
, diagnostic
);
1135 /* First we assume there is a caret line. */
1136 pp_set_prefix (context
->printer
, NULL
);
1137 if (pp_needs_newline (context
->printer
))
1138 pp_newline (context
->printer
);
1139 pp_verbatim (context
->printer
, locus_prefix
);
1140 /* Fortran uses an empty line between locus and caret line. */
1141 pp_newline (context
->printer
);
1142 diagnostic_show_locus (context
, diagnostic
);
1143 if (pp_needs_newline (context
->printer
))
1145 pp_newline (context
->printer
);
1146 /* If the caret line was shown, the prefix does not contain the
1148 pp_set_prefix (context
->printer
, prefix
);
1152 /* Otherwise, start again. */
1153 pp_clear_output_area(context
->printer
);
1154 pp_set_prefix (context
->printer
, concat (locus_prefix
, " ", prefix
, NULL
));
1157 free (locus_prefix
);
1161 gfc_diagnostic_finalizer (diagnostic_context
*context
,
1162 diagnostic_info
*diagnostic ATTRIBUTE_UNUSED
)
1164 pp_destroy_prefix (context
->printer
);
1165 pp_newline_and_flush (context
->printer
);
1168 /* Immediate warning (i.e. do not buffer the warning). */
1169 /* This function uses the common diagnostics, but does not support
1170 two locations; when being used in scanner.c, ensure that the location
1171 is properly setup. Otherwise, use gfc_warning_now_1. */
1174 gfc_warning_now (int opt
, const char *gmsgid
, ...)
1177 diagnostic_info diagnostic
;
1180 va_start (argp
, gmsgid
);
1181 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, UNKNOWN_LOCATION
,
1183 diagnostic
.option_index
= opt
;
1184 ret
= report_diagnostic (&diagnostic
);
1189 /* Immediate warning (i.e. do not buffer the warning). */
1190 /* This function uses the common diagnostics, but does not support
1191 two locations; when being used in scanner.c, ensure that the location
1192 is properly setup. Otherwise, use gfc_warning_now_1. */
1195 gfc_warning_now (const char *gmsgid
, ...)
1198 diagnostic_info diagnostic
;
1201 va_start (argp
, gmsgid
);
1202 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, UNKNOWN_LOCATION
,
1204 ret
= report_diagnostic (&diagnostic
);
1210 /* Immediate error (i.e. do not buffer). */
1211 /* This function uses the common diagnostics, but does not support
1212 two locations; when being used in scanner.c, ensure that the location
1213 is properly setup. Otherwise, use gfc_error_now_1. */
1216 gfc_error_now (const char *gmsgid
, ...)
1219 diagnostic_info diagnostic
;
1221 va_start (argp
, gmsgid
);
1222 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, UNKNOWN_LOCATION
, DK_ERROR
);
1223 report_diagnostic (&diagnostic
);
1228 /* Fatal error, never returns. */
1231 gfc_fatal_error (const char *gmsgid
, ...)
1234 diagnostic_info diagnostic
;
1236 va_start (argp
, gmsgid
);
1237 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, UNKNOWN_LOCATION
, DK_FATAL
);
1238 report_diagnostic (&diagnostic
);
1244 /* Clear the warning flag. */
1247 gfc_clear_warning (void)
1249 warning_buffer
.flag
= 0;
1251 gfc_clear_pp_buffer (&pp_warning_buffer
);
1252 warningcount_buffered
= 0;
1253 werrorcount_buffered
= 0;
1254 pp_warning_buffer
.flush_p
= false;
1258 /* Check to see if any warnings have been saved.
1259 If so, print the warning. */
1262 gfc_warning_check (void)
1264 if (warning_buffer
.flag
)
1267 if (warning_buffer
.message
!= NULL
)
1268 fputs (warning_buffer
.message
, stderr
);
1269 warning_buffer
.flag
= 0;
1272 /* This is for the new diagnostics machinery. */
1273 pretty_printer
*pp
= global_dc
->printer
;
1274 output_buffer
*tmp_buffer
= pp
->buffer
;
1275 pp
->buffer
= &pp_warning_buffer
;
1276 if (pp_last_position_in_text (pp
) != NULL
)
1278 pp_really_flush (pp
);
1279 pp_warning_buffer
.flush_p
= true;
1280 warningcount
+= warningcount_buffered
;
1281 werrorcount
+= werrorcount_buffered
;
1284 pp
->buffer
= tmp_buffer
;
1288 /* Issue an error. */
1291 gfc_error (const char *gmsgid
, ...)
1295 if (warnings_not_errors
)
1298 if (suppress_errors
)
1301 error_buffer
.flag
= 1;
1302 error_buffer
.index
= 0;
1303 cur_error_buffer
= &error_buffer
;
1305 va_start (argp
, gmsgid
);
1306 error_print (_("Error:"), _(gmsgid
), argp
);
1312 gfc_increment_error_count();
1318 if (inhibit_warnings
)
1321 warning_buffer
.flag
= 1;
1322 warning_buffer
.index
= 0;
1323 cur_error_buffer
= &warning_buffer
;
1325 va_start (argp
, gmsgid
);
1326 error_print (_("Warning:"), _(gmsgid
), argp
);
1334 if (warnings_are_errors
)
1335 gfc_increment_error_count();
1340 /* Immediate error. */
1341 /* Use gfc_error_now instead, unless two locations are used in the same
1342 warning or for scanner.c, if the location is not properly set up. */
1345 gfc_error_now_1 (const char *gmsgid
, ...)
1348 bool buffered_p_saved
;
1350 error_buffer
.flag
= 1;
1351 error_buffer
.index
= 0;
1352 cur_error_buffer
= &error_buffer
;
1354 buffered_p_saved
= buffered_p
;
1357 va_start (argp
, gmsgid
);
1358 error_print (_("Error:"), _(gmsgid
), argp
);
1363 gfc_increment_error_count();
1365 buffered_p
= buffered_p_saved
;
1367 if (flag_fatal_errors
)
1368 exit (FATAL_EXIT_CODE
);
1372 /* This shouldn't happen... but sometimes does. */
1375 gfc_internal_error (const char *gmsgid
, ...)
1378 diagnostic_info diagnostic
;
1380 va_start (argp
, gmsgid
);
1381 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, UNKNOWN_LOCATION
, DK_ICE
);
1382 report_diagnostic (&diagnostic
);
1389 /* Clear the error flag when we start to compile a source line. */
1392 gfc_clear_error (void)
1394 error_buffer
.flag
= 0;
1395 warnings_not_errors
= false;
1399 /* Tests the state of error_flag. */
1402 gfc_error_flag_test (void)
1404 return error_buffer
.flag
;
1408 /* Check to see if any errors have been saved.
1409 If so, print the error. Returns the state of error_flag. */
1412 gfc_error_check (void)
1414 bool error_raised
= (bool) error_buffer
.flag
;
1418 if (error_buffer
.message
!= NULL
)
1419 fputs (error_buffer
.message
, stderr
);
1420 error_buffer
.flag
= 0;
1422 gfc_increment_error_count();
1424 if (flag_fatal_errors
)
1425 exit (FATAL_EXIT_CODE
);
1428 return error_raised
;
1432 /* Save the existing error state. */
1435 gfc_push_error (gfc_error_buf
*err
)
1437 err
->flag
= error_buffer
.flag
;
1438 if (error_buffer
.flag
)
1439 err
->message
= xstrdup (error_buffer
.message
);
1441 error_buffer
.flag
= 0;
1445 /* Restore a previous pushed error state. */
1448 gfc_pop_error (gfc_error_buf
*err
)
1450 error_buffer
.flag
= err
->flag
;
1451 if (error_buffer
.flag
)
1453 size_t len
= strlen (err
->message
) + 1;
1454 gcc_assert (len
<= error_buffer
.allocated
);
1455 memcpy (error_buffer
.message
, err
->message
, len
);
1456 free (err
->message
);
1461 /* Free a pushed error state, but keep the current error state. */
1464 gfc_free_error (gfc_error_buf
*err
)
1467 free (err
->message
);
1471 /* Report the number of warnings and errors that occurred to the caller. */
1474 gfc_get_errors (int *w
, int *e
)
1477 *w
= warnings
+ warningcount
+ werrorcount
;
1479 *e
= errors
+ errorcount
+ sorrycount
+ werrorcount
;
1483 /* Switch errors into warnings. */
1486 gfc_errors_to_warnings (bool f
)
1488 warnings_not_errors
= f
;
1492 gfc_diagnostics_init (void)
1494 diagnostic_starter (global_dc
) = gfc_diagnostic_starter
;
1495 diagnostic_finalizer (global_dc
) = gfc_diagnostic_finalizer
;
1496 diagnostic_format_decoder (global_dc
) = gfc_format_decoder
;
1497 global_dc
->caret_char
= '^';
1498 new (&pp_warning_buffer
) output_buffer ();
1502 gfc_diagnostics_finish (void)
1504 tree_diagnostics_defaults (global_dc
);
1505 /* We still want to use the gfc starter and finalizer, not the tree
1507 diagnostic_starter (global_dc
) = gfc_diagnostic_starter
;
1508 diagnostic_finalizer (global_dc
) = gfc_diagnostic_finalizer
;
1509 global_dc
->caret_char
= '^';