OSDN Git Service

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