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