From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x634.google.com (mail-ej1-x634.google.com [IPv6:2a00:1450:4864:20::634]) by sourceware.org (Postfix) with ESMTPS id 6AB993858D20 for ; Wed, 9 Feb 2022 08:30:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6AB993858D20 Received: by mail-ej1-x634.google.com with SMTP id p24so5159386ejo.1 for ; Wed, 09 Feb 2022 00:30:42 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=gb+e0fkXhxmh1mCfVGBDZcDb33EvVS87tybo6YO2h9U=; b=2nML9tUV9rRrouQowjXBoF6SzkMRAHS89/za7NORqCvKBhtX4p0TmnHftfRo5la+mg IqZc5F0m09C2RD+5WCExzJWmRmumB46IJYONreNufHsjXDAN/u2KY13CKljhq2hphaXB HsQizT0xbLCdzdIm+TEhNU2sDgVH5eDDv6mnEuukPaSgJw/gebkTHt3a+pGJwp7FifGU p/7oTTOJuGVqIsytz5qeoO9yvhtkmY44srvHnxFoLZUEy1KbBJAGO5I0tp7dGzh41eN9 hHytEyaataWCvxu+xPRtZN7OXGo/1z8pe1bG1mo6m44cLqEMUpMVc0YdPzwKRcS8zxSh fj7g== X-Gm-Message-State: AOAM5301l/Ds+PMhadBiGtL0pKxjaSS8yuxBgYsIZdOk2cA0xGBCmmxP Idyz0zIciDH3fON+We3mXoUmL01hC9bN9vYAHwM= X-Google-Smtp-Source: ABdhPJwt9xK4PYXRyV6CU7A/Ms/ImcHougVUT2BMxSBzZ1jMnvYoYMyBTFOeFQewcoOikaaa1Ef8xai1/XXe2fLDaOY= X-Received: by 2002:a17:906:99c5:: with SMTP id s5mr958785ejn.497.1644395440304; Wed, 09 Feb 2022 00:30:40 -0800 (PST) MIME-Version: 1.0 References: <93612754-9bd2-7e45-f6fa-1704c2f78c54@gmail.com> <74c7ee44-1817-187c-9e0d-b9b8e052fa1b@redhat.com> In-Reply-To: <74c7ee44-1817-187c-9e0d-b9b8e052fa1b@redhat.com> From: Richard Biener Date: Wed, 9 Feb 2022 09:30:29 +0100 Message-ID: Subject: Re: [PATCH] handle "invisible" reference in -Wdangling-pointer (PR104436) To: Jason Merrill Cc: Martin Sebor , gcc-patches Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 09 Feb 2022 08:30:44 -0000 On Tue, Feb 8, 2022 at 11:38 PM Jason Merrill via Gcc-patches wrote: > > On 2/8/22 16:59, Martin Sebor wrote: > > Transforming a by-value arguments to by-reference as GCC does for some > > class types can trigger -Wdangling-pointer when the argument is used > > to store the address of a local variable. Since the stored value is > > not accessible in the caller the warning is a false positive. > > > > The attached patch handles this case by excluding PARM_DECLs with > > the DECL_BY_REFERENCE bit set from consideration. > > > > While testing the patch I noticed some instances of the warning are > > uninitentionally duplicated as the pass runs more than once. To avoid > > that, I also introduce warning suppression into the handler for this > > instance of the warning. (There might still be others.) > > The second test should verify that we do warn about returning 't' from a > function; we don't want to ignore the DECL_BY_REFERENCE RESULT_DECL. > > > + tree var = SSA_NAME_VAR (lhs_ref.ref); > > + if (DECL_BY_REFERENCE (var)) I think you need to test var && TREE_CODE (var) == PARM_DECL here since for DECL_BY_REFERENCE RESULT_DECL we _do_ escape to the caller. Also SSA_NAME_VAR var might be NULL. > > + /* Avoid by-value arguments transformed into by-reference. */ > > + continue; > > I wonder if we can we express this property of invisiref parms somewhere > more general? I imagine optimizations would find it useful as well. > Could pointer_query somehow treat the reference as pointing to a > function-local object? I think points-to analysis got this correct when the reference was marked restrict but now it also fails at this, making DSE fail to eliminate the store in struct A { A(); ~A(); int *p; }; void foo (struct A a, int *p) { a.p = p; } > I previously tried to express this by marking the reference as > 'restrict', but that was wrong > (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97474). > > Jason >