OSDN Git Service

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