This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [fortran,committed] Remove use of ungetc


I wonder what happens with this character when gfc_read_orig_filename
returns NULL. I think load_file would lose the first character of the
first or second line in the file if one of them doesn't start with '#'.


Oops, you're right. This shouldn't happen unless a user passed - fpreprocessed where she shouldn't have, but nonetheless it's a bad idea. I think the easiest way is thus to use fgetpos/fsetpos in that function. Do you see an issue with the following?

Index: scanner.c
===================================================================
--- scanner.c   (revision 136171)
+++ scanner.c   (working copy)
@@ -1308,10 +1308,6 @@ gfc_gobble_whitespace (void)
    label region to expand to spaces that leave the next character in
    the source region.

- If first_char is not NULL, it's a pointer to a single char value holding
- the first character of the line, which has already been read by the
- caller. This avoids the use of ungetc().
-
load_line returns whether the line was truncated.


NOTE: The error machinery isn't available at this point, so we can't
@@ -1319,7 +1315,7 @@ gfc_gobble_whitespace (void)
parts of gfortran. */


static int
-load_line (FILE *input, gfc_char_t **pbuf, int *pbuflen, const int *first_char)
+load_line (FILE *input, gfc_char_t **pbuf, int *pbuflen)
{
static int linenum = 0, current_line = 1;
int c, maxlen, i, preprocessor_flag, buflen = *pbuflen;
@@ -1354,10 +1350,7 @@ load_line (FILE *input, gfc_char_t **pbu
i = 0;
buffer = *pbuf;


-  if (first_char)
-    c = *first_char;
-  else
-    c = getc (input);
+  c = getc (input);

   /* In order to not truncate preprocessor lines, we have to
      remember that this is one.  */
@@ -1814,7 +1807,7 @@ load_file (const char *filename, bool in

   for (;;)
     {
-      int trunc = load_line (input, &line, &line_len, NULL);
+      int trunc = load_line (input, &line, &line_len);

       len = gfc_wide_strlen (line);
       if (feof (input) && len == 0)
@@ -1990,18 +1983,21 @@ gfc_read_orig_filename (const char *file
 {
   int c, len;
   char *dirname, *tmp;
+  fpos_t pos;

   gfc_src_file = gfc_open_file (filename);
   if (gfc_src_file == NULL)
     return NULL;

+  fgetpos (gfc_src_file, &pos);
   c = getc (gfc_src_file);
+  fsetpos (gfc_src_file, &pos);

   if (c != '#')
     return NULL;

   len = 0;
-  load_line (gfc_src_file, &gfc_src_preprocessor_lines[0], &len, &c);
+  load_line (gfc_src_file, &gfc_src_preprocessor_lines[0], &len);

   if (wide_strncmp (gfc_src_preprocessor_lines[0], "# 1 \"", 5) != 0)
     return NULL;
@@ -2012,13 +2008,15 @@ gfc_read_orig_filename (const char *file
   if (filename == NULL)
     return NULL;

+  fgetpos (gfc_src_file, &pos);
   c = getc (gfc_src_file);
+  fsetpos (gfc_src_file, &pos);

   if (c != '#')
     return filename;

   len = 0;
-  load_line (gfc_src_file, &gfc_src_preprocessor_lines[1], &len, &c);
+  load_line (gfc_src_file, &gfc_src_preprocessor_lines[1], &len);

   if (wide_strncmp (gfc_src_preprocessor_lines[1], "# 1 \"", 5) != 0)
     return filename;


-- François-Xavier Coudert http://www.homepages.ucl.ac.uk/~uccafco/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]