]>
Commit | Line | Data |
---|---|---|
6de9cd9a | 1 | /* Character scanner. |
835aac92 | 2 | Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 |
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" |
9e8a6720 FXC |
48 | #include "debug.h" |
49 | #include "flags.h" | |
670637ee | 50 | #include "cpp.h" |
6de9cd9a DN |
51 | |
52 | /* Structure for holding module and include file search path. */ | |
53 | typedef struct gfc_directorylist | |
54 | { | |
55 | char *path; | |
31198773 | 56 | bool use_for_modules; |
6de9cd9a DN |
57 | struct gfc_directorylist *next; |
58 | } | |
59 | gfc_directorylist; | |
60 | ||
61 | /* List of include file search directories. */ | |
31198773 | 62 | static gfc_directorylist *include_dirs, *intrinsic_modules_dirs; |
6de9cd9a | 63 | |
d4fa05b9 | 64 | static gfc_file *file_head, *current_file; |
6de9cd9a | 65 | |
6c7a4dfd | 66 | static int continue_flag, end_flag, openmp_flag; |
5a06474c | 67 | static int continue_count, continue_line; |
6c7a4dfd | 68 | static locus openmp_locus; |
6de9cd9a | 69 | |
d4fa05b9 TS |
70 | gfc_source_form gfc_current_form; |
71 | static gfc_linebuf *line_head, *line_tail; | |
72 | ||
63645982 | 73 | locus gfc_current_locus; |
e0bcf78c | 74 | const char *gfc_source_file; |
2d7c7df6 | 75 | static FILE *gfc_src_file; |
8fc541d3 | 76 | static gfc_char_t *gfc_src_preprocessor_lines[2]; |
2d7c7df6 | 77 | |
5a06474c | 78 | extern int pedantic; |
6de9cd9a | 79 | |
1b271c9b JJ |
80 | static struct gfc_file_change |
81 | { | |
82 | const char *filename; | |
83 | gfc_linebuf *lb; | |
84 | int line; | |
85 | } *file_changes; | |
86 | size_t file_changes_cur, file_changes_count; | |
87 | size_t file_changes_allocated; | |
88 | ||
8fc541d3 FXC |
89 | |
90 | /* Functions dealing with our wide characters (gfc_char_t) and | |
91 | sequences of such characters. */ | |
92 | ||
93 | int | |
94 | gfc_wide_fits_in_byte (gfc_char_t c) | |
95 | { | |
96 | return (c <= UCHAR_MAX); | |
97 | } | |
98 | ||
99 | static inline int | |
100 | wide_is_ascii (gfc_char_t c) | |
101 | { | |
102 | return (gfc_wide_fits_in_byte (c) && ((unsigned char) c & ~0x7f) == 0); | |
103 | } | |
104 | ||
105 | int | |
106 | gfc_wide_is_printable (gfc_char_t c) | |
107 | { | |
108 | return (gfc_wide_fits_in_byte (c) && ISPRINT ((unsigned char) c)); | |
109 | } | |
110 | ||
111 | gfc_char_t | |
112 | gfc_wide_tolower (gfc_char_t c) | |
113 | { | |
114 | return (wide_is_ascii (c) ? (gfc_char_t) TOLOWER((unsigned char) c) : c); | |
115 | } | |
116 | ||
00660189 FXC |
117 | gfc_char_t |
118 | gfc_wide_toupper (gfc_char_t c) | |
119 | { | |
120 | return (wide_is_ascii (c) ? (gfc_char_t) TOUPPER((unsigned char) c) : c); | |
121 | } | |
122 | ||
8fc541d3 FXC |
123 | int |
124 | gfc_wide_is_digit (gfc_char_t c) | |
125 | { | |
126 | return (c >= '0' && c <= '9'); | |
127 | } | |
128 | ||
129 | static inline int | |
130 | wide_atoi (gfc_char_t *c) | |
131 | { | |
132 | #define MAX_DIGITS 20 | |
133 | char buf[MAX_DIGITS+1]; | |
134 | int i = 0; | |
135 | ||
136 | while (gfc_wide_is_digit(*c) && i < MAX_DIGITS) | |
137 | buf[i++] = *c++; | |
138 | buf[i] = '\0'; | |
139 | return atoi (buf); | |
140 | } | |
141 | ||
142 | size_t | |
143 | gfc_wide_strlen (const gfc_char_t *str) | |
144 | { | |
145 | size_t i; | |
146 | ||
147 | for (i = 0; str[i]; i++) | |
148 | ; | |
149 | ||
150 | return i; | |
151 | } | |
152 | ||
00660189 FXC |
153 | gfc_char_t * |
154 | gfc_wide_memset (gfc_char_t *b, gfc_char_t c, size_t len) | |
155 | { | |
156 | size_t i; | |
157 | ||
158 | for (i = 0; i < len; i++) | |
159 | b[i] = c; | |
160 | ||
161 | return b; | |
162 | } | |
163 | ||
8fc541d3 FXC |
164 | static gfc_char_t * |
165 | wide_strcpy (gfc_char_t *dest, const gfc_char_t *src) | |
166 | { | |
167 | gfc_char_t *d; | |
168 | ||
169 | for (d = dest; (*d = *src) != '\0'; ++src, ++d) | |
170 | ; | |
171 | ||
172 | return dest; | |
173 | } | |
174 | ||
175 | static gfc_char_t * | |
00660189 | 176 | wide_strchr (const gfc_char_t *s, gfc_char_t c) |
8fc541d3 FXC |
177 | { |
178 | do { | |
179 | if (*s == c) | |
180 | { | |
00660189 | 181 | return CONST_CAST(gfc_char_t *, s); |
8fc541d3 FXC |
182 | } |
183 | } while (*s++); | |
184 | return 0; | |
185 | } | |
186 | ||
00660189 FXC |
187 | char * |
188 | gfc_widechar_to_char (const gfc_char_t *s, int length) | |
189 | { | |
190 | size_t len, i; | |
191 | char *res; | |
192 | ||
193 | if (s == NULL) | |
194 | return NULL; | |
195 | ||
196 | /* Passing a negative length is used to indicate that length should be | |
197 | calculated using gfc_wide_strlen(). */ | |
198 | len = (length >= 0 ? (size_t) length : gfc_wide_strlen (s)); | |
199 | res = gfc_getmem (len + 1); | |
200 | ||
201 | for (i = 0; i < len; i++) | |
202 | { | |
203 | gcc_assert (gfc_wide_fits_in_byte (s[i])); | |
204 | res[i] = (unsigned char) s[i]; | |
205 | } | |
206 | ||
207 | res[len] = '\0'; | |
208 | return res; | |
209 | } | |
210 | ||
211 | gfc_char_t * | |
212 | gfc_char_to_widechar (const char *s) | |
8fc541d3 | 213 | { |
00660189 FXC |
214 | size_t len, i; |
215 | gfc_char_t *res; | |
216 | ||
217 | if (s == NULL) | |
218 | return NULL; | |
219 | ||
220 | len = strlen (s); | |
221 | res = gfc_get_wide_string (len + 1); | |
8fc541d3 FXC |
222 | |
223 | for (i = 0; i < len; i++) | |
00660189 | 224 | res[i] = (unsigned char) s[i]; |
8fc541d3 FXC |
225 | |
226 | res[len] = '\0'; | |
227 | return res; | |
228 | } | |
229 | ||
230 | static int | |
231 | wide_strncmp (const gfc_char_t *s1, const char *s2, size_t n) | |
232 | { | |
233 | gfc_char_t c1, c2; | |
234 | ||
235 | while (n-- > 0) | |
236 | { | |
237 | c1 = *s1++; | |
238 | c2 = *s2++; | |
239 | if (c1 != c2) | |
240 | return (c1 > c2 ? 1 : -1); | |
241 | if (c1 == '\0') | |
242 | return 0; | |
243 | } | |
244 | return 0; | |
245 | } | |
246 | ||
00660189 FXC |
247 | int |
248 | gfc_wide_strncasecmp (const gfc_char_t *s1, const char *s2, size_t n) | |
8fc541d3 FXC |
249 | { |
250 | gfc_char_t c1, c2; | |
251 | ||
252 | while (n-- > 0) | |
253 | { | |
254 | c1 = gfc_wide_tolower (*s1++); | |
255 | c2 = TOLOWER (*s2++); | |
256 | if (c1 != c2) | |
257 | return (c1 > c2 ? 1 : -1); | |
258 | if (c1 == '\0') | |
259 | return 0; | |
260 | } | |
261 | return 0; | |
262 | } | |
263 | ||
264 | ||
6de9cd9a DN |
265 | /* Main scanner initialization. */ |
266 | ||
267 | void | |
268 | gfc_scanner_init_1 (void) | |
269 | { | |
d4fa05b9 TS |
270 | file_head = NULL; |
271 | line_head = NULL; | |
272 | line_tail = NULL; | |
6de9cd9a | 273 | |
5a06474c JD |
274 | continue_count = 0; |
275 | continue_line = 0; | |
276 | ||
6de9cd9a DN |
277 | end_flag = 0; |
278 | } | |
279 | ||
280 | ||
281 | /* Main scanner destructor. */ | |
282 | ||
283 | void | |
284 | gfc_scanner_done_1 (void) | |
285 | { | |
d4fa05b9 TS |
286 | gfc_linebuf *lb; |
287 | gfc_file *f; | |
6de9cd9a | 288 | |
d4fa05b9 | 289 | while(line_head != NULL) |
6de9cd9a | 290 | { |
d4fa05b9 TS |
291 | lb = line_head->next; |
292 | gfc_free(line_head); | |
293 | line_head = lb; | |
6de9cd9a | 294 | } |
d4fa05b9 TS |
295 | |
296 | while(file_head != NULL) | |
6de9cd9a | 297 | { |
d4fa05b9 TS |
298 | f = file_head->next; |
299 | gfc_free(file_head->filename); | |
300 | gfc_free(file_head); | |
301 | file_head = f; | |
6de9cd9a DN |
302 | } |
303 | } | |
304 | ||
305 | ||
306 | /* Adds path to the list pointed to by list. */ | |
307 | ||
31198773 FXC |
308 | static void |
309 | add_path_to_list (gfc_directorylist **list, const char *path, | |
310 | bool use_for_modules) | |
6de9cd9a DN |
311 | { |
312 | gfc_directorylist *dir; | |
313 | const char *p; | |
314 | ||
315 | p = path; | |
31198773 | 316 | while (*p == ' ' || *p == '\t') /* someone might do "-I include" */ |
6de9cd9a DN |
317 | if (*p++ == '\0') |
318 | return; | |
319 | ||
31198773 | 320 | dir = *list; |
6de9cd9a | 321 | if (!dir) |
31198773 | 322 | dir = *list = gfc_getmem (sizeof (gfc_directorylist)); |
6de9cd9a DN |
323 | else |
324 | { | |
325 | while (dir->next) | |
326 | dir = dir->next; | |
327 | ||
328 | dir->next = gfc_getmem (sizeof (gfc_directorylist)); | |
329 | dir = dir->next; | |
330 | } | |
331 | ||
332 | dir->next = NULL; | |
31198773 | 333 | dir->use_for_modules = use_for_modules; |
6de9cd9a DN |
334 | dir->path = gfc_getmem (strlen (p) + 2); |
335 | strcpy (dir->path, p); | |
336 | strcat (dir->path, "/"); /* make '/' last character */ | |
337 | } | |
338 | ||
339 | ||
31198773 FXC |
340 | void |
341 | gfc_add_include_path (const char *path, bool use_for_modules) | |
342 | { | |
343 | add_path_to_list (&include_dirs, path, use_for_modules); | |
670637ee | 344 | gfc_cpp_add_include_path (xstrdup(path), true); |
31198773 FXC |
345 | } |
346 | ||
347 | ||
348 | void | |
349 | gfc_add_intrinsic_modules_path (const char *path) | |
350 | { | |
351 | add_path_to_list (&intrinsic_modules_dirs, path, true); | |
352 | } | |
353 | ||
354 | ||
6de9cd9a DN |
355 | /* Release resources allocated for options. */ |
356 | ||
357 | void | |
358 | gfc_release_include_path (void) | |
359 | { | |
360 | gfc_directorylist *p; | |
361 | ||
6de9cd9a DN |
362 | while (include_dirs != NULL) |
363 | { | |
364 | p = include_dirs; | |
365 | include_dirs = include_dirs->next; | |
366 | gfc_free (p->path); | |
367 | gfc_free (p); | |
368 | } | |
31198773 | 369 | |
31198773 FXC |
370 | while (intrinsic_modules_dirs != NULL) |
371 | { | |
372 | p = intrinsic_modules_dirs; | |
373 | intrinsic_modules_dirs = intrinsic_modules_dirs->next; | |
374 | gfc_free (p->path); | |
375 | gfc_free (p); | |
376 | } | |
1bc23383 FXC |
377 | |
378 | gfc_free (gfc_option.module_dir); | |
6de9cd9a DN |
379 | } |
380 | ||
6de9cd9a | 381 | |
31198773 FXC |
382 | static FILE * |
383 | open_included_file (const char *name, gfc_directorylist *list, bool module) | |
6de9cd9a | 384 | { |
200cfbe7 | 385 | char *fullname; |
6de9cd9a DN |
386 | gfc_directorylist *p; |
387 | FILE *f; | |
388 | ||
31198773 | 389 | for (p = list; p; p = p->next) |
b424a572 | 390 | { |
31198773 FXC |
391 | if (module && !p->use_for_modules) |
392 | continue; | |
6de9cd9a | 393 | |
200cfbe7 | 394 | fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1); |
6de9cd9a DN |
395 | strcpy (fullname, p->path); |
396 | strcat (fullname, name); | |
397 | ||
398 | f = gfc_open_file (fullname); | |
399 | if (f != NULL) | |
400 | return f; | |
401 | } | |
402 | ||
403 | return NULL; | |
404 | } | |
405 | ||
31198773 FXC |
406 | |
407 | /* Opens file for reading, searching through the include directories | |
408 | given if necessary. If the include_cwd argument is true, we try | |
409 | to open the file in the current directory first. */ | |
410 | ||
411 | FILE * | |
412 | gfc_open_included_file (const char *name, bool include_cwd, bool module) | |
413 | { | |
414 | FILE *f; | |
415 | ||
e01f74e0 TB |
416 | if (IS_ABSOLUTE_PATH (name)) |
417 | return gfc_open_file (name); | |
418 | ||
31198773 FXC |
419 | if (include_cwd) |
420 | { | |
421 | f = gfc_open_file (name); | |
422 | if (f != NULL) | |
423 | return f; | |
424 | } | |
425 | ||
426 | return open_included_file (name, include_dirs, module); | |
427 | } | |
428 | ||
429 | FILE * | |
430 | gfc_open_intrinsic_module (const char *name) | |
431 | { | |
e01f74e0 TB |
432 | if (IS_ABSOLUTE_PATH (name)) |
433 | return gfc_open_file (name); | |
434 | ||
31198773 FXC |
435 | return open_included_file (name, intrinsic_modules_dirs, true); |
436 | } | |
437 | ||
edf1eac2 | 438 | |
6de9cd9a DN |
439 | /* Test to see if we're at the end of the main source file. */ |
440 | ||
441 | int | |
442 | gfc_at_end (void) | |
443 | { | |
6de9cd9a DN |
444 | return end_flag; |
445 | } | |
446 | ||
447 | ||
448 | /* Test to see if we're at the end of the current file. */ | |
449 | ||
450 | int | |
451 | gfc_at_eof (void) | |
452 | { | |
6de9cd9a DN |
453 | if (gfc_at_end ()) |
454 | return 1; | |
455 | ||
d4fa05b9 | 456 | if (line_head == NULL) |
6de9cd9a DN |
457 | return 1; /* Null file */ |
458 | ||
63645982 | 459 | if (gfc_current_locus.lb == NULL) |
6de9cd9a DN |
460 | return 1; |
461 | ||
462 | return 0; | |
463 | } | |
464 | ||
465 | ||
466 | /* Test to see if we're at the beginning of a new line. */ | |
467 | ||
468 | int | |
469 | gfc_at_bol (void) | |
470 | { | |
6de9cd9a DN |
471 | if (gfc_at_eof ()) |
472 | return 1; | |
473 | ||
63645982 | 474 | return (gfc_current_locus.nextc == gfc_current_locus.lb->line); |
6de9cd9a DN |
475 | } |
476 | ||
477 | ||
478 | /* Test to see if we're at the end of a line. */ | |
479 | ||
480 | int | |
481 | gfc_at_eol (void) | |
482 | { | |
6de9cd9a DN |
483 | if (gfc_at_eof ()) |
484 | return 1; | |
485 | ||
63645982 | 486 | return (*gfc_current_locus.nextc == '\0'); |
6de9cd9a DN |
487 | } |
488 | ||
60332588 | 489 | static void |
1b271c9b | 490 | add_file_change (const char *filename, int line) |
60332588 | 491 | { |
1b271c9b JJ |
492 | if (file_changes_count == file_changes_allocated) |
493 | { | |
494 | if (file_changes_allocated) | |
495 | file_changes_allocated *= 2; | |
496 | else | |
497 | file_changes_allocated = 16; | |
498 | file_changes | |
499 | = xrealloc (file_changes, | |
500 | file_changes_allocated * sizeof (*file_changes)); | |
501 | } | |
502 | file_changes[file_changes_count].filename = filename; | |
503 | file_changes[file_changes_count].lb = NULL; | |
504 | file_changes[file_changes_count++].line = line; | |
505 | } | |
60332588 | 506 | |
1b271c9b JJ |
507 | static void |
508 | report_file_change (gfc_linebuf *lb) | |
509 | { | |
510 | size_t c = file_changes_cur; | |
511 | while (c < file_changes_count | |
512 | && file_changes[c].lb == lb) | |
513 | { | |
514 | if (file_changes[c].filename) | |
515 | (*debug_hooks->start_source_file) (file_changes[c].line, | |
516 | file_changes[c].filename); | |
517 | else | |
518 | (*debug_hooks->end_source_file) (file_changes[c].line); | |
519 | ++c; | |
520 | } | |
521 | file_changes_cur = c; | |
60332588 JJ |
522 | } |
523 | ||
524 | void | |
525 | gfc_start_source_files (void) | |
526 | { | |
527 | /* If the debugger wants the name of the main source file, | |
528 | we give it. */ | |
529 | if (debug_hooks->start_end_main_source_file) | |
530 | (*debug_hooks->start_source_file) (0, gfc_source_file); | |
531 | ||
1b271c9b JJ |
532 | file_changes_cur = 0; |
533 | report_file_change (gfc_current_locus.lb); | |
60332588 JJ |
534 | } |
535 | ||
536 | void | |
537 | gfc_end_source_files (void) | |
538 | { | |
1b271c9b | 539 | report_file_change (NULL); |
60332588 JJ |
540 | |
541 | if (debug_hooks->start_end_main_source_file) | |
542 | (*debug_hooks->end_source_file) (0); | |
543 | } | |
6de9cd9a DN |
544 | |
545 | /* Advance the current line pointer to the next line. */ | |
546 | ||
547 | void | |
548 | gfc_advance_line (void) | |
549 | { | |
6de9cd9a | 550 | if (gfc_at_end ()) |
4a58b9ad | 551 | return; |
6de9cd9a | 552 | |
63645982 | 553 | if (gfc_current_locus.lb == NULL) |
6de9cd9a | 554 | { |
d4fa05b9 TS |
555 | end_flag = 1; |
556 | return; | |
557 | } | |
6de9cd9a | 558 | |
9e8a6720 | 559 | if (gfc_current_locus.lb->next |
60332588 | 560 | && !gfc_current_locus.lb->next->dbg_emitted) |
9e8a6720 | 561 | { |
1b271c9b | 562 | report_file_change (gfc_current_locus.lb->next); |
60332588 | 563 | gfc_current_locus.lb->next->dbg_emitted = true; |
9e8a6720 FXC |
564 | } |
565 | ||
63645982 | 566 | gfc_current_locus.lb = gfc_current_locus.lb->next; |
6de9cd9a | 567 | |
edf1eac2 | 568 | if (gfc_current_locus.lb != NULL) |
63645982 | 569 | gfc_current_locus.nextc = gfc_current_locus.lb->line; |
d4fa05b9 TS |
570 | else |
571 | { | |
63645982 | 572 | gfc_current_locus.nextc = NULL; |
d4fa05b9 TS |
573 | end_flag = 1; |
574 | } | |
6de9cd9a DN |
575 | } |
576 | ||
577 | ||
578 | /* Get the next character from the input, advancing gfc_current_file's | |
579 | locus. When we hit the end of the line or the end of the file, we | |
580 | start returning a '\n' in order to complete the current statement. | |
581 | No Fortran line conventions are implemented here. | |
582 | ||
583 | Requiring explicit advances to the next line prevents the parse | |
584 | pointer from being on the wrong line if the current statement ends | |
585 | prematurely. */ | |
586 | ||
8fc541d3 | 587 | static gfc_char_t |
6de9cd9a DN |
588 | next_char (void) |
589 | { | |
8fc541d3 | 590 | gfc_char_t c; |
d4fa05b9 | 591 | |
63645982 | 592 | if (gfc_current_locus.nextc == NULL) |
6de9cd9a DN |
593 | return '\n'; |
594 | ||
8fc541d3 | 595 | c = *gfc_current_locus.nextc++; |
6de9cd9a DN |
596 | if (c == '\0') |
597 | { | |
63645982 | 598 | gfc_current_locus.nextc--; /* Remain on this line. */ |
6de9cd9a DN |
599 | c = '\n'; |
600 | } | |
601 | ||
602 | return c; | |
603 | } | |
604 | ||
edf1eac2 | 605 | |
6de9cd9a DN |
606 | /* Skip a comment. When we come here the parse pointer is positioned |
607 | immediately after the comment character. If we ever implement | |
608 | compiler directives withing comments, here is where we parse the | |
609 | directive. */ | |
610 | ||
611 | static void | |
612 | skip_comment_line (void) | |
613 | { | |
8fc541d3 | 614 | gfc_char_t c; |
6de9cd9a DN |
615 | |
616 | do | |
617 | { | |
618 | c = next_char (); | |
619 | } | |
620 | while (c != '\n'); | |
621 | ||
622 | gfc_advance_line (); | |
623 | } | |
624 | ||
625 | ||
9e8a6720 FXC |
626 | int |
627 | gfc_define_undef_line (void) | |
628 | { | |
8fc541d3 FXC |
629 | char *tmp; |
630 | ||
9e8a6720 | 631 | /* All lines beginning with '#' are either #define or #undef. */ |
8fc541d3 | 632 | if (debug_info_level != DINFO_LEVEL_VERBOSE || gfc_peek_ascii_char () != '#') |
9e8a6720 FXC |
633 | return 0; |
634 | ||
8fc541d3 FXC |
635 | if (wide_strncmp (gfc_current_locus.nextc, "#define ", 8) == 0) |
636 | { | |
00660189 | 637 | tmp = gfc_widechar_to_char (&gfc_current_locus.nextc[8], -1); |
8fc541d3 FXC |
638 | (*debug_hooks->define) (gfc_linebuf_linenum (gfc_current_locus.lb), |
639 | tmp); | |
640 | gfc_free (tmp); | |
641 | } | |
9e8a6720 | 642 | |
8fc541d3 FXC |
643 | if (wide_strncmp (gfc_current_locus.nextc, "#undef ", 7) == 0) |
644 | { | |
00660189 | 645 | tmp = gfc_widechar_to_char (&gfc_current_locus.nextc[7], -1); |
8fc541d3 FXC |
646 | (*debug_hooks->undef) (gfc_linebuf_linenum (gfc_current_locus.lb), |
647 | tmp); | |
648 | gfc_free (tmp); | |
649 | } | |
9e8a6720 FXC |
650 | |
651 | /* Skip the rest of the line. */ | |
652 | skip_comment_line (); | |
653 | ||
654 | return 1; | |
655 | } | |
656 | ||
657 | ||
6de9cd9a | 658 | /* Comment lines are null lines, lines containing only blanks or lines |
0d3abf6f JJ |
659 | on which the first nonblank line is a '!'. |
660 | Return true if !$ openmp conditional compilation sentinel was | |
661 | seen. */ | |
6de9cd9a | 662 | |
0d3abf6f | 663 | static bool |
6de9cd9a DN |
664 | skip_free_comments (void) |
665 | { | |
666 | locus start; | |
8fc541d3 | 667 | gfc_char_t c; |
6c7a4dfd | 668 | int at_bol; |
6de9cd9a DN |
669 | |
670 | for (;;) | |
671 | { | |
6c7a4dfd | 672 | at_bol = gfc_at_bol (); |
63645982 | 673 | start = gfc_current_locus; |
6de9cd9a DN |
674 | if (gfc_at_eof ()) |
675 | break; | |
676 | ||
677 | do | |
6c7a4dfd | 678 | c = next_char (); |
6de9cd9a DN |
679 | while (gfc_is_whitespace (c)); |
680 | ||
681 | if (c == '\n') | |
682 | { | |
683 | gfc_advance_line (); | |
684 | continue; | |
685 | } | |
686 | ||
687 | if (c == '!') | |
688 | { | |
6c7a4dfd JJ |
689 | /* If -fopenmp, we need to handle here 2 things: |
690 | 1) don't treat !$omp as comments, but directives | |
691 | 2) handle OpenMP conditional compilation, where | |
692 | !$ should be treated as 2 spaces (for initial lines | |
693 | only if followed by space). */ | |
694 | if (gfc_option.flag_openmp && at_bol) | |
695 | { | |
696 | locus old_loc = gfc_current_locus; | |
697 | if (next_char () == '$') | |
698 | { | |
699 | c = next_char (); | |
700 | if (c == 'o' || c == 'O') | |
701 | { | |
702 | if (((c = next_char ()) == 'm' || c == 'M') | |
9fa6cfec | 703 | && ((c = next_char ()) == 'p' || c == 'P')) |
6c7a4dfd | 704 | { |
9fa6cfec | 705 | if ((c = next_char ()) == ' ' || continue_flag) |
6c7a4dfd | 706 | { |
9fa6cfec TB |
707 | while (gfc_is_whitespace (c)) |
708 | c = next_char (); | |
709 | if (c != '\n' && c != '!') | |
710 | { | |
711 | openmp_flag = 1; | |
712 | openmp_locus = old_loc; | |
713 | gfc_current_locus = start; | |
714 | return false; | |
715 | } | |
6c7a4dfd | 716 | } |
9fa6cfec TB |
717 | else |
718 | gfc_warning_now ("!$OMP at %C starts a commented " | |
719 | "line as it neither is followed " | |
720 | "by a space nor is a " | |
721 | "continuation line"); | |
6c7a4dfd JJ |
722 | } |
723 | gfc_current_locus = old_loc; | |
724 | next_char (); | |
725 | c = next_char (); | |
726 | } | |
727 | if (continue_flag || c == ' ') | |
728 | { | |
729 | gfc_current_locus = old_loc; | |
730 | next_char (); | |
b30c6a0d | 731 | openmp_flag = 0; |
0d3abf6f | 732 | return true; |
6c7a4dfd JJ |
733 | } |
734 | } | |
735 | gfc_current_locus = old_loc; | |
736 | } | |
6de9cd9a DN |
737 | skip_comment_line (); |
738 | continue; | |
739 | } | |
740 | ||
741 | break; | |
742 | } | |
743 | ||
6c7a4dfd JJ |
744 | if (openmp_flag && at_bol) |
745 | openmp_flag = 0; | |
63645982 | 746 | gfc_current_locus = start; |
0d3abf6f | 747 | return false; |
6de9cd9a DN |
748 | } |
749 | ||
750 | ||
751 | /* Skip comment lines in fixed source mode. We have the same rules as | |
752 | in skip_free_comment(), except that we can have a 'c', 'C' or '*' | |
e0bcf78c TS |
753 | in column 1, and a '!' cannot be in column 6. Also, we deal with |
754 | lines with 'd' or 'D' in column 1, if the user requested this. */ | |
6de9cd9a DN |
755 | |
756 | static void | |
757 | skip_fixed_comments (void) | |
758 | { | |
759 | locus start; | |
760 | int col; | |
8fc541d3 | 761 | gfc_char_t c; |
6de9cd9a | 762 | |
6c7a4dfd JJ |
763 | if (! gfc_at_bol ()) |
764 | { | |
765 | start = gfc_current_locus; | |
766 | if (! gfc_at_eof ()) | |
767 | { | |
768 | do | |
769 | c = next_char (); | |
770 | while (gfc_is_whitespace (c)); | |
771 | ||
772 | if (c == '\n') | |
773 | gfc_advance_line (); | |
774 | else if (c == '!') | |
775 | skip_comment_line (); | |
776 | } | |
777 | ||
778 | if (! gfc_at_bol ()) | |
779 | { | |
780 | gfc_current_locus = start; | |
781 | return; | |
782 | } | |
783 | } | |
784 | ||
6de9cd9a DN |
785 | for (;;) |
786 | { | |
63645982 | 787 | start = gfc_current_locus; |
6de9cd9a DN |
788 | if (gfc_at_eof ()) |
789 | break; | |
790 | ||
791 | c = next_char (); | |
792 | if (c == '\n') | |
793 | { | |
794 | gfc_advance_line (); | |
795 | continue; | |
796 | } | |
797 | ||
798 | if (c == '!' || c == 'c' || c == 'C' || c == '*') | |
799 | { | |
6c7a4dfd JJ |
800 | /* If -fopenmp, we need to handle here 2 things: |
801 | 1) don't treat !$omp|c$omp|*$omp as comments, but directives | |
802 | 2) handle OpenMP conditional compilation, where | |
803 | !$|c$|*$ should be treated as 2 spaces if the characters | |
804 | in columns 3 to 6 are valid fixed form label columns | |
805 | characters. */ | |
f449022d JD |
806 | if (gfc_current_locus.lb != NULL |
807 | && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb)) | |
808 | continue_line = gfc_linebuf_linenum (gfc_current_locus.lb); | |
809 | ||
6c7a4dfd JJ |
810 | if (gfc_option.flag_openmp) |
811 | { | |
812 | if (next_char () == '$') | |
813 | { | |
814 | c = next_char (); | |
815 | if (c == 'o' || c == 'O') | |
816 | { | |
817 | if (((c = next_char ()) == 'm' || c == 'M') | |
818 | && ((c = next_char ()) == 'p' || c == 'P')) | |
819 | { | |
820 | c = next_char (); | |
821 | if (c != '\n' | |
822 | && ((openmp_flag && continue_flag) | |
823 | || c == ' ' || c == '0')) | |
824 | { | |
825 | c = next_char (); | |
826 | while (gfc_is_whitespace (c)) | |
827 | c = next_char (); | |
828 | if (c != '\n' && c != '!') | |
829 | { | |
830 | /* Canonicalize to *$omp. */ | |
831 | *start.nextc = '*'; | |
832 | openmp_flag = 1; | |
833 | gfc_current_locus = start; | |
834 | return; | |
835 | } | |
836 | } | |
837 | } | |
838 | } | |
839 | else | |
840 | { | |
841 | int digit_seen = 0; | |
842 | ||
843 | for (col = 3; col < 6; col++, c = next_char ()) | |
844 | if (c == ' ') | |
845 | continue; | |
846 | else if (c < '0' || c > '9') | |
847 | break; | |
848 | else | |
849 | digit_seen = 1; | |
850 | ||
851 | if (col == 6 && c != '\n' | |
852 | && ((continue_flag && !digit_seen) | |
853 | || c == ' ' || c == '0')) | |
854 | { | |
855 | gfc_current_locus = start; | |
856 | start.nextc[0] = ' '; | |
857 | start.nextc[1] = ' '; | |
858 | continue; | |
859 | } | |
860 | } | |
861 | } | |
862 | gfc_current_locus = start; | |
863 | } | |
6de9cd9a DN |
864 | skip_comment_line (); |
865 | continue; | |
866 | } | |
867 | ||
e0bcf78c TS |
868 | if (gfc_option.flag_d_lines != -1 && (c == 'd' || c == 'D')) |
869 | { | |
870 | if (gfc_option.flag_d_lines == 0) | |
871 | { | |
872 | skip_comment_line (); | |
873 | continue; | |
874 | } | |
875 | else | |
876 | *start.nextc = c = ' '; | |
877 | } | |
878 | ||
6de9cd9a | 879 | col = 1; |
e0bcf78c TS |
880 | |
881 | while (gfc_is_whitespace (c)) | |
6de9cd9a DN |
882 | { |
883 | c = next_char (); | |
884 | col++; | |
885 | } | |
6de9cd9a DN |
886 | |
887 | if (c == '\n') | |
888 | { | |
889 | gfc_advance_line (); | |
890 | continue; | |
891 | } | |
892 | ||
893 | if (col != 6 && c == '!') | |
894 | { | |
f449022d JD |
895 | if (gfc_current_locus.lb != NULL |
896 | && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb)) | |
897 | continue_line = gfc_linebuf_linenum (gfc_current_locus.lb); | |
6de9cd9a DN |
898 | skip_comment_line (); |
899 | continue; | |
900 | } | |
901 | ||
902 | break; | |
903 | } | |
904 | ||
6c7a4dfd | 905 | openmp_flag = 0; |
63645982 | 906 | gfc_current_locus = start; |
6de9cd9a DN |
907 | } |
908 | ||
909 | ||
6c7a4dfd | 910 | /* Skips the current line if it is a comment. */ |
6de9cd9a DN |
911 | |
912 | void | |
913 | gfc_skip_comments (void) | |
914 | { | |
6c7a4dfd | 915 | if (gfc_current_form == FORM_FREE) |
6de9cd9a DN |
916 | skip_free_comments (); |
917 | else | |
918 | skip_fixed_comments (); | |
919 | } | |
920 | ||
921 | ||
922 | /* Get the next character from the input, taking continuation lines | |
923 | and end-of-line comments into account. This implies that comment | |
924 | lines between continued lines must be eaten here. For higher-level | |
925 | subroutines, this flattens continued lines into a single logical | |
926 | line. The in_string flag denotes whether we're inside a character | |
927 | context or not. */ | |
928 | ||
8fc541d3 | 929 | gfc_char_t |
6de9cd9a DN |
930 | gfc_next_char_literal (int in_string) |
931 | { | |
932 | locus old_loc; | |
8fc541d3 FXC |
933 | int i, prev_openmp_flag; |
934 | gfc_char_t c; | |
6de9cd9a DN |
935 | |
936 | continue_flag = 0; | |
937 | ||
938 | restart: | |
939 | c = next_char (); | |
940 | if (gfc_at_end ()) | |
5a06474c JD |
941 | { |
942 | continue_count = 0; | |
943 | return c; | |
944 | } | |
6de9cd9a | 945 | |
d4fa05b9 | 946 | if (gfc_current_form == FORM_FREE) |
6de9cd9a | 947 | { |
0d3abf6f JJ |
948 | bool openmp_cond_flag; |
949 | ||
6de9cd9a DN |
950 | if (!in_string && c == '!') |
951 | { | |
6c7a4dfd JJ |
952 | if (openmp_flag |
953 | && memcmp (&gfc_current_locus, &openmp_locus, | |
954 | sizeof (gfc_current_locus)) == 0) | |
955 | goto done; | |
956 | ||
6de9cd9a DN |
957 | /* This line can't be continued */ |
958 | do | |
959 | { | |
960 | c = next_char (); | |
961 | } | |
962 | while (c != '\n'); | |
963 | ||
a34938be RG |
964 | /* Avoid truncation warnings for comment ending lines. */ |
965 | gfc_current_locus.lb->truncated = 0; | |
966 | ||
6de9cd9a DN |
967 | goto done; |
968 | } | |
969 | ||
970 | if (c != '&') | |
971 | goto done; | |
972 | ||
973 | /* If the next nonblank character is a ! or \n, we've got a | |
6c7a4dfd | 974 | continuation line. */ |
63645982 | 975 | old_loc = gfc_current_locus; |
6de9cd9a DN |
976 | |
977 | c = next_char (); | |
978 | while (gfc_is_whitespace (c)) | |
979 | c = next_char (); | |
980 | ||
981 | /* Character constants to be continued cannot have commentary | |
6c7a4dfd | 982 | after the '&'. */ |
6de9cd9a DN |
983 | |
984 | if (in_string && c != '\n') | |
985 | { | |
63645982 | 986 | gfc_current_locus = old_loc; |
6de9cd9a DN |
987 | c = '&'; |
988 | goto done; | |
989 | } | |
990 | ||
991 | if (c != '!' && c != '\n') | |
992 | { | |
63645982 | 993 | gfc_current_locus = old_loc; |
6de9cd9a DN |
994 | c = '&'; |
995 | goto done; | |
996 | } | |
997 | ||
6c7a4dfd | 998 | prev_openmp_flag = openmp_flag; |
6de9cd9a DN |
999 | continue_flag = 1; |
1000 | if (c == '!') | |
1001 | skip_comment_line (); | |
1002 | else | |
1003 | gfc_advance_line (); | |
0267ffdc JD |
1004 | |
1005 | if (gfc_at_eof()) | |
1006 | goto not_continuation; | |
6de9cd9a | 1007 | |
5a06474c JD |
1008 | /* We've got a continuation line. If we are on the very next line after |
1009 | the last continuation, increment the continuation line count and | |
1010 | check whether the limit has been exceeded. */ | |
5ffeb913 | 1011 | if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1) |
5a06474c JD |
1012 | { |
1013 | if (++continue_count == gfc_option.max_continue_free) | |
1014 | { | |
edf1eac2 SK |
1015 | if (gfc_notification_std (GFC_STD_GNU) || pedantic) |
1016 | gfc_warning ("Limit of %d continuations exceeded in " | |
1017 | "statement at %C", gfc_option.max_continue_free); | |
5a06474c JD |
1018 | } |
1019 | } | |
5a06474c JD |
1020 | |
1021 | /* Now find where it continues. First eat any comment lines. */ | |
0d3abf6f | 1022 | openmp_cond_flag = skip_free_comments (); |
6de9cd9a | 1023 | |
f449022d JD |
1024 | if (gfc_current_locus.lb != NULL |
1025 | && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb)) | |
1026 | continue_line = gfc_linebuf_linenum (gfc_current_locus.lb); | |
1027 | ||
6c7a4dfd JJ |
1028 | if (prev_openmp_flag != openmp_flag) |
1029 | { | |
1030 | gfc_current_locus = old_loc; | |
1031 | openmp_flag = prev_openmp_flag; | |
1032 | c = '&'; | |
1033 | goto done; | |
1034 | } | |
1035 | ||
6de9cd9a | 1036 | /* Now that we have a non-comment line, probe ahead for the |
6c7a4dfd JJ |
1037 | first non-whitespace character. If it is another '&', then |
1038 | reading starts at the next character, otherwise we must back | |
1039 | up to where the whitespace started and resume from there. */ | |
6de9cd9a | 1040 | |
63645982 | 1041 | old_loc = gfc_current_locus; |
6de9cd9a DN |
1042 | |
1043 | c = next_char (); | |
1044 | while (gfc_is_whitespace (c)) | |
1045 | c = next_char (); | |
1046 | ||
6c7a4dfd JJ |
1047 | if (openmp_flag) |
1048 | { | |
1049 | for (i = 0; i < 5; i++, c = next_char ()) | |
1050 | { | |
8fc541d3 | 1051 | gcc_assert (gfc_wide_tolower (c) == (unsigned char) "!$omp"[i]); |
6c7a4dfd JJ |
1052 | if (i == 4) |
1053 | old_loc = gfc_current_locus; | |
1054 | } | |
1055 | while (gfc_is_whitespace (c)) | |
1056 | c = next_char (); | |
1057 | } | |
1058 | ||
6de9cd9a | 1059 | if (c != '&') |
3fbab549 | 1060 | { |
5a06474c JD |
1061 | if (in_string) |
1062 | { | |
1063 | if (gfc_option.warn_ampersand) | |
edf1eac2 SK |
1064 | gfc_warning_now ("Missing '&' in continued character " |
1065 | "constant at %C"); | |
5a06474c JD |
1066 | gfc_current_locus.nextc--; |
1067 | } | |
0d3abf6f JJ |
1068 | /* Both !$omp and !$ -fopenmp continuation lines have & on the |
1069 | continuation line only optionally. */ | |
1070 | else if (openmp_flag || openmp_cond_flag) | |
1071 | gfc_current_locus.nextc--; | |
5a06474c JD |
1072 | else |
1073 | { | |
1074 | c = ' '; | |
1075 | gfc_current_locus = old_loc; | |
1076 | goto done; | |
1077 | } | |
3fbab549 | 1078 | } |
6de9cd9a DN |
1079 | } |
1080 | else | |
1081 | { | |
1082 | /* Fixed form continuation. */ | |
1083 | if (!in_string && c == '!') | |
1084 | { | |
1085 | /* Skip comment at end of line. */ | |
1086 | do | |
1087 | { | |
1088 | c = next_char (); | |
1089 | } | |
1090 | while (c != '\n'); | |
a34938be RG |
1091 | |
1092 | /* Avoid truncation warnings for comment ending lines. */ | |
1093 | gfc_current_locus.lb->truncated = 0; | |
6de9cd9a DN |
1094 | } |
1095 | ||
1096 | if (c != '\n') | |
1097 | goto done; | |
1098 | ||
6c7a4dfd | 1099 | prev_openmp_flag = openmp_flag; |
6de9cd9a | 1100 | continue_flag = 1; |
63645982 | 1101 | old_loc = gfc_current_locus; |
6de9cd9a DN |
1102 | |
1103 | gfc_advance_line (); | |
0d3abf6f | 1104 | skip_fixed_comments (); |
6de9cd9a DN |
1105 | |
1106 | /* See if this line is a continuation line. */ | |
6c7a4dfd | 1107 | if (openmp_flag != prev_openmp_flag) |
6de9cd9a | 1108 | { |
6c7a4dfd JJ |
1109 | openmp_flag = prev_openmp_flag; |
1110 | goto not_continuation; | |
6de9cd9a DN |
1111 | } |
1112 | ||
6c7a4dfd JJ |
1113 | if (!openmp_flag) |
1114 | for (i = 0; i < 5; i++) | |
1115 | { | |
1116 | c = next_char (); | |
1117 | if (c != ' ') | |
1118 | goto not_continuation; | |
1119 | } | |
1120 | else | |
1121 | for (i = 0; i < 5; i++) | |
1122 | { | |
1123 | c = next_char (); | |
8fc541d3 | 1124 | if (gfc_wide_tolower (c) != (unsigned char) "*$omp"[i]) |
6c7a4dfd JJ |
1125 | goto not_continuation; |
1126 | } | |
1127 | ||
6de9cd9a | 1128 | c = next_char (); |
6c7a4dfd | 1129 | if (c == '0' || c == ' ' || c == '\n') |
6de9cd9a | 1130 | goto not_continuation; |
5a06474c JD |
1131 | |
1132 | /* We've got a continuation line. If we are on the very next line after | |
1133 | the last continuation, increment the continuation line count and | |
1134 | check whether the limit has been exceeded. */ | |
5ffeb913 | 1135 | if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1) |
5a06474c JD |
1136 | { |
1137 | if (++continue_count == gfc_option.max_continue_fixed) | |
1138 | { | |
edf1eac2 SK |
1139 | if (gfc_notification_std (GFC_STD_GNU) || pedantic) |
1140 | gfc_warning ("Limit of %d continuations exceeded in " | |
1141 | "statement at %C", | |
1142 | gfc_option.max_continue_fixed); | |
5a06474c JD |
1143 | } |
1144 | } | |
1145 | ||
f449022d JD |
1146 | if (gfc_current_locus.lb != NULL |
1147 | && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb)) | |
5ffeb913 | 1148 | continue_line = gfc_linebuf_linenum (gfc_current_locus.lb); |
6de9cd9a DN |
1149 | } |
1150 | ||
1151 | /* Ready to read first character of continuation line, which might | |
1152 | be another continuation line! */ | |
1153 | goto restart; | |
1154 | ||
1155 | not_continuation: | |
1156 | c = '\n'; | |
63645982 | 1157 | gfc_current_locus = old_loc; |
6de9cd9a DN |
1158 | |
1159 | done: | |
5a06474c JD |
1160 | if (c == '\n') |
1161 | continue_count = 0; | |
6de9cd9a DN |
1162 | continue_flag = 0; |
1163 | return c; | |
1164 | } | |
1165 | ||
1166 | ||
1167 | /* Get the next character of input, folded to lowercase. In fixed | |
1168 | form mode, we also ignore spaces. When matcher subroutines are | |
1169 | parsing character literals, they have to call | |
1170 | gfc_next_char_literal(). */ | |
1171 | ||
8fc541d3 | 1172 | gfc_char_t |
6de9cd9a DN |
1173 | gfc_next_char (void) |
1174 | { | |
8fc541d3 | 1175 | gfc_char_t c; |
6de9cd9a DN |
1176 | |
1177 | do | |
1178 | { | |
1179 | c = gfc_next_char_literal (0); | |
1180 | } | |
d4fa05b9 | 1181 | while (gfc_current_form == FORM_FIXED && gfc_is_whitespace (c)); |
6de9cd9a | 1182 | |
8fc541d3 | 1183 | return gfc_wide_tolower (c); |
6de9cd9a DN |
1184 | } |
1185 | ||
8fc541d3 FXC |
1186 | char |
1187 | gfc_next_ascii_char (void) | |
1188 | { | |
1189 | gfc_char_t c = gfc_next_char (); | |
6de9cd9a | 1190 | |
8fc541d3 FXC |
1191 | return (gfc_wide_fits_in_byte (c) ? (unsigned char) c |
1192 | : (unsigned char) UCHAR_MAX); | |
1193 | } | |
1194 | ||
1195 | ||
1196 | gfc_char_t | |
6de9cd9a DN |
1197 | gfc_peek_char (void) |
1198 | { | |
1199 | locus old_loc; | |
8fc541d3 | 1200 | gfc_char_t c; |
6de9cd9a | 1201 | |
63645982 | 1202 | old_loc = gfc_current_locus; |
6de9cd9a | 1203 | c = gfc_next_char (); |
63645982 | 1204 | gfc_current_locus = old_loc; |
6de9cd9a DN |
1205 | |
1206 | return c; | |
1207 | } | |
1208 | ||
1209 | ||
8fc541d3 FXC |
1210 | char |
1211 | gfc_peek_ascii_char (void) | |
1212 | { | |
1213 | gfc_char_t c = gfc_peek_char (); | |
1214 | ||
1215 | return (gfc_wide_fits_in_byte (c) ? (unsigned char) c | |
1216 | : (unsigned char) UCHAR_MAX); | |
1217 | } | |
1218 | ||
1219 | ||
6de9cd9a DN |
1220 | /* Recover from an error. We try to get past the current statement |
1221 | and get lined up for the next. The next statement follows a '\n' | |
1222 | or a ';'. We also assume that we are not within a character | |
1223 | constant, and deal with finding a '\'' or '"'. */ | |
1224 | ||
1225 | void | |
1226 | gfc_error_recovery (void) | |
1227 | { | |
8fc541d3 | 1228 | gfc_char_t c, delim; |
6de9cd9a DN |
1229 | |
1230 | if (gfc_at_eof ()) | |
1231 | return; | |
1232 | ||
1233 | for (;;) | |
1234 | { | |
1235 | c = gfc_next_char (); | |
1236 | if (c == '\n' || c == ';') | |
1237 | break; | |
1238 | ||
1239 | if (c != '\'' && c != '"') | |
1240 | { | |
1241 | if (gfc_at_eof ()) | |
1242 | break; | |
1243 | continue; | |
1244 | } | |
1245 | delim = c; | |
1246 | ||
1247 | for (;;) | |
1248 | { | |
1249 | c = next_char (); | |
1250 | ||
1251 | if (c == delim) | |
1252 | break; | |
1253 | if (c == '\n') | |
ba1defa5 | 1254 | return; |
6de9cd9a DN |
1255 | if (c == '\\') |
1256 | { | |
1257 | c = next_char (); | |
1258 | if (c == '\n') | |
ba1defa5 | 1259 | return; |
6de9cd9a DN |
1260 | } |
1261 | } | |
1262 | if (gfc_at_eof ()) | |
1263 | break; | |
1264 | } | |
6de9cd9a DN |
1265 | } |
1266 | ||
1267 | ||
1268 | /* Read ahead until the next character to be read is not whitespace. */ | |
1269 | ||
1270 | void | |
1271 | gfc_gobble_whitespace (void) | |
1272 | { | |
840bd9f7 | 1273 | static int linenum = 0; |
6de9cd9a | 1274 | locus old_loc; |
8fc541d3 | 1275 | gfc_char_t c; |
6de9cd9a DN |
1276 | |
1277 | do | |
1278 | { | |
63645982 | 1279 | old_loc = gfc_current_locus; |
6de9cd9a | 1280 | c = gfc_next_char_literal (0); |
840bd9f7 SK |
1281 | /* Issue a warning for nonconforming tabs. We keep track of the line |
1282 | number because the Fortran matchers will often back up and the same | |
1283 | line will be scanned multiple times. */ | |
45a82bd9 | 1284 | if (!gfc_option.warn_tabs && c == '\t') |
840bd9f7 | 1285 | { |
45a82bd9 | 1286 | int cur_linenum = LOCATION_LINE (gfc_current_locus.lb->location); |
45a82bd9 PB |
1287 | if (cur_linenum != linenum) |
1288 | { | |
1289 | linenum = cur_linenum; | |
1290 | gfc_warning_now ("Nonconforming tab character at %C"); | |
1291 | } | |
840bd9f7 | 1292 | } |
6de9cd9a DN |
1293 | } |
1294 | while (gfc_is_whitespace (c)); | |
1295 | ||
63645982 | 1296 | gfc_current_locus = old_loc; |
6de9cd9a DN |
1297 | } |
1298 | ||
1299 | ||
f56c5d5d TS |
1300 | /* Load a single line into pbuf. |
1301 | ||
1302 | If pbuf points to a NULL pointer, it is allocated. | |
1303 | We truncate lines that are too long, unless we're dealing with | |
1304 | preprocessor lines or if the option -ffixed-line-length-none is set, | |
1305 | in which case we reallocate the buffer to fit the entire line, if | |
1306 | need be. | |
1307 | In fixed mode, we expand a tab that occurs within the statement | |
1308 | label region to expand to spaces that leave the next character in | |
ba1defa5 | 1309 | the source region. |
1526c4b5 JD |
1310 | load_line returns whether the line was truncated. |
1311 | ||
1312 | NOTE: The error machinery isn't available at this point, so we can't | |
1313 | easily report line and column numbers consistent with other | |
1314 | parts of gfortran. */ | |
6de9cd9a | 1315 | |
ba1defa5 | 1316 | static int |
8fc541d3 | 1317 | load_line (FILE *input, gfc_char_t **pbuf, int *pbuflen) |
6de9cd9a | 1318 | { |
840bd9f7 | 1319 | static int linenum = 0, current_line = 1; |
d1e3d6ae | 1320 | int c, maxlen, i, preprocessor_flag, buflen = *pbuflen; |
840bd9f7 | 1321 | int trunc_flag = 0, seen_comment = 0; |
1526c4b5 | 1322 | int seen_printable = 0, seen_ampersand = 0; |
8fc541d3 | 1323 | gfc_char_t *buffer; |
fd1935d5 | 1324 | bool found_tab = false; |
f56c5d5d | 1325 | |
1dde8683 | 1326 | /* Determine the maximum allowed line length. */ |
f56c5d5d | 1327 | if (gfc_current_form == FORM_FREE) |
1dde8683 | 1328 | maxlen = gfc_option.free_line_length; |
16ab8e74 | 1329 | else if (gfc_current_form == FORM_FIXED) |
1dde8683 | 1330 | maxlen = gfc_option.fixed_line_length; |
f56c5d5d | 1331 | else |
16ab8e74 | 1332 | maxlen = 72; |
f56c5d5d TS |
1333 | |
1334 | if (*pbuf == NULL) | |
1335 | { | |
1dde8683 BM |
1336 | /* Allocate the line buffer, storing its length into buflen. |
1337 | Note that if maxlen==0, indicating that arbitrary-length lines | |
1338 | are allowed, the buffer will be reallocated if this length is | |
1339 | insufficient; since 132 characters is the length of a standard | |
1340 | free-form line, we use that as a starting guess. */ | |
f56c5d5d TS |
1341 | if (maxlen > 0) |
1342 | buflen = maxlen; | |
1343 | else | |
1dde8683 | 1344 | buflen = 132; |
6de9cd9a | 1345 | |
00660189 | 1346 | *pbuf = gfc_get_wide_string (buflen + 1); |
f56c5d5d | 1347 | } |
6de9cd9a DN |
1348 | |
1349 | i = 0; | |
f56c5d5d | 1350 | buffer = *pbuf; |
6de9cd9a | 1351 | |
fa841200 | 1352 | preprocessor_flag = 0; |
c4da1827 | 1353 | c = getc (input); |
fa841200 | 1354 | if (c == '#') |
f56c5d5d TS |
1355 | /* In order to not truncate preprocessor lines, we have to |
1356 | remember that this is one. */ | |
fa841200 TS |
1357 | preprocessor_flag = 1; |
1358 | ungetc (c, input); | |
1359 | ||
6de9cd9a DN |
1360 | for (;;) |
1361 | { | |
c4da1827 | 1362 | c = getc (input); |
6de9cd9a DN |
1363 | |
1364 | if (c == EOF) | |
1365 | break; | |
1366 | if (c == '\n') | |
1526c4b5 JD |
1367 | { |
1368 | /* Check for illegal use of ampersand. See F95 Standard 3.3.1.3. */ | |
1369 | if (gfc_current_form == FORM_FREE | |
c284e499 | 1370 | && !seen_printable && seen_ampersand) |
1526c4b5 JD |
1371 | { |
1372 | if (pedantic) | |
edf1eac2 SK |
1373 | gfc_error_now ("'&' not allowed by itself in line %d", |
1374 | current_line); | |
1526c4b5 | 1375 | else |
edf1eac2 SK |
1376 | gfc_warning_now ("'&' not allowed by itself in line %d", |
1377 | current_line); | |
1526c4b5 JD |
1378 | } |
1379 | break; | |
1380 | } | |
6de9cd9a DN |
1381 | |
1382 | if (c == '\r') | |
d4fa05b9 | 1383 | continue; /* Gobble characters. */ |
6de9cd9a DN |
1384 | if (c == '\0') |
1385 | continue; | |
1386 | ||
1526c4b5 | 1387 | if (c == '&') |
1526c4b5 | 1388 | { |
c284e499 JD |
1389 | if (seen_ampersand) |
1390 | seen_ampersand = 0; | |
1526c4b5 | 1391 | else |
c284e499 | 1392 | seen_ampersand = 1; |
1526c4b5 JD |
1393 | } |
1394 | ||
bd5db9de | 1395 | if ((c != '&' && c != '!' && c != ' ') || (c == '!' && !seen_ampersand)) |
c284e499 JD |
1396 | seen_printable = 1; |
1397 | ||
840bd9f7 SK |
1398 | /* Is this a fixed-form comment? */ |
1399 | if (gfc_current_form == FORM_FIXED && i == 0 | |
1400 | && (c == '*' || c == 'c' || c == 'd')) | |
1401 | seen_comment = 1; | |
1402 | ||
fd1935d5 TB |
1403 | /* Vendor extension: "<tab>1" marks a continuation line. */ |
1404 | if (found_tab) | |
840bd9f7 | 1405 | { |
fd1935d5 TB |
1406 | found_tab = false; |
1407 | if (c >= '1' && c <= '9') | |
1408 | { | |
1409 | *(buffer-1) = c; | |
1410 | continue; | |
1411 | } | |
1412 | } | |
1413 | ||
1414 | if (gfc_current_form == FORM_FIXED && c == '\t' && i < 6) | |
1415 | { | |
1416 | found_tab = true; | |
1417 | ||
840bd9f7 SK |
1418 | if (!gfc_option.warn_tabs && seen_comment == 0 |
1419 | && current_line != linenum) | |
1420 | { | |
1421 | linenum = current_line; | |
fd1935d5 TB |
1422 | gfc_warning_now ("Nonconforming tab character in column %d " |
1423 | "of line %d", i+1, linenum); | |
840bd9f7 SK |
1424 | } |
1425 | ||
fd1935d5 | 1426 | while (i < 6) |
6de9cd9a DN |
1427 | { |
1428 | *buffer++ = ' '; | |
1429 | i++; | |
1430 | } | |
1431 | ||
1432 | continue; | |
1433 | } | |
1434 | ||
1435 | *buffer++ = c; | |
1436 | i++; | |
1437 | ||
d1e3d6ae | 1438 | if (maxlen == 0 || preprocessor_flag) |
f56c5d5d | 1439 | { |
d1e3d6ae JJ |
1440 | if (i >= buflen) |
1441 | { | |
1442 | /* Reallocate line buffer to double size to hold the | |
3fbab549 | 1443 | overlong line. */ |
d1e3d6ae | 1444 | buflen = buflen * 2; |
8fc541d3 | 1445 | *pbuf = xrealloc (*pbuf, (buflen + 1) * sizeof (gfc_char_t)); |
edf1eac2 | 1446 | buffer = (*pbuf) + i; |
d1e3d6ae | 1447 | } |
f56c5d5d | 1448 | } |
d1e3d6ae | 1449 | else if (i >= maxlen) |
16ab8e74 | 1450 | { |
f56c5d5d | 1451 | /* Truncate the rest of the line. */ |
6de9cd9a DN |
1452 | for (;;) |
1453 | { | |
c4da1827 | 1454 | c = getc (input); |
6de9cd9a DN |
1455 | if (c == '\n' || c == EOF) |
1456 | break; | |
a34938be RG |
1457 | |
1458 | trunc_flag = 1; | |
6de9cd9a DN |
1459 | } |
1460 | ||
1461 | ungetc ('\n', input); | |
1462 | } | |
1463 | } | |
1464 | ||
f56c5d5d TS |
1465 | /* Pad lines to the selected line length in fixed form. */ |
1466 | if (gfc_current_form == FORM_FIXED | |
043c2d9e | 1467 | && gfc_option.fixed_line_length != 0 |
f56c5d5d TS |
1468 | && !preprocessor_flag |
1469 | && c != EOF) | |
043c2d9e BF |
1470 | { |
1471 | while (i++ < maxlen) | |
1472 | *buffer++ = ' '; | |
1473 | } | |
f56c5d5d | 1474 | |
6de9cd9a | 1475 | *buffer = '\0'; |
d1e3d6ae | 1476 | *pbuflen = buflen; |
840bd9f7 | 1477 | current_line++; |
ba1defa5 RG |
1478 | |
1479 | return trunc_flag; | |
6de9cd9a DN |
1480 | } |
1481 | ||
1482 | ||
d4fa05b9 TS |
1483 | /* Get a gfc_file structure, initialize it and add it to |
1484 | the file stack. */ | |
1485 | ||
1486 | static gfc_file * | |
e0bcf78c | 1487 | get_file (const char *name, enum lc_reason reason ATTRIBUTE_UNUSED) |
d4fa05b9 TS |
1488 | { |
1489 | gfc_file *f; | |
1490 | ||
1491 | f = gfc_getmem (sizeof (gfc_file)); | |
1492 | ||
1493 | f->filename = gfc_getmem (strlen (name) + 1); | |
1494 | strcpy (f->filename, name); | |
1495 | ||
1496 | f->next = file_head; | |
1497 | file_head = f; | |
1498 | ||
60332588 | 1499 | f->up = current_file; |
d4fa05b9 | 1500 | if (current_file != NULL) |
1b271c9b | 1501 | f->inclusion_line = current_file->line; |
d4fa05b9 | 1502 | |
5ffeb913 | 1503 | linemap_add (line_table, reason, false, f->filename, 1); |
c8cc8542 | 1504 | |
d4fa05b9 TS |
1505 | return f; |
1506 | } | |
1507 | ||
8fc541d3 | 1508 | |
d4fa05b9 TS |
1509 | /* Deal with a line from the C preprocessor. The |
1510 | initial octothorp has already been seen. */ | |
6de9cd9a DN |
1511 | |
1512 | static void | |
8fc541d3 | 1513 | preprocessor_line (gfc_char_t *c) |
6de9cd9a | 1514 | { |
d4fa05b9 TS |
1515 | bool flag[5]; |
1516 | int i, line; | |
8fc541d3 | 1517 | gfc_char_t *wide_filename; |
d4fa05b9 | 1518 | gfc_file *f; |
2d7c7df6 | 1519 | int escaped, unescape; |
8fc541d3 | 1520 | char *filename; |
6de9cd9a | 1521 | |
d4fa05b9 TS |
1522 | c++; |
1523 | while (*c == ' ' || *c == '\t') | |
1524 | c++; | |
6de9cd9a | 1525 | |
d4fa05b9 | 1526 | if (*c < '0' || *c > '9') |
fa841200 | 1527 | goto bad_cpp_line; |
6de9cd9a | 1528 | |
8fc541d3 | 1529 | line = wide_atoi (c); |
d4fa05b9 | 1530 | |
8fc541d3 | 1531 | c = wide_strchr (c, ' '); |
fa841200 | 1532 | if (c == NULL) |
4c3a6ca1 JJ |
1533 | { |
1534 | /* No file name given. Set new line number. */ | |
1535 | current_file->line = line; | |
1536 | return; | |
1537 | } | |
d7d528c8 ES |
1538 | |
1539 | /* Skip spaces. */ | |
1540 | while (*c == ' ' || *c == '\t') | |
1541 | c++; | |
1542 | ||
1543 | /* Skip quote. */ | |
1544 | if (*c != '"') | |
fa841200 | 1545 | goto bad_cpp_line; |
d7d528c8 ES |
1546 | ++c; |
1547 | ||
8fc541d3 | 1548 | wide_filename = c; |
d4fa05b9 | 1549 | |
d7d528c8 | 1550 | /* Make filename end at quote. */ |
2d7c7df6 | 1551 | unescape = 0; |
d7d528c8 | 1552 | escaped = false; |
edf1eac2 | 1553 | while (*c && ! (!escaped && *c == '"')) |
d7d528c8 ES |
1554 | { |
1555 | if (escaped) | |
edf1eac2 | 1556 | escaped = false; |
2d7c7df6 JJ |
1557 | else if (*c == '\\') |
1558 | { | |
1559 | escaped = true; | |
1560 | unescape++; | |
1561 | } | |
d7d528c8 ES |
1562 | ++c; |
1563 | } | |
1564 | ||
1565 | if (! *c) | |
fa841200 TS |
1566 | /* Preprocessor line has no closing quote. */ |
1567 | goto bad_cpp_line; | |
d7d528c8 | 1568 | |
d4fa05b9 TS |
1569 | *c++ = '\0'; |
1570 | ||
2d7c7df6 JJ |
1571 | /* Undo effects of cpp_quote_string. */ |
1572 | if (unescape) | |
1573 | { | |
8fc541d3 | 1574 | gfc_char_t *s = wide_filename; |
b0b14c7b | 1575 | gfc_char_t *d = gfc_get_wide_string (c - wide_filename - unescape); |
d7d528c8 | 1576 | |
8fc541d3 | 1577 | wide_filename = d; |
2d7c7df6 JJ |
1578 | while (*s) |
1579 | { | |
1580 | if (*s == '\\') | |
1581 | *d++ = *++s; | |
1582 | else | |
1583 | *d++ = *s; | |
1584 | s++; | |
1585 | } | |
1586 | *d = '\0'; | |
1587 | } | |
d7d528c8 | 1588 | |
d4fa05b9 | 1589 | /* Get flags. */ |
4c3a6ca1 | 1590 | |
1e39a151 | 1591 | flag[1] = flag[2] = flag[3] = flag[4] = false; |
6de9cd9a | 1592 | |
6de9cd9a DN |
1593 | for (;;) |
1594 | { | |
8fc541d3 | 1595 | c = wide_strchr (c, ' '); |
d4fa05b9 TS |
1596 | if (c == NULL) |
1597 | break; | |
6de9cd9a | 1598 | |
d4fa05b9 | 1599 | c++; |
8fc541d3 | 1600 | i = wide_atoi (c); |
6de9cd9a | 1601 | |
d4fa05b9 TS |
1602 | if (1 <= i && i <= 4) |
1603 | flag[i] = true; | |
1604 | } | |
4c3a6ca1 | 1605 | |
8fc541d3 FXC |
1606 | /* Convert the filename in wide characters into a filename in narrow |
1607 | characters. */ | |
00660189 | 1608 | filename = gfc_widechar_to_char (wide_filename, -1); |
8fc541d3 | 1609 | |
d4fa05b9 | 1610 | /* Interpret flags. */ |
4c3a6ca1 | 1611 | |
94b00ee4 | 1612 | if (flag[1]) /* Starting new file. */ |
d4fa05b9 | 1613 | { |
c8cc8542 | 1614 | f = get_file (filename, LC_RENAME); |
1b271c9b | 1615 | add_file_change (f->filename, f->inclusion_line); |
d4fa05b9 TS |
1616 | current_file = f; |
1617 | } | |
4c3a6ca1 | 1618 | |
d4fa05b9 TS |
1619 | if (flag[2]) /* Ending current file. */ |
1620 | { | |
94b00ee4 JJ |
1621 | if (!current_file->up |
1622 | || strcmp (current_file->up->filename, filename) != 0) | |
4c3a6ca1 JJ |
1623 | { |
1624 | gfc_warning_now ("%s:%d: file %s left but not entered", | |
1625 | current_file->filename, current_file->line, | |
1626 | filename); | |
2d7c7df6 | 1627 | if (unescape) |
8fc541d3 FXC |
1628 | gfc_free (wide_filename); |
1629 | gfc_free (filename); | |
4c3a6ca1 JJ |
1630 | return; |
1631 | } | |
ee07457b | 1632 | |
1b271c9b | 1633 | add_file_change (NULL, line); |
94b00ee4 | 1634 | current_file = current_file->up; |
ee07457b FXC |
1635 | linemap_add (line_table, LC_RENAME, false, current_file->filename, |
1636 | current_file->line); | |
d4fa05b9 | 1637 | } |
4c3a6ca1 | 1638 | |
d4fa05b9 TS |
1639 | /* The name of the file can be a temporary file produced by |
1640 | cpp. Replace the name if it is different. */ | |
4c3a6ca1 | 1641 | |
d4fa05b9 TS |
1642 | if (strcmp (current_file->filename, filename) != 0) |
1643 | { | |
1644 | gfc_free (current_file->filename); | |
1645 | current_file->filename = gfc_getmem (strlen (filename) + 1); | |
1646 | strcpy (current_file->filename, filename); | |
1647 | } | |
fa841200 | 1648 | |
4c3a6ca1 JJ |
1649 | /* Set new line number. */ |
1650 | current_file->line = line; | |
2d7c7df6 | 1651 | if (unescape) |
8fc541d3 FXC |
1652 | gfc_free (wide_filename); |
1653 | gfc_free (filename); | |
fa841200 TS |
1654 | return; |
1655 | ||
1656 | bad_cpp_line: | |
4c3a6ca1 | 1657 | gfc_warning_now ("%s:%d: Illegal preprocessor directive", |
fa841200 TS |
1658 | current_file->filename, current_file->line); |
1659 | current_file->line++; | |
d4fa05b9 TS |
1660 | } |
1661 | ||
1662 | ||
e0bcf78c | 1663 | static try load_file (const char *, bool); |
d4fa05b9 TS |
1664 | |
1665 | /* include_line()-- Checks a line buffer to see if it is an include | |
1666 | line. If so, we call load_file() recursively to load the included | |
1667 | file. We never return a syntax error because a statement like | |
1668 | "include = 5" is perfectly legal. We return false if no include was | |
1669 | processed or true if we matched an include. */ | |
1670 | ||
1671 | static bool | |
8fc541d3 | 1672 | include_line (gfc_char_t *line) |
d4fa05b9 | 1673 | { |
8fc541d3 FXC |
1674 | gfc_char_t quote, *c, *begin, *stop; |
1675 | char *filename; | |
9b9e4cd6 | 1676 | |
d4fa05b9 | 1677 | c = line; |
9b9e4cd6 JJ |
1678 | |
1679 | if (gfc_option.flag_openmp) | |
1680 | { | |
1681 | if (gfc_current_form == FORM_FREE) | |
1682 | { | |
1683 | while (*c == ' ' || *c == '\t') | |
1684 | c++; | |
1685 | if (*c == '!' && c[1] == '$' && (c[2] == ' ' || c[2] == '\t')) | |
1686 | c += 3; | |
1687 | } | |
1688 | else | |
1689 | { | |
1690 | if ((*c == '!' || *c == 'c' || *c == 'C' || *c == '*') | |
1691 | && c[1] == '$' && (c[2] == ' ' || c[2] == '\t')) | |
1692 | c += 3; | |
1693 | } | |
1694 | } | |
1695 | ||
d4fa05b9 TS |
1696 | while (*c == ' ' || *c == '\t') |
1697 | c++; | |
1698 | ||
00660189 | 1699 | if (gfc_wide_strncasecmp (c, "include", 7)) |
8fc541d3 | 1700 | return false; |
d4fa05b9 TS |
1701 | |
1702 | c += 7; | |
1703 | while (*c == ' ' || *c == '\t') | |
1704 | c++; | |
1705 | ||
1706 | /* Find filename between quotes. */ | |
1707 | ||
1708 | quote = *c++; | |
1709 | if (quote != '"' && quote != '\'') | |
1710 | return false; | |
1711 | ||
1712 | begin = c; | |
1713 | ||
1714 | while (*c != quote && *c != '\0') | |
1715 | c++; | |
1716 | ||
1717 | if (*c == '\0') | |
1718 | return false; | |
1719 | ||
1720 | stop = c++; | |
1721 | ||
1722 | while (*c == ' ' || *c == '\t') | |
1723 | c++; | |
1724 | ||
1725 | if (*c != '\0' && *c != '!') | |
1726 | return false; | |
1727 | ||
f7b529fa | 1728 | /* We have an include line at this point. */ |
d4fa05b9 TS |
1729 | |
1730 | *stop = '\0'; /* It's ok to trash the buffer, as this line won't be | |
1731 | read by anything else. */ | |
1732 | ||
00660189 | 1733 | filename = gfc_widechar_to_char (begin, -1); |
8fc541d3 FXC |
1734 | load_file (filename, false); |
1735 | gfc_free (filename); | |
d4fa05b9 TS |
1736 | return true; |
1737 | } | |
1738 | ||
edf1eac2 | 1739 | |
d4fa05b9 TS |
1740 | /* Load a file into memory by calling load_line until the file ends. */ |
1741 | ||
1742 | static try | |
e0bcf78c | 1743 | load_file (const char *filename, bool initial) |
d4fa05b9 | 1744 | { |
8fc541d3 | 1745 | gfc_char_t *line; |
d4fa05b9 TS |
1746 | gfc_linebuf *b; |
1747 | gfc_file *f; | |
1748 | FILE *input; | |
d1e3d6ae | 1749 | int len, line_len; |
caef7872 | 1750 | bool first_line; |
d4fa05b9 TS |
1751 | |
1752 | for (f = current_file; f; f = f->up) | |
1753 | if (strcmp (filename, f->filename) == 0) | |
1754 | { | |
1755 | gfc_error_now ("File '%s' is being included recursively", filename); | |
1756 | return FAILURE; | |
1757 | } | |
1758 | ||
1759 | if (initial) | |
1760 | { | |
2d7c7df6 JJ |
1761 | if (gfc_src_file) |
1762 | { | |
1763 | input = gfc_src_file; | |
1764 | gfc_src_file = NULL; | |
1765 | } | |
1766 | else | |
1767 | input = gfc_open_file (filename); | |
d4fa05b9 TS |
1768 | if (input == NULL) |
1769 | { | |
1770 | gfc_error_now ("Can't open file '%s'", filename); | |
1771 | return FAILURE; | |
1772 | } | |
1773 | } | |
1774 | else | |
1775 | { | |
31198773 | 1776 | input = gfc_open_included_file (filename, false, false); |
d4fa05b9 TS |
1777 | if (input == NULL) |
1778 | { | |
1779 | gfc_error_now ("Can't open included file '%s'", filename); | |
1780 | return FAILURE; | |
1781 | } | |
1782 | } | |
1783 | ||
1784 | /* Load the file. */ | |
1785 | ||
c8cc8542 | 1786 | f = get_file (filename, initial ? LC_RENAME : LC_ENTER); |
1b271c9b JJ |
1787 | if (!initial) |
1788 | add_file_change (f->filename, f->inclusion_line); | |
d4fa05b9 TS |
1789 | current_file = f; |
1790 | current_file->line = 1; | |
f56c5d5d | 1791 | line = NULL; |
d1e3d6ae | 1792 | line_len = 0; |
caef7872 | 1793 | first_line = true; |
d4fa05b9 | 1794 | |
2d7c7df6 JJ |
1795 | if (initial && gfc_src_preprocessor_lines[0]) |
1796 | { | |
1797 | preprocessor_line (gfc_src_preprocessor_lines[0]); | |
1798 | gfc_free (gfc_src_preprocessor_lines[0]); | |
1799 | gfc_src_preprocessor_lines[0] = NULL; | |
1800 | if (gfc_src_preprocessor_lines[1]) | |
1801 | { | |
1802 | preprocessor_line (gfc_src_preprocessor_lines[1]); | |
1803 | gfc_free (gfc_src_preprocessor_lines[1]); | |
1804 | gfc_src_preprocessor_lines[1] = NULL; | |
1805 | } | |
1806 | } | |
1807 | ||
16ab8e74 | 1808 | for (;;) |
d4fa05b9 | 1809 | { |
d1e3d6ae | 1810 | int trunc = load_line (input, &line, &line_len); |
d4fa05b9 | 1811 | |
8fc541d3 | 1812 | len = gfc_wide_strlen (line); |
6de9cd9a DN |
1813 | if (feof (input) && len == 0) |
1814 | break; | |
1815 | ||
caef7872 FXC |
1816 | /* If this is the first line of the file, it can contain a byte |
1817 | order mark (BOM), which we will ignore: | |
1818 | FF FE is UTF-16 little endian, | |
1819 | FE FF is UTF-16 big endian, | |
1820 | EF BB BF is UTF-8. */ | |
1821 | if (first_line | |
8fc541d3 FXC |
1822 | && ((line_len >= 2 && line[0] == (unsigned char) '\xFF' |
1823 | && line[1] == (unsigned char) '\xFE') | |
1824 | || (line_len >= 2 && line[0] == (unsigned char) '\xFE' | |
1825 | && line[1] == (unsigned char) '\xFF') | |
1826 | || (line_len >= 3 && line[0] == (unsigned char) '\xEF' | |
1827 | && line[1] == (unsigned char) '\xBB' | |
1828 | && line[2] == (unsigned char) '\xBF'))) | |
caef7872 | 1829 | { |
8fc541d3 | 1830 | int n = line[1] == (unsigned char) '\xBB' ? 3 : 2; |
00660189 | 1831 | gfc_char_t *new = gfc_get_wide_string (line_len); |
caef7872 | 1832 | |
8fc541d3 | 1833 | wide_strcpy (new, &line[n]); |
caef7872 FXC |
1834 | gfc_free (line); |
1835 | line = new; | |
1836 | len -= n; | |
1837 | } | |
1838 | ||
d4fa05b9 TS |
1839 | /* There are three things this line can be: a line of Fortran |
1840 | source, an include line or a C preprocessor directive. */ | |
6de9cd9a | 1841 | |
d4fa05b9 TS |
1842 | if (line[0] == '#') |
1843 | { | |
9e8a6720 FXC |
1844 | /* When -g3 is specified, it's possible that we emit #define |
1845 | and #undef lines, which we need to pass to the middle-end | |
1846 | so that it can emit correct debug info. */ | |
1847 | if (debug_info_level == DINFO_LEVEL_VERBOSE | |
8fc541d3 FXC |
1848 | && (wide_strncmp (line, "#define ", 8) == 0 |
1849 | || wide_strncmp (line, "#undef ", 7) == 0)) | |
9e8a6720 FXC |
1850 | ; |
1851 | else | |
1852 | { | |
1853 | preprocessor_line (line); | |
1854 | continue; | |
1855 | } | |
d4fa05b9 | 1856 | } |
6de9cd9a | 1857 | |
caef7872 FXC |
1858 | /* Preprocessed files have preprocessor lines added before the byte |
1859 | order mark, so first_line is not about the first line of the file | |
1860 | but the first line that's not a preprocessor line. */ | |
1861 | first_line = false; | |
1862 | ||
d4fa05b9 TS |
1863 | if (include_line (line)) |
1864 | { | |
1865 | current_file->line++; | |
1866 | continue; | |
6de9cd9a DN |
1867 | } |
1868 | ||
d4fa05b9 TS |
1869 | /* Add line. */ |
1870 | ||
8fc541d3 FXC |
1871 | b = gfc_getmem (gfc_linebuf_header_size |
1872 | + (len + 1) * sizeof (gfc_char_t)); | |
d4fa05b9 | 1873 | |
c8cc8542 | 1874 | b->location |
5ffeb913 | 1875 | = linemap_line_start (line_table, current_file->line++, 120); |
d4fa05b9 | 1876 | b->file = current_file; |
ba1defa5 | 1877 | b->truncated = trunc; |
8fc541d3 | 1878 | wide_strcpy (b->line, line); |
d4fa05b9 TS |
1879 | |
1880 | if (line_head == NULL) | |
1881 | line_head = b; | |
1882 | else | |
1883 | line_tail->next = b; | |
1884 | ||
1885 | line_tail = b; | |
1b271c9b JJ |
1886 | |
1887 | while (file_changes_cur < file_changes_count) | |
1888 | file_changes[file_changes_cur++].lb = b; | |
6de9cd9a | 1889 | } |
d4fa05b9 | 1890 | |
f56c5d5d TS |
1891 | /* Release the line buffer allocated in load_line. */ |
1892 | gfc_free (line); | |
1893 | ||
d4fa05b9 TS |
1894 | fclose (input); |
1895 | ||
1b271c9b JJ |
1896 | if (!initial) |
1897 | add_file_change (NULL, current_file->inclusion_line + 1); | |
d4fa05b9 | 1898 | current_file = current_file->up; |
5ffeb913 | 1899 | linemap_add (line_table, LC_LEAVE, 0, NULL, 0); |
d4fa05b9 | 1900 | return SUCCESS; |
6de9cd9a DN |
1901 | } |
1902 | ||
1903 | ||
d4fa05b9 TS |
1904 | /* Open a new file and start scanning from that file. Returns SUCCESS |
1905 | if everything went OK, FAILURE otherwise. If form == FORM_UKNOWN | |
1906 | it tries to determine the source form from the filename, defaulting | |
1907 | to free form. */ | |
6de9cd9a DN |
1908 | |
1909 | try | |
e0bcf78c | 1910 | gfc_new_file (void) |
6de9cd9a | 1911 | { |
d4fa05b9 | 1912 | try result; |
6de9cd9a | 1913 | |
670637ee DF |
1914 | if (gfc_cpp_enabled ()) |
1915 | { | |
1916 | result = gfc_cpp_preprocess (gfc_source_file); | |
1917 | if (!gfc_cpp_preprocess_only ()) | |
1918 | result = load_file (gfc_cpp_temporary_file (), true); | |
1919 | } | |
1920 | else | |
1921 | result = load_file (gfc_source_file, true); | |
6de9cd9a | 1922 | |
63645982 TS |
1923 | gfc_current_locus.lb = line_head; |
1924 | gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line; | |
6de9cd9a | 1925 | |
d4fa05b9 TS |
1926 | #if 0 /* Debugging aid. */ |
1927 | for (; line_head; line_head = line_head->next) | |
6c1abb5c FXC |
1928 | printf ("%s:%3d %s\n", LOCATION_FILE (line_head->location), |
1929 | LOCATION_LINE (line_head->location), line_head->line); | |
6de9cd9a | 1930 | |
d4fa05b9 TS |
1931 | exit (0); |
1932 | #endif | |
6de9cd9a | 1933 | |
d4fa05b9 | 1934 | return result; |
6de9cd9a | 1935 | } |
2d7c7df6 JJ |
1936 | |
1937 | static char * | |
1938 | unescape_filename (const char *ptr) | |
1939 | { | |
1940 | const char *p = ptr, *s; | |
1941 | char *d, *ret; | |
1942 | int escaped, unescape = 0; | |
1943 | ||
1944 | /* Make filename end at quote. */ | |
1945 | escaped = false; | |
1946 | while (*p && ! (! escaped && *p == '"')) | |
1947 | { | |
1948 | if (escaped) | |
1949 | escaped = false; | |
1950 | else if (*p == '\\') | |
1951 | { | |
1952 | escaped = true; | |
1953 | unescape++; | |
1954 | } | |
1955 | ++p; | |
1956 | } | |
1957 | ||
edf1eac2 | 1958 | if (!*p || p[1]) |
2d7c7df6 JJ |
1959 | return NULL; |
1960 | ||
1961 | /* Undo effects of cpp_quote_string. */ | |
1962 | s = ptr; | |
1963 | d = gfc_getmem (p + 1 - ptr - unescape); | |
1964 | ret = d; | |
1965 | ||
1966 | while (s != p) | |
1967 | { | |
1968 | if (*s == '\\') | |
1969 | *d++ = *++s; | |
1970 | else | |
1971 | *d++ = *s; | |
1972 | s++; | |
1973 | } | |
1974 | *d = '\0'; | |
1975 | return ret; | |
1976 | } | |
1977 | ||
1978 | /* For preprocessed files, if the first tokens are of the form # NUM. | |
1979 | handle the directives so we know the original file name. */ | |
1980 | ||
1981 | const char * | |
1982 | gfc_read_orig_filename (const char *filename, const char **canon_source_file) | |
1983 | { | |
1984 | int c, len; | |
8fc541d3 | 1985 | char *dirname, *tmp; |
2d7c7df6 JJ |
1986 | |
1987 | gfc_src_file = gfc_open_file (filename); | |
1988 | if (gfc_src_file == NULL) | |
1989 | return NULL; | |
1990 | ||
c4da1827 | 1991 | c = getc (gfc_src_file); |
2d7c7df6 JJ |
1992 | ungetc (c, gfc_src_file); |
1993 | ||
1994 | if (c != '#') | |
1995 | return NULL; | |
1996 | ||
1997 | len = 0; | |
1998 | load_line (gfc_src_file, &gfc_src_preprocessor_lines[0], &len); | |
1999 | ||
8fc541d3 | 2000 | if (wide_strncmp (gfc_src_preprocessor_lines[0], "# 1 \"", 5) != 0) |
2d7c7df6 JJ |
2001 | return NULL; |
2002 | ||
00660189 | 2003 | tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[0][5], -1); |
8fc541d3 FXC |
2004 | filename = unescape_filename (tmp); |
2005 | gfc_free (tmp); | |
2d7c7df6 JJ |
2006 | if (filename == NULL) |
2007 | return NULL; | |
2008 | ||
c4da1827 | 2009 | c = getc (gfc_src_file); |
2d7c7df6 JJ |
2010 | ungetc (c, gfc_src_file); |
2011 | ||
2012 | if (c != '#') | |
2013 | return filename; | |
2014 | ||
2015 | len = 0; | |
2016 | load_line (gfc_src_file, &gfc_src_preprocessor_lines[1], &len); | |
2017 | ||
8fc541d3 | 2018 | if (wide_strncmp (gfc_src_preprocessor_lines[1], "# 1 \"", 5) != 0) |
2d7c7df6 JJ |
2019 | return filename; |
2020 | ||
00660189 | 2021 | tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[1][5], -1); |
8fc541d3 FXC |
2022 | dirname = unescape_filename (tmp); |
2023 | gfc_free (tmp); | |
2d7c7df6 JJ |
2024 | if (dirname == NULL) |
2025 | return filename; | |
2026 | ||
2027 | len = strlen (dirname); | |
2028 | if (len < 3 || dirname[len - 1] != '/' || dirname[len - 2] != '/') | |
2029 | { | |
2030 | gfc_free (dirname); | |
2031 | return filename; | |
2032 | } | |
2033 | dirname[len - 2] = '\0'; | |
2034 | set_src_pwd (dirname); | |
2035 | ||
2036 | if (! IS_ABSOLUTE_PATH (filename)) | |
2037 | { | |
2038 | char *p = gfc_getmem (len + strlen (filename)); | |
2039 | ||
2040 | memcpy (p, dirname, len - 2); | |
2041 | p[len - 2] = '/'; | |
2042 | strcpy (p + len - 1, filename); | |
2043 | *canon_source_file = p; | |
2044 | } | |
2045 | ||
2046 | gfc_free (dirname); | |
2047 | return filename; | |
2048 | } |