2 Copyright (C) 2000-2021 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
;
43 /* True if the error/warnings should be buffered. */
44 static bool buffered_p
;
46 static gfc_error_buffer error_buffer
;
47 /* These are always buffered buffers (.flush_p == false) to be used by
48 the pretty-printer. */
49 static output_buffer
*pp_error_buffer
, *pp_warning_buffer
;
50 static int warningcount_buffered
, werrorcount_buffered
;
52 /* Return true if there output_buffer is empty. */
55 gfc_output_buffer_empty_p (const output_buffer
* buf
)
57 return output_buffer_last_position_in_text (buf
) == NULL
;
60 /* Go one level deeper suppressing errors. */
63 gfc_push_suppress_errors (void)
65 gcc_assert (suppress_errors
>= 0);
70 gfc_error_opt (int opt
, const char *gmsgid
, va_list ap
) ATTRIBUTE_GCC_GFC(2,0);
73 gfc_warning (int opt
, const char *gmsgid
, va_list ap
) ATTRIBUTE_GCC_GFC(2,0);
76 /* Leave one level of error suppressing. */
79 gfc_pop_suppress_errors (void)
81 gcc_assert (suppress_errors
> 0);
86 /* Determine terminal width (for trimming source lines in output). */
89 gfc_get_terminal_width (void)
91 return isatty (STDERR_FILENO
) ? get_terminal_width () : INT_MAX
;
95 /* Per-file error initialization. */
98 gfc_error_init_1 (void)
100 terminal_width
= gfc_get_terminal_width ();
101 gfc_buffer_error (false);
105 /* Set the flag for buffering errors or not. */
108 gfc_buffer_error (bool flag
)
114 /* Add a single character to the error buffer or output depending on
120 /* FIXME: Unused function to be removed in a subsequent patch. */
124 /* Copy a string to wherever it needs to go. */
127 error_string (const char *p
)
134 /* Print a formatted integer to the error buffer or output. */
139 error_uinteger (unsigned long int i
)
141 char *p
, int_buf
[IBUF_LEN
];
143 p
= int_buf
+ IBUF_LEN
- 1;
155 error_string (p
+ 1);
159 error_integer (long int i
)
165 u
= (unsigned long int) -i
;
176 gfc_widechar_display_length (gfc_char_t c
)
178 if (gfc_wide_is_printable (c
) || c
== '\t')
179 /* Printable ASCII character, or tabulation (output as a space). */
181 else if (c
< ((gfc_char_t
) 1 << 8))
182 /* Displayed as \x?? */
184 else if (c
< ((gfc_char_t
) 1 << 16))
185 /* Displayed as \u???? */
188 /* Displayed as \U???????? */
193 /* Length of the ASCII representation of the wide string, escaping wide
194 characters as print_wide_char_into_buffer() does. */
197 gfc_wide_display_length (const gfc_char_t
*str
)
201 for (i
= 0, len
= 0; str
[i
]; i
++)
202 len
+= gfc_widechar_display_length (str
[i
]);
208 print_wide_char_into_buffer (gfc_char_t c
, char *buf
)
210 static const char xdigit
[16] = { '0', '1', '2', '3', '4', '5', '6',
211 '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
213 if (gfc_wide_is_printable (c
) || c
== '\t')
216 /* Tabulation is output as a space. */
217 buf
[0] = (unsigned char) (c
== '\t' ? ' ' : c
);
220 else if (c
< ((gfc_char_t
) 1 << 8))
223 buf
[3] = xdigit
[c
& 0x0F];
225 buf
[2] = xdigit
[c
& 0x0F];
231 else if (c
< ((gfc_char_t
) 1 << 16))
234 buf
[5] = xdigit
[c
& 0x0F];
236 buf
[4] = xdigit
[c
& 0x0F];
238 buf
[3] = xdigit
[c
& 0x0F];
240 buf
[2] = xdigit
[c
& 0x0F];
249 buf
[9] = xdigit
[c
& 0x0F];
251 buf
[8] = xdigit
[c
& 0x0F];
253 buf
[7] = xdigit
[c
& 0x0F];
255 buf
[6] = xdigit
[c
& 0x0F];
257 buf
[5] = xdigit
[c
& 0x0F];
259 buf
[4] = xdigit
[c
& 0x0F];
261 buf
[3] = xdigit
[c
& 0x0F];
263 buf
[2] = xdigit
[c
& 0x0F];
271 static char wide_char_print_buffer
[11];
274 gfc_print_wide_char (gfc_char_t c
)
276 print_wide_char_into_buffer (c
, wide_char_print_buffer
);
277 return wide_char_print_buffer
;
281 /* Show the file, where it was included, and the source line, give a
282 locus. Calls error_printf() recursively, but the recursion is at
283 most one level deep. */
285 static void error_printf (const char *, ...) ATTRIBUTE_GCC_GFC(1,2);
288 show_locus (locus
*loc
, int c1
, int c2
)
295 /* TODO: Either limit the total length and number of included files
296 displayed or add buffering of arbitrary number of characters in
299 /* Write out the error header line, giving the source file and error
300 location (in GNU standard "[file]:[line].[column]:" format),
301 followed by an "included by" stack and a blank line. This header
302 format is matched by a testsuite parser defined in
303 lib/gfortran-dg.exp. */
308 error_string (f
->filename
);
311 error_integer (LOCATION_LINE (lb
->location
));
313 if ((c1
> 0) || (c2
> 0))
319 if ((c1
> 0) && (c2
> 0))
330 i
= f
->inclusion_line
;
333 if (f
== NULL
) break;
335 error_printf (" Included at %s:%d:", f
->filename
, i
);
340 /* Calculate an appropriate horizontal offset of the source line in
341 order to get the error locus within the visible portion of the
342 line. Note that if the margin of 5 here is changed, the
343 corresponding margin of 10 in show_loci should be changed. */
347 /* If the two loci would appear in the same column, we shift
348 '2' one column to the right, so as to print '12' rather than
349 just '1'. We do this here so it will be accounted for in the
350 margin calculations. */
355 cmax
= (c1
< c2
) ? c2
: c1
;
356 if (cmax
> terminal_width
- 5)
357 offset
= cmax
- terminal_width
+ 5;
359 /* Show the line itself, taking care not to print more than what can
360 show up on the terminal. Tabs are converted to spaces, and
361 nonprintable characters are converted to a "\xNN" sequence. */
363 p
= &(lb
->line
[offset
]);
364 i
= gfc_wide_display_length (p
);
365 if (i
> terminal_width
)
366 i
= terminal_width
- 1;
370 static char buffer
[11];
371 i
-= print_wide_char_into_buffer (*p
++, buffer
);
372 error_string (buffer
);
377 /* Show the '1' and/or '2' corresponding to the column of the error
378 locus. Note that a value of -1 for c1 or c2 will simply cause
379 the relevant number not to be printed. */
385 p
= &(lb
->line
[offset
]);
386 for (i
= 0; i
< cmax
; i
++)
389 spaces
= gfc_widechar_display_length (*p
++);
392 error_char ('1'), spaces
--;
394 error_char ('2'), spaces
--;
396 for (j
= 0; j
< spaces
; j
++)
410 /* As part of printing an error, we show the source lines that caused
411 the problem. We show at least one, and possibly two loci; the two
412 loci may or may not be on the same source line. */
415 show_loci (locus
*l1
, locus
*l2
)
419 if (l1
== NULL
|| l1
->lb
== NULL
)
421 error_printf ("<During initialization>\n");
425 /* While calculating parameters for printing the loci, we consider possible
426 reasons for printing one per line. If appropriate, print the loci
427 individually; otherwise we print them both on the same line. */
429 c1
= l1
->nextc
- l1
->lb
->line
;
432 show_locus (l1
, c1
, -1);
436 c2
= l2
->nextc
- l2
->lb
->line
;
443 /* Note that the margin value of 10 here needs to be less than the
444 margin of 5 used in the calculation of offset in show_locus. */
446 if (l1
->lb
!= l2
->lb
|| m
> terminal_width
- 10)
448 show_locus (l1
, c1
, -1);
449 show_locus (l2
, -1, c2
);
453 show_locus (l1
, c1
, c2
);
459 /* Workhorse for the error printing subroutines. This subroutine is
460 inspired by g77's error handling and is similar to printf() with
461 the following %-codes:
463 %c Character, %d or %i Integer, %s String, %% Percent
464 %L Takes locus argument
465 %C Current locus (no argument)
467 If a locus pointer is given, the actual source line is printed out
468 and the column is indicated. Since we want the error message at
469 the bottom of any source file information, we must scan the
470 argument list twice -- once to determine whether the loci are
471 present and record this for printing, and once to print the error
472 message after and loci have been printed. A maximum of two locus
473 arguments are permitted.
475 This function is also called (recursively) by show_locus in the
476 case of included files; however, as show_locus does not resupply
477 any loci, the recursion is at most one level deep. */
481 static void ATTRIBUTE_GCC_GFC(2,0)
482 error_print (const char *type
, const char *format0
, va_list argp
)
484 enum { TYPE_CURRENTLOC
, TYPE_LOCUS
, TYPE_INTEGER
, TYPE_UINTEGER
,
485 TYPE_LONGINT
, TYPE_ULONGINT
, TYPE_CHAR
, TYPE_STRING
,
494 unsigned int uintval
;
496 unsigned long int ulongintval
;
498 const char * stringval
;
500 } arg
[MAX_ARGS
], spec
[MAX_ARGS
];
501 /* spec is the array of specifiers, in the same order as they
502 appear in the format string. arg is the array of arguments,
503 in the same order as they appear in the va_list. */
506 int i
, n
, have_l1
, pos
, maxpos
;
507 locus
*l1
, *l2
, *loc
;
510 loc
= l1
= l2
= NULL
;
519 for (i
= 0; i
< MAX_ARGS
; i
++)
521 arg
[i
].type
= NOTYPE
;
525 /* First parse the format string for position specifiers. */
538 if (ISDIGIT (*format
))
540 /* This is a position specifier. For example, the number
541 12 in the format string "%12$d", which specifies the third
542 argument of the va_list, formatted in %d format.
543 For details, see "man 3 printf". */
544 pos
= atoi(format
) - 1;
545 gcc_assert (pos
>= 0);
546 while (ISDIGIT(*format
))
548 gcc_assert (*format
== '$');
562 arg
[pos
].type
= TYPE_CURRENTLOC
;
566 arg
[pos
].type
= TYPE_LOCUS
;
571 arg
[pos
].type
= TYPE_INTEGER
;
575 arg
[pos
].type
= TYPE_UINTEGER
;
581 arg
[pos
].type
= TYPE_ULONGINT
;
582 else if (c
== 'i' || c
== 'd')
583 arg
[pos
].type
= TYPE_LONGINT
;
589 arg
[pos
].type
= TYPE_CHAR
;
593 arg
[pos
].type
= TYPE_STRING
;
603 /* Then convert the values for each %-style argument. */
604 for (pos
= 0; pos
<= maxpos
; pos
++)
606 gcc_assert (arg
[pos
].type
!= NOTYPE
);
607 switch (arg
[pos
].type
)
609 case TYPE_CURRENTLOC
:
610 loc
= &gfc_current_locus
;
614 if (arg
[pos
].type
== TYPE_LOCUS
)
615 loc
= va_arg (argp
, locus
*);
620 arg
[pos
].u
.stringval
= "(2)";
621 /* Point %C first offending character not the last good one. */
622 if (arg
[pos
].type
== TYPE_CURRENTLOC
&& *l2
->nextc
!= '\0')
629 arg
[pos
].u
.stringval
= "(1)";
630 /* Point %C first offending character not the last good one. */
631 if (arg
[pos
].type
== TYPE_CURRENTLOC
&& *l1
->nextc
!= '\0')
637 arg
[pos
].u
.intval
= va_arg (argp
, int);
641 arg
[pos
].u
.uintval
= va_arg (argp
, unsigned int);
645 arg
[pos
].u
.longintval
= va_arg (argp
, long int);
649 arg
[pos
].u
.ulongintval
= va_arg (argp
, unsigned long int);
653 arg
[pos
].u
.charval
= (char) va_arg (argp
, int);
657 arg
[pos
].u
.stringval
= (const char *) va_arg (argp
, char *);
665 for (n
= 0; spec
[n
].pos
>= 0; n
++)
666 spec
[n
].u
= arg
[spec
[n
].pos
].u
;
668 /* Show the current loci if we have to. */
682 for (; *format
; format
++)
686 error_char (*format
);
691 if (ISDIGIT (*format
))
693 /* This is a position specifier. See comment above. */
694 while (ISDIGIT (*format
))
697 /* Skip over the dollar sign. */
708 error_char (spec
[n
++].u
.charval
);
712 case 'C': /* Current locus */
713 case 'L': /* Specified locus */
714 error_string (spec
[n
++].u
.stringval
);
719 error_integer (spec
[n
++].u
.intval
);
723 error_uinteger (spec
[n
++].u
.uintval
);
729 error_uinteger (spec
[n
++].u
.ulongintval
);
731 error_integer (spec
[n
++].u
.longintval
);
741 /* Wrapper for error_print(). */
744 error_printf (const char *gmsgid
, ...)
748 va_start (argp
, gmsgid
);
749 error_print ("", _(gmsgid
), argp
);
754 /* Clear any output buffered in a pretty-print output_buffer. */
757 gfc_clear_pp_buffer (output_buffer
*this_buffer
)
759 pretty_printer
*pp
= global_dc
->printer
;
760 output_buffer
*tmp_buffer
= pp
->buffer
;
761 pp
->buffer
= this_buffer
;
762 pp_clear_output_area (pp
);
763 pp
->buffer
= tmp_buffer
;
764 /* We need to reset last_location, otherwise we may skip caret lines
765 when we actually give a diagnostic. */
766 global_dc
->last_location
= UNKNOWN_LOCATION
;
769 /* The currently-printing diagnostic, for use by gfc_format_decoder,
770 for colorizing %C and %L. */
772 static diagnostic_info
*curr_diagnostic
;
774 /* A helper function to call diagnostic_report_diagnostic, while setting
775 curr_diagnostic for the duration of the call. */
778 gfc_report_diagnostic (diagnostic_info
*diagnostic
)
780 gcc_assert (diagnostic
!= NULL
);
781 curr_diagnostic
= diagnostic
;
782 bool ret
= diagnostic_report_diagnostic (global_dc
, diagnostic
);
783 curr_diagnostic
= NULL
;
787 /* This is just a helper function to avoid duplicating the logic of
791 gfc_warning (int opt
, const char *gmsgid
, va_list ap
)
796 diagnostic_info diagnostic
;
797 rich_location
rich_loc (line_table
, UNKNOWN_LOCATION
);
798 bool fatal_errors
= global_dc
->fatal_errors
;
799 pretty_printer
*pp
= global_dc
->printer
;
800 output_buffer
*tmp_buffer
= pp
->buffer
;
802 gfc_clear_pp_buffer (pp_warning_buffer
);
806 pp
->buffer
= pp_warning_buffer
;
807 global_dc
->fatal_errors
= false;
808 /* To prevent -fmax-errors= triggering. */
812 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, &rich_loc
,
814 diagnostic
.option_index
= opt
;
815 bool ret
= gfc_report_diagnostic (&diagnostic
);
819 pp
->buffer
= tmp_buffer
;
820 global_dc
->fatal_errors
= fatal_errors
;
822 warningcount_buffered
= 0;
823 werrorcount_buffered
= 0;
824 /* Undo the above --werrorcount if not Werror, otherwise
825 werrorcount is correct already. */
828 else if (diagnostic
.kind
== DK_ERROR
)
829 ++werrorcount_buffered
;
831 ++werrorcount
, --warningcount
, ++warningcount_buffered
;
838 /* Issue a warning. */
841 gfc_warning (int opt
, const char *gmsgid
, ...)
845 va_start (argp
, gmsgid
);
846 bool ret
= gfc_warning (opt
, gmsgid
, argp
);
852 /* Whether, for a feature included in a given standard set (GFC_STD_*),
853 we should issue an error or a warning, or be quiet. */
856 gfc_notification_std (int std
)
860 warning
= ((gfc_option
.warn_std
& std
) != 0) && !inhibit_warnings
;
861 if ((gfc_option
.allow_std
& std
) != 0 && !warning
)
864 return warning
? WARNING
: ERROR
;
868 /* Return a string describing the nature of a standard violation
869 * and/or the relevant version of the standard. */
872 notify_std_msg(int std
)
875 if (std
& GFC_STD_F2018_DEL
)
876 return _("Fortran 2018 deleted feature:");
877 else if (std
& GFC_STD_F2018_OBS
)
878 return _("Fortran 2018 obsolescent feature:");
879 else if (std
& GFC_STD_F2018
)
880 return _("Fortran 2018:");
881 else if (std
& GFC_STD_F2008_OBS
)
882 return _("Fortran 2008 obsolescent feature:");
883 else if (std
& GFC_STD_F2008
)
884 return "Fortran 2008:";
885 else if (std
& GFC_STD_F2003
)
886 return "Fortran 2003:";
887 else if (std
& GFC_STD_GNU
)
888 return _("GNU Extension:");
889 else if (std
& GFC_STD_LEGACY
)
890 return _("Legacy Extension:");
891 else if (std
& GFC_STD_F95_OBS
)
892 return _("Obsolescent feature:");
893 else if (std
& GFC_STD_F95_DEL
)
894 return _("Deleted feature:");
900 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
901 feature. An error/warning will be issued if the currently selected
902 standard does not contain the requested bits. Return false if
903 an error is generated. */
906 gfc_notify_std (int std
, const char *gmsgid
, ...)
909 const char *msg
, *msg2
;
912 /* Determine whether an error or a warning is needed. */
913 const int wstd
= std
& gfc_option
.warn_std
; /* Standard to warn about. */
914 const int estd
= std
& ~gfc_option
.allow_std
; /* Standard to error about. */
915 const bool warning
= (wstd
!= 0) && !inhibit_warnings
;
916 const bool error
= (estd
!= 0);
918 if (!error
&& !warning
)
924 msg
= notify_std_msg (estd
);
926 msg
= notify_std_msg (wstd
);
929 buffer
= (char *) alloca (strlen (msg
) + strlen (msg2
) + 2);
930 strcpy (buffer
, msg
);
931 strcat (buffer
, " ");
932 strcat (buffer
, msg2
);
934 va_start (argp
, gmsgid
);
936 gfc_error_opt (0, buffer
, argp
);
938 gfc_warning (0, buffer
, argp
);
944 return (warning
&& !warnings_are_errors
);
948 /* Called from output_format -- during diagnostic message processing
949 to handle Fortran specific format specifiers with the following meanings:
951 %C Current locus (no argument)
952 %L Takes locus argument
955 gfc_format_decoder (pretty_printer
*pp
, text_info
*text
, const char *spec
,
956 int precision
, bool wide
, bool set_locus
, bool hash
,
957 bool *quoted
, const char **buffer_ptr
)
964 static const char *result
[2] = { "(1)", "(2)" };
967 loc
= &gfc_current_locus
;
969 loc
= va_arg (*text
->args_ptr
, locus
*);
970 gcc_assert (loc
->nextc
- loc
->lb
->line
>= 0);
971 unsigned int offset
= loc
->nextc
- loc
->lb
->line
;
972 if (*spec
== 'C' && *loc
->nextc
!= '\0')
973 /* Point %C first offending character not the last good one. */
975 /* If location[0] != UNKNOWN_LOCATION means that we already
976 processed one of %C/%L. */
977 int loc_num
= text
->get_location (0) == UNKNOWN_LOCATION
? 0 : 1;
979 = linemap_position_for_loc_and_offset (line_table
,
982 text
->set_location (loc_num
, src_loc
, SHOW_RANGE_WITH_CARET
);
983 /* Colorize the markers to match the color choices of
984 diagnostic_show_locus (the initial location has a color given
985 by the "kind" of the diagnostic, the secondary location has
987 gcc_assert (curr_diagnostic
!= NULL
);
991 : diagnostic_get_color_for_kind (curr_diagnostic
->kind
));
992 pp_string (pp
, colorize_start (pp_show_color (pp
), color
));
993 pp_string (pp
, result
[loc_num
]);
994 pp_string (pp
, colorize_stop (pp_show_color (pp
)));
998 /* Fall through info the middle-end decoder, as e.g. stor-layout.c
999 etc. diagnostics can use the FE printer while the FE is still
1001 return default_tree_printer (pp
, text
, spec
, precision
, wide
,
1002 set_locus
, hash
, quoted
, buffer_ptr
);
1006 /* Return a malloc'd string describing the kind of diagnostic. The
1007 caller is responsible for freeing the memory. */
1009 gfc_diagnostic_build_kind_prefix (diagnostic_context
*context
,
1010 const diagnostic_info
*diagnostic
)
1012 static const char *const diagnostic_kind_text
[] = {
1013 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
1014 #include "gfc-diagnostic.def"
1015 #undef DEFINE_DIAGNOSTIC_KIND
1018 static const char *const diagnostic_kind_color
[] = {
1019 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
1020 #include "gfc-diagnostic.def"
1021 #undef DEFINE_DIAGNOSTIC_KIND
1024 gcc_assert (diagnostic
->kind
< DK_LAST_DIAGNOSTIC_KIND
);
1025 const char *text
= _(diagnostic_kind_text
[diagnostic
->kind
]);
1026 const char *text_cs
= "", *text_ce
= "";
1027 pretty_printer
*pp
= context
->printer
;
1029 if (diagnostic_kind_color
[diagnostic
->kind
])
1031 text_cs
= colorize_start (pp_show_color (pp
),
1032 diagnostic_kind_color
[diagnostic
->kind
]);
1033 text_ce
= colorize_stop (pp_show_color (pp
));
1035 return build_message_string ("%s%s:%s ", text_cs
, text
, text_ce
);
1038 /* Return a malloc'd string describing a location. The caller is
1039 responsible for freeing the memory. */
1041 gfc_diagnostic_build_locus_prefix (diagnostic_context
*context
,
1042 expanded_location s
)
1044 pretty_printer
*pp
= context
->printer
;
1045 const char *locus_cs
= colorize_start (pp_show_color (pp
), "locus");
1046 const char *locus_ce
= colorize_stop (pp_show_color (pp
));
1047 return (s
.file
== NULL
1048 ? build_message_string ("%s%s:%s", locus_cs
, progname
, locus_ce
)
1049 : !strcmp (s
.file
, N_("<built-in>"))
1050 ? build_message_string ("%s%s:%s", locus_cs
, s
.file
, locus_ce
)
1051 : context
->show_column
1052 ? build_message_string ("%s%s:%d:%d:%s", locus_cs
, s
.file
, s
.line
,
1054 : build_message_string ("%s%s:%d:%s", locus_cs
, s
.file
, s
.line
, locus_ce
));
1057 /* Return a malloc'd string describing two locations. The caller is
1058 responsible for freeing the memory. */
1060 gfc_diagnostic_build_locus_prefix (diagnostic_context
*context
,
1061 expanded_location s
, expanded_location s2
)
1063 pretty_printer
*pp
= context
->printer
;
1064 const char *locus_cs
= colorize_start (pp_show_color (pp
), "locus");
1065 const char *locus_ce
= colorize_stop (pp_show_color (pp
));
1067 return (s
.file
== NULL
1068 ? build_message_string ("%s%s:%s", locus_cs
, progname
, locus_ce
)
1069 : !strcmp (s
.file
, N_("<built-in>"))
1070 ? build_message_string ("%s%s:%s", locus_cs
, s
.file
, locus_ce
)
1071 : context
->show_column
1072 ? build_message_string ("%s%s:%d:%d-%d:%s", locus_cs
, s
.file
, s
.line
,
1073 MIN (s
.column
, s2
.column
),
1074 MAX (s
.column
, s2
.column
), locus_ce
)
1075 : build_message_string ("%s%s:%d:%s", locus_cs
, s
.file
, s
.line
,
1079 /* This function prints the locus (file:line:column), the diagnostic kind
1080 (Error, Warning) and (optionally) the relevant lines of code with
1081 annotation lines with '1' and/or '2' below them.
1083 With -fdiagnostic-show-caret (the default) it prints:
1085 [locus of primary range]:
1089 Error: Some error at (1)
1091 With -fno-diagnostic-show-caret or if the primary range is not
1094 [locus of primary range]: Error: Some error at (1) and (2)
1097 gfc_diagnostic_starter (diagnostic_context
*context
,
1098 diagnostic_info
*diagnostic
)
1100 char * kind_prefix
= gfc_diagnostic_build_kind_prefix (context
, diagnostic
);
1102 expanded_location s1
= diagnostic_expand_location (diagnostic
);
1103 expanded_location s2
;
1104 bool one_locus
= diagnostic
->richloc
->get_num_locations () < 2;
1105 bool same_locus
= false;
1109 s2
= diagnostic_expand_location (diagnostic
, 1);
1110 same_locus
= diagnostic_same_line (context
, s1
, s2
);
1113 char * locus_prefix
= (one_locus
|| !same_locus
)
1114 ? gfc_diagnostic_build_locus_prefix (context
, s1
)
1115 : gfc_diagnostic_build_locus_prefix (context
, s1
, s2
);
1117 if (!context
->show_caret
1118 || diagnostic_location (diagnostic
, 0) <= BUILTINS_LOCATION
1119 || diagnostic_location (diagnostic
, 0) == context
->last_location
)
1121 pp_set_prefix (context
->printer
,
1122 concat (locus_prefix
, " ", kind_prefix
, NULL
));
1123 free (locus_prefix
);
1125 if (one_locus
|| same_locus
)
1130 /* In this case, we print the previous locus and prefix as:
1132 [locus]:[prefix]: (1)
1134 and we flush with a new line before setting the new prefix. */
1135 pp_string (context
->printer
, "(1)");
1136 pp_newline (context
->printer
);
1137 locus_prefix
= gfc_diagnostic_build_locus_prefix (context
, s2
);
1138 pp_set_prefix (context
->printer
,
1139 concat (locus_prefix
, " ", kind_prefix
, NULL
));
1141 free (locus_prefix
);
1145 pp_verbatim (context
->printer
, "%s", locus_prefix
);
1146 free (locus_prefix
);
1147 /* Fortran uses an empty line between locus and caret line. */
1148 pp_newline (context
->printer
);
1149 pp_set_prefix (context
->printer
, NULL
);
1150 pp_newline (context
->printer
);
1151 diagnostic_show_locus (context
, diagnostic
->richloc
, diagnostic
->kind
);
1152 /* If the caret line was shown, the prefix does not contain the
1154 pp_set_prefix (context
->printer
, kind_prefix
);
1159 gfc_diagnostic_start_span (diagnostic_context
*context
,
1160 expanded_location exploc
)
1163 locus_prefix
= gfc_diagnostic_build_locus_prefix (context
, exploc
);
1164 pp_verbatim (context
->printer
, "%s", locus_prefix
);
1165 free (locus_prefix
);
1166 pp_newline (context
->printer
);
1167 /* Fortran uses an empty line between locus and caret line. */
1168 pp_newline (context
->printer
);
1173 gfc_diagnostic_finalizer (diagnostic_context
*context
,
1174 diagnostic_info
*diagnostic ATTRIBUTE_UNUSED
,
1175 diagnostic_t orig_diag_kind ATTRIBUTE_UNUSED
)
1177 pp_destroy_prefix (context
->printer
);
1178 pp_newline_and_flush (context
->printer
);
1181 /* Immediate warning (i.e. do not buffer the warning) with an explicit
1185 gfc_warning_now_at (location_t loc
, int opt
, const char *gmsgid
, ...)
1188 diagnostic_info diagnostic
;
1189 rich_location
rich_loc (line_table
, loc
);
1192 va_start (argp
, gmsgid
);
1193 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, &rich_loc
, DK_WARNING
);
1194 diagnostic
.option_index
= opt
;
1195 ret
= gfc_report_diagnostic (&diagnostic
);
1200 /* Immediate warning (i.e. do not buffer the warning). */
1203 gfc_warning_now (int opt
, const char *gmsgid
, ...)
1206 diagnostic_info diagnostic
;
1207 rich_location
rich_loc (line_table
, UNKNOWN_LOCATION
);
1210 va_start (argp
, gmsgid
);
1211 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, &rich_loc
,
1213 diagnostic
.option_index
= opt
;
1214 ret
= gfc_report_diagnostic (&diagnostic
);
1219 /* Internal warning, do not buffer. */
1222 gfc_warning_internal (int opt
, const char *gmsgid
, ...)
1225 diagnostic_info diagnostic
;
1226 rich_location
rich_loc (line_table
, UNKNOWN_LOCATION
);
1229 va_start (argp
, gmsgid
);
1230 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, &rich_loc
,
1232 diagnostic
.option_index
= opt
;
1233 ret
= gfc_report_diagnostic (&diagnostic
);
1238 /* Immediate error (i.e. do not buffer). */
1241 gfc_error_now (const char *gmsgid
, ...)
1244 diagnostic_info diagnostic
;
1245 rich_location
rich_loc (line_table
, UNKNOWN_LOCATION
);
1247 error_buffer
.flag
= true;
1249 va_start (argp
, gmsgid
);
1250 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, &rich_loc
, DK_ERROR
);
1251 gfc_report_diagnostic (&diagnostic
);
1256 /* Fatal error, never returns. */
1259 gfc_fatal_error (const char *gmsgid
, ...)
1262 diagnostic_info diagnostic
;
1263 rich_location
rich_loc (line_table
, UNKNOWN_LOCATION
);
1265 va_start (argp
, gmsgid
);
1266 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, &rich_loc
, DK_FATAL
);
1267 gfc_report_diagnostic (&diagnostic
);
1273 /* Clear the warning flag. */
1276 gfc_clear_warning (void)
1278 gfc_clear_pp_buffer (pp_warning_buffer
);
1279 warningcount_buffered
= 0;
1280 werrorcount_buffered
= 0;
1284 /* Check to see if any warnings have been saved.
1285 If so, print the warning. */
1288 gfc_warning_check (void)
1290 if (! gfc_output_buffer_empty_p (pp_warning_buffer
))
1292 pretty_printer
*pp
= global_dc
->printer
;
1293 output_buffer
*tmp_buffer
= pp
->buffer
;
1294 pp
->buffer
= pp_warning_buffer
;
1295 pp_really_flush (pp
);
1296 warningcount
+= warningcount_buffered
;
1297 werrorcount
+= werrorcount_buffered
;
1298 gcc_assert (warningcount_buffered
+ werrorcount_buffered
== 1);
1299 pp
->buffer
= tmp_buffer
;
1300 diagnostic_action_after_output (global_dc
,
1301 warningcount_buffered
1302 ? DK_WARNING
: DK_ERROR
);
1303 diagnostic_check_max_errors (global_dc
, true);
1308 /* Issue an error. */
1311 gfc_error_opt (int opt
, const char *gmsgid
, va_list ap
)
1315 bool saved_abort_on_error
= false;
1317 if (warnings_not_errors
)
1319 gfc_warning (opt
, gmsgid
, argp
);
1324 if (suppress_errors
)
1330 diagnostic_info diagnostic
;
1331 rich_location
richloc (line_table
, UNKNOWN_LOCATION
);
1332 bool fatal_errors
= global_dc
->fatal_errors
;
1333 pretty_printer
*pp
= global_dc
->printer
;
1334 output_buffer
*tmp_buffer
= pp
->buffer
;
1336 gfc_clear_pp_buffer (pp_error_buffer
);
1340 /* To prevent -dH from triggering an abort on a buffered error,
1341 save abort_on_error and restore it below. */
1342 saved_abort_on_error
= global_dc
->abort_on_error
;
1343 global_dc
->abort_on_error
= false;
1344 pp
->buffer
= pp_error_buffer
;
1345 global_dc
->fatal_errors
= false;
1346 /* To prevent -fmax-errors= triggering, we decrease it before
1347 report_diagnostic increases it. */
1351 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, &richloc
, DK_ERROR
);
1352 gfc_report_diagnostic (&diagnostic
);
1356 pp
->buffer
= tmp_buffer
;
1357 global_dc
->fatal_errors
= fatal_errors
;
1358 global_dc
->abort_on_error
= saved_abort_on_error
;
1367 gfc_error_opt (int opt
, const char *gmsgid
, ...)
1370 va_start (argp
, gmsgid
);
1371 gfc_error_opt (opt
, gmsgid
, argp
);
1377 gfc_error (const char *gmsgid
, ...)
1380 va_start (argp
, gmsgid
);
1381 gfc_error_opt (0, gmsgid
, argp
);
1386 /* This shouldn't happen... but sometimes does. */
1389 gfc_internal_error (const char *gmsgid
, ...)
1393 diagnostic_info diagnostic
;
1394 rich_location
rich_loc (line_table
, UNKNOWN_LOCATION
);
1396 gfc_get_errors (&w
, &e
);
1400 va_start (argp
, gmsgid
);
1401 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, &rich_loc
, DK_ICE
);
1402 gfc_report_diagnostic (&diagnostic
);
1409 /* Clear the error flag when we start to compile a source line. */
1412 gfc_clear_error (void)
1414 error_buffer
.flag
= false;
1415 warnings_not_errors
= false;
1416 gfc_clear_pp_buffer (pp_error_buffer
);
1420 /* Tests the state of error_flag. */
1423 gfc_error_flag_test (void)
1425 return error_buffer
.flag
1426 || !gfc_output_buffer_empty_p (pp_error_buffer
);
1430 /* Check to see if any errors have been saved.
1431 If so, print the error. Returns the state of error_flag. */
1434 gfc_error_check (void)
1436 if (error_buffer
.flag
1437 || ! gfc_output_buffer_empty_p (pp_error_buffer
))
1439 error_buffer
.flag
= false;
1440 pretty_printer
*pp
= global_dc
->printer
;
1441 output_buffer
*tmp_buffer
= pp
->buffer
;
1442 pp
->buffer
= pp_error_buffer
;
1443 pp_really_flush (pp
);
1445 gcc_assert (gfc_output_buffer_empty_p (pp_error_buffer
));
1446 pp
->buffer
= tmp_buffer
;
1447 diagnostic_action_after_output (global_dc
, DK_ERROR
);
1448 diagnostic_check_max_errors (global_dc
, true);
1455 /* Move the text buffered from FROM to TO, then clear
1456 FROM. Independently if there was text in FROM, TO is also
1460 gfc_move_error_buffer_from_to (gfc_error_buffer
* buffer_from
,
1461 gfc_error_buffer
* buffer_to
)
1463 output_buffer
* from
= &(buffer_from
->buffer
);
1464 output_buffer
* to
= &(buffer_to
->buffer
);
1466 buffer_to
->flag
= buffer_from
->flag
;
1467 buffer_from
->flag
= false;
1469 gfc_clear_pp_buffer (to
);
1470 /* We make sure this is always buffered. */
1471 to
->flush_p
= false;
1473 if (! gfc_output_buffer_empty_p (from
))
1475 const char *str
= output_buffer_formatted_text (from
);
1476 output_buffer_append_r (to
, str
, strlen (str
));
1477 gfc_clear_pp_buffer (from
);
1481 /* Save the existing error state. */
1484 gfc_push_error (gfc_error_buffer
*err
)
1486 gfc_move_error_buffer_from_to (&error_buffer
, err
);
1490 /* Restore a previous pushed error state. */
1493 gfc_pop_error (gfc_error_buffer
*err
)
1495 gfc_move_error_buffer_from_to (err
, &error_buffer
);
1499 /* Free a pushed error state, but keep the current error state. */
1502 gfc_free_error (gfc_error_buffer
*err
)
1504 gfc_clear_pp_buffer (&(err
->buffer
));
1508 /* Report the number of warnings and errors that occurred to the caller. */
1511 gfc_get_errors (int *w
, int *e
)
1514 *w
= warningcount
+ werrorcount
;
1516 *e
= errorcount
+ sorrycount
+ werrorcount
;
1520 /* Switch errors into warnings. */
1523 gfc_errors_to_warnings (bool f
)
1525 warnings_not_errors
= f
;
1529 gfc_diagnostics_init (void)
1531 diagnostic_starter (global_dc
) = gfc_diagnostic_starter
;
1532 global_dc
->start_span
= gfc_diagnostic_start_span
;
1533 diagnostic_finalizer (global_dc
) = gfc_diagnostic_finalizer
;
1534 diagnostic_format_decoder (global_dc
) = gfc_format_decoder
;
1535 global_dc
->caret_chars
[0] = '1';
1536 global_dc
->caret_chars
[1] = '2';
1537 pp_warning_buffer
= new (XNEW (output_buffer
)) output_buffer ();
1538 pp_warning_buffer
->flush_p
= false;
1539 /* pp_error_buffer is statically allocated. This simplifies memory
1540 management when using gfc_push/pop_error. */
1541 pp_error_buffer
= &(error_buffer
.buffer
);
1542 pp_error_buffer
->flush_p
= false;
1546 gfc_diagnostics_finish (void)
1548 tree_diagnostics_defaults (global_dc
);
1549 /* We still want to use the gfc starter and finalizer, not the tree
1551 diagnostic_starter (global_dc
) = gfc_diagnostic_starter
;
1552 diagnostic_finalizer (global_dc
) = gfc_diagnostic_finalizer
;
1553 global_dc
->caret_chars
[0] = '^';
1554 global_dc
->caret_chars
[1] = '^';