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