Blame SOURCES/gcc11-fortran-fdec-add-missing-indexes.patch

2e9d4a
From 7001d522d0273658d9e1fb12ca104d56bfcae34d Mon Sep 17 00:00:00 2001
2e9d4a
From: Mark Eggleston <markeggleston@gcc.gnu.org>
2e9d4a
Date: Fri, 22 Jan 2021 15:06:08 +0000
2e9d4a
Subject: [PATCH 10/10] Fill in missing array dimensions using the lower bound
2e9d4a
2e9d4a
Use -fdec-add-missing-indexes to enable feature. Also enabled by fdec.
2e9d4a
---
2e9d4a
 gcc/fortran/lang.opt                  |  8 ++++++++
2e9d4a
 gcc/fortran/options.c                 |  1 +
2e9d4a
 gcc/fortran/resolve.c                 | 24 ++++++++++++++++++++++++
2e9d4a
 gcc/testsuite/gfortran.dg/array_6.f90 | 23 +++++++++++++++++++++++
2e9d4a
 gcc/testsuite/gfortran.dg/array_7.f90 | 23 +++++++++++++++++++++++
2e9d4a
 gcc/testsuite/gfortran.dg/array_8.f90 | 23 +++++++++++++++++++++++
2e9d4a
 6 files changed, 102 insertions(+)
2e9d4a
 create mode 100644 gcc/testsuite/gfortran.dg/array_6.f90
2e9d4a
 create mode 100644 gcc/testsuite/gfortran.dg/array_7.f90
2e9d4a
 create mode 100644 gcc/testsuite/gfortran.dg/array_8.f90
2e9d4a
2e9d4a
diff --git a/gcc/fortran/lang.opt b/gcc/fortran/lang.opt
2e9d4a
index 019c798cf09..f27de88ea3f 100644
2e9d4a
--- a/gcc/fortran/lang.opt
2e9d4a
+++ b/gcc/fortran/lang.opt
2e9d4a
@@ -281,6 +281,10 @@ Wmissing-include-dirs
2e9d4a
 Fortran
2e9d4a
 ; Documented in C/C++
2e9d4a
 
2e9d4a
+Wmissing-index
2e9d4a
+Fortran Var(warn_missing_index) Warning LangEnabledBy(Fortran,Wall)
2e9d4a
+Warn that the lower bound of a missing index will be used.
2e9d4a
+
2e9d4a
 Wuse-without-only
2e9d4a
 Fortran Var(warn_use_without_only) Warning
2e9d4a
 Warn about USE statements that have no ONLY qualifier.
2e9d4a
@@ -460,6 +464,10 @@ fdec
2e9d4a
 Fortran Var(flag_dec)
2e9d4a
 Enable all DEC language extensions.
2e9d4a
 
2e9d4a
+fdec-add-missing-indexes
2e9d4a
+Fortran Var(flag_dec_add_missing_indexes)
2e9d4a
+Enable the addition of missing indexes using their lower bounds.
2e9d4a
+
2e9d4a
 fdec-blank-format-item
2e9d4a
 Fortran Var(flag_dec_blank_format_item)
2e9d4a
 Enable the use of blank format items in format strings.
2e9d4a
diff --git a/gcc/fortran/options.c b/gcc/fortran/options.c
2e9d4a
index 050f56fdc25..c3b2822685d 100644
2e9d4a
--- a/gcc/fortran/options.c
2e9d4a
+++ b/gcc/fortran/options.c
2e9d4a
@@ -84,6 +84,7 @@ set_dec_flags (int value)
2e9d4a
   SET_BITFLAG (flag_dec_non_logical_if, value, value);
2e9d4a
   SET_BITFLAG (flag_dec_promotion, value, value);
2e9d4a
   SET_BITFLAG (flag_dec_sequence, value, value);
2e9d4a
+  SET_BITFLAG (flag_dec_add_missing_indexes, value, value);
2e9d4a
 }
2e9d4a
 
2e9d4a
 /* Finalize DEC flags.  */
2e9d4a
diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
2e9d4a
index fe7d0cc5944..0efeedab46e 100644
2e9d4a
--- a/gcc/fortran/resolve.c
2e9d4a
+++ b/gcc/fortran/resolve.c
2e9d4a
@@ -4806,6 +4806,30 @@ compare_spec_to_ref (gfc_array_ref *ar)
2e9d4a
   if (ar->type == AR_FULL)
2e9d4a
     return true;
2e9d4a
 
2e9d4a
+  if (flag_dec_add_missing_indexes && as->rank > ar->dimen)
2e9d4a
+    {
2e9d4a
+      /* Add in the missing dimensions, assuming they are the lower bound
2e9d4a
+	 of that dimension if not specified.  */
2e9d4a
+      int j;
2e9d4a
+      if (warn_missing_index)
2e9d4a
+	{
2e9d4a
+	  gfc_warning (OPT_Wmissing_index, "Using the lower bound for "
2e9d4a
+		       "unspecified dimensions in array reference at %L",
2e9d4a
+		       &ar->where);
2e9d4a
+	}
2e9d4a
+      /* Other parts of the code iterate ar->start and ar->end from 0 to
2e9d4a
+	 ar->dimen, so it is safe to assume slots from ar->dimen upwards
2e9d4a
+	 are unused (i.e. there are no gaps; the specified indexes are
2e9d4a
+	 contiguous and start at zero.  */
2e9d4a
+      for(j = ar->dimen; j <= as->rank; j++)
2e9d4a
+	{
2e9d4a
+	  ar->start[j] = gfc_copy_expr (as->lower[j]);
2e9d4a
+	  ar->end[j]   = gfc_copy_expr (as->lower[j]);
2e9d4a
+	  ar->dimen_type[j] = DIMEN_ELEMENT;
2e9d4a
+	}
2e9d4a
+      ar->dimen = as->rank;
2e9d4a
+    }
2e9d4a
+
2e9d4a
   if (as->rank != ar->dimen)
2e9d4a
     {
2e9d4a
       gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
2e9d4a
diff --git a/gcc/testsuite/gfortran.dg/array_6.f90 b/gcc/testsuite/gfortran.dg/array_6.f90
2e9d4a
new file mode 100644
2e9d4a
index 00000000000..5c26e18ab3e
2e9d4a
--- /dev/null
2e9d4a
+++ b/gcc/testsuite/gfortran.dg/array_6.f90
2e9d4a
@@ -0,0 +1,23 @@
2e9d4a
+! { dg-do run }
2e9d4a
+! { dg-options "-fdec -Wmissing-index" }!
2e9d4a
+! Checks that under-specified arrays (referencing arrays with fewer
2e9d4a
+! dimensions than the array spec) generates a warning.
2e9d4a
+!
2e9d4a
+! Contributed by Jim MacArthur <jim.macarthur@codethink.co.uk>
2e9d4a
+! Updated by Mark Eggleston <mark.eggleston@codethink.co.uk>
2e9d4a
+!
2e9d4a
+
2e9d4a
+program under_specified_array
2e9d4a
+    integer chessboard(8,8)
2e9d4a
+    integer chessboard3d(8,8,3:5)
2e9d4a
+    chessboard(3,1) = 5
2e9d4a
+    chessboard(3,2) = 55
2e9d4a
+    chessboard3d(4,1,3) = 6
2e9d4a
+    chessboard3d(4,1,4) = 66
2e9d4a
+    chessboard3d(4,4,3) = 7
2e9d4a
+    chessboard3d(4,4,4) = 77
2e9d4a
+  
2e9d4a
+    if (chessboard(3).ne.5) stop 1  ! { dg-warning "Using the lower bound for unspecified dimensions in array reference" }
2e9d4a
+    if (chessboard3d(4).ne.6) stop 2  ! { dg-warning "Using the lower bound for unspecified dimensions in array reference" }
2e9d4a
+    if (chessboard3d(4,4).ne.7) stop 3  ! { dg-warning "Using the lower bound for unspecified dimensions in array reference" }
2e9d4a
+end program
2e9d4a
diff --git a/gcc/testsuite/gfortran.dg/array_7.f90 b/gcc/testsuite/gfortran.dg/array_7.f90
2e9d4a
new file mode 100644
2e9d4a
index 00000000000..5588a5bd02d
2e9d4a
--- /dev/null
2e9d4a
+++ b/gcc/testsuite/gfortran.dg/array_7.f90
2e9d4a
@@ -0,0 +1,23 @@
2e9d4a
+! { dg-do run }
2e9d4a
+! { dg-options "-fdec-add-missing-indexes -Wmissing-index" }!
2e9d4a
+! Checks that under-specified arrays (referencing arrays with fewer
2e9d4a
+! dimensions than the array spec) generates a warning.
2e9d4a
+!
2e9d4a
+! Contributed by Jim MacArthur <jim.macarthur@codethink.co.uk>
2e9d4a
+! Updated by Mark Eggleston <mark.eggleston@codethink.co.uk>
2e9d4a
+!
2e9d4a
+
2e9d4a
+program under_specified_array
2e9d4a
+    integer chessboard(8,8)
2e9d4a
+    integer chessboard3d(8,8,3:5)
2e9d4a
+    chessboard(3,1) = 5
2e9d4a
+    chessboard(3,2) = 55
2e9d4a
+    chessboard3d(4,1,3) = 6
2e9d4a
+    chessboard3d(4,1,4) = 66
2e9d4a
+    chessboard3d(4,4,3) = 7
2e9d4a
+    chessboard3d(4,4,4) = 77
2e9d4a
+  
2e9d4a
+    if (chessboard(3).ne.5) stop 1  ! { dg-warning "Using the lower bound for unspecified dimensions in array reference" }
2e9d4a
+    if (chessboard3d(4).ne.6) stop 2  ! { dg-warning "Using the lower bound for unspecified dimensions in array reference" }
2e9d4a
+    if (chessboard3d(4,4).ne.7) stop 3  ! { dg-warning "Using the lower bound for unspecified dimensions in array reference" }
2e9d4a
+end program
2e9d4a
diff --git a/gcc/testsuite/gfortran.dg/array_8.f90 b/gcc/testsuite/gfortran.dg/array_8.f90
2e9d4a
new file mode 100644
2e9d4a
index 00000000000..f0d2ef5e37d
2e9d4a
--- /dev/null
2e9d4a
+++ b/gcc/testsuite/gfortran.dg/array_8.f90
2e9d4a
@@ -0,0 +1,23 @@
2e9d4a
+! { dg-do compile }
2e9d4a
+! { dg-options "-fdec -fno-dec-add-missing-indexes" }!
2e9d4a
+! Checks that under-specified arrays (referencing arrays with fewer
2e9d4a
+! dimensions than the array spec) generates a warning.
2e9d4a
+!
2e9d4a
+! Contributed by Jim MacArthur <jim.macarthur@codethink.co.uk>
2e9d4a
+! Updated by Mark Eggleston <mark.eggleston@codethink.co.uk>
2e9d4a
+!
2e9d4a
+
2e9d4a
+program under_specified_array
2e9d4a
+    integer chessboard(8,8)
2e9d4a
+    integer chessboard3d(8,8,3:5)
2e9d4a
+    chessboard(3,1) = 5
2e9d4a
+    chessboard(3,2) = 55
2e9d4a
+    chessboard3d(4,1,3) = 6
2e9d4a
+    chessboard3d(4,1,4) = 66
2e9d4a
+    chessboard3d(4,4,3) = 7
2e9d4a
+    chessboard3d(4,4,4) = 77
2e9d4a
+  
2e9d4a
+    if (chessboard(3).ne.5) stop 1  ! { dg-error "Rank mismatch" }
2e9d4a
+    if (chessboard3d(4).ne.6) stop 2  ! { dg-error "Rank mismatch" }
2e9d4a
+    if (chessboard3d(4,4).ne.7) stop 3  ! { dg-error "Rank mismatch" }
2e9d4a
+end program
2e9d4a
-- 
2e9d4a
2.27.0
2e9d4a