Fix ChangeLog
[official-gcc.git] / gcc / fortran / scanner.c
blob1b0eeca1e6593a59a654731161349ccdc558f6b8
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"
50 #include "cpp.h"
52 /* Structure for holding module and include file search path. */
53 typedef struct gfc_directorylist
55 char *path;
56 bool use_for_modules;
57 struct gfc_directorylist *next;
59 gfc_directorylist;
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];
78 extern int pedantic;
80 static struct gfc_file_change
82 const char *filename;
83 gfc_linebuf *lb;
84 int line;
85 } *file_changes;
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. */
93 int
94 gfc_wide_fits_in_byte (gfc_char_t c)
96 return (c <= UCHAR_MAX);
99 static inline int
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));
111 gfc_char_t
112 gfc_wide_tolower (gfc_char_t c)
114 return (wide_is_ascii (c) ? (gfc_char_t) TOLOWER((unsigned char) c) : c);
117 gfc_char_t
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');
129 static inline int
130 wide_atoi (gfc_char_t *c)
132 #define MAX_DIGITS 20
133 char buf[MAX_DIGITS+1];
134 int i = 0;
136 while (gfc_wide_is_digit(*c) && i < MAX_DIGITS)
137 buf[i++] = *c++;
138 buf[i] = '\0';
139 return atoi (buf);
142 size_t
143 gfc_wide_strlen (const gfc_char_t *str)
145 size_t i;
147 for (i = 0; str[i]; i++)
150 return i;
153 gfc_char_t *
154 gfc_wide_memset (gfc_char_t *b, gfc_char_t c, size_t len)
156 size_t i;
158 for (i = 0; i < len; i++)
159 b[i] = c;
161 return b;
164 static gfc_char_t *
165 wide_strcpy (gfc_char_t *dest, const gfc_char_t *src)
167 gfc_char_t *d;
169 for (d = dest; (*d = *src) != '\0'; ++src, ++d)
172 return dest;
175 static gfc_char_t *
176 wide_strchr (const gfc_char_t *s, gfc_char_t c)
178 do {
179 if (*s == c)
181 return CONST_CAST(gfc_char_t *, s);
183 } while (*s++);
184 return 0;
187 char *
188 gfc_widechar_to_char (const gfc_char_t *s, int length)
190 size_t len, i;
191 char *res;
193 if (s == NULL)
194 return NULL;
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];
207 res[len] = '\0';
208 return res;
211 gfc_char_t *
212 gfc_char_to_widechar (const char *s)
214 size_t len, i;
215 gfc_char_t *res;
217 if (s == NULL)
218 return NULL;
220 len = strlen (s);
221 res = gfc_get_wide_string (len + 1);
223 for (i = 0; i < len; i++)
224 res[i] = (unsigned char) s[i];
226 res[len] = '\0';
227 return res;
230 static int
231 wide_strncmp (const gfc_char_t *s1, const char *s2, size_t n)
233 gfc_char_t c1, c2;
235 while (n-- > 0)
237 c1 = *s1++;
238 c2 = *s2++;
239 if (c1 != c2)
240 return (c1 > c2 ? 1 : -1);
241 if (c1 == '\0')
242 return 0;
244 return 0;
248 gfc_wide_strncasecmp (const gfc_char_t *s1, const char *s2, size_t n)
250 gfc_char_t c1, c2;
252 while (n-- > 0)
254 c1 = gfc_wide_tolower (*s1++);
255 c2 = TOLOWER (*s2++);
256 if (c1 != c2)
257 return (c1 > c2 ? 1 : -1);
258 if (c1 == '\0')
259 return 0;
261 return 0;
265 /* Main scanner initialization. */
267 void
268 gfc_scanner_init_1 (void)
270 file_head = NULL;
271 line_head = NULL;
272 line_tail = NULL;
274 continue_count = 0;
275 continue_line = 0;
277 end_flag = 0;
281 /* Main scanner destructor. */
283 void
284 gfc_scanner_done_1 (void)
286 gfc_linebuf *lb;
287 gfc_file *f;
289 while(line_head != NULL)
291 lb = line_head->next;
292 gfc_free(line_head);
293 line_head = lb;
296 while(file_head != NULL)
298 f = file_head->next;
299 gfc_free(file_head->filename);
300 gfc_free(file_head);
301 file_head = f;
306 /* Adds path to the list pointed to by list. */
308 static void
309 add_path_to_list (gfc_directorylist **list, const char *path,
310 bool use_for_modules)
312 gfc_directorylist *dir;
313 const char *p;
315 p = path;
316 while (*p == ' ' || *p == '\t') /* someone might do "-I include" */
317 if (*p++ == '\0')
318 return;
320 dir = *list;
321 if (!dir)
322 dir = *list = gfc_getmem (sizeof (gfc_directorylist));
323 else
325 while (dir->next)
326 dir = dir->next;
328 dir->next = gfc_getmem (sizeof (gfc_directorylist));
329 dir = dir->next;
332 dir->next = NULL;
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 */
340 void
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);
348 void
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. */
357 void
358 gfc_release_include_path (void)
360 gfc_directorylist *p;
362 while (include_dirs != NULL)
364 p = include_dirs;
365 include_dirs = include_dirs->next;
366 gfc_free (p->path);
367 gfc_free (p);
370 while (intrinsic_modules_dirs != NULL)
372 p = intrinsic_modules_dirs;
373 intrinsic_modules_dirs = intrinsic_modules_dirs->next;
374 gfc_free (p->path);
375 gfc_free (p);
378 gfc_free (gfc_option.module_dir);
382 static FILE *
383 open_included_file (const char *name, gfc_directorylist *list, bool module)
385 char *fullname;
386 gfc_directorylist *p;
387 FILE *f;
389 for (p = list; p; p = p->next)
391 if (module && !p->use_for_modules)
392 continue;
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);
399 if (f != NULL)
400 return f;
403 return NULL;
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. */
411 FILE *
412 gfc_open_included_file (const char *name, bool include_cwd, bool module)
414 FILE *f;
416 if (IS_ABSOLUTE_PATH (name))
417 return gfc_open_file (name);
419 if (include_cwd)
421 f = gfc_open_file (name);
422 if (f != NULL)
423 return f;
426 return open_included_file (name, include_dirs, module);
429 FILE *
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. */
442 gfc_at_end (void)
444 return end_flag;
448 /* Test to see if we're at the end of the current file. */
451 gfc_at_eof (void)
453 if (gfc_at_end ())
454 return 1;
456 if (line_head == NULL)
457 return 1; /* Null file */
459 if (gfc_current_locus.lb == NULL)
460 return 1;
462 return 0;
466 /* Test to see if we're at the beginning of a new line. */
469 gfc_at_bol (void)
471 if (gfc_at_eof ())
472 return 1;
474 return (gfc_current_locus.nextc == gfc_current_locus.lb->line);
478 /* Test to see if we're at the end of a line. */
481 gfc_at_eol (void)
483 if (gfc_at_eof ())
484 return 1;
486 return (*gfc_current_locus.nextc == '\0');
489 static void
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;
496 else
497 file_changes_allocated = 16;
498 file_changes
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;
507 static void
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);
517 else
518 (*debug_hooks->end_source_file) (file_changes[c].line);
519 ++c;
521 file_changes_cur = c;
524 void
525 gfc_start_source_files (void)
527 /* If the debugger wants the name of the main source file,
528 we give it. */
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);
536 void
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. */
547 void
548 gfc_advance_line (void)
550 if (gfc_at_end ())
551 return;
553 if (gfc_current_locus.lb == NULL)
555 end_flag = 1;
556 return;
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;
570 else
572 gfc_current_locus.nextc = NULL;
573 end_flag = 1;
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
585 prematurely. */
587 static gfc_char_t
588 next_char (void)
590 gfc_char_t c;
592 if (gfc_current_locus.nextc == NULL)
593 return '\n';
595 c = *gfc_current_locus.nextc++;
596 if (c == '\0')
598 gfc_current_locus.nextc--; /* Remain on this line. */
599 c = '\n';
602 return c;
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
609 directive. */
611 static void
612 skip_comment_line (void)
614 gfc_char_t c;
618 c = next_char ();
620 while (c != '\n');
622 gfc_advance_line ();
627 gfc_define_undef_line (void)
629 char *tmp;
631 /* All lines beginning with '#' are either #define or #undef. */
632 if (debug_info_level != DINFO_LEVEL_VERBOSE || gfc_peek_ascii_char () != '#')
633 return 0;
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),
639 tmp);
640 gfc_free (tmp);
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),
647 tmp);
648 gfc_free (tmp);
651 /* Skip the rest of the line. */
652 skip_comment_line ();
654 return 1;
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
661 seen. */
663 static bool
664 skip_free_comments (void)
666 locus start;
667 gfc_char_t c;
668 int at_bol;
670 for (;;)
672 at_bol = gfc_at_bol ();
673 start = gfc_current_locus;
674 if (gfc_at_eof ())
675 break;
678 c = next_char ();
679 while (gfc_is_whitespace (c));
681 if (c == '\n')
683 gfc_advance_line ();
684 continue;
687 if (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 () == '$')
699 c = 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 ()) == ' ' || c == '\t'
706 || continue_flag)
708 while (gfc_is_whitespace (c))
709 c = next_char ();
710 if (c != '\n' && c != '!')
712 openmp_flag = 1;
713 openmp_locus = old_loc;
714 gfc_current_locus = start;
715 return false;
718 else
719 gfc_warning_now ("!$OMP at %C starts a commented "
720 "line as it neither is followed "
721 "by a space nor is a "
722 "continuation line");
724 gfc_current_locus = old_loc;
725 next_char ();
726 c = next_char ();
728 if (continue_flag || c == ' ' || c == '\t')
730 gfc_current_locus = old_loc;
731 next_char ();
732 openmp_flag = 0;
733 return true;
736 gfc_current_locus = old_loc;
738 skip_comment_line ();
739 continue;
742 break;
745 if (openmp_flag && at_bol)
746 openmp_flag = 0;
747 gfc_current_locus = start;
748 return false;
752 /* Skip comment lines in fixed source mode. We have the same rules as
753 in skip_free_comment(), except that we can have a 'c', 'C' or '*'
754 in column 1, and a '!' cannot be in column 6. Also, we deal with
755 lines with 'd' or 'D' in column 1, if the user requested this. */
757 static void
758 skip_fixed_comments (void)
760 locus start;
761 int col;
762 gfc_char_t c;
764 if (! gfc_at_bol ())
766 start = gfc_current_locus;
767 if (! gfc_at_eof ())
770 c = next_char ();
771 while (gfc_is_whitespace (c));
773 if (c == '\n')
774 gfc_advance_line ();
775 else if (c == '!')
776 skip_comment_line ();
779 if (! gfc_at_bol ())
781 gfc_current_locus = start;
782 return;
786 for (;;)
788 start = gfc_current_locus;
789 if (gfc_at_eof ())
790 break;
792 c = next_char ();
793 if (c == '\n')
795 gfc_advance_line ();
796 continue;
799 if (c == '!' || c == 'c' || c == 'C' || c == '*')
801 /* If -fopenmp, we need to handle here 2 things:
802 1) don't treat !$omp|c$omp|*$omp as comments, but directives
803 2) handle OpenMP conditional compilation, where
804 !$|c$|*$ should be treated as 2 spaces if the characters
805 in columns 3 to 6 are valid fixed form label columns
806 characters. */
807 if (gfc_current_locus.lb != NULL
808 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
809 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
811 if (gfc_option.flag_openmp)
813 if (next_char () == '$')
815 c = next_char ();
816 if (c == 'o' || c == 'O')
818 if (((c = next_char ()) == 'm' || c == 'M')
819 && ((c = next_char ()) == 'p' || c == 'P'))
821 c = next_char ();
822 if (c != '\n'
823 && ((openmp_flag && continue_flag)
824 || c == ' ' || c == '\t' || c == '0'))
827 c = next_char ();
828 while (gfc_is_whitespace (c));
829 if (c != '\n' && c != '!')
831 /* Canonicalize to *$omp. */
832 *start.nextc = '*';
833 openmp_flag = 1;
834 gfc_current_locus = start;
835 return;
840 else
842 int digit_seen = 0;
844 for (col = 3; col < 6; col++, c = next_char ())
845 if (c == ' ')
846 continue;
847 else if (c == '\t')
849 col = 6;
850 break;
852 else if (c < '0' || c > '9')
853 break;
854 else
855 digit_seen = 1;
857 if (col == 6 && c != '\n'
858 && ((continue_flag && !digit_seen)
859 || c == ' ' || c == '\t' || c == '0'))
861 gfc_current_locus = start;
862 start.nextc[0] = ' ';
863 start.nextc[1] = ' ';
864 continue;
868 gfc_current_locus = start;
870 skip_comment_line ();
871 continue;
874 if (gfc_option.flag_d_lines != -1 && (c == 'd' || c == 'D'))
876 if (gfc_option.flag_d_lines == 0)
878 skip_comment_line ();
879 continue;
881 else
882 *start.nextc = c = ' ';
885 col = 1;
887 while (gfc_is_whitespace (c))
889 c = next_char ();
890 col++;
893 if (c == '\n')
895 gfc_advance_line ();
896 continue;
899 if (col != 6 && c == '!')
901 if (gfc_current_locus.lb != NULL
902 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
903 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
904 skip_comment_line ();
905 continue;
908 break;
911 openmp_flag = 0;
912 gfc_current_locus = start;
916 /* Skips the current line if it is a comment. */
918 void
919 gfc_skip_comments (void)
921 if (gfc_current_form == FORM_FREE)
922 skip_free_comments ();
923 else
924 skip_fixed_comments ();
928 /* Get the next character from the input, taking continuation lines
929 and end-of-line comments into account. This implies that comment
930 lines between continued lines must be eaten here. For higher-level
931 subroutines, this flattens continued lines into a single logical
932 line. The in_string flag denotes whether we're inside a character
933 context or not. */
935 gfc_char_t
936 gfc_next_char_literal (int in_string)
938 locus old_loc;
939 int i, prev_openmp_flag;
940 gfc_char_t c;
942 continue_flag = 0;
944 restart:
945 c = next_char ();
946 if (gfc_at_end ())
948 continue_count = 0;
949 return c;
952 if (gfc_current_form == FORM_FREE)
954 bool openmp_cond_flag;
956 if (!in_string && c == '!')
958 if (openmp_flag
959 && memcmp (&gfc_current_locus, &openmp_locus,
960 sizeof (gfc_current_locus)) == 0)
961 goto done;
963 /* This line can't be continued */
966 c = next_char ();
968 while (c != '\n');
970 /* Avoid truncation warnings for comment ending lines. */
971 gfc_current_locus.lb->truncated = 0;
973 goto done;
976 if (c != '&')
977 goto done;
979 /* If the next nonblank character is a ! or \n, we've got a
980 continuation line. */
981 old_loc = gfc_current_locus;
983 c = next_char ();
984 while (gfc_is_whitespace (c))
985 c = next_char ();
987 /* Character constants to be continued cannot have commentary
988 after the '&'. */
990 if (in_string && c != '\n')
992 gfc_current_locus = old_loc;
993 c = '&';
994 goto done;
997 if (c != '!' && c != '\n')
999 gfc_current_locus = old_loc;
1000 c = '&';
1001 goto done;
1004 prev_openmp_flag = openmp_flag;
1005 continue_flag = 1;
1006 if (c == '!')
1007 skip_comment_line ();
1008 else
1009 gfc_advance_line ();
1011 if (gfc_at_eof())
1012 goto not_continuation;
1014 /* We've got a continuation line. If we are on the very next line after
1015 the last continuation, increment the continuation line count and
1016 check whether the limit has been exceeded. */
1017 if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1)
1019 if (++continue_count == gfc_option.max_continue_free)
1021 if (gfc_notification_std (GFC_STD_GNU) || pedantic)
1022 gfc_warning ("Limit of %d continuations exceeded in "
1023 "statement at %C", gfc_option.max_continue_free);
1027 /* Now find where it continues. First eat any comment lines. */
1028 openmp_cond_flag = skip_free_comments ();
1030 if (gfc_current_locus.lb != NULL
1031 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
1032 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
1034 if (prev_openmp_flag != openmp_flag)
1036 gfc_current_locus = old_loc;
1037 openmp_flag = prev_openmp_flag;
1038 c = '&';
1039 goto done;
1042 /* Now that we have a non-comment line, probe ahead for the
1043 first non-whitespace character. If it is another '&', then
1044 reading starts at the next character, otherwise we must back
1045 up to where the whitespace started and resume from there. */
1047 old_loc = gfc_current_locus;
1049 c = next_char ();
1050 while (gfc_is_whitespace (c))
1051 c = next_char ();
1053 if (openmp_flag)
1055 for (i = 0; i < 5; i++, c = next_char ())
1057 gcc_assert (gfc_wide_tolower (c) == (unsigned char) "!$omp"[i]);
1058 if (i == 4)
1059 old_loc = gfc_current_locus;
1061 while (gfc_is_whitespace (c))
1062 c = next_char ();
1065 if (c != '&')
1067 if (in_string)
1069 if (gfc_option.warn_ampersand)
1070 gfc_warning_now ("Missing '&' in continued character "
1071 "constant at %C");
1072 gfc_current_locus.nextc--;
1074 /* Both !$omp and !$ -fopenmp continuation lines have & on the
1075 continuation line only optionally. */
1076 else if (openmp_flag || openmp_cond_flag)
1077 gfc_current_locus.nextc--;
1078 else
1080 c = ' ';
1081 gfc_current_locus = old_loc;
1082 goto done;
1086 else
1088 /* Fixed form continuation. */
1089 if (!in_string && c == '!')
1091 /* Skip comment at end of line. */
1094 c = next_char ();
1096 while (c != '\n');
1098 /* Avoid truncation warnings for comment ending lines. */
1099 gfc_current_locus.lb->truncated = 0;
1102 if (c != '\n')
1103 goto done;
1105 prev_openmp_flag = openmp_flag;
1106 continue_flag = 1;
1107 old_loc = gfc_current_locus;
1109 gfc_advance_line ();
1110 skip_fixed_comments ();
1112 /* See if this line is a continuation line. */
1113 if (openmp_flag != prev_openmp_flag)
1115 openmp_flag = prev_openmp_flag;
1116 goto not_continuation;
1119 if (!openmp_flag)
1120 for (i = 0; i < 5; i++)
1122 c = next_char ();
1123 if (c != ' ')
1124 goto not_continuation;
1126 else
1127 for (i = 0; i < 5; i++)
1129 c = next_char ();
1130 if (gfc_wide_tolower (c) != (unsigned char) "*$omp"[i])
1131 goto not_continuation;
1134 c = next_char ();
1135 if (c == '0' || c == ' ' || c == '\n')
1136 goto not_continuation;
1138 /* We've got a continuation line. If we are on the very next line after
1139 the last continuation, increment the continuation line count and
1140 check whether the limit has been exceeded. */
1141 if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1)
1143 if (++continue_count == gfc_option.max_continue_fixed)
1145 if (gfc_notification_std (GFC_STD_GNU) || pedantic)
1146 gfc_warning ("Limit of %d continuations exceeded in "
1147 "statement at %C",
1148 gfc_option.max_continue_fixed);
1152 if (gfc_current_locus.lb != NULL
1153 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
1154 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
1157 /* Ready to read first character of continuation line, which might
1158 be another continuation line! */
1159 goto restart;
1161 not_continuation:
1162 c = '\n';
1163 gfc_current_locus = old_loc;
1165 done:
1166 if (c == '\n')
1167 continue_count = 0;
1168 continue_flag = 0;
1169 return c;
1173 /* Get the next character of input, folded to lowercase. In fixed
1174 form mode, we also ignore spaces. When matcher subroutines are
1175 parsing character literals, they have to call
1176 gfc_next_char_literal(). */
1178 gfc_char_t
1179 gfc_next_char (void)
1181 gfc_char_t c;
1185 c = gfc_next_char_literal (0);
1187 while (gfc_current_form == FORM_FIXED && gfc_is_whitespace (c));
1189 return gfc_wide_tolower (c);
1192 char
1193 gfc_next_ascii_char (void)
1195 gfc_char_t c = gfc_next_char ();
1197 return (gfc_wide_fits_in_byte (c) ? (unsigned char) c
1198 : (unsigned char) UCHAR_MAX);
1202 gfc_char_t
1203 gfc_peek_char (void)
1205 locus old_loc;
1206 gfc_char_t c;
1208 old_loc = gfc_current_locus;
1209 c = gfc_next_char ();
1210 gfc_current_locus = old_loc;
1212 return c;
1216 char
1217 gfc_peek_ascii_char (void)
1219 gfc_char_t c = gfc_peek_char ();
1221 return (gfc_wide_fits_in_byte (c) ? (unsigned char) c
1222 : (unsigned char) UCHAR_MAX);
1226 /* Recover from an error. We try to get past the current statement
1227 and get lined up for the next. The next statement follows a '\n'
1228 or a ';'. We also assume that we are not within a character
1229 constant, and deal with finding a '\'' or '"'. */
1231 void
1232 gfc_error_recovery (void)
1234 gfc_char_t c, delim;
1236 if (gfc_at_eof ())
1237 return;
1239 for (;;)
1241 c = gfc_next_char ();
1242 if (c == '\n' || c == ';')
1243 break;
1245 if (c != '\'' && c != '"')
1247 if (gfc_at_eof ())
1248 break;
1249 continue;
1251 delim = c;
1253 for (;;)
1255 c = next_char ();
1257 if (c == delim)
1258 break;
1259 if (c == '\n')
1260 return;
1261 if (c == '\\')
1263 c = next_char ();
1264 if (c == '\n')
1265 return;
1268 if (gfc_at_eof ())
1269 break;
1274 /* Read ahead until the next character to be read is not whitespace. */
1276 void
1277 gfc_gobble_whitespace (void)
1279 static int linenum = 0;
1280 locus old_loc;
1281 gfc_char_t c;
1285 old_loc = gfc_current_locus;
1286 c = gfc_next_char_literal (0);
1287 /* Issue a warning for nonconforming tabs. We keep track of the line
1288 number because the Fortran matchers will often back up and the same
1289 line will be scanned multiple times. */
1290 if (!gfc_option.warn_tabs && c == '\t')
1292 int cur_linenum = LOCATION_LINE (gfc_current_locus.lb->location);
1293 if (cur_linenum != linenum)
1295 linenum = cur_linenum;
1296 gfc_warning_now ("Nonconforming tab character at %C");
1300 while (gfc_is_whitespace (c));
1302 gfc_current_locus = old_loc;
1306 /* Load a single line into pbuf.
1308 If pbuf points to a NULL pointer, it is allocated.
1309 We truncate lines that are too long, unless we're dealing with
1310 preprocessor lines or if the option -ffixed-line-length-none is set,
1311 in which case we reallocate the buffer to fit the entire line, if
1312 need be.
1313 In fixed mode, we expand a tab that occurs within the statement
1314 label region to expand to spaces that leave the next character in
1315 the source region.
1317 If first_char is not NULL, it's a pointer to a single char value holding
1318 the first character of the line, which has already been read by the
1319 caller. This avoids the use of ungetc().
1321 load_line returns whether the line was truncated.
1323 NOTE: The error machinery isn't available at this point, so we can't
1324 easily report line and column numbers consistent with other
1325 parts of gfortran. */
1327 static int
1328 load_line (FILE *input, gfc_char_t **pbuf, int *pbuflen, const int *first_char)
1330 static int linenum = 0, current_line = 1;
1331 int c, maxlen, i, preprocessor_flag, buflen = *pbuflen;
1332 int trunc_flag = 0, seen_comment = 0;
1333 int seen_printable = 0, seen_ampersand = 0;
1334 gfc_char_t *buffer;
1335 bool found_tab = false;
1337 /* Determine the maximum allowed line length. */
1338 if (gfc_current_form == FORM_FREE)
1339 maxlen = gfc_option.free_line_length;
1340 else if (gfc_current_form == FORM_FIXED)
1341 maxlen = gfc_option.fixed_line_length;
1342 else
1343 maxlen = 72;
1345 if (*pbuf == NULL)
1347 /* Allocate the line buffer, storing its length into buflen.
1348 Note that if maxlen==0, indicating that arbitrary-length lines
1349 are allowed, the buffer will be reallocated if this length is
1350 insufficient; since 132 characters is the length of a standard
1351 free-form line, we use that as a starting guess. */
1352 if (maxlen > 0)
1353 buflen = maxlen;
1354 else
1355 buflen = 132;
1357 *pbuf = gfc_get_wide_string (buflen + 1);
1360 i = 0;
1361 buffer = *pbuf;
1363 if (first_char)
1364 c = *first_char;
1365 else
1366 c = getc (input);
1368 /* In order to not truncate preprocessor lines, we have to
1369 remember that this is one. */
1370 preprocessor_flag = (c == '#' ? 1 : 0);
1372 for (;;)
1374 if (c == EOF)
1375 break;
1377 if (c == '\n')
1379 /* Check for illegal use of ampersand. See F95 Standard 3.3.1.3. */
1380 if (gfc_current_form == FORM_FREE
1381 && !seen_printable && seen_ampersand)
1383 if (pedantic)
1384 gfc_error_now ("'&' not allowed by itself in line %d",
1385 current_line);
1386 else
1387 gfc_warning_now ("'&' not allowed by itself in line %d",
1388 current_line);
1390 break;
1393 if (c == '\r' || c == '\0')
1394 goto next_char; /* Gobble characters. */
1396 if (c == '&')
1398 if (seen_ampersand)
1399 seen_ampersand = 0;
1400 else
1401 seen_ampersand = 1;
1404 if ((c != '&' && c != '!' && c != ' ') || (c == '!' && !seen_ampersand))
1405 seen_printable = 1;
1407 /* Is this a fixed-form comment? */
1408 if (gfc_current_form == FORM_FIXED && i == 0
1409 && (c == '*' || c == 'c' || c == 'd'))
1410 seen_comment = 1;
1412 /* Vendor extension: "<tab>1" marks a continuation line. */
1413 if (found_tab)
1415 found_tab = false;
1416 if (c >= '1' && c <= '9')
1418 *(buffer-1) = c;
1419 goto next_char;
1423 if (gfc_current_form == FORM_FIXED && c == '\t' && i < 6)
1425 found_tab = true;
1427 if (!gfc_option.warn_tabs && seen_comment == 0
1428 && current_line != linenum)
1430 linenum = current_line;
1431 gfc_warning_now ("Nonconforming tab character in column %d "
1432 "of line %d", i+1, linenum);
1435 while (i < 6)
1437 *buffer++ = ' ';
1438 i++;
1441 goto next_char;
1444 *buffer++ = c;
1445 i++;
1447 if (maxlen == 0 || preprocessor_flag)
1449 if (i >= buflen)
1451 /* Reallocate line buffer to double size to hold the
1452 overlong line. */
1453 buflen = buflen * 2;
1454 *pbuf = xrealloc (*pbuf, (buflen + 1) * sizeof (gfc_char_t));
1455 buffer = (*pbuf) + i;
1458 else if (i >= maxlen)
1460 /* Truncate the rest of the line. */
1461 for (;;)
1463 c = getc (input);
1464 if (c == '\n' || c == EOF)
1465 break;
1467 trunc_flag = 1;
1470 c = '\n';
1471 continue;
1474 next_char:
1475 c = getc (input);
1478 /* Pad lines to the selected line length in fixed form. */
1479 if (gfc_current_form == FORM_FIXED
1480 && gfc_option.fixed_line_length != 0
1481 && !preprocessor_flag
1482 && c != EOF)
1484 while (i++ < maxlen)
1485 *buffer++ = ' ';
1488 *buffer = '\0';
1489 *pbuflen = buflen;
1490 current_line++;
1492 return trunc_flag;
1496 /* Get a gfc_file structure, initialize it and add it to
1497 the file stack. */
1499 static gfc_file *
1500 get_file (const char *name, enum lc_reason reason ATTRIBUTE_UNUSED)
1502 gfc_file *f;
1504 f = gfc_getmem (sizeof (gfc_file));
1506 f->filename = gfc_getmem (strlen (name) + 1);
1507 strcpy (f->filename, name);
1509 f->next = file_head;
1510 file_head = f;
1512 f->up = current_file;
1513 if (current_file != NULL)
1514 f->inclusion_line = current_file->line;
1516 linemap_add (line_table, reason, false, f->filename, 1);
1518 return f;
1522 /* Deal with a line from the C preprocessor. The
1523 initial octothorp has already been seen. */
1525 static void
1526 preprocessor_line (gfc_char_t *c)
1528 bool flag[5];
1529 int i, line;
1530 gfc_char_t *wide_filename;
1531 gfc_file *f;
1532 int escaped, unescape;
1533 char *filename;
1535 c++;
1536 while (*c == ' ' || *c == '\t')
1537 c++;
1539 if (*c < '0' || *c > '9')
1540 goto bad_cpp_line;
1542 line = wide_atoi (c);
1544 c = wide_strchr (c, ' ');
1545 if (c == NULL)
1547 /* No file name given. Set new line number. */
1548 current_file->line = line;
1549 return;
1552 /* Skip spaces. */
1553 while (*c == ' ' || *c == '\t')
1554 c++;
1556 /* Skip quote. */
1557 if (*c != '"')
1558 goto bad_cpp_line;
1559 ++c;
1561 wide_filename = c;
1563 /* Make filename end at quote. */
1564 unescape = 0;
1565 escaped = false;
1566 while (*c && ! (!escaped && *c == '"'))
1568 if (escaped)
1569 escaped = false;
1570 else if (*c == '\\')
1572 escaped = true;
1573 unescape++;
1575 ++c;
1578 if (! *c)
1579 /* Preprocessor line has no closing quote. */
1580 goto bad_cpp_line;
1582 *c++ = '\0';
1584 /* Undo effects of cpp_quote_string. */
1585 if (unescape)
1587 gfc_char_t *s = wide_filename;
1588 gfc_char_t *d = gfc_get_wide_string (c - wide_filename - unescape);
1590 wide_filename = d;
1591 while (*s)
1593 if (*s == '\\')
1594 *d++ = *++s;
1595 else
1596 *d++ = *s;
1597 s++;
1599 *d = '\0';
1602 /* Get flags. */
1604 flag[1] = flag[2] = flag[3] = flag[4] = false;
1606 for (;;)
1608 c = wide_strchr (c, ' ');
1609 if (c == NULL)
1610 break;
1612 c++;
1613 i = wide_atoi (c);
1615 if (1 <= i && i <= 4)
1616 flag[i] = true;
1619 /* Convert the filename in wide characters into a filename in narrow
1620 characters. */
1621 filename = gfc_widechar_to_char (wide_filename, -1);
1623 /* Interpret flags. */
1625 if (flag[1]) /* Starting new file. */
1627 f = get_file (filename, LC_RENAME);
1628 add_file_change (f->filename, f->inclusion_line);
1629 current_file = f;
1632 if (flag[2]) /* Ending current file. */
1634 if (!current_file->up
1635 || strcmp (current_file->up->filename, filename) != 0)
1637 gfc_warning_now ("%s:%d: file %s left but not entered",
1638 current_file->filename, current_file->line,
1639 filename);
1640 if (unescape)
1641 gfc_free (wide_filename);
1642 gfc_free (filename);
1643 return;
1646 add_file_change (NULL, line);
1647 current_file = current_file->up;
1648 linemap_add (line_table, LC_RENAME, false, current_file->filename,
1649 current_file->line);
1652 /* The name of the file can be a temporary file produced by
1653 cpp. Replace the name if it is different. */
1655 if (strcmp (current_file->filename, filename) != 0)
1657 gfc_free (current_file->filename);
1658 current_file->filename = gfc_getmem (strlen (filename) + 1);
1659 strcpy (current_file->filename, filename);
1662 /* Set new line number. */
1663 current_file->line = line;
1664 if (unescape)
1665 gfc_free (wide_filename);
1666 gfc_free (filename);
1667 return;
1669 bad_cpp_line:
1670 gfc_warning_now ("%s:%d: Illegal preprocessor directive",
1671 current_file->filename, current_file->line);
1672 current_file->line++;
1676 static try load_file (const char *, bool);
1678 /* include_line()-- Checks a line buffer to see if it is an include
1679 line. If so, we call load_file() recursively to load the included
1680 file. We never return a syntax error because a statement like
1681 "include = 5" is perfectly legal. We return false if no include was
1682 processed or true if we matched an include. */
1684 static bool
1685 include_line (gfc_char_t *line)
1687 gfc_char_t quote, *c, *begin, *stop;
1688 char *filename;
1690 c = line;
1692 if (gfc_option.flag_openmp)
1694 if (gfc_current_form == FORM_FREE)
1696 while (*c == ' ' || *c == '\t')
1697 c++;
1698 if (*c == '!' && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
1699 c += 3;
1701 else
1703 if ((*c == '!' || *c == 'c' || *c == 'C' || *c == '*')
1704 && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
1705 c += 3;
1709 while (*c == ' ' || *c == '\t')
1710 c++;
1712 if (gfc_wide_strncasecmp (c, "include", 7))
1713 return false;
1715 c += 7;
1716 while (*c == ' ' || *c == '\t')
1717 c++;
1719 /* Find filename between quotes. */
1721 quote = *c++;
1722 if (quote != '"' && quote != '\'')
1723 return false;
1725 begin = c;
1727 while (*c != quote && *c != '\0')
1728 c++;
1730 if (*c == '\0')
1731 return false;
1733 stop = c++;
1735 while (*c == ' ' || *c == '\t')
1736 c++;
1738 if (*c != '\0' && *c != '!')
1739 return false;
1741 /* We have an include line at this point. */
1743 *stop = '\0'; /* It's ok to trash the buffer, as this line won't be
1744 read by anything else. */
1746 filename = gfc_widechar_to_char (begin, -1);
1747 load_file (filename, false);
1748 gfc_free (filename);
1749 return true;
1753 /* Load a file into memory by calling load_line until the file ends. */
1755 static try
1756 load_file (const char *filename, bool initial)
1758 gfc_char_t *line;
1759 gfc_linebuf *b;
1760 gfc_file *f;
1761 FILE *input;
1762 int len, line_len;
1763 bool first_line;
1765 for (f = current_file; f; f = f->up)
1766 if (strcmp (filename, f->filename) == 0)
1768 gfc_error_now ("File '%s' is being included recursively", filename);
1769 return FAILURE;
1772 if (initial)
1774 if (gfc_src_file)
1776 input = gfc_src_file;
1777 gfc_src_file = NULL;
1779 else
1780 input = gfc_open_file (filename);
1781 if (input == NULL)
1783 gfc_error_now ("Can't open file '%s'", filename);
1784 return FAILURE;
1787 else
1789 input = gfc_open_included_file (filename, false, false);
1790 if (input == NULL)
1792 gfc_error_now ("Can't open included file '%s'", filename);
1793 return FAILURE;
1797 /* Load the file. */
1799 f = get_file (filename, initial ? LC_RENAME : LC_ENTER);
1800 if (!initial)
1801 add_file_change (f->filename, f->inclusion_line);
1802 current_file = f;
1803 current_file->line = 1;
1804 line = NULL;
1805 line_len = 0;
1806 first_line = true;
1808 if (initial && gfc_src_preprocessor_lines[0])
1810 preprocessor_line (gfc_src_preprocessor_lines[0]);
1811 gfc_free (gfc_src_preprocessor_lines[0]);
1812 gfc_src_preprocessor_lines[0] = NULL;
1813 if (gfc_src_preprocessor_lines[1])
1815 preprocessor_line (gfc_src_preprocessor_lines[1]);
1816 gfc_free (gfc_src_preprocessor_lines[1]);
1817 gfc_src_preprocessor_lines[1] = NULL;
1821 for (;;)
1823 int trunc = load_line (input, &line, &line_len, NULL);
1825 len = gfc_wide_strlen (line);
1826 if (feof (input) && len == 0)
1827 break;
1829 /* If this is the first line of the file, it can contain a byte
1830 order mark (BOM), which we will ignore:
1831 FF FE is UTF-16 little endian,
1832 FE FF is UTF-16 big endian,
1833 EF BB BF is UTF-8. */
1834 if (first_line
1835 && ((line_len >= 2 && line[0] == (unsigned char) '\xFF'
1836 && line[1] == (unsigned char) '\xFE')
1837 || (line_len >= 2 && line[0] == (unsigned char) '\xFE'
1838 && line[1] == (unsigned char) '\xFF')
1839 || (line_len >= 3 && line[0] == (unsigned char) '\xEF'
1840 && line[1] == (unsigned char) '\xBB'
1841 && line[2] == (unsigned char) '\xBF')))
1843 int n = line[1] == (unsigned char) '\xBB' ? 3 : 2;
1844 gfc_char_t *new = gfc_get_wide_string (line_len);
1846 wide_strcpy (new, &line[n]);
1847 gfc_free (line);
1848 line = new;
1849 len -= n;
1852 /* There are three things this line can be: a line of Fortran
1853 source, an include line or a C preprocessor directive. */
1855 if (line[0] == '#')
1857 /* When -g3 is specified, it's possible that we emit #define
1858 and #undef lines, which we need to pass to the middle-end
1859 so that it can emit correct debug info. */
1860 if (debug_info_level == DINFO_LEVEL_VERBOSE
1861 && (wide_strncmp (line, "#define ", 8) == 0
1862 || wide_strncmp (line, "#undef ", 7) == 0))
1864 else
1866 preprocessor_line (line);
1867 continue;
1871 /* Preprocessed files have preprocessor lines added before the byte
1872 order mark, so first_line is not about the first line of the file
1873 but the first line that's not a preprocessor line. */
1874 first_line = false;
1876 if (include_line (line))
1878 current_file->line++;
1879 continue;
1882 /* Add line. */
1884 b = gfc_getmem (gfc_linebuf_header_size
1885 + (len + 1) * sizeof (gfc_char_t));
1887 b->location
1888 = linemap_line_start (line_table, current_file->line++, 120);
1889 b->file = current_file;
1890 b->truncated = trunc;
1891 wide_strcpy (b->line, line);
1893 if (line_head == NULL)
1894 line_head = b;
1895 else
1896 line_tail->next = b;
1898 line_tail = b;
1900 while (file_changes_cur < file_changes_count)
1901 file_changes[file_changes_cur++].lb = b;
1904 /* Release the line buffer allocated in load_line. */
1905 gfc_free (line);
1907 fclose (input);
1909 if (!initial)
1910 add_file_change (NULL, current_file->inclusion_line + 1);
1911 current_file = current_file->up;
1912 linemap_add (line_table, LC_LEAVE, 0, NULL, 0);
1913 return SUCCESS;
1917 /* Open a new file and start scanning from that file. Returns SUCCESS
1918 if everything went OK, FAILURE otherwise. If form == FORM_UKNOWN
1919 it tries to determine the source form from the filename, defaulting
1920 to free form. */
1923 gfc_new_file (void)
1925 try result;
1927 if (gfc_cpp_enabled ())
1929 result = gfc_cpp_preprocess (gfc_source_file);
1930 if (!gfc_cpp_preprocess_only ())
1931 result = load_file (gfc_cpp_temporary_file (), true);
1933 else
1934 result = load_file (gfc_source_file, true);
1936 gfc_current_locus.lb = line_head;
1937 gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line;
1939 #if 0 /* Debugging aid. */
1940 for (; line_head; line_head = line_head->next)
1941 printf ("%s:%3d %s\n", LOCATION_FILE (line_head->location),
1942 LOCATION_LINE (line_head->location), line_head->line);
1944 exit (0);
1945 #endif
1947 return result;
1950 static char *
1951 unescape_filename (const char *ptr)
1953 const char *p = ptr, *s;
1954 char *d, *ret;
1955 int escaped, unescape = 0;
1957 /* Make filename end at quote. */
1958 escaped = false;
1959 while (*p && ! (! escaped && *p == '"'))
1961 if (escaped)
1962 escaped = false;
1963 else if (*p == '\\')
1965 escaped = true;
1966 unescape++;
1968 ++p;
1971 if (!*p || p[1])
1972 return NULL;
1974 /* Undo effects of cpp_quote_string. */
1975 s = ptr;
1976 d = gfc_getmem (p + 1 - ptr - unescape);
1977 ret = d;
1979 while (s != p)
1981 if (*s == '\\')
1982 *d++ = *++s;
1983 else
1984 *d++ = *s;
1985 s++;
1987 *d = '\0';
1988 return ret;
1991 /* For preprocessed files, if the first tokens are of the form # NUM.
1992 handle the directives so we know the original file name. */
1994 const char *
1995 gfc_read_orig_filename (const char *filename, const char **canon_source_file)
1997 int c, len;
1998 char *dirname, *tmp;
2000 gfc_src_file = gfc_open_file (filename);
2001 if (gfc_src_file == NULL)
2002 return NULL;
2004 c = getc (gfc_src_file);
2006 if (c != '#')
2007 return NULL;
2009 len = 0;
2010 load_line (gfc_src_file, &gfc_src_preprocessor_lines[0], &len, &c);
2012 if (wide_strncmp (gfc_src_preprocessor_lines[0], "# 1 \"", 5) != 0)
2013 return NULL;
2015 tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[0][5], -1);
2016 filename = unescape_filename (tmp);
2017 gfc_free (tmp);
2018 if (filename == NULL)
2019 return NULL;
2021 c = getc (gfc_src_file);
2023 if (c != '#')
2024 return filename;
2026 len = 0;
2027 load_line (gfc_src_file, &gfc_src_preprocessor_lines[1], &len, &c);
2029 if (wide_strncmp (gfc_src_preprocessor_lines[1], "# 1 \"", 5) != 0)
2030 return filename;
2032 tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[1][5], -1);
2033 dirname = unescape_filename (tmp);
2034 gfc_free (tmp);
2035 if (dirname == NULL)
2036 return filename;
2038 len = strlen (dirname);
2039 if (len < 3 || dirname[len - 1] != '/' || dirname[len - 2] != '/')
2041 gfc_free (dirname);
2042 return filename;
2044 dirname[len - 2] = '\0';
2045 set_src_pwd (dirname);
2047 if (! IS_ABSOLUTE_PATH (filename))
2049 char *p = gfc_getmem (len + strlen (filename));
2051 memcpy (p, dirname, len - 2);
2052 p[len - 2] = '/';
2053 strcpy (p + len - 1, filename);
2054 *canon_source_file = p;
2057 gfc_free (dirname);
2058 return filename;