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