Home | History | Annotate | Line # | Download | only in fortran
      1  1.1  mrg /* Character scanner.
      2  1.1  mrg    Copyright (C) 2000-2022 Free Software Foundation, Inc.
      3  1.1  mrg    Contributed by Andy Vaught
      4  1.1  mrg 
      5  1.1  mrg This file is part of GCC.
      6  1.1  mrg 
      7  1.1  mrg GCC is free software; you can redistribute it and/or modify it under
      8  1.1  mrg the terms of the GNU General Public License as published by the Free
      9  1.1  mrg Software Foundation; either version 3, or (at your option) any later
     10  1.1  mrg version.
     11  1.1  mrg 
     12  1.1  mrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY
     13  1.1  mrg WARRANTY; without even the implied warranty of MERCHANTABILITY or
     14  1.1  mrg FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     15  1.1  mrg for more details.
     16  1.1  mrg 
     17  1.1  mrg You should have received a copy of the GNU General Public License
     18  1.1  mrg along with GCC; see the file COPYING3.  If not see
     19  1.1  mrg <http://www.gnu.org/licenses/>.  */
     20  1.1  mrg 
     21  1.1  mrg /* Set of subroutines to (ultimately) return the next character to the
     22  1.1  mrg    various matching subroutines.  This file's job is to read files and
     23  1.1  mrg    build up lines that are parsed by the parser.  This means that we
     24  1.1  mrg    handle continuation lines and "include" lines.
     25  1.1  mrg 
     26  1.1  mrg    The first thing the scanner does is to load an entire file into
     27  1.1  mrg    memory.  We load the entire file into memory for a couple reasons.
     28  1.1  mrg    The first is that we want to be able to deal with nonseekable input
     29  1.1  mrg    (pipes, stdin) and there is a lot of backing up involved during
     30  1.1  mrg    parsing.
     31  1.1  mrg 
     32  1.1  mrg    The second is that we want to be able to print the locus of errors,
     33  1.1  mrg    and an error on line 999999 could conflict with something on line
     34  1.1  mrg    one.  Given nonseekable input, we've got to store the whole thing.
     35  1.1  mrg 
     36  1.1  mrg    One thing that helps are the column truncation limits that give us
     37  1.1  mrg    an upper bound on the size of individual lines.  We don't store the
     38  1.1  mrg    truncated stuff.
     39  1.1  mrg 
     40  1.1  mrg    From the scanner's viewpoint, the higher level subroutines ask for
     41  1.1  mrg    new characters and do a lot of jumping backwards.  */
     42  1.1  mrg 
     43  1.1  mrg #include "config.h"
     44  1.1  mrg #include "system.h"
     45  1.1  mrg #include "coretypes.h"
     46  1.1  mrg #include "gfortran.h"
     47  1.1  mrg #include "toplev.h"	/* For set_src_pwd.  */
     48  1.1  mrg #include "debug.h"
     49  1.1  mrg #include "options.h"
     50  1.1  mrg #include "diagnostic-core.h"  /* For fatal_error. */
     51  1.1  mrg #include "cpp.h"
     52  1.1  mrg #include "scanner.h"
     53  1.1  mrg 
     54  1.1  mrg /* List of include file search directories.  */
     55  1.1  mrg gfc_directorylist *include_dirs, *intrinsic_modules_dirs;
     56  1.1  mrg 
     57  1.1  mrg static gfc_file *file_head, *current_file;
     58  1.1  mrg 
     59  1.1  mrg static int continue_flag, end_flag, gcc_attribute_flag;
     60  1.1  mrg /* If !$omp/!$acc occurred in current comment line.  */
     61  1.1  mrg static int openmp_flag, openacc_flag;
     62  1.1  mrg static int continue_count, continue_line;
     63  1.1  mrg static locus openmp_locus;
     64  1.1  mrg static locus openacc_locus;
     65  1.1  mrg static locus gcc_attribute_locus;
     66  1.1  mrg 
     67  1.1  mrg gfc_source_form gfc_current_form;
     68  1.1  mrg static gfc_linebuf *line_head, *line_tail;
     69  1.1  mrg 
     70  1.1  mrg locus gfc_current_locus;
     71  1.1  mrg const char *gfc_source_file;
     72  1.1  mrg static FILE *gfc_src_file;
     73  1.1  mrg static gfc_char_t *gfc_src_preprocessor_lines[2];
     74  1.1  mrg 
     75  1.1  mrg static struct gfc_file_change
     76  1.1  mrg {
     77  1.1  mrg   const char *filename;
     78  1.1  mrg   gfc_linebuf *lb;
     79  1.1  mrg   int line;
     80  1.1  mrg } *file_changes;
     81  1.1  mrg static size_t file_changes_cur, file_changes_count;
     82  1.1  mrg static size_t file_changes_allocated;
     83  1.1  mrg 
     84  1.1  mrg static gfc_char_t *last_error_char;
     85  1.1  mrg 
     86  1.1  mrg /* Functions dealing with our wide characters (gfc_char_t) and
     87  1.1  mrg    sequences of such characters.  */
     88  1.1  mrg 
     89  1.1  mrg int
     90  1.1  mrg gfc_wide_fits_in_byte (gfc_char_t c)
     91  1.1  mrg {
     92  1.1  mrg   return (c <= UCHAR_MAX);
     93  1.1  mrg }
     94  1.1  mrg 
     95  1.1  mrg static inline int
     96  1.1  mrg wide_is_ascii (gfc_char_t c)
     97  1.1  mrg {
     98  1.1  mrg   return (gfc_wide_fits_in_byte (c) && ((unsigned char) c & ~0x7f) == 0);
     99  1.1  mrg }
    100  1.1  mrg 
    101  1.1  mrg int
    102  1.1  mrg gfc_wide_is_printable (gfc_char_t c)
    103  1.1  mrg {
    104  1.1  mrg   return (gfc_wide_fits_in_byte (c) && ISPRINT ((unsigned char) c));
    105  1.1  mrg }
    106  1.1  mrg 
    107  1.1  mrg gfc_char_t
    108  1.1  mrg gfc_wide_tolower (gfc_char_t c)
    109  1.1  mrg {
    110  1.1  mrg   return (wide_is_ascii (c) ? (gfc_char_t) TOLOWER((unsigned char) c) : c);
    111  1.1  mrg }
    112  1.1  mrg 
    113  1.1  mrg gfc_char_t
    114  1.1  mrg gfc_wide_toupper (gfc_char_t c)
    115  1.1  mrg {
    116  1.1  mrg   return (wide_is_ascii (c) ? (gfc_char_t) TOUPPER((unsigned char) c) : c);
    117  1.1  mrg }
    118  1.1  mrg 
    119  1.1  mrg int
    120  1.1  mrg gfc_wide_is_digit (gfc_char_t c)
    121  1.1  mrg {
    122  1.1  mrg   return (c >= '0' && c <= '9');
    123  1.1  mrg }
    124  1.1  mrg 
    125  1.1  mrg static inline int
    126  1.1  mrg wide_atoi (gfc_char_t *c)
    127  1.1  mrg {
    128  1.1  mrg #define MAX_DIGITS 20
    129  1.1  mrg   char buf[MAX_DIGITS+1];
    130  1.1  mrg   int i = 0;
    131  1.1  mrg 
    132  1.1  mrg   while (gfc_wide_is_digit(*c) && i < MAX_DIGITS)
    133  1.1  mrg     buf[i++] = *c++;
    134  1.1  mrg   buf[i] = '\0';
    135  1.1  mrg   return atoi (buf);
    136  1.1  mrg }
    137  1.1  mrg 
    138  1.1  mrg size_t
    139  1.1  mrg gfc_wide_strlen (const gfc_char_t *str)
    140  1.1  mrg {
    141  1.1  mrg   size_t i;
    142  1.1  mrg 
    143  1.1  mrg   for (i = 0; str[i]; i++)
    144  1.1  mrg     ;
    145  1.1  mrg 
    146  1.1  mrg   return i;
    147  1.1  mrg }
    148  1.1  mrg 
    149  1.1  mrg gfc_char_t *
    150  1.1  mrg gfc_wide_memset (gfc_char_t *b, gfc_char_t c, size_t len)
    151  1.1  mrg {
    152  1.1  mrg   size_t i;
    153  1.1  mrg 
    154  1.1  mrg   for (i = 0; i < len; i++)
    155  1.1  mrg     b[i] = c;
    156  1.1  mrg 
    157  1.1  mrg   return b;
    158  1.1  mrg }
    159  1.1  mrg 
    160  1.1  mrg static gfc_char_t *
    161  1.1  mrg wide_strcpy (gfc_char_t *dest, const gfc_char_t *src)
    162  1.1  mrg {
    163  1.1  mrg   gfc_char_t *d;
    164  1.1  mrg 
    165  1.1  mrg   for (d = dest; (*d = *src) != '\0'; ++src, ++d)
    166  1.1  mrg     ;
    167  1.1  mrg 
    168  1.1  mrg   return dest;
    169  1.1  mrg }
    170  1.1  mrg 
    171  1.1  mrg static gfc_char_t *
    172  1.1  mrg wide_strchr (const gfc_char_t *s, gfc_char_t c)
    173  1.1  mrg {
    174  1.1  mrg   do {
    175  1.1  mrg     if (*s == c)
    176  1.1  mrg       {
    177  1.1  mrg         return CONST_CAST(gfc_char_t *, s);
    178  1.1  mrg       }
    179  1.1  mrg   } while (*s++);
    180  1.1  mrg   return 0;
    181  1.1  mrg }
    182  1.1  mrg 
    183  1.1  mrg char *
    184  1.1  mrg gfc_widechar_to_char (const gfc_char_t *s, int length)
    185  1.1  mrg {
    186  1.1  mrg   size_t len, i;
    187  1.1  mrg   char *res;
    188  1.1  mrg 
    189  1.1  mrg   if (s == NULL)
    190  1.1  mrg     return NULL;
    191  1.1  mrg 
    192  1.1  mrg   /* Passing a negative length is used to indicate that length should be
    193  1.1  mrg      calculated using gfc_wide_strlen().  */
    194  1.1  mrg   len = (length >= 0 ? (size_t) length : gfc_wide_strlen (s));
    195  1.1  mrg   res = XNEWVEC (char, len + 1);
    196  1.1  mrg 
    197  1.1  mrg   for (i = 0; i < len; i++)
    198  1.1  mrg     {
    199  1.1  mrg       gcc_assert (gfc_wide_fits_in_byte (s[i]));
    200  1.1  mrg       res[i] = (unsigned char) s[i];
    201  1.1  mrg     }
    202  1.1  mrg 
    203  1.1  mrg   res[len] = '\0';
    204  1.1  mrg   return res;
    205  1.1  mrg }
    206  1.1  mrg 
    207  1.1  mrg gfc_char_t *
    208  1.1  mrg gfc_char_to_widechar (const char *s)
    209  1.1  mrg {
    210  1.1  mrg   size_t len, i;
    211  1.1  mrg   gfc_char_t *res;
    212  1.1  mrg 
    213  1.1  mrg   if (s == NULL)
    214  1.1  mrg     return NULL;
    215  1.1  mrg 
    216  1.1  mrg   len = strlen (s);
    217  1.1  mrg   res = gfc_get_wide_string (len + 1);
    218  1.1  mrg 
    219  1.1  mrg   for (i = 0; i < len; i++)
    220  1.1  mrg     res[i] = (unsigned char) s[i];
    221  1.1  mrg 
    222  1.1  mrg   res[len] = '\0';
    223  1.1  mrg   return res;
    224  1.1  mrg }
    225  1.1  mrg 
    226  1.1  mrg static int
    227  1.1  mrg wide_strncmp (const gfc_char_t *s1, const char *s2, size_t n)
    228  1.1  mrg {
    229  1.1  mrg   gfc_char_t c1, c2;
    230  1.1  mrg 
    231  1.1  mrg   while (n-- > 0)
    232  1.1  mrg     {
    233  1.1  mrg       c1 = *s1++;
    234  1.1  mrg       c2 = *s2++;
    235  1.1  mrg       if (c1 != c2)
    236  1.1  mrg 	return (c1 > c2 ? 1 : -1);
    237  1.1  mrg       if (c1 == '\0')
    238  1.1  mrg 	return 0;
    239  1.1  mrg     }
    240  1.1  mrg   return 0;
    241  1.1  mrg }
    242  1.1  mrg 
    243  1.1  mrg int
    244  1.1  mrg gfc_wide_strncasecmp (const gfc_char_t *s1, const char *s2, size_t n)
    245  1.1  mrg {
    246  1.1  mrg   gfc_char_t c1, c2;
    247  1.1  mrg 
    248  1.1  mrg   while (n-- > 0)
    249  1.1  mrg     {
    250  1.1  mrg       c1 = gfc_wide_tolower (*s1++);
    251  1.1  mrg       c2 = TOLOWER (*s2++);
    252  1.1  mrg       if (c1 != c2)
    253  1.1  mrg 	return (c1 > c2 ? 1 : -1);
    254  1.1  mrg       if (c1 == '\0')
    255  1.1  mrg 	return 0;
    256  1.1  mrg     }
    257  1.1  mrg   return 0;
    258  1.1  mrg }
    259  1.1  mrg 
    260  1.1  mrg 
    261  1.1  mrg /* Main scanner initialization.  */
    262  1.1  mrg 
    263  1.1  mrg void
    264  1.1  mrg gfc_scanner_init_1 (void)
    265  1.1  mrg {
    266  1.1  mrg   file_head = NULL;
    267  1.1  mrg   line_head = NULL;
    268  1.1  mrg   line_tail = NULL;
    269  1.1  mrg 
    270  1.1  mrg   continue_count = 0;
    271  1.1  mrg   continue_line = 0;
    272  1.1  mrg 
    273  1.1  mrg   end_flag = 0;
    274  1.1  mrg   last_error_char = NULL;
    275  1.1  mrg }
    276  1.1  mrg 
    277  1.1  mrg 
    278  1.1  mrg /* Main scanner destructor.  */
    279  1.1  mrg 
    280  1.1  mrg void
    281  1.1  mrg gfc_scanner_done_1 (void)
    282  1.1  mrg {
    283  1.1  mrg   gfc_linebuf *lb;
    284  1.1  mrg   gfc_file *f;
    285  1.1  mrg 
    286  1.1  mrg   while(line_head != NULL)
    287  1.1  mrg     {
    288  1.1  mrg       lb = line_head->next;
    289  1.1  mrg       free (line_head);
    290  1.1  mrg       line_head = lb;
    291  1.1  mrg     }
    292  1.1  mrg 
    293  1.1  mrg   while(file_head != NULL)
    294  1.1  mrg     {
    295  1.1  mrg       f = file_head->next;
    296  1.1  mrg       free (file_head->filename);
    297  1.1  mrg       free (file_head);
    298  1.1  mrg       file_head = f;
    299  1.1  mrg     }
    300  1.1  mrg }
    301  1.1  mrg 
    302  1.1  mrg static bool
    303  1.1  mrg gfc_do_check_include_dir (const char *path, bool warn)
    304  1.1  mrg {
    305  1.1  mrg   struct stat st;
    306  1.1  mrg   if (stat (path, &st))
    307  1.1  mrg     {
    308  1.1  mrg       if (errno != ENOENT)
    309  1.1  mrg 	gfc_warning_now (0, "Include directory %qs: %s",
    310  1.1  mrg 			 path, xstrerror(errno));
    311  1.1  mrg       else if (warn)
    312  1.1  mrg 	  gfc_warning_now (OPT_Wmissing_include_dirs,
    313  1.1  mrg 			   "Nonexistent include directory %qs", path);
    314  1.1  mrg       return false;
    315  1.1  mrg     }
    316  1.1  mrg   else if (!S_ISDIR (st.st_mode))
    317  1.1  mrg     {
    318  1.1  mrg       gfc_fatal_error ("%qs is not a directory", path);
    319  1.1  mrg       return false;
    320  1.1  mrg     }
    321  1.1  mrg   return true;
    322  1.1  mrg }
    323  1.1  mrg 
    324  1.1  mrg /* In order that -W(no-)missing-include-dirs works, the diagnostic can only be
    325  1.1  mrg    run after processing the commandline.  */
    326  1.1  mrg static void
    327  1.1  mrg gfc_do_check_include_dirs (gfc_directorylist **list, bool do_warn)
    328  1.1  mrg {
    329  1.1  mrg   gfc_directorylist *prev, *q, *n;
    330  1.1  mrg   prev = NULL;
    331  1.1  mrg   n = *list;
    332  1.1  mrg   while (n)
    333  1.1  mrg     {
    334  1.1  mrg       q = n; n = n->next;
    335  1.1  mrg       if (gfc_do_check_include_dir (q->path, q->warn && do_warn))
    336  1.1  mrg 	{
    337  1.1  mrg 	  prev = q;
    338  1.1  mrg 	  continue;
    339  1.1  mrg 	}
    340  1.1  mrg       if (prev == NULL)
    341  1.1  mrg 	*list = n;
    342  1.1  mrg       else
    343  1.1  mrg 	prev->next = n;
    344  1.1  mrg       free (q->path);
    345  1.1  mrg       free (q);
    346  1.1  mrg     }
    347  1.1  mrg }
    348  1.1  mrg 
    349  1.1  mrg void
    350  1.1  mrg gfc_check_include_dirs (bool verbose_missing_dir_warn)
    351  1.1  mrg {
    352  1.1  mrg   /* This is a bit convoluted: If gfc_cpp_enabled () and
    353  1.1  mrg      verbose_missing_dir_warn, the warning is shown by libcpp. Otherwise,
    354  1.1  mrg      it is shown here, still conditional on OPT_Wmissing_include_dirs.  */
    355  1.1  mrg   bool warn = !gfc_cpp_enabled () || !verbose_missing_dir_warn;
    356  1.1  mrg   gfc_do_check_include_dirs (&include_dirs, warn);
    357  1.1  mrg   gfc_do_check_include_dirs (&intrinsic_modules_dirs, verbose_missing_dir_warn);
    358  1.1  mrg   if (gfc_option.module_dir && gfc_cpp_enabled ())
    359  1.1  mrg     gfc_do_check_include_dirs (&include_dirs, true);
    360  1.1  mrg }
    361  1.1  mrg 
    362  1.1  mrg /* Adds path to the list pointed to by list.  */
    363  1.1  mrg 
    364  1.1  mrg static void
    365  1.1  mrg add_path_to_list (gfc_directorylist **list, const char *path,
    366  1.1  mrg 		  bool use_for_modules, bool head, bool warn, bool defer_warn)
    367  1.1  mrg {
    368  1.1  mrg   gfc_directorylist *dir;
    369  1.1  mrg   const char *p;
    370  1.1  mrg   char *q;
    371  1.1  mrg   size_t len;
    372  1.1  mrg   int i;
    373  1.1  mrg 
    374  1.1  mrg   p = path;
    375  1.1  mrg   while (*p == ' ' || *p == '\t')  /* someone might do "-I include" */
    376  1.1  mrg     if (*p++ == '\0')
    377  1.1  mrg       return;
    378  1.1  mrg 
    379  1.1  mrg   /* Strip trailing directory separators from the path, as this
    380  1.1  mrg      will confuse Windows systems.  */
    381  1.1  mrg   len = strlen (p);
    382  1.1  mrg   q = (char *) alloca (len + 1);
    383  1.1  mrg   memcpy (q, p, len + 1);
    384  1.1  mrg   i = len - 1;
    385  1.1  mrg   while (i >=0 && IS_DIR_SEPARATOR (q[i]))
    386  1.1  mrg     q[i--] = '\0';
    387  1.1  mrg 
    388  1.1  mrg   if (!defer_warn && !gfc_do_check_include_dir (q, warn))
    389  1.1  mrg     return;
    390  1.1  mrg 
    391  1.1  mrg   if (head || *list == NULL)
    392  1.1  mrg     {
    393  1.1  mrg       dir = XCNEW (gfc_directorylist);
    394  1.1  mrg       if (!head)
    395  1.1  mrg         *list = dir;
    396  1.1  mrg     }
    397  1.1  mrg   else
    398  1.1  mrg     {
    399  1.1  mrg       dir = *list;
    400  1.1  mrg       while (dir->next)
    401  1.1  mrg 	dir = dir->next;
    402  1.1  mrg 
    403  1.1  mrg       dir->next = XCNEW (gfc_directorylist);
    404  1.1  mrg       dir = dir->next;
    405  1.1  mrg     }
    406  1.1  mrg 
    407  1.1  mrg   dir->next = head ? *list : NULL;
    408  1.1  mrg   if (head)
    409  1.1  mrg     *list = dir;
    410  1.1  mrg   dir->use_for_modules = use_for_modules;
    411  1.1  mrg   dir->warn = warn;
    412  1.1  mrg   dir->path = xstrdup (p);
    413  1.1  mrg }
    414  1.1  mrg 
    415  1.1  mrg /* defer_warn is set to true while parsing the commandline.  */
    416  1.1  mrg 
    417  1.1  mrg void
    418  1.1  mrg gfc_add_include_path (const char *path, bool use_for_modules, bool file_dir,
    419  1.1  mrg 		      bool warn, bool defer_warn)
    420  1.1  mrg {
    421  1.1  mrg   add_path_to_list (&include_dirs, path, use_for_modules, file_dir, warn,
    422  1.1  mrg 		    defer_warn);
    423  1.1  mrg 
    424  1.1  mrg   /* For '#include "..."' these directories are automatically searched.  */
    425  1.1  mrg   if (!file_dir)
    426  1.1  mrg     gfc_cpp_add_include_path (xstrdup(path), true);
    427  1.1  mrg }
    428  1.1  mrg 
    429  1.1  mrg 
    430  1.1  mrg void
    431  1.1  mrg gfc_add_intrinsic_modules_path (const char *path)
    432  1.1  mrg {
    433  1.1  mrg   add_path_to_list (&intrinsic_modules_dirs, path, true, false, false, false);
    434  1.1  mrg }
    435  1.1  mrg 
    436  1.1  mrg 
    437  1.1  mrg /* Release resources allocated for options.  */
    438  1.1  mrg 
    439  1.1  mrg void
    440  1.1  mrg gfc_release_include_path (void)
    441  1.1  mrg {
    442  1.1  mrg   gfc_directorylist *p;
    443  1.1  mrg 
    444  1.1  mrg   while (include_dirs != NULL)
    445  1.1  mrg     {
    446  1.1  mrg       p = include_dirs;
    447  1.1  mrg       include_dirs = include_dirs->next;
    448  1.1  mrg       free (p->path);
    449  1.1  mrg       free (p);
    450  1.1  mrg     }
    451  1.1  mrg 
    452  1.1  mrg   while (intrinsic_modules_dirs != NULL)
    453  1.1  mrg     {
    454  1.1  mrg       p = intrinsic_modules_dirs;
    455  1.1  mrg       intrinsic_modules_dirs = intrinsic_modules_dirs->next;
    456  1.1  mrg       free (p->path);
    457  1.1  mrg       free (p);
    458  1.1  mrg     }
    459  1.1  mrg 
    460  1.1  mrg   free (gfc_option.module_dir);
    461  1.1  mrg }
    462  1.1  mrg 
    463  1.1  mrg 
    464  1.1  mrg static FILE *
    465  1.1  mrg open_included_file (const char *name, gfc_directorylist *list,
    466  1.1  mrg 		    bool module, bool system)
    467  1.1  mrg {
    468  1.1  mrg   char *fullname;
    469  1.1  mrg   gfc_directorylist *p;
    470  1.1  mrg   FILE *f;
    471  1.1  mrg 
    472  1.1  mrg   for (p = list; p; p = p->next)
    473  1.1  mrg     {
    474  1.1  mrg       if (module && !p->use_for_modules)
    475  1.1  mrg 	continue;
    476  1.1  mrg 
    477  1.1  mrg       fullname = (char *) alloca(strlen (p->path) + strlen (name) + 2);
    478  1.1  mrg       strcpy (fullname, p->path);
    479  1.1  mrg       strcat (fullname, "/");
    480  1.1  mrg       strcat (fullname, name);
    481  1.1  mrg 
    482  1.1  mrg       f = gfc_open_file (fullname);
    483  1.1  mrg       if (f != NULL)
    484  1.1  mrg 	{
    485  1.1  mrg 	  if (gfc_cpp_makedep ())
    486  1.1  mrg 	    gfc_cpp_add_dep (fullname, system);
    487  1.1  mrg 
    488  1.1  mrg 	  return f;
    489  1.1  mrg 	}
    490  1.1  mrg     }
    491  1.1  mrg 
    492  1.1  mrg   return NULL;
    493  1.1  mrg }
    494  1.1  mrg 
    495  1.1  mrg 
    496  1.1  mrg /* Opens file for reading, searching through the include directories
    497  1.1  mrg    given if necessary.  If the include_cwd argument is true, we try
    498  1.1  mrg    to open the file in the current directory first.  */
    499  1.1  mrg 
    500  1.1  mrg FILE *
    501  1.1  mrg gfc_open_included_file (const char *name, bool include_cwd, bool module)
    502  1.1  mrg {
    503  1.1  mrg   FILE *f = NULL;
    504  1.1  mrg 
    505  1.1  mrg   if (IS_ABSOLUTE_PATH (name) || include_cwd)
    506  1.1  mrg     {
    507  1.1  mrg       f = gfc_open_file (name);
    508  1.1  mrg       if (f && gfc_cpp_makedep ())
    509  1.1  mrg 	gfc_cpp_add_dep (name, false);
    510  1.1  mrg     }
    511  1.1  mrg 
    512  1.1  mrg   if (!f)
    513  1.1  mrg     f = open_included_file (name, include_dirs, module, false);
    514  1.1  mrg 
    515  1.1  mrg   return f;
    516  1.1  mrg }
    517  1.1  mrg 
    518  1.1  mrg 
    519  1.1  mrg /* Test to see if we're at the end of the main source file.  */
    520  1.1  mrg 
    521  1.1  mrg int
    522  1.1  mrg gfc_at_end (void)
    523  1.1  mrg {
    524  1.1  mrg   return end_flag;
    525  1.1  mrg }
    526  1.1  mrg 
    527  1.1  mrg 
    528  1.1  mrg /* Test to see if we're at the end of the current file.  */
    529  1.1  mrg 
    530  1.1  mrg int
    531  1.1  mrg gfc_at_eof (void)
    532  1.1  mrg {
    533  1.1  mrg   if (gfc_at_end ())
    534  1.1  mrg     return 1;
    535  1.1  mrg 
    536  1.1  mrg   if (line_head == NULL)
    537  1.1  mrg     return 1;			/* Null file */
    538  1.1  mrg 
    539  1.1  mrg   if (gfc_current_locus.lb == NULL)
    540  1.1  mrg     return 1;
    541  1.1  mrg 
    542  1.1  mrg   return 0;
    543  1.1  mrg }
    544  1.1  mrg 
    545  1.1  mrg 
    546  1.1  mrg /* Test to see if we're at the beginning of a new line.  */
    547  1.1  mrg 
    548  1.1  mrg int
    549  1.1  mrg gfc_at_bol (void)
    550  1.1  mrg {
    551  1.1  mrg   if (gfc_at_eof ())
    552  1.1  mrg     return 1;
    553  1.1  mrg 
    554  1.1  mrg   return (gfc_current_locus.nextc == gfc_current_locus.lb->line);
    555  1.1  mrg }
    556  1.1  mrg 
    557  1.1  mrg 
    558  1.1  mrg /* Test to see if we're at the end of a line.  */
    559  1.1  mrg 
    560  1.1  mrg int
    561  1.1  mrg gfc_at_eol (void)
    562  1.1  mrg {
    563  1.1  mrg   if (gfc_at_eof ())
    564  1.1  mrg     return 1;
    565  1.1  mrg 
    566  1.1  mrg   return (*gfc_current_locus.nextc == '\0');
    567  1.1  mrg }
    568  1.1  mrg 
    569  1.1  mrg static void
    570  1.1  mrg add_file_change (const char *filename, int line)
    571  1.1  mrg {
    572  1.1  mrg   if (file_changes_count == file_changes_allocated)
    573  1.1  mrg     {
    574  1.1  mrg       if (file_changes_allocated)
    575  1.1  mrg 	file_changes_allocated *= 2;
    576  1.1  mrg       else
    577  1.1  mrg 	file_changes_allocated = 16;
    578  1.1  mrg       file_changes = XRESIZEVEC (struct gfc_file_change, file_changes,
    579  1.1  mrg 				 file_changes_allocated);
    580  1.1  mrg     }
    581  1.1  mrg   file_changes[file_changes_count].filename = filename;
    582  1.1  mrg   file_changes[file_changes_count].lb = NULL;
    583  1.1  mrg   file_changes[file_changes_count++].line = line;
    584  1.1  mrg }
    585  1.1  mrg 
    586  1.1  mrg static void
    587  1.1  mrg report_file_change (gfc_linebuf *lb)
    588  1.1  mrg {
    589  1.1  mrg   size_t c = file_changes_cur;
    590  1.1  mrg   while (c < file_changes_count
    591  1.1  mrg 	 && file_changes[c].lb == lb)
    592  1.1  mrg     {
    593  1.1  mrg       if (file_changes[c].filename)
    594  1.1  mrg 	(*debug_hooks->start_source_file) (file_changes[c].line,
    595  1.1  mrg 					   file_changes[c].filename);
    596  1.1  mrg       else
    597  1.1  mrg 	(*debug_hooks->end_source_file) (file_changes[c].line);
    598  1.1  mrg       ++c;
    599  1.1  mrg     }
    600  1.1  mrg   file_changes_cur = c;
    601  1.1  mrg }
    602  1.1  mrg 
    603  1.1  mrg void
    604  1.1  mrg gfc_start_source_files (void)
    605  1.1  mrg {
    606  1.1  mrg   /* If the debugger wants the name of the main source file,
    607  1.1  mrg      we give it.  */
    608  1.1  mrg   if (debug_hooks->start_end_main_source_file)
    609  1.1  mrg     (*debug_hooks->start_source_file) (0, gfc_source_file);
    610  1.1  mrg 
    611  1.1  mrg   file_changes_cur = 0;
    612  1.1  mrg   report_file_change (gfc_current_locus.lb);
    613  1.1  mrg }
    614  1.1  mrg 
    615  1.1  mrg void
    616  1.1  mrg gfc_end_source_files (void)
    617  1.1  mrg {
    618  1.1  mrg   report_file_change (NULL);
    619  1.1  mrg 
    620  1.1  mrg   if (debug_hooks->start_end_main_source_file)
    621  1.1  mrg     (*debug_hooks->end_source_file) (0);
    622  1.1  mrg }
    623  1.1  mrg 
    624  1.1  mrg /* Advance the current line pointer to the next line.  */
    625  1.1  mrg 
    626  1.1  mrg void
    627  1.1  mrg gfc_advance_line (void)
    628  1.1  mrg {
    629  1.1  mrg   if (gfc_at_end ())
    630  1.1  mrg     return;
    631  1.1  mrg 
    632  1.1  mrg   if (gfc_current_locus.lb == NULL)
    633  1.1  mrg     {
    634  1.1  mrg       end_flag = 1;
    635  1.1  mrg       return;
    636  1.1  mrg     }
    637  1.1  mrg 
    638  1.1  mrg   if (gfc_current_locus.lb->next
    639  1.1  mrg       && !gfc_current_locus.lb->next->dbg_emitted)
    640  1.1  mrg     {
    641  1.1  mrg       report_file_change (gfc_current_locus.lb->next);
    642  1.1  mrg       gfc_current_locus.lb->next->dbg_emitted = true;
    643  1.1  mrg     }
    644  1.1  mrg 
    645  1.1  mrg   gfc_current_locus.lb = gfc_current_locus.lb->next;
    646  1.1  mrg 
    647  1.1  mrg   if (gfc_current_locus.lb != NULL)
    648  1.1  mrg     gfc_current_locus.nextc = gfc_current_locus.lb->line;
    649  1.1  mrg   else
    650  1.1  mrg     {
    651  1.1  mrg       gfc_current_locus.nextc = NULL;
    652  1.1  mrg       end_flag = 1;
    653  1.1  mrg     }
    654  1.1  mrg }
    655  1.1  mrg 
    656  1.1  mrg 
    657  1.1  mrg /* Get the next character from the input, advancing gfc_current_file's
    658  1.1  mrg    locus.  When we hit the end of the line or the end of the file, we
    659  1.1  mrg    start returning a '\n' in order to complete the current statement.
    660  1.1  mrg    No Fortran line conventions are implemented here.
    661  1.1  mrg 
    662  1.1  mrg    Requiring explicit advances to the next line prevents the parse
    663  1.1  mrg    pointer from being on the wrong line if the current statement ends
    664  1.1  mrg    prematurely.  */
    665  1.1  mrg 
    666  1.1  mrg static gfc_char_t
    667  1.1  mrg next_char (void)
    668  1.1  mrg {
    669  1.1  mrg   gfc_char_t c;
    670  1.1  mrg 
    671  1.1  mrg   if (gfc_current_locus.nextc == NULL)
    672  1.1  mrg     return '\n';
    673  1.1  mrg 
    674  1.1  mrg   c = *gfc_current_locus.nextc++;
    675  1.1  mrg   if (c == '\0')
    676  1.1  mrg     {
    677  1.1  mrg       gfc_current_locus.nextc--; /* Remain on this line.  */
    678  1.1  mrg       c = '\n';
    679  1.1  mrg     }
    680  1.1  mrg 
    681  1.1  mrg   return c;
    682  1.1  mrg }
    683  1.1  mrg 
    684  1.1  mrg 
    685  1.1  mrg /* Skip a comment.  When we come here the parse pointer is positioned
    686  1.1  mrg    immediately after the comment character.  If we ever implement
    687  1.1  mrg    compiler directives within comments, here is where we parse the
    688  1.1  mrg    directive.  */
    689  1.1  mrg 
    690  1.1  mrg static void
    691  1.1  mrg skip_comment_line (void)
    692  1.1  mrg {
    693  1.1  mrg   gfc_char_t c;
    694  1.1  mrg 
    695  1.1  mrg   do
    696  1.1  mrg     {
    697  1.1  mrg       c = next_char ();
    698  1.1  mrg     }
    699  1.1  mrg   while (c != '\n');
    700  1.1  mrg 
    701  1.1  mrg   gfc_advance_line ();
    702  1.1  mrg }
    703  1.1  mrg 
    704  1.1  mrg 
    705  1.1  mrg int
    706  1.1  mrg gfc_define_undef_line (void)
    707  1.1  mrg {
    708  1.1  mrg   char *tmp;
    709  1.1  mrg 
    710  1.1  mrg   /* All lines beginning with '#' are either #define or #undef.  */
    711  1.1  mrg   if (debug_info_level != DINFO_LEVEL_VERBOSE || gfc_peek_ascii_char () != '#')
    712  1.1  mrg     return 0;
    713  1.1  mrg 
    714  1.1  mrg   if (wide_strncmp (gfc_current_locus.nextc, "#define ", 8) == 0)
    715  1.1  mrg     {
    716  1.1  mrg       tmp = gfc_widechar_to_char (&gfc_current_locus.nextc[8], -1);
    717  1.1  mrg       (*debug_hooks->define) (gfc_linebuf_linenum (gfc_current_locus.lb),
    718  1.1  mrg 			      tmp);
    719  1.1  mrg       free (tmp);
    720  1.1  mrg     }
    721  1.1  mrg 
    722  1.1  mrg   if (wide_strncmp (gfc_current_locus.nextc, "#undef ", 7) == 0)
    723  1.1  mrg     {
    724  1.1  mrg       tmp = gfc_widechar_to_char (&gfc_current_locus.nextc[7], -1);
    725  1.1  mrg       (*debug_hooks->undef) (gfc_linebuf_linenum (gfc_current_locus.lb),
    726  1.1  mrg 			     tmp);
    727  1.1  mrg       free (tmp);
    728  1.1  mrg     }
    729  1.1  mrg 
    730  1.1  mrg   /* Skip the rest of the line.  */
    731  1.1  mrg   skip_comment_line ();
    732  1.1  mrg 
    733  1.1  mrg   return 1;
    734  1.1  mrg }
    735  1.1  mrg 
    736  1.1  mrg 
    737  1.1  mrg /* Return true if GCC$ was matched.  */
    738  1.1  mrg static bool
    739  1.1  mrg skip_gcc_attribute (locus start)
    740  1.1  mrg {
    741  1.1  mrg   bool r = false;
    742  1.1  mrg   char c;
    743  1.1  mrg   locus old_loc = gfc_current_locus;
    744  1.1  mrg 
    745  1.1  mrg   if ((c = next_char ()) == 'g' || c == 'G')
    746  1.1  mrg     if ((c = next_char ()) == 'c' || c == 'C')
    747  1.1  mrg       if ((c = next_char ()) == 'c' || c == 'C')
    748  1.1  mrg 	if ((c = next_char ()) == '$')
    749  1.1  mrg 	  r = true;
    750  1.1  mrg 
    751  1.1  mrg   if (r == false)
    752  1.1  mrg     gfc_current_locus = old_loc;
    753  1.1  mrg   else
    754  1.1  mrg    {
    755  1.1  mrg       gcc_attribute_flag = 1;
    756  1.1  mrg       gcc_attribute_locus = old_loc;
    757  1.1  mrg       gfc_current_locus = start;
    758  1.1  mrg    }
    759  1.1  mrg 
    760  1.1  mrg   return r;
    761  1.1  mrg }
    762  1.1  mrg 
    763  1.1  mrg /* Return true if CC was matched.  */
    764  1.1  mrg static bool
    765  1.1  mrg skip_free_oacc_sentinel (locus start, locus old_loc)
    766  1.1  mrg {
    767  1.1  mrg   bool r = false;
    768  1.1  mrg   char c;
    769  1.1  mrg 
    770  1.1  mrg   if ((c = next_char ()) == 'c' || c == 'C')
    771  1.1  mrg     if ((c = next_char ()) == 'c' || c == 'C')
    772  1.1  mrg       r = true;
    773  1.1  mrg 
    774  1.1  mrg   if (r)
    775  1.1  mrg    {
    776  1.1  mrg       if ((c = next_char ()) == ' ' || c == '\t'
    777  1.1  mrg 	  || continue_flag)
    778  1.1  mrg 	{
    779  1.1  mrg 	  while (gfc_is_whitespace (c))
    780  1.1  mrg 	    c = next_char ();
    781  1.1  mrg 	  if (c != '\n' && c != '!')
    782  1.1  mrg 	    {
    783  1.1  mrg 	      openacc_flag = 1;
    784  1.1  mrg 	      openacc_locus = old_loc;
    785  1.1  mrg 	      gfc_current_locus = start;
    786  1.1  mrg 	    }
    787  1.1  mrg 	  else
    788  1.1  mrg 	    r = false;
    789  1.1  mrg 	}
    790  1.1  mrg       else
    791  1.1  mrg 	{
    792  1.1  mrg 	  gfc_warning_now (0, "!$ACC at %C starts a commented "
    793  1.1  mrg 			   "line as it neither is followed "
    794  1.1  mrg 			   "by a space nor is a "
    795  1.1  mrg 			   "continuation line");
    796  1.1  mrg 	  r = false;
    797  1.1  mrg 	}
    798  1.1  mrg    }
    799  1.1  mrg 
    800  1.1  mrg   return r;
    801  1.1  mrg }
    802  1.1  mrg 
    803  1.1  mrg /* Return true if MP was matched.  */
    804  1.1  mrg static bool
    805  1.1  mrg skip_free_omp_sentinel (locus start, locus old_loc)
    806  1.1  mrg {
    807  1.1  mrg   bool r = false;
    808  1.1  mrg   char c;
    809  1.1  mrg 
    810  1.1  mrg   if ((c = next_char ()) == 'm' || c == 'M')
    811  1.1  mrg     if ((c = next_char ()) == 'p' || c == 'P')
    812  1.1  mrg       r = true;
    813  1.1  mrg 
    814  1.1  mrg   if (r)
    815  1.1  mrg    {
    816  1.1  mrg       if ((c = next_char ()) == ' ' || c == '\t'
    817  1.1  mrg 	  || continue_flag)
    818  1.1  mrg 	{
    819  1.1  mrg 	  while (gfc_is_whitespace (c))
    820  1.1  mrg 	    c = next_char ();
    821  1.1  mrg 	  if (c != '\n' && c != '!')
    822  1.1  mrg 	    {
    823  1.1  mrg 	      openmp_flag = 1;
    824  1.1  mrg 	      openmp_locus = old_loc;
    825  1.1  mrg 	      gfc_current_locus = start;
    826  1.1  mrg 	    }
    827  1.1  mrg 	  else
    828  1.1  mrg 	    r = false;
    829  1.1  mrg 	}
    830  1.1  mrg       else
    831  1.1  mrg 	{
    832  1.1  mrg 	  gfc_warning_now (0, "!$OMP at %C starts a commented "
    833  1.1  mrg 			   "line as it neither is followed "
    834  1.1  mrg 			   "by a space nor is a "
    835  1.1  mrg 			   "continuation line");
    836  1.1  mrg 	  r = false;
    837  1.1  mrg 	}
    838  1.1  mrg    }
    839  1.1  mrg 
    840  1.1  mrg   return r;
    841  1.1  mrg }
    842  1.1  mrg 
    843  1.1  mrg /* Comment lines are null lines, lines containing only blanks or lines
    844  1.1  mrg    on which the first nonblank line is a '!'.
    845  1.1  mrg    Return true if !$ openmp or openacc conditional compilation sentinel was
    846  1.1  mrg    seen.  */
    847  1.1  mrg 
    848  1.1  mrg static bool
    849  1.1  mrg skip_free_comments (void)
    850  1.1  mrg {
    851  1.1  mrg   locus start;
    852  1.1  mrg   gfc_char_t c;
    853  1.1  mrg   int at_bol;
    854  1.1  mrg 
    855  1.1  mrg   for (;;)
    856  1.1  mrg     {
    857  1.1  mrg       at_bol = gfc_at_bol ();
    858  1.1  mrg       start = gfc_current_locus;
    859  1.1  mrg       if (gfc_at_eof ())
    860  1.1  mrg 	break;
    861  1.1  mrg 
    862  1.1  mrg       do
    863  1.1  mrg 	c = next_char ();
    864  1.1  mrg       while (gfc_is_whitespace (c));
    865  1.1  mrg 
    866  1.1  mrg       if (c == '\n')
    867  1.1  mrg 	{
    868  1.1  mrg 	  gfc_advance_line ();
    869  1.1  mrg 	  continue;
    870  1.1  mrg 	}
    871  1.1  mrg 
    872  1.1  mrg       if (c == '!')
    873  1.1  mrg 	{
    874  1.1  mrg 	  /* Keep the !GCC$ line.  */
    875  1.1  mrg 	  if (at_bol && skip_gcc_attribute (start))
    876  1.1  mrg 	    return false;
    877  1.1  mrg 
    878  1.1  mrg 	  /* If -fopenmp/-fopenacc, we need to handle here 2 things:
    879  1.1  mrg 	     1) don't treat !$omp/!$acc as comments, but directives
    880  1.1  mrg 	     2) handle OpenMP/OpenACC conditional compilation, where
    881  1.1  mrg 		!$ should be treated as 2 spaces (for initial lines
    882  1.1  mrg 		only if followed by space).  */
    883  1.1  mrg 	  if (at_bol)
    884  1.1  mrg 	  {
    885  1.1  mrg 	    if ((flag_openmp || flag_openmp_simd)
    886  1.1  mrg 		&& flag_openacc)
    887  1.1  mrg 	      {
    888  1.1  mrg 		locus old_loc = gfc_current_locus;
    889  1.1  mrg 		if (next_char () == '$')
    890  1.1  mrg 		  {
    891  1.1  mrg 		    c = next_char ();
    892  1.1  mrg 		    if (c == 'o' || c == 'O')
    893  1.1  mrg 		      {
    894  1.1  mrg 			if (skip_free_omp_sentinel (start, old_loc))
    895  1.1  mrg 			  return false;
    896  1.1  mrg 			gfc_current_locus = old_loc;
    897  1.1  mrg 			next_char ();
    898  1.1  mrg 			c = next_char ();
    899  1.1  mrg 		      }
    900  1.1  mrg 		    else if (c == 'a' || c == 'A')
    901  1.1  mrg 		      {
    902  1.1  mrg 			if (skip_free_oacc_sentinel (start, old_loc))
    903  1.1  mrg 			  return false;
    904  1.1  mrg 			gfc_current_locus = old_loc;
    905  1.1  mrg 			next_char ();
    906  1.1  mrg 			c = next_char ();
    907  1.1  mrg 		      }
    908  1.1  mrg 		    if (continue_flag || c == ' ' || c == '\t')
    909  1.1  mrg 		      {
    910  1.1  mrg 			gfc_current_locus = old_loc;
    911  1.1  mrg 			next_char ();
    912  1.1  mrg 			openmp_flag = openacc_flag = 0;
    913  1.1  mrg 			return true;
    914  1.1  mrg 		      }
    915  1.1  mrg 		  }
    916  1.1  mrg 		gfc_current_locus = old_loc;
    917  1.1  mrg 	      }
    918  1.1  mrg 	    else if ((flag_openmp || flag_openmp_simd)
    919  1.1  mrg 		     && !flag_openacc)
    920  1.1  mrg 	      {
    921  1.1  mrg 		locus old_loc = gfc_current_locus;
    922  1.1  mrg 		if (next_char () == '$')
    923  1.1  mrg 		  {
    924  1.1  mrg 		    c = next_char ();
    925  1.1  mrg 		    if (c == 'o' || c == 'O')
    926  1.1  mrg 		      {
    927  1.1  mrg 			if (skip_free_omp_sentinel (start, old_loc))
    928  1.1  mrg 			  return false;
    929  1.1  mrg 			gfc_current_locus = old_loc;
    930  1.1  mrg 			next_char ();
    931  1.1  mrg 			c = next_char ();
    932  1.1  mrg 		      }
    933  1.1  mrg 		    if (continue_flag || c == ' ' || c == '\t')
    934  1.1  mrg 		      {
    935  1.1  mrg 			gfc_current_locus = old_loc;
    936  1.1  mrg 			next_char ();
    937  1.1  mrg 			openmp_flag = 0;
    938  1.1  mrg 			return true;
    939  1.1  mrg 		      }
    940  1.1  mrg 		  }
    941  1.1  mrg 		gfc_current_locus = old_loc;
    942  1.1  mrg 	      }
    943  1.1  mrg 	    else if (flag_openacc
    944  1.1  mrg 		     && !(flag_openmp || flag_openmp_simd))
    945  1.1  mrg 	      {
    946  1.1  mrg 		locus old_loc = gfc_current_locus;
    947  1.1  mrg 		if (next_char () == '$')
    948  1.1  mrg 		  {
    949  1.1  mrg 		    c = next_char ();
    950  1.1  mrg 		    if (c == 'a' || c == 'A')
    951  1.1  mrg 		      {
    952  1.1  mrg 			if (skip_free_oacc_sentinel (start, old_loc))
    953  1.1  mrg 			  return false;
    954  1.1  mrg 			gfc_current_locus = old_loc;
    955  1.1  mrg 			next_char();
    956  1.1  mrg 			c = next_char();
    957  1.1  mrg 		      }
    958  1.1  mrg 		  }
    959  1.1  mrg 		gfc_current_locus = old_loc;
    960  1.1  mrg 	      }
    961  1.1  mrg 	  }
    962  1.1  mrg 	  skip_comment_line ();
    963  1.1  mrg 	  continue;
    964  1.1  mrg 	}
    965  1.1  mrg 
    966  1.1  mrg       break;
    967  1.1  mrg     }
    968  1.1  mrg 
    969  1.1  mrg   if (openmp_flag && at_bol)
    970  1.1  mrg     openmp_flag = 0;
    971  1.1  mrg 
    972  1.1  mrg   if (openacc_flag && at_bol)
    973  1.1  mrg     openacc_flag = 0;
    974  1.1  mrg 
    975  1.1  mrg   gcc_attribute_flag = 0;
    976  1.1  mrg   gfc_current_locus = start;
    977  1.1  mrg   return false;
    978  1.1  mrg }
    979  1.1  mrg 
    980  1.1  mrg /* Return true if MP was matched in fixed form.  */
    981  1.1  mrg static bool
    982  1.1  mrg skip_fixed_omp_sentinel (locus *start)
    983  1.1  mrg {
    984  1.1  mrg   gfc_char_t c;
    985  1.1  mrg   if (((c = next_char ()) == 'm' || c == 'M')
    986  1.1  mrg       && ((c = next_char ()) == 'p' || c == 'P'))
    987  1.1  mrg     {
    988  1.1  mrg       c = next_char ();
    989  1.1  mrg       if (c != '\n'
    990  1.1  mrg 	  && (continue_flag
    991  1.1  mrg 	      || c == ' ' || c == '\t' || c == '0'))
    992  1.1  mrg 	{
    993  1.1  mrg 	  if (c == ' ' || c == '\t' || c == '0')
    994  1.1  mrg 	    openacc_flag = 0;
    995  1.1  mrg 	  do
    996  1.1  mrg 	    c = next_char ();
    997  1.1  mrg 	  while (gfc_is_whitespace (c));
    998  1.1  mrg 	  if (c != '\n' && c != '!')
    999  1.1  mrg 	    {
   1000  1.1  mrg 	      /* Canonicalize to *$omp.  */
   1001  1.1  mrg 	      *start->nextc = '*';
   1002  1.1  mrg 	      openmp_flag = 1;
   1003  1.1  mrg 	      gfc_current_locus = *start;
   1004  1.1  mrg 	      return true;
   1005  1.1  mrg 	    }
   1006  1.1  mrg 	}
   1007  1.1  mrg     }
   1008  1.1  mrg   return false;
   1009  1.1  mrg }
   1010  1.1  mrg 
   1011  1.1  mrg /* Return true if CC was matched in fixed form.  */
   1012  1.1  mrg static bool
   1013  1.1  mrg skip_fixed_oacc_sentinel (locus *start)
   1014  1.1  mrg {
   1015  1.1  mrg   gfc_char_t c;
   1016  1.1  mrg   if (((c = next_char ()) == 'c' || c == 'C')
   1017  1.1  mrg       && ((c = next_char ()) == 'c' || c == 'C'))
   1018  1.1  mrg     {
   1019  1.1  mrg       c = next_char ();
   1020  1.1  mrg       if (c != '\n'
   1021  1.1  mrg 	  && (continue_flag
   1022  1.1  mrg 	      || c == ' ' || c == '\t' || c == '0'))
   1023  1.1  mrg 	{
   1024  1.1  mrg 	  if (c == ' ' || c == '\t' || c == '0')
   1025  1.1  mrg 	    openmp_flag = 0;
   1026  1.1  mrg 	  do
   1027  1.1  mrg 	    c = next_char ();
   1028  1.1  mrg 	  while (gfc_is_whitespace (c));
   1029  1.1  mrg 	  if (c != '\n' && c != '!')
   1030  1.1  mrg 	    {
   1031  1.1  mrg 	      /* Canonicalize to *$acc.  */
   1032  1.1  mrg 	      *start->nextc = '*';
   1033  1.1  mrg 	      openacc_flag = 1;
   1034  1.1  mrg 	      gfc_current_locus = *start;
   1035  1.1  mrg 	      return true;
   1036  1.1  mrg 	    }
   1037  1.1  mrg 	}
   1038  1.1  mrg     }
   1039  1.1  mrg   return false;
   1040  1.1  mrg }
   1041  1.1  mrg 
   1042  1.1  mrg /* Skip comment lines in fixed source mode.  We have the same rules as
   1043  1.1  mrg    in skip_free_comment(), except that we can have a 'c', 'C' or '*'
   1044  1.1  mrg    in column 1, and a '!' cannot be in column 6.  Also, we deal with
   1045  1.1  mrg    lines with 'd' or 'D' in column 1, if the user requested this.  */
   1046  1.1  mrg 
   1047  1.1  mrg static void
   1048  1.1  mrg skip_fixed_comments (void)
   1049  1.1  mrg {
   1050  1.1  mrg   locus start;
   1051  1.1  mrg   int col;
   1052  1.1  mrg   gfc_char_t c;
   1053  1.1  mrg 
   1054  1.1  mrg   if (! gfc_at_bol ())
   1055  1.1  mrg     {
   1056  1.1  mrg       start = gfc_current_locus;
   1057  1.1  mrg       if (! gfc_at_eof ())
   1058  1.1  mrg 	{
   1059  1.1  mrg 	  do
   1060  1.1  mrg 	    c = next_char ();
   1061  1.1  mrg 	  while (gfc_is_whitespace (c));
   1062  1.1  mrg 
   1063  1.1  mrg 	  if (c == '\n')
   1064  1.1  mrg 	    gfc_advance_line ();
   1065  1.1  mrg 	  else if (c == '!')
   1066  1.1  mrg 	    skip_comment_line ();
   1067  1.1  mrg 	}
   1068  1.1  mrg 
   1069  1.1  mrg       if (! gfc_at_bol ())
   1070  1.1  mrg 	{
   1071  1.1  mrg 	  gfc_current_locus = start;
   1072  1.1  mrg 	  return;
   1073  1.1  mrg 	}
   1074  1.1  mrg     }
   1075  1.1  mrg 
   1076  1.1  mrg   for (;;)
   1077  1.1  mrg     {
   1078  1.1  mrg       start = gfc_current_locus;
   1079  1.1  mrg       if (gfc_at_eof ())
   1080  1.1  mrg 	break;
   1081  1.1  mrg 
   1082  1.1  mrg       c = next_char ();
   1083  1.1  mrg       if (c == '\n')
   1084  1.1  mrg 	{
   1085  1.1  mrg 	  gfc_advance_line ();
   1086  1.1  mrg 	  continue;
   1087  1.1  mrg 	}
   1088  1.1  mrg 
   1089  1.1  mrg       if (c == '!' || c == 'c' || c == 'C' || c == '*')
   1090  1.1  mrg 	{
   1091  1.1  mrg 	  if (skip_gcc_attribute (start))
   1092  1.1  mrg 	    {
   1093  1.1  mrg 	      /* Canonicalize to *$omp.  */
   1094  1.1  mrg 	      *start.nextc = '*';
   1095  1.1  mrg 	      return;
   1096  1.1  mrg 	    }
   1097  1.1  mrg 
   1098  1.1  mrg 	  if (gfc_current_locus.lb != NULL
   1099  1.1  mrg 	      && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
   1100  1.1  mrg 	    continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
   1101  1.1  mrg 
   1102  1.1  mrg 	  /* If -fopenmp/-fopenacc, we need to handle here 2 things:
   1103  1.1  mrg 	     1) don't treat !$omp/!$acc|c$omp/c$acc|*$omp / *$acc as comments,
   1104  1.1  mrg 		but directives
   1105  1.1  mrg 	     2) handle OpenMP/OpenACC conditional compilation, where
   1106  1.1  mrg 		!$|c$|*$ should be treated as 2 spaces if the characters
   1107  1.1  mrg 		in columns 3 to 6 are valid fixed form label columns
   1108  1.1  mrg 		characters.  */
   1109  1.1  mrg 	  if ((flag_openmp || flag_openmp_simd) && !flag_openacc)
   1110  1.1  mrg 	    {
   1111  1.1  mrg 	      if (next_char () == '$')
   1112  1.1  mrg 		{
   1113  1.1  mrg 		  c = next_char ();
   1114  1.1  mrg 		  if (c == 'o' || c == 'O')
   1115  1.1  mrg 		    {
   1116  1.1  mrg 		      if (skip_fixed_omp_sentinel (&start))
   1117  1.1  mrg 			return;
   1118  1.1  mrg 		    }
   1119  1.1  mrg 		  else
   1120  1.1  mrg 		    goto check_for_digits;
   1121  1.1  mrg 		}
   1122  1.1  mrg 	      gfc_current_locus = start;
   1123  1.1  mrg 	    }
   1124  1.1  mrg 	  else if (flag_openacc && !(flag_openmp || flag_openmp_simd))
   1125  1.1  mrg 	    {
   1126  1.1  mrg 	      if (next_char () == '$')
   1127  1.1  mrg 		{
   1128  1.1  mrg 		  c = next_char ();
   1129  1.1  mrg 		  if (c == 'a' || c == 'A')
   1130  1.1  mrg 		    {
   1131  1.1  mrg 		      if (skip_fixed_oacc_sentinel (&start))
   1132  1.1  mrg 			return;
   1133  1.1  mrg 		    }
   1134  1.1  mrg 		}
   1135  1.1  mrg 	      gfc_current_locus = start;
   1136  1.1  mrg 	    }
   1137  1.1  mrg 	  else if (flag_openacc || flag_openmp || flag_openmp_simd)
   1138  1.1  mrg 	    {
   1139  1.1  mrg 	      if (next_char () == '$')
   1140  1.1  mrg 		{
   1141  1.1  mrg 		  c = next_char ();
   1142  1.1  mrg 		  if (c == 'a' || c == 'A')
   1143  1.1  mrg 		    {
   1144  1.1  mrg 		      if (skip_fixed_oacc_sentinel (&start))
   1145  1.1  mrg 			return;
   1146  1.1  mrg 		    }
   1147  1.1  mrg 		  else if (c == 'o' || c == 'O')
   1148  1.1  mrg 		    {
   1149  1.1  mrg 		      if (skip_fixed_omp_sentinel (&start))
   1150  1.1  mrg 			return;
   1151  1.1  mrg 		    }
   1152  1.1  mrg 		  else
   1153  1.1  mrg 		    goto check_for_digits;
   1154  1.1  mrg 		}
   1155  1.1  mrg 	      gfc_current_locus = start;
   1156  1.1  mrg 	    }
   1157  1.1  mrg 
   1158  1.1  mrg 	  skip_comment_line ();
   1159  1.1  mrg 	  continue;
   1160  1.1  mrg 
   1161  1.1  mrg check_for_digits:
   1162  1.1  mrg 	  {
   1163  1.1  mrg 	    /* Required for OpenMP's conditional compilation sentinel. */
   1164  1.1  mrg 	    int digit_seen = 0;
   1165  1.1  mrg 
   1166  1.1  mrg 	    for (col = 3; col < 6; col++, c = next_char ())
   1167  1.1  mrg 	      if (c == ' ')
   1168  1.1  mrg 		continue;
   1169  1.1  mrg 	      else if (c == '\t')
   1170  1.1  mrg 		{
   1171  1.1  mrg 		  col = 6;
   1172  1.1  mrg 		  break;
   1173  1.1  mrg 		}
   1174  1.1  mrg 	      else if (c < '0' || c > '9')
   1175  1.1  mrg 		break;
   1176  1.1  mrg 	      else
   1177  1.1  mrg 		digit_seen = 1;
   1178  1.1  mrg 
   1179  1.1  mrg 	    if (col == 6 && c != '\n'
   1180  1.1  mrg 		&& ((continue_flag && !digit_seen)
   1181  1.1  mrg 		    || c == ' ' || c == '\t' || c == '0'))
   1182  1.1  mrg 	      {
   1183  1.1  mrg 		gfc_current_locus = start;
   1184  1.1  mrg 		start.nextc[0] = ' ';
   1185  1.1  mrg 		start.nextc[1] = ' ';
   1186  1.1  mrg 		continue;
   1187  1.1  mrg 	      }
   1188  1.1  mrg 	    }
   1189  1.1  mrg 	  skip_comment_line ();
   1190  1.1  mrg 	  continue;
   1191  1.1  mrg 	}
   1192  1.1  mrg 
   1193  1.1  mrg       if (gfc_option.flag_d_lines != -1 && (c == 'd' || c == 'D'))
   1194  1.1  mrg 	{
   1195  1.1  mrg 	  if (gfc_option.flag_d_lines == 0)
   1196  1.1  mrg 	    {
   1197  1.1  mrg 	      skip_comment_line ();
   1198  1.1  mrg 	      continue;
   1199  1.1  mrg 	    }
   1200  1.1  mrg 	  else
   1201  1.1  mrg 	    *start.nextc = c = ' ';
   1202  1.1  mrg 	}
   1203  1.1  mrg 
   1204  1.1  mrg       col = 1;
   1205  1.1  mrg 
   1206  1.1  mrg       while (gfc_is_whitespace (c))
   1207  1.1  mrg 	{
   1208  1.1  mrg 	  c = next_char ();
   1209  1.1  mrg 	  col++;
   1210  1.1  mrg 	}
   1211  1.1  mrg 
   1212  1.1  mrg       if (c == '\n')
   1213  1.1  mrg 	{
   1214  1.1  mrg 	  gfc_advance_line ();
   1215  1.1  mrg 	  continue;
   1216  1.1  mrg 	}
   1217  1.1  mrg 
   1218  1.1  mrg       if (col != 6 && c == '!')
   1219  1.1  mrg 	{
   1220  1.1  mrg 	  if (gfc_current_locus.lb != NULL
   1221  1.1  mrg 	      && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
   1222  1.1  mrg 	    continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
   1223  1.1  mrg 	  skip_comment_line ();
   1224  1.1  mrg 	  continue;
   1225  1.1  mrg 	}
   1226  1.1  mrg 
   1227  1.1  mrg       break;
   1228  1.1  mrg     }
   1229  1.1  mrg 
   1230  1.1  mrg   openmp_flag = 0;
   1231  1.1  mrg   openacc_flag = 0;
   1232  1.1  mrg   gcc_attribute_flag = 0;
   1233  1.1  mrg   gfc_current_locus = start;
   1234  1.1  mrg }
   1235  1.1  mrg 
   1236  1.1  mrg 
   1237  1.1  mrg /* Skips the current line if it is a comment.  */
   1238  1.1  mrg 
   1239  1.1  mrg void
   1240  1.1  mrg gfc_skip_comments (void)
   1241  1.1  mrg {
   1242  1.1  mrg   if (gfc_current_form == FORM_FREE)
   1243  1.1  mrg     skip_free_comments ();
   1244  1.1  mrg   else
   1245  1.1  mrg     skip_fixed_comments ();
   1246  1.1  mrg }
   1247  1.1  mrg 
   1248  1.1  mrg 
   1249  1.1  mrg /* Get the next character from the input, taking continuation lines
   1250  1.1  mrg    and end-of-line comments into account.  This implies that comment
   1251  1.1  mrg    lines between continued lines must be eaten here.  For higher-level
   1252  1.1  mrg    subroutines, this flattens continued lines into a single logical
   1253  1.1  mrg    line.  The in_string flag denotes whether we're inside a character
   1254  1.1  mrg    context or not.  */
   1255  1.1  mrg 
   1256  1.1  mrg gfc_char_t
   1257  1.1  mrg gfc_next_char_literal (gfc_instring in_string)
   1258  1.1  mrg {
   1259  1.1  mrg   static locus omp_acc_err_loc = {};
   1260  1.1  mrg   locus old_loc;
   1261  1.1  mrg   int i, prev_openmp_flag, prev_openacc_flag;
   1262  1.1  mrg   gfc_char_t c;
   1263  1.1  mrg 
   1264  1.1  mrg   continue_flag = 0;
   1265  1.1  mrg   prev_openacc_flag = prev_openmp_flag = 0;
   1266  1.1  mrg 
   1267  1.1  mrg restart:
   1268  1.1  mrg   c = next_char ();
   1269  1.1  mrg   if (gfc_at_end ())
   1270  1.1  mrg     {
   1271  1.1  mrg       continue_count = 0;
   1272  1.1  mrg       return c;
   1273  1.1  mrg     }
   1274  1.1  mrg 
   1275  1.1  mrg   if (gfc_current_form == FORM_FREE)
   1276  1.1  mrg     {
   1277  1.1  mrg       bool openmp_cond_flag;
   1278  1.1  mrg 
   1279  1.1  mrg       if (!in_string && c == '!')
   1280  1.1  mrg 	{
   1281  1.1  mrg 	  if (gcc_attribute_flag
   1282  1.1  mrg 	      && memcmp (&gfc_current_locus, &gcc_attribute_locus,
   1283  1.1  mrg 		 sizeof (gfc_current_locus)) == 0)
   1284  1.1  mrg 	    goto done;
   1285  1.1  mrg 
   1286  1.1  mrg 	  if (openmp_flag
   1287  1.1  mrg 	      && memcmp (&gfc_current_locus, &openmp_locus,
   1288  1.1  mrg 		 sizeof (gfc_current_locus)) == 0)
   1289  1.1  mrg 	    goto done;
   1290  1.1  mrg 
   1291  1.1  mrg 	  if (openacc_flag
   1292  1.1  mrg 	      && memcmp (&gfc_current_locus, &openacc_locus,
   1293  1.1  mrg 	         sizeof (gfc_current_locus)) == 0)
   1294  1.1  mrg 	    goto done;
   1295  1.1  mrg 
   1296  1.1  mrg 	  /* This line can't be continued */
   1297  1.1  mrg 	  do
   1298  1.1  mrg 	    {
   1299  1.1  mrg 	      c = next_char ();
   1300  1.1  mrg 	    }
   1301  1.1  mrg 	  while (c != '\n');
   1302  1.1  mrg 
   1303  1.1  mrg 	  /* Avoid truncation warnings for comment ending lines.  */
   1304  1.1  mrg 	  gfc_current_locus.lb->truncated = 0;
   1305  1.1  mrg 
   1306  1.1  mrg 	  goto done;
   1307  1.1  mrg 	}
   1308  1.1  mrg 
   1309  1.1  mrg       /* Check to see if the continuation line was truncated.  */
   1310  1.1  mrg       if (warn_line_truncation && gfc_current_locus.lb != NULL
   1311  1.1  mrg 	  && gfc_current_locus.lb->truncated)
   1312  1.1  mrg 	{
   1313  1.1  mrg 	  int maxlen = flag_free_line_length;
   1314  1.1  mrg 	  gfc_char_t *current_nextc = gfc_current_locus.nextc;
   1315  1.1  mrg 
   1316  1.1  mrg 	  gfc_current_locus.lb->truncated = 0;
   1317  1.1  mrg 	  gfc_current_locus.nextc =  gfc_current_locus.lb->line + maxlen;
   1318  1.1  mrg 	  gfc_warning_now (OPT_Wline_truncation,
   1319  1.1  mrg 			   "Line truncated at %L", &gfc_current_locus);
   1320  1.1  mrg 	  gfc_current_locus.nextc = current_nextc;
   1321  1.1  mrg 	}
   1322  1.1  mrg 
   1323  1.1  mrg       if (c != '&')
   1324  1.1  mrg 	goto done;
   1325  1.1  mrg 
   1326  1.1  mrg       /* If the next nonblank character is a ! or \n, we've got a
   1327  1.1  mrg 	 continuation line.  */
   1328  1.1  mrg       old_loc = gfc_current_locus;
   1329  1.1  mrg 
   1330  1.1  mrg       c = next_char ();
   1331  1.1  mrg       while (gfc_is_whitespace (c))
   1332  1.1  mrg 	c = next_char ();
   1333  1.1  mrg 
   1334  1.1  mrg       /* Character constants to be continued cannot have commentary
   1335  1.1  mrg 	 after the '&'. However, there are cases where we may think we
   1336  1.1  mrg 	 are still in a string and we are looking for a possible
   1337  1.1  mrg 	 doubled quote and we end up here. See PR64506.  */
   1338  1.1  mrg 
   1339  1.1  mrg       if (in_string && c != '\n')
   1340  1.1  mrg 	{
   1341  1.1  mrg 	  gfc_current_locus = old_loc;
   1342  1.1  mrg 	  c = '&';
   1343  1.1  mrg 	  goto done;
   1344  1.1  mrg 	}
   1345  1.1  mrg 
   1346  1.1  mrg       if (c != '!' && c != '\n')
   1347  1.1  mrg 	{
   1348  1.1  mrg 	  gfc_current_locus = old_loc;
   1349  1.1  mrg 	  c = '&';
   1350  1.1  mrg 	  goto done;
   1351  1.1  mrg 	}
   1352  1.1  mrg 
   1353  1.1  mrg       if (flag_openmp)
   1354  1.1  mrg 	prev_openmp_flag = openmp_flag;
   1355  1.1  mrg       if (flag_openacc)
   1356  1.1  mrg 	prev_openacc_flag = openacc_flag;
   1357  1.1  mrg 
   1358  1.1  mrg       /* This can happen if the input file changed or via cpp's #line
   1359  1.1  mrg 	 without getting reset (e.g. via input_stmt). It also happens
   1360  1.1  mrg 	 when pre-including files via -fpre-include=.  */
   1361  1.1  mrg       if (continue_count == 0
   1362  1.1  mrg 	  && gfc_current_locus.lb
   1363  1.1  mrg 	  && continue_line > gfc_linebuf_linenum (gfc_current_locus.lb) + 1)
   1364  1.1  mrg 	continue_line = gfc_linebuf_linenum (gfc_current_locus.lb) + 1;
   1365  1.1  mrg 
   1366  1.1  mrg       continue_flag = 1;
   1367  1.1  mrg       if (c == '!')
   1368  1.1  mrg 	skip_comment_line ();
   1369  1.1  mrg       else
   1370  1.1  mrg 	gfc_advance_line ();
   1371  1.1  mrg 
   1372  1.1  mrg       if (gfc_at_eof ())
   1373  1.1  mrg 	goto not_continuation;
   1374  1.1  mrg 
   1375  1.1  mrg       /* We've got a continuation line.  If we are on the very next line after
   1376  1.1  mrg 	 the last continuation, increment the continuation line count and
   1377  1.1  mrg 	 check whether the limit has been exceeded.  */
   1378  1.1  mrg       if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1)
   1379  1.1  mrg 	{
   1380  1.1  mrg 	  if (++continue_count == gfc_option.max_continue_free)
   1381  1.1  mrg 	    {
   1382  1.1  mrg 	      if (gfc_notification_std (GFC_STD_GNU) || pedantic)
   1383  1.1  mrg 		gfc_warning (0, "Limit of %d continuations exceeded in "
   1384  1.1  mrg 			     "statement at %C", gfc_option.max_continue_free);
   1385  1.1  mrg 	    }
   1386  1.1  mrg 	}
   1387  1.1  mrg 
   1388  1.1  mrg       /* Now find where it continues. First eat any comment lines.  */
   1389  1.1  mrg       openmp_cond_flag = skip_free_comments ();
   1390  1.1  mrg 
   1391  1.1  mrg       if (gfc_current_locus.lb != NULL
   1392  1.1  mrg 	  && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
   1393  1.1  mrg 	continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
   1394  1.1  mrg 
   1395  1.1  mrg       if (flag_openmp)
   1396  1.1  mrg 	if (prev_openmp_flag != openmp_flag && !openacc_flag)
   1397  1.1  mrg 	  {
   1398  1.1  mrg 	    gfc_current_locus = old_loc;
   1399  1.1  mrg 	    openmp_flag = prev_openmp_flag;
   1400  1.1  mrg 	    c = '&';
   1401  1.1  mrg 	    goto done;
   1402  1.1  mrg 	  }
   1403  1.1  mrg 
   1404  1.1  mrg       if (flag_openacc)
   1405  1.1  mrg 	if (prev_openacc_flag != openacc_flag && !openmp_flag)
   1406  1.1  mrg 	  {
   1407  1.1  mrg 	    gfc_current_locus = old_loc;
   1408  1.1  mrg 	    openacc_flag = prev_openacc_flag;
   1409  1.1  mrg 	    c = '&';
   1410  1.1  mrg 	    goto done;
   1411  1.1  mrg 	  }
   1412  1.1  mrg 
   1413  1.1  mrg       /* Now that we have a non-comment line, probe ahead for the
   1414  1.1  mrg 	 first non-whitespace character.  If it is another '&', then
   1415  1.1  mrg 	 reading starts at the next character, otherwise we must back
   1416  1.1  mrg 	 up to where the whitespace started and resume from there.  */
   1417  1.1  mrg 
   1418  1.1  mrg       old_loc = gfc_current_locus;
   1419  1.1  mrg 
   1420  1.1  mrg       c = next_char ();
   1421  1.1  mrg       while (gfc_is_whitespace (c))
   1422  1.1  mrg 	c = next_char ();
   1423  1.1  mrg 
   1424  1.1  mrg       if (openmp_flag && !openacc_flag)
   1425  1.1  mrg 	{
   1426  1.1  mrg 	  for (i = 0; i < 5; i++, c = next_char ())
   1427  1.1  mrg 	    {
   1428  1.1  mrg 	      gcc_assert (gfc_wide_tolower (c) == (unsigned char) "!$omp"[i]);
   1429  1.1  mrg 	      if (i == 4)
   1430  1.1  mrg 		old_loc = gfc_current_locus;
   1431  1.1  mrg 	    }
   1432  1.1  mrg 	  while (gfc_is_whitespace (c))
   1433  1.1  mrg 	    c = next_char ();
   1434  1.1  mrg 	}
   1435  1.1  mrg       if (openacc_flag && !openmp_flag)
   1436  1.1  mrg 	{
   1437  1.1  mrg 	  for (i = 0; i < 5; i++, c = next_char ())
   1438  1.1  mrg 	    {
   1439  1.1  mrg 	      gcc_assert (gfc_wide_tolower (c) == (unsigned char) "!$acc"[i]);
   1440  1.1  mrg 	      if (i == 4)
   1441  1.1  mrg 		old_loc = gfc_current_locus;
   1442  1.1  mrg 	    }
   1443  1.1  mrg 	  while (gfc_is_whitespace (c))
   1444  1.1  mrg 	    c = next_char ();
   1445  1.1  mrg 	}
   1446  1.1  mrg 
   1447  1.1  mrg       /* In case we have an OpenMP directive continued by OpenACC
   1448  1.1  mrg 	 sentinel, or vice versa, we get both openmp_flag and
   1449  1.1  mrg 	 openacc_flag on.  */
   1450  1.1  mrg 
   1451  1.1  mrg       if (openacc_flag && openmp_flag)
   1452  1.1  mrg 	{
   1453  1.1  mrg 	  int is_openmp = 0;
   1454  1.1  mrg 	  for (i = 0; i < 5; i++, c = next_char ())
   1455  1.1  mrg 	    {
   1456  1.1  mrg 	      if (gfc_wide_tolower (c) != (unsigned char) "!$acc"[i])
   1457  1.1  mrg 		is_openmp = 1;
   1458  1.1  mrg 	    }
   1459  1.1  mrg 	  if (omp_acc_err_loc.nextc != gfc_current_locus.nextc
   1460  1.1  mrg 	      || omp_acc_err_loc.lb != gfc_current_locus.lb)
   1461  1.1  mrg 	    gfc_error_now (is_openmp
   1462  1.1  mrg 			   ? G_("Wrong OpenACC continuation at %C: "
   1463  1.1  mrg 				"expected !$ACC, got !$OMP")
   1464  1.1  mrg 			   : G_("Wrong OpenMP continuation at %C: "
   1465  1.1  mrg 				"expected !$OMP, got !$ACC"));
   1466  1.1  mrg 	  omp_acc_err_loc = gfc_current_locus;
   1467  1.1  mrg 	  goto not_continuation;
   1468  1.1  mrg 	}
   1469  1.1  mrg 
   1470  1.1  mrg       if (c != '&')
   1471  1.1  mrg 	{
   1472  1.1  mrg 	  if (in_string && gfc_current_locus.nextc)
   1473  1.1  mrg 	    {
   1474  1.1  mrg 	      gfc_current_locus.nextc--;
   1475  1.1  mrg 	      if (warn_ampersand && in_string == INSTRING_WARN)
   1476  1.1  mrg 		gfc_warning (OPT_Wampersand,
   1477  1.1  mrg 			     "Missing %<&%> in continued character "
   1478  1.1  mrg 			     "constant at %C");
   1479  1.1  mrg 	    }
   1480  1.1  mrg 	  else if (!in_string && (c == '\'' || c == '"'))
   1481  1.1  mrg 	      goto done;
   1482  1.1  mrg 	  /* Both !$omp and !$ -fopenmp continuation lines have & on the
   1483  1.1  mrg 	     continuation line only optionally.  */
   1484  1.1  mrg 	  else if (openmp_flag || openacc_flag || openmp_cond_flag)
   1485  1.1  mrg 	    {
   1486  1.1  mrg 	      if (gfc_current_locus.nextc)
   1487  1.1  mrg 		  gfc_current_locus.nextc--;
   1488  1.1  mrg 	    }
   1489  1.1  mrg 	  else
   1490  1.1  mrg 	    {
   1491  1.1  mrg 	      c = ' ';
   1492  1.1  mrg 	      gfc_current_locus = old_loc;
   1493  1.1  mrg 	      goto done;
   1494  1.1  mrg 	    }
   1495  1.1  mrg 	}
   1496  1.1  mrg     }
   1497  1.1  mrg   else /* Fixed form.  */
   1498  1.1  mrg     {
   1499  1.1  mrg       /* Fixed form continuation.  */
   1500  1.1  mrg       if (in_string != INSTRING_WARN && c == '!')
   1501  1.1  mrg 	{
   1502  1.1  mrg 	  /* Skip comment at end of line.  */
   1503  1.1  mrg 	  do
   1504  1.1  mrg 	    {
   1505  1.1  mrg 	      c = next_char ();
   1506  1.1  mrg 	    }
   1507  1.1  mrg 	  while (c != '\n');
   1508  1.1  mrg 
   1509  1.1  mrg 	  /* Avoid truncation warnings for comment ending lines.  */
   1510  1.1  mrg 	  gfc_current_locus.lb->truncated = 0;
   1511  1.1  mrg 	}
   1512  1.1  mrg 
   1513  1.1  mrg       if (c != '\n')
   1514  1.1  mrg 	goto done;
   1515  1.1  mrg 
   1516  1.1  mrg       /* Check to see if the continuation line was truncated.  */
   1517  1.1  mrg       if (warn_line_truncation && gfc_current_locus.lb != NULL
   1518  1.1  mrg 	  && gfc_current_locus.lb->truncated)
   1519  1.1  mrg 	{
   1520  1.1  mrg 	  gfc_current_locus.lb->truncated = 0;
   1521  1.1  mrg 	  gfc_warning_now (OPT_Wline_truncation,
   1522  1.1  mrg 			   "Line truncated at %L", &gfc_current_locus);
   1523  1.1  mrg 	}
   1524  1.1  mrg 
   1525  1.1  mrg       if (flag_openmp)
   1526  1.1  mrg 	prev_openmp_flag = openmp_flag;
   1527  1.1  mrg       if (flag_openacc)
   1528  1.1  mrg 	prev_openacc_flag = openacc_flag;
   1529  1.1  mrg 
   1530  1.1  mrg       /* This can happen if the input file changed or via cpp's #line
   1531  1.1  mrg 	 without getting reset (e.g. via input_stmt). It also happens
   1532  1.1  mrg 	 when pre-including files via -fpre-include=.  */
   1533  1.1  mrg       if (continue_count == 0
   1534  1.1  mrg 	  && gfc_current_locus.lb
   1535  1.1  mrg 	  && continue_line > gfc_linebuf_linenum (gfc_current_locus.lb) + 1)
   1536  1.1  mrg 	continue_line = gfc_linebuf_linenum (gfc_current_locus.lb) + 1;
   1537  1.1  mrg 
   1538  1.1  mrg       continue_flag = 1;
   1539  1.1  mrg       old_loc = gfc_current_locus;
   1540  1.1  mrg 
   1541  1.1  mrg       gfc_advance_line ();
   1542  1.1  mrg       skip_fixed_comments ();
   1543  1.1  mrg 
   1544  1.1  mrg       /* See if this line is a continuation line.  */
   1545  1.1  mrg       if (flag_openmp && openmp_flag != prev_openmp_flag && !openacc_flag)
   1546  1.1  mrg 	{
   1547  1.1  mrg 	  openmp_flag = prev_openmp_flag;
   1548  1.1  mrg 	  goto not_continuation;
   1549  1.1  mrg 	}
   1550  1.1  mrg       if (flag_openacc && openacc_flag != prev_openacc_flag && !openmp_flag)
   1551  1.1  mrg 	{
   1552  1.1  mrg 	  openacc_flag = prev_openacc_flag;
   1553  1.1  mrg 	  goto not_continuation;
   1554  1.1  mrg 	}
   1555  1.1  mrg 
   1556  1.1  mrg       /* In case we have an OpenMP directive continued by OpenACC
   1557  1.1  mrg 	 sentinel, or vice versa, we get both openmp_flag and
   1558  1.1  mrg 	 openacc_flag on.  */
   1559  1.1  mrg       if (openacc_flag && openmp_flag)
   1560  1.1  mrg 	{
   1561  1.1  mrg 	  int is_openmp = 0;
   1562  1.1  mrg 	  for (i = 0; i < 5; i++)
   1563  1.1  mrg 	    {
   1564  1.1  mrg 	      c = next_char ();
   1565  1.1  mrg 	      if (gfc_wide_tolower (c) != (unsigned char) "*$acc"[i])
   1566  1.1  mrg 		is_openmp = 1;
   1567  1.1  mrg 	    }
   1568  1.1  mrg 	  if (omp_acc_err_loc.nextc != gfc_current_locus.nextc
   1569  1.1  mrg 	      || omp_acc_err_loc.lb != gfc_current_locus.lb)
   1570  1.1  mrg 	    gfc_error_now (is_openmp
   1571  1.1  mrg 			   ? G_("Wrong OpenACC continuation at %C: "
   1572  1.1  mrg 				"expected !$ACC, got !$OMP")
   1573  1.1  mrg 			   : G_("Wrong OpenMP continuation at %C: "
   1574  1.1  mrg 				"expected !$OMP, got !$ACC"));
   1575  1.1  mrg 	  omp_acc_err_loc = gfc_current_locus;
   1576  1.1  mrg 	  goto not_continuation;
   1577  1.1  mrg 	}
   1578  1.1  mrg       else if (!openmp_flag && !openacc_flag)
   1579  1.1  mrg 	for (i = 0; i < 5; i++)
   1580  1.1  mrg 	  {
   1581  1.1  mrg 	    c = next_char ();
   1582  1.1  mrg 	    if (c != ' ')
   1583  1.1  mrg 	      goto not_continuation;
   1584  1.1  mrg 	  }
   1585  1.1  mrg       else if (openmp_flag)
   1586  1.1  mrg 	for (i = 0; i < 5; i++)
   1587  1.1  mrg 	  {
   1588  1.1  mrg 	    c = next_char ();
   1589  1.1  mrg 	    if (gfc_wide_tolower (c) != (unsigned char) "*$omp"[i])
   1590  1.1  mrg 	      goto not_continuation;
   1591  1.1  mrg 	  }
   1592  1.1  mrg       else if (openacc_flag)
   1593  1.1  mrg 	for (i = 0; i < 5; i++)
   1594  1.1  mrg 	  {
   1595  1.1  mrg 	    c = next_char ();
   1596  1.1  mrg 	    if (gfc_wide_tolower (c) != (unsigned char) "*$acc"[i])
   1597  1.1  mrg 	      goto not_continuation;
   1598  1.1  mrg 	  }
   1599  1.1  mrg 
   1600  1.1  mrg       c = next_char ();
   1601  1.1  mrg       if (c == '0' || c == ' ' || c == '\n')
   1602  1.1  mrg 	goto not_continuation;
   1603  1.1  mrg 
   1604  1.1  mrg       /* We've got a continuation line.  If we are on the very next line after
   1605  1.1  mrg 	 the last continuation, increment the continuation line count and
   1606  1.1  mrg 	 check whether the limit has been exceeded.  */
   1607  1.1  mrg       if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1)
   1608  1.1  mrg 	{
   1609  1.1  mrg 	  if (++continue_count == gfc_option.max_continue_fixed)
   1610  1.1  mrg 	    {
   1611  1.1  mrg 	      if (gfc_notification_std (GFC_STD_GNU) || pedantic)
   1612  1.1  mrg 		gfc_warning (0, "Limit of %d continuations exceeded in "
   1613  1.1  mrg 			     "statement at %C",
   1614  1.1  mrg 			     gfc_option.max_continue_fixed);
   1615  1.1  mrg 	    }
   1616  1.1  mrg 	}
   1617  1.1  mrg 
   1618  1.1  mrg       if (gfc_current_locus.lb != NULL
   1619  1.1  mrg 	  && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
   1620  1.1  mrg 	continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
   1621  1.1  mrg     }
   1622  1.1  mrg 
   1623  1.1  mrg   /* Ready to read first character of continuation line, which might
   1624  1.1  mrg      be another continuation line!  */
   1625  1.1  mrg   goto restart;
   1626  1.1  mrg 
   1627  1.1  mrg not_continuation:
   1628  1.1  mrg   c = '\n';
   1629  1.1  mrg   gfc_current_locus = old_loc;
   1630  1.1  mrg   end_flag = 0;
   1631  1.1  mrg 
   1632  1.1  mrg done:
   1633  1.1  mrg   if (c == '\n')
   1634  1.1  mrg     continue_count = 0;
   1635  1.1  mrg   continue_flag = 0;
   1636  1.1  mrg   return c;
   1637  1.1  mrg }
   1638  1.1  mrg 
   1639  1.1  mrg 
   1640  1.1  mrg /* Get the next character of input, folded to lowercase.  In fixed
   1641  1.1  mrg    form mode, we also ignore spaces.  When matcher subroutines are
   1642  1.1  mrg    parsing character literals, they have to call
   1643  1.1  mrg    gfc_next_char_literal().  */
   1644  1.1  mrg 
   1645  1.1  mrg gfc_char_t
   1646  1.1  mrg gfc_next_char (void)
   1647  1.1  mrg {
   1648  1.1  mrg   gfc_char_t c;
   1649  1.1  mrg 
   1650  1.1  mrg   do
   1651  1.1  mrg     {
   1652  1.1  mrg       c = gfc_next_char_literal (NONSTRING);
   1653  1.1  mrg     }
   1654  1.1  mrg   while (gfc_current_form == FORM_FIXED && gfc_is_whitespace (c));
   1655  1.1  mrg 
   1656  1.1  mrg   return gfc_wide_tolower (c);
   1657  1.1  mrg }
   1658  1.1  mrg 
   1659  1.1  mrg char
   1660  1.1  mrg gfc_next_ascii_char (void)
   1661  1.1  mrg {
   1662  1.1  mrg   gfc_char_t c = gfc_next_char ();
   1663  1.1  mrg 
   1664  1.1  mrg   return (gfc_wide_fits_in_byte (c) ? (unsigned char) c
   1665  1.1  mrg 				    : (unsigned char) UCHAR_MAX);
   1666  1.1  mrg }
   1667  1.1  mrg 
   1668  1.1  mrg 
   1669  1.1  mrg gfc_char_t
   1670  1.1  mrg gfc_peek_char (void)
   1671  1.1  mrg {
   1672  1.1  mrg   locus old_loc;
   1673  1.1  mrg   gfc_char_t c;
   1674  1.1  mrg 
   1675  1.1  mrg   old_loc = gfc_current_locus;
   1676  1.1  mrg   c = gfc_next_char ();
   1677  1.1  mrg   gfc_current_locus = old_loc;
   1678  1.1  mrg 
   1679  1.1  mrg   return c;
   1680  1.1  mrg }
   1681  1.1  mrg 
   1682  1.1  mrg 
   1683  1.1  mrg char
   1684  1.1  mrg gfc_peek_ascii_char (void)
   1685  1.1  mrg {
   1686  1.1  mrg   gfc_char_t c = gfc_peek_char ();
   1687  1.1  mrg 
   1688  1.1  mrg   return (gfc_wide_fits_in_byte (c) ? (unsigned char) c
   1689  1.1  mrg 				    : (unsigned char) UCHAR_MAX);
   1690  1.1  mrg }
   1691  1.1  mrg 
   1692  1.1  mrg 
   1693  1.1  mrg /* Recover from an error.  We try to get past the current statement
   1694  1.1  mrg    and get lined up for the next.  The next statement follows a '\n'
   1695  1.1  mrg    or a ';'.  We also assume that we are not within a character
   1696  1.1  mrg    constant, and deal with finding a '\'' or '"'.  */
   1697  1.1  mrg 
   1698  1.1  mrg void
   1699  1.1  mrg gfc_error_recovery (void)
   1700  1.1  mrg {
   1701  1.1  mrg   gfc_char_t c, delim;
   1702  1.1  mrg 
   1703  1.1  mrg   if (gfc_at_eof ())
   1704  1.1  mrg     return;
   1705  1.1  mrg 
   1706  1.1  mrg   for (;;)
   1707  1.1  mrg     {
   1708  1.1  mrg       c = gfc_next_char ();
   1709  1.1  mrg       if (c == '\n' || c == ';')
   1710  1.1  mrg 	break;
   1711  1.1  mrg 
   1712  1.1  mrg       if (c != '\'' && c != '"')
   1713  1.1  mrg 	{
   1714  1.1  mrg 	  if (gfc_at_eof ())
   1715  1.1  mrg 	    break;
   1716  1.1  mrg 	  continue;
   1717  1.1  mrg 	}
   1718  1.1  mrg       delim = c;
   1719  1.1  mrg 
   1720  1.1  mrg       for (;;)
   1721  1.1  mrg 	{
   1722  1.1  mrg 	  c = next_char ();
   1723  1.1  mrg 
   1724  1.1  mrg 	  if (c == delim)
   1725  1.1  mrg 	    break;
   1726  1.1  mrg 	  if (c == '\n')
   1727  1.1  mrg 	    return;
   1728  1.1  mrg 	  if (c == '\\')
   1729  1.1  mrg 	    {
   1730  1.1  mrg 	      c = next_char ();
   1731  1.1  mrg 	      if (c == '\n')
   1732  1.1  mrg 		return;
   1733  1.1  mrg 	    }
   1734  1.1  mrg 	}
   1735  1.1  mrg       if (gfc_at_eof ())
   1736  1.1  mrg 	break;
   1737  1.1  mrg     }
   1738  1.1  mrg }
   1739  1.1  mrg 
   1740  1.1  mrg 
   1741  1.1  mrg /* Read ahead until the next character to be read is not whitespace.  */
   1742  1.1  mrg 
   1743  1.1  mrg void
   1744  1.1  mrg gfc_gobble_whitespace (void)
   1745  1.1  mrg {
   1746  1.1  mrg   static int linenum = 0;
   1747  1.1  mrg   locus old_loc;
   1748  1.1  mrg   gfc_char_t c;
   1749  1.1  mrg 
   1750  1.1  mrg   do
   1751  1.1  mrg     {
   1752  1.1  mrg       old_loc = gfc_current_locus;
   1753  1.1  mrg       c = gfc_next_char_literal (NONSTRING);
   1754  1.1  mrg       /* Issue a warning for nonconforming tabs.  We keep track of the line
   1755  1.1  mrg 	 number because the Fortran matchers will often back up and the same
   1756  1.1  mrg 	 line will be scanned multiple times.  */
   1757  1.1  mrg       if (warn_tabs && c == '\t')
   1758  1.1  mrg 	{
   1759  1.1  mrg 	  int cur_linenum = LOCATION_LINE (gfc_current_locus.lb->location);
   1760  1.1  mrg 	  if (cur_linenum != linenum)
   1761  1.1  mrg 	    {
   1762  1.1  mrg 	      linenum = cur_linenum;
   1763  1.1  mrg 	      gfc_warning_now (OPT_Wtabs, "Nonconforming tab character at %C");
   1764  1.1  mrg 	    }
   1765  1.1  mrg 	}
   1766  1.1  mrg     }
   1767  1.1  mrg   while (gfc_is_whitespace (c));
   1768  1.1  mrg 
   1769  1.1  mrg   if (!ISPRINT(c) && c != '\n' && last_error_char != gfc_current_locus.nextc)
   1770  1.1  mrg     {
   1771  1.1  mrg       char buf[20];
   1772  1.1  mrg       last_error_char = gfc_current_locus.nextc;
   1773  1.1  mrg       snprintf (buf, 20, "%2.2X", c);
   1774  1.1  mrg       gfc_error_now ("Invalid character 0x%s at %C", buf);
   1775  1.1  mrg     }
   1776  1.1  mrg 
   1777  1.1  mrg   gfc_current_locus = old_loc;
   1778  1.1  mrg }
   1779  1.1  mrg 
   1780  1.1  mrg 
   1781  1.1  mrg /* Load a single line into pbuf.
   1782  1.1  mrg 
   1783  1.1  mrg    If pbuf points to a NULL pointer, it is allocated.
   1784  1.1  mrg    We truncate lines that are too long, unless we're dealing with
   1785  1.1  mrg    preprocessor lines or if the option -ffixed-line-length-none is set,
   1786  1.1  mrg    in which case we reallocate the buffer to fit the entire line, if
   1787  1.1  mrg    need be.
   1788  1.1  mrg    In fixed mode, we expand a tab that occurs within the statement
   1789  1.1  mrg    label region to expand to spaces that leave the next character in
   1790  1.1  mrg    the source region.
   1791  1.1  mrg 
   1792  1.1  mrg    If first_char is not NULL, it's a pointer to a single char value holding
   1793  1.1  mrg    the first character of the line, which has already been read by the
   1794  1.1  mrg    caller.  This avoids the use of ungetc().
   1795  1.1  mrg 
   1796  1.1  mrg    load_line returns whether the line was truncated.
   1797  1.1  mrg 
   1798  1.1  mrg    NOTE: The error machinery isn't available at this point, so we can't
   1799  1.1  mrg 	 easily report line and column numbers consistent with other
   1800  1.1  mrg 	 parts of gfortran.  */
   1801  1.1  mrg 
   1802  1.1  mrg static int
   1803  1.1  mrg load_line (FILE *input, gfc_char_t **pbuf, int *pbuflen, const int *first_char)
   1804  1.1  mrg {
   1805  1.1  mrg   int c, maxlen, i, preprocessor_flag, buflen = *pbuflen;
   1806  1.1  mrg   int quoted = ' ', comment_ix = -1;
   1807  1.1  mrg   bool seen_comment = false;
   1808  1.1  mrg   bool first_comment = true;
   1809  1.1  mrg   bool trunc_flag = false;
   1810  1.1  mrg   bool seen_printable = false;
   1811  1.1  mrg   bool seen_ampersand = false;
   1812  1.1  mrg   bool found_tab = false;
   1813  1.1  mrg   bool warned_tabs = false;
   1814  1.1  mrg   gfc_char_t *buffer;
   1815  1.1  mrg 
   1816  1.1  mrg   /* Determine the maximum allowed line length.  */
   1817  1.1  mrg   if (gfc_current_form == FORM_FREE)
   1818  1.1  mrg     maxlen = flag_free_line_length;
   1819  1.1  mrg   else if (gfc_current_form == FORM_FIXED)
   1820  1.1  mrg     maxlen = flag_fixed_line_length;
   1821  1.1  mrg   else
   1822  1.1  mrg     maxlen = 72;
   1823  1.1  mrg 
   1824  1.1  mrg   if (*pbuf == NULL)
   1825  1.1  mrg     {
   1826  1.1  mrg       /* Allocate the line buffer, storing its length into buflen.
   1827  1.1  mrg 	 Note that if maxlen==0, indicating that arbitrary-length lines
   1828  1.1  mrg 	 are allowed, the buffer will be reallocated if this length is
   1829  1.1  mrg 	 insufficient; since 132 characters is the length of a standard
   1830  1.1  mrg 	 free-form line, we use that as a starting guess.  */
   1831  1.1  mrg       if (maxlen > 0)
   1832  1.1  mrg 	buflen = maxlen;
   1833  1.1  mrg       else
   1834  1.1  mrg 	buflen = 132;
   1835  1.1  mrg 
   1836  1.1  mrg       *pbuf = gfc_get_wide_string (buflen + 1);
   1837  1.1  mrg     }
   1838  1.1  mrg 
   1839  1.1  mrg   i = 0;
   1840  1.1  mrg   buffer = *pbuf;
   1841  1.1  mrg 
   1842  1.1  mrg   if (first_char)
   1843  1.1  mrg     c = *first_char;
   1844  1.1  mrg   else
   1845  1.1  mrg     c = getc (input);
   1846  1.1  mrg 
   1847  1.1  mrg   /* In order to not truncate preprocessor lines, we have to
   1848  1.1  mrg      remember that this is one.  */
   1849  1.1  mrg   preprocessor_flag = (c == '#');
   1850  1.1  mrg 
   1851  1.1  mrg   for (;;)
   1852  1.1  mrg     {
   1853  1.1  mrg       if (c == EOF)
   1854  1.1  mrg 	break;
   1855  1.1  mrg 
   1856  1.1  mrg       if (c == '\n')
   1857  1.1  mrg 	{
   1858  1.1  mrg 	  /* Check for illegal use of ampersand. See F95 Standard 3.3.1.3.  */
   1859  1.1  mrg 	  if (gfc_current_form == FORM_FREE
   1860  1.1  mrg 	      && !seen_printable && seen_ampersand)
   1861  1.1  mrg 	    {
   1862  1.1  mrg 	      if (pedantic)
   1863  1.1  mrg 		gfc_error_now ("%<&%> not allowed by itself in line %d",
   1864  1.1  mrg 			       current_file->line);
   1865  1.1  mrg 	      else
   1866  1.1  mrg 		gfc_warning_now (0, "%<&%> not allowed by itself in line %d",
   1867  1.1  mrg 				 current_file->line);
   1868  1.1  mrg 	    }
   1869  1.1  mrg 	  break;
   1870  1.1  mrg 	}
   1871  1.1  mrg 
   1872  1.1  mrg       if (c == '\r' || c == '\0')
   1873  1.1  mrg 	goto next_char;			/* Gobble characters.  */
   1874  1.1  mrg 
   1875  1.1  mrg       if (c == '&')
   1876  1.1  mrg 	{
   1877  1.1  mrg 	  if (seen_ampersand)
   1878  1.1  mrg 	    {
   1879  1.1  mrg 	      seen_ampersand = false;
   1880  1.1  mrg 	      seen_printable = true;
   1881  1.1  mrg 	    }
   1882  1.1  mrg 	  else
   1883  1.1  mrg 	    seen_ampersand = true;
   1884  1.1  mrg 	}
   1885  1.1  mrg 
   1886  1.1  mrg       if ((c != '&' && c != '!' && c != ' ') || (c == '!' && !seen_ampersand))
   1887  1.1  mrg 	seen_printable = true;
   1888  1.1  mrg 
   1889  1.1  mrg       /* Is this a fixed-form comment?  */
   1890  1.1  mrg       if (gfc_current_form == FORM_FIXED && i == 0
   1891  1.1  mrg 	  && (c == '*' || c == 'c' || c == 'C'
   1892  1.1  mrg 	      || (gfc_option.flag_d_lines != -1 && (c == 'd' || c == 'D'))))
   1893  1.1  mrg 	{
   1894  1.1  mrg 	  seen_comment = true;
   1895  1.1  mrg 	  comment_ix = i;
   1896  1.1  mrg 	}
   1897  1.1  mrg 
   1898  1.1  mrg       if (quoted == ' ')
   1899  1.1  mrg 	{
   1900  1.1  mrg 	  if (c == '\'' || c == '"')
   1901  1.1  mrg 	    quoted = c;
   1902  1.1  mrg 	}
   1903  1.1  mrg       else if (c == quoted)
   1904  1.1  mrg 	quoted = ' ';
   1905  1.1  mrg 
   1906  1.1  mrg       /* Is this a free-form comment?  */
   1907  1.1  mrg       if (c == '!' && quoted == ' ')
   1908  1.1  mrg 	{
   1909  1.1  mrg 	  if (seen_comment)
   1910  1.1  mrg 	    first_comment = false;
   1911  1.1  mrg 	  seen_comment = true;
   1912  1.1  mrg 	  comment_ix = i;
   1913  1.1  mrg 	}
   1914  1.1  mrg 
   1915  1.1  mrg       /* For truncation and tab warnings, set seen_comment to false if one has
   1916  1.1  mrg 	 either an OpenMP or OpenACC directive - or a !GCC$ attribute.  If
   1917  1.1  mrg 	 OpenMP is enabled, use '!$' as conditional compilation sentinel
   1918  1.1  mrg 	 and OpenMP directive ('!$omp').  */
   1919  1.1  mrg       if (seen_comment && first_comment && flag_openmp && comment_ix + 1 == i
   1920  1.1  mrg 	  && c == '$')
   1921  1.1  mrg 	first_comment = seen_comment = false;
   1922  1.1  mrg       if (seen_comment && first_comment && comment_ix + 4 == i)
   1923  1.1  mrg 	{
   1924  1.1  mrg 	  if (((*pbuf)[comment_ix+1] == 'g' || (*pbuf)[comment_ix+1] == 'G')
   1925  1.1  mrg 	      && ((*pbuf)[comment_ix+2] == 'c' || (*pbuf)[comment_ix+2] == 'C')
   1926  1.1  mrg 	      && ((*pbuf)[comment_ix+3] == 'c' || (*pbuf)[comment_ix+3] == 'C')
   1927  1.1  mrg 	      && c == '$')
   1928  1.1  mrg 	    first_comment = seen_comment = false;
   1929  1.1  mrg 	  if (flag_openacc
   1930  1.1  mrg 	      && (*pbuf)[comment_ix+1] == '$'
   1931  1.1  mrg 	      && ((*pbuf)[comment_ix+2] == 'a' || (*pbuf)[comment_ix+2] == 'A')
   1932  1.1  mrg 	      && ((*pbuf)[comment_ix+3] == 'c' || (*pbuf)[comment_ix+3] == 'C')
   1933  1.1  mrg 	      && (c == 'c' || c == 'C'))
   1934  1.1  mrg 	    first_comment = seen_comment = false;
   1935  1.1  mrg 	}
   1936  1.1  mrg 
   1937  1.1  mrg       /* Vendor extension: "<tab>1" marks a continuation line.  */
   1938  1.1  mrg       if (found_tab)
   1939  1.1  mrg 	{
   1940  1.1  mrg 	  found_tab = false;
   1941  1.1  mrg 	  if (c >= '1' && c <= '9')
   1942  1.1  mrg 	    {
   1943  1.1  mrg 	      *(buffer-1) = c;
   1944  1.1  mrg 	      goto next_char;
   1945  1.1  mrg 	    }
   1946  1.1  mrg 	}
   1947  1.1  mrg 
   1948  1.1  mrg       if (gfc_current_form == FORM_FIXED && c == '\t' && i < 6)
   1949  1.1  mrg 	{
   1950  1.1  mrg 	  found_tab = true;
   1951  1.1  mrg 
   1952  1.1  mrg 	  if (warn_tabs && seen_comment == 0 && !warned_tabs)
   1953  1.1  mrg 	    {
   1954  1.1  mrg 	      warned_tabs = true;
   1955  1.1  mrg 	      gfc_warning_now (OPT_Wtabs,
   1956  1.1  mrg 			       "Nonconforming tab character in column %d "
   1957  1.1  mrg 			       "of line %d", i + 1, current_file->line);
   1958  1.1  mrg 	    }
   1959  1.1  mrg 
   1960  1.1  mrg 	  while (i < 6)
   1961  1.1  mrg 	    {
   1962  1.1  mrg 	      *buffer++ = ' ';
   1963  1.1  mrg 	      i++;
   1964  1.1  mrg 	    }
   1965  1.1  mrg 
   1966  1.1  mrg 	  goto next_char;
   1967  1.1  mrg 	}
   1968  1.1  mrg 
   1969  1.1  mrg       *buffer++ = c;
   1970  1.1  mrg       i++;
   1971  1.1  mrg 
   1972  1.1  mrg       if (maxlen == 0 || preprocessor_flag)
   1973  1.1  mrg 	{
   1974  1.1  mrg 	  if (i >= buflen)
   1975  1.1  mrg 	    {
   1976  1.1  mrg 	      /* Reallocate line buffer to double size to hold the
   1977  1.1  mrg 		overlong line.  */
   1978  1.1  mrg 	      buflen = buflen * 2;
   1979  1.1  mrg 	      *pbuf = XRESIZEVEC (gfc_char_t, *pbuf, (buflen + 1));
   1980  1.1  mrg 	      buffer = (*pbuf) + i;
   1981  1.1  mrg 	    }
   1982  1.1  mrg 	}
   1983  1.1  mrg       else if (i >= maxlen)
   1984  1.1  mrg 	{
   1985  1.1  mrg 	  bool trunc_warn = true;
   1986  1.1  mrg 
   1987  1.1  mrg 	  /* Enhancement, if the very next non-space character is an ampersand
   1988  1.1  mrg 	     or comment that we would otherwise warn about, don't mark as
   1989  1.1  mrg 	     truncated.  */
   1990  1.1  mrg 
   1991  1.1  mrg 	  /* Truncate the rest of the line.  */
   1992  1.1  mrg 	  for (;;)
   1993  1.1  mrg 	    {
   1994  1.1  mrg 	      c = getc (input);
   1995  1.1  mrg 	      if (c == '\r' || c == ' ')
   1996  1.1  mrg 	        continue;
   1997  1.1  mrg 
   1998  1.1  mrg 	      if (c == '\n' || c == EOF)
   1999  1.1  mrg 		break;
   2000  1.1  mrg 
   2001  1.1  mrg 	      if (!trunc_warn && c != '!')
   2002  1.1  mrg 		trunc_warn = true;
   2003  1.1  mrg 
   2004  1.1  mrg 	      if (trunc_warn && ((gfc_current_form == FORM_FIXED && c == '&')
   2005  1.1  mrg 		  || c == '!'))
   2006  1.1  mrg 		trunc_warn = false;
   2007  1.1  mrg 
   2008  1.1  mrg 	      if (c == '!')
   2009  1.1  mrg 		seen_comment = 1;
   2010  1.1  mrg 
   2011  1.1  mrg 	      if (trunc_warn && !seen_comment)
   2012  1.1  mrg 		trunc_flag = 1;
   2013  1.1  mrg 	    }
   2014  1.1  mrg 
   2015  1.1  mrg 	  c = '\n';
   2016  1.1  mrg 	  continue;
   2017  1.1  mrg 	}
   2018  1.1  mrg 
   2019  1.1  mrg next_char:
   2020  1.1  mrg       c = getc (input);
   2021  1.1  mrg     }
   2022  1.1  mrg 
   2023  1.1  mrg   /* Pad lines to the selected line length in fixed form.  */
   2024  1.1  mrg   if (gfc_current_form == FORM_FIXED
   2025  1.1  mrg       && flag_fixed_line_length != 0
   2026  1.1  mrg       && flag_pad_source
   2027  1.1  mrg       && !preprocessor_flag
   2028  1.1  mrg       && c != EOF)
   2029  1.1  mrg     {
   2030  1.1  mrg       while (i++ < maxlen)
   2031  1.1  mrg 	*buffer++ = ' ';
   2032  1.1  mrg     }
   2033  1.1  mrg 
   2034  1.1  mrg   *buffer = '\0';
   2035  1.1  mrg   *pbuflen = buflen;
   2036  1.1  mrg 
   2037  1.1  mrg   return trunc_flag;
   2038  1.1  mrg }
   2039  1.1  mrg 
   2040  1.1  mrg 
   2041  1.1  mrg /* Get a gfc_file structure, initialize it and add it to
   2042  1.1  mrg    the file stack.  */
   2043  1.1  mrg 
   2044  1.1  mrg static gfc_file *
   2045  1.1  mrg get_file (const char *name, enum lc_reason reason)
   2046  1.1  mrg {
   2047  1.1  mrg   gfc_file *f;
   2048  1.1  mrg 
   2049  1.1  mrg   f = XCNEW (gfc_file);
   2050  1.1  mrg 
   2051  1.1  mrg   f->filename = xstrdup (name);
   2052  1.1  mrg 
   2053  1.1  mrg   f->next = file_head;
   2054  1.1  mrg   file_head = f;
   2055  1.1  mrg 
   2056  1.1  mrg   f->up = current_file;
   2057  1.1  mrg   if (current_file != NULL)
   2058  1.1  mrg     f->inclusion_line = current_file->line;
   2059  1.1  mrg 
   2060  1.1  mrg   linemap_add (line_table, reason, false, f->filename, 1);
   2061  1.1  mrg 
   2062  1.1  mrg   return f;
   2063  1.1  mrg }
   2064  1.1  mrg 
   2065  1.1  mrg 
   2066  1.1  mrg /* Deal with a line from the C preprocessor. The
   2067  1.1  mrg    initial octothorp has already been seen.  */
   2068  1.1  mrg 
   2069  1.1  mrg static void
   2070  1.1  mrg preprocessor_line (gfc_char_t *c)
   2071  1.1  mrg {
   2072  1.1  mrg   bool flag[5];
   2073  1.1  mrg   int i, line;
   2074  1.1  mrg   gfc_char_t *wide_filename;
   2075  1.1  mrg   gfc_file *f;
   2076  1.1  mrg   int escaped, unescape;
   2077  1.1  mrg   char *filename;
   2078  1.1  mrg 
   2079  1.1  mrg   c++;
   2080  1.1  mrg   while (*c == ' ' || *c == '\t')
   2081  1.1  mrg     c++;
   2082  1.1  mrg 
   2083  1.1  mrg   if (*c < '0' || *c > '9')
   2084  1.1  mrg     goto bad_cpp_line;
   2085  1.1  mrg 
   2086  1.1  mrg   line = wide_atoi (c);
   2087  1.1  mrg 
   2088  1.1  mrg   c = wide_strchr (c, ' ');
   2089  1.1  mrg   if (c == NULL)
   2090  1.1  mrg     {
   2091  1.1  mrg       /* No file name given.  Set new line number.  */
   2092  1.1  mrg       current_file->line = line;
   2093  1.1  mrg       return;
   2094  1.1  mrg     }
   2095  1.1  mrg 
   2096  1.1  mrg   /* Skip spaces.  */
   2097  1.1  mrg   while (*c == ' ' || *c == '\t')
   2098  1.1  mrg     c++;
   2099  1.1  mrg 
   2100  1.1  mrg   /* Skip quote.  */
   2101  1.1  mrg   if (*c != '"')
   2102  1.1  mrg     goto bad_cpp_line;
   2103  1.1  mrg   ++c;
   2104  1.1  mrg 
   2105  1.1  mrg   wide_filename = c;
   2106  1.1  mrg 
   2107  1.1  mrg   /* Make filename end at quote.  */
   2108  1.1  mrg   unescape = 0;
   2109  1.1  mrg   escaped = false;
   2110  1.1  mrg   while (*c && ! (!escaped && *c == '"'))
   2111  1.1  mrg     {
   2112  1.1  mrg       if (escaped)
   2113  1.1  mrg 	escaped = false;
   2114  1.1  mrg       else if (*c == '\\')
   2115  1.1  mrg 	{
   2116  1.1  mrg 	  escaped = true;
   2117  1.1  mrg 	  unescape++;
   2118  1.1  mrg 	}
   2119  1.1  mrg       ++c;
   2120  1.1  mrg     }
   2121  1.1  mrg 
   2122  1.1  mrg   if (! *c)
   2123  1.1  mrg     /* Preprocessor line has no closing quote.  */
   2124  1.1  mrg     goto bad_cpp_line;
   2125  1.1  mrg 
   2126  1.1  mrg   *c++ = '\0';
   2127  1.1  mrg 
   2128  1.1  mrg   /* Undo effects of cpp_quote_string.  */
   2129  1.1  mrg   if (unescape)
   2130  1.1  mrg     {
   2131  1.1  mrg       gfc_char_t *s = wide_filename;
   2132  1.1  mrg       gfc_char_t *d = gfc_get_wide_string (c - wide_filename - unescape);
   2133  1.1  mrg 
   2134  1.1  mrg       wide_filename = d;
   2135  1.1  mrg       while (*s)
   2136  1.1  mrg 	{
   2137  1.1  mrg 	  if (*s == '\\')
   2138  1.1  mrg 	    *d++ = *++s;
   2139  1.1  mrg 	  else
   2140  1.1  mrg 	    *d++ = *s;
   2141  1.1  mrg 	  s++;
   2142  1.1  mrg 	}
   2143  1.1  mrg       *d = '\0';
   2144  1.1  mrg     }
   2145  1.1  mrg 
   2146  1.1  mrg   /* Get flags.  */
   2147  1.1  mrg 
   2148  1.1  mrg   flag[1] = flag[2] = flag[3] = flag[4] = false;
   2149  1.1  mrg 
   2150  1.1  mrg   for (;;)
   2151  1.1  mrg     {
   2152  1.1  mrg       c = wide_strchr (c, ' ');
   2153  1.1  mrg       if (c == NULL)
   2154  1.1  mrg 	break;
   2155  1.1  mrg 
   2156  1.1  mrg       c++;
   2157  1.1  mrg       i = wide_atoi (c);
   2158  1.1  mrg 
   2159  1.1  mrg       if (i >= 1 && i <= 4)
   2160  1.1  mrg 	flag[i] = true;
   2161  1.1  mrg     }
   2162  1.1  mrg 
   2163  1.1  mrg   /* Convert the filename in wide characters into a filename in narrow
   2164  1.1  mrg      characters.  */
   2165  1.1  mrg   filename = gfc_widechar_to_char (wide_filename, -1);
   2166  1.1  mrg 
   2167  1.1  mrg   /* Interpret flags.  */
   2168  1.1  mrg 
   2169  1.1  mrg   if (flag[1]) /* Starting new file.  */
   2170  1.1  mrg     {
   2171  1.1  mrg       f = get_file (filename, LC_RENAME);
   2172  1.1  mrg       add_file_change (f->filename, f->inclusion_line);
   2173  1.1  mrg       current_file = f;
   2174  1.1  mrg     }
   2175  1.1  mrg 
   2176  1.1  mrg   if (flag[2]) /* Ending current file.  */
   2177  1.1  mrg     {
   2178  1.1  mrg       if (!current_file->up
   2179  1.1  mrg 	  || filename_cmp (current_file->up->filename, filename) != 0)
   2180  1.1  mrg 	{
   2181  1.1  mrg 	  linemap_line_start (line_table, current_file->line, 80);
   2182  1.1  mrg 	  /* ??? One could compute the exact column where the filename
   2183  1.1  mrg 	     starts and compute the exact location here.  */
   2184  1.1  mrg 	  gfc_warning_now_at (linemap_position_for_column (line_table, 1),
   2185  1.1  mrg 			      0, "file %qs left but not entered",
   2186  1.1  mrg 			      filename);
   2187  1.1  mrg 	  current_file->line++;
   2188  1.1  mrg 	  if (unescape)
   2189  1.1  mrg 	    free (wide_filename);
   2190  1.1  mrg 	  free (filename);
   2191  1.1  mrg 	  return;
   2192  1.1  mrg 	}
   2193  1.1  mrg 
   2194  1.1  mrg       add_file_change (NULL, line);
   2195  1.1  mrg       current_file = current_file->up;
   2196  1.1  mrg       linemap_add (line_table, LC_RENAME, false, current_file->filename,
   2197  1.1  mrg 		   current_file->line);
   2198  1.1  mrg     }
   2199  1.1  mrg 
   2200  1.1  mrg   /* The name of the file can be a temporary file produced by
   2201  1.1  mrg      cpp. Replace the name if it is different.  */
   2202  1.1  mrg 
   2203  1.1  mrg   if (filename_cmp (current_file->filename, filename) != 0)
   2204  1.1  mrg     {
   2205  1.1  mrg        /* FIXME: we leak the old filename because a pointer to it may be stored
   2206  1.1  mrg           in the linemap.  Alternative could be using GC or updating linemap to
   2207  1.1  mrg           point to the new name, but there is no API for that currently.  */
   2208  1.1  mrg       current_file->filename = xstrdup (filename);
   2209  1.1  mrg 
   2210  1.1  mrg       /* We need to tell the linemap API that the filename changed.  Just
   2211  1.1  mrg 	 changing current_file is insufficient.  */
   2212  1.1  mrg       linemap_add (line_table, LC_RENAME, false, current_file->filename, line);
   2213  1.1  mrg     }
   2214  1.1  mrg 
   2215  1.1  mrg   /* Set new line number.  */
   2216  1.1  mrg   current_file->line = line;
   2217  1.1  mrg   if (unescape)
   2218  1.1  mrg     free (wide_filename);
   2219  1.1  mrg   free (filename);
   2220  1.1  mrg   return;
   2221  1.1  mrg 
   2222  1.1  mrg  bad_cpp_line:
   2223  1.1  mrg   linemap_line_start (line_table, current_file->line, 80);
   2224  1.1  mrg   /* ??? One could compute the exact column where the directive
   2225  1.1  mrg      starts and compute the exact location here.  */
   2226  1.1  mrg   gfc_warning_now_at (linemap_position_for_column (line_table, 2), 0,
   2227  1.1  mrg 		      "Illegal preprocessor directive");
   2228  1.1  mrg   current_file->line++;
   2229  1.1  mrg }
   2230  1.1  mrg 
   2231  1.1  mrg 
   2232  1.1  mrg static void load_file (const char *, const char *, bool);
   2233  1.1  mrg 
   2234  1.1  mrg /* include_line()-- Checks a line buffer to see if it is an include
   2235  1.1  mrg    line.  If so, we call load_file() recursively to load the included
   2236  1.1  mrg    file.  We never return a syntax error because a statement like
   2237  1.1  mrg    "include = 5" is perfectly legal.  We return 0 if no include was
   2238  1.1  mrg    processed, 1 if we matched an include or -1 if include was
   2239  1.1  mrg    partially processed, but will need continuation lines.  */
   2240  1.1  mrg 
   2241  1.1  mrg static int
   2242  1.1  mrg include_line (gfc_char_t *line)
   2243  1.1  mrg {
   2244  1.1  mrg   gfc_char_t quote, *c, *begin, *stop;
   2245  1.1  mrg   char *filename;
   2246  1.1  mrg   const char *include = "include";
   2247  1.1  mrg   bool allow_continuation = flag_dec_include;
   2248  1.1  mrg   int i;
   2249  1.1  mrg 
   2250  1.1  mrg   c = line;
   2251  1.1  mrg 
   2252  1.1  mrg   if (flag_openmp || flag_openmp_simd)
   2253  1.1  mrg     {
   2254  1.1  mrg       if (gfc_current_form == FORM_FREE)
   2255  1.1  mrg 	{
   2256  1.1  mrg 	  while (*c == ' ' || *c == '\t')
   2257  1.1  mrg 	    c++;
   2258  1.1  mrg 	  if (*c == '!' && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
   2259  1.1  mrg 	    c += 3;
   2260  1.1  mrg 	}
   2261  1.1  mrg       else
   2262  1.1  mrg 	{
   2263  1.1  mrg 	  if ((*c == '!' || *c == 'c' || *c == 'C' || *c == '*')
   2264  1.1  mrg 	      && c[1] == '$' && c[2] == ' ')
   2265  1.1  mrg 	    c += 3;
   2266  1.1  mrg 	}
   2267  1.1  mrg     }
   2268  1.1  mrg 
   2269  1.1  mrg   if (gfc_current_form == FORM_FREE)
   2270  1.1  mrg     {
   2271  1.1  mrg       while (*c == ' ' || *c == '\t')
   2272  1.1  mrg 	c++;
   2273  1.1  mrg       if (gfc_wide_strncasecmp (c, "include", 7))
   2274  1.1  mrg 	{
   2275  1.1  mrg 	  if (!allow_continuation)
   2276  1.1  mrg 	    return 0;
   2277  1.1  mrg 	  for (i = 0; i < 7; ++i)
   2278  1.1  mrg 	    {
   2279  1.1  mrg 	      gfc_char_t c1 = gfc_wide_tolower (*c);
   2280  1.1  mrg 	      if (c1 != (unsigned char) include[i])
   2281  1.1  mrg 		break;
   2282  1.1  mrg 	      c++;
   2283  1.1  mrg 	    }
   2284  1.1  mrg 	  if (i == 0 || *c != '&')
   2285  1.1  mrg 	    return 0;
   2286  1.1  mrg 	  c++;
   2287  1.1  mrg 	  while (*c == ' ' || *c == '\t')
   2288  1.1  mrg 	    c++;
   2289  1.1  mrg 	  if (*c == '\0' || *c == '!')
   2290  1.1  mrg 	    return -1;
   2291  1.1  mrg 	  return 0;
   2292  1.1  mrg 	}
   2293  1.1  mrg 
   2294  1.1  mrg       c += 7;
   2295  1.1  mrg     }
   2296  1.1  mrg   else
   2297  1.1  mrg     {
   2298  1.1  mrg       while (*c == ' ' || *c == '\t')
   2299  1.1  mrg 	c++;
   2300  1.1  mrg       if (flag_dec_include && *c == '0' && c - line == 5)
   2301  1.1  mrg 	{
   2302  1.1  mrg 	  c++;
   2303  1.1  mrg 	  while (*c == ' ' || *c == '\t')
   2304  1.1  mrg 	    c++;
   2305  1.1  mrg 	}
   2306  1.1  mrg       if (c - line < 6)
   2307  1.1  mrg 	allow_continuation = false;
   2308  1.1  mrg       for (i = 0; i < 7; ++i)
   2309  1.1  mrg 	{
   2310  1.1  mrg 	  gfc_char_t c1 = gfc_wide_tolower (*c);
   2311  1.1  mrg 	  if (c1 != (unsigned char) include[i])
   2312  1.1  mrg 	    break;
   2313  1.1  mrg 	  c++;
   2314  1.1  mrg 	  while (*c == ' ' || *c == '\t')
   2315  1.1  mrg 	    c++;
   2316  1.1  mrg 	}
   2317  1.1  mrg       if (!allow_continuation)
   2318  1.1  mrg 	{
   2319  1.1  mrg 	  if (i != 7)
   2320  1.1  mrg 	    return 0;
   2321  1.1  mrg 	}
   2322  1.1  mrg       else if (i != 7)
   2323  1.1  mrg 	{
   2324  1.1  mrg 	  if (i == 0)
   2325  1.1  mrg 	    return 0;
   2326  1.1  mrg 
   2327  1.1  mrg 	  /* At the end of line or comment this might be continued.  */
   2328  1.1  mrg 	  if (*c == '\0' || *c == '!')
   2329  1.1  mrg 	    return -1;
   2330  1.1  mrg 
   2331  1.1  mrg 	  return 0;
   2332  1.1  mrg 	}
   2333  1.1  mrg     }
   2334  1.1  mrg 
   2335  1.1  mrg   while (*c == ' ' || *c == '\t')
   2336  1.1  mrg     c++;
   2337  1.1  mrg 
   2338  1.1  mrg   /* Find filename between quotes.  */
   2339  1.1  mrg 
   2340  1.1  mrg   quote = *c++;
   2341  1.1  mrg   if (quote != '"' && quote != '\'')
   2342  1.1  mrg     {
   2343  1.1  mrg       if (allow_continuation)
   2344  1.1  mrg 	{
   2345  1.1  mrg 	  if (gfc_current_form == FORM_FREE)
   2346  1.1  mrg 	    {
   2347  1.1  mrg 	      if (quote == '&')
   2348  1.1  mrg 		{
   2349  1.1  mrg 		  while (*c == ' ' || *c == '\t')
   2350  1.1  mrg 		    c++;
   2351  1.1  mrg 		  if (*c == '\0' || *c == '!')
   2352  1.1  mrg 		    return -1;
   2353  1.1  mrg 		}
   2354  1.1  mrg 	    }
   2355  1.1  mrg 	  else if (quote == '\0' || quote == '!')
   2356  1.1  mrg 	    return -1;
   2357  1.1  mrg 	}
   2358  1.1  mrg       return 0;
   2359  1.1  mrg     }
   2360  1.1  mrg 
   2361  1.1  mrg   begin = c;
   2362  1.1  mrg 
   2363  1.1  mrg   bool cont = false;
   2364  1.1  mrg   while (*c != quote && *c != '\0')
   2365  1.1  mrg     {
   2366  1.1  mrg       if (allow_continuation && gfc_current_form == FORM_FREE)
   2367  1.1  mrg 	{
   2368  1.1  mrg 	  if (*c == '&')
   2369  1.1  mrg 	    cont = true;
   2370  1.1  mrg 	  else if (*c != ' ' && *c != '\t')
   2371  1.1  mrg 	    cont = false;
   2372  1.1  mrg 	}
   2373  1.1  mrg       c++;
   2374  1.1  mrg     }
   2375  1.1  mrg 
   2376  1.1  mrg   if (*c == '\0')
   2377  1.1  mrg     {
   2378  1.1  mrg       if (allow_continuation
   2379  1.1  mrg 	  && (cont || gfc_current_form != FORM_FREE))
   2380  1.1  mrg 	return -1;
   2381  1.1  mrg       return 0;
   2382  1.1  mrg     }
   2383  1.1  mrg 
   2384  1.1  mrg   stop = c++;
   2385  1.1  mrg 
   2386  1.1  mrg   while (*c == ' ' || *c == '\t')
   2387  1.1  mrg     c++;
   2388  1.1  mrg 
   2389  1.1  mrg   if (*c != '\0' && *c != '!')
   2390  1.1  mrg     return 0;
   2391  1.1  mrg 
   2392  1.1  mrg   /* We have an include line at this point.  */
   2393  1.1  mrg 
   2394  1.1  mrg   *stop = '\0'; /* It's ok to trash the buffer, as this line won't be
   2395  1.1  mrg 		   read by anything else.  */
   2396  1.1  mrg 
   2397  1.1  mrg   filename = gfc_widechar_to_char (begin, -1);
   2398  1.1  mrg   load_file (filename, NULL, false);
   2399  1.1  mrg   free (filename);
   2400  1.1  mrg   return 1;
   2401  1.1  mrg }
   2402  1.1  mrg 
   2403  1.1  mrg /* Similarly, but try to parse an INCLUDE statement, using gfc_next_char etc.
   2404  1.1  mrg    APIs.  Return 1 if recognized as valid INCLUDE statement and load_file has
   2405  1.1  mrg    been called, 0 if it is not a valid INCLUDE statement and -1 if eof has
   2406  1.1  mrg    been encountered while parsing it.  */
   2407  1.1  mrg static int
   2408  1.1  mrg include_stmt (gfc_linebuf *b)
   2409  1.1  mrg {
   2410  1.1  mrg   int ret = 0, i, length;
   2411  1.1  mrg   const char *include = "include";
   2412  1.1  mrg   gfc_char_t c, quote = 0;
   2413  1.1  mrg   locus str_locus;
   2414  1.1  mrg   char *filename;
   2415  1.1  mrg 
   2416  1.1  mrg   continue_flag = 0;
   2417  1.1  mrg   end_flag = 0;
   2418  1.1  mrg   gcc_attribute_flag = 0;
   2419  1.1  mrg   openmp_flag = 0;
   2420  1.1  mrg   openacc_flag = 0;
   2421  1.1  mrg   continue_count = 0;
   2422  1.1  mrg   continue_line = 0;
   2423  1.1  mrg   gfc_current_locus.lb = b;
   2424  1.1  mrg   gfc_current_locus.nextc = b->line;
   2425  1.1  mrg 
   2426  1.1  mrg   gfc_skip_comments ();
   2427  1.1  mrg   gfc_gobble_whitespace ();
   2428  1.1  mrg 
   2429  1.1  mrg   for (i = 0; i < 7; i++)
   2430  1.1  mrg     {
   2431  1.1  mrg       c = gfc_next_char ();
   2432  1.1  mrg       if (c != (unsigned char) include[i])
   2433  1.1  mrg 	{
   2434  1.1  mrg 	  if (gfc_current_form == FORM_FIXED
   2435  1.1  mrg 	      && i == 0
   2436  1.1  mrg 	      && c == '0'
   2437  1.1  mrg 	      && gfc_current_locus.nextc == b->line + 6)
   2438  1.1  mrg 	    {
   2439  1.1  mrg 	      gfc_gobble_whitespace ();
   2440  1.1  mrg 	      i--;
   2441  1.1  mrg 	      continue;
   2442  1.1  mrg 	    }
   2443  1.1  mrg 	  gcc_assert (i != 0);
   2444  1.1  mrg 	  if (c == '\n')
   2445  1.1  mrg 	    {
   2446  1.1  mrg 	      gfc_advance_line ();
   2447  1.1  mrg 	      gfc_skip_comments ();
   2448  1.1  mrg 	      if (gfc_at_eof ())
   2449  1.1  mrg 		ret = -1;
   2450  1.1  mrg 	    }
   2451  1.1  mrg 	  goto do_ret;
   2452  1.1  mrg 	}
   2453  1.1  mrg     }
   2454  1.1  mrg   gfc_gobble_whitespace ();
   2455  1.1  mrg 
   2456  1.1  mrg   c = gfc_next_char ();
   2457  1.1  mrg   if (c == '\'' || c == '"')
   2458  1.1  mrg     quote = c;
   2459  1.1  mrg   else
   2460  1.1  mrg     {
   2461  1.1  mrg       if (c == '\n')
   2462  1.1  mrg 	{
   2463  1.1  mrg 	  gfc_advance_line ();
   2464  1.1  mrg 	  gfc_skip_comments ();
   2465  1.1  mrg 	  if (gfc_at_eof ())
   2466  1.1  mrg 	    ret = -1;
   2467  1.1  mrg 	}
   2468  1.1  mrg       goto do_ret;
   2469  1.1  mrg     }
   2470  1.1  mrg 
   2471  1.1  mrg   str_locus = gfc_current_locus;
   2472  1.1  mrg   length = 0;
   2473  1.1  mrg   do
   2474  1.1  mrg     {
   2475  1.1  mrg       c = gfc_next_char_literal (INSTRING_NOWARN);
   2476  1.1  mrg       if (c == quote)
   2477  1.1  mrg 	break;
   2478  1.1  mrg       if (c == '\n')
   2479  1.1  mrg 	{
   2480  1.1  mrg 	  gfc_advance_line ();
   2481  1.1  mrg 	  gfc_skip_comments ();
   2482  1.1  mrg 	  if (gfc_at_eof ())
   2483  1.1  mrg 	    ret = -1;
   2484  1.1  mrg 	  goto do_ret;
   2485  1.1  mrg 	}
   2486  1.1  mrg       length++;
   2487  1.1  mrg     }
   2488  1.1  mrg   while (1);
   2489  1.1  mrg 
   2490  1.1  mrg   gfc_gobble_whitespace ();
   2491  1.1  mrg   c = gfc_next_char ();
   2492  1.1  mrg   if (c != '\n')
   2493  1.1  mrg     goto do_ret;
   2494  1.1  mrg 
   2495  1.1  mrg   gfc_current_locus = str_locus;
   2496  1.1  mrg   ret = 1;
   2497  1.1  mrg   filename = XNEWVEC (char, length + 1);
   2498  1.1  mrg   for (i = 0; i < length; i++)
   2499  1.1  mrg     {
   2500  1.1  mrg       c = gfc_next_char_literal (INSTRING_WARN);
   2501  1.1  mrg       gcc_assert (gfc_wide_fits_in_byte (c));
   2502  1.1  mrg       filename[i] = (unsigned char) c;
   2503  1.1  mrg     }
   2504  1.1  mrg   filename[length] = '\0';
   2505  1.1  mrg   load_file (filename, NULL, false);
   2506  1.1  mrg   free (filename);
   2507  1.1  mrg 
   2508  1.1  mrg do_ret:
   2509  1.1  mrg   continue_flag = 0;
   2510  1.1  mrg   end_flag = 0;
   2511  1.1  mrg   gcc_attribute_flag = 0;
   2512  1.1  mrg   openmp_flag = 0;
   2513  1.1  mrg   openacc_flag = 0;
   2514  1.1  mrg   continue_count = 0;
   2515  1.1  mrg   continue_line = 0;
   2516  1.1  mrg   memset (&gfc_current_locus, '\0', sizeof (locus));
   2517  1.1  mrg   memset (&openmp_locus, '\0', sizeof (locus));
   2518  1.1  mrg   memset (&openacc_locus, '\0', sizeof (locus));
   2519  1.1  mrg   memset (&gcc_attribute_locus, '\0', sizeof (locus));
   2520  1.1  mrg   return ret;
   2521  1.1  mrg }
   2522  1.1  mrg 
   2523  1.1  mrg 
   2524  1.1  mrg 
   2525  1.1  mrg /* Load a file into memory by calling load_line until the file ends.  */
   2526  1.1  mrg 
   2527  1.1  mrg static void
   2528  1.1  mrg load_file (const char *realfilename, const char *displayedname, bool initial)
   2529  1.1  mrg {
   2530  1.1  mrg   gfc_char_t *line;
   2531  1.1  mrg   gfc_linebuf *b, *include_b = NULL;
   2532  1.1  mrg   gfc_file *f;
   2533  1.1  mrg   FILE *input;
   2534  1.1  mrg   int len, line_len;
   2535  1.1  mrg   bool first_line;
   2536  1.1  mrg   struct stat st;
   2537  1.1  mrg   int stat_result;
   2538  1.1  mrg   const char *filename;
   2539  1.1  mrg   /* If realfilename and displayedname are different and non-null then
   2540  1.1  mrg      surely realfilename is the preprocessed form of
   2541  1.1  mrg      displayedname.  */
   2542  1.1  mrg   bool preprocessed_p = (realfilename && displayedname
   2543  1.1  mrg 			 && strcmp (realfilename, displayedname));
   2544  1.1  mrg 
   2545  1.1  mrg   filename = displayedname ? displayedname : realfilename;
   2546  1.1  mrg 
   2547  1.1  mrg   for (f = current_file; f; f = f->up)
   2548  1.1  mrg     if (filename_cmp (filename, f->filename) == 0)
   2549  1.1  mrg       fatal_error (linemap_line_start (line_table, current_file->line, 0),
   2550  1.1  mrg 		   "File %qs is being included recursively", filename);
   2551  1.1  mrg   if (initial)
   2552  1.1  mrg     {
   2553  1.1  mrg       if (gfc_src_file)
   2554  1.1  mrg 	{
   2555  1.1  mrg 	  input = gfc_src_file;
   2556  1.1  mrg 	  gfc_src_file = NULL;
   2557  1.1  mrg 	}
   2558  1.1  mrg       else
   2559  1.1  mrg 	input = gfc_open_file (realfilename);
   2560  1.1  mrg 
   2561  1.1  mrg       if (input == NULL)
   2562  1.1  mrg 	gfc_fatal_error ("Cannot open file %qs", filename);
   2563  1.1  mrg     }
   2564  1.1  mrg   else
   2565  1.1  mrg     {
   2566  1.1  mrg       input = gfc_open_included_file (realfilename, false, false);
   2567  1.1  mrg       if (input == NULL)
   2568  1.1  mrg 	{
   2569  1.1  mrg 	  /* For -fpre-include file, current_file is NULL.  */
   2570  1.1  mrg 	  if (current_file)
   2571  1.1  mrg 	    fatal_error (linemap_line_start (line_table, current_file->line, 0),
   2572  1.1  mrg 			 "Cannot open included file %qs", filename);
   2573  1.1  mrg 	  else
   2574  1.1  mrg 	    gfc_fatal_error ("Cannot open pre-included file %qs", filename);
   2575  1.1  mrg 	}
   2576  1.1  mrg       stat_result = stat (realfilename, &st);
   2577  1.1  mrg       if (stat_result == 0 && !S_ISREG (st.st_mode))
   2578  1.1  mrg 	{
   2579  1.1  mrg 	  fclose (input);
   2580  1.1  mrg 	  if (current_file)
   2581  1.1  mrg 	    fatal_error (linemap_line_start (line_table, current_file->line, 0),
   2582  1.1  mrg 			 "Included file %qs is not a regular file", filename);
   2583  1.1  mrg 	  else
   2584  1.1  mrg 	    gfc_fatal_error ("Included file %qs is not a regular file", filename);
   2585  1.1  mrg 	}
   2586  1.1  mrg     }
   2587  1.1  mrg 
   2588  1.1  mrg   /* Load the file.
   2589  1.1  mrg 
   2590  1.1  mrg      A "non-initial" file means a file that is being included.  In
   2591  1.1  mrg      that case we are creating an LC_ENTER map.
   2592  1.1  mrg 
   2593  1.1  mrg      An "initial" file means a main file; one that is not included.
   2594  1.1  mrg      That file has already got at least one (surely more) line map(s)
   2595  1.1  mrg      created by gfc_init.  So the subsequent map created in that case
   2596  1.1  mrg      must have LC_RENAME reason.
   2597  1.1  mrg 
   2598  1.1  mrg      This latter case is not true for a preprocessed file.  In that
   2599  1.1  mrg      case, although the file is "initial", the line maps created by
   2600  1.1  mrg      gfc_init was used during the preprocessing of the file.  Now that
   2601  1.1  mrg      the preprocessing is over and we are being fed the result of that
   2602  1.1  mrg      preprocessing, we need to create a brand new line map for the
   2603  1.1  mrg      preprocessed file, so the reason is going to be LC_ENTER.  */
   2604  1.1  mrg 
   2605  1.1  mrg   f = get_file (filename, (initial && !preprocessed_p) ? LC_RENAME : LC_ENTER);
   2606  1.1  mrg   if (!initial)
   2607  1.1  mrg     add_file_change (f->filename, f->inclusion_line);
   2608  1.1  mrg   current_file = f;
   2609  1.1  mrg   current_file->line = 1;
   2610  1.1  mrg   line = NULL;
   2611  1.1  mrg   line_len = 0;
   2612  1.1  mrg   first_line = true;
   2613  1.1  mrg 
   2614  1.1  mrg   if (initial && gfc_src_preprocessor_lines[0])
   2615  1.1  mrg     {
   2616  1.1  mrg       preprocessor_line (gfc_src_preprocessor_lines[0]);
   2617  1.1  mrg       free (gfc_src_preprocessor_lines[0]);
   2618  1.1  mrg       gfc_src_preprocessor_lines[0] = NULL;
   2619  1.1  mrg       if (gfc_src_preprocessor_lines[1])
   2620  1.1  mrg 	{
   2621  1.1  mrg 	  preprocessor_line (gfc_src_preprocessor_lines[1]);
   2622  1.1  mrg 	  free (gfc_src_preprocessor_lines[1]);
   2623  1.1  mrg 	  gfc_src_preprocessor_lines[1] = NULL;
   2624  1.1  mrg 	}
   2625  1.1  mrg     }
   2626  1.1  mrg 
   2627  1.1  mrg   for (;;)
   2628  1.1  mrg     {
   2629  1.1  mrg       int trunc = load_line (input, &line, &line_len, NULL);
   2630  1.1  mrg       int inc_line;
   2631  1.1  mrg 
   2632  1.1  mrg       len = gfc_wide_strlen (line);
   2633  1.1  mrg       if (feof (input) && len == 0)
   2634  1.1  mrg 	break;
   2635  1.1  mrg 
   2636  1.1  mrg       /* If this is the first line of the file, it can contain a byte
   2637  1.1  mrg 	 order mark (BOM), which we will ignore:
   2638  1.1  mrg 	   FF FE is UTF-16 little endian,
   2639  1.1  mrg 	   FE FF is UTF-16 big endian,
   2640  1.1  mrg 	   EF BB BF is UTF-8.  */
   2641  1.1  mrg       if (first_line
   2642  1.1  mrg 	  && ((line_len >= 2 && line[0] == (unsigned char) '\xFF'
   2643  1.1  mrg 			     && line[1] == (unsigned char) '\xFE')
   2644  1.1  mrg 	      || (line_len >= 2 && line[0] == (unsigned char) '\xFE'
   2645  1.1  mrg 			        && line[1] == (unsigned char) '\xFF')
   2646  1.1  mrg 	      || (line_len >= 3 && line[0] == (unsigned char) '\xEF'
   2647  1.1  mrg 				&& line[1] == (unsigned char) '\xBB'
   2648  1.1  mrg 				&& line[2] == (unsigned char) '\xBF')))
   2649  1.1  mrg 	{
   2650  1.1  mrg 	  int n = line[1] == (unsigned char) '\xBB' ? 3 : 2;
   2651  1.1  mrg 	  gfc_char_t *new_char = gfc_get_wide_string (line_len);
   2652  1.1  mrg 
   2653  1.1  mrg 	  wide_strcpy (new_char, &line[n]);
   2654  1.1  mrg 	  free (line);
   2655  1.1  mrg 	  line = new_char;
   2656  1.1  mrg 	  len -= n;
   2657  1.1  mrg 	}
   2658  1.1  mrg 
   2659  1.1  mrg       /* There are three things this line can be: a line of Fortran
   2660  1.1  mrg 	 source, an include line or a C preprocessor directive.  */
   2661  1.1  mrg 
   2662  1.1  mrg       if (line[0] == '#')
   2663  1.1  mrg 	{
   2664  1.1  mrg 	  /* When -g3 is specified, it's possible that we emit #define
   2665  1.1  mrg 	     and #undef lines, which we need to pass to the middle-end
   2666  1.1  mrg 	     so that it can emit correct debug info.  */
   2667  1.1  mrg 	  if (debug_info_level == DINFO_LEVEL_VERBOSE
   2668  1.1  mrg 	      && (wide_strncmp (line, "#define ", 8) == 0
   2669  1.1  mrg 		  || wide_strncmp (line, "#undef ", 7) == 0))
   2670  1.1  mrg 	    ;
   2671  1.1  mrg 	  else
   2672  1.1  mrg 	    {
   2673  1.1  mrg 	      preprocessor_line (line);
   2674  1.1  mrg 	      continue;
   2675  1.1  mrg 	    }
   2676  1.1  mrg 	}
   2677  1.1  mrg 
   2678  1.1  mrg       /* Preprocessed files have preprocessor lines added before the byte
   2679  1.1  mrg 	 order mark, so first_line is not about the first line of the file
   2680  1.1  mrg 	 but the first line that's not a preprocessor line.  */
   2681  1.1  mrg       first_line = false;
   2682  1.1  mrg 
   2683  1.1  mrg       inc_line = include_line (line);
   2684  1.1  mrg       if (inc_line > 0)
   2685  1.1  mrg 	{
   2686  1.1  mrg 	  current_file->line++;
   2687  1.1  mrg 	  continue;
   2688  1.1  mrg 	}
   2689  1.1  mrg 
   2690  1.1  mrg       /* Add line.  */
   2691  1.1  mrg 
   2692  1.1  mrg       b = XCNEWVAR (gfc_linebuf, gfc_linebuf_header_size
   2693  1.1  mrg 		    + (len + 1) * sizeof (gfc_char_t));
   2694  1.1  mrg 
   2695  1.1  mrg 
   2696  1.1  mrg       b->location
   2697  1.1  mrg 	= linemap_line_start (line_table, current_file->line++, len);
   2698  1.1  mrg       /* ??? We add the location for the maximum column possible here,
   2699  1.1  mrg 	 because otherwise if the next call creates a new line-map, it
   2700  1.1  mrg 	 will not reserve space for any offset.  */
   2701  1.1  mrg       if (len > 0)
   2702  1.1  mrg 	linemap_position_for_column (line_table, len);
   2703  1.1  mrg 
   2704  1.1  mrg       b->file = current_file;
   2705  1.1  mrg       b->truncated = trunc;
   2706  1.1  mrg       wide_strcpy (b->line, line);
   2707  1.1  mrg 
   2708  1.1  mrg       if (line_head == NULL)
   2709  1.1  mrg 	line_head = b;
   2710  1.1  mrg       else
   2711  1.1  mrg 	line_tail->next = b;
   2712  1.1  mrg 
   2713  1.1  mrg       line_tail = b;
   2714  1.1  mrg 
   2715  1.1  mrg       while (file_changes_cur < file_changes_count)
   2716  1.1  mrg 	file_changes[file_changes_cur++].lb = b;
   2717  1.1  mrg 
   2718  1.1  mrg       if (flag_dec_include)
   2719  1.1  mrg 	{
   2720  1.1  mrg 	  if (include_b && b != include_b)
   2721  1.1  mrg 	    {
   2722  1.1  mrg 	      int inc_line2 = include_stmt (include_b);
   2723  1.1  mrg 	      if (inc_line2 == 0)
   2724  1.1  mrg 		include_b = NULL;
   2725  1.1  mrg 	      else if (inc_line2 > 0)
   2726  1.1  mrg 		{
   2727  1.1  mrg 		  do
   2728  1.1  mrg 		    {
   2729  1.1  mrg 		      if (gfc_current_form == FORM_FIXED)
   2730  1.1  mrg 			{
   2731  1.1  mrg 			  for (gfc_char_t *p = include_b->line; *p; p++)
   2732  1.1  mrg 			    *p = ' ';
   2733  1.1  mrg 			}
   2734  1.1  mrg 		      else
   2735  1.1  mrg 			include_b->line[0] = '\0';
   2736  1.1  mrg                       if (include_b == b)
   2737  1.1  mrg 			break;
   2738  1.1  mrg 		      include_b = include_b->next;
   2739  1.1  mrg 		    }
   2740  1.1  mrg 		  while (1);
   2741  1.1  mrg 		  include_b = NULL;
   2742  1.1  mrg 		}
   2743  1.1  mrg 	    }
   2744  1.1  mrg 	  if (inc_line == -1 && !include_b)
   2745  1.1  mrg 	    include_b = b;
   2746  1.1  mrg 	}
   2747  1.1  mrg     }
   2748  1.1  mrg 
   2749  1.1  mrg   /* Release the line buffer allocated in load_line.  */
   2750  1.1  mrg   free (line);
   2751  1.1  mrg 
   2752  1.1  mrg   fclose (input);
   2753  1.1  mrg 
   2754  1.1  mrg   if (!initial)
   2755  1.1  mrg     add_file_change (NULL, current_file->inclusion_line + 1);
   2756  1.1  mrg   current_file = current_file->up;
   2757  1.1  mrg   linemap_add (line_table, LC_LEAVE, 0, NULL, 0);
   2758  1.1  mrg }
   2759  1.1  mrg 
   2760  1.1  mrg 
   2761  1.1  mrg /* Open a new file and start scanning from that file. Returns true
   2762  1.1  mrg    if everything went OK, false otherwise.  If form == FORM_UNKNOWN
   2763  1.1  mrg    it tries to determine the source form from the filename, defaulting
   2764  1.1  mrg    to free form.  */
   2765  1.1  mrg 
   2766  1.1  mrg void
   2767  1.1  mrg gfc_new_file (void)
   2768  1.1  mrg {
   2769  1.1  mrg   if (flag_pre_include != NULL)
   2770  1.1  mrg     load_file (flag_pre_include, NULL, false);
   2771  1.1  mrg 
   2772  1.1  mrg   if (gfc_cpp_enabled ())
   2773  1.1  mrg     {
   2774  1.1  mrg       gfc_cpp_preprocess (gfc_source_file);
   2775  1.1  mrg       if (!gfc_cpp_preprocess_only ())
   2776  1.1  mrg 	load_file (gfc_cpp_temporary_file (), gfc_source_file, true);
   2777  1.1  mrg     }
   2778  1.1  mrg   else
   2779  1.1  mrg     load_file (gfc_source_file, NULL, true);
   2780  1.1  mrg 
   2781  1.1  mrg   gfc_current_locus.lb = line_head;
   2782  1.1  mrg   gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line;
   2783  1.1  mrg 
   2784  1.1  mrg #if 0 /* Debugging aid.  */
   2785  1.1  mrg   for (; line_head; line_head = line_head->next)
   2786  1.1  mrg     printf ("%s:%3d %s\n", LOCATION_FILE (line_head->location),
   2787  1.1  mrg 	    LOCATION_LINE (line_head->location), line_head->line);
   2788  1.1  mrg 
   2789  1.1  mrg   exit (SUCCESS_EXIT_CODE);
   2790  1.1  mrg #endif
   2791  1.1  mrg }
   2792  1.1  mrg 
   2793  1.1  mrg static char *
   2794  1.1  mrg unescape_filename (const char *ptr)
   2795  1.1  mrg {
   2796  1.1  mrg   const char *p = ptr, *s;
   2797  1.1  mrg   char *d, *ret;
   2798  1.1  mrg   int escaped, unescape = 0;
   2799  1.1  mrg 
   2800  1.1  mrg   /* Make filename end at quote.  */
   2801  1.1  mrg   escaped = false;
   2802  1.1  mrg   while (*p && ! (! escaped && *p == '"'))
   2803  1.1  mrg     {
   2804  1.1  mrg       if (escaped)
   2805  1.1  mrg 	escaped = false;
   2806  1.1  mrg       else if (*p == '\\')
   2807  1.1  mrg 	{
   2808  1.1  mrg 	  escaped = true;
   2809  1.1  mrg 	  unescape++;
   2810  1.1  mrg 	}
   2811  1.1  mrg       ++p;
   2812  1.1  mrg     }
   2813  1.1  mrg 
   2814  1.1  mrg   if (!*p || p[1])
   2815  1.1  mrg     return NULL;
   2816  1.1  mrg 
   2817  1.1  mrg   /* Undo effects of cpp_quote_string.  */
   2818  1.1  mrg   s = ptr;
   2819  1.1  mrg   d = XCNEWVEC (char, p + 1 - ptr - unescape);
   2820  1.1  mrg   ret = d;
   2821  1.1  mrg 
   2822  1.1  mrg   while (s != p)
   2823  1.1  mrg     {
   2824  1.1  mrg       if (*s == '\\')
   2825  1.1  mrg 	*d++ = *++s;
   2826  1.1  mrg       else
   2827  1.1  mrg 	*d++ = *s;
   2828  1.1  mrg       s++;
   2829  1.1  mrg     }
   2830  1.1  mrg   *d = '\0';
   2831  1.1  mrg   return ret;
   2832  1.1  mrg }
   2833  1.1  mrg 
   2834  1.1  mrg /* For preprocessed files, if the first tokens are of the form # NUM.
   2835  1.1  mrg    handle the directives so we know the original file name.  */
   2836  1.1  mrg 
   2837  1.1  mrg const char *
   2838  1.1  mrg gfc_read_orig_filename (const char *filename, const char **canon_source_file)
   2839  1.1  mrg {
   2840  1.1  mrg   int c, len;
   2841  1.1  mrg   char *dirname, *tmp;
   2842  1.1  mrg 
   2843  1.1  mrg   gfc_src_file = gfc_open_file (filename);
   2844  1.1  mrg   if (gfc_src_file == NULL)
   2845  1.1  mrg     return NULL;
   2846  1.1  mrg 
   2847  1.1  mrg   c = getc (gfc_src_file);
   2848  1.1  mrg 
   2849  1.1  mrg   if (c != '#')
   2850  1.1  mrg     return NULL;
   2851  1.1  mrg 
   2852  1.1  mrg   len = 0;
   2853  1.1  mrg   load_line (gfc_src_file, &gfc_src_preprocessor_lines[0], &len, &c);
   2854  1.1  mrg 
   2855  1.1  mrg   if (wide_strncmp (gfc_src_preprocessor_lines[0], "# 1 \"", 5) != 0)
   2856  1.1  mrg     return NULL;
   2857  1.1  mrg 
   2858  1.1  mrg   tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[0][5], -1);
   2859  1.1  mrg   filename = unescape_filename (tmp);
   2860  1.1  mrg   free (tmp);
   2861  1.1  mrg   if (filename == NULL)
   2862  1.1  mrg     return NULL;
   2863  1.1  mrg 
   2864  1.1  mrg   c = getc (gfc_src_file);
   2865  1.1  mrg 
   2866  1.1  mrg   if (c != '#')
   2867  1.1  mrg     return filename;
   2868  1.1  mrg 
   2869  1.1  mrg   len = 0;
   2870  1.1  mrg   load_line (gfc_src_file, &gfc_src_preprocessor_lines[1], &len, &c);
   2871  1.1  mrg 
   2872  1.1  mrg   if (wide_strncmp (gfc_src_preprocessor_lines[1], "# 1 \"", 5) != 0)
   2873  1.1  mrg     return filename;
   2874  1.1  mrg 
   2875  1.1  mrg   tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[1][5], -1);
   2876  1.1  mrg   dirname = unescape_filename (tmp);
   2877  1.1  mrg   free (tmp);
   2878  1.1  mrg   if (dirname == NULL)
   2879  1.1  mrg     return filename;
   2880  1.1  mrg 
   2881  1.1  mrg   len = strlen (dirname);
   2882  1.1  mrg   if (len < 3 || dirname[len - 1] != '/' || dirname[len - 2] != '/')
   2883  1.1  mrg     {
   2884  1.1  mrg       free (dirname);
   2885  1.1  mrg       return filename;
   2886  1.1  mrg     }
   2887  1.1  mrg   dirname[len - 2] = '\0';
   2888  1.1  mrg   set_src_pwd (dirname);
   2889  1.1  mrg 
   2890  1.1  mrg   if (! IS_ABSOLUTE_PATH (filename))
   2891  1.1  mrg     {
   2892  1.1  mrg       char *p = XCNEWVEC (char, len + strlen (filename));
   2893  1.1  mrg 
   2894  1.1  mrg       memcpy (p, dirname, len - 2);
   2895  1.1  mrg       p[len - 2] = '/';
   2896  1.1  mrg       strcpy (p + len - 1, filename);
   2897  1.1  mrg       *canon_source_file = p;
   2898  1.1  mrg     }
   2899  1.1  mrg 
   2900  1.1  mrg   free (dirname);
   2901  1.1  mrg   return filename;
   2902  1.1  mrg }
   2903