2014-07-03 Tobias Burnus <burnus@net-b.de>
[official-gcc.git] / gcc / fortran / scanner.c
blobadb926a65e711106c56decf87bfadea6ba0eb38f
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 \"%s\": %s", path,
328 xstrerror(errno));
329 else
331 /* FIXME: Also support -Wmissing-include-dirs. */
332 if (warn)
333 gfc_warning_now ("Nonexistent include directory \"%s\"", path);
335 return;
337 else if (!S_ISDIR (st.st_mode))
339 gfc_warning_now ("\"%s\" 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;
715 /* Comment lines are null lines, lines containing only blanks or lines
716 on which the first nonblank line is a '!'.
717 Return true if !$ openmp conditional compilation sentinel was
718 seen. */
720 static bool
721 skip_free_comments (void)
723 locus start;
724 gfc_char_t c;
725 int at_bol;
727 for (;;)
729 at_bol = gfc_at_bol ();
730 start = gfc_current_locus;
731 if (gfc_at_eof ())
732 break;
735 c = next_char ();
736 while (gfc_is_whitespace (c));
738 if (c == '\n')
740 gfc_advance_line ();
741 continue;
744 if (c == '!')
746 /* Keep the !GCC$ line. */
747 if (at_bol && skip_gcc_attribute (start))
748 return false;
750 /* If -fopenmp, we need to handle here 2 things:
751 1) don't treat !$omp as comments, but directives
752 2) handle OpenMP conditional compilation, where
753 !$ should be treated as 2 spaces (for initial lines
754 only if followed by space). */
755 if ((gfc_option.gfc_flag_openmp
756 || gfc_option.gfc_flag_openmp_simd) && at_bol)
758 locus old_loc = gfc_current_locus;
759 if (next_char () == '$')
761 c = next_char ();
762 if (c == 'o' || c == 'O')
764 if (((c = next_char ()) == 'm' || c == 'M')
765 && ((c = next_char ()) == 'p' || c == 'P'))
767 if ((c = next_char ()) == ' ' || c == '\t'
768 || continue_flag)
770 while (gfc_is_whitespace (c))
771 c = next_char ();
772 if (c != '\n' && c != '!')
774 openmp_flag = 1;
775 openmp_locus = old_loc;
776 gfc_current_locus = start;
777 return false;
780 else
781 gfc_warning_now ("!$OMP at %C starts a commented "
782 "line as it neither is followed "
783 "by a space nor is a "
784 "continuation line");
786 gfc_current_locus = old_loc;
787 next_char ();
788 c = next_char ();
790 if (continue_flag || c == ' ' || c == '\t')
792 gfc_current_locus = old_loc;
793 next_char ();
794 openmp_flag = 0;
795 return true;
798 gfc_current_locus = old_loc;
800 skip_comment_line ();
801 continue;
804 break;
807 if (openmp_flag && at_bol)
808 openmp_flag = 0;
810 gcc_attribute_flag = 0;
811 gfc_current_locus = start;
812 return false;
816 /* Skip comment lines in fixed source mode. We have the same rules as
817 in skip_free_comment(), except that we can have a 'c', 'C' or '*'
818 in column 1, and a '!' cannot be in column 6. Also, we deal with
819 lines with 'd' or 'D' in column 1, if the user requested this. */
821 static void
822 skip_fixed_comments (void)
824 locus start;
825 int col;
826 gfc_char_t c;
828 if (! gfc_at_bol ())
830 start = gfc_current_locus;
831 if (! gfc_at_eof ())
834 c = next_char ();
835 while (gfc_is_whitespace (c));
837 if (c == '\n')
838 gfc_advance_line ();
839 else if (c == '!')
840 skip_comment_line ();
843 if (! gfc_at_bol ())
845 gfc_current_locus = start;
846 return;
850 for (;;)
852 start = gfc_current_locus;
853 if (gfc_at_eof ())
854 break;
856 c = next_char ();
857 if (c == '\n')
859 gfc_advance_line ();
860 continue;
863 if (c == '!' || c == 'c' || c == 'C' || c == '*')
865 if (skip_gcc_attribute (start))
867 /* Canonicalize to *$omp. */
868 *start.nextc = '*';
869 return;
872 /* If -fopenmp, we need to handle here 2 things:
873 1) don't treat !$omp|c$omp|*$omp as comments, but directives
874 2) handle OpenMP conditional compilation, where
875 !$|c$|*$ should be treated as 2 spaces if the characters
876 in columns 3 to 6 are valid fixed form label columns
877 characters. */
878 if (gfc_current_locus.lb != NULL
879 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
880 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
882 if (gfc_option.gfc_flag_openmp || gfc_option.gfc_flag_openmp_simd)
884 if (next_char () == '$')
886 c = next_char ();
887 if (c == 'o' || c == 'O')
889 if (((c = next_char ()) == 'm' || c == 'M')
890 && ((c = next_char ()) == 'p' || c == 'P'))
892 c = next_char ();
893 if (c != '\n'
894 && ((openmp_flag && continue_flag)
895 || c == ' ' || c == '\t' || c == '0'))
898 c = next_char ();
899 while (gfc_is_whitespace (c));
900 if (c != '\n' && c != '!')
902 /* Canonicalize to *$omp. */
903 *start.nextc = '*';
904 openmp_flag = 1;
905 gfc_current_locus = start;
906 return;
911 else
913 int digit_seen = 0;
915 for (col = 3; col < 6; col++, c = next_char ())
916 if (c == ' ')
917 continue;
918 else if (c == '\t')
920 col = 6;
921 break;
923 else if (c < '0' || c > '9')
924 break;
925 else
926 digit_seen = 1;
928 if (col == 6 && c != '\n'
929 && ((continue_flag && !digit_seen)
930 || c == ' ' || c == '\t' || c == '0'))
932 gfc_current_locus = start;
933 start.nextc[0] = ' ';
934 start.nextc[1] = ' ';
935 continue;
939 gfc_current_locus = start;
941 skip_comment_line ();
942 continue;
945 if (gfc_option.flag_d_lines != -1 && (c == 'd' || c == 'D'))
947 if (gfc_option.flag_d_lines == 0)
949 skip_comment_line ();
950 continue;
952 else
953 *start.nextc = c = ' ';
956 col = 1;
958 while (gfc_is_whitespace (c))
960 c = next_char ();
961 col++;
964 if (c == '\n')
966 gfc_advance_line ();
967 continue;
970 if (col != 6 && c == '!')
972 if (gfc_current_locus.lb != NULL
973 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
974 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
975 skip_comment_line ();
976 continue;
979 break;
982 openmp_flag = 0;
983 gcc_attribute_flag = 0;
984 gfc_current_locus = start;
988 /* Skips the current line if it is a comment. */
990 void
991 gfc_skip_comments (void)
993 if (gfc_current_form == FORM_FREE)
994 skip_free_comments ();
995 else
996 skip_fixed_comments ();
1000 /* Get the next character from the input, taking continuation lines
1001 and end-of-line comments into account. This implies that comment
1002 lines between continued lines must be eaten here. For higher-level
1003 subroutines, this flattens continued lines into a single logical
1004 line. The in_string flag denotes whether we're inside a character
1005 context or not. */
1007 gfc_char_t
1008 gfc_next_char_literal (gfc_instring in_string)
1010 locus old_loc;
1011 int i, prev_openmp_flag;
1012 gfc_char_t c;
1014 continue_flag = 0;
1016 restart:
1017 c = next_char ();
1018 if (gfc_at_end ())
1020 continue_count = 0;
1021 return c;
1024 if (gfc_current_form == FORM_FREE)
1026 bool openmp_cond_flag;
1028 if (!in_string && c == '!')
1030 if (gcc_attribute_flag
1031 && memcmp (&gfc_current_locus, &gcc_attribute_locus,
1032 sizeof (gfc_current_locus)) == 0)
1033 goto done;
1035 if (openmp_flag
1036 && memcmp (&gfc_current_locus, &openmp_locus,
1037 sizeof (gfc_current_locus)) == 0)
1038 goto done;
1040 /* This line can't be continued */
1043 c = next_char ();
1045 while (c != '\n');
1047 /* Avoid truncation warnings for comment ending lines. */
1048 gfc_current_locus.lb->truncated = 0;
1050 goto done;
1053 /* Check to see if the continuation line was truncated. */
1054 if (gfc_option.warn_line_truncation && gfc_current_locus.lb != NULL
1055 && gfc_current_locus.lb->truncated)
1057 int maxlen = gfc_option.free_line_length;
1058 gfc_char_t *current_nextc = gfc_current_locus.nextc;
1060 gfc_current_locus.lb->truncated = 0;
1061 gfc_current_locus.nextc = gfc_current_locus.lb->line + maxlen;
1062 gfc_warning_now ("Line truncated at %L", &gfc_current_locus);
1063 gfc_current_locus.nextc = current_nextc;
1066 if (c != '&')
1067 goto done;
1069 /* If the next nonblank character is a ! or \n, we've got a
1070 continuation line. */
1071 old_loc = gfc_current_locus;
1073 c = next_char ();
1074 while (gfc_is_whitespace (c))
1075 c = next_char ();
1077 /* Character constants to be continued cannot have commentary
1078 after the '&'. */
1080 if (in_string && c != '\n')
1082 gfc_current_locus = old_loc;
1083 c = '&';
1084 goto done;
1087 if (c != '!' && c != '\n')
1089 gfc_current_locus = old_loc;
1090 c = '&';
1091 goto done;
1094 prev_openmp_flag = openmp_flag;
1095 continue_flag = 1;
1096 if (c == '!')
1097 skip_comment_line ();
1098 else
1099 gfc_advance_line ();
1101 if (gfc_at_eof ())
1102 goto not_continuation;
1104 /* We've got a continuation line. If we are on the very next line after
1105 the last continuation, increment the continuation line count and
1106 check whether the limit has been exceeded. */
1107 if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1)
1109 if (++continue_count == gfc_option.max_continue_free)
1111 if (gfc_notification_std (GFC_STD_GNU) || pedantic)
1112 gfc_warning ("Limit of %d continuations exceeded in "
1113 "statement at %C", gfc_option.max_continue_free);
1117 /* Now find where it continues. First eat any comment lines. */
1118 openmp_cond_flag = skip_free_comments ();
1120 if (gfc_current_locus.lb != NULL
1121 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
1122 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
1124 if (prev_openmp_flag != openmp_flag)
1126 gfc_current_locus = old_loc;
1127 openmp_flag = prev_openmp_flag;
1128 c = '&';
1129 goto done;
1132 /* Now that we have a non-comment line, probe ahead for the
1133 first non-whitespace character. If it is another '&', then
1134 reading starts at the next character, otherwise we must back
1135 up to where the whitespace started and resume from there. */
1137 old_loc = gfc_current_locus;
1139 c = next_char ();
1140 while (gfc_is_whitespace (c))
1141 c = next_char ();
1143 if (openmp_flag)
1145 for (i = 0; i < 5; i++, c = next_char ())
1147 gcc_assert (gfc_wide_tolower (c) == (unsigned char) "!$omp"[i]);
1148 if (i == 4)
1149 old_loc = gfc_current_locus;
1151 while (gfc_is_whitespace (c))
1152 c = next_char ();
1155 if (c != '&')
1157 if (in_string)
1159 gfc_current_locus.nextc--;
1160 if (gfc_option.warn_ampersand && in_string == INSTRING_WARN)
1161 gfc_warning ("Missing '&' in continued character "
1162 "constant at %C");
1164 /* Both !$omp and !$ -fopenmp continuation lines have & on the
1165 continuation line only optionally. */
1166 else if (openmp_flag || openmp_cond_flag)
1167 gfc_current_locus.nextc--;
1168 else
1170 c = ' ';
1171 gfc_current_locus = old_loc;
1172 goto done;
1176 else /* Fixed form. */
1178 /* Fixed form continuation. */
1179 if (!in_string && c == '!')
1181 /* Skip comment at end of line. */
1184 c = next_char ();
1186 while (c != '\n');
1188 /* Avoid truncation warnings for comment ending lines. */
1189 gfc_current_locus.lb->truncated = 0;
1192 if (c != '\n')
1193 goto done;
1195 /* Check to see if the continuation line was truncated. */
1196 if (gfc_option.warn_line_truncation && gfc_current_locus.lb != NULL
1197 && gfc_current_locus.lb->truncated)
1199 gfc_current_locus.lb->truncated = 0;
1200 gfc_warning_now ("Line truncated at %L", &gfc_current_locus);
1203 prev_openmp_flag = openmp_flag;
1204 continue_flag = 1;
1205 old_loc = gfc_current_locus;
1207 gfc_advance_line ();
1208 skip_fixed_comments ();
1210 /* See if this line is a continuation line. */
1211 if (openmp_flag != prev_openmp_flag)
1213 openmp_flag = prev_openmp_flag;
1214 goto not_continuation;
1217 if (!openmp_flag)
1218 for (i = 0; i < 5; i++)
1220 c = next_char ();
1221 if (c != ' ')
1222 goto not_continuation;
1224 else
1225 for (i = 0; i < 5; i++)
1227 c = next_char ();
1228 if (gfc_wide_tolower (c) != (unsigned char) "*$omp"[i])
1229 goto not_continuation;
1232 c = next_char ();
1233 if (c == '0' || c == ' ' || c == '\n')
1234 goto not_continuation;
1236 /* We've got a continuation line. If we are on the very next line after
1237 the last continuation, increment the continuation line count and
1238 check whether the limit has been exceeded. */
1239 if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1)
1241 if (++continue_count == gfc_option.max_continue_fixed)
1243 if (gfc_notification_std (GFC_STD_GNU) || pedantic)
1244 gfc_warning ("Limit of %d continuations exceeded in "
1245 "statement at %C",
1246 gfc_option.max_continue_fixed);
1250 if (gfc_current_locus.lb != NULL
1251 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
1252 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
1255 /* Ready to read first character of continuation line, which might
1256 be another continuation line! */
1257 goto restart;
1259 not_continuation:
1260 c = '\n';
1261 gfc_current_locus = old_loc;
1263 done:
1264 if (c == '\n')
1265 continue_count = 0;
1266 continue_flag = 0;
1267 return c;
1271 /* Get the next character of input, folded to lowercase. In fixed
1272 form mode, we also ignore spaces. When matcher subroutines are
1273 parsing character literals, they have to call
1274 gfc_next_char_literal(). */
1276 gfc_char_t
1277 gfc_next_char (void)
1279 gfc_char_t c;
1283 c = gfc_next_char_literal (NONSTRING);
1285 while (gfc_current_form == FORM_FIXED && gfc_is_whitespace (c));
1287 return gfc_wide_tolower (c);
1290 char
1291 gfc_next_ascii_char (void)
1293 gfc_char_t c = gfc_next_char ();
1295 return (gfc_wide_fits_in_byte (c) ? (unsigned char) c
1296 : (unsigned char) UCHAR_MAX);
1300 gfc_char_t
1301 gfc_peek_char (void)
1303 locus old_loc;
1304 gfc_char_t c;
1306 old_loc = gfc_current_locus;
1307 c = gfc_next_char ();
1308 gfc_current_locus = old_loc;
1310 return c;
1314 char
1315 gfc_peek_ascii_char (void)
1317 gfc_char_t c = gfc_peek_char ();
1319 return (gfc_wide_fits_in_byte (c) ? (unsigned char) c
1320 : (unsigned char) UCHAR_MAX);
1324 /* Recover from an error. We try to get past the current statement
1325 and get lined up for the next. The next statement follows a '\n'
1326 or a ';'. We also assume that we are not within a character
1327 constant, and deal with finding a '\'' or '"'. */
1329 void
1330 gfc_error_recovery (void)
1332 gfc_char_t c, delim;
1334 if (gfc_at_eof ())
1335 return;
1337 for (;;)
1339 c = gfc_next_char ();
1340 if (c == '\n' || c == ';')
1341 break;
1343 if (c != '\'' && c != '"')
1345 if (gfc_at_eof ())
1346 break;
1347 continue;
1349 delim = c;
1351 for (;;)
1353 c = next_char ();
1355 if (c == delim)
1356 break;
1357 if (c == '\n')
1358 return;
1359 if (c == '\\')
1361 c = next_char ();
1362 if (c == '\n')
1363 return;
1366 if (gfc_at_eof ())
1367 break;
1372 /* Read ahead until the next character to be read is not whitespace. */
1374 void
1375 gfc_gobble_whitespace (void)
1377 static int linenum = 0;
1378 locus old_loc;
1379 gfc_char_t c;
1383 old_loc = gfc_current_locus;
1384 c = gfc_next_char_literal (NONSTRING);
1385 /* Issue a warning for nonconforming tabs. We keep track of the line
1386 number because the Fortran matchers will often back up and the same
1387 line will be scanned multiple times. */
1388 if (!gfc_option.warn_tabs && c == '\t')
1390 int cur_linenum = LOCATION_LINE (gfc_current_locus.lb->location);
1391 if (cur_linenum != linenum)
1393 linenum = cur_linenum;
1394 gfc_warning_now ("Nonconforming tab character at %C");
1398 while (gfc_is_whitespace (c));
1400 gfc_current_locus = old_loc;
1404 /* Load a single line into pbuf.
1406 If pbuf points to a NULL pointer, it is allocated.
1407 We truncate lines that are too long, unless we're dealing with
1408 preprocessor lines or if the option -ffixed-line-length-none is set,
1409 in which case we reallocate the buffer to fit the entire line, if
1410 need be.
1411 In fixed mode, we expand a tab that occurs within the statement
1412 label region to expand to spaces that leave the next character in
1413 the source region.
1415 If first_char is not NULL, it's a pointer to a single char value holding
1416 the first character of the line, which has already been read by the
1417 caller. This avoids the use of ungetc().
1419 load_line returns whether the line was truncated.
1421 NOTE: The error machinery isn't available at this point, so we can't
1422 easily report line and column numbers consistent with other
1423 parts of gfortran. */
1425 static int
1426 load_line (FILE *input, gfc_char_t **pbuf, int *pbuflen, const int *first_char)
1428 static int linenum = 0, current_line = 1;
1429 int c, maxlen, i, preprocessor_flag, buflen = *pbuflen;
1430 int trunc_flag = 0, seen_comment = 0;
1431 int seen_printable = 0, seen_ampersand = 0, quoted = ' ';
1432 gfc_char_t *buffer;
1433 bool found_tab = false;
1435 /* Determine the maximum allowed line length. */
1436 if (gfc_current_form == FORM_FREE)
1437 maxlen = gfc_option.free_line_length;
1438 else if (gfc_current_form == FORM_FIXED)
1439 maxlen = gfc_option.fixed_line_length;
1440 else
1441 maxlen = 72;
1443 if (*pbuf == NULL)
1445 /* Allocate the line buffer, storing its length into buflen.
1446 Note that if maxlen==0, indicating that arbitrary-length lines
1447 are allowed, the buffer will be reallocated if this length is
1448 insufficient; since 132 characters is the length of a standard
1449 free-form line, we use that as a starting guess. */
1450 if (maxlen > 0)
1451 buflen = maxlen;
1452 else
1453 buflen = 132;
1455 *pbuf = gfc_get_wide_string (buflen + 1);
1458 i = 0;
1459 buffer = *pbuf;
1461 if (first_char)
1462 c = *first_char;
1463 else
1464 c = getc (input);
1466 /* In order to not truncate preprocessor lines, we have to
1467 remember that this is one. */
1468 preprocessor_flag = (c == '#' ? 1 : 0);
1470 for (;;)
1472 if (c == EOF)
1473 break;
1475 if (c == '\n')
1477 /* Check for illegal use of ampersand. See F95 Standard 3.3.1.3. */
1478 if (gfc_current_form == FORM_FREE
1479 && !seen_printable && seen_ampersand)
1481 if (pedantic)
1482 gfc_error_now ("'&' not allowed by itself in line %d",
1483 current_line);
1484 else
1485 gfc_warning_now ("'&' not allowed by itself in line %d",
1486 current_line);
1488 break;
1491 if (c == '\r' || c == '\0')
1492 goto next_char; /* Gobble characters. */
1494 if (c == '&')
1496 if (seen_ampersand)
1498 seen_ampersand = 0;
1499 seen_printable = 1;
1501 else
1502 seen_ampersand = 1;
1505 if ((c != '&' && c != '!' && c != ' ') || (c == '!' && !seen_ampersand))
1506 seen_printable = 1;
1508 /* Is this a fixed-form comment? */
1509 if (gfc_current_form == FORM_FIXED && i == 0
1510 && (c == '*' || c == 'c' || c == 'd'))
1511 seen_comment = 1;
1513 if (quoted == ' ')
1515 if (c == '\'' || c == '"')
1516 quoted = c;
1518 else if (c == quoted)
1519 quoted = ' ';
1521 /* Is this a free-form comment? */
1522 if (c == '!' && quoted == ' ')
1523 seen_comment = 1;
1525 /* Vendor extension: "<tab>1" marks a continuation line. */
1526 if (found_tab)
1528 found_tab = false;
1529 if (c >= '1' && c <= '9')
1531 *(buffer-1) = c;
1532 goto next_char;
1536 if (gfc_current_form == FORM_FIXED && c == '\t' && i < 6)
1538 found_tab = true;
1540 if (!gfc_option.warn_tabs && seen_comment == 0
1541 && current_line != linenum)
1543 linenum = current_line;
1544 gfc_warning_now ("Nonconforming tab character in column %d "
1545 "of line %d", i+1, linenum);
1548 while (i < 6)
1550 *buffer++ = ' ';
1551 i++;
1554 goto next_char;
1557 *buffer++ = c;
1558 i++;
1560 if (maxlen == 0 || preprocessor_flag)
1562 if (i >= buflen)
1564 /* Reallocate line buffer to double size to hold the
1565 overlong line. */
1566 buflen = buflen * 2;
1567 *pbuf = XRESIZEVEC (gfc_char_t, *pbuf, (buflen + 1));
1568 buffer = (*pbuf) + i;
1571 else if (i >= maxlen)
1573 bool trunc_warn = true;
1575 /* Enhancement, if the very next non-space character is an ampersand
1576 or comment that we would otherwise warn about, don't mark as
1577 truncated. */
1579 /* Truncate the rest of the line. */
1580 for (;;)
1582 c = getc (input);
1583 if (c == '\r' || c == ' ')
1584 continue;
1586 if (c == '\n' || c == EOF)
1587 break;
1589 if (!trunc_warn && c != '!')
1590 trunc_warn = true;
1592 if (trunc_warn && ((gfc_current_form == FORM_FIXED && c == '&')
1593 || c == '!'))
1594 trunc_warn = false;
1596 if (c == '!')
1597 seen_comment = 1;
1599 if (trunc_warn && !seen_comment)
1600 trunc_flag = 1;
1603 c = '\n';
1604 continue;
1607 next_char:
1608 c = getc (input);
1611 /* Pad lines to the selected line length in fixed form. */
1612 if (gfc_current_form == FORM_FIXED
1613 && gfc_option.fixed_line_length != 0
1614 && !preprocessor_flag
1615 && c != EOF)
1617 while (i++ < maxlen)
1618 *buffer++ = ' ';
1621 *buffer = '\0';
1622 *pbuflen = buflen;
1623 current_line++;
1625 return trunc_flag;
1629 /* Get a gfc_file structure, initialize it and add it to
1630 the file stack. */
1632 static gfc_file *
1633 get_file (const char *name, enum lc_reason reason ATTRIBUTE_UNUSED)
1635 gfc_file *f;
1637 f = XCNEW (gfc_file);
1639 f->filename = xstrdup (name);
1641 f->next = file_head;
1642 file_head = f;
1644 f->up = current_file;
1645 if (current_file != NULL)
1646 f->inclusion_line = current_file->line;
1648 linemap_add (line_table, reason, false, f->filename, 1);
1650 return f;
1654 /* Deal with a line from the C preprocessor. The
1655 initial octothorp has already been seen. */
1657 static void
1658 preprocessor_line (gfc_char_t *c)
1660 bool flag[5];
1661 int i, line;
1662 gfc_char_t *wide_filename;
1663 gfc_file *f;
1664 int escaped, unescape;
1665 char *filename;
1667 c++;
1668 while (*c == ' ' || *c == '\t')
1669 c++;
1671 if (*c < '0' || *c > '9')
1672 goto bad_cpp_line;
1674 line = wide_atoi (c);
1676 c = wide_strchr (c, ' ');
1677 if (c == NULL)
1679 /* No file name given. Set new line number. */
1680 current_file->line = line;
1681 return;
1684 /* Skip spaces. */
1685 while (*c == ' ' || *c == '\t')
1686 c++;
1688 /* Skip quote. */
1689 if (*c != '"')
1690 goto bad_cpp_line;
1691 ++c;
1693 wide_filename = c;
1695 /* Make filename end at quote. */
1696 unescape = 0;
1697 escaped = false;
1698 while (*c && ! (!escaped && *c == '"'))
1700 if (escaped)
1701 escaped = false;
1702 else if (*c == '\\')
1704 escaped = true;
1705 unescape++;
1707 ++c;
1710 if (! *c)
1711 /* Preprocessor line has no closing quote. */
1712 goto bad_cpp_line;
1714 *c++ = '\0';
1716 /* Undo effects of cpp_quote_string. */
1717 if (unescape)
1719 gfc_char_t *s = wide_filename;
1720 gfc_char_t *d = gfc_get_wide_string (c - wide_filename - unescape);
1722 wide_filename = d;
1723 while (*s)
1725 if (*s == '\\')
1726 *d++ = *++s;
1727 else
1728 *d++ = *s;
1729 s++;
1731 *d = '\0';
1734 /* Get flags. */
1736 flag[1] = flag[2] = flag[3] = flag[4] = false;
1738 for (;;)
1740 c = wide_strchr (c, ' ');
1741 if (c == NULL)
1742 break;
1744 c++;
1745 i = wide_atoi (c);
1747 if (1 <= i && i <= 4)
1748 flag[i] = true;
1751 /* Convert the filename in wide characters into a filename in narrow
1752 characters. */
1753 filename = gfc_widechar_to_char (wide_filename, -1);
1755 /* Interpret flags. */
1757 if (flag[1]) /* Starting new file. */
1759 f = get_file (filename, LC_RENAME);
1760 add_file_change (f->filename, f->inclusion_line);
1761 current_file = f;
1764 if (flag[2]) /* Ending current file. */
1766 if (!current_file->up
1767 || filename_cmp (current_file->up->filename, filename) != 0)
1769 gfc_warning_now ("%s:%d: file %s left but not entered",
1770 current_file->filename, current_file->line,
1771 filename);
1772 if (unescape)
1773 free (wide_filename);
1774 free (filename);
1775 return;
1778 add_file_change (NULL, line);
1779 current_file = current_file->up;
1780 linemap_add (line_table, LC_RENAME, false, current_file->filename,
1781 current_file->line);
1784 /* The name of the file can be a temporary file produced by
1785 cpp. Replace the name if it is different. */
1787 if (filename_cmp (current_file->filename, filename) != 0)
1789 /* FIXME: we leak the old filename because a pointer to it may be stored
1790 in the linemap. Alternative could be using GC or updating linemap to
1791 point to the new name, but there is no API for that currently. */
1792 current_file->filename = xstrdup (filename);
1795 /* Set new line number. */
1796 current_file->line = line;
1797 if (unescape)
1798 free (wide_filename);
1799 free (filename);
1800 return;
1802 bad_cpp_line:
1803 gfc_warning_now ("%s:%d: Illegal preprocessor directive",
1804 current_file->filename, current_file->line);
1805 current_file->line++;
1809 static bool load_file (const char *, const char *, bool);
1811 /* include_line()-- Checks a line buffer to see if it is an include
1812 line. If so, we call load_file() recursively to load the included
1813 file. We never return a syntax error because a statement like
1814 "include = 5" is perfectly legal. We return false if no include was
1815 processed or true if we matched an include. */
1817 static bool
1818 include_line (gfc_char_t *line)
1820 gfc_char_t quote, *c, *begin, *stop;
1821 char *filename;
1823 c = line;
1825 if (gfc_option.gfc_flag_openmp || gfc_option.gfc_flag_openmp_simd)
1827 if (gfc_current_form == FORM_FREE)
1829 while (*c == ' ' || *c == '\t')
1830 c++;
1831 if (*c == '!' && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
1832 c += 3;
1834 else
1836 if ((*c == '!' || *c == 'c' || *c == 'C' || *c == '*')
1837 && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
1838 c += 3;
1842 while (*c == ' ' || *c == '\t')
1843 c++;
1845 if (gfc_wide_strncasecmp (c, "include", 7))
1846 return false;
1848 c += 7;
1849 while (*c == ' ' || *c == '\t')
1850 c++;
1852 /* Find filename between quotes. */
1854 quote = *c++;
1855 if (quote != '"' && quote != '\'')
1856 return false;
1858 begin = c;
1860 while (*c != quote && *c != '\0')
1861 c++;
1863 if (*c == '\0')
1864 return false;
1866 stop = c++;
1868 while (*c == ' ' || *c == '\t')
1869 c++;
1871 if (*c != '\0' && *c != '!')
1872 return false;
1874 /* We have an include line at this point. */
1876 *stop = '\0'; /* It's ok to trash the buffer, as this line won't be
1877 read by anything else. */
1879 filename = gfc_widechar_to_char (begin, -1);
1880 if (!load_file (filename, NULL, false))
1881 exit (FATAL_EXIT_CODE);
1883 free (filename);
1884 return true;
1888 /* Load a file into memory by calling load_line until the file ends. */
1890 static bool
1891 load_file (const char *realfilename, const char *displayedname, bool initial)
1893 gfc_char_t *line;
1894 gfc_linebuf *b;
1895 gfc_file *f;
1896 FILE *input;
1897 int len, line_len;
1898 bool first_line;
1899 const char *filename;
1900 /* If realfilename and displayedname are different and non-null then
1901 surely realfilename is the preprocessed form of
1902 displayedname. */
1903 bool preprocessed_p = (realfilename && displayedname
1904 && strcmp (realfilename, displayedname));
1906 filename = displayedname ? displayedname : realfilename;
1908 for (f = current_file; f; f = f->up)
1909 if (filename_cmp (filename, f->filename) == 0)
1911 fprintf (stderr, "%s:%d: Error: File '%s' is being included "
1912 "recursively\n", current_file->filename, current_file->line,
1913 filename);
1914 return false;
1917 if (initial)
1919 if (gfc_src_file)
1921 input = gfc_src_file;
1922 gfc_src_file = NULL;
1924 else
1925 input = gfc_open_file (realfilename);
1926 if (input == NULL)
1928 gfc_error_now ("Can't open file '%s'", filename);
1929 return false;
1932 else
1934 input = gfc_open_included_file (realfilename, false, false);
1935 if (input == NULL)
1937 fprintf (stderr, "%s:%d: Error: Can't open included file '%s'\n",
1938 current_file->filename, current_file->line, filename);
1939 return false;
1943 /* Load the file.
1945 A "non-initial" file means a file that is being included. In
1946 that case we are creating an LC_ENTER map.
1948 An "initial" file means a main file; one that is not included.
1949 That file has already got at least one (surely more) line map(s)
1950 created by gfc_init. So the subsequent map created in that case
1951 must have LC_RENAME reason.
1953 This latter case is not true for a preprocessed file. In that
1954 case, although the file is "initial", the line maps created by
1955 gfc_init was used during the preprocessing of the file. Now that
1956 the preprocessing is over and we are being fed the result of that
1957 preprocessing, we need to create a brand new line map for the
1958 preprocessed file, so the reason is going to be LC_ENTER. */
1960 f = get_file (filename, (initial && !preprocessed_p) ? LC_RENAME : LC_ENTER);
1961 if (!initial)
1962 add_file_change (f->filename, f->inclusion_line);
1963 current_file = f;
1964 current_file->line = 1;
1965 line = NULL;
1966 line_len = 0;
1967 first_line = true;
1969 if (initial && gfc_src_preprocessor_lines[0])
1971 preprocessor_line (gfc_src_preprocessor_lines[0]);
1972 free (gfc_src_preprocessor_lines[0]);
1973 gfc_src_preprocessor_lines[0] = NULL;
1974 if (gfc_src_preprocessor_lines[1])
1976 preprocessor_line (gfc_src_preprocessor_lines[1]);
1977 free (gfc_src_preprocessor_lines[1]);
1978 gfc_src_preprocessor_lines[1] = NULL;
1982 for (;;)
1984 int trunc = load_line (input, &line, &line_len, NULL);
1986 len = gfc_wide_strlen (line);
1987 if (feof (input) && len == 0)
1988 break;
1990 /* If this is the first line of the file, it can contain a byte
1991 order mark (BOM), which we will ignore:
1992 FF FE is UTF-16 little endian,
1993 FE FF is UTF-16 big endian,
1994 EF BB BF is UTF-8. */
1995 if (first_line
1996 && ((line_len >= 2 && line[0] == (unsigned char) '\xFF'
1997 && line[1] == (unsigned char) '\xFE')
1998 || (line_len >= 2 && line[0] == (unsigned char) '\xFE'
1999 && line[1] == (unsigned char) '\xFF')
2000 || (line_len >= 3 && line[0] == (unsigned char) '\xEF'
2001 && line[1] == (unsigned char) '\xBB'
2002 && line[2] == (unsigned char) '\xBF')))
2004 int n = line[1] == (unsigned char) '\xBB' ? 3 : 2;
2005 gfc_char_t *new_char = gfc_get_wide_string (line_len);
2007 wide_strcpy (new_char, &line[n]);
2008 free (line);
2009 line = new_char;
2010 len -= n;
2013 /* There are three things this line can be: a line of Fortran
2014 source, an include line or a C preprocessor directive. */
2016 if (line[0] == '#')
2018 /* When -g3 is specified, it's possible that we emit #define
2019 and #undef lines, which we need to pass to the middle-end
2020 so that it can emit correct debug info. */
2021 if (debug_info_level == DINFO_LEVEL_VERBOSE
2022 && (wide_strncmp (line, "#define ", 8) == 0
2023 || wide_strncmp (line, "#undef ", 7) == 0))
2025 else
2027 preprocessor_line (line);
2028 continue;
2032 /* Preprocessed files have preprocessor lines added before the byte
2033 order mark, so first_line is not about the first line of the file
2034 but the first line that's not a preprocessor line. */
2035 first_line = false;
2037 if (include_line (line))
2039 current_file->line++;
2040 continue;
2043 /* Add line. */
2045 b = XCNEWVAR (gfc_linebuf, gfc_linebuf_header_size
2046 + (len + 1) * sizeof (gfc_char_t));
2048 b->location
2049 = linemap_line_start (line_table, current_file->line++, 120);
2050 b->file = current_file;
2051 b->truncated = trunc;
2052 wide_strcpy (b->line, line);
2054 if (line_head == NULL)
2055 line_head = b;
2056 else
2057 line_tail->next = b;
2059 line_tail = b;
2061 while (file_changes_cur < file_changes_count)
2062 file_changes[file_changes_cur++].lb = b;
2065 /* Release the line buffer allocated in load_line. */
2066 free (line);
2068 fclose (input);
2070 if (!initial)
2071 add_file_change (NULL, current_file->inclusion_line + 1);
2072 current_file = current_file->up;
2073 linemap_add (line_table, LC_LEAVE, 0, NULL, 0);
2074 return true;
2078 /* Open a new file and start scanning from that file. Returns true
2079 if everything went OK, false otherwise. If form == FORM_UNKNOWN
2080 it tries to determine the source form from the filename, defaulting
2081 to free form. */
2083 bool
2084 gfc_new_file (void)
2086 bool result;
2088 if (gfc_cpp_enabled ())
2090 result = gfc_cpp_preprocess (gfc_source_file);
2091 if (!gfc_cpp_preprocess_only ())
2092 result = load_file (gfc_cpp_temporary_file (), gfc_source_file, true);
2094 else
2095 result = load_file (gfc_source_file, NULL, true);
2097 gfc_current_locus.lb = line_head;
2098 gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line;
2100 #if 0 /* Debugging aid. */
2101 for (; line_head; line_head = line_head->next)
2102 printf ("%s:%3d %s\n", LOCATION_FILE (line_head->location),
2103 LOCATION_LINE (line_head->location), line_head->line);
2105 exit (SUCCESS_EXIT_CODE);
2106 #endif
2108 return result;
2111 static char *
2112 unescape_filename (const char *ptr)
2114 const char *p = ptr, *s;
2115 char *d, *ret;
2116 int escaped, unescape = 0;
2118 /* Make filename end at quote. */
2119 escaped = false;
2120 while (*p && ! (! escaped && *p == '"'))
2122 if (escaped)
2123 escaped = false;
2124 else if (*p == '\\')
2126 escaped = true;
2127 unescape++;
2129 ++p;
2132 if (!*p || p[1])
2133 return NULL;
2135 /* Undo effects of cpp_quote_string. */
2136 s = ptr;
2137 d = XCNEWVEC (char, p + 1 - ptr - unescape);
2138 ret = d;
2140 while (s != p)
2142 if (*s == '\\')
2143 *d++ = *++s;
2144 else
2145 *d++ = *s;
2146 s++;
2148 *d = '\0';
2149 return ret;
2152 /* For preprocessed files, if the first tokens are of the form # NUM.
2153 handle the directives so we know the original file name. */
2155 const char *
2156 gfc_read_orig_filename (const char *filename, const char **canon_source_file)
2158 int c, len;
2159 char *dirname, *tmp;
2161 gfc_src_file = gfc_open_file (filename);
2162 if (gfc_src_file == NULL)
2163 return NULL;
2165 c = getc (gfc_src_file);
2167 if (c != '#')
2168 return NULL;
2170 len = 0;
2171 load_line (gfc_src_file, &gfc_src_preprocessor_lines[0], &len, &c);
2173 if (wide_strncmp (gfc_src_preprocessor_lines[0], "# 1 \"", 5) != 0)
2174 return NULL;
2176 tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[0][5], -1);
2177 filename = unescape_filename (tmp);
2178 free (tmp);
2179 if (filename == NULL)
2180 return NULL;
2182 c = getc (gfc_src_file);
2184 if (c != '#')
2185 return filename;
2187 len = 0;
2188 load_line (gfc_src_file, &gfc_src_preprocessor_lines[1], &len, &c);
2190 if (wide_strncmp (gfc_src_preprocessor_lines[1], "# 1 \"", 5) != 0)
2191 return filename;
2193 tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[1][5], -1);
2194 dirname = unescape_filename (tmp);
2195 free (tmp);
2196 if (dirname == NULL)
2197 return filename;
2199 len = strlen (dirname);
2200 if (len < 3 || dirname[len - 1] != '/' || dirname[len - 2] != '/')
2202 free (dirname);
2203 return filename;
2205 dirname[len - 2] = '\0';
2206 set_src_pwd (dirname);
2208 if (! IS_ABSOLUTE_PATH (filename))
2210 char *p = XCNEWVEC (char, len + strlen (filename));
2212 memcpy (p, dirname, len - 2);
2213 p[len - 2] = '/';
2214 strcpy (p + len - 1, filename);
2215 *canon_source_file = p;
2218 free (dirname);
2219 return filename;