2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 /* Set of subroutines to (ultimately) return the next character to the
24 various matching subroutines. This file's job is to read files and
25 build up lines that are parsed by the parser. This means that we
26 handle continuation lines and "include" lines.
28 The first thing the scanner does is to load an entire file into
29 memory. We load the entire file into memory for a couple reasons.
30 The first is that we want to be able to deal with nonseekable input
31 (pipes, stdin) and there is a lot of backing up involved during
34 The second is that we want to be able to print the locus of errors,
35 and an error on line 999999 could conflict with something on line
36 one. Given nonseekable input, we've got to store the whole thing.
38 One thing that helps are the column truncation limits that give us
39 an upper bound on the size of individual lines. We don't store the
42 From the scanner's viewpoint, the higher level subroutines ask for
43 new characters and do a lot of jumping backwards. */
50 /* Structure for holding module and include file search path. */
51 typedef struct gfc_directorylist
55 struct gfc_directorylist *next;
59 /* List of include file search directories. */
60 static gfc_directorylist *include_dirs, *intrinsic_modules_dirs;
62 static gfc_file *file_head, *current_file;
64 static int continue_flag, end_flag, openmp_flag;
65 static int continue_count, continue_line;
66 static locus openmp_locus;
68 gfc_source_form gfc_current_form;
69 static gfc_linebuf *line_head, *line_tail;
71 locus gfc_current_locus;
72 const char *gfc_source_file;
73 static FILE *gfc_src_file;
74 static char *gfc_src_preprocessor_lines[2];
78 /* Main scanner initialization. */
81 gfc_scanner_init_1 (void)
94 /* Main scanner destructor. */
97 gfc_scanner_done_1 (void)
102 while(line_head != NULL)
104 lb = line_head->next;
109 while(file_head != NULL)
112 gfc_free(file_head->filename);
119 /* Adds path to the list pointed to by list. */
122 add_path_to_list (gfc_directorylist **list, const char *path,
123 bool use_for_modules)
125 gfc_directorylist *dir;
129 while (*p == ' ' || *p == '\t') /* someone might do "-I include" */
135 dir = *list = gfc_getmem (sizeof (gfc_directorylist));
141 dir->next = gfc_getmem (sizeof (gfc_directorylist));
146 dir->use_for_modules = use_for_modules;
147 dir->path = gfc_getmem (strlen (p) + 2);
148 strcpy (dir->path, p);
149 strcat (dir->path, "/"); /* make '/' last character */
154 gfc_add_include_path (const char *path, bool use_for_modules)
156 add_path_to_list (&include_dirs, path, use_for_modules);
161 gfc_add_intrinsic_modules_path (const char *path)
163 add_path_to_list (&intrinsic_modules_dirs, path, true);
167 /* Release resources allocated for options. */
170 gfc_release_include_path (void)
172 gfc_directorylist *p;
174 while (include_dirs != NULL)
177 include_dirs = include_dirs->next;
182 while (intrinsic_modules_dirs != NULL)
184 p = intrinsic_modules_dirs;
185 intrinsic_modules_dirs = intrinsic_modules_dirs->next;
190 gfc_free (gfc_option.module_dir);
195 open_included_file (const char *name, gfc_directorylist *list, bool module)
198 gfc_directorylist *p;
201 for (p = list; p; p = p->next)
203 if (module && !p->use_for_modules)
206 fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1);
207 strcpy (fullname, p->path);
208 strcat (fullname, name);
210 f = gfc_open_file (fullname);
219 /* Opens file for reading, searching through the include directories
220 given if necessary. If the include_cwd argument is true, we try
221 to open the file in the current directory first. */
224 gfc_open_included_file (const char *name, bool include_cwd, bool module)
228 if (IS_ABSOLUTE_PATH (name))
229 return gfc_open_file (name);
233 f = gfc_open_file (name);
238 return open_included_file (name, include_dirs, module);
242 gfc_open_intrinsic_module (const char *name)
244 if (IS_ABSOLUTE_PATH (name))
245 return gfc_open_file (name);
247 return open_included_file (name, intrinsic_modules_dirs, true);
251 /* Test to see if we're at the end of the main source file. */
260 /* Test to see if we're at the end of the current file. */
268 if (line_head == NULL)
269 return 1; /* Null file */
271 if (gfc_current_locus.lb == NULL)
278 /* Test to see if we're at the beginning of a new line. */
286 return (gfc_current_locus.nextc == gfc_current_locus.lb->line);
290 /* Test to see if we're at the end of a line. */
298 return (*gfc_current_locus.nextc == '\0');
302 /* Advance the current line pointer to the next line. */
305 gfc_advance_line (void)
310 if (gfc_current_locus.lb == NULL)
316 gfc_current_locus.lb = gfc_current_locus.lb->next;
318 if (gfc_current_locus.lb != NULL)
319 gfc_current_locus.nextc = gfc_current_locus.lb->line;
322 gfc_current_locus.nextc = NULL;
328 /* Get the next character from the input, advancing gfc_current_file's
329 locus. When we hit the end of the line or the end of the file, we
330 start returning a '\n' in order to complete the current statement.
331 No Fortran line conventions are implemented here.
333 Requiring explicit advances to the next line prevents the parse
334 pointer from being on the wrong line if the current statement ends
342 if (gfc_current_locus.nextc == NULL)
345 c = (unsigned char) *gfc_current_locus.nextc++;
348 gfc_current_locus.nextc--; /* Remain on this line. */
356 /* Skip a comment. When we come here the parse pointer is positioned
357 immediately after the comment character. If we ever implement
358 compiler directives withing comments, here is where we parse the
362 skip_comment_line (void)
376 /* Comment lines are null lines, lines containing only blanks or lines
377 on which the first nonblank line is a '!'.
378 Return true if !$ openmp conditional compilation sentinel was
382 skip_free_comments (void)
390 at_bol = gfc_at_bol ();
391 start = gfc_current_locus;
397 while (gfc_is_whitespace (c));
407 /* If -fopenmp, we need to handle here 2 things:
408 1) don't treat !$omp as comments, but directives
409 2) handle OpenMP conditional compilation, where
410 !$ should be treated as 2 spaces (for initial lines
411 only if followed by space). */
412 if (gfc_option.flag_openmp && at_bol)
414 locus old_loc = gfc_current_locus;
415 if (next_char () == '$')
418 if (c == 'o' || c == 'O')
420 if (((c = next_char ()) == 'm' || c == 'M')
421 && ((c = next_char ()) == 'p' || c == 'P')
422 && ((c = next_char ()) == ' ' || continue_flag))
424 while (gfc_is_whitespace (c))
426 if (c != '\n' && c != '!')
429 openmp_locus = old_loc;
430 gfc_current_locus = start;
434 gfc_current_locus = old_loc;
438 if (continue_flag || c == ' ')
440 gfc_current_locus = old_loc;
446 gfc_current_locus = old_loc;
448 skip_comment_line ();
455 if (openmp_flag && at_bol)
457 gfc_current_locus = start;
462 /* Skip comment lines in fixed source mode. We have the same rules as
463 in skip_free_comment(), except that we can have a 'c', 'C' or '*'
464 in column 1, and a '!' cannot be in column 6. Also, we deal with
465 lines with 'd' or 'D' in column 1, if the user requested this. */
468 skip_fixed_comments (void)
476 start = gfc_current_locus;
481 while (gfc_is_whitespace (c));
486 skip_comment_line ();
491 gfc_current_locus = start;
498 start = gfc_current_locus;
509 if (c == '!' || c == 'c' || c == 'C' || c == '*')
511 /* If -fopenmp, we need to handle here 2 things:
512 1) don't treat !$omp|c$omp|*$omp as comments, but directives
513 2) handle OpenMP conditional compilation, where
514 !$|c$|*$ should be treated as 2 spaces if the characters
515 in columns 3 to 6 are valid fixed form label columns
517 if (gfc_option.flag_openmp)
519 if (next_char () == '$')
522 if (c == 'o' || c == 'O')
524 if (((c = next_char ()) == 'm' || c == 'M')
525 && ((c = next_char ()) == 'p' || c == 'P'))
529 && ((openmp_flag && continue_flag)
530 || c == ' ' || c == '0'))
533 while (gfc_is_whitespace (c))
535 if (c != '\n' && c != '!')
537 /* Canonicalize to *$omp. */
540 gfc_current_locus = start;
550 for (col = 3; col < 6; col++, c = next_char ())
553 else if (c < '0' || c > '9')
558 if (col == 6 && c != '\n'
559 && ((continue_flag && !digit_seen)
560 || c == ' ' || c == '0'))
562 gfc_current_locus = start;
563 start.nextc[0] = ' ';
564 start.nextc[1] = ' ';
569 gfc_current_locus = start;
571 skip_comment_line ();
575 if (gfc_option.flag_d_lines != -1 && (c == 'd' || c == 'D'))
577 if (gfc_option.flag_d_lines == 0)
579 skip_comment_line ();
583 *start.nextc = c = ' ';
588 while (gfc_is_whitespace (c))
600 if (col != 6 && c == '!')
602 skip_comment_line ();
610 gfc_current_locus = start;
614 /* Skips the current line if it is a comment. */
617 gfc_skip_comments (void)
619 if (gfc_current_form == FORM_FREE)
620 skip_free_comments ();
622 skip_fixed_comments ();
626 /* Get the next character from the input, taking continuation lines
627 and end-of-line comments into account. This implies that comment
628 lines between continued lines must be eaten here. For higher-level
629 subroutines, this flattens continued lines into a single logical
630 line. The in_string flag denotes whether we're inside a character
634 gfc_next_char_literal (int in_string)
637 int i, c, prev_openmp_flag;
649 if (gfc_current_form == FORM_FREE)
651 bool openmp_cond_flag;
653 if (!in_string && c == '!')
656 && memcmp (&gfc_current_locus, &openmp_locus,
657 sizeof (gfc_current_locus)) == 0)
660 /* This line can't be continued */
667 /* Avoid truncation warnings for comment ending lines. */
668 gfc_current_locus.lb->truncated = 0;
676 /* If the next nonblank character is a ! or \n, we've got a
677 continuation line. */
678 old_loc = gfc_current_locus;
681 while (gfc_is_whitespace (c))
684 /* Character constants to be continued cannot have commentary
687 if (in_string && c != '\n')
689 gfc_current_locus = old_loc;
694 if (c != '!' && c != '\n')
696 gfc_current_locus = old_loc;
701 prev_openmp_flag = openmp_flag;
704 skip_comment_line ();
709 goto not_continuation;
711 /* We've got a continuation line. If we are on the very next line after
712 the last continuation, increment the continuation line count and
713 check whether the limit has been exceeded. */
714 if (gfc_current_locus.lb->linenum == continue_line + 1)
716 if (++continue_count == gfc_option.max_continue_free)
718 if (gfc_notification_std (GFC_STD_GNU) || pedantic)
719 gfc_warning ("Limit of %d continuations exceeded in "
720 "statement at %C", gfc_option.max_continue_free);
723 continue_line = gfc_current_locus.lb->linenum;
725 /* Now find where it continues. First eat any comment lines. */
726 openmp_cond_flag = skip_free_comments ();
728 if (prev_openmp_flag != openmp_flag)
730 gfc_current_locus = old_loc;
731 openmp_flag = prev_openmp_flag;
736 /* Now that we have a non-comment line, probe ahead for the
737 first non-whitespace character. If it is another '&', then
738 reading starts at the next character, otherwise we must back
739 up to where the whitespace started and resume from there. */
741 old_loc = gfc_current_locus;
744 while (gfc_is_whitespace (c))
749 for (i = 0; i < 5; i++, c = next_char ())
751 gcc_assert (TOLOWER (c) == "!$omp"[i]);
753 old_loc = gfc_current_locus;
755 while (gfc_is_whitespace (c))
763 if (gfc_option.warn_ampersand)
764 gfc_warning_now ("Missing '&' in continued character "
766 gfc_current_locus.nextc--;
768 /* Both !$omp and !$ -fopenmp continuation lines have & on the
769 continuation line only optionally. */
770 else if (openmp_flag || openmp_cond_flag)
771 gfc_current_locus.nextc--;
775 gfc_current_locus = old_loc;
782 /* Fixed form continuation. */
783 if (!in_string && c == '!')
785 /* Skip comment at end of line. */
792 /* Avoid truncation warnings for comment ending lines. */
793 gfc_current_locus.lb->truncated = 0;
799 prev_openmp_flag = openmp_flag;
801 old_loc = gfc_current_locus;
804 skip_fixed_comments ();
806 /* See if this line is a continuation line. */
807 if (openmp_flag != prev_openmp_flag)
809 openmp_flag = prev_openmp_flag;
810 goto not_continuation;
814 for (i = 0; i < 5; i++)
818 goto not_continuation;
821 for (i = 0; i < 5; i++)
824 if (TOLOWER (c) != "*$omp"[i])
825 goto not_continuation;
829 if (c == '0' || c == ' ' || c == '\n')
830 goto not_continuation;
832 /* We've got a continuation line. If we are on the very next line after
833 the last continuation, increment the continuation line count and
834 check whether the limit has been exceeded. */
835 if (gfc_current_locus.lb->linenum == continue_line + 1)
837 if (++continue_count == gfc_option.max_continue_fixed)
839 if (gfc_notification_std (GFC_STD_GNU) || pedantic)
840 gfc_warning ("Limit of %d continuations exceeded in "
842 gfc_option.max_continue_fixed);
846 if (continue_line < gfc_current_locus.lb->linenum)
847 continue_line = gfc_current_locus.lb->linenum;
850 /* Ready to read first character of continuation line, which might
851 be another continuation line! */
856 gfc_current_locus = old_loc;
866 /* Get the next character of input, folded to lowercase. In fixed
867 form mode, we also ignore spaces. When matcher subroutines are
868 parsing character literals, they have to call
869 gfc_next_char_literal(). */
878 c = gfc_next_char_literal (0);
880 while (gfc_current_form == FORM_FIXED && gfc_is_whitespace (c));
892 old_loc = gfc_current_locus;
893 c = gfc_next_char ();
894 gfc_current_locus = old_loc;
900 /* Recover from an error. We try to get past the current statement
901 and get lined up for the next. The next statement follows a '\n'
902 or a ';'. We also assume that we are not within a character
903 constant, and deal with finding a '\'' or '"'. */
906 gfc_error_recovery (void)
915 c = gfc_next_char ();
916 if (c == '\n' || c == ';')
919 if (c != '\'' && c != '"')
948 /* Read ahead until the next character to be read is not whitespace. */
951 gfc_gobble_whitespace (void)
953 static int linenum = 0;
959 old_loc = gfc_current_locus;
960 c = gfc_next_char_literal (0);
961 /* Issue a warning for nonconforming tabs. We keep track of the line
962 number because the Fortran matchers will often back up and the same
963 line will be scanned multiple times. */
964 if (!gfc_option.warn_tabs && c == '\t')
966 #ifdef USE_MAPPED_LOCATION
967 int cur_linenum = LOCATION_LINE (gfc_current_locus.lb->location);
969 int cur_linenum = gfc_current_locus.lb->linenum;
971 if (cur_linenum != linenum)
973 linenum = cur_linenum;
974 gfc_warning_now ("Nonconforming tab character at %C");
978 while (gfc_is_whitespace (c));
980 gfc_current_locus = old_loc;
984 /* Load a single line into pbuf.
986 If pbuf points to a NULL pointer, it is allocated.
987 We truncate lines that are too long, unless we're dealing with
988 preprocessor lines or if the option -ffixed-line-length-none is set,
989 in which case we reallocate the buffer to fit the entire line, if
991 In fixed mode, we expand a tab that occurs within the statement
992 label region to expand to spaces that leave the next character in
994 load_line returns whether the line was truncated.
996 NOTE: The error machinery isn't available at this point, so we can't
997 easily report line and column numbers consistent with other
998 parts of gfortran. */
1001 load_line (FILE *input, char **pbuf, int *pbuflen)
1003 static int linenum = 0, current_line = 1;
1004 int c, maxlen, i, preprocessor_flag, buflen = *pbuflen;
1005 int trunc_flag = 0, seen_comment = 0;
1006 int seen_printable = 0, seen_ampersand = 0;
1009 /* Determine the maximum allowed line length. */
1010 if (gfc_current_form == FORM_FREE)
1011 maxlen = gfc_option.free_line_length;
1012 else if (gfc_current_form == FORM_FIXED)
1013 maxlen = gfc_option.fixed_line_length;
1019 /* Allocate the line buffer, storing its length into buflen.
1020 Note that if maxlen==0, indicating that arbitrary-length lines
1021 are allowed, the buffer will be reallocated if this length is
1022 insufficient; since 132 characters is the length of a standard
1023 free-form line, we use that as a starting guess. */
1029 *pbuf = gfc_getmem (buflen + 1);
1035 preprocessor_flag = 0;
1038 /* In order to not truncate preprocessor lines, we have to
1039 remember that this is one. */
1040 preprocessor_flag = 1;
1051 /* Check for illegal use of ampersand. See F95 Standard 3.3.1.3. */
1052 if (gfc_current_form == FORM_FREE
1053 && !seen_printable && seen_ampersand)
1056 gfc_error_now ("'&' not allowed by itself in line %d",
1059 gfc_warning_now ("'&' not allowed by itself in line %d",
1066 continue; /* Gobble characters. */
1078 if ((c != '&' && c != '!') || (c == '!' && !seen_ampersand))
1081 /* Is this a fixed-form comment? */
1082 if (gfc_current_form == FORM_FIXED && i == 0
1083 && (c == '*' || c == 'c' || c == 'd'))
1086 if (gfc_current_form == FORM_FIXED && c == '\t' && i <= 6)
1088 if (!gfc_option.warn_tabs && seen_comment == 0
1089 && current_line != linenum)
1091 linenum = current_line;
1092 gfc_warning_now ("Nonconforming tab character in column 1 "
1093 "of line %d", linenum);
1108 if (maxlen == 0 || preprocessor_flag)
1112 /* Reallocate line buffer to double size to hold the
1114 buflen = buflen * 2;
1115 *pbuf = xrealloc (*pbuf, buflen + 1);
1116 buffer = (*pbuf) + i;
1119 else if (i >= maxlen)
1121 /* Truncate the rest of the line. */
1125 if (c == '\n' || c == EOF)
1131 ungetc ('\n', input);
1135 /* Pad lines to the selected line length in fixed form. */
1136 if (gfc_current_form == FORM_FIXED
1137 && gfc_option.fixed_line_length != 0
1138 && !preprocessor_flag
1141 while (i++ < maxlen)
1153 /* Get a gfc_file structure, initialize it and add it to
1157 get_file (const char *name, enum lc_reason reason ATTRIBUTE_UNUSED)
1161 f = gfc_getmem (sizeof (gfc_file));
1163 f->filename = gfc_getmem (strlen (name) + 1);
1164 strcpy (f->filename, name);
1166 f->next = file_head;
1169 f->included_by = current_file;
1170 if (current_file != NULL)
1171 f->inclusion_line = current_file->line;
1173 #ifdef USE_MAPPED_LOCATION
1174 linemap_add (&line_table, reason, false, f->filename, 1);
1180 /* Deal with a line from the C preprocessor. The
1181 initial octothorp has already been seen. */
1184 preprocessor_line (char *c)
1190 int escaped, unescape;
1193 while (*c == ' ' || *c == '\t')
1196 if (*c < '0' || *c > '9')
1201 c = strchr (c, ' ');
1204 /* No file name given. Set new line number. */
1205 current_file->line = line;
1210 while (*c == ' ' || *c == '\t')
1220 /* Make filename end at quote. */
1223 while (*c && ! (!escaped && *c == '"'))
1227 else if (*c == '\\')
1236 /* Preprocessor line has no closing quote. */
1241 /* Undo effects of cpp_quote_string. */
1245 char *d = gfc_getmem (c - filename - unescape);
1261 flag[1] = flag[2] = flag[3] = flag[4] = false;
1265 c = strchr (c, ' ');
1272 if (1 <= i && i <= 4)
1276 /* Interpret flags. */
1278 if (flag[1]) /* Starting new file. */
1280 f = get_file (filename, LC_RENAME);
1281 f->up = current_file;
1285 if (flag[2]) /* Ending current file. */
1287 if (!current_file->up
1288 || strcmp (current_file->up->filename, filename) != 0)
1290 gfc_warning_now ("%s:%d: file %s left but not entered",
1291 current_file->filename, current_file->line,
1294 gfc_free (filename);
1297 current_file = current_file->up;
1300 /* The name of the file can be a temporary file produced by
1301 cpp. Replace the name if it is different. */
1303 if (strcmp (current_file->filename, filename) != 0)
1305 gfc_free (current_file->filename);
1306 current_file->filename = gfc_getmem (strlen (filename) + 1);
1307 strcpy (current_file->filename, filename);
1310 /* Set new line number. */
1311 current_file->line = line;
1313 gfc_free (filename);
1317 gfc_warning_now ("%s:%d: Illegal preprocessor directive",
1318 current_file->filename, current_file->line);
1319 current_file->line++;
1323 static try load_file (const char *, bool);
1325 /* include_line()-- Checks a line buffer to see if it is an include
1326 line. If so, we call load_file() recursively to load the included
1327 file. We never return a syntax error because a statement like
1328 "include = 5" is perfectly legal. We return false if no include was
1329 processed or true if we matched an include. */
1332 include_line (char *line)
1334 char quote, *c, *begin, *stop;
1338 if (gfc_option.flag_openmp)
1340 if (gfc_current_form == FORM_FREE)
1342 while (*c == ' ' || *c == '\t')
1344 if (*c == '!' && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
1349 if ((*c == '!' || *c == 'c' || *c == 'C' || *c == '*')
1350 && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
1355 while (*c == ' ' || *c == '\t')
1358 if (strncasecmp (c, "include", 7))
1362 while (*c == ' ' || *c == '\t')
1365 /* Find filename between quotes. */
1368 if (quote != '"' && quote != '\'')
1373 while (*c != quote && *c != '\0')
1381 while (*c == ' ' || *c == '\t')
1384 if (*c != '\0' && *c != '!')
1387 /* We have an include line at this point. */
1389 *stop = '\0'; /* It's ok to trash the buffer, as this line won't be
1390 read by anything else. */
1392 load_file (begin, false);
1397 /* Load a file into memory by calling load_line until the file ends. */
1400 load_file (const char *filename, bool initial)
1409 for (f = current_file; f; f = f->up)
1410 if (strcmp (filename, f->filename) == 0)
1412 gfc_error_now ("File '%s' is being included recursively", filename);
1420 input = gfc_src_file;
1421 gfc_src_file = NULL;
1424 input = gfc_open_file (filename);
1427 gfc_error_now ("Can't open file '%s'", filename);
1433 input = gfc_open_included_file (filename, false, false);
1436 gfc_error_now ("Can't open included file '%s'", filename);
1441 /* Load the file. */
1443 f = get_file (filename, initial ? LC_RENAME : LC_ENTER);
1444 f->up = current_file;
1446 current_file->line = 1;
1451 if (initial && gfc_src_preprocessor_lines[0])
1453 preprocessor_line (gfc_src_preprocessor_lines[0]);
1454 gfc_free (gfc_src_preprocessor_lines[0]);
1455 gfc_src_preprocessor_lines[0] = NULL;
1456 if (gfc_src_preprocessor_lines[1])
1458 preprocessor_line (gfc_src_preprocessor_lines[1]);
1459 gfc_free (gfc_src_preprocessor_lines[1]);
1460 gfc_src_preprocessor_lines[1] = NULL;
1466 int trunc = load_line (input, &line, &line_len);
1468 len = strlen (line);
1469 if (feof (input) && len == 0)
1472 /* If this is the first line of the file, it can contain a byte
1473 order mark (BOM), which we will ignore:
1474 FF FE is UTF-16 little endian,
1475 FE FF is UTF-16 big endian,
1476 EF BB BF is UTF-8. */
1478 && ((line_len >= 2 && line[0] == '\xFF' && line[1] == '\xFE')
1479 || (line_len >= 2 && line[0] == '\xFE' && line[1] == '\xFF')
1480 || (line_len >= 3 && line[0] == '\xEF' && line[1] == '\xBB'
1481 && line[2] == '\xBF')))
1483 int n = line[1] == '\xBB' ? 3 : 2;
1484 char * new = gfc_getmem (line_len);
1486 strcpy (new, line + n);
1492 /* There are three things this line can be: a line of Fortran
1493 source, an include line or a C preprocessor directive. */
1497 preprocessor_line (line);
1501 /* Preprocessed files have preprocessor lines added before the byte
1502 order mark, so first_line is not about the first line of the file
1503 but the first line that's not a preprocessor line. */
1506 if (include_line (line))
1508 current_file->line++;
1514 b = gfc_getmem (gfc_linebuf_header_size + len + 1);
1516 #ifdef USE_MAPPED_LOCATION
1518 = linemap_line_start (&line_table, current_file->line++, 120);
1520 b->linenum = current_file->line++;
1522 b->file = current_file;
1523 b->truncated = trunc;
1524 strcpy (b->line, line);
1526 if (line_head == NULL)
1529 line_tail->next = b;
1534 /* Release the line buffer allocated in load_line. */
1539 current_file = current_file->up;
1540 #ifdef USE_MAPPED_LOCATION
1541 linemap_add (&line_table, LC_LEAVE, 0, NULL, 0);
1547 /* Open a new file and start scanning from that file. Returns SUCCESS
1548 if everything went OK, FAILURE otherwise. If form == FORM_UKNOWN
1549 it tries to determine the source form from the filename, defaulting
1557 result = load_file (gfc_source_file, true);
1559 gfc_current_locus.lb = line_head;
1560 gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line;
1562 #if 0 /* Debugging aid. */
1563 for (; line_head; line_head = line_head->next)
1564 gfc_status ("%s:%3d %s\n", line_head->file->filename,
1565 #ifdef USE_MAPPED_LOCATION
1566 LOCATION_LINE (line_head->location),
1579 unescape_filename (const char *ptr)
1581 const char *p = ptr, *s;
1583 int escaped, unescape = 0;
1585 /* Make filename end at quote. */
1587 while (*p && ! (! escaped && *p == '"'))
1591 else if (*p == '\\')
1602 /* Undo effects of cpp_quote_string. */
1604 d = gfc_getmem (p + 1 - ptr - unescape);
1619 /* For preprocessed files, if the first tokens are of the form # NUM.
1620 handle the directives so we know the original file name. */
1623 gfc_read_orig_filename (const char *filename, const char **canon_source_file)
1628 gfc_src_file = gfc_open_file (filename);
1629 if (gfc_src_file == NULL)
1632 c = getc (gfc_src_file);
1633 ungetc (c, gfc_src_file);
1639 load_line (gfc_src_file, &gfc_src_preprocessor_lines[0], &len);
1641 if (strncmp (gfc_src_preprocessor_lines[0], "# 1 \"", 5) != 0)
1644 filename = unescape_filename (gfc_src_preprocessor_lines[0] + 5);
1645 if (filename == NULL)
1648 c = getc (gfc_src_file);
1649 ungetc (c, gfc_src_file);
1655 load_line (gfc_src_file, &gfc_src_preprocessor_lines[1], &len);
1657 if (strncmp (gfc_src_preprocessor_lines[1], "# 1 \"", 5) != 0)
1660 dirname = unescape_filename (gfc_src_preprocessor_lines[1] + 5);
1661 if (dirname == NULL)
1664 len = strlen (dirname);
1665 if (len < 3 || dirname[len - 1] != '/' || dirname[len - 2] != '/')
1670 dirname[len - 2] = '\0';
1671 set_src_pwd (dirname);
1673 if (! IS_ABSOLUTE_PATH (filename))
1675 char *p = gfc_getmem (len + strlen (filename));
1677 memcpy (p, dirname, len - 2);
1679 strcpy (p + len - 1, filename);
1680 *canon_source_file = p;