public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Pr fortran/68153 -- Enhance checking of RESHAPE shape arg
@ 2015-11-01 19:24 Steve Kargl
  2015-11-02 12:16 ` Mikael Morin
  0 siblings, 1 reply; 3+ messages in thread
From: Steve Kargl @ 2015-11-01 19:24 UTC (permalink / raw)
  To: fortran, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 441 bytes --]

The attached patch enhances the check of the shape argument
when it is a named constant.  See the testcase for example.
Built and regression tested on i386-*-freebsd.  OK to commit?

2015-11-01  Steven G. Kargl  <kargl@gcc.gnu.org>

	PR fortran/68153
	* check.c (gfc_check_reshape): Improve check for valid SHAPE argument.

2015-11-01  Steven G. Kargl  <kargl@gcc.gnu.org>

	PR fortran/68153
	* gfortran.dg/pr68153.f90: New test.

-- 
Steve

[-- Attachment #2: pr68153.diff --]
[-- Type: text/x-diff, Size: 1776 bytes --]

Index: gcc/fortran/check.c
===================================================================
--- gcc/fortran/check.c	(revision 229634)
+++ gcc/fortran/check.c	(working copy)
@@ -3711,6 +3711,36 @@ gfc_check_reshape (gfc_expr *source, gfc
 	    }
 	}
     }
+  else if (shape->expr_type == EXPR_VARIABLE && shape->ref
+	   && shape->ref->u.ar.type == AR_FULL && shape->ref->u.ar.dimen == 1
+	   && shape->ref->u.ar.as
+	   && shape->ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
+	   && shape->ref->u.ar.as->lower[0]->ts.type == BT_INTEGER
+	   && shape->ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT
+	   && shape->ref->u.ar.as->upper[0]->ts.type == BT_INTEGER
+	   && shape->symtree->n.sym->attr.flavor == FL_PARAMETER)
+    {
+      int i, dim;
+      gfc_expr *e, *v;
+
+      v = shape->symtree->n.sym->value;
+
+      for (i = 0; i < shape_size; i++)
+	{
+	  e = gfc_constructor_lookup_expr (v->value.constructor, i);
+	  if (e == NULL)
+	     break;
+
+	  gfc_extract_int (e, &dim);
+
+	  if (dim < 0)
+	    {
+	      gfc_error ("Element %d of actual argument of RESHAPE at %L "
+			 "cannot be negative", i + 1, &shape->where);
+	      return false;
+	    }
+	}
+    }
 
   if (pad != NULL)
     {
Index: gcc/testsuite/gfortran.dg/pr68153.f90
===================================================================
--- gcc/testsuite/gfortran.dg/pr68153.f90	(revision 0)
+++ gcc/testsuite/gfortran.dg/pr68153.f90	(working copy)
@@ -0,0 +1,9 @@
+! { dg-do compile }
+! PR fortran/68153
+! Original code contribute by Gerhard Steinmetz
+! <gerhard dot steinmetz dot fortran at t-online dot de>
+!
+program foo
+   integer, parameter :: a(2) = [2, -2]
+   integer, parameter :: b(2,2) = reshape([1, 2, 3, 4], a) ! { dg-error "cannot be negative" }
+end program foo

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Pr fortran/68153 -- Enhance checking of RESHAPE shape arg
  2015-11-01 19:24 [PATCH] Pr fortran/68153 -- Enhance checking of RESHAPE shape arg Steve Kargl
@ 2015-11-02 12:16 ` Mikael Morin
  0 siblings, 0 replies; 3+ messages in thread
From: Mikael Morin @ 2015-11-02 12:16 UTC (permalink / raw)
  To: Steve Kargl, fortran, gcc-patches

Le 01/11/2015 20:24, Steve Kargl a écrit :
> The attached patch enhances the check of the shape argument
> when it is a named constant.  See the testcase for example.
> Built and regression tested on i386-*-freebsd.  OK to commit?
>
Looks good, yes.  Thanks.

Mikael

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Pr fortran/68153 -- Enhance checking of RESHAPE shape arg
@ 2015-11-02 16:28 Dominique d'Humières
  0 siblings, 0 replies; 3+ messages in thread
From: Dominique d'Humières @ 2015-11-02 16:28 UTC (permalink / raw)
  To: Steve Kargl; +Cc: Mikael Morin, gfortran, gcc-patches

Steve,

The error for the test

program p
   integer, parameter :: sh(2) = [2, 2]
   integer, parameter :: a(2,2) = reshape([1, 2, 3, 4], -sh)
   print *, a
end

is

pr68153_2.f90:2:34:

    integer, parameter :: sh(2) = [2, 2]
                                  1
Error: 'shape' argument of 'reshape' intrinsic at (1) has negative element (-2)

I am pretty sure that some day a user will complain about a confusing error message!-(

Dominique

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-11-02 16:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-01 19:24 [PATCH] Pr fortran/68153 -- Enhance checking of RESHAPE shape arg Steve Kargl
2015-11-02 12:16 ` Mikael Morin
2015-11-02 16:28 Dominique d'Humières

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).