OSDN Git Service

PR debug/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 static void
303 change_file (gfc_file *to)
304 {
305   if (current_file == NULL)
306     return;
307
308   while (current_file != to)
309     if (current_file->down)
310       {
311         gfc_file *f = current_file->down;
312         /* Ensure we don't enter it ever again.  */
313         current_file->down = NULL;
314         current_file = f;
315         (*debug_hooks->start_source_file) (current_file->inclusion_line,
316                                            current_file->filename);
317       }
318     else if (current_file->sibling)
319       current_file = current_file->sibling;
320     else
321       {
322         gcc_assert (current_file->up);
323         (*debug_hooks->end_source_file) (current_file->inclusion_line + 1);
324         current_file = current_file->up;
325       }
326 }
327
328 void
329 gfc_start_source_files (void)
330 {
331   /* If the debugger wants the name of the main source file,
332      we give it.  */
333   if (debug_hooks->start_end_main_source_file)
334     (*debug_hooks->start_source_file) (0, gfc_source_file);
335
336   if (gfc_current_locus.lb && gfc_current_locus.lb->file)
337     {
338       current_file = gfc_current_locus.lb->file;
339       while (current_file->up)
340         current_file = current_file->up;
341       change_file (gfc_current_locus.lb->file);
342     }
343   else
344     current_file = NULL;
345 }
346
347 void
348 gfc_end_source_files (void)
349 {
350   if (current_file != NULL)
351     {
352       gfc_file *to = current_file;
353       while (to->up)
354         to = to->up;
355       change_file (to);
356     }
357
358   if (debug_hooks->start_end_main_source_file)
359     (*debug_hooks->end_source_file) (0);
360 }
361
362 /* Advance the current line pointer to the next line.  */
363
364 void
365 gfc_advance_line (void)
366 {
367   if (gfc_at_end ())
368     return;
369
370   if (gfc_current_locus.lb == NULL) 
371     {
372       end_flag = 1;
373       return;
374     } 
375
376   if (gfc_current_locus.lb->next
377       && gfc_current_locus.lb->next->file != gfc_current_locus.lb->file
378       && !gfc_current_locus.lb->next->dbg_emitted)
379     {
380       change_file (gfc_current_locus.lb->next->file);
381       gfc_current_locus.lb->next->dbg_emitted = true;
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->up = current_file;
1267   /* Already cleared by gfc_getmem.
1268      f->down = NULL;
1269      f->sibling = NULL;  */
1270   if (current_file != NULL)
1271     {
1272       f->inclusion_line = current_file->line;
1273       if (current_file->down == NULL)
1274         current_file->down = f;
1275       else
1276         {
1277           gfc_file *s;
1278
1279           for (s = current_file->down; s->sibling; s = s->sibling)
1280             ;
1281           s->sibling = f;
1282         }
1283     }
1284
1285 #ifdef USE_MAPPED_LOCATION
1286   linemap_add (line_table, reason, false, f->filename, 1);
1287 #endif
1288
1289   return f;
1290 }
1291
1292 /* Deal with a line from the C preprocessor. The
1293    initial octothorp has already been seen.  */
1294
1295 static void
1296 preprocessor_line (char *c)
1297 {
1298   bool flag[5];
1299   int i, line;
1300   char *filename;
1301   gfc_file *f;
1302   int escaped, unescape;
1303
1304   c++;
1305   while (*c == ' ' || *c == '\t')
1306     c++;
1307
1308   if (*c < '0' || *c > '9')
1309     goto bad_cpp_line;
1310
1311   line = atoi (c);
1312
1313   c = strchr (c, ' ');
1314   if (c == NULL)
1315     {
1316       /* No file name given.  Set new line number.  */
1317       current_file->line = line;
1318       return;
1319     }
1320
1321   /* Skip spaces.  */
1322   while (*c == ' ' || *c == '\t')
1323     c++;
1324
1325   /* Skip quote.  */
1326   if (*c != '"')
1327     goto bad_cpp_line;
1328   ++c;
1329
1330   filename = c;
1331
1332   /* Make filename end at quote.  */
1333   unescape = 0;
1334   escaped = false;
1335   while (*c && ! (!escaped && *c == '"'))
1336     {
1337       if (escaped)
1338         escaped = false;
1339       else if (*c == '\\')
1340         {
1341           escaped = true;
1342           unescape++;
1343         }
1344       ++c;
1345     }
1346
1347   if (! *c)
1348     /* Preprocessor line has no closing quote.  */
1349     goto bad_cpp_line;
1350
1351   *c++ = '\0';
1352
1353   /* Undo effects of cpp_quote_string.  */
1354   if (unescape)
1355     {
1356       char *s = filename;
1357       char *d = gfc_getmem (c - filename - unescape);
1358
1359       filename = d;
1360       while (*s)
1361         {
1362           if (*s == '\\')
1363             *d++ = *++s;
1364           else
1365             *d++ = *s;
1366           s++;
1367         }
1368       *d = '\0';
1369     }
1370
1371   /* Get flags.  */
1372
1373   flag[1] = flag[2] = flag[3] = flag[4] = false;
1374
1375   for (;;)
1376     {
1377       c = strchr (c, ' ');
1378       if (c == NULL)
1379         break;
1380
1381       c++;
1382       i = atoi (c);
1383
1384       if (1 <= i && i <= 4)
1385         flag[i] = true;
1386     }
1387
1388   /* Interpret flags.  */
1389
1390   if (flag[1]) /* Starting new file.  */
1391     {
1392       f = get_file (filename, LC_RENAME);
1393       current_file = f;
1394     }
1395
1396   if (flag[2]) /* Ending current file.  */
1397     {
1398       if (!current_file->up
1399           || strcmp (current_file->up->filename, filename) != 0)
1400         {
1401           gfc_warning_now ("%s:%d: file %s left but not entered",
1402                            current_file->filename, current_file->line,
1403                            filename);
1404           if (unescape)
1405             gfc_free (filename);
1406           return;
1407         }
1408
1409       current_file = current_file->up;
1410 #ifdef USE_MAPPED_LOCATION
1411       linemap_add (line_table, LC_RENAME, false, current_file->filename,
1412                    current_file->line);
1413 #endif
1414     }
1415
1416   /* The name of the file can be a temporary file produced by
1417      cpp. Replace the name if it is different.  */
1418
1419   if (strcmp (current_file->filename, filename) != 0)
1420     {
1421       gfc_free (current_file->filename);
1422       current_file->filename = gfc_getmem (strlen (filename) + 1);
1423       strcpy (current_file->filename, filename);
1424     }
1425
1426   /* Set new line number.  */
1427   current_file->line = line;
1428   if (unescape)
1429     gfc_free (filename);
1430   return;
1431
1432  bad_cpp_line:
1433   gfc_warning_now ("%s:%d: Illegal preprocessor directive",
1434                    current_file->filename, current_file->line);
1435   current_file->line++;
1436 }
1437
1438
1439 static try load_file (const char *, bool);
1440
1441 /* include_line()-- Checks a line buffer to see if it is an include
1442    line.  If so, we call load_file() recursively to load the included
1443    file.  We never return a syntax error because a statement like
1444    "include = 5" is perfectly legal.  We return false if no include was
1445    processed or true if we matched an include.  */
1446
1447 static bool
1448 include_line (char *line)
1449 {
1450   char quote, *c, *begin, *stop;
1451
1452   c = line;
1453
1454   if (gfc_option.flag_openmp)
1455     {
1456       if (gfc_current_form == FORM_FREE)
1457         {
1458           while (*c == ' ' || *c == '\t')
1459             c++;
1460           if (*c == '!' && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
1461             c += 3;
1462         }
1463       else
1464         {
1465           if ((*c == '!' || *c == 'c' || *c == 'C' || *c == '*')
1466               && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
1467             c += 3;
1468         }
1469     }
1470
1471   while (*c == ' ' || *c == '\t')
1472     c++;
1473
1474   if (strncasecmp (c, "include", 7))
1475       return false;
1476
1477   c += 7;
1478   while (*c == ' ' || *c == '\t')
1479     c++;
1480
1481   /* Find filename between quotes.  */
1482   
1483   quote = *c++;
1484   if (quote != '"' && quote != '\'')
1485     return false;
1486
1487   begin = c;
1488
1489   while (*c != quote && *c != '\0')
1490     c++;
1491
1492   if (*c == '\0')
1493     return false;
1494
1495   stop = c++;
1496   
1497   while (*c == ' ' || *c == '\t')
1498     c++;
1499
1500   if (*c != '\0' && *c != '!')
1501     return false;
1502
1503   /* We have an include line at this point.  */
1504
1505   *stop = '\0'; /* It's ok to trash the buffer, as this line won't be
1506                    read by anything else.  */
1507
1508   load_file (begin, false);
1509   return true;
1510 }
1511
1512
1513 /* Load a file into memory by calling load_line until the file ends.  */
1514
1515 static try
1516 load_file (const char *filename, bool initial)
1517 {
1518   char *line;
1519   gfc_linebuf *b;
1520   gfc_file *f;
1521   FILE *input;
1522   int len, line_len;
1523   bool first_line;
1524
1525   for (f = current_file; f; f = f->up)
1526     if (strcmp (filename, f->filename) == 0)
1527       {
1528         gfc_error_now ("File '%s' is being included recursively", filename);
1529         return FAILURE;
1530       }
1531
1532   if (initial)
1533     {
1534       if (gfc_src_file)
1535         {
1536           input = gfc_src_file;
1537           gfc_src_file = NULL;
1538         }
1539       else
1540         input = gfc_open_file (filename);
1541       if (input == NULL)
1542         {
1543           gfc_error_now ("Can't open file '%s'", filename);
1544           return FAILURE;
1545         }
1546     }
1547   else
1548     {
1549       input = gfc_open_included_file (filename, false, false);
1550       if (input == NULL)
1551         {
1552           gfc_error_now ("Can't open included file '%s'", filename);
1553           return FAILURE;
1554         }
1555     }
1556
1557   /* Load the file.  */
1558
1559   f = get_file (filename, initial ? LC_RENAME : LC_ENTER);
1560   current_file = f;
1561   current_file->line = 1;
1562   line = NULL;
1563   line_len = 0;
1564   first_line = true;
1565
1566   if (initial && gfc_src_preprocessor_lines[0])
1567     {
1568       preprocessor_line (gfc_src_preprocessor_lines[0]);
1569       gfc_free (gfc_src_preprocessor_lines[0]);
1570       gfc_src_preprocessor_lines[0] = NULL;
1571       if (gfc_src_preprocessor_lines[1])
1572         {
1573           preprocessor_line (gfc_src_preprocessor_lines[1]);
1574           gfc_free (gfc_src_preprocessor_lines[1]);
1575           gfc_src_preprocessor_lines[1] = NULL;
1576         }
1577     }
1578
1579   for (;;)
1580     {
1581       int trunc = load_line (input, &line, &line_len);
1582
1583       len = strlen (line);
1584       if (feof (input) && len == 0)
1585         break;
1586
1587       /* If this is the first line of the file, it can contain a byte
1588          order mark (BOM), which we will ignore:
1589            FF FE is UTF-16 little endian,
1590            FE FF is UTF-16 big endian,
1591            EF BB BF is UTF-8.  */
1592       if (first_line
1593           && ((line_len >= 2 && line[0] == '\xFF' && line[1] == '\xFE')
1594               || (line_len >= 2 && line[0] == '\xFE' && line[1] == '\xFF')
1595               || (line_len >= 3 && line[0] == '\xEF' && line[1] == '\xBB'
1596                   && line[2] == '\xBF')))
1597         {
1598           int n = line[1] == '\xBB' ? 3 : 2;
1599           char * new = gfc_getmem (line_len);
1600
1601           strcpy (new, line + n);
1602           gfc_free (line);
1603           line = new;
1604           len -= n;
1605         }
1606
1607       /* There are three things this line can be: a line of Fortran
1608          source, an include line or a C preprocessor directive.  */
1609
1610       if (line[0] == '#')
1611         {
1612           /* When -g3 is specified, it's possible that we emit #define
1613              and #undef lines, which we need to pass to the middle-end
1614              so that it can emit correct debug info.  */
1615           if (debug_info_level == DINFO_LEVEL_VERBOSE
1616               && (strncmp (line, "#define ", 8) == 0
1617                   || strncmp (line, "#undef ", 7) == 0))
1618             ;
1619           else
1620             {
1621               preprocessor_line (line);
1622               continue;
1623             }
1624         }
1625
1626       /* Preprocessed files have preprocessor lines added before the byte
1627          order mark, so first_line is not about the first line of the file
1628          but the first line that's not a preprocessor line.  */
1629       first_line = false;
1630
1631       if (include_line (line))
1632         {
1633           current_file->line++;
1634           continue;
1635         }
1636
1637       /* Add line.  */
1638
1639       b = gfc_getmem (gfc_linebuf_header_size + len + 1);
1640
1641 #ifdef USE_MAPPED_LOCATION
1642       b->location
1643         = linemap_line_start (line_table, current_file->line++, 120);
1644 #else
1645       b->linenum = current_file->line++;
1646 #endif
1647       b->file = current_file;
1648       b->truncated = trunc;
1649       strcpy (b->line, line);
1650
1651       if (line_head == NULL)
1652         line_head = b;
1653       else
1654         line_tail->next = b;
1655
1656       line_tail = b;
1657     }
1658
1659   /* Release the line buffer allocated in load_line.  */
1660   gfc_free (line);
1661
1662   fclose (input);
1663
1664   current_file = current_file->up;
1665 #ifdef USE_MAPPED_LOCATION
1666   linemap_add (line_table, LC_LEAVE, 0, NULL, 0);
1667 #endif
1668   return SUCCESS;
1669 }
1670
1671
1672 /* Open a new file and start scanning from that file. Returns SUCCESS
1673    if everything went OK, FAILURE otherwise.  If form == FORM_UKNOWN
1674    it tries to determine the source form from the filename, defaulting
1675    to free form.  */
1676
1677 try
1678 gfc_new_file (void)
1679 {
1680   try result;
1681
1682   result = load_file (gfc_source_file, true);
1683
1684   gfc_current_locus.lb = line_head;
1685   gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line;
1686
1687 #if 0 /* Debugging aid.  */
1688   for (; line_head; line_head = line_head->next)
1689     gfc_status ("%s:%3d %s\n",
1690 #ifdef USE_MAPPED_LOCATION
1691                 LOCATION_FILE (line_head->location),
1692                 LOCATION_LINE (line_head->location),
1693 #else
1694                 line_head->file->filename, 
1695                 line_head->linenum,
1696 #endif
1697                 line_head->line);
1698
1699   exit (0);
1700 #endif
1701
1702   return result;
1703 }
1704
1705 static char *
1706 unescape_filename (const char *ptr)
1707 {
1708   const char *p = ptr, *s;
1709   char *d, *ret;
1710   int escaped, unescape = 0;
1711
1712   /* Make filename end at quote.  */
1713   escaped = false;
1714   while (*p && ! (! escaped && *p == '"'))
1715     {
1716       if (escaped)
1717         escaped = false;
1718       else if (*p == '\\')
1719         {
1720           escaped = true;
1721           unescape++;
1722         }
1723       ++p;
1724     }
1725
1726   if (!*p || p[1])
1727     return NULL;
1728
1729   /* Undo effects of cpp_quote_string.  */
1730   s = ptr;
1731   d = gfc_getmem (p + 1 - ptr - unescape);
1732   ret = d;
1733
1734   while (s != p)
1735     {
1736       if (*s == '\\')
1737         *d++ = *++s;
1738       else
1739         *d++ = *s;
1740       s++;
1741     }
1742   *d = '\0';
1743   return ret;
1744 }
1745
1746 /* For preprocessed files, if the first tokens are of the form # NUM.
1747    handle the directives so we know the original file name.  */
1748
1749 const char *
1750 gfc_read_orig_filename (const char *filename, const char **canon_source_file)
1751 {
1752   int c, len;
1753   char *dirname;
1754
1755   gfc_src_file = gfc_open_file (filename);
1756   if (gfc_src_file == NULL)
1757     return NULL;
1758
1759   c = getc (gfc_src_file);
1760   ungetc (c, gfc_src_file);
1761
1762   if (c != '#')
1763     return NULL;
1764
1765   len = 0;
1766   load_line (gfc_src_file, &gfc_src_preprocessor_lines[0], &len);
1767
1768   if (strncmp (gfc_src_preprocessor_lines[0], "# 1 \"", 5) != 0)
1769     return NULL;
1770
1771   filename = unescape_filename (gfc_src_preprocessor_lines[0] + 5);
1772   if (filename == NULL)
1773     return NULL;
1774
1775   c = getc (gfc_src_file);
1776   ungetc (c, gfc_src_file);
1777
1778   if (c != '#')
1779     return filename;
1780
1781   len = 0;
1782   load_line (gfc_src_file, &gfc_src_preprocessor_lines[1], &len);
1783
1784   if (strncmp (gfc_src_preprocessor_lines[1], "# 1 \"", 5) != 0)
1785     return filename;
1786
1787   dirname = unescape_filename (gfc_src_preprocessor_lines[1] + 5);
1788   if (dirname == NULL)
1789     return filename;
1790
1791   len = strlen (dirname);
1792   if (len < 3 || dirname[len - 1] != '/' || dirname[len - 2] != '/')
1793     {
1794       gfc_free (dirname);
1795       return filename;
1796     }
1797   dirname[len - 2] = '\0';
1798   set_src_pwd (dirname);
1799
1800   if (! IS_ABSOLUTE_PATH (filename))
1801     {
1802       char *p = gfc_getmem (len + strlen (filename));
1803
1804       memcpy (p, dirname, len - 2);
1805       p[len - 2] = '/';
1806       strcpy (p + len - 1, filename);
1807       *canon_source_file = p;
1808     }
1809
1810   gfc_free (dirname);
1811   return filename;
1812 }