This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Patch, Fortran] PR fortran/37688: Relax "used before typed" check


Hi,

as suggested by Tobias, this patch relaxes the "used before typed" check to allow code like this:

SUBROUTINE (n)
  INTEGER :: arr(n + 1)
  INTEGER :: n

which is strictly speaking invalid but used in real world code and poses no problem to implement. At the moment, a slightly stricter variant is implemented in gfortran which allows only arr(n) (in -std=gnu) for not-yet-typed n, and this patch extends this permission for basic arithmetic expressions like the one above.

Regression test running on GNU/Linux-x86-32. Ok for trunk if no regressions?

Yours,
Daniel

--
Done:  Arc-Bar-Cav-Rog-Sam-Val-Wiz
To go: Hea-Kni-Mon-Pri-Ran-Tou
2008-10-12  Daniel Kraft  <d@domob.eu>

	PR fortran/37688
	* expr.c (gfc_expr_check_typed): Extend permission of untyped
	expressions to both top-level variable and basic arithmetic expressions.

2008-10-12  Daniel Kraft  <d@domob.eu>

	PR fortran/37688
	* gfortran.dg/used_before_typed_6.f90: New test.
Index: gcc/fortran/expr.c
===================================================================
--- gcc/fortran/expr.c	(revision 141001)
+++ gcc/fortran/expr.c	(working copy)
@@ -3429,9 +3429,11 @@ gfc_expr_set_symbols_referenced (gfc_exp
 
 /* Walk an expression tree and check each variable encountered for being typed.
    If strict is not set, a top-level variable is tolerated untyped in -std=gnu
-   mode; this is for things in legacy-code like:
+   mode as is a basic arithmetic expression using those; this is for things in
+   legacy-code like:
 
      INTEGER :: arr(n), n
+     INTEGER :: arr(n + 1), n
 
    The namespace is needed for IMPLICIT typing.  */
 
@@ -3458,9 +3460,26 @@ gfc_expr_check_typed (gfc_expr* e, gfc_n
 {
   bool error_found;
 
-  /* If this is a top-level variable, do the check with strict given to us.  */
-  if (!strict && e->expr_type == EXPR_VARIABLE && !e->ref)
-    return gfc_check_symbol_typed (e->symtree->n.sym, ns, strict, e->where);
+  /* If this is a top-level variable or EXPR_OP, do the check with strict given
+     to us.  */
+  if (!strict)
+    {
+      if (e->expr_type == EXPR_VARIABLE && !e->ref)
+	return gfc_check_symbol_typed (e->symtree->n.sym, ns, strict, e->where);
+
+      if (e->expr_type == EXPR_OP)
+	{
+	  gfc_try t = SUCCESS;
+
+	  gcc_assert (e->value.op.op1);
+	  t = gfc_expr_check_typed (e->value.op.op1, ns, strict);
+
+	  if (t == SUCCESS && e->value.op.op2)
+	    t = gfc_expr_check_typed (e->value.op.op2, ns, strict);
+
+	  return t;
+	}
+    }
 
   /* Otherwise, walk the expression and do it strictly.  */
   check_typed_ns = ns;
Index: gcc/testsuite/gfortran.dg/used_before_typed_6.f90
===================================================================
--- gcc/testsuite/gfortran.dg/used_before_typed_6.f90	(revision 0)
+++ gcc/testsuite/gfortran.dg/used_before_typed_6.f90	(revision 0)
@@ -0,0 +1,20 @@
+! { dg-do compile }
+! { dg-options "-std=gnu" }
+
+! Allow legacy code to work even if not only a single symbol is used as
+! expression but a basic arithmetic expression.
+
+SUBROUTINE test (n, m)
+  IMPLICIT NONE
+
+  ! These should go fine.
+  INTEGER :: arr1(n + 1) ! { dg-bogus "used before it is typed" }
+  INTEGER :: arr2(n / (2 * m**5)) ! { dg-bogus "used before it is typed" }
+
+  ! These should fail for obvious reasons.
+  INTEGER :: arr3(n * 1.1) ! { dg-error "must be of INTEGER type" }
+  INTEGER :: arr4(REAL (m)) ! { dg-error "used before it is typed" }
+  INTEGER :: arr5(SIN (m)) ! { dg-error "used before it is typed" }
+
+  INTEGER :: n, m
+END SUBROUTINE test

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]