PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/gcc-4.2.1/gcc/fortran/error.c

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