gcc/testsuite/ChangeLog:
[official-gcc.git] / gcc / fortran / scanner.c
blob0de09983c23349164122c1fa6f10f2c7b28cfd7d
1 /* Character scanner.
2 Copyright (C) 2000-2014 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, openmp_flag, gcc_attribute_flag;
59 static int continue_count, continue_line;
60 static locus openmp_locus;
61 static locus gcc_attribute_locus;
63 gfc_source_form gfc_current_form;
64 static gfc_linebuf *line_head, *line_tail;
66 locus gfc_current_locus;
67 const char *gfc_source_file;
68 static FILE *gfc_src_file;
69 static gfc_char_t *gfc_src_preprocessor_lines[2];
71 static struct gfc_file_change
73 const char *filename;
74 gfc_linebuf *lb;
75 int line;
76 } *file_changes;
77 size_t file_changes_cur, file_changes_count;
78 size_t file_changes_allocated;
81 /* Functions dealing with our wide characters (gfc_char_t) and
82 sequences of such characters. */
84 int
85 gfc_wide_fits_in_byte (gfc_char_t c)
87 return (c <= UCHAR_MAX);
90 static inline int
91 wide_is_ascii (gfc_char_t c)
93 return (gfc_wide_fits_in_byte (c) && ((unsigned char) c & ~0x7f) == 0);
96 int
97 gfc_wide_is_printable (gfc_char_t c)
99 return (gfc_wide_fits_in_byte (c) && ISPRINT ((unsigned char) c));
102 gfc_char_t
103 gfc_wide_tolower (gfc_char_t c)
105 return (wide_is_ascii (c) ? (gfc_char_t) TOLOWER((unsigned char) c) : c);
108 gfc_char_t
109 gfc_wide_toupper (gfc_char_t c)
111 return (wide_is_ascii (c) ? (gfc_char_t) TOUPPER((unsigned char) c) : c);
115 gfc_wide_is_digit (gfc_char_t c)
117 return (c >= '0' && c <= '9');
120 static inline int
121 wide_atoi (gfc_char_t *c)
123 #define MAX_DIGITS 20
124 char buf[MAX_DIGITS+1];
125 int i = 0;
127 while (gfc_wide_is_digit(*c) && i < MAX_DIGITS)
128 buf[i++] = *c++;
129 buf[i] = '\0';
130 return atoi (buf);
133 size_t
134 gfc_wide_strlen (const gfc_char_t *str)
136 size_t i;
138 for (i = 0; str[i]; i++)
141 return i;
144 gfc_char_t *
145 gfc_wide_memset (gfc_char_t *b, gfc_char_t c, size_t len)
147 size_t i;
149 for (i = 0; i < len; i++)
150 b[i] = c;
152 return b;
155 static gfc_char_t *
156 wide_strcpy (gfc_char_t *dest, const gfc_char_t *src)
158 gfc_char_t *d;
160 for (d = dest; (*d = *src) != '\0'; ++src, ++d)
163 return dest;
166 static gfc_char_t *
167 wide_strchr (const gfc_char_t *s, gfc_char_t c)
169 do {
170 if (*s == c)
172 return CONST_CAST(gfc_char_t *, s);
174 } while (*s++);
175 return 0;
178 char *
179 gfc_widechar_to_char (const gfc_char_t *s, int length)
181 size_t len, i;
182 char *res;
184 if (s == NULL)
185 return NULL;
187 /* Passing a negative length is used to indicate that length should be
188 calculated using gfc_wide_strlen(). */
189 len = (length >= 0 ? (size_t) length : gfc_wide_strlen (s));
190 res = XNEWVEC (char, len + 1);
192 for (i = 0; i < len; i++)
194 gcc_assert (gfc_wide_fits_in_byte (s[i]));
195 res[i] = (unsigned char) s[i];
198 res[len] = '\0';
199 return res;
202 gfc_char_t *
203 gfc_char_to_widechar (const char *s)
205 size_t len, i;
206 gfc_char_t *res;
208 if (s == NULL)
209 return NULL;
211 len = strlen (s);
212 res = gfc_get_wide_string (len + 1);
214 for (i = 0; i < len; i++)
215 res[i] = (unsigned char) s[i];
217 res[len] = '\0';
218 return res;
221 static int
222 wide_strncmp (const gfc_char_t *s1, const char *s2, size_t n)
224 gfc_char_t c1, c2;
226 while (n-- > 0)
228 c1 = *s1++;
229 c2 = *s2++;
230 if (c1 != c2)
231 return (c1 > c2 ? 1 : -1);
232 if (c1 == '\0')
233 return 0;
235 return 0;
239 gfc_wide_strncasecmp (const gfc_char_t *s1, const char *s2, size_t n)
241 gfc_char_t c1, c2;
243 while (n-- > 0)
245 c1 = gfc_wide_tolower (*s1++);
246 c2 = TOLOWER (*s2++);
247 if (c1 != c2)
248 return (c1 > c2 ? 1 : -1);
249 if (c1 == '\0')
250 return 0;
252 return 0;
256 /* Main scanner initialization. */
258 void
259 gfc_scanner_init_1 (void)
261 file_head = NULL;
262 line_head = NULL;
263 line_tail = NULL;
265 continue_count = 0;
266 continue_line = 0;
268 end_flag = 0;
272 /* Main scanner destructor. */
274 void
275 gfc_scanner_done_1 (void)
277 gfc_linebuf *lb;
278 gfc_file *f;
280 while(line_head != NULL)
282 lb = line_head->next;
283 free (line_head);
284 line_head = lb;
287 while(file_head != NULL)
289 f = file_head->next;
290 free (file_head->filename);
291 free (file_head);
292 file_head = f;
297 /* Adds path to the list pointed to by list. */
299 static void
300 add_path_to_list (gfc_directorylist **list, const char *path,
301 bool use_for_modules, bool head, bool warn)
303 gfc_directorylist *dir;
304 const char *p;
305 char *q;
306 struct stat st;
307 size_t len;
308 int i;
310 p = path;
311 while (*p == ' ' || *p == '\t') /* someone might do "-I include" */
312 if (*p++ == '\0')
313 return;
315 /* Strip trailing directory separators from the path, as this
316 will confuse Windows systems. */
317 len = strlen (p);
318 q = (char *) alloca (len + 1);
319 memcpy (q, p, len + 1);
320 i = len - 1;
321 while (i >=0 && IS_DIR_SEPARATOR (q[i]))
322 q[i--] = '\0';
324 if (stat (q, &st))
326 if (errno != ENOENT)
327 gfc_warning_now ("Include directory %qs: %s", path,
328 xstrerror(errno));
329 else if (warn)
330 gfc_warning_now (OPT_Wmissing_include_dirs,
331 "Nonexistent include directory %qs", path);
332 return;
334 else if (!S_ISDIR (st.st_mode))
336 gfc_warning_now ("%qs is not a directory", path);
337 return;
340 if (head || *list == NULL)
342 dir = XCNEW (gfc_directorylist);
343 if (!head)
344 *list = dir;
346 else
348 dir = *list;
349 while (dir->next)
350 dir = dir->next;
352 dir->next = XCNEW (gfc_directorylist);
353 dir = dir->next;
356 dir->next = head ? *list : NULL;
357 if (head)
358 *list = dir;
359 dir->use_for_modules = use_for_modules;
360 dir->path = XCNEWVEC (char, strlen (p) + 2);
361 strcpy (dir->path, p);
362 strcat (dir->path, "/"); /* make '/' last character */
366 void
367 gfc_add_include_path (const char *path, bool use_for_modules, bool file_dir,
368 bool warn)
370 add_path_to_list (&include_dirs, path, use_for_modules, file_dir, warn);
372 /* For '#include "..."' these directories are automatically searched. */
373 if (!file_dir)
374 gfc_cpp_add_include_path (xstrdup(path), true);
378 void
379 gfc_add_intrinsic_modules_path (const char *path)
381 add_path_to_list (&intrinsic_modules_dirs, path, true, false, false);
385 /* Release resources allocated for options. */
387 void
388 gfc_release_include_path (void)
390 gfc_directorylist *p;
392 while (include_dirs != NULL)
394 p = include_dirs;
395 include_dirs = include_dirs->next;
396 free (p->path);
397 free (p);
400 while (intrinsic_modules_dirs != NULL)
402 p = intrinsic_modules_dirs;
403 intrinsic_modules_dirs = intrinsic_modules_dirs->next;
404 free (p->path);
405 free (p);
408 free (gfc_option.module_dir);
412 static FILE *
413 open_included_file (const char *name, gfc_directorylist *list,
414 bool module, bool system)
416 char *fullname;
417 gfc_directorylist *p;
418 FILE *f;
420 for (p = list; p; p = p->next)
422 if (module && !p->use_for_modules)
423 continue;
425 fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1);
426 strcpy (fullname, p->path);
427 strcat (fullname, name);
429 f = gfc_open_file (fullname);
430 if (f != NULL)
432 if (gfc_cpp_makedep ())
433 gfc_cpp_add_dep (fullname, system);
435 return f;
439 return NULL;
443 /* Opens file for reading, searching through the include directories
444 given if necessary. If the include_cwd argument is true, we try
445 to open the file in the current directory first. */
447 FILE *
448 gfc_open_included_file (const char *name, bool include_cwd, bool module)
450 FILE *f = NULL;
452 if (IS_ABSOLUTE_PATH (name) || include_cwd)
454 f = gfc_open_file (name);
455 if (f && gfc_cpp_makedep ())
456 gfc_cpp_add_dep (name, false);
459 if (!f)
460 f = open_included_file (name, include_dirs, module, false);
462 return f;
466 /* Test to see if we're at the end of the main source file. */
469 gfc_at_end (void)
471 return end_flag;
475 /* Test to see if we're at the end of the current file. */
478 gfc_at_eof (void)
480 if (gfc_at_end ())
481 return 1;
483 if (line_head == NULL)
484 return 1; /* Null file */
486 if (gfc_current_locus.lb == NULL)
487 return 1;
489 return 0;
493 /* Test to see if we're at the beginning of a new line. */
496 gfc_at_bol (void)
498 if (gfc_at_eof ())
499 return 1;
501 return (gfc_current_locus.nextc == gfc_current_locus.lb->line);
505 /* Test to see if we're at the end of a line. */
508 gfc_at_eol (void)
510 if (gfc_at_eof ())
511 return 1;
513 return (*gfc_current_locus.nextc == '\0');
516 static void
517 add_file_change (const char *filename, int line)
519 if (file_changes_count == file_changes_allocated)
521 if (file_changes_allocated)
522 file_changes_allocated *= 2;
523 else
524 file_changes_allocated = 16;
525 file_changes = XRESIZEVEC (struct gfc_file_change, file_changes,
526 file_changes_allocated);
528 file_changes[file_changes_count].filename = filename;
529 file_changes[file_changes_count].lb = NULL;
530 file_changes[file_changes_count++].line = line;
533 static void
534 report_file_change (gfc_linebuf *lb)
536 size_t c = file_changes_cur;
537 while (c < file_changes_count
538 && file_changes[c].lb == lb)
540 if (file_changes[c].filename)
541 (*debug_hooks->start_source_file) (file_changes[c].line,
542 file_changes[c].filename);
543 else
544 (*debug_hooks->end_source_file) (file_changes[c].line);
545 ++c;
547 file_changes_cur = c;
550 void
551 gfc_start_source_files (void)
553 /* If the debugger wants the name of the main source file,
554 we give it. */
555 if (debug_hooks->start_end_main_source_file)
556 (*debug_hooks->start_source_file) (0, gfc_source_file);
558 file_changes_cur = 0;
559 report_file_change (gfc_current_locus.lb);
562 void
563 gfc_end_source_files (void)
565 report_file_change (NULL);
567 if (debug_hooks->start_end_main_source_file)
568 (*debug_hooks->end_source_file) (0);
571 /* Advance the current line pointer to the next line. */
573 void
574 gfc_advance_line (void)
576 if (gfc_at_end ())
577 return;
579 if (gfc_current_locus.lb == NULL)
581 end_flag = 1;
582 return;
585 if (gfc_current_locus.lb->next
586 && !gfc_current_locus.lb->next->dbg_emitted)
588 report_file_change (gfc_current_locus.lb->next);
589 gfc_current_locus.lb->next->dbg_emitted = true;
592 gfc_current_locus.lb = gfc_current_locus.lb->next;
594 if (gfc_current_locus.lb != NULL)
595 gfc_current_locus.nextc = gfc_current_locus.lb->line;
596 else
598 gfc_current_locus.nextc = NULL;
599 end_flag = 1;
604 /* Get the next character from the input, advancing gfc_current_file's
605 locus. When we hit the end of the line or the end of the file, we
606 start returning a '\n' in order to complete the current statement.
607 No Fortran line conventions are implemented here.
609 Requiring explicit advances to the next line prevents the parse
610 pointer from being on the wrong line if the current statement ends
611 prematurely. */
613 static gfc_char_t
614 next_char (void)
616 gfc_char_t c;
618 if (gfc_current_locus.nextc == NULL)
619 return '\n';
621 c = *gfc_current_locus.nextc++;
622 if (c == '\0')
624 gfc_current_locus.nextc--; /* Remain on this line. */
625 c = '\n';
628 return c;
632 /* Skip a comment. When we come here the parse pointer is positioned
633 immediately after the comment character. If we ever implement
634 compiler directives within comments, here is where we parse the
635 directive. */
637 static void
638 skip_comment_line (void)
640 gfc_char_t c;
644 c = next_char ();
646 while (c != '\n');
648 gfc_advance_line ();
653 gfc_define_undef_line (void)
655 char *tmp;
657 /* All lines beginning with '#' are either #define or #undef. */
658 if (debug_info_level != DINFO_LEVEL_VERBOSE || gfc_peek_ascii_char () != '#')
659 return 0;
661 if (wide_strncmp (gfc_current_locus.nextc, "#define ", 8) == 0)
663 tmp = gfc_widechar_to_char (&gfc_current_locus.nextc[8], -1);
664 (*debug_hooks->define) (gfc_linebuf_linenum (gfc_current_locus.lb),
665 tmp);
666 free (tmp);
669 if (wide_strncmp (gfc_current_locus.nextc, "#undef ", 7) == 0)
671 tmp = gfc_widechar_to_char (&gfc_current_locus.nextc[7], -1);
672 (*debug_hooks->undef) (gfc_linebuf_linenum (gfc_current_locus.lb),
673 tmp);
674 free (tmp);
677 /* Skip the rest of the line. */
678 skip_comment_line ();
680 return 1;
684 /* Return true if GCC$ was matched. */
685 static bool
686 skip_gcc_attribute (locus start)
688 bool r = false;
689 char c;
690 locus old_loc = gfc_current_locus;
692 if ((c = next_char ()) == 'g' || c == 'G')
693 if ((c = next_char ()) == 'c' || c == 'C')
694 if ((c = next_char ()) == 'c' || c == 'C')
695 if ((c = next_char ()) == '$')
696 r = true;
698 if (r == false)
699 gfc_current_locus = old_loc;
700 else
702 gcc_attribute_flag = 1;
703 gcc_attribute_locus = old_loc;
704 gfc_current_locus = start;
707 return r;
712 /* Comment lines are null lines, lines containing only blanks or lines
713 on which the first nonblank line is a '!'.
714 Return true if !$ openmp conditional compilation sentinel was
715 seen. */
717 static bool
718 skip_free_comments (void)
720 locus start;
721 gfc_char_t c;
722 int at_bol;
724 for (;;)
726 at_bol = gfc_at_bol ();
727 start = gfc_current_locus;
728 if (gfc_at_eof ())
729 break;
732 c = next_char ();
733 while (gfc_is_whitespace (c));
735 if (c == '\n')
737 gfc_advance_line ();
738 continue;
741 if (c == '!')
743 /* Keep the !GCC$ line. */
744 if (at_bol && skip_gcc_attribute (start))
745 return false;
747 /* If -fopenmp, we need to handle here 2 things:
748 1) don't treat !$omp as comments, but directives
749 2) handle OpenMP conditional compilation, where
750 !$ should be treated as 2 spaces (for initial lines
751 only if followed by space). */
752 if ((gfc_option.gfc_flag_openmp
753 || gfc_option.gfc_flag_openmp_simd) && at_bol)
755 locus old_loc = gfc_current_locus;
756 if (next_char () == '$')
758 c = next_char ();
759 if (c == 'o' || c == 'O')
761 if (((c = next_char ()) == 'm' || c == 'M')
762 && ((c = next_char ()) == 'p' || c == 'P'))
764 if ((c = next_char ()) == ' ' || c == '\t'
765 || continue_flag)
767 while (gfc_is_whitespace (c))
768 c = next_char ();
769 if (c != '\n' && c != '!')
771 openmp_flag = 1;
772 openmp_locus = old_loc;
773 gfc_current_locus = start;
774 return false;
777 else
778 gfc_warning_now ("!$OMP at %C starts a commented "
779 "line as it neither is followed "
780 "by a space nor is a "
781 "continuation line");
783 gfc_current_locus = old_loc;
784 next_char ();
785 c = next_char ();
787 if (continue_flag || c == ' ' || c == '\t')
789 gfc_current_locus = old_loc;
790 next_char ();
791 openmp_flag = 0;
792 return true;
795 gfc_current_locus = old_loc;
797 skip_comment_line ();
798 continue;
801 break;
804 if (openmp_flag && at_bol)
805 openmp_flag = 0;
807 gcc_attribute_flag = 0;
808 gfc_current_locus = start;
809 return false;
813 /* Skip comment lines in fixed source mode. We have the same rules as
814 in skip_free_comment(), except that we can have a 'c', 'C' or '*'
815 in column 1, and a '!' cannot be in column 6. Also, we deal with
816 lines with 'd' or 'D' in column 1, if the user requested this. */
818 static void
819 skip_fixed_comments (void)
821 locus start;
822 int col;
823 gfc_char_t c;
825 if (! gfc_at_bol ())
827 start = gfc_current_locus;
828 if (! gfc_at_eof ())
831 c = next_char ();
832 while (gfc_is_whitespace (c));
834 if (c == '\n')
835 gfc_advance_line ();
836 else if (c == '!')
837 skip_comment_line ();
840 if (! gfc_at_bol ())
842 gfc_current_locus = start;
843 return;
847 for (;;)
849 start = gfc_current_locus;
850 if (gfc_at_eof ())
851 break;
853 c = next_char ();
854 if (c == '\n')
856 gfc_advance_line ();
857 continue;
860 if (c == '!' || c == 'c' || c == 'C' || c == '*')
862 if (skip_gcc_attribute (start))
864 /* Canonicalize to *$omp. */
865 *start.nextc = '*';
866 return;
869 /* If -fopenmp, we need to handle here 2 things:
870 1) don't treat !$omp|c$omp|*$omp as comments, but directives
871 2) handle OpenMP conditional compilation, where
872 !$|c$|*$ should be treated as 2 spaces if the characters
873 in columns 3 to 6 are valid fixed form label columns
874 characters. */
875 if (gfc_current_locus.lb != NULL
876 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
877 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
879 if (gfc_option.gfc_flag_openmp || gfc_option.gfc_flag_openmp_simd)
881 if (next_char () == '$')
883 c = next_char ();
884 if (c == 'o' || c == 'O')
886 if (((c = next_char ()) == 'm' || c == 'M')
887 && ((c = next_char ()) == 'p' || c == 'P'))
889 c = next_char ();
890 if (c != '\n'
891 && ((openmp_flag && continue_flag)
892 || c == ' ' || c == '\t' || c == '0'))
895 c = next_char ();
896 while (gfc_is_whitespace (c));
897 if (c != '\n' && c != '!')
899 /* Canonicalize to *$omp. */
900 *start.nextc = '*';
901 openmp_flag = 1;
902 gfc_current_locus = start;
903 return;
908 else
910 int digit_seen = 0;
912 for (col = 3; col < 6; col++, c = next_char ())
913 if (c == ' ')
914 continue;
915 else if (c == '\t')
917 col = 6;
918 break;
920 else if (c < '0' || c > '9')
921 break;
922 else
923 digit_seen = 1;
925 if (col == 6 && c != '\n'
926 && ((continue_flag && !digit_seen)
927 || c == ' ' || c == '\t' || c == '0'))
929 gfc_current_locus = start;
930 start.nextc[0] = ' ';
931 start.nextc[1] = ' ';
932 continue;
936 gfc_current_locus = start;
938 skip_comment_line ();
939 continue;
942 if (gfc_option.flag_d_lines != -1 && (c == 'd' || c == 'D'))
944 if (gfc_option.flag_d_lines == 0)
946 skip_comment_line ();
947 continue;
949 else
950 *start.nextc = c = ' ';
953 col = 1;
955 while (gfc_is_whitespace (c))
957 c = next_char ();
958 col++;
961 if (c == '\n')
963 gfc_advance_line ();
964 continue;
967 if (col != 6 && c == '!')
969 if (gfc_current_locus.lb != NULL
970 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
971 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
972 skip_comment_line ();
973 continue;
976 break;
979 openmp_flag = 0;
980 gcc_attribute_flag = 0;
981 gfc_current_locus = start;
985 /* Skips the current line if it is a comment. */
987 void
988 gfc_skip_comments (void)
990 if (gfc_current_form == FORM_FREE)
991 skip_free_comments ();
992 else
993 skip_fixed_comments ();
997 /* Get the next character from the input, taking continuation lines
998 and end-of-line comments into account. This implies that comment
999 lines between continued lines must be eaten here. For higher-level
1000 subroutines, this flattens continued lines into a single logical
1001 line. The in_string flag denotes whether we're inside a character
1002 context or not. */
1004 gfc_char_t
1005 gfc_next_char_literal (gfc_instring in_string)
1007 locus old_loc;
1008 int i, prev_openmp_flag;
1009 gfc_char_t c;
1011 continue_flag = 0;
1013 restart:
1014 c = next_char ();
1015 if (gfc_at_end ())
1017 continue_count = 0;
1018 return c;
1021 if (gfc_current_form == FORM_FREE)
1023 bool openmp_cond_flag;
1025 if (!in_string && c == '!')
1027 if (gcc_attribute_flag
1028 && memcmp (&gfc_current_locus, &gcc_attribute_locus,
1029 sizeof (gfc_current_locus)) == 0)
1030 goto done;
1032 if (openmp_flag
1033 && memcmp (&gfc_current_locus, &openmp_locus,
1034 sizeof (gfc_current_locus)) == 0)
1035 goto done;
1037 /* This line can't be continued */
1040 c = next_char ();
1042 while (c != '\n');
1044 /* Avoid truncation warnings for comment ending lines. */
1045 gfc_current_locus.lb->truncated = 0;
1047 goto done;
1050 /* Check to see if the continuation line was truncated. */
1051 if (warn_line_truncation && gfc_current_locus.lb != NULL
1052 && gfc_current_locus.lb->truncated)
1054 int maxlen = gfc_option.free_line_length;
1055 gfc_char_t *current_nextc = gfc_current_locus.nextc;
1057 gfc_current_locus.lb->truncated = 0;
1058 gfc_current_locus.nextc = gfc_current_locus.lb->line + maxlen;
1059 gfc_warning_now_1 ("Line truncated at %L", &gfc_current_locus);
1060 gfc_current_locus.nextc = current_nextc;
1063 if (c != '&')
1064 goto done;
1066 /* If the next nonblank character is a ! or \n, we've got a
1067 continuation line. */
1068 old_loc = gfc_current_locus;
1070 c = next_char ();
1071 while (gfc_is_whitespace (c))
1072 c = next_char ();
1074 /* Character constants to be continued cannot have commentary
1075 after the '&'. */
1077 if (in_string && c != '\n')
1079 gfc_current_locus = old_loc;
1080 c = '&';
1081 goto done;
1084 if (c != '!' && c != '\n')
1086 gfc_current_locus = old_loc;
1087 c = '&';
1088 goto done;
1091 prev_openmp_flag = openmp_flag;
1092 continue_flag = 1;
1093 if (c == '!')
1094 skip_comment_line ();
1095 else
1096 gfc_advance_line ();
1098 if (gfc_at_eof ())
1099 goto not_continuation;
1101 /* We've got a continuation line. If we are on the very next line after
1102 the last continuation, increment the continuation line count and
1103 check whether the limit has been exceeded. */
1104 if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1)
1106 if (++continue_count == gfc_option.max_continue_free)
1108 if (gfc_notification_std (GFC_STD_GNU) || pedantic)
1109 gfc_warning ("Limit of %d continuations exceeded in "
1110 "statement at %C", gfc_option.max_continue_free);
1114 /* Now find where it continues. First eat any comment lines. */
1115 openmp_cond_flag = skip_free_comments ();
1117 if (gfc_current_locus.lb != NULL
1118 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
1119 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
1121 if (prev_openmp_flag != openmp_flag)
1123 gfc_current_locus = old_loc;
1124 openmp_flag = prev_openmp_flag;
1125 c = '&';
1126 goto done;
1129 /* Now that we have a non-comment line, probe ahead for the
1130 first non-whitespace character. If it is another '&', then
1131 reading starts at the next character, otherwise we must back
1132 up to where the whitespace started and resume from there. */
1134 old_loc = gfc_current_locus;
1136 c = next_char ();
1137 while (gfc_is_whitespace (c))
1138 c = next_char ();
1140 if (openmp_flag)
1142 for (i = 0; i < 5; i++, c = next_char ())
1144 gcc_assert (gfc_wide_tolower (c) == (unsigned char) "!$omp"[i]);
1145 if (i == 4)
1146 old_loc = gfc_current_locus;
1148 while (gfc_is_whitespace (c))
1149 c = next_char ();
1152 if (c != '&')
1154 if (in_string)
1156 gfc_current_locus.nextc--;
1157 if (warn_ampersand && in_string == INSTRING_WARN)
1158 gfc_warning (OPT_Wampersand,
1159 "Missing %<&%> in continued character "
1160 "constant at %C");
1162 /* Both !$omp and !$ -fopenmp continuation lines have & on the
1163 continuation line only optionally. */
1164 else if (openmp_flag || openmp_cond_flag)
1165 gfc_current_locus.nextc--;
1166 else
1168 c = ' ';
1169 gfc_current_locus = old_loc;
1170 goto done;
1174 else /* Fixed form. */
1176 /* Fixed form continuation. */
1177 if (!in_string && c == '!')
1179 /* Skip comment at end of line. */
1182 c = next_char ();
1184 while (c != '\n');
1186 /* Avoid truncation warnings for comment ending lines. */
1187 gfc_current_locus.lb->truncated = 0;
1190 if (c != '\n')
1191 goto done;
1193 /* Check to see if the continuation line was truncated. */
1194 if (warn_line_truncation && gfc_current_locus.lb != NULL
1195 && gfc_current_locus.lb->truncated)
1197 gfc_current_locus.lb->truncated = 0;
1198 gfc_warning_now_1 ("Line truncated at %L", &gfc_current_locus);
1201 prev_openmp_flag = openmp_flag;
1202 continue_flag = 1;
1203 old_loc = gfc_current_locus;
1205 gfc_advance_line ();
1206 skip_fixed_comments ();
1208 /* See if this line is a continuation line. */
1209 if (openmp_flag != prev_openmp_flag)
1211 openmp_flag = prev_openmp_flag;
1212 goto not_continuation;
1215 if (!openmp_flag)
1216 for (i = 0; i < 5; i++)
1218 c = next_char ();
1219 if (c != ' ')
1220 goto not_continuation;
1222 else
1223 for (i = 0; i < 5; i++)
1225 c = next_char ();
1226 if (gfc_wide_tolower (c) != (unsigned char) "*$omp"[i])
1227 goto not_continuation;
1230 c = next_char ();
1231 if (c == '0' || c == ' ' || c == '\n')
1232 goto not_continuation;
1234 /* We've got a continuation line. If we are on the very next line after
1235 the last continuation, increment the continuation line count and
1236 check whether the limit has been exceeded. */
1237 if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1)
1239 if (++continue_count == gfc_option.max_continue_fixed)
1241 if (gfc_notification_std (GFC_STD_GNU) || pedantic)
1242 gfc_warning ("Limit of %d continuations exceeded in "
1243 "statement at %C",
1244 gfc_option.max_continue_fixed);
1248 if (gfc_current_locus.lb != NULL
1249 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
1250 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
1253 /* Ready to read first character of continuation line, which might
1254 be another continuation line! */
1255 goto restart;
1257 not_continuation:
1258 c = '\n';
1259 gfc_current_locus = old_loc;
1261 done:
1262 if (c == '\n')
1263 continue_count = 0;
1264 continue_flag = 0;
1265 return c;
1269 /* Get the next character of input, folded to lowercase. In fixed
1270 form mode, we also ignore spaces. When matcher subroutines are
1271 parsing character literals, they have to call
1272 gfc_next_char_literal(). */
1274 gfc_char_t
1275 gfc_next_char (void)
1277 gfc_char_t c;
1281 c = gfc_next_char_literal (NONSTRING);
1283 while (gfc_current_form == FORM_FIXED && gfc_is_whitespace (c));
1285 return gfc_wide_tolower (c);
1288 char
1289 gfc_next_ascii_char (void)
1291 gfc_char_t c = gfc_next_char ();
1293 return (gfc_wide_fits_in_byte (c) ? (unsigned char) c
1294 : (unsigned char) UCHAR_MAX);
1298 gfc_char_t
1299 gfc_peek_char (void)
1301 locus old_loc;
1302 gfc_char_t c;
1304 old_loc = gfc_current_locus;
1305 c = gfc_next_char ();
1306 gfc_current_locus = old_loc;
1308 return c;
1312 char
1313 gfc_peek_ascii_char (void)
1315 gfc_char_t c = gfc_peek_char ();
1317 return (gfc_wide_fits_in_byte (c) ? (unsigned char) c
1318 : (unsigned char) UCHAR_MAX);
1322 /* Recover from an error. We try to get past the current statement
1323 and get lined up for the next. The next statement follows a '\n'
1324 or a ';'. We also assume that we are not within a character
1325 constant, and deal with finding a '\'' or '"'. */
1327 void
1328 gfc_error_recovery (void)
1330 gfc_char_t c, delim;
1332 if (gfc_at_eof ())
1333 return;
1335 for (;;)
1337 c = gfc_next_char ();
1338 if (c == '\n' || c == ';')
1339 break;
1341 if (c != '\'' && c != '"')
1343 if (gfc_at_eof ())
1344 break;
1345 continue;
1347 delim = c;
1349 for (;;)
1351 c = next_char ();
1353 if (c == delim)
1354 break;
1355 if (c == '\n')
1356 return;
1357 if (c == '\\')
1359 c = next_char ();
1360 if (c == '\n')
1361 return;
1364 if (gfc_at_eof ())
1365 break;
1370 /* Read ahead until the next character to be read is not whitespace. */
1372 void
1373 gfc_gobble_whitespace (void)
1375 static int linenum = 0;
1376 locus old_loc;
1377 gfc_char_t c;
1381 old_loc = gfc_current_locus;
1382 c = gfc_next_char_literal (NONSTRING);
1383 /* Issue a warning for nonconforming tabs. We keep track of the line
1384 number because the Fortran matchers will often back up and the same
1385 line will be scanned multiple times. */
1386 if (warn_tabs && c == '\t')
1388 int cur_linenum = LOCATION_LINE (gfc_current_locus.lb->location);
1389 if (cur_linenum != linenum)
1391 linenum = cur_linenum;
1392 gfc_warning_now (OPT_Wtabs, "Nonconforming tab character at %C");
1396 while (gfc_is_whitespace (c));
1398 gfc_current_locus = old_loc;
1402 /* Load a single line into pbuf.
1404 If pbuf points to a NULL pointer, it is allocated.
1405 We truncate lines that are too long, unless we're dealing with
1406 preprocessor lines or if the option -ffixed-line-length-none is set,
1407 in which case we reallocate the buffer to fit the entire line, if
1408 need be.
1409 In fixed mode, we expand a tab that occurs within the statement
1410 label region to expand to spaces that leave the next character in
1411 the source region.
1413 If first_char is not NULL, it's a pointer to a single char value holding
1414 the first character of the line, which has already been read by the
1415 caller. This avoids the use of ungetc().
1417 load_line returns whether the line was truncated.
1419 NOTE: The error machinery isn't available at this point, so we can't
1420 easily report line and column numbers consistent with other
1421 parts of gfortran. */
1423 static int
1424 load_line (FILE *input, gfc_char_t **pbuf, int *pbuflen, const int *first_char)
1426 static int linenum = 0, current_line = 1;
1427 int c, maxlen, i, preprocessor_flag, buflen = *pbuflen;
1428 int trunc_flag = 0, seen_comment = 0;
1429 int seen_printable = 0, seen_ampersand = 0, quoted = ' ';
1430 gfc_char_t *buffer;
1431 bool found_tab = false;
1433 /* Determine the maximum allowed line length. */
1434 if (gfc_current_form == FORM_FREE)
1435 maxlen = gfc_option.free_line_length;
1436 else if (gfc_current_form == FORM_FIXED)
1437 maxlen = gfc_option.fixed_line_length;
1438 else
1439 maxlen = 72;
1441 if (*pbuf == NULL)
1443 /* Allocate the line buffer, storing its length into buflen.
1444 Note that if maxlen==0, indicating that arbitrary-length lines
1445 are allowed, the buffer will be reallocated if this length is
1446 insufficient; since 132 characters is the length of a standard
1447 free-form line, we use that as a starting guess. */
1448 if (maxlen > 0)
1449 buflen = maxlen;
1450 else
1451 buflen = 132;
1453 *pbuf = gfc_get_wide_string (buflen + 1);
1456 i = 0;
1457 buffer = *pbuf;
1459 if (first_char)
1460 c = *first_char;
1461 else
1462 c = getc (input);
1464 /* In order to not truncate preprocessor lines, we have to
1465 remember that this is one. */
1466 preprocessor_flag = (c == '#' ? 1 : 0);
1468 for (;;)
1470 if (c == EOF)
1471 break;
1473 if (c == '\n')
1475 /* Check for illegal use of ampersand. See F95 Standard 3.3.1.3. */
1476 if (gfc_current_form == FORM_FREE
1477 && !seen_printable && seen_ampersand)
1479 if (pedantic)
1480 gfc_error_now ("%<&%> not allowed by itself in line %d",
1481 current_line);
1482 else
1483 gfc_warning_now ("%<&%> not allowed by itself in line %d",
1484 current_line);
1486 break;
1489 if (c == '\r' || c == '\0')
1490 goto next_char; /* Gobble characters. */
1492 if (c == '&')
1494 if (seen_ampersand)
1496 seen_ampersand = 0;
1497 seen_printable = 1;
1499 else
1500 seen_ampersand = 1;
1503 if ((c != '&' && c != '!' && c != ' ') || (c == '!' && !seen_ampersand))
1504 seen_printable = 1;
1506 /* Is this a fixed-form comment? */
1507 if (gfc_current_form == FORM_FIXED && i == 0
1508 && (c == '*' || c == 'c' || c == 'd'))
1509 seen_comment = 1;
1511 if (quoted == ' ')
1513 if (c == '\'' || c == '"')
1514 quoted = c;
1516 else if (c == quoted)
1517 quoted = ' ';
1519 /* Is this a free-form comment? */
1520 if (c == '!' && quoted == ' ')
1521 seen_comment = 1;
1523 /* Vendor extension: "<tab>1" marks a continuation line. */
1524 if (found_tab)
1526 found_tab = false;
1527 if (c >= '1' && c <= '9')
1529 *(buffer-1) = c;
1530 goto next_char;
1534 if (gfc_current_form == FORM_FIXED && c == '\t' && i < 6)
1536 found_tab = true;
1538 if (warn_tabs && seen_comment == 0 && current_line != linenum)
1540 linenum = current_line;
1541 gfc_warning_now (OPT_Wtabs,
1542 "Nonconforming tab character in column %d "
1543 "of line %d", i+1, linenum);
1546 while (i < 6)
1548 *buffer++ = ' ';
1549 i++;
1552 goto next_char;
1555 *buffer++ = c;
1556 i++;
1558 if (maxlen == 0 || preprocessor_flag)
1560 if (i >= buflen)
1562 /* Reallocate line buffer to double size to hold the
1563 overlong line. */
1564 buflen = buflen * 2;
1565 *pbuf = XRESIZEVEC (gfc_char_t, *pbuf, (buflen + 1));
1566 buffer = (*pbuf) + i;
1569 else if (i >= maxlen)
1571 bool trunc_warn = true;
1573 /* Enhancement, if the very next non-space character is an ampersand
1574 or comment that we would otherwise warn about, don't mark as
1575 truncated. */
1577 /* Truncate the rest of the line. */
1578 for (;;)
1580 c = getc (input);
1581 if (c == '\r' || c == ' ')
1582 continue;
1584 if (c == '\n' || c == EOF)
1585 break;
1587 if (!trunc_warn && c != '!')
1588 trunc_warn = true;
1590 if (trunc_warn && ((gfc_current_form == FORM_FIXED && c == '&')
1591 || c == '!'))
1592 trunc_warn = false;
1594 if (c == '!')
1595 seen_comment = 1;
1597 if (trunc_warn && !seen_comment)
1598 trunc_flag = 1;
1601 c = '\n';
1602 continue;
1605 next_char:
1606 c = getc (input);
1609 /* Pad lines to the selected line length in fixed form. */
1610 if (gfc_current_form == FORM_FIXED
1611 && gfc_option.fixed_line_length != 0
1612 && !preprocessor_flag
1613 && c != EOF)
1615 while (i++ < maxlen)
1616 *buffer++ = ' ';
1619 *buffer = '\0';
1620 *pbuflen = buflen;
1621 current_line++;
1623 return trunc_flag;
1627 /* Get a gfc_file structure, initialize it and add it to
1628 the file stack. */
1630 static gfc_file *
1631 get_file (const char *name, enum lc_reason reason ATTRIBUTE_UNUSED)
1633 gfc_file *f;
1635 f = XCNEW (gfc_file);
1637 f->filename = xstrdup (name);
1639 f->next = file_head;
1640 file_head = f;
1642 f->up = current_file;
1643 if (current_file != NULL)
1644 f->inclusion_line = current_file->line;
1646 linemap_add (line_table, reason, false, f->filename, 1);
1648 return f;
1652 /* Deal with a line from the C preprocessor. The
1653 initial octothorp has already been seen. */
1655 static void
1656 preprocessor_line (gfc_char_t *c)
1658 bool flag[5];
1659 int i, line;
1660 gfc_char_t *wide_filename;
1661 gfc_file *f;
1662 int escaped, unescape;
1663 char *filename;
1665 c++;
1666 while (*c == ' ' || *c == '\t')
1667 c++;
1669 if (*c < '0' || *c > '9')
1670 goto bad_cpp_line;
1672 line = wide_atoi (c);
1674 c = wide_strchr (c, ' ');
1675 if (c == NULL)
1677 /* No file name given. Set new line number. */
1678 current_file->line = line;
1679 return;
1682 /* Skip spaces. */
1683 while (*c == ' ' || *c == '\t')
1684 c++;
1686 /* Skip quote. */
1687 if (*c != '"')
1688 goto bad_cpp_line;
1689 ++c;
1691 wide_filename = c;
1693 /* Make filename end at quote. */
1694 unescape = 0;
1695 escaped = false;
1696 while (*c && ! (!escaped && *c == '"'))
1698 if (escaped)
1699 escaped = false;
1700 else if (*c == '\\')
1702 escaped = true;
1703 unescape++;
1705 ++c;
1708 if (! *c)
1709 /* Preprocessor line has no closing quote. */
1710 goto bad_cpp_line;
1712 *c++ = '\0';
1714 /* Undo effects of cpp_quote_string. */
1715 if (unescape)
1717 gfc_char_t *s = wide_filename;
1718 gfc_char_t *d = gfc_get_wide_string (c - wide_filename - unescape);
1720 wide_filename = d;
1721 while (*s)
1723 if (*s == '\\')
1724 *d++ = *++s;
1725 else
1726 *d++ = *s;
1727 s++;
1729 *d = '\0';
1732 /* Get flags. */
1734 flag[1] = flag[2] = flag[3] = flag[4] = false;
1736 for (;;)
1738 c = wide_strchr (c, ' ');
1739 if (c == NULL)
1740 break;
1742 c++;
1743 i = wide_atoi (c);
1745 if (1 <= i && i <= 4)
1746 flag[i] = true;
1749 /* Convert the filename in wide characters into a filename in narrow
1750 characters. */
1751 filename = gfc_widechar_to_char (wide_filename, -1);
1753 /* Interpret flags. */
1755 if (flag[1]) /* Starting new file. */
1757 f = get_file (filename, LC_RENAME);
1758 add_file_change (f->filename, f->inclusion_line);
1759 current_file = f;
1762 if (flag[2]) /* Ending current file. */
1764 if (!current_file->up
1765 || filename_cmp (current_file->up->filename, filename) != 0)
1767 gfc_warning_now_1 ("%s:%d: file %s left but not entered",
1768 current_file->filename, current_file->line,
1769 filename);
1770 if (unescape)
1771 free (wide_filename);
1772 free (filename);
1773 return;
1776 add_file_change (NULL, line);
1777 current_file = current_file->up;
1778 linemap_add (line_table, LC_RENAME, false, current_file->filename,
1779 current_file->line);
1782 /* The name of the file can be a temporary file produced by
1783 cpp. Replace the name if it is different. */
1785 if (filename_cmp (current_file->filename, filename) != 0)
1787 /* FIXME: we leak the old filename because a pointer to it may be stored
1788 in the linemap. Alternative could be using GC or updating linemap to
1789 point to the new name, but there is no API for that currently. */
1790 current_file->filename = xstrdup (filename);
1793 /* Set new line number. */
1794 current_file->line = line;
1795 if (unescape)
1796 free (wide_filename);
1797 free (filename);
1798 return;
1800 bad_cpp_line:
1801 gfc_warning_now_1 ("%s:%d: Illegal preprocessor directive",
1802 current_file->filename, current_file->line);
1803 current_file->line++;
1807 static bool load_file (const char *, const char *, bool);
1809 /* include_line()-- Checks a line buffer to see if it is an include
1810 line. If so, we call load_file() recursively to load the included
1811 file. We never return a syntax error because a statement like
1812 "include = 5" is perfectly legal. We return false if no include was
1813 processed or true if we matched an include. */
1815 static bool
1816 include_line (gfc_char_t *line)
1818 gfc_char_t quote, *c, *begin, *stop;
1819 char *filename;
1821 c = line;
1823 if (gfc_option.gfc_flag_openmp || gfc_option.gfc_flag_openmp_simd)
1825 if (gfc_current_form == FORM_FREE)
1827 while (*c == ' ' || *c == '\t')
1828 c++;
1829 if (*c == '!' && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
1830 c += 3;
1832 else
1834 if ((*c == '!' || *c == 'c' || *c == 'C' || *c == '*')
1835 && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
1836 c += 3;
1840 while (*c == ' ' || *c == '\t')
1841 c++;
1843 if (gfc_wide_strncasecmp (c, "include", 7))
1844 return false;
1846 c += 7;
1847 while (*c == ' ' || *c == '\t')
1848 c++;
1850 /* Find filename between quotes. */
1852 quote = *c++;
1853 if (quote != '"' && quote != '\'')
1854 return false;
1856 begin = c;
1858 while (*c != quote && *c != '\0')
1859 c++;
1861 if (*c == '\0')
1862 return false;
1864 stop = c++;
1866 while (*c == ' ' || *c == '\t')
1867 c++;
1869 if (*c != '\0' && *c != '!')
1870 return false;
1872 /* We have an include line at this point. */
1874 *stop = '\0'; /* It's ok to trash the buffer, as this line won't be
1875 read by anything else. */
1877 filename = gfc_widechar_to_char (begin, -1);
1878 if (!load_file (filename, NULL, false))
1879 exit (FATAL_EXIT_CODE);
1881 free (filename);
1882 return true;
1886 /* Load a file into memory by calling load_line until the file ends. */
1888 static bool
1889 load_file (const char *realfilename, const char *displayedname, bool initial)
1891 gfc_char_t *line;
1892 gfc_linebuf *b;
1893 gfc_file *f;
1894 FILE *input;
1895 int len, line_len;
1896 bool first_line;
1897 const char *filename;
1898 /* If realfilename and displayedname are different and non-null then
1899 surely realfilename is the preprocessed form of
1900 displayedname. */
1901 bool preprocessed_p = (realfilename && displayedname
1902 && strcmp (realfilename, displayedname));
1904 filename = displayedname ? displayedname : realfilename;
1906 for (f = current_file; f; f = f->up)
1907 if (filename_cmp (filename, f->filename) == 0)
1909 fprintf (stderr, "%s:%d: Error: File '%s' is being included "
1910 "recursively\n", current_file->filename, current_file->line,
1911 filename);
1912 return false;
1915 if (initial)
1917 if (gfc_src_file)
1919 input = gfc_src_file;
1920 gfc_src_file = NULL;
1922 else
1923 input = gfc_open_file (realfilename);
1924 if (input == NULL)
1926 gfc_error_now ("Can't open file %qs", filename);
1927 return false;
1930 else
1932 input = gfc_open_included_file (realfilename, false, false);
1933 if (input == NULL)
1935 fprintf (stderr, "%s:%d: Error: Can't open included file '%s'\n",
1936 current_file->filename, current_file->line, filename);
1937 return false;
1941 /* Load the file.
1943 A "non-initial" file means a file that is being included. In
1944 that case we are creating an LC_ENTER map.
1946 An "initial" file means a main file; one that is not included.
1947 That file has already got at least one (surely more) line map(s)
1948 created by gfc_init. So the subsequent map created in that case
1949 must have LC_RENAME reason.
1951 This latter case is not true for a preprocessed file. In that
1952 case, although the file is "initial", the line maps created by
1953 gfc_init was used during the preprocessing of the file. Now that
1954 the preprocessing is over and we are being fed the result of that
1955 preprocessing, we need to create a brand new line map for the
1956 preprocessed file, so the reason is going to be LC_ENTER. */
1958 f = get_file (filename, (initial && !preprocessed_p) ? LC_RENAME : LC_ENTER);
1959 if (!initial)
1960 add_file_change (f->filename, f->inclusion_line);
1961 current_file = f;
1962 current_file->line = 1;
1963 line = NULL;
1964 line_len = 0;
1965 first_line = true;
1967 if (initial && gfc_src_preprocessor_lines[0])
1969 preprocessor_line (gfc_src_preprocessor_lines[0]);
1970 free (gfc_src_preprocessor_lines[0]);
1971 gfc_src_preprocessor_lines[0] = NULL;
1972 if (gfc_src_preprocessor_lines[1])
1974 preprocessor_line (gfc_src_preprocessor_lines[1]);
1975 free (gfc_src_preprocessor_lines[1]);
1976 gfc_src_preprocessor_lines[1] = NULL;
1980 for (;;)
1982 int trunc = load_line (input, &line, &line_len, NULL);
1984 len = gfc_wide_strlen (line);
1985 if (feof (input) && len == 0)
1986 break;
1988 /* If this is the first line of the file, it can contain a byte
1989 order mark (BOM), which we will ignore:
1990 FF FE is UTF-16 little endian,
1991 FE FF is UTF-16 big endian,
1992 EF BB BF is UTF-8. */
1993 if (first_line
1994 && ((line_len >= 2 && line[0] == (unsigned char) '\xFF'
1995 && line[1] == (unsigned char) '\xFE')
1996 || (line_len >= 2 && line[0] == (unsigned char) '\xFE'
1997 && line[1] == (unsigned char) '\xFF')
1998 || (line_len >= 3 && line[0] == (unsigned char) '\xEF'
1999 && line[1] == (unsigned char) '\xBB'
2000 && line[2] == (unsigned char) '\xBF')))
2002 int n = line[1] == (unsigned char) '\xBB' ? 3 : 2;
2003 gfc_char_t *new_char = gfc_get_wide_string (line_len);
2005 wide_strcpy (new_char, &line[n]);
2006 free (line);
2007 line = new_char;
2008 len -= n;
2011 /* There are three things this line can be: a line of Fortran
2012 source, an include line or a C preprocessor directive. */
2014 if (line[0] == '#')
2016 /* When -g3 is specified, it's possible that we emit #define
2017 and #undef lines, which we need to pass to the middle-end
2018 so that it can emit correct debug info. */
2019 if (debug_info_level == DINFO_LEVEL_VERBOSE
2020 && (wide_strncmp (line, "#define ", 8) == 0
2021 || wide_strncmp (line, "#undef ", 7) == 0))
2023 else
2025 preprocessor_line (line);
2026 continue;
2030 /* Preprocessed files have preprocessor lines added before the byte
2031 order mark, so first_line is not about the first line of the file
2032 but the first line that's not a preprocessor line. */
2033 first_line = false;
2035 if (include_line (line))
2037 current_file->line++;
2038 continue;
2041 /* Add line. */
2043 b = XCNEWVAR (gfc_linebuf, gfc_linebuf_header_size
2044 + (len + 1) * sizeof (gfc_char_t));
2046 b->location
2047 = linemap_line_start (line_table, current_file->line++, 120);
2048 b->file = current_file;
2049 b->truncated = trunc;
2050 wide_strcpy (b->line, line);
2052 if (line_head == NULL)
2053 line_head = b;
2054 else
2055 line_tail->next = b;
2057 line_tail = b;
2059 while (file_changes_cur < file_changes_count)
2060 file_changes[file_changes_cur++].lb = b;
2063 /* Release the line buffer allocated in load_line. */
2064 free (line);
2066 fclose (input);
2068 if (!initial)
2069 add_file_change (NULL, current_file->inclusion_line + 1);
2070 current_file = current_file->up;
2071 linemap_add (line_table, LC_LEAVE, 0, NULL, 0);
2072 return true;
2076 /* Open a new file and start scanning from that file. Returns true
2077 if everything went OK, false otherwise. If form == FORM_UNKNOWN
2078 it tries to determine the source form from the filename, defaulting
2079 to free form. */
2081 bool
2082 gfc_new_file (void)
2084 bool result;
2086 if (gfc_cpp_enabled ())
2088 result = gfc_cpp_preprocess (gfc_source_file);
2089 if (!gfc_cpp_preprocess_only ())
2090 result = load_file (gfc_cpp_temporary_file (), gfc_source_file, true);
2092 else
2093 result = load_file (gfc_source_file, NULL, true);
2095 gfc_current_locus.lb = line_head;
2096 gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line;
2098 #if 0 /* Debugging aid. */
2099 for (; line_head; line_head = line_head->next)
2100 printf ("%s:%3d %s\n", LOCATION_FILE (line_head->location),
2101 LOCATION_LINE (line_head->location), line_head->line);
2103 exit (SUCCESS_EXIT_CODE);
2104 #endif
2106 return result;
2109 static char *
2110 unescape_filename (const char *ptr)
2112 const char *p = ptr, *s;
2113 char *d, *ret;
2114 int escaped, unescape = 0;
2116 /* Make filename end at quote. */
2117 escaped = false;
2118 while (*p && ! (! escaped && *p == '"'))
2120 if (escaped)
2121 escaped = false;
2122 else if (*p == '\\')
2124 escaped = true;
2125 unescape++;
2127 ++p;
2130 if (!*p || p[1])
2131 return NULL;
2133 /* Undo effects of cpp_quote_string. */
2134 s = ptr;
2135 d = XCNEWVEC (char, p + 1 - ptr - unescape);
2136 ret = d;
2138 while (s != p)
2140 if (*s == '\\')
2141 *d++ = *++s;
2142 else
2143 *d++ = *s;
2144 s++;
2146 *d = '\0';
2147 return ret;
2150 /* For preprocessed files, if the first tokens are of the form # NUM.
2151 handle the directives so we know the original file name. */
2153 const char *
2154 gfc_read_orig_filename (const char *filename, const char **canon_source_file)
2156 int c, len;
2157 char *dirname, *tmp;
2159 gfc_src_file = gfc_open_file (filename);
2160 if (gfc_src_file == NULL)
2161 return NULL;
2163 c = getc (gfc_src_file);
2165 if (c != '#')
2166 return NULL;
2168 len = 0;
2169 load_line (gfc_src_file, &gfc_src_preprocessor_lines[0], &len, &c);
2171 if (wide_strncmp (gfc_src_preprocessor_lines[0], "# 1 \"", 5) != 0)
2172 return NULL;
2174 tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[0][5], -1);
2175 filename = unescape_filename (tmp);
2176 free (tmp);
2177 if (filename == NULL)
2178 return NULL;
2180 c = getc (gfc_src_file);
2182 if (c != '#')
2183 return filename;
2185 len = 0;
2186 load_line (gfc_src_file, &gfc_src_preprocessor_lines[1], &len, &c);
2188 if (wide_strncmp (gfc_src_preprocessor_lines[1], "# 1 \"", 5) != 0)
2189 return filename;
2191 tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[1][5], -1);
2192 dirname = unescape_filename (tmp);
2193 free (tmp);
2194 if (dirname == NULL)
2195 return filename;
2197 len = strlen (dirname);
2198 if (len < 3 || dirname[len - 1] != '/' || dirname[len - 2] != '/')
2200 free (dirname);
2201 return filename;
2203 dirname[len - 2] = '\0';
2204 set_src_pwd (dirname);
2206 if (! IS_ABSOLUTE_PATH (filename))
2208 char *p = XCNEWVEC (char, len + strlen (filename));
2210 memcpy (p, dirname, len - 2);
2211 p[len - 2] = '/';
2212 strcpy (p + len - 1, filename);
2213 *canon_source_file = p;
2216 free (dirname);
2217 return filename;