PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/gcc/fortran/error.c

https://bitbucket.org/vaporoid/gcc
C | 1165 lines | 769 code | 263 blank | 133 comment | 150 complexity | 38ecc4546d797e4b9b77fcb8e0309e71 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, LGPL-2.0, GPL-2.0, LGPL-2.1, AGPL-1.0
  1. /* Handle errors.
  2. Copyright (C) 2000-2013 Free Software Foundation, Inc.
  3. Contributed by Andy Vaught & Niels Kristian Bech Jensen
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. /* Handle the inevitable errors. A major catch here is that things
  17. flagged as errors in one match subroutine can conceivably be legal
  18. elsewhere. This means that error messages are recorded and saved
  19. for possible use later. If a line does not match a legal
  20. construction, then the saved error message is reported. */
  21. #include "config.h"
  22. #include "system.h"
  23. #include "coretypes.h"
  24. #include "flags.h"
  25. #include "gfortran.h"
  26. static int suppress_errors = 0;
  27. static int warnings_not_errors = 0;
  28. static int terminal_width, buffer_flag, errors, warnings;
  29. static gfc_error_buf error_buffer, warning_buffer, *cur_error_buffer;
  30. /* Go one level deeper suppressing errors. */
  31. void
  32. gfc_push_suppress_errors (void)
  33. {
  34. gcc_assert (suppress_errors >= 0);
  35. ++suppress_errors;
  36. }
  37. /* Leave one level of error suppressing. */
  38. void
  39. gfc_pop_suppress_errors (void)
  40. {
  41. gcc_assert (suppress_errors > 0);
  42. --suppress_errors;
  43. }
  44. /* Per-file error initialization. */
  45. void
  46. gfc_error_init_1 (void)
  47. {
  48. terminal_width = gfc_terminal_width ();
  49. errors = 0;
  50. warnings = 0;
  51. buffer_flag = 0;
  52. }
  53. /* Set the flag for buffering errors or not. */
  54. void
  55. gfc_buffer_error (int flag)
  56. {
  57. buffer_flag = flag;
  58. }
  59. /* Add a single character to the error buffer or output depending on
  60. buffer_flag. */
  61. static void
  62. error_char (char c)
  63. {
  64. if (buffer_flag)
  65. {
  66. if (cur_error_buffer->index >= cur_error_buffer->allocated)
  67. {
  68. cur_error_buffer->allocated = cur_error_buffer->allocated
  69. ? cur_error_buffer->allocated * 2 : 1000;
  70. cur_error_buffer->message = XRESIZEVEC (char, cur_error_buffer->message,
  71. cur_error_buffer->allocated);
  72. }
  73. cur_error_buffer->message[cur_error_buffer->index++] = c;
  74. }
  75. else
  76. {
  77. if (c != 0)
  78. {
  79. /* We build up complete lines before handing things
  80. over to the library in order to speed up error printing. */
  81. static char *line;
  82. static size_t allocated = 0, index = 0;
  83. if (index + 1 >= allocated)
  84. {
  85. allocated = allocated ? allocated * 2 : 1000;
  86. line = XRESIZEVEC (char, line, allocated);
  87. }
  88. line[index++] = c;
  89. if (c == '\n')
  90. {
  91. line[index] = '\0';
  92. fputs (line, stderr);
  93. index = 0;
  94. }
  95. }
  96. }
  97. }
  98. /* Copy a string to wherever it needs to go. */
  99. static void
  100. error_string (const char *p)
  101. {
  102. while (*p)
  103. error_char (*p++);
  104. }
  105. /* Print a formatted integer to the error buffer or output. */
  106. #define IBUF_LEN 60
  107. static void
  108. error_uinteger (unsigned long int i)
  109. {
  110. char *p, int_buf[IBUF_LEN];
  111. p = int_buf + IBUF_LEN - 1;
  112. *p-- = '\0';
  113. if (i == 0)
  114. *p-- = '0';
  115. while (i > 0)
  116. {
  117. *p-- = i % 10 + '0';
  118. i = i / 10;
  119. }
  120. error_string (p + 1);
  121. }
  122. static void
  123. error_integer (long int i)
  124. {
  125. unsigned long int u;
  126. if (i < 0)
  127. {
  128. u = (unsigned long int) -i;
  129. error_char ('-');
  130. }
  131. else
  132. u = i;
  133. error_uinteger (u);
  134. }
  135. static size_t
  136. gfc_widechar_display_length (gfc_char_t c)
  137. {
  138. if (gfc_wide_is_printable (c) || c == '\t')
  139. /* Printable ASCII character, or tabulation (output as a space). */
  140. return 1;
  141. else if (c < ((gfc_char_t) 1 << 8))
  142. /* Displayed as \x?? */
  143. return 4;
  144. else if (c < ((gfc_char_t) 1 << 16))
  145. /* Displayed as \u???? */
  146. return 6;
  147. else
  148. /* Displayed as \U???????? */
  149. return 10;
  150. }
  151. /* Length of the ASCII representation of the wide string, escaping wide
  152. characters as print_wide_char_into_buffer() does. */
  153. static size_t
  154. gfc_wide_display_length (const gfc_char_t *str)
  155. {
  156. size_t i, len;
  157. for (i = 0, len = 0; str[i]; i++)
  158. len += gfc_widechar_display_length (str[i]);
  159. return len;
  160. }
  161. static int
  162. print_wide_char_into_buffer (gfc_char_t c, char *buf)
  163. {
  164. static const char xdigit[16] = { '0', '1', '2', '3', '4', '5', '6',
  165. '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  166. if (gfc_wide_is_printable (c) || c == '\t')
  167. {
  168. buf[1] = '\0';
  169. /* Tabulation is output as a space. */
  170. buf[0] = (unsigned char) (c == '\t' ? ' ' : c);
  171. return 1;
  172. }
  173. else if (c < ((gfc_char_t) 1 << 8))
  174. {
  175. buf[4] = '\0';
  176. buf[3] = xdigit[c & 0x0F];
  177. c = c >> 4;
  178. buf[2] = xdigit[c & 0x0F];
  179. buf[1] = 'x';
  180. buf[0] = '\\';
  181. return 4;
  182. }
  183. else if (c < ((gfc_char_t) 1 << 16))
  184. {
  185. buf[6] = '\0';
  186. buf[5] = xdigit[c & 0x0F];
  187. c = c >> 4;
  188. buf[4] = xdigit[c & 0x0F];
  189. c = c >> 4;
  190. buf[3] = xdigit[c & 0x0F];
  191. c = c >> 4;
  192. buf[2] = xdigit[c & 0x0F];
  193. buf[1] = 'u';
  194. buf[0] = '\\';
  195. return 6;
  196. }
  197. else
  198. {
  199. buf[10] = '\0';
  200. buf[9] = xdigit[c & 0x0F];
  201. c = c >> 4;
  202. buf[8] = xdigit[c & 0x0F];
  203. c = c >> 4;
  204. buf[7] = xdigit[c & 0x0F];
  205. c = c >> 4;
  206. buf[6] = xdigit[c & 0x0F];
  207. c = c >> 4;
  208. buf[5] = xdigit[c & 0x0F];
  209. c = c >> 4;
  210. buf[4] = xdigit[c & 0x0F];
  211. c = c >> 4;
  212. buf[3] = xdigit[c & 0x0F];
  213. c = c >> 4;
  214. buf[2] = xdigit[c & 0x0F];
  215. buf[1] = 'U';
  216. buf[0] = '\\';
  217. return 10;
  218. }
  219. }
  220. static char wide_char_print_buffer[11];
  221. const char *
  222. gfc_print_wide_char (gfc_char_t c)
  223. {
  224. print_wide_char_into_buffer (c, wide_char_print_buffer);
  225. return wide_char_print_buffer;
  226. }
  227. /* Show the file, where it was included, and the source line, give a
  228. locus. Calls error_printf() recursively, but the recursion is at
  229. most one level deep. */
  230. static void error_printf (const char *, ...) ATTRIBUTE_GCC_GFC(1,2);
  231. static void
  232. show_locus (locus *loc, int c1, int c2)
  233. {
  234. gfc_linebuf *lb;
  235. gfc_file *f;
  236. gfc_char_t *p;
  237. int i, offset, cmax;
  238. /* TODO: Either limit the total length and number of included files
  239. displayed or add buffering of arbitrary number of characters in
  240. error messages. */
  241. /* Write out the error header line, giving the source file and error
  242. location (in GNU standard "[file]:[line].[column]:" format),
  243. followed by an "included by" stack and a blank line. This header
  244. format is matched by a testsuite parser defined in
  245. lib/gfortran-dg.exp. */
  246. lb = loc->lb;
  247. f = lb->file;
  248. error_string (f->filename);
  249. error_char (':');
  250. error_integer (LOCATION_LINE (lb->location));
  251. if ((c1 > 0) || (c2 > 0))
  252. error_char ('.');
  253. if (c1 > 0)
  254. error_integer (c1);
  255. if ((c1 > 0) && (c2 > 0))
  256. error_char ('-');
  257. if (c2 > 0)
  258. error_integer (c2);
  259. error_char (':');
  260. error_char ('\n');
  261. for (;;)
  262. {
  263. i = f->inclusion_line;
  264. f = f->up;
  265. if (f == NULL) break;
  266. error_printf (" Included at %s:%d:", f->filename, i);
  267. }
  268. error_char ('\n');
  269. /* Calculate an appropriate horizontal offset of the source line in
  270. order to get the error locus within the visible portion of the
  271. line. Note that if the margin of 5 here is changed, the
  272. corresponding margin of 10 in show_loci should be changed. */
  273. offset = 0;
  274. /* If the two loci would appear in the same column, we shift
  275. '2' one column to the right, so as to print '12' rather than
  276. just '1'. We do this here so it will be accounted for in the
  277. margin calculations. */
  278. if (c1 == c2)
  279. c2 += 1;
  280. cmax = (c1 < c2) ? c2 : c1;
  281. if (cmax > terminal_width - 5)
  282. offset = cmax - terminal_width + 5;
  283. /* Show the line itself, taking care not to print more than what can
  284. show up on the terminal. Tabs are converted to spaces, and
  285. nonprintable characters are converted to a "\xNN" sequence. */
  286. p = &(lb->line[offset]);
  287. i = gfc_wide_display_length (p);
  288. if (i > terminal_width)
  289. i = terminal_width - 1;
  290. while (i > 0)
  291. {
  292. static char buffer[11];
  293. i -= print_wide_char_into_buffer (*p++, buffer);
  294. error_string (buffer);
  295. }
  296. error_char ('\n');
  297. /* Show the '1' and/or '2' corresponding to the column of the error
  298. locus. Note that a value of -1 for c1 or c2 will simply cause
  299. the relevant number not to be printed. */
  300. c1 -= offset;
  301. c2 -= offset;
  302. cmax -= offset;
  303. p = &(lb->line[offset]);
  304. for (i = 0; i < cmax; i++)
  305. {
  306. int spaces, j;
  307. spaces = gfc_widechar_display_length (*p++);
  308. if (i == c1)
  309. error_char ('1'), spaces--;
  310. else if (i == c2)
  311. error_char ('2'), spaces--;
  312. for (j = 0; j < spaces; j++)
  313. error_char (' ');
  314. }
  315. if (i == c1)
  316. error_char ('1');
  317. else if (i == c2)
  318. error_char ('2');
  319. error_char ('\n');
  320. }
  321. /* As part of printing an error, we show the source lines that caused
  322. the problem. We show at least one, and possibly two loci; the two
  323. loci may or may not be on the same source line. */
  324. static void
  325. show_loci (locus *l1, locus *l2)
  326. {
  327. int m, c1, c2;
  328. if (l1 == NULL || l1->lb == NULL)
  329. {
  330. error_printf ("<During initialization>\n");
  331. return;
  332. }
  333. /* While calculating parameters for printing the loci, we consider possible
  334. reasons for printing one per line. If appropriate, print the loci
  335. individually; otherwise we print them both on the same line. */
  336. c1 = l1->nextc - l1->lb->line;
  337. if (l2 == NULL)
  338. {
  339. show_locus (l1, c1, -1);
  340. return;
  341. }
  342. c2 = l2->nextc - l2->lb->line;
  343. if (c1 < c2)
  344. m = c2 - c1;
  345. else
  346. m = c1 - c2;
  347. /* Note that the margin value of 10 here needs to be less than the
  348. margin of 5 used in the calculation of offset in show_locus. */
  349. if (l1->lb != l2->lb || m > terminal_width - 10)
  350. {
  351. show_locus (l1, c1, -1);
  352. show_locus (l2, -1, c2);
  353. return;
  354. }
  355. show_locus (l1, c1, c2);
  356. return;
  357. }
  358. /* Workhorse for the error printing subroutines. This subroutine is
  359. inspired by g77's error handling and is similar to printf() with
  360. the following %-codes:
  361. %c Character, %d or %i Integer, %s String, %% Percent
  362. %L Takes locus argument
  363. %C Current locus (no argument)
  364. If a locus pointer is given, the actual source line is printed out
  365. and the column is indicated. Since we want the error message at
  366. the bottom of any source file information, we must scan the
  367. argument list twice -- once to determine whether the loci are
  368. present and record this for printing, and once to print the error
  369. message after and loci have been printed. A maximum of two locus
  370. arguments are permitted.
  371. This function is also called (recursively) by show_locus in the
  372. case of included files; however, as show_locus does not resupply
  373. any loci, the recursion is at most one level deep. */
  374. #define MAX_ARGS 10
  375. static void ATTRIBUTE_GCC_GFC(2,0)
  376. error_print (const char *type, const char *format0, va_list argp)
  377. {
  378. enum { TYPE_CURRENTLOC, TYPE_LOCUS, TYPE_INTEGER, TYPE_UINTEGER,
  379. TYPE_LONGINT, TYPE_ULONGINT, TYPE_CHAR, TYPE_STRING,
  380. NOTYPE };
  381. struct
  382. {
  383. int type;
  384. int pos;
  385. union
  386. {
  387. int intval;
  388. unsigned int uintval;
  389. long int longintval;
  390. unsigned long int ulongintval;
  391. char charval;
  392. const char * stringval;
  393. } u;
  394. } arg[MAX_ARGS], spec[MAX_ARGS];
  395. /* spec is the array of specifiers, in the same order as they
  396. appear in the format string. arg is the array of arguments,
  397. in the same order as they appear in the va_list. */
  398. char c;
  399. int i, n, have_l1, pos, maxpos;
  400. locus *l1, *l2, *loc;
  401. const char *format;
  402. loc = l1 = l2 = NULL;
  403. have_l1 = 0;
  404. pos = -1;
  405. maxpos = -1;
  406. n = 0;
  407. format = format0;
  408. for (i = 0; i < MAX_ARGS; i++)
  409. {
  410. arg[i].type = NOTYPE;
  411. spec[i].pos = -1;
  412. }
  413. /* First parse the format string for position specifiers. */
  414. while (*format)
  415. {
  416. c = *format++;
  417. if (c != '%')
  418. continue;
  419. if (*format == '%')
  420. {
  421. format++;
  422. continue;
  423. }
  424. if (ISDIGIT (*format))
  425. {
  426. /* This is a position specifier. For example, the number
  427. 12 in the format string "%12$d", which specifies the third
  428. argument of the va_list, formatted in %d format.
  429. For details, see "man 3 printf". */
  430. pos = atoi(format) - 1;
  431. gcc_assert (pos >= 0);
  432. while (ISDIGIT(*format))
  433. format++;
  434. gcc_assert (*format == '$');
  435. format++;
  436. }
  437. else
  438. pos++;
  439. c = *format++;
  440. if (pos > maxpos)
  441. maxpos = pos;
  442. switch (c)
  443. {
  444. case 'C':
  445. arg[pos].type = TYPE_CURRENTLOC;
  446. break;
  447. case 'L':
  448. arg[pos].type = TYPE_LOCUS;
  449. break;
  450. case 'd':
  451. case 'i':
  452. arg[pos].type = TYPE_INTEGER;
  453. break;
  454. case 'u':
  455. arg[pos].type = TYPE_UINTEGER;
  456. break;
  457. case 'l':
  458. c = *format++;
  459. if (c == 'u')
  460. arg[pos].type = TYPE_ULONGINT;
  461. else if (c == 'i' || c == 'd')
  462. arg[pos].type = TYPE_LONGINT;
  463. else
  464. gcc_unreachable ();
  465. break;
  466. case 'c':
  467. arg[pos].type = TYPE_CHAR;
  468. break;
  469. case 's':
  470. arg[pos].type = TYPE_STRING;
  471. break;
  472. default:
  473. gcc_unreachable ();
  474. }
  475. spec[n++].pos = pos;
  476. }
  477. /* Then convert the values for each %-style argument. */
  478. for (pos = 0; pos <= maxpos; pos++)
  479. {
  480. gcc_assert (arg[pos].type != NOTYPE);
  481. switch (arg[pos].type)
  482. {
  483. case TYPE_CURRENTLOC:
  484. loc = &gfc_current_locus;
  485. /* Fall through. */
  486. case TYPE_LOCUS:
  487. if (arg[pos].type == TYPE_LOCUS)
  488. loc = va_arg (argp, locus *);
  489. if (have_l1)
  490. {
  491. l2 = loc;
  492. arg[pos].u.stringval = "(2)";
  493. }
  494. else
  495. {
  496. l1 = loc;
  497. have_l1 = 1;
  498. arg[pos].u.stringval = "(1)";
  499. }
  500. break;
  501. case TYPE_INTEGER:
  502. arg[pos].u.intval = va_arg (argp, int);
  503. break;
  504. case TYPE_UINTEGER:
  505. arg[pos].u.uintval = va_arg (argp, unsigned int);
  506. break;
  507. case TYPE_LONGINT:
  508. arg[pos].u.longintval = va_arg (argp, long int);
  509. break;
  510. case TYPE_ULONGINT:
  511. arg[pos].u.ulongintval = va_arg (argp, unsigned long int);
  512. break;
  513. case TYPE_CHAR:
  514. arg[pos].u.charval = (char) va_arg (argp, int);
  515. break;
  516. case TYPE_STRING:
  517. arg[pos].u.stringval = (const char *) va_arg (argp, char *);
  518. break;
  519. default:
  520. gcc_unreachable ();
  521. }
  522. }
  523. for (n = 0; spec[n].pos >= 0; n++)
  524. spec[n].u = arg[spec[n].pos].u;
  525. /* Show the current loci if we have to. */
  526. if (have_l1)
  527. show_loci (l1, l2);
  528. if (*type)
  529. {
  530. error_string (type);
  531. error_char (' ');
  532. }
  533. have_l1 = 0;
  534. format = format0;
  535. n = 0;
  536. for (; *format; format++)
  537. {
  538. if (*format != '%')
  539. {
  540. error_char (*format);
  541. continue;
  542. }
  543. format++;
  544. if (ISDIGIT (*format))
  545. {
  546. /* This is a position specifier. See comment above. */
  547. while (ISDIGIT (*format))
  548. format++;
  549. /* Skip over the dollar sign. */
  550. format++;
  551. }
  552. switch (*format)
  553. {
  554. case '%':
  555. error_char ('%');
  556. break;
  557. case 'c':
  558. error_char (spec[n++].u.charval);
  559. break;
  560. case 's':
  561. case 'C': /* Current locus */
  562. case 'L': /* Specified locus */
  563. error_string (spec[n++].u.stringval);
  564. break;
  565. case 'd':
  566. case 'i':
  567. error_integer (spec[n++].u.intval);
  568. break;
  569. case 'u':
  570. error_uinteger (spec[n++].u.uintval);
  571. break;
  572. case 'l':
  573. format++;
  574. if (*format == 'u')
  575. error_uinteger (spec[n++].u.ulongintval);
  576. else
  577. error_integer (spec[n++].u.longintval);
  578. break;
  579. }
  580. }
  581. error_char ('\n');
  582. }
  583. /* Wrapper for error_print(). */
  584. static void
  585. error_printf (const char *gmsgid, ...)
  586. {
  587. va_list argp;
  588. va_start (argp, gmsgid);
  589. error_print ("", _(gmsgid), argp);
  590. va_end (argp);
  591. }
  592. /* Increment the number of errors, and check whether too many have
  593. been printed. */
  594. static void
  595. gfc_increment_error_count (void)
  596. {
  597. errors++;
  598. if ((gfc_option.max_errors != 0) && (errors >= gfc_option.max_errors))
  599. gfc_fatal_error ("Error count reached limit of %d.", gfc_option.max_errors);
  600. }
  601. /* Issue a warning. */
  602. void
  603. gfc_warning (const char *gmsgid, ...)
  604. {
  605. va_list argp;
  606. if (inhibit_warnings)
  607. return;
  608. warning_buffer.flag = 1;
  609. warning_buffer.index = 0;
  610. cur_error_buffer = &warning_buffer;
  611. va_start (argp, gmsgid);
  612. error_print (_("Warning:"), _(gmsgid), argp);
  613. va_end (argp);
  614. error_char ('\0');
  615. if (buffer_flag == 0)
  616. {
  617. warnings++;
  618. if (warnings_are_errors)
  619. gfc_increment_error_count();
  620. }
  621. }
  622. /* Whether, for a feature included in a given standard set (GFC_STD_*),
  623. we should issue an error or a warning, or be quiet. */
  624. notification
  625. gfc_notification_std (int std)
  626. {
  627. bool warning;
  628. warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
  629. if ((gfc_option.allow_std & std) != 0 && !warning)
  630. return SILENT;
  631. return warning ? WARNING : ERROR;
  632. }
  633. /* Possibly issue a warning/error about use of a nonstandard (or deleted)
  634. feature. An error/warning will be issued if the currently selected
  635. standard does not contain the requested bits. Return FAILURE if
  636. an error is generated. */
  637. gfc_try
  638. gfc_notify_std (int std, const char *gmsgid, ...)
  639. {
  640. va_list argp;
  641. bool warning;
  642. const char *msg1, *msg2;
  643. char *buffer;
  644. warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
  645. if ((gfc_option.allow_std & std) != 0 && !warning)
  646. return SUCCESS;
  647. if (suppress_errors)
  648. return warning ? SUCCESS : FAILURE;
  649. cur_error_buffer = warning ? &warning_buffer : &error_buffer;
  650. cur_error_buffer->flag = 1;
  651. cur_error_buffer->index = 0;
  652. if (warning)
  653. msg1 = _("Warning:");
  654. else
  655. msg1 = _("Error:");
  656. switch (std)
  657. {
  658. case GFC_STD_F2008_TS:
  659. msg2 = "TS 29113:";
  660. break;
  661. case GFC_STD_F2008_OBS:
  662. msg2 = _("Fortran 2008 obsolescent feature:");
  663. break;
  664. case GFC_STD_F2008:
  665. msg2 = "Fortran 2008:";
  666. break;
  667. case GFC_STD_F2003:
  668. msg2 = "Fortran 2003:";
  669. break;
  670. case GFC_STD_GNU:
  671. msg2 = _("GNU Extension:");
  672. break;
  673. case GFC_STD_LEGACY:
  674. msg2 = _("Legacy Extension:");
  675. break;
  676. case GFC_STD_F95_OBS:
  677. msg2 = _("Obsolescent feature:");
  678. break;
  679. case GFC_STD_F95_DEL:
  680. msg2 = _("Deleted feature:");
  681. break;
  682. default:
  683. gcc_unreachable ();
  684. }
  685. buffer = (char *) alloca (strlen (msg1) + strlen (msg2) + 2);
  686. strcpy (buffer, msg1);
  687. strcat (buffer, " ");
  688. strcat (buffer, msg2);
  689. va_start (argp, gmsgid);
  690. error_print (buffer, _(gmsgid), argp);
  691. va_end (argp);
  692. error_char ('\0');
  693. if (buffer_flag == 0)
  694. {
  695. if (warning && !warnings_are_errors)
  696. warnings++;
  697. else
  698. gfc_increment_error_count();
  699. cur_error_buffer->flag = 0;
  700. }
  701. return (warning && !warnings_are_errors) ? SUCCESS : FAILURE;
  702. }
  703. /* Immediate warning (i.e. do not buffer the warning). */
  704. void
  705. gfc_warning_now (const char *gmsgid, ...)
  706. {
  707. va_list argp;
  708. int i;
  709. if (inhibit_warnings)
  710. return;
  711. i = buffer_flag;
  712. buffer_flag = 0;
  713. warnings++;
  714. va_start (argp, gmsgid);
  715. error_print (_("Warning:"), _(gmsgid), argp);
  716. va_end (argp);
  717. error_char ('\0');
  718. if (warnings_are_errors)
  719. gfc_increment_error_count();
  720. buffer_flag = i;
  721. }
  722. /* Clear the warning flag. */
  723. void
  724. gfc_clear_warning (void)
  725. {
  726. warning_buffer.flag = 0;
  727. }
  728. /* Check to see if any warnings have been saved.
  729. If so, print the warning. */
  730. void
  731. gfc_warning_check (void)
  732. {
  733. if (warning_buffer.flag)
  734. {
  735. warnings++;
  736. if (warning_buffer.message != NULL)
  737. fputs (warning_buffer.message, stderr);
  738. warning_buffer.flag = 0;
  739. }
  740. }
  741. /* Issue an error. */
  742. void
  743. gfc_error (const char *gmsgid, ...)
  744. {
  745. va_list argp;
  746. if (warnings_not_errors)
  747. goto warning;
  748. if (suppress_errors)
  749. return;
  750. error_buffer.flag = 1;
  751. error_buffer.index = 0;
  752. cur_error_buffer = &error_buffer;
  753. va_start (argp, gmsgid);
  754. error_print (_("Error:"), _(gmsgid), argp);
  755. va_end (argp);
  756. error_char ('\0');
  757. if (buffer_flag == 0)
  758. gfc_increment_error_count();
  759. return;
  760. warning:
  761. if (inhibit_warnings)
  762. return;
  763. warning_buffer.flag = 1;
  764. warning_buffer.index = 0;
  765. cur_error_buffer = &warning_buffer;
  766. va_start (argp, gmsgid);
  767. error_print (_("Warning:"), _(gmsgid), argp);
  768. va_end (argp);
  769. error_char ('\0');
  770. if (buffer_flag == 0)
  771. {
  772. warnings++;
  773. if (warnings_are_errors)
  774. gfc_increment_error_count();
  775. }
  776. }
  777. /* Immediate error. */
  778. void
  779. gfc_error_now (const char *gmsgid, ...)
  780. {
  781. va_list argp;
  782. int i;
  783. error_buffer.flag = 1;
  784. error_buffer.index = 0;
  785. cur_error_buffer = &error_buffer;
  786. i = buffer_flag;
  787. buffer_flag = 0;
  788. va_start (argp, gmsgid);
  789. error_print (_("Error:"), _(gmsgid), argp);
  790. va_end (argp);
  791. error_char ('\0');
  792. gfc_increment_error_count();
  793. buffer_flag = i;
  794. if (flag_fatal_errors)
  795. exit (FATAL_EXIT_CODE);
  796. }
  797. /* Fatal error, never returns. */
  798. void
  799. gfc_fatal_error (const char *gmsgid, ...)
  800. {
  801. va_list argp;
  802. buffer_flag = 0;
  803. va_start (argp, gmsgid);
  804. error_print (_("Fatal Error:"), _(gmsgid), argp);
  805. va_end (argp);
  806. exit (FATAL_EXIT_CODE);
  807. }
  808. /* This shouldn't happen... but sometimes does. */
  809. void
  810. gfc_internal_error (const char *format, ...)
  811. {
  812. va_list argp;
  813. buffer_flag = 0;
  814. va_start (argp, format);
  815. show_loci (&gfc_current_locus, NULL);
  816. error_printf ("Internal Error at (1):");
  817. error_print ("", format, argp);
  818. va_end (argp);
  819. exit (ICE_EXIT_CODE);
  820. }
  821. /* Clear the error flag when we start to compile a source line. */
  822. void
  823. gfc_clear_error (void)
  824. {
  825. error_buffer.flag = 0;
  826. warnings_not_errors = 0;
  827. }
  828. /* Tests the state of error_flag. */
  829. int
  830. gfc_error_flag_test (void)
  831. {
  832. return error_buffer.flag;
  833. }
  834. /* Check to see if any errors have been saved.
  835. If so, print the error. Returns the state of error_flag. */
  836. int
  837. gfc_error_check (void)
  838. {
  839. int rc;
  840. rc = error_buffer.flag;
  841. if (error_buffer.flag)
  842. {
  843. if (error_buffer.message != NULL)
  844. fputs (error_buffer.message, stderr);
  845. error_buffer.flag = 0;
  846. gfc_increment_error_count();
  847. if (flag_fatal_errors)
  848. exit (FATAL_EXIT_CODE);
  849. }
  850. return rc;
  851. }
  852. /* Save the existing error state. */
  853. void
  854. gfc_push_error (gfc_error_buf *err)
  855. {
  856. err->flag = error_buffer.flag;
  857. if (error_buffer.flag)
  858. err->message = xstrdup (error_buffer.message);
  859. error_buffer.flag = 0;
  860. }
  861. /* Restore a previous pushed error state. */
  862. void
  863. gfc_pop_error (gfc_error_buf *err)
  864. {
  865. error_buffer.flag = err->flag;
  866. if (error_buffer.flag)
  867. {
  868. size_t len = strlen (err->message) + 1;
  869. gcc_assert (len <= error_buffer.allocated);
  870. memcpy (error_buffer.message, err->message, len);
  871. free (err->message);
  872. }
  873. }
  874. /* Free a pushed error state, but keep the current error state. */
  875. void
  876. gfc_free_error (gfc_error_buf *err)
  877. {
  878. if (err->flag)
  879. free (err->message);
  880. }
  881. /* Report the number of warnings and errors that occurred to the caller. */
  882. void
  883. gfc_get_errors (int *w, int *e)
  884. {
  885. if (w != NULL)
  886. *w = warnings;
  887. if (e != NULL)
  888. *e = errors;
  889. }
  890. /* Switch errors into warnings. */
  891. void
  892. gfc_errors_to_warnings (int f)
  893. {
  894. warnings_not_errors = (f == 1) ? 1 : 0;
  895. }