From patchwork Wed Jan 26 16:35:45 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tobias Burnus X-Patchwork-Id: 80521 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 010921007E2 for ; Thu, 27 Jan 2011 03:36:07 +1100 (EST) Received: (qmail 3052 invoked by alias); 26 Jan 2011 16:36:03 -0000 Received: (qmail 2981 invoked by uid 22791); 26 Jan 2011 16:35:58 -0000 X-SWARE-Spam-Status: No, hits=-1.8 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_NONE X-Spam-Check-By: sourceware.org Received: from mx02.qsc.de (HELO mx02.qsc.de) (213.148.130.14) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 26 Jan 2011 16:35:48 +0000 Received: from [192.168.178.22] (port-92-204-33-159.dynamic.qsc.de [92.204.33.159]) by mx02.qsc.de (Postfix) with ESMTP id CB5C81E182; Wed, 26 Jan 2011 17:35:45 +0100 (CET) Message-ID: <4D404D61.3020102@net-b.de> Date: Wed, 26 Jan 2011 17:35:45 +0100 From: Tobias Burnus User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 SUSE/3.1.7 Thunderbird/3.1.7 MIME-Version: 1.0 To: gcc patches , gfortran Subject: [Patch, Fortran] PR 47474: NULL init for scalar allocatable result vars with allocatable components Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org 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 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)