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