From 19b76346c046b95ee4360a5c35d1e3a846aba6f4 Mon Sep 17 00:00:00 2001 From: Thomas Koenig Date: Tue, 28 Jun 2011 18:59:04 +0000 Subject: [PATCH] re PR fortran/49479 (reshape / optionals / zero sized arrays) 2011-06-28 Thomas Koenig PR fortran/49479 * m4/reshape.m4: If source allocation is smaller than one, set it to one. * intrinsics/reshape_generic.c: Likewise. * generated/reshape_r16.c: Regenerated. * generated/reshape_c4.c: Regenerated. * generated/reshape_c16.c: Regenerated. * generated/reshape_c8.c: Regenerated. * generated/reshape_r4.c: Regenerated. * generated/reshape_i4.c: Regenerated. * generated/reshape_r10.c: Regenerated. * generated/reshape_r8.c: Regenerated. * generated/reshape_c10.c: Regenerated. * generated/reshape_i8.c: Regenerated. * generated/reshape_i16.c: Regenerated. 2011-06-28 Thomas Koenig PR fortran/49479 * gfortran.dg/reshape_zerosize_3.f90: New test. From-SVN: r175594 --- gcc/testsuite/ChangeLog | 5 +++ .../gfortran.dg/reshape_zerosize_3.f90 | 43 +++++++++++++++++++ libgfortran/ChangeLog | 18 ++++++++ libgfortran/generated/reshape_c10.c | 10 ++++- libgfortran/generated/reshape_c16.c | 10 ++++- libgfortran/generated/reshape_c4.c | 10 ++++- libgfortran/generated/reshape_c8.c | 10 ++++- libgfortran/generated/reshape_i16.c | 10 ++++- libgfortran/generated/reshape_i4.c | 10 ++++- libgfortran/generated/reshape_i8.c | 10 ++++- libgfortran/generated/reshape_r10.c | 10 ++++- libgfortran/generated/reshape_r16.c | 10 ++++- libgfortran/generated/reshape_r4.c | 10 ++++- libgfortran/generated/reshape_r8.c | 10 ++++- libgfortran/intrinsics/reshape_generic.c | 11 ++++- libgfortran/m4/reshape.m4 | 10 ++++- 16 files changed, 184 insertions(+), 13 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/reshape_zerosize_3.f90 diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index d270b3eea9d..77a408639ac 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2011-06-28 Thomas Koenig + + PR fortran/49479 + * gfortran.dg/reshape_zerosize_3.f90: New test. + 2011-06-28 Janis Johnson * gcc.target/arm/vfp-ldmdbs.c: Skip for soft float. diff --git a/gcc/testsuite/gfortran.dg/reshape_zerosize_3.f90 b/gcc/testsuite/gfortran.dg/reshape_zerosize_3.f90 new file mode 100644 index 00000000000..870a76c652e --- /dev/null +++ b/gcc/testsuite/gfortran.dg/reshape_zerosize_3.f90 @@ -0,0 +1,43 @@ +! { dg-do run } +! PR 49479 - this used not to print anything. +! Test case by Joost VandeVondele. +MODULE M1 + IMPLICIT NONE + type foo + character(len=5) :: x + end type foo +CONTAINS + SUBROUTINE S1(data) + INTEGER, DIMENSION(:), INTENT(IN), & + OPTIONAL :: DATA + character(20) :: line + IF (.not. PRESENT(data)) call abort + write (unit=line,fmt='(I5)') size(data) + if (line /= ' 0 ') call abort + END SUBROUTINE S1 + + subroutine s_type(data) + type(foo), dimension(:), intent(in), optional :: data + character(20) :: line + IF (.not. PRESENT(data)) call abort + write (unit=line,fmt='(I5)') size(data) + if (line /= ' 0 ') call abort + end subroutine s_type + + SUBROUTINE S2(N) + INTEGER :: N + INTEGER, ALLOCATABLE, DIMENSION(:, :) :: blki + type(foo), allocatable, dimension(:, :) :: bar + ALLOCATE(blki(3,N)) + allocate (bar(3,n)) + blki=0 + CALL S1(RESHAPE(blki,(/3*N/))) + call s_type(reshape(bar, (/3*N/))) + END SUBROUTINE S2 + +END MODULE M1 + +USE M1 +CALL S2(0) +END +! { dg-final { cleanup-modules "m1" } } diff --git a/libgfortran/ChangeLog b/libgfortran/ChangeLog index b0f0666a3b5..88ee3638d17 100644 --- a/libgfortran/ChangeLog +++ b/libgfortran/ChangeLog @@ -1,3 +1,21 @@ +2011-06-28 Thomas Koenig + + PR fortran/49479 + * m4/reshape.m4: If source allocation is smaller than one, set it + to one. + * intrinsics/reshape_generic.c: Likewise. + * generated/reshape_r16.c: Regenerated. + * generated/reshape_c4.c: Regenerated. + * generated/reshape_c16.c: Regenerated. + * generated/reshape_c8.c: Regenerated. + * generated/reshape_r4.c: Regenerated. + * generated/reshape_i4.c: Regenerated. + * generated/reshape_r10.c: Regenerated. + * generated/reshape_r8.c: Regenerated. + * generated/reshape_c10.c: Regenerated. + * generated/reshape_i8.c: Regenerated. + * generated/reshape_i16.c: Regenerated. + 2011-06-18 Janne Blomqvist PR libfortran/49296 diff --git a/libgfortran/generated/reshape_c10.c b/libgfortran/generated/reshape_c10.c index 34eff9086a0..44446ebfee8 100644 --- a/libgfortran/generated/reshape_c10.c +++ b/libgfortran/generated/reshape_c10.c @@ -97,6 +97,8 @@ reshape_c10 (gfc_array_c10 * const restrict ret, if (ret->data == NULL) { + index_type alloc_size; + rs = 1; for (n = 0; n < rdim; n++) { @@ -107,7 +109,13 @@ reshape_c10 (gfc_array_c10 * const restrict ret, rs *= rex; } ret->offset = 0; - ret->data = internal_malloc_size ( rs * sizeof (GFC_COMPLEX_10)); + + if (unlikely (rs < 1)) + alloc_size = 1; + else + alloc_size = rs * sizeof (GFC_COMPLEX_10); + + ret->data = internal_malloc_size (alloc_size); ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rdim; } diff --git a/libgfortran/generated/reshape_c16.c b/libgfortran/generated/reshape_c16.c index 569b76ce4a8..ca588dc2c93 100644 --- a/libgfortran/generated/reshape_c16.c +++ b/libgfortran/generated/reshape_c16.c @@ -97,6 +97,8 @@ reshape_c16 (gfc_array_c16 * const restrict ret, if (ret->data == NULL) { + index_type alloc_size; + rs = 1; for (n = 0; n < rdim; n++) { @@ -107,7 +109,13 @@ reshape_c16 (gfc_array_c16 * const restrict ret, rs *= rex; } ret->offset = 0; - ret->data = internal_malloc_size ( rs * sizeof (GFC_COMPLEX_16)); + + if (unlikely (rs < 1)) + alloc_size = 1; + else + alloc_size = rs * sizeof (GFC_COMPLEX_16); + + ret->data = internal_malloc_size (alloc_size); ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rdim; } diff --git a/libgfortran/generated/reshape_c4.c b/libgfortran/generated/reshape_c4.c index c8b7355de37..e16d32c5239 100644 --- a/libgfortran/generated/reshape_c4.c +++ b/libgfortran/generated/reshape_c4.c @@ -97,6 +97,8 @@ reshape_c4 (gfc_array_c4 * const restrict ret, if (ret->data == NULL) { + index_type alloc_size; + rs = 1; for (n = 0; n < rdim; n++) { @@ -107,7 +109,13 @@ reshape_c4 (gfc_array_c4 * const restrict ret, rs *= rex; } ret->offset = 0; - ret->data = internal_malloc_size ( rs * sizeof (GFC_COMPLEX_4)); + + if (unlikely (rs < 1)) + alloc_size = 1; + else + alloc_size = rs * sizeof (GFC_COMPLEX_4); + + ret->data = internal_malloc_size (alloc_size); ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rdim; } diff --git a/libgfortran/generated/reshape_c8.c b/libgfortran/generated/reshape_c8.c index 1a390b4509b..c8da0bc5f6a 100644 --- a/libgfortran/generated/reshape_c8.c +++ b/libgfortran/generated/reshape_c8.c @@ -97,6 +97,8 @@ reshape_c8 (gfc_array_c8 * const restrict ret, if (ret->data == NULL) { + index_type alloc_size; + rs = 1; for (n = 0; n < rdim; n++) { @@ -107,7 +109,13 @@ reshape_c8 (gfc_array_c8 * const restrict ret, rs *= rex; } ret->offset = 0; - ret->data = internal_malloc_size ( rs * sizeof (GFC_COMPLEX_8)); + + if (unlikely (rs < 1)) + alloc_size = 1; + else + alloc_size = rs * sizeof (GFC_COMPLEX_8); + + ret->data = internal_malloc_size (alloc_size); ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rdim; } diff --git a/libgfortran/generated/reshape_i16.c b/libgfortran/generated/reshape_i16.c index 4f69ce0d287..43fbefc8ccb 100644 --- a/libgfortran/generated/reshape_i16.c +++ b/libgfortran/generated/reshape_i16.c @@ -97,6 +97,8 @@ reshape_16 (gfc_array_i16 * const restrict ret, if (ret->data == NULL) { + index_type alloc_size; + rs = 1; for (n = 0; n < rdim; n++) { @@ -107,7 +109,13 @@ reshape_16 (gfc_array_i16 * const restrict ret, rs *= rex; } ret->offset = 0; - ret->data = internal_malloc_size ( rs * sizeof (GFC_INTEGER_16)); + + if (unlikely (rs < 1)) + alloc_size = 1; + else + alloc_size = rs * sizeof (GFC_INTEGER_16); + + ret->data = internal_malloc_size (alloc_size); ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rdim; } diff --git a/libgfortran/generated/reshape_i4.c b/libgfortran/generated/reshape_i4.c index 53016bdf597..2c136deeae2 100644 --- a/libgfortran/generated/reshape_i4.c +++ b/libgfortran/generated/reshape_i4.c @@ -97,6 +97,8 @@ reshape_4 (gfc_array_i4 * const restrict ret, if (ret->data == NULL) { + index_type alloc_size; + rs = 1; for (n = 0; n < rdim; n++) { @@ -107,7 +109,13 @@ reshape_4 (gfc_array_i4 * const restrict ret, rs *= rex; } ret->offset = 0; - ret->data = internal_malloc_size ( rs * sizeof (GFC_INTEGER_4)); + + if (unlikely (rs < 1)) + alloc_size = 1; + else + alloc_size = rs * sizeof (GFC_INTEGER_4); + + ret->data = internal_malloc_size (alloc_size); ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rdim; } diff --git a/libgfortran/generated/reshape_i8.c b/libgfortran/generated/reshape_i8.c index 34620cf6d40..c97b747ee6d 100644 --- a/libgfortran/generated/reshape_i8.c +++ b/libgfortran/generated/reshape_i8.c @@ -97,6 +97,8 @@ reshape_8 (gfc_array_i8 * const restrict ret, if (ret->data == NULL) { + index_type alloc_size; + rs = 1; for (n = 0; n < rdim; n++) { @@ -107,7 +109,13 @@ reshape_8 (gfc_array_i8 * const restrict ret, rs *= rex; } ret->offset = 0; - ret->data = internal_malloc_size ( rs * sizeof (GFC_INTEGER_8)); + + if (unlikely (rs < 1)) + alloc_size = 1; + else + alloc_size = rs * sizeof (GFC_INTEGER_8); + + ret->data = internal_malloc_size (alloc_size); ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rdim; } diff --git a/libgfortran/generated/reshape_r10.c b/libgfortran/generated/reshape_r10.c index 3bf319aa434..fa3873e1617 100644 --- a/libgfortran/generated/reshape_r10.c +++ b/libgfortran/generated/reshape_r10.c @@ -97,6 +97,8 @@ reshape_r10 (gfc_array_r10 * const restrict ret, if (ret->data == NULL) { + index_type alloc_size; + rs = 1; for (n = 0; n < rdim; n++) { @@ -107,7 +109,13 @@ reshape_r10 (gfc_array_r10 * const restrict ret, rs *= rex; } ret->offset = 0; - ret->data = internal_malloc_size ( rs * sizeof (GFC_REAL_10)); + + if (unlikely (rs < 1)) + alloc_size = 1; + else + alloc_size = rs * sizeof (GFC_REAL_10); + + ret->data = internal_malloc_size (alloc_size); ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rdim; } diff --git a/libgfortran/generated/reshape_r16.c b/libgfortran/generated/reshape_r16.c index 6794b506059..c2b6556b017 100644 --- a/libgfortran/generated/reshape_r16.c +++ b/libgfortran/generated/reshape_r16.c @@ -97,6 +97,8 @@ reshape_r16 (gfc_array_r16 * const restrict ret, if (ret->data == NULL) { + index_type alloc_size; + rs = 1; for (n = 0; n < rdim; n++) { @@ -107,7 +109,13 @@ reshape_r16 (gfc_array_r16 * const restrict ret, rs *= rex; } ret->offset = 0; - ret->data = internal_malloc_size ( rs * sizeof (GFC_REAL_16)); + + if (unlikely (rs < 1)) + alloc_size = 1; + else + alloc_size = rs * sizeof (GFC_REAL_16); + + ret->data = internal_malloc_size (alloc_size); ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rdim; } diff --git a/libgfortran/generated/reshape_r4.c b/libgfortran/generated/reshape_r4.c index e7bfbfbf538..2b859fca7d8 100644 --- a/libgfortran/generated/reshape_r4.c +++ b/libgfortran/generated/reshape_r4.c @@ -97,6 +97,8 @@ reshape_r4 (gfc_array_r4 * const restrict ret, if (ret->data == NULL) { + index_type alloc_size; + rs = 1; for (n = 0; n < rdim; n++) { @@ -107,7 +109,13 @@ reshape_r4 (gfc_array_r4 * const restrict ret, rs *= rex; } ret->offset = 0; - ret->data = internal_malloc_size ( rs * sizeof (GFC_REAL_4)); + + if (unlikely (rs < 1)) + alloc_size = 1; + else + alloc_size = rs * sizeof (GFC_REAL_4); + + ret->data = internal_malloc_size (alloc_size); ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rdim; } diff --git a/libgfortran/generated/reshape_r8.c b/libgfortran/generated/reshape_r8.c index d0441c0fe5d..49b23524fdb 100644 --- a/libgfortran/generated/reshape_r8.c +++ b/libgfortran/generated/reshape_r8.c @@ -97,6 +97,8 @@ reshape_r8 (gfc_array_r8 * const restrict ret, if (ret->data == NULL) { + index_type alloc_size; + rs = 1; for (n = 0; n < rdim; n++) { @@ -107,7 +109,13 @@ reshape_r8 (gfc_array_r8 * const restrict ret, rs *= rex; } ret->offset = 0; - ret->data = internal_malloc_size ( rs * sizeof (GFC_REAL_8)); + + if (unlikely (rs < 1)) + alloc_size = 1; + else + alloc_size = rs * sizeof (GFC_REAL_8); + + ret->data = internal_malloc_size (alloc_size); ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rdim; } diff --git a/libgfortran/intrinsics/reshape_generic.c b/libgfortran/intrinsics/reshape_generic.c index bb1552aa433..5b39711cea6 100644 --- a/libgfortran/intrinsics/reshape_generic.c +++ b/libgfortran/intrinsics/reshape_generic.c @@ -85,6 +85,8 @@ reshape_internal (parray *ret, parray *source, shape_type *shape, if (ret->data == NULL) { + index_type alloc_size; + rs = 1; for (n = 0; n < rdim; n++) { @@ -95,7 +97,14 @@ reshape_internal (parray *ret, parray *source, shape_type *shape, rs *= rex; } ret->offset = 0; - ret->data = internal_malloc_size ( rs * size ); + + if (unlikely (rs < 1)) + alloc_size = 1; + else + alloc_size = rs * size; + + ret->data = internal_malloc_size (alloc_size); + ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rdim; } diff --git a/libgfortran/m4/reshape.m4 b/libgfortran/m4/reshape.m4 index 4052a5ecc15..d1486f3942e 100644 --- a/libgfortran/m4/reshape.m4 +++ b/libgfortran/m4/reshape.m4 @@ -101,6 +101,8 @@ reshape_'rtype_ccode` ('rtype` * const restrict ret, if (ret->data == NULL) { + index_type alloc_size; + rs = 1; for (n = 0; n < rdim; n++) { @@ -111,7 +113,13 @@ reshape_'rtype_ccode` ('rtype` * const restrict ret, rs *= rex; } ret->offset = 0; - ret->data = internal_malloc_size ( rs * sizeof ('rtype_name`)); + + if (unlikely (rs < 1)) + alloc_size = 1; + else + alloc_size = rs * sizeof ('rtype_name`); + + ret->data = internal_malloc_size (alloc_size); ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rdim; } -- 2.30.2