This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran 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, Fortran] PR 47474: NULL init for scalar allocatable result vars with allocatable components


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
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)

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