OSDN Git Service

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