[arm][2/2] Remove support for -march=armv3 and older
[official-gcc.git] / gcc / fortran / scanner.c
blob55d6dafdb5dd46d68f28da929ace3939d070e97d
1 /* Character scanner.
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
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.
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.
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/>. */
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.
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.
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.
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.
40 From the scanner's viewpoint, the higher level subroutines ask for
41 new characters and do a lot of jumping backwards. */
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 "cpp.h"
51 #include "scanner.h"
53 /* List of include file search directories. */
54 gfc_directorylist *include_dirs, *intrinsic_modules_dirs;
56 static gfc_file *file_head, *current_file;
58 static int continue_flag, end_flag, gcc_attribute_flag;
59 /* If !$omp/!$acc occurred in current comment line. */
60 static int openmp_flag, openacc_flag;
61 static int continue_count, continue_line;
62 static locus openmp_locus;
63 static locus openacc_locus;
64 static locus gcc_attribute_locus;
66 gfc_source_form gfc_current_form;
67 static gfc_linebuf *line_head, *line_tail;
69 locus gfc_current_locus;
70 const char *gfc_source_file;
71 static FILE *gfc_src_file;
72 static gfc_char_t *gfc_src_preprocessor_lines[2];
74 static struct gfc_file_change
76 const char *filename;
77 gfc_linebuf *lb;
78 int line;
79 } *file_changes;
80 size_t file_changes_cur, file_changes_count;
81 size_t file_changes_allocated;
83 static gfc_char_t *last_error_char;
85 /* Functions dealing with our wide characters (gfc_char_t) and
86 sequences of such characters. */
88 int
89 gfc_wide_fits_in_byte (gfc_char_t c)
91 return (c <= UCHAR_MAX);
94 static inline int
95 wide_is_ascii (gfc_char_t c)
97 return (gfc_wide_fits_in_byte (c) && ((unsigned char) c & ~0x7f) == 0);
101 gfc_wide_is_printable (gfc_char_t c)
103 return (gfc_wide_fits_in_byte (c) && ISPRINT ((unsigned char) c));
106 gfc_char_t
107 gfc_wide_tolower (gfc_char_t c)
109 return (wide_is_ascii (c) ? (gfc_char_t) TOLOWER((unsigned char) c) : c);
112 gfc_char_t
113 gfc_wide_toupper (gfc_char_t c)
115 return (wide_is_ascii (c) ? (gfc_char_t) TOUPPER((unsigned char) c) : c);
119 gfc_wide_is_digit (gfc_char_t c)
121 return (c >= '0' && c <= '9');
124 static inline int
125 wide_atoi (gfc_char_t *c)
127 #define MAX_DIGITS 20
128 char buf[MAX_DIGITS+1];
129 int i = 0;
131 while (gfc_wide_is_digit(*c) && i < MAX_DIGITS)
132 buf[i++] = *c++;
133 buf[i] = '\0';
134 return atoi (buf);
137 size_t
138 gfc_wide_strlen (const gfc_char_t *str)
140 size_t i;
142 for (i = 0; str[i]; i++)
145 return i;
148 gfc_char_t *
149 gfc_wide_memset (gfc_char_t *b, gfc_char_t c, size_t len)
151 size_t i;
153 for (i = 0; i < len; i++)
154 b[i] = c;
156 return b;
159 static gfc_char_t *
160 wide_strcpy (gfc_char_t *dest, const gfc_char_t *src)
162 gfc_char_t *d;
164 for (d = dest; (*d = *src) != '\0'; ++src, ++d)
167 return dest;
170 static gfc_char_t *
171 wide_strchr (const gfc_char_t *s, gfc_char_t c)
173 do {
174 if (*s == c)
176 return CONST_CAST(gfc_char_t *, s);
178 } while (*s++);
179 return 0;
182 char *
183 gfc_widechar_to_char (const gfc_char_t *s, int length)
185 size_t len, i;
186 char *res;
188 if (s == NULL)
189 return NULL;
191 /* Passing a negative length is used to indicate that length should be
192 calculated using gfc_wide_strlen(). */
193 len = (length >= 0 ? (size_t) length : gfc_wide_strlen (s));
194 res = XNEWVEC (char, len + 1);
196 for (i = 0; i < len; i++)
198 gcc_assert (gfc_wide_fits_in_byte (s[i]));
199 res[i] = (unsigned char) s[i];
202 res[len] = '\0';
203 return res;
206 gfc_char_t *
207 gfc_char_to_widechar (const char *s)
209 size_t len, i;
210 gfc_char_t *res;
212 if (s == NULL)
213 return NULL;
215 len = strlen (s);
216 res = gfc_get_wide_string (len + 1);
218 for (i = 0; i < len; i++)
219 res[i] = (unsigned char) s[i];
221 res[len] = '\0';
222 return res;
225 static int
226 wide_strncmp (const gfc_char_t *s1, const char *s2, size_t n)
228 gfc_char_t c1, c2;
230 while (n-- > 0)
232 c1 = *s1++;
233 c2 = *s2++;
234 if (c1 != c2)
235 return (c1 > c2 ? 1 : -1);
236 if (c1 == '\0')
237 return 0;
239 return 0;
243 gfc_wide_strncasecmp (const gfc_char_t *s1, const char *s2, size_t n)
245 gfc_char_t c1, c2;
247 while (n-- > 0)
249 c1 = gfc_wide_tolower (*s1++);
250 c2 = TOLOWER (*s2++);
251 if (c1 != c2)
252 return (c1 > c2 ? 1 : -1);
253 if (c1 == '\0')
254 return 0;
256 return 0;
260 /* Main scanner initialization. */
262 void
263 gfc_scanner_init_1 (void)
265 file_head = NULL;
266 line_head = NULL;
267 line_tail = NULL;
269 continue_count = 0;
270 continue_line = 0;
272 end_flag = 0;
273 last_error_char = NULL;
277 /* Main scanner destructor. */
279 void
280 gfc_scanner_done_1 (void)
282 gfc_linebuf *lb;
283 gfc_file *f;
285 while(line_head != NULL)
287 lb = line_head->next;
288 free (line_head);
289 line_head = lb;
292 while(file_head != NULL)
294 f = file_head->next;
295 free (file_head->filename);
296 free (file_head);
297 file_head = f;
302 /* Adds path to the list pointed to by list. */
304 static void
305 add_path_to_list (gfc_directorylist **list, const char *path,
306 bool use_for_modules, bool head, bool warn)
308 gfc_directorylist *dir;
309 const char *p;
310 char *q;
311 struct stat st;
312 size_t len;
313 int i;
315 p = path;
316 while (*p == ' ' || *p == '\t') /* someone might do "-I include" */
317 if (*p++ == '\0')
318 return;
320 /* Strip trailing directory separators from the path, as this
321 will confuse Windows systems. */
322 len = strlen (p);
323 q = (char *) alloca (len + 1);
324 memcpy (q, p, len + 1);
325 i = len - 1;
326 while (i >=0 && IS_DIR_SEPARATOR (q[i]))
327 q[i--] = '\0';
329 if (stat (q, &st))
331 if (errno != ENOENT)
332 gfc_warning_now (0, "Include directory %qs: %s", path,
333 xstrerror(errno));
334 else if (warn)
335 gfc_warning_now (OPT_Wmissing_include_dirs,
336 "Nonexistent include directory %qs", path);
337 return;
339 else if (!S_ISDIR (st.st_mode))
341 gfc_fatal_error ("%qs is not a directory", path);
342 return;
345 if (head || *list == NULL)
347 dir = XCNEW (gfc_directorylist);
348 if (!head)
349 *list = dir;
351 else
353 dir = *list;
354 while (dir->next)
355 dir = dir->next;
357 dir->next = XCNEW (gfc_directorylist);
358 dir = dir->next;
361 dir->next = head ? *list : NULL;
362 if (head)
363 *list = dir;
364 dir->use_for_modules = use_for_modules;
365 dir->path = XCNEWVEC (char, strlen (p) + 2);
366 strcpy (dir->path, p);
367 strcat (dir->path, "/"); /* make '/' last character */
371 void
372 gfc_add_include_path (const char *path, bool use_for_modules, bool file_dir,
373 bool warn)
375 add_path_to_list (&include_dirs, path, use_for_modules, file_dir, warn);
377 /* For '#include "..."' these directories are automatically searched. */
378 if (!file_dir)
379 gfc_cpp_add_include_path (xstrdup(path), true);
383 void
384 gfc_add_intrinsic_modules_path (const char *path)
386 add_path_to_list (&intrinsic_modules_dirs, path, true, false, false);
390 /* Release resources allocated for options. */
392 void
393 gfc_release_include_path (void)
395 gfc_directorylist *p;
397 while (include_dirs != NULL)
399 p = include_dirs;
400 include_dirs = include_dirs->next;
401 free (p->path);
402 free (p);
405 while (intrinsic_modules_dirs != NULL)
407 p = intrinsic_modules_dirs;
408 intrinsic_modules_dirs = intrinsic_modules_dirs->next;
409 free (p->path);
410 free (p);
413 free (gfc_option.module_dir);
417 static FILE *
418 open_included_file (const char *name, gfc_directorylist *list,
419 bool module, bool system)
421 char *fullname;
422 gfc_directorylist *p;
423 FILE *f;
425 for (p = list; p; p = p->next)
427 if (module && !p->use_for_modules)
428 continue;
430 fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1);
431 strcpy (fullname, p->path);
432 strcat (fullname, name);
434 f = gfc_open_file (fullname);
435 if (f != NULL)
437 if (gfc_cpp_makedep ())
438 gfc_cpp_add_dep (fullname, system);
440 return f;
444 return NULL;
448 /* Opens file for reading, searching through the include directories
449 given if necessary. If the include_cwd argument is true, we try
450 to open the file in the current directory first. */
452 FILE *
453 gfc_open_included_file (const char *name, bool include_cwd, bool module)
455 FILE *f = NULL;
457 if (IS_ABSOLUTE_PATH (name) || include_cwd)
459 f = gfc_open_file (name);
460 if (f && gfc_cpp_makedep ())
461 gfc_cpp_add_dep (name, false);
464 if (!f)
465 f = open_included_file (name, include_dirs, module, false);
467 return f;
471 /* Test to see if we're at the end of the main source file. */
474 gfc_at_end (void)
476 return end_flag;
480 /* Test to see if we're at the end of the current file. */
483 gfc_at_eof (void)
485 if (gfc_at_end ())
486 return 1;
488 if (line_head == NULL)
489 return 1; /* Null file */
491 if (gfc_current_locus.lb == NULL)
492 return 1;
494 return 0;
498 /* Test to see if we're at the beginning of a new line. */
501 gfc_at_bol (void)
503 if (gfc_at_eof ())
504 return 1;
506 return (gfc_current_locus.nextc == gfc_current_locus.lb->line);
510 /* Test to see if we're at the end of a line. */
513 gfc_at_eol (void)
515 if (gfc_at_eof ())
516 return 1;
518 return (*gfc_current_locus.nextc == '\0');
521 static void
522 add_file_change (const char *filename, int line)
524 if (file_changes_count == file_changes_allocated)
526 if (file_changes_allocated)
527 file_changes_allocated *= 2;
528 else
529 file_changes_allocated = 16;
530 file_changes = XRESIZEVEC (struct gfc_file_change, file_changes,
531 file_changes_allocated);
533 file_changes[file_changes_count].filename = filename;
534 file_changes[file_changes_count].lb = NULL;
535 file_changes[file_changes_count++].line = line;
538 static void
539 report_file_change (gfc_linebuf *lb)
541 size_t c = file_changes_cur;
542 while (c < file_changes_count
543 && file_changes[c].lb == lb)
545 if (file_changes[c].filename)
546 (*debug_hooks->start_source_file) (file_changes[c].line,
547 file_changes[c].filename);
548 else
549 (*debug_hooks->end_source_file) (file_changes[c].line);
550 ++c;
552 file_changes_cur = c;
555 void
556 gfc_start_source_files (void)
558 /* If the debugger wants the name of the main source file,
559 we give it. */
560 if (debug_hooks->start_end_main_source_file)
561 (*debug_hooks->start_source_file) (0, gfc_source_file);
563 file_changes_cur = 0;
564 report_file_change (gfc_current_locus.lb);
567 void
568 gfc_end_source_files (void)
570 report_file_change (NULL);
572 if (debug_hooks->start_end_main_source_file)
573 (*debug_hooks->end_source_file) (0);
576 /* Advance the current line pointer to the next line. */
578 void
579 gfc_advance_line (void)
581 if (gfc_at_end ())
582 return;
584 if (gfc_current_locus.lb == NULL)
586 end_flag = 1;
587 return;
590 if (gfc_current_locus.lb->next
591 && !gfc_current_locus.lb->next->dbg_emitted)
593 report_file_change (gfc_current_locus.lb->next);
594 gfc_current_locus.lb->next->dbg_emitted = true;
597 gfc_current_locus.lb = gfc_current_locus.lb->next;
599 if (gfc_current_locus.lb != NULL)
600 gfc_current_locus.nextc = gfc_current_locus.lb->line;
601 else
603 gfc_current_locus.nextc = NULL;
604 end_flag = 1;
609 /* Get the next character from the input, advancing gfc_current_file's
610 locus. When we hit the end of the line or the end of the file, we
611 start returning a '\n' in order to complete the current statement.
612 No Fortran line conventions are implemented here.
614 Requiring explicit advances to the next line prevents the parse
615 pointer from being on the wrong line if the current statement ends
616 prematurely. */
618 static gfc_char_t
619 next_char (void)
621 gfc_char_t c;
623 if (gfc_current_locus.nextc == NULL)
624 return '\n';
626 c = *gfc_current_locus.nextc++;
627 if (c == '\0')
629 gfc_current_locus.nextc--; /* Remain on this line. */
630 c = '\n';
633 return c;
637 /* Skip a comment. When we come here the parse pointer is positioned
638 immediately after the comment character. If we ever implement
639 compiler directives within comments, here is where we parse the
640 directive. */
642 static void
643 skip_comment_line (void)
645 gfc_char_t c;
649 c = next_char ();
651 while (c != '\n');
653 gfc_advance_line ();
658 gfc_define_undef_line (void)
660 char *tmp;
662 /* All lines beginning with '#' are either #define or #undef. */
663 if (debug_info_level != DINFO_LEVEL_VERBOSE || gfc_peek_ascii_char () != '#')
664 return 0;
666 if (wide_strncmp (gfc_current_locus.nextc, "#define ", 8) == 0)
668 tmp = gfc_widechar_to_char (&gfc_current_locus.nextc[8], -1);
669 (*debug_hooks->define) (gfc_linebuf_linenum (gfc_current_locus.lb),
670 tmp);
671 free (tmp);
674 if (wide_strncmp (gfc_current_locus.nextc, "#undef ", 7) == 0)
676 tmp = gfc_widechar_to_char (&gfc_current_locus.nextc[7], -1);
677 (*debug_hooks->undef) (gfc_linebuf_linenum (gfc_current_locus.lb),
678 tmp);
679 free (tmp);
682 /* Skip the rest of the line. */
683 skip_comment_line ();
685 return 1;
689 /* Return true if GCC$ was matched. */
690 static bool
691 skip_gcc_attribute (locus start)
693 bool r = false;
694 char c;
695 locus old_loc = gfc_current_locus;
697 if ((c = next_char ()) == 'g' || c == 'G')
698 if ((c = next_char ()) == 'c' || c == 'C')
699 if ((c = next_char ()) == 'c' || c == 'C')
700 if ((c = next_char ()) == '$')
701 r = true;
703 if (r == false)
704 gfc_current_locus = old_loc;
705 else
707 gcc_attribute_flag = 1;
708 gcc_attribute_locus = old_loc;
709 gfc_current_locus = start;
712 return r;
715 /* Return true if CC was matched. */
716 static bool
717 skip_free_oacc_sentinel (locus start, locus old_loc)
719 bool r = false;
720 char c;
722 if ((c = next_char ()) == 'c' || c == 'C')
723 if ((c = next_char ()) == 'c' || c == 'C')
724 r = true;
726 if (r)
728 if ((c = next_char ()) == ' ' || c == '\t'
729 || continue_flag)
731 while (gfc_is_whitespace (c))
732 c = next_char ();
733 if (c != '\n' && c != '!')
735 openacc_flag = 1;
736 openacc_locus = old_loc;
737 gfc_current_locus = start;
739 else
740 r = false;
742 else
744 gfc_warning_now (0, "!$ACC at %C starts a commented "
745 "line as it neither is followed "
746 "by a space nor is a "
747 "continuation line");
748 r = false;
752 return r;
755 /* Return true if MP was matched. */
756 static bool
757 skip_free_omp_sentinel (locus start, locus old_loc)
759 bool r = false;
760 char c;
762 if ((c = next_char ()) == 'm' || c == 'M')
763 if ((c = next_char ()) == 'p' || c == 'P')
764 r = true;
766 if (r)
768 if ((c = next_char ()) == ' ' || c == '\t'
769 || continue_flag)
771 while (gfc_is_whitespace (c))
772 c = next_char ();
773 if (c != '\n' && c != '!')
775 openmp_flag = 1;
776 openmp_locus = old_loc;
777 gfc_current_locus = start;
779 else
780 r = false;
782 else
784 gfc_warning_now (0, "!$OMP at %C starts a commented "
785 "line as it neither is followed "
786 "by a space nor is a "
787 "continuation line");
788 r = false;
792 return r;
795 /* Comment lines are null lines, lines containing only blanks or lines
796 on which the first nonblank line is a '!'.
797 Return true if !$ openmp or openacc conditional compilation sentinel was
798 seen. */
800 static bool
801 skip_free_comments (void)
803 locus start;
804 gfc_char_t c;
805 int at_bol;
807 for (;;)
809 at_bol = gfc_at_bol ();
810 start = gfc_current_locus;
811 if (gfc_at_eof ())
812 break;
815 c = next_char ();
816 while (gfc_is_whitespace (c));
818 if (c == '\n')
820 gfc_advance_line ();
821 continue;
824 if (c == '!')
826 /* Keep the !GCC$ line. */
827 if (at_bol && skip_gcc_attribute (start))
828 return false;
830 /* If -fopenmp/-fopenacc, we need to handle here 2 things:
831 1) don't treat !$omp/!$acc as comments, but directives
832 2) handle OpenMP/OpenACC conditional compilation, where
833 !$ should be treated as 2 spaces (for initial lines
834 only if followed by space). */
835 if (at_bol)
837 if ((flag_openmp || flag_openmp_simd)
838 && flag_openacc)
840 locus old_loc = gfc_current_locus;
841 if (next_char () == '$')
843 c = next_char ();
844 if (c == 'o' || c == 'O')
846 if (skip_free_omp_sentinel (start, old_loc))
847 return false;
848 gfc_current_locus = old_loc;
849 next_char ();
850 c = next_char ();
852 else if (c == 'a' || c == 'A')
854 if (skip_free_oacc_sentinel (start, old_loc))
855 return false;
856 gfc_current_locus = old_loc;
857 next_char ();
858 c = next_char ();
860 if (continue_flag || c == ' ' || c == '\t')
862 gfc_current_locus = old_loc;
863 next_char ();
864 openmp_flag = openacc_flag = 0;
865 return true;
868 gfc_current_locus = old_loc;
870 else if ((flag_openmp || flag_openmp_simd)
871 && !flag_openacc)
873 locus old_loc = gfc_current_locus;
874 if (next_char () == '$')
876 c = next_char ();
877 if (c == 'o' || c == 'O')
879 if (skip_free_omp_sentinel (start, old_loc))
880 return false;
881 gfc_current_locus = old_loc;
882 next_char ();
883 c = next_char ();
885 if (continue_flag || c == ' ' || c == '\t')
887 gfc_current_locus = old_loc;
888 next_char ();
889 openmp_flag = 0;
890 return true;
893 gfc_current_locus = old_loc;
895 else if (flag_openacc
896 && !(flag_openmp || flag_openmp_simd))
898 locus old_loc = gfc_current_locus;
899 if (next_char () == '$')
901 c = next_char ();
902 if (c == 'a' || c == 'A')
904 if (skip_free_oacc_sentinel (start, old_loc))
905 return false;
906 gfc_current_locus = old_loc;
907 next_char();
908 c = next_char();
910 if (continue_flag || c == ' ' || c == '\t')
912 gfc_current_locus = old_loc;
913 next_char();
914 openacc_flag = 0;
915 return true;
918 gfc_current_locus = old_loc;
921 skip_comment_line ();
922 continue;
925 break;
928 if (openmp_flag && at_bol)
929 openmp_flag = 0;
931 if (openacc_flag && at_bol)
932 openacc_flag = 0;
934 gcc_attribute_flag = 0;
935 gfc_current_locus = start;
936 return false;
939 /* Return true if MP was matched in fixed form. */
940 static bool
941 skip_fixed_omp_sentinel (locus *start)
943 gfc_char_t c;
944 if (((c = next_char ()) == 'm' || c == 'M')
945 && ((c = next_char ()) == 'p' || c == 'P'))
947 c = next_char ();
948 if (c != '\n'
949 && (continue_flag
950 || c == ' ' || c == '\t' || c == '0'))
953 c = next_char ();
954 while (gfc_is_whitespace (c));
955 if (c != '\n' && c != '!')
957 /* Canonicalize to *$omp. */
958 *start->nextc = '*';
959 openmp_flag = 1;
960 gfc_current_locus = *start;
961 return true;
965 return false;
968 /* Return true if CC was matched in fixed form. */
969 static bool
970 skip_fixed_oacc_sentinel (locus *start)
972 gfc_char_t c;
973 if (((c = next_char ()) == 'c' || c == 'C')
974 && ((c = next_char ()) == 'c' || c == 'C'))
976 c = next_char ();
977 if (c != '\n'
978 && (continue_flag
979 || c == ' ' || c == '\t' || c == '0'))
982 c = next_char ();
983 while (gfc_is_whitespace (c));
984 if (c != '\n' && c != '!')
986 /* Canonicalize to *$acc. */
987 *start->nextc = '*';
988 openacc_flag = 1;
989 gfc_current_locus = *start;
990 return true;
994 return false;
997 /* Skip comment lines in fixed source mode. We have the same rules as
998 in skip_free_comment(), except that we can have a 'c', 'C' or '*'
999 in column 1, and a '!' cannot be in column 6. Also, we deal with
1000 lines with 'd' or 'D' in column 1, if the user requested this. */
1002 static void
1003 skip_fixed_comments (void)
1005 locus start;
1006 int col;
1007 gfc_char_t c;
1009 if (! gfc_at_bol ())
1011 start = gfc_current_locus;
1012 if (! gfc_at_eof ())
1015 c = next_char ();
1016 while (gfc_is_whitespace (c));
1018 if (c == '\n')
1019 gfc_advance_line ();
1020 else if (c == '!')
1021 skip_comment_line ();
1024 if (! gfc_at_bol ())
1026 gfc_current_locus = start;
1027 return;
1031 for (;;)
1033 start = gfc_current_locus;
1034 if (gfc_at_eof ())
1035 break;
1037 c = next_char ();
1038 if (c == '\n')
1040 gfc_advance_line ();
1041 continue;
1044 if (c == '!' || c == 'c' || c == 'C' || c == '*')
1046 if (skip_gcc_attribute (start))
1048 /* Canonicalize to *$omp. */
1049 *start.nextc = '*';
1050 return;
1053 /* If -fopenmp/-fopenacc, we need to handle here 2 things:
1054 1) don't treat !$omp/!$acc|c$omp/c$acc|*$omp / *$acc as comments,
1055 but directives
1056 2) handle OpenMP/OpenACC conditional compilation, where
1057 !$|c$|*$ should be treated as 2 spaces if the characters
1058 in columns 3 to 6 are valid fixed form label columns
1059 characters. */
1060 if (gfc_current_locus.lb != NULL
1061 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
1062 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
1064 if ((flag_openmp || flag_openmp_simd) && !flag_openacc)
1066 if (next_char () == '$')
1068 c = next_char ();
1069 if (c == 'o' || c == 'O')
1071 if (skip_fixed_omp_sentinel (&start))
1072 return;
1074 else
1075 goto check_for_digits;
1077 gfc_current_locus = start;
1080 if (flag_openacc && !(flag_openmp || flag_openmp_simd))
1082 if (next_char () == '$')
1084 c = next_char ();
1085 if (c == 'a' || c == 'A')
1087 if (skip_fixed_oacc_sentinel (&start))
1088 return;
1090 else
1091 goto check_for_digits;
1093 gfc_current_locus = start;
1096 if (flag_openacc || flag_openmp || flag_openmp_simd)
1098 if (next_char () == '$')
1100 c = next_char ();
1101 if (c == 'a' || c == 'A')
1103 if (skip_fixed_oacc_sentinel (&start))
1104 return;
1106 else if (c == 'o' || c == 'O')
1108 if (skip_fixed_omp_sentinel (&start))
1109 return;
1111 else
1112 goto check_for_digits;
1114 gfc_current_locus = start;
1117 skip_comment_line ();
1118 continue;
1120 gcc_unreachable ();
1121 check_for_digits:
1123 int digit_seen = 0;
1125 for (col = 3; col < 6; col++, c = next_char ())
1126 if (c == ' ')
1127 continue;
1128 else if (c == '\t')
1130 col = 6;
1131 break;
1133 else if (c < '0' || c > '9')
1134 break;
1135 else
1136 digit_seen = 1;
1138 if (col == 6 && c != '\n'
1139 && ((continue_flag && !digit_seen)
1140 || c == ' ' || c == '\t' || c == '0'))
1142 gfc_current_locus = start;
1143 start.nextc[0] = ' ';
1144 start.nextc[1] = ' ';
1145 continue;
1148 skip_comment_line ();
1149 continue;
1152 if (gfc_option.flag_d_lines != -1 && (c == 'd' || c == 'D'))
1154 if (gfc_option.flag_d_lines == 0)
1156 skip_comment_line ();
1157 continue;
1159 else
1160 *start.nextc = c = ' ';
1163 col = 1;
1165 while (gfc_is_whitespace (c))
1167 c = next_char ();
1168 col++;
1171 if (c == '\n')
1173 gfc_advance_line ();
1174 continue;
1177 if (col != 6 && c == '!')
1179 if (gfc_current_locus.lb != NULL
1180 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
1181 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
1182 skip_comment_line ();
1183 continue;
1186 break;
1189 openmp_flag = 0;
1190 openacc_flag = 0;
1191 gcc_attribute_flag = 0;
1192 gfc_current_locus = start;
1196 /* Skips the current line if it is a comment. */
1198 void
1199 gfc_skip_comments (void)
1201 if (gfc_current_form == FORM_FREE)
1202 skip_free_comments ();
1203 else
1204 skip_fixed_comments ();
1208 /* Get the next character from the input, taking continuation lines
1209 and end-of-line comments into account. This implies that comment
1210 lines between continued lines must be eaten here. For higher-level
1211 subroutines, this flattens continued lines into a single logical
1212 line. The in_string flag denotes whether we're inside a character
1213 context or not. */
1215 gfc_char_t
1216 gfc_next_char_literal (gfc_instring in_string)
1218 locus old_loc;
1219 int i, prev_openmp_flag, prev_openacc_flag;
1220 gfc_char_t c;
1222 continue_flag = 0;
1223 prev_openacc_flag = prev_openmp_flag = 0;
1225 restart:
1226 c = next_char ();
1227 if (gfc_at_end ())
1229 continue_count = 0;
1230 return c;
1233 if (gfc_current_form == FORM_FREE)
1235 bool openmp_cond_flag;
1237 if (!in_string && c == '!')
1239 if (gcc_attribute_flag
1240 && memcmp (&gfc_current_locus, &gcc_attribute_locus,
1241 sizeof (gfc_current_locus)) == 0)
1242 goto done;
1244 if (openmp_flag
1245 && memcmp (&gfc_current_locus, &openmp_locus,
1246 sizeof (gfc_current_locus)) == 0)
1247 goto done;
1249 if (openacc_flag
1250 && memcmp (&gfc_current_locus, &openacc_locus,
1251 sizeof (gfc_current_locus)) == 0)
1252 goto done;
1254 /* This line can't be continued */
1257 c = next_char ();
1259 while (c != '\n');
1261 /* Avoid truncation warnings for comment ending lines. */
1262 gfc_current_locus.lb->truncated = 0;
1264 goto done;
1267 /* Check to see if the continuation line was truncated. */
1268 if (warn_line_truncation && gfc_current_locus.lb != NULL
1269 && gfc_current_locus.lb->truncated)
1271 int maxlen = flag_free_line_length;
1272 gfc_char_t *current_nextc = gfc_current_locus.nextc;
1274 gfc_current_locus.lb->truncated = 0;
1275 gfc_current_locus.nextc = gfc_current_locus.lb->line + maxlen;
1276 gfc_warning_now (OPT_Wline_truncation,
1277 "Line truncated at %L", &gfc_current_locus);
1278 gfc_current_locus.nextc = current_nextc;
1281 if (c != '&')
1282 goto done;
1284 /* If the next nonblank character is a ! or \n, we've got a
1285 continuation line. */
1286 old_loc = gfc_current_locus;
1288 c = next_char ();
1289 while (gfc_is_whitespace (c))
1290 c = next_char ();
1292 /* Character constants to be continued cannot have commentary
1293 after the '&'. However, there are cases where we may think we
1294 are still in a string and we are looking for a possible
1295 doubled quote and we end up here. See PR64506. */
1297 if (in_string && c != '\n')
1299 gfc_current_locus = old_loc;
1300 c = '&';
1301 goto done;
1304 if (c != '!' && c != '\n')
1306 gfc_current_locus = old_loc;
1307 c = '&';
1308 goto done;
1311 if (flag_openmp)
1312 prev_openmp_flag = openmp_flag;
1313 if (flag_openacc)
1314 prev_openacc_flag = openacc_flag;
1316 continue_flag = 1;
1317 if (c == '!')
1318 skip_comment_line ();
1319 else
1320 gfc_advance_line ();
1322 if (gfc_at_eof ())
1323 goto not_continuation;
1325 /* We've got a continuation line. If we are on the very next line after
1326 the last continuation, increment the continuation line count and
1327 check whether the limit has been exceeded. */
1328 if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1)
1330 if (++continue_count == gfc_option.max_continue_free)
1332 if (gfc_notification_std (GFC_STD_GNU) || pedantic)
1333 gfc_warning (0, "Limit of %d continuations exceeded in "
1334 "statement at %C", gfc_option.max_continue_free);
1338 /* Now find where it continues. First eat any comment lines. */
1339 openmp_cond_flag = skip_free_comments ();
1341 if (gfc_current_locus.lb != NULL
1342 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
1343 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
1345 if (flag_openmp)
1346 if (prev_openmp_flag != openmp_flag && !openacc_flag)
1348 gfc_current_locus = old_loc;
1349 openmp_flag = prev_openmp_flag;
1350 c = '&';
1351 goto done;
1354 if (flag_openacc)
1355 if (prev_openacc_flag != openacc_flag && !openmp_flag)
1357 gfc_current_locus = old_loc;
1358 openacc_flag = prev_openacc_flag;
1359 c = '&';
1360 goto done;
1363 /* Now that we have a non-comment line, probe ahead for the
1364 first non-whitespace character. If it is another '&', then
1365 reading starts at the next character, otherwise we must back
1366 up to where the whitespace started and resume from there. */
1368 old_loc = gfc_current_locus;
1370 c = next_char ();
1371 while (gfc_is_whitespace (c))
1372 c = next_char ();
1374 if (openmp_flag && !openacc_flag)
1376 for (i = 0; i < 5; i++, c = next_char ())
1378 gcc_assert (gfc_wide_tolower (c) == (unsigned char) "!$omp"[i]);
1379 if (i == 4)
1380 old_loc = gfc_current_locus;
1382 while (gfc_is_whitespace (c))
1383 c = next_char ();
1385 if (openacc_flag && !openmp_flag)
1387 for (i = 0; i < 5; i++, c = next_char ())
1389 gcc_assert (gfc_wide_tolower (c) == (unsigned char) "!$acc"[i]);
1390 if (i == 4)
1391 old_loc = gfc_current_locus;
1393 while (gfc_is_whitespace (c))
1394 c = next_char ();
1397 /* In case we have an OpenMP directive continued by OpenACC
1398 sentinel, or vice versa, we get both openmp_flag and
1399 openacc_flag on. */
1401 if (openacc_flag && openmp_flag)
1403 int is_openmp = 0;
1404 for (i = 0; i < 5; i++, c = next_char ())
1406 if (gfc_wide_tolower (c) != (unsigned char) "!$acc"[i])
1407 is_openmp = 1;
1408 if (i == 4)
1409 old_loc = gfc_current_locus;
1411 gfc_error (is_openmp
1412 ? G_("Wrong OpenACC continuation at %C: "
1413 "expected !$ACC, got !$OMP")
1414 : G_("Wrong OpenMP continuation at %C: "
1415 "expected !$OMP, got !$ACC"));
1418 if (c != '&')
1420 if (in_string && gfc_current_locus.nextc)
1422 gfc_current_locus.nextc--;
1423 if (warn_ampersand && in_string == INSTRING_WARN)
1424 gfc_warning (OPT_Wampersand,
1425 "Missing %<&%> in continued character "
1426 "constant at %C");
1428 else if (!in_string && (c == '\'' || c == '"'))
1429 goto done;
1430 /* Both !$omp and !$ -fopenmp continuation lines have & on the
1431 continuation line only optionally. */
1432 else if (openmp_flag || openacc_flag || openmp_cond_flag)
1434 if (gfc_current_locus.nextc)
1435 gfc_current_locus.nextc--;
1437 else
1439 c = ' ';
1440 gfc_current_locus = old_loc;
1441 goto done;
1445 else /* Fixed form. */
1447 /* Fixed form continuation. */
1448 if (in_string != INSTRING_WARN && c == '!')
1450 /* Skip comment at end of line. */
1453 c = next_char ();
1455 while (c != '\n');
1457 /* Avoid truncation warnings for comment ending lines. */
1458 gfc_current_locus.lb->truncated = 0;
1461 if (c != '\n')
1462 goto done;
1464 /* Check to see if the continuation line was truncated. */
1465 if (warn_line_truncation && gfc_current_locus.lb != NULL
1466 && gfc_current_locus.lb->truncated)
1468 gfc_current_locus.lb->truncated = 0;
1469 gfc_warning_now (OPT_Wline_truncation,
1470 "Line truncated at %L", &gfc_current_locus);
1473 if (flag_openmp)
1474 prev_openmp_flag = openmp_flag;
1475 if (flag_openacc)
1476 prev_openacc_flag = openacc_flag;
1478 continue_flag = 1;
1479 old_loc = gfc_current_locus;
1481 gfc_advance_line ();
1482 skip_fixed_comments ();
1484 /* See if this line is a continuation line. */
1485 if (flag_openmp && openmp_flag != prev_openmp_flag && !openacc_flag)
1487 openmp_flag = prev_openmp_flag;
1488 goto not_continuation;
1490 if (flag_openacc && openacc_flag != prev_openacc_flag && !openmp_flag)
1492 openacc_flag = prev_openacc_flag;
1493 goto not_continuation;
1496 /* In case we have an OpenMP directive continued by OpenACC
1497 sentinel, or vice versa, we get both openmp_flag and
1498 openacc_flag on. */
1499 if (openacc_flag && openmp_flag)
1501 int is_openmp = 0;
1502 for (i = 0; i < 5; i++)
1504 c = next_char ();
1505 if (gfc_wide_tolower (c) != (unsigned char) "*$acc"[i])
1506 is_openmp = 1;
1508 gfc_error (is_openmp
1509 ? G_("Wrong OpenACC continuation at %C: "
1510 "expected !$ACC, got !$OMP")
1511 : G_("Wrong OpenMP continuation at %C: "
1512 "expected !$OMP, got !$ACC"));
1514 else if (!openmp_flag && !openacc_flag)
1515 for (i = 0; i < 5; i++)
1517 c = next_char ();
1518 if (c != ' ')
1519 goto not_continuation;
1521 else if (openmp_flag)
1522 for (i = 0; i < 5; i++)
1524 c = next_char ();
1525 if (gfc_wide_tolower (c) != (unsigned char) "*$omp"[i])
1526 goto not_continuation;
1528 else if (openacc_flag)
1529 for (i = 0; i < 5; i++)
1531 c = next_char ();
1532 if (gfc_wide_tolower (c) != (unsigned char) "*$acc"[i])
1533 goto not_continuation;
1536 c = next_char ();
1537 if (c == '0' || c == ' ' || c == '\n')
1538 goto not_continuation;
1540 /* We've got a continuation line. If we are on the very next line after
1541 the last continuation, increment the continuation line count and
1542 check whether the limit has been exceeded. */
1543 if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1)
1545 if (++continue_count == gfc_option.max_continue_fixed)
1547 if (gfc_notification_std (GFC_STD_GNU) || pedantic)
1548 gfc_warning (0, "Limit of %d continuations exceeded in "
1549 "statement at %C",
1550 gfc_option.max_continue_fixed);
1554 if (gfc_current_locus.lb != NULL
1555 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
1556 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
1559 /* Ready to read first character of continuation line, which might
1560 be another continuation line! */
1561 goto restart;
1563 not_continuation:
1564 c = '\n';
1565 gfc_current_locus = old_loc;
1566 end_flag = 0;
1568 done:
1569 if (c == '\n')
1570 continue_count = 0;
1571 continue_flag = 0;
1572 return c;
1576 /* Get the next character of input, folded to lowercase. In fixed
1577 form mode, we also ignore spaces. When matcher subroutines are
1578 parsing character literals, they have to call
1579 gfc_next_char_literal(). */
1581 gfc_char_t
1582 gfc_next_char (void)
1584 gfc_char_t c;
1588 c = gfc_next_char_literal (NONSTRING);
1590 while (gfc_current_form == FORM_FIXED && gfc_is_whitespace (c));
1592 return gfc_wide_tolower (c);
1595 char
1596 gfc_next_ascii_char (void)
1598 gfc_char_t c = gfc_next_char ();
1600 return (gfc_wide_fits_in_byte (c) ? (unsigned char) c
1601 : (unsigned char) UCHAR_MAX);
1605 gfc_char_t
1606 gfc_peek_char (void)
1608 locus old_loc;
1609 gfc_char_t c;
1611 old_loc = gfc_current_locus;
1612 c = gfc_next_char ();
1613 gfc_current_locus = old_loc;
1615 return c;
1619 char
1620 gfc_peek_ascii_char (void)
1622 gfc_char_t c = gfc_peek_char ();
1624 return (gfc_wide_fits_in_byte (c) ? (unsigned char) c
1625 : (unsigned char) UCHAR_MAX);
1629 /* Recover from an error. We try to get past the current statement
1630 and get lined up for the next. The next statement follows a '\n'
1631 or a ';'. We also assume that we are not within a character
1632 constant, and deal with finding a '\'' or '"'. */
1634 void
1635 gfc_error_recovery (void)
1637 gfc_char_t c, delim;
1639 if (gfc_at_eof ())
1640 return;
1642 for (;;)
1644 c = gfc_next_char ();
1645 if (c == '\n' || c == ';')
1646 break;
1648 if (c != '\'' && c != '"')
1650 if (gfc_at_eof ())
1651 break;
1652 continue;
1654 delim = c;
1656 for (;;)
1658 c = next_char ();
1660 if (c == delim)
1661 break;
1662 if (c == '\n')
1663 return;
1664 if (c == '\\')
1666 c = next_char ();
1667 if (c == '\n')
1668 return;
1671 if (gfc_at_eof ())
1672 break;
1677 /* Read ahead until the next character to be read is not whitespace. */
1679 void
1680 gfc_gobble_whitespace (void)
1682 static int linenum = 0;
1683 locus old_loc;
1684 gfc_char_t c;
1688 old_loc = gfc_current_locus;
1689 c = gfc_next_char_literal (NONSTRING);
1690 /* Issue a warning for nonconforming tabs. We keep track of the line
1691 number because the Fortran matchers will often back up and the same
1692 line will be scanned multiple times. */
1693 if (warn_tabs && c == '\t')
1695 int cur_linenum = LOCATION_LINE (gfc_current_locus.lb->location);
1696 if (cur_linenum != linenum)
1698 linenum = cur_linenum;
1699 gfc_warning_now (OPT_Wtabs, "Nonconforming tab character at %C");
1703 while (gfc_is_whitespace (c));
1705 if (!ISPRINT(c) && c != '\n' && last_error_char != gfc_current_locus.nextc)
1707 char buf[20];
1708 last_error_char = gfc_current_locus.nextc;
1709 snprintf (buf, 20, "%2.2X", c);
1710 gfc_error_now ("Invalid character 0x%s at %C", buf);
1713 gfc_current_locus = old_loc;
1717 /* Load a single line into pbuf.
1719 If pbuf points to a NULL pointer, it is allocated.
1720 We truncate lines that are too long, unless we're dealing with
1721 preprocessor lines or if the option -ffixed-line-length-none is set,
1722 in which case we reallocate the buffer to fit the entire line, if
1723 need be.
1724 In fixed mode, we expand a tab that occurs within the statement
1725 label region to expand to spaces that leave the next character in
1726 the source region.
1728 If first_char is not NULL, it's a pointer to a single char value holding
1729 the first character of the line, which has already been read by the
1730 caller. This avoids the use of ungetc().
1732 load_line returns whether the line was truncated.
1734 NOTE: The error machinery isn't available at this point, so we can't
1735 easily report line and column numbers consistent with other
1736 parts of gfortran. */
1738 static int
1739 load_line (FILE *input, gfc_char_t **pbuf, int *pbuflen, const int *first_char)
1741 static int linenum = 0, current_line = 1;
1742 int c, maxlen, i, preprocessor_flag, buflen = *pbuflen;
1743 int trunc_flag = 0, seen_comment = 0;
1744 int seen_printable = 0, seen_ampersand = 0, quoted = ' ';
1745 gfc_char_t *buffer;
1746 bool found_tab = false;
1748 /* Determine the maximum allowed line length. */
1749 if (gfc_current_form == FORM_FREE)
1750 maxlen = flag_free_line_length;
1751 else if (gfc_current_form == FORM_FIXED)
1752 maxlen = flag_fixed_line_length;
1753 else
1754 maxlen = 72;
1756 if (*pbuf == NULL)
1758 /* Allocate the line buffer, storing its length into buflen.
1759 Note that if maxlen==0, indicating that arbitrary-length lines
1760 are allowed, the buffer will be reallocated if this length is
1761 insufficient; since 132 characters is the length of a standard
1762 free-form line, we use that as a starting guess. */
1763 if (maxlen > 0)
1764 buflen = maxlen;
1765 else
1766 buflen = 132;
1768 *pbuf = gfc_get_wide_string (buflen + 1);
1771 i = 0;
1772 buffer = *pbuf;
1774 if (first_char)
1775 c = *first_char;
1776 else
1777 c = getc (input);
1779 /* In order to not truncate preprocessor lines, we have to
1780 remember that this is one. */
1781 preprocessor_flag = (c == '#' ? 1 : 0);
1783 for (;;)
1785 if (c == EOF)
1786 break;
1788 if (c == '\n')
1790 /* Check for illegal use of ampersand. See F95 Standard 3.3.1.3. */
1791 if (gfc_current_form == FORM_FREE
1792 && !seen_printable && seen_ampersand)
1794 if (pedantic)
1795 gfc_error_now ("%<&%> not allowed by itself in line %d",
1796 current_line);
1797 else
1798 gfc_warning_now (0, "%<&%> not allowed by itself in line %d",
1799 current_line);
1801 break;
1804 if (c == '\r' || c == '\0')
1805 goto next_char; /* Gobble characters. */
1807 if (c == '&')
1809 if (seen_ampersand)
1811 seen_ampersand = 0;
1812 seen_printable = 1;
1814 else
1815 seen_ampersand = 1;
1818 if ((c != '&' && c != '!' && c != ' ') || (c == '!' && !seen_ampersand))
1819 seen_printable = 1;
1821 /* Is this a fixed-form comment? */
1822 if (gfc_current_form == FORM_FIXED && i == 0
1823 && (c == '*' || c == 'c' || c == 'd'))
1824 seen_comment = 1;
1826 if (quoted == ' ')
1828 if (c == '\'' || c == '"')
1829 quoted = c;
1831 else if (c == quoted)
1832 quoted = ' ';
1834 /* Is this a free-form comment? */
1835 if (c == '!' && quoted == ' ')
1836 seen_comment = 1;
1838 /* Vendor extension: "<tab>1" marks a continuation line. */
1839 if (found_tab)
1841 found_tab = false;
1842 if (c >= '1' && c <= '9')
1844 *(buffer-1) = c;
1845 goto next_char;
1849 if (gfc_current_form == FORM_FIXED && c == '\t' && i < 6)
1851 found_tab = true;
1853 if (warn_tabs && seen_comment == 0 && current_line != linenum)
1855 linenum = current_line;
1856 gfc_warning_now (OPT_Wtabs,
1857 "Nonconforming tab character in column %d "
1858 "of line %d", i+1, linenum);
1861 while (i < 6)
1863 *buffer++ = ' ';
1864 i++;
1867 goto next_char;
1870 *buffer++ = c;
1871 i++;
1873 if (maxlen == 0 || preprocessor_flag)
1875 if (i >= buflen)
1877 /* Reallocate line buffer to double size to hold the
1878 overlong line. */
1879 buflen = buflen * 2;
1880 *pbuf = XRESIZEVEC (gfc_char_t, *pbuf, (buflen + 1));
1881 buffer = (*pbuf) + i;
1884 else if (i >= maxlen)
1886 bool trunc_warn = true;
1888 /* Enhancement, if the very next non-space character is an ampersand
1889 or comment that we would otherwise warn about, don't mark as
1890 truncated. */
1892 /* Truncate the rest of the line. */
1893 for (;;)
1895 c = getc (input);
1896 if (c == '\r' || c == ' ')
1897 continue;
1899 if (c == '\n' || c == EOF)
1900 break;
1902 if (!trunc_warn && c != '!')
1903 trunc_warn = true;
1905 if (trunc_warn && ((gfc_current_form == FORM_FIXED && c == '&')
1906 || c == '!'))
1907 trunc_warn = false;
1909 if (c == '!')
1910 seen_comment = 1;
1912 if (trunc_warn && !seen_comment)
1913 trunc_flag = 1;
1916 c = '\n';
1917 continue;
1920 next_char:
1921 c = getc (input);
1924 /* Pad lines to the selected line length in fixed form. */
1925 if (gfc_current_form == FORM_FIXED
1926 && flag_fixed_line_length != 0
1927 && !preprocessor_flag
1928 && c != EOF)
1930 while (i++ < maxlen)
1931 *buffer++ = ' ';
1934 *buffer = '\0';
1935 *pbuflen = buflen;
1936 current_line++;
1938 return trunc_flag;
1942 /* Get a gfc_file structure, initialize it and add it to
1943 the file stack. */
1945 static gfc_file *
1946 get_file (const char *name, enum lc_reason reason ATTRIBUTE_UNUSED)
1948 gfc_file *f;
1950 f = XCNEW (gfc_file);
1952 f->filename = xstrdup (name);
1954 f->next = file_head;
1955 file_head = f;
1957 f->up = current_file;
1958 if (current_file != NULL)
1959 f->inclusion_line = current_file->line;
1961 linemap_add (line_table, reason, false, f->filename, 1);
1963 return f;
1967 /* Deal with a line from the C preprocessor. The
1968 initial octothorp has already been seen. */
1970 static void
1971 preprocessor_line (gfc_char_t *c)
1973 bool flag[5];
1974 int i, line;
1975 gfc_char_t *wide_filename;
1976 gfc_file *f;
1977 int escaped, unescape;
1978 char *filename;
1980 c++;
1981 while (*c == ' ' || *c == '\t')
1982 c++;
1984 if (*c < '0' || *c > '9')
1985 goto bad_cpp_line;
1987 line = wide_atoi (c);
1989 c = wide_strchr (c, ' ');
1990 if (c == NULL)
1992 /* No file name given. Set new line number. */
1993 current_file->line = line;
1994 return;
1997 /* Skip spaces. */
1998 while (*c == ' ' || *c == '\t')
1999 c++;
2001 /* Skip quote. */
2002 if (*c != '"')
2003 goto bad_cpp_line;
2004 ++c;
2006 wide_filename = c;
2008 /* Make filename end at quote. */
2009 unescape = 0;
2010 escaped = false;
2011 while (*c && ! (!escaped && *c == '"'))
2013 if (escaped)
2014 escaped = false;
2015 else if (*c == '\\')
2017 escaped = true;
2018 unescape++;
2020 ++c;
2023 if (! *c)
2024 /* Preprocessor line has no closing quote. */
2025 goto bad_cpp_line;
2027 *c++ = '\0';
2029 /* Undo effects of cpp_quote_string. */
2030 if (unescape)
2032 gfc_char_t *s = wide_filename;
2033 gfc_char_t *d = gfc_get_wide_string (c - wide_filename - unescape);
2035 wide_filename = d;
2036 while (*s)
2038 if (*s == '\\')
2039 *d++ = *++s;
2040 else
2041 *d++ = *s;
2042 s++;
2044 *d = '\0';
2047 /* Get flags. */
2049 flag[1] = flag[2] = flag[3] = flag[4] = false;
2051 for (;;)
2053 c = wide_strchr (c, ' ');
2054 if (c == NULL)
2055 break;
2057 c++;
2058 i = wide_atoi (c);
2060 if (i >= 1 && i <= 4)
2061 flag[i] = true;
2064 /* Convert the filename in wide characters into a filename in narrow
2065 characters. */
2066 filename = gfc_widechar_to_char (wide_filename, -1);
2068 /* Interpret flags. */
2070 if (flag[1]) /* Starting new file. */
2072 f = get_file (filename, LC_RENAME);
2073 add_file_change (f->filename, f->inclusion_line);
2074 current_file = f;
2077 if (flag[2]) /* Ending current file. */
2079 if (!current_file->up
2080 || filename_cmp (current_file->up->filename, filename) != 0)
2082 linemap_line_start (line_table, current_file->line, 80);
2083 /* ??? One could compute the exact column where the filename
2084 starts and compute the exact location here. */
2085 gfc_warning_now_at (linemap_position_for_column (line_table, 1),
2086 0, "file %qs left but not entered",
2087 filename);
2088 current_file->line++;
2089 if (unescape)
2090 free (wide_filename);
2091 free (filename);
2092 return;
2095 add_file_change (NULL, line);
2096 current_file = current_file->up;
2097 linemap_add (line_table, LC_RENAME, false, current_file->filename,
2098 current_file->line);
2101 /* The name of the file can be a temporary file produced by
2102 cpp. Replace the name if it is different. */
2104 if (filename_cmp (current_file->filename, filename) != 0)
2106 /* FIXME: we leak the old filename because a pointer to it may be stored
2107 in the linemap. Alternative could be using GC or updating linemap to
2108 point to the new name, but there is no API for that currently. */
2109 current_file->filename = xstrdup (filename);
2111 /* We need to tell the linemap API that the filename changed. Just
2112 changing current_file is insufficient. */
2113 linemap_add (line_table, LC_RENAME, false, current_file->filename, line);
2116 /* Set new line number. */
2117 current_file->line = line;
2118 if (unescape)
2119 free (wide_filename);
2120 free (filename);
2121 return;
2123 bad_cpp_line:
2124 linemap_line_start (line_table, current_file->line, 80);
2125 /* ??? One could compute the exact column where the directive
2126 starts and compute the exact location here. */
2127 gfc_warning_now_at (linemap_position_for_column (line_table, 2), 0,
2128 "Illegal preprocessor directive");
2129 current_file->line++;
2133 static bool load_file (const char *, const char *, bool);
2135 /* include_line()-- Checks a line buffer to see if it is an include
2136 line. If so, we call load_file() recursively to load the included
2137 file. We never return a syntax error because a statement like
2138 "include = 5" is perfectly legal. We return false if no include was
2139 processed or true if we matched an include. */
2141 static bool
2142 include_line (gfc_char_t *line)
2144 gfc_char_t quote, *c, *begin, *stop;
2145 char *filename;
2147 c = line;
2149 if (flag_openmp || flag_openmp_simd)
2151 if (gfc_current_form == FORM_FREE)
2153 while (*c == ' ' || *c == '\t')
2154 c++;
2155 if (*c == '!' && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
2156 c += 3;
2158 else
2160 if ((*c == '!' || *c == 'c' || *c == 'C' || *c == '*')
2161 && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
2162 c += 3;
2166 while (*c == ' ' || *c == '\t')
2167 c++;
2169 if (gfc_wide_strncasecmp (c, "include", 7))
2170 return false;
2172 c += 7;
2173 while (*c == ' ' || *c == '\t')
2174 c++;
2176 /* Find filename between quotes. */
2178 quote = *c++;
2179 if (quote != '"' && quote != '\'')
2180 return false;
2182 begin = c;
2184 while (*c != quote && *c != '\0')
2185 c++;
2187 if (*c == '\0')
2188 return false;
2190 stop = c++;
2192 while (*c == ' ' || *c == '\t')
2193 c++;
2195 if (*c != '\0' && *c != '!')
2196 return false;
2198 /* We have an include line at this point. */
2200 *stop = '\0'; /* It's ok to trash the buffer, as this line won't be
2201 read by anything else. */
2203 filename = gfc_widechar_to_char (begin, -1);
2204 if (!load_file (filename, NULL, false))
2205 exit (FATAL_EXIT_CODE);
2207 free (filename);
2208 return true;
2212 /* Load a file into memory by calling load_line until the file ends. */
2214 static bool
2215 load_file (const char *realfilename, const char *displayedname, bool initial)
2217 gfc_char_t *line;
2218 gfc_linebuf *b;
2219 gfc_file *f;
2220 FILE *input;
2221 int len, line_len;
2222 bool first_line;
2223 struct stat st;
2224 int stat_result;
2225 const char *filename;
2226 /* If realfilename and displayedname are different and non-null then
2227 surely realfilename is the preprocessed form of
2228 displayedname. */
2229 bool preprocessed_p = (realfilename && displayedname
2230 && strcmp (realfilename, displayedname));
2232 filename = displayedname ? displayedname : realfilename;
2234 for (f = current_file; f; f = f->up)
2235 if (filename_cmp (filename, f->filename) == 0)
2237 fprintf (stderr, "%s:%d: Error: File '%s' is being included "
2238 "recursively\n", current_file->filename, current_file->line,
2239 filename);
2240 return false;
2243 if (initial)
2245 if (gfc_src_file)
2247 input = gfc_src_file;
2248 gfc_src_file = NULL;
2250 else
2251 input = gfc_open_file (realfilename);
2253 if (input == NULL)
2255 gfc_error_now ("Can't open file %qs", filename);
2256 return false;
2259 else
2261 input = gfc_open_included_file (realfilename, false, false);
2262 if (input == NULL)
2264 fprintf (stderr, "%s:%d: Error: Can't open included file '%s'\n",
2265 current_file->filename, current_file->line, filename);
2266 return false;
2268 stat_result = stat (realfilename, &st);
2269 if (stat_result == 0 && !S_ISREG(st.st_mode))
2271 fprintf (stderr, "%s:%d: Error: Included path '%s'"
2272 " is not a regular file\n",
2273 current_file->filename, current_file->line, filename);
2274 fclose (input);
2275 return false;
2279 /* Load the file.
2281 A "non-initial" file means a file that is being included. In
2282 that case we are creating an LC_ENTER map.
2284 An "initial" file means a main file; one that is not included.
2285 That file has already got at least one (surely more) line map(s)
2286 created by gfc_init. So the subsequent map created in that case
2287 must have LC_RENAME reason.
2289 This latter case is not true for a preprocessed file. In that
2290 case, although the file is "initial", the line maps created by
2291 gfc_init was used during the preprocessing of the file. Now that
2292 the preprocessing is over and we are being fed the result of that
2293 preprocessing, we need to create a brand new line map for the
2294 preprocessed file, so the reason is going to be LC_ENTER. */
2296 f = get_file (filename, (initial && !preprocessed_p) ? LC_RENAME : LC_ENTER);
2297 if (!initial)
2298 add_file_change (f->filename, f->inclusion_line);
2299 current_file = f;
2300 current_file->line = 1;
2301 line = NULL;
2302 line_len = 0;
2303 first_line = true;
2305 if (initial && gfc_src_preprocessor_lines[0])
2307 preprocessor_line (gfc_src_preprocessor_lines[0]);
2308 free (gfc_src_preprocessor_lines[0]);
2309 gfc_src_preprocessor_lines[0] = NULL;
2310 if (gfc_src_preprocessor_lines[1])
2312 preprocessor_line (gfc_src_preprocessor_lines[1]);
2313 free (gfc_src_preprocessor_lines[1]);
2314 gfc_src_preprocessor_lines[1] = NULL;
2318 for (;;)
2320 int trunc = load_line (input, &line, &line_len, NULL);
2322 len = gfc_wide_strlen (line);
2323 if (feof (input) && len == 0)
2324 break;
2326 /* If this is the first line of the file, it can contain a byte
2327 order mark (BOM), which we will ignore:
2328 FF FE is UTF-16 little endian,
2329 FE FF is UTF-16 big endian,
2330 EF BB BF is UTF-8. */
2331 if (first_line
2332 && ((line_len >= 2 && line[0] == (unsigned char) '\xFF'
2333 && line[1] == (unsigned char) '\xFE')
2334 || (line_len >= 2 && line[0] == (unsigned char) '\xFE'
2335 && line[1] == (unsigned char) '\xFF')
2336 || (line_len >= 3 && line[0] == (unsigned char) '\xEF'
2337 && line[1] == (unsigned char) '\xBB'
2338 && line[2] == (unsigned char) '\xBF')))
2340 int n = line[1] == (unsigned char) '\xBB' ? 3 : 2;
2341 gfc_char_t *new_char = gfc_get_wide_string (line_len);
2343 wide_strcpy (new_char, &line[n]);
2344 free (line);
2345 line = new_char;
2346 len -= n;
2349 /* There are three things this line can be: a line of Fortran
2350 source, an include line or a C preprocessor directive. */
2352 if (line[0] == '#')
2354 /* When -g3 is specified, it's possible that we emit #define
2355 and #undef lines, which we need to pass to the middle-end
2356 so that it can emit correct debug info. */
2357 if (debug_info_level == DINFO_LEVEL_VERBOSE
2358 && (wide_strncmp (line, "#define ", 8) == 0
2359 || wide_strncmp (line, "#undef ", 7) == 0))
2361 else
2363 preprocessor_line (line);
2364 continue;
2368 /* Preprocessed files have preprocessor lines added before the byte
2369 order mark, so first_line is not about the first line of the file
2370 but the first line that's not a preprocessor line. */
2371 first_line = false;
2373 if (include_line (line))
2375 current_file->line++;
2376 continue;
2379 /* Add line. */
2381 b = XCNEWVAR (gfc_linebuf, gfc_linebuf_header_size
2382 + (len + 1) * sizeof (gfc_char_t));
2385 b->location
2386 = linemap_line_start (line_table, current_file->line++, len);
2387 /* ??? We add the location for the maximum column possible here,
2388 because otherwise if the next call creates a new line-map, it
2389 will not reserve space for any offset. */
2390 if (len > 0)
2391 linemap_position_for_column (line_table, len);
2393 b->file = current_file;
2394 b->truncated = trunc;
2395 wide_strcpy (b->line, line);
2397 if (line_head == NULL)
2398 line_head = b;
2399 else
2400 line_tail->next = b;
2402 line_tail = b;
2404 while (file_changes_cur < file_changes_count)
2405 file_changes[file_changes_cur++].lb = b;
2408 /* Release the line buffer allocated in load_line. */
2409 free (line);
2411 fclose (input);
2413 if (!initial)
2414 add_file_change (NULL, current_file->inclusion_line + 1);
2415 current_file = current_file->up;
2416 linemap_add (line_table, LC_LEAVE, 0, NULL, 0);
2417 return true;
2421 /* Open a new file and start scanning from that file. Returns true
2422 if everything went OK, false otherwise. If form == FORM_UNKNOWN
2423 it tries to determine the source form from the filename, defaulting
2424 to free form. */
2426 bool
2427 gfc_new_file (void)
2429 bool result;
2431 if (gfc_cpp_enabled ())
2433 result = gfc_cpp_preprocess (gfc_source_file);
2434 if (!gfc_cpp_preprocess_only ())
2435 result = load_file (gfc_cpp_temporary_file (), gfc_source_file, true);
2437 else
2438 result = load_file (gfc_source_file, NULL, true);
2440 gfc_current_locus.lb = line_head;
2441 gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line;
2443 #if 0 /* Debugging aid. */
2444 for (; line_head; line_head = line_head->next)
2445 printf ("%s:%3d %s\n", LOCATION_FILE (line_head->location),
2446 LOCATION_LINE (line_head->location), line_head->line);
2448 exit (SUCCESS_EXIT_CODE);
2449 #endif
2451 return result;
2454 static char *
2455 unescape_filename (const char *ptr)
2457 const char *p = ptr, *s;
2458 char *d, *ret;
2459 int escaped, unescape = 0;
2461 /* Make filename end at quote. */
2462 escaped = false;
2463 while (*p && ! (! escaped && *p == '"'))
2465 if (escaped)
2466 escaped = false;
2467 else if (*p == '\\')
2469 escaped = true;
2470 unescape++;
2472 ++p;
2475 if (!*p || p[1])
2476 return NULL;
2478 /* Undo effects of cpp_quote_string. */
2479 s = ptr;
2480 d = XCNEWVEC (char, p + 1 - ptr - unescape);
2481 ret = d;
2483 while (s != p)
2485 if (*s == '\\')
2486 *d++ = *++s;
2487 else
2488 *d++ = *s;
2489 s++;
2491 *d = '\0';
2492 return ret;
2495 /* For preprocessed files, if the first tokens are of the form # NUM.
2496 handle the directives so we know the original file name. */
2498 const char *
2499 gfc_read_orig_filename (const char *filename, const char **canon_source_file)
2501 int c, len;
2502 char *dirname, *tmp;
2504 gfc_src_file = gfc_open_file (filename);
2505 if (gfc_src_file == NULL)
2506 return NULL;
2508 c = getc (gfc_src_file);
2510 if (c != '#')
2511 return NULL;
2513 len = 0;
2514 load_line (gfc_src_file, &gfc_src_preprocessor_lines[0], &len, &c);
2516 if (wide_strncmp (gfc_src_preprocessor_lines[0], "# 1 \"", 5) != 0)
2517 return NULL;
2519 tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[0][5], -1);
2520 filename = unescape_filename (tmp);
2521 free (tmp);
2522 if (filename == NULL)
2523 return NULL;
2525 c = getc (gfc_src_file);
2527 if (c != '#')
2528 return filename;
2530 len = 0;
2531 load_line (gfc_src_file, &gfc_src_preprocessor_lines[1], &len, &c);
2533 if (wide_strncmp (gfc_src_preprocessor_lines[1], "# 1 \"", 5) != 0)
2534 return filename;
2536 tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[1][5], -1);
2537 dirname = unescape_filename (tmp);
2538 free (tmp);
2539 if (dirname == NULL)
2540 return filename;
2542 len = strlen (dirname);
2543 if (len < 3 || dirname[len - 1] != '/' || dirname[len - 2] != '/')
2545 free (dirname);
2546 return filename;
2548 dirname[len - 2] = '\0';
2549 set_src_pwd (dirname);
2551 if (! IS_ABSOLUTE_PATH (filename))
2553 char *p = XCNEWVEC (char, len + strlen (filename));
2555 memcpy (p, dirname, len - 2);
2556 p[len - 2] = '/';
2557 strcpy (p + len - 1, filename);
2558 *canon_source_file = p;
2561 free (dirname);
2562 return filename;