Line data Source code
1 : /* Character scanner.
2 : Copyright (C) 2000-2026 Free Software Foundation, Inc.
3 : Contributed by Andy Vaught
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it under
8 : the terms of the GNU General Public License as published by the Free
9 : Software Foundation; either version 3, or (at your option) any later
10 : version.
11 :
12 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : /* Set of subroutines to (ultimately) return the next character to the
22 : various matching subroutines. This file's job is to read files and
23 : build up lines that are parsed by the parser. This means that we
24 : handle continuation lines and "include" lines.
25 :
26 : The first thing the scanner does is to load an entire file into
27 : memory. We load the entire file into memory for a couple reasons.
28 : The first is that we want to be able to deal with nonseekable input
29 : (pipes, stdin) and there is a lot of backing up involved during
30 : parsing.
31 :
32 : The second is that we want to be able to print the locus of errors,
33 : and an error on line 999999 could conflict with something on line
34 : one. Given nonseekable input, we've got to store the whole thing.
35 :
36 : One thing that helps are the column truncation limits that give us
37 : an upper bound on the size of individual lines. We don't store the
38 : truncated stuff.
39 :
40 : From the scanner's viewpoint, the higher level subroutines ask for
41 : new characters and do a lot of jumping backwards. */
42 :
43 : #include "config.h"
44 : #include "system.h"
45 : #include "coretypes.h"
46 : #include "gfortran.h"
47 : #include "toplev.h" /* For set_src_pwd. */
48 : #include "debug.h"
49 : #include "options.h"
50 : #include "diagnostic-core.h" /* For fatal_error. */
51 : #include "cpp.h"
52 : #include "scanner.h"
53 :
54 : /* List of include file search directories. */
55 : gfc_directorylist *include_dirs, *intrinsic_modules_dirs;
56 :
57 : static gfc_file *file_head, *current_file;
58 :
59 : static bool continue_flag, end_flag, gcc_attribute_flag;
60 : /* If !$omp/!$acc occurred in current comment line. */
61 : static int openmp_flag, openacc_flag;
62 : static int continue_count, continue_line;
63 : static locus openmp_locus;
64 : static locus openacc_locus;
65 : static locus gcc_attribute_locus;
66 :
67 : gfc_source_form gfc_current_form;
68 : static gfc_linebuf *line_head, *line_tail;
69 :
70 : locus gfc_current_locus;
71 : const char *gfc_source_file;
72 : static FILE *gfc_src_file;
73 : static gfc_char_t *gfc_src_preprocessor_lines[2];
74 :
75 : static struct gfc_file_change
76 : {
77 : const char *filename;
78 : gfc_linebuf *lb;
79 : int line;
80 : } *file_changes;
81 : static size_t file_changes_cur, file_changes_count;
82 : static size_t file_changes_allocated;
83 :
84 : static gfc_char_t *last_error_char;
85 :
86 : /* Functions dealing with our wide characters (gfc_char_t) and
87 : sequences of such characters. */
88 :
89 : bool
90 1401670414 : gfc_wide_fits_in_byte (gfc_char_t c)
91 : {
92 1401670414 : return (c <= UCHAR_MAX);
93 : }
94 :
95 : static inline int
96 709983152 : wide_is_ascii (gfc_char_t c)
97 : {
98 709983152 : return (gfc_wide_fits_in_byte (c) && ((unsigned char) c & ~0x7f) == 0);
99 : }
100 :
101 : bool
102 30969 : gfc_wide_is_printable (gfc_char_t c)
103 : {
104 30969 : return (gfc_wide_fits_in_byte (c) && ISPRINT ((unsigned char) c));
105 : }
106 :
107 : gfc_char_t
108 709843045 : gfc_wide_tolower (gfc_char_t c)
109 : {
110 709843045 : return (wide_is_ascii (c) ? (gfc_char_t) TOLOWER((unsigned char) c) : c);
111 : }
112 :
113 : gfc_char_t
114 140107 : gfc_wide_toupper (gfc_char_t c)
115 : {
116 140107 : return (wide_is_ascii (c) ? (gfc_char_t) TOUPPER((unsigned char) c) : c);
117 : }
118 :
119 : bool
120 12941761 : gfc_wide_is_digit (gfc_char_t c)
121 : {
122 12941761 : return (c >= '0' && c <= '9');
123 : }
124 :
125 : static inline int
126 13388 : wide_atoi (gfc_char_t *c)
127 : {
128 : #define MAX_DIGITS 20
129 13388 : char buf[MAX_DIGITS+1];
130 13388 : int i = 0;
131 :
132 28966 : while (gfc_wide_is_digit(*c) && i < MAX_DIGITS)
133 15578 : buf[i++] = *c++;
134 13388 : buf[i] = '\0';
135 13388 : return atoi (buf);
136 : }
137 :
138 : size_t
139 7069052 : gfc_wide_strlen (const gfc_char_t *str)
140 : {
141 7069052 : size_t i;
142 :
143 317031454 : for (i = 0; str[i]; i++)
144 : ;
145 :
146 7069052 : return i;
147 : }
148 :
149 : gfc_char_t *
150 358267 : gfc_wide_memset (gfc_char_t *b, gfc_char_t c, size_t len)
151 : {
152 358267 : size_t i;
153 :
154 3174518 : for (i = 0; i < len; i++)
155 2816251 : b[i] = c;
156 :
157 358267 : return b;
158 : }
159 :
160 : static gfc_char_t *
161 6976485 : wide_strcpy (gfc_char_t *dest, const gfc_char_t *src)
162 : {
163 6976485 : gfc_char_t *d;
164 :
165 315948968 : for (d = dest; (*d = *src) != '\0'; ++src, ++d)
166 : ;
167 :
168 6976485 : return dest;
169 : }
170 :
171 : static gfc_char_t *
172 0 : wide_strchr (const gfc_char_t *s, gfc_char_t c)
173 : {
174 38075 : do {
175 38075 : if (*s == c)
176 : return const_cast<gfc_char_t *> (s);
177 24687 : } while (*s++);
178 : return 0;
179 : }
180 :
181 : char *
182 10813 : gfc_widechar_to_char (const gfc_char_t *s, int length)
183 : {
184 10813 : size_t len, i;
185 10813 : char *res;
186 :
187 10813 : if (s == NULL)
188 : return NULL;
189 :
190 : /* Passing a negative length is used to indicate that length should be
191 : calculated using gfc_wide_strlen(). */
192 10813 : len = (length >= 0 ? (size_t) length : gfc_wide_strlen (s));
193 10813 : res = XNEWVEC (char, len + 1);
194 :
195 467919 : for (i = 0; i < len; i++)
196 : {
197 446293 : gcc_assert (gfc_wide_fits_in_byte (s[i]));
198 446293 : res[i] = (unsigned char) s[i];
199 : }
200 :
201 10813 : res[len] = '\0';
202 10813 : return res;
203 : }
204 :
205 : gfc_char_t *
206 3966 : gfc_char_to_widechar (const char *s, gfc_charlen_t length)
207 : {
208 3966 : size_t len, i;
209 3966 : gfc_char_t *res;
210 :
211 3966 : if (s == NULL)
212 : return NULL;
213 :
214 : /* Passing a negative length is used to indicate that length should be
215 : calculated using strlen(). */
216 3966 : len = (length >= 0 ? (size_t) length : strlen (s));
217 3966 : res = gfc_get_wide_string (len + 1);
218 :
219 54144 : for (i = 0; i < len; i++)
220 46212 : res[i] = (unsigned char) s[i];
221 :
222 3966 : res[len] = '\0';
223 3966 : return res;
224 : }
225 :
226 : static int
227 87 : wide_strncmp (const gfc_char_t *s1, const char *s2, size_t n)
228 : {
229 87 : gfc_char_t c1, c2;
230 :
231 320 : while (n-- > 0)
232 : {
233 294 : c1 = *s1++;
234 294 : c2 = *s2++;
235 294 : if (c1 != c2)
236 61 : return (c1 > c2 ? 1 : -1);
237 233 : if (c1 == '\0')
238 : return 0;
239 : }
240 : return 0;
241 : }
242 :
243 : int
244 6635916 : gfc_wide_strncasecmp (const gfc_char_t *s1, const char *s2, size_t n)
245 : {
246 6635916 : gfc_char_t c1, c2;
247 :
248 7181370 : while (n-- > 0)
249 : {
250 7171954 : c1 = gfc_wide_tolower (*s1++);
251 7171954 : c2 = TOLOWER (*s2++);
252 7171954 : if (c1 != c2)
253 6626500 : return (c1 > c2 ? 1 : -1);
254 545454 : if (c1 == '\0')
255 : return 0;
256 : }
257 : return 0;
258 : }
259 :
260 :
261 : /* Main scanner initialization. */
262 :
263 : void
264 32690 : gfc_scanner_init_1 (void)
265 : {
266 32690 : file_head = NULL;
267 32690 : line_head = NULL;
268 32690 : line_tail = NULL;
269 :
270 32690 : continue_count = 0;
271 32690 : continue_line = 0;
272 :
273 32690 : end_flag = 0;
274 32690 : last_error_char = NULL;
275 32690 : }
276 :
277 :
278 : /* Main scanner destructor. */
279 :
280 : void
281 32671 : gfc_scanner_done_1 (void)
282 : {
283 32671 : gfc_linebuf *lb;
284 32671 : gfc_file *f;
285 :
286 7006475 : while(line_head != NULL)
287 : {
288 6973804 : lb = line_head->next;
289 6973804 : free (line_head);
290 6973804 : line_head = lb;
291 : }
292 :
293 100489 : while(file_head != NULL)
294 : {
295 67818 : f = file_head->next;
296 67818 : free (file_head->filename);
297 67818 : free (file_head);
298 67818 : file_head = f;
299 : }
300 32671 : }
301 :
302 : static bool
303 128290 : gfc_do_check_include_dir (const char *path, bool warn)
304 : {
305 128290 : struct stat st;
306 128290 : if (stat (path, &st))
307 : {
308 65355 : if (errno != ENOENT)
309 0 : gfc_warning_now (0, "Include directory %qs: %s",
310 : path, xstrerror(errno));
311 65355 : else if (warn)
312 14 : gfc_warning_now (OPT_Wmissing_include_dirs,
313 : "Nonexistent include directory %qs", path);
314 : return false;
315 : }
316 62935 : else if (!S_ISDIR (st.st_mode))
317 : {
318 1 : gfc_fatal_error ("%qs is not a directory", path);
319 : return false;
320 : }
321 : return true;
322 : }
323 :
324 : /* In order that -W(no-)missing-include-dirs works, the diagnostic can only be
325 : run after processing the commandline. */
326 : static void
327 65384 : gfc_do_check_include_dirs (gfc_directorylist **list, bool do_warn)
328 : {
329 65384 : gfc_directorylist *prev, *q, *n;
330 65384 : prev = NULL;
331 65384 : n = *list;
332 124439 : while (n)
333 : {
334 59056 : q = n; n = n->next;
335 99473 : if (gfc_do_check_include_dir (q->path, q->warn && do_warn))
336 : {
337 26372 : prev = q;
338 26372 : continue;
339 : }
340 32683 : if (prev == NULL)
341 27950 : *list = n;
342 : else
343 4733 : prev->next = n;
344 32683 : free (q->path);
345 32683 : free (q);
346 : }
347 65383 : }
348 :
349 : void
350 32691 : gfc_check_include_dirs (bool verbose_missing_dir_warn)
351 : {
352 : /* This is a bit convoluted: If gfc_cpp_enabled () and
353 : verbose_missing_dir_warn, the warning is shown by libcpp. Otherwise,
354 : it is shown here, still conditional on OPT_Wmissing_include_dirs. */
355 32691 : bool warn = !gfc_cpp_enabled () || !verbose_missing_dir_warn;
356 32691 : gfc_do_check_include_dirs (&include_dirs, warn);
357 32690 : gfc_do_check_include_dirs (&intrinsic_modules_dirs, verbose_missing_dir_warn);
358 32690 : if (gfc_option.module_dir && gfc_cpp_enabled ())
359 3 : gfc_do_check_include_dirs (&include_dirs, true);
360 32690 : }
361 :
362 : /* Adds path to the list pointed to by list. */
363 :
364 : static void
365 124417 : add_path_to_list (gfc_directorylist **list, const char *path,
366 : bool use_for_modules, bool head, bool warn, bool defer_warn)
367 : {
368 124417 : gfc_directorylist *dir;
369 124417 : const char *p;
370 124417 : char *q;
371 124417 : size_t len;
372 124417 : int i;
373 :
374 124417 : p = path;
375 124417 : while (*p == ' ' || *p == '\t') /* someone might do "-I include" */
376 0 : if (*p++ == '\0')
377 : return;
378 :
379 : /* Strip trailing directory separators from the path, as this
380 : will confuse Windows systems. */
381 124417 : len = strlen (p);
382 124417 : q = (char *) alloca (len + 1);
383 124417 : memcpy (q, p, len + 1);
384 124417 : i = len - 1;
385 124936 : while (i >=0 && IS_DIR_SEPARATOR (q[i]))
386 519 : q[i--] = '\0';
387 :
388 124417 : if (!defer_warn && !gfc_do_check_include_dir (q, warn))
389 : return;
390 :
391 91745 : if (head || *list == NULL)
392 : {
393 69232 : dir = XCNEW (gfc_directorylist);
394 69232 : if (!head)
395 36544 : *list = dir;
396 : }
397 : else
398 : {
399 : dir = *list;
400 82269 : while (dir->next)
401 : dir = dir->next;
402 :
403 22513 : dir->next = XCNEW (gfc_directorylist);
404 22513 : dir = dir->next;
405 : }
406 :
407 59057 : dir->next = head ? *list : NULL;
408 59057 : if (head)
409 32688 : *list = dir;
410 91745 : dir->use_for_modules = use_for_modules;
411 91745 : dir->warn = warn;
412 91745 : dir->path = xstrdup (p);
413 : }
414 :
415 : /* defer_warn is set to true while parsing the commandline. */
416 :
417 : void
418 87873 : gfc_add_include_path (const char *path, bool use_for_modules, bool file_dir,
419 : bool warn, bool defer_warn)
420 : {
421 87873 : add_path_to_list (&include_dirs, path, use_for_modules, file_dir, warn,
422 : defer_warn);
423 :
424 : /* For '#include "..."' these directories are automatically searched. */
425 87873 : if (!file_dir)
426 55183 : gfc_cpp_add_include_path (xstrdup(path), true);
427 87873 : }
428 :
429 :
430 : void
431 36544 : gfc_add_intrinsic_modules_path (const char *path)
432 : {
433 36544 : add_path_to_list (&intrinsic_modules_dirs, path, true, false, false, false);
434 36544 : }
435 :
436 :
437 : /* Release resources allocated for options. */
438 :
439 : void
440 32671 : gfc_release_include_path (void)
441 : {
442 32671 : gfc_directorylist *p;
443 :
444 87834 : while (include_dirs != NULL)
445 : {
446 55163 : p = include_dirs;
447 55163 : include_dirs = include_dirs->next;
448 55163 : free (p->path);
449 55163 : free (p);
450 : }
451 :
452 36544 : while (intrinsic_modules_dirs != NULL)
453 : {
454 3873 : p = intrinsic_modules_dirs;
455 3873 : intrinsic_modules_dirs = intrinsic_modules_dirs->next;
456 3873 : free (p->path);
457 3873 : free (p);
458 : }
459 :
460 32671 : free (gfc_option.module_dir);
461 32671 : }
462 :
463 :
464 : static FILE *
465 375 : open_included_file (const char *name, gfc_directorylist *list,
466 : bool module, bool system)
467 : {
468 375 : char *fullname;
469 375 : gfc_directorylist *p;
470 375 : FILE *f;
471 :
472 641 : for (p = list; p; p = p->next)
473 : {
474 639 : if (module && !p->use_for_modules)
475 0 : continue;
476 :
477 639 : fullname = (char *) alloca(strlen (p->path) + strlen (name) + 2);
478 639 : strcpy (fullname, p->path);
479 639 : strcat (fullname, "/");
480 639 : strcat (fullname, name);
481 :
482 639 : f = gfc_open_file (fullname);
483 639 : if (f != NULL)
484 : {
485 373 : if (gfc_cpp_makedep ())
486 0 : gfc_cpp_add_dep (fullname, system);
487 :
488 : return f;
489 : }
490 : }
491 :
492 : return NULL;
493 : }
494 :
495 :
496 : /* Opens file for reading, searching through the include directories
497 : given if necessary. If the include_cwd argument is true, we try
498 : to open the file in the current directory first. */
499 :
500 : FILE *
501 33044 : gfc_open_included_file (const char *name, bool include_cwd, bool module)
502 : {
503 33044 : FILE *f = NULL;
504 :
505 33044 : if (IS_ABSOLUTE_PATH (name) || include_cwd)
506 : {
507 32670 : f = gfc_open_file (name);
508 32670 : if (f && gfc_cpp_makedep ())
509 0 : gfc_cpp_add_dep (name, false);
510 : }
511 :
512 32670 : if (!f)
513 375 : f = open_included_file (name, include_dirs, module, false);
514 :
515 33044 : return f;
516 : }
517 :
518 :
519 : /* Test to see if we're at the end of the main source file. */
520 :
521 : bool
522 1256867633 : gfc_at_end (void)
523 : {
524 1256867633 : return end_flag;
525 : }
526 :
527 :
528 : /* Test to see if we're at the end of the current file. */
529 :
530 : bool
531 33111665 : gfc_at_eof (void)
532 : {
533 33111665 : if (gfc_at_end ())
534 : return 1;
535 :
536 32833463 : if (line_head == NULL)
537 : return 1; /* Null file */
538 :
539 32833463 : if (gfc_current_locus.u.lb == NULL)
540 0 : return 1;
541 :
542 : return 0;
543 : }
544 :
545 :
546 : /* Test to see if we're at the beginning of a new line. */
547 :
548 : bool
549 15648774 : gfc_at_bol (void)
550 : {
551 15648774 : if (gfc_at_eof ())
552 : return 1;
553 :
554 15525800 : return (gfc_current_locus.nextc == gfc_current_locus.u.lb->line);
555 : }
556 :
557 :
558 : /* Test to see if we're at the end of a line. */
559 :
560 : bool
561 5029010 : gfc_at_eol (void)
562 : {
563 5029010 : if (gfc_at_eof ())
564 : return 1;
565 :
566 5029006 : return (*gfc_current_locus.nextc == '\0');
567 : }
568 :
569 : static void
570 70356 : add_file_change (const char *filename, int line)
571 : {
572 70356 : if (file_changes_count == file_changes_allocated)
573 : {
574 32675 : if (file_changes_allocated)
575 1 : file_changes_allocated *= 2;
576 : else
577 : file_changes_allocated = 16;
578 32675 : file_changes = XRESIZEVEC (struct gfc_file_change, file_changes,
579 : file_changes_allocated);
580 : }
581 70356 : file_changes[file_changes_count].filename = filename;
582 70356 : file_changes[file_changes_count].lb = NULL;
583 70356 : file_changes[file_changes_count++].line = line;
584 70356 : }
585 :
586 : static void
587 7008661 : report_file_change (gfc_linebuf *lb)
588 : {
589 7008661 : size_t c = file_changes_cur;
590 7008661 : while (c < file_changes_count
591 7079011 : && file_changes[c].lb == lb)
592 : {
593 70350 : if (file_changes[c].filename)
594 35175 : (*debug_hooks->start_source_file) (file_changes[c].line,
595 : file_changes[c].filename);
596 : else
597 35175 : (*debug_hooks->end_source_file) (file_changes[c].line);
598 70350 : ++c;
599 : }
600 7008661 : file_changes_cur = c;
601 7008661 : }
602 :
603 : void
604 32673 : gfc_start_source_files (void)
605 : {
606 : /* If the debugger wants the name of the main source file,
607 : we give it. */
608 32673 : if (debug_hooks->start_end_main_source_file)
609 5330 : (*debug_hooks->start_source_file) (0, gfc_source_file);
610 :
611 32673 : file_changes_cur = 0;
612 32673 : report_file_change (gfc_current_locus.u.lb);
613 32673 : }
614 :
615 : void
616 32627 : gfc_end_source_files (void)
617 : {
618 32627 : report_file_change (NULL);
619 :
620 32627 : if (debug_hooks->start_end_main_source_file)
621 5330 : (*debug_hooks->end_source_file) (0);
622 32627 : }
623 :
624 : /* Advance the current line pointer to the next line. */
625 :
626 : void
627 11983217 : gfc_advance_line (void)
628 : {
629 11983217 : if (gfc_at_end ())
630 : return;
631 :
632 11983207 : if (gfc_current_locus.u.lb == NULL)
633 : {
634 0 : end_flag = 1;
635 0 : return;
636 : }
637 :
638 11983207 : if (gfc_current_locus.u.lb->next
639 11828034 : && !gfc_current_locus.u.lb->next->dbg_emitted)
640 : {
641 6943361 : report_file_change (gfc_current_locus.u.lb->next);
642 6943361 : gfc_current_locus.u.lb->next->dbg_emitted = true;
643 : }
644 :
645 11983207 : gfc_current_locus.u.lb = gfc_current_locus.u.lb->next;
646 :
647 11983207 : if (gfc_current_locus.u.lb != NULL)
648 11828034 : gfc_current_locus.nextc = gfc_current_locus.u.lb->line;
649 : else
650 : {
651 155173 : gfc_current_locus.nextc = NULL;
652 155173 : end_flag = 1;
653 : }
654 : }
655 :
656 :
657 : /* Get the next character from the input, advancing gfc_current_file's
658 : locus. When we hit the end of the line or the end of the file, we
659 : start returning a '\n' in order to complete the current statement.
660 : No Fortran line conventions are implemented here.
661 :
662 : Requiring explicit advances to the next line prevents the parse
663 : pointer from being on the wrong line if the current statement ends
664 : prematurely. */
665 :
666 : static gfc_char_t
667 1450159822 : next_char (void)
668 : {
669 1450159822 : gfc_char_t c;
670 :
671 1450159822 : if (gfc_current_locus.nextc == NULL)
672 : return '\n';
673 :
674 1450037353 : c = *gfc_current_locus.nextc++;
675 1450037353 : if (c == '\0')
676 : {
677 44665169 : gfc_current_locus.nextc--; /* Remain on this line. */
678 44665169 : c = '\n';
679 : }
680 :
681 : return c;
682 : }
683 :
684 :
685 : /* Skip a comment. When we come here the parse pointer is positioned
686 : immediately after the comment character. If we ever implement
687 : compiler directives within comments, here is where we parse the
688 : directive. */
689 :
690 : static void
691 1459337 : skip_comment_line (void)
692 : {
693 86328625 : gfc_char_t c;
694 :
695 86328625 : do
696 : {
697 86328625 : c = next_char ();
698 : }
699 86328625 : while (c != '\n');
700 :
701 1459337 : gfc_advance_line ();
702 1459337 : }
703 :
704 :
705 : bool
706 4996346 : gfc_define_undef_line (void)
707 : {
708 4996346 : char *tmp;
709 :
710 : /* All lines beginning with '#' are either #define or #undef. */
711 4996346 : if (debug_info_level != DINFO_LEVEL_VERBOSE || gfc_peek_ascii_char () != '#')
712 : return 0;
713 :
714 8 : if (wide_strncmp (gfc_current_locus.nextc, "#define ", 8) == 0)
715 : {
716 5 : tmp = gfc_widechar_to_char (&gfc_current_locus.nextc[8], -1);
717 5 : (*debug_hooks->define) (gfc_linebuf_linenum (gfc_current_locus.u.lb),
718 : tmp);
719 5 : free (tmp);
720 : }
721 :
722 8 : if (wide_strncmp (gfc_current_locus.nextc, "#undef ", 7) == 0)
723 : {
724 3 : tmp = gfc_widechar_to_char (&gfc_current_locus.nextc[7], -1);
725 3 : (*debug_hooks->undef) (gfc_linebuf_linenum (gfc_current_locus.u.lb),
726 : tmp);
727 3 : free (tmp);
728 : }
729 :
730 : /* Skip the rest of the line. */
731 8 : skip_comment_line ();
732 :
733 8 : return 1;
734 : }
735 :
736 :
737 : /* Return true if GCC$ was matched. */
738 : static bool
739 5317766 : skip_gcc_attribute (locus start)
740 : {
741 5317766 : bool r = false;
742 5317766 : char c;
743 5317766 : locus old_loc = gfc_current_locus;
744 :
745 5317766 : if ((c = next_char ()) == 'g' || c == 'G')
746 3771424 : if ((c = next_char ()) == 'c' || c == 'C')
747 3769352 : if ((c = next_char ()) == 'c' || c == 'C')
748 3769352 : if ((c = next_char ()) == '$')
749 3769352 : r = true;
750 :
751 3769352 : if (r == false)
752 1548414 : gfc_current_locus = old_loc;
753 : else
754 : {
755 3769352 : gcc_attribute_flag = 1;
756 3769352 : gcc_attribute_locus = old_loc;
757 3769352 : gfc_current_locus = start;
758 : }
759 :
760 5317766 : return r;
761 : }
762 :
763 : /* Return true if CC was matched. */
764 : static bool
765 20716 : skip_free_oacc_sentinel (locus start, locus old_loc)
766 : {
767 20716 : bool r = false;
768 20716 : char c;
769 :
770 20716 : if ((c = next_char ()) == 'c' || c == 'C')
771 20716 : if ((c = next_char ()) == 'c' || c == 'C')
772 20716 : r = true;
773 :
774 20716 : if (r)
775 : {
776 20728 : if ((c = next_char ()) == ' ' || c == '\t'
777 20728 : || continue_flag)
778 : {
779 20715 : if (!continue_flag && (c == ' ' || c == '\t'))
780 20232 : openmp_flag = 0;
781 41617 : while (gfc_is_whitespace (c))
782 20902 : c = next_char ();
783 20715 : if (c != '\n' && c != '!')
784 : {
785 20714 : openacc_flag = 1;
786 20714 : openacc_locus = old_loc;
787 20714 : gfc_current_locus = start;
788 : }
789 : else
790 : r = false;
791 : }
792 : else
793 : {
794 1 : gfc_warning_now (0, "!$ACC at %C starts a commented "
795 : "line as it neither is followed "
796 : "by a space nor is a "
797 : "continuation line");
798 1 : r = false;
799 : }
800 : }
801 :
802 20716 : return r;
803 : }
804 :
805 : /* Return true if MP was matched. */
806 : static bool
807 40920 : skip_free_omp_sentinel (locus start, locus old_loc)
808 : {
809 40920 : bool r = false;
810 40920 : char c;
811 :
812 40920 : if ((c = next_char ()) == 'm' || c == 'M')
813 40919 : if ((c = next_char ()) == 'p' || c == 'P')
814 40919 : r = true;
815 :
816 40919 : if (r)
817 : {
818 43102 : if ((c = next_char ()) == ' ' || c == '\t'
819 43101 : || continue_flag)
820 : {
821 40915 : if (!continue_flag && (c == ' ' || c == '\t'))
822 33933 : openacc_flag = 0;
823 82852 : while (gfc_is_whitespace (c))
824 41937 : c = next_char ();
825 40915 : if (c != '\n' && c != '!')
826 : {
827 40915 : openmp_flag = 1;
828 40915 : openmp_locus = old_loc;
829 40915 : gfc_current_locus = start;
830 : }
831 : else
832 : r = false;
833 : }
834 : else
835 : {
836 4 : gfc_warning_now (0, "!$OMP at %C starts a commented "
837 : "line as it neither is followed "
838 : "by a space nor is a "
839 : "continuation line");
840 4 : r = false;
841 : }
842 : }
843 :
844 40920 : return r;
845 : }
846 :
847 : /* Comment lines are null lines, lines containing only blanks or lines
848 : on which the first nonblank line is a '!'.
849 : Return true if !$ openmp or openacc conditional compilation sentinel was
850 : seen. */
851 :
852 : static bool
853 5015928 : skip_free_comments (void)
854 : {
855 6933354 : locus start;
856 6933354 : gfc_char_t c;
857 6933354 : int at_bol;
858 :
859 6933354 : for (;;)
860 : {
861 6933354 : at_bol = gfc_at_bol ();
862 6933354 : start = gfc_current_locus;
863 6933354 : if (gfc_at_eof ())
864 : break;
865 :
866 14885580 : do
867 14885580 : c = next_char ();
868 14885580 : while (gfc_is_whitespace (c));
869 :
870 6902445 : if (c == '\n')
871 : {
872 1176504 : gfc_advance_line ();
873 1176504 : continue;
874 : }
875 :
876 5725941 : if (c == '!')
877 : {
878 : /* Keep the !GCC$ line. */
879 4140872 : if (at_bol && skip_gcc_attribute (start))
880 : return false;
881 :
882 : /* If -fopenmp/-fopenacc, we need to handle here 2 things:
883 : 1) don't treat !$omp/!$acc as comments, but directives
884 : 2) handle OpenMP conditional compilation, where
885 : !$ should be treated as 2 spaces (for initial lines
886 : only if followed by space). */
887 803086 : if (at_bol)
888 : {
889 803015 : if ((flag_openmp || flag_openmp_simd)
890 112968 : && flag_openacc)
891 : {
892 635 : locus old_loc = gfc_current_locus;
893 635 : if (next_char () == '$')
894 : {
895 427 : c = next_char ();
896 427 : if (c == 'o' || c == 'O')
897 : {
898 111 : if (skip_free_omp_sentinel (start, old_loc))
899 423 : return false;
900 0 : gfc_current_locus = old_loc;
901 0 : next_char ();
902 0 : c = next_char ();
903 : }
904 316 : else if (c == 'a' || c == 'A')
905 : {
906 196 : if (skip_free_oacc_sentinel (start, old_loc))
907 : return false;
908 0 : gfc_current_locus = old_loc;
909 0 : next_char ();
910 0 : c = next_char ();
911 : }
912 120 : if (continue_flag || c == ' ' || c == '\t')
913 : {
914 116 : gfc_current_locus = old_loc;
915 116 : next_char ();
916 116 : openmp_flag = openacc_flag = 0;
917 116 : return true;
918 : }
919 : }
920 212 : gfc_current_locus = old_loc;
921 212 : }
922 802380 : else if ((flag_openmp || flag_openmp_simd)
923 112333 : && !flag_openacc)
924 : {
925 112333 : locus old_loc = gfc_current_locus;
926 112333 : if (next_char () == '$')
927 : {
928 41278 : c = next_char ();
929 41278 : if (c == 'o' || c == 'O')
930 : {
931 40809 : if (skip_free_omp_sentinel (start, old_loc))
932 41223 : return false;
933 5 : gfc_current_locus = old_loc;
934 5 : next_char ();
935 5 : c = next_char ();
936 : }
937 474 : if (continue_flag || c == ' ' || c == '\t')
938 : {
939 419 : gfc_current_locus = old_loc;
940 419 : next_char ();
941 419 : openmp_flag = 0;
942 419 : return true;
943 : }
944 : }
945 71110 : gfc_current_locus = old_loc;
946 71110 : }
947 690047 : else if (flag_openacc
948 58738 : && !(flag_openmp || flag_openmp_simd))
949 : {
950 58738 : locus old_loc = gfc_current_locus;
951 58738 : if (next_char () == '$')
952 : {
953 20552 : c = next_char ();
954 20552 : if (c == 'a' || c == 'A')
955 : {
956 20520 : if (skip_free_oacc_sentinel (start, old_loc))
957 20518 : return false;
958 2 : gfc_current_locus = old_loc;
959 2 : next_char();
960 2 : c = next_char();
961 : }
962 : }
963 38220 : gfc_current_locus = old_loc;
964 : }
965 : }
966 740922 : skip_comment_line ();
967 740922 : continue;
968 740922 : }
969 :
970 : break;
971 : }
972 :
973 1615978 : if (openmp_flag && at_bol)
974 23443 : openmp_flag = 0;
975 :
976 1615978 : if (openacc_flag && at_bol)
977 12302 : openacc_flag = 0;
978 :
979 1615978 : gcc_attribute_flag = 0;
980 1615978 : gfc_current_locus = start;
981 1615978 : return false;
982 : }
983 :
984 : /* Return true if MP was matched in fixed form. */
985 : static bool
986 9961 : skip_fixed_omp_sentinel (locus *start)
987 : {
988 9961 : gfc_char_t c;
989 9961 : if ((c = next_char ()) != 'm' && c != 'M')
990 : return false;
991 9961 : if ((c = next_char ()) == 'p' || c == 'P')
992 : {
993 9937 : c = next_char ();
994 9937 : if (c != '\n'
995 9937 : && (continue_flag
996 277 : || c == ' ' || c == '\t' || c == '0'))
997 : {
998 9936 : if (c == ' ' || c == '\t' || c == '0')
999 9865 : openacc_flag = 0;
1000 10149 : do
1001 10149 : c = next_char ();
1002 10149 : while (gfc_is_whitespace (c));
1003 9936 : if (c != '\n' && c != '!')
1004 : {
1005 : /* Canonicalize to *$omp. */
1006 9936 : *start->nextc = '*';
1007 9936 : openmp_flag = 1;
1008 9936 : gfc_current_locus = *start;
1009 9936 : return true;
1010 : }
1011 : }
1012 : }
1013 24 : else if (UNLIKELY (c == 'x' || c == 'X'))
1014 24 : gfc_warning_now (OPT_Wsurprising,
1015 : "Ignoring %<!$omx%> vendor-extension sentinel at %C");
1016 : return false;
1017 : }
1018 :
1019 : /* Return true if CC was matched in fixed form. */
1020 : static bool
1021 41325 : skip_fixed_oacc_sentinel (locus *start)
1022 : {
1023 41325 : gfc_char_t c;
1024 69172 : if (((c = next_char ()) == 'c' || c == 'C')
1025 69158 : && ((c = next_char ()) == 'c' || c == 'C'))
1026 : {
1027 41311 : c = next_char ();
1028 41311 : if (c != '\n'
1029 41311 : && (continue_flag
1030 1052 : || c == ' ' || c == '\t' || c == '0'))
1031 : {
1032 41308 : if (c == ' ' || c == '\t' || c == '0')
1033 41173 : openmp_flag = 0;
1034 41443 : do
1035 41443 : c = next_char ();
1036 41443 : while (gfc_is_whitespace (c));
1037 41308 : if (c != '\n' && c != '!')
1038 : {
1039 : /* Canonicalize to *$acc. */
1040 41308 : *start->nextc = '*';
1041 41308 : openacc_flag = 1;
1042 41308 : gfc_current_locus = *start;
1043 41308 : return true;
1044 : }
1045 : }
1046 : }
1047 : return false;
1048 : }
1049 :
1050 : /* Skip comment lines in fixed source mode. We have the same rules as
1051 : in skip_free_comment(), except that we can have a 'c', 'C' or '*'
1052 : in column 1, and a '!' cannot be in column 6. Also, we deal with
1053 : lines with 'd' or 'D' in column 1, if the user requested this. */
1054 :
1055 : static void
1056 3719033 : skip_fixed_comments (void)
1057 : {
1058 3719033 : locus start;
1059 3719033 : int col;
1060 3719033 : gfc_char_t c;
1061 :
1062 3719033 : if (! gfc_at_bol ())
1063 : {
1064 49 : start = gfc_current_locus;
1065 49 : if (! gfc_at_eof ())
1066 : {
1067 137 : do
1068 137 : c = next_char ();
1069 137 : while (gfc_is_whitespace (c));
1070 :
1071 49 : if (c == '\n')
1072 2 : gfc_advance_line ();
1073 47 : else if (c == '!')
1074 1 : skip_comment_line ();
1075 : }
1076 :
1077 49 : if (! gfc_at_bol ())
1078 : {
1079 46 : gfc_current_locus = start;
1080 482856 : return;
1081 : }
1082 : }
1083 :
1084 5121496 : for (;;)
1085 : {
1086 5121496 : start = gfc_current_locus;
1087 5121496 : if (gfc_at_eof ())
1088 : break;
1089 :
1090 4997245 : c = next_char ();
1091 4997245 : if (c == '\n')
1092 : {
1093 4779 : gfc_advance_line ();
1094 4779 : continue;
1095 : }
1096 :
1097 : if (c == '!' || c == 'c' || c == 'C' || c == '*')
1098 : {
1099 1176965 : if (skip_gcc_attribute (start))
1100 : {
1101 : /* Canonicalize to *$omp. */
1102 431566 : *start.nextc = '*';
1103 431566 : return;
1104 : }
1105 :
1106 745399 : if (gfc_current_locus.u.lb != NULL
1107 745399 : && continue_line < gfc_linebuf_linenum (gfc_current_locus.u.lb))
1108 549770 : continue_line = gfc_linebuf_linenum (gfc_current_locus.u.lb);
1109 :
1110 : /* If -fopenmp/-fopenacc, we need to handle here 2 things:
1111 : 1) don't treat !$omp/!$acc|c$omp/c$acc|*$omp / *$acc as comments,
1112 : but directives
1113 : 2) handle OpenMP conditional compilation, where
1114 : !$|c$|*$ should be treated as 2 spaces if the characters
1115 : in columns 3 to 6 are valid fixed form label columns
1116 : characters. */
1117 745399 : if ((flag_openmp || flag_openmp_simd) && !flag_openacc)
1118 : {
1119 108102 : if (next_char () == '$')
1120 : {
1121 9661 : c = next_char ();
1122 9661 : if (c == 'o' || c == 'O')
1123 : {
1124 9523 : if (skip_fixed_omp_sentinel (&start))
1125 : return;
1126 : }
1127 : else
1128 138 : goto check_for_digits;
1129 : }
1130 98465 : gfc_current_locus = start;
1131 : }
1132 637297 : else if (flag_openacc && !(flag_openmp || flag_openmp_simd))
1133 : {
1134 153540 : if (next_char () == '$')
1135 : {
1136 41133 : c = next_char ();
1137 41133 : if (c == 'a' || c == 'A')
1138 : {
1139 41017 : if (skip_fixed_oacc_sentinel (&start))
1140 : return;
1141 : }
1142 : }
1143 112525 : gfc_current_locus = start;
1144 : }
1145 483757 : else if (flag_openacc || flag_openmp || flag_openmp_simd)
1146 : {
1147 2717 : if (next_char () == '$')
1148 : {
1149 1308 : c = next_char ();
1150 1308 : if (c == 'a' || c == 'A')
1151 : {
1152 308 : if (skip_fixed_oacc_sentinel (&start))
1153 : return;
1154 : }
1155 1000 : else if (c == 'o' || c == 'O')
1156 : {
1157 438 : if (skip_fixed_omp_sentinel (&start))
1158 : return;
1159 : }
1160 : else
1161 562 : goto check_for_digits;
1162 : }
1163 1425 : gfc_current_locus = start;
1164 : }
1165 :
1166 693455 : skip_comment_line ();
1167 693455 : continue;
1168 :
1169 700 : check_for_digits:
1170 700 : {
1171 : /* Required for OpenMP's conditional compilation sentinel. */
1172 700 : int digit_seen = 0;
1173 :
1174 1388 : for (col = 3; col < 6; col++, c = next_char ())
1175 1306 : if (c == ' ')
1176 578 : continue;
1177 728 : else if (c == '\t')
1178 : {
1179 : col = 6;
1180 : break;
1181 : }
1182 728 : else if (c < '0' || c > '9')
1183 : break;
1184 : else
1185 : digit_seen = 1;
1186 :
1187 700 : if (col == 6 && c != '\n'
1188 82 : && ((continue_flag && !digit_seen)
1189 47 : || c == ' ' || c == '\t' || c == '0'))
1190 : {
1191 45 : gfc_current_locus = start;
1192 45 : start.nextc[0] = ' ';
1193 45 : start.nextc[1] = ' ';
1194 45 : continue;
1195 : }
1196 : }
1197 655 : skip_comment_line ();
1198 655 : continue;
1199 694110 : }
1200 :
1201 3815501 : if (gfc_option.flag_d_lines != -1 && (c == 'd' || c == 'D'))
1202 : {
1203 16 : if (gfc_option.flag_d_lines == 0)
1204 : {
1205 8 : skip_comment_line ();
1206 8 : continue;
1207 : }
1208 : else
1209 8 : *start.nextc = c = ' ';
1210 : }
1211 :
1212 3815493 : col = 1;
1213 :
1214 76132063 : while (gfc_is_whitespace (c))
1215 : {
1216 72316570 : c = next_char ();
1217 72316570 : col++;
1218 : }
1219 :
1220 3815493 : if (c == '\n')
1221 : {
1222 693196 : gfc_advance_line ();
1223 693196 : continue;
1224 : }
1225 :
1226 3122297 : if (col != 6 && c == '!')
1227 : {
1228 10371 : if (gfc_current_locus.u.lb != NULL
1229 10371 : && continue_line < gfc_linebuf_linenum (gfc_current_locus.u.lb))
1230 5490 : continue_line = gfc_linebuf_linenum (gfc_current_locus.u.lb);
1231 10371 : skip_comment_line ();
1232 10371 : continue;
1233 : }
1234 :
1235 : break;
1236 : }
1237 :
1238 3236177 : openmp_flag = 0;
1239 3236177 : openacc_flag = 0;
1240 3236177 : gcc_attribute_flag = 0;
1241 3236177 : gfc_current_locus = start;
1242 : }
1243 :
1244 :
1245 : /* Skips the current line if it is a comment. */
1246 :
1247 : void
1248 5029113 : gfc_skip_comments (void)
1249 : {
1250 5029113 : if (gfc_current_form == FORM_FREE)
1251 4745509 : skip_free_comments ();
1252 : else
1253 283604 : skip_fixed_comments ();
1254 5029113 : }
1255 :
1256 :
1257 : /* Get the next character from the input, taking continuation lines
1258 : and end-of-line comments into account. This implies that comment
1259 : lines between continued lines must be eaten here. For higher-level
1260 : subroutines, this flattens continued lines into a single logical
1261 : line. The in_string flag denotes whether we're inside a character
1262 : context or not. */
1263 :
1264 : gfc_char_t
1265 1206675727 : gfc_next_char_literal (gfc_instring in_string)
1266 : {
1267 1206675727 : static locus omp_acc_err_loc = {};
1268 1206675727 : locus old_loc;
1269 1206675727 : int i, prev_openmp_flag, prev_openacc_flag;
1270 1206675727 : gfc_char_t c;
1271 :
1272 1206675727 : continue_flag = 0;
1273 1206675727 : prev_openacc_flag = prev_openmp_flag = 0;
1274 :
1275 1206743741 : restart:
1276 1206743741 : c = next_char ();
1277 1206743741 : if (gfc_at_end ())
1278 : {
1279 170 : continue_count = 0;
1280 170 : return c;
1281 : }
1282 :
1283 1206743571 : if (gfc_current_form == FORM_FREE)
1284 : {
1285 1081164963 : bool openmp_cond_flag;
1286 :
1287 1081164963 : if (!in_string && c == '!')
1288 : {
1289 10831605 : if (gcc_attribute_flag
1290 10014317 : && memcmp (&gfc_current_locus, &gcc_attribute_locus,
1291 : sizeof (gfc_current_locus)) == 0)
1292 10013790 : goto done;
1293 :
1294 817815 : if (openmp_flag
1295 118358 : && memcmp (&gfc_current_locus, &openmp_locus,
1296 : sizeof (gfc_current_locus)) == 0)
1297 101799 : goto done;
1298 :
1299 716016 : if (openacc_flag
1300 75399 : && memcmp (&gfc_current_locus, &openacc_locus,
1301 : sizeof (gfc_current_locus)) == 0)
1302 60693 : goto done;
1303 :
1304 : /* This line can't be continued */
1305 23316676 : do
1306 : {
1307 23316676 : c = next_char ();
1308 : }
1309 23316676 : while (c != '\n');
1310 :
1311 : /* Avoid truncation warnings for comment ending lines. */
1312 655323 : gfc_current_locus.u.lb->truncated = 0;
1313 :
1314 655323 : goto done;
1315 : }
1316 :
1317 : /* Check to see if the continuation line was truncated. */
1318 1070333358 : if (warn_line_truncation && gfc_current_locus.u.lb != NULL
1319 1070242054 : && gfc_current_locus.u.lb->truncated)
1320 : {
1321 14 : int maxlen = flag_free_line_length;
1322 14 : gfc_char_t *current_nextc = gfc_current_locus.nextc;
1323 :
1324 14 : gfc_current_locus.u.lb->truncated = 0;
1325 14 : gfc_current_locus.nextc = gfc_current_locus.u.lb->line + maxlen;
1326 14 : gfc_warning_now (OPT_Wline_truncation,
1327 : "Line truncated at %L", &gfc_current_locus);
1328 14 : gfc_current_locus.nextc = current_nextc;
1329 : }
1330 :
1331 1070333358 : if (c != '&')
1332 1070058543 : goto done;
1333 :
1334 : /* If the next nonblank character is a ! or \n, we've got a
1335 : continuation line. */
1336 274815 : old_loc = gfc_current_locus;
1337 :
1338 274815 : c = next_char ();
1339 571365 : while (gfc_is_whitespace (c))
1340 21735 : c = next_char ();
1341 :
1342 : /* Character constants to be continued cannot have commentary
1343 : after the '&'. However, there are cases where we may think we
1344 : are still in a string and we are looking for a possible
1345 : doubled quote and we end up here. See PR64506. */
1346 :
1347 274815 : if (in_string && c != '\n')
1348 : {
1349 4178 : gfc_current_locus = old_loc;
1350 4178 : c = '&';
1351 4178 : goto done;
1352 : }
1353 :
1354 270637 : if (c != '!' && c != '\n')
1355 : {
1356 195 : gfc_current_locus = old_loc;
1357 195 : c = '&';
1358 195 : goto done;
1359 : }
1360 :
1361 270442 : if (flag_openmp)
1362 30123 : prev_openmp_flag = openmp_flag;
1363 270442 : if (flag_openacc)
1364 4874 : prev_openacc_flag = openacc_flag;
1365 :
1366 : /* This can happen if the input file changed or via cpp's #line
1367 : without getting reset (e.g. via input_stmt). It also happens
1368 : when pre-including files via -fpre-include=. */
1369 270442 : if (continue_count == 0
1370 122807 : && gfc_current_locus.u.lb
1371 393249 : && continue_line > gfc_linebuf_linenum (gfc_current_locus.u.lb) + 1)
1372 3978 : continue_line = gfc_linebuf_linenum (gfc_current_locus.u.lb) + 1;
1373 :
1374 270442 : continue_flag = 1;
1375 270442 : if (c == '!')
1376 13917 : skip_comment_line ();
1377 : else
1378 256525 : gfc_advance_line ();
1379 :
1380 270442 : if (gfc_at_eof ())
1381 23 : goto not_continuation;
1382 :
1383 : /* We've got a continuation line. If we are on the very next line after
1384 : the last continuation, increment the continuation line count and
1385 : check whether the limit has been exceeded. */
1386 270419 : if (gfc_linebuf_linenum (gfc_current_locus.u.lb) == continue_line + 1)
1387 : {
1388 21392 : if (++continue_count == gfc_option.max_continue_free)
1389 : {
1390 4 : if (gfc_notification_std (GFC_STD_GNU) || pedantic)
1391 4 : gfc_warning (0, "Limit of %d continuations exceeded in "
1392 : "statement at %C", gfc_option.max_continue_free);
1393 : }
1394 : }
1395 :
1396 : /* Now find where it continues. First eat any comment lines. */
1397 270419 : openmp_cond_flag = skip_free_comments ();
1398 :
1399 270419 : if (gfc_current_locus.u.lb != NULL
1400 270419 : && continue_line < gfc_linebuf_linenum (gfc_current_locus.u.lb))
1401 37966 : continue_line = gfc_linebuf_linenum (gfc_current_locus.u.lb);
1402 :
1403 270419 : if (flag_openmp)
1404 30118 : if (prev_openmp_flag != openmp_flag && !openacc_flag)
1405 : {
1406 286 : gfc_current_locus = old_loc;
1407 286 : openmp_flag = prev_openmp_flag;
1408 286 : c = '&';
1409 286 : goto done;
1410 : }
1411 :
1412 270133 : if (flag_openacc)
1413 4874 : if (prev_openacc_flag != openacc_flag && !openmp_flag)
1414 : {
1415 5 : gfc_current_locus = old_loc;
1416 5 : openacc_flag = prev_openacc_flag;
1417 5 : c = '&';
1418 5 : goto done;
1419 : }
1420 :
1421 : /* Now that we have a non-comment line, probe ahead for the
1422 : first non-whitespace character. If it is another '&', then
1423 : reading starts at the next character, otherwise we must back
1424 : up to where the whitespace started and resume from there. */
1425 :
1426 270128 : old_loc = gfc_current_locus;
1427 :
1428 270128 : c = next_char ();
1429 4069733 : while (gfc_is_whitespace (c))
1430 3529477 : c = next_char ();
1431 :
1432 270128 : if (openmp_flag && !openacc_flag)
1433 : {
1434 40848 : for (i = 0; i < 5; i++, c = next_char ())
1435 : {
1436 34040 : gcc_assert (gfc_wide_tolower (c) == (unsigned char) "!$omp"[i]);
1437 34040 : if (i == 4)
1438 6808 : old_loc = gfc_current_locus;
1439 : }
1440 14569 : while (gfc_is_whitespace (c))
1441 7761 : c = next_char ();
1442 : }
1443 270128 : if (openacc_flag && !openmp_flag)
1444 : {
1445 2550 : for (i = 0; i < 5; i++, c = next_char ())
1446 : {
1447 2125 : gcc_assert (gfc_wide_tolower (c) == (unsigned char) "!$acc"[i]);
1448 2125 : if (i == 4)
1449 425 : old_loc = gfc_current_locus;
1450 : }
1451 1034 : while (gfc_is_whitespace (c))
1452 609 : c = next_char ();
1453 : }
1454 :
1455 : /* In case we have an OpenMP directive continued by OpenACC
1456 : sentinel, or vice versa, we get both openmp_flag and
1457 : openacc_flag on. */
1458 :
1459 270128 : if (openacc_flag && openmp_flag)
1460 : {
1461 : int is_openmp = 0;
1462 396 : for (i = 0; i < 5; i++, c = next_char ())
1463 : {
1464 330 : if (gfc_wide_tolower (c) != (unsigned char) "!$acc"[i])
1465 24 : is_openmp = 1;
1466 : }
1467 66 : if (omp_acc_err_loc.nextc != gfc_current_locus.nextc
1468 63 : || omp_acc_err_loc.u.lb != gfc_current_locus.u.lb)
1469 5 : gfc_error_now (is_openmp
1470 : ? G_("Wrong OpenACC continuation at %C: "
1471 : "expected !$ACC, got !$OMP")
1472 : : G_("Wrong OpenMP continuation at %C: "
1473 : "expected !$OMP, got !$ACC"));
1474 66 : omp_acc_err_loc = gfc_current_locus;
1475 66 : goto not_continuation;
1476 : }
1477 :
1478 270062 : if (c != '&')
1479 : {
1480 251292 : if (in_string && gfc_current_locus.nextc)
1481 : {
1482 44 : gfc_current_locus.nextc--;
1483 44 : if (warn_ampersand && in_string == INSTRING_WARN)
1484 14 : gfc_warning (OPT_Wampersand,
1485 : "Missing %<&%> in continued character "
1486 : "constant at %C");
1487 : }
1488 251247 : else if (!in_string && (c == '\'' || c == '"'))
1489 88585 : goto done;
1490 : /* Both !$omp and !$ -fopenmp continuation lines have & on the
1491 : continuation line only optionally. */
1492 162663 : else if (openmp_flag || openacc_flag || openmp_cond_flag)
1493 : {
1494 3048 : if (gfc_current_locus.nextc)
1495 3048 : gfc_current_locus.nextc--;
1496 : }
1497 : else
1498 : {
1499 159615 : c = ' ';
1500 159615 : gfc_current_locus = old_loc;
1501 159615 : goto done;
1502 : }
1503 : }
1504 : }
1505 : else /* Fixed form. */
1506 : {
1507 : /* Fixed form continuation. */
1508 125578608 : if (in_string != INSTRING_WARN && c == '!')
1509 : {
1510 : /* Skip comment at end of line. */
1511 1698973 : do
1512 : {
1513 1698973 : c = next_char ();
1514 : }
1515 1698973 : while (c != '\n');
1516 :
1517 : /* Avoid truncation warnings for comment ending lines. */
1518 39361 : gfc_current_locus.u.lb->truncated = 0;
1519 : }
1520 :
1521 125578608 : if (c != '\n')
1522 122143179 : goto done;
1523 :
1524 : /* Check to see if the continuation line was truncated. */
1525 3435429 : if (warn_line_truncation && gfc_current_locus.u.lb != NULL
1526 23712 : && gfc_current_locus.u.lb->truncated)
1527 : {
1528 5 : gfc_current_locus.u.lb->truncated = 0;
1529 5 : gfc_warning_now (OPT_Wline_truncation,
1530 : "Line truncated at %L", &gfc_current_locus);
1531 : }
1532 :
1533 3435429 : if (flag_openmp)
1534 602105 : prev_openmp_flag = openmp_flag;
1535 3435429 : if (flag_openacc)
1536 1015932 : prev_openacc_flag = openacc_flag;
1537 :
1538 : /* This can happen if the input file changed or via cpp's #line
1539 : without getting reset (e.g. via input_stmt). It also happens
1540 : when pre-including files via -fpre-include=. */
1541 3435429 : if (continue_count == 0
1542 3404825 : && gfc_current_locus.u.lb
1543 6840254 : && continue_line > gfc_linebuf_linenum (gfc_current_locus.u.lb) + 1)
1544 91793 : continue_line = gfc_linebuf_linenum (gfc_current_locus.u.lb) + 1;
1545 :
1546 3435429 : continue_flag = 1;
1547 3435429 : old_loc = gfc_current_locus;
1548 :
1549 3435429 : gfc_advance_line ();
1550 3435429 : skip_fixed_comments ();
1551 :
1552 : /* See if this line is a continuation line. */
1553 3435429 : if (flag_openmp && openmp_flag != prev_openmp_flag && !openacc_flag)
1554 : {
1555 11060 : openmp_flag = prev_openmp_flag;
1556 11060 : goto not_continuation;
1557 : }
1558 3424369 : if (flag_openacc && openacc_flag != prev_openacc_flag && !openmp_flag)
1559 : {
1560 42407 : openacc_flag = prev_openacc_flag;
1561 42407 : goto not_continuation;
1562 : }
1563 :
1564 : /* In case we have an OpenMP directive continued by OpenACC
1565 : sentinel, or vice versa, we get both openmp_flag and
1566 : openacc_flag on. */
1567 3381962 : if (openacc_flag && openmp_flag)
1568 : {
1569 : int is_openmp = 0;
1570 552 : for (i = 0; i < 5; i++)
1571 : {
1572 460 : c = next_char ();
1573 460 : if (gfc_wide_tolower (c) != (unsigned char) "*$acc"[i])
1574 24 : is_openmp = 1;
1575 : }
1576 92 : if (omp_acc_err_loc.nextc != gfc_current_locus.nextc
1577 88 : || omp_acc_err_loc.u.lb != gfc_current_locus.u.lb)
1578 7 : gfc_error_now (is_openmp
1579 : ? G_("Wrong OpenACC continuation at %C: "
1580 : "expected !$ACC, got !$OMP")
1581 : : G_("Wrong OpenMP continuation at %C: "
1582 : "expected !$OMP, got !$ACC"));
1583 92 : omp_acc_err_loc = gfc_current_locus;
1584 92 : goto not_continuation;
1585 : }
1586 3381870 : else if (!openmp_flag && !openacc_flag)
1587 18081633 : for (i = 0; i < 5; i++)
1588 : {
1589 15183069 : c = next_char ();
1590 15183069 : if (c != ' ')
1591 481021 : goto not_continuation;
1592 : }
1593 2285 : else if (openmp_flag)
1594 4944 : for (i = 0; i < 5; i++)
1595 : {
1596 4120 : c = next_char ();
1597 4120 : if (gfc_wide_tolower (c) != (unsigned char) "*$omp"[i])
1598 0 : goto not_continuation;
1599 : }
1600 1461 : else if (openacc_flag)
1601 8766 : for (i = 0; i < 5; i++)
1602 : {
1603 7305 : c = next_char ();
1604 7305 : if (gfc_wide_tolower (c) != (unsigned char) "*$acc"[i])
1605 0 : goto not_continuation;
1606 : }
1607 :
1608 2900849 : c = next_char ();
1609 2900849 : if (c == '0' || c == ' ' || c == '\n')
1610 2854697 : goto not_continuation;
1611 :
1612 : /* We've got a continuation line. If we are on the very next line after
1613 : the last continuation, increment the continuation line count and
1614 : check whether the limit has been exceeded. */
1615 46152 : if (gfc_linebuf_linenum (gfc_current_locus.u.lb) == continue_line + 1)
1616 : {
1617 4119 : if (++continue_count == gfc_option.max_continue_fixed)
1618 : {
1619 2 : if (gfc_notification_std (GFC_STD_GNU) || pedantic)
1620 2 : gfc_warning (0, "Limit of %d continuations exceeded in "
1621 : "statement at %C",
1622 : gfc_option.max_continue_fixed);
1623 : }
1624 : }
1625 :
1626 46152 : if (gfc_current_locus.u.lb != NULL
1627 46152 : && continue_line < gfc_linebuf_linenum (gfc_current_locus.u.lb))
1628 6877 : continue_line = gfc_linebuf_linenum (gfc_current_locus.u.lb);
1629 : }
1630 :
1631 : /* Ready to read first character of continuation line, which might
1632 : be another continuation line! */
1633 68014 : goto restart;
1634 :
1635 3389366 : not_continuation:
1636 3389366 : c = '\n';
1637 3389366 : gfc_current_locus = old_loc;
1638 3389366 : end_flag = 0;
1639 :
1640 1206675557 : done:
1641 1206675557 : if (c == '\n')
1642 40885225 : continue_count = 0;
1643 1206675557 : continue_flag = 0;
1644 1206675557 : return c;
1645 : }
1646 :
1647 :
1648 : /* Get the next character of input, folded to lowercase. In fixed
1649 : form mode, we also ignore spaces. When matcher subroutines are
1650 : parsing character literals, they have to call
1651 : gfc_next_char_literal(). */
1652 :
1653 : gfc_char_t
1654 702182460 : gfc_next_char (void)
1655 : {
1656 731710420 : gfc_char_t c;
1657 :
1658 731710420 : do
1659 : {
1660 731710420 : c = gfc_next_char_literal (NONSTRING);
1661 : }
1662 731710420 : while (gfc_current_form == FORM_FIXED && gfc_is_whitespace (c));
1663 :
1664 702182460 : return gfc_wide_tolower (c);
1665 : }
1666 :
1667 : char
1668 651444352 : gfc_next_ascii_char (void)
1669 : {
1670 651444352 : gfc_char_t c = gfc_next_char ();
1671 :
1672 651444352 : return (gfc_wide_fits_in_byte (c) ? (unsigned char) c
1673 651444352 : : (unsigned char) UCHAR_MAX);
1674 : }
1675 :
1676 :
1677 : gfc_char_t
1678 39743636 : gfc_peek_char (void)
1679 : {
1680 39743636 : locus old_loc;
1681 39743636 : gfc_char_t c;
1682 :
1683 39743636 : old_loc = gfc_current_locus;
1684 39743636 : c = gfc_next_char ();
1685 39743636 : gfc_current_locus = old_loc;
1686 :
1687 39743636 : return c;
1688 : }
1689 :
1690 :
1691 : char
1692 39727851 : gfc_peek_ascii_char (void)
1693 : {
1694 39727851 : gfc_char_t c = gfc_peek_char ();
1695 :
1696 39727851 : return (gfc_wide_fits_in_byte (c) ? (unsigned char) c
1697 39727851 : : (unsigned char) UCHAR_MAX);
1698 : }
1699 :
1700 :
1701 : /* Recover from an error. We try to get past the current statement
1702 : and get lined up for the next. The next statement follows a '\n'
1703 : or a ';'. We also assume that we are not within a character
1704 : constant, and deal with finding a '\'' or '"'. */
1705 :
1706 : void
1707 3640 : gfc_error_recovery (void)
1708 : {
1709 3640 : gfc_char_t c, delim;
1710 :
1711 3640 : if (gfc_at_eof ())
1712 : return;
1713 :
1714 75826 : for (;;)
1715 : {
1716 75826 : c = gfc_next_char ();
1717 75826 : if (c == '\n' || c == ';')
1718 : break;
1719 :
1720 72194 : if (c != '\'' && c != '"')
1721 : {
1722 71816 : if (gfc_at_eof ())
1723 : break;
1724 71816 : continue;
1725 : }
1726 : delim = c;
1727 :
1728 2746 : for (;;)
1729 : {
1730 2746 : c = next_char ();
1731 :
1732 2746 : if (c == delim)
1733 : break;
1734 2375 : if (c == '\n')
1735 : return;
1736 2368 : if (c == '\\')
1737 : {
1738 8 : c = next_char ();
1739 8 : if (c == '\n')
1740 : return;
1741 : }
1742 : }
1743 371 : if (gfc_at_eof ())
1744 : break;
1745 : }
1746 : }
1747 :
1748 :
1749 : /* Read ahead until the next character to be read is not whitespace. */
1750 :
1751 : void
1752 362992918 : gfc_gobble_whitespace (void)
1753 : {
1754 452928610 : static int linenum = 0;
1755 452928610 : locus old_loc;
1756 452928610 : gfc_char_t c;
1757 :
1758 452928610 : do
1759 : {
1760 452928610 : old_loc = gfc_current_locus;
1761 452928610 : c = gfc_next_char_literal (NONSTRING);
1762 : /* Issue a warning for nonconforming tabs. We keep track of the line
1763 : number because the Fortran matchers will often back up and the same
1764 : line will be scanned multiple times. */
1765 452928610 : if (warn_tabs && c == '\t')
1766 : {
1767 24 : int cur_linenum = LOCATION_LINE (gfc_current_locus.u.lb->location);
1768 24 : if (cur_linenum != linenum)
1769 : {
1770 3 : linenum = cur_linenum;
1771 3 : gfc_warning_now (OPT_Wtabs, "Nonconforming tab character at %C");
1772 : }
1773 : }
1774 : }
1775 452928610 : while (gfc_is_whitespace (c));
1776 :
1777 362992918 : if (!ISPRINT(c) && c != '\n' && last_error_char != gfc_current_locus.nextc)
1778 : {
1779 3 : char buf[20];
1780 3 : last_error_char = gfc_current_locus.nextc;
1781 3 : snprintf (buf, 20, "%2.2X", c);
1782 3 : gfc_error_now ("Invalid character 0x%s at %C", buf);
1783 : }
1784 :
1785 362992918 : gfc_current_locus = old_loc;
1786 362992918 : }
1787 :
1788 :
1789 : /* Load a single line into pbuf.
1790 :
1791 : If pbuf points to a NULL pointer, it is allocated.
1792 : We truncate lines that are too long, unless we're dealing with
1793 : preprocessor lines or if the option -ffixed-line-length-none is set,
1794 : in which case we reallocate the buffer to fit the entire line, if
1795 : need be.
1796 : In fixed mode, we expand a tab that occurs within the statement
1797 : label region to expand to spaces that leave the next character in
1798 : the source region.
1799 :
1800 : If first_char is not NULL, it's a pointer to a single char value holding
1801 : the first character of the line, which has already been read by the
1802 : caller. This avoids the use of ungetc().
1803 :
1804 : If allow_bom is true, increase length of line buffer to allow for UTF-8 BOM
1805 : (byte order mark) at the beginning of the line.
1806 :
1807 : load_line returns whether the line was truncated.
1808 :
1809 : NOTE: The error machinery isn't available at this point, so we can't
1810 : easily report line and column numbers consistent with other
1811 : parts of gfortran. */
1812 :
1813 : static bool
1814 7051655 : load_line (FILE *input, gfc_char_t **pbuf, int *pbuflen, const int *first_char,
1815 : bool allow_bom = false)
1816 : {
1817 7051655 : int c, maxlen, i, preprocessor_flag, buflen = *pbuflen;
1818 7051655 : int quoted = ' ', comment_ix = -1;
1819 7051655 : bool seen_comment = false;
1820 7051655 : bool first_comment = true;
1821 7051655 : bool trunc_flag = false;
1822 7051655 : bool seen_printable = false;
1823 7051655 : bool seen_ampersand = false;
1824 7051655 : bool found_tab = false;
1825 7051655 : bool warned_tabs = false;
1826 7051655 : bool seen_utf8_bom = false;
1827 7051655 : gfc_char_t *buffer;
1828 :
1829 : /* Determine the maximum allowed line length. */
1830 7051655 : if (gfc_current_form == FORM_FREE)
1831 6692278 : maxlen = flag_free_line_length;
1832 359377 : else if (gfc_current_form == FORM_FIXED)
1833 359377 : maxlen = flag_fixed_line_length;
1834 : else
1835 : maxlen = 72;
1836 :
1837 7051655 : if (*pbuf == NULL)
1838 : {
1839 : /* Allocate the line buffer, storing its length into buflen.
1840 : Note that if maxlen==0, indicating that arbitrary-length lines
1841 : are allowed, the buffer will be reallocated if this length is
1842 : insufficient; since 132 characters is the length of a standard
1843 : free-form line, we use that as a starting guess. */
1844 65726 : if (maxlen > 0)
1845 : buflen = maxlen;
1846 : else
1847 308 : buflen = 132;
1848 :
1849 : /* Increase line buffer length for BOM. */
1850 65726 : if (allow_bom)
1851 65716 : buflen += 3;
1852 :
1853 65726 : *pbuf = gfc_get_wide_string (buflen + 1);
1854 : }
1855 :
1856 7051655 : i = 0;
1857 7051655 : buffer = *pbuf;
1858 :
1859 7051655 : if (first_char)
1860 10 : c = *first_char;
1861 : else
1862 7051645 : c = getc (input);
1863 :
1864 : /* In order to not truncate preprocessor lines, we have to
1865 : remember that this is one. */
1866 7051655 : preprocessor_flag = (c == '#');
1867 :
1868 308572876 : for (;;)
1869 : {
1870 308572876 : if (c == EOF)
1871 : break;
1872 :
1873 : if (c == '\n')
1874 : {
1875 : /* Check for illegal use of ampersand. See F95 Standard 3.3.1.3. */
1876 6985810 : if (gfc_current_form == FORM_FREE
1877 6630081 : && !seen_printable && seen_ampersand)
1878 : {
1879 9 : if (pedantic)
1880 0 : gfc_error_now ("%<&%> not allowed by itself in line %d",
1881 : current_file->line);
1882 : else
1883 9 : gfc_warning_now (0, "%<&%> not allowed by itself in line %d",
1884 : current_file->line);
1885 : }
1886 : break;
1887 : }
1888 :
1889 : if (c == '\r' || c == '\0')
1890 10955 : goto next_char; /* Gobble characters. */
1891 :
1892 : if (c == '&')
1893 : {
1894 41831 : if (seen_ampersand)
1895 : {
1896 : seen_ampersand = false;
1897 : seen_printable = true;
1898 : }
1899 : else
1900 39227 : seen_ampersand = true;
1901 : }
1902 :
1903 301510266 : if ((c != '&' && c != '!' && c != ' ') || (c == '!' && !seen_ampersand))
1904 262471672 : seen_printable = true;
1905 :
1906 : /* Is this a fixed-form comment? */
1907 301510266 : if (gfc_current_form == FORM_FIXED && i == 0
1908 339699 : && (c == '*' || c == 'c' || c == 'C'
1909 325588 : || (gfc_option.flag_d_lines != -1 && (c == 'd' || c == 'D'))))
1910 : {
1911 301510266 : seen_comment = true;
1912 301510266 : comment_ix = i;
1913 : }
1914 :
1915 301510266 : if (quoted == ' ')
1916 : {
1917 278310704 : if (c == '\'' || c == '"')
1918 301510266 : quoted = c;
1919 : }
1920 23199562 : else if (c == quoted)
1921 278300920 : quoted = ' ';
1922 :
1923 : /* Is this a free-form comment? */
1924 301510266 : if (c == '!' && quoted == ' ')
1925 : {
1926 4406904 : if (seen_comment)
1927 : first_comment = false;
1928 : seen_comment = true;
1929 : comment_ix = i;
1930 : }
1931 :
1932 : /* For truncation and tab warnings, set seen_comment to false if one has
1933 : either an OpenMP or OpenACC directive - or a !GCC$ attribute. If
1934 : OpenMP is enabled, use '!$' as conditional compilation sentinel
1935 : and OpenMP directive ('!$omp'). */
1936 301504930 : if (seen_comment && first_comment && flag_openmp && comment_ix + 1 == i
1937 447962 : && c == '$')
1938 : first_comment = seen_comment = false;
1939 301473378 : if (seen_comment && first_comment && comment_ix + 4 == i)
1940 : {
1941 4232145 : if (((*pbuf)[comment_ix+1] == 'g' || (*pbuf)[comment_ix+1] == 'G')
1942 3533369 : && ((*pbuf)[comment_ix+2] == 'c' || (*pbuf)[comment_ix+2] == 'C')
1943 3531298 : && ((*pbuf)[comment_ix+3] == 'c' || (*pbuf)[comment_ix+3] == 'C')
1944 3531298 : && c == '$')
1945 4232145 : first_comment = seen_comment = false;
1946 4232145 : if (flag_openacc
1947 221313 : && (*pbuf)[comment_ix+1] == '$'
1948 21297 : && ((*pbuf)[comment_ix+2] == 'a' || (*pbuf)[comment_ix+2] == 'A')
1949 21241 : && ((*pbuf)[comment_ix+3] == 'c' || (*pbuf)[comment_ix+3] == 'C')
1950 21240 : && (c == 'c' || c == 'C'))
1951 301510266 : first_comment = seen_comment = false;
1952 : }
1953 :
1954 : /* Vendor extension: "<tab>1" marks a continuation line. */
1955 301510266 : if (found_tab)
1956 : {
1957 106 : found_tab = false;
1958 106 : if (c >= '1' && c <= '9')
1959 : {
1960 1 : *(buffer-1) = c;
1961 1 : goto next_char;
1962 : }
1963 : }
1964 :
1965 301510265 : if (gfc_current_form == FORM_FIXED && c == '\t' && i < 6)
1966 : {
1967 106 : found_tab = true;
1968 :
1969 106 : if (warn_tabs && seen_comment == 0 && !warned_tabs)
1970 : {
1971 4 : warned_tabs = true;
1972 4 : gfc_warning_now (OPT_Wtabs,
1973 : "Nonconforming tab character in column %d "
1974 : "of line %d", i + 1, current_file->line);
1975 : }
1976 :
1977 648 : while (i < 6)
1978 : {
1979 542 : *buffer++ = ' ';
1980 542 : i++;
1981 : }
1982 :
1983 106 : goto next_char;
1984 : }
1985 :
1986 301510159 : *buffer++ = c;
1987 301510159 : i++;
1988 :
1989 : /* Check for possible UTF-8 BOM. */
1990 301510159 : if (allow_bom
1991 301510159 : && i == 3
1992 65590 : && buffer[-3] == (unsigned char) '\xEF'
1993 3 : && buffer[-2] == (unsigned char) '\xBB'
1994 3 : && buffer[-1] == (unsigned char) '\xBF')
1995 301510159 : seen_utf8_bom = true;
1996 :
1997 : /* Check for sentinel '#' after BOM. */
1998 301510159 : if (i == 4 && seen_utf8_bom)
1999 3 : preprocessor_flag = (c == '#');
2000 :
2001 301510159 : if (maxlen == 0 || preprocessor_flag)
2002 : {
2003 3122174 : if (i >= buflen)
2004 : {
2005 : /* Reallocate line buffer to double size to hold the
2006 : overlong line. */
2007 225 : buflen = buflen * 2;
2008 225 : *pbuf = XRESIZEVEC (gfc_char_t, *pbuf, (buflen + 1));
2009 225 : buffer = (*pbuf) + i;
2010 : }
2011 : }
2012 596775922 : else if (i >= maxlen + (seen_utf8_bom ? 3 : 0))
2013 : {
2014 : bool trunc_warn = true;
2015 :
2016 : /* Enhancement, if the very next non-space character is an ampersand
2017 : or comment that we would otherwise warn about, don't mark as
2018 : truncated. */
2019 :
2020 : /* Truncate the rest of the line. */
2021 116463 : for (;;)
2022 : {
2023 116463 : c = getc (input);
2024 116463 : if (c == '\r' || c == ' ')
2025 50855 : continue;
2026 :
2027 65608 : if (c == '\n' || c == EOF)
2028 : break;
2029 :
2030 56326 : if (!trunc_warn && c != '!')
2031 : trunc_warn = true;
2032 :
2033 56326 : if (trunc_warn && ((gfc_current_form == FORM_FIXED && c == '&')
2034 54349 : || c == '!'))
2035 : trunc_warn = false;
2036 :
2037 2 : if (c == '!')
2038 66 : seen_comment = 1;
2039 :
2040 56326 : if (trunc_warn && !seen_comment)
2041 8452 : trunc_flag = 1;
2042 : }
2043 :
2044 9282 : c = '\n';
2045 9282 : continue;
2046 9282 : }
2047 :
2048 298378703 : next_char:
2049 301511939 : c = getc (input);
2050 : }
2051 :
2052 : /* Pad lines to the selected line length in fixed form. */
2053 7051655 : if (gfc_current_form == FORM_FIXED
2054 359377 : && flag_fixed_line_length != 0
2055 355351 : && flag_pad_source
2056 354529 : && !preprocessor_flag
2057 354529 : && c != EOF)
2058 : {
2059 16639982 : while (i++ < maxlen + (seen_utf8_bom ? 3 : 0))
2060 7969393 : *buffer++ = ' ';
2061 : }
2062 :
2063 7051655 : *buffer = '\0';
2064 7051655 : *pbuflen = buflen;
2065 :
2066 7051655 : return trunc_flag;
2067 : }
2068 :
2069 :
2070 : /* Get a gfc_file structure, initialize it and add it to
2071 : the file stack. */
2072 :
2073 : static gfc_file *
2074 67854 : get_file (const char *name, enum lc_reason reason)
2075 : {
2076 67854 : gfc_file *f;
2077 :
2078 67854 : f = XCNEW (gfc_file);
2079 :
2080 67854 : f->filename = xstrdup (name);
2081 :
2082 67854 : f->next = file_head;
2083 67854 : file_head = f;
2084 :
2085 67854 : f->up = current_file;
2086 67854 : if (current_file != NULL)
2087 2504 : f->inclusion_line = current_file->line;
2088 :
2089 67854 : linemap_add (line_table, reason, false, f->filename, 1);
2090 :
2091 67854 : return f;
2092 : }
2093 :
2094 :
2095 : /* Deal with a line from the C preprocessor. The
2096 : initial octothorp has already been seen. */
2097 :
2098 : static void
2099 9115 : preprocessor_line (gfc_char_t *c)
2100 : {
2101 9115 : bool flag[5];
2102 9115 : int i, line;
2103 9115 : gfc_char_t *wide_filename;
2104 9115 : gfc_file *f;
2105 9115 : int escaped, unescape;
2106 9115 : char *filename;
2107 :
2108 9115 : c++;
2109 18226 : while (*c == ' ' || *c == '\t')
2110 9111 : c++;
2111 :
2112 9115 : if (*c < '0' || *c > '9')
2113 6 : goto bad_cpp_line;
2114 :
2115 9109 : line = wide_atoi (c);
2116 :
2117 9109 : c = wide_strchr (c, ' ');
2118 9109 : if (c == NULL)
2119 : {
2120 : /* No file name given. Set new line number. */
2121 0 : current_file->line = line;
2122 9109 : return;
2123 : }
2124 :
2125 : /* Skip spaces. */
2126 18218 : while (*c == ' ' || *c == '\t')
2127 9109 : c++;
2128 :
2129 : /* Skip quote. */
2130 9109 : if (*c != '"')
2131 0 : goto bad_cpp_line;
2132 9109 : ++c;
2133 :
2134 9109 : wide_filename = c;
2135 :
2136 : /* Make filename end at quote. */
2137 9109 : unescape = 0;
2138 9109 : escaped = false;
2139 436102 : while (*c && ! (!escaped && *c == '"'))
2140 : {
2141 426979 : if (escaped)
2142 : escaped = false;
2143 426979 : else if (*c == '\\')
2144 : {
2145 14 : escaped = true;
2146 14 : unescape++;
2147 : }
2148 426993 : ++c;
2149 : }
2150 :
2151 9109 : if (! *c)
2152 : /* Preprocessor line has no closing quote. */
2153 0 : goto bad_cpp_line;
2154 :
2155 9109 : *c++ = '\0';
2156 :
2157 : /* Undo effects of cpp_quote_string. */
2158 9109 : if (unescape)
2159 : {
2160 2 : gfc_char_t *s = wide_filename;
2161 2 : gfc_char_t *d = gfc_get_wide_string (c - wide_filename - unescape);
2162 :
2163 2 : wide_filename = d;
2164 118 : while (*s)
2165 : {
2166 114 : if (*s == '\\')
2167 14 : *d++ = *++s;
2168 : else
2169 100 : *d++ = *s;
2170 114 : s++;
2171 : }
2172 2 : *d = '\0';
2173 : }
2174 :
2175 : /* Get flags. */
2176 :
2177 9109 : flag[1] = flag[2] = flag[3] = flag[4] = false;
2178 :
2179 13388 : for (;;)
2180 : {
2181 13388 : c = wide_strchr (c, ' ');
2182 13388 : if (c == NULL)
2183 : break;
2184 :
2185 4279 : c++;
2186 4279 : i = wide_atoi (c);
2187 :
2188 4279 : if (i >= 1 && i <= 4)
2189 4279 : flag[i] = true;
2190 : }
2191 :
2192 : /* Convert the filename in wide characters into a filename in narrow
2193 : characters. */
2194 9109 : filename = gfc_widechar_to_char (wide_filename, -1);
2195 :
2196 : /* Interpret flags. */
2197 :
2198 9109 : if (flag[1]) /* Starting new file. */
2199 : {
2200 2138 : f = get_file (filename, LC_RENAME);
2201 2138 : add_file_change (f->filename, f->inclusion_line);
2202 2138 : current_file = f;
2203 : }
2204 :
2205 9109 : if (flag[2]) /* Ending current file. */
2206 : {
2207 2139 : if (!current_file->up
2208 2139 : || filename_cmp (current_file->up->filename, filename) != 0)
2209 : {
2210 1 : linemap_line_start (line_table, current_file->line, 80);
2211 : /* ??? One could compute the exact column where the filename
2212 : starts and compute the exact location here. */
2213 1 : gfc_warning_now_at (linemap_position_for_column (line_table, 1),
2214 : 0, "file %qs left but not entered",
2215 : filename);
2216 1 : current_file->line++;
2217 1 : if (unescape)
2218 0 : free (wide_filename);
2219 1 : free (filename);
2220 1 : return;
2221 : }
2222 :
2223 2138 : add_file_change (NULL, line);
2224 2138 : current_file = current_file->up;
2225 2138 : linemap_add (line_table, LC_RENAME, false, current_file->filename,
2226 2138 : current_file->line);
2227 : }
2228 :
2229 : /* The name of the file can be a temporary file produced by
2230 : cpp. Replace the name if it is different. */
2231 :
2232 9108 : if (filename_cmp (current_file->filename, filename) != 0)
2233 : {
2234 : /* FIXME: we leak the old filename because a pointer to it may be stored
2235 : in the linemap. Alternative could be using GC or updating linemap to
2236 : point to the new name, but there is no API for that currently. */
2237 3562 : current_file->filename = xstrdup (filename);
2238 :
2239 : /* We need to tell the linemap API that the filename changed. Just
2240 : changing current_file is insufficient. */
2241 3562 : linemap_add (line_table, LC_RENAME, false, current_file->filename, line);
2242 : }
2243 :
2244 : /* Set new line number. */
2245 9108 : current_file->line = line;
2246 9108 : if (unescape)
2247 2 : free (wide_filename);
2248 9108 : free (filename);
2249 9108 : return;
2250 :
2251 6 : bad_cpp_line:
2252 6 : linemap_line_start (line_table, current_file->line, 80);
2253 : /* ??? One could compute the exact column where the directive
2254 : starts and compute the exact location here. */
2255 6 : gfc_warning_now_at (linemap_position_for_column (line_table, 2), 0,
2256 : "Illegal preprocessor directive");
2257 6 : current_file->line++;
2258 : }
2259 :
2260 :
2261 : static void load_file (const char *, const char *, bool);
2262 :
2263 : /* include_line()-- Checks a line buffer to see if it is an include
2264 : line. If so, we call load_file() recursively to load the included
2265 : file. We never return a syntax error because a statement like
2266 : "include = 5" is perfectly legal. We return 0 if no include was
2267 : processed, 1 if we matched an include or -1 if include was
2268 : partially processed, but will need continuation lines. */
2269 :
2270 : static int
2271 6976825 : include_line (gfc_char_t *line)
2272 : {
2273 6976825 : gfc_char_t quote, *c, *begin, *stop;
2274 6976825 : char *filename;
2275 6976825 : const char *include = "include";
2276 6976825 : bool allow_continuation = flag_dec_include;
2277 6976825 : int i;
2278 :
2279 6976825 : c = line;
2280 :
2281 6976825 : if (flag_openmp || flag_openmp_simd)
2282 : {
2283 700911 : if (gfc_current_form == FORM_FREE)
2284 : {
2285 1509897 : while (*c == ' ' || *c == '\t')
2286 848417 : c++;
2287 661480 : if (*c == '!' && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
2288 265 : c += 3;
2289 : }
2290 : else
2291 : {
2292 39431 : if ((*c == '!' || *c == 'c' || *c == 'C' || *c == '*')
2293 11864 : && c[1] == '$' && c[2] == ' ')
2294 59 : c += 3;
2295 : }
2296 : }
2297 :
2298 6976825 : if (gfc_current_form == FORM_FREE)
2299 : {
2300 10453740 : while (*c == ' ' || *c == '\t')
2301 3832267 : c++;
2302 6621473 : if (gfc_wide_strncasecmp (c, "include", 7))
2303 : {
2304 6621190 : if (!allow_continuation)
2305 : return 0;
2306 37754 : for (i = 0; i < 7; ++i)
2307 : {
2308 37754 : gfc_char_t c1 = gfc_wide_tolower (*c);
2309 37754 : if (c1 != (unsigned char) include[i])
2310 : break;
2311 2333 : c++;
2312 : }
2313 35421 : if (i == 0 || *c != '&')
2314 : return 0;
2315 2 : c++;
2316 4 : while (*c == ' ' || *c == '\t')
2317 2 : c++;
2318 2 : if (*c == '\0' || *c == '!')
2319 : return -1;
2320 0 : return 0;
2321 : }
2322 :
2323 283 : c += 7;
2324 : }
2325 : else
2326 : {
2327 2207087 : while (*c == ' ' || *c == '\t')
2328 1851735 : c++;
2329 355352 : if (flag_dec_include && *c == '0' && c - line == 5)
2330 : {
2331 6 : c++;
2332 6 : while (*c == ' ' || *c == '\t')
2333 0 : c++;
2334 : }
2335 355352 : if (c - line < 6)
2336 254617 : allow_continuation = false;
2337 398620 : for (i = 0; i < 7; ++i)
2338 : {
2339 398537 : gfc_char_t c1 = gfc_wide_tolower (*c);
2340 398537 : if (c1 != (unsigned char) include[i])
2341 : break;
2342 43268 : c++;
2343 44358 : while (*c == ' ' || *c == '\t')
2344 1090 : c++;
2345 : }
2346 355352 : if (!allow_continuation)
2347 : {
2348 355168 : if (i != 7)
2349 : return 0;
2350 : }
2351 184 : else if (i != 7)
2352 : {
2353 173 : if (i == 0)
2354 : return 0;
2355 :
2356 : /* At the end of line or comment this might be continued. */
2357 60 : if (*c == '\0' || *c == '!')
2358 : return -1;
2359 :
2360 56 : return 0;
2361 : }
2362 : }
2363 :
2364 645 : while (*c == ' ' || *c == '\t')
2365 279 : c++;
2366 :
2367 : /* Find filename between quotes. */
2368 :
2369 366 : quote = *c++;
2370 366 : if (quote != '"' && quote != '\'')
2371 : {
2372 15 : if (allow_continuation)
2373 : {
2374 15 : if (gfc_current_form == FORM_FREE)
2375 : {
2376 8 : if (quote == '&')
2377 : {
2378 6 : while (*c == ' ' || *c == '\t')
2379 0 : c++;
2380 6 : if (*c == '\0' || *c == '!')
2381 : return -1;
2382 : }
2383 : }
2384 7 : else if (quote == '\0' || quote == '!')
2385 : return -1;
2386 : }
2387 2 : return 0;
2388 : }
2389 :
2390 : begin = c;
2391 :
2392 : bool cont = false;
2393 7471 : while (*c != quote && *c != '\0')
2394 : {
2395 7120 : if (allow_continuation && gfc_current_form == FORM_FREE)
2396 : {
2397 2145 : if (*c == '&')
2398 : cont = true;
2399 2143 : else if (*c != ' ' && *c != '\t')
2400 7120 : cont = false;
2401 : }
2402 7120 : c++;
2403 : }
2404 :
2405 351 : if (*c == '\0')
2406 : {
2407 4 : if (allow_continuation
2408 4 : && (cont || gfc_current_form != FORM_FREE))
2409 : return -1;
2410 0 : return 0;
2411 : }
2412 :
2413 347 : stop = c++;
2414 :
2415 3553 : while (*c == ' ' || *c == '\t')
2416 3206 : c++;
2417 :
2418 347 : if (*c != '\0' && *c != '!')
2419 : return 0;
2420 :
2421 : /* We have an include line at this point. */
2422 :
2423 347 : *stop = '\0'; /* It's ok to trash the buffer, as this line won't be
2424 : read by anything else. */
2425 :
2426 347 : filename = gfc_widechar_to_char (begin, -1);
2427 347 : load_file (filename, NULL, false);
2428 344 : free (filename);
2429 344 : return 1;
2430 : }
2431 :
2432 : /* Similarly, but try to parse an INCLUDE statement, using gfc_next_char etc.
2433 : APIs. Return 1 if recognized as valid INCLUDE statement and load_file has
2434 : been called, 0 if it is not a valid INCLUDE statement and -1 if eof has
2435 : been encountered while parsing it. */
2436 : static int
2437 63 : include_stmt (gfc_linebuf *b)
2438 : {
2439 63 : int ret = 0, i, length;
2440 63 : const char *include = "include";
2441 63 : gfc_char_t c, quote = 0;
2442 63 : locus str_locus;
2443 63 : char *filename;
2444 :
2445 63 : continue_flag = 0;
2446 63 : end_flag = 0;
2447 63 : gcc_attribute_flag = 0;
2448 63 : openmp_flag = 0;
2449 63 : openacc_flag = 0;
2450 63 : continue_count = 0;
2451 63 : continue_line = 0;
2452 63 : gfc_current_locus.u.lb = b;
2453 63 : gfc_current_locus.nextc = b->line;
2454 :
2455 63 : gfc_skip_comments ();
2456 63 : gfc_gobble_whitespace ();
2457 :
2458 509 : for (i = 0; i < 7; i++)
2459 : {
2460 405 : c = gfc_next_char ();
2461 405 : if (c != (unsigned char) include[i])
2462 : {
2463 30 : if (gfc_current_form == FORM_FIXED
2464 28 : && i == 0
2465 28 : && c == '0'
2466 8 : && gfc_current_locus.nextc == b->line + 6)
2467 : {
2468 8 : gfc_gobble_whitespace ();
2469 8 : i--;
2470 8 : continue;
2471 : }
2472 22 : gcc_assert (i != 0);
2473 22 : if (c == '\n')
2474 : {
2475 22 : gfc_advance_line ();
2476 22 : gfc_skip_comments ();
2477 22 : if (gfc_at_eof ())
2478 22 : ret = -1;
2479 : }
2480 22 : goto do_ret;
2481 : }
2482 : }
2483 41 : gfc_gobble_whitespace ();
2484 :
2485 41 : c = gfc_next_char ();
2486 41 : if (c == '\'' || c == '"')
2487 30 : quote = c;
2488 : else
2489 : {
2490 11 : if (c == '\n')
2491 : {
2492 10 : gfc_advance_line ();
2493 10 : gfc_skip_comments ();
2494 10 : if (gfc_at_eof ())
2495 11 : ret = -1;
2496 : }
2497 11 : goto do_ret;
2498 : }
2499 :
2500 30 : str_locus = gfc_current_locus;
2501 30 : length = 0;
2502 710 : do
2503 : {
2504 370 : c = gfc_next_char_literal (INSTRING_NOWARN);
2505 370 : if (c == quote)
2506 : break;
2507 348 : if (c == '\n')
2508 : {
2509 8 : gfc_advance_line ();
2510 8 : gfc_skip_comments ();
2511 8 : if (gfc_at_eof ())
2512 8 : ret = -1;
2513 8 : goto do_ret;
2514 : }
2515 340 : length++;
2516 : }
2517 : while (1);
2518 :
2519 22 : gfc_gobble_whitespace ();
2520 22 : c = gfc_next_char ();
2521 22 : if (c != '\n')
2522 0 : goto do_ret;
2523 :
2524 22 : gfc_current_locus = str_locus;
2525 22 : ret = 1;
2526 22 : filename = XNEWVEC (char, length + 1);
2527 343 : for (i = 0; i < length; i++)
2528 : {
2529 299 : c = gfc_next_char_literal (INSTRING_WARN);
2530 299 : gcc_assert (gfc_wide_fits_in_byte (c));
2531 299 : filename[i] = (unsigned char) c;
2532 : }
2533 22 : filename[length] = '\0';
2534 22 : load_file (filename, NULL, false);
2535 22 : free (filename);
2536 :
2537 63 : do_ret:
2538 63 : continue_flag = 0;
2539 63 : end_flag = 0;
2540 63 : gcc_attribute_flag = 0;
2541 63 : openmp_flag = 0;
2542 63 : openacc_flag = 0;
2543 63 : continue_count = 0;
2544 63 : continue_line = 0;
2545 63 : memset (&gfc_current_locus, '\0', sizeof (locus));
2546 63 : memset (&openmp_locus, '\0', sizeof (locus));
2547 63 : memset (&openacc_locus, '\0', sizeof (locus));
2548 63 : memset (&gcc_attribute_locus, '\0', sizeof (locus));
2549 63 : return ret;
2550 : }
2551 :
2552 :
2553 :
2554 : /* Load a file into memory by calling load_line until the file ends. */
2555 :
2556 : static void
2557 65719 : load_file (const char *realfilename, const char *displayedname, bool initial)
2558 : {
2559 65719 : gfc_char_t *line;
2560 65719 : gfc_linebuf *b, *include_b = NULL;
2561 65719 : gfc_file *f;
2562 65719 : FILE *input;
2563 65719 : int len, line_len;
2564 65719 : bool first_line;
2565 65719 : bool allow_bom;
2566 65719 : struct stat st;
2567 65719 : int stat_result;
2568 65719 : const char *filename;
2569 : /* If realfilename and displayedname are different and non-null then
2570 : surely realfilename is the preprocessed form of
2571 : displayedname. */
2572 195979 : bool preprocessed_p = (realfilename && displayedname
2573 65719 : && strcmp (realfilename, displayedname));
2574 :
2575 64541 : filename = displayedname ? displayedname : realfilename;
2576 :
2577 66092 : for (f = current_file; f; f = f->up)
2578 373 : if (filename_cmp (filename, f->filename) == 0)
2579 0 : fatal_error (linemap_line_start (line_table, current_file->line, 0),
2580 : "File %qs is being included recursively", filename);
2581 65719 : if (initial)
2582 : {
2583 32676 : if (gfc_src_file)
2584 : {
2585 4 : input = gfc_src_file;
2586 4 : gfc_src_file = NULL;
2587 : }
2588 : else
2589 32672 : input = gfc_open_file (realfilename);
2590 :
2591 32676 : if (input == NULL)
2592 0 : gfc_fatal_error ("Cannot open file %qs", filename);
2593 : }
2594 : else
2595 : {
2596 33043 : input = gfc_open_included_file (realfilename, false, false);
2597 33043 : if (input == NULL)
2598 : {
2599 : /* For -fpre-include file, current_file is NULL. */
2600 1 : if (current_file)
2601 1 : fatal_error (linemap_line_start (line_table, current_file->line, 0),
2602 : "Cannot open included file %qs", filename);
2603 : else
2604 0 : gfc_fatal_error ("Cannot open pre-included file %qs", filename);
2605 : }
2606 33042 : stat_result = stat (realfilename, &st);
2607 33042 : if (stat_result == 0 && !S_ISREG (st.st_mode))
2608 : {
2609 2 : fclose (input);
2610 2 : if (current_file)
2611 2 : fatal_error (linemap_line_start (line_table, current_file->line, 0),
2612 : "Included file %qs is not a regular file", filename);
2613 : else
2614 0 : gfc_fatal_error ("Included file %qs is not a regular file", filename);
2615 : }
2616 : }
2617 :
2618 : /* Load the file.
2619 :
2620 : A "non-initial" file means a file that is being included. In
2621 : that case we are creating an LC_ENTER map.
2622 :
2623 : An "initial" file means a main file; one that is not included.
2624 : That file has already got at least one (surely more) line map(s)
2625 : created by gfc_init. So the subsequent map created in that case
2626 : must have LC_RENAME reason.
2627 :
2628 : This latter case is not true for a preprocessed file. In that
2629 : case, although the file is "initial", the line maps created by
2630 : gfc_init was used during the preprocessing of the file. Now that
2631 : the preprocessing is over and we are being fed the result of that
2632 : preprocessing, we need to create a brand new line map for the
2633 : preprocessed file, so the reason is going to be LC_ENTER. */
2634 :
2635 99934 : f = get_file (filename, (initial && !preprocessed_p) ? LC_RENAME : LC_ENTER);
2636 65716 : if (!initial)
2637 33040 : add_file_change (f->filename, f->inclusion_line);
2638 65716 : current_file = f;
2639 65716 : current_file->line = 1;
2640 65716 : line = NULL;
2641 65716 : line_len = 0;
2642 65716 : first_line = true;
2643 :
2644 65716 : if (initial && gfc_src_preprocessor_lines[0])
2645 : {
2646 4 : preprocessor_line (gfc_src_preprocessor_lines[0]);
2647 4 : free (gfc_src_preprocessor_lines[0]);
2648 4 : gfc_src_preprocessor_lines[0] = NULL;
2649 4 : if (gfc_src_preprocessor_lines[1])
2650 : {
2651 4 : preprocessor_line (gfc_src_preprocessor_lines[1]);
2652 4 : free (gfc_src_preprocessor_lines[1]);
2653 4 : gfc_src_preprocessor_lines[1] = NULL;
2654 : }
2655 : }
2656 :
2657 : /* Allow BOM on very first line of file. */
2658 65716 : allow_bom = true;
2659 7051645 : for (;;)
2660 : {
2661 7051645 : int trunc = load_line (input, &line, &line_len, NULL, allow_bom);
2662 7051645 : int inc_line;
2663 :
2664 7051645 : allow_bom = false;
2665 7051645 : len = gfc_wide_strlen (line);
2666 7051645 : if (feof (input) && len == 0)
2667 : break;
2668 :
2669 : /* If this is the first line of the file, it can contain a byte
2670 : order mark (BOM), which we will ignore:
2671 : FF FE is UTF-16 little endian,
2672 : FE FF is UTF-16 big endian,
2673 : EF BB BF is UTF-8. */
2674 6985932 : if (first_line
2675 70442 : && ((line_len >= 2 && line[0] == (unsigned char) '\xFF'
2676 3 : && line[1] == (unsigned char) '\xFE')
2677 70439 : || (line_len >= 2 && line[0] == (unsigned char) '\xFE'
2678 1 : && line[1] == (unsigned char) '\xFF')
2679 70438 : || (line_len >= 3 && line[0] == (unsigned char) '\xEF'
2680 3 : && line[1] == (unsigned char) '\xBB'
2681 3 : && line[2] == (unsigned char) '\xBF')))
2682 : {
2683 7 : int n = line[1] == (unsigned char) '\xBB' ? 3 : 2;
2684 7 : gfc_char_t *new_char = gfc_get_wide_string (line_len);
2685 :
2686 7 : wide_strcpy (new_char, &line[n]);
2687 7 : free (line);
2688 7 : line = new_char;
2689 7 : len -= n;
2690 : }
2691 :
2692 : /* There are three things this line can be: a line of Fortran
2693 : source, an include line or a C preprocessor directive. */
2694 :
2695 6985932 : if (line[0] == '#')
2696 : {
2697 : /* When -g3 is specified, it's possible that we emit #define
2698 : and #undef lines, which we need to pass to the middle-end
2699 : so that it can emit correct debug info. */
2700 18222 : if (debug_info_level == DINFO_LEVEL_VERBOSE
2701 9115 : && (wide_strncmp (line, "#define ", 8) == 0
2702 28 : || wide_strncmp (line, "#undef ", 7) == 0))
2703 : ;
2704 : else
2705 : {
2706 9107 : preprocessor_line (line);
2707 9107 : continue;
2708 : }
2709 : }
2710 :
2711 : /* Preprocessed files have preprocessor lines added before the byte
2712 : order mark, so first_line is not about the first line of the file
2713 : but the first line that's not a preprocessor line. */
2714 6976825 : first_line = false;
2715 :
2716 6976825 : inc_line = include_line (line);
2717 6976822 : if (inc_line > 0)
2718 : {
2719 344 : current_file->line++;
2720 344 : continue;
2721 : }
2722 :
2723 : /* Add line. */
2724 :
2725 6976478 : b = XCNEWVAR (gfc_linebuf, gfc_linebuf_header_size
2726 : + (len + 1) * sizeof (gfc_char_t));
2727 :
2728 :
2729 6976478 : b->location
2730 6976478 : = linemap_line_start (line_table, current_file->line++, len);
2731 : /* ??? We add the location for the maximum column possible here,
2732 : because otherwise if the next call creates a new line-map, it
2733 : will not reserve space for any offset. */
2734 6976478 : if (len > 0)
2735 5806112 : linemap_position_for_column (line_table, len);
2736 :
2737 6976478 : b->file = current_file;
2738 6976478 : b->truncated = trunc;
2739 6976478 : wide_strcpy (b->line, line);
2740 :
2741 6976478 : if (line_head == NULL)
2742 32676 : line_head = b;
2743 : else
2744 6943802 : line_tail->next = b;
2745 :
2746 6976478 : line_tail = b;
2747 :
2748 7046753 : while (file_changes_cur < file_changes_count)
2749 70275 : file_changes[file_changes_cur++].lb = b;
2750 :
2751 6976478 : if (flag_dec_include)
2752 : {
2753 37104 : if (include_b && b != include_b)
2754 : {
2755 63 : int inc_line2 = include_stmt (include_b);
2756 63 : if (inc_line2 == 0)
2757 : include_b = NULL;
2758 62 : else if (inc_line2 > 0)
2759 : {
2760 146 : do
2761 : {
2762 84 : if (gfc_current_form == FORM_FIXED)
2763 : {
2764 3650 : for (gfc_char_t *p = include_b->line; *p; p++)
2765 3600 : *p = ' ';
2766 : }
2767 : else
2768 34 : include_b->line[0] = '\0';
2769 84 : if (include_b == b)
2770 : break;
2771 62 : include_b = include_b->next;
2772 62 : }
2773 : while (1);
2774 : include_b = NULL;
2775 : }
2776 : }
2777 37104 : if (inc_line == -1 && !include_b)
2778 23 : include_b = b;
2779 : }
2780 : }
2781 :
2782 : /* Release the line buffer allocated in load_line. */
2783 65713 : free (line);
2784 :
2785 65713 : fclose (input);
2786 :
2787 65713 : if (!initial)
2788 33040 : add_file_change (NULL, current_file->inclusion_line + 1);
2789 65713 : current_file = current_file->up;
2790 65713 : linemap_add (line_table, LC_LEAVE, 0, NULL, 0);
2791 65713 : }
2792 :
2793 :
2794 : /* Open a new file and start scanning from that file. Returns true
2795 : if everything went OK, false otherwise. If form == FORM_UNKNOWN
2796 : it tries to determine the source form from the filename, defaulting
2797 : to free form. */
2798 :
2799 : void
2800 32690 : gfc_new_file (void)
2801 : {
2802 32690 : if (flag_pre_include != NULL)
2803 32674 : load_file (flag_pre_include, NULL, false);
2804 :
2805 32690 : if (gfc_cpp_enabled ())
2806 : {
2807 1193 : if (gfc_cpp_preprocess (gfc_source_file))
2808 : {
2809 1191 : if (!gfc_cpp_preprocess_only ())
2810 1178 : load_file (gfc_cpp_temporary_file (), gfc_source_file, true);
2811 : }
2812 : else
2813 1 : load_file (gfc_source_file, NULL, true);
2814 : }
2815 : else
2816 31497 : load_file (gfc_source_file, NULL, true);
2817 :
2818 32686 : gfc_current_locus.u.lb = line_head;
2819 32686 : gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line;
2820 :
2821 : #if 0 /* Debugging aid. */
2822 : for (; line_head; line_head = line_head->next)
2823 : printf ("%s:%3d %s\n", LOCATION_FILE (line_head->location),
2824 : LOCATION_LINE (line_head->location), line_head->line);
2825 :
2826 : exit (SUCCESS_EXIT_CODE);
2827 : #endif
2828 32686 : }
2829 :
2830 : static char *
2831 10 : unescape_filename (const char *ptr)
2832 : {
2833 10 : const char *p = ptr, *s;
2834 10 : char *d, *ret;
2835 10 : int escaped, unescape = 0;
2836 :
2837 : /* Make filename end at quote. */
2838 10 : escaped = false;
2839 284 : while (*p && ! (! escaped && *p == '"'))
2840 : {
2841 260 : if (escaped)
2842 : escaped = false;
2843 260 : else if (*p == '\\')
2844 : {
2845 14 : escaped = true;
2846 14 : unescape++;
2847 : }
2848 274 : ++p;
2849 : }
2850 :
2851 10 : if (!*p || p[1])
2852 : return NULL;
2853 :
2854 : /* Undo effects of cpp_quote_string. */
2855 10 : s = ptr;
2856 10 : d = XCNEWVEC (char, p + 1 - ptr - unescape);
2857 10 : ret = d;
2858 :
2859 280 : while (s != p)
2860 : {
2861 260 : if (*s == '\\')
2862 14 : *d++ = *++s;
2863 : else
2864 246 : *d++ = *s;
2865 260 : s++;
2866 : }
2867 10 : *d = '\0';
2868 10 : return ret;
2869 : }
2870 :
2871 : /* For preprocessed files, if the first tokens are of the form # NUM.
2872 : handle the directives so we know the original file name. */
2873 :
2874 : const char *
2875 5 : gfc_read_orig_filename (const char *filename, const char **canon_source_file)
2876 : {
2877 5 : int c, len;
2878 5 : char *dirname, *tmp;
2879 :
2880 5 : gfc_src_file = gfc_open_file (filename);
2881 5 : if (gfc_src_file == NULL)
2882 : return NULL;
2883 :
2884 5 : c = getc (gfc_src_file);
2885 :
2886 5 : if (c != '#')
2887 : return NULL;
2888 :
2889 5 : len = 0;
2890 5 : load_line (gfc_src_file, &gfc_src_preprocessor_lines[0], &len, &c);
2891 :
2892 5 : if (wide_strncmp (gfc_src_preprocessor_lines[0], "# 1 \"", 5) != 0)
2893 : return NULL;
2894 :
2895 5 : tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[0][5], -1);
2896 5 : filename = unescape_filename (tmp);
2897 5 : free (tmp);
2898 5 : if (filename == NULL)
2899 : return NULL;
2900 :
2901 5 : c = getc (gfc_src_file);
2902 :
2903 5 : if (c != '#')
2904 : return filename;
2905 :
2906 5 : len = 0;
2907 5 : load_line (gfc_src_file, &gfc_src_preprocessor_lines[1], &len, &c);
2908 :
2909 5 : if (wide_strncmp (gfc_src_preprocessor_lines[1], "# 1 \"", 5) != 0)
2910 : return filename;
2911 :
2912 5 : tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[1][5], -1);
2913 5 : dirname = unescape_filename (tmp);
2914 5 : free (tmp);
2915 5 : if (dirname == NULL)
2916 : return filename;
2917 :
2918 5 : len = strlen (dirname);
2919 5 : if (len < 3 || dirname[len - 1] != '/' || dirname[len - 2] != '/')
2920 : {
2921 3 : free (dirname);
2922 3 : return filename;
2923 : }
2924 2 : dirname[len - 2] = '\0';
2925 2 : set_src_pwd (dirname);
2926 :
2927 2 : if (! IS_ABSOLUTE_PATH (filename))
2928 : {
2929 2 : char *p = XCNEWVEC (char, len + strlen (filename));
2930 :
2931 2 : memcpy (p, dirname, len - 2);
2932 2 : p[len - 2] = '/';
2933 2 : strcpy (p + len - 1, filename);
2934 2 : *canon_source_file = p;
2935 : }
2936 :
2937 2 : free (dirname);
2938 2 : return filename;
2939 : }
|