2 ! { dg-options "-fdump-tree-original" }
4 ! Check some basic functionality of allocatable components, including that they
5 ! are nullified when created and automatically deallocated when
6 ! 1. A variable goes out of scope
7 ! 2. INTENT(OUT) dummies
11 ! Contributed by Erik Edelmann <eedelmann@gcc.gnu.org>
12 ! and Paul Thomas <pault@gcc.gnu.org>
19 real, allocatable
:: x(:)
32 type(alloc1
), allocatable
:: a1(:)
33 integer, allocatable
:: a2(:)
38 BLOCK
! To ensure that the allocatables are freed at the end of the scope
40 type(alloc2
), allocatable
:: c(:)
42 if (allocated(b
%a2
) .OR
. allocated(b
%a1
)) then
43 write (0, *) 'main - 1'
47 ! 3 calls to _gfortran_deallocate (INTENT(OUT) dummy)
48 call allocate_alloc2(b
)
52 ! 1 call to _gfortran_deallocate
56 ! 3 calls to _gfortran_deallocate (INTENT(OUT) dummy)
57 call allocate_alloc2(b
)
59 call check_alloc2(return_alloc2())
60 ! 3 calls to _gfortran_deallocate (function result)
63 ! 3 calls to _gfortran_deallocate (INTENT(OUT) dummy)
64 call allocate_alloc2(c(1))
65 ! 4 calls to _gfortran_deallocate
68 ! 7 calls to _gfortran_deallocate (b (3) and c(4) goes aout of scope)
72 subroutine allocate_alloc2(b
)
73 type(alloc2
), intent(out
) :: b
76 if (allocated(b
%a2
) .OR
. allocated(b
%a1
)) then
77 write (0, *) 'allocate_alloc2 - 1'
87 if (allocated(b
%a1(i
)%x
)) then
88 write (0, *) 'allocate_alloc2 - 2', i
91 allocate (b
%a1(i
)%x(3))
92 b
%a1(i
)%x
= i
+ [ 1.0, 2.0, 3.0 ]
95 end subroutine allocate_alloc2
98 type(alloc2
) function return_alloc2() result(b
)
99 if (allocated(b
%a2
) .OR
. allocated(b
%a1
)) then
100 write (0, *) 'return_alloc2 - 1'
110 if (allocated(b
%a1(i
)%x
)) then
111 write (0, *) 'return_alloc2 - 2', i
114 allocate (b
%a1(i
)%x(3))
115 b
%a1(i
)%x
= i
+ [ 1.0, 2.0, 3.0 ]
117 end function return_alloc2
120 subroutine check_alloc2(b
)
121 type(alloc2
), intent(in
) :: b
123 if (.NOT
.(allocated(b
%a2
) .AND
. allocated(b
%a1
))) then
124 write (0, *) 'check_alloc2 - 1'
127 if (any(b
%a2
/= [ 1, 2, 3 ])) then
128 write (0, *) 'check_alloc2 - 2'
132 if (.NOT
.allocated(b
%a1(i
)%x
)) then
133 write (0, *) 'check_alloc2 - 3', i
136 if (any(b
%a1(i
)%x
/= i
+ [ 1.0, 2.0, 3.0 ])) then
137 write (0, *) 'check_alloc2 - 4', i
141 end subroutine check_alloc2
144 ! { dg-final { scan-tree-dump-times "builtin_free" 21 "original" } }