Daily bump.
[official-gcc.git] / gcc / fortran / scanner.c
blobf0e6404c6254ac355c2562becf6a8ace7dfda227
1 /* Character scanner.
2 Copyright (C) 2000-2015 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 "flags.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;
84 /* Functions dealing with our wide characters (gfc_char_t) and
85 sequences of such characters. */
87 int
88 gfc_wide_fits_in_byte (gfc_char_t c)
90 return (c <= UCHAR_MAX);
93 static inline int
94 wide_is_ascii (gfc_char_t c)
96 return (gfc_wide_fits_in_byte (c) && ((unsigned char) c & ~0x7f) == 0);
99 int
100 gfc_wide_is_printable (gfc_char_t c)
102 return (gfc_wide_fits_in_byte (c) && ISPRINT ((unsigned char) c));
105 gfc_char_t
106 gfc_wide_tolower (gfc_char_t c)
108 return (wide_is_ascii (c) ? (gfc_char_t) TOLOWER((unsigned char) c) : c);
111 gfc_char_t
112 gfc_wide_toupper (gfc_char_t c)
114 return (wide_is_ascii (c) ? (gfc_char_t) TOUPPER((unsigned char) c) : c);
118 gfc_wide_is_digit (gfc_char_t c)
120 return (c >= '0' && c <= '9');
123 static inline int
124 wide_atoi (gfc_char_t *c)
126 #define MAX_DIGITS 20
127 char buf[MAX_DIGITS+1];
128 int i = 0;
130 while (gfc_wide_is_digit(*c) && i < MAX_DIGITS)
131 buf[i++] = *c++;
132 buf[i] = '\0';
133 return atoi (buf);
136 size_t
137 gfc_wide_strlen (const gfc_char_t *str)
139 size_t i;
141 for (i = 0; str[i]; i++)
144 return i;
147 gfc_char_t *
148 gfc_wide_memset (gfc_char_t *b, gfc_char_t c, size_t len)
150 size_t i;
152 for (i = 0; i < len; i++)
153 b[i] = c;
155 return b;
158 static gfc_char_t *
159 wide_strcpy (gfc_char_t *dest, const gfc_char_t *src)
161 gfc_char_t *d;
163 for (d = dest; (*d = *src) != '\0'; ++src, ++d)
166 return dest;
169 static gfc_char_t *
170 wide_strchr (const gfc_char_t *s, gfc_char_t c)
172 do {
173 if (*s == c)
175 return CONST_CAST(gfc_char_t *, s);
177 } while (*s++);
178 return 0;
181 char *
182 gfc_widechar_to_char (const gfc_char_t *s, int length)
184 size_t len, i;
185 char *res;
187 if (s == NULL)
188 return NULL;
190 /* Passing a negative length is used to indicate that length should be
191 calculated using gfc_wide_strlen(). */
192 len = (length >= 0 ? (size_t) length : gfc_wide_strlen (s));
193 res = XNEWVEC (char, len + 1);
195 for (i = 0; i < len; i++)
197 gcc_assert (gfc_wide_fits_in_byte (s[i]));
198 res[i] = (unsigned char) s[i];
201 res[len] = '\0';
202 return res;
205 gfc_char_t *
206 gfc_char_to_widechar (const char *s)
208 size_t len, i;
209 gfc_char_t *res;
211 if (s == NULL)
212 return NULL;
214 len = strlen (s);
215 res = gfc_get_wide_string (len + 1);
217 for (i = 0; i < len; i++)
218 res[i] = (unsigned char) s[i];
220 res[len] = '\0';
221 return res;
224 static int
225 wide_strncmp (const gfc_char_t *s1, const char *s2, size_t n)
227 gfc_char_t c1, c2;
229 while (n-- > 0)
231 c1 = *s1++;
232 c2 = *s2++;
233 if (c1 != c2)
234 return (c1 > c2 ? 1 : -1);
235 if (c1 == '\0')
236 return 0;
238 return 0;
242 gfc_wide_strncasecmp (const gfc_char_t *s1, const char *s2, size_t n)
244 gfc_char_t c1, c2;
246 while (n-- > 0)
248 c1 = gfc_wide_tolower (*s1++);
249 c2 = TOLOWER (*s2++);
250 if (c1 != c2)
251 return (c1 > c2 ? 1 : -1);
252 if (c1 == '\0')
253 return 0;
255 return 0;
259 /* Main scanner initialization. */
261 void
262 gfc_scanner_init_1 (void)
264 file_head = NULL;
265 line_head = NULL;
266 line_tail = NULL;
268 continue_count = 0;
269 continue_line = 0;
271 end_flag = 0;
275 /* Main scanner destructor. */
277 void
278 gfc_scanner_done_1 (void)
280 gfc_linebuf *lb;
281 gfc_file *f;
283 while(line_head != NULL)
285 lb = line_head->next;
286 free (line_head);
287 line_head = lb;
290 while(file_head != NULL)
292 f = file_head->next;
293 free (file_head->filename);
294 free (file_head);
295 file_head = f;
300 /* Adds path to the list pointed to by list. */
302 static void
303 add_path_to_list (gfc_directorylist **list, const char *path,
304 bool use_for_modules, bool head, bool warn)
306 gfc_directorylist *dir;
307 const char *p;
308 char *q;
309 struct stat st;
310 size_t len;
311 int i;
313 p = path;
314 while (*p == ' ' || *p == '\t') /* someone might do "-I include" */
315 if (*p++ == '\0')
316 return;
318 /* Strip trailing directory separators from the path, as this
319 will confuse Windows systems. */
320 len = strlen (p);
321 q = (char *) alloca (len + 1);
322 memcpy (q, p, len + 1);
323 i = len - 1;
324 while (i >=0 && IS_DIR_SEPARATOR (q[i]))
325 q[i--] = '\0';
327 if (stat (q, &st))
329 if (errno != ENOENT)
330 gfc_warning_now (0, "Include directory %qs: %s", path,
331 xstrerror(errno));
332 else if (warn)
333 gfc_warning_now (OPT_Wmissing_include_dirs,
334 "Nonexistent include directory %qs", path);
335 return;
337 else if (!S_ISDIR (st.st_mode))
339 gfc_warning_now (0, "%qs is not a directory", path);
340 return;
343 if (head || *list == NULL)
345 dir = XCNEW (gfc_directorylist);
346 if (!head)
347 *list = dir;
349 else
351 dir = *list;
352 while (dir->next)
353 dir = dir->next;
355 dir->next = XCNEW (gfc_directorylist);
356 dir = dir->next;
359 dir->next = head ? *list : NULL;
360 if (head)
361 *list = dir;
362 dir->use_for_modules = use_for_modules;
363 dir->path = XCNEWVEC (char, strlen (p) + 2);
364 strcpy (dir->path, p);
365 strcat (dir->path, "/"); /* make '/' last character */
369 void
370 gfc_add_include_path (const char *path, bool use_for_modules, bool file_dir,
371 bool warn)
373 add_path_to_list (&include_dirs, path, use_for_modules, file_dir, warn);
375 /* For '#include "..."' these directories are automatically searched. */
376 if (!file_dir)
377 gfc_cpp_add_include_path (xstrdup(path), true);
381 void
382 gfc_add_intrinsic_modules_path (const char *path)
384 add_path_to_list (&intrinsic_modules_dirs, path, true, false, false);
388 /* Release resources allocated for options. */
390 void
391 gfc_release_include_path (void)
393 gfc_directorylist *p;
395 while (include_dirs != NULL)
397 p = include_dirs;
398 include_dirs = include_dirs->next;
399 free (p->path);
400 free (p);
403 while (intrinsic_modules_dirs != NULL)
405 p = intrinsic_modules_dirs;
406 intrinsic_modules_dirs = intrinsic_modules_dirs->next;
407 free (p->path);
408 free (p);
411 free (gfc_option.module_dir);
415 static FILE *
416 open_included_file (const char *name, gfc_directorylist *list,
417 bool module, bool system)
419 char *fullname;
420 gfc_directorylist *p;
421 FILE *f;
423 for (p = list; p; p = p->next)
425 if (module && !p->use_for_modules)
426 continue;
428 fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1);
429 strcpy (fullname, p->path);
430 strcat (fullname, name);
432 f = gfc_open_file (fullname);
433 if (f != NULL)
435 if (gfc_cpp_makedep ())
436 gfc_cpp_add_dep (fullname, system);
438 return f;
442 return NULL;
446 /* Opens file for reading, searching through the include directories
447 given if necessary. If the include_cwd argument is true, we try
448 to open the file in the current directory first. */
450 FILE *
451 gfc_open_included_file (const char *name, bool include_cwd, bool module)
453 FILE *f = NULL;
455 if (IS_ABSOLUTE_PATH (name) || include_cwd)
457 f = gfc_open_file (name);
458 if (f && gfc_cpp_makedep ())
459 gfc_cpp_add_dep (name, false);
462 if (!f)
463 f = open_included_file (name, include_dirs, module, false);
465 return f;
469 /* Test to see if we're at the end of the main source file. */
472 gfc_at_end (void)
474 return end_flag;
478 /* Test to see if we're at the end of the current file. */
481 gfc_at_eof (void)
483 if (gfc_at_end ())
484 return 1;
486 if (line_head == NULL)
487 return 1; /* Null file */
489 if (gfc_current_locus.lb == NULL)
490 return 1;
492 return 0;
496 /* Test to see if we're at the beginning of a new line. */
499 gfc_at_bol (void)
501 if (gfc_at_eof ())
502 return 1;
504 return (gfc_current_locus.nextc == gfc_current_locus.lb->line);
508 /* Test to see if we're at the end of a line. */
511 gfc_at_eol (void)
513 if (gfc_at_eof ())
514 return 1;
516 return (*gfc_current_locus.nextc == '\0');
519 static void
520 add_file_change (const char *filename, int line)
522 if (file_changes_count == file_changes_allocated)
524 if (file_changes_allocated)
525 file_changes_allocated *= 2;
526 else
527 file_changes_allocated = 16;
528 file_changes = XRESIZEVEC (struct gfc_file_change, file_changes,
529 file_changes_allocated);
531 file_changes[file_changes_count].filename = filename;
532 file_changes[file_changes_count].lb = NULL;
533 file_changes[file_changes_count++].line = line;
536 static void
537 report_file_change (gfc_linebuf *lb)
539 size_t c = file_changes_cur;
540 while (c < file_changes_count
541 && file_changes[c].lb == lb)
543 if (file_changes[c].filename)
544 (*debug_hooks->start_source_file) (file_changes[c].line,
545 file_changes[c].filename);
546 else
547 (*debug_hooks->end_source_file) (file_changes[c].line);
548 ++c;
550 file_changes_cur = c;
553 void
554 gfc_start_source_files (void)
556 /* If the debugger wants the name of the main source file,
557 we give it. */
558 if (debug_hooks->start_end_main_source_file)
559 (*debug_hooks->start_source_file) (0, gfc_source_file);
561 file_changes_cur = 0;
562 report_file_change (gfc_current_locus.lb);
565 void
566 gfc_end_source_files (void)
568 report_file_change (NULL);
570 if (debug_hooks->start_end_main_source_file)
571 (*debug_hooks->end_source_file) (0);
574 /* Advance the current line pointer to the next line. */
576 void
577 gfc_advance_line (void)
579 if (gfc_at_end ())
580 return;
582 if (gfc_current_locus.lb == NULL)
584 end_flag = 1;
585 return;
588 if (gfc_current_locus.lb->next
589 && !gfc_current_locus.lb->next->dbg_emitted)
591 report_file_change (gfc_current_locus.lb->next);
592 gfc_current_locus.lb->next->dbg_emitted = true;
595 gfc_current_locus.lb = gfc_current_locus.lb->next;
597 if (gfc_current_locus.lb != NULL)
598 gfc_current_locus.nextc = gfc_current_locus.lb->line;
599 else
601 gfc_current_locus.nextc = NULL;
602 end_flag = 1;
607 /* Get the next character from the input, advancing gfc_current_file's
608 locus. When we hit the end of the line or the end of the file, we
609 start returning a '\n' in order to complete the current statement.
610 No Fortran line conventions are implemented here.
612 Requiring explicit advances to the next line prevents the parse
613 pointer from being on the wrong line if the current statement ends
614 prematurely. */
616 static gfc_char_t
617 next_char (void)
619 gfc_char_t c;
621 if (gfc_current_locus.nextc == NULL)
622 return '\n';
624 c = *gfc_current_locus.nextc++;
625 if (c == '\0')
627 gfc_current_locus.nextc--; /* Remain on this line. */
628 c = '\n';
631 return c;
635 /* Skip a comment. When we come here the parse pointer is positioned
636 immediately after the comment character. If we ever implement
637 compiler directives within comments, here is where we parse the
638 directive. */
640 static void
641 skip_comment_line (void)
643 gfc_char_t c;
647 c = next_char ();
649 while (c != '\n');
651 gfc_advance_line ();
656 gfc_define_undef_line (void)
658 char *tmp;
660 /* All lines beginning with '#' are either #define or #undef. */
661 if (debug_info_level != DINFO_LEVEL_VERBOSE || gfc_peek_ascii_char () != '#')
662 return 0;
664 if (wide_strncmp (gfc_current_locus.nextc, "#define ", 8) == 0)
666 tmp = gfc_widechar_to_char (&gfc_current_locus.nextc[8], -1);
667 (*debug_hooks->define) (gfc_linebuf_linenum (gfc_current_locus.lb),
668 tmp);
669 free (tmp);
672 if (wide_strncmp (gfc_current_locus.nextc, "#undef ", 7) == 0)
674 tmp = gfc_widechar_to_char (&gfc_current_locus.nextc[7], -1);
675 (*debug_hooks->undef) (gfc_linebuf_linenum (gfc_current_locus.lb),
676 tmp);
677 free (tmp);
680 /* Skip the rest of the line. */
681 skip_comment_line ();
683 return 1;
687 /* Return true if GCC$ was matched. */
688 static bool
689 skip_gcc_attribute (locus start)
691 bool r = false;
692 char c;
693 locus old_loc = gfc_current_locus;
695 if ((c = next_char ()) == 'g' || c == 'G')
696 if ((c = next_char ()) == 'c' || c == 'C')
697 if ((c = next_char ()) == 'c' || c == 'C')
698 if ((c = next_char ()) == '$')
699 r = true;
701 if (r == false)
702 gfc_current_locus = old_loc;
703 else
705 gcc_attribute_flag = 1;
706 gcc_attribute_locus = old_loc;
707 gfc_current_locus = start;
710 return r;
713 /* Return true if CC was matched. */
714 static bool
715 skip_oacc_attribute (locus start, locus old_loc, bool continue_flag)
717 bool r = false;
718 char c;
720 if ((c = next_char ()) == 'c' || c == 'C')
721 if ((c = next_char ()) == 'c' || c == 'C')
722 r = true;
724 if (r)
726 if ((c = next_char ()) == ' ' || c == '\t'
727 || continue_flag)
729 while (gfc_is_whitespace (c))
730 c = next_char ();
731 if (c != '\n' && c != '!')
733 openacc_flag = 1;
734 openacc_locus = old_loc;
735 gfc_current_locus = start;
737 else
738 r = false;
740 else
742 gfc_warning_now (0, "!$ACC at %C starts a commented "
743 "line as it neither is followed "
744 "by a space nor is a "
745 "continuation line");
746 r = false;
750 return r;
753 /* Return true if MP was matched. */
754 static bool
755 skip_omp_attribute (locus start, locus old_loc, bool continue_flag)
757 bool r = false;
758 char c;
760 if ((c = next_char ()) == 'm' || c == 'M')
761 if ((c = next_char ()) == 'p' || c == 'P')
762 r = true;
764 if (r)
766 if ((c = next_char ()) == ' ' || c == '\t'
767 || continue_flag)
769 while (gfc_is_whitespace (c))
770 c = next_char ();
771 if (c != '\n' && c != '!')
773 openmp_flag = 1;
774 openmp_locus = old_loc;
775 gfc_current_locus = start;
777 else
778 r = false;
780 else
782 gfc_warning_now (0, "!$OMP at %C starts a commented "
783 "line as it neither is followed "
784 "by a space nor is a "
785 "continuation line");
786 r = false;
790 return r;
793 /* Comment lines are null lines, lines containing only blanks or lines
794 on which the first nonblank line is a '!'.
795 Return true if !$ openmp or openacc conditional compilation sentinel was
796 seen. */
798 static bool
799 skip_free_comments (void)
801 locus start;
802 gfc_char_t c;
803 int at_bol;
805 for (;;)
807 at_bol = gfc_at_bol ();
808 start = gfc_current_locus;
809 if (gfc_at_eof ())
810 break;
813 c = next_char ();
814 while (gfc_is_whitespace (c));
816 if (c == '\n')
818 gfc_advance_line ();
819 continue;
822 if (c == '!')
824 /* Keep the !GCC$ line. */
825 if (at_bol && skip_gcc_attribute (start))
826 return false;
828 /* If -fopenmp/-fopenacc, we need to handle here 2 things:
829 1) don't treat !$omp/!$acc as comments, but directives
830 2) handle OpenMP/OpenACC conditional compilation, where
831 !$ should be treated as 2 spaces (for initial lines
832 only if followed by space). */
833 if (at_bol)
835 if ((flag_openmp || flag_openmp_simd)
836 && flag_openacc)
838 locus old_loc = gfc_current_locus;
839 if (next_char () == '$')
841 c = next_char ();
842 if (c == 'o' || c == 'O')
844 if (skip_omp_attribute (start, old_loc, continue_flag))
845 return false;
846 gfc_current_locus = old_loc;
847 next_char ();
848 c = next_char ();
850 else if (c == 'a' || c == 'A')
852 if (skip_oacc_attribute (start, old_loc, continue_flag))
853 return false;
854 gfc_current_locus = old_loc;
855 next_char ();
856 c = next_char ();
858 if (continue_flag || c == ' ' || c == '\t')
860 gfc_current_locus = old_loc;
861 next_char ();
862 openmp_flag = openacc_flag = 0;
863 return true;
866 gfc_current_locus = old_loc;
868 else if ((flag_openmp || flag_openmp_simd)
869 && !flag_openacc)
871 locus old_loc = gfc_current_locus;
872 if (next_char () == '$')
874 c = next_char ();
875 if (c == 'o' || c == 'O')
877 if (skip_omp_attribute (start, old_loc, continue_flag))
878 return false;
879 gfc_current_locus = old_loc;
880 next_char ();
881 c = next_char ();
883 if (continue_flag || c == ' ' || c == '\t')
885 gfc_current_locus = old_loc;
886 next_char ();
887 openmp_flag = 0;
888 return true;
891 gfc_current_locus = old_loc;
893 else if (flag_openacc
894 && !(flag_openmp || flag_openmp_simd))
896 locus old_loc = gfc_current_locus;
897 if (next_char () == '$')
899 c = next_char ();
900 if (c == 'a' || c == 'A')
902 if (skip_oacc_attribute (start, old_loc,
903 continue_flag))
904 return false;
905 gfc_current_locus = old_loc;
906 next_char();
907 c = next_char();
909 if (continue_flag || c == ' ' || c == '\t')
911 gfc_current_locus = old_loc;
912 next_char();
913 openacc_flag = 0;
914 return true;
917 gfc_current_locus = old_loc;
920 skip_comment_line ();
921 continue;
924 break;
927 if (openmp_flag && at_bol)
928 openmp_flag = 0;
930 if (openacc_flag && at_bol)
931 openacc_flag = 0;
933 gcc_attribute_flag = 0;
934 gfc_current_locus = start;
935 return false;
939 /* Skip comment lines in fixed source mode. We have the same rules as
940 in skip_free_comment(), except that we can have a 'c', 'C' or '*'
941 in column 1, and a '!' cannot be in column 6. Also, we deal with
942 lines with 'd' or 'D' in column 1, if the user requested this. */
944 static void
945 skip_fixed_comments (void)
947 locus start;
948 int col;
949 gfc_char_t c;
951 if (! gfc_at_bol ())
953 start = gfc_current_locus;
954 if (! gfc_at_eof ())
957 c = next_char ();
958 while (gfc_is_whitespace (c));
960 if (c == '\n')
961 gfc_advance_line ();
962 else if (c == '!')
963 skip_comment_line ();
966 if (! gfc_at_bol ())
968 gfc_current_locus = start;
969 return;
973 for (;;)
975 start = gfc_current_locus;
976 if (gfc_at_eof ())
977 break;
979 c = next_char ();
980 if (c == '\n')
982 gfc_advance_line ();
983 continue;
986 if (c == '!' || c == 'c' || c == 'C' || c == '*')
988 if (skip_gcc_attribute (start))
990 /* Canonicalize to *$omp. */
991 *start.nextc = '*';
992 return;
995 /* If -fopenmp/-fopenacc, we need to handle here 2 things:
996 1) don't treat !$omp/!$acc|c$omp/c$acc|*$omp / *$acc as comments,
997 but directives
998 2) handle OpenMP/OpenACC conditional compilation, where
999 !$|c$|*$ should be treated as 2 spaces if the characters
1000 in columns 3 to 6 are valid fixed form label columns
1001 characters. */
1002 if (gfc_current_locus.lb != NULL
1003 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
1004 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
1006 if (flag_openmp || flag_openmp_simd)
1008 if (next_char () == '$')
1010 c = next_char ();
1011 if (c == 'o' || c == 'O')
1013 if (((c = next_char ()) == 'm' || c == 'M')
1014 && ((c = next_char ()) == 'p' || c == 'P'))
1016 c = next_char ();
1017 if (c != '\n'
1018 && ((openmp_flag && continue_flag)
1019 || c == ' ' || c == '\t' || c == '0'))
1022 c = next_char ();
1023 while (gfc_is_whitespace (c));
1024 if (c != '\n' && c != '!')
1026 /* Canonicalize to *$omp. */
1027 *start.nextc = '*';
1028 openmp_flag = 1;
1029 gfc_current_locus = start;
1030 return;
1035 else
1037 int digit_seen = 0;
1039 for (col = 3; col < 6; col++, c = next_char ())
1040 if (c == ' ')
1041 continue;
1042 else if (c == '\t')
1044 col = 6;
1045 break;
1047 else if (c < '0' || c > '9')
1048 break;
1049 else
1050 digit_seen = 1;
1052 if (col == 6 && c != '\n'
1053 && ((continue_flag && !digit_seen)
1054 || c == ' ' || c == '\t' || c == '0'))
1056 gfc_current_locus = start;
1057 start.nextc[0] = ' ';
1058 start.nextc[1] = ' ';
1059 continue;
1063 gfc_current_locus = start;
1066 if (flag_openacc)
1068 if (next_char () == '$')
1070 c = next_char ();
1071 if (c == 'a' || c == 'A')
1073 if (((c = next_char ()) == 'c' || c == 'C')
1074 && ((c = next_char ()) == 'c' || c == 'C'))
1076 c = next_char ();
1077 if (c != '\n'
1078 && ((openacc_flag && continue_flag)
1079 || c == ' ' || c == '\t' || c == '0'))
1082 c = next_char ();
1083 while (gfc_is_whitespace (c));
1084 if (c != '\n' && c != '!')
1086 /* Canonicalize to *$acc. */
1087 *start.nextc = '*';
1088 openacc_flag = 1;
1089 gfc_current_locus = start;
1090 return;
1095 else
1097 int digit_seen = 0;
1099 for (col = 3; col < 6; col++, c = next_char ())
1100 if (c == ' ')
1101 continue;
1102 else if (c == '\t')
1104 col = 6;
1105 break;
1107 else if (c < '0' || c > '9')
1108 break;
1109 else
1110 digit_seen = 1;
1112 if (col == 6 && c != '\n'
1113 && ((continue_flag && !digit_seen)
1114 || c == ' ' || c == '\t' || c == '0'))
1116 gfc_current_locus = start;
1117 start.nextc[0] = ' ';
1118 start.nextc[1] = ' ';
1119 continue;
1123 gfc_current_locus = start;
1126 skip_comment_line ();
1127 continue;
1130 if (gfc_option.flag_d_lines != -1 && (c == 'd' || c == 'D'))
1132 if (gfc_option.flag_d_lines == 0)
1134 skip_comment_line ();
1135 continue;
1137 else
1138 *start.nextc = c = ' ';
1141 col = 1;
1143 while (gfc_is_whitespace (c))
1145 c = next_char ();
1146 col++;
1149 if (c == '\n')
1151 gfc_advance_line ();
1152 continue;
1155 if (col != 6 && c == '!')
1157 if (gfc_current_locus.lb != NULL
1158 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
1159 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
1160 skip_comment_line ();
1161 continue;
1164 break;
1167 openmp_flag = 0;
1168 openacc_flag = 0;
1169 gcc_attribute_flag = 0;
1170 gfc_current_locus = start;
1174 /* Skips the current line if it is a comment. */
1176 void
1177 gfc_skip_comments (void)
1179 if (gfc_current_form == FORM_FREE)
1180 skip_free_comments ();
1181 else
1182 skip_fixed_comments ();
1186 /* Get the next character from the input, taking continuation lines
1187 and end-of-line comments into account. This implies that comment
1188 lines between continued lines must be eaten here. For higher-level
1189 subroutines, this flattens continued lines into a single logical
1190 line. The in_string flag denotes whether we're inside a character
1191 context or not. */
1193 gfc_char_t
1194 gfc_next_char_literal (gfc_instring in_string)
1196 locus old_loc;
1197 int i, prev_openmp_flag, prev_openacc_flag;
1198 gfc_char_t c;
1200 continue_flag = 0;
1201 prev_openacc_flag = prev_openmp_flag = 0;
1203 restart:
1204 c = next_char ();
1205 if (gfc_at_end ())
1207 continue_count = 0;
1208 return c;
1211 if (gfc_current_form == FORM_FREE)
1213 bool openmp_cond_flag;
1215 if (!in_string && c == '!')
1217 if (gcc_attribute_flag
1218 && memcmp (&gfc_current_locus, &gcc_attribute_locus,
1219 sizeof (gfc_current_locus)) == 0)
1220 goto done;
1222 if (openmp_flag
1223 && memcmp (&gfc_current_locus, &openmp_locus,
1224 sizeof (gfc_current_locus)) == 0)
1225 goto done;
1227 if (openacc_flag
1228 && memcmp (&gfc_current_locus, &openacc_locus,
1229 sizeof (gfc_current_locus)) == 0)
1230 goto done;
1232 /* This line can't be continued */
1235 c = next_char ();
1237 while (c != '\n');
1239 /* Avoid truncation warnings for comment ending lines. */
1240 gfc_current_locus.lb->truncated = 0;
1242 goto done;
1245 /* Check to see if the continuation line was truncated. */
1246 if (warn_line_truncation && gfc_current_locus.lb != NULL
1247 && gfc_current_locus.lb->truncated)
1249 int maxlen = flag_free_line_length;
1250 gfc_char_t *current_nextc = gfc_current_locus.nextc;
1252 gfc_current_locus.lb->truncated = 0;
1253 gfc_current_locus.nextc = gfc_current_locus.lb->line + maxlen;
1254 gfc_warning_now (OPT_Wline_truncation,
1255 "Line truncated at %L", &gfc_current_locus);
1256 gfc_current_locus.nextc = current_nextc;
1259 if (c != '&')
1260 goto done;
1262 /* If the next nonblank character is a ! or \n, we've got a
1263 continuation line. */
1264 old_loc = gfc_current_locus;
1266 c = next_char ();
1267 while (gfc_is_whitespace (c))
1268 c = next_char ();
1270 /* Character constants to be continued cannot have commentary
1271 after the '&'. However, there are cases where we may think we
1272 are still in a string and we are looking for a possible
1273 doubled quote and we end up here. See PR64506. */
1275 if (in_string)
1277 gfc_current_locus = old_loc;
1279 if (c == '!')
1281 skip_comment_line ();
1282 goto restart;
1285 if (c != '\n')
1287 c = '&';
1288 goto done;
1292 if (c != '!' && c != '\n')
1294 gfc_current_locus = old_loc;
1295 c = '&';
1296 goto done;
1299 if (flag_openmp)
1300 prev_openmp_flag = openmp_flag;
1301 if (flag_openacc)
1302 prev_openacc_flag = openacc_flag;
1304 continue_flag = 1;
1305 if (c == '!')
1306 skip_comment_line ();
1307 else
1308 gfc_advance_line ();
1310 if (gfc_at_eof ())
1311 goto not_continuation;
1313 /* We've got a continuation line. If we are on the very next line after
1314 the last continuation, increment the continuation line count and
1315 check whether the limit has been exceeded. */
1316 if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1)
1318 if (++continue_count == gfc_option.max_continue_free)
1320 if (gfc_notification_std (GFC_STD_GNU) || pedantic)
1321 gfc_warning (0, "Limit of %d continuations exceeded in "
1322 "statement at %C", gfc_option.max_continue_free);
1326 /* Now find where it continues. First eat any comment lines. */
1327 openmp_cond_flag = skip_free_comments ();
1329 if (gfc_current_locus.lb != NULL
1330 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
1331 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
1333 if (flag_openmp)
1334 if (prev_openmp_flag != openmp_flag)
1336 gfc_current_locus = old_loc;
1337 openmp_flag = prev_openmp_flag;
1338 c = '&';
1339 goto done;
1342 if (flag_openacc)
1343 if (prev_openacc_flag != openacc_flag)
1345 gfc_current_locus = old_loc;
1346 openacc_flag = prev_openacc_flag;
1347 c = '&';
1348 goto done;
1351 /* Now that we have a non-comment line, probe ahead for the
1352 first non-whitespace character. If it is another '&', then
1353 reading starts at the next character, otherwise we must back
1354 up to where the whitespace started and resume from there. */
1356 old_loc = gfc_current_locus;
1358 c = next_char ();
1359 while (gfc_is_whitespace (c))
1360 c = next_char ();
1362 if (openmp_flag)
1364 for (i = 0; i < 5; i++, c = next_char ())
1366 gcc_assert (gfc_wide_tolower (c) == (unsigned char) "!$omp"[i]);
1367 if (i == 4)
1368 old_loc = gfc_current_locus;
1370 while (gfc_is_whitespace (c))
1371 c = next_char ();
1373 if (openacc_flag)
1375 for (i = 0; i < 5; i++, c = next_char ())
1377 gcc_assert (gfc_wide_tolower (c) == (unsigned char) "!$acc"[i]);
1378 if (i == 4)
1379 old_loc = gfc_current_locus;
1381 while (gfc_is_whitespace (c))
1382 c = next_char ();
1385 if (c != '&')
1387 if (in_string)
1389 gfc_current_locus.nextc--;
1390 if (warn_ampersand && in_string == INSTRING_WARN)
1391 gfc_warning (OPT_Wampersand,
1392 "Missing %<&%> in continued character "
1393 "constant at %C");
1395 /* Both !$omp and !$ -fopenmp continuation lines have & on the
1396 continuation line only optionally. */
1397 else if (openmp_flag || openacc_flag || openmp_cond_flag)
1398 gfc_current_locus.nextc--;
1399 else
1401 c = ' ';
1402 gfc_current_locus = old_loc;
1403 goto done;
1407 else /* Fixed form. */
1409 /* Fixed form continuation. */
1410 if (in_string != INSTRING_WARN && c == '!')
1412 /* Skip comment at end of line. */
1415 c = next_char ();
1417 while (c != '\n');
1419 /* Avoid truncation warnings for comment ending lines. */
1420 gfc_current_locus.lb->truncated = 0;
1423 if (c != '\n')
1424 goto done;
1426 /* Check to see if the continuation line was truncated. */
1427 if (warn_line_truncation && gfc_current_locus.lb != NULL
1428 && gfc_current_locus.lb->truncated)
1430 gfc_current_locus.lb->truncated = 0;
1431 gfc_warning_now (OPT_Wline_truncation,
1432 "Line truncated at %L", &gfc_current_locus);
1435 if (flag_openmp)
1436 prev_openmp_flag = openmp_flag;
1437 if (flag_openacc)
1438 prev_openacc_flag = openacc_flag;
1440 continue_flag = 1;
1441 old_loc = gfc_current_locus;
1443 gfc_advance_line ();
1444 skip_fixed_comments ();
1446 /* See if this line is a continuation line. */
1447 if (flag_openmp && openmp_flag != prev_openmp_flag)
1449 openmp_flag = prev_openmp_flag;
1450 goto not_continuation;
1452 if (flag_openacc && openacc_flag != prev_openacc_flag)
1454 openacc_flag = prev_openacc_flag;
1455 goto not_continuation;
1458 if (!openmp_flag && !openacc_flag)
1459 for (i = 0; i < 5; i++)
1461 c = next_char ();
1462 if (c != ' ')
1463 goto not_continuation;
1465 else if (openmp_flag)
1466 for (i = 0; i < 5; i++)
1468 c = next_char ();
1469 if (gfc_wide_tolower (c) != (unsigned char) "*$omp"[i])
1470 goto not_continuation;
1472 else if (openacc_flag)
1473 for (i = 0; i < 5; i++)
1475 c = next_char ();
1476 if (gfc_wide_tolower (c) != (unsigned char) "*$acc"[i])
1477 goto not_continuation;
1480 c = next_char ();
1481 if (c == '0' || c == ' ' || c == '\n')
1482 goto not_continuation;
1484 /* We've got a continuation line. If we are on the very next line after
1485 the last continuation, increment the continuation line count and
1486 check whether the limit has been exceeded. */
1487 if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1)
1489 if (++continue_count == gfc_option.max_continue_fixed)
1491 if (gfc_notification_std (GFC_STD_GNU) || pedantic)
1492 gfc_warning (0, "Limit of %d continuations exceeded in "
1493 "statement at %C",
1494 gfc_option.max_continue_fixed);
1498 if (gfc_current_locus.lb != NULL
1499 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
1500 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
1503 /* Ready to read first character of continuation line, which might
1504 be another continuation line! */
1505 goto restart;
1507 not_continuation:
1508 c = '\n';
1509 gfc_current_locus = old_loc;
1511 done:
1512 if (c == '\n')
1513 continue_count = 0;
1514 continue_flag = 0;
1515 return c;
1519 /* Get the next character of input, folded to lowercase. In fixed
1520 form mode, we also ignore spaces. When matcher subroutines are
1521 parsing character literals, they have to call
1522 gfc_next_char_literal(). */
1524 gfc_char_t
1525 gfc_next_char (void)
1527 gfc_char_t c;
1531 c = gfc_next_char_literal (NONSTRING);
1533 while (gfc_current_form == FORM_FIXED && gfc_is_whitespace (c));
1535 return gfc_wide_tolower (c);
1538 char
1539 gfc_next_ascii_char (void)
1541 gfc_char_t c = gfc_next_char ();
1543 return (gfc_wide_fits_in_byte (c) ? (unsigned char) c
1544 : (unsigned char) UCHAR_MAX);
1548 gfc_char_t
1549 gfc_peek_char (void)
1551 locus old_loc;
1552 gfc_char_t c;
1554 old_loc = gfc_current_locus;
1555 c = gfc_next_char ();
1556 gfc_current_locus = old_loc;
1558 return c;
1562 char
1563 gfc_peek_ascii_char (void)
1565 gfc_char_t c = gfc_peek_char ();
1567 return (gfc_wide_fits_in_byte (c) ? (unsigned char) c
1568 : (unsigned char) UCHAR_MAX);
1572 /* Recover from an error. We try to get past the current statement
1573 and get lined up for the next. The next statement follows a '\n'
1574 or a ';'. We also assume that we are not within a character
1575 constant, and deal with finding a '\'' or '"'. */
1577 void
1578 gfc_error_recovery (void)
1580 gfc_char_t c, delim;
1582 if (gfc_at_eof ())
1583 return;
1585 for (;;)
1587 c = gfc_next_char ();
1588 if (c == '\n' || c == ';')
1589 break;
1591 if (c != '\'' && c != '"')
1593 if (gfc_at_eof ())
1594 break;
1595 continue;
1597 delim = c;
1599 for (;;)
1601 c = next_char ();
1603 if (c == delim)
1604 break;
1605 if (c == '\n')
1606 return;
1607 if (c == '\\')
1609 c = next_char ();
1610 if (c == '\n')
1611 return;
1614 if (gfc_at_eof ())
1615 break;
1620 /* Read ahead until the next character to be read is not whitespace. */
1622 void
1623 gfc_gobble_whitespace (void)
1625 static int linenum = 0;
1626 locus old_loc;
1627 gfc_char_t c;
1631 old_loc = gfc_current_locus;
1632 c = gfc_next_char_literal (NONSTRING);
1633 /* Issue a warning for nonconforming tabs. We keep track of the line
1634 number because the Fortran matchers will often back up and the same
1635 line will be scanned multiple times. */
1636 if (warn_tabs && c == '\t')
1638 int cur_linenum = LOCATION_LINE (gfc_current_locus.lb->location);
1639 if (cur_linenum != linenum)
1641 linenum = cur_linenum;
1642 gfc_warning_now (OPT_Wtabs, "Nonconforming tab character at %C");
1646 while (gfc_is_whitespace (c));
1648 gfc_current_locus = old_loc;
1652 /* Load a single line into pbuf.
1654 If pbuf points to a NULL pointer, it is allocated.
1655 We truncate lines that are too long, unless we're dealing with
1656 preprocessor lines or if the option -ffixed-line-length-none is set,
1657 in which case we reallocate the buffer to fit the entire line, if
1658 need be.
1659 In fixed mode, we expand a tab that occurs within the statement
1660 label region to expand to spaces that leave the next character in
1661 the source region.
1663 If first_char is not NULL, it's a pointer to a single char value holding
1664 the first character of the line, which has already been read by the
1665 caller. This avoids the use of ungetc().
1667 load_line returns whether the line was truncated.
1669 NOTE: The error machinery isn't available at this point, so we can't
1670 easily report line and column numbers consistent with other
1671 parts of gfortran. */
1673 static int
1674 load_line (FILE *input, gfc_char_t **pbuf, int *pbuflen, const int *first_char)
1676 static int linenum = 0, current_line = 1;
1677 int c, maxlen, i, preprocessor_flag, buflen = *pbuflen;
1678 int trunc_flag = 0, seen_comment = 0;
1679 int seen_printable = 0, seen_ampersand = 0, quoted = ' ';
1680 gfc_char_t *buffer;
1681 bool found_tab = false;
1683 /* Determine the maximum allowed line length. */
1684 if (gfc_current_form == FORM_FREE)
1685 maxlen = flag_free_line_length;
1686 else if (gfc_current_form == FORM_FIXED)
1687 maxlen = flag_fixed_line_length;
1688 else
1689 maxlen = 72;
1691 if (*pbuf == NULL)
1693 /* Allocate the line buffer, storing its length into buflen.
1694 Note that if maxlen==0, indicating that arbitrary-length lines
1695 are allowed, the buffer will be reallocated if this length is
1696 insufficient; since 132 characters is the length of a standard
1697 free-form line, we use that as a starting guess. */
1698 if (maxlen > 0)
1699 buflen = maxlen;
1700 else
1701 buflen = 132;
1703 *pbuf = gfc_get_wide_string (buflen + 1);
1706 i = 0;
1707 buffer = *pbuf;
1709 if (first_char)
1710 c = *first_char;
1711 else
1712 c = getc (input);
1714 /* In order to not truncate preprocessor lines, we have to
1715 remember that this is one. */
1716 preprocessor_flag = (c == '#' ? 1 : 0);
1718 for (;;)
1720 if (c == EOF)
1721 break;
1723 if (c == '\n')
1725 /* Check for illegal use of ampersand. See F95 Standard 3.3.1.3. */
1726 if (gfc_current_form == FORM_FREE
1727 && !seen_printable && seen_ampersand)
1729 if (pedantic)
1730 gfc_error_now ("%<&%> not allowed by itself in line %d",
1731 current_line);
1732 else
1733 gfc_warning_now (0, "%<&%> not allowed by itself in line %d",
1734 current_line);
1736 break;
1739 if (c == '\r' || c == '\0')
1740 goto next_char; /* Gobble characters. */
1742 if (c == '&')
1744 if (seen_ampersand)
1746 seen_ampersand = 0;
1747 seen_printable = 1;
1749 else
1750 seen_ampersand = 1;
1753 if ((c != '&' && c != '!' && c != ' ') || (c == '!' && !seen_ampersand))
1754 seen_printable = 1;
1756 /* Is this a fixed-form comment? */
1757 if (gfc_current_form == FORM_FIXED && i == 0
1758 && (c == '*' || c == 'c' || c == 'd'))
1759 seen_comment = 1;
1761 if (quoted == ' ')
1763 if (c == '\'' || c == '"')
1764 quoted = c;
1766 else if (c == quoted)
1767 quoted = ' ';
1769 /* Is this a free-form comment? */
1770 if (c == '!' && quoted == ' ')
1771 seen_comment = 1;
1773 /* Vendor extension: "<tab>1" marks a continuation line. */
1774 if (found_tab)
1776 found_tab = false;
1777 if (c >= '1' && c <= '9')
1779 *(buffer-1) = c;
1780 goto next_char;
1784 if (gfc_current_form == FORM_FIXED && c == '\t' && i < 6)
1786 found_tab = true;
1788 if (warn_tabs && seen_comment == 0 && current_line != linenum)
1790 linenum = current_line;
1791 gfc_warning_now (OPT_Wtabs,
1792 "Nonconforming tab character in column %d "
1793 "of line %d", i+1, linenum);
1796 while (i < 6)
1798 *buffer++ = ' ';
1799 i++;
1802 goto next_char;
1805 *buffer++ = c;
1806 i++;
1808 if (maxlen == 0 || preprocessor_flag)
1810 if (i >= buflen)
1812 /* Reallocate line buffer to double size to hold the
1813 overlong line. */
1814 buflen = buflen * 2;
1815 *pbuf = XRESIZEVEC (gfc_char_t, *pbuf, (buflen + 1));
1816 buffer = (*pbuf) + i;
1819 else if (i >= maxlen)
1821 bool trunc_warn = true;
1823 /* Enhancement, if the very next non-space character is an ampersand
1824 or comment that we would otherwise warn about, don't mark as
1825 truncated. */
1827 /* Truncate the rest of the line. */
1828 for (;;)
1830 c = getc (input);
1831 if (c == '\r' || c == ' ')
1832 continue;
1834 if (c == '\n' || c == EOF)
1835 break;
1837 if (!trunc_warn && c != '!')
1838 trunc_warn = true;
1840 if (trunc_warn && ((gfc_current_form == FORM_FIXED && c == '&')
1841 || c == '!'))
1842 trunc_warn = false;
1844 if (c == '!')
1845 seen_comment = 1;
1847 if (trunc_warn && !seen_comment)
1848 trunc_flag = 1;
1851 c = '\n';
1852 continue;
1855 next_char:
1856 c = getc (input);
1859 /* Pad lines to the selected line length in fixed form. */
1860 if (gfc_current_form == FORM_FIXED
1861 && flag_fixed_line_length != 0
1862 && !preprocessor_flag
1863 && c != EOF)
1865 while (i++ < maxlen)
1866 *buffer++ = ' ';
1869 *buffer = '\0';
1870 *pbuflen = buflen;
1871 current_line++;
1873 return trunc_flag;
1877 /* Get a gfc_file structure, initialize it and add it to
1878 the file stack. */
1880 static gfc_file *
1881 get_file (const char *name, enum lc_reason reason ATTRIBUTE_UNUSED)
1883 gfc_file *f;
1885 f = XCNEW (gfc_file);
1887 f->filename = xstrdup (name);
1889 f->next = file_head;
1890 file_head = f;
1892 f->up = current_file;
1893 if (current_file != NULL)
1894 f->inclusion_line = current_file->line;
1896 linemap_add (line_table, reason, false, f->filename, 1);
1898 return f;
1902 /* Deal with a line from the C preprocessor. The
1903 initial octothorp has already been seen. */
1905 static void
1906 preprocessor_line (gfc_char_t *c)
1908 bool flag[5];
1909 int i, line;
1910 gfc_char_t *wide_filename;
1911 gfc_file *f;
1912 int escaped, unescape;
1913 char *filename;
1915 c++;
1916 while (*c == ' ' || *c == '\t')
1917 c++;
1919 if (*c < '0' || *c > '9')
1920 goto bad_cpp_line;
1922 line = wide_atoi (c);
1924 c = wide_strchr (c, ' ');
1925 if (c == NULL)
1927 /* No file name given. Set new line number. */
1928 current_file->line = line;
1929 return;
1932 /* Skip spaces. */
1933 while (*c == ' ' || *c == '\t')
1934 c++;
1936 /* Skip quote. */
1937 if (*c != '"')
1938 goto bad_cpp_line;
1939 ++c;
1941 wide_filename = c;
1943 /* Make filename end at quote. */
1944 unescape = 0;
1945 escaped = false;
1946 while (*c && ! (!escaped && *c == '"'))
1948 if (escaped)
1949 escaped = false;
1950 else if (*c == '\\')
1952 escaped = true;
1953 unescape++;
1955 ++c;
1958 if (! *c)
1959 /* Preprocessor line has no closing quote. */
1960 goto bad_cpp_line;
1962 *c++ = '\0';
1964 /* Undo effects of cpp_quote_string. */
1965 if (unescape)
1967 gfc_char_t *s = wide_filename;
1968 gfc_char_t *d = gfc_get_wide_string (c - wide_filename - unescape);
1970 wide_filename = d;
1971 while (*s)
1973 if (*s == '\\')
1974 *d++ = *++s;
1975 else
1976 *d++ = *s;
1977 s++;
1979 *d = '\0';
1982 /* Get flags. */
1984 flag[1] = flag[2] = flag[3] = flag[4] = false;
1986 for (;;)
1988 c = wide_strchr (c, ' ');
1989 if (c == NULL)
1990 break;
1992 c++;
1993 i = wide_atoi (c);
1995 if (1 <= i && i <= 4)
1996 flag[i] = true;
1999 /* Convert the filename in wide characters into a filename in narrow
2000 characters. */
2001 filename = gfc_widechar_to_char (wide_filename, -1);
2003 /* Interpret flags. */
2005 if (flag[1]) /* Starting new file. */
2007 f = get_file (filename, LC_RENAME);
2008 add_file_change (f->filename, f->inclusion_line);
2009 current_file = f;
2012 if (flag[2]) /* Ending current file. */
2014 if (!current_file->up
2015 || filename_cmp (current_file->up->filename, filename) != 0)
2017 gfc_warning_now_1 ("%s:%d: file %s left but not entered",
2018 current_file->filename, current_file->line,
2019 filename);
2020 if (unescape)
2021 free (wide_filename);
2022 free (filename);
2023 return;
2026 add_file_change (NULL, line);
2027 current_file = current_file->up;
2028 linemap_add (line_table, LC_RENAME, false, current_file->filename,
2029 current_file->line);
2032 /* The name of the file can be a temporary file produced by
2033 cpp. Replace the name if it is different. */
2035 if (filename_cmp (current_file->filename, filename) != 0)
2037 /* FIXME: we leak the old filename because a pointer to it may be stored
2038 in the linemap. Alternative could be using GC or updating linemap to
2039 point to the new name, but there is no API for that currently. */
2040 current_file->filename = xstrdup (filename);
2043 /* Set new line number. */
2044 current_file->line = line;
2045 if (unescape)
2046 free (wide_filename);
2047 free (filename);
2048 return;
2050 bad_cpp_line:
2051 gfc_warning_now_1 ("%s:%d: Illegal preprocessor directive",
2052 current_file->filename, current_file->line);
2053 current_file->line++;
2057 static bool load_file (const char *, const char *, bool);
2059 /* include_line()-- Checks a line buffer to see if it is an include
2060 line. If so, we call load_file() recursively to load the included
2061 file. We never return a syntax error because a statement like
2062 "include = 5" is perfectly legal. We return false if no include was
2063 processed or true if we matched an include. */
2065 static bool
2066 include_line (gfc_char_t *line)
2068 gfc_char_t quote, *c, *begin, *stop;
2069 char *filename;
2071 c = line;
2073 if (flag_openmp || flag_openmp_simd)
2075 if (gfc_current_form == FORM_FREE)
2077 while (*c == ' ' || *c == '\t')
2078 c++;
2079 if (*c == '!' && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
2080 c += 3;
2082 else
2084 if ((*c == '!' || *c == 'c' || *c == 'C' || *c == '*')
2085 && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
2086 c += 3;
2090 while (*c == ' ' || *c == '\t')
2091 c++;
2093 if (gfc_wide_strncasecmp (c, "include", 7))
2094 return false;
2096 c += 7;
2097 while (*c == ' ' || *c == '\t')
2098 c++;
2100 /* Find filename between quotes. */
2102 quote = *c++;
2103 if (quote != '"' && quote != '\'')
2104 return false;
2106 begin = c;
2108 while (*c != quote && *c != '\0')
2109 c++;
2111 if (*c == '\0')
2112 return false;
2114 stop = c++;
2116 while (*c == ' ' || *c == '\t')
2117 c++;
2119 if (*c != '\0' && *c != '!')
2120 return false;
2122 /* We have an include line at this point. */
2124 *stop = '\0'; /* It's ok to trash the buffer, as this line won't be
2125 read by anything else. */
2127 filename = gfc_widechar_to_char (begin, -1);
2128 if (!load_file (filename, NULL, false))
2129 exit (FATAL_EXIT_CODE);
2131 free (filename);
2132 return true;
2136 /* Load a file into memory by calling load_line until the file ends. */
2138 static bool
2139 load_file (const char *realfilename, const char *displayedname, bool initial)
2141 gfc_char_t *line;
2142 gfc_linebuf *b;
2143 gfc_file *f;
2144 FILE *input;
2145 int len, line_len;
2146 bool first_line;
2147 const char *filename;
2148 /* If realfilename and displayedname are different and non-null then
2149 surely realfilename is the preprocessed form of
2150 displayedname. */
2151 bool preprocessed_p = (realfilename && displayedname
2152 && strcmp (realfilename, displayedname));
2154 filename = displayedname ? displayedname : realfilename;
2156 for (f = current_file; f; f = f->up)
2157 if (filename_cmp (filename, f->filename) == 0)
2159 fprintf (stderr, "%s:%d: Error: File '%s' is being included "
2160 "recursively\n", current_file->filename, current_file->line,
2161 filename);
2162 return false;
2165 if (initial)
2167 if (gfc_src_file)
2169 input = gfc_src_file;
2170 gfc_src_file = NULL;
2172 else
2173 input = gfc_open_file (realfilename);
2174 if (input == NULL)
2176 gfc_error_now ("Can't open file %qs", filename);
2177 return false;
2180 else
2182 input = gfc_open_included_file (realfilename, false, false);
2183 if (input == NULL)
2185 fprintf (stderr, "%s:%d: Error: Can't open included file '%s'\n",
2186 current_file->filename, current_file->line, filename);
2187 return false;
2191 /* Load the file.
2193 A "non-initial" file means a file that is being included. In
2194 that case we are creating an LC_ENTER map.
2196 An "initial" file means a main file; one that is not included.
2197 That file has already got at least one (surely more) line map(s)
2198 created by gfc_init. So the subsequent map created in that case
2199 must have LC_RENAME reason.
2201 This latter case is not true for a preprocessed file. In that
2202 case, although the file is "initial", the line maps created by
2203 gfc_init was used during the preprocessing of the file. Now that
2204 the preprocessing is over and we are being fed the result of that
2205 preprocessing, we need to create a brand new line map for the
2206 preprocessed file, so the reason is going to be LC_ENTER. */
2208 f = get_file (filename, (initial && !preprocessed_p) ? LC_RENAME : LC_ENTER);
2209 if (!initial)
2210 add_file_change (f->filename, f->inclusion_line);
2211 current_file = f;
2212 current_file->line = 1;
2213 line = NULL;
2214 line_len = 0;
2215 first_line = true;
2217 if (initial && gfc_src_preprocessor_lines[0])
2219 preprocessor_line (gfc_src_preprocessor_lines[0]);
2220 free (gfc_src_preprocessor_lines[0]);
2221 gfc_src_preprocessor_lines[0] = NULL;
2222 if (gfc_src_preprocessor_lines[1])
2224 preprocessor_line (gfc_src_preprocessor_lines[1]);
2225 free (gfc_src_preprocessor_lines[1]);
2226 gfc_src_preprocessor_lines[1] = NULL;
2230 for (;;)
2232 int trunc = load_line (input, &line, &line_len, NULL);
2234 len = gfc_wide_strlen (line);
2235 if (feof (input) && len == 0)
2236 break;
2238 /* If this is the first line of the file, it can contain a byte
2239 order mark (BOM), which we will ignore:
2240 FF FE is UTF-16 little endian,
2241 FE FF is UTF-16 big endian,
2242 EF BB BF is UTF-8. */
2243 if (first_line
2244 && ((line_len >= 2 && line[0] == (unsigned char) '\xFF'
2245 && line[1] == (unsigned char) '\xFE')
2246 || (line_len >= 2 && line[0] == (unsigned char) '\xFE'
2247 && line[1] == (unsigned char) '\xFF')
2248 || (line_len >= 3 && line[0] == (unsigned char) '\xEF'
2249 && line[1] == (unsigned char) '\xBB'
2250 && line[2] == (unsigned char) '\xBF')))
2252 int n = line[1] == (unsigned char) '\xBB' ? 3 : 2;
2253 gfc_char_t *new_char = gfc_get_wide_string (line_len);
2255 wide_strcpy (new_char, &line[n]);
2256 free (line);
2257 line = new_char;
2258 len -= n;
2261 /* There are three things this line can be: a line of Fortran
2262 source, an include line or a C preprocessor directive. */
2264 if (line[0] == '#')
2266 /* When -g3 is specified, it's possible that we emit #define
2267 and #undef lines, which we need to pass to the middle-end
2268 so that it can emit correct debug info. */
2269 if (debug_info_level == DINFO_LEVEL_VERBOSE
2270 && (wide_strncmp (line, "#define ", 8) == 0
2271 || wide_strncmp (line, "#undef ", 7) == 0))
2273 else
2275 preprocessor_line (line);
2276 continue;
2280 /* Preprocessed files have preprocessor lines added before the byte
2281 order mark, so first_line is not about the first line of the file
2282 but the first line that's not a preprocessor line. */
2283 first_line = false;
2285 if (include_line (line))
2287 current_file->line++;
2288 continue;
2291 /* Add line. */
2293 b = XCNEWVAR (gfc_linebuf, gfc_linebuf_header_size
2294 + (len + 1) * sizeof (gfc_char_t));
2297 b->location
2298 = linemap_line_start (line_table, current_file->line++, len);
2299 /* ??? We add the location for the maximum column possible here,
2300 because otherwise if the next call creates a new line-map, it
2301 will not reserve space for any offset. */
2302 if (len > 0)
2303 linemap_position_for_column (line_table, len);
2305 b->file = current_file;
2306 b->truncated = trunc;
2307 wide_strcpy (b->line, line);
2309 if (line_head == NULL)
2310 line_head = b;
2311 else
2312 line_tail->next = b;
2314 line_tail = b;
2316 while (file_changes_cur < file_changes_count)
2317 file_changes[file_changes_cur++].lb = b;
2320 /* Release the line buffer allocated in load_line. */
2321 free (line);
2323 fclose (input);
2325 if (!initial)
2326 add_file_change (NULL, current_file->inclusion_line + 1);
2327 current_file = current_file->up;
2328 linemap_add (line_table, LC_LEAVE, 0, NULL, 0);
2329 return true;
2333 /* Open a new file and start scanning from that file. Returns true
2334 if everything went OK, false otherwise. If form == FORM_UNKNOWN
2335 it tries to determine the source form from the filename, defaulting
2336 to free form. */
2338 bool
2339 gfc_new_file (void)
2341 bool result;
2343 if (gfc_cpp_enabled ())
2345 result = gfc_cpp_preprocess (gfc_source_file);
2346 if (!gfc_cpp_preprocess_only ())
2347 result = load_file (gfc_cpp_temporary_file (), gfc_source_file, true);
2349 else
2350 result = load_file (gfc_source_file, NULL, true);
2352 gfc_current_locus.lb = line_head;
2353 gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line;
2355 #if 0 /* Debugging aid. */
2356 for (; line_head; line_head = line_head->next)
2357 printf ("%s:%3d %s\n", LOCATION_FILE (line_head->location),
2358 LOCATION_LINE (line_head->location), line_head->line);
2360 exit (SUCCESS_EXIT_CODE);
2361 #endif
2363 return result;
2366 static char *
2367 unescape_filename (const char *ptr)
2369 const char *p = ptr, *s;
2370 char *d, *ret;
2371 int escaped, unescape = 0;
2373 /* Make filename end at quote. */
2374 escaped = false;
2375 while (*p && ! (! escaped && *p == '"'))
2377 if (escaped)
2378 escaped = false;
2379 else if (*p == '\\')
2381 escaped = true;
2382 unescape++;
2384 ++p;
2387 if (!*p || p[1])
2388 return NULL;
2390 /* Undo effects of cpp_quote_string. */
2391 s = ptr;
2392 d = XCNEWVEC (char, p + 1 - ptr - unescape);
2393 ret = d;
2395 while (s != p)
2397 if (*s == '\\')
2398 *d++ = *++s;
2399 else
2400 *d++ = *s;
2401 s++;
2403 *d = '\0';
2404 return ret;
2407 /* For preprocessed files, if the first tokens are of the form # NUM.
2408 handle the directives so we know the original file name. */
2410 const char *
2411 gfc_read_orig_filename (const char *filename, const char **canon_source_file)
2413 int c, len;
2414 char *dirname, *tmp;
2416 gfc_src_file = gfc_open_file (filename);
2417 if (gfc_src_file == NULL)
2418 return NULL;
2420 c = getc (gfc_src_file);
2422 if (c != '#')
2423 return NULL;
2425 len = 0;
2426 load_line (gfc_src_file, &gfc_src_preprocessor_lines[0], &len, &c);
2428 if (wide_strncmp (gfc_src_preprocessor_lines[0], "# 1 \"", 5) != 0)
2429 return NULL;
2431 tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[0][5], -1);
2432 filename = unescape_filename (tmp);
2433 free (tmp);
2434 if (filename == NULL)
2435 return NULL;
2437 c = getc (gfc_src_file);
2439 if (c != '#')
2440 return filename;
2442 len = 0;
2443 load_line (gfc_src_file, &gfc_src_preprocessor_lines[1], &len, &c);
2445 if (wide_strncmp (gfc_src_preprocessor_lines[1], "# 1 \"", 5) != 0)
2446 return filename;
2448 tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[1][5], -1);
2449 dirname = unescape_filename (tmp);
2450 free (tmp);
2451 if (dirname == NULL)
2452 return filename;
2454 len = strlen (dirname);
2455 if (len < 3 || dirname[len - 1] != '/' || dirname[len - 2] != '/')
2457 free (dirname);
2458 return filename;
2460 dirname[len - 2] = '\0';
2461 set_src_pwd (dirname);
2463 if (! IS_ABSOLUTE_PATH (filename))
2465 char *p = XCNEWVEC (char, len + strlen (filename));
2467 memcpy (p, dirname, len - 2);
2468 p[len - 2] = '/';
2469 strcpy (p + len - 1, filename);
2470 *canon_source_file = p;
2473 free (dirname);
2474 return filename;