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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Set of subroutines to (ultimately) return the next character to the
23 various matching subroutines. This file's job is to read files and
24 build up lines that are parsed by the parser. This means that we
25 handle continuation lines and "include" lines.
27 The first thing the scanner does is to load an entire file into
28 memory. We load the entire file into memory for a couple reasons.
29 The first is that we want to be able to deal with nonseekable input
30 (pipes, stdin) and there is a lot of backing up involved during
33 The second is that we want to be able to print the locus of errors,
34 and an error on line 999999 could conflict with something on line
35 one. Given nonseekable input, we've got to store the whole thing.
37 One thing that helps are the column truncation limits that give us
38 an upper bound on the size of individual lines. We don't store the
41 From the scanner's viewpoint, the higher level subroutines ask for
42 new characters and do a lot of jumping backwards. */
49 /* Structure for holding module and include file search path. */
50 typedef struct gfc_directorylist
54 struct gfc_directorylist *next;
58 /* List of include file search directories. */
59 static gfc_directorylist *include_dirs, *intrinsic_modules_dirs;
61 static gfc_file *file_head, *current_file;
63 static int continue_flag, end_flag, openmp_flag;
64 static int continue_count, continue_line;
65 static locus openmp_locus;
67 gfc_source_form gfc_current_form;
68 static gfc_linebuf *line_head, *line_tail;
70 locus gfc_current_locus;
71 const char *gfc_source_file;
72 static FILE *gfc_src_file;
73 static char *gfc_src_preprocessor_lines[2];
77 /* Main scanner initialization. */
80 gfc_scanner_init_1 (void)
93 /* Main scanner destructor. */
96 gfc_scanner_done_1 (void)
101 while(line_head != NULL)
103 lb = line_head->next;
108 while(file_head != NULL)
111 gfc_free(file_head->filename);
118 /* Adds path to the list pointed to by list. */
121 add_path_to_list (gfc_directorylist **list, const char *path,
122 bool use_for_modules)
124 gfc_directorylist *dir;
128 while (*p == ' ' || *p == '\t') /* someone might do "-I include" */
134 dir = *list = gfc_getmem (sizeof (gfc_directorylist));
140 dir->next = gfc_getmem (sizeof (gfc_directorylist));
145 dir->use_for_modules = use_for_modules;
146 dir->path = gfc_getmem (strlen (p) + 2);
147 strcpy (dir->path, p);
148 strcat (dir->path, "/"); /* make '/' last character */
153 gfc_add_include_path (const char *path, bool use_for_modules)
155 add_path_to_list (&include_dirs, path, use_for_modules);
160 gfc_add_intrinsic_modules_path (const char *path)
162 add_path_to_list (&intrinsic_modules_dirs, path, true);
166 /* Release resources allocated for options. */
169 gfc_release_include_path (void)
171 gfc_directorylist *p;
173 while (include_dirs != NULL)
176 include_dirs = include_dirs->next;
181 while (intrinsic_modules_dirs != NULL)
183 p = intrinsic_modules_dirs;
184 intrinsic_modules_dirs = intrinsic_modules_dirs->next;
189 gfc_free (gfc_option.module_dir);
194 open_included_file (const char *name, gfc_directorylist *list, bool module)
197 gfc_directorylist *p;
200 for (p = list; p; p = p->next)
202 if (module && !p->use_for_modules)
205 fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1);
206 strcpy (fullname, p->path);
207 strcat (fullname, name);
209 f = gfc_open_file (fullname);
218 /* Opens file for reading, searching through the include directories
219 given if necessary. If the include_cwd argument is true, we try
220 to open the file in the current directory first. */
223 gfc_open_included_file (const char *name, bool include_cwd, bool module)
227 if (IS_ABSOLUTE_PATH (name))
228 return gfc_open_file (name);
232 f = gfc_open_file (name);
237 return open_included_file (name, include_dirs, module);
241 gfc_open_intrinsic_module (const char *name)
243 if (IS_ABSOLUTE_PATH (name))
244 return gfc_open_file (name);
246 return open_included_file (name, intrinsic_modules_dirs, true);
250 /* Test to see if we're at the end of the main source file. */
259 /* Test to see if we're at the end of the current file. */
267 if (line_head == NULL)
268 return 1; /* Null file */
270 if (gfc_current_locus.lb == NULL)
277 /* Test to see if we're at the beginning of a new line. */
285 return (gfc_current_locus.nextc == gfc_current_locus.lb->line);
289 /* Test to see if we're at the end of a line. */
297 return (*gfc_current_locus.nextc == '\0');
301 /* Advance the current line pointer to the next line. */
304 gfc_advance_line (void)
309 if (gfc_current_locus.lb == NULL)
315 gfc_current_locus.lb = gfc_current_locus.lb->next;
317 if (gfc_current_locus.lb != NULL)
318 gfc_current_locus.nextc = gfc_current_locus.lb->line;
321 gfc_current_locus.nextc = NULL;
327 /* Get the next character from the input, advancing gfc_current_file's
328 locus. When we hit the end of the line or the end of the file, we
329 start returning a '\n' in order to complete the current statement.
330 No Fortran line conventions are implemented here.
332 Requiring explicit advances to the next line prevents the parse
333 pointer from being on the wrong line if the current statement ends
341 if (gfc_current_locus.nextc == NULL)
344 c = (unsigned char) *gfc_current_locus.nextc++;
347 gfc_current_locus.nextc--; /* Remain on this line. */
355 /* Skip a comment. When we come here the parse pointer is positioned
356 immediately after the comment character. If we ever implement
357 compiler directives withing comments, here is where we parse the
361 skip_comment_line (void)
375 /* Comment lines are null lines, lines containing only blanks or lines
376 on which the first nonblank line is a '!'.
377 Return true if !$ openmp conditional compilation sentinel was
381 skip_free_comments (void)
389 at_bol = gfc_at_bol ();
390 start = gfc_current_locus;
396 while (gfc_is_whitespace (c));
406 /* If -fopenmp, we need to handle here 2 things:
407 1) don't treat !$omp as comments, but directives
408 2) handle OpenMP conditional compilation, where
409 !$ should be treated as 2 spaces (for initial lines
410 only if followed by space). */
411 if (gfc_option.flag_openmp && at_bol)
413 locus old_loc = gfc_current_locus;
414 if (next_char () == '$')
417 if (c == 'o' || c == 'O')
419 if (((c = next_char ()) == 'm' || c == 'M')
420 && ((c = next_char ()) == 'p' || c == 'P'))
422 if ((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;
435 gfc_warning_now ("!$OMP at %C starts a commented "
436 "line as it neither is followed "
437 "by a space nor is a "
438 "continuation line");
440 gfc_current_locus = old_loc;
444 if (continue_flag || c == ' ')
446 gfc_current_locus = old_loc;
452 gfc_current_locus = old_loc;
454 skip_comment_line ();
461 if (openmp_flag && at_bol)
463 gfc_current_locus = start;
468 /* Skip comment lines in fixed source mode. We have the same rules as
469 in skip_free_comment(), except that we can have a 'c', 'C' or '*'
470 in column 1, and a '!' cannot be in column 6. Also, we deal with
471 lines with 'd' or 'D' in column 1, if the user requested this. */
474 skip_fixed_comments (void)
482 start = gfc_current_locus;
487 while (gfc_is_whitespace (c));
492 skip_comment_line ();
497 gfc_current_locus = start;
504 start = gfc_current_locus;
515 if (c == '!' || c == 'c' || c == 'C' || c == '*')
517 /* If -fopenmp, we need to handle here 2 things:
518 1) don't treat !$omp|c$omp|*$omp as comments, but directives
519 2) handle OpenMP conditional compilation, where
520 !$|c$|*$ should be treated as 2 spaces if the characters
521 in columns 3 to 6 are valid fixed form label columns
523 if (gfc_option.flag_openmp)
525 if (next_char () == '$')
528 if (c == 'o' || c == 'O')
530 if (((c = next_char ()) == 'm' || c == 'M')
531 && ((c = next_char ()) == 'p' || c == 'P'))
535 && ((openmp_flag && continue_flag)
536 || c == ' ' || c == '0'))
539 while (gfc_is_whitespace (c))
541 if (c != '\n' && c != '!')
543 /* Canonicalize to *$omp. */
546 gfc_current_locus = start;
556 for (col = 3; col < 6; col++, c = next_char ())
559 else if (c < '0' || c > '9')
564 if (col == 6 && c != '\n'
565 && ((continue_flag && !digit_seen)
566 || c == ' ' || c == '0'))
568 gfc_current_locus = start;
569 start.nextc[0] = ' ';
570 start.nextc[1] = ' ';
575 gfc_current_locus = start;
577 skip_comment_line ();
581 if (gfc_option.flag_d_lines != -1 && (c == 'd' || c == 'D'))
583 if (gfc_option.flag_d_lines == 0)
585 skip_comment_line ();
589 *start.nextc = c = ' ';
594 while (gfc_is_whitespace (c))
606 if (col != 6 && c == '!')
608 skip_comment_line ();
616 gfc_current_locus = start;
620 /* Skips the current line if it is a comment. */
623 gfc_skip_comments (void)
625 if (gfc_current_form == FORM_FREE)
626 skip_free_comments ();
628 skip_fixed_comments ();
632 /* Get the next character from the input, taking continuation lines
633 and end-of-line comments into account. This implies that comment
634 lines between continued lines must be eaten here. For higher-level
635 subroutines, this flattens continued lines into a single logical
636 line. The in_string flag denotes whether we're inside a character
640 gfc_next_char_literal (int in_string)
643 int i, c, prev_openmp_flag;
655 if (gfc_current_form == FORM_FREE)
657 bool openmp_cond_flag;
659 if (!in_string && c == '!')
662 && memcmp (&gfc_current_locus, &openmp_locus,
663 sizeof (gfc_current_locus)) == 0)
666 /* This line can't be continued */
673 /* Avoid truncation warnings for comment ending lines. */
674 gfc_current_locus.lb->truncated = 0;
682 /* If the next nonblank character is a ! or \n, we've got a
683 continuation line. */
684 old_loc = gfc_current_locus;
687 while (gfc_is_whitespace (c))
690 /* Character constants to be continued cannot have commentary
693 if (in_string && c != '\n')
695 gfc_current_locus = old_loc;
700 if (c != '!' && c != '\n')
702 gfc_current_locus = old_loc;
707 prev_openmp_flag = openmp_flag;
710 skip_comment_line ();
715 goto not_continuation;
717 /* We've got a continuation line. If we are on the very next line after
718 the last continuation, increment the continuation line count and
719 check whether the limit has been exceeded. */
720 if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1)
722 if (++continue_count == gfc_option.max_continue_free)
724 if (gfc_notification_std (GFC_STD_GNU) || pedantic)
725 gfc_warning ("Limit of %d continuations exceeded in "
726 "statement at %C", gfc_option.max_continue_free);
729 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
731 /* Now find where it continues. First eat any comment lines. */
732 openmp_cond_flag = skip_free_comments ();
734 if (prev_openmp_flag != openmp_flag)
736 gfc_current_locus = old_loc;
737 openmp_flag = prev_openmp_flag;
742 /* Now that we have a non-comment line, probe ahead for the
743 first non-whitespace character. If it is another '&', then
744 reading starts at the next character, otherwise we must back
745 up to where the whitespace started and resume from there. */
747 old_loc = gfc_current_locus;
750 while (gfc_is_whitespace (c))
755 for (i = 0; i < 5; i++, c = next_char ())
757 gcc_assert (TOLOWER (c) == "!$omp"[i]);
759 old_loc = gfc_current_locus;
761 while (gfc_is_whitespace (c))
769 if (gfc_option.warn_ampersand)
770 gfc_warning_now ("Missing '&' in continued character "
772 gfc_current_locus.nextc--;
774 /* Both !$omp and !$ -fopenmp continuation lines have & on the
775 continuation line only optionally. */
776 else if (openmp_flag || openmp_cond_flag)
777 gfc_current_locus.nextc--;
781 gfc_current_locus = old_loc;
788 /* Fixed form continuation. */
789 if (!in_string && c == '!')
791 /* Skip comment at end of line. */
798 /* Avoid truncation warnings for comment ending lines. */
799 gfc_current_locus.lb->truncated = 0;
805 prev_openmp_flag = openmp_flag;
807 old_loc = gfc_current_locus;
810 skip_fixed_comments ();
812 /* See if this line is a continuation line. */
813 if (openmp_flag != prev_openmp_flag)
815 openmp_flag = prev_openmp_flag;
816 goto not_continuation;
820 for (i = 0; i < 5; i++)
824 goto not_continuation;
827 for (i = 0; i < 5; i++)
830 if (TOLOWER (c) != "*$omp"[i])
831 goto not_continuation;
835 if (c == '0' || c == ' ' || c == '\n')
836 goto not_continuation;
838 /* We've got a continuation line. If we are on the very next line after
839 the last continuation, increment the continuation line count and
840 check whether the limit has been exceeded. */
841 if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1)
843 if (++continue_count == gfc_option.max_continue_fixed)
845 if (gfc_notification_std (GFC_STD_GNU) || pedantic)
846 gfc_warning ("Limit of %d continuations exceeded in "
848 gfc_option.max_continue_fixed);
852 if (continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
853 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
856 /* Ready to read first character of continuation line, which might
857 be another continuation line! */
862 gfc_current_locus = old_loc;
872 /* Get the next character of input, folded to lowercase. In fixed
873 form mode, we also ignore spaces. When matcher subroutines are
874 parsing character literals, they have to call
875 gfc_next_char_literal(). */
884 c = gfc_next_char_literal (0);
886 while (gfc_current_form == FORM_FIXED && gfc_is_whitespace (c));
898 old_loc = gfc_current_locus;
899 c = gfc_next_char ();
900 gfc_current_locus = old_loc;
906 /* Recover from an error. We try to get past the current statement
907 and get lined up for the next. The next statement follows a '\n'
908 or a ';'. We also assume that we are not within a character
909 constant, and deal with finding a '\'' or '"'. */
912 gfc_error_recovery (void)
921 c = gfc_next_char ();
922 if (c == '\n' || c == ';')
925 if (c != '\'' && c != '"')
954 /* Read ahead until the next character to be read is not whitespace. */
957 gfc_gobble_whitespace (void)
959 static int linenum = 0;
965 old_loc = gfc_current_locus;
966 c = gfc_next_char_literal (0);
967 /* Issue a warning for nonconforming tabs. We keep track of the line
968 number because the Fortran matchers will often back up and the same
969 line will be scanned multiple times. */
970 if (!gfc_option.warn_tabs && c == '\t')
972 #ifdef USE_MAPPED_LOCATION
973 int cur_linenum = LOCATION_LINE (gfc_current_locus.lb->location);
975 int cur_linenum = gfc_current_locus.lb->linenum;
977 if (cur_linenum != linenum)
979 linenum = cur_linenum;
980 gfc_warning_now ("Nonconforming tab character at %C");
984 while (gfc_is_whitespace (c));
986 gfc_current_locus = old_loc;
990 /* Load a single line into pbuf.
992 If pbuf points to a NULL pointer, it is allocated.
993 We truncate lines that are too long, unless we're dealing with
994 preprocessor lines or if the option -ffixed-line-length-none is set,
995 in which case we reallocate the buffer to fit the entire line, if
997 In fixed mode, we expand a tab that occurs within the statement
998 label region to expand to spaces that leave the next character in
1000 load_line returns whether the line was truncated.
1002 NOTE: The error machinery isn't available at this point, so we can't
1003 easily report line and column numbers consistent with other
1004 parts of gfortran. */
1007 load_line (FILE *input, char **pbuf, int *pbuflen)
1009 static int linenum = 0, current_line = 1;
1010 int c, maxlen, i, preprocessor_flag, buflen = *pbuflen;
1011 int trunc_flag = 0, seen_comment = 0;
1012 int seen_printable = 0, seen_ampersand = 0;
1015 /* Determine the maximum allowed line length. */
1016 if (gfc_current_form == FORM_FREE)
1017 maxlen = gfc_option.free_line_length;
1018 else if (gfc_current_form == FORM_FIXED)
1019 maxlen = gfc_option.fixed_line_length;
1025 /* Allocate the line buffer, storing its length into buflen.
1026 Note that if maxlen==0, indicating that arbitrary-length lines
1027 are allowed, the buffer will be reallocated if this length is
1028 insufficient; since 132 characters is the length of a standard
1029 free-form line, we use that as a starting guess. */
1035 *pbuf = gfc_getmem (buflen + 1);
1041 preprocessor_flag = 0;
1044 /* In order to not truncate preprocessor lines, we have to
1045 remember that this is one. */
1046 preprocessor_flag = 1;
1057 /* Check for illegal use of ampersand. See F95 Standard 3.3.1.3. */
1058 if (gfc_current_form == FORM_FREE
1059 && !seen_printable && seen_ampersand)
1062 gfc_error_now ("'&' not allowed by itself in line %d",
1065 gfc_warning_now ("'&' not allowed by itself in line %d",
1072 continue; /* Gobble characters. */
1084 if ((c != '&' && c != '!') || (c == '!' && !seen_ampersand))
1087 /* Is this a fixed-form comment? */
1088 if (gfc_current_form == FORM_FIXED && i == 0
1089 && (c == '*' || c == 'c' || c == 'd'))
1092 if (gfc_current_form == FORM_FIXED && c == '\t' && i <= 6)
1094 if (!gfc_option.warn_tabs && seen_comment == 0
1095 && current_line != linenum)
1097 linenum = current_line;
1098 gfc_warning_now ("Nonconforming tab character in column 1 "
1099 "of line %d", linenum);
1114 if (maxlen == 0 || preprocessor_flag)
1118 /* Reallocate line buffer to double size to hold the
1120 buflen = buflen * 2;
1121 *pbuf = xrealloc (*pbuf, buflen + 1);
1122 buffer = (*pbuf) + i;
1125 else if (i >= maxlen)
1127 /* Truncate the rest of the line. */
1131 if (c == '\n' || c == EOF)
1137 ungetc ('\n', input);
1141 /* Pad lines to the selected line length in fixed form. */
1142 if (gfc_current_form == FORM_FIXED
1143 && gfc_option.fixed_line_length != 0
1144 && !preprocessor_flag
1147 while (i++ < maxlen)
1159 /* Get a gfc_file structure, initialize it and add it to
1163 get_file (const char *name, enum lc_reason reason ATTRIBUTE_UNUSED)
1167 f = gfc_getmem (sizeof (gfc_file));
1169 f->filename = gfc_getmem (strlen (name) + 1);
1170 strcpy (f->filename, name);
1172 f->next = file_head;
1175 f->included_by = current_file;
1176 if (current_file != NULL)
1177 f->inclusion_line = current_file->line;
1179 #ifdef USE_MAPPED_LOCATION
1180 linemap_add (line_table, reason, false, f->filename, 1);
1186 /* Deal with a line from the C preprocessor. The
1187 initial octothorp has already been seen. */
1190 preprocessor_line (char *c)
1196 int escaped, unescape;
1199 while (*c == ' ' || *c == '\t')
1202 if (*c < '0' || *c > '9')
1207 c = strchr (c, ' ');
1210 /* No file name given. Set new line number. */
1211 current_file->line = line;
1216 while (*c == ' ' || *c == '\t')
1226 /* Make filename end at quote. */
1229 while (*c && ! (!escaped && *c == '"'))
1233 else if (*c == '\\')
1242 /* Preprocessor line has no closing quote. */
1247 /* Undo effects of cpp_quote_string. */
1251 char *d = gfc_getmem (c - filename - unescape);
1267 flag[1] = flag[2] = flag[3] = flag[4] = false;
1271 c = strchr (c, ' ');
1278 if (1 <= i && i <= 4)
1282 /* Interpret flags. */
1284 if (flag[1]) /* Starting new file. */
1286 f = get_file (filename, LC_RENAME);
1287 f->up = current_file;
1291 if (flag[2]) /* Ending current file. */
1293 if (!current_file->up
1294 || strcmp (current_file->up->filename, filename) != 0)
1296 gfc_warning_now ("%s:%d: file %s left but not entered",
1297 current_file->filename, current_file->line,
1300 gfc_free (filename);
1303 current_file = current_file->up;
1306 /* The name of the file can be a temporary file produced by
1307 cpp. Replace the name if it is different. */
1309 if (strcmp (current_file->filename, filename) != 0)
1311 gfc_free (current_file->filename);
1312 current_file->filename = gfc_getmem (strlen (filename) + 1);
1313 strcpy (current_file->filename, filename);
1316 /* Set new line number. */
1317 current_file->line = line;
1319 gfc_free (filename);
1323 gfc_warning_now ("%s:%d: Illegal preprocessor directive",
1324 current_file->filename, current_file->line);
1325 current_file->line++;
1329 static try load_file (const char *, bool);
1331 /* include_line()-- Checks a line buffer to see if it is an include
1332 line. If so, we call load_file() recursively to load the included
1333 file. We never return a syntax error because a statement like
1334 "include = 5" is perfectly legal. We return false if no include was
1335 processed or true if we matched an include. */
1338 include_line (char *line)
1340 char quote, *c, *begin, *stop;
1344 if (gfc_option.flag_openmp)
1346 if (gfc_current_form == FORM_FREE)
1348 while (*c == ' ' || *c == '\t')
1350 if (*c == '!' && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
1355 if ((*c == '!' || *c == 'c' || *c == 'C' || *c == '*')
1356 && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
1361 while (*c == ' ' || *c == '\t')
1364 if (strncasecmp (c, "include", 7))
1368 while (*c == ' ' || *c == '\t')
1371 /* Find filename between quotes. */
1374 if (quote != '"' && quote != '\'')
1379 while (*c != quote && *c != '\0')
1387 while (*c == ' ' || *c == '\t')
1390 if (*c != '\0' && *c != '!')
1393 /* We have an include line at this point. */
1395 *stop = '\0'; /* It's ok to trash the buffer, as this line won't be
1396 read by anything else. */
1398 load_file (begin, false);
1403 /* Load a file into memory by calling load_line until the file ends. */
1406 load_file (const char *filename, bool initial)
1415 for (f = current_file; f; f = f->up)
1416 if (strcmp (filename, f->filename) == 0)
1418 gfc_error_now ("File '%s' is being included recursively", filename);
1426 input = gfc_src_file;
1427 gfc_src_file = NULL;
1430 input = gfc_open_file (filename);
1433 gfc_error_now ("Can't open file '%s'", filename);
1439 input = gfc_open_included_file (filename, false, false);
1442 gfc_error_now ("Can't open included file '%s'", filename);
1447 /* Load the file. */
1449 f = get_file (filename, initial ? LC_RENAME : LC_ENTER);
1450 f->up = current_file;
1452 current_file->line = 1;
1457 if (initial && gfc_src_preprocessor_lines[0])
1459 preprocessor_line (gfc_src_preprocessor_lines[0]);
1460 gfc_free (gfc_src_preprocessor_lines[0]);
1461 gfc_src_preprocessor_lines[0] = NULL;
1462 if (gfc_src_preprocessor_lines[1])
1464 preprocessor_line (gfc_src_preprocessor_lines[1]);
1465 gfc_free (gfc_src_preprocessor_lines[1]);
1466 gfc_src_preprocessor_lines[1] = NULL;
1472 int trunc = load_line (input, &line, &line_len);
1474 len = strlen (line);
1475 if (feof (input) && len == 0)
1478 /* If this is the first line of the file, it can contain a byte
1479 order mark (BOM), which we will ignore:
1480 FF FE is UTF-16 little endian,
1481 FE FF is UTF-16 big endian,
1482 EF BB BF is UTF-8. */
1484 && ((line_len >= 2 && line[0] == '\xFF' && line[1] == '\xFE')
1485 || (line_len >= 2 && line[0] == '\xFE' && line[1] == '\xFF')
1486 || (line_len >= 3 && line[0] == '\xEF' && line[1] == '\xBB'
1487 && line[2] == '\xBF')))
1489 int n = line[1] == '\xBB' ? 3 : 2;
1490 char * new = gfc_getmem (line_len);
1492 strcpy (new, line + n);
1498 /* There are three things this line can be: a line of Fortran
1499 source, an include line or a C preprocessor directive. */
1503 preprocessor_line (line);
1507 /* Preprocessed files have preprocessor lines added before the byte
1508 order mark, so first_line is not about the first line of the file
1509 but the first line that's not a preprocessor line. */
1512 if (include_line (line))
1514 current_file->line++;
1520 b = gfc_getmem (gfc_linebuf_header_size + len + 1);
1522 #ifdef USE_MAPPED_LOCATION
1524 = linemap_line_start (line_table, current_file->line++, 120);
1526 b->linenum = current_file->line++;
1528 b->file = current_file;
1529 b->truncated = trunc;
1530 strcpy (b->line, line);
1532 if (line_head == NULL)
1535 line_tail->next = b;
1540 /* Release the line buffer allocated in load_line. */
1545 current_file = current_file->up;
1546 #ifdef USE_MAPPED_LOCATION
1547 linemap_add (line_table, LC_LEAVE, 0, NULL, 0);
1553 /* Open a new file and start scanning from that file. Returns SUCCESS
1554 if everything went OK, FAILURE otherwise. If form == FORM_UKNOWN
1555 it tries to determine the source form from the filename, defaulting
1563 result = load_file (gfc_source_file, true);
1565 gfc_current_locus.lb = line_head;
1566 gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line;
1568 #if 0 /* Debugging aid. */
1569 for (; line_head; line_head = line_head->next)
1570 gfc_status ("%s:%3d %s\n", line_head->file->filename,
1571 #ifdef USE_MAPPED_LOCATION
1572 LOCATION_LINE (line_head->location),
1585 unescape_filename (const char *ptr)
1587 const char *p = ptr, *s;
1589 int escaped, unescape = 0;
1591 /* Make filename end at quote. */
1593 while (*p && ! (! escaped && *p == '"'))
1597 else if (*p == '\\')
1608 /* Undo effects of cpp_quote_string. */
1610 d = gfc_getmem (p + 1 - ptr - unescape);
1625 /* For preprocessed files, if the first tokens are of the form # NUM.
1626 handle the directives so we know the original file name. */
1629 gfc_read_orig_filename (const char *filename, const char **canon_source_file)
1634 gfc_src_file = gfc_open_file (filename);
1635 if (gfc_src_file == NULL)
1638 c = getc (gfc_src_file);
1639 ungetc (c, gfc_src_file);
1645 load_line (gfc_src_file, &gfc_src_preprocessor_lines[0], &len);
1647 if (strncmp (gfc_src_preprocessor_lines[0], "# 1 \"", 5) != 0)
1650 filename = unescape_filename (gfc_src_preprocessor_lines[0] + 5);
1651 if (filename == NULL)
1654 c = getc (gfc_src_file);
1655 ungetc (c, gfc_src_file);
1661 load_line (gfc_src_file, &gfc_src_preprocessor_lines[1], &len);
1663 if (strncmp (gfc_src_preprocessor_lines[1], "# 1 \"", 5) != 0)
1666 dirname = unescape_filename (gfc_src_preprocessor_lines[1] + 5);
1667 if (dirname == NULL)
1670 len = strlen (dirname);
1671 if (len < 3 || dirname[len - 1] != '/' || dirname[len - 2] != '/')
1676 dirname[len - 2] = '\0';
1677 set_src_pwd (dirname);
1679 if (! IS_ABSOLUTE_PATH (filename))
1681 char *p = gfc_getmem (len + strlen (filename));
1683 memcpy (p, dirname, len - 2);
1685 strcpy (p + len - 1, filename);
1686 *canon_source_file = p;