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);
120 /* Adds path to the list pointed to by list. */
123 add_path_to_list (gfc_directorylist **list, const char *path,
124 bool use_for_modules)
126 gfc_directorylist *dir;
130 while (*p == ' ' || *p == '\t') /* someone might do "-I include" */
136 dir = *list = gfc_getmem (sizeof (gfc_directorylist));
142 dir->next = gfc_getmem (sizeof (gfc_directorylist));
147 dir->use_for_modules = use_for_modules;
148 dir->path = gfc_getmem (strlen (p) + 2);
149 strcpy (dir->path, p);
150 strcat (dir->path, "/"); /* make '/' last character */
155 gfc_add_include_path (const char *path, bool use_for_modules)
157 add_path_to_list (&include_dirs, path, use_for_modules);
162 gfc_add_intrinsic_modules_path (const char *path)
164 add_path_to_list (&intrinsic_modules_dirs, path, true);
168 /* Release resources allocated for options. */
171 gfc_release_include_path (void)
173 gfc_directorylist *p;
175 while (include_dirs != NULL)
178 include_dirs = include_dirs->next;
183 while (intrinsic_modules_dirs != NULL)
185 p = intrinsic_modules_dirs;
186 intrinsic_modules_dirs = intrinsic_modules_dirs->next;
191 gfc_free (gfc_option.module_dir);
196 open_included_file (const char *name, gfc_directorylist *list, bool module)
199 gfc_directorylist *p;
202 for (p = list; p; p = p->next)
204 if (module && !p->use_for_modules)
207 fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1);
208 strcpy (fullname, p->path);
209 strcat (fullname, name);
211 f = gfc_open_file (fullname);
220 /* Opens file for reading, searching through the include directories
221 given if necessary. If the include_cwd argument is true, we try
222 to open the file in the current directory first. */
225 gfc_open_included_file (const char *name, bool include_cwd, bool module)
229 if (IS_ABSOLUTE_PATH (name))
230 return gfc_open_file (name);
234 f = gfc_open_file (name);
239 return open_included_file (name, include_dirs, module);
243 gfc_open_intrinsic_module (const char *name)
245 if (IS_ABSOLUTE_PATH (name))
246 return gfc_open_file (name);
248 return open_included_file (name, intrinsic_modules_dirs, true);
251 /* Test to see if we're at the end of the main source file. */
261 /* Test to see if we're at the end of the current file. */
270 if (line_head == NULL)
271 return 1; /* Null file */
273 if (gfc_current_locus.lb == NULL)
280 /* Test to see if we're at the beginning of a new line. */
288 return (gfc_current_locus.nextc == gfc_current_locus.lb->line);
292 /* Test to see if we're at the end of a line. */
301 return (*gfc_current_locus.nextc == '\0');
305 /* Advance the current line pointer to the next line. */
308 gfc_advance_line (void)
313 if (gfc_current_locus.lb == NULL)
319 gfc_current_locus.lb = gfc_current_locus.lb->next;
321 if (gfc_current_locus.lb != NULL)
322 gfc_current_locus.nextc = gfc_current_locus.lb->line;
325 gfc_current_locus.nextc = NULL;
331 /* Get the next character from the input, advancing gfc_current_file's
332 locus. When we hit the end of the line or the end of the file, we
333 start returning a '\n' in order to complete the current statement.
334 No Fortran line conventions are implemented here.
336 Requiring explicit advances to the next line prevents the parse
337 pointer from being on the wrong line if the current statement ends
345 if (gfc_current_locus.nextc == NULL)
348 c = *gfc_current_locus.nextc++;
351 gfc_current_locus.nextc--; /* Remain on this line. */
358 /* Skip a comment. When we come here the parse pointer is positioned
359 immediately after the comment character. If we ever implement
360 compiler directives withing comments, here is where we parse the
364 skip_comment_line (void)
378 /* Comment lines are null lines, lines containing only blanks or lines
379 on which the first nonblank line is a '!'.
380 Return true if !$ openmp conditional compilation sentinel was
384 skip_free_comments (void)
392 at_bol = gfc_at_bol ();
393 start = gfc_current_locus;
399 while (gfc_is_whitespace (c));
409 /* If -fopenmp, we need to handle here 2 things:
410 1) don't treat !$omp as comments, but directives
411 2) handle OpenMP conditional compilation, where
412 !$ should be treated as 2 spaces (for initial lines
413 only if followed by space). */
414 if (gfc_option.flag_openmp && at_bol)
416 locus old_loc = gfc_current_locus;
417 if (next_char () == '$')
420 if (c == 'o' || c == 'O')
422 if (((c = next_char ()) == 'm' || c == 'M')
423 && ((c = next_char ()) == 'p' || c == 'P')
424 && ((c = next_char ()) == ' ' || continue_flag))
426 while (gfc_is_whitespace (c))
428 if (c != '\n' && c != '!')
431 openmp_locus = old_loc;
432 gfc_current_locus = start;
436 gfc_current_locus = old_loc;
440 if (continue_flag || c == ' ')
442 gfc_current_locus = old_loc;
448 gfc_current_locus = old_loc;
450 skip_comment_line ();
457 if (openmp_flag && at_bol)
459 gfc_current_locus = start;
464 /* Skip comment lines in fixed source mode. We have the same rules as
465 in skip_free_comment(), except that we can have a 'c', 'C' or '*'
466 in column 1, and a '!' cannot be in column 6. Also, we deal with
467 lines with 'd' or 'D' in column 1, if the user requested this. */
470 skip_fixed_comments (void)
478 start = gfc_current_locus;
483 while (gfc_is_whitespace (c));
488 skip_comment_line ();
493 gfc_current_locus = start;
500 start = gfc_current_locus;
511 if (c == '!' || c == 'c' || c == 'C' || c == '*')
513 /* If -fopenmp, we need to handle here 2 things:
514 1) don't treat !$omp|c$omp|*$omp as comments, but directives
515 2) handle OpenMP conditional compilation, where
516 !$|c$|*$ should be treated as 2 spaces if the characters
517 in columns 3 to 6 are valid fixed form label columns
519 if (gfc_option.flag_openmp)
521 if (next_char () == '$')
524 if (c == 'o' || c == 'O')
526 if (((c = next_char ()) == 'm' || c == 'M')
527 && ((c = next_char ()) == 'p' || c == 'P'))
531 && ((openmp_flag && continue_flag)
532 || c == ' ' || c == '0'))
535 while (gfc_is_whitespace (c))
537 if (c != '\n' && c != '!')
539 /* Canonicalize to *$omp. */
542 gfc_current_locus = start;
552 for (col = 3; col < 6; col++, c = next_char ())
555 else if (c < '0' || c > '9')
560 if (col == 6 && c != '\n'
561 && ((continue_flag && !digit_seen)
562 || c == ' ' || c == '0'))
564 gfc_current_locus = start;
565 start.nextc[0] = ' ';
566 start.nextc[1] = ' ';
571 gfc_current_locus = start;
573 skip_comment_line ();
577 if (gfc_option.flag_d_lines != -1 && (c == 'd' || c == 'D'))
579 if (gfc_option.flag_d_lines == 0)
581 skip_comment_line ();
585 *start.nextc = c = ' ';
590 while (gfc_is_whitespace (c))
602 if (col != 6 && c == '!')
604 skip_comment_line ();
612 gfc_current_locus = start;
616 /* Skips the current line if it is a comment. */
619 gfc_skip_comments (void)
621 if (gfc_current_form == FORM_FREE)
622 skip_free_comments ();
624 skip_fixed_comments ();
628 /* Get the next character from the input, taking continuation lines
629 and end-of-line comments into account. This implies that comment
630 lines between continued lines must be eaten here. For higher-level
631 subroutines, this flattens continued lines into a single logical
632 line. The in_string flag denotes whether we're inside a character
636 gfc_next_char_literal (int in_string)
639 int i, c, prev_openmp_flag;
651 if (gfc_current_form == FORM_FREE)
653 bool openmp_cond_flag;
655 if (!in_string && c == '!')
658 && memcmp (&gfc_current_locus, &openmp_locus,
659 sizeof (gfc_current_locus)) == 0)
662 /* This line can't be continued */
669 /* Avoid truncation warnings for comment ending lines. */
670 gfc_current_locus.lb->truncated = 0;
678 /* If the next nonblank character is a ! or \n, we've got a
679 continuation line. */
680 old_loc = gfc_current_locus;
683 while (gfc_is_whitespace (c))
686 /* Character constants to be continued cannot have commentary
689 if (in_string && c != '\n')
691 gfc_current_locus = old_loc;
696 if (c != '!' && c != '\n')
698 gfc_current_locus = old_loc;
703 prev_openmp_flag = openmp_flag;
706 skip_comment_line ();
710 /* We've got a continuation line. If we are on the very next line after
711 the last continuation, increment the continuation line count and
712 check whether the limit has been exceeded. */
713 if (gfc_current_locus.lb->linenum == continue_line + 1)
715 if (++continue_count == gfc_option.max_continue_free)
717 if (gfc_notification_std (GFC_STD_GNU)
719 gfc_warning ("Limit of %d continuations exceeded in statement at %C",
720 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 constant at %C");
765 gfc_current_locus.nextc--;
767 /* Both !$omp and !$ -fopenmp continuation lines have & on the
768 continuation line only optionally. */
769 else if (openmp_flag || openmp_cond_flag)
770 gfc_current_locus.nextc--;
774 gfc_current_locus = old_loc;
781 /* Fixed form continuation. */
782 if (!in_string && c == '!')
784 /* Skip comment at end of line. */
791 /* Avoid truncation warnings for comment ending lines. */
792 gfc_current_locus.lb->truncated = 0;
798 prev_openmp_flag = openmp_flag;
800 old_loc = gfc_current_locus;
803 skip_fixed_comments ();
805 /* See if this line is a continuation line. */
806 if (openmp_flag != prev_openmp_flag)
808 openmp_flag = prev_openmp_flag;
809 goto not_continuation;
813 for (i = 0; i < 5; i++)
817 goto not_continuation;
820 for (i = 0; i < 5; i++)
823 if (TOLOWER (c) != "*$omp"[i])
824 goto not_continuation;
828 if (c == '0' || c == ' ' || c == '\n')
829 goto not_continuation;
831 /* We've got a continuation line. If we are on the very next line after
832 the last continuation, increment the continuation line count and
833 check whether the limit has been exceeded. */
834 if (gfc_current_locus.lb->linenum == continue_line + 1)
836 if (++continue_count == gfc_option.max_continue_fixed)
838 if (gfc_notification_std (GFC_STD_GNU)
840 gfc_warning ("Limit of %d continuations exceeded in statement at %C",
841 gfc_option.max_continue_fixed);
845 if (continue_line < gfc_current_locus.lb->linenum)
846 continue_line = gfc_current_locus.lb->linenum;
849 /* Ready to read first character of continuation line, which might
850 be another continuation line! */
855 gfc_current_locus = old_loc;
865 /* Get the next character of input, folded to lowercase. In fixed
866 form mode, we also ignore spaces. When matcher subroutines are
867 parsing character literals, they have to call
868 gfc_next_char_literal(). */
877 c = gfc_next_char_literal (0);
879 while (gfc_current_form == FORM_FIXED && gfc_is_whitespace (c));
891 old_loc = gfc_current_locus;
892 c = gfc_next_char ();
893 gfc_current_locus = old_loc;
899 /* Recover from an error. We try to get past the current statement
900 and get lined up for the next. The next statement follows a '\n'
901 or a ';'. We also assume that we are not within a character
902 constant, and deal with finding a '\'' or '"'. */
905 gfc_error_recovery (void)
914 c = gfc_next_char ();
915 if (c == '\n' || c == ';')
918 if (c != '\'' && c != '"')
947 /* Read ahead until the next character to be read is not whitespace. */
950 gfc_gobble_whitespace (void)
952 static int linenum = 0;
958 old_loc = gfc_current_locus;
959 c = gfc_next_char_literal (0);
960 /* Issue a warning for nonconforming tabs. We keep track of the line
961 number because the Fortran matchers will often back up and the same
962 line will be scanned multiple times. */
963 if (!gfc_option.warn_tabs && c == '\t')
965 #ifdef USE_MAPPED_LOCATION
966 int cur_linenum = LOCATION_LINE (gfc_current_locus.lb->location);
968 int cur_linenum = gfc_current_locus.lb->linenum;
970 if (cur_linenum != linenum)
972 linenum = cur_linenum;
973 gfc_warning_now ("Nonconforming tab character at %C");
977 while (gfc_is_whitespace (c));
979 gfc_current_locus = old_loc;
983 /* Load a single line into pbuf.
985 If pbuf points to a NULL pointer, it is allocated.
986 We truncate lines that are too long, unless we're dealing with
987 preprocessor lines or if the option -ffixed-line-length-none is set,
988 in which case we reallocate the buffer to fit the entire line, if
990 In fixed mode, we expand a tab that occurs within the statement
991 label region to expand to spaces that leave the next character in
993 load_line returns whether the line was truncated.
995 NOTE: The error machinery isn't available at this point, so we can't
996 easily report line and column numbers consistent with other
997 parts of gfortran. */
1000 load_line (FILE * input, char **pbuf, int *pbuflen)
1002 static int linenum = 0, current_line = 1;
1003 int c, maxlen, i, preprocessor_flag, buflen = *pbuflen;
1004 int trunc_flag = 0, seen_comment = 0;
1005 int seen_printable = 0, seen_ampersand = 0;
1008 /* Determine the maximum allowed line length. */
1009 if (gfc_current_form == FORM_FREE)
1010 maxlen = gfc_option.free_line_length;
1011 else if (gfc_current_form == FORM_FIXED)
1012 maxlen = gfc_option.fixed_line_length;
1018 /* Allocate the line buffer, storing its length into buflen.
1019 Note that if maxlen==0, indicating that arbitrary-length lines
1020 are allowed, the buffer will be reallocated if this length is
1021 insufficient; since 132 characters is the length of a standard
1022 free-form line, we use that as a starting guess. */
1028 *pbuf = gfc_getmem (buflen + 1);
1034 preprocessor_flag = 0;
1037 /* In order to not truncate preprocessor lines, we have to
1038 remember that this is one. */
1039 preprocessor_flag = 1;
1050 /* Check for illegal use of ampersand. See F95 Standard 3.3.1.3. */
1051 if (gfc_current_form == FORM_FREE
1052 && !seen_printable && seen_ampersand)
1056 ("'&' not allowed by itself in line %d", current_line);
1059 ("'&' not allowed by itself in line %d", current_line);
1065 continue; /* Gobble characters. */
1071 /* Ctrl-Z ends the file. */
1072 while (fgetc (input) != EOF);
1076 /* Check for illegal use of ampersand. See F95 Standard 3.3.1.3. */
1080 if ((c != ' ' && c != '&' && c != '!') || (c == '!' && !seen_ampersand))
1083 if (gfc_current_form == FORM_FREE
1084 && c == '!' && !seen_printable && seen_ampersand)
1088 "'&' not allowed by itself with comment in line %d", current_line);
1091 "'&' not allowed by itself with comment in line %d", current_line);
1095 /* Is this a fixed-form comment? */
1096 if (gfc_current_form == FORM_FIXED && i == 0
1097 && (c == '*' || c == 'c' || c == 'd'))
1100 if (gfc_current_form == FORM_FIXED && c == '\t' && i <= 6)
1102 if (!gfc_option.warn_tabs && seen_comment == 0
1103 && current_line != linenum)
1105 linenum = current_line;
1107 "Nonconforming tab character in column 1 of line %d", linenum);
1122 if (maxlen == 0 || preprocessor_flag)
1126 /* Reallocate line buffer to double size to hold the
1128 buflen = buflen * 2;
1129 *pbuf = xrealloc (*pbuf, buflen + 1);
1133 else if (i >= maxlen)
1135 /* Truncate the rest of the line. */
1139 if (c == '\n' || c == EOF)
1145 ungetc ('\n', input);
1149 /* Pad lines to the selected line length in fixed form. */
1150 if (gfc_current_form == FORM_FIXED
1151 && gfc_option.fixed_line_length != 0
1152 && !preprocessor_flag
1155 while (i++ < maxlen)
1167 /* Get a gfc_file structure, initialize it and add it to
1171 get_file (const char *name, enum lc_reason reason ATTRIBUTE_UNUSED)
1175 f = gfc_getmem (sizeof (gfc_file));
1177 f->filename = gfc_getmem (strlen (name) + 1);
1178 strcpy (f->filename, name);
1180 f->next = file_head;
1183 f->included_by = current_file;
1184 if (current_file != NULL)
1185 f->inclusion_line = current_file->line;
1187 #ifdef USE_MAPPED_LOCATION
1188 linemap_add (&line_table, reason, false, f->filename, 1);
1194 /* Deal with a line from the C preprocessor. The
1195 initial octothorp has already been seen. */
1198 preprocessor_line (char *c)
1204 int escaped, unescape;
1207 while (*c == ' ' || *c == '\t')
1210 if (*c < '0' || *c > '9')
1215 c = strchr (c, ' ');
1218 /* No file name given. Set new line number. */
1219 current_file->line = line;
1224 while (*c == ' ' || *c == '\t')
1234 /* Make filename end at quote. */
1237 while (*c && ! (! escaped && *c == '"'))
1241 else if (*c == '\\')
1250 /* Preprocessor line has no closing quote. */
1255 /* Undo effects of cpp_quote_string. */
1259 char *d = gfc_getmem (c - filename - unescape);
1275 flag[1] = flag[2] = flag[3] = flag[4] = false;
1279 c = strchr (c, ' ');
1286 if (1 <= i && i <= 4)
1290 /* Interpret flags. */
1292 if (flag[1]) /* Starting new file. */
1294 f = get_file (filename, LC_RENAME);
1295 f->up = current_file;
1299 if (flag[2]) /* Ending current file. */
1301 if (!current_file->up
1302 || strcmp (current_file->up->filename, filename) != 0)
1304 gfc_warning_now ("%s:%d: file %s left but not entered",
1305 current_file->filename, current_file->line,
1308 gfc_free (filename);
1311 current_file = current_file->up;
1314 /* The name of the file can be a temporary file produced by
1315 cpp. Replace the name if it is different. */
1317 if (strcmp (current_file->filename, filename) != 0)
1319 gfc_free (current_file->filename);
1320 current_file->filename = gfc_getmem (strlen (filename) + 1);
1321 strcpy (current_file->filename, filename);
1324 /* Set new line number. */
1325 current_file->line = line;
1327 gfc_free (filename);
1331 gfc_warning_now ("%s:%d: Illegal preprocessor directive",
1332 current_file->filename, current_file->line);
1333 current_file->line++;
1337 static try load_file (const char *, bool);
1339 /* include_line()-- Checks a line buffer to see if it is an include
1340 line. If so, we call load_file() recursively to load the included
1341 file. We never return a syntax error because a statement like
1342 "include = 5" is perfectly legal. We return false if no include was
1343 processed or true if we matched an include. */
1346 include_line (char *line)
1348 char quote, *c, *begin, *stop;
1352 if (gfc_option.flag_openmp)
1354 if (gfc_current_form == FORM_FREE)
1356 while (*c == ' ' || *c == '\t')
1358 if (*c == '!' && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
1363 if ((*c == '!' || *c == 'c' || *c == 'C' || *c == '*')
1364 && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
1369 while (*c == ' ' || *c == '\t')
1372 if (strncasecmp (c, "include", 7))
1376 while (*c == ' ' || *c == '\t')
1379 /* Find filename between quotes. */
1382 if (quote != '"' && quote != '\'')
1387 while (*c != quote && *c != '\0')
1395 while (*c == ' ' || *c == '\t')
1398 if (*c != '\0' && *c != '!')
1401 /* We have an include line at this point. */
1403 *stop = '\0'; /* It's ok to trash the buffer, as this line won't be
1404 read by anything else. */
1406 load_file (begin, false);
1410 /* Load a file into memory by calling load_line until the file ends. */
1413 load_file (const char *filename, bool initial)
1421 for (f = current_file; f; f = f->up)
1422 if (strcmp (filename, f->filename) == 0)
1424 gfc_error_now ("File '%s' is being included recursively", filename);
1432 input = gfc_src_file;
1433 gfc_src_file = NULL;
1436 input = gfc_open_file (filename);
1439 gfc_error_now ("Can't open file '%s'", filename);
1445 input = gfc_open_included_file (filename, false, false);
1448 gfc_error_now ("Can't open included file '%s'", filename);
1453 /* Load the file. */
1455 f = get_file (filename, initial ? LC_RENAME : LC_ENTER);
1456 f->up = current_file;
1458 current_file->line = 1;
1462 if (initial && gfc_src_preprocessor_lines[0])
1464 preprocessor_line (gfc_src_preprocessor_lines[0]);
1465 gfc_free (gfc_src_preprocessor_lines[0]);
1466 gfc_src_preprocessor_lines[0] = NULL;
1467 if (gfc_src_preprocessor_lines[1])
1469 preprocessor_line (gfc_src_preprocessor_lines[1]);
1470 gfc_free (gfc_src_preprocessor_lines[1]);
1471 gfc_src_preprocessor_lines[1] = NULL;
1477 int trunc = load_line (input, &line, &line_len);
1479 len = strlen (line);
1480 if (feof (input) && len == 0)
1483 /* There are three things this line can be: a line of Fortran
1484 source, an include line or a C preprocessor directive. */
1488 preprocessor_line (line);
1492 if (include_line (line))
1494 current_file->line++;
1500 b = gfc_getmem (gfc_linebuf_header_size + len + 1);
1502 #ifdef USE_MAPPED_LOCATION
1504 = linemap_line_start (&line_table, current_file->line++, 120);
1506 b->linenum = current_file->line++;
1508 b->file = current_file;
1509 b->truncated = trunc;
1510 strcpy (b->line, line);
1512 if (line_head == NULL)
1515 line_tail->next = b;
1520 /* Release the line buffer allocated in load_line. */
1525 current_file = current_file->up;
1526 #ifdef USE_MAPPED_LOCATION
1527 linemap_add (&line_table, LC_LEAVE, 0, NULL, 0);
1533 /* Open a new file and start scanning from that file. Returns SUCCESS
1534 if everything went OK, FAILURE otherwise. If form == FORM_UKNOWN
1535 it tries to determine the source form from the filename, defaulting
1543 result = load_file (gfc_source_file, true);
1545 gfc_current_locus.lb = line_head;
1546 gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line;
1548 #if 0 /* Debugging aid. */
1549 for (; line_head; line_head = line_head->next)
1550 gfc_status ("%s:%3d %s\n", line_head->file->filename,
1551 #ifdef USE_MAPPED_LOCATION
1552 LOCATION_LINE (line_head->location),
1565 unescape_filename (const char *ptr)
1567 const char *p = ptr, *s;
1569 int escaped, unescape = 0;
1571 /* Make filename end at quote. */
1573 while (*p && ! (! escaped && *p == '"'))
1577 else if (*p == '\\')
1588 /* Undo effects of cpp_quote_string. */
1590 d = gfc_getmem (p + 1 - ptr - unescape);
1605 /* For preprocessed files, if the first tokens are of the form # NUM.
1606 handle the directives so we know the original file name. */
1609 gfc_read_orig_filename (const char *filename, const char **canon_source_file)
1614 gfc_src_file = gfc_open_file (filename);
1615 if (gfc_src_file == NULL)
1618 c = fgetc (gfc_src_file);
1619 ungetc (c, gfc_src_file);
1625 load_line (gfc_src_file, &gfc_src_preprocessor_lines[0], &len);
1627 if (strncmp (gfc_src_preprocessor_lines[0], "# 1 \"", 5) != 0)
1630 filename = unescape_filename (gfc_src_preprocessor_lines[0] + 5);
1631 if (filename == NULL)
1634 c = fgetc (gfc_src_file);
1635 ungetc (c, gfc_src_file);
1641 load_line (gfc_src_file, &gfc_src_preprocessor_lines[1], &len);
1643 if (strncmp (gfc_src_preprocessor_lines[1], "# 1 \"", 5) != 0)
1646 dirname = unescape_filename (gfc_src_preprocessor_lines[1] + 5);
1647 if (dirname == NULL)
1650 len = strlen (dirname);
1651 if (len < 3 || dirname[len - 1] != '/' || dirname[len - 2] != '/')
1656 dirname[len - 2] = '\0';
1657 set_src_pwd (dirname);
1659 if (! IS_ABSOLUTE_PATH (filename))
1661 char *p = gfc_getmem (len + strlen (filename));
1663 memcpy (p, dirname, len - 2);
1665 strcpy (p + len - 1, filename);
1666 *canon_source_file = p;