From patchwork Sat Jul 1 03:30:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 114942 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp10806018vqr; Fri, 30 Jun 2023 20:31:01 -0700 (PDT) X-Google-Smtp-Source: APBJJlHpgZ78PdhZgooJ+fgROQJw/KVIvldRHzl5pEEbFErWwvkPrc3uL+gCxuew6mwFS3KFE7r+ X-Received: by 2002:a05:6402:12da:b0:51d:9e0c:1396 with SMTP id k26-20020a05640212da00b0051d9e0c1396mr2620985edx.35.1688182261708; Fri, 30 Jun 2023 20:31:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688182261; cv=none; d=google.com; s=arc-20160816; b=sIMy24JI/opUoFlkBs+88d432+O6ccSd+c5ACalznydqjBS0i/RKh0DEDtRyqHDuon 1BHidzc5/z6NOFJBXnfp9WaWSCT0iPhm5M5vnDdookDUyIjkvihPilN5Akdsdtgsdmzx LIPQTSmmUiY76r+SYro+4XFCEq7kf7Sdxl75YSjx+F0/IPJfKK+MTn8cwfzOnIl88RnE /1ZP90KuJpMUljktBLE9Skcv6eyUgWql+7mIQRrPxYlC/AL6sab4xFl+bngwQzqwZZXb qwMkFDDgh+Ry4nty3nMB8cyfelvzQ3c2Zriy09kM46ADQ1IJGRvH8SC+6ye9ydDr24uN zlew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:in-reply-to :content-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=MQbPoQDj5Jsz0Ohf/GwNlhq286NS1+cBG1sXUYyz6k0=; fh=rniJ05hh5TtJEgvFF322qtTpFQcCvp9aWk5OC4T+KQA=; b=KbtHWB4gfXpjIvKopcnbppUDkVRbQ2+TqhkSDFvrgU+vOru6y6+u5kMy5ljsc/6Epd iYH8HjRBaJA9YAKIu39PgZmej5ETxLE9ZAt3AUGsQQwjMuPIoa9508aoeZB+V1XmU873 d6afZ29Z329LfP4LywZtXy8d9JMrFCCfbixN+vYDkK/FC0A+L2a6SiRO9I322CrDEET3 gstBUlsXKhzg1Kvxm9l1/3h61QUOVmO6Y9ANSmezGVCFGF+a28bqH8RZHOmp78/zLz64 K2TOUD1mlgQ54ijgL3v7i81arrDwHCfjbCY8cWTmt26Uz17yDv6Nu3Yu2F73h8ZmMHoP d0xg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="dOb/uTr3"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id n21-20020aa7d055000000b0051a6077e2cesi8581626edo.535.2023.06.30.20.31.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jun 2023 20:31:01 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="dOb/uTr3"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6B9283858425 for ; Sat, 1 Jul 2023 03:31:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6B9283858425 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1688182260; bh=MQbPoQDj5Jsz0Ohf/GwNlhq286NS1+cBG1sXUYyz6k0=; h=Date:To:Cc:Subject:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=dOb/uTr3QXpFibf/hFaWDqvMs79NVy/94d58Lp4ORpwVwEyAKz4T9sldAn0Tl9/cP /8JbtPA7NIU/wOIcC9joZMnsfddRVUaIe7U+d9DA1iuFcVwWgJUA5jalxaOaDK3WtG heA3ylAd8KOAq4aHV+6EcoqfcWrusXAv9EXLIfzg= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-oa1-x2a.google.com (mail-oa1-x2a.google.com [IPv6:2001:4860:4864:20::2a]) by sourceware.org (Postfix) with ESMTPS id D06013858C50 for ; Sat, 1 Jul 2023 03:30:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D06013858C50 Received: by mail-oa1-x2a.google.com with SMTP id 586e51a60fabf-1b078b34df5so2456963fac.2 for ; Fri, 30 Jun 2023 20:30:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688182211; x=1690774211; h=in-reply-to:content-disposition:mime-version:message-id:subject:cc :to:from:date:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=MQbPoQDj5Jsz0Ohf/GwNlhq286NS1+cBG1sXUYyz6k0=; b=iJfmRIv0CyoWoN2/WLNxErlImdzCOjAvwD91EvPSd7njB1vGBP1dUquNsE6hBm8SVA etnuHVHPrinkYeJ7nd/ZiZZc2l/lHr6maOS2cQLuOyDMzGPcRrF82/LRKrV3ZGhAkahW YkRzoN2aAENK1GkRcvFs3tOksJBXleuZxUGY6OvmybY6VPgrKzV84H3za3tcAeKQhUFQ qurOcaFM2FSgJJkeV7t4uXYkk9EyvJtZ+HSWyrc6/dxXHqzQR9LIctpJAhqn4/5ZDwWK f/fFUSwU+N3JLM96ZJYF/bRq6hjcO9F3J3yAxm28Q614dKfJWt4Ea5VINJYRkxWPn0Pi BsLQ== X-Gm-Message-State: ABy/qLZu94dprF+Jg8RiWYBieOOcM4Pmx69LmsnOxAh7d1cVrPAKRU7E INg30Bri0ne7rlhzkBZkBixXAw5lZr4= X-Received: by 2002:a05:6870:96a0:b0:19f:9495:95ff with SMTP id o32-20020a05687096a000b0019f949595ffmr6333063oaq.27.1688182210670; Fri, 30 Jun 2023 20:30:10 -0700 (PDT) Received: from Thaum.localdomain (59-102-120-25.tpgi.com.au. [59.102.120.25]) by smtp.gmail.com with ESMTPSA id 18-20020a17090a035200b00262fc689d50sm9025696pjf.4.2023.06.30.20.30.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jun 2023 20:30:10 -0700 (PDT) Date: Sat, 1 Jul 2023 13:30:00 +1000 To: gcc-patches@gcc.gnu.org Cc: Patrick Palka Subject: [PATCH v3 2/3] c++: Improve constexpr error for dangling local variables Message-ID: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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-Patchwork-Original-From: Nathaniel Shead via Gcc-patches From: Nathaniel Shead Reply-To: Nathaniel Shead Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770187403078809178?= X-GMAIL-MSGID: =?utf-8?q?1770187403078809178?= Currently, when typeck discovers that a return statement will refer to a local variable it rewrites to return a null pointer. This causes the error messages for using the return value in a constant expression to be unhelpful, especially for reference return values. This patch removes this "optimisation". Relying on this raises a warning by default and causes UB anyway, so there should be no issue in doing so. We also suppress additional warnings from later passes that detect this as a dangling pointer, since we've already indicated this anyway. gcc/cp/ChangeLog: * semantics.cc (finish_return_stmt): Suppress dangling pointer reporting on return statement if already reported. * typeck.cc (check_return_expr): Don't set return expression to zero for dangling addresses. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/constexpr-lifetime5.C: Test reported message is correct. * g++.dg/warn/Wreturn-local-addr-6.C: Remove check for return value optimisation. Signed-off-by: Nathaniel Shead --- gcc/cp/semantics.cc | 5 ++++- gcc/cp/typeck.cc | 5 +++-- gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C | 4 ++-- gcc/testsuite/g++.dg/warn/Wreturn-local-addr-6.C | 3 --- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 8fb47fd179e..107407de513 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -1260,7 +1260,10 @@ finish_return_stmt (tree expr) r = build_stmt (input_location, RETURN_EXPR, expr); if (no_warning) - suppress_warning (r, OPT_Wreturn_type); + { + suppress_warning (r, OPT_Wreturn_type); + suppress_warning (r, OPT_Wdangling_pointer_); + } r = maybe_cleanup_point_expr_void (r); r = add_stmt (r); diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index 859b133a18d..47233b3b717 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -11273,8 +11273,9 @@ check_return_expr (tree retval, bool *no_warning) else if (!processing_template_decl && maybe_warn_about_returning_address_of_local (retval, loc) && INDIRECT_TYPE_P (valtype)) - retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval, - build_zero_cst (TREE_TYPE (retval))); + /* Suppress the Wdangling-pointer warning in the return statement + that would otherwise occur. */ + *no_warning = true; } /* A naive attempt to reduce the number of -Wdangling-reference false diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C index a4bc71d890a..ad3ef579f63 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C @@ -1,11 +1,11 @@ // { dg-do compile { target c++14 } } // { dg-options "-Wno-return-local-addr" } -constexpr const int& id(int x) { return x; } +constexpr const int& id(int x) { return x; } // { dg-message "note: declared here" } constexpr bool test() { const int& y = id(3); return y == 3; } -constexpr bool x = test(); // { dg-error "" } +constexpr bool x = test(); // { dg-error "accessing object outside its lifetime" } diff --git a/gcc/testsuite/g++.dg/warn/Wreturn-local-addr-6.C b/gcc/testsuite/g++.dg/warn/Wreturn-local-addr-6.C index fae8b7e766f..ec8e241d83e 100644 --- a/gcc/testsuite/g++.dg/warn/Wreturn-local-addr-6.C +++ b/gcc/testsuite/g++.dg/warn/Wreturn-local-addr-6.C @@ -24,6 +24,3 @@ return_addr_local_as_intref (void) return (const intptr_t&)a; // { dg-warning "\\\[-Wreturn-local-addr]" } */ } - -/* Verify that the return value has been replaced with zero: - { dg-final { scan-tree-dump-times "return 0;" 2 "optimized" } } */