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 /* This is just a helper function to avoid duplicating the logic of
814 gfc_warning (int opt
, const char *gmsgid
, va_list ap
)
819 diagnostic_info diagnostic
;
820 bool fatal_errors
= global_dc
->fatal_errors
;
821 pretty_printer
*pp
= global_dc
->printer
;
822 output_buffer
*tmp_buffer
= pp
->buffer
;
824 gfc_clear_pp_buffer (pp_warning_buffer
);
828 pp
->buffer
= pp_warning_buffer
;
829 global_dc
->fatal_errors
= false;
830 /* To prevent -fmax-errors= triggering. */
834 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, UNKNOWN_LOCATION
,
836 diagnostic
.option_index
= opt
;
837 bool ret
= report_diagnostic (&diagnostic
);
841 pp
->buffer
= tmp_buffer
;
842 global_dc
->fatal_errors
= fatal_errors
;
844 warningcount_buffered
= 0;
845 werrorcount_buffered
= 0;
846 /* Undo the above --werrorcount if not Werror, otherwise
847 werrorcount is correct already. */
850 else if (diagnostic
.kind
== DK_ERROR
)
851 ++werrorcount_buffered
;
853 ++werrorcount
, --warningcount
, ++warningcount_buffered
;
860 /* Issue a warning. */
863 gfc_warning (int opt
, const char *gmsgid
, ...)
867 va_start (argp
, gmsgid
);
868 bool ret
= gfc_warning (opt
, gmsgid
, argp
);
874 /* Whether, for a feature included in a given standard set (GFC_STD_*),
875 we should issue an error or a warning, or be quiet. */
878 gfc_notification_std (int std
)
882 warning
= ((gfc_option
.warn_std
& std
) != 0) && !inhibit_warnings
;
883 if ((gfc_option
.allow_std
& std
) != 0 && !warning
)
886 return warning
? WARNING
: ERROR
;
890 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
891 feature. An error/warning will be issued if the currently selected
892 standard does not contain the requested bits. Return false if
893 an error is generated. */
896 gfc_notify_std (int std
, const char *gmsgid
, ...)
900 const char *msg
, *msg2
;
903 warning
= ((gfc_option
.warn_std
& std
) != 0) && !inhibit_warnings
;
904 if ((gfc_option
.allow_std
& std
) != 0 && !warning
)
908 return warning
? true : false;
912 case GFC_STD_F2008_TS
:
913 msg
= "TS 29113/TS 18508:";
915 case GFC_STD_F2008_OBS
:
916 msg
= _("Fortran 2008 obsolescent feature:");
919 msg
= "Fortran 2008:";
922 msg
= "Fortran 2003:";
925 msg
= _("GNU Extension:");
928 msg
= _("Legacy Extension:");
930 case GFC_STD_F95_OBS
:
931 msg
= _("Obsolescent feature:");
933 case GFC_STD_F95_DEL
:
934 msg
= _("Deleted feature:");
941 buffer
= (char *) alloca (strlen (msg
) + strlen (msg2
) + 2);
942 strcpy (buffer
, msg
);
943 strcat (buffer
, " ");
944 strcat (buffer
, msg2
);
946 va_start (argp
, gmsgid
);
948 gfc_warning (0, buffer
, argp
);
950 gfc_error (buffer
, argp
);
953 return (warning
&& !warnings_are_errors
) ? true : false;
957 /* Called from output_format -- during diagnostic message processing
958 to handle Fortran specific format specifiers with the following meanings:
960 %C Current locus (no argument)
961 %L Takes locus argument
964 gfc_format_decoder (pretty_printer
*pp
,
965 text_info
*text
, const char *spec
,
966 int precision ATTRIBUTE_UNUSED
, bool wide ATTRIBUTE_UNUSED
,
967 bool plus ATTRIBUTE_UNUSED
, bool hash ATTRIBUTE_UNUSED
)
974 static const char *result
[2] = { "(1)", "(2)" };
977 loc
= &gfc_current_locus
;
979 loc
= va_arg (*text
->args_ptr
, locus
*);
980 gcc_assert (loc
->nextc
- loc
->lb
->line
>= 0);
981 unsigned int offset
= loc
->nextc
- loc
->lb
->line
;
982 /* If location[0] != UNKNOWN_LOCATION means that we already
983 processed one of %C/%L. */
984 int loc_num
= text
->get_location (0) == UNKNOWN_LOCATION
? 0 : 1;
985 text
->set_location (loc_num
,
986 linemap_position_for_loc_and_offset (line_table
,
989 pp_string (pp
, result
[loc_num
]);
997 /* Return a malloc'd string describing the kind of diagnostic. The
998 caller is responsible for freeing the memory. */
1000 gfc_diagnostic_build_kind_prefix (diagnostic_context
*context
,
1001 const diagnostic_info
*diagnostic
)
1003 static const char *const diagnostic_kind_text
[] = {
1004 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
1005 #include "gfc-diagnostic.def"
1006 #undef DEFINE_DIAGNOSTIC_KIND
1009 static const char *const diagnostic_kind_color
[] = {
1010 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
1011 #include "gfc-diagnostic.def"
1012 #undef DEFINE_DIAGNOSTIC_KIND
1015 gcc_assert (diagnostic
->kind
< DK_LAST_DIAGNOSTIC_KIND
);
1016 const char *text
= _(diagnostic_kind_text
[diagnostic
->kind
]);
1017 const char *text_cs
= "", *text_ce
= "";
1018 pretty_printer
*pp
= context
->printer
;
1020 if (diagnostic_kind_color
[diagnostic
->kind
])
1022 text_cs
= colorize_start (pp_show_color (pp
),
1023 diagnostic_kind_color
[diagnostic
->kind
]);
1024 text_ce
= colorize_stop (pp_show_color (pp
));
1026 return build_message_string ("%s%s:%s ", text_cs
, text
, text_ce
);
1029 /* Return a malloc'd string describing a location. The caller is
1030 responsible for freeing the memory. */
1032 gfc_diagnostic_build_locus_prefix (diagnostic_context
*context
,
1033 expanded_location s
)
1035 pretty_printer
*pp
= context
->printer
;
1036 const char *locus_cs
= colorize_start (pp_show_color (pp
), "locus");
1037 const char *locus_ce
= colorize_stop (pp_show_color (pp
));
1038 return (s
.file
== NULL
1039 ? build_message_string ("%s%s:%s", locus_cs
, progname
, locus_ce
)
1040 : !strcmp (s
.file
, N_("<built-in>"))
1041 ? build_message_string ("%s%s:%s", locus_cs
, s
.file
, locus_ce
)
1042 : context
->show_column
1043 ? build_message_string ("%s%s:%d:%d:%s", locus_cs
, s
.file
, s
.line
,
1045 : build_message_string ("%s%s:%d:%s", locus_cs
, s
.file
, s
.line
, locus_ce
));
1048 /* Return a malloc'd string describing two locations. The caller is
1049 responsible for freeing the memory. */
1051 gfc_diagnostic_build_locus_prefix (diagnostic_context
*context
,
1052 expanded_location s
, expanded_location s2
)
1054 pretty_printer
*pp
= context
->printer
;
1055 const char *locus_cs
= colorize_start (pp_show_color (pp
), "locus");
1056 const char *locus_ce
= colorize_stop (pp_show_color (pp
));
1058 return (s
.file
== NULL
1059 ? build_message_string ("%s%s:%s", locus_cs
, progname
, locus_ce
)
1060 : !strcmp (s
.file
, N_("<built-in>"))
1061 ? build_message_string ("%s%s:%s", locus_cs
, s
.file
, locus_ce
)
1062 : context
->show_column
1063 ? build_message_string ("%s%s:%d:%d-%d:%s", locus_cs
, s
.file
, s
.line
,
1064 MIN (s
.column
, s2
.column
),
1065 MAX (s
.column
, s2
.column
), locus_ce
)
1066 : build_message_string ("%s%s:%d:%s", locus_cs
, s
.file
, s
.line
,
1070 /* This function prints the locus (file:line:column), the diagnostic kind
1071 (Error, Warning) and (optionally) the caret line (a source line
1072 with '1' and/or '2' below it).
1074 With -fdiagnostic-show-caret (the default) and for valid locations,
1075 it prints for one location:
1081 Error: Some error at (1)
1083 for two locations that fit in the same locus line:
1087 some code and some more code
1089 Error: Some error at (1) and (2)
1091 and for two locations that do not fit in the same locus line:
1101 Error: Some error at (1) and (2)
1103 With -fno-diagnostic-show-caret or if one of the locations is not
1104 valid, it prints for one location (or for two locations that fit in
1105 the same locus line):
1107 [locus]: Error: Some error at (1) and (2)
1109 and for two locations that do not fit in the same locus line:
1111 [name]:[locus]: Error: (1)
1112 [name]:[locus2]: Error: Some error at (1) and (2)
1115 gfc_diagnostic_starter (diagnostic_context
*context
,
1116 diagnostic_info
*diagnostic
)
1118 char * kind_prefix
= gfc_diagnostic_build_kind_prefix (context
, diagnostic
);
1120 expanded_location s1
= diagnostic_expand_location (diagnostic
);
1121 expanded_location s2
;
1122 bool one_locus
= diagnostic_location (diagnostic
, 1) == UNKNOWN_LOCATION
;
1123 bool same_locus
= false;
1127 s2
= diagnostic_expand_location (diagnostic
, 1);
1128 same_locus
= diagnostic_same_line (context
, s1
, s2
);
1131 char * locus_prefix
= (one_locus
|| !same_locus
)
1132 ? gfc_diagnostic_build_locus_prefix (context
, s1
)
1133 : gfc_diagnostic_build_locus_prefix (context
, s1
, s2
);
1135 if (!context
->show_caret
1136 || diagnostic_location (diagnostic
, 0) <= BUILTINS_LOCATION
1137 || diagnostic_location (diagnostic
, 0) == context
->last_location
)
1139 pp_set_prefix (context
->printer
,
1140 concat (locus_prefix
, " ", kind_prefix
, NULL
));
1141 free (locus_prefix
);
1143 if (one_locus
|| same_locus
)
1148 /* In this case, we print the previous locus and prefix as:
1150 [locus]:[prefix]: (1)
1152 and we flush with a new line before setting the new prefix. */
1153 pp_string (context
->printer
, "(1)");
1154 pp_newline (context
->printer
);
1155 locus_prefix
= gfc_diagnostic_build_locus_prefix (context
, s2
);
1156 pp_set_prefix (context
->printer
,
1157 concat (locus_prefix
, " ", kind_prefix
, NULL
));
1159 free (locus_prefix
);
1163 pp_verbatim (context
->printer
, locus_prefix
);
1164 free (locus_prefix
);
1165 /* Fortran uses an empty line between locus and caret line. */
1166 pp_newline (context
->printer
);
1167 diagnostic_show_locus (context
, diagnostic
);
1168 pp_newline (context
->printer
);
1169 /* If the caret line was shown, the prefix does not contain the
1171 pp_set_prefix (context
->printer
, kind_prefix
);
1173 if (one_locus
|| same_locus
)
1176 locus_prefix
= gfc_diagnostic_build_locus_prefix (context
, s2
);
1177 if (diagnostic_location (diagnostic
, 1) <= BUILTINS_LOCATION
)
1179 /* No caret line for the second location. Override the previous
1180 prefix with [locus2]:[prefix]. */
1181 pp_set_prefix (context
->printer
,
1182 concat (locus_prefix
, " ", kind_prefix
, NULL
));
1184 free (locus_prefix
);
1188 /* We print the caret for the second location. */
1189 pp_verbatim (context
->printer
, locus_prefix
);
1190 free (locus_prefix
);
1191 /* Fortran uses an empty line between locus and caret line. */
1192 pp_newline (context
->printer
);
1193 s1
.column
= 0; /* Print only a caret line for s2. */
1194 diagnostic_print_caret_line (context
, s2
, s1
,
1195 context
->caret_chars
[1], '\0');
1196 pp_newline (context
->printer
);
1197 /* If the caret line was shown, the prefix does not contain the
1199 pp_set_prefix (context
->printer
, kind_prefix
);
1205 gfc_diagnostic_finalizer (diagnostic_context
*context
,
1206 diagnostic_info
*diagnostic ATTRIBUTE_UNUSED
)
1208 pp_destroy_prefix (context
->printer
);
1209 pp_newline_and_flush (context
->printer
);
1212 /* Immediate warning (i.e. do not buffer the warning) with an explicit
1216 gfc_warning_now_at (location_t loc
, int opt
, const char *gmsgid
, ...)
1219 diagnostic_info diagnostic
;
1222 va_start (argp
, gmsgid
);
1223 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, loc
, DK_WARNING
);
1224 diagnostic
.option_index
= opt
;
1225 ret
= report_diagnostic (&diagnostic
);
1230 /* Immediate warning (i.e. do not buffer the warning). */
1233 gfc_warning_now (int opt
, const char *gmsgid
, ...)
1236 diagnostic_info diagnostic
;
1239 va_start (argp
, gmsgid
);
1240 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, UNKNOWN_LOCATION
,
1242 diagnostic
.option_index
= opt
;
1243 ret
= report_diagnostic (&diagnostic
);
1249 /* Immediate error (i.e. do not buffer). */
1250 /* This function uses the common diagnostics, but does not support
1251 two locations; when being used in scanner.c, ensure that the location
1252 is properly setup. Otherwise, use gfc_error_now_1. */
1255 gfc_error_now (const char *gmsgid
, ...)
1258 diagnostic_info diagnostic
;
1260 va_start (argp
, gmsgid
);
1261 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, UNKNOWN_LOCATION
, DK_ERROR
);
1262 report_diagnostic (&diagnostic
);
1267 /* Fatal error, never returns. */
1270 gfc_fatal_error (const char *gmsgid
, ...)
1273 diagnostic_info diagnostic
;
1275 va_start (argp
, gmsgid
);
1276 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, UNKNOWN_LOCATION
, DK_FATAL
);
1277 report_diagnostic (&diagnostic
);
1283 /* Clear the warning flag. */
1286 gfc_clear_warning (void)
1288 warning_buffer
.flag
= 0;
1290 gfc_clear_pp_buffer (pp_warning_buffer
);
1291 warningcount_buffered
= 0;
1292 werrorcount_buffered
= 0;
1296 /* Check to see if any warnings have been saved.
1297 If so, print the warning. */
1300 gfc_warning_check (void)
1302 if (warning_buffer
.flag
)
1305 if (warning_buffer
.message
!= NULL
)
1306 fputs (warning_buffer
.message
, stderr
);
1307 gfc_clear_warning ();
1309 /* This is for the new diagnostics machinery. */
1310 else if (! gfc_output_buffer_empty_p (pp_warning_buffer
))
1312 pretty_printer
*pp
= global_dc
->printer
;
1313 output_buffer
*tmp_buffer
= pp
->buffer
;
1314 pp
->buffer
= pp_warning_buffer
;
1315 pp_really_flush (pp
);
1316 warningcount
+= warningcount_buffered
;
1317 werrorcount
+= werrorcount_buffered
;
1318 gcc_assert (warningcount_buffered
+ werrorcount_buffered
== 1);
1319 diagnostic_action_after_output (global_dc
,
1320 warningcount_buffered
1321 ? DK_WARNING
: DK_ERROR
);
1322 pp
->buffer
= tmp_buffer
;
1327 /* Issue an error. */
1328 /* Use gfc_error instead, unless two locations are used in the same
1329 warning or for scanner.c, if the location is not properly set up. */
1332 gfc_error_1 (const char *gmsgid
, ...)
1336 if (warnings_not_errors
)
1339 if (suppress_errors
)
1342 error_buffer
.flag
= 1;
1343 error_buffer
.index
= 0;
1344 cur_error_buffer
= &error_buffer
;
1346 va_start (argp
, gmsgid
);
1347 error_print (_("Error:"), _(gmsgid
), argp
);
1353 gfc_increment_error_count();
1359 if (inhibit_warnings
)
1362 warning_buffer
.flag
= 1;
1363 warning_buffer
.index
= 0;
1364 cur_error_buffer
= &warning_buffer
;
1366 va_start (argp
, gmsgid
);
1367 error_print (_("Warning:"), _(gmsgid
), argp
);
1375 if (warnings_are_errors
)
1376 gfc_increment_error_count();
1380 /* Issue an error. */
1381 /* This function uses the common diagnostics, but does not support
1382 two locations; when being used in scanner.c, ensure that the location
1383 is properly setup. Otherwise, use gfc_error_1. */
1386 gfc_error (const char *gmsgid
, va_list ap
)
1391 if (warnings_not_errors
)
1393 gfc_warning (/*opt=*/0, gmsgid
, argp
);
1398 if (suppress_errors
)
1404 diagnostic_info diagnostic
;
1405 bool fatal_errors
= global_dc
->fatal_errors
;
1406 pretty_printer
*pp
= global_dc
->printer
;
1407 output_buffer
*tmp_buffer
= pp
->buffer
;
1409 gfc_clear_pp_buffer (pp_error_buffer
);
1413 pp
->buffer
= pp_error_buffer
;
1414 global_dc
->fatal_errors
= false;
1415 /* To prevent -fmax-errors= triggering, we decrease it before
1416 report_diagnostic increases it. */
1420 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, UNKNOWN_LOCATION
, DK_ERROR
);
1421 report_diagnostic (&diagnostic
);
1425 pp
->buffer
= tmp_buffer
;
1426 global_dc
->fatal_errors
= fatal_errors
;
1434 gfc_error (const char *gmsgid
, ...)
1437 va_start (argp
, gmsgid
);
1438 gfc_error (gmsgid
, argp
);
1443 /* Immediate error. */
1444 /* Use gfc_error_now instead, unless two locations are used in the same
1445 warning or for scanner.c, if the location is not properly set up. */
1448 gfc_error_now_1 (const char *gmsgid
, ...)
1451 bool buffered_p_saved
;
1453 error_buffer
.flag
= 1;
1454 error_buffer
.index
= 0;
1455 cur_error_buffer
= &error_buffer
;
1457 buffered_p_saved
= buffered_p
;
1460 va_start (argp
, gmsgid
);
1461 error_print (_("Error:"), _(gmsgid
), argp
);
1466 gfc_increment_error_count();
1468 buffered_p
= buffered_p_saved
;
1470 if (flag_fatal_errors
)
1471 exit (FATAL_EXIT_CODE
);
1475 /* This shouldn't happen... but sometimes does. */
1478 gfc_internal_error (const char *gmsgid
, ...)
1481 diagnostic_info diagnostic
;
1483 va_start (argp
, gmsgid
);
1484 diagnostic_set_info (&diagnostic
, gmsgid
, &argp
, UNKNOWN_LOCATION
, DK_ICE
);
1485 report_diagnostic (&diagnostic
);
1492 /* Clear the error flag when we start to compile a source line. */
1495 gfc_clear_error (void)
1497 error_buffer
.flag
= 0;
1498 warnings_not_errors
= false;
1499 gfc_clear_pp_buffer (pp_error_buffer
);
1503 /* Tests the state of error_flag. */
1506 gfc_error_flag_test (void)
1508 return error_buffer
.flag
1509 || !gfc_output_buffer_empty_p (pp_error_buffer
);
1513 /* Check to see if any errors have been saved.
1514 If so, print the error. Returns the state of error_flag. */
1517 gfc_error_check (void)
1519 bool error_raised
= (bool) error_buffer
.flag
;
1523 if (error_buffer
.message
!= NULL
)
1524 fputs (error_buffer
.message
, stderr
);
1525 error_buffer
.flag
= 0;
1526 gfc_clear_pp_buffer (pp_error_buffer
);
1528 gfc_increment_error_count();
1530 if (flag_fatal_errors
)
1531 exit (FATAL_EXIT_CODE
);
1533 /* This is for the new diagnostics machinery. */
1534 else if (! gfc_output_buffer_empty_p (pp_error_buffer
))
1536 error_raised
= true;
1537 pretty_printer
*pp
= global_dc
->printer
;
1538 output_buffer
*tmp_buffer
= pp
->buffer
;
1539 pp
->buffer
= pp_error_buffer
;
1540 pp_really_flush (pp
);
1542 gcc_assert (gfc_output_buffer_empty_p (pp_error_buffer
));
1543 diagnostic_action_after_output (global_dc
, DK_ERROR
);
1544 pp
->buffer
= tmp_buffer
;
1547 return error_raised
;
1550 /* Move the text buffered from FROM to TO, then clear
1551 FROM. Independently if there was text in FROM, TO is also
1555 gfc_move_output_buffer_from_to (output_buffer
*from
, output_buffer
*to
)
1557 gfc_clear_pp_buffer (to
);
1558 /* We make sure this is always buffered. */
1559 to
->flush_p
= false;
1561 if (! gfc_output_buffer_empty_p (from
))
1563 const char *str
= output_buffer_formatted_text (from
);
1564 output_buffer_append_r (to
, str
, strlen (str
));
1565 gfc_clear_pp_buffer (from
);
1569 /* Save the existing error state. */
1572 gfc_push_error (output_buffer
*buffer_err
, gfc_error_buf
*err
)
1574 err
->flag
= error_buffer
.flag
;
1575 if (error_buffer
.flag
)
1576 err
->message
= xstrdup (error_buffer
.message
);
1578 error_buffer
.flag
= 0;
1580 /* This part uses the common diagnostics. */
1581 gfc_move_output_buffer_from_to (pp_error_buffer
, buffer_err
);
1585 /* Restore a previous pushed error state. */
1588 gfc_pop_error (output_buffer
*buffer_err
, gfc_error_buf
*err
)
1590 error_buffer
.flag
= err
->flag
;
1591 if (error_buffer
.flag
)
1593 size_t len
= strlen (err
->message
) + 1;
1594 gcc_assert (len
<= error_buffer
.allocated
);
1595 memcpy (error_buffer
.message
, err
->message
, len
);
1596 free (err
->message
);
1598 /* This part uses the common diagnostics. */
1599 gfc_move_output_buffer_from_to (buffer_err
, pp_error_buffer
);
1603 /* Free a pushed error state, but keep the current error state. */
1606 gfc_free_error (output_buffer
*buffer_err
, gfc_error_buf
*err
)
1609 free (err
->message
);
1611 gfc_clear_pp_buffer (buffer_err
);
1615 /* Report the number of warnings and errors that occurred to the caller. */
1618 gfc_get_errors (int *w
, int *e
)
1621 *w
= warnings
+ warningcount
+ werrorcount
;
1623 *e
= errors
+ errorcount
+ sorrycount
+ werrorcount
;
1627 /* Switch errors into warnings. */
1630 gfc_errors_to_warnings (bool f
)
1632 warnings_not_errors
= f
;
1636 gfc_diagnostics_init (void)
1638 diagnostic_starter (global_dc
) = gfc_diagnostic_starter
;
1639 diagnostic_finalizer (global_dc
) = gfc_diagnostic_finalizer
;
1640 diagnostic_format_decoder (global_dc
) = gfc_format_decoder
;
1641 global_dc
->caret_chars
[0] = '1';
1642 global_dc
->caret_chars
[1] = '2';
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_chars
[0] = '^';
1658 global_dc
->caret_chars
[1] = '^';