2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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. */
52 /* Structure for holding module and include file search path. */
53 typedef struct gfc_directorylist
57 struct gfc_directorylist *next;
61 /* List of include file search directories. */
62 static gfc_directorylist *include_dirs, *intrinsic_modules_dirs;
64 static gfc_file *file_head, *current_file;
66 static int continue_flag, end_flag, openmp_flag;
67 static int continue_count, continue_line;
68 static locus openmp_locus;
70 gfc_source_form gfc_current_form;
71 static gfc_linebuf *line_head, *line_tail;
73 locus gfc_current_locus;
74 const char *gfc_source_file;
75 static FILE *gfc_src_file;
76 static gfc_char_t *gfc_src_preprocessor_lines[2];
80 static struct gfc_file_change
86 size_t file_changes_cur, file_changes_count;
87 size_t file_changes_allocated;
90 /* Functions dealing with our wide characters (gfc_char_t) and
91 sequences of such characters. */
94 gfc_wide_fits_in_byte (gfc_char_t c)
96 return (c <= UCHAR_MAX);
100 wide_is_ascii (gfc_char_t c)
102 return (gfc_wide_fits_in_byte (c) && ((unsigned char) c & ~0x7f) == 0);
106 gfc_wide_is_printable (gfc_char_t c)
108 return (gfc_wide_fits_in_byte (c) && ISPRINT ((unsigned char) c));
112 gfc_wide_tolower (gfc_char_t c)
114 return (wide_is_ascii (c) ? (gfc_char_t) TOLOWER((unsigned char) c) : c);
118 gfc_wide_toupper (gfc_char_t c)
120 return (wide_is_ascii (c) ? (gfc_char_t) TOUPPER((unsigned char) c) : c);
124 gfc_wide_is_digit (gfc_char_t c)
126 return (c >= '0' && c <= '9');
130 wide_atoi (gfc_char_t *c)
132 #define MAX_DIGITS 20
133 char buf[MAX_DIGITS+1];
136 while (gfc_wide_is_digit(*c) && i < MAX_DIGITS)
143 gfc_wide_strlen (const gfc_char_t *str)
147 for (i = 0; str[i]; i++)
154 gfc_wide_memset (gfc_char_t *b, gfc_char_t c, size_t len)
158 for (i = 0; i < len; i++)
165 wide_strcpy (gfc_char_t *dest, const gfc_char_t *src)
169 for (d = dest; (*d = *src) != '\0'; ++src, ++d)
176 wide_strchr (const gfc_char_t *s, gfc_char_t c)
181 return CONST_CAST(gfc_char_t *, s);
188 gfc_widechar_to_char (const gfc_char_t *s, int length)
196 /* Passing a negative length is used to indicate that length should be
197 calculated using gfc_wide_strlen(). */
198 len = (length >= 0 ? (size_t) length : gfc_wide_strlen (s));
199 res = gfc_getmem (len + 1);
201 for (i = 0; i < len; i++)
203 gcc_assert (gfc_wide_fits_in_byte (s[i]));
204 res[i] = (unsigned char) s[i];
212 gfc_char_to_widechar (const char *s)
221 res = gfc_get_wide_string (len + 1);
223 for (i = 0; i < len; i++)
224 res[i] = (unsigned char) s[i];
231 wide_strncmp (const gfc_char_t *s1, const char *s2, size_t n)
240 return (c1 > c2 ? 1 : -1);
248 gfc_wide_strncasecmp (const gfc_char_t *s1, const char *s2, size_t n)
254 c1 = gfc_wide_tolower (*s1++);
255 c2 = TOLOWER (*s2++);
257 return (c1 > c2 ? 1 : -1);
265 /* Main scanner initialization. */
268 gfc_scanner_init_1 (void)
281 /* Main scanner destructor. */
284 gfc_scanner_done_1 (void)
289 while(line_head != NULL)
291 lb = line_head->next;
296 while(file_head != NULL)
299 gfc_free(file_head->filename);
306 /* Adds path to the list pointed to by list. */
309 add_path_to_list (gfc_directorylist **list, const char *path,
310 bool use_for_modules)
312 gfc_directorylist *dir;
316 while (*p == ' ' || *p == '\t') /* someone might do "-I include" */
322 dir = *list = gfc_getmem (sizeof (gfc_directorylist));
328 dir->next = gfc_getmem (sizeof (gfc_directorylist));
333 dir->use_for_modules = use_for_modules;
334 dir->path = gfc_getmem (strlen (p) + 2);
335 strcpy (dir->path, p);
336 strcat (dir->path, "/"); /* make '/' last character */
341 gfc_add_include_path (const char *path, bool use_for_modules)
343 add_path_to_list (&include_dirs, path, use_for_modules);
344 gfc_cpp_add_include_path (xstrdup(path), true);
349 gfc_add_intrinsic_modules_path (const char *path)
351 add_path_to_list (&intrinsic_modules_dirs, path, true);
355 /* Release resources allocated for options. */
358 gfc_release_include_path (void)
360 gfc_directorylist *p;
362 while (include_dirs != NULL)
365 include_dirs = include_dirs->next;
370 while (intrinsic_modules_dirs != NULL)
372 p = intrinsic_modules_dirs;
373 intrinsic_modules_dirs = intrinsic_modules_dirs->next;
378 gfc_free (gfc_option.module_dir);
383 open_included_file (const char *name, gfc_directorylist *list, bool module)
386 gfc_directorylist *p;
389 for (p = list; p; p = p->next)
391 if (module && !p->use_for_modules)
394 fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1);
395 strcpy (fullname, p->path);
396 strcat (fullname, name);
398 f = gfc_open_file (fullname);
407 /* Opens file for reading, searching through the include directories
408 given if necessary. If the include_cwd argument is true, we try
409 to open the file in the current directory first. */
412 gfc_open_included_file (const char *name, bool include_cwd, bool module)
416 if (IS_ABSOLUTE_PATH (name))
417 return gfc_open_file (name);
421 f = gfc_open_file (name);
426 return open_included_file (name, include_dirs, module);
430 gfc_open_intrinsic_module (const char *name)
432 if (IS_ABSOLUTE_PATH (name))
433 return gfc_open_file (name);
435 return open_included_file (name, intrinsic_modules_dirs, true);
439 /* Test to see if we're at the end of the main source file. */
448 /* Test to see if we're at the end of the current file. */
456 if (line_head == NULL)
457 return 1; /* Null file */
459 if (gfc_current_locus.lb == NULL)
466 /* Test to see if we're at the beginning of a new line. */
474 return (gfc_current_locus.nextc == gfc_current_locus.lb->line);
478 /* Test to see if we're at the end of a line. */
486 return (*gfc_current_locus.nextc == '\0');
490 add_file_change (const char *filename, int line)
492 if (file_changes_count == file_changes_allocated)
494 if (file_changes_allocated)
495 file_changes_allocated *= 2;
497 file_changes_allocated = 16;
499 = xrealloc (file_changes,
500 file_changes_allocated * sizeof (*file_changes));
502 file_changes[file_changes_count].filename = filename;
503 file_changes[file_changes_count].lb = NULL;
504 file_changes[file_changes_count++].line = line;
508 report_file_change (gfc_linebuf *lb)
510 size_t c = file_changes_cur;
511 while (c < file_changes_count
512 && file_changes[c].lb == lb)
514 if (file_changes[c].filename)
515 (*debug_hooks->start_source_file) (file_changes[c].line,
516 file_changes[c].filename);
518 (*debug_hooks->end_source_file) (file_changes[c].line);
521 file_changes_cur = c;
525 gfc_start_source_files (void)
527 /* If the debugger wants the name of the main source file,
529 if (debug_hooks->start_end_main_source_file)
530 (*debug_hooks->start_source_file) (0, gfc_source_file);
532 file_changes_cur = 0;
533 report_file_change (gfc_current_locus.lb);
537 gfc_end_source_files (void)
539 report_file_change (NULL);
541 if (debug_hooks->start_end_main_source_file)
542 (*debug_hooks->end_source_file) (0);
545 /* Advance the current line pointer to the next line. */
548 gfc_advance_line (void)
553 if (gfc_current_locus.lb == NULL)
559 if (gfc_current_locus.lb->next
560 && !gfc_current_locus.lb->next->dbg_emitted)
562 report_file_change (gfc_current_locus.lb->next);
563 gfc_current_locus.lb->next->dbg_emitted = true;
566 gfc_current_locus.lb = gfc_current_locus.lb->next;
568 if (gfc_current_locus.lb != NULL)
569 gfc_current_locus.nextc = gfc_current_locus.lb->line;
572 gfc_current_locus.nextc = NULL;
578 /* Get the next character from the input, advancing gfc_current_file's
579 locus. When we hit the end of the line or the end of the file, we
580 start returning a '\n' in order to complete the current statement.
581 No Fortran line conventions are implemented here.
583 Requiring explicit advances to the next line prevents the parse
584 pointer from being on the wrong line if the current statement ends
592 if (gfc_current_locus.nextc == NULL)
595 c = *gfc_current_locus.nextc++;
598 gfc_current_locus.nextc--; /* Remain on this line. */
606 /* Skip a comment. When we come here the parse pointer is positioned
607 immediately after the comment character. If we ever implement
608 compiler directives withing comments, here is where we parse the
612 skip_comment_line (void)
627 gfc_define_undef_line (void)
631 /* All lines beginning with '#' are either #define or #undef. */
632 if (debug_info_level != DINFO_LEVEL_VERBOSE || gfc_peek_ascii_char () != '#')
635 if (wide_strncmp (gfc_current_locus.nextc, "#define ", 8) == 0)
637 tmp = gfc_widechar_to_char (&gfc_current_locus.nextc[8], -1);
638 (*debug_hooks->define) (gfc_linebuf_linenum (gfc_current_locus.lb),
643 if (wide_strncmp (gfc_current_locus.nextc, "#undef ", 7) == 0)
645 tmp = gfc_widechar_to_char (&gfc_current_locus.nextc[7], -1);
646 (*debug_hooks->undef) (gfc_linebuf_linenum (gfc_current_locus.lb),
651 /* Skip the rest of the line. */
652 skip_comment_line ();
658 /* Comment lines are null lines, lines containing only blanks or lines
659 on which the first nonblank line is a '!'.
660 Return true if !$ openmp conditional compilation sentinel was
664 skip_free_comments (void)
672 at_bol = gfc_at_bol ();
673 start = gfc_current_locus;
679 while (gfc_is_whitespace (c));
689 /* If -fopenmp, we need to handle here 2 things:
690 1) don't treat !$omp as comments, but directives
691 2) handle OpenMP conditional compilation, where
692 !$ should be treated as 2 spaces (for initial lines
693 only if followed by space). */
694 if (gfc_option.flag_openmp && at_bol)
696 locus old_loc = gfc_current_locus;
697 if (next_char () == '$')
700 if (c == 'o' || c == 'O')
702 if (((c = next_char ()) == 'm' || c == 'M')
703 && ((c = next_char ()) == 'p' || c == 'P'))
705 if ((c = next_char ()) == ' ' || continue_flag)
707 while (gfc_is_whitespace (c))
709 if (c != '\n' && c != '!')
712 openmp_locus = old_loc;
713 gfc_current_locus = start;
718 gfc_warning_now ("!$OMP at %C starts a commented "
719 "line as it neither is followed "
720 "by a space nor is a "
721 "continuation line");
723 gfc_current_locus = old_loc;
727 if (continue_flag || c == ' ')
729 gfc_current_locus = old_loc;
735 gfc_current_locus = old_loc;
737 skip_comment_line ();
744 if (openmp_flag && at_bol)
746 gfc_current_locus = start;
751 /* Skip comment lines in fixed source mode. We have the same rules as
752 in skip_free_comment(), except that we can have a 'c', 'C' or '*'
753 in column 1, and a '!' cannot be in column 6. Also, we deal with
754 lines with 'd' or 'D' in column 1, if the user requested this. */
757 skip_fixed_comments (void)
765 start = gfc_current_locus;
770 while (gfc_is_whitespace (c));
775 skip_comment_line ();
780 gfc_current_locus = start;
787 start = gfc_current_locus;
798 if (c == '!' || c == 'c' || c == 'C' || c == '*')
800 /* If -fopenmp, we need to handle here 2 things:
801 1) don't treat !$omp|c$omp|*$omp as comments, but directives
802 2) handle OpenMP conditional compilation, where
803 !$|c$|*$ should be treated as 2 spaces if the characters
804 in columns 3 to 6 are valid fixed form label columns
806 if (gfc_current_locus.lb != NULL
807 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
808 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
810 if (gfc_option.flag_openmp)
812 if (next_char () == '$')
815 if (c == 'o' || c == 'O')
817 if (((c = next_char ()) == 'm' || c == 'M')
818 && ((c = next_char ()) == 'p' || c == 'P'))
822 && ((openmp_flag && continue_flag)
823 || c == ' ' || c == '0'))
826 while (gfc_is_whitespace (c))
828 if (c != '\n' && c != '!')
830 /* Canonicalize to *$omp. */
833 gfc_current_locus = start;
843 for (col = 3; col < 6; col++, c = next_char ())
846 else if (c < '0' || c > '9')
851 if (col == 6 && c != '\n'
852 && ((continue_flag && !digit_seen)
853 || c == ' ' || c == '0'))
855 gfc_current_locus = start;
856 start.nextc[0] = ' ';
857 start.nextc[1] = ' ';
862 gfc_current_locus = start;
864 skip_comment_line ();
868 if (gfc_option.flag_d_lines != -1 && (c == 'd' || c == 'D'))
870 if (gfc_option.flag_d_lines == 0)
872 skip_comment_line ();
876 *start.nextc = c = ' ';
881 while (gfc_is_whitespace (c))
893 if (col != 6 && c == '!')
895 if (gfc_current_locus.lb != NULL
896 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
897 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
898 skip_comment_line ();
906 gfc_current_locus = start;
910 /* Skips the current line if it is a comment. */
913 gfc_skip_comments (void)
915 if (gfc_current_form == FORM_FREE)
916 skip_free_comments ();
918 skip_fixed_comments ();
922 /* Get the next character from the input, taking continuation lines
923 and end-of-line comments into account. This implies that comment
924 lines between continued lines must be eaten here. For higher-level
925 subroutines, this flattens continued lines into a single logical
926 line. The in_string flag denotes whether we're inside a character
930 gfc_next_char_literal (int in_string)
933 int i, prev_openmp_flag;
946 if (gfc_current_form == FORM_FREE)
948 bool openmp_cond_flag;
950 if (!in_string && c == '!')
953 && memcmp (&gfc_current_locus, &openmp_locus,
954 sizeof (gfc_current_locus)) == 0)
957 /* This line can't be continued */
964 /* Avoid truncation warnings for comment ending lines. */
965 gfc_current_locus.lb->truncated = 0;
973 /* If the next nonblank character is a ! or \n, we've got a
974 continuation line. */
975 old_loc = gfc_current_locus;
978 while (gfc_is_whitespace (c))
981 /* Character constants to be continued cannot have commentary
984 if (in_string && c != '\n')
986 gfc_current_locus = old_loc;
991 if (c != '!' && c != '\n')
993 gfc_current_locus = old_loc;
998 prev_openmp_flag = openmp_flag;
1001 skip_comment_line ();
1003 gfc_advance_line ();
1006 goto not_continuation;
1008 /* We've got a continuation line. If we are on the very next line after
1009 the last continuation, increment the continuation line count and
1010 check whether the limit has been exceeded. */
1011 if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1)
1013 if (++continue_count == gfc_option.max_continue_free)
1015 if (gfc_notification_std (GFC_STD_GNU) || pedantic)
1016 gfc_warning ("Limit of %d continuations exceeded in "
1017 "statement at %C", gfc_option.max_continue_free);
1021 /* Now find where it continues. First eat any comment lines. */
1022 openmp_cond_flag = skip_free_comments ();
1024 if (gfc_current_locus.lb != NULL
1025 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
1026 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
1028 if (prev_openmp_flag != openmp_flag)
1030 gfc_current_locus = old_loc;
1031 openmp_flag = prev_openmp_flag;
1036 /* Now that we have a non-comment line, probe ahead for the
1037 first non-whitespace character. If it is another '&', then
1038 reading starts at the next character, otherwise we must back
1039 up to where the whitespace started and resume from there. */
1041 old_loc = gfc_current_locus;
1044 while (gfc_is_whitespace (c))
1049 for (i = 0; i < 5; i++, c = next_char ())
1051 gcc_assert (gfc_wide_tolower (c) == (unsigned char) "!$omp"[i]);
1053 old_loc = gfc_current_locus;
1055 while (gfc_is_whitespace (c))
1063 if (gfc_option.warn_ampersand)
1064 gfc_warning_now ("Missing '&' in continued character "
1066 gfc_current_locus.nextc--;
1068 /* Both !$omp and !$ -fopenmp continuation lines have & on the
1069 continuation line only optionally. */
1070 else if (openmp_flag || openmp_cond_flag)
1071 gfc_current_locus.nextc--;
1075 gfc_current_locus = old_loc;
1082 /* Fixed form continuation. */
1083 if (!in_string && c == '!')
1085 /* Skip comment at end of line. */
1092 /* Avoid truncation warnings for comment ending lines. */
1093 gfc_current_locus.lb->truncated = 0;
1099 prev_openmp_flag = openmp_flag;
1101 old_loc = gfc_current_locus;
1103 gfc_advance_line ();
1104 skip_fixed_comments ();
1106 /* See if this line is a continuation line. */
1107 if (openmp_flag != prev_openmp_flag)
1109 openmp_flag = prev_openmp_flag;
1110 goto not_continuation;
1114 for (i = 0; i < 5; i++)
1118 goto not_continuation;
1121 for (i = 0; i < 5; i++)
1124 if (gfc_wide_tolower (c) != (unsigned char) "*$omp"[i])
1125 goto not_continuation;
1129 if (c == '0' || c == ' ' || c == '\n')
1130 goto not_continuation;
1132 /* We've got a continuation line. If we are on the very next line after
1133 the last continuation, increment the continuation line count and
1134 check whether the limit has been exceeded. */
1135 if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1)
1137 if (++continue_count == gfc_option.max_continue_fixed)
1139 if (gfc_notification_std (GFC_STD_GNU) || pedantic)
1140 gfc_warning ("Limit of %d continuations exceeded in "
1142 gfc_option.max_continue_fixed);
1146 if (gfc_current_locus.lb != NULL
1147 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
1148 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
1151 /* Ready to read first character of continuation line, which might
1152 be another continuation line! */
1157 gfc_current_locus = old_loc;
1167 /* Get the next character of input, folded to lowercase. In fixed
1168 form mode, we also ignore spaces. When matcher subroutines are
1169 parsing character literals, they have to call
1170 gfc_next_char_literal(). */
1173 gfc_next_char (void)
1179 c = gfc_next_char_literal (0);
1181 while (gfc_current_form == FORM_FIXED && gfc_is_whitespace (c));
1183 return gfc_wide_tolower (c);
1187 gfc_next_ascii_char (void)
1189 gfc_char_t c = gfc_next_char ();
1191 return (gfc_wide_fits_in_byte (c) ? (unsigned char) c
1192 : (unsigned char) UCHAR_MAX);
1197 gfc_peek_char (void)
1202 old_loc = gfc_current_locus;
1203 c = gfc_next_char ();
1204 gfc_current_locus = old_loc;
1211 gfc_peek_ascii_char (void)
1213 gfc_char_t c = gfc_peek_char ();
1215 return (gfc_wide_fits_in_byte (c) ? (unsigned char) c
1216 : (unsigned char) UCHAR_MAX);
1220 /* Recover from an error. We try to get past the current statement
1221 and get lined up for the next. The next statement follows a '\n'
1222 or a ';'. We also assume that we are not within a character
1223 constant, and deal with finding a '\'' or '"'. */
1226 gfc_error_recovery (void)
1228 gfc_char_t c, delim;
1235 c = gfc_next_char ();
1236 if (c == '\n' || c == ';')
1239 if (c != '\'' && c != '"')
1268 /* Read ahead until the next character to be read is not whitespace. */
1271 gfc_gobble_whitespace (void)
1273 static int linenum = 0;
1279 old_loc = gfc_current_locus;
1280 c = gfc_next_char_literal (0);
1281 /* Issue a warning for nonconforming tabs. We keep track of the line
1282 number because the Fortran matchers will often back up and the same
1283 line will be scanned multiple times. */
1284 if (!gfc_option.warn_tabs && c == '\t')
1286 int cur_linenum = LOCATION_LINE (gfc_current_locus.lb->location);
1287 if (cur_linenum != linenum)
1289 linenum = cur_linenum;
1290 gfc_warning_now ("Nonconforming tab character at %C");
1294 while (gfc_is_whitespace (c));
1296 gfc_current_locus = old_loc;
1300 /* Load a single line into pbuf.
1302 If pbuf points to a NULL pointer, it is allocated.
1303 We truncate lines that are too long, unless we're dealing with
1304 preprocessor lines or if the option -ffixed-line-length-none is set,
1305 in which case we reallocate the buffer to fit the entire line, if
1307 In fixed mode, we expand a tab that occurs within the statement
1308 label region to expand to spaces that leave the next character in
1311 If first_char is not NULL, it's a pointer to a single char value holding
1312 the first character of the line, which has already been read by the
1313 caller. This avoids the use of ungetc().
1315 load_line returns whether the line was truncated.
1317 NOTE: The error machinery isn't available at this point, so we can't
1318 easily report line and column numbers consistent with other
1319 parts of gfortran. */
1322 load_line (FILE *input, gfc_char_t **pbuf, int *pbuflen, const int *first_char)
1324 static int linenum = 0, current_line = 1;
1325 int c, maxlen, i, preprocessor_flag, buflen = *pbuflen;
1326 int trunc_flag = 0, seen_comment = 0;
1327 int seen_printable = 0, seen_ampersand = 0;
1329 bool found_tab = false;
1331 /* Determine the maximum allowed line length. */
1332 if (gfc_current_form == FORM_FREE)
1333 maxlen = gfc_option.free_line_length;
1334 else if (gfc_current_form == FORM_FIXED)
1335 maxlen = gfc_option.fixed_line_length;
1341 /* Allocate the line buffer, storing its length into buflen.
1342 Note that if maxlen==0, indicating that arbitrary-length lines
1343 are allowed, the buffer will be reallocated if this length is
1344 insufficient; since 132 characters is the length of a standard
1345 free-form line, we use that as a starting guess. */
1351 *pbuf = gfc_get_wide_string (buflen + 1);
1362 /* In order to not truncate preprocessor lines, we have to
1363 remember that this is one. */
1364 preprocessor_flag = (c == '#' ? 1 : 0);
1373 /* Check for illegal use of ampersand. See F95 Standard 3.3.1.3. */
1374 if (gfc_current_form == FORM_FREE
1375 && !seen_printable && seen_ampersand)
1378 gfc_error_now ("'&' not allowed by itself in line %d",
1381 gfc_warning_now ("'&' not allowed by itself in line %d",
1387 if (c == '\r' || c == '\0')
1388 goto next_char; /* Gobble characters. */
1398 if ((c != '&' && c != '!' && c != ' ') || (c == '!' && !seen_ampersand))
1401 /* Is this a fixed-form comment? */
1402 if (gfc_current_form == FORM_FIXED && i == 0
1403 && (c == '*' || c == 'c' || c == 'd'))
1406 /* Vendor extension: "<tab>1" marks a continuation line. */
1410 if (c >= '1' && c <= '9')
1417 if (gfc_current_form == FORM_FIXED && c == '\t' && i < 6)
1421 if (!gfc_option.warn_tabs && seen_comment == 0
1422 && current_line != linenum)
1424 linenum = current_line;
1425 gfc_warning_now ("Nonconforming tab character in column %d "
1426 "of line %d", i+1, linenum);
1441 if (maxlen == 0 || preprocessor_flag)
1445 /* Reallocate line buffer to double size to hold the
1447 buflen = buflen * 2;
1448 *pbuf = xrealloc (*pbuf, (buflen + 1) * sizeof (gfc_char_t));
1449 buffer = (*pbuf) + i;
1452 else if (i >= maxlen)
1454 /* Truncate the rest of the line. */
1458 if (c == '\n' || c == EOF)
1472 /* Pad lines to the selected line length in fixed form. */
1473 if (gfc_current_form == FORM_FIXED
1474 && gfc_option.fixed_line_length != 0
1475 && !preprocessor_flag
1478 while (i++ < maxlen)
1490 /* Get a gfc_file structure, initialize it and add it to
1494 get_file (const char *name, enum lc_reason reason ATTRIBUTE_UNUSED)
1498 f = gfc_getmem (sizeof (gfc_file));
1500 f->filename = gfc_getmem (strlen (name) + 1);
1501 strcpy (f->filename, name);
1503 f->next = file_head;
1506 f->up = current_file;
1507 if (current_file != NULL)
1508 f->inclusion_line = current_file->line;
1510 linemap_add (line_table, reason, false, f->filename, 1);
1516 /* Deal with a line from the C preprocessor. The
1517 initial octothorp has already been seen. */
1520 preprocessor_line (gfc_char_t *c)
1524 gfc_char_t *wide_filename;
1526 int escaped, unescape;
1530 while (*c == ' ' || *c == '\t')
1533 if (*c < '0' || *c > '9')
1536 line = wide_atoi (c);
1538 c = wide_strchr (c, ' ');
1541 /* No file name given. Set new line number. */
1542 current_file->line = line;
1547 while (*c == ' ' || *c == '\t')
1557 /* Make filename end at quote. */
1560 while (*c && ! (!escaped && *c == '"'))
1564 else if (*c == '\\')
1573 /* Preprocessor line has no closing quote. */
1578 /* Undo effects of cpp_quote_string. */
1581 gfc_char_t *s = wide_filename;
1582 gfc_char_t *d = gfc_get_wide_string (c - wide_filename - unescape);
1598 flag[1] = flag[2] = flag[3] = flag[4] = false;
1602 c = wide_strchr (c, ' ');
1609 if (1 <= i && i <= 4)
1613 /* Convert the filename in wide characters into a filename in narrow
1615 filename = gfc_widechar_to_char (wide_filename, -1);
1617 /* Interpret flags. */
1619 if (flag[1]) /* Starting new file. */
1621 f = get_file (filename, LC_RENAME);
1622 add_file_change (f->filename, f->inclusion_line);
1626 if (flag[2]) /* Ending current file. */
1628 if (!current_file->up
1629 || strcmp (current_file->up->filename, filename) != 0)
1631 gfc_warning_now ("%s:%d: file %s left but not entered",
1632 current_file->filename, current_file->line,
1635 gfc_free (wide_filename);
1636 gfc_free (filename);
1640 add_file_change (NULL, line);
1641 current_file = current_file->up;
1642 linemap_add (line_table, LC_RENAME, false, current_file->filename,
1643 current_file->line);
1646 /* The name of the file can be a temporary file produced by
1647 cpp. Replace the name if it is different. */
1649 if (strcmp (current_file->filename, filename) != 0)
1651 gfc_free (current_file->filename);
1652 current_file->filename = gfc_getmem (strlen (filename) + 1);
1653 strcpy (current_file->filename, filename);
1656 /* Set new line number. */
1657 current_file->line = line;
1659 gfc_free (wide_filename);
1660 gfc_free (filename);
1664 gfc_warning_now ("%s:%d: Illegal preprocessor directive",
1665 current_file->filename, current_file->line);
1666 current_file->line++;
1670 static try load_file (const char *, bool);
1672 /* include_line()-- Checks a line buffer to see if it is an include
1673 line. If so, we call load_file() recursively to load the included
1674 file. We never return a syntax error because a statement like
1675 "include = 5" is perfectly legal. We return false if no include was
1676 processed or true if we matched an include. */
1679 include_line (gfc_char_t *line)
1681 gfc_char_t quote, *c, *begin, *stop;
1686 if (gfc_option.flag_openmp)
1688 if (gfc_current_form == FORM_FREE)
1690 while (*c == ' ' || *c == '\t')
1692 if (*c == '!' && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
1697 if ((*c == '!' || *c == 'c' || *c == 'C' || *c == '*')
1698 && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
1703 while (*c == ' ' || *c == '\t')
1706 if (gfc_wide_strncasecmp (c, "include", 7))
1710 while (*c == ' ' || *c == '\t')
1713 /* Find filename between quotes. */
1716 if (quote != '"' && quote != '\'')
1721 while (*c != quote && *c != '\0')
1729 while (*c == ' ' || *c == '\t')
1732 if (*c != '\0' && *c != '!')
1735 /* We have an include line at this point. */
1737 *stop = '\0'; /* It's ok to trash the buffer, as this line won't be
1738 read by anything else. */
1740 filename = gfc_widechar_to_char (begin, -1);
1741 load_file (filename, false);
1742 gfc_free (filename);
1747 /* Load a file into memory by calling load_line until the file ends. */
1750 load_file (const char *filename, bool initial)
1759 for (f = current_file; f; f = f->up)
1760 if (strcmp (filename, f->filename) == 0)
1762 gfc_error_now ("File '%s' is being included recursively", filename);
1770 input = gfc_src_file;
1771 gfc_src_file = NULL;
1774 input = gfc_open_file (filename);
1777 gfc_error_now ("Can't open file '%s'", filename);
1783 input = gfc_open_included_file (filename, false, false);
1786 gfc_error_now ("Can't open included file '%s'", filename);
1791 /* Load the file. */
1793 f = get_file (filename, initial ? LC_RENAME : LC_ENTER);
1795 add_file_change (f->filename, f->inclusion_line);
1797 current_file->line = 1;
1802 if (initial && gfc_src_preprocessor_lines[0])
1804 preprocessor_line (gfc_src_preprocessor_lines[0]);
1805 gfc_free (gfc_src_preprocessor_lines[0]);
1806 gfc_src_preprocessor_lines[0] = NULL;
1807 if (gfc_src_preprocessor_lines[1])
1809 preprocessor_line (gfc_src_preprocessor_lines[1]);
1810 gfc_free (gfc_src_preprocessor_lines[1]);
1811 gfc_src_preprocessor_lines[1] = NULL;
1817 int trunc = load_line (input, &line, &line_len, NULL);
1819 len = gfc_wide_strlen (line);
1820 if (feof (input) && len == 0)
1823 /* If this is the first line of the file, it can contain a byte
1824 order mark (BOM), which we will ignore:
1825 FF FE is UTF-16 little endian,
1826 FE FF is UTF-16 big endian,
1827 EF BB BF is UTF-8. */
1829 && ((line_len >= 2 && line[0] == (unsigned char) '\xFF'
1830 && line[1] == (unsigned char) '\xFE')
1831 || (line_len >= 2 && line[0] == (unsigned char) '\xFE'
1832 && line[1] == (unsigned char) '\xFF')
1833 || (line_len >= 3 && line[0] == (unsigned char) '\xEF'
1834 && line[1] == (unsigned char) '\xBB'
1835 && line[2] == (unsigned char) '\xBF')))
1837 int n = line[1] == (unsigned char) '\xBB' ? 3 : 2;
1838 gfc_char_t *new = gfc_get_wide_string (line_len);
1840 wide_strcpy (new, &line[n]);
1846 /* There are three things this line can be: a line of Fortran
1847 source, an include line or a C preprocessor directive. */
1851 /* When -g3 is specified, it's possible that we emit #define
1852 and #undef lines, which we need to pass to the middle-end
1853 so that it can emit correct debug info. */
1854 if (debug_info_level == DINFO_LEVEL_VERBOSE
1855 && (wide_strncmp (line, "#define ", 8) == 0
1856 || wide_strncmp (line, "#undef ", 7) == 0))
1860 preprocessor_line (line);
1865 /* Preprocessed files have preprocessor lines added before the byte
1866 order mark, so first_line is not about the first line of the file
1867 but the first line that's not a preprocessor line. */
1870 if (include_line (line))
1872 current_file->line++;
1878 b = gfc_getmem (gfc_linebuf_header_size
1879 + (len + 1) * sizeof (gfc_char_t));
1882 = linemap_line_start (line_table, current_file->line++, 120);
1883 b->file = current_file;
1884 b->truncated = trunc;
1885 wide_strcpy (b->line, line);
1887 if (line_head == NULL)
1890 line_tail->next = b;
1894 while (file_changes_cur < file_changes_count)
1895 file_changes[file_changes_cur++].lb = b;
1898 /* Release the line buffer allocated in load_line. */
1904 add_file_change (NULL, current_file->inclusion_line + 1);
1905 current_file = current_file->up;
1906 linemap_add (line_table, LC_LEAVE, 0, NULL, 0);
1911 /* Open a new file and start scanning from that file. Returns SUCCESS
1912 if everything went OK, FAILURE otherwise. If form == FORM_UKNOWN
1913 it tries to determine the source form from the filename, defaulting
1921 if (gfc_cpp_enabled ())
1923 result = gfc_cpp_preprocess (gfc_source_file);
1924 if (!gfc_cpp_preprocess_only ())
1925 result = load_file (gfc_cpp_temporary_file (), true);
1928 result = load_file (gfc_source_file, true);
1930 gfc_current_locus.lb = line_head;
1931 gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line;
1933 #if 0 /* Debugging aid. */
1934 for (; line_head; line_head = line_head->next)
1935 printf ("%s:%3d %s\n", LOCATION_FILE (line_head->location),
1936 LOCATION_LINE (line_head->location), line_head->line);
1945 unescape_filename (const char *ptr)
1947 const char *p = ptr, *s;
1949 int escaped, unescape = 0;
1951 /* Make filename end at quote. */
1953 while (*p && ! (! escaped && *p == '"'))
1957 else if (*p == '\\')
1968 /* Undo effects of cpp_quote_string. */
1970 d = gfc_getmem (p + 1 - ptr - unescape);
1985 /* For preprocessed files, if the first tokens are of the form # NUM.
1986 handle the directives so we know the original file name. */
1989 gfc_read_orig_filename (const char *filename, const char **canon_source_file)
1992 char *dirname, *tmp;
1994 gfc_src_file = gfc_open_file (filename);
1995 if (gfc_src_file == NULL)
1998 c = getc (gfc_src_file);
2004 load_line (gfc_src_file, &gfc_src_preprocessor_lines[0], &len, &c);
2006 if (wide_strncmp (gfc_src_preprocessor_lines[0], "# 1 \"", 5) != 0)
2009 tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[0][5], -1);
2010 filename = unescape_filename (tmp);
2012 if (filename == NULL)
2015 c = getc (gfc_src_file);
2021 load_line (gfc_src_file, &gfc_src_preprocessor_lines[1], &len, &c);
2023 if (wide_strncmp (gfc_src_preprocessor_lines[1], "# 1 \"", 5) != 0)
2026 tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[1][5], -1);
2027 dirname = unescape_filename (tmp);
2029 if (dirname == NULL)
2032 len = strlen (dirname);
2033 if (len < 3 || dirname[len - 1] != '/' || dirname[len - 2] != '/')
2038 dirname[len - 2] = '\0';
2039 set_src_pwd (dirname);
2041 if (! IS_ABSOLUTE_PATH (filename))
2043 char *p = gfc_getmem (len + strlen (filename));
2045 memcpy (p, dirname, len - 2);
2047 strcpy (p + len - 1, filename);
2048 *canon_source_file = p;