]>
Commit | Line | Data |
---|---|---|
6de9cd9a | 1 | /* Character scanner. |
4d499824 | 2 | Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 |
ec378180 | 3 | Free Software Foundation, Inc. |
6de9cd9a DN |
4 | Contributed by Andy Vaught |
5 | ||
9fc4d79b | 6 | This file is part of GCC. |
6de9cd9a | 7 | |
9fc4d79b TS |
8 | GCC is free software; you can redistribute it and/or modify it under |
9 | the terms of the GNU General Public License as published by the Free | |
d234d788 | 10 | Software Foundation; either version 3, or (at your option) any later |
9fc4d79b | 11 | version. |
6de9cd9a | 12 | |
9fc4d79b TS |
13 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | for more details. | |
6de9cd9a DN |
17 | |
18 | You should have received a copy of the GNU General Public License | |
d234d788 NC |
19 | along with GCC; see the file COPYING3. If not see |
20 | <http://www.gnu.org/licenses/>. */ | |
6de9cd9a DN |
21 | |
22 | /* Set of subroutines to (ultimately) return the next character to the | |
23 | various matching subroutines. This file's job is to read files and | |
24 | build up lines that are parsed by the parser. This means that we | |
25 | handle continuation lines and "include" lines. | |
26 | ||
27 | The first thing the scanner does is to load an entire file into | |
28 | memory. We load the entire file into memory for a couple reasons. | |
29 | The first is that we want to be able to deal with nonseekable input | |
30 | (pipes, stdin) and there is a lot of backing up involved during | |
31 | parsing. | |
32 | ||
33 | The second is that we want to be able to print the locus of errors, | |
34 | and an error on line 999999 could conflict with something on line | |
35 | one. Given nonseekable input, we've got to store the whole thing. | |
36 | ||
37 | One thing that helps are the column truncation limits that give us | |
38 | an upper bound on the size of individual lines. We don't store the | |
39 | truncated stuff. | |
40 | ||
41 | From the scanner's viewpoint, the higher level subroutines ask for | |
42 | new characters and do a lot of jumping backwards. */ | |
43 | ||
44 | #include "config.h" | |
d22e4895 | 45 | #include "system.h" |
6de9cd9a | 46 | #include "gfortran.h" |
2d7c7df6 | 47 | #include "toplev.h" |
6de9cd9a DN |
48 | |
49 | /* Structure for holding module and include file search path. */ | |
50 | typedef struct gfc_directorylist | |
51 | { | |
52 | char *path; | |
31198773 | 53 | bool use_for_modules; |
6de9cd9a DN |
54 | struct gfc_directorylist *next; |
55 | } | |
56 | gfc_directorylist; | |
57 | ||
58 | /* List of include file search directories. */ | |
31198773 | 59 | static gfc_directorylist *include_dirs, *intrinsic_modules_dirs; |
6de9cd9a | 60 | |
d4fa05b9 | 61 | static gfc_file *file_head, *current_file; |
6de9cd9a | 62 | |
6c7a4dfd | 63 | static int continue_flag, end_flag, openmp_flag; |
5a06474c | 64 | static int continue_count, continue_line; |
6c7a4dfd | 65 | static locus openmp_locus; |
6de9cd9a | 66 | |
d4fa05b9 TS |
67 | gfc_source_form gfc_current_form; |
68 | static gfc_linebuf *line_head, *line_tail; | |
69 | ||
63645982 | 70 | locus gfc_current_locus; |
e0bcf78c | 71 | const char *gfc_source_file; |
2d7c7df6 JJ |
72 | static FILE *gfc_src_file; |
73 | static char *gfc_src_preprocessor_lines[2]; | |
74 | ||
5a06474c | 75 | extern int pedantic; |
6de9cd9a DN |
76 | |
77 | /* Main scanner initialization. */ | |
78 | ||
79 | void | |
80 | gfc_scanner_init_1 (void) | |
81 | { | |
d4fa05b9 TS |
82 | file_head = NULL; |
83 | line_head = NULL; | |
84 | line_tail = NULL; | |
6de9cd9a | 85 | |
5a06474c JD |
86 | continue_count = 0; |
87 | continue_line = 0; | |
88 | ||
6de9cd9a DN |
89 | end_flag = 0; |
90 | } | |
91 | ||
92 | ||
93 | /* Main scanner destructor. */ | |
94 | ||
95 | void | |
96 | gfc_scanner_done_1 (void) | |
97 | { | |
d4fa05b9 TS |
98 | gfc_linebuf *lb; |
99 | gfc_file *f; | |
6de9cd9a | 100 | |
d4fa05b9 | 101 | while(line_head != NULL) |
6de9cd9a | 102 | { |
d4fa05b9 TS |
103 | lb = line_head->next; |
104 | gfc_free(line_head); | |
105 | line_head = lb; | |
6de9cd9a | 106 | } |
d4fa05b9 TS |
107 | |
108 | while(file_head != NULL) | |
6de9cd9a | 109 | { |
d4fa05b9 TS |
110 | f = file_head->next; |
111 | gfc_free(file_head->filename); | |
112 | gfc_free(file_head); | |
113 | file_head = f; | |
6de9cd9a DN |
114 | } |
115 | } | |
116 | ||
117 | ||
118 | /* Adds path to the list pointed to by list. */ | |
119 | ||
31198773 FXC |
120 | static void |
121 | add_path_to_list (gfc_directorylist **list, const char *path, | |
122 | bool use_for_modules) | |
6de9cd9a DN |
123 | { |
124 | gfc_directorylist *dir; | |
125 | const char *p; | |
126 | ||
127 | p = path; | |
31198773 | 128 | while (*p == ' ' || *p == '\t') /* someone might do "-I include" */ |
6de9cd9a DN |
129 | if (*p++ == '\0') |
130 | return; | |
131 | ||
31198773 | 132 | dir = *list; |
6de9cd9a | 133 | if (!dir) |
31198773 | 134 | dir = *list = gfc_getmem (sizeof (gfc_directorylist)); |
6de9cd9a DN |
135 | else |
136 | { | |
137 | while (dir->next) | |
138 | dir = dir->next; | |
139 | ||
140 | dir->next = gfc_getmem (sizeof (gfc_directorylist)); | |
141 | dir = dir->next; | |
142 | } | |
143 | ||
144 | dir->next = NULL; | |
31198773 | 145 | dir->use_for_modules = use_for_modules; |
6de9cd9a DN |
146 | dir->path = gfc_getmem (strlen (p) + 2); |
147 | strcpy (dir->path, p); | |
148 | strcat (dir->path, "/"); /* make '/' last character */ | |
149 | } | |
150 | ||
151 | ||
31198773 FXC |
152 | void |
153 | gfc_add_include_path (const char *path, bool use_for_modules) | |
154 | { | |
155 | add_path_to_list (&include_dirs, path, use_for_modules); | |
156 | } | |
157 | ||
158 | ||
159 | void | |
160 | gfc_add_intrinsic_modules_path (const char *path) | |
161 | { | |
162 | add_path_to_list (&intrinsic_modules_dirs, path, true); | |
163 | } | |
164 | ||
165 | ||
6de9cd9a DN |
166 | /* Release resources allocated for options. */ |
167 | ||
168 | void | |
169 | gfc_release_include_path (void) | |
170 | { | |
171 | gfc_directorylist *p; | |
172 | ||
6de9cd9a DN |
173 | while (include_dirs != NULL) |
174 | { | |
175 | p = include_dirs; | |
176 | include_dirs = include_dirs->next; | |
177 | gfc_free (p->path); | |
178 | gfc_free (p); | |
179 | } | |
31198773 | 180 | |
31198773 FXC |
181 | while (intrinsic_modules_dirs != NULL) |
182 | { | |
183 | p = intrinsic_modules_dirs; | |
184 | intrinsic_modules_dirs = intrinsic_modules_dirs->next; | |
185 | gfc_free (p->path); | |
186 | gfc_free (p); | |
187 | } | |
1bc23383 FXC |
188 | |
189 | gfc_free (gfc_option.module_dir); | |
6de9cd9a DN |
190 | } |
191 | ||
6de9cd9a | 192 | |
31198773 FXC |
193 | static FILE * |
194 | open_included_file (const char *name, gfc_directorylist *list, bool module) | |
6de9cd9a | 195 | { |
200cfbe7 | 196 | char *fullname; |
6de9cd9a DN |
197 | gfc_directorylist *p; |
198 | FILE *f; | |
199 | ||
31198773 | 200 | for (p = list; p; p = p->next) |
b424a572 | 201 | { |
31198773 FXC |
202 | if (module && !p->use_for_modules) |
203 | continue; | |
6de9cd9a | 204 | |
200cfbe7 | 205 | fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1); |
6de9cd9a DN |
206 | strcpy (fullname, p->path); |
207 | strcat (fullname, name); | |
208 | ||
209 | f = gfc_open_file (fullname); | |
210 | if (f != NULL) | |
211 | return f; | |
212 | } | |
213 | ||
214 | return NULL; | |
215 | } | |
216 | ||
31198773 FXC |
217 | |
218 | /* Opens file for reading, searching through the include directories | |
219 | given if necessary. If the include_cwd argument is true, we try | |
220 | to open the file in the current directory first. */ | |
221 | ||
222 | FILE * | |
223 | gfc_open_included_file (const char *name, bool include_cwd, bool module) | |
224 | { | |
225 | FILE *f; | |
226 | ||
e01f74e0 TB |
227 | if (IS_ABSOLUTE_PATH (name)) |
228 | return gfc_open_file (name); | |
229 | ||
31198773 FXC |
230 | if (include_cwd) |
231 | { | |
232 | f = gfc_open_file (name); | |
233 | if (f != NULL) | |
234 | return f; | |
235 | } | |
236 | ||
237 | return open_included_file (name, include_dirs, module); | |
238 | } | |
239 | ||
240 | FILE * | |
241 | gfc_open_intrinsic_module (const char *name) | |
242 | { | |
e01f74e0 TB |
243 | if (IS_ABSOLUTE_PATH (name)) |
244 | return gfc_open_file (name); | |
245 | ||
31198773 FXC |
246 | return open_included_file (name, intrinsic_modules_dirs, true); |
247 | } | |
248 | ||
edf1eac2 | 249 | |
6de9cd9a DN |
250 | /* Test to see if we're at the end of the main source file. */ |
251 | ||
252 | int | |
253 | gfc_at_end (void) | |
254 | { | |
6de9cd9a DN |
255 | return end_flag; |
256 | } | |
257 | ||
258 | ||
259 | /* Test to see if we're at the end of the current file. */ | |
260 | ||
261 | int | |
262 | gfc_at_eof (void) | |
263 | { | |
6de9cd9a DN |
264 | if (gfc_at_end ()) |
265 | return 1; | |
266 | ||
d4fa05b9 | 267 | if (line_head == NULL) |
6de9cd9a DN |
268 | return 1; /* Null file */ |
269 | ||
63645982 | 270 | if (gfc_current_locus.lb == NULL) |
6de9cd9a DN |
271 | return 1; |
272 | ||
273 | return 0; | |
274 | } | |
275 | ||
276 | ||
277 | /* Test to see if we're at the beginning of a new line. */ | |
278 | ||
279 | int | |
280 | gfc_at_bol (void) | |
281 | { | |
6de9cd9a DN |
282 | if (gfc_at_eof ()) |
283 | return 1; | |
284 | ||
63645982 | 285 | return (gfc_current_locus.nextc == gfc_current_locus.lb->line); |
6de9cd9a DN |
286 | } |
287 | ||
288 | ||
289 | /* Test to see if we're at the end of a line. */ | |
290 | ||
291 | int | |
292 | gfc_at_eol (void) | |
293 | { | |
6de9cd9a DN |
294 | if (gfc_at_eof ()) |
295 | return 1; | |
296 | ||
63645982 | 297 | return (*gfc_current_locus.nextc == '\0'); |
6de9cd9a DN |
298 | } |
299 | ||
300 | ||
301 | /* Advance the current line pointer to the next line. */ | |
302 | ||
303 | void | |
304 | gfc_advance_line (void) | |
305 | { | |
6de9cd9a DN |
306 | if (gfc_at_end ()) |
307 | return; | |
308 | ||
63645982 | 309 | if (gfc_current_locus.lb == NULL) |
6de9cd9a | 310 | { |
d4fa05b9 TS |
311 | end_flag = 1; |
312 | return; | |
313 | } | |
6de9cd9a | 314 | |
63645982 | 315 | gfc_current_locus.lb = gfc_current_locus.lb->next; |
6de9cd9a | 316 | |
edf1eac2 | 317 | if (gfc_current_locus.lb != NULL) |
63645982 | 318 | gfc_current_locus.nextc = gfc_current_locus.lb->line; |
d4fa05b9 TS |
319 | else |
320 | { | |
63645982 | 321 | gfc_current_locus.nextc = NULL; |
d4fa05b9 TS |
322 | end_flag = 1; |
323 | } | |
6de9cd9a DN |
324 | } |
325 | ||
326 | ||
327 | /* Get the next character from the input, advancing gfc_current_file's | |
328 | locus. When we hit the end of the line or the end of the file, we | |
329 | start returning a '\n' in order to complete the current statement. | |
330 | No Fortran line conventions are implemented here. | |
331 | ||
332 | Requiring explicit advances to the next line prevents the parse | |
333 | pointer from being on the wrong line if the current statement ends | |
334 | prematurely. */ | |
335 | ||
336 | static int | |
337 | next_char (void) | |
338 | { | |
6de9cd9a | 339 | int c; |
d4fa05b9 | 340 | |
63645982 | 341 | if (gfc_current_locus.nextc == NULL) |
6de9cd9a DN |
342 | return '\n'; |
343 | ||
16a374c6 | 344 | c = (unsigned char) *gfc_current_locus.nextc++; |
6de9cd9a DN |
345 | if (c == '\0') |
346 | { | |
63645982 | 347 | gfc_current_locus.nextc--; /* Remain on this line. */ |
6de9cd9a DN |
348 | c = '\n'; |
349 | } | |
350 | ||
351 | return c; | |
352 | } | |
353 | ||
edf1eac2 | 354 | |
6de9cd9a DN |
355 | /* Skip a comment. When we come here the parse pointer is positioned |
356 | immediately after the comment character. If we ever implement | |
357 | compiler directives withing comments, here is where we parse the | |
358 | directive. */ | |
359 | ||
360 | static void | |
361 | skip_comment_line (void) | |
362 | { | |
363 | char c; | |
364 | ||
365 | do | |
366 | { | |
367 | c = next_char (); | |
368 | } | |
369 | while (c != '\n'); | |
370 | ||
371 | gfc_advance_line (); | |
372 | } | |
373 | ||
374 | ||
375 | /* Comment lines are null lines, lines containing only blanks or lines | |
0d3abf6f JJ |
376 | on which the first nonblank line is a '!'. |
377 | Return true if !$ openmp conditional compilation sentinel was | |
378 | seen. */ | |
6de9cd9a | 379 | |
0d3abf6f | 380 | static bool |
6de9cd9a DN |
381 | skip_free_comments (void) |
382 | { | |
383 | locus start; | |
384 | char c; | |
6c7a4dfd | 385 | int at_bol; |
6de9cd9a DN |
386 | |
387 | for (;;) | |
388 | { | |
6c7a4dfd | 389 | at_bol = gfc_at_bol (); |
63645982 | 390 | start = gfc_current_locus; |
6de9cd9a DN |
391 | if (gfc_at_eof ()) |
392 | break; | |
393 | ||
394 | do | |
6c7a4dfd | 395 | c = next_char (); |
6de9cd9a DN |
396 | while (gfc_is_whitespace (c)); |
397 | ||
398 | if (c == '\n') | |
399 | { | |
400 | gfc_advance_line (); | |
401 | continue; | |
402 | } | |
403 | ||
404 | if (c == '!') | |
405 | { | |
6c7a4dfd JJ |
406 | /* If -fopenmp, we need to handle here 2 things: |
407 | 1) don't treat !$omp as comments, but directives | |
408 | 2) handle OpenMP conditional compilation, where | |
409 | !$ should be treated as 2 spaces (for initial lines | |
410 | only if followed by space). */ | |
411 | if (gfc_option.flag_openmp && at_bol) | |
412 | { | |
413 | locus old_loc = gfc_current_locus; | |
414 | if (next_char () == '$') | |
415 | { | |
416 | c = next_char (); | |
417 | if (c == 'o' || c == 'O') | |
418 | { | |
419 | if (((c = next_char ()) == 'm' || c == 'M') | |
9fa6cfec | 420 | && ((c = next_char ()) == 'p' || c == 'P')) |
6c7a4dfd | 421 | { |
9fa6cfec | 422 | if ((c = next_char ()) == ' ' || continue_flag) |
6c7a4dfd | 423 | { |
9fa6cfec TB |
424 | while (gfc_is_whitespace (c)) |
425 | c = next_char (); | |
426 | if (c != '\n' && c != '!') | |
427 | { | |
428 | openmp_flag = 1; | |
429 | openmp_locus = old_loc; | |
430 | gfc_current_locus = start; | |
431 | return false; | |
432 | } | |
6c7a4dfd | 433 | } |
9fa6cfec TB |
434 | else |
435 | gfc_warning_now ("!$OMP at %C starts a commented " | |
436 | "line as it neither is followed " | |
437 | "by a space nor is a " | |
438 | "continuation line"); | |
6c7a4dfd JJ |
439 | } |
440 | gfc_current_locus = old_loc; | |
441 | next_char (); | |
442 | c = next_char (); | |
443 | } | |
444 | if (continue_flag || c == ' ') | |
445 | { | |
446 | gfc_current_locus = old_loc; | |
447 | next_char (); | |
b30c6a0d | 448 | openmp_flag = 0; |
0d3abf6f | 449 | return true; |
6c7a4dfd JJ |
450 | } |
451 | } | |
452 | gfc_current_locus = old_loc; | |
453 | } | |
6de9cd9a DN |
454 | skip_comment_line (); |
455 | continue; | |
456 | } | |
457 | ||
458 | break; | |
459 | } | |
460 | ||
6c7a4dfd JJ |
461 | if (openmp_flag && at_bol) |
462 | openmp_flag = 0; | |
63645982 | 463 | gfc_current_locus = start; |
0d3abf6f | 464 | return false; |
6de9cd9a DN |
465 | } |
466 | ||
467 | ||
468 | /* Skip comment lines in fixed source mode. We have the same rules as | |
469 | in skip_free_comment(), except that we can have a 'c', 'C' or '*' | |
e0bcf78c TS |
470 | in column 1, and a '!' cannot be in column 6. Also, we deal with |
471 | lines with 'd' or 'D' in column 1, if the user requested this. */ | |
6de9cd9a DN |
472 | |
473 | static void | |
474 | skip_fixed_comments (void) | |
475 | { | |
476 | locus start; | |
477 | int col; | |
478 | char c; | |
479 | ||
6c7a4dfd JJ |
480 | if (! gfc_at_bol ()) |
481 | { | |
482 | start = gfc_current_locus; | |
483 | if (! gfc_at_eof ()) | |
484 | { | |
485 | do | |
486 | c = next_char (); | |
487 | while (gfc_is_whitespace (c)); | |
488 | ||
489 | if (c == '\n') | |
490 | gfc_advance_line (); | |
491 | else if (c == '!') | |
492 | skip_comment_line (); | |
493 | } | |
494 | ||
495 | if (! gfc_at_bol ()) | |
496 | { | |
497 | gfc_current_locus = start; | |
498 | return; | |
499 | } | |
500 | } | |
501 | ||
6de9cd9a DN |
502 | for (;;) |
503 | { | |
63645982 | 504 | start = gfc_current_locus; |
6de9cd9a DN |
505 | if (gfc_at_eof ()) |
506 | break; | |
507 | ||
508 | c = next_char (); | |
509 | if (c == '\n') | |
510 | { | |
511 | gfc_advance_line (); | |
512 | continue; | |
513 | } | |
514 | ||
515 | if (c == '!' || c == 'c' || c == 'C' || c == '*') | |
516 | { | |
6c7a4dfd JJ |
517 | /* If -fopenmp, we need to handle here 2 things: |
518 | 1) don't treat !$omp|c$omp|*$omp as comments, but directives | |
519 | 2) handle OpenMP conditional compilation, where | |
520 | !$|c$|*$ should be treated as 2 spaces if the characters | |
521 | in columns 3 to 6 are valid fixed form label columns | |
522 | characters. */ | |
523 | if (gfc_option.flag_openmp) | |
524 | { | |
525 | if (next_char () == '$') | |
526 | { | |
527 | c = next_char (); | |
528 | if (c == 'o' || c == 'O') | |
529 | { | |
530 | if (((c = next_char ()) == 'm' || c == 'M') | |
531 | && ((c = next_char ()) == 'p' || c == 'P')) | |
532 | { | |
533 | c = next_char (); | |
534 | if (c != '\n' | |
535 | && ((openmp_flag && continue_flag) | |
536 | || c == ' ' || c == '0')) | |
537 | { | |
538 | c = next_char (); | |
539 | while (gfc_is_whitespace (c)) | |
540 | c = next_char (); | |
541 | if (c != '\n' && c != '!') | |
542 | { | |
543 | /* Canonicalize to *$omp. */ | |
544 | *start.nextc = '*'; | |
545 | openmp_flag = 1; | |
546 | gfc_current_locus = start; | |
547 | return; | |
548 | } | |
549 | } | |
550 | } | |
551 | } | |
552 | else | |
553 | { | |
554 | int digit_seen = 0; | |
555 | ||
556 | for (col = 3; col < 6; col++, c = next_char ()) | |
557 | if (c == ' ') | |
558 | continue; | |
559 | else if (c < '0' || c > '9') | |
560 | break; | |
561 | else | |
562 | digit_seen = 1; | |
563 | ||
564 | if (col == 6 && c != '\n' | |
565 | && ((continue_flag && !digit_seen) | |
566 | || c == ' ' || c == '0')) | |
567 | { | |
568 | gfc_current_locus = start; | |
569 | start.nextc[0] = ' '; | |
570 | start.nextc[1] = ' '; | |
571 | continue; | |
572 | } | |
573 | } | |
574 | } | |
575 | gfc_current_locus = start; | |
576 | } | |
6de9cd9a DN |
577 | skip_comment_line (); |
578 | continue; | |
579 | } | |
580 | ||
e0bcf78c TS |
581 | if (gfc_option.flag_d_lines != -1 && (c == 'd' || c == 'D')) |
582 | { | |
583 | if (gfc_option.flag_d_lines == 0) | |
584 | { | |
585 | skip_comment_line (); | |
586 | continue; | |
587 | } | |
588 | else | |
589 | *start.nextc = c = ' '; | |
590 | } | |
591 | ||
6de9cd9a | 592 | col = 1; |
e0bcf78c TS |
593 | |
594 | while (gfc_is_whitespace (c)) | |
6de9cd9a DN |
595 | { |
596 | c = next_char (); | |
597 | col++; | |
598 | } | |
6de9cd9a DN |
599 | |
600 | if (c == '\n') | |
601 | { | |
602 | gfc_advance_line (); | |
603 | continue; | |
604 | } | |
605 | ||
606 | if (col != 6 && c == '!') | |
607 | { | |
608 | skip_comment_line (); | |
609 | continue; | |
610 | } | |
611 | ||
612 | break; | |
613 | } | |
614 | ||
6c7a4dfd | 615 | openmp_flag = 0; |
63645982 | 616 | gfc_current_locus = start; |
6de9cd9a DN |
617 | } |
618 | ||
619 | ||
6c7a4dfd | 620 | /* Skips the current line if it is a comment. */ |
6de9cd9a DN |
621 | |
622 | void | |
623 | gfc_skip_comments (void) | |
624 | { | |
6c7a4dfd | 625 | if (gfc_current_form == FORM_FREE) |
6de9cd9a DN |
626 | skip_free_comments (); |
627 | else | |
628 | skip_fixed_comments (); | |
629 | } | |
630 | ||
631 | ||
632 | /* Get the next character from the input, taking continuation lines | |
633 | and end-of-line comments into account. This implies that comment | |
634 | lines between continued lines must be eaten here. For higher-level | |
635 | subroutines, this flattens continued lines into a single logical | |
636 | line. The in_string flag denotes whether we're inside a character | |
637 | context or not. */ | |
638 | ||
639 | int | |
640 | gfc_next_char_literal (int in_string) | |
641 | { | |
642 | locus old_loc; | |
6c7a4dfd | 643 | int i, c, prev_openmp_flag; |
6de9cd9a DN |
644 | |
645 | continue_flag = 0; | |
646 | ||
647 | restart: | |
648 | c = next_char (); | |
649 | if (gfc_at_end ()) | |
5a06474c JD |
650 | { |
651 | continue_count = 0; | |
652 | return c; | |
653 | } | |
6de9cd9a | 654 | |
d4fa05b9 | 655 | if (gfc_current_form == FORM_FREE) |
6de9cd9a | 656 | { |
0d3abf6f JJ |
657 | bool openmp_cond_flag; |
658 | ||
6de9cd9a DN |
659 | if (!in_string && c == '!') |
660 | { | |
6c7a4dfd JJ |
661 | if (openmp_flag |
662 | && memcmp (&gfc_current_locus, &openmp_locus, | |
663 | sizeof (gfc_current_locus)) == 0) | |
664 | goto done; | |
665 | ||
6de9cd9a DN |
666 | /* This line can't be continued */ |
667 | do | |
668 | { | |
669 | c = next_char (); | |
670 | } | |
671 | while (c != '\n'); | |
672 | ||
a34938be RG |
673 | /* Avoid truncation warnings for comment ending lines. */ |
674 | gfc_current_locus.lb->truncated = 0; | |
675 | ||
6de9cd9a DN |
676 | goto done; |
677 | } | |
678 | ||
679 | if (c != '&') | |
680 | goto done; | |
681 | ||
682 | /* If the next nonblank character is a ! or \n, we've got a | |
6c7a4dfd | 683 | continuation line. */ |
63645982 | 684 | old_loc = gfc_current_locus; |
6de9cd9a DN |
685 | |
686 | c = next_char (); | |
687 | while (gfc_is_whitespace (c)) | |
688 | c = next_char (); | |
689 | ||
690 | /* Character constants to be continued cannot have commentary | |
6c7a4dfd | 691 | after the '&'. */ |
6de9cd9a DN |
692 | |
693 | if (in_string && c != '\n') | |
694 | { | |
63645982 | 695 | gfc_current_locus = old_loc; |
6de9cd9a DN |
696 | c = '&'; |
697 | goto done; | |
698 | } | |
699 | ||
700 | if (c != '!' && c != '\n') | |
701 | { | |
63645982 | 702 | gfc_current_locus = old_loc; |
6de9cd9a DN |
703 | c = '&'; |
704 | goto done; | |
705 | } | |
706 | ||
6c7a4dfd | 707 | prev_openmp_flag = openmp_flag; |
6de9cd9a DN |
708 | continue_flag = 1; |
709 | if (c == '!') | |
710 | skip_comment_line (); | |
711 | else | |
712 | gfc_advance_line (); | |
0267ffdc JD |
713 | |
714 | if (gfc_at_eof()) | |
715 | goto not_continuation; | |
6de9cd9a | 716 | |
5a06474c JD |
717 | /* We've got a continuation line. If we are on the very next line after |
718 | the last continuation, increment the continuation line count and | |
719 | check whether the limit has been exceeded. */ | |
5ffeb913 | 720 | if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1) |
5a06474c JD |
721 | { |
722 | if (++continue_count == gfc_option.max_continue_free) | |
723 | { | |
edf1eac2 SK |
724 | if (gfc_notification_std (GFC_STD_GNU) || pedantic) |
725 | gfc_warning ("Limit of %d continuations exceeded in " | |
726 | "statement at %C", gfc_option.max_continue_free); | |
5a06474c JD |
727 | } |
728 | } | |
5ffeb913 | 729 | continue_line = gfc_linebuf_linenum (gfc_current_locus.lb); |
5a06474c JD |
730 | |
731 | /* Now find where it continues. First eat any comment lines. */ | |
0d3abf6f | 732 | openmp_cond_flag = skip_free_comments (); |
6de9cd9a | 733 | |
6c7a4dfd JJ |
734 | if (prev_openmp_flag != openmp_flag) |
735 | { | |
736 | gfc_current_locus = old_loc; | |
737 | openmp_flag = prev_openmp_flag; | |
738 | c = '&'; | |
739 | goto done; | |
740 | } | |
741 | ||
6de9cd9a | 742 | /* Now that we have a non-comment line, probe ahead for the |
6c7a4dfd JJ |
743 | first non-whitespace character. If it is another '&', then |
744 | reading starts at the next character, otherwise we must back | |
745 | up to where the whitespace started and resume from there. */ | |
6de9cd9a | 746 | |
63645982 | 747 | old_loc = gfc_current_locus; |
6de9cd9a DN |
748 | |
749 | c = next_char (); | |
750 | while (gfc_is_whitespace (c)) | |
751 | c = next_char (); | |
752 | ||
6c7a4dfd JJ |
753 | if (openmp_flag) |
754 | { | |
755 | for (i = 0; i < 5; i++, c = next_char ()) | |
756 | { | |
757 | gcc_assert (TOLOWER (c) == "!$omp"[i]); | |
758 | if (i == 4) | |
759 | old_loc = gfc_current_locus; | |
760 | } | |
761 | while (gfc_is_whitespace (c)) | |
762 | c = next_char (); | |
763 | } | |
764 | ||
6de9cd9a | 765 | if (c != '&') |
3fbab549 | 766 | { |
5a06474c JD |
767 | if (in_string) |
768 | { | |
769 | if (gfc_option.warn_ampersand) | |
edf1eac2 SK |
770 | gfc_warning_now ("Missing '&' in continued character " |
771 | "constant at %C"); | |
5a06474c JD |
772 | gfc_current_locus.nextc--; |
773 | } | |
0d3abf6f JJ |
774 | /* Both !$omp and !$ -fopenmp continuation lines have & on the |
775 | continuation line only optionally. */ | |
776 | else if (openmp_flag || openmp_cond_flag) | |
777 | gfc_current_locus.nextc--; | |
5a06474c JD |
778 | else |
779 | { | |
780 | c = ' '; | |
781 | gfc_current_locus = old_loc; | |
782 | goto done; | |
783 | } | |
3fbab549 | 784 | } |
6de9cd9a DN |
785 | } |
786 | else | |
787 | { | |
788 | /* Fixed form continuation. */ | |
789 | if (!in_string && c == '!') | |
790 | { | |
791 | /* Skip comment at end of line. */ | |
792 | do | |
793 | { | |
794 | c = next_char (); | |
795 | } | |
796 | while (c != '\n'); | |
a34938be RG |
797 | |
798 | /* Avoid truncation warnings for comment ending lines. */ | |
799 | gfc_current_locus.lb->truncated = 0; | |
6de9cd9a DN |
800 | } |
801 | ||
802 | if (c != '\n') | |
803 | goto done; | |
804 | ||
6c7a4dfd | 805 | prev_openmp_flag = openmp_flag; |
6de9cd9a | 806 | continue_flag = 1; |
63645982 | 807 | old_loc = gfc_current_locus; |
6de9cd9a DN |
808 | |
809 | gfc_advance_line (); | |
0d3abf6f | 810 | skip_fixed_comments (); |
6de9cd9a DN |
811 | |
812 | /* See if this line is a continuation line. */ | |
6c7a4dfd | 813 | if (openmp_flag != prev_openmp_flag) |
6de9cd9a | 814 | { |
6c7a4dfd JJ |
815 | openmp_flag = prev_openmp_flag; |
816 | goto not_continuation; | |
6de9cd9a DN |
817 | } |
818 | ||
6c7a4dfd JJ |
819 | if (!openmp_flag) |
820 | for (i = 0; i < 5; i++) | |
821 | { | |
822 | c = next_char (); | |
823 | if (c != ' ') | |
824 | goto not_continuation; | |
825 | } | |
826 | else | |
827 | for (i = 0; i < 5; i++) | |
828 | { | |
829 | c = next_char (); | |
830 | if (TOLOWER (c) != "*$omp"[i]) | |
831 | goto not_continuation; | |
832 | } | |
833 | ||
6de9cd9a | 834 | c = next_char (); |
6c7a4dfd | 835 | if (c == '0' || c == ' ' || c == '\n') |
6de9cd9a | 836 | goto not_continuation; |
5a06474c JD |
837 | |
838 | /* We've got a continuation line. If we are on the very next line after | |
839 | the last continuation, increment the continuation line count and | |
840 | check whether the limit has been exceeded. */ | |
5ffeb913 | 841 | if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1) |
5a06474c JD |
842 | { |
843 | if (++continue_count == gfc_option.max_continue_fixed) | |
844 | { | |
edf1eac2 SK |
845 | if (gfc_notification_std (GFC_STD_GNU) || pedantic) |
846 | gfc_warning ("Limit of %d continuations exceeded in " | |
847 | "statement at %C", | |
848 | gfc_option.max_continue_fixed); | |
5a06474c JD |
849 | } |
850 | } | |
851 | ||
5ffeb913 TT |
852 | if (continue_line < gfc_linebuf_linenum (gfc_current_locus.lb)) |
853 | continue_line = gfc_linebuf_linenum (gfc_current_locus.lb); | |
6de9cd9a DN |
854 | } |
855 | ||
856 | /* Ready to read first character of continuation line, which might | |
857 | be another continuation line! */ | |
858 | goto restart; | |
859 | ||
860 | not_continuation: | |
861 | c = '\n'; | |
63645982 | 862 | gfc_current_locus = old_loc; |
6de9cd9a DN |
863 | |
864 | done: | |
5a06474c JD |
865 | if (c == '\n') |
866 | continue_count = 0; | |
6de9cd9a DN |
867 | continue_flag = 0; |
868 | return c; | |
869 | } | |
870 | ||
871 | ||
872 | /* Get the next character of input, folded to lowercase. In fixed | |
873 | form mode, we also ignore spaces. When matcher subroutines are | |
874 | parsing character literals, they have to call | |
875 | gfc_next_char_literal(). */ | |
876 | ||
877 | int | |
878 | gfc_next_char (void) | |
879 | { | |
880 | int c; | |
881 | ||
882 | do | |
883 | { | |
884 | c = gfc_next_char_literal (0); | |
885 | } | |
d4fa05b9 | 886 | while (gfc_current_form == FORM_FIXED && gfc_is_whitespace (c)); |
6de9cd9a DN |
887 | |
888 | return TOLOWER (c); | |
889 | } | |
890 | ||
891 | ||
892 | int | |
893 | gfc_peek_char (void) | |
894 | { | |
895 | locus old_loc; | |
896 | int c; | |
897 | ||
63645982 | 898 | old_loc = gfc_current_locus; |
6de9cd9a | 899 | c = gfc_next_char (); |
63645982 | 900 | gfc_current_locus = old_loc; |
6de9cd9a DN |
901 | |
902 | return c; | |
903 | } | |
904 | ||
905 | ||
906 | /* Recover from an error. We try to get past the current statement | |
907 | and get lined up for the next. The next statement follows a '\n' | |
908 | or a ';'. We also assume that we are not within a character | |
909 | constant, and deal with finding a '\'' or '"'. */ | |
910 | ||
911 | void | |
912 | gfc_error_recovery (void) | |
913 | { | |
914 | char c, delim; | |
915 | ||
916 | if (gfc_at_eof ()) | |
917 | return; | |
918 | ||
919 | for (;;) | |
920 | { | |
921 | c = gfc_next_char (); | |
922 | if (c == '\n' || c == ';') | |
923 | break; | |
924 | ||
925 | if (c != '\'' && c != '"') | |
926 | { | |
927 | if (gfc_at_eof ()) | |
928 | break; | |
929 | continue; | |
930 | } | |
931 | delim = c; | |
932 | ||
933 | for (;;) | |
934 | { | |
935 | c = next_char (); | |
936 | ||
937 | if (c == delim) | |
938 | break; | |
939 | if (c == '\n') | |
ba1defa5 | 940 | return; |
6de9cd9a DN |
941 | if (c == '\\') |
942 | { | |
943 | c = next_char (); | |
944 | if (c == '\n') | |
ba1defa5 | 945 | return; |
6de9cd9a DN |
946 | } |
947 | } | |
948 | if (gfc_at_eof ()) | |
949 | break; | |
950 | } | |
6de9cd9a DN |
951 | } |
952 | ||
953 | ||
954 | /* Read ahead until the next character to be read is not whitespace. */ | |
955 | ||
956 | void | |
957 | gfc_gobble_whitespace (void) | |
958 | { | |
840bd9f7 | 959 | static int linenum = 0; |
6de9cd9a DN |
960 | locus old_loc; |
961 | int c; | |
962 | ||
963 | do | |
964 | { | |
63645982 | 965 | old_loc = gfc_current_locus; |
6de9cd9a | 966 | c = gfc_next_char_literal (0); |
840bd9f7 SK |
967 | /* Issue a warning for nonconforming tabs. We keep track of the line |
968 | number because the Fortran matchers will often back up and the same | |
969 | line will be scanned multiple times. */ | |
45a82bd9 | 970 | if (!gfc_option.warn_tabs && c == '\t') |
840bd9f7 | 971 | { |
45a82bd9 PB |
972 | #ifdef USE_MAPPED_LOCATION |
973 | int cur_linenum = LOCATION_LINE (gfc_current_locus.lb->location); | |
974 | #else | |
975 | int cur_linenum = gfc_current_locus.lb->linenum; | |
976 | #endif | |
977 | if (cur_linenum != linenum) | |
978 | { | |
979 | linenum = cur_linenum; | |
980 | gfc_warning_now ("Nonconforming tab character at %C"); | |
981 | } | |
840bd9f7 | 982 | } |
6de9cd9a DN |
983 | } |
984 | while (gfc_is_whitespace (c)); | |
985 | ||
63645982 | 986 | gfc_current_locus = old_loc; |
6de9cd9a DN |
987 | } |
988 | ||
989 | ||
f56c5d5d TS |
990 | /* Load a single line into pbuf. |
991 | ||
992 | If pbuf points to a NULL pointer, it is allocated. | |
993 | We truncate lines that are too long, unless we're dealing with | |
994 | preprocessor lines or if the option -ffixed-line-length-none is set, | |
995 | in which case we reallocate the buffer to fit the entire line, if | |
996 | need be. | |
997 | In fixed mode, we expand a tab that occurs within the statement | |
998 | label region to expand to spaces that leave the next character in | |
ba1defa5 | 999 | the source region. |
1526c4b5 JD |
1000 | load_line returns whether the line was truncated. |
1001 | ||
1002 | NOTE: The error machinery isn't available at this point, so we can't | |
1003 | easily report line and column numbers consistent with other | |
1004 | parts of gfortran. */ | |
6de9cd9a | 1005 | |
ba1defa5 | 1006 | static int |
edf1eac2 | 1007 | load_line (FILE *input, char **pbuf, int *pbuflen) |
6de9cd9a | 1008 | { |
840bd9f7 | 1009 | static int linenum = 0, current_line = 1; |
d1e3d6ae | 1010 | int c, maxlen, i, preprocessor_flag, buflen = *pbuflen; |
840bd9f7 | 1011 | int trunc_flag = 0, seen_comment = 0; |
1526c4b5 | 1012 | int seen_printable = 0, seen_ampersand = 0; |
f56c5d5d TS |
1013 | char *buffer; |
1014 | ||
1dde8683 | 1015 | /* Determine the maximum allowed line length. */ |
f56c5d5d | 1016 | if (gfc_current_form == FORM_FREE) |
1dde8683 | 1017 | maxlen = gfc_option.free_line_length; |
16ab8e74 | 1018 | else if (gfc_current_form == FORM_FIXED) |
1dde8683 | 1019 | maxlen = gfc_option.fixed_line_length; |
f56c5d5d | 1020 | else |
16ab8e74 | 1021 | maxlen = 72; |
f56c5d5d TS |
1022 | |
1023 | if (*pbuf == NULL) | |
1024 | { | |
1dde8683 BM |
1025 | /* Allocate the line buffer, storing its length into buflen. |
1026 | Note that if maxlen==0, indicating that arbitrary-length lines | |
1027 | are allowed, the buffer will be reallocated if this length is | |
1028 | insufficient; since 132 characters is the length of a standard | |
1029 | free-form line, we use that as a starting guess. */ | |
f56c5d5d TS |
1030 | if (maxlen > 0) |
1031 | buflen = maxlen; | |
1032 | else | |
1dde8683 | 1033 | buflen = 132; |
6de9cd9a | 1034 | |
f56c5d5d TS |
1035 | *pbuf = gfc_getmem (buflen + 1); |
1036 | } | |
6de9cd9a DN |
1037 | |
1038 | i = 0; | |
f56c5d5d | 1039 | buffer = *pbuf; |
6de9cd9a | 1040 | |
fa841200 | 1041 | preprocessor_flag = 0; |
c4da1827 | 1042 | c = getc (input); |
fa841200 | 1043 | if (c == '#') |
f56c5d5d TS |
1044 | /* In order to not truncate preprocessor lines, we have to |
1045 | remember that this is one. */ | |
fa841200 TS |
1046 | preprocessor_flag = 1; |
1047 | ungetc (c, input); | |
1048 | ||
6de9cd9a DN |
1049 | for (;;) |
1050 | { | |
c4da1827 | 1051 | c = getc (input); |
6de9cd9a DN |
1052 | |
1053 | if (c == EOF) | |
1054 | break; | |
1055 | if (c == '\n') | |
1526c4b5 JD |
1056 | { |
1057 | /* Check for illegal use of ampersand. See F95 Standard 3.3.1.3. */ | |
1058 | if (gfc_current_form == FORM_FREE | |
c284e499 | 1059 | && !seen_printable && seen_ampersand) |
1526c4b5 JD |
1060 | { |
1061 | if (pedantic) | |
edf1eac2 SK |
1062 | gfc_error_now ("'&' not allowed by itself in line %d", |
1063 | current_line); | |
1526c4b5 | 1064 | else |
edf1eac2 SK |
1065 | gfc_warning_now ("'&' not allowed by itself in line %d", |
1066 | current_line); | |
1526c4b5 JD |
1067 | } |
1068 | break; | |
1069 | } | |
6de9cd9a DN |
1070 | |
1071 | if (c == '\r') | |
d4fa05b9 | 1072 | continue; /* Gobble characters. */ |
6de9cd9a DN |
1073 | if (c == '\0') |
1074 | continue; | |
1075 | ||
1526c4b5 | 1076 | if (c == '&') |
1526c4b5 | 1077 | { |
c284e499 JD |
1078 | if (seen_ampersand) |
1079 | seen_ampersand = 0; | |
1526c4b5 | 1080 | else |
c284e499 | 1081 | seen_ampersand = 1; |
1526c4b5 JD |
1082 | } |
1083 | ||
c284e499 JD |
1084 | if ((c != '&' && c != '!') || (c == '!' && !seen_ampersand)) |
1085 | seen_printable = 1; | |
1086 | ||
840bd9f7 SK |
1087 | /* Is this a fixed-form comment? */ |
1088 | if (gfc_current_form == FORM_FIXED && i == 0 | |
1089 | && (c == '*' || c == 'c' || c == 'd')) | |
1090 | seen_comment = 1; | |
1091 | ||
d4fa05b9 | 1092 | if (gfc_current_form == FORM_FIXED && c == '\t' && i <= 6) |
840bd9f7 | 1093 | { |
840bd9f7 SK |
1094 | if (!gfc_option.warn_tabs && seen_comment == 0 |
1095 | && current_line != linenum) | |
1096 | { | |
1097 | linenum = current_line; | |
edf1eac2 SK |
1098 | gfc_warning_now ("Nonconforming tab character in column 1 " |
1099 | "of line %d", linenum); | |
840bd9f7 SK |
1100 | } |
1101 | ||
6de9cd9a DN |
1102 | while (i <= 6) |
1103 | { | |
1104 | *buffer++ = ' '; | |
1105 | i++; | |
1106 | } | |
1107 | ||
1108 | continue; | |
1109 | } | |
1110 | ||
1111 | *buffer++ = c; | |
1112 | i++; | |
1113 | ||
d1e3d6ae | 1114 | if (maxlen == 0 || preprocessor_flag) |
f56c5d5d | 1115 | { |
d1e3d6ae JJ |
1116 | if (i >= buflen) |
1117 | { | |
1118 | /* Reallocate line buffer to double size to hold the | |
3fbab549 | 1119 | overlong line. */ |
d1e3d6ae JJ |
1120 | buflen = buflen * 2; |
1121 | *pbuf = xrealloc (*pbuf, buflen + 1); | |
edf1eac2 | 1122 | buffer = (*pbuf) + i; |
d1e3d6ae | 1123 | } |
f56c5d5d | 1124 | } |
d1e3d6ae | 1125 | else if (i >= maxlen) |
16ab8e74 | 1126 | { |
f56c5d5d | 1127 | /* Truncate the rest of the line. */ |
6de9cd9a DN |
1128 | for (;;) |
1129 | { | |
c4da1827 | 1130 | c = getc (input); |
6de9cd9a DN |
1131 | if (c == '\n' || c == EOF) |
1132 | break; | |
a34938be RG |
1133 | |
1134 | trunc_flag = 1; | |
6de9cd9a DN |
1135 | } |
1136 | ||
1137 | ungetc ('\n', input); | |
1138 | } | |
1139 | } | |
1140 | ||
f56c5d5d TS |
1141 | /* Pad lines to the selected line length in fixed form. */ |
1142 | if (gfc_current_form == FORM_FIXED | |
043c2d9e | 1143 | && gfc_option.fixed_line_length != 0 |
f56c5d5d TS |
1144 | && !preprocessor_flag |
1145 | && c != EOF) | |
043c2d9e BF |
1146 | { |
1147 | while (i++ < maxlen) | |
1148 | *buffer++ = ' '; | |
1149 | } | |
f56c5d5d | 1150 | |
6de9cd9a | 1151 | *buffer = '\0'; |
d1e3d6ae | 1152 | *pbuflen = buflen; |
840bd9f7 | 1153 | current_line++; |
ba1defa5 RG |
1154 | |
1155 | return trunc_flag; | |
6de9cd9a DN |
1156 | } |
1157 | ||
1158 | ||
d4fa05b9 TS |
1159 | /* Get a gfc_file structure, initialize it and add it to |
1160 | the file stack. */ | |
1161 | ||
1162 | static gfc_file * | |
e0bcf78c | 1163 | get_file (const char *name, enum lc_reason reason ATTRIBUTE_UNUSED) |
d4fa05b9 TS |
1164 | { |
1165 | gfc_file *f; | |
1166 | ||
1167 | f = gfc_getmem (sizeof (gfc_file)); | |
1168 | ||
1169 | f->filename = gfc_getmem (strlen (name) + 1); | |
1170 | strcpy (f->filename, name); | |
1171 | ||
1172 | f->next = file_head; | |
1173 | file_head = f; | |
1174 | ||
1175 | f->included_by = current_file; | |
1176 | if (current_file != NULL) | |
1177 | f->inclusion_line = current_file->line; | |
1178 | ||
c8cc8542 | 1179 | #ifdef USE_MAPPED_LOCATION |
5ffeb913 | 1180 | linemap_add (line_table, reason, false, f->filename, 1); |
c8cc8542 PB |
1181 | #endif |
1182 | ||
d4fa05b9 TS |
1183 | return f; |
1184 | } | |
1185 | ||
1186 | /* Deal with a line from the C preprocessor. The | |
1187 | initial octothorp has already been seen. */ | |
6de9cd9a DN |
1188 | |
1189 | static void | |
d4fa05b9 | 1190 | preprocessor_line (char *c) |
6de9cd9a | 1191 | { |
d4fa05b9 TS |
1192 | bool flag[5]; |
1193 | int i, line; | |
1194 | char *filename; | |
1195 | gfc_file *f; | |
2d7c7df6 | 1196 | int escaped, unescape; |
6de9cd9a | 1197 | |
d4fa05b9 TS |
1198 | c++; |
1199 | while (*c == ' ' || *c == '\t') | |
1200 | c++; | |
6de9cd9a | 1201 | |
d4fa05b9 | 1202 | if (*c < '0' || *c > '9') |
fa841200 | 1203 | goto bad_cpp_line; |
6de9cd9a | 1204 | |
d4fa05b9 TS |
1205 | line = atoi (c); |
1206 | ||
4c3a6ca1 | 1207 | c = strchr (c, ' '); |
fa841200 | 1208 | if (c == NULL) |
4c3a6ca1 JJ |
1209 | { |
1210 | /* No file name given. Set new line number. */ | |
1211 | current_file->line = line; | |
1212 | return; | |
1213 | } | |
d7d528c8 ES |
1214 | |
1215 | /* Skip spaces. */ | |
1216 | while (*c == ' ' || *c == '\t') | |
1217 | c++; | |
1218 | ||
1219 | /* Skip quote. */ | |
1220 | if (*c != '"') | |
fa841200 | 1221 | goto bad_cpp_line; |
d7d528c8 ES |
1222 | ++c; |
1223 | ||
d4fa05b9 TS |
1224 | filename = c; |
1225 | ||
d7d528c8 | 1226 | /* Make filename end at quote. */ |
2d7c7df6 | 1227 | unescape = 0; |
d7d528c8 | 1228 | escaped = false; |
edf1eac2 | 1229 | while (*c && ! (!escaped && *c == '"')) |
d7d528c8 ES |
1230 | { |
1231 | if (escaped) | |
edf1eac2 | 1232 | escaped = false; |
2d7c7df6 JJ |
1233 | else if (*c == '\\') |
1234 | { | |
1235 | escaped = true; | |
1236 | unescape++; | |
1237 | } | |
d7d528c8 ES |
1238 | ++c; |
1239 | } | |
1240 | ||
1241 | if (! *c) | |
fa841200 TS |
1242 | /* Preprocessor line has no closing quote. */ |
1243 | goto bad_cpp_line; | |
d7d528c8 | 1244 | |
d4fa05b9 TS |
1245 | *c++ = '\0'; |
1246 | ||
2d7c7df6 JJ |
1247 | /* Undo effects of cpp_quote_string. */ |
1248 | if (unescape) | |
1249 | { | |
1250 | char *s = filename; | |
1251 | char *d = gfc_getmem (c - filename - unescape); | |
d7d528c8 | 1252 | |
2d7c7df6 JJ |
1253 | filename = d; |
1254 | while (*s) | |
1255 | { | |
1256 | if (*s == '\\') | |
1257 | *d++ = *++s; | |
1258 | else | |
1259 | *d++ = *s; | |
1260 | s++; | |
1261 | } | |
1262 | *d = '\0'; | |
1263 | } | |
d7d528c8 | 1264 | |
d4fa05b9 | 1265 | /* Get flags. */ |
4c3a6ca1 | 1266 | |
1e39a151 | 1267 | flag[1] = flag[2] = flag[3] = flag[4] = false; |
6de9cd9a | 1268 | |
6de9cd9a DN |
1269 | for (;;) |
1270 | { | |
d4fa05b9 TS |
1271 | c = strchr (c, ' '); |
1272 | if (c == NULL) | |
1273 | break; | |
6de9cd9a | 1274 | |
d4fa05b9 TS |
1275 | c++; |
1276 | i = atoi (c); | |
6de9cd9a | 1277 | |
d4fa05b9 TS |
1278 | if (1 <= i && i <= 4) |
1279 | flag[i] = true; | |
1280 | } | |
4c3a6ca1 | 1281 | |
d4fa05b9 | 1282 | /* Interpret flags. */ |
4c3a6ca1 | 1283 | |
94b00ee4 | 1284 | if (flag[1]) /* Starting new file. */ |
d4fa05b9 | 1285 | { |
c8cc8542 | 1286 | f = get_file (filename, LC_RENAME); |
d4fa05b9 TS |
1287 | f->up = current_file; |
1288 | current_file = f; | |
1289 | } | |
4c3a6ca1 | 1290 | |
d4fa05b9 TS |
1291 | if (flag[2]) /* Ending current file. */ |
1292 | { | |
94b00ee4 JJ |
1293 | if (!current_file->up |
1294 | || strcmp (current_file->up->filename, filename) != 0) | |
4c3a6ca1 JJ |
1295 | { |
1296 | gfc_warning_now ("%s:%d: file %s left but not entered", | |
1297 | current_file->filename, current_file->line, | |
1298 | filename); | |
2d7c7df6 JJ |
1299 | if (unescape) |
1300 | gfc_free (filename); | |
4c3a6ca1 JJ |
1301 | return; |
1302 | } | |
ee07457b | 1303 | |
94b00ee4 | 1304 | current_file = current_file->up; |
ee07457b FXC |
1305 | #ifdef USE_MAPPED_LOCATION |
1306 | linemap_add (line_table, LC_RENAME, false, current_file->filename, | |
1307 | current_file->line); | |
1308 | #endif | |
d4fa05b9 | 1309 | } |
4c3a6ca1 | 1310 | |
d4fa05b9 TS |
1311 | /* The name of the file can be a temporary file produced by |
1312 | cpp. Replace the name if it is different. */ | |
4c3a6ca1 | 1313 | |
d4fa05b9 TS |
1314 | if (strcmp (current_file->filename, filename) != 0) |
1315 | { | |
1316 | gfc_free (current_file->filename); | |
1317 | current_file->filename = gfc_getmem (strlen (filename) + 1); | |
1318 | strcpy (current_file->filename, filename); | |
1319 | } | |
fa841200 | 1320 | |
4c3a6ca1 JJ |
1321 | /* Set new line number. */ |
1322 | current_file->line = line; | |
2d7c7df6 JJ |
1323 | if (unescape) |
1324 | gfc_free (filename); | |
fa841200 TS |
1325 | return; |
1326 | ||
1327 | bad_cpp_line: | |
4c3a6ca1 | 1328 | gfc_warning_now ("%s:%d: Illegal preprocessor directive", |
fa841200 TS |
1329 | current_file->filename, current_file->line); |
1330 | current_file->line++; | |
d4fa05b9 TS |
1331 | } |
1332 | ||
1333 | ||
e0bcf78c | 1334 | static try load_file (const char *, bool); |
d4fa05b9 TS |
1335 | |
1336 | /* include_line()-- Checks a line buffer to see if it is an include | |
1337 | line. If so, we call load_file() recursively to load the included | |
1338 | file. We never return a syntax error because a statement like | |
1339 | "include = 5" is perfectly legal. We return false if no include was | |
1340 | processed or true if we matched an include. */ | |
1341 | ||
1342 | static bool | |
1343 | include_line (char *line) | |
1344 | { | |
1345 | char quote, *c, *begin, *stop; | |
9b9e4cd6 | 1346 | |
d4fa05b9 | 1347 | c = line; |
9b9e4cd6 JJ |
1348 | |
1349 | if (gfc_option.flag_openmp) | |
1350 | { | |
1351 | if (gfc_current_form == FORM_FREE) | |
1352 | { | |
1353 | while (*c == ' ' || *c == '\t') | |
1354 | c++; | |
1355 | if (*c == '!' && c[1] == '$' && (c[2] == ' ' || c[2] == '\t')) | |
1356 | c += 3; | |
1357 | } | |
1358 | else | |
1359 | { | |
1360 | if ((*c == '!' || *c == 'c' || *c == 'C' || *c == '*') | |
1361 | && c[1] == '$' && (c[2] == ' ' || c[2] == '\t')) | |
1362 | c += 3; | |
1363 | } | |
1364 | } | |
1365 | ||
d4fa05b9 TS |
1366 | while (*c == ' ' || *c == '\t') |
1367 | c++; | |
1368 | ||
1369 | if (strncasecmp (c, "include", 7)) | |
1370 | return false; | |
1371 | ||
1372 | c += 7; | |
1373 | while (*c == ' ' || *c == '\t') | |
1374 | c++; | |
1375 | ||
1376 | /* Find filename between quotes. */ | |
1377 | ||
1378 | quote = *c++; | |
1379 | if (quote != '"' && quote != '\'') | |
1380 | return false; | |
1381 | ||
1382 | begin = c; | |
1383 | ||
1384 | while (*c != quote && *c != '\0') | |
1385 | c++; | |
1386 | ||
1387 | if (*c == '\0') | |
1388 | return false; | |
1389 | ||
1390 | stop = c++; | |
1391 | ||
1392 | while (*c == ' ' || *c == '\t') | |
1393 | c++; | |
1394 | ||
1395 | if (*c != '\0' && *c != '!') | |
1396 | return false; | |
1397 | ||
f7b529fa | 1398 | /* We have an include line at this point. */ |
d4fa05b9 TS |
1399 | |
1400 | *stop = '\0'; /* It's ok to trash the buffer, as this line won't be | |
1401 | read by anything else. */ | |
1402 | ||
1403 | load_file (begin, false); | |
1404 | return true; | |
1405 | } | |
1406 | ||
edf1eac2 | 1407 | |
d4fa05b9 TS |
1408 | /* Load a file into memory by calling load_line until the file ends. */ |
1409 | ||
1410 | static try | |
e0bcf78c | 1411 | load_file (const char *filename, bool initial) |
d4fa05b9 | 1412 | { |
f56c5d5d | 1413 | char *line; |
d4fa05b9 TS |
1414 | gfc_linebuf *b; |
1415 | gfc_file *f; | |
1416 | FILE *input; | |
d1e3d6ae | 1417 | int len, line_len; |
caef7872 | 1418 | bool first_line; |
d4fa05b9 TS |
1419 | |
1420 | for (f = current_file; f; f = f->up) | |
1421 | if (strcmp (filename, f->filename) == 0) | |
1422 | { | |
1423 | gfc_error_now ("File '%s' is being included recursively", filename); | |
1424 | return FAILURE; | |
1425 | } | |
1426 | ||
1427 | if (initial) | |
1428 | { | |
2d7c7df6 JJ |
1429 | if (gfc_src_file) |
1430 | { | |
1431 | input = gfc_src_file; | |
1432 | gfc_src_file = NULL; | |
1433 | } | |
1434 | else | |
1435 | input = gfc_open_file (filename); | |
d4fa05b9 TS |
1436 | if (input == NULL) |
1437 | { | |
1438 | gfc_error_now ("Can't open file '%s'", filename); | |
1439 | return FAILURE; | |
1440 | } | |
1441 | } | |
1442 | else | |
1443 | { | |
31198773 | 1444 | input = gfc_open_included_file (filename, false, false); |
d4fa05b9 TS |
1445 | if (input == NULL) |
1446 | { | |
1447 | gfc_error_now ("Can't open included file '%s'", filename); | |
1448 | return FAILURE; | |
1449 | } | |
1450 | } | |
1451 | ||
1452 | /* Load the file. */ | |
1453 | ||
c8cc8542 | 1454 | f = get_file (filename, initial ? LC_RENAME : LC_ENTER); |
d4fa05b9 TS |
1455 | f->up = current_file; |
1456 | current_file = f; | |
1457 | current_file->line = 1; | |
f56c5d5d | 1458 | line = NULL; |
d1e3d6ae | 1459 | line_len = 0; |
caef7872 | 1460 | first_line = true; |
d4fa05b9 | 1461 | |
2d7c7df6 JJ |
1462 | if (initial && gfc_src_preprocessor_lines[0]) |
1463 | { | |
1464 | preprocessor_line (gfc_src_preprocessor_lines[0]); | |
1465 | gfc_free (gfc_src_preprocessor_lines[0]); | |
1466 | gfc_src_preprocessor_lines[0] = NULL; | |
1467 | if (gfc_src_preprocessor_lines[1]) | |
1468 | { | |
1469 | preprocessor_line (gfc_src_preprocessor_lines[1]); | |
1470 | gfc_free (gfc_src_preprocessor_lines[1]); | |
1471 | gfc_src_preprocessor_lines[1] = NULL; | |
1472 | } | |
1473 | } | |
1474 | ||
16ab8e74 | 1475 | for (;;) |
d4fa05b9 | 1476 | { |
d1e3d6ae | 1477 | int trunc = load_line (input, &line, &line_len); |
d4fa05b9 TS |
1478 | |
1479 | len = strlen (line); | |
6de9cd9a DN |
1480 | if (feof (input) && len == 0) |
1481 | break; | |
1482 | ||
caef7872 FXC |
1483 | /* If this is the first line of the file, it can contain a byte |
1484 | order mark (BOM), which we will ignore: | |
1485 | FF FE is UTF-16 little endian, | |
1486 | FE FF is UTF-16 big endian, | |
1487 | EF BB BF is UTF-8. */ | |
1488 | if (first_line | |
1489 | && ((line_len >= 2 && line[0] == '\xFF' && line[1] == '\xFE') | |
1490 | || (line_len >= 2 && line[0] == '\xFE' && line[1] == '\xFF') | |
1491 | || (line_len >= 3 && line[0] == '\xEF' && line[1] == '\xBB' | |
1492 | && line[2] == '\xBF'))) | |
1493 | { | |
1494 | int n = line[1] == '\xBB' ? 3 : 2; | |
1495 | char * new = gfc_getmem (line_len); | |
1496 | ||
1497 | strcpy (new, line + n); | |
1498 | gfc_free (line); | |
1499 | line = new; | |
1500 | len -= n; | |
1501 | } | |
1502 | ||
d4fa05b9 TS |
1503 | /* There are three things this line can be: a line of Fortran |
1504 | source, an include line or a C preprocessor directive. */ | |
6de9cd9a | 1505 | |
d4fa05b9 TS |
1506 | if (line[0] == '#') |
1507 | { | |
7b7bdf67 FXC |
1508 | preprocessor_line (line); |
1509 | continue; | |
d4fa05b9 | 1510 | } |
6de9cd9a | 1511 | |
caef7872 FXC |
1512 | /* Preprocessed files have preprocessor lines added before the byte |
1513 | order mark, so first_line is not about the first line of the file | |
1514 | but the first line that's not a preprocessor line. */ | |
1515 | first_line = false; | |
1516 | ||
d4fa05b9 TS |
1517 | if (include_line (line)) |
1518 | { | |
1519 | current_file->line++; | |
1520 | continue; | |
6de9cd9a DN |
1521 | } |
1522 | ||
d4fa05b9 TS |
1523 | /* Add line. */ |
1524 | ||
4cdf7223 | 1525 | b = gfc_getmem (gfc_linebuf_header_size + len + 1); |
d4fa05b9 | 1526 | |
c8cc8542 PB |
1527 | #ifdef USE_MAPPED_LOCATION |
1528 | b->location | |
5ffeb913 | 1529 | = linemap_line_start (line_table, current_file->line++, 120); |
c8cc8542 | 1530 | #else |
d4fa05b9 | 1531 | b->linenum = current_file->line++; |
c8cc8542 | 1532 | #endif |
d4fa05b9 | 1533 | b->file = current_file; |
ba1defa5 | 1534 | b->truncated = trunc; |
d4fa05b9 TS |
1535 | strcpy (b->line, line); |
1536 | ||
1537 | if (line_head == NULL) | |
1538 | line_head = b; | |
1539 | else | |
1540 | line_tail->next = b; | |
1541 | ||
1542 | line_tail = b; | |
6de9cd9a | 1543 | } |
d4fa05b9 | 1544 | |
f56c5d5d TS |
1545 | /* Release the line buffer allocated in load_line. */ |
1546 | gfc_free (line); | |
1547 | ||
d4fa05b9 TS |
1548 | fclose (input); |
1549 | ||
1550 | current_file = current_file->up; | |
c8cc8542 | 1551 | #ifdef USE_MAPPED_LOCATION |
5ffeb913 | 1552 | linemap_add (line_table, LC_LEAVE, 0, NULL, 0); |
c8cc8542 | 1553 | #endif |
d4fa05b9 | 1554 | return SUCCESS; |
6de9cd9a DN |
1555 | } |
1556 | ||
1557 | ||
d4fa05b9 TS |
1558 | /* Open a new file and start scanning from that file. Returns SUCCESS |
1559 | if everything went OK, FAILURE otherwise. If form == FORM_UKNOWN | |
1560 | it tries to determine the source form from the filename, defaulting | |
1561 | to free form. */ | |
6de9cd9a DN |
1562 | |
1563 | try | |
e0bcf78c | 1564 | gfc_new_file (void) |
6de9cd9a | 1565 | { |
d4fa05b9 | 1566 | try result; |
6de9cd9a | 1567 | |
d4fa05b9 | 1568 | result = load_file (gfc_source_file, true); |
6de9cd9a | 1569 | |
63645982 TS |
1570 | gfc_current_locus.lb = line_head; |
1571 | gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line; | |
6de9cd9a | 1572 | |
d4fa05b9 TS |
1573 | #if 0 /* Debugging aid. */ |
1574 | for (; line_head; line_head = line_head->next) | |
ee07457b | 1575 | gfc_status ("%s:%3d %s\n", |
c8cc8542 | 1576 | #ifdef USE_MAPPED_LOCATION |
ee07457b | 1577 | LOCATION_FILE (line_head->location), |
c8cc8542 PB |
1578 | LOCATION_LINE (line_head->location), |
1579 | #else | |
ee07457b | 1580 | line_head->file->filename, |
c8cc8542 PB |
1581 | line_head->linenum, |
1582 | #endif | |
1583 | line_head->line); | |
6de9cd9a | 1584 | |
d4fa05b9 TS |
1585 | exit (0); |
1586 | #endif | |
6de9cd9a | 1587 | |
d4fa05b9 | 1588 | return result; |
6de9cd9a | 1589 | } |
2d7c7df6 JJ |
1590 | |
1591 | static char * | |
1592 | unescape_filename (const char *ptr) | |
1593 | { | |
1594 | const char *p = ptr, *s; | |
1595 | char *d, *ret; | |
1596 | int escaped, unescape = 0; | |
1597 | ||
1598 | /* Make filename end at quote. */ | |
1599 | escaped = false; | |
1600 | while (*p && ! (! escaped && *p == '"')) | |
1601 | { | |
1602 | if (escaped) | |
1603 | escaped = false; | |
1604 | else if (*p == '\\') | |
1605 | { | |
1606 | escaped = true; | |
1607 | unescape++; | |
1608 | } | |
1609 | ++p; | |
1610 | } | |
1611 | ||
edf1eac2 | 1612 | if (!*p || p[1]) |
2d7c7df6 JJ |
1613 | return NULL; |
1614 | ||
1615 | /* Undo effects of cpp_quote_string. */ | |
1616 | s = ptr; | |
1617 | d = gfc_getmem (p + 1 - ptr - unescape); | |
1618 | ret = d; | |
1619 | ||
1620 | while (s != p) | |
1621 | { | |
1622 | if (*s == '\\') | |
1623 | *d++ = *++s; | |
1624 | else | |
1625 | *d++ = *s; | |
1626 | s++; | |
1627 | } | |
1628 | *d = '\0'; | |
1629 | return ret; | |
1630 | } | |
1631 | ||
1632 | /* For preprocessed files, if the first tokens are of the form # NUM. | |
1633 | handle the directives so we know the original file name. */ | |
1634 | ||
1635 | const char * | |
1636 | gfc_read_orig_filename (const char *filename, const char **canon_source_file) | |
1637 | { | |
1638 | int c, len; | |
1639 | char *dirname; | |
1640 | ||
1641 | gfc_src_file = gfc_open_file (filename); | |
1642 | if (gfc_src_file == NULL) | |
1643 | return NULL; | |
1644 | ||
c4da1827 | 1645 | c = getc (gfc_src_file); |
2d7c7df6 JJ |
1646 | ungetc (c, gfc_src_file); |
1647 | ||
1648 | if (c != '#') | |
1649 | return NULL; | |
1650 | ||
1651 | len = 0; | |
1652 | load_line (gfc_src_file, &gfc_src_preprocessor_lines[0], &len); | |
1653 | ||
1654 | if (strncmp (gfc_src_preprocessor_lines[0], "# 1 \"", 5) != 0) | |
1655 | return NULL; | |
1656 | ||
1657 | filename = unescape_filename (gfc_src_preprocessor_lines[0] + 5); | |
1658 | if (filename == NULL) | |
1659 | return NULL; | |
1660 | ||
c4da1827 | 1661 | c = getc (gfc_src_file); |
2d7c7df6 JJ |
1662 | ungetc (c, gfc_src_file); |
1663 | ||
1664 | if (c != '#') | |
1665 | return filename; | |
1666 | ||
1667 | len = 0; | |
1668 | load_line (gfc_src_file, &gfc_src_preprocessor_lines[1], &len); | |
1669 | ||
1670 | if (strncmp (gfc_src_preprocessor_lines[1], "# 1 \"", 5) != 0) | |
1671 | return filename; | |
1672 | ||
1673 | dirname = unescape_filename (gfc_src_preprocessor_lines[1] + 5); | |
1674 | if (dirname == NULL) | |
1675 | return filename; | |
1676 | ||
1677 | len = strlen (dirname); | |
1678 | if (len < 3 || dirname[len - 1] != '/' || dirname[len - 2] != '/') | |
1679 | { | |
1680 | gfc_free (dirname); | |
1681 | return filename; | |
1682 | } | |
1683 | dirname[len - 2] = '\0'; | |
1684 | set_src_pwd (dirname); | |
1685 | ||
1686 | if (! IS_ABSOLUTE_PATH (filename)) | |
1687 | { | |
1688 | char *p = gfc_getmem (len + strlen (filename)); | |
1689 | ||
1690 | memcpy (p, dirname, len - 2); | |
1691 | p[len - 2] = '/'; | |
1692 | strcpy (p + len - 1, filename); | |
1693 | *canon_source_file = p; | |
1694 | } | |
1695 | ||
1696 | gfc_free (dirname); | |
1697 | return filename; | |
1698 | } |