| 1 |
/* Handle errors. |
| 2 |
Copyright (C) 2000-2025 Free Software Foundation, Inc. |
| 3 |
Contributed by Andy Vaught & Niels Kristian Bech Jensen |
| 4 |
|
| 5 |
This file is part of GCC. |
| 6 |
|
| 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 |
| 10 |
version. |
| 11 |
|
| 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 |
| 15 |
for more details. |
| 16 |
|
| 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/>. */ |
| 20 |
|
| 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. */ |
| 26 |
|
| 27 |
#include "config.h" |
| 28 |
#include "system.h" |
| 29 |
#include "coretypes.h" |
| 30 |
#include "options.h" |
| 31 |
#include "gfortran.h" |
| 32 |
|
| 33 |
#include "diagnostic.h" |
| 34 |
#include "diagnostic-color.h" |
| 35 |
#include "tree-diagnostic.h" /* tree_diagnostics_defaults */ |
| 36 |
#include "diagnostic-format-text.h" |
| 37 |
|
| 38 |
static int suppress_errors = 0; |
| 39 |
|
| 40 |
static bool warnings_not_errors = false; |
| 41 |
|
| 42 |
/* True if the error/warnings should be buffered. */ |
| 43 |
static bool buffered_p; |
| 44 |
|
| 45 |
static gfc_error_buffer *error_buffer; |
| 46 |
static diagnostic_buffer *pp_error_buffer, *pp_warning_buffer; |
| 47 |
|
| 48 |
gfc_error_buffer::gfc_error_buffer () |
| 49 |
: flag (false), buffer (*global_dc) |
| 50 |
{ |
| 51 |
} |
| 52 |
|
| 53 |
/* Return a location_t suitable for 'tree' for a gfortran locus. During |
| 54 |
parsing in gfortran, loc->u.lb->location contains only the line number |
| 55 |
and LOCATION_COLUMN is 0; hence, the column has to be added when generating |
| 56 |
locations for 'tree'. If available, return location_t directly, which |
| 57 |
might be a range. */ |
| 58 |
|
| 59 |
location_t |
| 60 |
gfc_get_location_with_offset (locus *loc, unsigned offset) |
| 61 |
{ |
| 62 |
if (loc->nextc == (gfc_char_t *) -1) |
| 63 |
{ |
| 64 |
gcc_checking_assert (offset == 0); |
| 65 |
return loc->u.location; |
| 66 |
} |
| 67 |
gcc_checking_assert (loc->nextc >= loc->u.lb->line); |
| 68 |
return linemap_position_for_loc_and_offset (line_table, loc->u.lb->location, |
| 69 |
loc->nextc - loc->u.lb->line |
| 70 |
+ offset); |
| 71 |
} |
| 72 |
|
| 73 |
/* Convert a locus to a range. */ |
| 74 |
|
| 75 |
locus |
| 76 |
gfc_get_location_range (locus *caret_loc, unsigned caret_offset, |
| 77 |
locus *start_loc, unsigned start_offset, |
| 78 |
locus *end_loc) |
| 79 |
{ |
| 80 |
location_t caret; |
| 81 |
location_t start = gfc_get_location_with_offset (start_loc, start_offset); |
| 82 |
location_t end = gfc_get_location_with_offset (end_loc, 0); |
| 83 |
|
| 84 |
if (caret_loc) |
| 85 |
caret = gfc_get_location_with_offset (caret_loc, caret_offset); |
| 86 |
|
| 87 |
locus range; |
| 88 |
range.nextc = (gfc_char_t *) -1; |
| 89 |
range.u.location = make_location (caret_loc ? caret : start, start, end); |
| 90 |
return range; |
| 91 |
} |
| 92 |
|
| 93 |
/* Return buffered_p. */ |
| 94 |
bool |
| 95 |
gfc_buffered_p (void) |
| 96 |
{ |
| 97 |
return buffered_p; |
| 98 |
} |
| 99 |
|
| 100 |
/* Go one level deeper suppressing errors. */ |
| 101 |
|
| 102 |
void |
| 103 |
gfc_push_suppress_errors (void) |
| 104 |
{ |
| 105 |
gcc_assert (suppress_errors >= 0); |
| 106 |
++suppress_errors; |
| 107 |
} |
| 108 |
|
| 109 |
static void |
| 110 |
gfc_error_opt (int opt, const char *gmsgid, va_list ap) ATTRIBUTE_GCC_GFC(2,0); |
| 111 |
|
| 112 |
static bool |
| 113 |
gfc_warning (int opt, const char *gmsgid, va_list ap) ATTRIBUTE_GCC_GFC(2,0); |
| 114 |
|
| 115 |
|
| 116 |
/* Leave one level of error suppressing. */ |
| 117 |
|
| 118 |
void |
| 119 |
gfc_pop_suppress_errors (void) |
| 120 |
{ |
| 121 |
gcc_assert (suppress_errors > 0); |
| 122 |
--suppress_errors; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
/* Query whether errors are suppressed. */ |
| 127 |
|
| 128 |
bool |
| 129 |
gfc_query_suppress_errors (void) |
| 130 |
{ |
| 131 |
return suppress_errors > 0; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
/* Per-file error initialization. */ |
| 136 |
|
| 137 |
void |
| 138 |
gfc_error_init_1 (void) |
| 139 |
{ |
| 140 |
gfc_buffer_error (false); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
/* Set the flag for buffering errors or not. */ |
| 145 |
|
| 146 |
void |
| 147 |
gfc_buffer_error (bool flag) |
| 148 |
{ |
| 149 |
buffered_p = flag; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
static int |
| 154 |
print_wide_char_into_buffer (gfc_char_t c, char *buf) |
| 155 |
{ |
| 156 |
static const char xdigit[16] = { '0', '1', '2', '3', '4', '5', '6', |
| 157 |
'7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; |
| 158 |
|
| 159 |
if (gfc_wide_is_printable (c) || c == '\t') |
| 160 |
{ |
| 161 |
buf[1] = '\0'; |
| 162 |
/* Tabulation is output as a space. */ |
| 163 |
buf[0] = (unsigned char) (c == '\t' ? ' ' : c); |
| 164 |
return 1; |
| 165 |
} |
| 166 |
else if (c < ((gfc_char_t) 1 << 8)) |
| 167 |
{ |
| 168 |
buf[4] = '\0'; |
| 169 |
buf[3] = xdigit[c & 0x0F]; |
| 170 |
c = c >> 4; |
| 171 |
buf[2] = xdigit[c & 0x0F]; |
| 172 |
|
| 173 |
buf[1] = 'x'; |
| 174 |
buf[0] = '\\'; |
| 175 |
return 4; |
| 176 |
} |
| 177 |
else if (c < ((gfc_char_t) 1 << 16)) |
| 178 |
{ |
| 179 |
buf[6] = '\0'; |
| 180 |
buf[5] = xdigit[c & 0x0F]; |
| 181 |
c = c >> 4; |
| 182 |
buf[4] = xdigit[c & 0x0F]; |
| 183 |
c = c >> 4; |
| 184 |
buf[3] = xdigit[c & 0x0F]; |
| 185 |
c = c >> 4; |
| 186 |
buf[2] = xdigit[c & 0x0F]; |
| 187 |
|
| 188 |
buf[1] = 'u'; |
| 189 |
buf[0] = '\\'; |
| 190 |
return 6; |
| 191 |
} |
| 192 |
else |
| 193 |
{ |
| 194 |
buf[10] = '\0'; |
| 195 |
buf[9] = xdigit[c & 0x0F]; |
| 196 |
c = c >> 4; |
| 197 |
buf[8] = xdigit[c & 0x0F]; |
| 198 |
c = c >> 4; |
| 199 |
buf[7] = xdigit[c & 0x0F]; |
| 200 |
c = c >> 4; |
| 201 |
buf[6] = xdigit[c & 0x0F]; |
| 202 |
c = c >> 4; |
| 203 |
buf[5] = xdigit[c & 0x0F]; |
| 204 |
c = c >> 4; |
| 205 |
buf[4] = xdigit[c & 0x0F]; |
| 206 |
c = c >> 4; |
| 207 |
buf[3] = xdigit[c & 0x0F]; |
| 208 |
c = c >> 4; |
| 209 |
buf[2] = xdigit[c & 0x0F]; |
| 210 |
|
| 211 |
buf[1] = 'U'; |
| 212 |
buf[0] = '\\'; |
| 213 |
return 10; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
static char wide_char_print_buffer[11]; |
| 218 |
|
| 219 |
const char * |
| 220 |
gfc_print_wide_char (gfc_char_t c) |
| 221 |
{ |
| 222 |
print_wide_char_into_buffer (c, wide_char_print_buffer); |
| 223 |
return wide_char_print_buffer; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
/* Clear any output buffered in THIS_BUFFER without issuing |
| 228 |
it to global_dc. */ |
| 229 |
|
| 230 |
static void |
| 231 |
gfc_clear_diagnostic_buffer (diagnostic_buffer *this_buffer) |
| 232 |
{ |
| 233 |
gcc_assert (this_buffer); |
| 234 |
global_dc->clear_diagnostic_buffer (*this_buffer); |
| 235 |
} |
| 236 |
|
| 237 |
/* The currently-printing diagnostic, for use by gfc_format_decoder, |
| 238 |
for colorizing %C and %L. */ |
| 239 |
|
| 240 |
static diagnostic_info *curr_diagnostic; |
| 241 |
|
| 242 |
/* A helper function to call diagnostic_report_diagnostic, while setting |
| 243 |
curr_diagnostic for the duration of the call. */ |
| 244 |
|
| 245 |
static bool |
| 246 |
gfc_report_diagnostic (diagnostic_info *diagnostic) |
| 247 |
{ |
| 248 |
gcc_assert (diagnostic != NULL); |
| 249 |
curr_diagnostic = diagnostic; |
| 250 |
bool ret = diagnostic_report_diagnostic (global_dc, diagnostic); |
| 251 |
curr_diagnostic = NULL; |
| 252 |
return ret; |
| 253 |
} |
| 254 |
|
| 255 |
/* This is just a helper function to avoid duplicating the logic of |
| 256 |
gfc_warning. */ |
| 257 |
|
| 258 |
static bool |
| 259 |
gfc_warning (int opt, const char *gmsgid, va_list ap) |
| 260 |
{ |
| 261 |
va_list argp; |
| 262 |
va_copy (argp, ap); |
| 263 |
|
| 264 |
diagnostic_info diagnostic; |
| 265 |
rich_location rich_loc (line_table, UNKNOWN_LOCATION); |
| 266 |
diagnostic_buffer *old_buffer = global_dc->get_diagnostic_buffer (); |
| 267 |
gcc_assert (!old_buffer); |
| 268 |
|
| 269 |
gfc_clear_diagnostic_buffer (pp_warning_buffer); |
| 270 |
|
| 271 |
if (buffered_p) |
| 272 |
global_dc->set_diagnostic_buffer (pp_warning_buffer); |
| 273 |
|
| 274 |
diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc, |
| 275 |
DK_WARNING); |
| 276 |
diagnostic.option_id = opt; |
| 277 |
bool ret = gfc_report_diagnostic (&diagnostic); |
| 278 |
|
| 279 |
if (buffered_p) |
| 280 |
global_dc->set_diagnostic_buffer (old_buffer); |
| 281 |
|
| 282 |
va_end (argp); |
| 283 |
return ret; |
| 284 |
} |
| 285 |
|
| 286 |
/* Issue a warning. */ |
| 287 |
|
| 288 |
bool |
| 289 |
gfc_warning (int opt, const char *gmsgid, ...) |
| 290 |
{ |
| 291 |
va_list argp; |
| 292 |
|
| 293 |
va_start (argp, gmsgid); |
| 294 |
bool ret = gfc_warning (opt, gmsgid, argp); |
| 295 |
va_end (argp); |
| 296 |
return ret; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
/* Whether, for a feature included in a given standard set (GFC_STD_*), |
| 301 |
we should issue an error or a warning, or be quiet. */ |
| 302 |
|
| 303 |
notification |
| 304 |
gfc_notification_std (int std) |
| 305 |
{ |
| 306 |
bool warning; |
| 307 |
|
| 308 |
warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings; |
| 309 |
if ((gfc_option.allow_std & std) != 0 && !warning) |
| 310 |
return SILENT; |
| 311 |
|
| 312 |
return warning ? WARNING : ERROR; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
/* Return a string describing the nature of a standard violation |
| 317 |
* and/or the relevant version of the standard. */ |
| 318 |
|
| 319 |
char const* |
| 320 |
notify_std_msg(int std) |
| 321 |
{ |
| 322 |
|
| 323 |
if (std & GFC_STD_F2023_DEL) |
| 324 |
return _("Prohibited in Fortran 2023:"); |
| 325 |
else if (std & GFC_STD_F2023) |
| 326 |
return _("Fortran 2023:"); |
| 327 |
else if (std & GFC_STD_F2018_DEL) |
| 328 |
return _("Fortran 2018 deleted feature:"); |
| 329 |
else if (std & GFC_STD_F2018_OBS) |
| 330 |
return _("Fortran 2018 obsolescent feature:"); |
| 331 |
else if (std & GFC_STD_F2018) |
| 332 |
return _("Fortran 2018:"); |
| 333 |
else if (std & GFC_STD_F2008_OBS) |
| 334 |
return _("Fortran 2008 obsolescent feature:"); |
| 335 |
else if (std & GFC_STD_F2008) |
| 336 |
return "Fortran 2008:"; |
| 337 |
else if (std & GFC_STD_F2003) |
| 338 |
return "Fortran 2003:"; |
| 339 |
else if (std & GFC_STD_GNU) |
| 340 |
return _("GNU Extension:"); |
| 341 |
else if (std & GFC_STD_LEGACY) |
| 342 |
return _("Legacy Extension:"); |
| 343 |
else if (std & GFC_STD_F95_OBS) |
| 344 |
return _("Obsolescent feature:"); |
| 345 |
else if (std & GFC_STD_F95_DEL) |
| 346 |
return _("Deleted feature:"); |
| 347 |
else if (std & GFC_STD_UNSIGNED) |
| 348 |
return _("Unsigned:"); |
| 349 |
else |
| 350 |
gcc_unreachable (); |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
/* Possibly issue a warning/error about use of a nonstandard (or deleted) |
| 355 |
feature. An error/warning will be issued if the currently selected |
| 356 |
standard does not contain the requested bits. Return false if |
| 357 |
an error is generated. */ |
| 358 |
|
| 359 |
bool |
| 360 |
gfc_notify_std (int std, const char *gmsgid, ...) |
| 361 |
{ |
| 362 |
va_list argp; |
| 363 |
const char *msg, *msg2; |
| 364 |
char *buffer; |
| 365 |
|
| 366 |
/* Determine whether an error or a warning is needed. */ |
| 367 |
const int wstd = std & gfc_option.warn_std; /* Standard to warn about. */ |
| 368 |
const int estd = std & ~gfc_option.allow_std; /* Standard to error about. */ |
| 369 |
const bool warning = (wstd != 0) && !inhibit_warnings; |
| 370 |
const bool error = (estd != 0); |
| 371 |
|
| 372 |
if (!error && !warning) |
| 373 |
return true; |
| 374 |
if (suppress_errors) |
| 375 |
return !error; |
| 376 |
|
| 377 |
if (error) |
| 378 |
msg = notify_std_msg (estd); |
| 379 |
else |
| 380 |
msg = notify_std_msg (wstd); |
| 381 |
|
| 382 |
msg2 = _(gmsgid); |
| 383 |
buffer = (char *) alloca (strlen (msg) + strlen (msg2) + 2); |
| 384 |
strcpy (buffer, msg); |
| 385 |
strcat (buffer, " "); |
| 386 |
strcat (buffer, msg2); |
| 387 |
|
| 388 |
va_start (argp, gmsgid); |
| 389 |
if (error) |
| 390 |
gfc_error_opt (0, buffer, argp); |
| 391 |
else |
| 392 |
gfc_warning (0, buffer, argp); |
| 393 |
va_end (argp); |
| 394 |
|
| 395 |
if (error) |
| 396 |
return false; |
| 397 |
else |
| 398 |
return (warning && !warnings_are_errors); |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
/* Called from output_format -- during diagnostic message processing |
| 403 |
to handle Fortran specific format specifiers with the following meanings: |
| 404 |
|
| 405 |
%C Current locus (no argument) |
| 406 |
%L Takes locus argument |
| 407 |
*/ |
| 408 |
static bool |
| 409 |
gfc_format_decoder (pretty_printer *pp, text_info *text, const char *spec, |
| 410 |
int precision, bool wide, bool set_locus, bool hash, |
| 411 |
bool *quoted, pp_token_list &formatted_token_list) |
| 412 |
{ |
| 413 |
unsigned offset = 0; |
| 414 |
switch (*spec) |
| 415 |
{ |
| 416 |
case 'C': |
| 417 |
case 'L': |
| 418 |
{ |
| 419 |
static const char *result[2] = { "(1)", "(2)" }; |
| 420 |
locus *loc; |
| 421 |
if (*spec == 'C') |
| 422 |
{ |
| 423 |
loc = &gfc_current_locus; |
| 424 |
/* Point %C first offending character not the last good one. */ |
| 425 |
if (*loc->nextc != '\0') |
| 426 |
offset++; |
| 427 |
} |
| 428 |
else |
| 429 |
loc = va_arg (*text->m_args_ptr, locus *); |
| 430 |
|
| 431 |
/* If location[0] != UNKNOWN_LOCATION means that we already |
| 432 |
processed one of %C/%L. */ |
| 433 |
int loc_num = text->get_location (0) == UNKNOWN_LOCATION ? 0 : 1; |
| 434 |
location_t src_loc = gfc_get_location_with_offset (loc, offset); |
| 435 |
text->set_location (loc_num, src_loc, SHOW_RANGE_WITH_CARET); |
| 436 |
/* Colorize the markers to match the color choices of |
| 437 |
diagnostic_show_locus (the initial location has a color given |
| 438 |
by the "kind" of the diagnostic, the secondary location has |
| 439 |
color "range1"). */ |
| 440 |
gcc_assert (curr_diagnostic != NULL); |
| 441 |
const char *color |
| 442 |
= (loc_num |
| 443 |
? "range1" |
| 444 |
: diagnostic_get_color_for_kind (curr_diagnostic->kind)); |
| 445 |
pp_string (pp, colorize_start (pp_show_color (pp), color)); |
| 446 |
pp_string (pp, result[loc_num]); |
| 447 |
pp_string (pp, colorize_stop (pp_show_color (pp))); |
| 448 |
return true; |
| 449 |
} |
| 450 |
default: |
| 451 |
/* Fall through info the middle-end decoder, as e.g. stor-layout.cc |
| 452 |
etc. diagnostics can use the FE printer while the FE is still |
| 453 |
active. */ |
| 454 |
return default_tree_printer (pp, text, spec, precision, wide, |
| 455 |
set_locus, hash, quoted, |
| 456 |
formatted_token_list); |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
/* Return a malloc'd string describing the kind of diagnostic. The |
| 461 |
caller is responsible for freeing the memory. */ |
| 462 |
static char * |
| 463 |
gfc_diagnostic_build_kind_prefix (diagnostic_context *context, |
| 464 |
const diagnostic_info *diagnostic) |
| 465 |
{ |
| 466 |
static const char *const diagnostic_kind_text[] = { |
| 467 |
#define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T), |
| 468 |
#include "gfc-diagnostic.def" |
| 469 |
#undef DEFINE_DIAGNOSTIC_KIND |
| 470 |
"must-not-happen" |
| 471 |
}; |
| 472 |
static const char *const diagnostic_kind_color[] = { |
| 473 |
#define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C), |
| 474 |
#include "gfc-diagnostic.def" |
| 475 |
#undef DEFINE_DIAGNOSTIC_KIND |
| 476 |
NULL |
| 477 |
}; |
| 478 |
gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND); |
| 479 |
const char *text = _(diagnostic_kind_text[diagnostic->kind]); |
| 480 |
const char *text_cs = "", *text_ce = ""; |
| 481 |
pretty_printer *const pp = context->get_reference_printer (); |
| 482 |
|
| 483 |
if (diagnostic_kind_color[diagnostic->kind]) |
| 484 |
{ |
| 485 |
text_cs = colorize_start (pp_show_color (pp), |
| 486 |
diagnostic_kind_color[diagnostic->kind]); |
| 487 |
text_ce = colorize_stop (pp_show_color (pp)); |
| 488 |
} |
| 489 |
return build_message_string ("%s%s:%s ", text_cs, text, text_ce); |
| 490 |
} |
| 491 |
|
| 492 |
/* Return a malloc'd string describing a location. The caller is |
| 493 |
responsible for freeing the memory. */ |
| 494 |
static char * |
| 495 |
gfc_diagnostic_build_locus_prefix (const diagnostic_location_print_policy &loc_policy, |
| 496 |
expanded_location s, |
| 497 |
bool colorize) |
| 498 |
{ |
| 499 |
const char *locus_cs = colorize_start (colorize, "locus"); |
| 500 |
const char *locus_ce = colorize_stop (colorize); |
| 501 |
return (s.file == NULL |
| 502 |
? build_message_string ("%s%s:%s", locus_cs, progname, locus_ce ) |
| 503 |
: !strcmp (s.file, special_fname_builtin ()) |
| 504 |
? build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce) |
| 505 |
: loc_policy.show_column_p () |
| 506 |
? build_message_string ("%s%s:%d:%d:%s", locus_cs, s.file, s.line, |
| 507 |
s.column, locus_ce) |
| 508 |
: build_message_string ("%s%s:%d:%s", locus_cs, s.file, s.line, locus_ce)); |
| 509 |
} |
| 510 |
|
| 511 |
/* Return a malloc'd string describing two locations. The caller is |
| 512 |
responsible for freeing the memory. */ |
| 513 |
static char * |
| 514 |
gfc_diagnostic_build_locus_prefix (const diagnostic_location_print_policy &loc_policy, |
| 515 |
expanded_location s, expanded_location s2, |
| 516 |
bool colorize) |
| 517 |
{ |
| 518 |
const char *locus_cs = colorize_start (colorize, "locus"); |
| 519 |
const char *locus_ce = colorize_stop (colorize); |
| 520 |
|
| 521 |
return (s.file == NULL |
| 522 |
? build_message_string ("%s%s:%s", locus_cs, progname, locus_ce ) |
| 523 |
: !strcmp (s.file, special_fname_builtin ()) |
| 524 |
? build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce) |
| 525 |
: loc_policy.show_column_p () |
| 526 |
? build_message_string ("%s%s:%d:%d-%d:%s", locus_cs, s.file, s.line, |
| 527 |
MIN (s.column, s2.column), |
| 528 |
MAX (s.column, s2.column), locus_ce) |
| 529 |
: build_message_string ("%s%s:%d:%s", locus_cs, s.file, s.line, |
| 530 |
locus_ce)); |
| 531 |
} |
| 532 |
|
| 533 |
/* This function prints the locus (file:line:column), the diagnostic kind |
| 534 |
(Error, Warning) and (optionally) the relevant lines of code with |
| 535 |
annotation lines with '1' and/or '2' below them. |
| 536 |
|
| 537 |
With -fdiagnostic-show-caret (the default) it prints: |
| 538 |
|
| 539 |
[locus of primary range]: |
| 540 |
|
| 541 |
some code |
| 542 |
1 |
| 543 |
Error: Some error at (1) |
| 544 |
|
| 545 |
With -fno-diagnostic-show-caret or if the primary range is not |
| 546 |
valid, it prints: |
| 547 |
|
| 548 |
[locus of primary range]: Error: Some error at (1) and (2) |
| 549 |
*/ |
| 550 |
static void |
| 551 |
gfc_diagnostic_text_starter (diagnostic_text_output_format &text_output, |
| 552 |
const diagnostic_info *diagnostic) |
| 553 |
{ |
| 554 |
diagnostic_context *const context = &text_output.get_context (); |
| 555 |
pretty_printer *const pp = text_output.get_printer (); |
| 556 |
char * kind_prefix = gfc_diagnostic_build_kind_prefix (context, diagnostic); |
| 557 |
|
| 558 |
expanded_location s1 = diagnostic_expand_location (diagnostic); |
| 559 |
expanded_location s2; |
| 560 |
bool one_locus = diagnostic->richloc->get_num_locations () < 2; |
| 561 |
bool same_locus = false; |
| 562 |
|
| 563 |
if (!one_locus) |
| 564 |
{ |
| 565 |
s2 = diagnostic_expand_location (diagnostic, 1); |
| 566 |
same_locus = diagnostic_same_line (context, s1, s2); |
| 567 |
} |
| 568 |
|
| 569 |
diagnostic_location_print_policy loc_policy (text_output); |
| 570 |
const bool colorize = pp_show_color (pp); |
| 571 |
char * locus_prefix = (one_locus || !same_locus) |
| 572 |
? gfc_diagnostic_build_locus_prefix (loc_policy, s1, colorize) |
| 573 |
: gfc_diagnostic_build_locus_prefix (loc_policy, s1, s2, colorize); |
| 574 |
|
| 575 |
if (!context->m_source_printing.enabled |
| 576 |
|| diagnostic_location (diagnostic, 0) <= BUILTINS_LOCATION |
| 577 |
|| diagnostic_location (diagnostic, 0) == context->m_last_location) |
| 578 |
{ |
| 579 |
pp_set_prefix (pp, |
| 580 |
concat (locus_prefix, " ", kind_prefix, NULL)); |
| 581 |
free (locus_prefix); |
| 582 |
|
| 583 |
if (one_locus || same_locus) |
| 584 |
{ |
| 585 |
free (kind_prefix); |
| 586 |
return; |
| 587 |
} |
| 588 |
/* In this case, we print the previous locus and prefix as: |
| 589 |
|
| 590 |
[locus]:[prefix]: (1) |
| 591 |
|
| 592 |
and we flush with a new line before setting the new prefix. */ |
| 593 |
pp_string (pp, "(1)"); |
| 594 |
pp_newline (pp); |
| 595 |
locus_prefix = gfc_diagnostic_build_locus_prefix (loc_policy, s2, colorize); |
| 596 |
pp_set_prefix (pp, |
| 597 |
concat (locus_prefix, " ", kind_prefix, NULL)); |
| 598 |
free (kind_prefix); |
| 599 |
free (locus_prefix); |
| 600 |
} |
| 601 |
else |
| 602 |
{ |
| 603 |
pp_verbatim (pp, "%s", locus_prefix); |
| 604 |
free (locus_prefix); |
| 605 |
/* Fortran uses an empty line between locus and caret line. */ |
| 606 |
pp_newline (pp); |
| 607 |
pp_set_prefix (pp, NULL); |
| 608 |
pp_newline (pp); |
| 609 |
diagnostic_show_locus (context, |
| 610 |
text_output.get_source_printing_options (), |
| 611 |
diagnostic->richloc, diagnostic->kind, |
| 612 |
pp); |
| 613 |
/* If the caret line was shown, the prefix does not contain the |
| 614 |
locus. */ |
| 615 |
pp_set_prefix (pp, kind_prefix); |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
static void |
| 620 |
gfc_diagnostic_start_span (const diagnostic_location_print_policy &loc_policy, |
| 621 |
pretty_printer *pp, |
| 622 |
expanded_location exploc) |
| 623 |
{ |
| 624 |
const bool colorize = pp_show_color (pp); |
| 625 |
char *locus_prefix |
| 626 |
= gfc_diagnostic_build_locus_prefix (loc_policy, exploc, colorize); |
| 627 |
pp_verbatim (pp, "%s", locus_prefix); |
| 628 |
free (locus_prefix); |
| 629 |
pp_newline (pp); |
| 630 |
/* Fortran uses an empty line between locus and caret line. */ |
| 631 |
pp_newline (pp); |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
static void |
| 636 |
gfc_diagnostic_text_finalizer (diagnostic_text_output_format &text_output, |
| 637 |
const diagnostic_info *diagnostic ATTRIBUTE_UNUSED, |
| 638 |
diagnostic_t orig_diag_kind ATTRIBUTE_UNUSED) |
| 639 |
{ |
| 640 |
pretty_printer *const pp = text_output.get_printer (); |
| 641 |
pp_destroy_prefix (pp); |
| 642 |
pp_newline_and_flush (pp); |
| 643 |
} |
| 644 |
|
| 645 |
/* Immediate warning (i.e. do not buffer the warning) with an explicit |
| 646 |
location. */ |
| 647 |
|
| 648 |
bool |
| 649 |
gfc_warning_now_at (location_t loc, int opt, const char *gmsgid, ...) |
| 650 |
{ |
| 651 |
va_list argp; |
| 652 |
diagnostic_info diagnostic; |
| 653 |
rich_location rich_loc (line_table, loc); |
| 654 |
bool ret; |
| 655 |
|
| 656 |
va_start (argp, gmsgid); |
| 657 |
diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc, DK_WARNING); |
| 658 |
diagnostic.option_id = opt; |
| 659 |
ret = gfc_report_diagnostic (&diagnostic); |
| 660 |
va_end (argp); |
| 661 |
return ret; |
| 662 |
} |
| 663 |
|
| 664 |
/* Immediate warning (i.e. do not buffer the warning). */ |
| 665 |
|
| 666 |
bool |
| 667 |
gfc_warning_now (int opt, const char *gmsgid, ...) |
| 668 |
{ |
| 669 |
va_list argp; |
| 670 |
diagnostic_info diagnostic; |
| 671 |
rich_location rich_loc (line_table, UNKNOWN_LOCATION); |
| 672 |
bool ret; |
| 673 |
|
| 674 |
va_start (argp, gmsgid); |
| 675 |
diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc, |
| 676 |
DK_WARNING); |
| 677 |
diagnostic.option_id = opt; |
| 678 |
ret = gfc_report_diagnostic (&diagnostic); |
| 679 |
va_end (argp); |
| 680 |
return ret; |
| 681 |
} |
| 682 |
|
| 683 |
/* Internal warning, do not buffer. */ |
| 684 |
|
| 685 |
bool |
| 686 |
gfc_warning_internal (int opt, const char *gmsgid, ...) |
| 687 |
{ |
| 688 |
va_list argp; |
| 689 |
diagnostic_info diagnostic; |
| 690 |
rich_location rich_loc (line_table, UNKNOWN_LOCATION); |
| 691 |
bool ret; |
| 692 |
|
| 693 |
va_start (argp, gmsgid); |
| 694 |
diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc, |
| 695 |
DK_WARNING); |
| 696 |
diagnostic.option_id = opt; |
| 697 |
ret = gfc_report_diagnostic (&diagnostic); |
| 698 |
va_end (argp); |
| 699 |
return ret; |
| 700 |
} |
| 701 |
|
| 702 |
/* Immediate error (i.e. do not buffer). */ |
| 703 |
|
| 704 |
void |
| 705 |
gfc_error_now (const char *gmsgid, ...) |
| 706 |
{ |
| 707 |
va_list argp; |
| 708 |
diagnostic_info diagnostic; |
| 709 |
rich_location rich_loc (line_table, UNKNOWN_LOCATION); |
| 710 |
|
| 711 |
error_buffer->flag = true; |
| 712 |
|
| 713 |
va_start (argp, gmsgid); |
| 714 |
diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc, DK_ERROR); |
| 715 |
gfc_report_diagnostic (&diagnostic); |
| 716 |
va_end (argp); |
| 717 |
} |
| 718 |
|
| 719 |
|
| 720 |
/* Fatal error, never returns. */ |
| 721 |
|
| 722 |
void |
| 723 |
gfc_fatal_error (const char *gmsgid, ...) |
| 724 |
{ |
| 725 |
va_list argp; |
| 726 |
diagnostic_info diagnostic; |
| 727 |
rich_location rich_loc (line_table, UNKNOWN_LOCATION); |
| 728 |
|
| 729 |
va_start (argp, gmsgid); |
| 730 |
diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc, DK_FATAL); |
| 731 |
gfc_report_diagnostic (&diagnostic); |
| 732 |
va_end (argp); |
| 733 |
|
| 734 |
gcc_unreachable (); |
| 735 |
} |
| 736 |
|
| 737 |
/* Clear the warning flag. */ |
| 738 |
|
| 739 |
void |
| 740 |
gfc_clear_warning (void) |
| 741 |
{ |
| 742 |
gfc_clear_diagnostic_buffer (pp_warning_buffer); |
| 743 |
} |
| 744 |
|
| 745 |
|
| 746 |
/* Check to see if any warnings have been saved. |
| 747 |
If so, print the warning. */ |
| 748 |
|
| 749 |
void |
| 750 |
gfc_warning_check (void) |
| 751 |
{ |
| 752 |
if (! pp_warning_buffer->empty_p ()) |
| 753 |
global_dc->flush_diagnostic_buffer (*pp_warning_buffer); |
| 754 |
} |
| 755 |
|
| 756 |
|
| 757 |
/* Issue an error. */ |
| 758 |
|
| 759 |
static void |
| 760 |
gfc_error_opt (int opt, const char *gmsgid, va_list ap) |
| 761 |
{ |
| 762 |
va_list argp; |
| 763 |
va_copy (argp, ap); |
| 764 |
|
| 765 |
if (warnings_not_errors) |
| 766 |
{ |
| 767 |
gfc_warning (opt, gmsgid, argp); |
| 768 |
va_end (argp); |
| 769 |
return; |
| 770 |
} |
| 771 |
|
| 772 |
if (suppress_errors) |
| 773 |
{ |
| 774 |
va_end (argp); |
| 775 |
return; |
| 776 |
} |
| 777 |
|
| 778 |
diagnostic_info diagnostic; |
| 779 |
rich_location richloc (line_table, UNKNOWN_LOCATION); |
| 780 |
diagnostic_buffer *old_buffer = global_dc->get_diagnostic_buffer (); |
| 781 |
gcc_assert (!old_buffer); |
| 782 |
|
| 783 |
gfc_clear_diagnostic_buffer (pp_error_buffer); |
| 784 |
|
| 785 |
if (buffered_p) |
| 786 |
global_dc->set_diagnostic_buffer (pp_error_buffer); |
| 787 |
|
| 788 |
diagnostic_set_info (&diagnostic, gmsgid, &argp, &richloc, DK_ERROR); |
| 789 |
gfc_report_diagnostic (&diagnostic); |
| 790 |
|
| 791 |
if (buffered_p) |
| 792 |
global_dc->set_diagnostic_buffer (old_buffer); |
| 793 |
|
| 794 |
va_end (argp); |
| 795 |
} |
| 796 |
|
| 797 |
|
| 798 |
void |
| 799 |
gfc_error_opt (int opt, const char *gmsgid, ...) |
| 800 |
{ |
| 801 |
va_list argp; |
| 802 |
va_start (argp, gmsgid); |
| 803 |
gfc_error_opt (opt, gmsgid, argp); |
| 804 |
va_end (argp); |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
void |
| 809 |
gfc_error (const char *gmsgid, ...) |
| 810 |
{ |
| 811 |
va_list argp; |
| 812 |
va_start (argp, gmsgid); |
| 813 |
gfc_error_opt (0, gmsgid, argp); |
| 814 |
va_end (argp); |
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
/* This shouldn't happen... but sometimes does. */ |
| 819 |
|
| 820 |
void |
| 821 |
gfc_internal_error (const char *gmsgid, ...) |
| 822 |
{ |
| 823 |
int e, w; |
| 824 |
va_list argp; |
| 825 |
diagnostic_info diagnostic; |
| 826 |
rich_location rich_loc (line_table, UNKNOWN_LOCATION); |
| 827 |
|
| 828 |
gfc_get_errors (&w, &e); |
| 829 |
if (e > 0) |
| 830 |
exit(EXIT_FAILURE); |
| 831 |
|
| 832 |
va_start (argp, gmsgid); |
| 833 |
diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc, DK_ICE); |
| 834 |
gfc_report_diagnostic (&diagnostic); |
| 835 |
va_end (argp); |
| 836 |
|
| 837 |
gcc_unreachable (); |
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
/* Clear the error flag when we start to compile a source line. */ |
| 842 |
|
| 843 |
void |
| 844 |
gfc_clear_error (void) |
| 845 |
{ |
| 846 |
error_buffer->flag = false; |
| 847 |
warnings_not_errors = false; |
| 848 |
gfc_clear_diagnostic_buffer (pp_error_buffer); |
| 849 |
} |
| 850 |
|
| 851 |
|
| 852 |
/* Tests the state of error_flag. */ |
| 853 |
|
| 854 |
bool |
| 855 |
gfc_error_flag_test (void) |
| 856 |
{ |
| 857 |
return (error_buffer->flag |
| 858 |
|| !pp_error_buffer->empty_p ()); |
| 859 |
} |
| 860 |
|
| 861 |
|
| 862 |
/* Check to see if any errors have been saved. |
| 863 |
If so, print the error. Returns the state of error_flag. */ |
| 864 |
|
| 865 |
bool |
| 866 |
gfc_error_check (void) |
| 867 |
{ |
| 868 |
if (error_buffer->flag |
| 869 |
|| ! pp_error_buffer->empty_p ()) |
| 870 |
{ |
| 871 |
error_buffer->flag = false; |
| 872 |
global_dc->flush_diagnostic_buffer (*pp_error_buffer); |
| 873 |
return true; |
| 874 |
} |
| 875 |
|
| 876 |
return false; |
| 877 |
} |
| 878 |
|
| 879 |
/* Move the text buffered from FROM to TO, then clear |
| 880 |
FROM. Independently if there was text in FROM, TO is also |
| 881 |
cleared. */ |
| 882 |
|
| 883 |
static void |
| 884 |
gfc_move_error_buffer_from_to (gfc_error_buffer * buffer_from, |
| 885 |
gfc_error_buffer * buffer_to) |
| 886 |
{ |
| 887 |
diagnostic_buffer * from = &(buffer_from->buffer); |
| 888 |
diagnostic_buffer * to = &(buffer_to->buffer); |
| 889 |
|
| 890 |
buffer_to->flag = buffer_from->flag; |
| 891 |
buffer_from->flag = false; |
| 892 |
|
| 893 |
gfc_clear_diagnostic_buffer (to); |
| 894 |
|
| 895 |
if (! from->empty_p ()) |
| 896 |
{ |
| 897 |
from->move_to (*to); |
| 898 |
gfc_clear_diagnostic_buffer (from); |
| 899 |
} |
| 900 |
} |
| 901 |
|
| 902 |
/* Save the existing error state. */ |
| 903 |
|
| 904 |
void |
| 905 |
gfc_push_error (gfc_error_buffer *err) |
| 906 |
{ |
| 907 |
gfc_move_error_buffer_from_to (error_buffer, err); |
| 908 |
} |
| 909 |
|
| 910 |
|
| 911 |
/* Restore a previous pushed error state. */ |
| 912 |
|
| 913 |
void |
| 914 |
gfc_pop_error (gfc_error_buffer *err) |
| 915 |
{ |
| 916 |
gfc_move_error_buffer_from_to (err, error_buffer); |
| 917 |
} |
| 918 |
|
| 919 |
|
| 920 |
/* Free a pushed error state, but keep the current error state. */ |
| 921 |
|
| 922 |
void |
| 923 |
gfc_free_error (gfc_error_buffer *err) |
| 924 |
{ |
| 925 |
gfc_clear_diagnostic_buffer (&(err->buffer)); |
| 926 |
} |
| 927 |
|
| 928 |
|
| 929 |
/* Report the number of warnings and errors that occurred to the caller. */ |
| 930 |
|
| 931 |
void |
| 932 |
gfc_get_errors (int *w, int *e) |
| 933 |
{ |
| 934 |
if (w != NULL) |
| 935 |
*w = warningcount + werrorcount; |
| 936 |
if (e != NULL) |
| 937 |
*e = errorcount + sorrycount + werrorcount; |
| 938 |
} |
| 939 |
|
| 940 |
|
| 941 |
/* Switch errors into warnings. */ |
| 942 |
|
| 943 |
void |
| 944 |
gfc_errors_to_warnings (bool f) |
| 945 |
{ |
| 946 |
warnings_not_errors = f; |
| 947 |
} |
| 948 |
|
| 949 |
void |
| 950 |
gfc_diagnostics_init (void) |
| 951 |
{ |
| 952 |
diagnostic_text_starter (global_dc) = gfc_diagnostic_text_starter; |
| 953 |
diagnostic_start_span (global_dc) = gfc_diagnostic_start_span; |
| 954 |
diagnostic_text_finalizer (global_dc) = gfc_diagnostic_text_finalizer; |
| 955 |
global_dc->set_format_decoder (gfc_format_decoder); |
| 956 |
global_dc->m_source_printing.caret_chars[0] = '1'; |
| 957 |
global_dc->m_source_printing.caret_chars[1] = '2'; |
| 958 |
pp_warning_buffer = new diagnostic_buffer (*global_dc); |
| 959 |
error_buffer = new gfc_error_buffer (); |
| 960 |
pp_error_buffer = &(error_buffer->buffer); |
| 961 |
} |
| 962 |
|
| 963 |
void |
| 964 |
gfc_diagnostics_finish (void) |
| 965 |
{ |
| 966 |
tree_diagnostics_defaults (global_dc); |
| 967 |
/* We still want to use the gfc starter and finalizer, not the tree |
| 968 |
defaults. */ |
| 969 |
diagnostic_text_starter (global_dc) = gfc_diagnostic_text_starter; |
| 970 |
diagnostic_text_finalizer (global_dc) = gfc_diagnostic_text_finalizer; |
| 971 |
global_dc->m_source_printing.caret_chars[0] = '^'; |
| 972 |
global_dc->m_source_printing.caret_chars[1] = '^'; |
| 973 |
delete error_buffer; |
| 974 |
error_buffer = nullptr; |
| 975 |
pp_error_buffer = nullptr; |
| 976 |
} |