Patchwork [Fortran] PR 47474: NULL init for scalar allocatable result vars with allocatable components

login
register
mail settings
Submitter Tobias Burnus
Date Jan. 26, 2011, 4:35 p.m.
Message ID <4D404D61.3020102@net-b.de>
Download mbox | patch
Permalink /patch/80521/
State New
Headers show

Comments

Tobias Burnus - Jan. 26, 2011, 4:35 p.m.
Dear all,

functions such as

    function func() result(res)
      type(tx), allocatable :: res

produced code such as:

     struct tx * res;
     res.i.data = 0B;  /*<<<<  WRONG.  */
     res = 0B;

where the component is initialized although the pointer "res" is not 
associated. If one simply swaps the "if (alloc_comp) {...} else if 
(dimension == 0 && allocatable) { ... } the result is OK but there are two

     res = 0B;
     res = 0B;

if there is a separate result variable and one if the function name 
matches the result variable; thus, I added the extra "sym == 
sym->result" check.

Note that I failed for some reason to generate a failing test case. 
Thus, I did not include one. (I found the bug when trying a patch for 
P4-regression PR 47455 - thus, that PR might contain a test case.)

Build and regtested on x86-64-linux.
OK for the trunk?

Tobias
Jerry DeLisle - Jan. 27, 2011, 10:50 p.m.
On 01/26/2011 08:35 AM, Tobias Burnus wrote:
> Dear all,
>
> functions such as
>
> function func() result(res)
> type(tx), allocatable :: res
>
> produced code such as:
>
> struct tx * res;
> res.i.data = 0B; /*<<<< WRONG. */
> res = 0B;
>
> where the component is initialized although the pointer "res" is not associated.
> If one simply swaps the "if (alloc_comp) {...} else if (dimension == 0 &&
> allocatable) { ... } the result is OK but there are two
>
> res = 0B;
> res = 0B;
>
> if there is a separate result variable and one if the function name matches the
> result variable; thus, I added the extra "sym == sym->result" check.
>
> Note that I failed for some reason to generate a failing test case. Thus, I did
> not include one. (I found the bug when trying a patch for P4-regression PR 47455
> - thus, that PR might contain a test case.)
>
> Build and regtested on x86-64-linux.
> OK for the trunk?

OK, and thanks for patch.

Jerry

Patch

2011-01-26  Tobias Burnus  <burnus@net-b.de>

	PR fortran/47474
	* trans-decl.c (gfc_generate_function_code): Fix init
	of allocatable result variable with allocatable components.

diff --git a/gcc/fortran/trans-decl.c b/gcc/fortran/trans-decl.c
index 5e3afbe..fb2f9a8 100644
--- a/gcc/fortran/trans-decl.c
+++ b/gcc/fortran/trans-decl.c
@@ -4602,16 +4716,18 @@  gfc_generate_function_code (gfc_namespace * ns)
 	    && sym->attr.function
 	    && !sym->attr.pointer)
 	{
-	  if (sym->ts.type == BT_DERIVED
-	      && sym->ts.u.derived->attr.alloc_comp)
+	  if (sym->attr.allocatable && sym->attr.dimension == 0
+	      && sym->result == sym)
+	    gfc_add_modify (&init, result, fold_convert (TREE_TYPE (result),
+							 null_pointer_node));
+	  else if (sym->ts.type == BT_DERIVED
+	      && sym->ts.u.derived->attr.alloc_comp
+	      && !sym->attr.allocatable)
 	    {
 	      rank = sym->as ? sym->as->rank : 0;
 	      tmp = gfc_nullify_alloc_comp (sym->ts.u.derived, result, rank);
 	      gfc_add_expr_to_block (&init, tmp);
 	    }
-	  else if (sym->attr.allocatable && sym->attr.dimension == 0)
-	    gfc_add_modify (&init, result, fold_convert (TREE_TYPE (result),
-							 null_pointer_node));
 	}
 
       if (result == NULL_TREE)