Mark ChangeLog
[official-gcc.git] / gcc / fortran / scanner.c
blobedc6578c89dad5299880149f3f5350cb773b8d56
1 /* Character scanner.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
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"
49 /* Structure for holding module and include file search path. */
50 typedef struct gfc_directorylist
52 char *path;
53 struct gfc_directorylist *next;
55 gfc_directorylist;
57 /* List of include file search directories. */
58 static gfc_directorylist *include_dirs;
60 static gfc_file *file_head, *current_file;
62 static int continue_flag, end_flag, openmp_flag;
63 static int continue_count, continue_line;
64 static locus openmp_locus;
66 gfc_source_form gfc_current_form;
67 static gfc_linebuf *line_head, *line_tail;
69 locus gfc_current_locus;
70 const char *gfc_source_file;
71 static FILE *gfc_src_file;
72 static char *gfc_src_preprocessor_lines[2];
74 extern int pedantic;
76 /* Main scanner initialization. */
78 void
79 gfc_scanner_init_1 (void)
81 file_head = NULL;
82 line_head = NULL;
83 line_tail = NULL;
85 continue_count = 0;
86 continue_line = 0;
88 end_flag = 0;
92 /* Main scanner destructor. */
94 void
95 gfc_scanner_done_1 (void)
97 gfc_linebuf *lb;
98 gfc_file *f;
100 while(line_head != NULL)
102 lb = line_head->next;
103 gfc_free(line_head);
104 line_head = lb;
107 while(file_head != NULL)
109 f = file_head->next;
110 gfc_free(file_head->filename);
111 gfc_free(file_head);
112 file_head = f;
118 /* Adds path to the list pointed to by list. */
120 void
121 gfc_add_include_path (const char *path)
123 gfc_directorylist *dir;
124 const char *p;
126 p = path;
127 while (*p == ' ' || *p == '\t') /* someone might do 'gfortran "-I include"' */
128 if (*p++ == '\0')
129 return;
131 dir = include_dirs;
132 if (!dir)
134 dir = include_dirs = gfc_getmem (sizeof (gfc_directorylist));
136 else
138 while (dir->next)
139 dir = dir->next;
141 dir->next = gfc_getmem (sizeof (gfc_directorylist));
142 dir = dir->next;
145 dir->next = NULL;
146 dir->path = gfc_getmem (strlen (p) + 2);
147 strcpy (dir->path, p);
148 strcat (dir->path, "/"); /* make '/' last character */
152 /* Release resources allocated for options. */
154 void
155 gfc_release_include_path (void)
157 gfc_directorylist *p;
159 gfc_free (gfc_option.module_dir);
160 while (include_dirs != NULL)
162 p = include_dirs;
163 include_dirs = include_dirs->next;
164 gfc_free (p->path);
165 gfc_free (p);
169 /* Opens file for reading, searching through the include directories
170 given if necessary. If the include_cwd argument is true, we try
171 to open the file in the current directory first. */
173 FILE *
174 gfc_open_included_file (const char *name, const bool include_cwd)
176 char *fullname;
177 gfc_directorylist *p;
178 FILE *f;
180 if (IS_ABSOLUTE_PATH (name))
181 return gfc_open_file (name);
183 if (include_cwd)
185 f = gfc_open_file (name);
186 if (f != NULL)
187 return f;
190 for (p = include_dirs; p; p = p->next)
192 fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1);
193 strcpy (fullname, p->path);
194 strcat (fullname, name);
196 f = gfc_open_file (fullname);
197 if (f != NULL)
198 return f;
201 return NULL;
204 /* Test to see if we're at the end of the main source file. */
207 gfc_at_end (void)
210 return end_flag;
214 /* Test to see if we're at the end of the current file. */
217 gfc_at_eof (void)
220 if (gfc_at_end ())
221 return 1;
223 if (line_head == NULL)
224 return 1; /* Null file */
226 if (gfc_current_locus.lb == NULL)
227 return 1;
229 return 0;
233 /* Test to see if we're at the beginning of a new line. */
236 gfc_at_bol (void)
238 if (gfc_at_eof ())
239 return 1;
241 return (gfc_current_locus.nextc == gfc_current_locus.lb->line);
245 /* Test to see if we're at the end of a line. */
248 gfc_at_eol (void)
251 if (gfc_at_eof ())
252 return 1;
254 return (*gfc_current_locus.nextc == '\0');
258 /* Advance the current line pointer to the next line. */
260 void
261 gfc_advance_line (void)
263 if (gfc_at_end ())
264 return;
266 if (gfc_current_locus.lb == NULL)
268 end_flag = 1;
269 return;
272 gfc_current_locus.lb = gfc_current_locus.lb->next;
274 if (gfc_current_locus.lb != NULL)
275 gfc_current_locus.nextc = gfc_current_locus.lb->line;
276 else
278 gfc_current_locus.nextc = NULL;
279 end_flag = 1;
284 /* Get the next character from the input, advancing gfc_current_file's
285 locus. When we hit the end of the line or the end of the file, we
286 start returning a '\n' in order to complete the current statement.
287 No Fortran line conventions are implemented here.
289 Requiring explicit advances to the next line prevents the parse
290 pointer from being on the wrong line if the current statement ends
291 prematurely. */
293 static int
294 next_char (void)
296 int c;
298 if (gfc_current_locus.nextc == NULL)
299 return '\n';
301 c = (unsigned char) *gfc_current_locus.nextc++;
302 if (c == '\0')
304 gfc_current_locus.nextc--; /* Remain on this line. */
305 c = '\n';
308 return c;
311 /* Skip a comment. When we come here the parse pointer is positioned
312 immediately after the comment character. If we ever implement
313 compiler directives withing comments, here is where we parse the
314 directive. */
316 static void
317 skip_comment_line (void)
319 char c;
323 c = next_char ();
325 while (c != '\n');
327 gfc_advance_line ();
331 /* Comment lines are null lines, lines containing only blanks or lines
332 on which the first nonblank line is a '!'.
333 Return true if !$ openmp conditional compilation sentinel was
334 seen. */
336 static bool
337 skip_free_comments (void)
339 locus start;
340 char c;
341 int at_bol;
343 for (;;)
345 at_bol = gfc_at_bol ();
346 start = gfc_current_locus;
347 if (gfc_at_eof ())
348 break;
351 c = next_char ();
352 while (gfc_is_whitespace (c));
354 if (c == '\n')
356 gfc_advance_line ();
357 continue;
360 if (c == '!')
362 /* If -fopenmp, we need to handle here 2 things:
363 1) don't treat !$omp as comments, but directives
364 2) handle OpenMP conditional compilation, where
365 !$ should be treated as 2 spaces (for initial lines
366 only if followed by space). */
367 if (gfc_option.flag_openmp && at_bol)
369 locus old_loc = gfc_current_locus;
370 if (next_char () == '$')
372 c = next_char ();
373 if (c == 'o' || c == 'O')
375 if (((c = next_char ()) == 'm' || c == 'M')
376 && ((c = next_char ()) == 'p' || c == 'P')
377 && ((c = next_char ()) == ' ' || continue_flag))
379 while (gfc_is_whitespace (c))
380 c = next_char ();
381 if (c != '\n' && c != '!')
383 openmp_flag = 1;
384 openmp_locus = old_loc;
385 gfc_current_locus = start;
386 return false;
389 gfc_current_locus = old_loc;
390 next_char ();
391 c = next_char ();
393 if (continue_flag || c == ' ')
395 gfc_current_locus = old_loc;
396 next_char ();
397 openmp_flag = 0;
398 return true;
401 gfc_current_locus = old_loc;
403 skip_comment_line ();
404 continue;
407 break;
410 if (openmp_flag && at_bol)
411 openmp_flag = 0;
412 gfc_current_locus = start;
413 return false;
417 /* Skip comment lines in fixed source mode. We have the same rules as
418 in skip_free_comment(), except that we can have a 'c', 'C' or '*'
419 in column 1, and a '!' cannot be in column 6. Also, we deal with
420 lines with 'd' or 'D' in column 1, if the user requested this. */
422 static void
423 skip_fixed_comments (void)
425 locus start;
426 int col;
427 char c;
429 if (! gfc_at_bol ())
431 start = gfc_current_locus;
432 if (! gfc_at_eof ())
435 c = next_char ();
436 while (gfc_is_whitespace (c));
438 if (c == '\n')
439 gfc_advance_line ();
440 else if (c == '!')
441 skip_comment_line ();
444 if (! gfc_at_bol ())
446 gfc_current_locus = start;
447 return;
451 for (;;)
453 start = gfc_current_locus;
454 if (gfc_at_eof ())
455 break;
457 c = next_char ();
458 if (c == '\n')
460 gfc_advance_line ();
461 continue;
464 if (c == '!' || c == 'c' || c == 'C' || c == '*')
466 /* If -fopenmp, we need to handle here 2 things:
467 1) don't treat !$omp|c$omp|*$omp as comments, but directives
468 2) handle OpenMP conditional compilation, where
469 !$|c$|*$ should be treated as 2 spaces if the characters
470 in columns 3 to 6 are valid fixed form label columns
471 characters. */
472 if (gfc_option.flag_openmp)
474 if (next_char () == '$')
476 c = next_char ();
477 if (c == 'o' || c == 'O')
479 if (((c = next_char ()) == 'm' || c == 'M')
480 && ((c = next_char ()) == 'p' || c == 'P'))
482 c = next_char ();
483 if (c != '\n'
484 && ((openmp_flag && continue_flag)
485 || c == ' ' || c == '0'))
487 c = next_char ();
488 while (gfc_is_whitespace (c))
489 c = next_char ();
490 if (c != '\n' && c != '!')
492 /* Canonicalize to *$omp. */
493 *start.nextc = '*';
494 openmp_flag = 1;
495 gfc_current_locus = start;
496 return;
501 else
503 int digit_seen = 0;
505 for (col = 3; col < 6; col++, c = next_char ())
506 if (c == ' ')
507 continue;
508 else if (c < '0' || c > '9')
509 break;
510 else
511 digit_seen = 1;
513 if (col == 6 && c != '\n'
514 && ((continue_flag && !digit_seen)
515 || c == ' ' || c == '0'))
517 gfc_current_locus = start;
518 start.nextc[0] = ' ';
519 start.nextc[1] = ' ';
520 continue;
524 gfc_current_locus = start;
526 skip_comment_line ();
527 continue;
530 if (gfc_option.flag_d_lines != -1 && (c == 'd' || c == 'D'))
532 if (gfc_option.flag_d_lines == 0)
534 skip_comment_line ();
535 continue;
537 else
538 *start.nextc = c = ' ';
541 col = 1;
543 while (gfc_is_whitespace (c))
545 c = next_char ();
546 col++;
549 if (c == '\n')
551 gfc_advance_line ();
552 continue;
555 if (col != 6 && c == '!')
557 skip_comment_line ();
558 continue;
561 break;
564 openmp_flag = 0;
565 gfc_current_locus = start;
569 /* Skips the current line if it is a comment. */
571 void
572 gfc_skip_comments (void)
574 if (gfc_current_form == FORM_FREE)
575 skip_free_comments ();
576 else
577 skip_fixed_comments ();
581 /* Get the next character from the input, taking continuation lines
582 and end-of-line comments into account. This implies that comment
583 lines between continued lines must be eaten here. For higher-level
584 subroutines, this flattens continued lines into a single logical
585 line. The in_string flag denotes whether we're inside a character
586 context or not. */
589 gfc_next_char_literal (int in_string)
591 locus old_loc;
592 int i, c, prev_openmp_flag;
594 continue_flag = 0;
596 restart:
597 c = next_char ();
598 if (gfc_at_end ())
600 continue_count = 0;
601 return c;
604 if (gfc_current_form == FORM_FREE)
606 bool openmp_cond_flag;
608 if (!in_string && c == '!')
610 if (openmp_flag
611 && memcmp (&gfc_current_locus, &openmp_locus,
612 sizeof (gfc_current_locus)) == 0)
613 goto done;
615 /* This line can't be continued */
618 c = next_char ();
620 while (c != '\n');
622 /* Avoid truncation warnings for comment ending lines. */
623 gfc_current_locus.lb->truncated = 0;
625 goto done;
628 if (c != '&')
629 goto done;
631 /* If the next nonblank character is a ! or \n, we've got a
632 continuation line. */
633 old_loc = gfc_current_locus;
635 c = next_char ();
636 while (gfc_is_whitespace (c))
637 c = next_char ();
639 /* Character constants to be continued cannot have commentary
640 after the '&'. */
642 if (in_string && c != '\n')
644 gfc_current_locus = old_loc;
645 c = '&';
646 goto done;
649 if (c != '!' && c != '\n')
651 gfc_current_locus = old_loc;
652 c = '&';
653 goto done;
656 prev_openmp_flag = openmp_flag;
657 continue_flag = 1;
658 if (c == '!')
659 skip_comment_line ();
660 else
661 gfc_advance_line ();
663 if (gfc_at_eof())
664 goto not_continuation;
666 /* We've got a continuation line. If we are on the very next line after
667 the last continuation, increment the continuation line count and
668 check whether the limit has been exceeded. */
669 if (gfc_current_locus.lb->linenum == continue_line + 1)
671 if (++continue_count == gfc_option.max_continue_free)
673 if (gfc_notification_std (GFC_STD_GNU)
674 || pedantic)
675 gfc_warning ("Limit of %d continuations exceeded in statement at %C",
676 gfc_option.max_continue_free);
679 continue_line = gfc_current_locus.lb->linenum;
681 /* Now find where it continues. First eat any comment lines. */
682 openmp_cond_flag = skip_free_comments ();
684 if (prev_openmp_flag != openmp_flag)
686 gfc_current_locus = old_loc;
687 openmp_flag = prev_openmp_flag;
688 c = '&';
689 goto done;
692 /* Now that we have a non-comment line, probe ahead for the
693 first non-whitespace character. If it is another '&', then
694 reading starts at the next character, otherwise we must back
695 up to where the whitespace started and resume from there. */
697 old_loc = gfc_current_locus;
699 c = next_char ();
700 while (gfc_is_whitespace (c))
701 c = next_char ();
703 if (openmp_flag)
705 for (i = 0; i < 5; i++, c = next_char ())
707 gcc_assert (TOLOWER (c) == "!$omp"[i]);
708 if (i == 4)
709 old_loc = gfc_current_locus;
711 while (gfc_is_whitespace (c))
712 c = next_char ();
715 if (c != '&')
717 if (in_string)
719 if (gfc_option.warn_ampersand)
720 gfc_warning_now ("Missing '&' in continued character constant at %C");
721 gfc_current_locus.nextc--;
723 /* Both !$omp and !$ -fopenmp continuation lines have & on the
724 continuation line only optionally. */
725 else if (openmp_flag || openmp_cond_flag)
726 gfc_current_locus.nextc--;
727 else
729 c = ' ';
730 gfc_current_locus = old_loc;
731 goto done;
735 else
737 /* Fixed form continuation. */
738 if (!in_string && c == '!')
740 /* Skip comment at end of line. */
743 c = next_char ();
745 while (c != '\n');
747 /* Avoid truncation warnings for comment ending lines. */
748 gfc_current_locus.lb->truncated = 0;
751 if (c != '\n')
752 goto done;
754 prev_openmp_flag = openmp_flag;
755 continue_flag = 1;
756 old_loc = gfc_current_locus;
758 gfc_advance_line ();
759 skip_fixed_comments ();
761 /* See if this line is a continuation line. */
762 if (openmp_flag != prev_openmp_flag)
764 openmp_flag = prev_openmp_flag;
765 goto not_continuation;
768 if (!openmp_flag)
769 for (i = 0; i < 5; i++)
771 c = next_char ();
772 if (c != ' ')
773 goto not_continuation;
775 else
776 for (i = 0; i < 5; i++)
778 c = next_char ();
779 if (TOLOWER (c) != "*$omp"[i])
780 goto not_continuation;
783 c = next_char ();
784 if (c == '0' || c == ' ' || c == '\n')
785 goto not_continuation;
787 /* We've got a continuation line. If we are on the very next line after
788 the last continuation, increment the continuation line count and
789 check whether the limit has been exceeded. */
790 if (gfc_current_locus.lb->linenum == continue_line + 1)
792 if (++continue_count == gfc_option.max_continue_fixed)
794 if (gfc_notification_std (GFC_STD_GNU)
795 || pedantic)
796 gfc_warning ("Limit of %d continuations exceeded in statement at %C",
797 gfc_option.max_continue_fixed);
801 if (continue_line < gfc_current_locus.lb->linenum)
802 continue_line = gfc_current_locus.lb->linenum;
805 /* Ready to read first character of continuation line, which might
806 be another continuation line! */
807 goto restart;
809 not_continuation:
810 c = '\n';
811 gfc_current_locus = old_loc;
813 done:
814 if (c == '\n')
815 continue_count = 0;
816 continue_flag = 0;
817 return c;
821 /* Get the next character of input, folded to lowercase. In fixed
822 form mode, we also ignore spaces. When matcher subroutines are
823 parsing character literals, they have to call
824 gfc_next_char_literal(). */
827 gfc_next_char (void)
829 int c;
833 c = gfc_next_char_literal (0);
835 while (gfc_current_form == FORM_FIXED && gfc_is_whitespace (c));
837 return TOLOWER (c);
842 gfc_peek_char (void)
844 locus old_loc;
845 int c;
847 old_loc = gfc_current_locus;
848 c = gfc_next_char ();
849 gfc_current_locus = old_loc;
851 return c;
855 /* Recover from an error. We try to get past the current statement
856 and get lined up for the next. The next statement follows a '\n'
857 or a ';'. We also assume that we are not within a character
858 constant, and deal with finding a '\'' or '"'. */
860 void
861 gfc_error_recovery (void)
863 char c, delim;
865 if (gfc_at_eof ())
866 return;
868 for (;;)
870 c = gfc_next_char ();
871 if (c == '\n' || c == ';')
872 break;
874 if (c != '\'' && c != '"')
876 if (gfc_at_eof ())
877 break;
878 continue;
880 delim = c;
882 for (;;)
884 c = next_char ();
886 if (c == delim)
887 break;
888 if (c == '\n')
889 return;
890 if (c == '\\')
892 c = next_char ();
893 if (c == '\n')
894 return;
897 if (gfc_at_eof ())
898 break;
903 /* Read ahead until the next character to be read is not whitespace. */
905 void
906 gfc_gobble_whitespace (void)
908 static int linenum = 0;
909 locus old_loc;
910 int c;
914 old_loc = gfc_current_locus;
915 c = gfc_next_char_literal (0);
916 /* Issue a warning for nonconforming tabs. We keep track of the line
917 number because the Fortran matchers will often back up and the same
918 line will be scanned multiple times. */
919 if (!gfc_option.warn_tabs && c == '\t')
921 #ifdef USE_MAPPED_LOCATION
922 int cur_linenum = LOCATION_LINE (gfc_current_locus.lb->location);
923 #else
924 int cur_linenum = gfc_current_locus.lb->linenum;
925 #endif
926 if (cur_linenum != linenum)
928 linenum = cur_linenum;
929 gfc_warning_now ("Nonconforming tab character at %C");
933 while (gfc_is_whitespace (c));
935 gfc_current_locus = old_loc;
939 /* Load a single line into pbuf.
941 If pbuf points to a NULL pointer, it is allocated.
942 We truncate lines that are too long, unless we're dealing with
943 preprocessor lines or if the option -ffixed-line-length-none is set,
944 in which case we reallocate the buffer to fit the entire line, if
945 need be.
946 In fixed mode, we expand a tab that occurs within the statement
947 label region to expand to spaces that leave the next character in
948 the source region.
949 load_line returns whether the line was truncated.
951 NOTE: The error machinery isn't available at this point, so we can't
952 easily report line and column numbers consistent with other
953 parts of gfortran. */
955 static int
956 load_line (FILE * input, char **pbuf, int *pbuflen)
958 static int linenum = 0, current_line = 1;
959 int c, maxlen, i, preprocessor_flag, buflen = *pbuflen;
960 int trunc_flag = 0, seen_comment = 0;
961 int seen_printable = 0, seen_ampersand = 0;
962 char *buffer;
964 /* Determine the maximum allowed line length.
965 The default for free-form is GFC_MAX_LINE, for fixed-form or for
966 unknown form it is 72. Refer to the documentation in gfc_option_t. */
967 if (gfc_current_form == FORM_FREE)
969 if (gfc_option.free_line_length == -1)
970 maxlen = GFC_MAX_LINE;
971 else
972 maxlen = gfc_option.free_line_length;
974 else if (gfc_current_form == FORM_FIXED)
976 if (gfc_option.fixed_line_length == -1)
977 maxlen = 72;
978 else
979 maxlen = gfc_option.fixed_line_length;
981 else
982 maxlen = 72;
984 if (*pbuf == NULL)
986 /* Allocate the line buffer, storing its length into buflen. */
987 if (maxlen > 0)
988 buflen = maxlen;
989 else
990 buflen = GFC_MAX_LINE;
992 *pbuf = gfc_getmem (buflen + 1);
995 i = 0;
996 buffer = *pbuf;
998 preprocessor_flag = 0;
999 c = fgetc (input);
1000 if (c == '#')
1001 /* In order to not truncate preprocessor lines, we have to
1002 remember that this is one. */
1003 preprocessor_flag = 1;
1004 ungetc (c, input);
1006 for (;;)
1008 c = fgetc (input);
1010 if (c == EOF)
1011 break;
1012 if (c == '\n')
1014 /* Check for illegal use of ampersand. See F95 Standard 3.3.1.3. */
1015 if (gfc_current_form == FORM_FREE
1016 && !seen_printable && seen_ampersand)
1018 if (pedantic)
1019 gfc_error_now
1020 ("'&' not allowed by itself in line %d", current_line);
1021 else
1022 gfc_warning_now
1023 ("'&' not allowed by itself in line %d", current_line);
1025 break;
1028 if (c == '\r')
1029 continue; /* Gobble characters. */
1030 if (c == '\0')
1031 continue;
1033 /* Check for illegal use of ampersand. See F95 Standard 3.3.1.3. */
1034 if (c == '&')
1035 seen_ampersand = 1;
1037 if ((c != ' ' && c != '&' && c != '!') || (c == '!' && !seen_ampersand))
1038 seen_printable = 1;
1040 if (gfc_current_form == FORM_FREE
1041 && c == '!' && !seen_printable && seen_ampersand)
1043 if (pedantic)
1044 gfc_error_now (
1045 "'&' not allowed by itself with comment in line %d", current_line);
1046 else
1047 gfc_warning_now (
1048 "'&' not allowed by itself with comment in line %d", current_line);
1049 seen_printable = 1;
1052 /* Is this a fixed-form comment? */
1053 if (gfc_current_form == FORM_FIXED && i == 0
1054 && (c == '*' || c == 'c' || c == 'd'))
1055 seen_comment = 1;
1057 if (gfc_current_form == FORM_FIXED && c == '\t' && i <= 6)
1059 if (!gfc_option.warn_tabs && seen_comment == 0
1060 && current_line != linenum)
1062 linenum = current_line;
1063 gfc_warning_now (
1064 "Nonconforming tab character in column 1 of line %d", linenum);
1067 while (i <= 6)
1069 *buffer++ = ' ';
1070 i++;
1073 continue;
1076 *buffer++ = c;
1077 i++;
1079 if (maxlen == 0 || preprocessor_flag)
1081 if (i >= buflen)
1083 /* Reallocate line buffer to double size to hold the
1084 overlong line. */
1085 buflen = buflen * 2;
1086 *pbuf = xrealloc (*pbuf, buflen + 1);
1087 buffer = (*pbuf)+i;
1090 else if (i >= maxlen)
1092 /* Truncate the rest of the line. */
1093 for (;;)
1095 c = fgetc (input);
1096 if (c == '\n' || c == EOF)
1097 break;
1099 trunc_flag = 1;
1102 ungetc ('\n', input);
1106 /* Pad lines to the selected line length in fixed form. */
1107 if (gfc_current_form == FORM_FIXED
1108 && gfc_option.fixed_line_length != 0
1109 && !preprocessor_flag
1110 && c != EOF)
1112 while (i++ < maxlen)
1113 *buffer++ = ' ';
1116 *buffer = '\0';
1117 *pbuflen = buflen;
1118 current_line++;
1120 return trunc_flag;
1124 /* Get a gfc_file structure, initialize it and add it to
1125 the file stack. */
1127 static gfc_file *
1128 get_file (const char *name, enum lc_reason reason ATTRIBUTE_UNUSED)
1130 gfc_file *f;
1132 f = gfc_getmem (sizeof (gfc_file));
1134 f->filename = gfc_getmem (strlen (name) + 1);
1135 strcpy (f->filename, name);
1137 f->next = file_head;
1138 file_head = f;
1140 f->included_by = current_file;
1141 if (current_file != NULL)
1142 f->inclusion_line = current_file->line;
1144 #ifdef USE_MAPPED_LOCATION
1145 linemap_add (&line_table, reason, false, f->filename, 1);
1146 #endif
1148 return f;
1151 /* Deal with a line from the C preprocessor. The
1152 initial octothorp has already been seen. */
1154 static void
1155 preprocessor_line (char *c)
1157 bool flag[5];
1158 int i, line;
1159 char *filename;
1160 gfc_file *f;
1161 int escaped, unescape;
1163 c++;
1164 while (*c == ' ' || *c == '\t')
1165 c++;
1167 if (*c < '0' || *c > '9')
1168 goto bad_cpp_line;
1170 line = atoi (c);
1172 c = strchr (c, ' ');
1173 if (c == NULL)
1175 /* No file name given. Set new line number. */
1176 current_file->line = line;
1177 return;
1180 /* Skip spaces. */
1181 while (*c == ' ' || *c == '\t')
1182 c++;
1184 /* Skip quote. */
1185 if (*c != '"')
1186 goto bad_cpp_line;
1187 ++c;
1189 filename = c;
1191 /* Make filename end at quote. */
1192 unescape = 0;
1193 escaped = false;
1194 while (*c && ! (! escaped && *c == '"'))
1196 if (escaped)
1197 escaped = false;
1198 else if (*c == '\\')
1200 escaped = true;
1201 unescape++;
1203 ++c;
1206 if (! *c)
1207 /* Preprocessor line has no closing quote. */
1208 goto bad_cpp_line;
1210 *c++ = '\0';
1212 /* Undo effects of cpp_quote_string. */
1213 if (unescape)
1215 char *s = filename;
1216 char *d = gfc_getmem (c - filename - unescape);
1218 filename = d;
1219 while (*s)
1221 if (*s == '\\')
1222 *d++ = *++s;
1223 else
1224 *d++ = *s;
1225 s++;
1227 *d = '\0';
1230 /* Get flags. */
1232 flag[1] = flag[2] = flag[3] = flag[4] = false;
1234 for (;;)
1236 c = strchr (c, ' ');
1237 if (c == NULL)
1238 break;
1240 c++;
1241 i = atoi (c);
1243 if (1 <= i && i <= 4)
1244 flag[i] = true;
1247 /* Interpret flags. */
1249 if (flag[1]) /* Starting new file. */
1251 f = get_file (filename, LC_RENAME);
1252 f->up = current_file;
1253 current_file = f;
1256 if (flag[2]) /* Ending current file. */
1258 if (!current_file->up
1259 || strcmp (current_file->up->filename, filename) != 0)
1261 gfc_warning_now ("%s:%d: file %s left but not entered",
1262 current_file->filename, current_file->line,
1263 filename);
1264 if (unescape)
1265 gfc_free (filename);
1266 return;
1268 current_file = current_file->up;
1271 /* The name of the file can be a temporary file produced by
1272 cpp. Replace the name if it is different. */
1274 if (strcmp (current_file->filename, filename) != 0)
1276 gfc_free (current_file->filename);
1277 current_file->filename = gfc_getmem (strlen (filename) + 1);
1278 strcpy (current_file->filename, filename);
1281 /* Set new line number. */
1282 current_file->line = line;
1283 if (unescape)
1284 gfc_free (filename);
1285 return;
1287 bad_cpp_line:
1288 gfc_warning_now ("%s:%d: Illegal preprocessor directive",
1289 current_file->filename, current_file->line);
1290 current_file->line++;
1294 static try load_file (const char *, bool);
1296 /* include_line()-- Checks a line buffer to see if it is an include
1297 line. If so, we call load_file() recursively to load the included
1298 file. We never return a syntax error because a statement like
1299 "include = 5" is perfectly legal. We return false if no include was
1300 processed or true if we matched an include. */
1302 static bool
1303 include_line (char *line)
1305 char quote, *c, *begin, *stop;
1307 c = line;
1309 if (gfc_option.flag_openmp)
1311 if (gfc_current_form == FORM_FREE)
1313 while (*c == ' ' || *c == '\t')
1314 c++;
1315 if (*c == '!' && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
1316 c += 3;
1318 else
1320 if ((*c == '!' || *c == 'c' || *c == 'C' || *c == '*')
1321 && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
1322 c += 3;
1326 while (*c == ' ' || *c == '\t')
1327 c++;
1329 if (strncasecmp (c, "include", 7))
1330 return false;
1332 c += 7;
1333 while (*c == ' ' || *c == '\t')
1334 c++;
1336 /* Find filename between quotes. */
1338 quote = *c++;
1339 if (quote != '"' && quote != '\'')
1340 return false;
1342 begin = c;
1344 while (*c != quote && *c != '\0')
1345 c++;
1347 if (*c == '\0')
1348 return false;
1350 stop = c++;
1352 while (*c == ' ' || *c == '\t')
1353 c++;
1355 if (*c != '\0' && *c != '!')
1356 return false;
1358 /* We have an include line at this point. */
1360 *stop = '\0'; /* It's ok to trash the buffer, as this line won't be
1361 read by anything else. */
1363 load_file (begin, false);
1364 return true;
1367 /* Load a file into memory by calling load_line until the file ends. */
1369 static try
1370 load_file (const char *filename, bool initial)
1372 char *line;
1373 gfc_linebuf *b;
1374 gfc_file *f;
1375 FILE *input;
1376 int len, line_len;
1378 for (f = current_file; f; f = f->up)
1379 if (strcmp (filename, f->filename) == 0)
1381 gfc_error_now ("File '%s' is being included recursively", filename);
1382 return FAILURE;
1385 if (initial)
1387 if (gfc_src_file)
1389 input = gfc_src_file;
1390 gfc_src_file = NULL;
1392 else
1393 input = gfc_open_file (filename);
1394 if (input == NULL)
1396 gfc_error_now ("Can't open file '%s'", filename);
1397 return FAILURE;
1400 else
1402 input = gfc_open_included_file (filename, false);
1403 if (input == NULL)
1405 gfc_error_now ("Can't open included file '%s'", filename);
1406 return FAILURE;
1410 /* Load the file. */
1412 f = get_file (filename, initial ? LC_RENAME : LC_ENTER);
1413 f->up = current_file;
1414 current_file = f;
1415 current_file->line = 1;
1416 line = NULL;
1417 line_len = 0;
1419 if (initial && gfc_src_preprocessor_lines[0])
1421 preprocessor_line (gfc_src_preprocessor_lines[0]);
1422 gfc_free (gfc_src_preprocessor_lines[0]);
1423 gfc_src_preprocessor_lines[0] = NULL;
1424 if (gfc_src_preprocessor_lines[1])
1426 preprocessor_line (gfc_src_preprocessor_lines[1]);
1427 gfc_free (gfc_src_preprocessor_lines[1]);
1428 gfc_src_preprocessor_lines[1] = NULL;
1432 for (;;)
1434 int trunc = load_line (input, &line, &line_len);
1436 len = strlen (line);
1437 if (feof (input) && len == 0)
1438 break;
1440 /* There are three things this line can be: a line of Fortran
1441 source, an include line or a C preprocessor directive. */
1443 if (line[0] == '#')
1445 preprocessor_line (line);
1446 continue;
1449 if (include_line (line))
1451 current_file->line++;
1452 continue;
1455 /* Add line. */
1457 b = gfc_getmem (gfc_linebuf_header_size + len + 1);
1459 #ifdef USE_MAPPED_LOCATION
1460 b->location
1461 = linemap_line_start (&line_table, current_file->line++, 120);
1462 #else
1463 b->linenum = current_file->line++;
1464 #endif
1465 b->file = current_file;
1466 b->truncated = trunc;
1467 strcpy (b->line, line);
1469 if (line_head == NULL)
1470 line_head = b;
1471 else
1472 line_tail->next = b;
1474 line_tail = b;
1477 /* Release the line buffer allocated in load_line. */
1478 gfc_free (line);
1480 fclose (input);
1482 current_file = current_file->up;
1483 #ifdef USE_MAPPED_LOCATION
1484 linemap_add (&line_table, LC_LEAVE, 0, NULL, 0);
1485 #endif
1486 return SUCCESS;
1490 /* Open a new file and start scanning from that file. Returns SUCCESS
1491 if everything went OK, FAILURE otherwise. If form == FORM_UKNOWN
1492 it tries to determine the source form from the filename, defaulting
1493 to free form. */
1496 gfc_new_file (void)
1498 try result;
1500 result = load_file (gfc_source_file, true);
1502 gfc_current_locus.lb = line_head;
1503 gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line;
1505 #if 0 /* Debugging aid. */
1506 for (; line_head; line_head = line_head->next)
1507 gfc_status ("%s:%3d %s\n", line_head->file->filename,
1508 #ifdef USE_MAPPED_LOCATION
1509 LOCATION_LINE (line_head->location),
1510 #else
1511 line_head->linenum,
1512 #endif
1513 line_head->line);
1515 exit (0);
1516 #endif
1518 return result;
1521 static char *
1522 unescape_filename (const char *ptr)
1524 const char *p = ptr, *s;
1525 char *d, *ret;
1526 int escaped, unescape = 0;
1528 /* Make filename end at quote. */
1529 escaped = false;
1530 while (*p && ! (! escaped && *p == '"'))
1532 if (escaped)
1533 escaped = false;
1534 else if (*p == '\\')
1536 escaped = true;
1537 unescape++;
1539 ++p;
1542 if (! *p || p[1])
1543 return NULL;
1545 /* Undo effects of cpp_quote_string. */
1546 s = ptr;
1547 d = gfc_getmem (p + 1 - ptr - unescape);
1548 ret = d;
1550 while (s != p)
1552 if (*s == '\\')
1553 *d++ = *++s;
1554 else
1555 *d++ = *s;
1556 s++;
1558 *d = '\0';
1559 return ret;
1562 /* For preprocessed files, if the first tokens are of the form # NUM.
1563 handle the directives so we know the original file name. */
1565 const char *
1566 gfc_read_orig_filename (const char *filename, const char **canon_source_file)
1568 int c, len;
1569 char *dirname;
1571 gfc_src_file = gfc_open_file (filename);
1572 if (gfc_src_file == NULL)
1573 return NULL;
1575 c = fgetc (gfc_src_file);
1576 ungetc (c, gfc_src_file);
1578 if (c != '#')
1579 return NULL;
1581 len = 0;
1582 load_line (gfc_src_file, &gfc_src_preprocessor_lines[0], &len);
1584 if (strncmp (gfc_src_preprocessor_lines[0], "# 1 \"", 5) != 0)
1585 return NULL;
1587 filename = unescape_filename (gfc_src_preprocessor_lines[0] + 5);
1588 if (filename == NULL)
1589 return NULL;
1591 c = fgetc (gfc_src_file);
1592 ungetc (c, gfc_src_file);
1594 if (c != '#')
1595 return filename;
1597 len = 0;
1598 load_line (gfc_src_file, &gfc_src_preprocessor_lines[1], &len);
1600 if (strncmp (gfc_src_preprocessor_lines[1], "# 1 \"", 5) != 0)
1601 return filename;
1603 dirname = unescape_filename (gfc_src_preprocessor_lines[1] + 5);
1604 if (dirname == NULL)
1605 return filename;
1607 len = strlen (dirname);
1608 if (len < 3 || dirname[len - 1] != '/' || dirname[len - 2] != '/')
1610 gfc_free (dirname);
1611 return filename;
1613 dirname[len - 2] = '\0';
1614 set_src_pwd (dirname);
1616 if (! IS_ABSOLUTE_PATH (filename))
1618 char *p = gfc_getmem (len + strlen (filename));
1620 memcpy (p, dirname, len - 2);
1621 p[len - 2] = '/';
1622 strcpy (p + len - 1, filename);
1623 *canon_source_file = p;
1626 gfc_free (dirname);
1627 return filename;