public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [Patch, fortran] PR85742 sizeof allocatable arrays returning wrong value
@ 2018-05-13 13:08 Paul Richard Thomas
  0 siblings, 0 replies; only message in thread
From: Paul Richard Thomas @ 2018-05-13 13:08 UTC (permalink / raw)
  To: fortran, gcc-patches; +Cc: cesar

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

I intend to apply this 'obvious' patch to trunk and 8-branch tonight,
unless there are any objections.

Bootstrapped and regetested on FC27/x86_64.

Paul

2018-05-13  Paul Thomas  <pault@gcc.gnu.org>

    PR fortran/85742
    * trans-types.c (gfc_get_dtype_rank_type): Reorder evaluation
    of 'size'. If the element type is a pointer use the size of the
    TREE_TYPE of the type, unless it is VOID_TYPE. In this latter
    case, set the size to zero.

2018-05-13  Paul Thomas  <pault@gcc.gnu.org>

    PR fortran/85742
    * gfortran.dg/assumed_type_9.f90 : New test.

[-- Attachment #2: submit.diff --]
[-- Type: text/x-patch, Size: 3014 bytes --]

Index: gcc/fortran/trans-types.c
===================================================================
*** gcc/fortran/trans-types.c	(revision 260208)
--- gcc/fortran/trans-types.c	(working copy)
*************** gfc_get_dtype_rank_type (int rank, tree
*** 1518,1523 ****
--- 1518,1525 ----
    tree field;
    vec<constructor_elt, va_gc> *v = NULL;

+   size = TYPE_SIZE_UNIT (etype);
+
    switch (TREE_CODE (etype))
      {
      case INTEGER_TYPE:
*************** gfc_get_dtype_rank_type (int rank, tree
*** 1546,1567 ****
      /* We will never have arrays of arrays.  */
      case ARRAY_TYPE:
        n = BT_CHARACTER;
        break;

      case POINTER_TYPE:
        n = BT_ASSUMED;
      break;

      default:
        /* TODO: Don't do dtype for temporary descriptorless arrays.  */
!       /* We can strange array types for temporary arrays.  */
        return gfc_index_zero_node;
      }

-   size = TYPE_SIZE_UNIT (etype);
-   if (n == BT_CHARACTER && size == NULL_TREE)
-     size = TYPE_SIZE_UNIT (TREE_TYPE (etype));
-
    tmp = get_dtype_type_node ();
    field = gfc_advance_chain (TYPE_FIELDS (tmp),
  			     GFC_DTYPE_ELEM_LEN);
--- 1548,1571 ----
      /* We will never have arrays of arrays.  */
      case ARRAY_TYPE:
        n = BT_CHARACTER;
+       if (size == NULL_TREE)
+ 	size = TYPE_SIZE_UNIT (TREE_TYPE (etype));
        break;

      case POINTER_TYPE:
        n = BT_ASSUMED;
+       if (TREE_CODE (TREE_TYPE (etype)) != VOID_TYPE)
+ 	size = TYPE_SIZE_UNIT (TREE_TYPE (etype));
+       else
+ 	size = build_int_cst (size_type_node, 0);
      break;

      default:
        /* TODO: Don't do dtype for temporary descriptorless arrays.  */
!       /* We can encounter strange array types for temporary arrays.  */
        return gfc_index_zero_node;
      }

    tmp = get_dtype_type_node ();
    field = gfc_advance_chain (TYPE_FIELDS (tmp),
  			     GFC_DTYPE_ELEM_LEN);
Index: gcc/testsuite/gfortran.dg/assumed_type_9.f90
===================================================================
*** gcc/testsuite/gfortran.dg/assumed_type_9.f90	(nonexistent)
--- gcc/testsuite/gfortran.dg/assumed_type_9.f90	(working copy)
***************
*** 0 ****
--- 1,34 ----
+ ! { dg-do run }
+ !
+ ! Test the fix for PR85742 in which the descriptors, passed to alsize,
+ ! for 'a' and 'b' had the wrong element length.
+ !
+ ! Contributed by Cesar Philippidis  <cesar@gcc.gnu.org>
+ !
+ program main
+   implicit none
+   integer, allocatable :: a
+   real, pointer :: b
+   integer, allocatable :: am(:,:)
+   real, pointer :: bm(:,:)
+
+   allocate (a)
+   allocate (b)
+   allocate (am(3,3))
+   allocate (bm(4,4))
+
+   if (sizeof (a) /= alsize (a)) stop 1
+   if (sizeof (b) /= alsize (b)) stop 2
+   if (sizeof (am) /= alsize (am)) stop 3
+   if (sizeof (bm) /= alsize (bm)) stop 4
+
+   deallocate (b)
+   deallocate (bm)
+ contains
+   function alsize (a)
+     integer alsize
+     type (*), dimension (..), contiguous :: a
+     alsize = sizeof(a)
+   end function
+ end program main
+

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2018-05-13 13:08 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-13 13:08 [Patch, fortran] PR85742 sizeof allocatable arrays returning wrong value Paul Richard Thomas

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).