]>
Commit | Line | Data |
---|---|---|
6de9cd9a | 1 | /* Character scanner. |
818ab71a | 2 | Copyright (C) 2000-2016 Free Software Foundation, Inc. |
6de9cd9a DN |
3 | Contributed by Andy Vaught |
4 | ||
9fc4d79b | 5 | This file is part of GCC. |
6de9cd9a | 6 | |
9fc4d79b TS |
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 | |
d234d788 | 9 | Software Foundation; either version 3, or (at your option) any later |
9fc4d79b | 10 | version. |
6de9cd9a | 11 | |
9fc4d79b TS |
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 | |
15 | for more details. | |
6de9cd9a DN |
16 | |
17 | You should have received a copy of the GNU General Public License | |
d234d788 NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
6de9cd9a DN |
20 | |
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. | |
25 | ||
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 | |
30 | parsing. | |
31 | ||
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. | |
35 | ||
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 | |
38 | truncated stuff. | |
39 | ||
40 | From the scanner's viewpoint, the higher level subroutines ask for | |
41 | new characters and do a lot of jumping backwards. */ | |
42 | ||
43 | #include "config.h" | |
d22e4895 | 44 | #include "system.h" |
953bee7c | 45 | #include "coretypes.h" |
6de9cd9a | 46 | #include "gfortran.h" |
7274feea | 47 | #include "toplev.h" /* For set_src_pwd. */ |
9e8a6720 | 48 | #include "debug.h" |
1916bcb5 | 49 | #include "options.h" |
670637ee | 50 | #include "cpp.h" |
070edbc2 | 51 | #include "scanner.h" |
6de9cd9a DN |
52 | |
53 | /* List of include file search directories. */ | |
070edbc2 | 54 | gfc_directorylist *include_dirs, *intrinsic_modules_dirs; |
6de9cd9a | 55 | |
d4fa05b9 | 56 | static gfc_file *file_head, *current_file; |
6de9cd9a | 57 | |
41dbbb37 TS |
58 | static int continue_flag, end_flag, gcc_attribute_flag; |
59 | /* If !$omp/!$acc occurred in current comment line. */ | |
60 | static int openmp_flag, openacc_flag; | |
5a06474c | 61 | static int continue_count, continue_line; |
6c7a4dfd | 62 | static locus openmp_locus; |
41dbbb37 | 63 | static locus openacc_locus; |
08a6b8e0 | 64 | static locus gcc_attribute_locus; |
6de9cd9a | 65 | |
d4fa05b9 TS |
66 | gfc_source_form gfc_current_form; |
67 | static gfc_linebuf *line_head, *line_tail; | |
68 | ||
63645982 | 69 | locus gfc_current_locus; |
e0bcf78c | 70 | const char *gfc_source_file; |
2d7c7df6 | 71 | static FILE *gfc_src_file; |
8fc541d3 | 72 | static gfc_char_t *gfc_src_preprocessor_lines[2]; |
2d7c7df6 | 73 | |
1b271c9b JJ |
74 | static struct gfc_file_change |
75 | { | |
76 | const char *filename; | |
77 | gfc_linebuf *lb; | |
78 | int line; | |
79 | } *file_changes; | |
80 | size_t file_changes_cur, file_changes_count; | |
81 | size_t file_changes_allocated; | |
82 | ||
8fc541d3 FXC |
83 | |
84 | /* Functions dealing with our wide characters (gfc_char_t) and | |
85 | sequences of such characters. */ | |
86 | ||
87 | int | |
88 | gfc_wide_fits_in_byte (gfc_char_t c) | |
89 | { | |
90 | return (c <= UCHAR_MAX); | |
91 | } | |
92 | ||
93 | static inline int | |
94 | wide_is_ascii (gfc_char_t c) | |
95 | { | |
96 | return (gfc_wide_fits_in_byte (c) && ((unsigned char) c & ~0x7f) == 0); | |
97 | } | |
98 | ||
99 | int | |
100 | gfc_wide_is_printable (gfc_char_t c) | |
101 | { | |
102 | return (gfc_wide_fits_in_byte (c) && ISPRINT ((unsigned char) c)); | |
103 | } | |
104 | ||
105 | gfc_char_t | |
106 | gfc_wide_tolower (gfc_char_t c) | |
107 | { | |
108 | return (wide_is_ascii (c) ? (gfc_char_t) TOLOWER((unsigned char) c) : c); | |
109 | } | |
110 | ||
00660189 FXC |
111 | gfc_char_t |
112 | gfc_wide_toupper (gfc_char_t c) | |
113 | { | |
114 | return (wide_is_ascii (c) ? (gfc_char_t) TOUPPER((unsigned char) c) : c); | |
115 | } | |
116 | ||
8fc541d3 FXC |
117 | int |
118 | gfc_wide_is_digit (gfc_char_t c) | |
119 | { | |
120 | return (c >= '0' && c <= '9'); | |
121 | } | |
122 | ||
123 | static inline int | |
124 | wide_atoi (gfc_char_t *c) | |
125 | { | |
126 | #define MAX_DIGITS 20 | |
127 | char buf[MAX_DIGITS+1]; | |
128 | int i = 0; | |
129 | ||
130 | while (gfc_wide_is_digit(*c) && i < MAX_DIGITS) | |
131 | buf[i++] = *c++; | |
132 | buf[i] = '\0'; | |
133 | return atoi (buf); | |
134 | } | |
135 | ||
136 | size_t | |
137 | gfc_wide_strlen (const gfc_char_t *str) | |
138 | { | |
139 | size_t i; | |
140 | ||
141 | for (i = 0; str[i]; i++) | |
142 | ; | |
143 | ||
144 | return i; | |
145 | } | |
146 | ||
00660189 FXC |
147 | gfc_char_t * |
148 | gfc_wide_memset (gfc_char_t *b, gfc_char_t c, size_t len) | |
149 | { | |
150 | size_t i; | |
151 | ||
152 | for (i = 0; i < len; i++) | |
153 | b[i] = c; | |
154 | ||
155 | return b; | |
156 | } | |
157 | ||
8fc541d3 FXC |
158 | static gfc_char_t * |
159 | wide_strcpy (gfc_char_t *dest, const gfc_char_t *src) | |
160 | { | |
161 | gfc_char_t *d; | |
162 | ||
163 | for (d = dest; (*d = *src) != '\0'; ++src, ++d) | |
164 | ; | |
165 | ||
166 | return dest; | |
167 | } | |
168 | ||
169 | static gfc_char_t * | |
00660189 | 170 | wide_strchr (const gfc_char_t *s, gfc_char_t c) |
8fc541d3 FXC |
171 | { |
172 | do { | |
173 | if (*s == c) | |
174 | { | |
00660189 | 175 | return CONST_CAST(gfc_char_t *, s); |
8fc541d3 FXC |
176 | } |
177 | } while (*s++); | |
178 | return 0; | |
179 | } | |
180 | ||
00660189 FXC |
181 | char * |
182 | gfc_widechar_to_char (const gfc_char_t *s, int length) | |
183 | { | |
184 | size_t len, i; | |
185 | char *res; | |
186 | ||
187 | if (s == NULL) | |
188 | return NULL; | |
189 | ||
190 | /* Passing a negative length is used to indicate that length should be | |
191 | calculated using gfc_wide_strlen(). */ | |
192 | len = (length >= 0 ? (size_t) length : gfc_wide_strlen (s)); | |
ece3f663 | 193 | res = XNEWVEC (char, len + 1); |
00660189 FXC |
194 | |
195 | for (i = 0; i < len; i++) | |
196 | { | |
197 | gcc_assert (gfc_wide_fits_in_byte (s[i])); | |
198 | res[i] = (unsigned char) s[i]; | |
199 | } | |
200 | ||
201 | res[len] = '\0'; | |
202 | return res; | |
203 | } | |
204 | ||
205 | gfc_char_t * | |
206 | gfc_char_to_widechar (const char *s) | |
8fc541d3 | 207 | { |
00660189 FXC |
208 | size_t len, i; |
209 | gfc_char_t *res; | |
210 | ||
211 | if (s == NULL) | |
212 | return NULL; | |
213 | ||
214 | len = strlen (s); | |
215 | res = gfc_get_wide_string (len + 1); | |
8fc541d3 FXC |
216 | |
217 | for (i = 0; i < len; i++) | |
00660189 | 218 | res[i] = (unsigned char) s[i]; |
8fc541d3 FXC |
219 | |
220 | res[len] = '\0'; | |
221 | return res; | |
222 | } | |
223 | ||
224 | static int | |
225 | wide_strncmp (const gfc_char_t *s1, const char *s2, size_t n) | |
226 | { | |
227 | gfc_char_t c1, c2; | |
228 | ||
229 | while (n-- > 0) | |
230 | { | |
231 | c1 = *s1++; | |
232 | c2 = *s2++; | |
233 | if (c1 != c2) | |
234 | return (c1 > c2 ? 1 : -1); | |
235 | if (c1 == '\0') | |
236 | return 0; | |
237 | } | |
238 | return 0; | |
239 | } | |
240 | ||
00660189 FXC |
241 | int |
242 | gfc_wide_strncasecmp (const gfc_char_t *s1, const char *s2, size_t n) | |
8fc541d3 FXC |
243 | { |
244 | gfc_char_t c1, c2; | |
245 | ||
246 | while (n-- > 0) | |
247 | { | |
248 | c1 = gfc_wide_tolower (*s1++); | |
249 | c2 = TOLOWER (*s2++); | |
250 | if (c1 != c2) | |
251 | return (c1 > c2 ? 1 : -1); | |
252 | if (c1 == '\0') | |
253 | return 0; | |
254 | } | |
255 | return 0; | |
256 | } | |
257 | ||
258 | ||
6de9cd9a DN |
259 | /* Main scanner initialization. */ |
260 | ||
261 | void | |
262 | gfc_scanner_init_1 (void) | |
263 | { | |
d4fa05b9 TS |
264 | file_head = NULL; |
265 | line_head = NULL; | |
266 | line_tail = NULL; | |
6de9cd9a | 267 | |
5a06474c JD |
268 | continue_count = 0; |
269 | continue_line = 0; | |
270 | ||
6de9cd9a DN |
271 | end_flag = 0; |
272 | } | |
273 | ||
274 | ||
275 | /* Main scanner destructor. */ | |
276 | ||
277 | void | |
278 | gfc_scanner_done_1 (void) | |
279 | { | |
d4fa05b9 TS |
280 | gfc_linebuf *lb; |
281 | gfc_file *f; | |
6de9cd9a | 282 | |
d4fa05b9 | 283 | while(line_head != NULL) |
6de9cd9a | 284 | { |
d4fa05b9 | 285 | lb = line_head->next; |
cede9502 | 286 | free (line_head); |
d4fa05b9 | 287 | line_head = lb; |
6de9cd9a | 288 | } |
d4fa05b9 TS |
289 | |
290 | while(file_head != NULL) | |
6de9cd9a | 291 | { |
d4fa05b9 | 292 | f = file_head->next; |
cede9502 JM |
293 | free (file_head->filename); |
294 | free (file_head); | |
d4fa05b9 | 295 | file_head = f; |
6de9cd9a DN |
296 | } |
297 | } | |
298 | ||
299 | ||
300 | /* Adds path to the list pointed to by list. */ | |
301 | ||
31198773 FXC |
302 | static void |
303 | add_path_to_list (gfc_directorylist **list, const char *path, | |
57bdf399 | 304 | bool use_for_modules, bool head, bool warn) |
6de9cd9a DN |
305 | { |
306 | gfc_directorylist *dir; | |
307 | const char *p; | |
bfc16654 | 308 | char *q; |
ff9e56a9 | 309 | struct stat st; |
bfc16654 TK |
310 | size_t len; |
311 | int i; | |
ff9e56a9 | 312 | |
6de9cd9a | 313 | p = path; |
31198773 | 314 | while (*p == ' ' || *p == '\t') /* someone might do "-I include" */ |
6de9cd9a DN |
315 | if (*p++ == '\0') |
316 | return; | |
317 | ||
bfc16654 TK |
318 | /* Strip trailing directory separators from the path, as this |
319 | will confuse Windows systems. */ | |
320 | len = strlen (p); | |
321 | q = (char *) alloca (len + 1); | |
322 | memcpy (q, p, len + 1); | |
323 | i = len - 1; | |
524af0d6 | 324 | while (i >=0 && IS_DIR_SEPARATOR (q[i])) |
bfc16654 TK |
325 | q[i--] = '\0'; |
326 | ||
327 | if (stat (q, &st)) | |
ff9e56a9 TK |
328 | { |
329 | if (errno != ENOENT) | |
db30e21c | 330 | gfc_warning_now (0, "Include directory %qs: %s", path, |
4daa149b | 331 | xstrerror(errno)); |
7c02f68b | 332 | else if (warn) |
4daa149b TB |
333 | gfc_warning_now (OPT_Wmissing_include_dirs, |
334 | "Nonexistent include directory %qs", path); | |
ff9e56a9 TK |
335 | return; |
336 | } | |
337 | else if (!S_ISDIR (st.st_mode)) | |
338 | { | |
7cc5ec65 | 339 | gfc_fatal_error ("%qs is not a directory", path); |
ff9e56a9 TK |
340 | return; |
341 | } | |
342 | ||
0ee1b105 TB |
343 | if (head || *list == NULL) |
344 | { | |
345 | dir = XCNEW (gfc_directorylist); | |
346 | if (!head) | |
347 | *list = dir; | |
348 | } | |
6de9cd9a DN |
349 | else |
350 | { | |
0ee1b105 | 351 | dir = *list; |
6de9cd9a DN |
352 | while (dir->next) |
353 | dir = dir->next; | |
354 | ||
ece3f663 | 355 | dir->next = XCNEW (gfc_directorylist); |
6de9cd9a DN |
356 | dir = dir->next; |
357 | } | |
358 | ||
0ee1b105 TB |
359 | dir->next = head ? *list : NULL; |
360 | if (head) | |
361 | *list = dir; | |
31198773 | 362 | dir->use_for_modules = use_for_modules; |
ece3f663 | 363 | dir->path = XCNEWVEC (char, strlen (p) + 2); |
6de9cd9a DN |
364 | strcpy (dir->path, p); |
365 | strcat (dir->path, "/"); /* make '/' last character */ | |
366 | } | |
367 | ||
368 | ||
31198773 | 369 | void |
308f961b TK |
370 | gfc_add_include_path (const char *path, bool use_for_modules, bool file_dir, |
371 | bool warn) | |
31198773 | 372 | { |
308f961b | 373 | add_path_to_list (&include_dirs, path, use_for_modules, file_dir, warn); |
0ee1b105 TB |
374 | |
375 | /* For '#include "..."' these directories are automatically searched. */ | |
376 | if (!file_dir) | |
377 | gfc_cpp_add_include_path (xstrdup(path), true); | |
31198773 FXC |
378 | } |
379 | ||
380 | ||
381 | void | |
382 | gfc_add_intrinsic_modules_path (const char *path) | |
383 | { | |
57bdf399 | 384 | add_path_to_list (&intrinsic_modules_dirs, path, true, false, false); |
31198773 FXC |
385 | } |
386 | ||
387 | ||
6de9cd9a DN |
388 | /* Release resources allocated for options. */ |
389 | ||
390 | void | |
391 | gfc_release_include_path (void) | |
392 | { | |
393 | gfc_directorylist *p; | |
394 | ||
6de9cd9a DN |
395 | while (include_dirs != NULL) |
396 | { | |
397 | p = include_dirs; | |
398 | include_dirs = include_dirs->next; | |
cede9502 JM |
399 | free (p->path); |
400 | free (p); | |
6de9cd9a | 401 | } |
31198773 | 402 | |
31198773 FXC |
403 | while (intrinsic_modules_dirs != NULL) |
404 | { | |
405 | p = intrinsic_modules_dirs; | |
406 | intrinsic_modules_dirs = intrinsic_modules_dirs->next; | |
cede9502 JM |
407 | free (p->path); |
408 | free (p); | |
31198773 | 409 | } |
1bc23383 | 410 | |
cede9502 | 411 | free (gfc_option.module_dir); |
6de9cd9a DN |
412 | } |
413 | ||
6de9cd9a | 414 | |
31198773 | 415 | static FILE * |
d8ddea40 DF |
416 | open_included_file (const char *name, gfc_directorylist *list, |
417 | bool module, bool system) | |
6de9cd9a | 418 | { |
200cfbe7 | 419 | char *fullname; |
6de9cd9a DN |
420 | gfc_directorylist *p; |
421 | FILE *f; | |
422 | ||
31198773 | 423 | for (p = list; p; p = p->next) |
b424a572 | 424 | { |
31198773 FXC |
425 | if (module && !p->use_for_modules) |
426 | continue; | |
6de9cd9a | 427 | |
200cfbe7 | 428 | fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1); |
6de9cd9a DN |
429 | strcpy (fullname, p->path); |
430 | strcat (fullname, name); | |
431 | ||
432 | f = gfc_open_file (fullname); | |
433 | if (f != NULL) | |
d8ddea40 DF |
434 | { |
435 | if (gfc_cpp_makedep ()) | |
436 | gfc_cpp_add_dep (fullname, system); | |
437 | ||
438 | return f; | |
439 | } | |
6de9cd9a DN |
440 | } |
441 | ||
442 | return NULL; | |
443 | } | |
444 | ||
31198773 FXC |
445 | |
446 | /* Opens file for reading, searching through the include directories | |
447 | given if necessary. If the include_cwd argument is true, we try | |
448 | to open the file in the current directory first. */ | |
449 | ||
450 | FILE * | |
451 | gfc_open_included_file (const char *name, bool include_cwd, bool module) | |
452 | { | |
d8ddea40 | 453 | FILE *f = NULL; |
e01f74e0 | 454 | |
d8ddea40 | 455 | if (IS_ABSOLUTE_PATH (name) || include_cwd) |
31198773 FXC |
456 | { |
457 | f = gfc_open_file (name); | |
d8ddea40 DF |
458 | if (f && gfc_cpp_makedep ()) |
459 | gfc_cpp_add_dep (name, false); | |
31198773 FXC |
460 | } |
461 | ||
d8ddea40 DF |
462 | if (!f) |
463 | f = open_included_file (name, include_dirs, module, false); | |
464 | ||
465 | return f; | |
31198773 FXC |
466 | } |
467 | ||
edf1eac2 | 468 | |
6de9cd9a DN |
469 | /* Test to see if we're at the end of the main source file. */ |
470 | ||
471 | int | |
472 | gfc_at_end (void) | |
473 | { | |
6de9cd9a DN |
474 | return end_flag; |
475 | } | |
476 | ||
477 | ||
478 | /* Test to see if we're at the end of the current file. */ | |
479 | ||
480 | int | |
481 | gfc_at_eof (void) | |
482 | { | |
6de9cd9a DN |
483 | if (gfc_at_end ()) |
484 | return 1; | |
485 | ||
d4fa05b9 | 486 | if (line_head == NULL) |
6de9cd9a DN |
487 | return 1; /* Null file */ |
488 | ||
63645982 | 489 | if (gfc_current_locus.lb == NULL) |
6de9cd9a DN |
490 | return 1; |
491 | ||
492 | return 0; | |
493 | } | |
494 | ||
495 | ||
496 | /* Test to see if we're at the beginning of a new line. */ | |
497 | ||
498 | int | |
499 | gfc_at_bol (void) | |
500 | { | |
6de9cd9a DN |
501 | if (gfc_at_eof ()) |
502 | return 1; | |
503 | ||
63645982 | 504 | return (gfc_current_locus.nextc == gfc_current_locus.lb->line); |
6de9cd9a DN |
505 | } |
506 | ||
507 | ||
508 | /* Test to see if we're at the end of a line. */ | |
509 | ||
510 | int | |
511 | gfc_at_eol (void) | |
512 | { | |
6de9cd9a DN |
513 | if (gfc_at_eof ()) |
514 | return 1; | |
515 | ||
63645982 | 516 | return (*gfc_current_locus.nextc == '\0'); |
6de9cd9a DN |
517 | } |
518 | ||
60332588 | 519 | static void |
1b271c9b | 520 | add_file_change (const char *filename, int line) |
60332588 | 521 | { |
1b271c9b JJ |
522 | if (file_changes_count == file_changes_allocated) |
523 | { | |
524 | if (file_changes_allocated) | |
525 | file_changes_allocated *= 2; | |
526 | else | |
527 | file_changes_allocated = 16; | |
ece3f663 KG |
528 | file_changes = XRESIZEVEC (struct gfc_file_change, file_changes, |
529 | file_changes_allocated); | |
1b271c9b JJ |
530 | } |
531 | file_changes[file_changes_count].filename = filename; | |
532 | file_changes[file_changes_count].lb = NULL; | |
533 | file_changes[file_changes_count++].line = line; | |
534 | } | |
60332588 | 535 | |
1b271c9b JJ |
536 | static void |
537 | report_file_change (gfc_linebuf *lb) | |
538 | { | |
539 | size_t c = file_changes_cur; | |
540 | while (c < file_changes_count | |
541 | && file_changes[c].lb == lb) | |
542 | { | |
543 | if (file_changes[c].filename) | |
544 | (*debug_hooks->start_source_file) (file_changes[c].line, | |
545 | file_changes[c].filename); | |
546 | else | |
547 | (*debug_hooks->end_source_file) (file_changes[c].line); | |
548 | ++c; | |
549 | } | |
550 | file_changes_cur = c; | |
60332588 JJ |
551 | } |
552 | ||
553 | void | |
554 | gfc_start_source_files (void) | |
555 | { | |
556 | /* If the debugger wants the name of the main source file, | |
557 | we give it. */ | |
558 | if (debug_hooks->start_end_main_source_file) | |
559 | (*debug_hooks->start_source_file) (0, gfc_source_file); | |
560 | ||
1b271c9b JJ |
561 | file_changes_cur = 0; |
562 | report_file_change (gfc_current_locus.lb); | |
60332588 JJ |
563 | } |
564 | ||
565 | void | |
566 | gfc_end_source_files (void) | |
567 | { | |
1b271c9b | 568 | report_file_change (NULL); |
60332588 JJ |
569 | |
570 | if (debug_hooks->start_end_main_source_file) | |
571 | (*debug_hooks->end_source_file) (0); | |
572 | } | |
6de9cd9a DN |
573 | |
574 | /* Advance the current line pointer to the next line. */ | |
575 | ||
576 | void | |
577 | gfc_advance_line (void) | |
578 | { | |
6de9cd9a | 579 | if (gfc_at_end ()) |
4a58b9ad | 580 | return; |
6de9cd9a | 581 | |
63645982 | 582 | if (gfc_current_locus.lb == NULL) |
6de9cd9a | 583 | { |
d4fa05b9 TS |
584 | end_flag = 1; |
585 | return; | |
586 | } | |
6de9cd9a | 587 | |
9e8a6720 | 588 | if (gfc_current_locus.lb->next |
60332588 | 589 | && !gfc_current_locus.lb->next->dbg_emitted) |
9e8a6720 | 590 | { |
1b271c9b | 591 | report_file_change (gfc_current_locus.lb->next); |
60332588 | 592 | gfc_current_locus.lb->next->dbg_emitted = true; |
9e8a6720 FXC |
593 | } |
594 | ||
63645982 | 595 | gfc_current_locus.lb = gfc_current_locus.lb->next; |
6de9cd9a | 596 | |
edf1eac2 | 597 | if (gfc_current_locus.lb != NULL) |
63645982 | 598 | gfc_current_locus.nextc = gfc_current_locus.lb->line; |
d4fa05b9 TS |
599 | else |
600 | { | |
63645982 | 601 | gfc_current_locus.nextc = NULL; |
d4fa05b9 TS |
602 | end_flag = 1; |
603 | } | |
6de9cd9a DN |
604 | } |
605 | ||
606 | ||
607 | /* Get the next character from the input, advancing gfc_current_file's | |
608 | locus. When we hit the end of the line or the end of the file, we | |
609 | start returning a '\n' in order to complete the current statement. | |
610 | No Fortran line conventions are implemented here. | |
611 | ||
612 | Requiring explicit advances to the next line prevents the parse | |
613 | pointer from being on the wrong line if the current statement ends | |
614 | prematurely. */ | |
615 | ||
8fc541d3 | 616 | static gfc_char_t |
6de9cd9a DN |
617 | next_char (void) |
618 | { | |
8fc541d3 | 619 | gfc_char_t c; |
d4fa05b9 | 620 | |
63645982 | 621 | if (gfc_current_locus.nextc == NULL) |
6de9cd9a DN |
622 | return '\n'; |
623 | ||
8fc541d3 | 624 | c = *gfc_current_locus.nextc++; |
6de9cd9a DN |
625 | if (c == '\0') |
626 | { | |
63645982 | 627 | gfc_current_locus.nextc--; /* Remain on this line. */ |
6de9cd9a DN |
628 | c = '\n'; |
629 | } | |
630 | ||
631 | return c; | |
632 | } | |
633 | ||
edf1eac2 | 634 | |
6de9cd9a DN |
635 | /* Skip a comment. When we come here the parse pointer is positioned |
636 | immediately after the comment character. If we ever implement | |
9cd38d51 | 637 | compiler directives within comments, here is where we parse the |
6de9cd9a DN |
638 | directive. */ |
639 | ||
640 | static void | |
641 | skip_comment_line (void) | |
642 | { | |
8fc541d3 | 643 | gfc_char_t c; |
6de9cd9a DN |
644 | |
645 | do | |
646 | { | |
647 | c = next_char (); | |
648 | } | |
649 | while (c != '\n'); | |
650 | ||
651 | gfc_advance_line (); | |
652 | } | |
653 | ||
654 | ||
9e8a6720 FXC |
655 | int |
656 | gfc_define_undef_line (void) | |
657 | { | |
8fc541d3 FXC |
658 | char *tmp; |
659 | ||
9e8a6720 | 660 | /* All lines beginning with '#' are either #define or #undef. */ |
8fc541d3 | 661 | if (debug_info_level != DINFO_LEVEL_VERBOSE || gfc_peek_ascii_char () != '#') |
9e8a6720 FXC |
662 | return 0; |
663 | ||
8fc541d3 FXC |
664 | if (wide_strncmp (gfc_current_locus.nextc, "#define ", 8) == 0) |
665 | { | |
00660189 | 666 | tmp = gfc_widechar_to_char (&gfc_current_locus.nextc[8], -1); |
8fc541d3 FXC |
667 | (*debug_hooks->define) (gfc_linebuf_linenum (gfc_current_locus.lb), |
668 | tmp); | |
cede9502 | 669 | free (tmp); |
8fc541d3 | 670 | } |
9e8a6720 | 671 | |
8fc541d3 FXC |
672 | if (wide_strncmp (gfc_current_locus.nextc, "#undef ", 7) == 0) |
673 | { | |
00660189 | 674 | tmp = gfc_widechar_to_char (&gfc_current_locus.nextc[7], -1); |
8fc541d3 FXC |
675 | (*debug_hooks->undef) (gfc_linebuf_linenum (gfc_current_locus.lb), |
676 | tmp); | |
cede9502 | 677 | free (tmp); |
8fc541d3 | 678 | } |
9e8a6720 FXC |
679 | |
680 | /* Skip the rest of the line. */ | |
681 | skip_comment_line (); | |
682 | ||
683 | return 1; | |
684 | } | |
685 | ||
686 | ||
08a6b8e0 TB |
687 | /* Return true if GCC$ was matched. */ |
688 | static bool | |
689 | skip_gcc_attribute (locus start) | |
690 | { | |
691 | bool r = false; | |
692 | char c; | |
693 | locus old_loc = gfc_current_locus; | |
694 | ||
695 | if ((c = next_char ()) == 'g' || c == 'G') | |
696 | if ((c = next_char ()) == 'c' || c == 'C') | |
697 | if ((c = next_char ()) == 'c' || c == 'C') | |
698 | if ((c = next_char ()) == '$') | |
699 | r = true; | |
700 | ||
701 | if (r == false) | |
702 | gfc_current_locus = old_loc; | |
703 | else | |
704 | { | |
705 | gcc_attribute_flag = 1; | |
706 | gcc_attribute_locus = old_loc; | |
707 | gfc_current_locus = start; | |
708 | } | |
709 | ||
710 | return r; | |
711 | } | |
712 | ||
41dbbb37 TS |
713 | /* Return true if CC was matched. */ |
714 | static bool | |
aa81272c | 715 | skip_free_oacc_sentinel (locus start, locus old_loc) |
41dbbb37 TS |
716 | { |
717 | bool r = false; | |
718 | char c; | |
719 | ||
720 | if ((c = next_char ()) == 'c' || c == 'C') | |
721 | if ((c = next_char ()) == 'c' || c == 'C') | |
722 | r = true; | |
723 | ||
724 | if (r) | |
725 | { | |
726 | if ((c = next_char ()) == ' ' || c == '\t' | |
727 | || continue_flag) | |
728 | { | |
729 | while (gfc_is_whitespace (c)) | |
730 | c = next_char (); | |
731 | if (c != '\n' && c != '!') | |
732 | { | |
733 | openacc_flag = 1; | |
734 | openacc_locus = old_loc; | |
735 | gfc_current_locus = start; | |
736 | } | |
737 | else | |
738 | r = false; | |
739 | } | |
740 | else | |
741 | { | |
db30e21c | 742 | gfc_warning_now (0, "!$ACC at %C starts a commented " |
41dbbb37 TS |
743 | "line as it neither is followed " |
744 | "by a space nor is a " | |
745 | "continuation line"); | |
746 | r = false; | |
747 | } | |
748 | } | |
749 | ||
750 | return r; | |
751 | } | |
752 | ||
753 | /* Return true if MP was matched. */ | |
754 | static bool | |
aa81272c | 755 | skip_free_omp_sentinel (locus start, locus old_loc) |
41dbbb37 TS |
756 | { |
757 | bool r = false; | |
758 | char c; | |
759 | ||
760 | if ((c = next_char ()) == 'm' || c == 'M') | |
761 | if ((c = next_char ()) == 'p' || c == 'P') | |
762 | r = true; | |
763 | ||
764 | if (r) | |
765 | { | |
766 | if ((c = next_char ()) == ' ' || c == '\t' | |
767 | || continue_flag) | |
768 | { | |
769 | while (gfc_is_whitespace (c)) | |
770 | c = next_char (); | |
771 | if (c != '\n' && c != '!') | |
772 | { | |
773 | openmp_flag = 1; | |
774 | openmp_locus = old_loc; | |
775 | gfc_current_locus = start; | |
776 | } | |
777 | else | |
778 | r = false; | |
779 | } | |
780 | else | |
781 | { | |
db30e21c | 782 | gfc_warning_now (0, "!$OMP at %C starts a commented " |
41dbbb37 TS |
783 | "line as it neither is followed " |
784 | "by a space nor is a " | |
785 | "continuation line"); | |
786 | r = false; | |
787 | } | |
788 | } | |
08a6b8e0 | 789 | |
41dbbb37 TS |
790 | return r; |
791 | } | |
08a6b8e0 | 792 | |
6de9cd9a | 793 | /* Comment lines are null lines, lines containing only blanks or lines |
0d3abf6f | 794 | on which the first nonblank line is a '!'. |
41dbbb37 | 795 | Return true if !$ openmp or openacc conditional compilation sentinel was |
0d3abf6f | 796 | seen. */ |
6de9cd9a | 797 | |
0d3abf6f | 798 | static bool |
6de9cd9a DN |
799 | skip_free_comments (void) |
800 | { | |
801 | locus start; | |
8fc541d3 | 802 | gfc_char_t c; |
6c7a4dfd | 803 | int at_bol; |
6de9cd9a DN |
804 | |
805 | for (;;) | |
806 | { | |
6c7a4dfd | 807 | at_bol = gfc_at_bol (); |
63645982 | 808 | start = gfc_current_locus; |
6de9cd9a DN |
809 | if (gfc_at_eof ()) |
810 | break; | |
811 | ||
812 | do | |
6c7a4dfd | 813 | c = next_char (); |
6de9cd9a DN |
814 | while (gfc_is_whitespace (c)); |
815 | ||
816 | if (c == '\n') | |
817 | { | |
818 | gfc_advance_line (); | |
819 | continue; | |
820 | } | |
821 | ||
822 | if (c == '!') | |
823 | { | |
08a6b8e0 | 824 | /* Keep the !GCC$ line. */ |
90c4f6ba | 825 | if (at_bol && skip_gcc_attribute (start)) |
08a6b8e0 TB |
826 | return false; |
827 | ||
41dbbb37 TS |
828 | /* If -fopenmp/-fopenacc, we need to handle here 2 things: |
829 | 1) don't treat !$omp/!$acc as comments, but directives | |
830 | 2) handle OpenMP/OpenACC conditional compilation, where | |
6c7a4dfd JJ |
831 | !$ should be treated as 2 spaces (for initial lines |
832 | only if followed by space). */ | |
41dbbb37 TS |
833 | if (at_bol) |
834 | { | |
835 | if ((flag_openmp || flag_openmp_simd) | |
836 | && flag_openacc) | |
837 | { | |
838 | locus old_loc = gfc_current_locus; | |
839 | if (next_char () == '$') | |
840 | { | |
841 | c = next_char (); | |
842 | if (c == 'o' || c == 'O') | |
843 | { | |
aa81272c | 844 | if (skip_free_omp_sentinel (start, old_loc)) |
41dbbb37 TS |
845 | return false; |
846 | gfc_current_locus = old_loc; | |
847 | next_char (); | |
848 | c = next_char (); | |
849 | } | |
850 | else if (c == 'a' || c == 'A') | |
851 | { | |
aa81272c | 852 | if (skip_free_oacc_sentinel (start, old_loc)) |
41dbbb37 TS |
853 | return false; |
854 | gfc_current_locus = old_loc; | |
855 | next_char (); | |
856 | c = next_char (); | |
857 | } | |
858 | if (continue_flag || c == ' ' || c == '\t') | |
859 | { | |
860 | gfc_current_locus = old_loc; | |
861 | next_char (); | |
862 | openmp_flag = openacc_flag = 0; | |
863 | return true; | |
864 | } | |
865 | } | |
866 | gfc_current_locus = old_loc; | |
867 | } | |
868 | else if ((flag_openmp || flag_openmp_simd) | |
869 | && !flag_openacc) | |
870 | { | |
871 | locus old_loc = gfc_current_locus; | |
872 | if (next_char () == '$') | |
873 | { | |
874 | c = next_char (); | |
875 | if (c == 'o' || c == 'O') | |
876 | { | |
aa81272c | 877 | if (skip_free_omp_sentinel (start, old_loc)) |
41dbbb37 TS |
878 | return false; |
879 | gfc_current_locus = old_loc; | |
880 | next_char (); | |
881 | c = next_char (); | |
882 | } | |
883 | if (continue_flag || c == ' ' || c == '\t') | |
884 | { | |
885 | gfc_current_locus = old_loc; | |
886 | next_char (); | |
887 | openmp_flag = 0; | |
888 | return true; | |
889 | } | |
890 | } | |
891 | gfc_current_locus = old_loc; | |
892 | } | |
893 | else if (flag_openacc | |
894 | && !(flag_openmp || flag_openmp_simd)) | |
895 | { | |
896 | locus old_loc = gfc_current_locus; | |
897 | if (next_char () == '$') | |
898 | { | |
899 | c = next_char (); | |
900 | if (c == 'a' || c == 'A') | |
6c7a4dfd | 901 | { |
aa81272c | 902 | if (skip_free_oacc_sentinel (start, old_loc)) |
41dbbb37 TS |
903 | return false; |
904 | gfc_current_locus = old_loc; | |
905 | next_char(); | |
906 | c = next_char(); | |
6c7a4dfd | 907 | } |
41dbbb37 TS |
908 | if (continue_flag || c == ' ' || c == '\t') |
909 | { | |
910 | gfc_current_locus = old_loc; | |
911 | next_char(); | |
912 | openacc_flag = 0; | |
913 | return true; | |
914 | } | |
915 | } | |
916 | gfc_current_locus = old_loc; | |
917 | } | |
918 | } | |
6de9cd9a DN |
919 | skip_comment_line (); |
920 | continue; | |
921 | } | |
922 | ||
923 | break; | |
924 | } | |
925 | ||
6c7a4dfd JJ |
926 | if (openmp_flag && at_bol) |
927 | openmp_flag = 0; | |
08a6b8e0 | 928 | |
41dbbb37 TS |
929 | if (openacc_flag && at_bol) |
930 | openacc_flag = 0; | |
931 | ||
08a6b8e0 | 932 | gcc_attribute_flag = 0; |
63645982 | 933 | gfc_current_locus = start; |
0d3abf6f | 934 | return false; |
6de9cd9a DN |
935 | } |
936 | ||
aa81272c IU |
937 | /* Return true if MP was matched in fixed form. */ |
938 | static bool | |
939 | skip_fixed_omp_sentinel (locus *start) | |
940 | { | |
941 | gfc_char_t c; | |
942 | if (((c = next_char ()) == 'm' || c == 'M') | |
943 | && ((c = next_char ()) == 'p' || c == 'P')) | |
944 | { | |
945 | c = next_char (); | |
946 | if (c != '\n' | |
947 | && (continue_flag | |
948 | || c == ' ' || c == '\t' || c == '0')) | |
949 | { | |
950 | do | |
951 | c = next_char (); | |
952 | while (gfc_is_whitespace (c)); | |
953 | if (c != '\n' && c != '!') | |
954 | { | |
955 | /* Canonicalize to *$omp. */ | |
956 | *start->nextc = '*'; | |
957 | openmp_flag = 1; | |
958 | gfc_current_locus = *start; | |
959 | return true; | |
960 | } | |
961 | } | |
962 | } | |
963 | return false; | |
964 | } | |
965 | ||
966 | /* Return true if CC was matched in fixed form. */ | |
967 | static bool | |
968 | skip_fixed_oacc_sentinel (locus *start) | |
969 | { | |
970 | gfc_char_t c; | |
971 | if (((c = next_char ()) == 'c' || c == 'C') | |
972 | && ((c = next_char ()) == 'c' || c == 'C')) | |
973 | { | |
974 | c = next_char (); | |
975 | if (c != '\n' | |
976 | && (continue_flag | |
977 | || c == ' ' || c == '\t' || c == '0')) | |
978 | { | |
979 | do | |
980 | c = next_char (); | |
981 | while (gfc_is_whitespace (c)); | |
982 | if (c != '\n' && c != '!') | |
983 | { | |
984 | /* Canonicalize to *$acc. */ | |
985 | *start->nextc = '*'; | |
986 | openacc_flag = 1; | |
987 | gfc_current_locus = *start; | |
988 | return true; | |
989 | } | |
990 | } | |
991 | } | |
992 | return false; | |
993 | } | |
6de9cd9a DN |
994 | |
995 | /* Skip comment lines in fixed source mode. We have the same rules as | |
996 | in skip_free_comment(), except that we can have a 'c', 'C' or '*' | |
e0bcf78c TS |
997 | in column 1, and a '!' cannot be in column 6. Also, we deal with |
998 | lines with 'd' or 'D' in column 1, if the user requested this. */ | |
6de9cd9a DN |
999 | |
1000 | static void | |
1001 | skip_fixed_comments (void) | |
1002 | { | |
1003 | locus start; | |
1004 | int col; | |
8fc541d3 | 1005 | gfc_char_t c; |
6de9cd9a | 1006 | |
6c7a4dfd JJ |
1007 | if (! gfc_at_bol ()) |
1008 | { | |
1009 | start = gfc_current_locus; | |
1010 | if (! gfc_at_eof ()) | |
1011 | { | |
1012 | do | |
1013 | c = next_char (); | |
1014 | while (gfc_is_whitespace (c)); | |
1015 | ||
1016 | if (c == '\n') | |
1017 | gfc_advance_line (); | |
1018 | else if (c == '!') | |
1019 | skip_comment_line (); | |
1020 | } | |
1021 | ||
1022 | if (! gfc_at_bol ()) | |
1023 | { | |
1024 | gfc_current_locus = start; | |
1025 | return; | |
1026 | } | |
1027 | } | |
1028 | ||
6de9cd9a DN |
1029 | for (;;) |
1030 | { | |
63645982 | 1031 | start = gfc_current_locus; |
6de9cd9a DN |
1032 | if (gfc_at_eof ()) |
1033 | break; | |
1034 | ||
1035 | c = next_char (); | |
1036 | if (c == '\n') | |
1037 | { | |
1038 | gfc_advance_line (); | |
1039 | continue; | |
1040 | } | |
1041 | ||
1042 | if (c == '!' || c == 'c' || c == 'C' || c == '*') | |
1043 | { | |
08a6b8e0 TB |
1044 | if (skip_gcc_attribute (start)) |
1045 | { | |
1046 | /* Canonicalize to *$omp. */ | |
1047 | *start.nextc = '*'; | |
1048 | return; | |
1049 | } | |
1050 | ||
41dbbb37 TS |
1051 | /* If -fopenmp/-fopenacc, we need to handle here 2 things: |
1052 | 1) don't treat !$omp/!$acc|c$omp/c$acc|*$omp / *$acc as comments, | |
1053 | but directives | |
1054 | 2) handle OpenMP/OpenACC conditional compilation, where | |
6c7a4dfd JJ |
1055 | !$|c$|*$ should be treated as 2 spaces if the characters |
1056 | in columns 3 to 6 are valid fixed form label columns | |
1057 | characters. */ | |
f449022d JD |
1058 | if (gfc_current_locus.lb != NULL |
1059 | && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb)) | |
1060 | continue_line = gfc_linebuf_linenum (gfc_current_locus.lb); | |
1061 | ||
aa81272c | 1062 | if ((flag_openmp || flag_openmp_simd) && !flag_openacc) |
6c7a4dfd JJ |
1063 | { |
1064 | if (next_char () == '$') | |
1065 | { | |
1066 | c = next_char (); | |
1067 | if (c == 'o' || c == 'O') | |
1068 | { | |
aa81272c IU |
1069 | if (skip_fixed_omp_sentinel (&start)) |
1070 | return; | |
6c7a4dfd JJ |
1071 | } |
1072 | else | |
aa81272c IU |
1073 | goto check_for_digits; |
1074 | } | |
1075 | gfc_current_locus = start; | |
1076 | } | |
1077 | ||
1078 | if (flag_openacc && !(flag_openmp || flag_openmp_simd)) | |
1079 | { | |
1080 | if (next_char () == '$') | |
1081 | { | |
1082 | c = next_char (); | |
1083 | if (c == 'a' || c == 'A') | |
6c7a4dfd | 1084 | { |
aa81272c IU |
1085 | if (skip_fixed_oacc_sentinel (&start)) |
1086 | return; | |
6c7a4dfd | 1087 | } |
aa81272c IU |
1088 | else |
1089 | goto check_for_digits; | |
6c7a4dfd JJ |
1090 | } |
1091 | gfc_current_locus = start; | |
1092 | } | |
41dbbb37 | 1093 | |
aa81272c | 1094 | if (flag_openacc || flag_openmp || flag_openmp_simd) |
41dbbb37 TS |
1095 | { |
1096 | if (next_char () == '$') | |
1097 | { | |
1098 | c = next_char (); | |
1099 | if (c == 'a' || c == 'A') | |
1100 | { | |
aa81272c IU |
1101 | if (skip_fixed_oacc_sentinel (&start)) |
1102 | return; | |
41dbbb37 | 1103 | } |
aa81272c | 1104 | else if (c == 'o' || c == 'O') |
41dbbb37 | 1105 | { |
aa81272c IU |
1106 | if (skip_fixed_omp_sentinel (&start)) |
1107 | return; | |
41dbbb37 | 1108 | } |
aa81272c IU |
1109 | else |
1110 | goto check_for_digits; | |
41dbbb37 TS |
1111 | } |
1112 | gfc_current_locus = start; | |
1113 | } | |
1114 | ||
6de9cd9a DN |
1115 | skip_comment_line (); |
1116 | continue; | |
aa81272c IU |
1117 | |
1118 | gcc_unreachable (); | |
1119 | check_for_digits: | |
1120 | { | |
1121 | int digit_seen = 0; | |
1122 | ||
1123 | for (col = 3; col < 6; col++, c = next_char ()) | |
1124 | if (c == ' ') | |
1125 | continue; | |
1126 | else if (c == '\t') | |
1127 | { | |
1128 | col = 6; | |
1129 | break; | |
1130 | } | |
1131 | else if (c < '0' || c > '9') | |
1132 | break; | |
1133 | else | |
1134 | digit_seen = 1; | |
1135 | ||
1136 | if (col == 6 && c != '\n' | |
1137 | && ((continue_flag && !digit_seen) | |
1138 | || c == ' ' || c == '\t' || c == '0')) | |
1139 | { | |
1140 | gfc_current_locus = start; | |
1141 | start.nextc[0] = ' '; | |
1142 | start.nextc[1] = ' '; | |
1143 | continue; | |
1144 | } | |
1145 | } | |
1146 | skip_comment_line (); | |
1147 | continue; | |
6de9cd9a DN |
1148 | } |
1149 | ||
e0bcf78c TS |
1150 | if (gfc_option.flag_d_lines != -1 && (c == 'd' || c == 'D')) |
1151 | { | |
1152 | if (gfc_option.flag_d_lines == 0) | |
1153 | { | |
1154 | skip_comment_line (); | |
1155 | continue; | |
1156 | } | |
1157 | else | |
1158 | *start.nextc = c = ' '; | |
1159 | } | |
1160 | ||
6de9cd9a | 1161 | col = 1; |
e0bcf78c TS |
1162 | |
1163 | while (gfc_is_whitespace (c)) | |
6de9cd9a DN |
1164 | { |
1165 | c = next_char (); | |
1166 | col++; | |
1167 | } | |
6de9cd9a DN |
1168 | |
1169 | if (c == '\n') | |
1170 | { | |
1171 | gfc_advance_line (); | |
1172 | continue; | |
1173 | } | |
1174 | ||
1175 | if (col != 6 && c == '!') | |
1176 | { | |
f449022d JD |
1177 | if (gfc_current_locus.lb != NULL |
1178 | && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb)) | |
1179 | continue_line = gfc_linebuf_linenum (gfc_current_locus.lb); | |
6de9cd9a DN |
1180 | skip_comment_line (); |
1181 | continue; | |
1182 | } | |
1183 | ||
1184 | break; | |
1185 | } | |
1186 | ||
6c7a4dfd | 1187 | openmp_flag = 0; |
41dbbb37 | 1188 | openacc_flag = 0; |
08a6b8e0 | 1189 | gcc_attribute_flag = 0; |
63645982 | 1190 | gfc_current_locus = start; |
6de9cd9a DN |
1191 | } |
1192 | ||
1193 | ||
6c7a4dfd | 1194 | /* Skips the current line if it is a comment. */ |
6de9cd9a DN |
1195 | |
1196 | void | |
1197 | gfc_skip_comments (void) | |
1198 | { | |
6c7a4dfd | 1199 | if (gfc_current_form == FORM_FREE) |
6de9cd9a DN |
1200 | skip_free_comments (); |
1201 | else | |
1202 | skip_fixed_comments (); | |
1203 | } | |
1204 | ||
1205 | ||
1206 | /* Get the next character from the input, taking continuation lines | |
1207 | and end-of-line comments into account. This implies that comment | |
1208 | lines between continued lines must be eaten here. For higher-level | |
1209 | subroutines, this flattens continued lines into a single logical | |
1210 | line. The in_string flag denotes whether we're inside a character | |
1211 | context or not. */ | |
1212 | ||
8fc541d3 | 1213 | gfc_char_t |
696abb30 | 1214 | gfc_next_char_literal (gfc_instring in_string) |
6de9cd9a DN |
1215 | { |
1216 | locus old_loc; | |
41dbbb37 | 1217 | int i, prev_openmp_flag, prev_openacc_flag; |
8fc541d3 | 1218 | gfc_char_t c; |
6de9cd9a DN |
1219 | |
1220 | continue_flag = 0; | |
41dbbb37 | 1221 | prev_openacc_flag = prev_openmp_flag = 0; |
6de9cd9a DN |
1222 | |
1223 | restart: | |
1224 | c = next_char (); | |
1225 | if (gfc_at_end ()) | |
5a06474c JD |
1226 | { |
1227 | continue_count = 0; | |
1228 | return c; | |
1229 | } | |
6de9cd9a | 1230 | |
d4fa05b9 | 1231 | if (gfc_current_form == FORM_FREE) |
6de9cd9a | 1232 | { |
0d3abf6f JJ |
1233 | bool openmp_cond_flag; |
1234 | ||
6de9cd9a DN |
1235 | if (!in_string && c == '!') |
1236 | { | |
08a6b8e0 TB |
1237 | if (gcc_attribute_flag |
1238 | && memcmp (&gfc_current_locus, &gcc_attribute_locus, | |
1239 | sizeof (gfc_current_locus)) == 0) | |
1240 | goto done; | |
1241 | ||
6c7a4dfd JJ |
1242 | if (openmp_flag |
1243 | && memcmp (&gfc_current_locus, &openmp_locus, | |
1244 | sizeof (gfc_current_locus)) == 0) | |
1245 | goto done; | |
1246 | ||
41dbbb37 TS |
1247 | if (openacc_flag |
1248 | && memcmp (&gfc_current_locus, &openacc_locus, | |
1249 | sizeof (gfc_current_locus)) == 0) | |
1250 | goto done; | |
1251 | ||
6de9cd9a DN |
1252 | /* This line can't be continued */ |
1253 | do | |
1254 | { | |
1255 | c = next_char (); | |
1256 | } | |
1257 | while (c != '\n'); | |
1258 | ||
a34938be RG |
1259 | /* Avoid truncation warnings for comment ending lines. */ |
1260 | gfc_current_locus.lb->truncated = 0; | |
1261 | ||
6de9cd9a DN |
1262 | goto done; |
1263 | } | |
1264 | ||
ac64eec4 | 1265 | /* Check to see if the continuation line was truncated. */ |
73e42eef | 1266 | if (warn_line_truncation && gfc_current_locus.lb != NULL |
ac64eec4 JD |
1267 | && gfc_current_locus.lb->truncated) |
1268 | { | |
203c7ebf | 1269 | int maxlen = flag_free_line_length; |
021aa628 TB |
1270 | gfc_char_t *current_nextc = gfc_current_locus.nextc; |
1271 | ||
ac64eec4 | 1272 | gfc_current_locus.lb->truncated = 0; |
021aa628 | 1273 | gfc_current_locus.nextc = gfc_current_locus.lb->line + maxlen; |
b93c0722 MLI |
1274 | gfc_warning_now (OPT_Wline_truncation, |
1275 | "Line truncated at %L", &gfc_current_locus); | |
021aa628 | 1276 | gfc_current_locus.nextc = current_nextc; |
ac64eec4 JD |
1277 | } |
1278 | ||
6de9cd9a DN |
1279 | if (c != '&') |
1280 | goto done; | |
1281 | ||
1282 | /* If the next nonblank character is a ! or \n, we've got a | |
6c7a4dfd | 1283 | continuation line. */ |
63645982 | 1284 | old_loc = gfc_current_locus; |
6de9cd9a DN |
1285 | |
1286 | c = next_char (); | |
1287 | while (gfc_is_whitespace (c)) | |
1288 | c = next_char (); | |
1289 | ||
1290 | /* Character constants to be continued cannot have commentary | |
0d6fc963 JD |
1291 | after the '&'. However, there are cases where we may think we |
1292 | are still in a string and we are looking for a possible | |
1293 | doubled quote and we end up here. See PR64506. */ | |
6de9cd9a | 1294 | |
c55bbc72 | 1295 | if (in_string && c != '\n') |
6de9cd9a | 1296 | { |
63645982 | 1297 | gfc_current_locus = old_loc; |
c55bbc72 JD |
1298 | c = '&'; |
1299 | goto done; | |
6de9cd9a DN |
1300 | } |
1301 | ||
1302 | if (c != '!' && c != '\n') | |
1303 | { | |
63645982 | 1304 | gfc_current_locus = old_loc; |
6de9cd9a DN |
1305 | c = '&'; |
1306 | goto done; | |
1307 | } | |
1308 | ||
41dbbb37 TS |
1309 | if (flag_openmp) |
1310 | prev_openmp_flag = openmp_flag; | |
1311 | if (flag_openacc) | |
1312 | prev_openacc_flag = openacc_flag; | |
1313 | ||
6de9cd9a DN |
1314 | continue_flag = 1; |
1315 | if (c == '!') | |
1316 | skip_comment_line (); | |
1317 | else | |
1318 | gfc_advance_line (); | |
0267ffdc | 1319 | |
524af0d6 | 1320 | if (gfc_at_eof ()) |
0267ffdc | 1321 | goto not_continuation; |
6de9cd9a | 1322 | |
5a06474c JD |
1323 | /* We've got a continuation line. If we are on the very next line after |
1324 | the last continuation, increment the continuation line count and | |
1325 | check whether the limit has been exceeded. */ | |
5ffeb913 | 1326 | if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1) |
5a06474c JD |
1327 | { |
1328 | if (++continue_count == gfc_option.max_continue_free) | |
1329 | { | |
edf1eac2 | 1330 | if (gfc_notification_std (GFC_STD_GNU) || pedantic) |
db30e21c | 1331 | gfc_warning (0, "Limit of %d continuations exceeded in " |
edf1eac2 | 1332 | "statement at %C", gfc_option.max_continue_free); |
5a06474c JD |
1333 | } |
1334 | } | |
5a06474c JD |
1335 | |
1336 | /* Now find where it continues. First eat any comment lines. */ | |
0d3abf6f | 1337 | openmp_cond_flag = skip_free_comments (); |
6de9cd9a | 1338 | |
f449022d JD |
1339 | if (gfc_current_locus.lb != NULL |
1340 | && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb)) | |
1341 | continue_line = gfc_linebuf_linenum (gfc_current_locus.lb); | |
1342 | ||
41dbbb37 | 1343 | if (flag_openmp) |
aa81272c | 1344 | if (prev_openmp_flag != openmp_flag && !openacc_flag) |
41dbbb37 TS |
1345 | { |
1346 | gfc_current_locus = old_loc; | |
1347 | openmp_flag = prev_openmp_flag; | |
1348 | c = '&'; | |
1349 | goto done; | |
1350 | } | |
1351 | ||
1352 | if (flag_openacc) | |
aa81272c | 1353 | if (prev_openacc_flag != openacc_flag && !openmp_flag) |
41dbbb37 TS |
1354 | { |
1355 | gfc_current_locus = old_loc; | |
1356 | openacc_flag = prev_openacc_flag; | |
1357 | c = '&'; | |
1358 | goto done; | |
1359 | } | |
6c7a4dfd | 1360 | |
6de9cd9a | 1361 | /* Now that we have a non-comment line, probe ahead for the |
6c7a4dfd JJ |
1362 | first non-whitespace character. If it is another '&', then |
1363 | reading starts at the next character, otherwise we must back | |
1364 | up to where the whitespace started and resume from there. */ | |
6de9cd9a | 1365 | |
63645982 | 1366 | old_loc = gfc_current_locus; |
6de9cd9a DN |
1367 | |
1368 | c = next_char (); | |
1369 | while (gfc_is_whitespace (c)) | |
1370 | c = next_char (); | |
1371 | ||
aa81272c | 1372 | if (openmp_flag && !openacc_flag) |
6c7a4dfd JJ |
1373 | { |
1374 | for (i = 0; i < 5; i++, c = next_char ()) | |
1375 | { | |
8fc541d3 | 1376 | gcc_assert (gfc_wide_tolower (c) == (unsigned char) "!$omp"[i]); |
6c7a4dfd JJ |
1377 | if (i == 4) |
1378 | old_loc = gfc_current_locus; | |
1379 | } | |
1380 | while (gfc_is_whitespace (c)) | |
1381 | c = next_char (); | |
1382 | } | |
aa81272c | 1383 | if (openacc_flag && !openmp_flag) |
41dbbb37 TS |
1384 | { |
1385 | for (i = 0; i < 5; i++, c = next_char ()) | |
1386 | { | |
1387 | gcc_assert (gfc_wide_tolower (c) == (unsigned char) "!$acc"[i]); | |
1388 | if (i == 4) | |
1389 | old_loc = gfc_current_locus; | |
1390 | } | |
1391 | while (gfc_is_whitespace (c)) | |
1392 | c = next_char (); | |
1393 | } | |
6c7a4dfd | 1394 | |
aa81272c IU |
1395 | /* In case we have an OpenMP directive continued by OpenACC |
1396 | sentinel, or vice versa, we get both openmp_flag and | |
1397 | openacc_flag on. */ | |
1398 | ||
1399 | if (openacc_flag && openmp_flag) | |
1400 | { | |
1401 | int is_openmp = 0; | |
1402 | for (i = 0; i < 5; i++, c = next_char ()) | |
1403 | { | |
1404 | if (gfc_wide_tolower (c) != (unsigned char) "!$acc"[i]) | |
1405 | is_openmp = 1; | |
1406 | if (i == 4) | |
1407 | old_loc = gfc_current_locus; | |
1408 | } | |
1409 | gfc_error (is_openmp ? "Wrong OpenACC continuation at %C: " | |
1410 | "expected !$ACC, got !$OMP" | |
1411 | : "Wrong OpenMP continuation at %C: " | |
1412 | "expected !$OMP, got !$ACC"); | |
1413 | } | |
1414 | ||
6de9cd9a | 1415 | if (c != '&') |
3fbab549 | 1416 | { |
5a06474c JD |
1417 | if (in_string) |
1418 | { | |
5a06474c | 1419 | gfc_current_locus.nextc--; |
73e42eef | 1420 | if (warn_ampersand && in_string == INSTRING_WARN) |
48749dbc MLI |
1421 | gfc_warning (OPT_Wampersand, |
1422 | "Missing %<&%> in continued character " | |
696abb30 | 1423 | "constant at %C"); |
5a06474c | 1424 | } |
c55bbc72 JD |
1425 | else if (!in_string && (c == '\'' || c == '"')) |
1426 | goto done; | |
0d3abf6f JJ |
1427 | /* Both !$omp and !$ -fopenmp continuation lines have & on the |
1428 | continuation line only optionally. */ | |
41dbbb37 | 1429 | else if (openmp_flag || openacc_flag || openmp_cond_flag) |
0d3abf6f | 1430 | gfc_current_locus.nextc--; |
5a06474c JD |
1431 | else |
1432 | { | |
1433 | c = ' '; | |
1434 | gfc_current_locus = old_loc; | |
1435 | goto done; | |
1436 | } | |
3fbab549 | 1437 | } |
6de9cd9a | 1438 | } |
b823d9eb | 1439 | else /* Fixed form. */ |
6de9cd9a DN |
1440 | { |
1441 | /* Fixed form continuation. */ | |
0d6fc963 | 1442 | if (in_string != INSTRING_WARN && c == '!') |
6de9cd9a DN |
1443 | { |
1444 | /* Skip comment at end of line. */ | |
1445 | do | |
1446 | { | |
1447 | c = next_char (); | |
1448 | } | |
1449 | while (c != '\n'); | |
a34938be RG |
1450 | |
1451 | /* Avoid truncation warnings for comment ending lines. */ | |
1452 | gfc_current_locus.lb->truncated = 0; | |
6de9cd9a DN |
1453 | } |
1454 | ||
1455 | if (c != '\n') | |
1456 | goto done; | |
1457 | ||
9cd38d51 | 1458 | /* Check to see if the continuation line was truncated. */ |
73e42eef | 1459 | if (warn_line_truncation && gfc_current_locus.lb != NULL |
9cd38d51 JD |
1460 | && gfc_current_locus.lb->truncated) |
1461 | { | |
1462 | gfc_current_locus.lb->truncated = 0; | |
b93c0722 MLI |
1463 | gfc_warning_now (OPT_Wline_truncation, |
1464 | "Line truncated at %L", &gfc_current_locus); | |
9cd38d51 JD |
1465 | } |
1466 | ||
41dbbb37 TS |
1467 | if (flag_openmp) |
1468 | prev_openmp_flag = openmp_flag; | |
1469 | if (flag_openacc) | |
1470 | prev_openacc_flag = openacc_flag; | |
1471 | ||
6de9cd9a | 1472 | continue_flag = 1; |
63645982 | 1473 | old_loc = gfc_current_locus; |
6de9cd9a DN |
1474 | |
1475 | gfc_advance_line (); | |
0d3abf6f | 1476 | skip_fixed_comments (); |
6de9cd9a DN |
1477 | |
1478 | /* See if this line is a continuation line. */ | |
aa81272c | 1479 | if (flag_openmp && openmp_flag != prev_openmp_flag && !openacc_flag) |
6de9cd9a | 1480 | { |
6c7a4dfd JJ |
1481 | openmp_flag = prev_openmp_flag; |
1482 | goto not_continuation; | |
6de9cd9a | 1483 | } |
aa81272c | 1484 | if (flag_openacc && openacc_flag != prev_openacc_flag && !openmp_flag) |
41dbbb37 TS |
1485 | { |
1486 | openacc_flag = prev_openacc_flag; | |
1487 | goto not_continuation; | |
1488 | } | |
6de9cd9a | 1489 | |
aa81272c IU |
1490 | /* In case we have an OpenMP directive continued by OpenACC |
1491 | sentinel, or vice versa, we get both openmp_flag and | |
1492 | openacc_flag on. */ | |
1493 | if (openacc_flag && openmp_flag) | |
1494 | { | |
1495 | int is_openmp = 0; | |
1496 | for (i = 0; i < 5; i++) | |
1497 | { | |
1498 | c = next_char (); | |
1499 | if (gfc_wide_tolower (c) != (unsigned char) "*$acc"[i]) | |
1500 | is_openmp = 1; | |
1501 | } | |
1502 | gfc_error (is_openmp ? "Wrong OpenACC continuation at %C: " | |
1503 | "expected !$ACC, got !$OMP" | |
1504 | : "Wrong OpenMP continuation at %C: " | |
1505 | "expected !$OMP, got !$ACC"); | |
1506 | } | |
1507 | else if (!openmp_flag && !openacc_flag) | |
6c7a4dfd JJ |
1508 | for (i = 0; i < 5; i++) |
1509 | { | |
1510 | c = next_char (); | |
1511 | if (c != ' ') | |
1512 | goto not_continuation; | |
1513 | } | |
41dbbb37 | 1514 | else if (openmp_flag) |
6c7a4dfd JJ |
1515 | for (i = 0; i < 5; i++) |
1516 | { | |
1517 | c = next_char (); | |
8fc541d3 | 1518 | if (gfc_wide_tolower (c) != (unsigned char) "*$omp"[i]) |
6c7a4dfd JJ |
1519 | goto not_continuation; |
1520 | } | |
41dbbb37 TS |
1521 | else if (openacc_flag) |
1522 | for (i = 0; i < 5; i++) | |
1523 | { | |
1524 | c = next_char (); | |
1525 | if (gfc_wide_tolower (c) != (unsigned char) "*$acc"[i]) | |
1526 | goto not_continuation; | |
1527 | } | |
6c7a4dfd | 1528 | |
6de9cd9a | 1529 | c = next_char (); |
6c7a4dfd | 1530 | if (c == '0' || c == ' ' || c == '\n') |
6de9cd9a | 1531 | goto not_continuation; |
5a06474c JD |
1532 | |
1533 | /* We've got a continuation line. If we are on the very next line after | |
1534 | the last continuation, increment the continuation line count and | |
1535 | check whether the limit has been exceeded. */ | |
5ffeb913 | 1536 | if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1) |
5a06474c JD |
1537 | { |
1538 | if (++continue_count == gfc_option.max_continue_fixed) | |
1539 | { | |
edf1eac2 | 1540 | if (gfc_notification_std (GFC_STD_GNU) || pedantic) |
db30e21c | 1541 | gfc_warning (0, "Limit of %d continuations exceeded in " |
edf1eac2 SK |
1542 | "statement at %C", |
1543 | gfc_option.max_continue_fixed); | |
5a06474c JD |
1544 | } |
1545 | } | |
1546 | ||
f449022d JD |
1547 | if (gfc_current_locus.lb != NULL |
1548 | && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb)) | |
5ffeb913 | 1549 | continue_line = gfc_linebuf_linenum (gfc_current_locus.lb); |
6de9cd9a DN |
1550 | } |
1551 | ||
1552 | /* Ready to read first character of continuation line, which might | |
1553 | be another continuation line! */ | |
1554 | goto restart; | |
1555 | ||
1556 | not_continuation: | |
1557 | c = '\n'; | |
63645982 | 1558 | gfc_current_locus = old_loc; |
85d5c27d | 1559 | end_flag = 0; |
6de9cd9a DN |
1560 | |
1561 | done: | |
5a06474c JD |
1562 | if (c == '\n') |
1563 | continue_count = 0; | |
6de9cd9a DN |
1564 | continue_flag = 0; |
1565 | return c; | |
1566 | } | |
1567 | ||
1568 | ||
1569 | /* Get the next character of input, folded to lowercase. In fixed | |
1570 | form mode, we also ignore spaces. When matcher subroutines are | |
1571 | parsing character literals, they have to call | |
1572 | gfc_next_char_literal(). */ | |
1573 | ||
8fc541d3 | 1574 | gfc_char_t |
6de9cd9a DN |
1575 | gfc_next_char (void) |
1576 | { | |
8fc541d3 | 1577 | gfc_char_t c; |
6de9cd9a DN |
1578 | |
1579 | do | |
1580 | { | |
696abb30 | 1581 | c = gfc_next_char_literal (NONSTRING); |
6de9cd9a | 1582 | } |
d4fa05b9 | 1583 | while (gfc_current_form == FORM_FIXED && gfc_is_whitespace (c)); |
6de9cd9a | 1584 | |
8fc541d3 | 1585 | return gfc_wide_tolower (c); |
6de9cd9a DN |
1586 | } |
1587 | ||
8fc541d3 FXC |
1588 | char |
1589 | gfc_next_ascii_char (void) | |
1590 | { | |
1591 | gfc_char_t c = gfc_next_char (); | |
6de9cd9a | 1592 | |
8fc541d3 FXC |
1593 | return (gfc_wide_fits_in_byte (c) ? (unsigned char) c |
1594 | : (unsigned char) UCHAR_MAX); | |
1595 | } | |
1596 | ||
1597 | ||
1598 | gfc_char_t | |
6de9cd9a DN |
1599 | gfc_peek_char (void) |
1600 | { | |
1601 | locus old_loc; | |
8fc541d3 | 1602 | gfc_char_t c; |
6de9cd9a | 1603 | |
63645982 | 1604 | old_loc = gfc_current_locus; |
6de9cd9a | 1605 | c = gfc_next_char (); |
63645982 | 1606 | gfc_current_locus = old_loc; |
6de9cd9a DN |
1607 | |
1608 | return c; | |
1609 | } | |
1610 | ||
1611 | ||
8fc541d3 FXC |
1612 | char |
1613 | gfc_peek_ascii_char (void) | |
1614 | { | |
1615 | gfc_char_t c = gfc_peek_char (); | |
1616 | ||
1617 | return (gfc_wide_fits_in_byte (c) ? (unsigned char) c | |
1618 | : (unsigned char) UCHAR_MAX); | |
1619 | } | |
1620 | ||
1621 | ||
6de9cd9a DN |
1622 | /* Recover from an error. We try to get past the current statement |
1623 | and get lined up for the next. The next statement follows a '\n' | |
1624 | or a ';'. We also assume that we are not within a character | |
1625 | constant, and deal with finding a '\'' or '"'. */ | |
1626 | ||
1627 | void | |
1628 | gfc_error_recovery (void) | |
1629 | { | |
8fc541d3 | 1630 | gfc_char_t c, delim; |
6de9cd9a DN |
1631 | |
1632 | if (gfc_at_eof ()) | |
1633 | return; | |
1634 | ||
1635 | for (;;) | |
1636 | { | |
1637 | c = gfc_next_char (); | |
1638 | if (c == '\n' || c == ';') | |
1639 | break; | |
1640 | ||
1641 | if (c != '\'' && c != '"') | |
1642 | { | |
1643 | if (gfc_at_eof ()) | |
1644 | break; | |
1645 | continue; | |
1646 | } | |
1647 | delim = c; | |
1648 | ||
1649 | for (;;) | |
1650 | { | |
1651 | c = next_char (); | |
1652 | ||
1653 | if (c == delim) | |
1654 | break; | |
1655 | if (c == '\n') | |
ba1defa5 | 1656 | return; |
6de9cd9a DN |
1657 | if (c == '\\') |
1658 | { | |
1659 | c = next_char (); | |
1660 | if (c == '\n') | |
ba1defa5 | 1661 | return; |
6de9cd9a DN |
1662 | } |
1663 | } | |
1664 | if (gfc_at_eof ()) | |
1665 | break; | |
1666 | } | |
6de9cd9a DN |
1667 | } |
1668 | ||
1669 | ||
1670 | /* Read ahead until the next character to be read is not whitespace. */ | |
1671 | ||
1672 | void | |
1673 | gfc_gobble_whitespace (void) | |
1674 | { | |
840bd9f7 | 1675 | static int linenum = 0; |
6de9cd9a | 1676 | locus old_loc; |
8fc541d3 | 1677 | gfc_char_t c; |
6de9cd9a DN |
1678 | |
1679 | do | |
1680 | { | |
63645982 | 1681 | old_loc = gfc_current_locus; |
696abb30 | 1682 | c = gfc_next_char_literal (NONSTRING); |
840bd9f7 SK |
1683 | /* Issue a warning for nonconforming tabs. We keep track of the line |
1684 | number because the Fortran matchers will often back up and the same | |
1685 | line will be scanned multiple times. */ | |
16db2a6a | 1686 | if (warn_tabs && c == '\t') |
840bd9f7 | 1687 | { |
45a82bd9 | 1688 | int cur_linenum = LOCATION_LINE (gfc_current_locus.lb->location); |
45a82bd9 PB |
1689 | if (cur_linenum != linenum) |
1690 | { | |
1691 | linenum = cur_linenum; | |
d0e168a0 | 1692 | gfc_warning_now (OPT_Wtabs, "Nonconforming tab character at %C"); |
45a82bd9 | 1693 | } |
840bd9f7 | 1694 | } |
6de9cd9a DN |
1695 | } |
1696 | while (gfc_is_whitespace (c)); | |
1697 | ||
63645982 | 1698 | gfc_current_locus = old_loc; |
6de9cd9a DN |
1699 | } |
1700 | ||
1701 | ||
f56c5d5d TS |
1702 | /* Load a single line into pbuf. |
1703 | ||
1704 | If pbuf points to a NULL pointer, it is allocated. | |
1705 | We truncate lines that are too long, unless we're dealing with | |
1706 | preprocessor lines or if the option -ffixed-line-length-none is set, | |
1707 | in which case we reallocate the buffer to fit the entire line, if | |
1708 | need be. | |
1709 | In fixed mode, we expand a tab that occurs within the statement | |
1710 | label region to expand to spaces that leave the next character in | |
ba1defa5 | 1711 | the source region. |
f2f5443c FXC |
1712 | |
1713 | If first_char is not NULL, it's a pointer to a single char value holding | |
1714 | the first character of the line, which has already been read by the | |
1715 | caller. This avoids the use of ungetc(). | |
1716 | ||
1526c4b5 JD |
1717 | load_line returns whether the line was truncated. |
1718 | ||
1719 | NOTE: The error machinery isn't available at this point, so we can't | |
1720 | easily report line and column numbers consistent with other | |
1721 | parts of gfortran. */ | |
6de9cd9a | 1722 | |
ba1defa5 | 1723 | static int |
f2f5443c | 1724 | load_line (FILE *input, gfc_char_t **pbuf, int *pbuflen, const int *first_char) |
6de9cd9a | 1725 | { |
840bd9f7 | 1726 | static int linenum = 0, current_line = 1; |
d1e3d6ae | 1727 | int c, maxlen, i, preprocessor_flag, buflen = *pbuflen; |
840bd9f7 | 1728 | int trunc_flag = 0, seen_comment = 0; |
ac64eec4 | 1729 | int seen_printable = 0, seen_ampersand = 0, quoted = ' '; |
8fc541d3 | 1730 | gfc_char_t *buffer; |
fd1935d5 | 1731 | bool found_tab = false; |
f56c5d5d | 1732 | |
1dde8683 | 1733 | /* Determine the maximum allowed line length. */ |
f56c5d5d | 1734 | if (gfc_current_form == FORM_FREE) |
203c7ebf | 1735 | maxlen = flag_free_line_length; |
16ab8e74 | 1736 | else if (gfc_current_form == FORM_FIXED) |
203c7ebf | 1737 | maxlen = flag_fixed_line_length; |
f56c5d5d | 1738 | else |
16ab8e74 | 1739 | maxlen = 72; |
f56c5d5d TS |
1740 | |
1741 | if (*pbuf == NULL) | |
1742 | { | |
1dde8683 BM |
1743 | /* Allocate the line buffer, storing its length into buflen. |
1744 | Note that if maxlen==0, indicating that arbitrary-length lines | |
1745 | are allowed, the buffer will be reallocated if this length is | |
1746 | insufficient; since 132 characters is the length of a standard | |
1747 | free-form line, we use that as a starting guess. */ | |
f56c5d5d TS |
1748 | if (maxlen > 0) |
1749 | buflen = maxlen; | |
1750 | else | |
1dde8683 | 1751 | buflen = 132; |
6de9cd9a | 1752 | |
00660189 | 1753 | *pbuf = gfc_get_wide_string (buflen + 1); |
f56c5d5d | 1754 | } |
6de9cd9a DN |
1755 | |
1756 | i = 0; | |
f56c5d5d | 1757 | buffer = *pbuf; |
6de9cd9a | 1758 | |
f2f5443c FXC |
1759 | if (first_char) |
1760 | c = *first_char; | |
1761 | else | |
1762 | c = getc (input); | |
1763 | ||
1764 | /* In order to not truncate preprocessor lines, we have to | |
1765 | remember that this is one. */ | |
1766 | preprocessor_flag = (c == '#' ? 1 : 0); | |
fa841200 | 1767 | |
6de9cd9a DN |
1768 | for (;;) |
1769 | { | |
6de9cd9a DN |
1770 | if (c == EOF) |
1771 | break; | |
f2f5443c | 1772 | |
6de9cd9a | 1773 | if (c == '\n') |
1526c4b5 JD |
1774 | { |
1775 | /* Check for illegal use of ampersand. See F95 Standard 3.3.1.3. */ | |
1776 | if (gfc_current_form == FORM_FREE | |
c284e499 | 1777 | && !seen_printable && seen_ampersand) |
1526c4b5 JD |
1778 | { |
1779 | if (pedantic) | |
4daa149b TB |
1780 | gfc_error_now ("%<&%> not allowed by itself in line %d", |
1781 | current_line); | |
1526c4b5 | 1782 | else |
db30e21c | 1783 | gfc_warning_now (0, "%<&%> not allowed by itself in line %d", |
4daa149b | 1784 | current_line); |
1526c4b5 JD |
1785 | } |
1786 | break; | |
1787 | } | |
6de9cd9a | 1788 | |
f2f5443c FXC |
1789 | if (c == '\r' || c == '\0') |
1790 | goto next_char; /* Gobble characters. */ | |
6de9cd9a | 1791 | |
1526c4b5 | 1792 | if (c == '&') |
1526c4b5 | 1793 | { |
c284e499 | 1794 | if (seen_ampersand) |
47b0b4fa TB |
1795 | { |
1796 | seen_ampersand = 0; | |
1797 | seen_printable = 1; | |
1798 | } | |
1526c4b5 | 1799 | else |
c284e499 | 1800 | seen_ampersand = 1; |
1526c4b5 JD |
1801 | } |
1802 | ||
bd5db9de | 1803 | if ((c != '&' && c != '!' && c != ' ') || (c == '!' && !seen_ampersand)) |
c284e499 JD |
1804 | seen_printable = 1; |
1805 | ||
840bd9f7 SK |
1806 | /* Is this a fixed-form comment? */ |
1807 | if (gfc_current_form == FORM_FIXED && i == 0 | |
1808 | && (c == '*' || c == 'c' || c == 'd')) | |
1809 | seen_comment = 1; | |
1810 | ||
ac64eec4 JD |
1811 | if (quoted == ' ') |
1812 | { | |
1813 | if (c == '\'' || c == '"') | |
1814 | quoted = c; | |
1815 | } | |
1816 | else if (c == quoted) | |
1817 | quoted = ' '; | |
1818 | ||
1819 | /* Is this a free-form comment? */ | |
1820 | if (c == '!' && quoted == ' ') | |
1821 | seen_comment = 1; | |
1822 | ||
fd1935d5 TB |
1823 | /* Vendor extension: "<tab>1" marks a continuation line. */ |
1824 | if (found_tab) | |
840bd9f7 | 1825 | { |
fd1935d5 TB |
1826 | found_tab = false; |
1827 | if (c >= '1' && c <= '9') | |
1828 | { | |
1829 | *(buffer-1) = c; | |
f2f5443c | 1830 | goto next_char; |
fd1935d5 TB |
1831 | } |
1832 | } | |
1833 | ||
1834 | if (gfc_current_form == FORM_FIXED && c == '\t' && i < 6) | |
1835 | { | |
1836 | found_tab = true; | |
1837 | ||
16db2a6a | 1838 | if (warn_tabs && seen_comment == 0 && current_line != linenum) |
840bd9f7 SK |
1839 | { |
1840 | linenum = current_line; | |
4daa149b TB |
1841 | gfc_warning_now (OPT_Wtabs, |
1842 | "Nonconforming tab character in column %d " | |
1843 | "of line %d", i+1, linenum); | |
840bd9f7 SK |
1844 | } |
1845 | ||
fd1935d5 | 1846 | while (i < 6) |
6de9cd9a DN |
1847 | { |
1848 | *buffer++ = ' '; | |
1849 | i++; | |
1850 | } | |
1851 | ||
f2f5443c | 1852 | goto next_char; |
6de9cd9a DN |
1853 | } |
1854 | ||
1855 | *buffer++ = c; | |
1856 | i++; | |
1857 | ||
d1e3d6ae | 1858 | if (maxlen == 0 || preprocessor_flag) |
f56c5d5d | 1859 | { |
d1e3d6ae JJ |
1860 | if (i >= buflen) |
1861 | { | |
1862 | /* Reallocate line buffer to double size to hold the | |
3fbab549 | 1863 | overlong line. */ |
d1e3d6ae | 1864 | buflen = buflen * 2; |
ece3f663 | 1865 | *pbuf = XRESIZEVEC (gfc_char_t, *pbuf, (buflen + 1)); |
edf1eac2 | 1866 | buffer = (*pbuf) + i; |
d1e3d6ae | 1867 | } |
f56c5d5d | 1868 | } |
d1e3d6ae | 1869 | else if (i >= maxlen) |
16ab8e74 | 1870 | { |
ac64eec4 JD |
1871 | bool trunc_warn = true; |
1872 | ||
1873 | /* Enhancement, if the very next non-space character is an ampersand | |
1874 | or comment that we would otherwise warn about, don't mark as | |
1875 | truncated. */ | |
1876 | ||
f56c5d5d | 1877 | /* Truncate the rest of the line. */ |
6de9cd9a DN |
1878 | for (;;) |
1879 | { | |
c4da1827 | 1880 | c = getc (input); |
ac64eec4 | 1881 | if (c == '\r' || c == ' ') |
9c747b97 DF |
1882 | continue; |
1883 | ||
6de9cd9a DN |
1884 | if (c == '\n' || c == EOF) |
1885 | break; | |
a34938be | 1886 | |
ac64eec4 JD |
1887 | if (!trunc_warn && c != '!') |
1888 | trunc_warn = true; | |
1889 | ||
32e4257f JD |
1890 | if (trunc_warn && ((gfc_current_form == FORM_FIXED && c == '&') |
1891 | || c == '!')) | |
ac64eec4 JD |
1892 | trunc_warn = false; |
1893 | ||
1894 | if (c == '!') | |
1895 | seen_comment = 1; | |
1896 | ||
1897 | if (trunc_warn && !seen_comment) | |
1898 | trunc_flag = 1; | |
6de9cd9a DN |
1899 | } |
1900 | ||
f2f5443c FXC |
1901 | c = '\n'; |
1902 | continue; | |
6de9cd9a | 1903 | } |
f2f5443c FXC |
1904 | |
1905 | next_char: | |
1906 | c = getc (input); | |
6de9cd9a DN |
1907 | } |
1908 | ||
f56c5d5d TS |
1909 | /* Pad lines to the selected line length in fixed form. */ |
1910 | if (gfc_current_form == FORM_FIXED | |
203c7ebf | 1911 | && flag_fixed_line_length != 0 |
f56c5d5d TS |
1912 | && !preprocessor_flag |
1913 | && c != EOF) | |
043c2d9e BF |
1914 | { |
1915 | while (i++ < maxlen) | |
1916 | *buffer++ = ' '; | |
1917 | } | |
f56c5d5d | 1918 | |
6de9cd9a | 1919 | *buffer = '\0'; |
d1e3d6ae | 1920 | *pbuflen = buflen; |
840bd9f7 | 1921 | current_line++; |
ba1defa5 RG |
1922 | |
1923 | return trunc_flag; | |
6de9cd9a DN |
1924 | } |
1925 | ||
1926 | ||
d4fa05b9 TS |
1927 | /* Get a gfc_file structure, initialize it and add it to |
1928 | the file stack. */ | |
1929 | ||
1930 | static gfc_file * | |
e0bcf78c | 1931 | get_file (const char *name, enum lc_reason reason ATTRIBUTE_UNUSED) |
d4fa05b9 TS |
1932 | { |
1933 | gfc_file *f; | |
1934 | ||
ece3f663 | 1935 | f = XCNEW (gfc_file); |
d4fa05b9 | 1936 | |
ece3f663 | 1937 | f->filename = xstrdup (name); |
d4fa05b9 TS |
1938 | |
1939 | f->next = file_head; | |
1940 | file_head = f; | |
1941 | ||
60332588 | 1942 | f->up = current_file; |
d4fa05b9 | 1943 | if (current_file != NULL) |
1b271c9b | 1944 | f->inclusion_line = current_file->line; |
d4fa05b9 | 1945 | |
5ffeb913 | 1946 | linemap_add (line_table, reason, false, f->filename, 1); |
c8cc8542 | 1947 | |
d4fa05b9 TS |
1948 | return f; |
1949 | } | |
1950 | ||
8fc541d3 | 1951 | |
d4fa05b9 TS |
1952 | /* Deal with a line from the C preprocessor. The |
1953 | initial octothorp has already been seen. */ | |
6de9cd9a DN |
1954 | |
1955 | static void | |
8fc541d3 | 1956 | preprocessor_line (gfc_char_t *c) |
6de9cd9a | 1957 | { |
d4fa05b9 TS |
1958 | bool flag[5]; |
1959 | int i, line; | |
8fc541d3 | 1960 | gfc_char_t *wide_filename; |
d4fa05b9 | 1961 | gfc_file *f; |
2d7c7df6 | 1962 | int escaped, unescape; |
8fc541d3 | 1963 | char *filename; |
6de9cd9a | 1964 | |
d4fa05b9 TS |
1965 | c++; |
1966 | while (*c == ' ' || *c == '\t') | |
1967 | c++; | |
6de9cd9a | 1968 | |
d4fa05b9 | 1969 | if (*c < '0' || *c > '9') |
fa841200 | 1970 | goto bad_cpp_line; |
6de9cd9a | 1971 | |
8fc541d3 | 1972 | line = wide_atoi (c); |
d4fa05b9 | 1973 | |
8fc541d3 | 1974 | c = wide_strchr (c, ' '); |
fa841200 | 1975 | if (c == NULL) |
4c3a6ca1 JJ |
1976 | { |
1977 | /* No file name given. Set new line number. */ | |
1978 | current_file->line = line; | |
1979 | return; | |
1980 | } | |
d7d528c8 ES |
1981 | |
1982 | /* Skip spaces. */ | |
1983 | while (*c == ' ' || *c == '\t') | |
1984 | c++; | |
1985 | ||
1986 | /* Skip quote. */ | |
1987 | if (*c != '"') | |
fa841200 | 1988 | goto bad_cpp_line; |
d7d528c8 ES |
1989 | ++c; |
1990 | ||
8fc541d3 | 1991 | wide_filename = c; |
d4fa05b9 | 1992 | |
d7d528c8 | 1993 | /* Make filename end at quote. */ |
2d7c7df6 | 1994 | unescape = 0; |
d7d528c8 | 1995 | escaped = false; |
edf1eac2 | 1996 | while (*c && ! (!escaped && *c == '"')) |
d7d528c8 ES |
1997 | { |
1998 | if (escaped) | |
edf1eac2 | 1999 | escaped = false; |
2d7c7df6 JJ |
2000 | else if (*c == '\\') |
2001 | { | |
2002 | escaped = true; | |
2003 | unescape++; | |
2004 | } | |
d7d528c8 ES |
2005 | ++c; |
2006 | } | |
2007 | ||
2008 | if (! *c) | |
fa841200 TS |
2009 | /* Preprocessor line has no closing quote. */ |
2010 | goto bad_cpp_line; | |
d7d528c8 | 2011 | |
d4fa05b9 TS |
2012 | *c++ = '\0'; |
2013 | ||
2d7c7df6 JJ |
2014 | /* Undo effects of cpp_quote_string. */ |
2015 | if (unescape) | |
2016 | { | |
8fc541d3 | 2017 | gfc_char_t *s = wide_filename; |
b0b14c7b | 2018 | gfc_char_t *d = gfc_get_wide_string (c - wide_filename - unescape); |
d7d528c8 | 2019 | |
8fc541d3 | 2020 | wide_filename = d; |
2d7c7df6 JJ |
2021 | while (*s) |
2022 | { | |
2023 | if (*s == '\\') | |
2024 | *d++ = *++s; | |
2025 | else | |
2026 | *d++ = *s; | |
2027 | s++; | |
2028 | } | |
2029 | *d = '\0'; | |
2030 | } | |
d7d528c8 | 2031 | |
d4fa05b9 | 2032 | /* Get flags. */ |
4c3a6ca1 | 2033 | |
1e39a151 | 2034 | flag[1] = flag[2] = flag[3] = flag[4] = false; |
6de9cd9a | 2035 | |
6de9cd9a DN |
2036 | for (;;) |
2037 | { | |
8fc541d3 | 2038 | c = wide_strchr (c, ' '); |
d4fa05b9 TS |
2039 | if (c == NULL) |
2040 | break; | |
6de9cd9a | 2041 | |
d4fa05b9 | 2042 | c++; |
8fc541d3 | 2043 | i = wide_atoi (c); |
6de9cd9a | 2044 | |
d4fa05b9 TS |
2045 | if (1 <= i && i <= 4) |
2046 | flag[i] = true; | |
2047 | } | |
4c3a6ca1 | 2048 | |
8fc541d3 FXC |
2049 | /* Convert the filename in wide characters into a filename in narrow |
2050 | characters. */ | |
00660189 | 2051 | filename = gfc_widechar_to_char (wide_filename, -1); |
8fc541d3 | 2052 | |
d4fa05b9 | 2053 | /* Interpret flags. */ |
4c3a6ca1 | 2054 | |
94b00ee4 | 2055 | if (flag[1]) /* Starting new file. */ |
d4fa05b9 | 2056 | { |
c8cc8542 | 2057 | f = get_file (filename, LC_RENAME); |
1b271c9b | 2058 | add_file_change (f->filename, f->inclusion_line); |
d4fa05b9 TS |
2059 | current_file = f; |
2060 | } | |
4c3a6ca1 | 2061 | |
d4fa05b9 TS |
2062 | if (flag[2]) /* Ending current file. */ |
2063 | { | |
94b00ee4 | 2064 | if (!current_file->up |
ba78087b | 2065 | || filename_cmp (current_file->up->filename, filename) != 0) |
4c3a6ca1 | 2066 | { |
2a2703a2 MLI |
2067 | linemap_line_start (line_table, current_file->line, 80); |
2068 | /* ??? One could compute the exact column where the filename | |
2069 | starts and compute the exact location here. */ | |
2070 | gfc_warning_now_at (linemap_position_for_column (line_table, 1), | |
2071 | 0, "file %qs left but not entered", | |
2072 | filename); | |
2073 | current_file->line++; | |
2d7c7df6 | 2074 | if (unescape) |
cede9502 JM |
2075 | free (wide_filename); |
2076 | free (filename); | |
4c3a6ca1 JJ |
2077 | return; |
2078 | } | |
ee07457b | 2079 | |
1b271c9b | 2080 | add_file_change (NULL, line); |
94b00ee4 | 2081 | current_file = current_file->up; |
ee07457b FXC |
2082 | linemap_add (line_table, LC_RENAME, false, current_file->filename, |
2083 | current_file->line); | |
d4fa05b9 | 2084 | } |
4c3a6ca1 | 2085 | |
d4fa05b9 TS |
2086 | /* The name of the file can be a temporary file produced by |
2087 | cpp. Replace the name if it is different. */ | |
4c3a6ca1 | 2088 | |
ba78087b | 2089 | if (filename_cmp (current_file->filename, filename) != 0) |
d4fa05b9 | 2090 | { |
95213750 LB |
2091 | /* FIXME: we leak the old filename because a pointer to it may be stored |
2092 | in the linemap. Alternative could be using GC or updating linemap to | |
1cc0e193 | 2093 | point to the new name, but there is no API for that currently. */ |
ece3f663 | 2094 | current_file->filename = xstrdup (filename); |
d4fa05b9 | 2095 | } |
fa841200 | 2096 | |
4c3a6ca1 JJ |
2097 | /* Set new line number. */ |
2098 | current_file->line = line; | |
2d7c7df6 | 2099 | if (unescape) |
cede9502 JM |
2100 | free (wide_filename); |
2101 | free (filename); | |
fa841200 TS |
2102 | return; |
2103 | ||
2104 | bad_cpp_line: | |
2a2703a2 MLI |
2105 | linemap_line_start (line_table, current_file->line, 80); |
2106 | /* ??? One could compute the exact column where the directive | |
2107 | starts and compute the exact location here. */ | |
2108 | gfc_warning_now_at (linemap_position_for_column (line_table, 2), 0, | |
2109 | "Illegal preprocessor directive"); | |
fa841200 | 2110 | current_file->line++; |
d4fa05b9 TS |
2111 | } |
2112 | ||
2113 | ||
524af0d6 | 2114 | static bool load_file (const char *, const char *, bool); |
d4fa05b9 TS |
2115 | |
2116 | /* include_line()-- Checks a line buffer to see if it is an include | |
2117 | line. If so, we call load_file() recursively to load the included | |
2118 | file. We never return a syntax error because a statement like | |
2119 | "include = 5" is perfectly legal. We return false if no include was | |
2120 | processed or true if we matched an include. */ | |
2121 | ||
2122 | static bool | |
8fc541d3 | 2123 | include_line (gfc_char_t *line) |
d4fa05b9 | 2124 | { |
8fc541d3 FXC |
2125 | gfc_char_t quote, *c, *begin, *stop; |
2126 | char *filename; | |
9b9e4cd6 | 2127 | |
d4fa05b9 | 2128 | c = line; |
9b9e4cd6 | 2129 | |
c61819ff | 2130 | if (flag_openmp || flag_openmp_simd) |
9b9e4cd6 JJ |
2131 | { |
2132 | if (gfc_current_form == FORM_FREE) | |
2133 | { | |
2134 | while (*c == ' ' || *c == '\t') | |
2135 | c++; | |
2136 | if (*c == '!' && c[1] == '$' && (c[2] == ' ' || c[2] == '\t')) | |
2137 | c += 3; | |
2138 | } | |
2139 | else | |
2140 | { | |
2141 | if ((*c == '!' || *c == 'c' || *c == 'C' || *c == '*') | |
2142 | && c[1] == '$' && (c[2] == ' ' || c[2] == '\t')) | |
2143 | c += 3; | |
2144 | } | |
2145 | } | |
2146 | ||
d4fa05b9 TS |
2147 | while (*c == ' ' || *c == '\t') |
2148 | c++; | |
2149 | ||
00660189 | 2150 | if (gfc_wide_strncasecmp (c, "include", 7)) |
8fc541d3 | 2151 | return false; |
d4fa05b9 TS |
2152 | |
2153 | c += 7; | |
2154 | while (*c == ' ' || *c == '\t') | |
2155 | c++; | |
2156 | ||
2157 | /* Find filename between quotes. */ | |
2158 | ||
2159 | quote = *c++; | |
2160 | if (quote != '"' && quote != '\'') | |
2161 | return false; | |
2162 | ||
2163 | begin = c; | |
2164 | ||
2165 | while (*c != quote && *c != '\0') | |
2166 | c++; | |
2167 | ||
2168 | if (*c == '\0') | |
2169 | return false; | |
2170 | ||
2171 | stop = c++; | |
2172 | ||
2173 | while (*c == ' ' || *c == '\t') | |
2174 | c++; | |
2175 | ||
2176 | if (*c != '\0' && *c != '!') | |
2177 | return false; | |
2178 | ||
f7b529fa | 2179 | /* We have an include line at this point. */ |
d4fa05b9 TS |
2180 | |
2181 | *stop = '\0'; /* It's ok to trash the buffer, as this line won't be | |
2182 | read by anything else. */ | |
2183 | ||
00660189 | 2184 | filename = gfc_widechar_to_char (begin, -1); |
524af0d6 | 2185 | if (!load_file (filename, NULL, false)) |
abba1823 | 2186 | exit (FATAL_EXIT_CODE); |
570f4691 | 2187 | |
cede9502 | 2188 | free (filename); |
d4fa05b9 TS |
2189 | return true; |
2190 | } | |
2191 | ||
edf1eac2 | 2192 | |
d4fa05b9 TS |
2193 | /* Load a file into memory by calling load_line until the file ends. */ |
2194 | ||
524af0d6 | 2195 | static bool |
e513a086 | 2196 | load_file (const char *realfilename, const char *displayedname, bool initial) |
d4fa05b9 | 2197 | { |
8fc541d3 | 2198 | gfc_char_t *line; |
d4fa05b9 TS |
2199 | gfc_linebuf *b; |
2200 | gfc_file *f; | |
2201 | FILE *input; | |
d1e3d6ae | 2202 | int len, line_len; |
caef7872 | 2203 | bool first_line; |
44e66a77 JD |
2204 | struct stat st; |
2205 | int stat_result; | |
e513a086 | 2206 | const char *filename; |
892a371f DS |
2207 | /* If realfilename and displayedname are different and non-null then |
2208 | surely realfilename is the preprocessed form of | |
2209 | displayedname. */ | |
2210 | bool preprocessed_p = (realfilename && displayedname | |
2211 | && strcmp (realfilename, displayedname)); | |
e513a086 TB |
2212 | |
2213 | filename = displayedname ? displayedname : realfilename; | |
d4fa05b9 TS |
2214 | |
2215 | for (f = current_file; f; f = f->up) | |
ba78087b | 2216 | if (filename_cmp (filename, f->filename) == 0) |
d4fa05b9 | 2217 | { |
0ee1b105 TB |
2218 | fprintf (stderr, "%s:%d: Error: File '%s' is being included " |
2219 | "recursively\n", current_file->filename, current_file->line, | |
2220 | filename); | |
524af0d6 | 2221 | return false; |
d4fa05b9 TS |
2222 | } |
2223 | ||
2224 | if (initial) | |
2225 | { | |
2d7c7df6 JJ |
2226 | if (gfc_src_file) |
2227 | { | |
2228 | input = gfc_src_file; | |
2229 | gfc_src_file = NULL; | |
2230 | } | |
2231 | else | |
e513a086 | 2232 | input = gfc_open_file (realfilename); |
44e66a77 | 2233 | |
d4fa05b9 TS |
2234 | if (input == NULL) |
2235 | { | |
4daa149b | 2236 | gfc_error_now ("Can't open file %qs", filename); |
524af0d6 | 2237 | return false; |
d4fa05b9 TS |
2238 | } |
2239 | } | |
2240 | else | |
2241 | { | |
e513a086 | 2242 | input = gfc_open_included_file (realfilename, false, false); |
d4fa05b9 TS |
2243 | if (input == NULL) |
2244 | { | |
0ee1b105 TB |
2245 | fprintf (stderr, "%s:%d: Error: Can't open included file '%s'\n", |
2246 | current_file->filename, current_file->line, filename); | |
524af0d6 | 2247 | return false; |
d4fa05b9 | 2248 | } |
44e66a77 | 2249 | stat_result = stat (realfilename, &st); |
5803aa7c | 2250 | if (stat_result == 0 && !S_ISREG(st.st_mode)) |
44e66a77 JD |
2251 | { |
2252 | fprintf (stderr, "%s:%d: Error: Included path '%s'" | |
2253 | " is not a regular file\n", | |
2254 | current_file->filename, current_file->line, filename); | |
2255 | fclose (input); | |
2256 | return false; | |
2257 | } | |
d4fa05b9 TS |
2258 | } |
2259 | ||
892a371f | 2260 | /* Load the file. |
d4fa05b9 | 2261 | |
892a371f DS |
2262 | A "non-initial" file means a file that is being included. In |
2263 | that case we are creating an LC_ENTER map. | |
2264 | ||
2265 | An "initial" file means a main file; one that is not included. | |
2266 | That file has already got at least one (surely more) line map(s) | |
2267 | created by gfc_init. So the subsequent map created in that case | |
2268 | must have LC_RENAME reason. | |
2269 | ||
2270 | This latter case is not true for a preprocessed file. In that | |
2271 | case, although the file is "initial", the line maps created by | |
2272 | gfc_init was used during the preprocessing of the file. Now that | |
2273 | the preprocessing is over and we are being fed the result of that | |
2274 | preprocessing, we need to create a brand new line map for the | |
2275 | preprocessed file, so the reason is going to be LC_ENTER. */ | |
2276 | ||
2277 | f = get_file (filename, (initial && !preprocessed_p) ? LC_RENAME : LC_ENTER); | |
1b271c9b JJ |
2278 | if (!initial) |
2279 | add_file_change (f->filename, f->inclusion_line); | |
d4fa05b9 TS |
2280 | current_file = f; |
2281 | current_file->line = 1; | |
f56c5d5d | 2282 | line = NULL; |
d1e3d6ae | 2283 | line_len = 0; |
caef7872 | 2284 | first_line = true; |
d4fa05b9 | 2285 | |
2d7c7df6 JJ |
2286 | if (initial && gfc_src_preprocessor_lines[0]) |
2287 | { | |
2288 | preprocessor_line (gfc_src_preprocessor_lines[0]); | |
cede9502 | 2289 | free (gfc_src_preprocessor_lines[0]); |
2d7c7df6 JJ |
2290 | gfc_src_preprocessor_lines[0] = NULL; |
2291 | if (gfc_src_preprocessor_lines[1]) | |
2292 | { | |
2293 | preprocessor_line (gfc_src_preprocessor_lines[1]); | |
cede9502 | 2294 | free (gfc_src_preprocessor_lines[1]); |
2d7c7df6 JJ |
2295 | gfc_src_preprocessor_lines[1] = NULL; |
2296 | } | |
2297 | } | |
2298 | ||
16ab8e74 | 2299 | for (;;) |
d4fa05b9 | 2300 | { |
f2f5443c | 2301 | int trunc = load_line (input, &line, &line_len, NULL); |
d4fa05b9 | 2302 | |
8fc541d3 | 2303 | len = gfc_wide_strlen (line); |
6de9cd9a DN |
2304 | if (feof (input) && len == 0) |
2305 | break; | |
2306 | ||
caef7872 FXC |
2307 | /* If this is the first line of the file, it can contain a byte |
2308 | order mark (BOM), which we will ignore: | |
2309 | FF FE is UTF-16 little endian, | |
2310 | FE FF is UTF-16 big endian, | |
2311 | EF BB BF is UTF-8. */ | |
2312 | if (first_line | |
8fc541d3 FXC |
2313 | && ((line_len >= 2 && line[0] == (unsigned char) '\xFF' |
2314 | && line[1] == (unsigned char) '\xFE') | |
2315 | || (line_len >= 2 && line[0] == (unsigned char) '\xFE' | |
2316 | && line[1] == (unsigned char) '\xFF') | |
2317 | || (line_len >= 3 && line[0] == (unsigned char) '\xEF' | |
2318 | && line[1] == (unsigned char) '\xBB' | |
2319 | && line[2] == (unsigned char) '\xBF'))) | |
caef7872 | 2320 | { |
8fc541d3 | 2321 | int n = line[1] == (unsigned char) '\xBB' ? 3 : 2; |
7b901ac4 | 2322 | gfc_char_t *new_char = gfc_get_wide_string (line_len); |
caef7872 | 2323 | |
7b901ac4 | 2324 | wide_strcpy (new_char, &line[n]); |
cede9502 | 2325 | free (line); |
7b901ac4 | 2326 | line = new_char; |
caef7872 FXC |
2327 | len -= n; |
2328 | } | |
2329 | ||
d4fa05b9 TS |
2330 | /* There are three things this line can be: a line of Fortran |
2331 | source, an include line or a C preprocessor directive. */ | |
6de9cd9a | 2332 | |
d4fa05b9 TS |
2333 | if (line[0] == '#') |
2334 | { | |
9e8a6720 FXC |
2335 | /* When -g3 is specified, it's possible that we emit #define |
2336 | and #undef lines, which we need to pass to the middle-end | |
2337 | so that it can emit correct debug info. */ | |
2338 | if (debug_info_level == DINFO_LEVEL_VERBOSE | |
8fc541d3 FXC |
2339 | && (wide_strncmp (line, "#define ", 8) == 0 |
2340 | || wide_strncmp (line, "#undef ", 7) == 0)) | |
9e8a6720 FXC |
2341 | ; |
2342 | else | |
2343 | { | |
2344 | preprocessor_line (line); | |
2345 | continue; | |
2346 | } | |
d4fa05b9 | 2347 | } |
6de9cd9a | 2348 | |
caef7872 FXC |
2349 | /* Preprocessed files have preprocessor lines added before the byte |
2350 | order mark, so first_line is not about the first line of the file | |
2351 | but the first line that's not a preprocessor line. */ | |
2352 | first_line = false; | |
2353 | ||
d4fa05b9 TS |
2354 | if (include_line (line)) |
2355 | { | |
2356 | current_file->line++; | |
2357 | continue; | |
6de9cd9a DN |
2358 | } |
2359 | ||
d4fa05b9 TS |
2360 | /* Add line. */ |
2361 | ||
f7d2e5d4 JB |
2362 | b = XCNEWVAR (gfc_linebuf, gfc_linebuf_header_size |
2363 | + (len + 1) * sizeof (gfc_char_t)); | |
d4fa05b9 | 2364 | |
c4100eae | 2365 | |
c8cc8542 | 2366 | b->location |
b93c0722 MLI |
2367 | = linemap_line_start (line_table, current_file->line++, len); |
2368 | /* ??? We add the location for the maximum column possible here, | |
2369 | because otherwise if the next call creates a new line-map, it | |
2370 | will not reserve space for any offset. */ | |
2371 | if (len > 0) | |
2372 | linemap_position_for_column (line_table, len); | |
2373 | ||
d4fa05b9 | 2374 | b->file = current_file; |
ba1defa5 | 2375 | b->truncated = trunc; |
8fc541d3 | 2376 | wide_strcpy (b->line, line); |
d4fa05b9 TS |
2377 | |
2378 | if (line_head == NULL) | |
2379 | line_head = b; | |
2380 | else | |
2381 | line_tail->next = b; | |
2382 | ||
2383 | line_tail = b; | |
1b271c9b JJ |
2384 | |
2385 | while (file_changes_cur < file_changes_count) | |
2386 | file_changes[file_changes_cur++].lb = b; | |
6de9cd9a | 2387 | } |
d4fa05b9 | 2388 | |
f56c5d5d | 2389 | /* Release the line buffer allocated in load_line. */ |
cede9502 | 2390 | free (line); |
f56c5d5d | 2391 | |
d4fa05b9 TS |
2392 | fclose (input); |
2393 | ||
1b271c9b JJ |
2394 | if (!initial) |
2395 | add_file_change (NULL, current_file->inclusion_line + 1); | |
d4fa05b9 | 2396 | current_file = current_file->up; |
5ffeb913 | 2397 | linemap_add (line_table, LC_LEAVE, 0, NULL, 0); |
524af0d6 | 2398 | return true; |
6de9cd9a DN |
2399 | } |
2400 | ||
2401 | ||
524af0d6 JB |
2402 | /* Open a new file and start scanning from that file. Returns true |
2403 | if everything went OK, false otherwise. If form == FORM_UNKNOWN | |
d4fa05b9 TS |
2404 | it tries to determine the source form from the filename, defaulting |
2405 | to free form. */ | |
6de9cd9a | 2406 | |
524af0d6 | 2407 | bool |
e0bcf78c | 2408 | gfc_new_file (void) |
6de9cd9a | 2409 | { |
524af0d6 | 2410 | bool result; |
6de9cd9a | 2411 | |
670637ee DF |
2412 | if (gfc_cpp_enabled ()) |
2413 | { | |
2414 | result = gfc_cpp_preprocess (gfc_source_file); | |
2415 | if (!gfc_cpp_preprocess_only ()) | |
e513a086 | 2416 | result = load_file (gfc_cpp_temporary_file (), gfc_source_file, true); |
670637ee DF |
2417 | } |
2418 | else | |
e513a086 | 2419 | result = load_file (gfc_source_file, NULL, true); |
6de9cd9a | 2420 | |
63645982 TS |
2421 | gfc_current_locus.lb = line_head; |
2422 | gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line; | |
6de9cd9a | 2423 | |
d4fa05b9 TS |
2424 | #if 0 /* Debugging aid. */ |
2425 | for (; line_head; line_head = line_head->next) | |
6c1abb5c FXC |
2426 | printf ("%s:%3d %s\n", LOCATION_FILE (line_head->location), |
2427 | LOCATION_LINE (line_head->location), line_head->line); | |
6de9cd9a | 2428 | |
abba1823 | 2429 | exit (SUCCESS_EXIT_CODE); |
d4fa05b9 | 2430 | #endif |
6de9cd9a | 2431 | |
d4fa05b9 | 2432 | return result; |
6de9cd9a | 2433 | } |
2d7c7df6 JJ |
2434 | |
2435 | static char * | |
2436 | unescape_filename (const char *ptr) | |
2437 | { | |
2438 | const char *p = ptr, *s; | |
2439 | char *d, *ret; | |
2440 | int escaped, unescape = 0; | |
2441 | ||
2442 | /* Make filename end at quote. */ | |
2443 | escaped = false; | |
2444 | while (*p && ! (! escaped && *p == '"')) | |
2445 | { | |
2446 | if (escaped) | |
2447 | escaped = false; | |
2448 | else if (*p == '\\') | |
2449 | { | |
2450 | escaped = true; | |
2451 | unescape++; | |
2452 | } | |
2453 | ++p; | |
2454 | } | |
2455 | ||
edf1eac2 | 2456 | if (!*p || p[1]) |
2d7c7df6 JJ |
2457 | return NULL; |
2458 | ||
2459 | /* Undo effects of cpp_quote_string. */ | |
2460 | s = ptr; | |
ece3f663 | 2461 | d = XCNEWVEC (char, p + 1 - ptr - unescape); |
2d7c7df6 JJ |
2462 | ret = d; |
2463 | ||
2464 | while (s != p) | |
2465 | { | |
2466 | if (*s == '\\') | |
2467 | *d++ = *++s; | |
2468 | else | |
2469 | *d++ = *s; | |
2470 | s++; | |
2471 | } | |
2472 | *d = '\0'; | |
2473 | return ret; | |
2474 | } | |
2475 | ||
2476 | /* For preprocessed files, if the first tokens are of the form # NUM. | |
2477 | handle the directives so we know the original file name. */ | |
2478 | ||
2479 | const char * | |
2480 | gfc_read_orig_filename (const char *filename, const char **canon_source_file) | |
2481 | { | |
2482 | int c, len; | |
8fc541d3 | 2483 | char *dirname, *tmp; |
2d7c7df6 JJ |
2484 | |
2485 | gfc_src_file = gfc_open_file (filename); | |
2486 | if (gfc_src_file == NULL) | |
2487 | return NULL; | |
2488 | ||
c4da1827 | 2489 | c = getc (gfc_src_file); |
2d7c7df6 JJ |
2490 | |
2491 | if (c != '#') | |
2492 | return NULL; | |
2493 | ||
2494 | len = 0; | |
f2f5443c | 2495 | load_line (gfc_src_file, &gfc_src_preprocessor_lines[0], &len, &c); |
2d7c7df6 | 2496 | |
8fc541d3 | 2497 | if (wide_strncmp (gfc_src_preprocessor_lines[0], "# 1 \"", 5) != 0) |
2d7c7df6 JJ |
2498 | return NULL; |
2499 | ||
00660189 | 2500 | tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[0][5], -1); |
8fc541d3 | 2501 | filename = unescape_filename (tmp); |
cede9502 | 2502 | free (tmp); |
2d7c7df6 JJ |
2503 | if (filename == NULL) |
2504 | return NULL; | |
2505 | ||
c4da1827 | 2506 | c = getc (gfc_src_file); |
2d7c7df6 JJ |
2507 | |
2508 | if (c != '#') | |
2509 | return filename; | |
2510 | ||
2511 | len = 0; | |
f2f5443c | 2512 | load_line (gfc_src_file, &gfc_src_preprocessor_lines[1], &len, &c); |
2d7c7df6 | 2513 | |
8fc541d3 | 2514 | if (wide_strncmp (gfc_src_preprocessor_lines[1], "# 1 \"", 5) != 0) |
2d7c7df6 JJ |
2515 | return filename; |
2516 | ||
00660189 | 2517 | tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[1][5], -1); |
8fc541d3 | 2518 | dirname = unescape_filename (tmp); |
cede9502 | 2519 | free (tmp); |
2d7c7df6 JJ |
2520 | if (dirname == NULL) |
2521 | return filename; | |
2522 | ||
2523 | len = strlen (dirname); | |
2524 | if (len < 3 || dirname[len - 1] != '/' || dirname[len - 2] != '/') | |
2525 | { | |
cede9502 | 2526 | free (dirname); |
2d7c7df6 JJ |
2527 | return filename; |
2528 | } | |
2529 | dirname[len - 2] = '\0'; | |
2530 | set_src_pwd (dirname); | |
2531 | ||
2532 | if (! IS_ABSOLUTE_PATH (filename)) | |
2533 | { | |
ece3f663 | 2534 | char *p = XCNEWVEC (char, len + strlen (filename)); |
2d7c7df6 JJ |
2535 | |
2536 | memcpy (p, dirname, len - 2); | |
2537 | p[len - 2] = '/'; | |
2538 | strcpy (p + len - 1, filename); | |
2539 | *canon_source_file = p; | |
2540 | } | |
2541 | ||
cede9502 | 2542 | free (dirname); |
2d7c7df6 JJ |
2543 | return filename; |
2544 | } |