2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
3 2010 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Set of subroutines to (ultimately) return the next character to the
23 various matching subroutines. This file's job is to read files and
24 build up lines that are parsed by the parser. This means that we
25 handle continuation lines and "include" lines.
27 The first thing the scanner does is to load an entire file into
28 memory. We load the entire file into memory for a couple reasons.
29 The first is that we want to be able to deal with nonseekable input
30 (pipes, stdin) and there is a lot of backing up involved during
33 The second is that we want to be able to print the locus of errors,
34 and an error on line 999999 could conflict with something on line
35 one. Given nonseekable input, we've got to store the whole thing.
37 One thing that helps are the column truncation limits that give us
38 an upper bound on the size of individual lines. We don't store the
41 From the scanner's viewpoint, the higher level subroutines ask for
42 new characters and do a lot of jumping backwards. */
47 #include "toplev.h" /* For set_src_pwd. */
52 /* Structure for holding module and include file search path. */
53 typedef struct gfc_directorylist
57 struct gfc_directorylist
*next
;
61 /* List of include file search directories. */
62 static gfc_directorylist
*include_dirs
, *intrinsic_modules_dirs
;
64 static gfc_file
*file_head
, *current_file
;
66 static int continue_flag
, end_flag
, openmp_flag
, gcc_attribute_flag
;
67 static int continue_count
, continue_line
;
68 static locus openmp_locus
;
69 static locus gcc_attribute_locus
;
71 gfc_source_form gfc_current_form
;
72 static gfc_linebuf
*line_head
, *line_tail
;
74 locus gfc_current_locus
;
75 const char *gfc_source_file
;
76 static FILE *gfc_src_file
;
77 static gfc_char_t
*gfc_src_preprocessor_lines
[2];
79 static struct gfc_file_change
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. */
93 gfc_wide_fits_in_byte (gfc_char_t c
)
95 return (c
<= UCHAR_MAX
);
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
));
111 gfc_wide_tolower (gfc_char_t c
)
113 return (wide_is_ascii (c
) ? (gfc_char_t
) TOLOWER((unsigned char) c
) : c
);
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');
129 wide_atoi (gfc_char_t
*c
)
131 #define MAX_DIGITS 20
132 char buf
[MAX_DIGITS
+1];
135 while (gfc_wide_is_digit(*c
) && i
< MAX_DIGITS
)
142 gfc_wide_strlen (const gfc_char_t
*str
)
146 for (i
= 0; str
[i
]; i
++)
153 gfc_wide_memset (gfc_char_t
*b
, gfc_char_t c
, size_t len
)
157 for (i
= 0; i
< len
; i
++)
164 wide_strcpy (gfc_char_t
*dest
, const gfc_char_t
*src
)
168 for (d
= dest
; (*d
= *src
) != '\0'; ++src
, ++d
)
175 wide_strchr (const gfc_char_t
*s
, gfc_char_t c
)
180 return CONST_CAST(gfc_char_t
*, s
);
187 gfc_widechar_to_char (const gfc_char_t
*s
, int length
)
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
= XNEWVEC (char, 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
];
211 gfc_char_to_widechar (const char *s
)
220 res
= gfc_get_wide_string (len
+ 1);
222 for (i
= 0; i
< len
; i
++)
223 res
[i
] = (unsigned char) s
[i
];
230 wide_strncmp (const gfc_char_t
*s1
, const char *s2
, size_t n
)
239 return (c1
> c2
? 1 : -1);
247 gfc_wide_strncasecmp (const gfc_char_t
*s1
, const char *s2
, size_t n
)
253 c1
= gfc_wide_tolower (*s1
++);
254 c2
= TOLOWER (*s2
++);
256 return (c1
> c2
? 1 : -1);
264 /* Main scanner initialization. */
267 gfc_scanner_init_1 (void)
280 /* Main scanner destructor. */
283 gfc_scanner_done_1 (void)
288 while(line_head
!= NULL
)
290 lb
= line_head
->next
;
295 while(file_head
!= NULL
)
298 gfc_free(file_head
->filename
);
305 /* Adds path to the list pointed to by list. */
308 add_path_to_list (gfc_directorylist
**list
, const char *path
,
309 bool use_for_modules
, bool head
)
311 gfc_directorylist
*dir
;
315 while (*p
== ' ' || *p
== '\t') /* someone might do "-I include" */
319 if (head
|| *list
== NULL
)
321 dir
= XCNEW (gfc_directorylist
);
331 dir
->next
= XCNEW (gfc_directorylist
);
335 dir
->next
= head
? *list
: NULL
;
338 dir
->use_for_modules
= use_for_modules
;
339 dir
->path
= XCNEWVEC (char, strlen (p
) + 2);
340 strcpy (dir
->path
, p
);
341 strcat (dir
->path
, "/"); /* make '/' last character */
346 gfc_add_include_path (const char *path
, bool use_for_modules
, bool file_dir
)
348 add_path_to_list (&include_dirs
, path
, use_for_modules
, file_dir
);
350 /* For '#include "..."' these directories are automatically searched. */
352 gfc_cpp_add_include_path (xstrdup(path
), true);
357 gfc_add_intrinsic_modules_path (const char *path
)
359 add_path_to_list (&intrinsic_modules_dirs
, path
, true, false);
363 /* Release resources allocated for options. */
366 gfc_release_include_path (void)
368 gfc_directorylist
*p
;
370 while (include_dirs
!= NULL
)
373 include_dirs
= include_dirs
->next
;
378 while (intrinsic_modules_dirs
!= NULL
)
380 p
= intrinsic_modules_dirs
;
381 intrinsic_modules_dirs
= intrinsic_modules_dirs
->next
;
386 gfc_free (gfc_option
.module_dir
);
391 open_included_file (const char *name
, gfc_directorylist
*list
,
392 bool module
, bool system
)
395 gfc_directorylist
*p
;
398 for (p
= list
; p
; p
= p
->next
)
400 if (module
&& !p
->use_for_modules
)
403 fullname
= (char *) alloca(strlen (p
->path
) + strlen (name
) + 1);
404 strcpy (fullname
, p
->path
);
405 strcat (fullname
, name
);
407 f
= gfc_open_file (fullname
);
410 if (gfc_cpp_makedep ())
411 gfc_cpp_add_dep (fullname
, system
);
421 /* Opens file for reading, searching through the include directories
422 given if necessary. If the include_cwd argument is true, we try
423 to open the file in the current directory first. */
426 gfc_open_included_file (const char *name
, bool include_cwd
, bool module
)
430 if (IS_ABSOLUTE_PATH (name
) || include_cwd
)
432 f
= gfc_open_file (name
);
433 if (f
&& gfc_cpp_makedep ())
434 gfc_cpp_add_dep (name
, false);
438 f
= open_included_file (name
, include_dirs
, module
, false);
444 gfc_open_intrinsic_module (const char *name
)
448 if (IS_ABSOLUTE_PATH (name
))
450 f
= gfc_open_file (name
);
451 if (f
&& gfc_cpp_makedep ())
452 gfc_cpp_add_dep (name
, true);
456 f
= open_included_file (name
, intrinsic_modules_dirs
, true, true);
462 /* Test to see if we're at the end of the main source file. */
471 /* Test to see if we're at the end of the current file. */
479 if (line_head
== NULL
)
480 return 1; /* Null file */
482 if (gfc_current_locus
.lb
== NULL
)
489 /* Test to see if we're at the beginning of a new line. */
497 return (gfc_current_locus
.nextc
== gfc_current_locus
.lb
->line
);
501 /* Test to see if we're at the end of a line. */
509 return (*gfc_current_locus
.nextc
== '\0');
513 add_file_change (const char *filename
, int line
)
515 if (file_changes_count
== file_changes_allocated
)
517 if (file_changes_allocated
)
518 file_changes_allocated
*= 2;
520 file_changes_allocated
= 16;
521 file_changes
= XRESIZEVEC (struct gfc_file_change
, file_changes
,
522 file_changes_allocated
);
524 file_changes
[file_changes_count
].filename
= filename
;
525 file_changes
[file_changes_count
].lb
= NULL
;
526 file_changes
[file_changes_count
++].line
= line
;
530 report_file_change (gfc_linebuf
*lb
)
532 size_t c
= file_changes_cur
;
533 while (c
< file_changes_count
534 && file_changes
[c
].lb
== lb
)
536 if (file_changes
[c
].filename
)
537 (*debug_hooks
->start_source_file
) (file_changes
[c
].line
,
538 file_changes
[c
].filename
);
540 (*debug_hooks
->end_source_file
) (file_changes
[c
].line
);
543 file_changes_cur
= c
;
547 gfc_start_source_files (void)
549 /* If the debugger wants the name of the main source file,
551 if (debug_hooks
->start_end_main_source_file
)
552 (*debug_hooks
->start_source_file
) (0, gfc_source_file
);
554 file_changes_cur
= 0;
555 report_file_change (gfc_current_locus
.lb
);
559 gfc_end_source_files (void)
561 report_file_change (NULL
);
563 if (debug_hooks
->start_end_main_source_file
)
564 (*debug_hooks
->end_source_file
) (0);
567 /* Advance the current line pointer to the next line. */
570 gfc_advance_line (void)
575 if (gfc_current_locus
.lb
== NULL
)
581 if (gfc_current_locus
.lb
->next
582 && !gfc_current_locus
.lb
->next
->dbg_emitted
)
584 report_file_change (gfc_current_locus
.lb
->next
);
585 gfc_current_locus
.lb
->next
->dbg_emitted
= true;
588 gfc_current_locus
.lb
= gfc_current_locus
.lb
->next
;
590 if (gfc_current_locus
.lb
!= NULL
)
591 gfc_current_locus
.nextc
= gfc_current_locus
.lb
->line
;
594 gfc_current_locus
.nextc
= NULL
;
600 /* Get the next character from the input, advancing gfc_current_file's
601 locus. When we hit the end of the line or the end of the file, we
602 start returning a '\n' in order to complete the current statement.
603 No Fortran line conventions are implemented here.
605 Requiring explicit advances to the next line prevents the parse
606 pointer from being on the wrong line if the current statement ends
614 if (gfc_current_locus
.nextc
== NULL
)
617 c
= *gfc_current_locus
.nextc
++;
620 gfc_current_locus
.nextc
--; /* Remain on this line. */
628 /* Skip a comment. When we come here the parse pointer is positioned
629 immediately after the comment character. If we ever implement
630 compiler directives within comments, here is where we parse the
634 skip_comment_line (void)
649 gfc_define_undef_line (void)
653 /* All lines beginning with '#' are either #define or #undef. */
654 if (debug_info_level
!= DINFO_LEVEL_VERBOSE
|| gfc_peek_ascii_char () != '#')
657 if (wide_strncmp (gfc_current_locus
.nextc
, "#define ", 8) == 0)
659 tmp
= gfc_widechar_to_char (&gfc_current_locus
.nextc
[8], -1);
660 (*debug_hooks
->define
) (gfc_linebuf_linenum (gfc_current_locus
.lb
),
665 if (wide_strncmp (gfc_current_locus
.nextc
, "#undef ", 7) == 0)
667 tmp
= gfc_widechar_to_char (&gfc_current_locus
.nextc
[7], -1);
668 (*debug_hooks
->undef
) (gfc_linebuf_linenum (gfc_current_locus
.lb
),
673 /* Skip the rest of the line. */
674 skip_comment_line ();
680 /* Return true if GCC$ was matched. */
682 skip_gcc_attribute (locus start
)
686 locus old_loc
= gfc_current_locus
;
688 if ((c
= next_char ()) == 'g' || c
== 'G')
689 if ((c
= next_char ()) == 'c' || c
== 'C')
690 if ((c
= next_char ()) == 'c' || c
== 'C')
691 if ((c
= next_char ()) == '$')
695 gfc_current_locus
= old_loc
;
698 gcc_attribute_flag
= 1;
699 gcc_attribute_locus
= old_loc
;
700 gfc_current_locus
= start
;
708 /* Comment lines are null lines, lines containing only blanks or lines
709 on which the first nonblank line is a '!'.
710 Return true if !$ openmp conditional compilation sentinel was
714 skip_free_comments (void)
722 at_bol
= gfc_at_bol ();
723 start
= gfc_current_locus
;
729 while (gfc_is_whitespace (c
));
739 /* Keep the !GCC$ line. */
740 if (at_bol
&& skip_gcc_attribute (start
))
743 /* If -fopenmp, we need to handle here 2 things:
744 1) don't treat !$omp as comments, but directives
745 2) handle OpenMP conditional compilation, where
746 !$ should be treated as 2 spaces (for initial lines
747 only if followed by space). */
748 if (gfc_option
.gfc_flag_openmp
&& at_bol
)
750 locus old_loc
= gfc_current_locus
;
751 if (next_char () == '$')
754 if (c
== 'o' || c
== 'O')
756 if (((c
= next_char ()) == 'm' || c
== 'M')
757 && ((c
= next_char ()) == 'p' || c
== 'P'))
759 if ((c
= next_char ()) == ' ' || c
== '\t'
762 while (gfc_is_whitespace (c
))
764 if (c
!= '\n' && c
!= '!')
767 openmp_locus
= old_loc
;
768 gfc_current_locus
= start
;
773 gfc_warning_now ("!$OMP at %C starts a commented "
774 "line as it neither is followed "
775 "by a space nor is a "
776 "continuation line");
778 gfc_current_locus
= old_loc
;
782 if (continue_flag
|| c
== ' ' || c
== '\t')
784 gfc_current_locus
= old_loc
;
790 gfc_current_locus
= old_loc
;
792 skip_comment_line ();
799 if (openmp_flag
&& at_bol
)
802 gcc_attribute_flag
= 0;
803 gfc_current_locus
= start
;
808 /* Skip comment lines in fixed source mode. We have the same rules as
809 in skip_free_comment(), except that we can have a 'c', 'C' or '*'
810 in column 1, and a '!' cannot be in column 6. Also, we deal with
811 lines with 'd' or 'D' in column 1, if the user requested this. */
814 skip_fixed_comments (void)
822 start
= gfc_current_locus
;
827 while (gfc_is_whitespace (c
));
832 skip_comment_line ();
837 gfc_current_locus
= start
;
844 start
= gfc_current_locus
;
855 if (c
== '!' || c
== 'c' || c
== 'C' || c
== '*')
857 if (skip_gcc_attribute (start
))
859 /* Canonicalize to *$omp. */
864 /* If -fopenmp, we need to handle here 2 things:
865 1) don't treat !$omp|c$omp|*$omp as comments, but directives
866 2) handle OpenMP conditional compilation, where
867 !$|c$|*$ should be treated as 2 spaces if the characters
868 in columns 3 to 6 are valid fixed form label columns
870 if (gfc_current_locus
.lb
!= NULL
871 && continue_line
< gfc_linebuf_linenum (gfc_current_locus
.lb
))
872 continue_line
= gfc_linebuf_linenum (gfc_current_locus
.lb
);
874 if (gfc_option
.gfc_flag_openmp
)
876 if (next_char () == '$')
879 if (c
== 'o' || c
== 'O')
881 if (((c
= next_char ()) == 'm' || c
== 'M')
882 && ((c
= next_char ()) == 'p' || c
== 'P'))
886 && ((openmp_flag
&& continue_flag
)
887 || c
== ' ' || c
== '\t' || c
== '0'))
891 while (gfc_is_whitespace (c
));
892 if (c
!= '\n' && c
!= '!')
894 /* Canonicalize to *$omp. */
897 gfc_current_locus
= start
;
907 for (col
= 3; col
< 6; col
++, c
= next_char ())
915 else if (c
< '0' || c
> '9')
920 if (col
== 6 && c
!= '\n'
921 && ((continue_flag
&& !digit_seen
)
922 || c
== ' ' || c
== '\t' || c
== '0'))
924 gfc_current_locus
= start
;
925 start
.nextc
[0] = ' ';
926 start
.nextc
[1] = ' ';
931 gfc_current_locus
= start
;
933 skip_comment_line ();
937 if (gfc_option
.flag_d_lines
!= -1 && (c
== 'd' || c
== 'D'))
939 if (gfc_option
.flag_d_lines
== 0)
941 skip_comment_line ();
945 *start
.nextc
= c
= ' ';
950 while (gfc_is_whitespace (c
))
962 if (col
!= 6 && c
== '!')
964 if (gfc_current_locus
.lb
!= NULL
965 && continue_line
< gfc_linebuf_linenum (gfc_current_locus
.lb
))
966 continue_line
= gfc_linebuf_linenum (gfc_current_locus
.lb
);
967 skip_comment_line ();
975 gcc_attribute_flag
= 0;
976 gfc_current_locus
= start
;
980 /* Skips the current line if it is a comment. */
983 gfc_skip_comments (void)
985 if (gfc_current_form
== FORM_FREE
)
986 skip_free_comments ();
988 skip_fixed_comments ();
992 /* Get the next character from the input, taking continuation lines
993 and end-of-line comments into account. This implies that comment
994 lines between continued lines must be eaten here. For higher-level
995 subroutines, this flattens continued lines into a single logical
996 line. The in_string flag denotes whether we're inside a character
1000 gfc_next_char_literal (gfc_instring in_string
)
1003 int i
, prev_openmp_flag
;
1016 if (gfc_current_form
== FORM_FREE
)
1018 bool openmp_cond_flag
;
1020 if (!in_string
&& c
== '!')
1022 if (gcc_attribute_flag
1023 && memcmp (&gfc_current_locus
, &gcc_attribute_locus
,
1024 sizeof (gfc_current_locus
)) == 0)
1028 && memcmp (&gfc_current_locus
, &openmp_locus
,
1029 sizeof (gfc_current_locus
)) == 0)
1032 /* This line can't be continued */
1039 /* Avoid truncation warnings for comment ending lines. */
1040 gfc_current_locus
.lb
->truncated
= 0;
1045 /* Check to see if the continuation line was truncated. */
1046 if (gfc_option
.warn_line_truncation
&& gfc_current_locus
.lb
!= NULL
1047 && gfc_current_locus
.lb
->truncated
)
1049 int maxlen
= gfc_option
.free_line_length
;
1050 gfc_current_locus
.lb
->truncated
= 0;
1051 gfc_current_locus
.nextc
+= maxlen
;
1052 gfc_warning_now ("Line truncated at %L", &gfc_current_locus
);
1053 gfc_current_locus
.nextc
-= maxlen
;
1059 /* If the next nonblank character is a ! or \n, we've got a
1060 continuation line. */
1061 old_loc
= gfc_current_locus
;
1064 while (gfc_is_whitespace (c
))
1067 /* Character constants to be continued cannot have commentary
1070 if (in_string
&& c
!= '\n')
1072 gfc_current_locus
= old_loc
;
1077 if (c
!= '!' && c
!= '\n')
1079 gfc_current_locus
= old_loc
;
1084 prev_openmp_flag
= openmp_flag
;
1087 skip_comment_line ();
1089 gfc_advance_line ();
1092 goto not_continuation
;
1094 /* We've got a continuation line. If we are on the very next line after
1095 the last continuation, increment the continuation line count and
1096 check whether the limit has been exceeded. */
1097 if (gfc_linebuf_linenum (gfc_current_locus
.lb
) == continue_line
+ 1)
1099 if (++continue_count
== gfc_option
.max_continue_free
)
1101 if (gfc_notification_std (GFC_STD_GNU
) || pedantic
)
1102 gfc_warning ("Limit of %d continuations exceeded in "
1103 "statement at %C", gfc_option
.max_continue_free
);
1107 /* Now find where it continues. First eat any comment lines. */
1108 openmp_cond_flag
= skip_free_comments ();
1110 if (gfc_current_locus
.lb
!= NULL
1111 && continue_line
< gfc_linebuf_linenum (gfc_current_locus
.lb
))
1112 continue_line
= gfc_linebuf_linenum (gfc_current_locus
.lb
);
1114 if (prev_openmp_flag
!= openmp_flag
)
1116 gfc_current_locus
= old_loc
;
1117 openmp_flag
= prev_openmp_flag
;
1122 /* Now that we have a non-comment line, probe ahead for the
1123 first non-whitespace character. If it is another '&', then
1124 reading starts at the next character, otherwise we must back
1125 up to where the whitespace started and resume from there. */
1127 old_loc
= gfc_current_locus
;
1130 while (gfc_is_whitespace (c
))
1135 for (i
= 0; i
< 5; i
++, c
= next_char ())
1137 gcc_assert (gfc_wide_tolower (c
) == (unsigned char) "!$omp"[i
]);
1139 old_loc
= gfc_current_locus
;
1141 while (gfc_is_whitespace (c
))
1149 gfc_current_locus
.nextc
--;
1150 if (gfc_option
.warn_ampersand
&& in_string
== INSTRING_WARN
)
1151 gfc_warning ("Missing '&' in continued character "
1154 /* Both !$omp and !$ -fopenmp continuation lines have & on the
1155 continuation line only optionally. */
1156 else if (openmp_flag
|| openmp_cond_flag
)
1157 gfc_current_locus
.nextc
--;
1161 gfc_current_locus
= old_loc
;
1166 else /* Fixed form. */
1168 /* Fixed form continuation. */
1169 if (!in_string
&& c
== '!')
1171 /* Skip comment at end of line. */
1178 /* Avoid truncation warnings for comment ending lines. */
1179 gfc_current_locus
.lb
->truncated
= 0;
1185 /* Check to see if the continuation line was truncated. */
1186 if (gfc_option
.warn_line_truncation
&& gfc_current_locus
.lb
!= NULL
1187 && gfc_current_locus
.lb
->truncated
)
1189 gfc_current_locus
.lb
->truncated
= 0;
1190 gfc_warning_now ("Line truncated at %L", &gfc_current_locus
);
1193 prev_openmp_flag
= openmp_flag
;
1195 old_loc
= gfc_current_locus
;
1197 gfc_advance_line ();
1198 skip_fixed_comments ();
1200 /* See if this line is a continuation line. */
1201 if (openmp_flag
!= prev_openmp_flag
)
1203 openmp_flag
= prev_openmp_flag
;
1204 goto not_continuation
;
1208 for (i
= 0; i
< 5; i
++)
1212 goto not_continuation
;
1215 for (i
= 0; i
< 5; i
++)
1218 if (gfc_wide_tolower (c
) != (unsigned char) "*$omp"[i
])
1219 goto not_continuation
;
1223 if (c
== '0' || c
== ' ' || c
== '\n')
1224 goto not_continuation
;
1226 /* We've got a continuation line. If we are on the very next line after
1227 the last continuation, increment the continuation line count and
1228 check whether the limit has been exceeded. */
1229 if (gfc_linebuf_linenum (gfc_current_locus
.lb
) == continue_line
+ 1)
1231 if (++continue_count
== gfc_option
.max_continue_fixed
)
1233 if (gfc_notification_std (GFC_STD_GNU
) || pedantic
)
1234 gfc_warning ("Limit of %d continuations exceeded in "
1236 gfc_option
.max_continue_fixed
);
1240 if (gfc_current_locus
.lb
!= NULL
1241 && continue_line
< gfc_linebuf_linenum (gfc_current_locus
.lb
))
1242 continue_line
= gfc_linebuf_linenum (gfc_current_locus
.lb
);
1245 /* Ready to read first character of continuation line, which might
1246 be another continuation line! */
1251 gfc_current_locus
= old_loc
;
1261 /* Get the next character of input, folded to lowercase. In fixed
1262 form mode, we also ignore spaces. When matcher subroutines are
1263 parsing character literals, they have to call
1264 gfc_next_char_literal(). */
1267 gfc_next_char (void)
1273 c
= gfc_next_char_literal (NONSTRING
);
1275 while (gfc_current_form
== FORM_FIXED
&& gfc_is_whitespace (c
));
1277 return gfc_wide_tolower (c
);
1281 gfc_next_ascii_char (void)
1283 gfc_char_t c
= gfc_next_char ();
1285 return (gfc_wide_fits_in_byte (c
) ? (unsigned char) c
1286 : (unsigned char) UCHAR_MAX
);
1291 gfc_peek_char (void)
1296 old_loc
= gfc_current_locus
;
1297 c
= gfc_next_char ();
1298 gfc_current_locus
= old_loc
;
1305 gfc_peek_ascii_char (void)
1307 gfc_char_t c
= gfc_peek_char ();
1309 return (gfc_wide_fits_in_byte (c
) ? (unsigned char) c
1310 : (unsigned char) UCHAR_MAX
);
1314 /* Recover from an error. We try to get past the current statement
1315 and get lined up for the next. The next statement follows a '\n'
1316 or a ';'. We also assume that we are not within a character
1317 constant, and deal with finding a '\'' or '"'. */
1320 gfc_error_recovery (void)
1322 gfc_char_t c
, delim
;
1329 c
= gfc_next_char ();
1330 if (c
== '\n' || c
== ';')
1333 if (c
!= '\'' && c
!= '"')
1362 /* Read ahead until the next character to be read is not whitespace. */
1365 gfc_gobble_whitespace (void)
1367 static int linenum
= 0;
1373 old_loc
= gfc_current_locus
;
1374 c
= gfc_next_char_literal (NONSTRING
);
1375 /* Issue a warning for nonconforming tabs. We keep track of the line
1376 number because the Fortran matchers will often back up and the same
1377 line will be scanned multiple times. */
1378 if (!gfc_option
.warn_tabs
&& c
== '\t')
1380 int cur_linenum
= LOCATION_LINE (gfc_current_locus
.lb
->location
);
1381 if (cur_linenum
!= linenum
)
1383 linenum
= cur_linenum
;
1384 gfc_warning_now ("Nonconforming tab character at %C");
1388 while (gfc_is_whitespace (c
));
1390 gfc_current_locus
= old_loc
;
1394 /* Load a single line into pbuf.
1396 If pbuf points to a NULL pointer, it is allocated.
1397 We truncate lines that are too long, unless we're dealing with
1398 preprocessor lines or if the option -ffixed-line-length-none is set,
1399 in which case we reallocate the buffer to fit the entire line, if
1401 In fixed mode, we expand a tab that occurs within the statement
1402 label region to expand to spaces that leave the next character in
1405 If first_char is not NULL, it's a pointer to a single char value holding
1406 the first character of the line, which has already been read by the
1407 caller. This avoids the use of ungetc().
1409 load_line returns whether the line was truncated.
1411 NOTE: The error machinery isn't available at this point, so we can't
1412 easily report line and column numbers consistent with other
1413 parts of gfortran. */
1416 load_line (FILE *input
, gfc_char_t
**pbuf
, int *pbuflen
, const int *first_char
)
1418 static int linenum
= 0, current_line
= 1;
1419 int c
, maxlen
, i
, preprocessor_flag
, buflen
= *pbuflen
;
1420 int trunc_flag
= 0, seen_comment
= 0;
1421 int seen_printable
= 0, seen_ampersand
= 0, quoted
= ' ';
1423 bool found_tab
= false;
1425 /* Determine the maximum allowed line length. */
1426 if (gfc_current_form
== FORM_FREE
)
1427 maxlen
= gfc_option
.free_line_length
;
1428 else if (gfc_current_form
== FORM_FIXED
)
1429 maxlen
= gfc_option
.fixed_line_length
;
1435 /* Allocate the line buffer, storing its length into buflen.
1436 Note that if maxlen==0, indicating that arbitrary-length lines
1437 are allowed, the buffer will be reallocated if this length is
1438 insufficient; since 132 characters is the length of a standard
1439 free-form line, we use that as a starting guess. */
1445 *pbuf
= gfc_get_wide_string (buflen
+ 1);
1456 /* In order to not truncate preprocessor lines, we have to
1457 remember that this is one. */
1458 preprocessor_flag
= (c
== '#' ? 1 : 0);
1467 /* Check for illegal use of ampersand. See F95 Standard 3.3.1.3. */
1468 if (gfc_current_form
== FORM_FREE
1469 && !seen_printable
&& seen_ampersand
)
1472 gfc_error_now ("'&' not allowed by itself in line %d",
1475 gfc_warning_now ("'&' not allowed by itself in line %d",
1481 if (c
== '\r' || c
== '\0')
1482 goto next_char
; /* Gobble characters. */
1495 if ((c
!= '&' && c
!= '!' && c
!= ' ') || (c
== '!' && !seen_ampersand
))
1498 /* Is this a fixed-form comment? */
1499 if (gfc_current_form
== FORM_FIXED
&& i
== 0
1500 && (c
== '*' || c
== 'c' || c
== 'd'))
1505 if (c
== '\'' || c
== '"')
1508 else if (c
== quoted
)
1511 /* Is this a free-form comment? */
1512 if (c
== '!' && quoted
== ' ')
1515 /* Vendor extension: "<tab>1" marks a continuation line. */
1519 if (c
>= '1' && c
<= '9')
1526 if (gfc_current_form
== FORM_FIXED
&& c
== '\t' && i
< 6)
1530 if (!gfc_option
.warn_tabs
&& seen_comment
== 0
1531 && current_line
!= linenum
)
1533 linenum
= current_line
;
1534 gfc_warning_now ("Nonconforming tab character in column %d "
1535 "of line %d", i
+1, linenum
);
1550 if (maxlen
== 0 || preprocessor_flag
)
1554 /* Reallocate line buffer to double size to hold the
1556 buflen
= buflen
* 2;
1557 *pbuf
= XRESIZEVEC (gfc_char_t
, *pbuf
, (buflen
+ 1));
1558 buffer
= (*pbuf
) + i
;
1561 else if (i
>= maxlen
)
1563 bool trunc_warn
= true;
1565 /* Enhancement, if the very next non-space character is an ampersand
1566 or comment that we would otherwise warn about, don't mark as
1569 /* Truncate the rest of the line. */
1573 if (c
== '\r' || c
== ' ')
1576 if (c
== '\n' || c
== EOF
)
1579 if (!trunc_warn
&& c
!= '!')
1582 if (trunc_warn
&& ((gfc_current_form
== FORM_FIXED
&& c
== '&')
1589 if (trunc_warn
&& !seen_comment
)
1601 /* Pad lines to the selected line length in fixed form. */
1602 if (gfc_current_form
== FORM_FIXED
1603 && gfc_option
.fixed_line_length
!= 0
1604 && !preprocessor_flag
1607 while (i
++ < maxlen
)
1619 /* Get a gfc_file structure, initialize it and add it to
1623 get_file (const char *name
, enum lc_reason reason ATTRIBUTE_UNUSED
)
1627 f
= XCNEW (gfc_file
);
1629 f
->filename
= xstrdup (name
);
1631 f
->next
= file_head
;
1634 f
->up
= current_file
;
1635 if (current_file
!= NULL
)
1636 f
->inclusion_line
= current_file
->line
;
1638 linemap_add (line_table
, reason
, false, f
->filename
, 1);
1644 /* Deal with a line from the C preprocessor. The
1645 initial octothorp has already been seen. */
1648 preprocessor_line (gfc_char_t
*c
)
1652 gfc_char_t
*wide_filename
;
1654 int escaped
, unescape
;
1658 while (*c
== ' ' || *c
== '\t')
1661 if (*c
< '0' || *c
> '9')
1664 line
= wide_atoi (c
);
1666 c
= wide_strchr (c
, ' ');
1669 /* No file name given. Set new line number. */
1670 current_file
->line
= line
;
1675 while (*c
== ' ' || *c
== '\t')
1685 /* Make filename end at quote. */
1688 while (*c
&& ! (!escaped
&& *c
== '"'))
1692 else if (*c
== '\\')
1701 /* Preprocessor line has no closing quote. */
1706 /* Undo effects of cpp_quote_string. */
1709 gfc_char_t
*s
= wide_filename
;
1710 gfc_char_t
*d
= gfc_get_wide_string (c
- wide_filename
- unescape
);
1726 flag
[1] = flag
[2] = flag
[3] = flag
[4] = false;
1730 c
= wide_strchr (c
, ' ');
1737 if (1 <= i
&& i
<= 4)
1741 /* Convert the filename in wide characters into a filename in narrow
1743 filename
= gfc_widechar_to_char (wide_filename
, -1);
1745 /* Interpret flags. */
1747 if (flag
[1]) /* Starting new file. */
1749 f
= get_file (filename
, LC_RENAME
);
1750 add_file_change (f
->filename
, f
->inclusion_line
);
1754 if (flag
[2]) /* Ending current file. */
1756 if (!current_file
->up
1757 || strcmp (current_file
->up
->filename
, filename
) != 0)
1759 gfc_warning_now ("%s:%d: file %s left but not entered",
1760 current_file
->filename
, current_file
->line
,
1763 gfc_free (wide_filename
);
1764 gfc_free (filename
);
1768 add_file_change (NULL
, line
);
1769 current_file
= current_file
->up
;
1770 linemap_add (line_table
, LC_RENAME
, false, current_file
->filename
,
1771 current_file
->line
);
1774 /* The name of the file can be a temporary file produced by
1775 cpp. Replace the name if it is different. */
1777 if (strcmp (current_file
->filename
, filename
) != 0)
1779 /* FIXME: we leak the old filename because a pointer to it may be stored
1780 in the linemap. Alternative could be using GC or updating linemap to
1781 point to the new name, but there is no API for that currently. */
1782 current_file
->filename
= xstrdup (filename
);
1785 /* Set new line number. */
1786 current_file
->line
= line
;
1788 gfc_free (wide_filename
);
1789 gfc_free (filename
);
1793 gfc_warning_now ("%s:%d: Illegal preprocessor directive",
1794 current_file
->filename
, current_file
->line
);
1795 current_file
->line
++;
1799 static gfc_try
load_file (const char *, const char *, bool);
1801 /* include_line()-- Checks a line buffer to see if it is an include
1802 line. If so, we call load_file() recursively to load the included
1803 file. We never return a syntax error because a statement like
1804 "include = 5" is perfectly legal. We return false if no include was
1805 processed or true if we matched an include. */
1808 include_line (gfc_char_t
*line
)
1810 gfc_char_t quote
, *c
, *begin
, *stop
;
1815 if (gfc_option
.gfc_flag_openmp
)
1817 if (gfc_current_form
== FORM_FREE
)
1819 while (*c
== ' ' || *c
== '\t')
1821 if (*c
== '!' && c
[1] == '$' && (c
[2] == ' ' || c
[2] == '\t'))
1826 if ((*c
== '!' || *c
== 'c' || *c
== 'C' || *c
== '*')
1827 && c
[1] == '$' && (c
[2] == ' ' || c
[2] == '\t'))
1832 while (*c
== ' ' || *c
== '\t')
1835 if (gfc_wide_strncasecmp (c
, "include", 7))
1839 while (*c
== ' ' || *c
== '\t')
1842 /* Find filename between quotes. */
1845 if (quote
!= '"' && quote
!= '\'')
1850 while (*c
!= quote
&& *c
!= '\0')
1858 while (*c
== ' ' || *c
== '\t')
1861 if (*c
!= '\0' && *c
!= '!')
1864 /* We have an include line at this point. */
1866 *stop
= '\0'; /* It's ok to trash the buffer, as this line won't be
1867 read by anything else. */
1869 filename
= gfc_widechar_to_char (begin
, -1);
1870 if (load_file (filename
, NULL
, false) == FAILURE
)
1871 exit (FATAL_EXIT_CODE
);
1873 gfc_free (filename
);
1878 /* Load a file into memory by calling load_line until the file ends. */
1881 load_file (const char *realfilename
, const char *displayedname
, bool initial
)
1889 const char *filename
;
1891 filename
= displayedname
? displayedname
: realfilename
;
1893 for (f
= current_file
; f
; f
= f
->up
)
1894 if (strcmp (filename
, f
->filename
) == 0)
1896 fprintf (stderr
, "%s:%d: Error: File '%s' is being included "
1897 "recursively\n", current_file
->filename
, current_file
->line
,
1906 input
= gfc_src_file
;
1907 gfc_src_file
= NULL
;
1910 input
= gfc_open_file (realfilename
);
1913 gfc_error_now ("Can't open file '%s'", filename
);
1919 input
= gfc_open_included_file (realfilename
, false, false);
1922 fprintf (stderr
, "%s:%d: Error: Can't open included file '%s'\n",
1923 current_file
->filename
, current_file
->line
, filename
);
1928 /* Load the file. */
1930 f
= get_file (filename
, initial
? LC_RENAME
: LC_ENTER
);
1932 add_file_change (f
->filename
, f
->inclusion_line
);
1934 current_file
->line
= 1;
1939 if (initial
&& gfc_src_preprocessor_lines
[0])
1941 preprocessor_line (gfc_src_preprocessor_lines
[0]);
1942 gfc_free (gfc_src_preprocessor_lines
[0]);
1943 gfc_src_preprocessor_lines
[0] = NULL
;
1944 if (gfc_src_preprocessor_lines
[1])
1946 preprocessor_line (gfc_src_preprocessor_lines
[1]);
1947 gfc_free (gfc_src_preprocessor_lines
[1]);
1948 gfc_src_preprocessor_lines
[1] = NULL
;
1954 int trunc
= load_line (input
, &line
, &line_len
, NULL
);
1956 len
= gfc_wide_strlen (line
);
1957 if (feof (input
) && len
== 0)
1960 /* If this is the first line of the file, it can contain a byte
1961 order mark (BOM), which we will ignore:
1962 FF FE is UTF-16 little endian,
1963 FE FF is UTF-16 big endian,
1964 EF BB BF is UTF-8. */
1966 && ((line_len
>= 2 && line
[0] == (unsigned char) '\xFF'
1967 && line
[1] == (unsigned char) '\xFE')
1968 || (line_len
>= 2 && line
[0] == (unsigned char) '\xFE'
1969 && line
[1] == (unsigned char) '\xFF')
1970 || (line_len
>= 3 && line
[0] == (unsigned char) '\xEF'
1971 && line
[1] == (unsigned char) '\xBB'
1972 && line
[2] == (unsigned char) '\xBF')))
1974 int n
= line
[1] == (unsigned char) '\xBB' ? 3 : 2;
1975 gfc_char_t
*new_char
= gfc_get_wide_string (line_len
);
1977 wide_strcpy (new_char
, &line
[n
]);
1983 /* There are three things this line can be: a line of Fortran
1984 source, an include line or a C preprocessor directive. */
1988 /* When -g3 is specified, it's possible that we emit #define
1989 and #undef lines, which we need to pass to the middle-end
1990 so that it can emit correct debug info. */
1991 if (debug_info_level
== DINFO_LEVEL_VERBOSE
1992 && (wide_strncmp (line
, "#define ", 8) == 0
1993 || wide_strncmp (line
, "#undef ", 7) == 0))
1997 preprocessor_line (line
);
2002 /* Preprocessed files have preprocessor lines added before the byte
2003 order mark, so first_line is not about the first line of the file
2004 but the first line that's not a preprocessor line. */
2007 if (include_line (line
))
2009 current_file
->line
++;
2015 b
= (gfc_linebuf
*) gfc_getmem (gfc_linebuf_header_size
2016 + (len
+ 1) * sizeof (gfc_char_t
));
2019 = linemap_line_start (line_table
, current_file
->line
++, 120);
2020 b
->file
= current_file
;
2021 b
->truncated
= trunc
;
2022 wide_strcpy (b
->line
, line
);
2024 if (line_head
== NULL
)
2027 line_tail
->next
= b
;
2031 while (file_changes_cur
< file_changes_count
)
2032 file_changes
[file_changes_cur
++].lb
= b
;
2035 /* Release the line buffer allocated in load_line. */
2041 add_file_change (NULL
, current_file
->inclusion_line
+ 1);
2042 current_file
= current_file
->up
;
2043 linemap_add (line_table
, LC_LEAVE
, 0, NULL
, 0);
2048 /* Open a new file and start scanning from that file. Returns SUCCESS
2049 if everything went OK, FAILURE otherwise. If form == FORM_UNKNOWN
2050 it tries to determine the source form from the filename, defaulting
2058 if (gfc_cpp_enabled ())
2060 result
= gfc_cpp_preprocess (gfc_source_file
);
2061 if (!gfc_cpp_preprocess_only ())
2062 result
= load_file (gfc_cpp_temporary_file (), gfc_source_file
, true);
2065 result
= load_file (gfc_source_file
, NULL
, true);
2067 gfc_current_locus
.lb
= line_head
;
2068 gfc_current_locus
.nextc
= (line_head
== NULL
) ? NULL
: line_head
->line
;
2070 #if 0 /* Debugging aid. */
2071 for (; line_head
; line_head
= line_head
->next
)
2072 printf ("%s:%3d %s\n", LOCATION_FILE (line_head
->location
),
2073 LOCATION_LINE (line_head
->location
), line_head
->line
);
2075 exit (SUCCESS_EXIT_CODE
);
2082 unescape_filename (const char *ptr
)
2084 const char *p
= ptr
, *s
;
2086 int escaped
, unescape
= 0;
2088 /* Make filename end at quote. */
2090 while (*p
&& ! (! escaped
&& *p
== '"'))
2094 else if (*p
== '\\')
2105 /* Undo effects of cpp_quote_string. */
2107 d
= XCNEWVEC (char, p
+ 1 - ptr
- unescape
);
2122 /* For preprocessed files, if the first tokens are of the form # NUM.
2123 handle the directives so we know the original file name. */
2126 gfc_read_orig_filename (const char *filename
, const char **canon_source_file
)
2129 char *dirname
, *tmp
;
2131 gfc_src_file
= gfc_open_file (filename
);
2132 if (gfc_src_file
== NULL
)
2135 c
= getc (gfc_src_file
);
2141 load_line (gfc_src_file
, &gfc_src_preprocessor_lines
[0], &len
, &c
);
2143 if (wide_strncmp (gfc_src_preprocessor_lines
[0], "# 1 \"", 5) != 0)
2146 tmp
= gfc_widechar_to_char (&gfc_src_preprocessor_lines
[0][5], -1);
2147 filename
= unescape_filename (tmp
);
2149 if (filename
== NULL
)
2152 c
= getc (gfc_src_file
);
2158 load_line (gfc_src_file
, &gfc_src_preprocessor_lines
[1], &len
, &c
);
2160 if (wide_strncmp (gfc_src_preprocessor_lines
[1], "# 1 \"", 5) != 0)
2163 tmp
= gfc_widechar_to_char (&gfc_src_preprocessor_lines
[1][5], -1);
2164 dirname
= unescape_filename (tmp
);
2166 if (dirname
== NULL
)
2169 len
= strlen (dirname
);
2170 if (len
< 3 || dirname
[len
- 1] != '/' || dirname
[len
- 2] != '/')
2175 dirname
[len
- 2] = '\0';
2176 set_src_pwd (dirname
);
2178 if (! IS_ABSOLUTE_PATH (filename
))
2180 char *p
= XCNEWVEC (char, len
+ strlen (filename
));
2182 memcpy (p
, dirname
, len
- 2);
2184 strcpy (p
+ len
- 1, filename
);
2185 *canon_source_file
= p
;