Check in tree-dce enh to trunk
[official-gcc.git] / gcc / fortran / scanner.c
blob13e0615528386bd90f8b6ae56bbcd63d65c3db4f
1 /* Character scanner.
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
11 version.
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
16 for more details.
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
31 parsing.
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
39 truncated stuff.
41 From the scanner's viewpoint, the higher level subroutines ask for
42 new characters and do a lot of jumping backwards. */
44 #include "config.h"
45 #include "system.h"
46 #include "gfortran.h"
47 #include "toplev.h"
48 #include "debug.h"
49 #include "flags.h"
51 /* Structure for holding module and include file search path. */
52 typedef struct gfc_directorylist
54 char *path;
55 bool use_for_modules;
56 struct gfc_directorylist *next;
58 gfc_directorylist;
60 /* List of include file search directories. */
61 static gfc_directorylist *include_dirs, *intrinsic_modules_dirs;
63 static gfc_file *file_head, *current_file;
65 static int continue_flag, end_flag, openmp_flag;
66 static int continue_count, continue_line;
67 static locus openmp_locus;
69 gfc_source_form gfc_current_form;
70 static gfc_linebuf *line_head, *line_tail;
72 locus gfc_current_locus;
73 const char *gfc_source_file;
74 static FILE *gfc_src_file;
75 static gfc_char_t *gfc_src_preprocessor_lines[2];
77 extern int pedantic;
79 static struct gfc_file_change
81 const char *filename;
82 gfc_linebuf *lb;
83 int line;
84 } *file_changes;
85 size_t file_changes_cur, file_changes_count;
86 size_t file_changes_allocated;
89 /* Functions dealing with our wide characters (gfc_char_t) and
90 sequences of such characters. */
92 int
93 gfc_wide_fits_in_byte (gfc_char_t c)
95 return (c <= UCHAR_MAX);
98 static inline int
99 wide_is_ascii (gfc_char_t c)
101 return (gfc_wide_fits_in_byte (c) && ((unsigned char) c & ~0x7f) == 0);
105 gfc_wide_is_printable (gfc_char_t c)
107 return (gfc_wide_fits_in_byte (c) && ISPRINT ((unsigned char) c));
110 gfc_char_t
111 gfc_wide_tolower (gfc_char_t c)
113 return (wide_is_ascii (c) ? (gfc_char_t) TOLOWER((unsigned char) c) : c);
116 gfc_char_t
117 gfc_wide_toupper (gfc_char_t c)
119 return (wide_is_ascii (c) ? (gfc_char_t) TOUPPER((unsigned char) c) : c);
123 gfc_wide_is_digit (gfc_char_t c)
125 return (c >= '0' && c <= '9');
128 static inline int
129 wide_atoi (gfc_char_t *c)
131 #define MAX_DIGITS 20
132 char buf[MAX_DIGITS+1];
133 int i = 0;
135 while (gfc_wide_is_digit(*c) && i < MAX_DIGITS)
136 buf[i++] = *c++;
137 buf[i] = '\0';
138 return atoi (buf);
141 size_t
142 gfc_wide_strlen (const gfc_char_t *str)
144 size_t i;
146 for (i = 0; str[i]; i++)
149 return i;
152 gfc_char_t *
153 gfc_wide_memset (gfc_char_t *b, gfc_char_t c, size_t len)
155 size_t i;
157 for (i = 0; i < len; i++)
158 b[i] = c;
160 return b;
163 static gfc_char_t *
164 wide_strcpy (gfc_char_t *dest, const gfc_char_t *src)
166 gfc_char_t *d;
168 for (d = dest; (*d = *src) != '\0'; ++src, ++d)
171 return dest;
174 static gfc_char_t *
175 wide_strchr (const gfc_char_t *s, gfc_char_t c)
177 do {
178 if (*s == c)
180 return CONST_CAST(gfc_char_t *, s);
182 } while (*s++);
183 return 0;
186 char *
187 gfc_widechar_to_char (const gfc_char_t *s, int length)
189 size_t len, i;
190 char *res;
192 if (s == NULL)
193 return NULL;
195 /* Passing a negative length is used to indicate that length should be
196 calculated using gfc_wide_strlen(). */
197 len = (length >= 0 ? (size_t) length : gfc_wide_strlen (s));
198 res = gfc_getmem (len + 1);
200 for (i = 0; i < len; i++)
202 gcc_assert (gfc_wide_fits_in_byte (s[i]));
203 res[i] = (unsigned char) s[i];
206 res[len] = '\0';
207 return res;
210 gfc_char_t *
211 gfc_char_to_widechar (const char *s)
213 size_t len, i;
214 gfc_char_t *res;
216 if (s == NULL)
217 return NULL;
219 len = strlen (s);
220 res = gfc_get_wide_string (len + 1);
222 for (i = 0; i < len; i++)
223 res[i] = (unsigned char) s[i];
225 res[len] = '\0';
226 return res;
229 static int
230 wide_strncmp (const gfc_char_t *s1, const char *s2, size_t n)
232 gfc_char_t c1, c2;
234 while (n-- > 0)
236 c1 = *s1++;
237 c2 = *s2++;
238 if (c1 != c2)
239 return (c1 > c2 ? 1 : -1);
240 if (c1 == '\0')
241 return 0;
243 return 0;
247 gfc_wide_strncasecmp (const gfc_char_t *s1, const char *s2, size_t n)
249 gfc_char_t c1, c2;
251 while (n-- > 0)
253 c1 = gfc_wide_tolower (*s1++);
254 c2 = TOLOWER (*s2++);
255 if (c1 != c2)
256 return (c1 > c2 ? 1 : -1);
257 if (c1 == '\0')
258 return 0;
260 return 0;
264 /* Main scanner initialization. */
266 void
267 gfc_scanner_init_1 (void)
269 file_head = NULL;
270 line_head = NULL;
271 line_tail = NULL;
273 continue_count = 0;
274 continue_line = 0;
276 end_flag = 0;
280 /* Main scanner destructor. */
282 void
283 gfc_scanner_done_1 (void)
285 gfc_linebuf *lb;
286 gfc_file *f;
288 while(line_head != NULL)
290 lb = line_head->next;
291 gfc_free(line_head);
292 line_head = lb;
295 while(file_head != NULL)
297 f = file_head->next;
298 gfc_free(file_head->filename);
299 gfc_free(file_head);
300 file_head = f;
305 /* Adds path to the list pointed to by list. */
307 static void
308 add_path_to_list (gfc_directorylist **list, const char *path,
309 bool use_for_modules)
311 gfc_directorylist *dir;
312 const char *p;
314 p = path;
315 while (*p == ' ' || *p == '\t') /* someone might do "-I include" */
316 if (*p++ == '\0')
317 return;
319 dir = *list;
320 if (!dir)
321 dir = *list = gfc_getmem (sizeof (gfc_directorylist));
322 else
324 while (dir->next)
325 dir = dir->next;
327 dir->next = gfc_getmem (sizeof (gfc_directorylist));
328 dir = dir->next;
331 dir->next = NULL;
332 dir->use_for_modules = use_for_modules;
333 dir->path = gfc_getmem (strlen (p) + 2);
334 strcpy (dir->path, p);
335 strcat (dir->path, "/"); /* make '/' last character */
339 void
340 gfc_add_include_path (const char *path, bool use_for_modules)
342 add_path_to_list (&include_dirs, path, use_for_modules);
346 void
347 gfc_add_intrinsic_modules_path (const char *path)
349 add_path_to_list (&intrinsic_modules_dirs, path, true);
353 /* Release resources allocated for options. */
355 void
356 gfc_release_include_path (void)
358 gfc_directorylist *p;
360 while (include_dirs != NULL)
362 p = include_dirs;
363 include_dirs = include_dirs->next;
364 gfc_free (p->path);
365 gfc_free (p);
368 while (intrinsic_modules_dirs != NULL)
370 p = intrinsic_modules_dirs;
371 intrinsic_modules_dirs = intrinsic_modules_dirs->next;
372 gfc_free (p->path);
373 gfc_free (p);
376 gfc_free (gfc_option.module_dir);
380 static FILE *
381 open_included_file (const char *name, gfc_directorylist *list, bool module)
383 char *fullname;
384 gfc_directorylist *p;
385 FILE *f;
387 for (p = list; p; p = p->next)
389 if (module && !p->use_for_modules)
390 continue;
392 fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1);
393 strcpy (fullname, p->path);
394 strcat (fullname, name);
396 f = gfc_open_file (fullname);
397 if (f != NULL)
398 return f;
401 return NULL;
405 /* Opens file for reading, searching through the include directories
406 given if necessary. If the include_cwd argument is true, we try
407 to open the file in the current directory first. */
409 FILE *
410 gfc_open_included_file (const char *name, bool include_cwd, bool module)
412 FILE *f;
414 if (IS_ABSOLUTE_PATH (name))
415 return gfc_open_file (name);
417 if (include_cwd)
419 f = gfc_open_file (name);
420 if (f != NULL)
421 return f;
424 return open_included_file (name, include_dirs, module);
427 FILE *
428 gfc_open_intrinsic_module (const char *name)
430 if (IS_ABSOLUTE_PATH (name))
431 return gfc_open_file (name);
433 return open_included_file (name, intrinsic_modules_dirs, true);
437 /* Test to see if we're at the end of the main source file. */
440 gfc_at_end (void)
442 return end_flag;
446 /* Test to see if we're at the end of the current file. */
449 gfc_at_eof (void)
451 if (gfc_at_end ())
452 return 1;
454 if (line_head == NULL)
455 return 1; /* Null file */
457 if (gfc_current_locus.lb == NULL)
458 return 1;
460 return 0;
464 /* Test to see if we're at the beginning of a new line. */
467 gfc_at_bol (void)
469 if (gfc_at_eof ())
470 return 1;
472 return (gfc_current_locus.nextc == gfc_current_locus.lb->line);
476 /* Test to see if we're at the end of a line. */
479 gfc_at_eol (void)
481 if (gfc_at_eof ())
482 return 1;
484 return (*gfc_current_locus.nextc == '\0');
487 static void
488 add_file_change (const char *filename, int line)
490 if (file_changes_count == file_changes_allocated)
492 if (file_changes_allocated)
493 file_changes_allocated *= 2;
494 else
495 file_changes_allocated = 16;
496 file_changes
497 = xrealloc (file_changes,
498 file_changes_allocated * sizeof (*file_changes));
500 file_changes[file_changes_count].filename = filename;
501 file_changes[file_changes_count].lb = NULL;
502 file_changes[file_changes_count++].line = line;
505 static void
506 report_file_change (gfc_linebuf *lb)
508 size_t c = file_changes_cur;
509 while (c < file_changes_count
510 && file_changes[c].lb == lb)
512 if (file_changes[c].filename)
513 (*debug_hooks->start_source_file) (file_changes[c].line,
514 file_changes[c].filename);
515 else
516 (*debug_hooks->end_source_file) (file_changes[c].line);
517 ++c;
519 file_changes_cur = c;
522 void
523 gfc_start_source_files (void)
525 /* If the debugger wants the name of the main source file,
526 we give it. */
527 if (debug_hooks->start_end_main_source_file)
528 (*debug_hooks->start_source_file) (0, gfc_source_file);
530 file_changes_cur = 0;
531 report_file_change (gfc_current_locus.lb);
534 void
535 gfc_end_source_files (void)
537 report_file_change (NULL);
539 if (debug_hooks->start_end_main_source_file)
540 (*debug_hooks->end_source_file) (0);
543 /* Advance the current line pointer to the next line. */
545 void
546 gfc_advance_line (void)
548 if (gfc_at_end ())
549 return;
551 if (gfc_current_locus.lb == NULL)
553 end_flag = 1;
554 return;
557 if (gfc_current_locus.lb->next
558 && !gfc_current_locus.lb->next->dbg_emitted)
560 report_file_change (gfc_current_locus.lb->next);
561 gfc_current_locus.lb->next->dbg_emitted = true;
564 gfc_current_locus.lb = gfc_current_locus.lb->next;
566 if (gfc_current_locus.lb != NULL)
567 gfc_current_locus.nextc = gfc_current_locus.lb->line;
568 else
570 gfc_current_locus.nextc = NULL;
571 end_flag = 1;
576 /* Get the next character from the input, advancing gfc_current_file's
577 locus. When we hit the end of the line or the end of the file, we
578 start returning a '\n' in order to complete the current statement.
579 No Fortran line conventions are implemented here.
581 Requiring explicit advances to the next line prevents the parse
582 pointer from being on the wrong line if the current statement ends
583 prematurely. */
585 static gfc_char_t
586 next_char (void)
588 gfc_char_t c;
590 if (gfc_current_locus.nextc == NULL)
591 return '\n';
593 c = *gfc_current_locus.nextc++;
594 if (c == '\0')
596 gfc_current_locus.nextc--; /* Remain on this line. */
597 c = '\n';
600 return c;
604 /* Skip a comment. When we come here the parse pointer is positioned
605 immediately after the comment character. If we ever implement
606 compiler directives withing comments, here is where we parse the
607 directive. */
609 static void
610 skip_comment_line (void)
612 gfc_char_t c;
616 c = next_char ();
618 while (c != '\n');
620 gfc_advance_line ();
625 gfc_define_undef_line (void)
627 char *tmp;
629 /* All lines beginning with '#' are either #define or #undef. */
630 if (debug_info_level != DINFO_LEVEL_VERBOSE || gfc_peek_ascii_char () != '#')
631 return 0;
633 if (wide_strncmp (gfc_current_locus.nextc, "#define ", 8) == 0)
635 tmp = gfc_widechar_to_char (&gfc_current_locus.nextc[8], -1);
636 (*debug_hooks->define) (gfc_linebuf_linenum (gfc_current_locus.lb),
637 tmp);
638 gfc_free (tmp);
641 if (wide_strncmp (gfc_current_locus.nextc, "#undef ", 7) == 0)
643 tmp = gfc_widechar_to_char (&gfc_current_locus.nextc[7], -1);
644 (*debug_hooks->undef) (gfc_linebuf_linenum (gfc_current_locus.lb),
645 tmp);
646 gfc_free (tmp);
649 /* Skip the rest of the line. */
650 skip_comment_line ();
652 return 1;
656 /* Comment lines are null lines, lines containing only blanks or lines
657 on which the first nonblank line is a '!'.
658 Return true if !$ openmp conditional compilation sentinel was
659 seen. */
661 static bool
662 skip_free_comments (void)
664 locus start;
665 gfc_char_t c;
666 int at_bol;
668 for (;;)
670 at_bol = gfc_at_bol ();
671 start = gfc_current_locus;
672 if (gfc_at_eof ())
673 break;
676 c = next_char ();
677 while (gfc_is_whitespace (c));
679 if (c == '\n')
681 gfc_advance_line ();
682 continue;
685 if (c == '!')
687 /* If -fopenmp, we need to handle here 2 things:
688 1) don't treat !$omp as comments, but directives
689 2) handle OpenMP conditional compilation, where
690 !$ should be treated as 2 spaces (for initial lines
691 only if followed by space). */
692 if (gfc_option.flag_openmp && at_bol)
694 locus old_loc = gfc_current_locus;
695 if (next_char () == '$')
697 c = next_char ();
698 if (c == 'o' || c == 'O')
700 if (((c = next_char ()) == 'm' || c == 'M')
701 && ((c = next_char ()) == 'p' || c == 'P'))
703 if ((c = next_char ()) == ' ' || continue_flag)
705 while (gfc_is_whitespace (c))
706 c = next_char ();
707 if (c != '\n' && c != '!')
709 openmp_flag = 1;
710 openmp_locus = old_loc;
711 gfc_current_locus = start;
712 return false;
715 else
716 gfc_warning_now ("!$OMP at %C starts a commented "
717 "line as it neither is followed "
718 "by a space nor is a "
719 "continuation line");
721 gfc_current_locus = old_loc;
722 next_char ();
723 c = next_char ();
725 if (continue_flag || c == ' ')
727 gfc_current_locus = old_loc;
728 next_char ();
729 openmp_flag = 0;
730 return true;
733 gfc_current_locus = old_loc;
735 skip_comment_line ();
736 continue;
739 break;
742 if (openmp_flag && at_bol)
743 openmp_flag = 0;
744 gfc_current_locus = start;
745 return false;
749 /* Skip comment lines in fixed source mode. We have the same rules as
750 in skip_free_comment(), except that we can have a 'c', 'C' or '*'
751 in column 1, and a '!' cannot be in column 6. Also, we deal with
752 lines with 'd' or 'D' in column 1, if the user requested this. */
754 static void
755 skip_fixed_comments (void)
757 locus start;
758 int col;
759 gfc_char_t c;
761 if (! gfc_at_bol ())
763 start = gfc_current_locus;
764 if (! gfc_at_eof ())
767 c = next_char ();
768 while (gfc_is_whitespace (c));
770 if (c == '\n')
771 gfc_advance_line ();
772 else if (c == '!')
773 skip_comment_line ();
776 if (! gfc_at_bol ())
778 gfc_current_locus = start;
779 return;
783 for (;;)
785 start = gfc_current_locus;
786 if (gfc_at_eof ())
787 break;
789 c = next_char ();
790 if (c == '\n')
792 gfc_advance_line ();
793 continue;
796 if (c == '!' || c == 'c' || c == 'C' || c == '*')
798 /* If -fopenmp, we need to handle here 2 things:
799 1) don't treat !$omp|c$omp|*$omp as comments, but directives
800 2) handle OpenMP conditional compilation, where
801 !$|c$|*$ should be treated as 2 spaces if the characters
802 in columns 3 to 6 are valid fixed form label columns
803 characters. */
804 if (gfc_current_locus.lb != NULL
805 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
806 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
808 if (gfc_option.flag_openmp)
810 if (next_char () == '$')
812 c = next_char ();
813 if (c == 'o' || c == 'O')
815 if (((c = next_char ()) == 'm' || c == 'M')
816 && ((c = next_char ()) == 'p' || c == 'P'))
818 c = next_char ();
819 if (c != '\n'
820 && ((openmp_flag && continue_flag)
821 || c == ' ' || c == '0'))
823 c = next_char ();
824 while (gfc_is_whitespace (c))
825 c = next_char ();
826 if (c != '\n' && c != '!')
828 /* Canonicalize to *$omp. */
829 *start.nextc = '*';
830 openmp_flag = 1;
831 gfc_current_locus = start;
832 return;
837 else
839 int digit_seen = 0;
841 for (col = 3; col < 6; col++, c = next_char ())
842 if (c == ' ')
843 continue;
844 else if (c < '0' || c > '9')
845 break;
846 else
847 digit_seen = 1;
849 if (col == 6 && c != '\n'
850 && ((continue_flag && !digit_seen)
851 || c == ' ' || c == '0'))
853 gfc_current_locus = start;
854 start.nextc[0] = ' ';
855 start.nextc[1] = ' ';
856 continue;
860 gfc_current_locus = start;
862 skip_comment_line ();
863 continue;
866 if (gfc_option.flag_d_lines != -1 && (c == 'd' || c == 'D'))
868 if (gfc_option.flag_d_lines == 0)
870 skip_comment_line ();
871 continue;
873 else
874 *start.nextc = c = ' ';
877 col = 1;
879 while (gfc_is_whitespace (c))
881 c = next_char ();
882 col++;
885 if (c == '\n')
887 gfc_advance_line ();
888 continue;
891 if (col != 6 && c == '!')
893 if (gfc_current_locus.lb != NULL
894 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
895 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
896 skip_comment_line ();
897 continue;
900 break;
903 openmp_flag = 0;
904 gfc_current_locus = start;
908 /* Skips the current line if it is a comment. */
910 void
911 gfc_skip_comments (void)
913 if (gfc_current_form == FORM_FREE)
914 skip_free_comments ();
915 else
916 skip_fixed_comments ();
920 /* Get the next character from the input, taking continuation lines
921 and end-of-line comments into account. This implies that comment
922 lines between continued lines must be eaten here. For higher-level
923 subroutines, this flattens continued lines into a single logical
924 line. The in_string flag denotes whether we're inside a character
925 context or not. */
927 gfc_char_t
928 gfc_next_char_literal (int in_string)
930 locus old_loc;
931 int i, prev_openmp_flag;
932 gfc_char_t c;
934 continue_flag = 0;
936 restart:
937 c = next_char ();
938 if (gfc_at_end ())
940 continue_count = 0;
941 return c;
944 if (gfc_current_form == FORM_FREE)
946 bool openmp_cond_flag;
948 if (!in_string && c == '!')
950 if (openmp_flag
951 && memcmp (&gfc_current_locus, &openmp_locus,
952 sizeof (gfc_current_locus)) == 0)
953 goto done;
955 /* This line can't be continued */
958 c = next_char ();
960 while (c != '\n');
962 /* Avoid truncation warnings for comment ending lines. */
963 gfc_current_locus.lb->truncated = 0;
965 goto done;
968 if (c != '&')
969 goto done;
971 /* If the next nonblank character is a ! or \n, we've got a
972 continuation line. */
973 old_loc = gfc_current_locus;
975 c = next_char ();
976 while (gfc_is_whitespace (c))
977 c = next_char ();
979 /* Character constants to be continued cannot have commentary
980 after the '&'. */
982 if (in_string && c != '\n')
984 gfc_current_locus = old_loc;
985 c = '&';
986 goto done;
989 if (c != '!' && c != '\n')
991 gfc_current_locus = old_loc;
992 c = '&';
993 goto done;
996 prev_openmp_flag = openmp_flag;
997 continue_flag = 1;
998 if (c == '!')
999 skip_comment_line ();
1000 else
1001 gfc_advance_line ();
1003 if (gfc_at_eof())
1004 goto not_continuation;
1006 /* We've got a continuation line. If we are on the very next line after
1007 the last continuation, increment the continuation line count and
1008 check whether the limit has been exceeded. */
1009 if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1)
1011 if (++continue_count == gfc_option.max_continue_free)
1013 if (gfc_notification_std (GFC_STD_GNU) || pedantic)
1014 gfc_warning ("Limit of %d continuations exceeded in "
1015 "statement at %C", gfc_option.max_continue_free);
1019 /* Now find where it continues. First eat any comment lines. */
1020 openmp_cond_flag = skip_free_comments ();
1022 if (gfc_current_locus.lb != NULL
1023 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
1024 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
1026 if (prev_openmp_flag != openmp_flag)
1028 gfc_current_locus = old_loc;
1029 openmp_flag = prev_openmp_flag;
1030 c = '&';
1031 goto done;
1034 /* Now that we have a non-comment line, probe ahead for the
1035 first non-whitespace character. If it is another '&', then
1036 reading starts at the next character, otherwise we must back
1037 up to where the whitespace started and resume from there. */
1039 old_loc = gfc_current_locus;
1041 c = next_char ();
1042 while (gfc_is_whitespace (c))
1043 c = next_char ();
1045 if (openmp_flag)
1047 for (i = 0; i < 5; i++, c = next_char ())
1049 gcc_assert (gfc_wide_tolower (c) == (unsigned char) "!$omp"[i]);
1050 if (i == 4)
1051 old_loc = gfc_current_locus;
1053 while (gfc_is_whitespace (c))
1054 c = next_char ();
1057 if (c != '&')
1059 if (in_string)
1061 if (gfc_option.warn_ampersand)
1062 gfc_warning_now ("Missing '&' in continued character "
1063 "constant at %C");
1064 gfc_current_locus.nextc--;
1066 /* Both !$omp and !$ -fopenmp continuation lines have & on the
1067 continuation line only optionally. */
1068 else if (openmp_flag || openmp_cond_flag)
1069 gfc_current_locus.nextc--;
1070 else
1072 c = ' ';
1073 gfc_current_locus = old_loc;
1074 goto done;
1078 else
1080 /* Fixed form continuation. */
1081 if (!in_string && c == '!')
1083 /* Skip comment at end of line. */
1086 c = next_char ();
1088 while (c != '\n');
1090 /* Avoid truncation warnings for comment ending lines. */
1091 gfc_current_locus.lb->truncated = 0;
1094 if (c != '\n')
1095 goto done;
1097 prev_openmp_flag = openmp_flag;
1098 continue_flag = 1;
1099 old_loc = gfc_current_locus;
1101 gfc_advance_line ();
1102 skip_fixed_comments ();
1104 /* See if this line is a continuation line. */
1105 if (openmp_flag != prev_openmp_flag)
1107 openmp_flag = prev_openmp_flag;
1108 goto not_continuation;
1111 if (!openmp_flag)
1112 for (i = 0; i < 5; i++)
1114 c = next_char ();
1115 if (c != ' ')
1116 goto not_continuation;
1118 else
1119 for (i = 0; i < 5; i++)
1121 c = next_char ();
1122 if (gfc_wide_tolower (c) != (unsigned char) "*$omp"[i])
1123 goto not_continuation;
1126 c = next_char ();
1127 if (c == '0' || c == ' ' || c == '\n')
1128 goto not_continuation;
1130 /* We've got a continuation line. If we are on the very next line after
1131 the last continuation, increment the continuation line count and
1132 check whether the limit has been exceeded. */
1133 if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1)
1135 if (++continue_count == gfc_option.max_continue_fixed)
1137 if (gfc_notification_std (GFC_STD_GNU) || pedantic)
1138 gfc_warning ("Limit of %d continuations exceeded in "
1139 "statement at %C",
1140 gfc_option.max_continue_fixed);
1144 if (gfc_current_locus.lb != NULL
1145 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
1146 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
1149 /* Ready to read first character of continuation line, which might
1150 be another continuation line! */
1151 goto restart;
1153 not_continuation:
1154 c = '\n';
1155 gfc_current_locus = old_loc;
1157 done:
1158 if (c == '\n')
1159 continue_count = 0;
1160 continue_flag = 0;
1161 return c;
1165 /* Get the next character of input, folded to lowercase. In fixed
1166 form mode, we also ignore spaces. When matcher subroutines are
1167 parsing character literals, they have to call
1168 gfc_next_char_literal(). */
1170 gfc_char_t
1171 gfc_next_char (void)
1173 gfc_char_t c;
1177 c = gfc_next_char_literal (0);
1179 while (gfc_current_form == FORM_FIXED && gfc_is_whitespace (c));
1181 return gfc_wide_tolower (c);
1184 char
1185 gfc_next_ascii_char (void)
1187 gfc_char_t c = gfc_next_char ();
1189 return (gfc_wide_fits_in_byte (c) ? (unsigned char) c
1190 : (unsigned char) UCHAR_MAX);
1194 gfc_char_t
1195 gfc_peek_char (void)
1197 locus old_loc;
1198 gfc_char_t c;
1200 old_loc = gfc_current_locus;
1201 c = gfc_next_char ();
1202 gfc_current_locus = old_loc;
1204 return c;
1208 char
1209 gfc_peek_ascii_char (void)
1211 gfc_char_t c = gfc_peek_char ();
1213 return (gfc_wide_fits_in_byte (c) ? (unsigned char) c
1214 : (unsigned char) UCHAR_MAX);
1218 /* Recover from an error. We try to get past the current statement
1219 and get lined up for the next. The next statement follows a '\n'
1220 or a ';'. We also assume that we are not within a character
1221 constant, and deal with finding a '\'' or '"'. */
1223 void
1224 gfc_error_recovery (void)
1226 gfc_char_t c, delim;
1228 if (gfc_at_eof ())
1229 return;
1231 for (;;)
1233 c = gfc_next_char ();
1234 if (c == '\n' || c == ';')
1235 break;
1237 if (c != '\'' && c != '"')
1239 if (gfc_at_eof ())
1240 break;
1241 continue;
1243 delim = c;
1245 for (;;)
1247 c = next_char ();
1249 if (c == delim)
1250 break;
1251 if (c == '\n')
1252 return;
1253 if (c == '\\')
1255 c = next_char ();
1256 if (c == '\n')
1257 return;
1260 if (gfc_at_eof ())
1261 break;
1266 /* Read ahead until the next character to be read is not whitespace. */
1268 void
1269 gfc_gobble_whitespace (void)
1271 static int linenum = 0;
1272 locus old_loc;
1273 gfc_char_t c;
1277 old_loc = gfc_current_locus;
1278 c = gfc_next_char_literal (0);
1279 /* Issue a warning for nonconforming tabs. We keep track of the line
1280 number because the Fortran matchers will often back up and the same
1281 line will be scanned multiple times. */
1282 if (!gfc_option.warn_tabs && c == '\t')
1284 int cur_linenum = LOCATION_LINE (gfc_current_locus.lb->location);
1285 if (cur_linenum != linenum)
1287 linenum = cur_linenum;
1288 gfc_warning_now ("Nonconforming tab character at %C");
1292 while (gfc_is_whitespace (c));
1294 gfc_current_locus = old_loc;
1298 /* Load a single line into pbuf.
1300 If pbuf points to a NULL pointer, it is allocated.
1301 We truncate lines that are too long, unless we're dealing with
1302 preprocessor lines or if the option -ffixed-line-length-none is set,
1303 in which case we reallocate the buffer to fit the entire line, if
1304 need be.
1305 In fixed mode, we expand a tab that occurs within the statement
1306 label region to expand to spaces that leave the next character in
1307 the source region.
1308 load_line returns whether the line was truncated.
1310 NOTE: The error machinery isn't available at this point, so we can't
1311 easily report line and column numbers consistent with other
1312 parts of gfortran. */
1314 static int
1315 load_line (FILE *input, gfc_char_t **pbuf, int *pbuflen)
1317 static int linenum = 0, current_line = 1;
1318 int c, maxlen, i, preprocessor_flag, buflen = *pbuflen;
1319 int trunc_flag = 0, seen_comment = 0;
1320 int seen_printable = 0, seen_ampersand = 0;
1321 gfc_char_t *buffer;
1322 bool found_tab = false;
1324 /* Determine the maximum allowed line length. */
1325 if (gfc_current_form == FORM_FREE)
1326 maxlen = gfc_option.free_line_length;
1327 else if (gfc_current_form == FORM_FIXED)
1328 maxlen = gfc_option.fixed_line_length;
1329 else
1330 maxlen = 72;
1332 if (*pbuf == NULL)
1334 /* Allocate the line buffer, storing its length into buflen.
1335 Note that if maxlen==0, indicating that arbitrary-length lines
1336 are allowed, the buffer will be reallocated if this length is
1337 insufficient; since 132 characters is the length of a standard
1338 free-form line, we use that as a starting guess. */
1339 if (maxlen > 0)
1340 buflen = maxlen;
1341 else
1342 buflen = 132;
1344 *pbuf = gfc_get_wide_string (buflen + 1);
1347 i = 0;
1348 buffer = *pbuf;
1350 preprocessor_flag = 0;
1351 c = getc (input);
1352 if (c == '#')
1353 /* In order to not truncate preprocessor lines, we have to
1354 remember that this is one. */
1355 preprocessor_flag = 1;
1356 ungetc (c, input);
1358 for (;;)
1360 c = getc (input);
1362 if (c == EOF)
1363 break;
1364 if (c == '\n')
1366 /* Check for illegal use of ampersand. See F95 Standard 3.3.1.3. */
1367 if (gfc_current_form == FORM_FREE
1368 && !seen_printable && seen_ampersand)
1370 if (pedantic)
1371 gfc_error_now ("'&' not allowed by itself in line %d",
1372 current_line);
1373 else
1374 gfc_warning_now ("'&' not allowed by itself in line %d",
1375 current_line);
1377 break;
1380 if (c == '\r')
1381 continue; /* Gobble characters. */
1382 if (c == '\0')
1383 continue;
1385 if (c == '&')
1387 if (seen_ampersand)
1388 seen_ampersand = 0;
1389 else
1390 seen_ampersand = 1;
1393 if ((c != '&' && c != '!' && c != ' ') || (c == '!' && !seen_ampersand))
1394 seen_printable = 1;
1396 /* Is this a fixed-form comment? */
1397 if (gfc_current_form == FORM_FIXED && i == 0
1398 && (c == '*' || c == 'c' || c == 'd'))
1399 seen_comment = 1;
1401 /* Vendor extension: "<tab>1" marks a continuation line. */
1402 if (found_tab)
1404 found_tab = false;
1405 if (c >= '1' && c <= '9')
1407 *(buffer-1) = c;
1408 continue;
1412 if (gfc_current_form == FORM_FIXED && c == '\t' && i < 6)
1414 found_tab = true;
1416 if (!gfc_option.warn_tabs && seen_comment == 0
1417 && current_line != linenum)
1419 linenum = current_line;
1420 gfc_warning_now ("Nonconforming tab character in column %d "
1421 "of line %d", i+1, linenum);
1424 while (i < 6)
1426 *buffer++ = ' ';
1427 i++;
1430 continue;
1433 *buffer++ = c;
1434 i++;
1436 if (maxlen == 0 || preprocessor_flag)
1438 if (i >= buflen)
1440 /* Reallocate line buffer to double size to hold the
1441 overlong line. */
1442 buflen = buflen * 2;
1443 *pbuf = xrealloc (*pbuf, (buflen + 1) * sizeof (gfc_char_t));
1444 buffer = (*pbuf) + i;
1447 else if (i >= maxlen)
1449 /* Truncate the rest of the line. */
1450 for (;;)
1452 c = getc (input);
1453 if (c == '\n' || c == EOF)
1454 break;
1456 trunc_flag = 1;
1459 ungetc ('\n', input);
1463 /* Pad lines to the selected line length in fixed form. */
1464 if (gfc_current_form == FORM_FIXED
1465 && gfc_option.fixed_line_length != 0
1466 && !preprocessor_flag
1467 && c != EOF)
1469 while (i++ < maxlen)
1470 *buffer++ = ' ';
1473 *buffer = '\0';
1474 *pbuflen = buflen;
1475 current_line++;
1477 return trunc_flag;
1481 /* Get a gfc_file structure, initialize it and add it to
1482 the file stack. */
1484 static gfc_file *
1485 get_file (const char *name, enum lc_reason reason ATTRIBUTE_UNUSED)
1487 gfc_file *f;
1489 f = gfc_getmem (sizeof (gfc_file));
1491 f->filename = gfc_getmem (strlen (name) + 1);
1492 strcpy (f->filename, name);
1494 f->next = file_head;
1495 file_head = f;
1497 f->up = current_file;
1498 if (current_file != NULL)
1499 f->inclusion_line = current_file->line;
1501 linemap_add (line_table, reason, false, f->filename, 1);
1503 return f;
1507 /* Deal with a line from the C preprocessor. The
1508 initial octothorp has already been seen. */
1510 static void
1511 preprocessor_line (gfc_char_t *c)
1513 bool flag[5];
1514 int i, line;
1515 gfc_char_t *wide_filename;
1516 gfc_file *f;
1517 int escaped, unescape;
1518 char *filename;
1520 c++;
1521 while (*c == ' ' || *c == '\t')
1522 c++;
1524 if (*c < '0' || *c > '9')
1525 goto bad_cpp_line;
1527 line = wide_atoi (c);
1529 c = wide_strchr (c, ' ');
1530 if (c == NULL)
1532 /* No file name given. Set new line number. */
1533 current_file->line = line;
1534 return;
1537 /* Skip spaces. */
1538 while (*c == ' ' || *c == '\t')
1539 c++;
1541 /* Skip quote. */
1542 if (*c != '"')
1543 goto bad_cpp_line;
1544 ++c;
1546 wide_filename = c;
1548 /* Make filename end at quote. */
1549 unescape = 0;
1550 escaped = false;
1551 while (*c && ! (!escaped && *c == '"'))
1553 if (escaped)
1554 escaped = false;
1555 else if (*c == '\\')
1557 escaped = true;
1558 unescape++;
1560 ++c;
1563 if (! *c)
1564 /* Preprocessor line has no closing quote. */
1565 goto bad_cpp_line;
1567 *c++ = '\0';
1569 /* Undo effects of cpp_quote_string. */
1570 if (unescape)
1572 gfc_char_t *s = wide_filename;
1573 gfc_char_t *d = gfc_get_wide_string (c - wide_filename - unescape);
1575 wide_filename = d;
1576 while (*s)
1578 if (*s == '\\')
1579 *d++ = *++s;
1580 else
1581 *d++ = *s;
1582 s++;
1584 *d = '\0';
1587 /* Get flags. */
1589 flag[1] = flag[2] = flag[3] = flag[4] = false;
1591 for (;;)
1593 c = wide_strchr (c, ' ');
1594 if (c == NULL)
1595 break;
1597 c++;
1598 i = wide_atoi (c);
1600 if (1 <= i && i <= 4)
1601 flag[i] = true;
1604 /* Convert the filename in wide characters into a filename in narrow
1605 characters. */
1606 filename = gfc_widechar_to_char (wide_filename, -1);
1608 /* Interpret flags. */
1610 if (flag[1]) /* Starting new file. */
1612 f = get_file (filename, LC_RENAME);
1613 add_file_change (f->filename, f->inclusion_line);
1614 current_file = f;
1617 if (flag[2]) /* Ending current file. */
1619 if (!current_file->up
1620 || strcmp (current_file->up->filename, filename) != 0)
1622 gfc_warning_now ("%s:%d: file %s left but not entered",
1623 current_file->filename, current_file->line,
1624 filename);
1625 if (unescape)
1626 gfc_free (wide_filename);
1627 gfc_free (filename);
1628 return;
1631 add_file_change (NULL, line);
1632 current_file = current_file->up;
1633 linemap_add (line_table, LC_RENAME, false, current_file->filename,
1634 current_file->line);
1637 /* The name of the file can be a temporary file produced by
1638 cpp. Replace the name if it is different. */
1640 if (strcmp (current_file->filename, filename) != 0)
1642 gfc_free (current_file->filename);
1643 current_file->filename = gfc_getmem (strlen (filename) + 1);
1644 strcpy (current_file->filename, filename);
1647 /* Set new line number. */
1648 current_file->line = line;
1649 if (unescape)
1650 gfc_free (wide_filename);
1651 gfc_free (filename);
1652 return;
1654 bad_cpp_line:
1655 gfc_warning_now ("%s:%d: Illegal preprocessor directive",
1656 current_file->filename, current_file->line);
1657 current_file->line++;
1661 static try load_file (const char *, bool);
1663 /* include_line()-- Checks a line buffer to see if it is an include
1664 line. If so, we call load_file() recursively to load the included
1665 file. We never return a syntax error because a statement like
1666 "include = 5" is perfectly legal. We return false if no include was
1667 processed or true if we matched an include. */
1669 static bool
1670 include_line (gfc_char_t *line)
1672 gfc_char_t quote, *c, *begin, *stop;
1673 char *filename;
1675 c = line;
1677 if (gfc_option.flag_openmp)
1679 if (gfc_current_form == FORM_FREE)
1681 while (*c == ' ' || *c == '\t')
1682 c++;
1683 if (*c == '!' && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
1684 c += 3;
1686 else
1688 if ((*c == '!' || *c == 'c' || *c == 'C' || *c == '*')
1689 && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
1690 c += 3;
1694 while (*c == ' ' || *c == '\t')
1695 c++;
1697 if (gfc_wide_strncasecmp (c, "include", 7))
1698 return false;
1700 c += 7;
1701 while (*c == ' ' || *c == '\t')
1702 c++;
1704 /* Find filename between quotes. */
1706 quote = *c++;
1707 if (quote != '"' && quote != '\'')
1708 return false;
1710 begin = c;
1712 while (*c != quote && *c != '\0')
1713 c++;
1715 if (*c == '\0')
1716 return false;
1718 stop = c++;
1720 while (*c == ' ' || *c == '\t')
1721 c++;
1723 if (*c != '\0' && *c != '!')
1724 return false;
1726 /* We have an include line at this point. */
1728 *stop = '\0'; /* It's ok to trash the buffer, as this line won't be
1729 read by anything else. */
1731 filename = gfc_widechar_to_char (begin, -1);
1732 load_file (filename, false);
1733 gfc_free (filename);
1734 return true;
1738 /* Load a file into memory by calling load_line until the file ends. */
1740 static try
1741 load_file (const char *filename, bool initial)
1743 gfc_char_t *line;
1744 gfc_linebuf *b;
1745 gfc_file *f;
1746 FILE *input;
1747 int len, line_len;
1748 bool first_line;
1750 for (f = current_file; f; f = f->up)
1751 if (strcmp (filename, f->filename) == 0)
1753 gfc_error_now ("File '%s' is being included recursively", filename);
1754 return FAILURE;
1757 if (initial)
1759 if (gfc_src_file)
1761 input = gfc_src_file;
1762 gfc_src_file = NULL;
1764 else
1765 input = gfc_open_file (filename);
1766 if (input == NULL)
1768 gfc_error_now ("Can't open file '%s'", filename);
1769 return FAILURE;
1772 else
1774 input = gfc_open_included_file (filename, false, false);
1775 if (input == NULL)
1777 gfc_error_now ("Can't open included file '%s'", filename);
1778 return FAILURE;
1782 /* Load the file. */
1784 f = get_file (filename, initial ? LC_RENAME : LC_ENTER);
1785 if (!initial)
1786 add_file_change (f->filename, f->inclusion_line);
1787 current_file = f;
1788 current_file->line = 1;
1789 line = NULL;
1790 line_len = 0;
1791 first_line = true;
1793 if (initial && gfc_src_preprocessor_lines[0])
1795 preprocessor_line (gfc_src_preprocessor_lines[0]);
1796 gfc_free (gfc_src_preprocessor_lines[0]);
1797 gfc_src_preprocessor_lines[0] = NULL;
1798 if (gfc_src_preprocessor_lines[1])
1800 preprocessor_line (gfc_src_preprocessor_lines[1]);
1801 gfc_free (gfc_src_preprocessor_lines[1]);
1802 gfc_src_preprocessor_lines[1] = NULL;
1806 for (;;)
1808 int trunc = load_line (input, &line, &line_len);
1810 len = gfc_wide_strlen (line);
1811 if (feof (input) && len == 0)
1812 break;
1814 /* If this is the first line of the file, it can contain a byte
1815 order mark (BOM), which we will ignore:
1816 FF FE is UTF-16 little endian,
1817 FE FF is UTF-16 big endian,
1818 EF BB BF is UTF-8. */
1819 if (first_line
1820 && ((line_len >= 2 && line[0] == (unsigned char) '\xFF'
1821 && line[1] == (unsigned char) '\xFE')
1822 || (line_len >= 2 && line[0] == (unsigned char) '\xFE'
1823 && line[1] == (unsigned char) '\xFF')
1824 || (line_len >= 3 && line[0] == (unsigned char) '\xEF'
1825 && line[1] == (unsigned char) '\xBB'
1826 && line[2] == (unsigned char) '\xBF')))
1828 int n = line[1] == (unsigned char) '\xBB' ? 3 : 2;
1829 gfc_char_t *new = gfc_get_wide_string (line_len);
1831 wide_strcpy (new, &line[n]);
1832 gfc_free (line);
1833 line = new;
1834 len -= n;
1837 /* There are three things this line can be: a line of Fortran
1838 source, an include line or a C preprocessor directive. */
1840 if (line[0] == '#')
1842 /* When -g3 is specified, it's possible that we emit #define
1843 and #undef lines, which we need to pass to the middle-end
1844 so that it can emit correct debug info. */
1845 if (debug_info_level == DINFO_LEVEL_VERBOSE
1846 && (wide_strncmp (line, "#define ", 8) == 0
1847 || wide_strncmp (line, "#undef ", 7) == 0))
1849 else
1851 preprocessor_line (line);
1852 continue;
1856 /* Preprocessed files have preprocessor lines added before the byte
1857 order mark, so first_line is not about the first line of the file
1858 but the first line that's not a preprocessor line. */
1859 first_line = false;
1861 if (include_line (line))
1863 current_file->line++;
1864 continue;
1867 /* Add line. */
1869 b = gfc_getmem (gfc_linebuf_header_size
1870 + (len + 1) * sizeof (gfc_char_t));
1872 b->location
1873 = linemap_line_start (line_table, current_file->line++, 120);
1874 b->file = current_file;
1875 b->truncated = trunc;
1876 wide_strcpy (b->line, line);
1878 if (line_head == NULL)
1879 line_head = b;
1880 else
1881 line_tail->next = b;
1883 line_tail = b;
1885 while (file_changes_cur < file_changes_count)
1886 file_changes[file_changes_cur++].lb = b;
1889 /* Release the line buffer allocated in load_line. */
1890 gfc_free (line);
1892 fclose (input);
1894 if (!initial)
1895 add_file_change (NULL, current_file->inclusion_line + 1);
1896 current_file = current_file->up;
1897 linemap_add (line_table, LC_LEAVE, 0, NULL, 0);
1898 return SUCCESS;
1902 /* Open a new file and start scanning from that file. Returns SUCCESS
1903 if everything went OK, FAILURE otherwise. If form == FORM_UKNOWN
1904 it tries to determine the source form from the filename, defaulting
1905 to free form. */
1908 gfc_new_file (void)
1910 try result;
1912 result = load_file (gfc_source_file, true);
1914 gfc_current_locus.lb = line_head;
1915 gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line;
1917 #if 0 /* Debugging aid. */
1918 for (; line_head; line_head = line_head->next)
1919 printf ("%s:%3d %s\n", LOCATION_FILE (line_head->location),
1920 LOCATION_LINE (line_head->location), line_head->line);
1922 exit (0);
1923 #endif
1925 return result;
1928 static char *
1929 unescape_filename (const char *ptr)
1931 const char *p = ptr, *s;
1932 char *d, *ret;
1933 int escaped, unescape = 0;
1935 /* Make filename end at quote. */
1936 escaped = false;
1937 while (*p && ! (! escaped && *p == '"'))
1939 if (escaped)
1940 escaped = false;
1941 else if (*p == '\\')
1943 escaped = true;
1944 unescape++;
1946 ++p;
1949 if (!*p || p[1])
1950 return NULL;
1952 /* Undo effects of cpp_quote_string. */
1953 s = ptr;
1954 d = gfc_getmem (p + 1 - ptr - unescape);
1955 ret = d;
1957 while (s != p)
1959 if (*s == '\\')
1960 *d++ = *++s;
1961 else
1962 *d++ = *s;
1963 s++;
1965 *d = '\0';
1966 return ret;
1969 /* For preprocessed files, if the first tokens are of the form # NUM.
1970 handle the directives so we know the original file name. */
1972 const char *
1973 gfc_read_orig_filename (const char *filename, const char **canon_source_file)
1975 int c, len;
1976 char *dirname, *tmp;
1978 gfc_src_file = gfc_open_file (filename);
1979 if (gfc_src_file == NULL)
1980 return NULL;
1982 c = getc (gfc_src_file);
1983 ungetc (c, gfc_src_file);
1985 if (c != '#')
1986 return NULL;
1988 len = 0;
1989 load_line (gfc_src_file, &gfc_src_preprocessor_lines[0], &len);
1991 if (wide_strncmp (gfc_src_preprocessor_lines[0], "# 1 \"", 5) != 0)
1992 return NULL;
1994 tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[0][5], -1);
1995 filename = unescape_filename (tmp);
1996 gfc_free (tmp);
1997 if (filename == NULL)
1998 return NULL;
2000 c = getc (gfc_src_file);
2001 ungetc (c, gfc_src_file);
2003 if (c != '#')
2004 return filename;
2006 len = 0;
2007 load_line (gfc_src_file, &gfc_src_preprocessor_lines[1], &len);
2009 if (wide_strncmp (gfc_src_preprocessor_lines[1], "# 1 \"", 5) != 0)
2010 return filename;
2012 tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[1][5], -1);
2013 dirname = unescape_filename (tmp);
2014 gfc_free (tmp);
2015 if (dirname == NULL)
2016 return filename;
2018 len = strlen (dirname);
2019 if (len < 3 || dirname[len - 1] != '/' || dirname[len - 2] != '/')
2021 gfc_free (dirname);
2022 return filename;
2024 dirname[len - 2] = '\0';
2025 set_src_pwd (dirname);
2027 if (! IS_ABSOLUTE_PATH (filename))
2029 char *p = gfc_getmem (len + strlen (filename));
2031 memcpy (p, dirname, len - 2);
2032 p[len - 2] = '/';
2033 strcpy (p + len - 1, filename);
2034 *canon_source_file = p;
2037 gfc_free (dirname);
2038 return filename;