PageRenderTime 21ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

/fortranformat/spec/examples/gcc-4.4.0/gcc/fortran/error.c

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