From 3cae214f76deaf8271e26b094d6d14a31e537e00 Mon Sep 17 00:00:00 2001 From: Paul Thomas Date: Fri, 25 Nov 2016 12:23:43 +0000 Subject: [PATCH] [multiple changes] 2016-11-25 Andre Vehreschild Paul Thomas PR fortran/78293 * trans-expr.c (gfc_conv_procedure_call): Prepend deallocation of alloctable components to post, rather than adding to se->post. * trans-stmt.c (gfc_trans_allocate): Move deallocation of expr3 allocatable components so that all expr3s are visited. 2016-11-25 Paul Thomas PR fortran/78293 * gfortran.dg/allocatable_function_10.f90: New test. * gfortran.dg/class_array_15.f03: Increase builtin_free count from 11 to 12. From-SVN: r242875 --- gcc/fortran/ChangeLog | 17 +++++++ gcc/fortran/trans-expr.c | 2 +- gcc/fortran/trans-stmt.c | 25 +++++----- gcc/testsuite/ChangeLog | 9 +++- .../gfortran.dg/allocatable_function_10.f90 | 46 +++++++++++++++++++ gcc/testsuite/gfortran.dg/class_array_15.f03 | 2 +- 6 files changed, 87 insertions(+), 14 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/allocatable_function_10.f90 diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog index 4c27f1b622e..737a22c906f 100644 --- a/gcc/fortran/ChangeLog +++ b/gcc/fortran/ChangeLog @@ -1,3 +1,20 @@ +2016-11-25 Andre Vehreschild + Paul Thomas + + PR fortran/78293 + * trans-expr.c (gfc_conv_procedure_call): Prepend deallocation + of alloctable components to post, rather than adding to + se->post. + * trans-stmt.c (gfc_trans_allocate): Move deallocation of expr3 + allocatable components so that all expr3s are visited. + +2016-11-25 Paul Thomas + + PR fortran/78293 + * gfortran.dg/allocatable_function_10.f90: New test. + * gfortran.dg/class_array_15.f03: Increase builtin_free count + from 11 to 12. + 2016-11-24 Steven G. Kargl PR fortran/78500 diff --git a/gcc/fortran/trans-expr.c b/gcc/fortran/trans-expr.c index 1331b07a238..1c2d5e1ed3a 100644 --- a/gcc/fortran/trans-expr.c +++ b/gcc/fortran/trans-expr.c @@ -5568,7 +5568,7 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym, tmp = gfc_deallocate_alloc_comp (e->ts.u.derived, tmp, parm_rank); - gfc_add_expr_to_block (&se->post, tmp); + gfc_prepend_expr_to_block (&post, tmp); } /* Add argument checking of passing an unallocated/NULL actual to diff --git a/gcc/fortran/trans-stmt.c b/gcc/fortran/trans-stmt.c index 490b18dae31..19ecf685285 100644 --- a/gcc/fortran/trans-stmt.c +++ b/gcc/fortran/trans-stmt.c @@ -5684,17 +5684,6 @@ gfc_trans_allocate (gfc_code * code) } gfc_add_modify_loc (input_location, &block, var, tmp); - /* Deallocate any allocatable components after all the allocations - and assignments of expr3 have been completed. */ - if (code->expr3->ts.type == BT_DERIVED - && code->expr3->rank == 0 - && code->expr3->ts.u.derived->attr.alloc_comp) - { - tmp = gfc_deallocate_alloc_comp (code->expr3->ts.u.derived, - var, 0); - gfc_add_expr_to_block (&post, tmp); - } - expr3 = var; if (se.string_length) /* Evaluate it assuming that it also is complicated like expr3. */ @@ -5705,6 +5694,20 @@ gfc_trans_allocate (gfc_code * code) expr3 = se.expr; expr3_len = se.string_length; } + + /* Deallocate any allocatable components in expressions that use a + temporary, i.e. are not of expr-type EXPR_VARIABLE or force the + use of a temporary, after the assignment of expr3 is completed. */ + if ((code->expr3->ts.type == BT_DERIVED + || code->expr3->ts.type == BT_CLASS) + && (code->expr3->expr_type != EXPR_VARIABLE || temp_var_needed) + && code->expr3->ts.u.derived->attr.alloc_comp) + { + tmp = gfc_deallocate_alloc_comp (code->expr3->ts.u.derived, + expr3, code->expr3->rank); + gfc_prepend_expr_to_block (&post, tmp); + } + /* Store what the expr3 is to be used for. */ if (e3_is == E3_UNSET) e3_is = expr3 != NULL_TREE ? diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index fe43b97635f..87d6fca44dc 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,10 @@ +2016-11-25 Paul Thomas + + PR fortran/78293 + * gfortran.dg/allocatable_function_10.f90: New test. + * gfortran.dg/class_array_15.f03: Increase builtin_free count + from 11 to 12. + 2016-11-25 Bin Cheng PR middle-end/78507 @@ -45,7 +52,7 @@ 2016-11-25 Senthil Kumar Selvaraj * gcc.dg/pr64277.c: Use __INT32_TYPE__ for targets - with sizeof(int) < 4. + with sizeof(int) < 4. 2016-11-24 Martin Sebor diff --git a/gcc/testsuite/gfortran.dg/allocatable_function_10.f90 b/gcc/testsuite/gfortran.dg/allocatable_function_10.f90 new file mode 100644 index 00000000000..8d171976b01 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/allocatable_function_10.f90 @@ -0,0 +1,46 @@ +! { dg-do run } +! +! Test the fix for PR78293. The deallocations are present at the +! end of the main programme to aid memory leak searching. The +! allocation in 'tt' leaked memory from an intermediate temporary +! for the array constructor. +! +! Contributed by Andrew Benson +! +module m + implicit none + + type t + integer, allocatable, dimension(:) :: r + end type t + +contains + + function tt(a,b) + implicit none + type(t), allocatable, dimension(:) :: tt + type(t), intent(in), dimension(:) :: a,b + allocate(tt, source = [a,b]) + end function tt + + function ts(arg) + implicit none + type(t), allocatable, dimension(:) :: ts + integer, intent(in) :: arg(:) + allocate(ts(1)) + allocate(ts(1)%r, source = arg) + return + end function ts + +end module m + +program p + use m + implicit none + type(t), dimension(2) :: c + c=tt(ts([99,199,1999]),ts([42,142])) + if (any (c(1)%r .ne. [99,199,1999])) call abort + if (any (c(2)%r .ne. [42,142])) call abort + deallocate(c(1)%r) + deallocate(c(2)%r) +end program p diff --git a/gcc/testsuite/gfortran.dg/class_array_15.f03 b/gcc/testsuite/gfortran.dg/class_array_15.f03 index 85716f905cb..fd9e04c2828 100644 --- a/gcc/testsuite/gfortran.dg/class_array_15.f03 +++ b/gcc/testsuite/gfortran.dg/class_array_15.f03 @@ -115,4 +115,4 @@ subroutine pr54992 ! This test remains as the original. bh => bhGet(b,instance=2) if (loc (b) .ne. loc(bh%hostNode)) call abort end -! { dg-final { scan-tree-dump-times "builtin_free" 11 "original" } } +! { dg-final { scan-tree-dump-times "builtin_free" 12 "original" } } -- 2.30.2