diff mbox series

Fortran: restrictions on integer arguments to SYSTEM_CLOCK [PR112609]

Message ID trinity-2e24bb1a-a94a-4a96-b684-1aaf2879485b-1700345575361@3c-app-gmx-bs41
State New
Headers show
Series Fortran: restrictions on integer arguments to SYSTEM_CLOCK [PR112609] | expand

Commit Message

Harald Anlauf Nov. 18, 2023, 10:12 p.m. UTC
Hi all,

Fortran 2023 added restrictions on integer arguments to SYSTEM_CLOCK.
The attached patch implements these.

I was struggling with the way we should handle features that are sort-of
deleted in a new standard, but not described as such in the standard,
which is why we do not have GFC_STD_F2023_DEL.  As -std=gnu should not
apply this restriction, I came up with the solution in the patch.
While playing, I hit a gcc_unreachable in notify_std_msg due to a
missing case, also fixed.

Interestingly, the standard now has a recommendation:

16.9.202 SYSTEM_CLOCK

It it recommended that all references to SYSTEM_CLOCK use integer
arguments with a decimal exponent range of at least 18. ...

In case the user chooses integer(4), shall we emit a warning
e.g. under -pedantic, or some other flag?  This is not done
in the patch, but could be added.

Regtested on x86_64-pc-linux-gnu.  OK for mainline?

Thanks,
Harald

Comments

Steve Kargl Nov. 19, 2023, 12:04 a.m. UTC | #1
On Sat, Nov 18, 2023 at 11:12:55PM +0100, Harald Anlauf wrote:
> 
> Fortran 2023 added restrictions on integer arguments to SYSTEM_CLOCK.
> The attached patch implements these.
> 
> I was struggling with the way we should handle features that are sort-of
> deleted in a new standard, but not described as such in the standard,
> which is why we do not have GFC_STD_F2023_DEL.  As -std=gnu should not
> apply this restriction, I came up with the solution in the patch.
> While playing, I hit a gcc_unreachable in notify_std_msg due to a
> missing case, also fixed.
> 
> Interestingly, the standard now has a recommendation:
> 
> 16.9.202 SYSTEM_CLOCK
> 
> It it recommended that all references to SYSTEM_CLOCK use integer
> arguments with a decimal exponent range of at least 18. ...
> 
> In case the user chooses integer(4), shall we emit a warning
> e.g. under -pedantic, or some other flag?  This is not done
> in the patch, but could be added.
> 
> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
> 

Not in its current form.

>  {
> +  int first_int_kind = -1;
> +  bool f2023 = ((gfc_option.allow_std & GFC_STD_F2023) != 0
> +		&& (gfc_option.allow_std & GFC_STD_GNU) == 0);
> +

If you use the gfc_notify_std(), then you should not need the
above check on GFC_STD_GNU as it should include GFC_STD_F2023.

> 
> +      if (f2023 && count->ts.kind < gfc_default_integer_kind)
> +	{
> +	  gfc_error ("Fortran 2023: COUNT argument to SYSTEM_CLOCK "
> +		     "at %L must have kind of at least default integer",
> +		     &count->where);
> +	  return false;
> +	}

Elsewhere in the FE, gfortran uses gfc_notify_std() to enforce 
requirements of a Fortran standard.  The above would be 

      if (count->ts.kind < gfc_default_integer_kind
          && gfc_notify_std (GFC_STD_F2023, "COUNT argument to SYSTEM_CLOCK "
                             "at %L must have kind of at least default integer",
                             &count->where))

Note, gfc_notify_std() should add the 'Fortran 2023: ' string,
if not, that should be fixed.

Of course, I seldom provide patches if others don't have a comment
then do as you like.
diff mbox series

Patch

From 44814d9436b2e0be14b76b137602e40f3fdaf805 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Sat, 18 Nov 2023 22:51:35 +0100
Subject: [PATCH] Fortran: restrictions on integer arguments to SYSTEM_CLOCK
 [PR112609]

Fortran 2023 added restrictions on integer arguments to SYSTEM_CLOCK to
have a decimal exponent range at least as large as a default integer,
and that all integer arguments have the same kind type parameter.

gcc/fortran/ChangeLog:

	PR fortran/112609
	* check.cc (gfc_check_system_clock): Add checks on integer arguments
	to SYSTEM_CLOCK specific to F2023.
	* error.cc (notify_std_msg): Adjust to handle new features added
	in F2023.

gcc/testsuite/ChangeLog:

	PR fortran/112609
	* gfortran.dg/system_clock_4.f90: New test.
---
 gcc/fortran/check.cc                         | 57 ++++++++++++++++++++
 gcc/fortran/error.cc                         |  4 +-
 gcc/testsuite/gfortran.dg/system_clock_4.f90 | 24 +++++++++
 3 files changed, 84 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/system_clock_4.f90

diff --git a/gcc/fortran/check.cc b/gcc/fortran/check.cc
index 6c45e6542f0..8c2534ae1c9 100644
--- a/gcc/fortran/check.cc
+++ b/gcc/fortran/check.cc
@@ -6774,6 +6774,10 @@  bool
 gfc_check_system_clock (gfc_expr *count, gfc_expr *count_rate,
 			gfc_expr *count_max)
 {
+  int first_int_kind = -1;
+  bool f2023 = ((gfc_option.allow_std & GFC_STD_F2023) != 0
+		&& (gfc_option.allow_std & GFC_STD_GNU) == 0);
+
   if (count != NULL)
     {
       if (!scalar_check (count, 0))
@@ -6788,8 +6792,18 @@  gfc_check_system_clock (gfc_expr *count, gfc_expr *count_rate,
 			      &count->where))
 	return false;

+      if (f2023 && count->ts.kind < gfc_default_integer_kind)
+	{
+	  gfc_error ("Fortran 2023: COUNT argument to SYSTEM_CLOCK "
+		     "at %L must have kind of at least default integer",
+		     &count->where);
+	  return false;
+	}
+
       if (!variable_check (count, 0, false))
 	return false;
+
+      first_int_kind = count->ts.kind;
     }

   if (count_rate != NULL)
@@ -6816,6 +6830,17 @@  gfc_check_system_clock (gfc_expr *count, gfc_expr *count_rate,
 				  "SYSTEM_CLOCK at %L has non-default kind",
 				  &count_rate->where))
 	    return false;
+
+	  if (f2023 && count_rate->ts.kind < gfc_default_integer_kind)
+	    {
+	      gfc_error ("Fortran 2023: COUNT_RATE argument to SYSTEM_CLOCK "
+			 "at %L must have kind of at least default integer",
+			 &count_rate->where);
+	      return false;
+	    }
+
+	  if (first_int_kind < 0)
+	    first_int_kind = count_rate->ts.kind;
 	}

     }
@@ -6836,6 +6861,38 @@  gfc_check_system_clock (gfc_expr *count, gfc_expr *count_rate,

       if (!variable_check (count_max, 2, false))
 	return false;
+
+      if (f2023 && count_max->ts.kind < gfc_default_integer_kind)
+	{
+	  gfc_error ("Fortran 2023: COUNT_MAX argument to SYSTEM_CLOCK "
+		     "at %L must have kind of at least default integer",
+		     &count_max->where);
+	  return false;
+	}
+
+      if (first_int_kind < 0)
+	first_int_kind = count_max->ts.kind;
+    }
+
+  if (f2023 && first_int_kind > 0)
+    {
+      if (count_rate
+	  && count_rate->ts.type == BT_INTEGER
+	  && count_rate->ts.kind != first_int_kind)
+	{
+	  gfc_error ("Fortran 2023: all integer arguments to SYSTEM_CLOCK "
+		     "at %L must have the same kind",
+		     &count_rate->where);
+	  return false;
+	}
+
+      if (count_max && count_max->ts.kind != first_int_kind)
+	{
+	  gfc_error ("Fortran 2023: all integer arguments to SYSTEM_CLOCK "
+		     "at %L must have the same kind",
+		     &count_max->where);
+	  return false;
+	}
     }

   return true;
diff --git a/gcc/fortran/error.cc b/gcc/fortran/error.cc
index 2ac51e95e4d..b8b36c0cd7c 100644
--- a/gcc/fortran/error.cc
+++ b/gcc/fortran/error.cc
@@ -980,7 +980,9 @@  char const*
 notify_std_msg(int std)
 {

-  if (std & GFC_STD_F2018_DEL)
+  if (std & GFC_STD_F2023)
+    return _("Fortran 2023:");
+  else if (std & GFC_STD_F2018_DEL)
     return _("Fortran 2018 deleted feature:");
   else if (std & GFC_STD_F2018_OBS)
     return _("Fortran 2018 obsolescent feature:");
diff --git a/gcc/testsuite/gfortran.dg/system_clock_4.f90 b/gcc/testsuite/gfortran.dg/system_clock_4.f90
new file mode 100644
index 00000000000..f2d706f6d8c
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/system_clock_4.f90
@@ -0,0 +1,24 @@ 
+! { dg-do compile }
+! { dg-options "-std=f2023" }
+! PR fortran/112609 - F2023 restrictions on integer arguments to SYSTEM_CLOCK
+
+program p
+  implicit none
+  integer    :: i,  j,  k
+  integer(2) :: i2, j2, k2
+  integer(8) :: i8, j8, k8
+  real       :: x
+
+  call system_clock(count=i2)      ! { dg-error "at least default integer" }
+  call system_clock(count_rate=j2) ! { dg-error "at least default integer" }
+  call system_clock(count_max=k2)  ! { dg-error "at least default integer" }
+
+  call system_clock(count=i8,count_rate=x,count_max=k8)
+  call system_clock(count=i, count_rate=j8)     ! { dg-error "must have the same kind" }
+  call system_clock(count=i8,count_rate=j)      ! { dg-error "must have the same kind" }
+  call system_clock(count=i, count_max=k8)      ! { dg-error "must have the same kind" }
+  call system_clock(count=i8,count_max=k)       ! { dg-error "must have the same kind" }
+  call system_clock(count_rate=j, count_max=k8) ! { dg-error "must have the same kind" }
+  call system_clock(count_rate=j8,count_max=k)  ! { dg-error "must have the same kind" }
+  call system_clock(i,x,k8)                     ! { dg-error "must have the same kind" }
+end
--
2.35.3