error.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  1. /* Handle errors.
  2. Copyright (C) 2000-2015 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. #include "diagnostic.h"
  27. #include "diagnostic-color.h"
  28. #include "tree-diagnostic.h" /* tree_diagnostics_defaults */
  29. #include <new> /* For placement-new */
  30. static int suppress_errors = 0;
  31. static bool warnings_not_errors = false;
  32. static int terminal_width, errors, warnings;
  33. static gfc_error_buf error_buffer, warning_buffer, *cur_error_buffer;
  34. /* True if the error/warnings should be buffered. */
  35. static bool buffered_p;
  36. /* These are always buffered buffers (.flush_p == false) to be used by
  37. the pretty-printer. */
  38. static output_buffer *pp_error_buffer, *pp_warning_buffer;
  39. static int warningcount_buffered, werrorcount_buffered;
  40. /* Return true if there output_buffer is empty. */
  41. static bool
  42. gfc_output_buffer_empty_p (const output_buffer * buf)
  43. {
  44. return output_buffer_last_position_in_text (buf) == NULL;
  45. }
  46. /* Go one level deeper suppressing errors. */
  47. void
  48. gfc_push_suppress_errors (void)
  49. {
  50. gcc_assert (suppress_errors >= 0);
  51. ++suppress_errors;
  52. }
  53. static void
  54. gfc_error (const char *gmsgid, va_list ap) ATTRIBUTE_GCC_GFC(1,0);
  55. static bool
  56. gfc_warning (int opt, const char *gmsgid, va_list ap) ATTRIBUTE_GCC_GFC(2,0);
  57. /* Leave one level of error suppressing. */
  58. void
  59. gfc_pop_suppress_errors (void)
  60. {
  61. gcc_assert (suppress_errors > 0);
  62. --suppress_errors;
  63. }
  64. /* Determine terminal width (for trimming source lines in output). */
  65. static int
  66. gfc_get_terminal_width (void)
  67. {
  68. return isatty (STDERR_FILENO) ? get_terminal_width () : INT_MAX;
  69. }
  70. /* Per-file error initialization. */
  71. void
  72. gfc_error_init_1 (void)
  73. {
  74. terminal_width = gfc_get_terminal_width ();
  75. errors = 0;
  76. warnings = 0;
  77. gfc_buffer_error (false);
  78. }
  79. /* Set the flag for buffering errors or not. */
  80. void
  81. gfc_buffer_error (bool flag)
  82. {
  83. buffered_p = flag;
  84. }
  85. /* Add a single character to the error buffer or output depending on
  86. buffered_p. */
  87. static void
  88. error_char (char c)
  89. {
  90. if (buffered_p)
  91. {
  92. if (cur_error_buffer->index >= cur_error_buffer->allocated)
  93. {
  94. cur_error_buffer->allocated = cur_error_buffer->allocated
  95. ? cur_error_buffer->allocated * 2 : 1000;
  96. cur_error_buffer->message = XRESIZEVEC (char, cur_error_buffer->message,
  97. cur_error_buffer->allocated);
  98. }
  99. cur_error_buffer->message[cur_error_buffer->index++] = c;
  100. }
  101. else
  102. {
  103. if (c != 0)
  104. {
  105. /* We build up complete lines before handing things
  106. over to the library in order to speed up error printing. */
  107. static char *line;
  108. static size_t allocated = 0, index = 0;
  109. if (index + 1 >= allocated)
  110. {
  111. allocated = allocated ? allocated * 2 : 1000;
  112. line = XRESIZEVEC (char, line, allocated);
  113. }
  114. line[index++] = c;
  115. if (c == '\n')
  116. {
  117. line[index] = '\0';
  118. fputs (line, stderr);
  119. index = 0;
  120. }
  121. }
  122. }
  123. }
  124. /* Copy a string to wherever it needs to go. */
  125. static void
  126. error_string (const char *p)
  127. {
  128. while (*p)
  129. error_char (*p++);
  130. }
  131. /* Print a formatted integer to the error buffer or output. */
  132. #define IBUF_LEN 60
  133. static void
  134. error_uinteger (unsigned long int i)
  135. {
  136. char *p, int_buf[IBUF_LEN];
  137. p = int_buf + IBUF_LEN - 1;
  138. *p-- = '\0';
  139. if (i == 0)
  140. *p-- = '0';
  141. while (i > 0)
  142. {
  143. *p-- = i % 10 + '0';
  144. i = i / 10;
  145. }
  146. error_string (p + 1);
  147. }
  148. static void
  149. error_integer (long int i)
  150. {
  151. unsigned long int u;
  152. if (i < 0)
  153. {
  154. u = (unsigned long int) -i;
  155. error_char ('-');
  156. }
  157. else
  158. u = i;
  159. error_uinteger (u);
  160. }
  161. static size_t
  162. gfc_widechar_display_length (gfc_char_t c)
  163. {
  164. if (gfc_wide_is_printable (c) || c == '\t')
  165. /* Printable ASCII character, or tabulation (output as a space). */
  166. return 1;
  167. else if (c < ((gfc_char_t) 1 << 8))
  168. /* Displayed as \x?? */
  169. return 4;
  170. else if (c < ((gfc_char_t) 1 << 16))
  171. /* Displayed as \u???? */
  172. return 6;
  173. else
  174. /* Displayed as \U???????? */
  175. return 10;
  176. }
  177. /* Length of the ASCII representation of the wide string, escaping wide
  178. characters as print_wide_char_into_buffer() does. */
  179. static size_t
  180. gfc_wide_display_length (const gfc_char_t *str)
  181. {
  182. size_t i, len;
  183. for (i = 0, len = 0; str[i]; i++)
  184. len += gfc_widechar_display_length (str[i]);
  185. return len;
  186. }
  187. static int
  188. print_wide_char_into_buffer (gfc_char_t c, char *buf)
  189. {
  190. static const char xdigit[16] = { '0', '1', '2', '3', '4', '5', '6',
  191. '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  192. if (gfc_wide_is_printable (c) || c == '\t')
  193. {
  194. buf[1] = '\0';
  195. /* Tabulation is output as a space. */
  196. buf[0] = (unsigned char) (c == '\t' ? ' ' : c);
  197. return 1;
  198. }
  199. else if (c < ((gfc_char_t) 1 << 8))
  200. {
  201. buf[4] = '\0';
  202. buf[3] = xdigit[c & 0x0F];
  203. c = c >> 4;
  204. buf[2] = xdigit[c & 0x0F];
  205. buf[1] = 'x';
  206. buf[0] = '\\';
  207. return 4;
  208. }
  209. else if (c < ((gfc_char_t) 1 << 16))
  210. {
  211. buf[6] = '\0';
  212. buf[5] = xdigit[c & 0x0F];
  213. c = c >> 4;
  214. buf[4] = xdigit[c & 0x0F];
  215. c = c >> 4;
  216. buf[3] = xdigit[c & 0x0F];
  217. c = c >> 4;
  218. buf[2] = xdigit[c & 0x0F];
  219. buf[1] = 'u';
  220. buf[0] = '\\';
  221. return 6;
  222. }
  223. else
  224. {
  225. buf[10] = '\0';
  226. buf[9] = xdigit[c & 0x0F];
  227. c = c >> 4;
  228. buf[8] = xdigit[c & 0x0F];
  229. c = c >> 4;
  230. buf[7] = xdigit[c & 0x0F];
  231. c = c >> 4;
  232. buf[6] = xdigit[c & 0x0F];
  233. c = c >> 4;
  234. 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. buf[1] = 'U';
  242. buf[0] = '\\';
  243. return 10;
  244. }
  245. }
  246. static char wide_char_print_buffer[11];
  247. const char *
  248. gfc_print_wide_char (gfc_char_t c)
  249. {
  250. print_wide_char_into_buffer (c, wide_char_print_buffer);
  251. return wide_char_print_buffer;
  252. }
  253. /* Show the file, where it was included, and the source line, give a
  254. locus. Calls error_printf() recursively, but the recursion is at
  255. most one level deep. */
  256. static void error_printf (const char *, ...) ATTRIBUTE_GCC_GFC(1,2);
  257. static void
  258. show_locus (locus *loc, int c1, int c2)
  259. {
  260. gfc_linebuf *lb;
  261. gfc_file *f;
  262. gfc_char_t *p;
  263. int i, offset, cmax;
  264. /* TODO: Either limit the total length and number of included files
  265. displayed or add buffering of arbitrary number of characters in
  266. error messages. */
  267. /* Write out the error header line, giving the source file and error
  268. location (in GNU standard "[file]:[line].[column]:" format),
  269. followed by an "included by" stack and a blank line. This header
  270. format is matched by a testsuite parser defined in
  271. lib/gfortran-dg.exp. */
  272. lb = loc->lb;
  273. f = lb->file;
  274. error_string (f->filename);
  275. error_char (':');
  276. error_integer (LOCATION_LINE (lb->location));
  277. if ((c1 > 0) || (c2 > 0))
  278. error_char ('.');
  279. if (c1 > 0)
  280. error_integer (c1);
  281. if ((c1 > 0) && (c2 > 0))
  282. error_char ('-');
  283. if (c2 > 0)
  284. error_integer (c2);
  285. error_char (':');
  286. error_char ('\n');
  287. for (;;)
  288. {
  289. i = f->inclusion_line;
  290. f = f->up;
  291. if (f == NULL) break;
  292. error_printf (" Included at %s:%d:", f->filename, i);
  293. }
  294. error_char ('\n');
  295. /* Calculate an appropriate horizontal offset of the source line in
  296. order to get the error locus within the visible portion of the
  297. line. Note that if the margin of 5 here is changed, the
  298. corresponding margin of 10 in show_loci should be changed. */
  299. offset = 0;
  300. /* If the two loci would appear in the same column, we shift
  301. '2' one column to the right, so as to print '12' rather than
  302. just '1'. We do this here so it will be accounted for in the
  303. margin calculations. */
  304. if (c1 == c2)
  305. c2 += 1;
  306. cmax = (c1 < c2) ? c2 : c1;
  307. if (cmax > terminal_width - 5)
  308. offset = cmax - terminal_width + 5;
  309. /* Show the line itself, taking care not to print more than what can
  310. show up on the terminal. Tabs are converted to spaces, and
  311. nonprintable characters are converted to a "\xNN" sequence. */
  312. p = &(lb->line[offset]);
  313. i = gfc_wide_display_length (p);
  314. if (i > terminal_width)
  315. i = terminal_width - 1;
  316. while (i > 0)
  317. {
  318. static char buffer[11];
  319. i -= print_wide_char_into_buffer (*p++, buffer);
  320. error_string (buffer);
  321. }
  322. error_char ('\n');
  323. /* Show the '1' and/or '2' corresponding to the column of the error
  324. locus. Note that a value of -1 for c1 or c2 will simply cause
  325. the relevant number not to be printed. */
  326. c1 -= offset;
  327. c2 -= offset;
  328. cmax -= offset;
  329. p = &(lb->line[offset]);
  330. for (i = 0; i < cmax; i++)
  331. {
  332. int spaces, j;
  333. spaces = gfc_widechar_display_length (*p++);
  334. if (i == c1)
  335. error_char ('1'), spaces--;
  336. else if (i == c2)
  337. error_char ('2'), spaces--;
  338. for (j = 0; j < spaces; j++)
  339. error_char (' ');
  340. }
  341. if (i == c1)
  342. error_char ('1');
  343. else if (i == c2)
  344. error_char ('2');
  345. error_char ('\n');
  346. }
  347. /* As part of printing an error, we show the source lines that caused
  348. the problem. We show at least one, and possibly two loci; the two
  349. loci may or may not be on the same source line. */
  350. static void
  351. show_loci (locus *l1, locus *l2)
  352. {
  353. int m, c1, c2;
  354. if (l1 == NULL || l1->lb == NULL)
  355. {
  356. error_printf ("<During initialization>\n");
  357. return;
  358. }
  359. /* While calculating parameters for printing the loci, we consider possible
  360. reasons for printing one per line. If appropriate, print the loci
  361. individually; otherwise we print them both on the same line. */
  362. c1 = l1->nextc - l1->lb->line;
  363. if (l2 == NULL)
  364. {
  365. show_locus (l1, c1, -1);
  366. return;
  367. }
  368. c2 = l2->nextc - l2->lb->line;
  369. if (c1 < c2)
  370. m = c2 - c1;
  371. else
  372. m = c1 - c2;
  373. /* Note that the margin value of 10 here needs to be less than the
  374. margin of 5 used in the calculation of offset in show_locus. */
  375. if (l1->lb != l2->lb || m > terminal_width - 10)
  376. {
  377. show_locus (l1, c1, -1);
  378. show_locus (l2, -1, c2);
  379. return;
  380. }
  381. show_locus (l1, c1, c2);
  382. return;
  383. }
  384. /* Workhorse for the error printing subroutines. This subroutine is
  385. inspired by g77's error handling and is similar to printf() with
  386. the following %-codes:
  387. %c Character, %d or %i Integer, %s String, %% Percent
  388. %L Takes locus argument
  389. %C Current locus (no argument)
  390. If a locus pointer is given, the actual source line is printed out
  391. and the column is indicated. Since we want the error message at
  392. the bottom of any source file information, we must scan the
  393. argument list twice -- once to determine whether the loci are
  394. present and record this for printing, and once to print the error
  395. message after and loci have been printed. A maximum of two locus
  396. arguments are permitted.
  397. This function is also called (recursively) by show_locus in the
  398. case of included files; however, as show_locus does not resupply
  399. any loci, the recursion is at most one level deep. */
  400. #define MAX_ARGS 10
  401. static void ATTRIBUTE_GCC_GFC(2,0)
  402. error_print (const char *type, const char *format0, va_list argp)
  403. {
  404. enum { TYPE_CURRENTLOC, TYPE_LOCUS, TYPE_INTEGER, TYPE_UINTEGER,
  405. TYPE_LONGINT, TYPE_ULONGINT, TYPE_CHAR, TYPE_STRING,
  406. NOTYPE };
  407. struct
  408. {
  409. int type;
  410. int pos;
  411. union
  412. {
  413. int intval;
  414. unsigned int uintval;
  415. long int longintval;
  416. unsigned long int ulongintval;
  417. char charval;
  418. const char * stringval;
  419. } u;
  420. } arg[MAX_ARGS], spec[MAX_ARGS];
  421. /* spec is the array of specifiers, in the same order as they
  422. appear in the format string. arg is the array of arguments,
  423. in the same order as they appear in the va_list. */
  424. char c;
  425. int i, n, have_l1, pos, maxpos;
  426. locus *l1, *l2, *loc;
  427. const char *format;
  428. loc = l1 = l2 = NULL;
  429. have_l1 = 0;
  430. pos = -1;
  431. maxpos = -1;
  432. n = 0;
  433. format = format0;
  434. for (i = 0; i < MAX_ARGS; i++)
  435. {
  436. arg[i].type = NOTYPE;
  437. spec[i].pos = -1;
  438. }
  439. /* First parse the format string for position specifiers. */
  440. while (*format)
  441. {
  442. c = *format++;
  443. if (c != '%')
  444. continue;
  445. if (*format == '%')
  446. {
  447. format++;
  448. continue;
  449. }
  450. if (ISDIGIT (*format))
  451. {
  452. /* This is a position specifier. For example, the number
  453. 12 in the format string "%12$d", which specifies the third
  454. argument of the va_list, formatted in %d format.
  455. For details, see "man 3 printf". */
  456. pos = atoi(format) - 1;
  457. gcc_assert (pos >= 0);
  458. while (ISDIGIT(*format))
  459. format++;
  460. gcc_assert (*format == '$');
  461. format++;
  462. }
  463. else
  464. pos++;
  465. c = *format++;
  466. if (pos > maxpos)
  467. maxpos = pos;
  468. switch (c)
  469. {
  470. case 'C':
  471. arg[pos].type = TYPE_CURRENTLOC;
  472. break;
  473. case 'L':
  474. arg[pos].type = TYPE_LOCUS;
  475. break;
  476. case 'd':
  477. case 'i':
  478. arg[pos].type = TYPE_INTEGER;
  479. break;
  480. case 'u':
  481. arg[pos].type = TYPE_UINTEGER;
  482. break;
  483. case 'l':
  484. c = *format++;
  485. if (c == 'u')
  486. arg[pos].type = TYPE_ULONGINT;
  487. else if (c == 'i' || c == 'd')
  488. arg[pos].type = TYPE_LONGINT;
  489. else
  490. gcc_unreachable ();
  491. break;
  492. case 'c':
  493. arg[pos].type = TYPE_CHAR;
  494. break;
  495. case 's':
  496. arg[pos].type = TYPE_STRING;
  497. break;
  498. default:
  499. gcc_unreachable ();
  500. }
  501. spec[n++].pos = pos;
  502. }
  503. /* Then convert the values for each %-style argument. */
  504. for (pos = 0; pos <= maxpos; pos++)
  505. {
  506. gcc_assert (arg[pos].type != NOTYPE);
  507. switch (arg[pos].type)
  508. {
  509. case TYPE_CURRENTLOC:
  510. loc = &gfc_current_locus;
  511. /* Fall through. */
  512. case TYPE_LOCUS:
  513. if (arg[pos].type == TYPE_LOCUS)
  514. loc = va_arg (argp, locus *);
  515. if (have_l1)
  516. {
  517. l2 = loc;
  518. arg[pos].u.stringval = "(2)";
  519. }
  520. else
  521. {
  522. l1 = loc;
  523. have_l1 = 1;
  524. arg[pos].u.stringval = "(1)";
  525. }
  526. break;
  527. case TYPE_INTEGER:
  528. arg[pos].u.intval = va_arg (argp, int);
  529. break;
  530. case TYPE_UINTEGER:
  531. arg[pos].u.uintval = va_arg (argp, unsigned int);
  532. break;
  533. case TYPE_LONGINT:
  534. arg[pos].u.longintval = va_arg (argp, long int);
  535. break;
  536. case TYPE_ULONGINT:
  537. arg[pos].u.ulongintval = va_arg (argp, unsigned long int);
  538. break;
  539. case TYPE_CHAR:
  540. arg[pos].u.charval = (char) va_arg (argp, int);
  541. break;
  542. case TYPE_STRING:
  543. arg[pos].u.stringval = (const char *) va_arg (argp, char *);
  544. break;
  545. default:
  546. gcc_unreachable ();
  547. }
  548. }
  549. for (n = 0; spec[n].pos >= 0; n++)
  550. spec[n].u = arg[spec[n].pos].u;
  551. /* Show the current loci if we have to. */
  552. if (have_l1)
  553. show_loci (l1, l2);
  554. if (*type)
  555. {
  556. error_string (type);
  557. error_char (' ');
  558. }
  559. have_l1 = 0;
  560. format = format0;
  561. n = 0;
  562. for (; *format; format++)
  563. {
  564. if (*format != '%')
  565. {
  566. error_char (*format);
  567. continue;
  568. }
  569. format++;
  570. if (ISDIGIT (*format))
  571. {
  572. /* This is a position specifier. See comment above. */
  573. while (ISDIGIT (*format))
  574. format++;
  575. /* Skip over the dollar sign. */
  576. format++;
  577. }
  578. switch (*format)
  579. {
  580. case '%':
  581. error_char ('%');
  582. break;
  583. case 'c':
  584. error_char (spec[n++].u.charval);
  585. break;
  586. case 's':
  587. case 'C': /* Current locus */
  588. case 'L': /* Specified locus */
  589. error_string (spec[n++].u.stringval);
  590. break;
  591. case 'd':
  592. case 'i':
  593. error_integer (spec[n++].u.intval);
  594. break;
  595. case 'u':
  596. error_uinteger (spec[n++].u.uintval);
  597. break;
  598. case 'l':
  599. format++;
  600. if (*format == 'u')
  601. error_uinteger (spec[n++].u.ulongintval);
  602. else
  603. error_integer (spec[n++].u.longintval);
  604. break;
  605. }
  606. }
  607. error_char ('\n');
  608. }
  609. /* Wrapper for error_print(). */
  610. static void
  611. error_printf (const char *gmsgid, ...)
  612. {
  613. va_list argp;
  614. va_start (argp, gmsgid);
  615. error_print ("", _(gmsgid), argp);
  616. va_end (argp);
  617. }
  618. /* Increment the number of errors, and check whether too many have
  619. been printed. */
  620. static void
  621. gfc_increment_error_count (void)
  622. {
  623. errors++;
  624. if ((gfc_option.max_errors != 0) && (errors >= gfc_option.max_errors))
  625. gfc_fatal_error ("Error count reached limit of %d.", gfc_option.max_errors);
  626. }
  627. /* Clear any output buffered in a pretty-print output_buffer. */
  628. static void
  629. gfc_clear_pp_buffer (output_buffer *this_buffer)
  630. {
  631. pretty_printer *pp = global_dc->printer;
  632. output_buffer *tmp_buffer = pp->buffer;
  633. pp->buffer = this_buffer;
  634. pp_clear_output_area (pp);
  635. pp->buffer = tmp_buffer;
  636. }
  637. /* Issue a warning. */
  638. /* Use gfc_warning instead, unless two locations are used in the same
  639. warning or for scanner.c, if the location is not properly set up. */
  640. void
  641. gfc_warning_1 (const char *gmsgid, ...)
  642. {
  643. va_list argp;
  644. if (inhibit_warnings)
  645. return;
  646. warning_buffer.flag = 1;
  647. warning_buffer.index = 0;
  648. cur_error_buffer = &warning_buffer;
  649. va_start (argp, gmsgid);
  650. error_print (_("Warning:"), _(gmsgid), argp);
  651. va_end (argp);
  652. error_char ('\0');
  653. if (!buffered_p)
  654. {
  655. warnings++;
  656. if (warnings_are_errors)
  657. gfc_increment_error_count();
  658. }
  659. }
  660. /* This is just a helper function to avoid duplicating the logic of
  661. gfc_warning. */
  662. static bool
  663. gfc_warning (int opt, const char *gmsgid, va_list ap)
  664. {
  665. va_list argp;
  666. va_copy (argp, ap);
  667. diagnostic_info diagnostic;
  668. bool fatal_errors = global_dc->fatal_errors;
  669. pretty_printer *pp = global_dc->printer;
  670. output_buffer *tmp_buffer = pp->buffer;
  671. gfc_clear_pp_buffer (pp_warning_buffer);
  672. if (buffered_p)
  673. {
  674. pp->buffer = pp_warning_buffer;
  675. global_dc->fatal_errors = false;
  676. /* To prevent -fmax-errors= triggering. */
  677. --werrorcount;
  678. }
  679. diagnostic_set_info (&diagnostic, gmsgid, &argp, UNKNOWN_LOCATION,
  680. DK_WARNING);
  681. diagnostic.option_index = opt;
  682. bool ret = report_diagnostic (&diagnostic);
  683. if (buffered_p)
  684. {
  685. pp->buffer = tmp_buffer;
  686. global_dc->fatal_errors = fatal_errors;
  687. warningcount_buffered = 0;
  688. werrorcount_buffered = 0;
  689. /* Undo the above --werrorcount if not Werror, otherwise
  690. werrorcount is correct already. */
  691. if (!ret)
  692. ++werrorcount;
  693. else if (diagnostic.kind == DK_ERROR)
  694. ++werrorcount_buffered;
  695. else
  696. ++werrorcount, --warningcount, ++warningcount_buffered;
  697. }
  698. va_end (argp);
  699. return ret;
  700. }
  701. /* Issue a warning. */
  702. /* This function uses the common diagnostics, but does not support
  703. two locations; when being used in scanner.c, ensure that the location
  704. is properly setup. Otherwise, use gfc_warning_1. */
  705. bool
  706. gfc_warning (int opt, const char *gmsgid, ...)
  707. {
  708. va_list argp;
  709. va_start (argp, gmsgid);
  710. bool ret = gfc_warning (opt, gmsgid, argp);
  711. va_end (argp);
  712. return ret;
  713. }
  714. /* Whether, for a feature included in a given standard set (GFC_STD_*),
  715. we should issue an error or a warning, or be quiet. */
  716. notification
  717. gfc_notification_std (int std)
  718. {
  719. bool warning;
  720. warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
  721. if ((gfc_option.allow_std & std) != 0 && !warning)
  722. return SILENT;
  723. return warning ? WARNING : ERROR;
  724. }
  725. /* Possibly issue a warning/error about use of a nonstandard (or deleted)
  726. feature. An error/warning will be issued if the currently selected
  727. standard does not contain the requested bits. Return false if
  728. an error is generated. */
  729. bool
  730. gfc_notify_std_1 (int std, const char *gmsgid, ...)
  731. {
  732. va_list argp;
  733. bool warning;
  734. const char *msg1, *msg2;
  735. char *buffer;
  736. warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
  737. if ((gfc_option.allow_std & std) != 0 && !warning)
  738. return true;
  739. if (suppress_errors)
  740. return warning ? true : false;
  741. cur_error_buffer = warning ? &warning_buffer : &error_buffer;
  742. cur_error_buffer->flag = 1;
  743. cur_error_buffer->index = 0;
  744. if (warning)
  745. msg1 = _("Warning:");
  746. else
  747. msg1 = _("Error:");
  748. switch (std)
  749. {
  750. case GFC_STD_F2008_TS:
  751. msg2 = "TS 29113/TS 18508:";
  752. break;
  753. case GFC_STD_F2008_OBS:
  754. msg2 = _("Fortran 2008 obsolescent feature:");
  755. break;
  756. case GFC_STD_F2008:
  757. msg2 = "Fortran 2008:";
  758. break;
  759. case GFC_STD_F2003:
  760. msg2 = "Fortran 2003:";
  761. break;
  762. case GFC_STD_GNU:
  763. msg2 = _("GNU Extension:");
  764. break;
  765. case GFC_STD_LEGACY:
  766. msg2 = _("Legacy Extension:");
  767. break;
  768. case GFC_STD_F95_OBS:
  769. msg2 = _("Obsolescent feature:");
  770. break;
  771. case GFC_STD_F95_DEL:
  772. msg2 = _("Deleted feature:");
  773. break;
  774. default:
  775. gcc_unreachable ();
  776. }
  777. buffer = (char *) alloca (strlen (msg1) + strlen (msg2) + 2);
  778. strcpy (buffer, msg1);
  779. strcat (buffer, " ");
  780. strcat (buffer, msg2);
  781. va_start (argp, gmsgid);
  782. error_print (buffer, _(gmsgid), argp);
  783. va_end (argp);
  784. error_char ('\0');
  785. if (!buffered_p)
  786. {
  787. if (warning && !warnings_are_errors)
  788. warnings++;
  789. else
  790. gfc_increment_error_count();
  791. cur_error_buffer->flag = 0;
  792. }
  793. return (warning && !warnings_are_errors) ? true : false;
  794. }
  795. bool
  796. gfc_notify_std (int std, const char *gmsgid, ...)
  797. {
  798. va_list argp;
  799. bool warning;
  800. const char *msg, *msg2;
  801. char *buffer;
  802. warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
  803. if ((gfc_option.allow_std & std) != 0 && !warning)
  804. return true;
  805. if (suppress_errors)
  806. return warning ? true : false;
  807. switch (std)
  808. {
  809. case GFC_STD_F2008_TS:
  810. msg = "TS 29113/TS 18508:";
  811. break;
  812. case GFC_STD_F2008_OBS:
  813. msg = _("Fortran 2008 obsolescent feature:");
  814. break;
  815. case GFC_STD_F2008:
  816. msg = "Fortran 2008:";
  817. break;
  818. case GFC_STD_F2003:
  819. msg = "Fortran 2003:";
  820. break;
  821. case GFC_STD_GNU:
  822. msg = _("GNU Extension:");
  823. break;
  824. case GFC_STD_LEGACY:
  825. msg = _("Legacy Extension:");
  826. break;
  827. case GFC_STD_F95_OBS:
  828. msg = _("Obsolescent feature:");
  829. break;
  830. case GFC_STD_F95_DEL:
  831. msg = _("Deleted feature:");
  832. break;
  833. default:
  834. gcc_unreachable ();
  835. }
  836. msg2 = _(gmsgid);
  837. buffer = (char *) alloca (strlen (msg) + strlen (msg2) + 2);
  838. strcpy (buffer, msg);
  839. strcat (buffer, " ");
  840. strcat (buffer, msg2);
  841. va_start (argp, gmsgid);
  842. if (warning)
  843. gfc_warning (0, buffer, argp);
  844. else
  845. gfc_error (buffer, argp);
  846. va_end (argp);
  847. return (warning && !warnings_are_errors) ? true : false;
  848. }
  849. /* Immediate warning (i.e. do not buffer the warning). */
  850. /* Use gfc_warning_now instead, unless two locations are used in the same
  851. warning or for scanner.c, if the location is not properly set up. */
  852. void
  853. gfc_warning_now_1 (const char *gmsgid, ...)
  854. {
  855. va_list argp;
  856. bool buffered_p_saved;
  857. if (inhibit_warnings)
  858. return;
  859. buffered_p_saved = buffered_p;
  860. buffered_p = false;
  861. warnings++;
  862. va_start (argp, gmsgid);
  863. error_print (_("Warning:"), _(gmsgid), argp);
  864. va_end (argp);
  865. error_char ('\0');
  866. if (warnings_are_errors)
  867. gfc_increment_error_count();
  868. buffered_p = buffered_p_saved;
  869. }
  870. /* Called from output_format -- during diagnostic message processing
  871. to handle Fortran specific format specifiers with the following meanings:
  872. %C Current locus (no argument)
  873. %L Takes locus argument
  874. */
  875. static bool
  876. gfc_format_decoder (pretty_printer *pp,
  877. text_info *text, const char *spec,
  878. int precision ATTRIBUTE_UNUSED, bool wide ATTRIBUTE_UNUSED,
  879. bool plus ATTRIBUTE_UNUSED, bool hash ATTRIBUTE_UNUSED)
  880. {
  881. switch (*spec)
  882. {
  883. case 'C':
  884. case 'L':
  885. {
  886. static const char *result = "(1)";
  887. locus *loc;
  888. if (*spec == 'C')
  889. loc = &gfc_current_locus;
  890. else
  891. loc = va_arg (*text->args_ptr, locus *);
  892. gcc_assert (loc->nextc - loc->lb->line >= 0);
  893. unsigned int offset = loc->nextc - loc->lb->line;
  894. gcc_assert (text->locus);
  895. *text->locus
  896. = linemap_position_for_loc_and_offset (line_table,
  897. loc->lb->location,
  898. offset);
  899. global_dc->caret_char = '1';
  900. pp_string (pp, result);
  901. return true;
  902. }
  903. default:
  904. return false;
  905. }
  906. }
  907. /* Return a malloc'd string describing a location. The caller is
  908. responsible for freeing the memory. */
  909. static char *
  910. gfc_diagnostic_build_prefix (diagnostic_context *context,
  911. const diagnostic_info *diagnostic)
  912. {
  913. static const char *const diagnostic_kind_text[] = {
  914. #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
  915. #include "gfc-diagnostic.def"
  916. #undef DEFINE_DIAGNOSTIC_KIND
  917. "must-not-happen"
  918. };
  919. static const char *const diagnostic_kind_color[] = {
  920. #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
  921. #include "gfc-diagnostic.def"
  922. #undef DEFINE_DIAGNOSTIC_KIND
  923. NULL
  924. };
  925. gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
  926. const char *text = _(diagnostic_kind_text[diagnostic->kind]);
  927. const char *text_cs = "", *text_ce = "";
  928. pretty_printer *pp = context->printer;
  929. if (diagnostic_kind_color[diagnostic->kind])
  930. {
  931. text_cs = colorize_start (pp_show_color (pp),
  932. diagnostic_kind_color[diagnostic->kind]);
  933. text_ce = colorize_stop (pp_show_color (pp));
  934. }
  935. return build_message_string ("%s%s:%s ", text_cs, text, text_ce);
  936. }
  937. /* Return a malloc'd string describing a location. The caller is
  938. responsible for freeing the memory. */
  939. static char *
  940. gfc_diagnostic_build_locus_prefix (diagnostic_context *context,
  941. const diagnostic_info *diagnostic)
  942. {
  943. pretty_printer *pp = context->printer;
  944. const char *locus_cs = colorize_start (pp_show_color (pp), "locus");
  945. const char *locus_ce = colorize_stop (pp_show_color (pp));
  946. expanded_location s = diagnostic_expand_location (diagnostic);
  947. return (s.file == NULL
  948. ? build_message_string ("%s%s:%s", locus_cs, progname, locus_ce )
  949. : !strcmp (s.file, N_("<built-in>"))
  950. ? build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce)
  951. : context->show_column
  952. ? build_message_string ("%s%s:%d:%d:%s", locus_cs, s.file, s.line,
  953. s.column, locus_ce)
  954. : build_message_string ("%s%s:%d:%s", locus_cs, s.file, s.line, locus_ce));
  955. }
  956. static void
  957. gfc_diagnostic_starter (diagnostic_context *context,
  958. diagnostic_info *diagnostic)
  959. {
  960. char * locus_prefix = gfc_diagnostic_build_locus_prefix (context, diagnostic);
  961. char * prefix = gfc_diagnostic_build_prefix (context, diagnostic);
  962. /* First we assume there is a caret line. */
  963. pp_set_prefix (context->printer, NULL);
  964. if (pp_needs_newline (context->printer))
  965. pp_newline (context->printer);
  966. pp_verbatim (context->printer, locus_prefix);
  967. /* Fortran uses an empty line between locus and caret line. */
  968. pp_newline (context->printer);
  969. diagnostic_show_locus (context, diagnostic);
  970. if (pp_needs_newline (context->printer))
  971. {
  972. pp_newline (context->printer);
  973. /* If the caret line was shown, the prefix does not contain the
  974. locus. */
  975. pp_set_prefix (context->printer, prefix);
  976. }
  977. else
  978. {
  979. /* Otherwise, start again. */
  980. pp_clear_output_area(context->printer);
  981. pp_set_prefix (context->printer, concat (locus_prefix, " ", prefix, NULL));
  982. free (prefix);
  983. }
  984. free (locus_prefix);
  985. }
  986. static void
  987. gfc_diagnostic_finalizer (diagnostic_context *context,
  988. diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
  989. {
  990. pp_destroy_prefix (context->printer);
  991. pp_newline_and_flush (context->printer);
  992. }
  993. /* Immediate warning (i.e. do not buffer the warning). */
  994. /* This function uses the common diagnostics, but does not support
  995. two locations; when being used in scanner.c, ensure that the location
  996. is properly setup. Otherwise, use gfc_warning_now_1. */
  997. bool
  998. gfc_warning_now (int opt, const char *gmsgid, ...)
  999. {
  1000. va_list argp;
  1001. diagnostic_info diagnostic;
  1002. bool ret;
  1003. va_start (argp, gmsgid);
  1004. diagnostic_set_info (&diagnostic, gmsgid, &argp, UNKNOWN_LOCATION,
  1005. DK_WARNING);
  1006. diagnostic.option_index = opt;
  1007. ret = report_diagnostic (&diagnostic);
  1008. va_end (argp);
  1009. return ret;
  1010. }
  1011. /* Immediate error (i.e. do not buffer). */
  1012. /* This function uses the common diagnostics, but does not support
  1013. two locations; when being used in scanner.c, ensure that the location
  1014. is properly setup. Otherwise, use gfc_error_now_1. */
  1015. void
  1016. gfc_error_now (const char *gmsgid, ...)
  1017. {
  1018. va_list argp;
  1019. diagnostic_info diagnostic;
  1020. va_start (argp, gmsgid);
  1021. diagnostic_set_info (&diagnostic, gmsgid, &argp, UNKNOWN_LOCATION, DK_ERROR);
  1022. report_diagnostic (&diagnostic);
  1023. va_end (argp);
  1024. }
  1025. /* Fatal error, never returns. */
  1026. void
  1027. gfc_fatal_error (const char *gmsgid, ...)
  1028. {
  1029. va_list argp;
  1030. diagnostic_info diagnostic;
  1031. va_start (argp, gmsgid);
  1032. diagnostic_set_info (&diagnostic, gmsgid, &argp, UNKNOWN_LOCATION, DK_FATAL);
  1033. report_diagnostic (&diagnostic);
  1034. va_end (argp);
  1035. gcc_unreachable ();
  1036. }
  1037. /* Clear the warning flag. */
  1038. void
  1039. gfc_clear_warning (void)
  1040. {
  1041. warning_buffer.flag = 0;
  1042. gfc_clear_pp_buffer (pp_warning_buffer);
  1043. warningcount_buffered = 0;
  1044. werrorcount_buffered = 0;
  1045. }
  1046. /* Check to see if any warnings have been saved.
  1047. If so, print the warning. */
  1048. void
  1049. gfc_warning_check (void)
  1050. {
  1051. if (warning_buffer.flag)
  1052. {
  1053. warnings++;
  1054. if (warning_buffer.message != NULL)
  1055. fputs (warning_buffer.message, stderr);
  1056. gfc_clear_warning ();
  1057. }
  1058. /* This is for the new diagnostics machinery. */
  1059. else if (! gfc_output_buffer_empty_p (pp_warning_buffer))
  1060. {
  1061. pretty_printer *pp = global_dc->printer;
  1062. output_buffer *tmp_buffer = pp->buffer;
  1063. pp->buffer = pp_warning_buffer;
  1064. pp_really_flush (pp);
  1065. warningcount += warningcount_buffered;
  1066. werrorcount += werrorcount_buffered;
  1067. gcc_assert (warningcount_buffered + werrorcount_buffered == 1);
  1068. diagnostic_action_after_output (global_dc,
  1069. warningcount_buffered
  1070. ? DK_WARNING : DK_ERROR);
  1071. pp->buffer = tmp_buffer;
  1072. }
  1073. }
  1074. /* Issue an error. */
  1075. /* Use gfc_error instead, unless two locations are used in the same
  1076. warning or for scanner.c, if the location is not properly set up. */
  1077. void
  1078. gfc_error_1 (const char *gmsgid, ...)
  1079. {
  1080. va_list argp;
  1081. if (warnings_not_errors)
  1082. goto warning;
  1083. if (suppress_errors)
  1084. return;
  1085. error_buffer.flag = 1;
  1086. error_buffer.index = 0;
  1087. cur_error_buffer = &error_buffer;
  1088. va_start (argp, gmsgid);
  1089. error_print (_("Error:"), _(gmsgid), argp);
  1090. va_end (argp);
  1091. error_char ('\0');
  1092. if (!buffered_p)
  1093. gfc_increment_error_count();
  1094. return;
  1095. warning:
  1096. if (inhibit_warnings)
  1097. return;
  1098. warning_buffer.flag = 1;
  1099. warning_buffer.index = 0;
  1100. cur_error_buffer = &warning_buffer;
  1101. va_start (argp, gmsgid);
  1102. error_print (_("Warning:"), _(gmsgid), argp);
  1103. va_end (argp);
  1104. error_char ('\0');
  1105. if (!buffered_p)
  1106. {
  1107. warnings++;
  1108. if (warnings_are_errors)
  1109. gfc_increment_error_count();
  1110. }
  1111. }
  1112. /* Issue an error. */
  1113. /* This function uses the common diagnostics, but does not support
  1114. two locations; when being used in scanner.c, ensure that the location
  1115. is properly setup. Otherwise, use gfc_error_1. */
  1116. static void
  1117. gfc_error (const char *gmsgid, va_list ap)
  1118. {
  1119. va_list argp;
  1120. va_copy (argp, ap);
  1121. if (warnings_not_errors)
  1122. {
  1123. gfc_warning (/*opt=*/0, gmsgid, argp);
  1124. va_end (argp);
  1125. return;
  1126. }
  1127. if (suppress_errors)
  1128. {
  1129. va_end (argp);
  1130. return;
  1131. }
  1132. diagnostic_info diagnostic;
  1133. bool fatal_errors = global_dc->fatal_errors;
  1134. pretty_printer *pp = global_dc->printer;
  1135. output_buffer *tmp_buffer = pp->buffer;
  1136. gfc_clear_pp_buffer (pp_error_buffer);
  1137. if (buffered_p)
  1138. {
  1139. pp->buffer = pp_error_buffer;
  1140. global_dc->fatal_errors = false;
  1141. /* To prevent -fmax-errors= triggering, we decrease it before
  1142. report_diagnostic increases it. */
  1143. --errorcount;
  1144. }
  1145. diagnostic_set_info (&diagnostic, gmsgid, &argp, UNKNOWN_LOCATION, DK_ERROR);
  1146. report_diagnostic (&diagnostic);
  1147. if (buffered_p)
  1148. {
  1149. pp->buffer = tmp_buffer;
  1150. global_dc->fatal_errors = fatal_errors;
  1151. }
  1152. va_end (argp);
  1153. }
  1154. void
  1155. gfc_error (const char *gmsgid, ...)
  1156. {
  1157. va_list argp;
  1158. va_start (argp, gmsgid);
  1159. gfc_error (gmsgid, argp);
  1160. va_end (argp);
  1161. }
  1162. /* Immediate error. */
  1163. /* Use gfc_error_now instead, unless two locations are used in the same
  1164. warning or for scanner.c, if the location is not properly set up. */
  1165. void
  1166. gfc_error_now_1 (const char *gmsgid, ...)
  1167. {
  1168. va_list argp;
  1169. bool buffered_p_saved;
  1170. error_buffer.flag = 1;
  1171. error_buffer.index = 0;
  1172. cur_error_buffer = &error_buffer;
  1173. buffered_p_saved = buffered_p;
  1174. buffered_p = false;
  1175. va_start (argp, gmsgid);
  1176. error_print (_("Error:"), _(gmsgid), argp);
  1177. va_end (argp);
  1178. error_char ('\0');
  1179. gfc_increment_error_count();
  1180. buffered_p = buffered_p_saved;
  1181. if (flag_fatal_errors)
  1182. exit (FATAL_EXIT_CODE);
  1183. }
  1184. /* This shouldn't happen... but sometimes does. */
  1185. void
  1186. gfc_internal_error (const char *gmsgid, ...)
  1187. {
  1188. va_list argp;
  1189. diagnostic_info diagnostic;
  1190. va_start (argp, gmsgid);
  1191. diagnostic_set_info (&diagnostic, gmsgid, &argp, UNKNOWN_LOCATION, DK_ICE);
  1192. report_diagnostic (&diagnostic);
  1193. va_end (argp);
  1194. gcc_unreachable ();
  1195. }
  1196. /* Clear the error flag when we start to compile a source line. */
  1197. void
  1198. gfc_clear_error (void)
  1199. {
  1200. error_buffer.flag = 0;
  1201. warnings_not_errors = false;
  1202. gfc_clear_pp_buffer (pp_error_buffer);
  1203. }
  1204. /* Tests the state of error_flag. */
  1205. bool
  1206. gfc_error_flag_test (void)
  1207. {
  1208. return error_buffer.flag
  1209. || !gfc_output_buffer_empty_p (pp_error_buffer);
  1210. }
  1211. /* Check to see if any errors have been saved.
  1212. If so, print the error. Returns the state of error_flag. */
  1213. bool
  1214. gfc_error_check (void)
  1215. {
  1216. bool error_raised = (bool) error_buffer.flag;
  1217. if (error_raised)
  1218. {
  1219. if (error_buffer.message != NULL)
  1220. fputs (error_buffer.message, stderr);
  1221. error_buffer.flag = 0;
  1222. gfc_clear_pp_buffer (pp_error_buffer);
  1223. gfc_increment_error_count();
  1224. if (flag_fatal_errors)
  1225. exit (FATAL_EXIT_CODE);
  1226. }
  1227. /* This is for the new diagnostics machinery. */
  1228. else if (! gfc_output_buffer_empty_p (pp_error_buffer))
  1229. {
  1230. error_raised = true;
  1231. pretty_printer *pp = global_dc->printer;
  1232. output_buffer *tmp_buffer = pp->buffer;
  1233. pp->buffer = pp_error_buffer;
  1234. pp_really_flush (pp);
  1235. ++errorcount;
  1236. gcc_assert (gfc_output_buffer_empty_p (pp_error_buffer));
  1237. diagnostic_action_after_output (global_dc, DK_ERROR);
  1238. pp->buffer = tmp_buffer;
  1239. }
  1240. return error_raised;
  1241. }
  1242. /* Move the text buffered from FROM to TO, then clear
  1243. FROM. Independently if there was text in FROM, TO is also
  1244. cleared. */
  1245. static void
  1246. gfc_move_output_buffer_from_to (output_buffer *from, output_buffer *to)
  1247. {
  1248. gfc_clear_pp_buffer (to);
  1249. /* We make sure this is always buffered. */
  1250. to->flush_p = false;
  1251. if (! gfc_output_buffer_empty_p (from))
  1252. {
  1253. const char *str = output_buffer_formatted_text (from);
  1254. output_buffer_append_r (to, str, strlen (str));
  1255. gfc_clear_pp_buffer (from);
  1256. }
  1257. }
  1258. /* Save the existing error state. */
  1259. void
  1260. gfc_push_error (output_buffer *buffer_err, gfc_error_buf *err)
  1261. {
  1262. err->flag = error_buffer.flag;
  1263. if (error_buffer.flag)
  1264. err->message = xstrdup (error_buffer.message);
  1265. error_buffer.flag = 0;
  1266. /* This part uses the common diagnostics. */
  1267. gfc_move_output_buffer_from_to (pp_error_buffer, buffer_err);
  1268. }
  1269. /* Restore a previous pushed error state. */
  1270. void
  1271. gfc_pop_error (output_buffer *buffer_err, gfc_error_buf *err)
  1272. {
  1273. error_buffer.flag = err->flag;
  1274. if (error_buffer.flag)
  1275. {
  1276. size_t len = strlen (err->message) + 1;
  1277. gcc_assert (len <= error_buffer.allocated);
  1278. memcpy (error_buffer.message, err->message, len);
  1279. free (err->message);
  1280. }
  1281. /* This part uses the common diagnostics. */
  1282. gfc_move_output_buffer_from_to (buffer_err, pp_error_buffer);
  1283. }
  1284. /* Free a pushed error state, but keep the current error state. */
  1285. void
  1286. gfc_free_error (output_buffer *buffer_err, gfc_error_buf *err)
  1287. {
  1288. if (err->flag)
  1289. free (err->message);
  1290. gfc_clear_pp_buffer (buffer_err);
  1291. }
  1292. /* Report the number of warnings and errors that occurred to the caller. */
  1293. void
  1294. gfc_get_errors (int *w, int *e)
  1295. {
  1296. if (w != NULL)
  1297. *w = warnings + warningcount + werrorcount;
  1298. if (e != NULL)
  1299. *e = errors + errorcount + sorrycount + werrorcount;
  1300. }
  1301. /* Switch errors into warnings. */
  1302. void
  1303. gfc_errors_to_warnings (bool f)
  1304. {
  1305. warnings_not_errors = f;
  1306. }
  1307. void
  1308. gfc_diagnostics_init (void)
  1309. {
  1310. diagnostic_starter (global_dc) = gfc_diagnostic_starter;
  1311. diagnostic_finalizer (global_dc) = gfc_diagnostic_finalizer;
  1312. diagnostic_format_decoder (global_dc) = gfc_format_decoder;
  1313. global_dc->caret_char = '^';
  1314. pp_warning_buffer = new (XNEW (output_buffer)) output_buffer ();
  1315. pp_warning_buffer->flush_p = false;
  1316. pp_error_buffer = new (XNEW (output_buffer)) output_buffer ();
  1317. pp_error_buffer->flush_p = false;
  1318. }
  1319. void
  1320. gfc_diagnostics_finish (void)
  1321. {
  1322. tree_diagnostics_defaults (global_dc);
  1323. /* We still want to use the gfc starter and finalizer, not the tree
  1324. defaults. */
  1325. diagnostic_starter (global_dc) = gfc_diagnostic_starter;
  1326. diagnostic_finalizer (global_dc) = gfc_diagnostic_finalizer;
  1327. global_dc->caret_char = '^';
  1328. }