From c7f1781582ea16a4b5213278a984c83d948f08b1 Mon Sep 17 00:00:00 2001 From: Janus Weil Date: Wed, 9 Feb 2011 16:58:05 +0100 Subject: [PATCH] re PR fortran/47637 ([OOP] Memory leak involving INTENT(OUT) CLASS argument w/ allocatable components) 2011-02-09 Janus Weil PR fortran/47637 * trans-decl.c (init_intent_out_dt): Handle CLASS arguments. 2011-02-09 Janus Weil PR fortran/47637 * gfortran.dg/auto_dealloc_2.f90: New. From-SVN: r169978 --- gcc/fortran/ChangeLog | 5 ++++ gcc/fortran/trans-decl.c | 26 ++++++++++++++++++ gcc/testsuite/ChangeLog | 5 ++++ gcc/testsuite/gfortran.dg/auto_dealloc_2.f90 | 29 ++++++++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 gcc/testsuite/gfortran.dg/auto_dealloc_2.f90 diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog index cebbe361243..14ae30f9757 100644 --- a/gcc/fortran/ChangeLog +++ b/gcc/fortran/ChangeLog @@ -1,3 +1,8 @@ +2011-02-09 Janus Weil + + PR fortran/47637 + * trans-decl.c (init_intent_out_dt): Handle CLASS arguments. + 2011-02-08 Jerry DeLisle * io.c (match_io_element): Do not set dt if not inquire. diff --git a/gcc/fortran/trans-decl.c b/gcc/fortran/trans-decl.c index fb2f9a85d54..793b2620730 100644 --- a/gcc/fortran/trans-decl.c +++ b/gcc/fortran/trans-decl.c @@ -3192,6 +3192,32 @@ init_intent_out_dt (gfc_symbol * proc_sym, gfc_wrapped_block * block) else if (f->sym->value) gfc_init_default_dt (f->sym, &init, true); } + else if (f->sym && f->sym->attr.intent == INTENT_OUT + && f->sym->ts.type == BT_CLASS + && !CLASS_DATA (f->sym)->attr.class_pointer + && CLASS_DATA (f->sym)->ts.u.derived->attr.alloc_comp) + { + tree decl = build_fold_indirect_ref_loc (input_location, + f->sym->backend_decl); + tmp = CLASS_DATA (f->sym)->backend_decl; + tmp = fold_build3_loc (input_location, COMPONENT_REF, + TREE_TYPE (tmp), decl, tmp, NULL_TREE); + tmp = build_fold_indirect_ref_loc (input_location, tmp); + tmp = gfc_deallocate_alloc_comp (CLASS_DATA (f->sym)->ts.u.derived, + tmp, + CLASS_DATA (f->sym)->as ? + CLASS_DATA (f->sym)->as->rank : 0); + + if (f->sym->attr.optional || f->sym->ns->proc_name->attr.entry_master) + { + present = gfc_conv_expr_present (f->sym); + tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), + present, tmp, + build_empty_stmt (input_location)); + } + + gfc_add_expr_to_block (&init, tmp); + } gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE); } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 7d0f60eec9f..044dd3a5790 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2011-02-09 Janus Weil + + PR fortran/47637 + * gfortran.dg/auto_dealloc_2.f90: New. + 2011-02-09 Rainer Orth * gcc.dg/builtins-config.h: Remove __sgi handling. diff --git a/gcc/testsuite/gfortran.dg/auto_dealloc_2.f90 b/gcc/testsuite/gfortran.dg/auto_dealloc_2.f90 new file mode 100644 index 00000000000..4cbda828895 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/auto_dealloc_2.f90 @@ -0,0 +1,29 @@ +! { dg-do compile } +! { dg-options "-fdump-tree-original" } +! +! PR 47637: [OOP] Memory leak involving INTENT(OUT) CLASS argument w/ allocatable components +! +! Contributed by Rich Townsend + +program test + +type :: t + integer, allocatable :: i(:) +end type + +type(t) :: a + +call init(a) +call init(a) + +contains + + subroutine init(x) + class(t), intent(out) :: x + allocate(x%i(1000)) + end subroutine + +end program + +! { dg-final { scan-tree-dump-times "__builtin_free" 2 "original" } } +! { dg-final { cleanup-tree-dump "original" } } -- 2.30.2