ipa-cp.c (ipcp_cloning_candidate_p): Use opt_for_fn.
[gcc.git] / gcc / fortran / scanner.c
1 /* Character scanner.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
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"
44 #include "system.h"
45 #include "coretypes.h"
46 #include "gfortran.h"
47 #include "toplev.h" /* For set_src_pwd. */
48 #include "debug.h"
49 #include "flags.h"
50 #include "cpp.h"
51 #include "scanner.h"
52
53 /* List of include file search directories. */
54 gfc_directorylist *include_dirs, *intrinsic_modules_dirs;
55
56 static gfc_file *file_head, *current_file;
57
58 static int continue_flag, end_flag, openmp_flag, gcc_attribute_flag;
59 static int continue_count, continue_line;
60 static locus openmp_locus;
61 static locus gcc_attribute_locus;
62
63 gfc_source_form gfc_current_form;
64 static gfc_linebuf *line_head, *line_tail;
65
66 locus gfc_current_locus;
67 const char *gfc_source_file;
68 static FILE *gfc_src_file;
69 static gfc_char_t *gfc_src_preprocessor_lines[2];
70
71 static struct gfc_file_change
72 {
73 const char *filename;
74 gfc_linebuf *lb;
75 int line;
76 } *file_changes;
77 size_t file_changes_cur, file_changes_count;
78 size_t file_changes_allocated;
79
80
81 /* Functions dealing with our wide characters (gfc_char_t) and
82 sequences of such characters. */
83
84 int
85 gfc_wide_fits_in_byte (gfc_char_t c)
86 {
87 return (c <= UCHAR_MAX);
88 }
89
90 static inline int
91 wide_is_ascii (gfc_char_t c)
92 {
93 return (gfc_wide_fits_in_byte (c) && ((unsigned char) c & ~0x7f) == 0);
94 }
95
96 int
97 gfc_wide_is_printable (gfc_char_t c)
98 {
99 return (gfc_wide_fits_in_byte (c) && ISPRINT ((unsigned char) c));
100 }
101
102 gfc_char_t
103 gfc_wide_tolower (gfc_char_t c)
104 {
105 return (wide_is_ascii (c) ? (gfc_char_t) TOLOWER((unsigned char) c) : c);
106 }
107
108 gfc_char_t
109 gfc_wide_toupper (gfc_char_t c)
110 {
111 return (wide_is_ascii (c) ? (gfc_char_t) TOUPPER((unsigned char) c) : c);
112 }
113
114 int
115 gfc_wide_is_digit (gfc_char_t c)
116 {
117 return (c >= '0' && c <= '9');
118 }
119
120 static inline int
121 wide_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
133 size_t
134 gfc_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
144 gfc_char_t *
145 gfc_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
155 static gfc_char_t *
156 wide_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
166 static gfc_char_t *
167 wide_strchr (const gfc_char_t *s, gfc_char_t c)
168 {
169 do {
170 if (*s == c)
171 {
172 return CONST_CAST(gfc_char_t *, s);
173 }
174 } while (*s++);
175 return 0;
176 }
177
178 char *
179 gfc_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));
190 res = XNEWVEC (char, len + 1);
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
202 gfc_char_t *
203 gfc_char_to_widechar (const char *s)
204 {
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);
213
214 for (i = 0; i < len; i++)
215 res[i] = (unsigned char) s[i];
216
217 res[len] = '\0';
218 return res;
219 }
220
221 static int
222 wide_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
238 int
239 gfc_wide_strncasecmp (const gfc_char_t *s1, const char *s2, size_t n)
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
256 /* Main scanner initialization. */
257
258 void
259 gfc_scanner_init_1 (void)
260 {
261 file_head = NULL;
262 line_head = NULL;
263 line_tail = NULL;
264
265 continue_count = 0;
266 continue_line = 0;
267
268 end_flag = 0;
269 }
270
271
272 /* Main scanner destructor. */
273
274 void
275 gfc_scanner_done_1 (void)
276 {
277 gfc_linebuf *lb;
278 gfc_file *f;
279
280 while(line_head != NULL)
281 {
282 lb = line_head->next;
283 free (line_head);
284 line_head = lb;
285 }
286
287 while(file_head != NULL)
288 {
289 f = file_head->next;
290 free (file_head->filename);
291 free (file_head);
292 file_head = f;
293 }
294 }
295
296
297 /* Adds path to the list pointed to by list. */
298
299 static void
300 add_path_to_list (gfc_directorylist **list, const char *path,
301 bool use_for_modules, bool head, bool warn)
302 {
303 gfc_directorylist *dir;
304 const char *p;
305 char *q;
306 struct stat st;
307 size_t len;
308 int i;
309
310 p = path;
311 while (*p == ' ' || *p == '\t') /* someone might do "-I include" */
312 if (*p++ == '\0')
313 return;
314
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;
321 while (i >=0 && IS_DIR_SEPARATOR (q[i]))
322 q[i--] = '\0';
323
324 if (stat (q, &st))
325 {
326 if (errno != ENOENT)
327 gfc_warning_now_2 ("Include directory %qs: %s", path,
328 xstrerror(errno));
329 else if (warn)
330 gfc_warning_now_2 (OPT_Wmissing_include_dirs,
331 "Nonexistent include directory %qs", path);
332 return;
333 }
334 else if (!S_ISDIR (st.st_mode))
335 {
336 gfc_warning_now_2 ("%qs is not a directory", path);
337 return;
338 }
339
340 if (head || *list == NULL)
341 {
342 dir = XCNEW (gfc_directorylist);
343 if (!head)
344 *list = dir;
345 }
346 else
347 {
348 dir = *list;
349 while (dir->next)
350 dir = dir->next;
351
352 dir->next = XCNEW (gfc_directorylist);
353 dir = dir->next;
354 }
355
356 dir->next = head ? *list : NULL;
357 if (head)
358 *list = dir;
359 dir->use_for_modules = use_for_modules;
360 dir->path = XCNEWVEC (char, strlen (p) + 2);
361 strcpy (dir->path, p);
362 strcat (dir->path, "/"); /* make '/' last character */
363 }
364
365
366 void
367 gfc_add_include_path (const char *path, bool use_for_modules, bool file_dir,
368 bool warn)
369 {
370 add_path_to_list (&include_dirs, path, use_for_modules, file_dir, warn);
371
372 /* For '#include "..."' these directories are automatically searched. */
373 if (!file_dir)
374 gfc_cpp_add_include_path (xstrdup(path), true);
375 }
376
377
378 void
379 gfc_add_intrinsic_modules_path (const char *path)
380 {
381 add_path_to_list (&intrinsic_modules_dirs, path, true, false, false);
382 }
383
384
385 /* Release resources allocated for options. */
386
387 void
388 gfc_release_include_path (void)
389 {
390 gfc_directorylist *p;
391
392 while (include_dirs != NULL)
393 {
394 p = include_dirs;
395 include_dirs = include_dirs->next;
396 free (p->path);
397 free (p);
398 }
399
400 while (intrinsic_modules_dirs != NULL)
401 {
402 p = intrinsic_modules_dirs;
403 intrinsic_modules_dirs = intrinsic_modules_dirs->next;
404 free (p->path);
405 free (p);
406 }
407
408 free (gfc_option.module_dir);
409 }
410
411
412 static FILE *
413 open_included_file (const char *name, gfc_directorylist *list,
414 bool module, bool system)
415 {
416 char *fullname;
417 gfc_directorylist *p;
418 FILE *f;
419
420 for (p = list; p; p = p->next)
421 {
422 if (module && !p->use_for_modules)
423 continue;
424
425 fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1);
426 strcpy (fullname, p->path);
427 strcat (fullname, name);
428
429 f = gfc_open_file (fullname);
430 if (f != NULL)
431 {
432 if (gfc_cpp_makedep ())
433 gfc_cpp_add_dep (fullname, system);
434
435 return f;
436 }
437 }
438
439 return NULL;
440 }
441
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
447 FILE *
448 gfc_open_included_file (const char *name, bool include_cwd, bool module)
449 {
450 FILE *f = NULL;
451
452 if (IS_ABSOLUTE_PATH (name) || include_cwd)
453 {
454 f = gfc_open_file (name);
455 if (f && gfc_cpp_makedep ())
456 gfc_cpp_add_dep (name, false);
457 }
458
459 if (!f)
460 f = open_included_file (name, include_dirs, module, false);
461
462 return f;
463 }
464
465
466 /* Test to see if we're at the end of the main source file. */
467
468 int
469 gfc_at_end (void)
470 {
471 return end_flag;
472 }
473
474
475 /* Test to see if we're at the end of the current file. */
476
477 int
478 gfc_at_eof (void)
479 {
480 if (gfc_at_end ())
481 return 1;
482
483 if (line_head == NULL)
484 return 1; /* Null file */
485
486 if (gfc_current_locus.lb == NULL)
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
495 int
496 gfc_at_bol (void)
497 {
498 if (gfc_at_eof ())
499 return 1;
500
501 return (gfc_current_locus.nextc == gfc_current_locus.lb->line);
502 }
503
504
505 /* Test to see if we're at the end of a line. */
506
507 int
508 gfc_at_eol (void)
509 {
510 if (gfc_at_eof ())
511 return 1;
512
513 return (*gfc_current_locus.nextc == '\0');
514 }
515
516 static void
517 add_file_change (const char *filename, int line)
518 {
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;
525 file_changes = XRESIZEVEC (struct gfc_file_change, file_changes,
526 file_changes_allocated);
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 }
532
533 static void
534 report_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;
548 }
549
550 void
551 gfc_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
558 file_changes_cur = 0;
559 report_file_change (gfc_current_locus.lb);
560 }
561
562 void
563 gfc_end_source_files (void)
564 {
565 report_file_change (NULL);
566
567 if (debug_hooks->start_end_main_source_file)
568 (*debug_hooks->end_source_file) (0);
569 }
570
571 /* Advance the current line pointer to the next line. */
572
573 void
574 gfc_advance_line (void)
575 {
576 if (gfc_at_end ())
577 return;
578
579 if (gfc_current_locus.lb == NULL)
580 {
581 end_flag = 1;
582 return;
583 }
584
585 if (gfc_current_locus.lb->next
586 && !gfc_current_locus.lb->next->dbg_emitted)
587 {
588 report_file_change (gfc_current_locus.lb->next);
589 gfc_current_locus.lb->next->dbg_emitted = true;
590 }
591
592 gfc_current_locus.lb = gfc_current_locus.lb->next;
593
594 if (gfc_current_locus.lb != NULL)
595 gfc_current_locus.nextc = gfc_current_locus.lb->line;
596 else
597 {
598 gfc_current_locus.nextc = NULL;
599 end_flag = 1;
600 }
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
613 static gfc_char_t
614 next_char (void)
615 {
616 gfc_char_t c;
617
618 if (gfc_current_locus.nextc == NULL)
619 return '\n';
620
621 c = *gfc_current_locus.nextc++;
622 if (c == '\0')
623 {
624 gfc_current_locus.nextc--; /* Remain on this line. */
625 c = '\n';
626 }
627
628 return c;
629 }
630
631
632 /* Skip a comment. When we come here the parse pointer is positioned
633 immediately after the comment character. If we ever implement
634 compiler directives within comments, here is where we parse the
635 directive. */
636
637 static void
638 skip_comment_line (void)
639 {
640 gfc_char_t c;
641
642 do
643 {
644 c = next_char ();
645 }
646 while (c != '\n');
647
648 gfc_advance_line ();
649 }
650
651
652 int
653 gfc_define_undef_line (void)
654 {
655 char *tmp;
656
657 /* All lines beginning with '#' are either #define or #undef. */
658 if (debug_info_level != DINFO_LEVEL_VERBOSE || gfc_peek_ascii_char () != '#')
659 return 0;
660
661 if (wide_strncmp (gfc_current_locus.nextc, "#define ", 8) == 0)
662 {
663 tmp = gfc_widechar_to_char (&gfc_current_locus.nextc[8], -1);
664 (*debug_hooks->define) (gfc_linebuf_linenum (gfc_current_locus.lb),
665 tmp);
666 free (tmp);
667 }
668
669 if (wide_strncmp (gfc_current_locus.nextc, "#undef ", 7) == 0)
670 {
671 tmp = gfc_widechar_to_char (&gfc_current_locus.nextc[7], -1);
672 (*debug_hooks->undef) (gfc_linebuf_linenum (gfc_current_locus.lb),
673 tmp);
674 free (tmp);
675 }
676
677 /* Skip the rest of the line. */
678 skip_comment_line ();
679
680 return 1;
681 }
682
683
684 /* Return true if GCC$ was matched. */
685 static bool
686 skip_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
712 /* Comment lines are null lines, lines containing only blanks or lines
713 on which the first nonblank line is a '!'.
714 Return true if !$ openmp conditional compilation sentinel was
715 seen. */
716
717 static bool
718 skip_free_comments (void)
719 {
720 locus start;
721 gfc_char_t c;
722 int at_bol;
723
724 for (;;)
725 {
726 at_bol = gfc_at_bol ();
727 start = gfc_current_locus;
728 if (gfc_at_eof ())
729 break;
730
731 do
732 c = next_char ();
733 while (gfc_is_whitespace (c));
734
735 if (c == '\n')
736 {
737 gfc_advance_line ();
738 continue;
739 }
740
741 if (c == '!')
742 {
743 /* Keep the !GCC$ line. */
744 if (at_bol && skip_gcc_attribute (start))
745 return false;
746
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). */
752 if ((gfc_option.gfc_flag_openmp
753 || gfc_option.gfc_flag_openmp_simd) && at_bol)
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')
762 && ((c = next_char ()) == 'p' || c == 'P'))
763 {
764 if ((c = next_char ()) == ' ' || c == '\t'
765 || continue_flag)
766 {
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 }
776 }
777 else
778 gfc_warning_now ("!$OMP at %C starts a commented "
779 "line as it neither is followed "
780 "by a space nor is a "
781 "continuation line");
782 }
783 gfc_current_locus = old_loc;
784 next_char ();
785 c = next_char ();
786 }
787 if (continue_flag || c == ' ' || c == '\t')
788 {
789 gfc_current_locus = old_loc;
790 next_char ();
791 openmp_flag = 0;
792 return true;
793 }
794 }
795 gfc_current_locus = old_loc;
796 }
797 skip_comment_line ();
798 continue;
799 }
800
801 break;
802 }
803
804 if (openmp_flag && at_bol)
805 openmp_flag = 0;
806
807 gcc_attribute_flag = 0;
808 gfc_current_locus = start;
809 return false;
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 '*'
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. */
817
818 static void
819 skip_fixed_comments (void)
820 {
821 locus start;
822 int col;
823 gfc_char_t c;
824
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
847 for (;;)
848 {
849 start = gfc_current_locus;
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 {
862 if (skip_gcc_attribute (start))
863 {
864 /* Canonicalize to *$omp. */
865 *start.nextc = '*';
866 return;
867 }
868
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. */
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
879 if (gfc_option.gfc_flag_openmp || gfc_option.gfc_flag_openmp_simd)
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)
892 || c == ' ' || c == '\t' || c == '0'))
893 {
894 do
895 c = next_char ();
896 while (gfc_is_whitespace (c));
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;
915 else if (c == '\t')
916 {
917 col = 6;
918 break;
919 }
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)
927 || c == ' ' || c == '\t' || c == '0'))
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 }
938 skip_comment_line ();
939 continue;
940 }
941
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
953 col = 1;
954
955 while (gfc_is_whitespace (c))
956 {
957 c = next_char ();
958 col++;
959 }
960
961 if (c == '\n')
962 {
963 gfc_advance_line ();
964 continue;
965 }
966
967 if (col != 6 && c == '!')
968 {
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);
972 skip_comment_line ();
973 continue;
974 }
975
976 break;
977 }
978
979 openmp_flag = 0;
980 gcc_attribute_flag = 0;
981 gfc_current_locus = start;
982 }
983
984
985 /* Skips the current line if it is a comment. */
986
987 void
988 gfc_skip_comments (void)
989 {
990 if (gfc_current_form == FORM_FREE)
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
1004 gfc_char_t
1005 gfc_next_char_literal (gfc_instring in_string)
1006 {
1007 locus old_loc;
1008 int i, prev_openmp_flag;
1009 gfc_char_t c;
1010
1011 continue_flag = 0;
1012
1013 restart:
1014 c = next_char ();
1015 if (gfc_at_end ())
1016 {
1017 continue_count = 0;
1018 return c;
1019 }
1020
1021 if (gfc_current_form == FORM_FREE)
1022 {
1023 bool openmp_cond_flag;
1024
1025 if (!in_string && c == '!')
1026 {
1027 if (gcc_attribute_flag
1028 && memcmp (&gfc_current_locus, &gcc_attribute_locus,
1029 sizeof (gfc_current_locus)) == 0)
1030 goto done;
1031
1032 if (openmp_flag
1033 && memcmp (&gfc_current_locus, &openmp_locus,
1034 sizeof (gfc_current_locus)) == 0)
1035 goto done;
1036
1037 /* This line can't be continued */
1038 do
1039 {
1040 c = next_char ();
1041 }
1042 while (c != '\n');
1043
1044 /* Avoid truncation warnings for comment ending lines. */
1045 gfc_current_locus.lb->truncated = 0;
1046
1047 goto done;
1048 }
1049
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;
1055 gfc_char_t *current_nextc = gfc_current_locus.nextc;
1056
1057 gfc_current_locus.lb->truncated = 0;
1058 gfc_current_locus.nextc = gfc_current_locus.lb->line + maxlen;
1059 gfc_warning_now ("Line truncated at %L", &gfc_current_locus);
1060 gfc_current_locus.nextc = current_nextc;
1061 }
1062
1063 if (c != '&')
1064 goto done;
1065
1066 /* If the next nonblank character is a ! or \n, we've got a
1067 continuation line. */
1068 old_loc = gfc_current_locus;
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
1075 after the '&'. */
1076
1077 if (in_string && c != '\n')
1078 {
1079 gfc_current_locus = old_loc;
1080 c = '&';
1081 goto done;
1082 }
1083
1084 if (c != '!' && c != '\n')
1085 {
1086 gfc_current_locus = old_loc;
1087 c = '&';
1088 goto done;
1089 }
1090
1091 prev_openmp_flag = openmp_flag;
1092 continue_flag = 1;
1093 if (c == '!')
1094 skip_comment_line ();
1095 else
1096 gfc_advance_line ();
1097
1098 if (gfc_at_eof ())
1099 goto not_continuation;
1100
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. */
1104 if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1)
1105 {
1106 if (++continue_count == gfc_option.max_continue_free)
1107 {
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);
1111 }
1112 }
1113
1114 /* Now find where it continues. First eat any comment lines. */
1115 openmp_cond_flag = skip_free_comments ();
1116
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
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
1129 /* Now that we have a non-comment line, probe ahead for the
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. */
1133
1134 old_loc = gfc_current_locus;
1135
1136 c = next_char ();
1137 while (gfc_is_whitespace (c))
1138 c = next_char ();
1139
1140 if (openmp_flag)
1141 {
1142 for (i = 0; i < 5; i++, c = next_char ())
1143 {
1144 gcc_assert (gfc_wide_tolower (c) == (unsigned char) "!$omp"[i]);
1145 if (i == 4)
1146 old_loc = gfc_current_locus;
1147 }
1148 while (gfc_is_whitespace (c))
1149 c = next_char ();
1150 }
1151
1152 if (c != '&')
1153 {
1154 if (in_string)
1155 {
1156 gfc_current_locus.nextc--;
1157 if (gfc_option.warn_ampersand && in_string == INSTRING_WARN)
1158 gfc_warning ("Missing '&' in continued character "
1159 "constant at %C");
1160 }
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--;
1165 else
1166 {
1167 c = ' ';
1168 gfc_current_locus = old_loc;
1169 goto done;
1170 }
1171 }
1172 }
1173 else /* Fixed form. */
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');
1184
1185 /* Avoid truncation warnings for comment ending lines. */
1186 gfc_current_locus.lb->truncated = 0;
1187 }
1188
1189 if (c != '\n')
1190 goto done;
1191
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;
1197 gfc_warning_now ("Line truncated at %L", &gfc_current_locus);
1198 }
1199
1200 prev_openmp_flag = openmp_flag;
1201 continue_flag = 1;
1202 old_loc = gfc_current_locus;
1203
1204 gfc_advance_line ();
1205 skip_fixed_comments ();
1206
1207 /* See if this line is a continuation line. */
1208 if (openmp_flag != prev_openmp_flag)
1209 {
1210 openmp_flag = prev_openmp_flag;
1211 goto not_continuation;
1212 }
1213
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 ();
1225 if (gfc_wide_tolower (c) != (unsigned char) "*$omp"[i])
1226 goto not_continuation;
1227 }
1228
1229 c = next_char ();
1230 if (c == '0' || c == ' ' || c == '\n')
1231 goto not_continuation;
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. */
1236 if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1)
1237 {
1238 if (++continue_count == gfc_option.max_continue_fixed)
1239 {
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);
1244 }
1245 }
1246
1247 if (gfc_current_locus.lb != NULL
1248 && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
1249 continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
1250 }
1251
1252 /* Ready to read first character of continuation line, which might
1253 be another continuation line! */
1254 goto restart;
1255
1256 not_continuation:
1257 c = '\n';
1258 gfc_current_locus = old_loc;
1259
1260 done:
1261 if (c == '\n')
1262 continue_count = 0;
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
1273 gfc_char_t
1274 gfc_next_char (void)
1275 {
1276 gfc_char_t c;
1277
1278 do
1279 {
1280 c = gfc_next_char_literal (NONSTRING);
1281 }
1282 while (gfc_current_form == FORM_FIXED && gfc_is_whitespace (c));
1283
1284 return gfc_wide_tolower (c);
1285 }
1286
1287 char
1288 gfc_next_ascii_char (void)
1289 {
1290 gfc_char_t c = gfc_next_char ();
1291
1292 return (gfc_wide_fits_in_byte (c) ? (unsigned char) c
1293 : (unsigned char) UCHAR_MAX);
1294 }
1295
1296
1297 gfc_char_t
1298 gfc_peek_char (void)
1299 {
1300 locus old_loc;
1301 gfc_char_t c;
1302
1303 old_loc = gfc_current_locus;
1304 c = gfc_next_char ();
1305 gfc_current_locus = old_loc;
1306
1307 return c;
1308 }
1309
1310
1311 char
1312 gfc_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
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
1326 void
1327 gfc_error_recovery (void)
1328 {
1329 gfc_char_t c, delim;
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')
1355 return;
1356 if (c == '\\')
1357 {
1358 c = next_char ();
1359 if (c == '\n')
1360 return;
1361 }
1362 }
1363 if (gfc_at_eof ())
1364 break;
1365 }
1366 }
1367
1368
1369 /* Read ahead until the next character to be read is not whitespace. */
1370
1371 void
1372 gfc_gobble_whitespace (void)
1373 {
1374 static int linenum = 0;
1375 locus old_loc;
1376 gfc_char_t c;
1377
1378 do
1379 {
1380 old_loc = gfc_current_locus;
1381 c = gfc_next_char_literal (NONSTRING);
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. */
1385 if (!gfc_option.warn_tabs && c == '\t')
1386 {
1387 int cur_linenum = LOCATION_LINE (gfc_current_locus.lb->location);
1388 if (cur_linenum != linenum)
1389 {
1390 linenum = cur_linenum;
1391 gfc_warning_now ("Nonconforming tab character at %C");
1392 }
1393 }
1394 }
1395 while (gfc_is_whitespace (c));
1396
1397 gfc_current_locus = old_loc;
1398 }
1399
1400
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
1410 the source region.
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
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. */
1421
1422 static int
1423 load_line (FILE *input, gfc_char_t **pbuf, int *pbuflen, const int *first_char)
1424 {
1425 static int linenum = 0, current_line = 1;
1426 int c, maxlen, i, preprocessor_flag, buflen = *pbuflen;
1427 int trunc_flag = 0, seen_comment = 0;
1428 int seen_printable = 0, seen_ampersand = 0, quoted = ' ';
1429 gfc_char_t *buffer;
1430 bool found_tab = false;
1431
1432 /* Determine the maximum allowed line length. */
1433 if (gfc_current_form == FORM_FREE)
1434 maxlen = gfc_option.free_line_length;
1435 else if (gfc_current_form == FORM_FIXED)
1436 maxlen = gfc_option.fixed_line_length;
1437 else
1438 maxlen = 72;
1439
1440 if (*pbuf == NULL)
1441 {
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. */
1447 if (maxlen > 0)
1448 buflen = maxlen;
1449 else
1450 buflen = 132;
1451
1452 *pbuf = gfc_get_wide_string (buflen + 1);
1453 }
1454
1455 i = 0;
1456 buffer = *pbuf;
1457
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);
1466
1467 for (;;)
1468 {
1469 if (c == EOF)
1470 break;
1471
1472 if (c == '\n')
1473 {
1474 /* Check for illegal use of ampersand. See F95 Standard 3.3.1.3. */
1475 if (gfc_current_form == FORM_FREE
1476 && !seen_printable && seen_ampersand)
1477 {
1478 if (pedantic)
1479 gfc_error_now_2 ("%<&%> not allowed by itself in line %d",
1480 current_line);
1481 else
1482 gfc_warning_now_2 ("%<&%> not allowed by itself in line %d",
1483 current_line);
1484 }
1485 break;
1486 }
1487
1488 if (c == '\r' || c == '\0')
1489 goto next_char; /* Gobble characters. */
1490
1491 if (c == '&')
1492 {
1493 if (seen_ampersand)
1494 {
1495 seen_ampersand = 0;
1496 seen_printable = 1;
1497 }
1498 else
1499 seen_ampersand = 1;
1500 }
1501
1502 if ((c != '&' && c != '!' && c != ' ') || (c == '!' && !seen_ampersand))
1503 seen_printable = 1;
1504
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
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
1522 /* Vendor extension: "<tab>1" marks a continuation line. */
1523 if (found_tab)
1524 {
1525 found_tab = false;
1526 if (c >= '1' && c <= '9')
1527 {
1528 *(buffer-1) = c;
1529 goto next_char;
1530 }
1531 }
1532
1533 if (gfc_current_form == FORM_FIXED && c == '\t' && i < 6)
1534 {
1535 found_tab = true;
1536
1537 if (!gfc_option.warn_tabs && seen_comment == 0
1538 && current_line != linenum)
1539 {
1540 linenum = current_line;
1541 gfc_warning_now_2 ("Nonconforming tab character in column %d "
1542 "of line %d", i+1, linenum);
1543 }
1544
1545 while (i < 6)
1546 {
1547 *buffer++ = ' ';
1548 i++;
1549 }
1550
1551 goto next_char;
1552 }
1553
1554 *buffer++ = c;
1555 i++;
1556
1557 if (maxlen == 0 || preprocessor_flag)
1558 {
1559 if (i >= buflen)
1560 {
1561 /* Reallocate line buffer to double size to hold the
1562 overlong line. */
1563 buflen = buflen * 2;
1564 *pbuf = XRESIZEVEC (gfc_char_t, *pbuf, (buflen + 1));
1565 buffer = (*pbuf) + i;
1566 }
1567 }
1568 else if (i >= maxlen)
1569 {
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
1576 /* Truncate the rest of the line. */
1577 for (;;)
1578 {
1579 c = getc (input);
1580 if (c == '\r' || c == ' ')
1581 continue;
1582
1583 if (c == '\n' || c == EOF)
1584 break;
1585
1586 if (!trunc_warn && c != '!')
1587 trunc_warn = true;
1588
1589 if (trunc_warn && ((gfc_current_form == FORM_FIXED && c == '&')
1590 || c == '!'))
1591 trunc_warn = false;
1592
1593 if (c == '!')
1594 seen_comment = 1;
1595
1596 if (trunc_warn && !seen_comment)
1597 trunc_flag = 1;
1598 }
1599
1600 c = '\n';
1601 continue;
1602 }
1603
1604 next_char:
1605 c = getc (input);
1606 }
1607
1608 /* Pad lines to the selected line length in fixed form. */
1609 if (gfc_current_form == FORM_FIXED
1610 && gfc_option.fixed_line_length != 0
1611 && !preprocessor_flag
1612 && c != EOF)
1613 {
1614 while (i++ < maxlen)
1615 *buffer++ = ' ';
1616 }
1617
1618 *buffer = '\0';
1619 *pbuflen = buflen;
1620 current_line++;
1621
1622 return trunc_flag;
1623 }
1624
1625
1626 /* Get a gfc_file structure, initialize it and add it to
1627 the file stack. */
1628
1629 static gfc_file *
1630 get_file (const char *name, enum lc_reason reason ATTRIBUTE_UNUSED)
1631 {
1632 gfc_file *f;
1633
1634 f = XCNEW (gfc_file);
1635
1636 f->filename = xstrdup (name);
1637
1638 f->next = file_head;
1639 file_head = f;
1640
1641 f->up = current_file;
1642 if (current_file != NULL)
1643 f->inclusion_line = current_file->line;
1644
1645 linemap_add (line_table, reason, false, f->filename, 1);
1646
1647 return f;
1648 }
1649
1650
1651 /* Deal with a line from the C preprocessor. The
1652 initial octothorp has already been seen. */
1653
1654 static void
1655 preprocessor_line (gfc_char_t *c)
1656 {
1657 bool flag[5];
1658 int i, line;
1659 gfc_char_t *wide_filename;
1660 gfc_file *f;
1661 int escaped, unescape;
1662 char *filename;
1663
1664 c++;
1665 while (*c == ' ' || *c == '\t')
1666 c++;
1667
1668 if (*c < '0' || *c > '9')
1669 goto bad_cpp_line;
1670
1671 line = wide_atoi (c);
1672
1673 c = wide_strchr (c, ' ');
1674 if (c == NULL)
1675 {
1676 /* No file name given. Set new line number. */
1677 current_file->line = line;
1678 return;
1679 }
1680
1681 /* Skip spaces. */
1682 while (*c == ' ' || *c == '\t')
1683 c++;
1684
1685 /* Skip quote. */
1686 if (*c != '"')
1687 goto bad_cpp_line;
1688 ++c;
1689
1690 wide_filename = c;
1691
1692 /* Make filename end at quote. */
1693 unescape = 0;
1694 escaped = false;
1695 while (*c && ! (!escaped && *c == '"'))
1696 {
1697 if (escaped)
1698 escaped = false;
1699 else if (*c == '\\')
1700 {
1701 escaped = true;
1702 unescape++;
1703 }
1704 ++c;
1705 }
1706
1707 if (! *c)
1708 /* Preprocessor line has no closing quote. */
1709 goto bad_cpp_line;
1710
1711 *c++ = '\0';
1712
1713 /* Undo effects of cpp_quote_string. */
1714 if (unescape)
1715 {
1716 gfc_char_t *s = wide_filename;
1717 gfc_char_t *d = gfc_get_wide_string (c - wide_filename - unescape);
1718
1719 wide_filename = d;
1720 while (*s)
1721 {
1722 if (*s == '\\')
1723 *d++ = *++s;
1724 else
1725 *d++ = *s;
1726 s++;
1727 }
1728 *d = '\0';
1729 }
1730
1731 /* Get flags. */
1732
1733 flag[1] = flag[2] = flag[3] = flag[4] = false;
1734
1735 for (;;)
1736 {
1737 c = wide_strchr (c, ' ');
1738 if (c == NULL)
1739 break;
1740
1741 c++;
1742 i = wide_atoi (c);
1743
1744 if (1 <= i && i <= 4)
1745 flag[i] = true;
1746 }
1747
1748 /* Convert the filename in wide characters into a filename in narrow
1749 characters. */
1750 filename = gfc_widechar_to_char (wide_filename, -1);
1751
1752 /* Interpret flags. */
1753
1754 if (flag[1]) /* Starting new file. */
1755 {
1756 f = get_file (filename, LC_RENAME);
1757 add_file_change (f->filename, f->inclusion_line);
1758 current_file = f;
1759 }
1760
1761 if (flag[2]) /* Ending current file. */
1762 {
1763 if (!current_file->up
1764 || filename_cmp (current_file->up->filename, filename) != 0)
1765 {
1766 gfc_warning_now ("%s:%d: file %s left but not entered",
1767 current_file->filename, current_file->line,
1768 filename);
1769 if (unescape)
1770 free (wide_filename);
1771 free (filename);
1772 return;
1773 }
1774
1775 add_file_change (NULL, line);
1776 current_file = current_file->up;
1777 linemap_add (line_table, LC_RENAME, false, current_file->filename,
1778 current_file->line);
1779 }
1780
1781 /* The name of the file can be a temporary file produced by
1782 cpp. Replace the name if it is different. */
1783
1784 if (filename_cmp (current_file->filename, filename) != 0)
1785 {
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
1788 point to the new name, but there is no API for that currently. */
1789 current_file->filename = xstrdup (filename);
1790 }
1791
1792 /* Set new line number. */
1793 current_file->line = line;
1794 if (unescape)
1795 free (wide_filename);
1796 free (filename);
1797 return;
1798
1799 bad_cpp_line:
1800 gfc_warning_now ("%s:%d: Illegal preprocessor directive",
1801 current_file->filename, current_file->line);
1802 current_file->line++;
1803 }
1804
1805
1806 static bool load_file (const char *, const char *, bool);
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
1814 static bool
1815 include_line (gfc_char_t *line)
1816 {
1817 gfc_char_t quote, *c, *begin, *stop;
1818 char *filename;
1819
1820 c = line;
1821
1822 if (gfc_option.gfc_flag_openmp || gfc_option.gfc_flag_openmp_simd)
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
1839 while (*c == ' ' || *c == '\t')
1840 c++;
1841
1842 if (gfc_wide_strncasecmp (c, "include", 7))
1843 return false;
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
1871 /* We have an include line at this point. */
1872
1873 *stop = '\0'; /* It's ok to trash the buffer, as this line won't be
1874 read by anything else. */
1875
1876 filename = gfc_widechar_to_char (begin, -1);
1877 if (!load_file (filename, NULL, false))
1878 exit (FATAL_EXIT_CODE);
1879
1880 free (filename);
1881 return true;
1882 }
1883
1884
1885 /* Load a file into memory by calling load_line until the file ends. */
1886
1887 static bool
1888 load_file (const char *realfilename, const char *displayedname, bool initial)
1889 {
1890 gfc_char_t *line;
1891 gfc_linebuf *b;
1892 gfc_file *f;
1893 FILE *input;
1894 int len, line_len;
1895 bool first_line;
1896 const char *filename;
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));
1902
1903 filename = displayedname ? displayedname : realfilename;
1904
1905 for (f = current_file; f; f = f->up)
1906 if (filename_cmp (filename, f->filename) == 0)
1907 {
1908 fprintf (stderr, "%s:%d: Error: File '%s' is being included "
1909 "recursively\n", current_file->filename, current_file->line,
1910 filename);
1911 return false;
1912 }
1913
1914 if (initial)
1915 {
1916 if (gfc_src_file)
1917 {
1918 input = gfc_src_file;
1919 gfc_src_file = NULL;
1920 }
1921 else
1922 input = gfc_open_file (realfilename);
1923 if (input == NULL)
1924 {
1925 gfc_error_now_2 ("Can't open file %qs", filename);
1926 return false;
1927 }
1928 }
1929 else
1930 {
1931 input = gfc_open_included_file (realfilename, false, false);
1932 if (input == NULL)
1933 {
1934 fprintf (stderr, "%s:%d: Error: Can't open included file '%s'\n",
1935 current_file->filename, current_file->line, filename);
1936 return false;
1937 }
1938 }
1939
1940 /* Load the file.
1941
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);
1958 if (!initial)
1959 add_file_change (f->filename, f->inclusion_line);
1960 current_file = f;
1961 current_file->line = 1;
1962 line = NULL;
1963 line_len = 0;
1964 first_line = true;
1965
1966 if (initial && gfc_src_preprocessor_lines[0])
1967 {
1968 preprocessor_line (gfc_src_preprocessor_lines[0]);
1969 free (gfc_src_preprocessor_lines[0]);
1970 gfc_src_preprocessor_lines[0] = NULL;
1971 if (gfc_src_preprocessor_lines[1])
1972 {
1973 preprocessor_line (gfc_src_preprocessor_lines[1]);
1974 free (gfc_src_preprocessor_lines[1]);
1975 gfc_src_preprocessor_lines[1] = NULL;
1976 }
1977 }
1978
1979 for (;;)
1980 {
1981 int trunc = load_line (input, &line, &line_len, NULL);
1982
1983 len = gfc_wide_strlen (line);
1984 if (feof (input) && len == 0)
1985 break;
1986
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
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')))
2000 {
2001 int n = line[1] == (unsigned char) '\xBB' ? 3 : 2;
2002 gfc_char_t *new_char = gfc_get_wide_string (line_len);
2003
2004 wide_strcpy (new_char, &line[n]);
2005 free (line);
2006 line = new_char;
2007 len -= n;
2008 }
2009
2010 /* There are three things this line can be: a line of Fortran
2011 source, an include line or a C preprocessor directive. */
2012
2013 if (line[0] == '#')
2014 {
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
2019 && (wide_strncmp (line, "#define ", 8) == 0
2020 || wide_strncmp (line, "#undef ", 7) == 0))
2021 ;
2022 else
2023 {
2024 preprocessor_line (line);
2025 continue;
2026 }
2027 }
2028
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
2034 if (include_line (line))
2035 {
2036 current_file->line++;
2037 continue;
2038 }
2039
2040 /* Add line. */
2041
2042 b = XCNEWVAR (gfc_linebuf, gfc_linebuf_header_size
2043 + (len + 1) * sizeof (gfc_char_t));
2044
2045 b->location
2046 = linemap_line_start (line_table, current_file->line++, 120);
2047 b->file = current_file;
2048 b->truncated = trunc;
2049 wide_strcpy (b->line, line);
2050
2051 if (line_head == NULL)
2052 line_head = b;
2053 else
2054 line_tail->next = b;
2055
2056 line_tail = b;
2057
2058 while (file_changes_cur < file_changes_count)
2059 file_changes[file_changes_cur++].lb = b;
2060 }
2061
2062 /* Release the line buffer allocated in load_line. */
2063 free (line);
2064
2065 fclose (input);
2066
2067 if (!initial)
2068 add_file_change (NULL, current_file->inclusion_line + 1);
2069 current_file = current_file->up;
2070 linemap_add (line_table, LC_LEAVE, 0, NULL, 0);
2071 return true;
2072 }
2073
2074
2075 /* Open a new file and start scanning from that file. Returns true
2076 if everything went OK, false otherwise. If form == FORM_UNKNOWN
2077 it tries to determine the source form from the filename, defaulting
2078 to free form. */
2079
2080 bool
2081 gfc_new_file (void)
2082 {
2083 bool result;
2084
2085 if (gfc_cpp_enabled ())
2086 {
2087 result = gfc_cpp_preprocess (gfc_source_file);
2088 if (!gfc_cpp_preprocess_only ())
2089 result = load_file (gfc_cpp_temporary_file (), gfc_source_file, true);
2090 }
2091 else
2092 result = load_file (gfc_source_file, NULL, true);
2093
2094 gfc_current_locus.lb = line_head;
2095 gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line;
2096
2097 #if 0 /* Debugging aid. */
2098 for (; line_head; line_head = line_head->next)
2099 printf ("%s:%3d %s\n", LOCATION_FILE (line_head->location),
2100 LOCATION_LINE (line_head->location), line_head->line);
2101
2102 exit (SUCCESS_EXIT_CODE);
2103 #endif
2104
2105 return result;
2106 }
2107
2108 static char *
2109 unescape_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
2129 if (!*p || p[1])
2130 return NULL;
2131
2132 /* Undo effects of cpp_quote_string. */
2133 s = ptr;
2134 d = XCNEWVEC (char, p + 1 - ptr - unescape);
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
2152 const char *
2153 gfc_read_orig_filename (const char *filename, const char **canon_source_file)
2154 {
2155 int c, len;
2156 char *dirname, *tmp;
2157
2158 gfc_src_file = gfc_open_file (filename);
2159 if (gfc_src_file == NULL)
2160 return NULL;
2161
2162 c = getc (gfc_src_file);
2163
2164 if (c != '#')
2165 return NULL;
2166
2167 len = 0;
2168 load_line (gfc_src_file, &gfc_src_preprocessor_lines[0], &len, &c);
2169
2170 if (wide_strncmp (gfc_src_preprocessor_lines[0], "# 1 \"", 5) != 0)
2171 return NULL;
2172
2173 tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[0][5], -1);
2174 filename = unescape_filename (tmp);
2175 free (tmp);
2176 if (filename == NULL)
2177 return NULL;
2178
2179 c = getc (gfc_src_file);
2180
2181 if (c != '#')
2182 return filename;
2183
2184 len = 0;
2185 load_line (gfc_src_file, &gfc_src_preprocessor_lines[1], &len, &c);
2186
2187 if (wide_strncmp (gfc_src_preprocessor_lines[1], "# 1 \"", 5) != 0)
2188 return filename;
2189
2190 tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[1][5], -1);
2191 dirname = unescape_filename (tmp);
2192 free (tmp);
2193 if (dirname == NULL)
2194 return filename;
2195
2196 len = strlen (dirname);
2197 if (len < 3 || dirname[len - 1] != '/' || dirname[len - 2] != '/')
2198 {
2199 free (dirname);
2200 return filename;
2201 }
2202 dirname[len - 2] = '\0';
2203 set_src_pwd (dirname);
2204
2205 if (! IS_ABSOLUTE_PATH (filename))
2206 {
2207 char *p = XCNEWVEC (char, len + strlen (filename));
2208
2209 memcpy (p, dirname, len - 2);
2210 p[len - 2] = '/';
2211 strcpy (p + len - 1, filename);
2212 *canon_source_file = p;
2213 }
2214
2215 free (dirname);
2216 return filename;
2217 }