2 Copyright (C) 2000-2022 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* Set of subroutines to (ultimately) return the next character to the
22 various matching subroutines. This file's job is to read files and
23 build up lines that are parsed by the parser. This means that we
24 handle continuation lines and "include" lines.
26 The first thing the scanner does is to load an entire file into
27 memory. We load the entire file into memory for a couple reasons.
28 The first is that we want to be able to deal with nonseekable input
29 (pipes, stdin) and there is a lot of backing up involved during
32 The second is that we want to be able to print the locus of errors,
33 and an error on line 999999 could conflict with something on line
34 one. Given nonseekable input, we've got to store the whole thing.
36 One thing that helps are the column truncation limits that give us
37 an upper bound on the size of individual lines. We don't store the
40 From the scanner's viewpoint, the higher level subroutines ask for
41 new characters and do a lot of jumping backwards. */
45 #include "coretypes.h"
47 #include "toplev.h" /* For set_src_pwd. */
50 #include "diagnostic-core.h" /* For fatal_error. */
54 /* List of include file search directories. */
55 gfc_directorylist
*include_dirs
, *intrinsic_modules_dirs
;
57 static gfc_file
*file_head
, *current_file
;
59 static int continue_flag
, end_flag
, gcc_attribute_flag
;
60 /* If !$omp/!$acc occurred in current comment line. */
61 static int openmp_flag
, openacc_flag
;
62 static int continue_count
, continue_line
;
63 static locus openmp_locus
;
64 static locus openacc_locus
;
65 static locus gcc_attribute_locus
;
67 gfc_source_form gfc_current_form
;
68 static gfc_linebuf
*line_head
, *line_tail
;
70 locus gfc_current_locus
;
71 const char *gfc_source_file
;
72 static FILE *gfc_src_file
;
73 static gfc_char_t
*gfc_src_preprocessor_lines
[2];
75 static struct gfc_file_change
81 static size_t file_changes_cur
, file_changes_count
;
82 static size_t file_changes_allocated
;
84 static gfc_char_t
*last_error_char
;
86 /* Functions dealing with our wide characters (gfc_char_t) and
87 sequences of such characters. */
90 gfc_wide_fits_in_byte (gfc_char_t c
)
92 return (c
<= UCHAR_MAX
);
96 wide_is_ascii (gfc_char_t c
)
98 return (gfc_wide_fits_in_byte (c
) && ((unsigned char) c
& ~0x7f) == 0);
102 gfc_wide_is_printable (gfc_char_t c
)
104 return (gfc_wide_fits_in_byte (c
) && ISPRINT ((unsigned char) c
));
108 gfc_wide_tolower (gfc_char_t c
)
110 return (wide_is_ascii (c
) ? (gfc_char_t
) TOLOWER((unsigned char) c
) : c
);
114 gfc_wide_toupper (gfc_char_t c
)
116 return (wide_is_ascii (c
) ? (gfc_char_t
) TOUPPER((unsigned char) c
) : c
);
120 gfc_wide_is_digit (gfc_char_t c
)
122 return (c
>= '0' && c
<= '9');
126 wide_atoi (gfc_char_t
*c
)
128 #define MAX_DIGITS 20
129 char buf
[MAX_DIGITS
+1];
132 while (gfc_wide_is_digit(*c
) && i
< MAX_DIGITS
)
139 gfc_wide_strlen (const gfc_char_t
*str
)
143 for (i
= 0; str
[i
]; i
++)
150 gfc_wide_memset (gfc_char_t
*b
, gfc_char_t c
, size_t len
)
154 for (i
= 0; i
< len
; i
++)
161 wide_strcpy (gfc_char_t
*dest
, const gfc_char_t
*src
)
165 for (d
= dest
; (*d
= *src
) != '\0'; ++src
, ++d
)
172 wide_strchr (const gfc_char_t
*s
, gfc_char_t c
)
177 return CONST_CAST(gfc_char_t
*, s
);
184 gfc_widechar_to_char (const gfc_char_t
*s
, int length
)
192 /* Passing a negative length is used to indicate that length should be
193 calculated using gfc_wide_strlen(). */
194 len
= (length
>= 0 ? (size_t) length
: gfc_wide_strlen (s
));
195 res
= XNEWVEC (char, len
+ 1);
197 for (i
= 0; i
< len
; i
++)
199 gcc_assert (gfc_wide_fits_in_byte (s
[i
]));
200 res
[i
] = (unsigned char) s
[i
];
208 gfc_char_to_widechar (const char *s
)
217 res
= gfc_get_wide_string (len
+ 1);
219 for (i
= 0; i
< len
; i
++)
220 res
[i
] = (unsigned char) s
[i
];
227 wide_strncmp (const gfc_char_t
*s1
, const char *s2
, size_t n
)
236 return (c1
> c2
? 1 : -1);
244 gfc_wide_strncasecmp (const gfc_char_t
*s1
, const char *s2
, size_t n
)
250 c1
= gfc_wide_tolower (*s1
++);
251 c2
= TOLOWER (*s2
++);
253 return (c1
> c2
? 1 : -1);
261 /* Main scanner initialization. */
264 gfc_scanner_init_1 (void)
274 last_error_char
= NULL
;
278 /* Main scanner destructor. */
281 gfc_scanner_done_1 (void)
286 while(line_head
!= NULL
)
288 lb
= line_head
->next
;
293 while(file_head
!= NULL
)
296 free (file_head
->filename
);
303 gfc_do_check_include_dir (const char *path
, bool warn
)
306 if (stat (path
, &st
))
309 gfc_warning_now (0, "Include directory %qs: %s",
310 path
, xstrerror(errno
));
312 gfc_warning_now (OPT_Wmissing_include_dirs
,
313 "Nonexistent include directory %qs", path
);
316 else if (!S_ISDIR (st
.st_mode
))
318 gfc_fatal_error ("%qs is not a directory", path
);
324 /* In order that -W(no-)missing-include-dirs works, the diagnostic can only be
325 run after processing the commandline. */
327 gfc_do_check_include_dirs (gfc_directorylist
**list
, bool do_warn
)
329 gfc_directorylist
*prev
, *q
, *n
;
335 if (gfc_do_check_include_dir (q
->path
, q
->warn
&& do_warn
))
350 gfc_check_include_dirs (bool verbose_missing_dir_warn
)
352 /* This is a bit convoluted: If gfc_cpp_enabled () and
353 verbose_missing_dir_warn, the warning is shown by libcpp. Otherwise,
354 it is shown here, still conditional on OPT_Wmissing_include_dirs. */
355 bool warn
= !gfc_cpp_enabled () || !verbose_missing_dir_warn
;
356 gfc_do_check_include_dirs (&include_dirs
, warn
);
357 gfc_do_check_include_dirs (&intrinsic_modules_dirs
, verbose_missing_dir_warn
);
358 if (gfc_option
.module_dir
&& gfc_cpp_enabled ())
359 gfc_do_check_include_dirs (&include_dirs
, true);
362 /* Adds path to the list pointed to by list. */
365 add_path_to_list (gfc_directorylist
**list
, const char *path
,
366 bool use_for_modules
, bool head
, bool warn
, bool defer_warn
)
368 gfc_directorylist
*dir
;
375 while (*p
== ' ' || *p
== '\t') /* someone might do "-I include" */
379 /* Strip trailing directory separators from the path, as this
380 will confuse Windows systems. */
382 q
= (char *) alloca (len
+ 1);
383 memcpy (q
, p
, len
+ 1);
385 while (i
>=0 && IS_DIR_SEPARATOR (q
[i
]))
388 if (!defer_warn
&& !gfc_do_check_include_dir (q
, warn
))
391 if (head
|| *list
== NULL
)
393 dir
= XCNEW (gfc_directorylist
);
403 dir
->next
= XCNEW (gfc_directorylist
);
407 dir
->next
= head
? *list
: NULL
;
410 dir
->use_for_modules
= use_for_modules
;
412 dir
->path
= xstrdup (p
);
415 /* defer_warn is set to true while parsing the commandline. */
418 gfc_add_include_path (const char *path
, bool use_for_modules
, bool file_dir
,
419 bool warn
, bool defer_warn
)
421 add_path_to_list (&include_dirs
, path
, use_for_modules
, file_dir
, warn
,
424 /* For '#include "..."' these directories are automatically searched. */
426 gfc_cpp_add_include_path (xstrdup(path
), true);
431 gfc_add_intrinsic_modules_path (const char *path
)
433 add_path_to_list (&intrinsic_modules_dirs
, path
, true, false, false, false);
437 /* Release resources allocated for options. */
440 gfc_release_include_path (void)
442 gfc_directorylist
*p
;
444 while (include_dirs
!= NULL
)
447 include_dirs
= include_dirs
->next
;
452 while (intrinsic_modules_dirs
!= NULL
)
454 p
= intrinsic_modules_dirs
;
455 intrinsic_modules_dirs
= intrinsic_modules_dirs
->next
;
460 free (gfc_option
.module_dir
);
465 open_included_file (const char *name
, gfc_directorylist
*list
,
466 bool module
, bool system
)
469 gfc_directorylist
*p
;
472 for (p
= list
; p
; p
= p
->next
)
474 if (module
&& !p
->use_for_modules
)
477 fullname
= (char *) alloca(strlen (p
->path
) + strlen (name
) + 2);
478 strcpy (fullname
, p
->path
);
479 strcat (fullname
, "/");
480 strcat (fullname
, name
);
482 f
= gfc_open_file (fullname
);
485 if (gfc_cpp_makedep ())
486 gfc_cpp_add_dep (fullname
, system
);
496 /* Opens file for reading, searching through the include directories
497 given if necessary. If the include_cwd argument is true, we try
498 to open the file in the current directory first. */
501 gfc_open_included_file (const char *name
, bool include_cwd
, bool module
)
505 if (IS_ABSOLUTE_PATH (name
) || include_cwd
)
507 f
= gfc_open_file (name
);
508 if (f
&& gfc_cpp_makedep ())
509 gfc_cpp_add_dep (name
, false);
513 f
= open_included_file (name
, include_dirs
, module
, false);
519 /* Test to see if we're at the end of the main source file. */
528 /* Test to see if we're at the end of the current file. */
536 if (line_head
== NULL
)
537 return 1; /* Null file */
539 if (gfc_current_locus
.lb
== NULL
)
546 /* Test to see if we're at the beginning of a new line. */
554 return (gfc_current_locus
.nextc
== gfc_current_locus
.lb
->line
);
558 /* Test to see if we're at the end of a line. */
566 return (*gfc_current_locus
.nextc
== '\0');
570 add_file_change (const char *filename
, int line
)
572 if (file_changes_count
== file_changes_allocated
)
574 if (file_changes_allocated
)
575 file_changes_allocated
*= 2;
577 file_changes_allocated
= 16;
578 file_changes
= XRESIZEVEC (struct gfc_file_change
, file_changes
,
579 file_changes_allocated
);
581 file_changes
[file_changes_count
].filename
= filename
;
582 file_changes
[file_changes_count
].lb
= NULL
;
583 file_changes
[file_changes_count
++].line
= line
;
587 report_file_change (gfc_linebuf
*lb
)
589 size_t c
= file_changes_cur
;
590 while (c
< file_changes_count
591 && file_changes
[c
].lb
== lb
)
593 if (file_changes
[c
].filename
)
594 (*debug_hooks
->start_source_file
) (file_changes
[c
].line
,
595 file_changes
[c
].filename
);
597 (*debug_hooks
->end_source_file
) (file_changes
[c
].line
);
600 file_changes_cur
= c
;
604 gfc_start_source_files (void)
606 /* If the debugger wants the name of the main source file,
608 if (debug_hooks
->start_end_main_source_file
)
609 (*debug_hooks
->start_source_file
) (0, gfc_source_file
);
611 file_changes_cur
= 0;
612 report_file_change (gfc_current_locus
.lb
);
616 gfc_end_source_files (void)
618 report_file_change (NULL
);
620 if (debug_hooks
->start_end_main_source_file
)
621 (*debug_hooks
->end_source_file
) (0);
624 /* Advance the current line pointer to the next line. */
627 gfc_advance_line (void)
632 if (gfc_current_locus
.lb
== NULL
)
638 if (gfc_current_locus
.lb
->next
639 && !gfc_current_locus
.lb
->next
->dbg_emitted
)
641 report_file_change (gfc_current_locus
.lb
->next
);
642 gfc_current_locus
.lb
->next
->dbg_emitted
= true;
645 gfc_current_locus
.lb
= gfc_current_locus
.lb
->next
;
647 if (gfc_current_locus
.lb
!= NULL
)
648 gfc_current_locus
.nextc
= gfc_current_locus
.lb
->line
;
651 gfc_current_locus
.nextc
= NULL
;
657 /* Get the next character from the input, advancing gfc_current_file's
658 locus. When we hit the end of the line or the end of the file, we
659 start returning a '\n' in order to complete the current statement.
660 No Fortran line conventions are implemented here.
662 Requiring explicit advances to the next line prevents the parse
663 pointer from being on the wrong line if the current statement ends
671 if (gfc_current_locus
.nextc
== NULL
)
674 c
= *gfc_current_locus
.nextc
++;
677 gfc_current_locus
.nextc
--; /* Remain on this line. */
685 /* Skip a comment. When we come here the parse pointer is positioned
686 immediately after the comment character. If we ever implement
687 compiler directives within comments, here is where we parse the
691 skip_comment_line (void)
706 gfc_define_undef_line (void)
710 /* All lines beginning with '#' are either #define or #undef. */
711 if (debug_info_level
!= DINFO_LEVEL_VERBOSE
|| gfc_peek_ascii_char () != '#')
714 if (wide_strncmp (gfc_current_locus
.nextc
, "#define ", 8) == 0)
716 tmp
= gfc_widechar_to_char (&gfc_current_locus
.nextc
[8], -1);
717 (*debug_hooks
->define
) (gfc_linebuf_linenum (gfc_current_locus
.lb
),
722 if (wide_strncmp (gfc_current_locus
.nextc
, "#undef ", 7) == 0)
724 tmp
= gfc_widechar_to_char (&gfc_current_locus
.nextc
[7], -1);
725 (*debug_hooks
->undef
) (gfc_linebuf_linenum (gfc_current_locus
.lb
),
730 /* Skip the rest of the line. */
731 skip_comment_line ();
737 /* Return true if GCC$ was matched. */
739 skip_gcc_attribute (locus start
)
743 locus old_loc
= gfc_current_locus
;
745 if ((c
= next_char ()) == 'g' || c
== 'G')
746 if ((c
= next_char ()) == 'c' || c
== 'C')
747 if ((c
= next_char ()) == 'c' || c
== 'C')
748 if ((c
= next_char ()) == '$')
752 gfc_current_locus
= old_loc
;
755 gcc_attribute_flag
= 1;
756 gcc_attribute_locus
= old_loc
;
757 gfc_current_locus
= start
;
763 /* Return true if CC was matched. */
765 skip_free_oacc_sentinel (locus start
, locus old_loc
)
770 if ((c
= next_char ()) == 'c' || c
== 'C')
771 if ((c
= next_char ()) == 'c' || c
== 'C')
776 if ((c
= next_char ()) == ' ' || c
== '\t'
779 while (gfc_is_whitespace (c
))
781 if (c
!= '\n' && c
!= '!')
784 openacc_locus
= old_loc
;
785 gfc_current_locus
= start
;
792 gfc_warning_now (0, "!$ACC at %C starts a commented "
793 "line as it neither is followed "
794 "by a space nor is a "
795 "continuation line");
803 /* Return true if MP was matched. */
805 skip_free_omp_sentinel (locus start
, locus old_loc
)
810 if ((c
= next_char ()) == 'm' || c
== 'M')
811 if ((c
= next_char ()) == 'p' || c
== 'P')
816 if ((c
= next_char ()) == ' ' || c
== '\t'
819 while (gfc_is_whitespace (c
))
821 if (c
!= '\n' && c
!= '!')
824 openmp_locus
= old_loc
;
825 gfc_current_locus
= start
;
832 gfc_warning_now (0, "!$OMP at %C starts a commented "
833 "line as it neither is followed "
834 "by a space nor is a "
835 "continuation line");
843 /* Comment lines are null lines, lines containing only blanks or lines
844 on which the first nonblank line is a '!'.
845 Return true if !$ openmp or openacc conditional compilation sentinel was
849 skip_free_comments (void)
857 at_bol
= gfc_at_bol ();
858 start
= gfc_current_locus
;
864 while (gfc_is_whitespace (c
));
874 /* Keep the !GCC$ line. */
875 if (at_bol
&& skip_gcc_attribute (start
))
878 /* If -fopenmp/-fopenacc, we need to handle here 2 things:
879 1) don't treat !$omp/!$acc as comments, but directives
880 2) handle OpenMP/OpenACC conditional compilation, where
881 !$ should be treated as 2 spaces (for initial lines
882 only if followed by space). */
885 if ((flag_openmp
|| flag_openmp_simd
)
888 locus old_loc
= gfc_current_locus
;
889 if (next_char () == '$')
892 if (c
== 'o' || c
== 'O')
894 if (skip_free_omp_sentinel (start
, old_loc
))
896 gfc_current_locus
= old_loc
;
900 else if (c
== 'a' || c
== 'A')
902 if (skip_free_oacc_sentinel (start
, old_loc
))
904 gfc_current_locus
= old_loc
;
908 if (continue_flag
|| c
== ' ' || c
== '\t')
910 gfc_current_locus
= old_loc
;
912 openmp_flag
= openacc_flag
= 0;
916 gfc_current_locus
= old_loc
;
918 else if ((flag_openmp
|| flag_openmp_simd
)
921 locus old_loc
= gfc_current_locus
;
922 if (next_char () == '$')
925 if (c
== 'o' || c
== 'O')
927 if (skip_free_omp_sentinel (start
, old_loc
))
929 gfc_current_locus
= old_loc
;
933 if (continue_flag
|| c
== ' ' || c
== '\t')
935 gfc_current_locus
= old_loc
;
941 gfc_current_locus
= old_loc
;
943 else if (flag_openacc
944 && !(flag_openmp
|| flag_openmp_simd
))
946 locus old_loc
= gfc_current_locus
;
947 if (next_char () == '$')
950 if (c
== 'a' || c
== 'A')
952 if (skip_free_oacc_sentinel (start
, old_loc
))
954 gfc_current_locus
= old_loc
;
959 gfc_current_locus
= old_loc
;
962 skip_comment_line ();
969 if (openmp_flag
&& at_bol
)
972 if (openacc_flag
&& at_bol
)
975 gcc_attribute_flag
= 0;
976 gfc_current_locus
= start
;
980 /* Return true if MP was matched in fixed form. */
982 skip_fixed_omp_sentinel (locus
*start
)
985 if (((c
= next_char ()) == 'm' || c
== 'M')
986 && ((c
= next_char ()) == 'p' || c
== 'P'))
991 || c
== ' ' || c
== '\t' || c
== '0'))
993 if (c
== ' ' || c
== '\t' || c
== '0')
997 while (gfc_is_whitespace (c
));
998 if (c
!= '\n' && c
!= '!')
1000 /* Canonicalize to *$omp. */
1001 *start
->nextc
= '*';
1003 gfc_current_locus
= *start
;
1011 /* Return true if CC was matched in fixed form. */
1013 skip_fixed_oacc_sentinel (locus
*start
)
1016 if (((c
= next_char ()) == 'c' || c
== 'C')
1017 && ((c
= next_char ()) == 'c' || c
== 'C'))
1022 || c
== ' ' || c
== '\t' || c
== '0'))
1024 if (c
== ' ' || c
== '\t' || c
== '0')
1028 while (gfc_is_whitespace (c
));
1029 if (c
!= '\n' && c
!= '!')
1031 /* Canonicalize to *$acc. */
1032 *start
->nextc
= '*';
1034 gfc_current_locus
= *start
;
1042 /* Skip comment lines in fixed source mode. We have the same rules as
1043 in skip_free_comment(), except that we can have a 'c', 'C' or '*'
1044 in column 1, and a '!' cannot be in column 6. Also, we deal with
1045 lines with 'd' or 'D' in column 1, if the user requested this. */
1048 skip_fixed_comments (void)
1054 if (! gfc_at_bol ())
1056 start
= gfc_current_locus
;
1057 if (! gfc_at_eof ())
1061 while (gfc_is_whitespace (c
));
1064 gfc_advance_line ();
1066 skip_comment_line ();
1069 if (! gfc_at_bol ())
1071 gfc_current_locus
= start
;
1078 start
= gfc_current_locus
;
1085 gfc_advance_line ();
1089 if (c
== '!' || c
== 'c' || c
== 'C' || c
== '*')
1091 if (skip_gcc_attribute (start
))
1093 /* Canonicalize to *$omp. */
1098 if (gfc_current_locus
.lb
!= NULL
1099 && continue_line
< gfc_linebuf_linenum (gfc_current_locus
.lb
))
1100 continue_line
= gfc_linebuf_linenum (gfc_current_locus
.lb
);
1102 /* If -fopenmp/-fopenacc, we need to handle here 2 things:
1103 1) don't treat !$omp/!$acc|c$omp/c$acc|*$omp / *$acc as comments,
1105 2) handle OpenMP/OpenACC conditional compilation, where
1106 !$|c$|*$ should be treated as 2 spaces if the characters
1107 in columns 3 to 6 are valid fixed form label columns
1109 if ((flag_openmp
|| flag_openmp_simd
) && !flag_openacc
)
1111 if (next_char () == '$')
1114 if (c
== 'o' || c
== 'O')
1116 if (skip_fixed_omp_sentinel (&start
))
1120 goto check_for_digits
;
1122 gfc_current_locus
= start
;
1124 else if (flag_openacc
&& !(flag_openmp
|| flag_openmp_simd
))
1126 if (next_char () == '$')
1129 if (c
== 'a' || c
== 'A')
1131 if (skip_fixed_oacc_sentinel (&start
))
1135 gfc_current_locus
= start
;
1137 else if (flag_openacc
|| flag_openmp
|| flag_openmp_simd
)
1139 if (next_char () == '$')
1142 if (c
== 'a' || c
== 'A')
1144 if (skip_fixed_oacc_sentinel (&start
))
1147 else if (c
== 'o' || c
== 'O')
1149 if (skip_fixed_omp_sentinel (&start
))
1153 goto check_for_digits
;
1155 gfc_current_locus
= start
;
1158 skip_comment_line ();
1163 /* Required for OpenMP's conditional compilation sentinel. */
1166 for (col
= 3; col
< 6; col
++, c
= next_char ())
1174 else if (c
< '0' || c
> '9')
1179 if (col
== 6 && c
!= '\n'
1180 && ((continue_flag
&& !digit_seen
)
1181 || c
== ' ' || c
== '\t' || c
== '0'))
1183 gfc_current_locus
= start
;
1184 start
.nextc
[0] = ' ';
1185 start
.nextc
[1] = ' ';
1189 skip_comment_line ();
1193 if (gfc_option
.flag_d_lines
!= -1 && (c
== 'd' || c
== 'D'))
1195 if (gfc_option
.flag_d_lines
== 0)
1197 skip_comment_line ();
1201 *start
.nextc
= c
= ' ';
1206 while (gfc_is_whitespace (c
))
1214 gfc_advance_line ();
1218 if (col
!= 6 && c
== '!')
1220 if (gfc_current_locus
.lb
!= NULL
1221 && continue_line
< gfc_linebuf_linenum (gfc_current_locus
.lb
))
1222 continue_line
= gfc_linebuf_linenum (gfc_current_locus
.lb
);
1223 skip_comment_line ();
1232 gcc_attribute_flag
= 0;
1233 gfc_current_locus
= start
;
1237 /* Skips the current line if it is a comment. */
1240 gfc_skip_comments (void)
1242 if (gfc_current_form
== FORM_FREE
)
1243 skip_free_comments ();
1245 skip_fixed_comments ();
1249 /* Get the next character from the input, taking continuation lines
1250 and end-of-line comments into account. This implies that comment
1251 lines between continued lines must be eaten here. For higher-level
1252 subroutines, this flattens continued lines into a single logical
1253 line. The in_string flag denotes whether we're inside a character
1257 gfc_next_char_literal (gfc_instring in_string
)
1259 static locus omp_acc_err_loc
= {};
1261 int i
, prev_openmp_flag
, prev_openacc_flag
;
1265 prev_openacc_flag
= prev_openmp_flag
= 0;
1275 if (gfc_current_form
== FORM_FREE
)
1277 bool openmp_cond_flag
;
1279 if (!in_string
&& c
== '!')
1281 if (gcc_attribute_flag
1282 && memcmp (&gfc_current_locus
, &gcc_attribute_locus
,
1283 sizeof (gfc_current_locus
)) == 0)
1287 && memcmp (&gfc_current_locus
, &openmp_locus
,
1288 sizeof (gfc_current_locus
)) == 0)
1292 && memcmp (&gfc_current_locus
, &openacc_locus
,
1293 sizeof (gfc_current_locus
)) == 0)
1296 /* This line can't be continued */
1303 /* Avoid truncation warnings for comment ending lines. */
1304 gfc_current_locus
.lb
->truncated
= 0;
1309 /* Check to see if the continuation line was truncated. */
1310 if (warn_line_truncation
&& gfc_current_locus
.lb
!= NULL
1311 && gfc_current_locus
.lb
->truncated
)
1313 int maxlen
= flag_free_line_length
;
1314 gfc_char_t
*current_nextc
= gfc_current_locus
.nextc
;
1316 gfc_current_locus
.lb
->truncated
= 0;
1317 gfc_current_locus
.nextc
= gfc_current_locus
.lb
->line
+ maxlen
;
1318 gfc_warning_now (OPT_Wline_truncation
,
1319 "Line truncated at %L", &gfc_current_locus
);
1320 gfc_current_locus
.nextc
= current_nextc
;
1326 /* If the next nonblank character is a ! or \n, we've got a
1327 continuation line. */
1328 old_loc
= gfc_current_locus
;
1331 while (gfc_is_whitespace (c
))
1334 /* Character constants to be continued cannot have commentary
1335 after the '&'. However, there are cases where we may think we
1336 are still in a string and we are looking for a possible
1337 doubled quote and we end up here. See PR64506. */
1339 if (in_string
&& c
!= '\n')
1341 gfc_current_locus
= old_loc
;
1346 if (c
!= '!' && c
!= '\n')
1348 gfc_current_locus
= old_loc
;
1354 prev_openmp_flag
= openmp_flag
;
1356 prev_openacc_flag
= openacc_flag
;
1358 /* This can happen if the input file changed or via cpp's #line
1359 without getting reset (e.g. via input_stmt). It also happens
1360 when pre-including files via -fpre-include=. */
1361 if (continue_count
== 0
1362 && gfc_current_locus
.lb
1363 && continue_line
> gfc_linebuf_linenum (gfc_current_locus
.lb
) + 1)
1364 continue_line
= gfc_linebuf_linenum (gfc_current_locus
.lb
) + 1;
1368 skip_comment_line ();
1370 gfc_advance_line ();
1373 goto not_continuation
;
1375 /* We've got a continuation line. If we are on the very next line after
1376 the last continuation, increment the continuation line count and
1377 check whether the limit has been exceeded. */
1378 if (gfc_linebuf_linenum (gfc_current_locus
.lb
) == continue_line
+ 1)
1380 if (++continue_count
== gfc_option
.max_continue_free
)
1382 if (gfc_notification_std (GFC_STD_GNU
) || pedantic
)
1383 gfc_warning (0, "Limit of %d continuations exceeded in "
1384 "statement at %C", gfc_option
.max_continue_free
);
1388 /* Now find where it continues. First eat any comment lines. */
1389 openmp_cond_flag
= skip_free_comments ();
1391 if (gfc_current_locus
.lb
!= NULL
1392 && continue_line
< gfc_linebuf_linenum (gfc_current_locus
.lb
))
1393 continue_line
= gfc_linebuf_linenum (gfc_current_locus
.lb
);
1396 if (prev_openmp_flag
!= openmp_flag
&& !openacc_flag
)
1398 gfc_current_locus
= old_loc
;
1399 openmp_flag
= prev_openmp_flag
;
1405 if (prev_openacc_flag
!= openacc_flag
&& !openmp_flag
)
1407 gfc_current_locus
= old_loc
;
1408 openacc_flag
= prev_openacc_flag
;
1413 /* Now that we have a non-comment line, probe ahead for the
1414 first non-whitespace character. If it is another '&', then
1415 reading starts at the next character, otherwise we must back
1416 up to where the whitespace started and resume from there. */
1418 old_loc
= gfc_current_locus
;
1421 while (gfc_is_whitespace (c
))
1424 if (openmp_flag
&& !openacc_flag
)
1426 for (i
= 0; i
< 5; i
++, c
= next_char ())
1428 gcc_assert (gfc_wide_tolower (c
) == (unsigned char) "!$omp"[i
]);
1430 old_loc
= gfc_current_locus
;
1432 while (gfc_is_whitespace (c
))
1435 if (openacc_flag
&& !openmp_flag
)
1437 for (i
= 0; i
< 5; i
++, c
= next_char ())
1439 gcc_assert (gfc_wide_tolower (c
) == (unsigned char) "!$acc"[i
]);
1441 old_loc
= gfc_current_locus
;
1443 while (gfc_is_whitespace (c
))
1447 /* In case we have an OpenMP directive continued by OpenACC
1448 sentinel, or vice versa, we get both openmp_flag and
1451 if (openacc_flag
&& openmp_flag
)
1454 for (i
= 0; i
< 5; i
++, c
= next_char ())
1456 if (gfc_wide_tolower (c
) != (unsigned char) "!$acc"[i
])
1459 if (omp_acc_err_loc
.nextc
!= gfc_current_locus
.nextc
1460 || omp_acc_err_loc
.lb
!= gfc_current_locus
.lb
)
1461 gfc_error_now (is_openmp
1462 ? G_("Wrong OpenACC continuation at %C: "
1463 "expected !$ACC, got !$OMP")
1464 : G_("Wrong OpenMP continuation at %C: "
1465 "expected !$OMP, got !$ACC"));
1466 omp_acc_err_loc
= gfc_current_locus
;
1467 goto not_continuation
;
1472 if (in_string
&& gfc_current_locus
.nextc
)
1474 gfc_current_locus
.nextc
--;
1475 if (warn_ampersand
&& in_string
== INSTRING_WARN
)
1476 gfc_warning (OPT_Wampersand
,
1477 "Missing %<&%> in continued character "
1480 else if (!in_string
&& (c
== '\'' || c
== '"'))
1482 /* Both !$omp and !$ -fopenmp continuation lines have & on the
1483 continuation line only optionally. */
1484 else if (openmp_flag
|| openacc_flag
|| openmp_cond_flag
)
1486 if (gfc_current_locus
.nextc
)
1487 gfc_current_locus
.nextc
--;
1492 gfc_current_locus
= old_loc
;
1497 else /* Fixed form. */
1499 /* Fixed form continuation. */
1500 if (in_string
!= INSTRING_WARN
&& c
== '!')
1502 /* Skip comment at end of line. */
1509 /* Avoid truncation warnings for comment ending lines. */
1510 gfc_current_locus
.lb
->truncated
= 0;
1516 /* Check to see if the continuation line was truncated. */
1517 if (warn_line_truncation
&& gfc_current_locus
.lb
!= NULL
1518 && gfc_current_locus
.lb
->truncated
)
1520 gfc_current_locus
.lb
->truncated
= 0;
1521 gfc_warning_now (OPT_Wline_truncation
,
1522 "Line truncated at %L", &gfc_current_locus
);
1526 prev_openmp_flag
= openmp_flag
;
1528 prev_openacc_flag
= openacc_flag
;
1530 /* This can happen if the input file changed or via cpp's #line
1531 without getting reset (e.g. via input_stmt). It also happens
1532 when pre-including files via -fpre-include=. */
1533 if (continue_count
== 0
1534 && gfc_current_locus
.lb
1535 && continue_line
> gfc_linebuf_linenum (gfc_current_locus
.lb
) + 1)
1536 continue_line
= gfc_linebuf_linenum (gfc_current_locus
.lb
) + 1;
1539 old_loc
= gfc_current_locus
;
1541 gfc_advance_line ();
1542 skip_fixed_comments ();
1544 /* See if this line is a continuation line. */
1545 if (flag_openmp
&& openmp_flag
!= prev_openmp_flag
&& !openacc_flag
)
1547 openmp_flag
= prev_openmp_flag
;
1548 goto not_continuation
;
1550 if (flag_openacc
&& openacc_flag
!= prev_openacc_flag
&& !openmp_flag
)
1552 openacc_flag
= prev_openacc_flag
;
1553 goto not_continuation
;
1556 /* In case we have an OpenMP directive continued by OpenACC
1557 sentinel, or vice versa, we get both openmp_flag and
1559 if (openacc_flag
&& openmp_flag
)
1562 for (i
= 0; i
< 5; i
++)
1565 if (gfc_wide_tolower (c
) != (unsigned char) "*$acc"[i
])
1568 if (omp_acc_err_loc
.nextc
!= gfc_current_locus
.nextc
1569 || omp_acc_err_loc
.lb
!= gfc_current_locus
.lb
)
1570 gfc_error_now (is_openmp
1571 ? G_("Wrong OpenACC continuation at %C: "
1572 "expected !$ACC, got !$OMP")
1573 : G_("Wrong OpenMP continuation at %C: "
1574 "expected !$OMP, got !$ACC"));
1575 omp_acc_err_loc
= gfc_current_locus
;
1576 goto not_continuation
;
1578 else if (!openmp_flag
&& !openacc_flag
)
1579 for (i
= 0; i
< 5; i
++)
1583 goto not_continuation
;
1585 else if (openmp_flag
)
1586 for (i
= 0; i
< 5; i
++)
1589 if (gfc_wide_tolower (c
) != (unsigned char) "*$omp"[i
])
1590 goto not_continuation
;
1592 else if (openacc_flag
)
1593 for (i
= 0; i
< 5; i
++)
1596 if (gfc_wide_tolower (c
) != (unsigned char) "*$acc"[i
])
1597 goto not_continuation
;
1601 if (c
== '0' || c
== ' ' || c
== '\n')
1602 goto not_continuation
;
1604 /* We've got a continuation line. If we are on the very next line after
1605 the last continuation, increment the continuation line count and
1606 check whether the limit has been exceeded. */
1607 if (gfc_linebuf_linenum (gfc_current_locus
.lb
) == continue_line
+ 1)
1609 if (++continue_count
== gfc_option
.max_continue_fixed
)
1611 if (gfc_notification_std (GFC_STD_GNU
) || pedantic
)
1612 gfc_warning (0, "Limit of %d continuations exceeded in "
1614 gfc_option
.max_continue_fixed
);
1618 if (gfc_current_locus
.lb
!= NULL
1619 && continue_line
< gfc_linebuf_linenum (gfc_current_locus
.lb
))
1620 continue_line
= gfc_linebuf_linenum (gfc_current_locus
.lb
);
1623 /* Ready to read first character of continuation line, which might
1624 be another continuation line! */
1629 gfc_current_locus
= old_loc
;
1640 /* Get the next character of input, folded to lowercase. In fixed
1641 form mode, we also ignore spaces. When matcher subroutines are
1642 parsing character literals, they have to call
1643 gfc_next_char_literal(). */
1646 gfc_next_char (void)
1652 c
= gfc_next_char_literal (NONSTRING
);
1654 while (gfc_current_form
== FORM_FIXED
&& gfc_is_whitespace (c
));
1656 return gfc_wide_tolower (c
);
1660 gfc_next_ascii_char (void)
1662 gfc_char_t c
= gfc_next_char ();
1664 return (gfc_wide_fits_in_byte (c
) ? (unsigned char) c
1665 : (unsigned char) UCHAR_MAX
);
1670 gfc_peek_char (void)
1675 old_loc
= gfc_current_locus
;
1676 c
= gfc_next_char ();
1677 gfc_current_locus
= old_loc
;
1684 gfc_peek_ascii_char (void)
1686 gfc_char_t c
= gfc_peek_char ();
1688 return (gfc_wide_fits_in_byte (c
) ? (unsigned char) c
1689 : (unsigned char) UCHAR_MAX
);
1693 /* Recover from an error. We try to get past the current statement
1694 and get lined up for the next. The next statement follows a '\n'
1695 or a ';'. We also assume that we are not within a character
1696 constant, and deal with finding a '\'' or '"'. */
1699 gfc_error_recovery (void)
1701 gfc_char_t c
, delim
;
1708 c
= gfc_next_char ();
1709 if (c
== '\n' || c
== ';')
1712 if (c
!= '\'' && c
!= '"')
1741 /* Read ahead until the next character to be read is not whitespace. */
1744 gfc_gobble_whitespace (void)
1746 static int linenum
= 0;
1752 old_loc
= gfc_current_locus
;
1753 c
= gfc_next_char_literal (NONSTRING
);
1754 /* Issue a warning for nonconforming tabs. We keep track of the line
1755 number because the Fortran matchers will often back up and the same
1756 line will be scanned multiple times. */
1757 if (warn_tabs
&& c
== '\t')
1759 int cur_linenum
= LOCATION_LINE (gfc_current_locus
.lb
->location
);
1760 if (cur_linenum
!= linenum
)
1762 linenum
= cur_linenum
;
1763 gfc_warning_now (OPT_Wtabs
, "Nonconforming tab character at %C");
1767 while (gfc_is_whitespace (c
));
1769 if (!ISPRINT(c
) && c
!= '\n' && last_error_char
!= gfc_current_locus
.nextc
)
1772 last_error_char
= gfc_current_locus
.nextc
;
1773 snprintf (buf
, 20, "%2.2X", c
);
1774 gfc_error_now ("Invalid character 0x%s at %C", buf
);
1777 gfc_current_locus
= old_loc
;
1781 /* Load a single line into pbuf.
1783 If pbuf points to a NULL pointer, it is allocated.
1784 We truncate lines that are too long, unless we're dealing with
1785 preprocessor lines or if the option -ffixed-line-length-none is set,
1786 in which case we reallocate the buffer to fit the entire line, if
1788 In fixed mode, we expand a tab that occurs within the statement
1789 label region to expand to spaces that leave the next character in
1792 If first_char is not NULL, it's a pointer to a single char value holding
1793 the first character of the line, which has already been read by the
1794 caller. This avoids the use of ungetc().
1796 load_line returns whether the line was truncated.
1798 NOTE: The error machinery isn't available at this point, so we can't
1799 easily report line and column numbers consistent with other
1800 parts of gfortran. */
1803 load_line (FILE *input
, gfc_char_t
**pbuf
, int *pbuflen
, const int *first_char
)
1805 int c
, maxlen
, i
, preprocessor_flag
, buflen
= *pbuflen
;
1806 int quoted
= ' ', comment_ix
= -1;
1807 bool seen_comment
= false;
1808 bool first_comment
= true;
1809 bool trunc_flag
= false;
1810 bool seen_printable
= false;
1811 bool seen_ampersand
= false;
1812 bool found_tab
= false;
1813 bool warned_tabs
= false;
1816 /* Determine the maximum allowed line length. */
1817 if (gfc_current_form
== FORM_FREE
)
1818 maxlen
= flag_free_line_length
;
1819 else if (gfc_current_form
== FORM_FIXED
)
1820 maxlen
= flag_fixed_line_length
;
1826 /* Allocate the line buffer, storing its length into buflen.
1827 Note that if maxlen==0, indicating that arbitrary-length lines
1828 are allowed, the buffer will be reallocated if this length is
1829 insufficient; since 132 characters is the length of a standard
1830 free-form line, we use that as a starting guess. */
1836 *pbuf
= gfc_get_wide_string (buflen
+ 1);
1847 /* In order to not truncate preprocessor lines, we have to
1848 remember that this is one. */
1849 preprocessor_flag
= (c
== '#');
1858 /* Check for illegal use of ampersand. See F95 Standard 3.3.1.3. */
1859 if (gfc_current_form
== FORM_FREE
1860 && !seen_printable
&& seen_ampersand
)
1863 gfc_error_now ("%<&%> not allowed by itself in line %d",
1864 current_file
->line
);
1866 gfc_warning_now (0, "%<&%> not allowed by itself in line %d",
1867 current_file
->line
);
1872 if (c
== '\r' || c
== '\0')
1873 goto next_char
; /* Gobble characters. */
1879 seen_ampersand
= false;
1880 seen_printable
= true;
1883 seen_ampersand
= true;
1886 if ((c
!= '&' && c
!= '!' && c
!= ' ') || (c
== '!' && !seen_ampersand
))
1887 seen_printable
= true;
1889 /* Is this a fixed-form comment? */
1890 if (gfc_current_form
== FORM_FIXED
&& i
== 0
1891 && (c
== '*' || c
== 'c' || c
== 'C'
1892 || (gfc_option
.flag_d_lines
!= -1 && (c
== 'd' || c
== 'D'))))
1894 seen_comment
= true;
1900 if (c
== '\'' || c
== '"')
1903 else if (c
== quoted
)
1906 /* Is this a free-form comment? */
1907 if (c
== '!' && quoted
== ' ')
1910 first_comment
= false;
1911 seen_comment
= true;
1915 /* For truncation and tab warnings, set seen_comment to false if one has
1916 either an OpenMP or OpenACC directive - or a !GCC$ attribute. If
1917 OpenMP is enabled, use '!$' as conditional compilation sentinel
1918 and OpenMP directive ('!$omp'). */
1919 if (seen_comment
&& first_comment
&& flag_openmp
&& comment_ix
+ 1 == i
1921 first_comment
= seen_comment
= false;
1922 if (seen_comment
&& first_comment
&& comment_ix
+ 4 == i
)
1924 if (((*pbuf
)[comment_ix
+1] == 'g' || (*pbuf
)[comment_ix
+1] == 'G')
1925 && ((*pbuf
)[comment_ix
+2] == 'c' || (*pbuf
)[comment_ix
+2] == 'C')
1926 && ((*pbuf
)[comment_ix
+3] == 'c' || (*pbuf
)[comment_ix
+3] == 'C')
1928 first_comment
= seen_comment
= false;
1930 && (*pbuf
)[comment_ix
+1] == '$'
1931 && ((*pbuf
)[comment_ix
+2] == 'a' || (*pbuf
)[comment_ix
+2] == 'A')
1932 && ((*pbuf
)[comment_ix
+3] == 'c' || (*pbuf
)[comment_ix
+3] == 'C')
1933 && (c
== 'c' || c
== 'C'))
1934 first_comment
= seen_comment
= false;
1937 /* Vendor extension: "<tab>1" marks a continuation line. */
1941 if (c
>= '1' && c
<= '9')
1948 if (gfc_current_form
== FORM_FIXED
&& c
== '\t' && i
< 6)
1952 if (warn_tabs
&& seen_comment
== 0 && !warned_tabs
)
1955 gfc_warning_now (OPT_Wtabs
,
1956 "Nonconforming tab character in column %d "
1957 "of line %d", i
+ 1, current_file
->line
);
1972 if (maxlen
== 0 || preprocessor_flag
)
1976 /* Reallocate line buffer to double size to hold the
1978 buflen
= buflen
* 2;
1979 *pbuf
= XRESIZEVEC (gfc_char_t
, *pbuf
, (buflen
+ 1));
1980 buffer
= (*pbuf
) + i
;
1983 else if (i
>= maxlen
)
1985 bool trunc_warn
= true;
1987 /* Enhancement, if the very next non-space character is an ampersand
1988 or comment that we would otherwise warn about, don't mark as
1991 /* Truncate the rest of the line. */
1995 if (c
== '\r' || c
== ' ')
1998 if (c
== '\n' || c
== EOF
)
2001 if (!trunc_warn
&& c
!= '!')
2004 if (trunc_warn
&& ((gfc_current_form
== FORM_FIXED
&& c
== '&')
2011 if (trunc_warn
&& !seen_comment
)
2023 /* Pad lines to the selected line length in fixed form. */
2024 if (gfc_current_form
== FORM_FIXED
2025 && flag_fixed_line_length
!= 0
2027 && !preprocessor_flag
2030 while (i
++ < maxlen
)
2041 /* Get a gfc_file structure, initialize it and add it to
2045 get_file (const char *name
, enum lc_reason reason
)
2049 f
= XCNEW (gfc_file
);
2051 f
->filename
= xstrdup (name
);
2053 f
->next
= file_head
;
2056 f
->up
= current_file
;
2057 if (current_file
!= NULL
)
2058 f
->inclusion_line
= current_file
->line
;
2060 linemap_add (line_table
, reason
, false, f
->filename
, 1);
2066 /* Deal with a line from the C preprocessor. The
2067 initial octothorp has already been seen. */
2070 preprocessor_line (gfc_char_t
*c
)
2074 gfc_char_t
*wide_filename
;
2076 int escaped
, unescape
;
2080 while (*c
== ' ' || *c
== '\t')
2083 if (*c
< '0' || *c
> '9')
2086 line
= wide_atoi (c
);
2088 c
= wide_strchr (c
, ' ');
2091 /* No file name given. Set new line number. */
2092 current_file
->line
= line
;
2097 while (*c
== ' ' || *c
== '\t')
2107 /* Make filename end at quote. */
2110 while (*c
&& ! (!escaped
&& *c
== '"'))
2114 else if (*c
== '\\')
2123 /* Preprocessor line has no closing quote. */
2128 /* Undo effects of cpp_quote_string. */
2131 gfc_char_t
*s
= wide_filename
;
2132 gfc_char_t
*d
= gfc_get_wide_string (c
- wide_filename
- unescape
);
2148 flag
[1] = flag
[2] = flag
[3] = flag
[4] = false;
2152 c
= wide_strchr (c
, ' ');
2159 if (i
>= 1 && i
<= 4)
2163 /* Convert the filename in wide characters into a filename in narrow
2165 filename
= gfc_widechar_to_char (wide_filename
, -1);
2167 /* Interpret flags. */
2169 if (flag
[1]) /* Starting new file. */
2171 f
= get_file (filename
, LC_RENAME
);
2172 add_file_change (f
->filename
, f
->inclusion_line
);
2176 if (flag
[2]) /* Ending current file. */
2178 if (!current_file
->up
2179 || filename_cmp (current_file
->up
->filename
, filename
) != 0)
2181 linemap_line_start (line_table
, current_file
->line
, 80);
2182 /* ??? One could compute the exact column where the filename
2183 starts and compute the exact location here. */
2184 gfc_warning_now_at (linemap_position_for_column (line_table
, 1),
2185 0, "file %qs left but not entered",
2187 current_file
->line
++;
2189 free (wide_filename
);
2194 add_file_change (NULL
, line
);
2195 current_file
= current_file
->up
;
2196 linemap_add (line_table
, LC_RENAME
, false, current_file
->filename
,
2197 current_file
->line
);
2200 /* The name of the file can be a temporary file produced by
2201 cpp. Replace the name if it is different. */
2203 if (filename_cmp (current_file
->filename
, filename
) != 0)
2205 /* FIXME: we leak the old filename because a pointer to it may be stored
2206 in the linemap. Alternative could be using GC or updating linemap to
2207 point to the new name, but there is no API for that currently. */
2208 current_file
->filename
= xstrdup (filename
);
2210 /* We need to tell the linemap API that the filename changed. Just
2211 changing current_file is insufficient. */
2212 linemap_add (line_table
, LC_RENAME
, false, current_file
->filename
, line
);
2215 /* Set new line number. */
2216 current_file
->line
= line
;
2218 free (wide_filename
);
2223 linemap_line_start (line_table
, current_file
->line
, 80);
2224 /* ??? One could compute the exact column where the directive
2225 starts and compute the exact location here. */
2226 gfc_warning_now_at (linemap_position_for_column (line_table
, 2), 0,
2227 "Illegal preprocessor directive");
2228 current_file
->line
++;
2232 static void load_file (const char *, const char *, bool);
2234 /* include_line()-- Checks a line buffer to see if it is an include
2235 line. If so, we call load_file() recursively to load the included
2236 file. We never return a syntax error because a statement like
2237 "include = 5" is perfectly legal. We return 0 if no include was
2238 processed, 1 if we matched an include or -1 if include was
2239 partially processed, but will need continuation lines. */
2242 include_line (gfc_char_t
*line
)
2244 gfc_char_t quote
, *c
, *begin
, *stop
;
2246 const char *include
= "include";
2247 bool allow_continuation
= flag_dec_include
;
2252 if (flag_openmp
|| flag_openmp_simd
)
2254 if (gfc_current_form
== FORM_FREE
)
2256 while (*c
== ' ' || *c
== '\t')
2258 if (*c
== '!' && c
[1] == '$' && (c
[2] == ' ' || c
[2] == '\t'))
2263 if ((*c
== '!' || *c
== 'c' || *c
== 'C' || *c
== '*')
2264 && c
[1] == '$' && c
[2] == ' ')
2269 if (gfc_current_form
== FORM_FREE
)
2271 while (*c
== ' ' || *c
== '\t')
2273 if (gfc_wide_strncasecmp (c
, "include", 7))
2275 if (!allow_continuation
)
2277 for (i
= 0; i
< 7; ++i
)
2279 gfc_char_t c1
= gfc_wide_tolower (*c
);
2280 if (c1
!= (unsigned char) include
[i
])
2284 if (i
== 0 || *c
!= '&')
2287 while (*c
== ' ' || *c
== '\t')
2289 if (*c
== '\0' || *c
== '!')
2298 while (*c
== ' ' || *c
== '\t')
2300 if (flag_dec_include
&& *c
== '0' && c
- line
== 5)
2303 while (*c
== ' ' || *c
== '\t')
2307 allow_continuation
= false;
2308 for (i
= 0; i
< 7; ++i
)
2310 gfc_char_t c1
= gfc_wide_tolower (*c
);
2311 if (c1
!= (unsigned char) include
[i
])
2314 while (*c
== ' ' || *c
== '\t')
2317 if (!allow_continuation
)
2327 /* At the end of line or comment this might be continued. */
2328 if (*c
== '\0' || *c
== '!')
2335 while (*c
== ' ' || *c
== '\t')
2338 /* Find filename between quotes. */
2341 if (quote
!= '"' && quote
!= '\'')
2343 if (allow_continuation
)
2345 if (gfc_current_form
== FORM_FREE
)
2349 while (*c
== ' ' || *c
== '\t')
2351 if (*c
== '\0' || *c
== '!')
2355 else if (quote
== '\0' || quote
== '!')
2364 while (*c
!= quote
&& *c
!= '\0')
2366 if (allow_continuation
&& gfc_current_form
== FORM_FREE
)
2370 else if (*c
!= ' ' && *c
!= '\t')
2378 if (allow_continuation
2379 && (cont
|| gfc_current_form
!= FORM_FREE
))
2386 while (*c
== ' ' || *c
== '\t')
2389 if (*c
!= '\0' && *c
!= '!')
2392 /* We have an include line at this point. */
2394 *stop
= '\0'; /* It's ok to trash the buffer, as this line won't be
2395 read by anything else. */
2397 filename
= gfc_widechar_to_char (begin
, -1);
2398 load_file (filename
, NULL
, false);
2403 /* Similarly, but try to parse an INCLUDE statement, using gfc_next_char etc.
2404 APIs. Return 1 if recognized as valid INCLUDE statement and load_file has
2405 been called, 0 if it is not a valid INCLUDE statement and -1 if eof has
2406 been encountered while parsing it. */
2408 include_stmt (gfc_linebuf
*b
)
2410 int ret
= 0, i
, length
;
2411 const char *include
= "include";
2412 gfc_char_t c
, quote
= 0;
2418 gcc_attribute_flag
= 0;
2423 gfc_current_locus
.lb
= b
;
2424 gfc_current_locus
.nextc
= b
->line
;
2426 gfc_skip_comments ();
2427 gfc_gobble_whitespace ();
2429 for (i
= 0; i
< 7; i
++)
2431 c
= gfc_next_char ();
2432 if (c
!= (unsigned char) include
[i
])
2434 if (gfc_current_form
== FORM_FIXED
2437 && gfc_current_locus
.nextc
== b
->line
+ 6)
2439 gfc_gobble_whitespace ();
2443 gcc_assert (i
!= 0);
2446 gfc_advance_line ();
2447 gfc_skip_comments ();
2454 gfc_gobble_whitespace ();
2456 c
= gfc_next_char ();
2457 if (c
== '\'' || c
== '"')
2463 gfc_advance_line ();
2464 gfc_skip_comments ();
2471 str_locus
= gfc_current_locus
;
2475 c
= gfc_next_char_literal (INSTRING_NOWARN
);
2480 gfc_advance_line ();
2481 gfc_skip_comments ();
2490 gfc_gobble_whitespace ();
2491 c
= gfc_next_char ();
2495 gfc_current_locus
= str_locus
;
2497 filename
= XNEWVEC (char, length
+ 1);
2498 for (i
= 0; i
< length
; i
++)
2500 c
= gfc_next_char_literal (INSTRING_WARN
);
2501 gcc_assert (gfc_wide_fits_in_byte (c
));
2502 filename
[i
] = (unsigned char) c
;
2504 filename
[length
] = '\0';
2505 load_file (filename
, NULL
, false);
2511 gcc_attribute_flag
= 0;
2516 memset (&gfc_current_locus
, '\0', sizeof (locus
));
2517 memset (&openmp_locus
, '\0', sizeof (locus
));
2518 memset (&openacc_locus
, '\0', sizeof (locus
));
2519 memset (&gcc_attribute_locus
, '\0', sizeof (locus
));
2525 /* Load a file into memory by calling load_line until the file ends. */
2528 load_file (const char *realfilename
, const char *displayedname
, bool initial
)
2531 gfc_linebuf
*b
, *include_b
= NULL
;
2538 const char *filename
;
2539 /* If realfilename and displayedname are different and non-null then
2540 surely realfilename is the preprocessed form of
2542 bool preprocessed_p
= (realfilename
&& displayedname
2543 && strcmp (realfilename
, displayedname
));
2545 filename
= displayedname
? displayedname
: realfilename
;
2547 for (f
= current_file
; f
; f
= f
->up
)
2548 if (filename_cmp (filename
, f
->filename
) == 0)
2549 fatal_error (linemap_line_start (line_table
, current_file
->line
, 0),
2550 "File %qs is being included recursively", filename
);
2555 input
= gfc_src_file
;
2556 gfc_src_file
= NULL
;
2559 input
= gfc_open_file (realfilename
);
2562 gfc_fatal_error ("Cannot open file %qs", filename
);
2566 input
= gfc_open_included_file (realfilename
, false, false);
2569 /* For -fpre-include file, current_file is NULL. */
2571 fatal_error (linemap_line_start (line_table
, current_file
->line
, 0),
2572 "Cannot open included file %qs", filename
);
2574 gfc_fatal_error ("Cannot open pre-included file %qs", filename
);
2576 stat_result
= stat (realfilename
, &st
);
2577 if (stat_result
== 0 && !S_ISREG (st
.st_mode
))
2581 fatal_error (linemap_line_start (line_table
, current_file
->line
, 0),
2582 "Included file %qs is not a regular file", filename
);
2584 gfc_fatal_error ("Included file %qs is not a regular file", filename
);
2590 A "non-initial" file means a file that is being included. In
2591 that case we are creating an LC_ENTER map.
2593 An "initial" file means a main file; one that is not included.
2594 That file has already got at least one (surely more) line map(s)
2595 created by gfc_init. So the subsequent map created in that case
2596 must have LC_RENAME reason.
2598 This latter case is not true for a preprocessed file. In that
2599 case, although the file is "initial", the line maps created by
2600 gfc_init was used during the preprocessing of the file. Now that
2601 the preprocessing is over and we are being fed the result of that
2602 preprocessing, we need to create a brand new line map for the
2603 preprocessed file, so the reason is going to be LC_ENTER. */
2605 f
= get_file (filename
, (initial
&& !preprocessed_p
) ? LC_RENAME
: LC_ENTER
);
2607 add_file_change (f
->filename
, f
->inclusion_line
);
2609 current_file
->line
= 1;
2614 if (initial
&& gfc_src_preprocessor_lines
[0])
2616 preprocessor_line (gfc_src_preprocessor_lines
[0]);
2617 free (gfc_src_preprocessor_lines
[0]);
2618 gfc_src_preprocessor_lines
[0] = NULL
;
2619 if (gfc_src_preprocessor_lines
[1])
2621 preprocessor_line (gfc_src_preprocessor_lines
[1]);
2622 free (gfc_src_preprocessor_lines
[1]);
2623 gfc_src_preprocessor_lines
[1] = NULL
;
2629 int trunc
= load_line (input
, &line
, &line_len
, NULL
);
2632 len
= gfc_wide_strlen (line
);
2633 if (feof (input
) && len
== 0)
2636 /* If this is the first line of the file, it can contain a byte
2637 order mark (BOM), which we will ignore:
2638 FF FE is UTF-16 little endian,
2639 FE FF is UTF-16 big endian,
2640 EF BB BF is UTF-8. */
2642 && ((line_len
>= 2 && line
[0] == (unsigned char) '\xFF'
2643 && line
[1] == (unsigned char) '\xFE')
2644 || (line_len
>= 2 && line
[0] == (unsigned char) '\xFE'
2645 && line
[1] == (unsigned char) '\xFF')
2646 || (line_len
>= 3 && line
[0] == (unsigned char) '\xEF'
2647 && line
[1] == (unsigned char) '\xBB'
2648 && line
[2] == (unsigned char) '\xBF')))
2650 int n
= line
[1] == (unsigned char) '\xBB' ? 3 : 2;
2651 gfc_char_t
*new_char
= gfc_get_wide_string (line_len
);
2653 wide_strcpy (new_char
, &line
[n
]);
2659 /* There are three things this line can be: a line of Fortran
2660 source, an include line or a C preprocessor directive. */
2664 /* When -g3 is specified, it's possible that we emit #define
2665 and #undef lines, which we need to pass to the middle-end
2666 so that it can emit correct debug info. */
2667 if (debug_info_level
== DINFO_LEVEL_VERBOSE
2668 && (wide_strncmp (line
, "#define ", 8) == 0
2669 || wide_strncmp (line
, "#undef ", 7) == 0))
2673 preprocessor_line (line
);
2678 /* Preprocessed files have preprocessor lines added before the byte
2679 order mark, so first_line is not about the first line of the file
2680 but the first line that's not a preprocessor line. */
2683 inc_line
= include_line (line
);
2686 current_file
->line
++;
2692 b
= XCNEWVAR (gfc_linebuf
, gfc_linebuf_header_size
2693 + (len
+ 1) * sizeof (gfc_char_t
));
2697 = linemap_line_start (line_table
, current_file
->line
++, len
);
2698 /* ??? We add the location for the maximum column possible here,
2699 because otherwise if the next call creates a new line-map, it
2700 will not reserve space for any offset. */
2702 linemap_position_for_column (line_table
, len
);
2704 b
->file
= current_file
;
2705 b
->truncated
= trunc
;
2706 wide_strcpy (b
->line
, line
);
2708 if (line_head
== NULL
)
2711 line_tail
->next
= b
;
2715 while (file_changes_cur
< file_changes_count
)
2716 file_changes
[file_changes_cur
++].lb
= b
;
2718 if (flag_dec_include
)
2720 if (include_b
&& b
!= include_b
)
2722 int inc_line2
= include_stmt (include_b
);
2725 else if (inc_line2
> 0)
2729 if (gfc_current_form
== FORM_FIXED
)
2731 for (gfc_char_t
*p
= include_b
->line
; *p
; p
++)
2735 include_b
->line
[0] = '\0';
2738 include_b
= include_b
->next
;
2744 if (inc_line
== -1 && !include_b
)
2749 /* Release the line buffer allocated in load_line. */
2755 add_file_change (NULL
, current_file
->inclusion_line
+ 1);
2756 current_file
= current_file
->up
;
2757 linemap_add (line_table
, LC_LEAVE
, 0, NULL
, 0);
2761 /* Open a new file and start scanning from that file. Returns true
2762 if everything went OK, false otherwise. If form == FORM_UNKNOWN
2763 it tries to determine the source form from the filename, defaulting
2769 if (flag_pre_include
!= NULL
)
2770 load_file (flag_pre_include
, NULL
, false);
2772 if (gfc_cpp_enabled ())
2774 gfc_cpp_preprocess (gfc_source_file
);
2775 if (!gfc_cpp_preprocess_only ())
2776 load_file (gfc_cpp_temporary_file (), gfc_source_file
, true);
2779 load_file (gfc_source_file
, NULL
, true);
2781 gfc_current_locus
.lb
= line_head
;
2782 gfc_current_locus
.nextc
= (line_head
== NULL
) ? NULL
: line_head
->line
;
2784 #if 0 /* Debugging aid. */
2785 for (; line_head
; line_head
= line_head
->next
)
2786 printf ("%s:%3d %s\n", LOCATION_FILE (line_head
->location
),
2787 LOCATION_LINE (line_head
->location
), line_head
->line
);
2789 exit (SUCCESS_EXIT_CODE
);
2794 unescape_filename (const char *ptr
)
2796 const char *p
= ptr
, *s
;
2798 int escaped
, unescape
= 0;
2800 /* Make filename end at quote. */
2802 while (*p
&& ! (! escaped
&& *p
== '"'))
2806 else if (*p
== '\\')
2817 /* Undo effects of cpp_quote_string. */
2819 d
= XCNEWVEC (char, p
+ 1 - ptr
- unescape
);
2834 /* For preprocessed files, if the first tokens are of the form # NUM.
2835 handle the directives so we know the original file name. */
2838 gfc_read_orig_filename (const char *filename
, const char **canon_source_file
)
2841 char *dirname
, *tmp
;
2843 gfc_src_file
= gfc_open_file (filename
);
2844 if (gfc_src_file
== NULL
)
2847 c
= getc (gfc_src_file
);
2853 load_line (gfc_src_file
, &gfc_src_preprocessor_lines
[0], &len
, &c
);
2855 if (wide_strncmp (gfc_src_preprocessor_lines
[0], "# 1 \"", 5) != 0)
2858 tmp
= gfc_widechar_to_char (&gfc_src_preprocessor_lines
[0][5], -1);
2859 filename
= unescape_filename (tmp
);
2861 if (filename
== NULL
)
2864 c
= getc (gfc_src_file
);
2870 load_line (gfc_src_file
, &gfc_src_preprocessor_lines
[1], &len
, &c
);
2872 if (wide_strncmp (gfc_src_preprocessor_lines
[1], "# 1 \"", 5) != 0)
2875 tmp
= gfc_widechar_to_char (&gfc_src_preprocessor_lines
[1][5], -1);
2876 dirname
= unescape_filename (tmp
);
2878 if (dirname
== NULL
)
2881 len
= strlen (dirname
);
2882 if (len
< 3 || dirname
[len
- 1] != '/' || dirname
[len
- 2] != '/')
2887 dirname
[len
- 2] = '\0';
2888 set_src_pwd (dirname
);
2890 if (! IS_ABSOLUTE_PATH (filename
))
2892 char *p
= XCNEWVEC (char, len
+ strlen (filename
));
2894 memcpy (p
, dirname
, len
- 2);
2896 strcpy (p
+ len
- 1, filename
);
2897 *canon_source_file
= p
;