Debian Codegraph @

gcc/fortran/scanner.cc

raw
1 /* Character scanner.
2 Copyright (C) 2000-2025 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 gfc_wide_fits_in_byte (gfc_char_t c)
91 {
92 return (c <= UCHAR_MAX);
93 }
94
95 static inline int
96 wide_is_ascii (gfc_char_t c)
97 {
98 return (gfc_wide_fits_in_byte (c) && ((unsigned char) c & ~0x7f) == 0);
99 }
100
101 bool
102 gfc_wide_is_printable (gfc_char_t c)
103 {
104 return (gfc_wide_fits_in_byte (c) && ISPRINT ((unsigned char) c));
105 }
106
107 gfc_char_t
108 gfc_wide_tolower (gfc_char_t c)
109 {
110 return (wide_is_ascii (c) ? (gfc_char_t) TOLOWER((unsigned char) c) : c);
111 }
112
113 gfc_char_t
114 gfc_wide_toupper (gfc_char_t c)
115 {
116 return (wide_is_ascii (c) ? (gfc_char_t) TOUPPER((unsigned char) c) : c);
117 }
118
119 bool
120 gfc_wide_is_digit (gfc_char_t c)
121 {
122 return (c >= '0' && c <= '9');
123 }
124
125 static inline int
126 wide_atoi (gfc_char_t *c)
127 {
128 #define MAX_DIGITS 20
129 char buf[MAX_DIGITS+1];
130 int i = 0;
131
132 while (gfc_wide_is_digit(*c) && i < MAX_DIGITS)
133 buf[i++] = *c++;
134 buf[i] = '\0';
135 return atoi (buf);
136 }
137
138 size_t
139 gfc_wide_strlen (const gfc_char_t *str)
140 {
141 size_t i;
142
143 for (i = 0; str[i]; i++)
144 ;
145
146 return i;
147 }
148
149 gfc_char_t *
150 gfc_wide_memset (gfc_char_t *b, gfc_char_t c, size_t len)
151 {
152 size_t i;
153
154 for (i = 0; i < len; i++)
155 b[i] = c;
156
157 return b;
158 }
159
160 static gfc_char_t *
161 wide_strcpy (gfc_char_t *dest, const gfc_char_t *src)
162 {
163 gfc_char_t *d;
164
165 for (d = dest; (*d = *src) != '\0'; ++src, ++d)
166 ;
167
168 return dest;
169 }
170
171 static gfc_char_t *
172 wide_strchr (const gfc_char_t *s, gfc_char_t c)
173 {
174 do {
175 if (*s == c)
176 {
177 return CONST_CAST(gfc_char_t *, s);
178 }
179 } while (*s++);
180 return 0;
181 }
182
183 char *
184 gfc_widechar_to_char (const gfc_char_t *s, int length)
185 {
186 size_t len, i;
187 char *res;
188
189 if (s == NULL)
190 return NULL;
191
192 /* Passing a negative length is used to indicate that length should be
193 calculated using gfc_wide_strlen(). */
194 len = (length >= 0 ? (size_t) length : gfc_wide_strlen (s));
195 res = XNEWVEC (char, len + 1);
196
197 for (i = 0; i < len; i++)
198 {
199 gcc_assert (gfc_wide_fits_in_byte (s[i]));
200 res[i] = (unsigned char) s[i];
201 }
202
203 res[len] = '\0';
204 return res;
205 }
206
207 gfc_char_t *
208 gfc_char_to_widechar (const char *s)
209 {
210 size_t len, i;
211 gfc_char_t *res;
212
213 if (s == NULL)
214 return NULL;
215
216 len = strlen (s);
217 res = gfc_get_wide_string (len + 1);
218
219 for (i = 0; i < len; i++)
220 res[i] = (unsigned char) s[i];
221
222 res[len] = '\0';
223 return res;
224 }
225
226 static int
227 wide_strncmp (const gfc_char_t *s1, const char *s2, size_t n)
228 {
229 gfc_char_t c1, c2;
230
231 while (n-- > 0)
232 {
233 c1 = *s1++;
234 c2 = *s2++;
235 if (c1 != c2)
236 return (c1 > c2 ? 1 : -1);
237 if (c1 == '\0')
238 return 0;
239 }
240 return 0;
241 }
242
243 int
244 gfc_wide_strncasecmp (const gfc_char_t *s1, const char *s2, size_t n)
245 {
246 gfc_char_t c1, c2;
247
248 while (n-- > 0)
249 {
250 c1 = gfc_wide_tolower (*s1++);
251 c2 = TOLOWER (*s2++);
252 if (c1 != c2)
253 return (c1 > c2 ? 1 : -1);
254 if (c1 == '\0')
255 return 0;
256 }
257 return 0;
258 }
259
260
261 /* Main scanner initialization. */
262
263 void
264 gfc_scanner_init_1 (void)
265 {
266 file_head = NULL;
267 line_head = NULL;
268 line_tail = NULL;
269
270 continue_count = 0;
271 continue_line = 0;
272
273 end_flag = 0;
274 last_error_char = NULL;
275 }
276
277
278 /* Main scanner destructor. */
279
280 void
281 gfc_scanner_done_1 (void)
282 {
283 gfc_linebuf *lb;
284 gfc_file *f;
285
286 while(line_head != NULL)
287 {
288 lb = line_head->next;
289 free (line_head);
290 line_head = lb;
291 }
292
293 while(file_head != NULL)
294 {
295 f = file_head->next;
296 free (file_head->filename);
297 free (file_head);
298 file_head = f;
299 }
300 }
301
302 static bool
303 gfc_do_check_include_dir (const char *path, bool warn)
304 {
305 struct stat st;
306 if (stat (path, &st))
307 {
308 if (errno != ENOENT)
309 gfc_warning_now (0, "Include directory %qs: %s",
310 path, xstrerror(errno));
311 else if (warn)
312 gfc_warning_now (OPT_Wmissing_include_dirs,
313 "Nonexistent include directory %qs", path);
314 return false;
315 }
316 else if (!S_ISDIR (st.st_mode))
317 {
318 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 gfc_do_check_include_dirs (gfc_directorylist **list, bool do_warn)
328 {
329 gfc_directorylist *prev, *q, *n;
330 prev = NULL;
331 n = *list;
332 while (n)
333 {
334 q = n; n = n->next;
335 if (gfc_do_check_include_dir (q->path, q->warn && do_warn))
336 {
337 prev = q;
338 continue;
339 }
340 if (prev == NULL)
341 *list = n;
342 else
343 prev->next = n;
344 free (q->path);
345 free (q);
346 }
347 }
348
349 void
350 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 bool warn = !gfc_cpp_enabled () || !verbose_missing_dir_warn;
356 gfc_do_check_include_dirs (&include_dirs, warn);
357 gfc_do_check_include_dirs (&intrinsic_modules_dirs, verbose_missing_dir_warn);
358 if (gfc_option.module_dir && gfc_cpp_enabled ())
359 gfc_do_check_include_dirs (&include_dirs, true);
360 }
361
362 /* Adds path to the list pointed to by list. */
363
364 static void
365 add_path_to_list (gfc_directorylist **list, const char *path,
366 bool use_for_modules, bool head, bool warn, bool defer_warn)
367 {
368 gfc_directorylist *dir;
369 const char *p;
370 char *q;
371 size_t len;
372 int i;
373
374 p = path;
375 while (*p == ' ' || *p == '\t') /* someone might do "-I include" */
376 if (*p++ == '\0')
377 return;
378
379 /* Strip trailing directory separators from the path, as this
380 will confuse Windows systems. */
381 len = strlen (p);
382 q = (char *) alloca (len + 1);
383 memcpy (q, p, len + 1);
384 i = len - 1;
385 while (i >=0 && IS_DIR_SEPARATOR (q[i]))
386 q[i--] = '\0';
387
388 if (!defer_warn && !gfc_do_check_include_dir (q, warn))
389 return;
390
391 if (head || *list == NULL)
392 {
393 dir = XCNEW (gfc_directorylist);
394 if (!head)
395 *list = dir;
396 }
397 else
398 {
399 dir = *list;
400 while (dir->next)
401 dir = dir->next;
402
403 dir->next = XCNEW (gfc_directorylist);
404 dir = dir->next;
405 }
406
407 dir->next = head ? *list : NULL;
408 if (head)
409 *list = dir;
410 dir->use_for_modules = use_for_modules;
411 dir->warn = warn;
412 dir->path = xstrdup (p);
413 }
414
415 /* defer_warn is set to true while parsing the commandline. */
416
417 void
418 gfc_add_include_path (const char *path, bool use_for_modules, bool file_dir,
419 bool warn, bool defer_warn)
420 {
421 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 if (!file_dir)
426 gfc_cpp_add_include_path (xstrdup(path), true);
427 }
428
429
430 void
431 gfc_add_intrinsic_modules_path (const char *path)
432 {
433 add_path_to_list (&intrinsic_modules_dirs, path, true, false, false, false);
434 }
435
436
437 /* Release resources allocated for options. */
438
439 void
440 gfc_release_include_path (void)
441 {
442 gfc_directorylist *p;
443
444 while (include_dirs != NULL)
445 {
446 p = include_dirs;
447 include_dirs = include_dirs->next;
448 free (p->path);
449 free (p);
450 }
451
452 while (intrinsic_modules_dirs != NULL)
453 {
454 p = intrinsic_modules_dirs;
455 intrinsic_modules_dirs = intrinsic_modules_dirs->next;
456 free (p->path);
457 free (p);
458 }
459
460 free (gfc_option.module_dir);
461 }
462
463
464 static FILE *
465 open_included_file (const char *name, gfc_directorylist *list,
466 bool module, bool system)
467 {
468 char *fullname;
469 gfc_directorylist *p;
470 FILE *f;
471
472 for (p = list; p; p = p->next)
473 {
474 if (module && !p->use_for_modules)
475 continue;
476
477 fullname = (char *) alloca(strlen (p->path) + strlen (name) + 2);
478 strcpy (fullname, p->path);
479 strcat (fullname, "/");
480 strcat (fullname, name);
481
482 f = gfc_open_file (fullname);
483 if (f != NULL)
484 {
485 if (gfc_cpp_makedep ())
486 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 gfc_open_included_file (const char *name, bool include_cwd, bool module)
502 {
503 FILE *f = NULL;
504
505 if (IS_ABSOLUTE_PATH (name) || include_cwd)
506 {
507 f = gfc_open_file (name);
508 if (f && gfc_cpp_makedep ())
509 gfc_cpp_add_dep (name, false);
510 }
511
512 if (!f)
513 f = open_included_file (name, include_dirs, module, false);
514
515 return f;
516 }
517
518
519 /* Test to see if we're at the end of the main source file. */
520
521 bool
522 gfc_at_end (void)
523 {
524 return end_flag;
525 }
526
527
528 /* Test to see if we're at the end of the current file. */
529
530 bool
531 gfc_at_eof (void)
532 {
533 if (gfc_at_end ())
534 return 1;
535
536 if (line_head == NULL)
537 return 1; /* Null file */
538
539 if (gfc_current_locus.u.lb == NULL)
540 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 gfc_at_bol (void)
550 {
551 if (gfc_at_eof ())
552 return 1;
553
554 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 gfc_at_eol (void)
562 {
563 if (gfc_at_eof ())
564 return 1;
565
566 return (*gfc_current_locus.nextc == '\0');
567 }
568
569 static void
570 add_file_change (const char *filename, int line)
571 {
572 if (file_changes_count == file_changes_allocated)
573 {
574 if (file_changes_allocated)
575 file_changes_allocated *= 2;
576 else
577 file_changes_allocated = 16;
578 file_changes = XRESIZEVEC (struct gfc_file_change, file_changes,
579 file_changes_allocated);
580 }
581 file_changes[file_changes_count].filename = filename;
582 file_changes[file_changes_count].lb = NULL;
583 file_changes[file_changes_count++].line = line;
584 }
585
586 static void
587 report_file_change (gfc_linebuf *lb)
588 {
589 size_t c = file_changes_cur;
590 while (c < file_changes_count
591 && file_changes[c].lb == lb)
592 {
593 if (file_changes[c].filename)
594 (*debug_hooks->start_source_file) (file_changes[c].line,
595 file_changes[c].filename);
596 else
597 (*debug_hooks->end_source_file) (file_changes[c].line);
598 ++c;
599 }
600 file_changes_cur = c;
601 }
602
603 void
604 gfc_start_source_files (void)
605 {
606 /* If the debugger wants the name of the main source file,
607 we give it. */
608 if (debug_hooks->start_end_main_source_file)
609 (*debug_hooks->start_source_file) (0, gfc_source_file);
610
611 file_changes_cur = 0;
612 report_file_change (gfc_current_locus.u.lb);
613 }
614
615 void
616 gfc_end_source_files (void)
617 {
618 report_file_change (NULL);
619
620 if (debug_hooks->start_end_main_source_file)
621 (*debug_hooks->end_source_file) (0);
622 }
623
624 /* Advance the current line pointer to the next line. */
625
626 void
627 gfc_advance_line (void)
628 {
629 if (gfc_at_end ())
630 return;
631
632 if (gfc_current_locus.u.lb == NULL)
633 {
634 end_flag = 1;
635 return;
636 }
637
638 if (gfc_current_locus.u.lb->next
639 && !gfc_current_locus.u.lb->next->dbg_emitted)
640 {
641 report_file_change (gfc_current_locus.u.lb->next);
642 gfc_current_locus.u.lb->next->dbg_emitted = true;
643 }
644
645 gfc_current_locus.u.lb = gfc_current_locus.u.lb->next;
646
647 if (gfc_current_locus.u.lb != NULL)
648 gfc_current_locus.nextc = gfc_current_locus.u.lb->line;
649 else
650 {
651 gfc_current_locus.nextc = NULL;
652 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 next_char (void)
668 {
669 gfc_char_t c;
670
671 if (gfc_current_locus.nextc == NULL)
672 return '\n';
673
674 c = *gfc_current_locus.nextc++;
675 if (c == '\0')
676 {
677 gfc_current_locus.nextc--; /* Remain on this line. */
678 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 skip_comment_line (void)
692 {
693 gfc_char_t c;
694
695 do
696 {
697 c = next_char ();
698 }
699 while (c != '\n');
700
701 gfc_advance_line ();
702 }
703
704
705 bool
706 gfc_define_undef_line (void)
707 {
708 char *tmp;
709
710 /* All lines beginning with '#' are either #define or #undef. */
711 if (debug_info_level != DINFO_LEVEL_VERBOSE || gfc_peek_ascii_char () != '#')
712 return 0;
713
714 if (wide_strncmp (gfc_current_locus.nextc, "#define ", 8) == 0)
715 {
716 tmp = gfc_widechar_to_char (&gfc_current_locus.nextc[8], -1);
717 (*debug_hooks->define) (gfc_linebuf_linenum (gfc_current_locus.u.lb),
718 tmp);
719 free (tmp);
720 }
721
722 if (wide_strncmp (gfc_current_locus.nextc, "#undef ", 7) == 0)
723 {
724 tmp = gfc_widechar_to_char (&gfc_current_locus.nextc[7], -1);
725 (*debug_hooks->undef) (gfc_linebuf_linenum (gfc_current_locus.u.lb),
726 tmp);
727 free (tmp);
728 }
729
730 /* Skip the rest of the line. */
731 skip_comment_line ();
732
733 return 1;
734 }
735
736
737 /* Return true if GCC$ was matched. */
738 static bool
739 skip_gcc_attribute (locus start)
740 {
741 bool r = false;
742 char c;
743 locus old_loc = gfc_current_locus;
744
745 if ((c = next_char ()) == 'g' || c == 'G')
746 if ((c = next_char ()) == 'c' || c == 'C')
747 if ((c = next_char ()) == 'c' || c == 'C')
748 if ((c = next_char ()) == '$')
749 r = true;
750
751 if (r == false)
752 gfc_current_locus = old_loc;
753 else
754 {
755 gcc_attribute_flag = 1;
756 gcc_attribute_locus = old_loc;
757 gfc_current_locus = start;
758 }
759
760 return r;
761 }
762
763 /* Return true if CC was matched. */
764 static bool
765 skip_free_oacc_sentinel (locus start, locus old_loc)
766 {
767 bool r = false;
768 char c;
769
770 if ((c = next_char ()) == 'c' || c == 'C')
771 if ((c = next_char ()) == 'c' || c == 'C')
772 r = true;
773
774 if (r)
775 {
776 if ((c = next_char ()) == ' ' || c == '\t'
777 || continue_flag)
778 {
779 if (!continue_flag && (c == ' ' || c == '\t'))
780 openmp_flag = 0;
781 while (gfc_is_whitespace (c))
782 c = next_char ();
783 if (c != '\n' && c != '!')
784 {
785 openacc_flag = 1;
786 openacc_locus = old_loc;
787 gfc_current_locus = start;
788 }
789 else
790 r = false;
791 }
792 else
793 {
794 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 r = false;
799 }
800 }
801
802 return r;
803 }
804
805 /* Return true if MP was matched. */
806 static bool
807 skip_free_omp_sentinel (locus start, locus old_loc)
808 {
809 bool r = false;
810 char c;
811
812 if ((c = next_char ()) == 'm' || c == 'M')
813 if ((c = next_char ()) == 'p' || c == 'P')
814 r = true;
815
816 if (r)
817 {
818 if ((c = next_char ()) == ' ' || c == '\t'
819 || continue_flag)
820 {
821 if (!continue_flag && (c == ' ' || c == '\t'))
822 openacc_flag = 0;
823 while (gfc_is_whitespace (c))
824 c = next_char ();
825 if (c != '\n' && c != '!')
826 {
827 openmp_flag = 1;
828 openmp_locus = old_loc;
829 gfc_current_locus = start;
830 }
831 else
832 r = false;
833 }
834 else
835 {
836 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 r = false;
841 }
842 }
843
844 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 skip_free_comments (void)
854 {
855 locus start;
856 gfc_char_t c;
857 int at_bol;
858
859 for (;;)
860 {
861 at_bol = gfc_at_bol ();
862 start = gfc_current_locus;
863 if (gfc_at_eof ())
864 break;
865
866 do
867 c = next_char ();
868 while (gfc_is_whitespace (c));
869
870 if (c == '\n')
871 {
872 gfc_advance_line ();
873 continue;
874 }
875
876 if (c == '!')
877 {
878 /* Keep the !GCC$ line. */
879 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 if (at_bol)
888 {
889 if ((flag_openmp || flag_openmp_simd)
890 && flag_openacc)
891 {
892 locus old_loc = gfc_current_locus;
893 if (next_char () == '$')
894 {
895 c = next_char ();
896 if (c == 'o' || c == 'O')
897 {
898 if (skip_free_omp_sentinel (start, old_loc))
899 return false;
900 gfc_current_locus = old_loc;
901 next_char ();
902 c = next_char ();
903 }
904 else if (c == 'a' || c == 'A')
905 {
906 if (skip_free_oacc_sentinel (start, old_loc))
907 return false;
908 gfc_current_locus = old_loc;
909 next_char ();
910 c = next_char ();
911 }
912 if (continue_flag || c == ' ' || c == '\t')
913 {
914 gfc_current_locus = old_loc;
915 next_char ();
916 openmp_flag = openacc_flag = 0;
917 return true;
918 }
919 }
920 gfc_current_locus = old_loc;
921 }
922 else if ((flag_openmp || flag_openmp_simd)
923 && !flag_openacc)
924 {
925 locus old_loc = gfc_current_locus;
926 if (next_char () == '$')
927 {
928 c = next_char ();
929 if (c == 'o' || c == 'O')
930 {
931 if (skip_free_omp_sentinel (start, old_loc))
932 return false;
933 gfc_current_locus = old_loc;
934 next_char ();
935 c = next_char ();
936 }
937 if (continue_flag || c == ' ' || c == '\t')
938 {
939 gfc_current_locus = old_loc;
940 next_char ();
941 openmp_flag = 0;
942 return true;
943 }
944 }
945 gfc_current_locus = old_loc;
946 }
947 else if (flag_openacc
948 && !(flag_openmp || flag_openmp_simd))
949 {
950 locus old_loc = gfc_current_locus;
951 if (next_char () == '$')
952 {
953 c = next_char ();
954 if (c == 'a' || c == 'A')
955 {
956 if (skip_free_oacc_sentinel (start, old_loc))
957 return false;
958 gfc_current_locus = old_loc;
959 next_char();
960 c = next_char();
961 }
962 }
963 gfc_current_locus = old_loc;
964 }
965 }
966 skip_comment_line ();
967 continue;
968 }
969
970 break;
971 }
972
973 if (openmp_flag && at_bol)
974 openmp_flag = 0;
975
976 if (openacc_flag && at_bol)
977 openacc_flag = 0;
978
979 gcc_attribute_flag = 0;
980 gfc_current_locus = start;
981 return false;
982 }
983
984 /* Return true if MP was matched in fixed form. */
985 static bool
986 skip_fixed_omp_sentinel (locus *start)
987 {
988 gfc_char_t c;
989 if ((c = next_char ()) != 'm' && c != 'M')
990 return false;
991 if ((c = next_char ()) == 'p' || c == 'P')
992 {
993 c = next_char ();
994 if (c != '\n'
995 && (continue_flag
996 || c == ' ' || c == '\t' || c == '0'))
997 {
998 if (c == ' ' || c == '\t' || c == '0')
999 openacc_flag = 0;
1000 do
1001 c = next_char ();
1002 while (gfc_is_whitespace (c));
1003 if (c != '\n' && c != '!')
1004 {
1005 /* Canonicalize to *$omp. */
1006 *start->nextc = '*';
1007 openmp_flag = 1;
1008 gfc_current_locus = *start;
1009 return true;
1010 }
1011 }
1012 }
1013 else if (UNLIKELY (c == 'x' || c == 'X'))
1014 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 skip_fixed_oacc_sentinel (locus *start)
1022 {
1023 gfc_char_t c;
1024 if (((c = next_char ()) == 'c' || c == 'C')
1025 && ((c = next_char ()) == 'c' || c == 'C'))
1026 {
1027 c = next_char ();
1028 if (c != '\n'
1029 && (continue_flag
1030 || c == ' ' || c == '\t' || c == '0'))
1031 {
1032 if (c == ' ' || c == '\t' || c == '0')
1033 openmp_flag = 0;
1034 do
1035 c = next_char ();
1036 while (gfc_is_whitespace (c));
1037 if (c != '\n' && c != '!')
1038 {
1039 /* Canonicalize to *$acc. */
1040 *start->nextc = '*';
1041 openacc_flag = 1;
1042 gfc_current_locus = *start;
1043 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 skip_fixed_comments (void)
1057 {
1058 locus start;
1059 int col;
1060 gfc_char_t c;
1061
1062 if (! gfc_at_bol ())
1063 {
1064 start = gfc_current_locus;
1065 if (! gfc_at_eof ())
1066 {
1067 do
1068 c = next_char ();
1069 while (gfc_is_whitespace (c));
1070
1071 if (c == '\n')
1072 gfc_advance_line ();
1073 else if (c == '!')
1074 skip_comment_line ();
1075 }
1076
1077 if (! gfc_at_bol ())
1078 {
1079 gfc_current_locus = start;
1080 return;
1081 }
1082 }
1083
1084 for (;;)
1085 {
1086 start = gfc_current_locus;
1087 if (gfc_at_eof ())
1088 break;
1089
1090 c = next_char ();
1091 if (c == '\n')
1092 {
1093 gfc_advance_line ();
1094 continue;
1095 }
1096
1097 if (c == '!' || c == 'c' || c == 'C' || c == '*')
1098 {
1099 if (skip_gcc_attribute (start))
1100 {
1101 /* Canonicalize to *$omp. */
1102 *start.nextc = '*';
1103 return;
1104 }
1105
1106 if (gfc_current_locus.u.lb != NULL
1107 && continue_line < gfc_linebuf_linenum (gfc_current_locus.u.lb))
1108 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 if ((flag_openmp || flag_openmp_simd) && !flag_openacc)
1118 {
1119 if (next_char () == '$')
1120 {
1121 c = next_char ();
1122 if (c == 'o' || c == 'O')
1123 {
1124 if (skip_fixed_omp_sentinel (&start))
1125 return;
1126 }
1127 else
1128 goto check_for_digits;
1129 }
1130 gfc_current_locus = start;
1131 }
1132 else if (flag_openacc && !(flag_openmp || flag_openmp_simd))
1133 {
1134 if (next_char () == '$')
1135 {
1136 c = next_char ();
1137 if (c == 'a' || c == 'A')
1138 {
1139 if (skip_fixed_oacc_sentinel (&start))
1140 return;
1141 }
1142 }
1143 gfc_current_locus = start;
1144 }
1145 else if (flag_openacc || flag_openmp || flag_openmp_simd)
1146 {
1147 if (next_char () == '$')
1148 {
1149 c = next_char ();
1150 if (c == 'a' || c == 'A')
1151 {
1152 if (skip_fixed_oacc_sentinel (&start))
1153 return;
1154 }
1155 else if (c == 'o' || c == 'O')
1156 {
1157 if (skip_fixed_omp_sentinel (&start))
1158 return;
1159 }
1160 else
1161 goto check_for_digits;
1162 }
1163 gfc_current_locus = start;
1164 }
1165
1166 skip_comment_line ();
1167 continue;
1168
1169 check_for_digits:
1170 {
1171 /* Required for OpenMP's conditional compilation sentinel. */
1172 int digit_seen = 0;
1173
1174 for (col = 3; col < 6; col++, c = next_char ())
1175 if (c == ' ')
1176 continue;
1177 else if (c == '\t')
1178 {
1179 col = 6;
1180 break;
1181 }
1182 else if (c < '0' || c > '9')
1183 break;
1184 else
1185 digit_seen = 1;
1186
1187 if (col == 6 && c != '\n'
1188 && ((continue_flag && !digit_seen)
1189 || c == ' ' || c == '\t' || c == '0'))
1190 {
1191 gfc_current_locus = start;
1192 start.nextc[0] = ' ';
1193 start.nextc[1] = ' ';
1194 continue;
1195 }
1196 }
1197 skip_comment_line ();
1198 continue;
1199 }
1200
1201 if (gfc_option.flag_d_lines != -1 && (c == 'd' || c == 'D'))
1202 {
1203 if (gfc_option.flag_d_lines == 0)
1204 {
1205 skip_comment_line ();
1206 continue;
1207 }
1208 else
1209 *start.nextc = c = ' ';
1210 }
1211
1212 col = 1;
1213
1214 while (gfc_is_whitespace (c))
1215 {
1216 c = next_char ();
1217 col++;
1218 }
1219
1220 if (c == '\n')
1221 {
1222 gfc_advance_line ();
1223 continue;
1224 }
1225
1226 if (col != 6 && c == '!')
1227 {
1228 if (gfc_current_locus.u.lb != NULL
1229 && continue_line < gfc_linebuf_linenum (gfc_current_locus.u.lb))
1230 continue_line = gfc_linebuf_linenum (gfc_current_locus.u.lb);
1231 skip_comment_line ();
1232 continue;
1233 }
1234
1235 break;
1236 }
1237
1238 openmp_flag = 0;
1239 openacc_flag = 0;
1240 gcc_attribute_flag = 0;
1241 gfc_current_locus = start;
1242 }
1243
1244
1245 /* Skips the current line if it is a comment. */
1246
1247 void
1248 gfc_skip_comments (void)
1249 {
1250 if (gfc_current_form == FORM_FREE)
1251 skip_free_comments ();
1252 else
1253 skip_fixed_comments ();
1254 }
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 gfc_next_char_literal (gfc_instring in_string)
1266 {
1267 static locus omp_acc_err_loc = {};
1268 locus old_loc;
1269 int i, prev_openmp_flag, prev_openacc_flag;
1270 gfc_char_t c;
1271
1272 continue_flag = 0;
1273 prev_openacc_flag = prev_openmp_flag = 0;
1274
1275 restart:
1276 c = next_char ();
1277 if (gfc_at_end ())
1278 {
1279 continue_count = 0;
1280 return c;
1281 }
1282
1283 if (gfc_current_form == FORM_FREE)
1284 {
1285 bool openmp_cond_flag;
1286
1287 if (!in_string && c == '!')
1288 {
1289 if (gcc_attribute_flag
1290 && memcmp (&gfc_current_locus, &gcc_attribute_locus,
1291 sizeof (gfc_current_locus)) == 0)
1292 goto done;
1293
1294 if (openmp_flag
1295 && memcmp (&gfc_current_locus, &openmp_locus,
1296 sizeof (gfc_current_locus)) == 0)
1297 goto done;
1298
1299 if (openacc_flag
1300 && memcmp (&gfc_current_locus, &openacc_locus,
1301 sizeof (gfc_current_locus)) == 0)
1302 goto done;
1303
1304 /* This line can't be continued */
1305 do
1306 {
1307 c = next_char ();
1308 }
1309 while (c != '\n');
1310
1311 /* Avoid truncation warnings for comment ending lines. */
1312 gfc_current_locus.u.lb->truncated = 0;
1313
1314 goto done;
1315 }
1316
1317 /* Check to see if the continuation line was truncated. */
1318 if (warn_line_truncation && gfc_current_locus.u.lb != NULL
1319 && gfc_current_locus.u.lb->truncated)
1320 {
1321 int maxlen = flag_free_line_length;
1322 gfc_char_t *current_nextc = gfc_current_locus.nextc;
1323
1324 gfc_current_locus.u.lb->truncated = 0;
1325 gfc_current_locus.nextc = gfc_current_locus.u.lb->line + maxlen;
1326 gfc_warning_now (OPT_Wline_truncation,
1327 "Line truncated at %L", &gfc_current_locus);
1328 gfc_current_locus.nextc = current_nextc;
1329 }
1330
1331 if (c != '&')
1332 goto done;
1333
1334 /* If the next nonblank character is a ! or \n, we've got a
1335 continuation line. */
1336 old_loc = gfc_current_locus;
1337
1338 c = next_char ();
1339 while (gfc_is_whitespace (c))
1340 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 if (in_string && c != '\n')
1348 {
1349 gfc_current_locus = old_loc;
1350 c = '&';
1351 goto done;
1352 }
1353
1354 if (c != '!' && c != '\n')
1355 {
1356 gfc_current_locus = old_loc;
1357 c = '&';
1358 goto done;
1359 }
1360
1361 if (flag_openmp)
1362 prev_openmp_flag = openmp_flag;
1363 if (flag_openacc)
1364 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 if (continue_count == 0
1370 && gfc_current_locus.u.lb
1371 && continue_line > gfc_linebuf_linenum (gfc_current_locus.u.lb) + 1)
1372 continue_line = gfc_linebuf_linenum (gfc_current_locus.u.lb) + 1;
1373
1374 continue_flag = 1;
1375 if (c == '!')
1376 skip_comment_line ();
1377 else
1378 gfc_advance_line ();
1379
1380 if (gfc_at_eof ())
1381 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 if (gfc_linebuf_linenum (gfc_current_locus.u.lb) == continue_line + 1)
1387 {
1388 if (++continue_count == gfc_option.max_continue_free)
1389 {
1390 if (gfc_notification_std (GFC_STD_GNU) || pedantic)
1391 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 openmp_cond_flag = skip_free_comments ();
1398
1399 if (gfc_current_locus.u.lb != NULL
1400 && continue_line < gfc_linebuf_linenum (gfc_current_locus.u.lb))
1401 continue_line = gfc_linebuf_linenum (gfc_current_locus.u.lb);
1402
1403 if (flag_openmp)
1404 if (prev_openmp_flag != openmp_flag && !openacc_flag)
1405 {
1406 gfc_current_locus = old_loc;
1407 openmp_flag = prev_openmp_flag;
1408 c = '&';
1409 goto done;
1410 }
1411
1412 if (flag_openacc)
1413 if (prev_openacc_flag != openacc_flag && !openmp_flag)
1414 {
1415 gfc_current_locus = old_loc;
1416 openacc_flag = prev_openacc_flag;
1417 c = '&';
1418 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 old_loc = gfc_current_locus;
1427
1428 c = next_char ();
1429 while (gfc_is_whitespace (c))
1430 c = next_char ();
1431
1432 if (openmp_flag && !openacc_flag)
1433 {
1434 for (i = 0; i < 5; i++, c = next_char ())
1435 {
1436 gcc_assert (gfc_wide_tolower (c) == (unsigned char) "!$omp"[i]);
1437 if (i == 4)
1438 old_loc = gfc_current_locus;
1439 }
1440 while (gfc_is_whitespace (c))
1441 c = next_char ();
1442 }
1443 if (openacc_flag && !openmp_flag)
1444 {
1445 for (i = 0; i < 5; i++, c = next_char ())
1446 {
1447 gcc_assert (gfc_wide_tolower (c) == (unsigned char) "!$acc"[i]);
1448 if (i == 4)
1449 old_loc = gfc_current_locus;
1450 }
1451 while (gfc_is_whitespace (c))
1452 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 if (openacc_flag && openmp_flag)
1460 {
1461 int is_openmp = 0;
1462 for (i = 0; i < 5; i++, c = next_char ())
1463 {
1464 if (gfc_wide_tolower (c) != (unsigned char) "!$acc"[i])
1465 is_openmp = 1;
1466 }
1467 if (omp_acc_err_loc.nextc != gfc_current_locus.nextc
1468 || omp_acc_err_loc.u.lb != gfc_current_locus.u.lb)
1469 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 omp_acc_err_loc = gfc_current_locus;
1475 goto not_continuation;
1476 }
1477
1478 if (c != '&')
1479 {
1480 if (in_string && gfc_current_locus.nextc)
1481 {
1482 gfc_current_locus.nextc--;
1483 if (warn_ampersand && in_string == INSTRING_WARN)
1484 gfc_warning (OPT_Wampersand,
1485 "Missing %<&%> in continued character "
1486 "constant at %C");
1487 }
1488 else if (!in_string && (c == '\'' || c == '"'))
1489 goto done;
1490 /* Both !$omp and !$ -fopenmp continuation lines have & on the
1491 continuation line only optionally. */
1492 else if (openmp_flag || openacc_flag || openmp_cond_flag)
1493 {
1494 if (gfc_current_locus.nextc)
1495 gfc_current_locus.nextc--;
1496 }
1497 else
1498 {
1499 c = ' ';
1500 gfc_current_locus = old_loc;
1501 goto done;
1502 }
1503 }
1504 }
1505 else /* Fixed form. */
1506 {
1507 /* Fixed form continuation. */
1508 if (in_string != INSTRING_WARN && c == '!')
1509 {
1510 /* Skip comment at end of line. */
1511 do
1512 {
1513 c = next_char ();
1514 }
1515 while (c != '\n');
1516
1517 /* Avoid truncation warnings for comment ending lines. */
1518 gfc_current_locus.u.lb->truncated = 0;
1519 }
1520
1521 if (c != '\n')
1522 goto done;
1523
1524 /* Check to see if the continuation line was truncated. */
1525 if (warn_line_truncation && gfc_current_locus.u.lb != NULL
1526 && gfc_current_locus.u.lb->truncated)
1527 {
1528 gfc_current_locus.u.lb->truncated = 0;
1529 gfc_warning_now (OPT_Wline_truncation,
1530 "Line truncated at %L", &gfc_current_locus);
1531 }
1532
1533 if (flag_openmp)
1534 prev_openmp_flag = openmp_flag;
1535 if (flag_openacc)
1536 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 if (continue_count == 0
1542 && gfc_current_locus.u.lb
1543 && continue_line > gfc_linebuf_linenum (gfc_current_locus.u.lb) + 1)
1544 continue_line = gfc_linebuf_linenum (gfc_current_locus.u.lb) + 1;
1545
1546 continue_flag = 1;
1547 old_loc = gfc_current_locus;
1548
1549 gfc_advance_line ();
1550 skip_fixed_comments ();
1551
1552 /* See if this line is a continuation line. */
1553 if (flag_openmp && openmp_flag != prev_openmp_flag && !openacc_flag)
1554 {
1555 openmp_flag = prev_openmp_flag;
1556 goto not_continuation;
1557 }
1558 if (flag_openacc && openacc_flag != prev_openacc_flag && !openmp_flag)
1559 {
1560 openacc_flag = prev_openacc_flag;
1561 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 if (openacc_flag && openmp_flag)
1568 {
1569 int is_openmp = 0;
1570 for (i = 0; i < 5; i++)
1571 {
1572 c = next_char ();
1573 if (gfc_wide_tolower (c) != (unsigned char) "*$acc"[i])
1574 is_openmp = 1;
1575 }
1576 if (omp_acc_err_loc.nextc != gfc_current_locus.nextc
1577 || omp_acc_err_loc.u.lb != gfc_current_locus.u.lb)
1578 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 omp_acc_err_loc = gfc_current_locus;
1584 goto not_continuation;
1585 }
1586 else if (!openmp_flag && !openacc_flag)
1587 for (i = 0; i < 5; i++)
1588 {
1589 c = next_char ();
1590 if (c != ' ')
1591 goto not_continuation;
1592 }
1593 else if (openmp_flag)
1594 for (i = 0; i < 5; i++)
1595 {
1596 c = next_char ();
1597 if (gfc_wide_tolower (c) != (unsigned char) "*$omp"[i])
1598 goto not_continuation;
1599 }
1600 else if (openacc_flag)
1601 for (i = 0; i < 5; i++)
1602 {
1603 c = next_char ();
1604 if (gfc_wide_tolower (c) != (unsigned char) "*$acc"[i])
1605 goto not_continuation;
1606 }
1607
1608 c = next_char ();
1609 if (c == '0' || c == ' ' || c == '\n')
1610 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 if (gfc_linebuf_linenum (gfc_current_locus.u.lb) == continue_line + 1)
1616 {
1617 if (++continue_count == gfc_option.max_continue_fixed)
1618 {
1619 if (gfc_notification_std (GFC_STD_GNU) || pedantic)
1620 gfc_warning (0, "Limit of %d continuations exceeded in "
1621 "statement at %C",
1622 gfc_option.max_continue_fixed);
1623 }
1624 }
1625
1626 if (gfc_current_locus.u.lb != NULL
1627 && continue_line < gfc_linebuf_linenum (gfc_current_locus.u.lb))
1628 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 goto restart;
1634
1635 not_continuation:
1636 c = '\n';
1637 gfc_current_locus = old_loc;
1638 end_flag = 0;
1639
1640 done:
1641 if (c == '\n')
1642 continue_count = 0;
1643 continue_flag = 0;
1644 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 gfc_next_char (void)
1655 {
1656 gfc_char_t c;
1657
1658 do
1659 {
1660 c = gfc_next_char_literal (NONSTRING);
1661 }
1662 while (gfc_current_form == FORM_FIXED && gfc_is_whitespace (c));
1663
1664 return gfc_wide_tolower (c);
1665 }
1666
1667 char
1668 gfc_next_ascii_char (void)
1669 {
1670 gfc_char_t c = gfc_next_char ();
1671
1672 return (gfc_wide_fits_in_byte (c) ? (unsigned char) c
1673 : (unsigned char) UCHAR_MAX);
1674 }
1675
1676
1677 gfc_char_t
1678 gfc_peek_char (void)
1679 {
1680 locus old_loc;
1681 gfc_char_t c;
1682
1683 old_loc = gfc_current_locus;
1684 c = gfc_next_char ();
1685 gfc_current_locus = old_loc;
1686
1687 return c;
1688 }
1689
1690
1691 char
1692 gfc_peek_ascii_char (void)
1693 {
1694 gfc_char_t c = gfc_peek_char ();
1695
1696 return (gfc_wide_fits_in_byte (c) ? (unsigned char) c
1697 : (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 gfc_error_recovery (void)
1708 {
1709 gfc_char_t c, delim;
1710
1711 if (gfc_at_eof ())
1712 return;
1713
1714 for (;;)
1715 {
1716 c = gfc_next_char ();
1717 if (c == '\n' || c == ';')
1718 break;
1719
1720 if (c != '\'' && c != '"')
1721 {
1722 if (gfc_at_eof ())
1723 break;
1724 continue;
1725 }
1726 delim = c;
1727
1728 for (;;)
1729 {
1730 c = next_char ();
1731
1732 if (c == delim)
1733 break;
1734 if (c == '\n')
1735 return;
1736 if (c == '\\')
1737 {
1738 c = next_char ();
1739 if (c == '\n')
1740 return;
1741 }
1742 }
1743 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 gfc_gobble_whitespace (void)
1753 {
1754 static int linenum = 0;
1755 locus old_loc;
1756 gfc_char_t c;
1757
1758 do
1759 {
1760 old_loc = gfc_current_locus;
1761 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 if (warn_tabs && c == '\t')
1766 {
1767 int cur_linenum = LOCATION_LINE (gfc_current_locus.u.lb->location);
1768 if (cur_linenum != linenum)
1769 {
1770 linenum = cur_linenum;
1771 gfc_warning_now (OPT_Wtabs, "Nonconforming tab character at %C");
1772 }
1773 }
1774 }
1775 while (gfc_is_whitespace (c));
1776
1777 if (!ISPRINT(c) && c != '\n' && last_error_char != gfc_current_locus.nextc)
1778 {
1779 char buf[20];
1780 last_error_char = gfc_current_locus.nextc;
1781 snprintf (buf, 20, "%2.2X", c);
1782 gfc_error_now ("Invalid character 0x%s at %C", buf);
1783 }
1784
1785 gfc_current_locus = old_loc;
1786 }
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 load_line returns whether the line was truncated.
1805
1806 NOTE: The error machinery isn't available at this point, so we can't
1807 easily report line and column numbers consistent with other
1808 parts of gfortran. */
1809
1810 static bool
1811 load_line (FILE *input, gfc_char_t **pbuf, int *pbuflen, const int *first_char)
1812 {
1813 int c, maxlen, i, preprocessor_flag, buflen = *pbuflen;
1814 int quoted = ' ', comment_ix = -1;
1815 bool seen_comment = false;
1816 bool first_comment = true;
1817 bool trunc_flag = false;
1818 bool seen_printable = false;
1819 bool seen_ampersand = false;
1820 bool found_tab = false;
1821 bool warned_tabs = false;
1822 gfc_char_t *buffer;
1823
1824 /* Determine the maximum allowed line length. */
1825 if (gfc_current_form == FORM_FREE)
1826 maxlen = flag_free_line_length;
1827 else if (gfc_current_form == FORM_FIXED)
1828 maxlen = flag_fixed_line_length;
1829 else
1830 maxlen = 72;
1831
1832 if (*pbuf == NULL)
1833 {
1834 /* Allocate the line buffer, storing its length into buflen.
1835 Note that if maxlen==0, indicating that arbitrary-length lines
1836 are allowed, the buffer will be reallocated if this length is
1837 insufficient; since 132 characters is the length of a standard
1838 free-form line, we use that as a starting guess. */
1839 if (maxlen > 0)
1840 buflen = maxlen;
1841 else
1842 buflen = 132;
1843
1844 *pbuf = gfc_get_wide_string (buflen + 1);
1845 }
1846
1847 i = 0;
1848 buffer = *pbuf;
1849
1850 if (first_char)
1851 c = *first_char;
1852 else
1853 c = getc (input);
1854
1855 /* In order to not truncate preprocessor lines, we have to
1856 remember that this is one. */
1857 preprocessor_flag = (c == '#');
1858
1859 for (;;)
1860 {
1861 if (c == EOF)
1862 break;
1863
1864 if (c == '\n')
1865 {
1866 /* Check for illegal use of ampersand. See F95 Standard 3.3.1.3. */
1867 if (gfc_current_form == FORM_FREE
1868 && !seen_printable && seen_ampersand)
1869 {
1870 if (pedantic)
1871 gfc_error_now ("%<&%> not allowed by itself in line %d",
1872 current_file->line);
1873 else
1874 gfc_warning_now (0, "%<&%> not allowed by itself in line %d",
1875 current_file->line);
1876 }
1877 break;
1878 }
1879
1880 if (c == '\r' || c == '\0')
1881 goto next_char; /* Gobble characters. */
1882
1883 if (c == '&')
1884 {
1885 if (seen_ampersand)
1886 {
1887 seen_ampersand = false;
1888 seen_printable = true;
1889 }
1890 else
1891 seen_ampersand = true;
1892 }
1893
1894 if ((c != '&' && c != '!' && c != ' ') || (c == '!' && !seen_ampersand))
1895 seen_printable = true;
1896
1897 /* Is this a fixed-form comment? */
1898 if (gfc_current_form == FORM_FIXED && i == 0
1899 && (c == '*' || c == 'c' || c == 'C'
1900 || (gfc_option.flag_d_lines != -1 && (c == 'd' || c == 'D'))))
1901 {
1902 seen_comment = true;
1903 comment_ix = i;
1904 }
1905
1906 if (quoted == ' ')
1907 {
1908 if (c == '\'' || c == '"')
1909 quoted = c;
1910 }
1911 else if (c == quoted)
1912 quoted = ' ';
1913
1914 /* Is this a free-form comment? */
1915 if (c == '!' && quoted == ' ')
1916 {
1917 if (seen_comment)
1918 first_comment = false;
1919 seen_comment = true;
1920 comment_ix = i;
1921 }
1922
1923 /* For truncation and tab warnings, set seen_comment to false if one has
1924 either an OpenMP or OpenACC directive - or a !GCC$ attribute. If
1925 OpenMP is enabled, use '!$' as conditional compilation sentinel
1926 and OpenMP directive ('!$omp'). */
1927 if (seen_comment && first_comment && flag_openmp && comment_ix + 1 == i
1928 && c == '$')
1929 first_comment = seen_comment = false;
1930 if (seen_comment && first_comment && comment_ix + 4 == i)
1931 {
1932 if (((*pbuf)[comment_ix+1] == 'g' || (*pbuf)[comment_ix+1] == 'G')
1933 && ((*pbuf)[comment_ix+2] == 'c' || (*pbuf)[comment_ix+2] == 'C')
1934 && ((*pbuf)[comment_ix+3] == 'c' || (*pbuf)[comment_ix+3] == 'C')
1935 && c == '$')
1936 first_comment = seen_comment = false;
1937 if (flag_openacc
1938 && (*pbuf)[comment_ix+1] == '$'
1939 && ((*pbuf)[comment_ix+2] == 'a' || (*pbuf)[comment_ix+2] == 'A')
1940 && ((*pbuf)[comment_ix+3] == 'c' || (*pbuf)[comment_ix+3] == 'C')
1941 && (c == 'c' || c == 'C'))
1942 first_comment = seen_comment = false;
1943 }
1944
1945 /* Vendor extension: "<tab>1" marks a continuation line. */
1946 if (found_tab)
1947 {
1948 found_tab = false;
1949 if (c >= '1' && c <= '9')
1950 {
1951 *(buffer-1) = c;
1952 goto next_char;
1953 }
1954 }
1955
1956 if (gfc_current_form == FORM_FIXED && c == '\t' && i < 6)
1957 {
1958 found_tab = true;
1959
1960 if (warn_tabs && seen_comment == 0 && !warned_tabs)
1961 {
1962 warned_tabs = true;
1963 gfc_warning_now (OPT_Wtabs,
1964 "Nonconforming tab character in column %d "
1965 "of line %d", i + 1, current_file->line);
1966 }
1967
1968 while (i < 6)
1969 {
1970 *buffer++ = ' ';
1971 i++;
1972 }
1973
1974 goto next_char;
1975 }
1976
1977 *buffer++ = c;
1978 i++;
1979
1980 if (maxlen == 0 || preprocessor_flag)
1981 {
1982 if (i >= buflen)
1983 {
1984 /* Reallocate line buffer to double size to hold the
1985 overlong line. */
1986 buflen = buflen * 2;
1987 *pbuf = XRESIZEVEC (gfc_char_t, *pbuf, (buflen + 1));
1988 buffer = (*pbuf) + i;
1989 }
1990 }
1991 else if (i >= maxlen)
1992 {
1993 bool trunc_warn = true;
1994
1995 /* Enhancement, if the very next non-space character is an ampersand
1996 or comment that we would otherwise warn about, don't mark as
1997 truncated. */
1998
1999 /* Truncate the rest of the line. */
2000 for (;;)
2001 {
2002 c = getc (input);
2003 if (c == '\r' || c == ' ')
2004 continue;
2005
2006 if (c == '\n' || c == EOF)
2007 break;
2008
2009 if (!trunc_warn && c != '!')
2010 trunc_warn = true;
2011
2012 if (trunc_warn && ((gfc_current_form == FORM_FIXED && c == '&')
2013 || c == '!'))
2014 trunc_warn = false;
2015
2016 if (c == '!')
2017 seen_comment = 1;
2018
2019 if (trunc_warn && !seen_comment)
2020 trunc_flag = 1;
2021 }
2022
2023 c = '\n';
2024 continue;
2025 }
2026
2027 next_char:
2028 c = getc (input);
2029 }
2030
2031 /* Pad lines to the selected line length in fixed form. */
2032 if (gfc_current_form == FORM_FIXED
2033 && flag_fixed_line_length != 0
2034 && flag_pad_source
2035 && !preprocessor_flag
2036 && c != EOF)
2037 {
2038 while (i++ < maxlen)
2039 *buffer++ = ' ';
2040 }
2041
2042 *buffer = '\0';
2043 *pbuflen = buflen;
2044
2045 return trunc_flag;
2046 }
2047
2048
2049 /* Get a gfc_file structure, initialize it and add it to
2050 the file stack. */
2051
2052 static gfc_file *
2053 get_file (const char *name, enum lc_reason reason)
2054 {
2055 gfc_file *f;
2056
2057 f = XCNEW (gfc_file);
2058
2059 f->filename = xstrdup (name);
2060
2061 f->next = file_head;
2062 file_head = f;
2063
2064 f->up = current_file;
2065 if (current_file != NULL)
2066 f->inclusion_line = current_file->line;
2067
2068 linemap_add (line_table, reason, false, f->filename, 1);
2069
2070 return f;
2071 }
2072
2073
2074 /* Deal with a line from the C preprocessor. The
2075 initial octothorp has already been seen. */
2076
2077 static void
2078 preprocessor_line (gfc_char_t *c)
2079 {
2080 bool flag[5];
2081 int i, line;
2082 gfc_char_t *wide_filename;
2083 gfc_file *f;
2084 int escaped, unescape;
2085 char *filename;
2086
2087 c++;
2088 while (*c == ' ' || *c == '\t')
2089 c++;
2090
2091 if (*c < '0' || *c > '9')
2092 goto bad_cpp_line;
2093
2094 line = wide_atoi (c);
2095
2096 c = wide_strchr (c, ' ');
2097 if (c == NULL)
2098 {
2099 /* No file name given. Set new line number. */
2100 current_file->line = line;
2101 return;
2102 }
2103
2104 /* Skip spaces. */
2105 while (*c == ' ' || *c == '\t')
2106 c++;
2107
2108 /* Skip quote. */
2109 if (*c != '"')
2110 goto bad_cpp_line;
2111 ++c;
2112
2113 wide_filename = c;
2114
2115 /* Make filename end at quote. */
2116 unescape = 0;
2117 escaped = false;
2118 while (*c && ! (!escaped && *c == '"'))
2119 {
2120 if (escaped)
2121 escaped = false;
2122 else if (*c == '\\')
2123 {
2124 escaped = true;
2125 unescape++;
2126 }
2127 ++c;
2128 }
2129
2130 if (! *c)
2131 /* Preprocessor line has no closing quote. */
2132 goto bad_cpp_line;
2133
2134 *c++ = '\0';
2135
2136 /* Undo effects of cpp_quote_string. */
2137 if (unescape)
2138 {
2139 gfc_char_t *s = wide_filename;
2140 gfc_char_t *d = gfc_get_wide_string (c - wide_filename - unescape);
2141
2142 wide_filename = d;
2143 while (*s)
2144 {
2145 if (*s == '\\')
2146 *d++ = *++s;
2147 else
2148 *d++ = *s;
2149 s++;
2150 }
2151 *d = '\0';
2152 }
2153
2154 /* Get flags. */
2155
2156 flag[1] = flag[2] = flag[3] = flag[4] = false;
2157
2158 for (;;)
2159 {
2160 c = wide_strchr (c, ' ');
2161 if (c == NULL)
2162 break;
2163
2164 c++;
2165 i = wide_atoi (c);
2166
2167 if (i >= 1 && i <= 4)
2168 flag[i] = true;
2169 }
2170
2171 /* Convert the filename in wide characters into a filename in narrow
2172 characters. */
2173 filename = gfc_widechar_to_char (wide_filename, -1);
2174
2175 /* Interpret flags. */
2176
2177 if (flag[1]) /* Starting new file. */
2178 {
2179 f = get_file (filename, LC_RENAME);
2180 add_file_change (f->filename, f->inclusion_line);
2181 current_file = f;
2182 }
2183
2184 if (flag[2]) /* Ending current file. */
2185 {
2186 if (!current_file->up
2187 || filename_cmp (current_file->up->filename, filename) != 0)
2188 {
2189 linemap_line_start (line_table, current_file->line, 80);
2190 /* ??? One could compute the exact column where the filename
2191 starts and compute the exact location here. */
2192 gfc_warning_now_at (linemap_position_for_column (line_table, 1),
2193 0, "file %qs left but not entered",
2194 filename);
2195 current_file->line++;
2196 if (unescape)
2197 free (wide_filename);
2198 free (filename);
2199 return;
2200 }
2201
2202 add_file_change (NULL, line);
2203 current_file = current_file->up;
2204 linemap_add (line_table, LC_RENAME, false, current_file->filename,
2205 current_file->line);
2206 }
2207
2208 /* The name of the file can be a temporary file produced by
2209 cpp. Replace the name if it is different. */
2210
2211 if (filename_cmp (current_file->filename, filename) != 0)
2212 {
2213 /* FIXME: we leak the old filename because a pointer to it may be stored
2214 in the linemap. Alternative could be using GC or updating linemap to
2215 point to the new name, but there is no API for that currently. */
2216 current_file->filename = xstrdup (filename);
2217
2218 /* We need to tell the linemap API that the filename changed. Just
2219 changing current_file is insufficient. */
2220 linemap_add (line_table, LC_RENAME, false, current_file->filename, line);
2221 }
2222
2223 /* Set new line number. */
2224 current_file->line = line;
2225 if (unescape)
2226 free (wide_filename);
2227 free (filename);
2228 return;
2229
2230 bad_cpp_line:
2231 linemap_line_start (line_table, current_file->line, 80);
2232 /* ??? One could compute the exact column where the directive
2233 starts and compute the exact location here. */
2234 gfc_warning_now_at (linemap_position_for_column (line_table, 2), 0,
2235 "Illegal preprocessor directive");
2236 current_file->line++;
2237 }
2238
2239
2240 static void load_file (const char *, const char *, bool);
2241
2242 /* include_line()-- Checks a line buffer to see if it is an include
2243 line. If so, we call load_file() recursively to load the included
2244 file. We never return a syntax error because a statement like
2245 "include = 5" is perfectly legal. We return 0 if no include was
2246 processed, 1 if we matched an include or -1 if include was
2247 partially processed, but will need continuation lines. */
2248
2249 static int
2250 include_line (gfc_char_t *line)
2251 {
2252 gfc_char_t quote, *c, *begin, *stop;
2253 char *filename;
2254 const char *include = "include";
2255 bool allow_continuation = flag_dec_include;
2256 int i;
2257
2258 c = line;
2259
2260 if (flag_openmp || flag_openmp_simd)
2261 {
2262 if (gfc_current_form == FORM_FREE)
2263 {
2264 while (*c == ' ' || *c == '\t')
2265 c++;
2266 if (*c == '!' && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
2267 c += 3;
2268 }
2269 else
2270 {
2271 if ((*c == '!' || *c == 'c' || *c == 'C' || *c == '*')
2272 && c[1] == '$' && c[2] == ' ')
2273 c += 3;
2274 }
2275 }
2276
2277 if (gfc_current_form == FORM_FREE)
2278 {
2279 while (*c == ' ' || *c == '\t')
2280 c++;
2281 if (gfc_wide_strncasecmp (c, "include", 7))
2282 {
2283 if (!allow_continuation)
2284 return 0;
2285 for (i = 0; i < 7; ++i)
2286 {
2287 gfc_char_t c1 = gfc_wide_tolower (*c);
2288 if (c1 != (unsigned char) include[i])
2289 break;
2290 c++;
2291 }
2292 if (i == 0 || *c != '&')
2293 return 0;
2294 c++;
2295 while (*c == ' ' || *c == '\t')
2296 c++;
2297 if (*c == '\0' || *c == '!')
2298 return -1;
2299 return 0;
2300 }
2301
2302 c += 7;
2303 }
2304 else
2305 {
2306 while (*c == ' ' || *c == '\t')
2307 c++;
2308 if (flag_dec_include && *c == '0' && c - line == 5)
2309 {
2310 c++;
2311 while (*c == ' ' || *c == '\t')
2312 c++;
2313 }
2314 if (c - line < 6)
2315 allow_continuation = false;
2316 for (i = 0; i < 7; ++i)
2317 {
2318 gfc_char_t c1 = gfc_wide_tolower (*c);
2319 if (c1 != (unsigned char) include[i])
2320 break;
2321 c++;
2322 while (*c == ' ' || *c == '\t')
2323 c++;
2324 }
2325 if (!allow_continuation)
2326 {
2327 if (i != 7)
2328 return 0;
2329 }
2330 else if (i != 7)
2331 {
2332 if (i == 0)
2333 return 0;
2334
2335 /* At the end of line or comment this might be continued. */
2336 if (*c == '\0' || *c == '!')
2337 return -1;
2338
2339 return 0;
2340 }
2341 }
2342
2343 while (*c == ' ' || *c == '\t')
2344 c++;
2345
2346 /* Find filename between quotes. */
2347
2348 quote = *c++;
2349 if (quote != '"' && quote != '\'')
2350 {
2351 if (allow_continuation)
2352 {
2353 if (gfc_current_form == FORM_FREE)
2354 {
2355 if (quote == '&')
2356 {
2357 while (*c == ' ' || *c == '\t')
2358 c++;
2359 if (*c == '\0' || *c == '!')
2360 return -1;
2361 }
2362 }
2363 else if (quote == '\0' || quote == '!')
2364 return -1;
2365 }
2366 return 0;
2367 }
2368
2369 begin = c;
2370
2371 bool cont = false;
2372 while (*c != quote && *c != '\0')
2373 {
2374 if (allow_continuation && gfc_current_form == FORM_FREE)
2375 {
2376 if (*c == '&')
2377 cont = true;
2378 else if (*c != ' ' && *c != '\t')
2379 cont = false;
2380 }
2381 c++;
2382 }
2383
2384 if (*c == '\0')
2385 {
2386 if (allow_continuation
2387 && (cont || gfc_current_form != FORM_FREE))
2388 return -1;
2389 return 0;
2390 }
2391
2392 stop = c++;
2393
2394 while (*c == ' ' || *c == '\t')
2395 c++;
2396
2397 if (*c != '\0' && *c != '!')
2398 return 0;
2399
2400 /* We have an include line at this point. */
2401
2402 *stop = '\0'; /* It's ok to trash the buffer, as this line won't be
2403 read by anything else. */
2404
2405 filename = gfc_widechar_to_char (begin, -1);
2406 load_file (filename, NULL, false);
2407 free (filename);
2408 return 1;
2409 }
2410
2411 /* Similarly, but try to parse an INCLUDE statement, using gfc_next_char etc.
2412 APIs. Return 1 if recognized as valid INCLUDE statement and load_file has
2413 been called, 0 if it is not a valid INCLUDE statement and -1 if eof has
2414 been encountered while parsing it. */
2415 static int
2416 include_stmt (gfc_linebuf *b)
2417 {
2418 int ret = 0, i, length;
2419 const char *include = "include";
2420 gfc_char_t c, quote = 0;
2421 locus str_locus;
2422 char *filename;
2423
2424 continue_flag = 0;
2425 end_flag = 0;
2426 gcc_attribute_flag = 0;
2427 openmp_flag = 0;
2428 openacc_flag = 0;
2429 continue_count = 0;
2430 continue_line = 0;
2431 gfc_current_locus.u.lb = b;
2432 gfc_current_locus.nextc = b->line;
2433
2434 gfc_skip_comments ();
2435 gfc_gobble_whitespace ();
2436
2437 for (i = 0; i < 7; i++)
2438 {
2439 c = gfc_next_char ();
2440 if (c != (unsigned char) include[i])
2441 {
2442 if (gfc_current_form == FORM_FIXED
2443 && i == 0
2444 && c == '0'
2445 && gfc_current_locus.nextc == b->line + 6)
2446 {
2447 gfc_gobble_whitespace ();
2448 i--;
2449 continue;
2450 }
2451 gcc_assert (i != 0);
2452 if (c == '\n')
2453 {
2454 gfc_advance_line ();
2455 gfc_skip_comments ();
2456 if (gfc_at_eof ())
2457 ret = -1;
2458 }
2459 goto do_ret;
2460 }
2461 }
2462 gfc_gobble_whitespace ();
2463
2464 c = gfc_next_char ();
2465 if (c == '\'' || c == '"')
2466 quote = c;
2467 else
2468 {
2469 if (c == '\n')
2470 {
2471 gfc_advance_line ();
2472 gfc_skip_comments ();
2473 if (gfc_at_eof ())
2474 ret = -1;
2475 }
2476 goto do_ret;
2477 }
2478
2479 str_locus = gfc_current_locus;
2480 length = 0;
2481 do
2482 {
2483 c = gfc_next_char_literal (INSTRING_NOWARN);
2484 if (c == quote)
2485 break;
2486 if (c == '\n')
2487 {
2488 gfc_advance_line ();
2489 gfc_skip_comments ();
2490 if (gfc_at_eof ())
2491 ret = -1;
2492 goto do_ret;
2493 }
2494 length++;
2495 }
2496 while (1);
2497
2498 gfc_gobble_whitespace ();
2499 c = gfc_next_char ();
2500 if (c != '\n')
2501 goto do_ret;
2502
2503 gfc_current_locus = str_locus;
2504 ret = 1;
2505 filename = XNEWVEC (char, length + 1);
2506 for (i = 0; i < length; i++)
2507 {
2508 c = gfc_next_char_literal (INSTRING_WARN);
2509 gcc_assert (gfc_wide_fits_in_byte (c));
2510 filename[i] = (unsigned char) c;
2511 }
2512 filename[length] = '\0';
2513 load_file (filename, NULL, false);
2514 free (filename);
2515
2516 do_ret:
2517 continue_flag = 0;
2518 end_flag = 0;
2519 gcc_attribute_flag = 0;
2520 openmp_flag = 0;
2521 openacc_flag = 0;
2522 continue_count = 0;
2523 continue_line = 0;
2524 memset (&gfc_current_locus, '\0', sizeof (locus));
2525 memset (&openmp_locus, '\0', sizeof (locus));
2526 memset (&openacc_locus, '\0', sizeof (locus));
2527 memset (&gcc_attribute_locus, '\0', sizeof (locus));
2528 return ret;
2529 }
2530
2531
2532
2533 /* Load a file into memory by calling load_line until the file ends. */
2534
2535 static void
2536 load_file (const char *realfilename, const char *displayedname, bool initial)
2537 {
2538 gfc_char_t *line;
2539 gfc_linebuf *b, *include_b = NULL;
2540 gfc_file *f;
2541 FILE *input;
2542 int len, line_len;
2543 bool first_line;
2544 struct stat st;
2545 int stat_result;
2546 const char *filename;
2547 /* If realfilename and displayedname are different and non-null then
2548 surely realfilename is the preprocessed form of
2549 displayedname. */
2550 bool preprocessed_p = (realfilename && displayedname
2551 && strcmp (realfilename, displayedname));
2552
2553 filename = displayedname ? displayedname : realfilename;
2554
2555 for (f = current_file; f; f = f->up)
2556 if (filename_cmp (filename, f->filename) == 0)
2557 fatal_error (linemap_line_start (line_table, current_file->line, 0),
2558 "File %qs is being included recursively", filename);
2559 if (initial)
2560 {
2561 if (gfc_src_file)
2562 {
2563 input = gfc_src_file;
2564 gfc_src_file = NULL;
2565 }
2566 else
2567 input = gfc_open_file (realfilename);
2568
2569 if (input == NULL)
2570 gfc_fatal_error ("Cannot open file %qs", filename);
2571 }
2572 else
2573 {
2574 input = gfc_open_included_file (realfilename, false, false);
2575 if (input == NULL)
2576 {
2577 /* For -fpre-include file, current_file is NULL. */
2578 if (current_file)
2579 fatal_error (linemap_line_start (line_table, current_file->line, 0),
2580 "Cannot open included file %qs", filename);
2581 else
2582 gfc_fatal_error ("Cannot open pre-included file %qs", filename);
2583 }
2584 stat_result = stat (realfilename, &st);
2585 if (stat_result == 0 && !S_ISREG (st.st_mode))
2586 {
2587 fclose (input);
2588 if (current_file)
2589 fatal_error (linemap_line_start (line_table, current_file->line, 0),
2590 "Included file %qs is not a regular file", filename);
2591 else
2592 gfc_fatal_error ("Included file %qs is not a regular file", filename);
2593 }
2594 }
2595
2596 /* Load the file.
2597
2598 A "non-initial" file means a file that is being included. In
2599 that case we are creating an LC_ENTER map.
2600
2601 An "initial" file means a main file; one that is not included.
2602 That file has already got at least one (surely more) line map(s)
2603 created by gfc_init. So the subsequent map created in that case
2604 must have LC_RENAME reason.
2605
2606 This latter case is not true for a preprocessed file. In that
2607 case, although the file is "initial", the line maps created by
2608 gfc_init was used during the preprocessing of the file. Now that
2609 the preprocessing is over and we are being fed the result of that
2610 preprocessing, we need to create a brand new line map for the
2611 preprocessed file, so the reason is going to be LC_ENTER. */
2612
2613 f = get_file (filename, (initial && !preprocessed_p) ? LC_RENAME : LC_ENTER);
2614 if (!initial)
2615 add_file_change (f->filename, f->inclusion_line);
2616 current_file = f;
2617 current_file->line = 1;
2618 line = NULL;
2619 line_len = 0;
2620 first_line = true;
2621
2622 if (initial && gfc_src_preprocessor_lines[0])
2623 {
2624 preprocessor_line (gfc_src_preprocessor_lines[0]);
2625 free (gfc_src_preprocessor_lines[0]);
2626 gfc_src_preprocessor_lines[0] = NULL;
2627 if (gfc_src_preprocessor_lines[1])
2628 {
2629 preprocessor_line (gfc_src_preprocessor_lines[1]);
2630 free (gfc_src_preprocessor_lines[1]);
2631 gfc_src_preprocessor_lines[1] = NULL;
2632 }
2633 }
2634
2635 for (;;)
2636 {
2637 int trunc = load_line (input, &line, &line_len, NULL);
2638 int inc_line;
2639
2640 len = gfc_wide_strlen (line);
2641 if (feof (input) && len == 0)
2642 break;
2643
2644 /* If this is the first line of the file, it can contain a byte
2645 order mark (BOM), which we will ignore:
2646 FF FE is UTF-16 little endian,
2647 FE FF is UTF-16 big endian,
2648 EF BB BF is UTF-8. */
2649 if (first_line
2650 && ((line_len >= 2 && line[0] == (unsigned char) '\xFF'
2651 && line[1] == (unsigned char) '\xFE')
2652 || (line_len >= 2 && line[0] == (unsigned char) '\xFE'
2653 && line[1] == (unsigned char) '\xFF')
2654 || (line_len >= 3 && line[0] == (unsigned char) '\xEF'
2655 && line[1] == (unsigned char) '\xBB'
2656 && line[2] == (unsigned char) '\xBF')))
2657 {
2658 int n = line[1] == (unsigned char) '\xBB' ? 3 : 2;
2659 gfc_char_t *new_char = gfc_get_wide_string (line_len);
2660
2661 wide_strcpy (new_char, &line[n]);
2662 free (line);
2663 line = new_char;
2664 len -= n;
2665 }
2666
2667 /* There are three things this line can be: a line of Fortran
2668 source, an include line or a C preprocessor directive. */
2669
2670 if (line[0] == '#')
2671 {
2672 /* When -g3 is specified, it's possible that we emit #define
2673 and #undef lines, which we need to pass to the middle-end
2674 so that it can emit correct debug info. */
2675 if (debug_info_level == DINFO_LEVEL_VERBOSE
2676 && (wide_strncmp (line, "#define ", 8) == 0
2677 || wide_strncmp (line, "#undef ", 7) == 0))
2678 ;
2679 else
2680 {
2681 preprocessor_line (line);
2682 continue;
2683 }
2684 }
2685
2686 /* Preprocessed files have preprocessor lines added before the byte
2687 order mark, so first_line is not about the first line of the file
2688 but the first line that's not a preprocessor line. */
2689 first_line = false;
2690
2691 inc_line = include_line (line);
2692 if (inc_line > 0)
2693 {
2694 current_file->line++;
2695 continue;
2696 }
2697
2698 /* Add line. */
2699
2700 b = XCNEWVAR (gfc_linebuf, gfc_linebuf_header_size
2701 + (len + 1) * sizeof (gfc_char_t));
2702
2703
2704 b->location
2705 = linemap_line_start (line_table, current_file->line++, len);
2706 /* ??? We add the location for the maximum column possible here,
2707 because otherwise if the next call creates a new line-map, it
2708 will not reserve space for any offset. */
2709 if (len > 0)
2710 linemap_position_for_column (line_table, len);
2711
2712 b->file = current_file;
2713 b->truncated = trunc;
2714 wide_strcpy (b->line, line);
2715
2716 if (line_head == NULL)
2717 line_head = b;
2718 else
2719 line_tail->next = b;
2720
2721 line_tail = b;
2722
2723 while (file_changes_cur < file_changes_count)
2724 file_changes[file_changes_cur++].lb = b;
2725
2726 if (flag_dec_include)
2727 {
2728 if (include_b && b != include_b)
2729 {
2730 int inc_line2 = include_stmt (include_b);
2731 if (inc_line2 == 0)
2732 include_b = NULL;
2733 else if (inc_line2 > 0)
2734 {
2735 do
2736 {
2737 if (gfc_current_form == FORM_FIXED)
2738 {
2739 for (gfc_char_t *p = include_b->line; *p; p++)
2740 *p = ' ';
2741 }
2742 else
2743 include_b->line[0] = '\0';
2744 if (include_b == b)
2745 break;
2746 include_b = include_b->next;
2747 }
2748 while (1);
2749 include_b = NULL;
2750 }
2751 }
2752 if (inc_line == -1 && !include_b)
2753 include_b = b;
2754 }
2755 }
2756
2757 /* Release the line buffer allocated in load_line. */
2758 free (line);
2759
2760 fclose (input);
2761
2762 if (!initial)
2763 add_file_change (NULL, current_file->inclusion_line + 1);
2764 current_file = current_file->up;
2765 linemap_add (line_table, LC_LEAVE, 0, NULL, 0);
2766 }
2767
2768
2769 /* Open a new file and start scanning from that file. Returns true
2770 if everything went OK, false otherwise. If form == FORM_UNKNOWN
2771 it tries to determine the source form from the filename, defaulting
2772 to free form. */
2773
2774 void
2775 gfc_new_file (void)
2776 {
2777 if (flag_pre_include != NULL)
2778 load_file (flag_pre_include, NULL, false);
2779
2780 if (gfc_cpp_enabled ())
2781 {
2782 gfc_cpp_preprocess (gfc_source_file);
2783 if (!gfc_cpp_preprocess_only ())
2784 load_file (gfc_cpp_temporary_file (), gfc_source_file, true);
2785 }
2786 else
2787 load_file (gfc_source_file, NULL, true);
2788
2789 gfc_current_locus.u.lb = line_head;
2790 gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line;
2791
2792 #if 0 /* Debugging aid. */
2793 for (; line_head; line_head = line_head->next)
2794 printf ("%s:%3d %s\n", LOCATION_FILE (line_head->location),
2795 LOCATION_LINE (line_head->location), line_head->line);
2796
2797 exit (SUCCESS_EXIT_CODE);
2798 #endif
2799 }
2800
2801 static char *
2802 unescape_filename (const char *ptr)
2803 {
2804 const char *p = ptr, *s;
2805 char *d, *ret;
2806 int escaped, unescape = 0;
2807
2808 /* Make filename end at quote. */
2809 escaped = false;
2810 while (*p && ! (! escaped && *p == '"'))
2811 {
2812 if (escaped)
2813 escaped = false;
2814 else if (*p == '\\')
2815 {
2816 escaped = true;
2817 unescape++;
2818 }
2819 ++p;
2820 }
2821
2822 if (!*p || p[1])
2823 return NULL;
2824
2825 /* Undo effects of cpp_quote_string. */
2826 s = ptr;
2827 d = XCNEWVEC (char, p + 1 - ptr - unescape);
2828 ret = d;
2829
2830 while (s != p)
2831 {
2832 if (*s == '\\')
2833 *d++ = *++s;
2834 else
2835 *d++ = *s;
2836 s++;
2837 }
2838 *d = '\0';
2839 return ret;
2840 }
2841
2842 /* For preprocessed files, if the first tokens are of the form # NUM.
2843 handle the directives so we know the original file name. */
2844
2845 const char *
2846 gfc_read_orig_filename (const char *filename, const char **canon_source_file)
2847 {
2848 int c, len;
2849 char *dirname, *tmp;
2850
2851 gfc_src_file = gfc_open_file (filename);
2852 if (gfc_src_file == NULL)
2853 return NULL;
2854
2855 c = getc (gfc_src_file);
2856
2857 if (c != '#')
2858 return NULL;
2859
2860 len = 0;
2861 load_line (gfc_src_file, &gfc_src_preprocessor_lines[0], &len, &c);
2862
2863 if (wide_strncmp (gfc_src_preprocessor_lines[0], "# 1 \"", 5) != 0)
2864 return NULL;
2865
2866 tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[0][5], -1);
2867 filename = unescape_filename (tmp);
2868 free (tmp);
2869 if (filename == NULL)
2870 return NULL;
2871
2872 c = getc (gfc_src_file);
2873
2874 if (c != '#')
2875 return filename;
2876
2877 len = 0;
2878 load_line (gfc_src_file, &gfc_src_preprocessor_lines[1], &len, &c);
2879
2880 if (wide_strncmp (gfc_src_preprocessor_lines[1], "# 1 \"", 5) != 0)
2881 return filename;
2882
2883 tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[1][5], -1);
2884 dirname = unescape_filename (tmp);
2885 free (tmp);
2886 if (dirname == NULL)
2887 return filename;
2888
2889 len = strlen (dirname);
2890 if (len < 3 || dirname[len - 1] != '/' || dirname[len - 2] != '/')
2891 {
2892 free (dirname);
2893 return filename;
2894 }
2895 dirname[len - 2] = '\0';
2896 set_src_pwd (dirname);
2897
2898 if (! IS_ABSOLUTE_PATH (filename))
2899 {
2900 char *p = XCNEWVEC (char, len + strlen (filename));
2901
2902 memcpy (p, dirname, len - 2);
2903 p[len - 2] = '/';
2904 strcpy (p + len - 1, filename);
2905 *canon_source_file = p;
2906 }
2907
2908 free (dirname);
2909 return filename;
2910 }