From patchwork Tue Aug 6 18:24:24 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steve Kargl X-Patchwork-Id: 1143015 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-506359-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=troutmask.apl.washington.edu Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="jjrJ3+Sn"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46332X08x2z9sMr for ; Wed, 7 Aug 2019 04:24:49 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:reply-to:mime-version:content-type; q=dns; s=default; b=vResvKbDY7/ZwB6+IMOcavzT4EHvjlFun0K7XSdeEyF e+9zMCKWNLbjm/fT1+wy6lKJrY7hOmxsofBF+0CscWpJ4swfK8XKNYY+5L58Y5BX JWkVB5+FS4ckCbL/v6aIhd14IjlfDTDkwEToJ76woKHubNssXVUqLvbaaBP9EET8 = DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:reply-to:mime-version:content-type; s=default; bh=1Rvc8OufzJJYkGzXhK7o48JB0p0=; b=jjrJ3+Sn8E3cq70ii jpF+zKU7vuFlArkkCYyjoImdFMO/ba/ipplaDVzhQkhyTsmxEtQIn3BWB1fr/imJ nYnIFBlyTCeOOM1UV6qcLhgnRgtJXK/6GaYehOFXYbgaBOW8BHiUqNC7VixhCr1F VKyiphADFiJgERTFV9ErwWwZvE= Received: (qmail 110728 invoked by alias); 6 Aug 2019 18:24:41 -0000 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 Received: (qmail 110658 invoked by uid 89); 6 Aug 2019 18:24:37 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-8.1 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS autolearn=ham version=3.3.1 spammy=nota X-HELO: troutmask.apl.washington.edu Received: from troutmask.apl.washington.edu (HELO troutmask.apl.washington.edu) (128.95.76.21) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 06 Aug 2019 18:24:27 +0000 Received: from troutmask.apl.washington.edu (localhost [127.0.0.1]) by troutmask.apl.washington.edu (8.15.2/8.15.2) with ESMTPS id x76IOOho009403 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Tue, 6 Aug 2019 11:24:24 -0700 (PDT) (envelope-from sgk@troutmask.apl.washington.edu) Received: (from sgk@localhost) by troutmask.apl.washington.edu (8.15.2/8.15.2/Submit) id x76IOOeX009402; Tue, 6 Aug 2019 11:24:24 -0700 (PDT) (envelope-from sgk) Date: Tue, 6 Aug 2019 11:24:24 -0700 From: Steve Kargl To: fortran@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] PR fortran/91359 -- A function should return something Message-ID: <20190806182424.GA9372@troutmask.apl.washington.edu> Reply-To: sgk@troutmask.apl.washington.edu MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.12.1 (2019-06-15) The spaghetti code in PR fortran/91359 has a few gotos to jump to different places. In doing so, gfortran with generate a function that has a naked 'return'. Namely, foo () { logical(kind=4) __result_foo; goto __label_000002; __label_000001:; return; /* <-- THIS IS BAD. */ __label_000002:; __result_foo = 0; if (!__result_foo) goto __label_000001; L.1:; return __result_foo; return __result_foo; } The attached patch avoids the naked 'return' by adding the variable that is constructed from the the function name. Namely, foo () { logical(kind=4) __result_foo; goto __label_000002; __label_000001:; return __result_foo; /* <-- THIS IS GOOD. */ __label_000002:; __result_foo = 0; if (!__result_foo) goto __label_000001; L.1:; return __result_foo; return __result_foo; } Regression tested on x86_64-*-freebsd. OK to commit? 2019-08-06 Steven G. Kargl PR fortran/91359 * trans-decl.c (gfc_generate_return): Ensure something is returned from a function. 2019-08-06 Steven G. Kargl PR fortran/91359 * gfortran.dg/pr91359_1.f: New test. * gfortran.dg/pr91359_2.f: Ditto. Index: gcc/fortran/trans-decl.c =================================================================== --- gcc/fortran/trans-decl.c (revision 274121) +++ gcc/fortran/trans-decl.c (working copy) @@ -6449,6 +6449,20 @@ gfc_generate_return (void) TREE_TYPE (result), DECL_RESULT (fndecl), result); } + else + { + /* If the function does not have a result variable, result is + NULL_TREE, and a 'return' is generated without a variable. + The following generates a 'return __result_XXX' where XXX is + the function name. */ + if (sym == sym->result && sym->attr.function) + { + result = gfc_get_fake_result_decl (sym, 0); + result = fold_build2_loc (input_location, MODIFY_EXPR, + TREE_TYPE (result), + DECL_RESULT (fndecl), result); + } + } } return build1_v (RETURN_EXPR, result); Index: gcc/testsuite/gfortran.dg/pr91359_1.f =================================================================== --- gcc/testsuite/gfortran.dg/pr91359_1.f (nonexistent) +++ gcc/testsuite/gfortran.dg/pr91359_1.f (working copy) @@ -0,0 +1,17 @@ +! { dg do } +! PR fortran/91359 +! Orginal code contributed by Brian T. Carcich +! + logical function zero() result(a) + goto 2 +1 return +2 a = .false. + if (.not.a) goto 1 + return + end + + program test_zero + logical zero + if (zero()) stop 'FAIL: zero() returned .TRUE.' + stop 'OKAY: zero() returned .FALSE.' + end Index: gcc/testsuite/gfortran.dg/pr91359_2.f =================================================================== --- gcc/testsuite/gfortran.dg/pr91359_2.f (nonexistent) +++ gcc/testsuite/gfortran.dg/pr91359_2.f (working copy) @@ -0,0 +1,17 @@ +! { dg do } +! PR fortran/91359 +! Orginal code contributed by Brian T. Carcich +! + logical function zero() result(a) + goto 2 +1 return +2 a = .false. + if (.not.a) goto 1 + return + end + + program test_zero + logical zero + if (zero()) stop 'FAIL: zero() returned .TRUE.' + stop 'OKAY: zero() returned .FALSE.' + end