diff mbox series

[v3] Fortran: detect blanks within literal constants in free-form mode [PR92805]

Message ID fc72cd92-3f37-6d0f-30ac-0d93584524e4@gmx.de
State New
Headers show
Series [v3] Fortran: detect blanks within literal constants in free-form mode [PR92805] | expand

Commit Message

Harald Anlauf July 29, 2022, 9:09 p.m. UTC
Hi Mikael,

Am 29.07.22 um 22:36 schrieb Mikael Morin:
> 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.
>

how about the attached?

This introduces the helper function gfc_match_next_char, which is
your second option.

Thanks,
Harald

Comments

Thomas Koenig July 30, 2022, 7:46 a.m. UTC | #1
Hi Harald,

> This introduces the helper function gfc_match_next_char, which is
> your second option.

I would be a little bit concerned about compilation times
with the additional function call overhead.

The function is used only in one place; would it make
sense to put it into primary.cc and declare it static?

Best regards

	Thomas
Mikael Morin July 30, 2022, 8:28 a.m. UTC | #2
Le 29/07/2022 à 23:09, Harald Anlauf via Fortran a écrit :
> Hi Mikael,
> 
> Am 29.07.22 um 22:36 schrieb Mikael Morin:
>> 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.
>>
> 
> how about the attached?
> 
> This introduces the helper function gfc_match_next_char, which is
> your second option.
>
> diff --git a/gcc/fortran/primary.cc b/gcc/fortran/primary.cc
> index 3f01f67cd49..9fa6779200f 100644
> --- a/gcc/fortran/primary.cc
> +++ b/gcc/fortran/primary.cc
> @@ -92,14 +92,17 @@ get_kind (int *is_iso_c)
>  {
>    int kind;
>    match m;
> +  char c;
>  
>    *is_iso_c = 0;
>  
> -  if (gfc_match_char ('_') != MATCH_YES)
> +  if (gfc_match_next_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");
>  
Meh! We killed one check for gfc_current_form but the other one is still 
there.
OK, match_kind_param calls two functions that also gobble space, so 
there is work remaining here.
So please make match_small_literal_constant and gfc_match_name 
space-gobbling wrappers around space-non-gobbling inner functions and 
call those inner functions instead in match_kind_param.
Harald Anlauf July 30, 2022, 6:32 p.m. UTC | #3
Hi Thomas,

Am 30.07.22 um 09:46 schrieb Thomas Koenig via Fortran:
>
> Hi Harald,
>
>> This introduces the helper function gfc_match_next_char, which is
>> your second option.
>
> I would be a little bit concerned about compilation times
> with the additional function call overhead.

the function it replaces (gfc_match_char) is also in a different
file, thus the overhead is at least neutral.  Given that the latter
has an additional call to gfc_gobble_whitespace(), we actually get
better...

> The function is used only in one place; would it make
> sense to put it into primary.cc and declare it static?

Can do that.

> Best regards
>
>      Thomas
>

Thanks,
Harald
diff mbox series

Patch

From 0a95d103e4855fbcc20fd24d44b97b690d570333 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
	* gfortran.h (gfc_match_next_char): Declare it.
	* primary.cc (get_kind): Do not skip over blanks in free-form mode.
	(match_string_constant): Likewise.
	* scanner.cc (gfc_match_next_char): New.  Match next character of
	input, treating whitespace depending on fixed or free form.

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/gfortran.h                        |  1 +
 gcc/fortran/primary.cc                        | 17 +++++--------
 gcc/fortran/scanner.cc                        | 17 +++++++++++++
 gcc/testsuite/gfortran.dg/literal_constants.f | 20 ++++++++++++++++
 .../gfortran.dg/literal_constants.f90         | 24 +++++++++++++++++++
 5 files changed, 68 insertions(+), 11 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/literal_constants.f
 create mode 100644 gcc/testsuite/gfortran.dg/literal_constants.f90

diff --git a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h
index 696aadd7db6..645a30e7d49 100644
--- a/gcc/fortran/gfortran.h
+++ b/gcc/fortran/gfortran.h
@@ -3209,6 +3209,7 @@  gfc_char_t gfc_next_char (void);
 char gfc_next_ascii_char (void);
 gfc_char_t gfc_peek_char (void);
 char gfc_peek_ascii_char (void);
+match gfc_match_next_char (gfc_char_t);
 void gfc_error_recovery (void);
 void gfc_gobble_whitespace (void);
 void gfc_new_file (void);
diff --git a/gcc/fortran/primary.cc b/gcc/fortran/primary.cc
index 3f01f67cd49..9fa6779200f 100644
--- a/gcc/fortran/primary.cc
+++ b/gcc/fortran/primary.cc
@@ -92,14 +92,17 @@  get_kind (int *is_iso_c)
 {
   int kind;
   match m;
+  char c;
 
   *is_iso_c = 0;
 
-  if (gfc_match_char ('_') != MATCH_YES)
+  if (gfc_match_next_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 +1077,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;
diff --git a/gcc/fortran/scanner.cc b/gcc/fortran/scanner.cc
index 2dff2514700..2d1980c074c 100644
--- a/gcc/fortran/scanner.cc
+++ b/gcc/fortran/scanner.cc
@@ -1690,6 +1690,23 @@  gfc_peek_ascii_char (void)
 }
 
 
+/* Match next character of input.  In fixed form mode, we also ignore
+   spaces.  */
+
+match
+gfc_match_next_char (gfc_char_t c)
+{
+  locus where;
+
+  where = gfc_current_locus;
+  if (gfc_next_char () == c)
+    return MATCH_YES;
+
+  gfc_current_locus = where;
+  return MATCH_NO;
+}
+
+
 /* Recover from an error.  We try to get past the current statement
    and get lined up for the next.  The next statement follows a '\n'
    or a ';'.  We also assume that we are not within a character
diff --git a/gcc/testsuite/gfortran.dg/literal_constants.f b/gcc/testsuite/gfortran.dg/literal_constants.f
new file mode 100644
index 00000000000..4d1f1b7eb4c
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/literal_constants.f
@@ -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
diff --git a/gcc/testsuite/gfortran.dg/literal_constants.f90 b/gcc/testsuite/gfortran.dg/literal_constants.f90
new file mode 100644
index 00000000000..f8908f9ad76
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/literal_constants.f90
@@ -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