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