From patchwork Fri Jul 22 18:54:36 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [Fotran] Fix PR 4755 - Do not free + reallocate a variable that is already allocated. Date: Fri, 22 Jul 2011 08:54:36 -0000 From: Daniel Carrera X-Patchwork-Id: 106378 Message-Id: <4E29C76C.1070409@gmail.com> To: gfortran , gcc patches Hello, This is a simple patch that fixes PR 4755. Currently the ALLOCATE statement will free and re-allocate an already allocated scalar. The Fortran standard says that this is an error. The attached patch fixes the problem. I am also attaching two tree dumps of the same program, compiled before and after the application of this patch. The test program is this: program test integer, allocatable :: A(:), B[:] integer :: stat character(len=33) :: str allocate(A(1), B[*], stat=stat) end program If you compare the "before" and "after" files you'll see that with this patch GCC no longer tries to free and reallocate A(1). You will also notice that the block for B[*] didn't change. The block for B[*] is already correct in trunk and doesn't need changing. Here is my ChangeLog: 2011-07-22 Daniel Carrera * trans.c (gfc__allocate_allocatable): [PR 4755] Do not fix and the reallocate a variable that is already allocated. diff -r c8b6eb02738a gcc/fortran/trans.c --- a/gcc/fortran/trans.c Fri Jul 22 09:21:49 2011 +0000 +++ b/gcc/fortran/trans.c Fri Jul 22 20:36:23 2011 +0200 @@ -775,13 +775,8 @@ gfc_allocate_allocatable (stmtblock_t * stmtblock_t set_status_block; gfc_start_block (&set_status_block); - tmp = build_call_expr_loc (input_location, - built_in_decls[BUILT_IN_FREE], 1, - fold_convert (pvoid_type_node, mem)); - gfc_add_expr_to_block (&set_status_block, tmp); - tmp = gfc_allocate_using_malloc (&set_status_block, size, status); - gfc_add_modify (&set_status_block, res, fold_convert (type, tmp)); + gfc_add_modify (&set_status_block, res, fold_convert (type, mem)); gfc_add_modify (&set_status_block, status, build_int_cst (status_type, LIBERROR_ALLOCATION));