error.c 37.1 KB
Newer Older
1
/* Handle errors.
2
   Copyright (C) 2000-2019 Free Software Foundation, Inc.
3 4
   Contributed by Andy Vaught & Niels Kristian Bech Jensen

5
This file is part of GCC.
6

7 8
GCC is free software; you can redistribute it and/or modify it under
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 13 14 15
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.
16 17

You should have received a copy of the GNU General Public License
18 19
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */
20 21 22 23 24 25 26 27 28

/* Handle the inevitable errors.  A major catch here is that things
   flagged as errors in one match subroutine can conceivably be legal
   elsewhere.  This means that error messages are recorded and saved
   for possible use later.  If a line does not match a legal
   construction, then the saved error message is reported.  */

#include "config.h"
#include "system.h"
29
#include "coretypes.h"
30
#include "options.h"
31 32
#include "gfortran.h"

33 34
#include "diagnostic.h"
#include "diagnostic-color.h"
35
#include "tree-diagnostic.h" /* tree_diagnostics_defaults */
36

37
static int suppress_errors = 0;
38

39
static bool warnings_not_errors = false;
40

41
static int terminal_width;
42

43 44
/* True if the error/warnings should be buffered.  */
static bool buffered_p;
45 46

static gfc_error_buffer error_buffer;
47 48
/* These are always buffered buffers (.flush_p == false) to be used by
   the pretty-printer.  */
49
static output_buffer *pp_error_buffer, *pp_warning_buffer;
50 51
static int warningcount_buffered, werrorcount_buffered;

52 53 54 55 56 57 58
/* Return true if there output_buffer is empty.  */

static bool
gfc_output_buffer_empty_p (const output_buffer * buf)
{
  return output_buffer_last_position_in_text (buf) == NULL;
}
59

60 61 62 63 64 65 66 67 68
/* Go one level deeper suppressing errors.  */

void
gfc_push_suppress_errors (void)
{
  gcc_assert (suppress_errors >= 0);
  ++suppress_errors;
}

69
static void
70
gfc_error_opt (int opt, const char *gmsgid, va_list ap)  ATTRIBUTE_GCC_GFC(2,0);
71 72 73 74

static bool
gfc_warning (int opt, const char *gmsgid, va_list ap) ATTRIBUTE_GCC_GFC(2,0);

75 76 77 78 79 80 81 82 83 84 85

/* Leave one level of error suppressing.  */

void
gfc_pop_suppress_errors (void)
{
  gcc_assert (suppress_errors > 0);
  --suppress_errors;
}


86 87
/* Determine terminal width (for trimming source lines in output).  */

88
static int
89
gfc_get_terminal_width (void)
90
{
91
  return isatty (STDERR_FILENO) ? get_terminal_width () : INT_MAX;
92 93 94
}


95 96 97 98 99
/* Per-file error initialization.  */

void
gfc_error_init_1 (void)
{
100
  terminal_width = gfc_get_terminal_width ();
101
  gfc_buffer_error (false);
102 103 104 105 106 107
}


/* Set the flag for buffering errors or not.  */

void
108
gfc_buffer_error (bool flag)
109
{
110
  buffered_p = flag;
111 112 113 114
}


/* Add a single character to the error buffer or output depending on
115
   buffered_p.  */
116 117

static void
118
error_char (char)
119
{
120
  /* FIXME: Unused function to be removed in a subsequent patch.  */
121 122 123 124 125 126 127 128 129 130 131 132 133
}


/* Copy a string to wherever it needs to go.  */

static void
error_string (const char *p)
{
  while (*p)
    error_char (*p++);
}


134 135
/* Print a formatted integer to the error buffer or output.  */

136
#define IBUF_LEN 60
137 138

static void
139
error_uinteger (unsigned long int i)
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
{
  char *p, int_buf[IBUF_LEN];

  p = int_buf + IBUF_LEN - 1;
  *p-- = '\0';

  if (i == 0)
    *p-- = '0';

  while (i > 0)
    {
      *p-- = i % 10 + '0';
      i = i / 10;
    }

  error_string (p + 1);
}

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
static void
error_integer (long int i)
{
  unsigned long int u;

  if (i < 0)
    {
      u = (unsigned long int) -i;
      error_char ('-');
    }
  else
    u = i;

  error_uinteger (u);
}

174

175 176 177
static size_t
gfc_widechar_display_length (gfc_char_t c)
{
178 179
  if (gfc_wide_is_printable (c) || c == '\t')
    /* Printable ASCII character, or tabulation (output as a space).  */
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    return 1;
  else if (c < ((gfc_char_t) 1 << 8))
    /* Displayed as \x??  */
    return 4;
  else if (c < ((gfc_char_t) 1 << 16))
    /* Displayed as \u????  */
    return 6;
  else
    /* Displayed as \U????????  */
    return 10;
}


/* Length of the ASCII representation of the wide string, escaping wide
   characters as print_wide_char_into_buffer() does.  */

static size_t
gfc_wide_display_length (const gfc_char_t *str)
{
  size_t i, len;

  for (i = 0, len = 0; str[i]; i++)
    len += gfc_widechar_display_length (str[i]);

  return len;
}

static int
208
print_wide_char_into_buffer (gfc_char_t c, char *buf)
209 210 211 212
{
  static const char xdigit[16] = { '0', '1', '2', '3', '4', '5', '6',
    '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

213
  if (gfc_wide_is_printable (c) || c == '\t')
214 215
    {
      buf[1] = '\0';
216 217
      /* Tabulation is output as a space.  */
      buf[0] = (unsigned char) (c == '\t' ? ' ' : c);
218
      return 1;
219
    }
220 221
  else if (c < ((gfc_char_t) 1 << 8))
    {
222 223
      buf[4] = '\0';
      buf[3] = xdigit[c & 0x0F];
224
      c = c >> 4;
225
      buf[2] = xdigit[c & 0x0F];
226

227 228
      buf[1] = 'x';
      buf[0] = '\\';
229
      return 4;
230 231 232
    }
  else if (c < ((gfc_char_t) 1 << 16))
    {
233 234
      buf[6] = '\0';
      buf[5] = xdigit[c & 0x0F];
235
      c = c >> 4;
236
      buf[4] = xdigit[c & 0x0F];
237
      c = c >> 4;
238
      buf[3] = xdigit[c & 0x0F];
239
      c = c >> 4;
240
      buf[2] = xdigit[c & 0x0F];
241

242 243
      buf[1] = 'u';
      buf[0] = '\\';
244
      return 6;
245 246 247
    }
  else
    {
248 249 250 251 252
      buf[10] = '\0';
      buf[9] = xdigit[c & 0x0F];
      c = c >> 4;
      buf[8] = xdigit[c & 0x0F];
      c = c >> 4;
253 254 255 256 257 258 259 260 261 262 263 264
      buf[7] = xdigit[c & 0x0F];
      c = c >> 4;
      buf[6] = xdigit[c & 0x0F];
      c = c >> 4;
      buf[5] = xdigit[c & 0x0F];
      c = c >> 4;
      buf[4] = xdigit[c & 0x0F];
      c = c >> 4;
      buf[3] = xdigit[c & 0x0F];
      c = c >> 4;
      buf[2] = xdigit[c & 0x0F];

265 266
      buf[1] = 'U';
      buf[0] = '\\';
267
      return 10;
268
    }
269
}
270

271 272 273 274 275 276 277
static char wide_char_print_buffer[11];

const char *
gfc_print_wide_char (gfc_char_t c)
{
  print_wide_char_into_buffer (c, wide_char_print_buffer);
  return wide_char_print_buffer;
278 279
}

280

281 282 283 284
/* Show the file, where it was included, and the source line, give a
   locus.  Calls error_printf() recursively, but the recursion is at
   most one level deep.  */

285
static void error_printf (const char *, ...) ATTRIBUTE_GCC_GFC(1,2);
286 287

static void
288
show_locus (locus *loc, int c1, int c2)
289
{
290
  gfc_linebuf *lb;
291
  gfc_file *f;
292
  gfc_char_t *p;
293
  int i, offset, cmax;
294 295 296 297 298

  /* TODO: Either limit the total length and number of included files
     displayed or add buffering of arbitrary number of characters in
     error messages.  */

299 300 301 302 303 304
  /* Write out the error header line, giving the source file and error
     location (in GNU standard "[file]:[line].[column]:" format),
     followed by an "included by" stack and a blank line.  This header
     format is matched by a testsuite parser defined in
     lib/gfortran-dg.exp.  */

305 306
  lb = loc->lb;
  f = lb->file;
307 308 309

  error_string (f->filename);
  error_char (':');
Paul Thomas committed
310

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
  error_integer (LOCATION_LINE (lb->location));

  if ((c1 > 0) || (c2 > 0))
    error_char ('.');

  if (c1 > 0)
    error_integer (c1);

  if ((c1 > 0) && (c2 > 0))
    error_char ('-');

  if (c2 > 0)
    error_integer (c2);

  error_char (':');
  error_char ('\n');
327 328

  for (;;)
329
    {
330 331
      i = f->inclusion_line;

332
      f = f->up;
333 334
      if (f == NULL) break;

335
      error_printf ("    Included at %s:%d:", f->filename, i);
336 337
    }

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
  error_char ('\n');

  /* Calculate an appropriate horizontal offset of the source line in
     order to get the error locus within the visible portion of the
     line.  Note that if the margin of 5 here is changed, the
     corresponding margin of 10 in show_loci should be changed.  */

  offset = 0;

  /* If the two loci would appear in the same column, we shift
     '2' one column to the right, so as to print '12' rather than
     just '1'.  We do this here so it will be accounted for in the
     margin calculations.  */

  if (c1 == c2)
    c2 += 1;

  cmax = (c1 < c2) ? c2 : c1;
  if (cmax > terminal_width - 5)
    offset = cmax - terminal_width + 5;

359
  /* Show the line itself, taking care not to print more than what can
Paul Thomas committed
360
     show up on the terminal.  Tabs are converted to spaces, and
361 362
     nonprintable characters are converted to a "\xNN" sequence.  */

363
  p = &(lb->line[offset]);
364
  i = gfc_wide_display_length (p);
365 366 367
  if (i > terminal_width)
    i = terminal_width - 1;

368
  while (i > 0)
369
    {
370
      static char buffer[11];
371
      i -= print_wide_char_into_buffer (*p++, buffer);
372
      error_string (buffer);
373 374 375
    }

  error_char ('\n');
376 377

  /* Show the '1' and/or '2' corresponding to the column of the error
Paul Thomas committed
378
     locus.  Note that a value of -1 for c1 or c2 will simply cause
379 380 381 382
     the relevant number not to be printed.  */

  c1 -= offset;
  c2 -= offset;
383
  cmax -= offset;
384

385
  p = &(lb->line[offset]);
386
  for (i = 0; i < cmax; i++)
387
    {
388 389 390
      int spaces, j;
      spaces = gfc_widechar_display_length (*p++);

391
      if (i == c1)
392
	error_char ('1'), spaces--;
393
      else if (i == c2)
394 395 396
	error_char ('2'), spaces--;

      for (j = 0; j < spaces; j++)
397 398 399
	error_char (' ');
    }

400 401 402 403 404
  if (i == c1)
    error_char ('1');
  else if (i == c2)
    error_char ('2');

405 406
  error_char ('\n');

407 408 409 410
}


/* As part of printing an error, we show the source lines that caused
411 412
   the problem.  We show at least one, and possibly two loci; the two
   loci may or may not be on the same source line.  */
413 414

static void
415
show_loci (locus *l1, locus *l2)
416
{
417
  int m, c1, c2;
418

419
  if (l1 == NULL || l1->lb == NULL)
420 421 422 423 424
    {
      error_printf ("<During initialization>\n");
      return;
    }

425 426 427 428
  /* While calculating parameters for printing the loci, we consider possible
     reasons for printing one per line.  If appropriate, print the loci
     individually; otherwise we print them both on the same line.  */

429
  c1 = l1->nextc - l1->lb->line;
430
  if (l2 == NULL)
431 432 433 434
    {
      show_locus (l1, c1, -1);
      return;
    }
435

436
  c2 = l2->nextc - l2->lb->line;
437 438 439 440 441 442

  if (c1 < c2)
    m = c2 - c1;
  else
    m = c1 - c2;

Paul Thomas committed
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.  */
445

446
  if (l1->lb != l2->lb || m > terminal_width - 10)
447
    {
448 449 450
      show_locus (l1, c1, -1);
      show_locus (l2, -1, c2);
      return;
451 452
    }

453
  show_locus (l1, c1, c2);
454 455 456 457 458 459 460 461 462

  return;
}


/* Workhorse for the error printing subroutines.  This subroutine is
   inspired by g77's error handling and is similar to printf() with
   the following %-codes:

463
   %c Character, %d or %i Integer, %s String, %% Percent
464 465 466 467 468 469
   %L  Takes locus argument
   %C  Current locus (no argument)

   If a locus pointer is given, the actual source line is printed out
   and the column is indicated.  Since we want the error message at
   the bottom of any source file information, we must scan the
Paul Thomas committed
470
   argument list twice -- once to determine whether the loci are
471 472 473
   present and record this for printing, and once to print the error
   message after and loci have been printed.  A maximum of two locus
   arguments are permitted.
Paul Thomas committed
474

475 476 477
   This function is also called (recursively) by show_locus in the
   case of included files; however, as show_locus does not resupply
   any loci, the recursion is at most one level deep.  */
478 479 480

#define MAX_ARGS 10

481
static void ATTRIBUTE_GCC_GFC(2,0)
482 483
error_print (const char *type, const char *format0, va_list argp)
{
484 485
  enum { TYPE_CURRENTLOC, TYPE_LOCUS, TYPE_INTEGER, TYPE_UINTEGER,
         TYPE_LONGINT, TYPE_ULONGINT, TYPE_CHAR, TYPE_STRING,
486 487 488 489 490 491 492 493
	 NOTYPE };
  struct
  {
    int type;
    int pos;
    union
    {
      int intval;
494 495 496
      unsigned int uintval;
      long int longintval;
      unsigned long int ulongintval;
497 498 499 500 501 502 503 504 505 506
      char charval;
      const char * stringval;
    } u;
  } arg[MAX_ARGS], spec[MAX_ARGS];
  /* spec is the array of specifiers, in the same order as they
     appear in the format string.  arg is the array of arguments,
     in the same order as they appear in the va_list.  */

  char c;
  int i, n, have_l1, pos, maxpos;
507 508 509
  locus *l1, *l2, *loc;
  const char *format;

510
  loc = l1 = l2 = NULL;
511 512

  have_l1 = 0;
513 514
  pos = -1;
  maxpos = -1;
515 516 517 518

  n = 0;
  format = format0;

519 520 521 522 523 524 525
  for (i = 0; i < MAX_ARGS; i++)
    {
      arg[i].type = NOTYPE;
      spec[i].pos = -1;
    }

  /* First parse the format string for position specifiers.  */
526 527 528
  while (*format)
    {
      c = *format++;
529 530 531 532
      if (c != '%')
	continue;

      if (*format == '%')
533 534 535 536
	{
	  format++;
	  continue;
	}
537 538

      if (ISDIGIT (*format))
539
	{
540 541 542 543 544 545 546 547
	  /* This is a position specifier.  For example, the number
	     12 in the format string "%12$d", which specifies the third
	     argument of the va_list, formatted in %d format.
	     For details, see "man 3 printf".  */
	  pos = atoi(format) - 1;
	  gcc_assert (pos >= 0);
	  while (ISDIGIT(*format))
	    format++;
548 549
	  gcc_assert (*format == '$');
	  format++;
550 551 552
	}
      else
	pos++;
553

554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
      c = *format++;

      if (pos > maxpos)
	maxpos = pos;

      switch (c)
	{
	  case 'C':
	    arg[pos].type = TYPE_CURRENTLOC;
	    break;

	  case 'L':
	    arg[pos].type = TYPE_LOCUS;
	    break;

	  case 'd':
	  case 'i':
	    arg[pos].type = TYPE_INTEGER;
	    break;

574 575
	  case 'u':
	    arg[pos].type = TYPE_UINTEGER;
576
	    break;
577 578 579 580 581 582 583 584 585 586 587

	  case 'l':
	    c = *format++;
	    if (c == 'u')
	      arg[pos].type = TYPE_ULONGINT;
	    else if (c == 'i' || c == 'd')
	      arg[pos].type = TYPE_LONGINT;
	    else
	      gcc_unreachable ();
	    break;

588 589 590 591 592 593 594 595 596 597 598
	  case 'c':
	    arg[pos].type = TYPE_CHAR;
	    break;

	  case 's':
	    arg[pos].type = TYPE_STRING;
	    break;

	  default:
	    gcc_unreachable ();
	}
599

600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
      spec[n++].pos = pos;
    }

  /* Then convert the values for each %-style argument.  */
  for (pos = 0; pos <= maxpos; pos++)
    {
      gcc_assert (arg[pos].type != NOTYPE);
      switch (arg[pos].type)
	{
	  case TYPE_CURRENTLOC:
	    loc = &gfc_current_locus;
	    /* Fall through.  */

	  case TYPE_LOCUS:
	    if (arg[pos].type == TYPE_LOCUS)
615
	      loc = va_arg (argp, locus *);
616 617 618 619 620

	    if (have_l1)
	      {
		l2 = loc;
		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')
623
		  l2->nextc++;
624 625 626 627 628 629
	      }
	    else
	      {
		l1 = loc;
		have_l1 = 1;
		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')
632
		  l1->nextc++;
633 634 635 636 637 638 639
	      }
	    break;

	  case TYPE_INTEGER:
	    arg[pos].u.intval = va_arg (argp, int);
	    break;

640 641 642 643 644 645 646 647 648 649 650 651
	  case TYPE_UINTEGER:
	    arg[pos].u.uintval = va_arg (argp, unsigned int);
	    break;

	  case TYPE_LONGINT:
	    arg[pos].u.longintval = va_arg (argp, long int);
	    break;

	  case TYPE_ULONGINT:
	    arg[pos].u.ulongintval = va_arg (argp, unsigned long int);
	    break;

652 653 654 655 656 657 658 659 660 661
	  case TYPE_CHAR:
	    arg[pos].u.charval = (char) va_arg (argp, int);
	    break;

	  case TYPE_STRING:
	    arg[pos].u.stringval = (const char *) va_arg (argp, char *);
	    break;

	  default:
	    gcc_unreachable ();
662 663 664
	}
    }

665 666 667
  for (n = 0; spec[n].pos >= 0; n++)
    spec[n].u = arg[spec[n].pos].u;

668 669 670
  /* Show the current loci if we have to.  */
  if (have_l1)
    show_loci (l1, l2);
671

672
  if (*type)
673 674 675 676
    {
      error_string (type);
      error_char (' ');
    }
677 678 679 680 681 682 683 684 685 686 687 688 689 690

  have_l1 = 0;
  format = format0;
  n = 0;

  for (; *format; format++)
    {
      if (*format != '%')
	{
	  error_char (*format);
	  continue;
	}

      format++;
691
      if (ISDIGIT (*format))
692 693
	{
	  /* This is a position specifier.  See comment above.  */
694
	  while (ISDIGIT (*format))
695
	    format++;
Paul Thomas committed
696

697 698 699
	  /* Skip over the dollar sign.  */
	  format++;
	}
Paul Thomas committed
700

701 702 703 704 705 706 707
      switch (*format)
	{
	case '%':
	  error_char ('%');
	  break;

	case 'c':
708
	  error_char (spec[n++].u.charval);
709 710 711 712 713
	  break;

	case 's':
	case 'C':		/* Current locus */
	case 'L':		/* Specified locus */
714
	  error_string (spec[n++].u.stringval);
715
	  break;
716

717 718 719
	case 'd':
	case 'i':
	  error_integer (spec[n++].u.intval);
720
	  break;
721 722 723 724 725 726 727 728 729 730 731 732 733

	case 'u':
	  error_uinteger (spec[n++].u.uintval);
	  break;

	case 'l':
	  format++;
	  if (*format == 'u')
	    error_uinteger (spec[n++].u.ulongintval);
	  else
	    error_integer (spec[n++].u.longintval);
	  break;

734 735 736 737 738 739 740 741 742 743
	}
    }

  error_char ('\n');
}


/* Wrapper for error_print().  */

static void
744
error_printf (const char *gmsgid, ...)
745 746 747
{
  va_list argp;

748 749
  va_start (argp, gmsgid);
  error_print ("", _(gmsgid), argp);
750 751 752 753
  va_end (argp);
}


754 755 756 757 758 759 760 761 762 763
/* Clear any output buffered in a pretty-print output_buffer.  */

static void
gfc_clear_pp_buffer (output_buffer *this_buffer)
{
  pretty_printer *pp = global_dc->printer;
  output_buffer *tmp_buffer = pp->buffer;
  pp->buffer = this_buffer;
  pp_clear_output_area (pp);
  pp->buffer = tmp_buffer;
764 765 766
  /* We need to reset last_location, otherwise we may skip caret lines
     when we actually give a diagnostic.  */
  global_dc->last_location = UNKNOWN_LOCATION;
767 768
}

769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
/* The currently-printing diagnostic, for use by gfc_format_decoder,
   for colorizing %C and %L.  */

static diagnostic_info *curr_diagnostic;

/* A helper function to call diagnostic_report_diagnostic, while setting
   curr_diagnostic for the duration of the call.  */

static bool
gfc_report_diagnostic (diagnostic_info *diagnostic)
{
  gcc_assert (diagnostic != NULL);
  curr_diagnostic = diagnostic;
  bool ret = diagnostic_report_diagnostic (global_dc, diagnostic);
  curr_diagnostic = NULL;
  return ret;
}
786 787 788 789 790 791 792 793 794 795 796

/* This is just a helper function to avoid duplicating the logic of
   gfc_warning.  */

static bool
gfc_warning (int opt, const char *gmsgid, va_list ap)
{
  va_list argp;
  va_copy (argp, ap);

  diagnostic_info diagnostic;
797
  rich_location rich_loc (line_table, UNKNOWN_LOCATION);
798 799 800 801
  bool fatal_errors = global_dc->fatal_errors;
  pretty_printer *pp = global_dc->printer;
  output_buffer *tmp_buffer = pp->buffer;

802
  gfc_clear_pp_buffer (pp_warning_buffer);
803 804 805

  if (buffered_p)
    {
806
      pp->buffer = pp_warning_buffer;
807 808 809 810 811
      global_dc->fatal_errors = false;
      /* To prevent -fmax-errors= triggering.  */
      --werrorcount;
    }

812
  diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc,
813 814
		       DK_WARNING);
  diagnostic.option_index = opt;
815
  bool ret = gfc_report_diagnostic (&diagnostic);
816 817 818 819 820 821 822 823 824 825 826 827 828 829

  if (buffered_p)
    {
      pp->buffer = tmp_buffer;
      global_dc->fatal_errors = fatal_errors;

      warningcount_buffered = 0;
      werrorcount_buffered = 0;
      /* Undo the above --werrorcount if not Werror, otherwise
	 werrorcount is correct already.  */
      if (!ret)
	++werrorcount;
      else if (diagnostic.kind == DK_ERROR)
	++werrorcount_buffered;
Paul Thomas committed
830
      else
831 832
	++werrorcount, --warningcount, ++warningcount_buffered;
    }
Paul Thomas committed
833

834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
  va_end (argp);
  return ret;
}

/* Issue a warning.  */

bool
gfc_warning (int opt, const char *gmsgid, ...)
{
  va_list argp;

  va_start (argp, gmsgid);
  bool ret = gfc_warning (opt, gmsgid, argp);
  va_end (argp);
  return ret;
}


852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
/* Whether, for a feature included in a given standard set (GFC_STD_*),
   we should issue an error or a warning, or be quiet.  */

notification
gfc_notification_std (int std)
{
  bool warning;

  warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
  if ((gfc_option.allow_std & std) != 0 && !warning)
    return SILENT;

  return warning ? WARNING : ERROR;
}


868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
/* Return a string describing the nature of a standard violation
 * and/or the relevant version of the standard.  */

char const*
notify_std_msg(int std)
{

  if (std & GFC_STD_F2018_DEL)
    return _("Fortran 2018 deleted feature:");
  else if (std & GFC_STD_F2018_OBS)
    return _("Fortran 2018 obsolescent feature:");
  else if (std & GFC_STD_F2018)
    return _("Fortran 2018:");
  else if (std & GFC_STD_F2008_OBS)
    return _("Fortran 2008 obsolescent feature:");
  else if (std & GFC_STD_F2008)
    return "Fortran 2008:";
  else if (std & GFC_STD_F2003)
    return "Fortran 2003:";
  else if (std & GFC_STD_GNU)
    return _("GNU Extension:");
  else if (std & GFC_STD_LEGACY)
    return _("Legacy Extension:");
  else if (std & GFC_STD_F95_OBS)
    return _("Obsolescent feature:");
  else if (std & GFC_STD_F95_DEL)
    return _("Deleted feature:");
  else
    gcc_unreachable ();
}


900 901
/* Possibly issue a warning/error about use of a nonstandard (or deleted)
   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.  */
904

905
bool
906 907 908 909 910 911
gfc_notify_std (int std, const char *gmsgid, ...)
{
  va_list argp;
  const char *msg, *msg2;
  char *buffer;

912 913 914 915 916
  /* Determine whether an error or a warning is needed.  */
  const int wstd = std & gfc_option.warn_std;    /* Standard to warn about.  */
  const int estd = std & ~gfc_option.allow_std;  /* Standard to error about.  */
  const bool warning = (wstd != 0) && !inhibit_warnings;
  const bool error = (estd != 0);
917

918 919
  if (!error && !warning)
    return true;
920
  if (suppress_errors)
921
    return !error;
922

923 924 925 926
  if (error)
    msg = notify_std_msg (estd);
  else
    msg = notify_std_msg (wstd);
927 928 929 930 931 932 933 934

  msg2 = _(gmsgid);
  buffer = (char *) alloca (strlen (msg) + strlen (msg2) + 2);
  strcpy (buffer, msg);
  strcat (buffer, " ");
  strcat (buffer, msg2);

  va_start (argp, gmsgid);
935
  if (error)
936
    gfc_error_opt (0, buffer, argp);
937 938
  else
    gfc_warning (0, buffer, argp);
939 940
  va_end (argp);

941 942 943 944
  if (error)
    return false;
  else
    return (warning && !warnings_are_errors);
945 946 947
}


948 949 950 951
/* Called from output_format -- during diagnostic message processing
   to handle Fortran specific format specifiers with the following meanings:

   %C  Current locus (no argument)
952
   %L  Takes locus argument
953 954
*/
static bool
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)
958 959 960 961
{
  switch (*spec)
    {
    case 'C':
962
    case 'L':
963
      {
964
	static const char *result[2] = { "(1)", "(2)" };
965 966 967 968 969 970 971
	locus *loc;
	if (*spec == 'C')
	  loc = &gfc_current_locus;
	else
	  loc = va_arg (*text->args_ptr, locus *);
	gcc_assert (loc->nextc - loc->lb->line >= 0);
	unsigned int offset = loc->nextc - loc->lb->line;
972
	if (*spec == 'C' && *loc->nextc != '\0')
973 974
	  /* Point %C first offending character not the last good one. */
	  offset++;
975 976 977
	/* If location[0] != UNKNOWN_LOCATION means that we already
	   processed one of %C/%L.  */
	int loc_num = text->get_location (0) == UNKNOWN_LOCATION ? 0 : 1;
978 979 980 981
	location_t src_loc
	  = linemap_position_for_loc_and_offset (line_table,
						 loc->lb->location,
						 offset);
982
	text->set_location (loc_num, src_loc, SHOW_RANGE_WITH_CARET);
983 984 985 986 987 988 989 990 991 992
	/* Colorize the markers to match the color choices of
	   diagnostic_show_locus (the initial location has a color given
	   by the "kind" of the diagnostic, the secondary location has
	   color "range1").  */
	gcc_assert (curr_diagnostic != NULL);
	const char *color
	  = (loc_num
	     ? "range1"
	     : diagnostic_get_color_for_kind (curr_diagnostic->kind));
	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)));
995 996 997
	return true;
      }
    default:
998 999 1000 1001
      /* Fall through info the middle-end decoder, as e.g. stor-layout.c
	 etc. diagnostics can use the FE printer while the FE is still
	 active.  */
      return default_tree_printer (pp, text, spec, precision, wide,
1002
				   set_locus, hash, quoted, buffer_ptr);
1003 1004 1005
    }
}

1006 1007
/* Return a malloc'd string describing the kind of diagnostic.  The
   caller is responsible for freeing the memory.  */
1008
static char *
1009 1010
gfc_diagnostic_build_kind_prefix (diagnostic_context *context,
				  const diagnostic_info *diagnostic)
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
{
  static const char *const diagnostic_kind_text[] = {
#define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
#include "gfc-diagnostic.def"
#undef DEFINE_DIAGNOSTIC_KIND
    "must-not-happen"
  };
  static const char *const diagnostic_kind_color[] = {
#define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
#include "gfc-diagnostic.def"
#undef DEFINE_DIAGNOSTIC_KIND
    NULL
  };
  gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
  const char *text = _(diagnostic_kind_text[diagnostic->kind]);
  const char *text_cs = "", *text_ce = "";
  pretty_printer *pp = context->printer;

  if (diagnostic_kind_color[diagnostic->kind])
    {
      text_cs = colorize_start (pp_show_color (pp),
				diagnostic_kind_color[diagnostic->kind]);
      text_ce = colorize_stop (pp_show_color (pp));
    }
1035
  return build_message_string ("%s%s:%s ", text_cs, text, text_ce);
1036 1037 1038 1039 1040 1041
}

/* Return a malloc'd string describing a location.  The caller is
   responsible for freeing the memory.  */
static char *
gfc_diagnostic_build_locus_prefix (diagnostic_context *context,
1042
				   expanded_location s)
1043 1044
{
  pretty_printer *pp = context->printer;
1045 1046 1047
  const char *locus_cs = colorize_start (pp_show_color (pp), "locus");
  const char *locus_ce = colorize_stop (pp_show_color (pp));
  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,
1053
				  s.column, locus_ce)
1054
	  : build_message_string ("%s%s:%d:%s", locus_cs, s.file, s.line, locus_ce));
1055 1056
}

1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
/* Return a malloc'd string describing two locations.  The caller is
   responsible for freeing the memory.  */
static char *
gfc_diagnostic_build_locus_prefix (diagnostic_context *context,
				   expanded_location s, expanded_location s2)
{
  pretty_printer *pp = context->printer;
  const char *locus_cs = colorize_start (pp_show_color (pp), "locus");
  const char *locus_ce = colorize_stop (pp_show_color (pp));

  return (s.file == NULL
	  ? build_message_string ("%s%s:%s", locus_cs, progname, locus_ce )
	  : !strcmp (s.file, N_("<built-in>"))
	  ? build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce)
	  : context->show_column
	  ? build_message_string ("%s%s:%d:%d-%d:%s", locus_cs, s.file, s.line,
				  MIN (s.column, s2.column),
				  MAX (s.column, s2.column), locus_ce)
	  : build_message_string ("%s%s:%d:%s", locus_cs, s.file, s.line,
				  locus_ce));
}

/* This function prints the locus (file:line:column), the diagnostic kind
1080 1081
   (Error, Warning) and (optionally) the relevant lines of code with
   annotation lines with '1' and/or '2' below them.
1082

1083
   With -fdiagnostic-show-caret (the default) it prints:
1084

1085
       [locus of primary range]:
Paul Thomas committed
1086

1087 1088 1089
          some code
                 1
       Error: Some error at (1)
Paul Thomas committed
1090

1091 1092
  With -fno-diagnostic-show-caret or if the primary range is not
  valid, it prints:
1093

1094
       [locus of primary range]: Error: Some error at (1) and (2)
1095
*/
Paul Thomas committed
1096
static void
1097 1098 1099
gfc_diagnostic_starter (diagnostic_context *context,
			diagnostic_info *diagnostic)
{
1100 1101 1102 1103
  char * kind_prefix = gfc_diagnostic_build_kind_prefix (context, diagnostic);

  expanded_location s1 = diagnostic_expand_location (diagnostic);
  expanded_location s2;
1104
  bool one_locus = diagnostic->richloc->get_num_locations () < 2;
1105 1106
  bool same_locus = false;

Paul Thomas committed
1107
  if (!one_locus)
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
    {
      s2 = diagnostic_expand_location (diagnostic, 1);
      same_locus = diagnostic_same_line (context, s1, s2);
    }

  char * locus_prefix = (one_locus || !same_locus)
    ? gfc_diagnostic_build_locus_prefix (context, s1)
    : gfc_diagnostic_build_locus_prefix (context, s1, s2);

  if (!context->show_caret
      || diagnostic_location (diagnostic, 0) <= BUILTINS_LOCATION
      || diagnostic_location (diagnostic, 0) == context->last_location)
    {
      pp_set_prefix (context->printer,
		     concat (locus_prefix, " ", kind_prefix, NULL));
      free (locus_prefix);

      if (one_locus || same_locus)
	{
	  free (kind_prefix);
	  return;
	}
      /* In this case, we print the previous locus and prefix as:

	  [locus]:[prefix]: (1)

	 and we flush with a new line before setting the new prefix.  */
      pp_string (context->printer, "(1)");
      pp_newline (context->printer);
      locus_prefix = gfc_diagnostic_build_locus_prefix (context, s2);
      pp_set_prefix (context->printer,
		     concat (locus_prefix, " ", kind_prefix, NULL));
      free (kind_prefix);
      free (locus_prefix);
    }
  else
1144
    {
1145
      pp_verbatim (context->printer, "%s", locus_prefix);
1146 1147 1148
      free (locus_prefix);
      /* Fortran uses an empty line between locus and caret line.  */
      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
1153
	 locus.  */
1154
      pp_set_prefix (context->printer, kind_prefix);
1155
    }
1156 1157 1158
}

static void
1159 1160 1161 1162 1163
gfc_diagnostic_start_span (diagnostic_context *context,
			   expanded_location exploc)
{
  char *locus_prefix;
  locus_prefix = gfc_diagnostic_build_locus_prefix (context, exploc);
1164
  pp_verbatim (context->printer, "%s", locus_prefix);
1165 1166 1167 1168 1169 1170 1171 1172
  free (locus_prefix);
  pp_newline (context->printer);
  /* Fortran uses an empty line between locus and caret line.  */
  pp_newline (context->printer);
}


static void
1173
gfc_diagnostic_finalizer (diagnostic_context *context,
1174 1175
			  diagnostic_info *diagnostic ATTRIBUTE_UNUSED,
			  diagnostic_t orig_diag_kind ATTRIBUTE_UNUSED)
1176
{
1177 1178
  pp_destroy_prefix (context->printer);
  pp_newline_and_flush (context->printer);
1179 1180
}

1181 1182 1183 1184 1185 1186 1187 1188
/* Immediate warning (i.e. do not buffer the warning) with an explicit
   location.  */

bool
gfc_warning_now_at (location_t loc, int opt, const char *gmsgid, ...)
{
  va_list argp;
  diagnostic_info diagnostic;
1189
  rich_location rich_loc (line_table, loc);
1190 1191 1192
  bool ret;

  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);
1196 1197 1198 1199
  va_end (argp);
  return ret;
}

1200
/* Immediate warning (i.e. do not buffer the warning).  */
1201

1202
bool
1203
gfc_warning_now (int opt, const char *gmsgid, ...)
1204 1205 1206
{
  va_list argp;
  diagnostic_info diagnostic;
1207
  rich_location rich_loc (line_table, UNKNOWN_LOCATION);
1208 1209 1210
  bool ret;

  va_start (argp, gmsgid);
1211
  diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc,
1212 1213
		       DK_WARNING);
  diagnostic.option_index = opt;
1214
  ret = gfc_report_diagnostic (&diagnostic);
1215 1216 1217 1218
  va_end (argp);
  return ret;
}

1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
/* Internal warning, do not buffer.  */

bool
gfc_warning_internal (int opt, const char *gmsgid, ...)
{
  va_list argp;
  diagnostic_info diagnostic;
  rich_location rich_loc (line_table, UNKNOWN_LOCATION);
  bool ret;

  va_start (argp, gmsgid);
  diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc,
		       DK_WARNING);
  diagnostic.option_index = opt;
1233
  ret = gfc_report_diagnostic (&diagnostic);
1234 1235 1236
  va_end (argp);
  return ret;
}
1237

1238
/* Immediate error (i.e. do not buffer).  */
1239 1240

void
1241
gfc_error_now (const char *gmsgid, ...)
1242 1243 1244
{
  va_list argp;
  diagnostic_info diagnostic;
1245
  rich_location rich_loc (line_table, UNKNOWN_LOCATION);
1246

1247 1248
  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);
1252 1253
  va_end (argp);
}
1254

1255 1256 1257 1258 1259 1260 1261 1262

/* Fatal error, never returns.  */

void
gfc_fatal_error (const char *gmsgid, ...)
{
  va_list argp;
  diagnostic_info diagnostic;
1263
  rich_location rich_loc (line_table, UNKNOWN_LOCATION);
1264 1265

  va_start (argp, gmsgid);
1266
  diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc, DK_FATAL);
1267
  gfc_report_diagnostic (&diagnostic);
1268 1269 1270 1271 1272
  va_end (argp);

  gcc_unreachable ();
}

1273 1274 1275 1276 1277
/* Clear the warning flag.  */

void
gfc_clear_warning (void)
{
1278
  gfc_clear_pp_buffer (pp_warning_buffer);
1279 1280
  warningcount_buffered = 0;
  werrorcount_buffered = 0;
1281 1282 1283 1284 1285 1286 1287 1288 1289
}


/* Check to see if any warnings have been saved.
   If so, print the warning.  */

void
gfc_warning_check (void)
{
1290
  if (! gfc_output_buffer_empty_p (pp_warning_buffer))
1291
    {
1292 1293 1294
      pretty_printer *pp = global_dc->printer;
      output_buffer *tmp_buffer = pp->buffer;
      pp->buffer = pp_warning_buffer;
1295 1296 1297
      pp_really_flush (pp);
      warningcount += warningcount_buffered;
      werrorcount += werrorcount_buffered;
1298
      gcc_assert (warningcount_buffered + werrorcount_buffered == 1);
1299
      pp->buffer = tmp_buffer;
Paul Thomas committed
1300 1301
      diagnostic_action_after_output (global_dc,
				      warningcount_buffered
1302
				      ? DK_WARNING : DK_ERROR);
1303
      diagnostic_check_max_errors (global_dc, true);
1304
    }
1305 1306 1307 1308
}


/* Issue an error.  */
1309

1310
static void
1311
gfc_error_opt (int opt, const char *gmsgid, va_list ap)
1312 1313
{
  va_list argp;
1314
  va_copy (argp, ap);
1315
  bool saved_abort_on_error = false;
1316 1317 1318

  if (warnings_not_errors)
    {
1319
      gfc_warning (opt, gmsgid, argp);
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
      va_end (argp);
      return;
    }

  if (suppress_errors)
    {
      va_end (argp);
      return;
    }

  diagnostic_info diagnostic;
1331
  rich_location richloc (line_table, UNKNOWN_LOCATION);
1332 1333 1334 1335 1336 1337 1338 1339
  bool fatal_errors = global_dc->fatal_errors;
  pretty_printer *pp = global_dc->printer;
  output_buffer *tmp_buffer = pp->buffer;

  gfc_clear_pp_buffer (pp_error_buffer);

  if (buffered_p)
    {
1340 1341 1342 1343
      /* To prevent -dH from triggering an abort on a buffered error,
	 save abort_on_error and restore it below.  */
      saved_abort_on_error = global_dc->abort_on_error;
      global_dc->abort_on_error = false;
1344 1345 1346
      pp->buffer = pp_error_buffer;
      global_dc->fatal_errors = false;
      /* To prevent -fmax-errors= triggering, we decrease it before
1347
	 report_diagnostic increases it.  */
1348
      --errorcount;
1349 1350
    }

1351
  diagnostic_set_info (&diagnostic, gmsgid, &argp, &richloc, DK_ERROR);
1352
  gfc_report_diagnostic (&diagnostic);
1353 1354 1355 1356 1357

  if (buffered_p)
    {
      pp->buffer = tmp_buffer;
      global_dc->fatal_errors = fatal_errors;
1358 1359
      global_dc->abort_on_error = saved_abort_on_error;

1360
    }
1361

1362 1363 1364 1365
  va_end (argp);
}


1366
void
1367
gfc_error_opt (int opt, const char *gmsgid, ...)
1368 1369 1370
{
  va_list argp;
  va_start (argp, gmsgid);
1371
  gfc_error_opt (opt, gmsgid, argp);
1372 1373 1374 1375 1376
  va_end (argp);
}


void
1377 1378 1379 1380
gfc_error (const char *gmsgid, ...)
{
  va_list argp;
  va_start (argp, gmsgid);
1381
  gfc_error_opt (0, gmsgid, argp);
1382 1383 1384
  va_end (argp);
}

1385 1386 1387 1388

/* This shouldn't happen... but sometimes does.  */

void
1389
gfc_internal_error (const char *gmsgid, ...)
1390
{
Paul Thomas committed
1391
  int e, w;
1392
  va_list argp;
1393
  diagnostic_info diagnostic;
1394
  rich_location rich_loc (line_table, UNKNOWN_LOCATION);
1395

Paul Thomas committed
1396 1397 1398 1399
  gfc_get_errors (&w, &e);
  if (e > 0)
    exit(EXIT_FAILURE);

1400
  va_start (argp, gmsgid);
1401
  diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc, DK_ICE);
1402
  gfc_report_diagnostic (&diagnostic);
1403 1404
  va_end (argp);

1405
  gcc_unreachable ();
1406 1407 1408 1409 1410 1411 1412 1413
}


/* Clear the error flag when we start to compile a source line.  */

void
gfc_clear_error (void)
{
1414
  error_buffer.flag = false;
1415
  warnings_not_errors = false;
1416
  gfc_clear_pp_buffer (pp_error_buffer);
1417 1418 1419
}


1420 1421
/* Tests the state of error_flag.  */

1422
bool
1423 1424
gfc_error_flag_test (void)
{
Paul Thomas committed
1425
  return error_buffer.flag
1426
    || !gfc_output_buffer_empty_p (pp_error_buffer);
1427 1428 1429
}


1430 1431 1432
/* Check to see if any errors have been saved.
   If so, print the error.  Returns the state of error_flag.  */

1433
bool
1434 1435
gfc_error_check (void)
{
1436 1437
  if (error_buffer.flag
      || ! gfc_output_buffer_empty_p (pp_error_buffer))
1438
    {
1439
      error_buffer.flag = false;
1440 1441 1442 1443 1444 1445 1446
      pretty_printer *pp = global_dc->printer;
      output_buffer *tmp_buffer = pp->buffer;
      pp->buffer = pp_error_buffer;
      pp_really_flush (pp);
      ++errorcount;
      gcc_assert (gfc_output_buffer_empty_p (pp_error_buffer));
      pp->buffer = tmp_buffer;
1447
      diagnostic_action_after_output (global_dc, DK_ERROR);
1448
      diagnostic_check_max_errors (global_dc, true);
1449
      return true;
1450
    }
1451

1452
  return false;
1453 1454
}

1455 1456 1457 1458 1459
/* Move the text buffered from FROM to TO, then clear
   FROM. Independently if there was text in FROM, TO is also
   cleared. */

static void
1460 1461
gfc_move_error_buffer_from_to (gfc_error_buffer * buffer_from,
			       gfc_error_buffer * buffer_to)
1462
{
1463 1464 1465 1466 1467 1468
  output_buffer * from = &(buffer_from->buffer);
  output_buffer * to =  &(buffer_to->buffer);

  buffer_to->flag = buffer_from->flag;
  buffer_from->flag = false;

1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
  gfc_clear_pp_buffer (to);
  /* We make sure this is always buffered.  */
  to->flush_p = false;

  if (! gfc_output_buffer_empty_p (from))
    {
      const char *str = output_buffer_formatted_text (from);
      output_buffer_append_r (to, str, strlen (str));
      gfc_clear_pp_buffer (from);
    }
}
1480 1481 1482 1483

/* Save the existing error state.  */

void
1484
gfc_push_error (gfc_error_buffer *err)
1485
{
1486
  gfc_move_error_buffer_from_to (&error_buffer, err);
1487 1488 1489 1490 1491 1492
}


/* Restore a previous pushed error state.  */

void
1493
gfc_pop_error (gfc_error_buffer *err)
1494
{
1495
  gfc_move_error_buffer_from_to (err, &error_buffer);
1496 1497 1498 1499 1500 1501
}


/* Free a pushed error state, but keep the current error state.  */

void
1502
gfc_free_error (gfc_error_buffer *err)
1503
{
1504
  gfc_clear_pp_buffer (&(err->buffer));
1505 1506 1507
}


1508
/* Report the number of warnings and errors that occurred to the caller.  */
1509 1510 1511 1512 1513

void
gfc_get_errors (int *w, int *e)
{
  if (w != NULL)
1514
    *w = warningcount + werrorcount;
1515
  if (e != NULL)
1516
    *e = errorcount + sorrycount + werrorcount;
1517
}
1518 1519 1520 1521 1522


/* Switch errors into warnings.  */

void
1523
gfc_errors_to_warnings (bool f)
1524
{
1525
  warnings_not_errors = f;
1526
}
1527 1528 1529 1530 1531

void
gfc_diagnostics_init (void)
{
  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 1536
  global_dc->caret_chars[0] = '1';
  global_dc->caret_chars[1] = '2';
1537 1538
  pp_warning_buffer = new (XNEW (output_buffer)) output_buffer ();
  pp_warning_buffer->flush_p = false;
1539 1540
  /* pp_error_buffer is statically allocated.  This simplifies memory
     management when using gfc_push/pop_error. */
1541
  pp_error_buffer = &(error_buffer.buffer);
1542
  pp_error_buffer->flush_p = false;
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
}

void
gfc_diagnostics_finish (void)
{
  tree_diagnostics_defaults (global_dc);
  /* We still want to use the gfc starter and finalizer, not the tree
     defaults.  */
  diagnostic_starter (global_dc) = gfc_diagnostic_starter;
  diagnostic_finalizer (global_dc) = gfc_diagnostic_finalizer;
1553 1554
  global_dc->caret_chars[0] = '^';
  global_dc->caret_chars[1] = '^';
1555
}