This is the mail archive of the
fortran@gcc.gnu.org
mailing list for the GNU Fortran project.
Re: [fortran,committed] Remove use of ungetc
- From: FX <fxcoudert at gmail dot com>
- To: Andreas Schwab <schwab at suse dot de>
- Cc: "fortran at gcc dot gnu dot org" <fortran at gcc dot gnu dot org>, gcc-patches <gcc-patches at gcc dot gnu dot org>
- Date: Thu, 29 May 2008 17:49:46 +0100
- Subject: Re: [fortran,committed] Remove use of ungetc
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:in-reply-to:references:mime-version:content-type:message-id:cc:content-transfer-encoding:from:subject:date:to:x-mailer; bh=RId9NqsD3q8PoCezLW5X5IKPmqHMn8Jg+Li34lXGR0I=; b=WCCkq5Oat4Q/eNy9lxF0gwRhjrM8aXlV/3roB7TsXWZINvB7oivMssKzIc+ukq2Jmlbtlm79/BEgrWOIevfK3uWojMX7MloLpBFTa/Uld1AOlx2JkjWxcYNys1AGOJ66KD6NN40akCouxT4IaO2wRyz1pHQMAh8v6FTBwSblTDo=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=in-reply-to:references:mime-version:content-type:message-id:cc:content-transfer-encoding:from:subject:date:to:x-mailer; b=oMA4YZ+aWp4b9ppmDu9SqT/kmiefEhOiZyLqCDCd2ZM0W3jHWzuvo9VgZWC0nSHXqqtLzXNf5g1eBmOn5g9lpbr3VRbiFjryTRMFx8+taLWnwdllsZ8ppNjeFwGwhwKbJZDio7qvYO4rBKOepd1He0VcCkvgHBPwjz4M1tkCXuk=
- References: <19c433eb0805290824m74719665hfa277ebfcd597bc1@mail.gmail.com> <jek5hdulgy.fsf@sykes.suse.de>
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/