[v2] Fortran: detect blanks within literal constants in free-form mode [PR92805]
Commit Message
Hi Mikael,
Am 29.07.22 um 13:11 schrieb Mikael Morin:
> Hello,
>
> Le 28/07/2022 à 22:11, Harald Anlauf via Fortran a écrit :
>> Dear all,
>>
>> in free-form mode, blanks are significant, so they cannot appear
>> in literal constants, especially not before or after the "_" that
>> separates the literal and the kind specifier.
>>
>> The initial patch from Steve addressed numerical literals, which
>> I completed by adjusting the parsing of string literals.
>>
>> Regtested on x86_64-pc-linux-gnu. OK for mainline?
>>
> It looks correct, but I think we should continue to have the free vs
> fixed form abstracted away from the parsing code.
yes, that makes sense.
> So, I suggest instead to remove the calls to gfc_gobble_whitespace in
> match_string_constant,
Indeed, removing these simplifies the function and indeed works!
> and use gfc_next_char instead of gfc_match_char
> in get_kind.
There is one important functionality in gfc_match_char(): it manages
the locus. We would need then to add this explicitly to get_kind,
which does not look to me like a big improvement over the present
solution. Otherwise I get test regressions.
> Mikael
>
I've attached a revised version with improved match_string_constant().
What do you think?
Thanks,
Harald
Comments
Le 29/07/2022 à 21:59, Harald Anlauf via Fortran a écrit :
>
> Am 29.07.22 um 13:11 schrieb Mikael Morin:
>
> > and use gfc_next_char instead of gfc_match_char
>> in get_kind.
>
> There is one important functionality in gfc_match_char(): it manages
> the locus. We would need then to add this explicitly to get_kind,
> which does not look to me like a big improvement over the present
> solution. Otherwise I get test regressions.
>
Indeed, I overlooked that, but my opinion remains that we shouldn’t play
with fixed vs free form considerations here.
So the options I can see are:
- handle the locus in get_kind; we do it a lot already in matching
functions, so it wouldn’t be different here.
- implement a variant of gfc_match_char without space gobbling.
- use gfc_match(...), which is a bit heavy weight to match a single
char string, but otherwise would keep things concise.
My preference goes to the third option, but I’m fine with either of them
if you have a different one.
From f8e7c297b7c9e5a2b22185c7e0d638764c33aa71 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Thu, 28 Jul 2022 22:07:02 +0200
Subject: [PATCH] Fortran: detect blanks within literal constants in free-form
mode [PR92805]
gcc/fortran/ChangeLog:
PR fortran/92805
* primary.cc (get_kind): Do not skip over blanks in free-form mode.
(match_string_constant): Likewise.
gcc/testsuite/ChangeLog:
PR fortran/92805
* gfortran.dg/literal_constants.f: New test.
* gfortran.dg/literal_constants.f90: New test.
Co-authored-by: Steven G. Kargl <kargl@gcc.gnu.org>
---
gcc/fortran/primary.cc | 19 +++++++--------
gcc/testsuite/gfortran.dg/literal_constants.f | 20 ++++++++++++++++
.../gfortran.dg/literal_constants.f90 | 24 +++++++++++++++++++
3 files changed, 53 insertions(+), 10 deletions(-)
create mode 100644 gcc/testsuite/gfortran.dg/literal_constants.f
create mode 100644 gcc/testsuite/gfortran.dg/literal_constants.f90
@@ -92,14 +92,21 @@ get_kind (int *is_iso_c)
{
int kind;
match m;
+ char c;
*is_iso_c = 0;
+ c = gfc_peek_ascii_char ();
+ if (gfc_current_form == FORM_FREE && gfc_is_whitespace (c))
+ return -2;
+
if (gfc_match_char ('_') != MATCH_YES)
return -2;
- m = match_kind_param (&kind, is_iso_c);
- if (m == MATCH_NO)
+ m = MATCH_NO;
+ c = gfc_peek_ascii_char ();
+ if ((gfc_current_form == FORM_FREE && gfc_is_whitespace (c))
+ || (m = match_kind_param (&kind, is_iso_c)) == MATCH_NO)
gfc_error ("Missing kind-parameter at %C");
return (m == MATCH_YES) ? kind : -1;
@@ -1074,17 +1081,9 @@ match_string_constant (gfc_expr **result)
c = gfc_next_char ();
}
- if (c == ' ')
- {
- gfc_gobble_whitespace ();
- c = gfc_next_char ();
- }
-
if (c != '_')
goto no_match;
- gfc_gobble_whitespace ();
-
c = gfc_next_char ();
if (c != '\'' && c != '"')
goto no_match;
new file mode 100644
@@ -0,0 +1,20 @@
+! { dg-do compile }
+! { dg-options "-ffixed-form" }
+! PR fortran/92805 - blanks within literal constants in fixed-form mode
+
+ implicit none
+ integer, parameter :: ck = kind ("a") ! default character kind
+ integer, parameter :: rk = kind (1.0) ! default real kind
+ print *, 1_"abc"
+ print *, 1 _"abc"
+ print *, 1_ "abc"
+ print *, ck_"a"
+ print *, ck _"ab"
+ print *, ck_ "ab"
+ print *, 3.1415_4
+ print *, 3.1415 _4
+ print *, 3.1415_ 4
+ print *, 3.1415_rk
+ print *, 3.1415 _rk
+ print *, 3.1415_ rk
+ end
new file mode 100644
@@ -0,0 +1,24 @@
+! { dg-do compile }
+! { dg-options "-ffree-form" }
+! PR fortran/92805 - blanks within literal constants in free-form mode
+
+ implicit none
+ integer, parameter :: ck = kind ("a") ! default character kind
+ integer, parameter :: rk = kind (1.0) ! default real kind
+ print *, 1_"abc"
+ print *, 1 _"abc" ! { dg-error "Syntax error" }
+ print *, 1_ "abc" ! { dg-error "Missing kind-parameter" }
+ print *, 1 _ "abc" ! { dg-error "Syntax error" }
+ print *, ck_"a"
+ print *, ck _"ab" ! { dg-error "Syntax error" }
+ print *, ck_ "ab" ! { dg-error "Syntax error" }
+ print *, ck _ "ab" ! { dg-error "Syntax error" }
+ print *, 3.1415_4
+ print *, 3.1415 _4 ! { dg-error "Syntax error" }
+ print *, 3.1415_ 4 ! { dg-error "Missing kind-parameter" }
+ print *, 3.1415 _ 4 ! { dg-error "Syntax error" }
+ print *, 3.1415_rk
+ print *, 3.1415 _rk ! { dg-error "Syntax error" }
+ print *, 3.1415_ rk ! { dg-error "Missing kind-parameter" }
+ print *, 3.141 _ rk ! { dg-error "Syntax error" }
+ end
--
2.35.3