This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Patch, Fotran] Fix PR 4755 - Do not free + reallocate a variable that is already allocated.


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 <dcarrera@gmail.com>


	* trans.c (gfc__allocate_allocatable): [PR 4755] Do not fix
	and the reallocate a variable that is already allocated.
--
I'm not overweight, I'm undertall.
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));

Attachment: tree-after.original
Description: Text document

Attachment: tree-before.original
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]