public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [Patch, Fortran] PR 47474: NULL init for scalar allocatable result vars with allocatable components
@ 2011-01-26 17:54 Tobias Burnus
  2011-01-27 23:25 ` Jerry DeLisle
  0 siblings, 1 reply; 2+ messages in thread
From: Tobias Burnus @ 2011-01-26 17:54 UTC (permalink / raw)
  To: gcc patches, gfortran

[-- Attachment #1: Type: text/plain, Size: 870 bytes --]

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

[-- Attachment #2: null_ptr.diff --]
[-- Type: text/x-patch, Size: 1243 bytes --]

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)

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Patch, Fortran] PR 47474: NULL init for scalar allocatable result vars with allocatable components
  2011-01-26 17:54 [Patch, Fortran] PR 47474: NULL init for scalar allocatable result vars with allocatable components Tobias Burnus
@ 2011-01-27 23:25 ` Jerry DeLisle
  0 siblings, 0 replies; 2+ messages in thread
From: Jerry DeLisle @ 2011-01-27 23:25 UTC (permalink / raw)
  To: Tobias Burnus; +Cc: gcc patches, gfortran

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2011-01-27 22:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-01-26 17:54 [Patch, Fortran] PR 47474: NULL init for scalar allocatable result vars with allocatable components Tobias Burnus
2011-01-27 23:25 ` Jerry DeLisle

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).