From patchwork Tue Nov 1 22:06:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 13922 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp3235576wru; Tue, 1 Nov 2022 15:07:53 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7NRttoCKpKqTsKhI5QlGGfAvxWPTUigIe3ACoCL86Yf4q2cHk8h7T9t5UsVIh9IeTKzox6 X-Received: by 2002:a17:906:58c7:b0:722:f4bf:cb75 with SMTP id e7-20020a17090658c700b00722f4bfcb75mr20029859ejs.450.1667340473485; Tue, 01 Nov 2022 15:07:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667340473; cv=none; d=google.com; s=arc-20160816; b=hMyZeg3P3amxRplL8oLiPpNPMJOYppZDzlZuqJjE9KjT6h1ywuN4Tu6SjpdNWtOIP3 Pra1nKYqh32PkMdvWKKmzi+5+x3U8vr8hLov7XLOBa8Zh0cRkChiXJ73VnCfXLzpnAkJ XpOgfiCj2VoiY2aAv9tNuAfWgCoBdovPGUQaJWHPn+wdLLNTweyYALkI59uhOaLXcV9s 4IakU4lDUbk6JhvI8W0nwVUi5XpOMMa4Ru/bDpsSMLrx7nIKYLMujq31GBOgnNHWVD15 WOeITaGlghNirNpE5feJlkkuN25yxBYLZtYkIRpAg6TNFUyvWtsHHBXLd8InMXeP12Yd fEbg== 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 :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=uBwVtINrKm8b0mlfFPuU1wlCdDhPHr3MOTWrPpbz25Q=; b=iaOf6a1wbjUSrDf/KjnTZP+PfgHxgBYBZrq+W/nzkxh8NmsgTC/hzXYZWVZmTJKstM E2rLjC1atIB6n1Vscyu3l88sy6Oo9fwgOp94rhDvGzKsjfyXLpcEUQAjNGcPtfHnLgmR ImDh8+sl7ChatxcJqPYEQsMQ0MBTZRn/osen5MGbBYUkH+dXhTaEwcRDvIUQFx1JVE5a pwTATCzguNkP7E0eAQxGWSkpMK+zJt3Y9PWm47I2o8Gl1AtjRIqwSSqAN8s1Oqpqi6Sf kGY3m2B2d1IJVv3iTAr6AJdklfivvKhDBuyUvFOkmO2w/pz45sLLD5bsfk/ZqZIZJOtx TMQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=EUHbOkjC; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id d24-20020a056402145800b00461b5ba990dsi11838313edx.78.2022.11.01.15.07.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Nov 2022 15:07:53 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=EUHbOkjC; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 75BCC385841B for ; Tue, 1 Nov 2022 22:07:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 75BCC385841B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667340472; bh=uBwVtINrKm8b0mlfFPuU1wlCdDhPHr3MOTWrPpbz25Q=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=EUHbOkjCPVHz+6MZ2Y0MQgOnvm3M3X5JfBcRZRHYpRvcAQCl2t75WGda65M2C2GQC NvSU/RwhAXfTJdUos7KRD2ryqBqpPVhoq6idLX1NANF9oq0ktxT3q9DfKlTz2iLGMt ObG/I5acvCuysEBLTMKOIIpIEhjF0JoEx28dUNpQ= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 9C7093858D37 for ; Tue, 1 Nov 2022 22:07:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9C7093858D37 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-375-e0TfHcSWMs6ToMpY1W-qHg-1; Tue, 01 Nov 2022 18:07:02 -0400 X-MC-Unique: e0TfHcSWMs6ToMpY1W-qHg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5F056381078E for ; Tue, 1 Nov 2022 22:07:02 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.16.170]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3B4CD1415121; Tue, 1 Nov 2022 22:07:02 +0000 (UTC) To: Jason Merrill , GCC Patches Subject: [PATCH] c++: Quash -Wdangling-reference for member operator* [PR107488] Date: Tue, 1 Nov 2022 18:06:52 -0400 Message-Id: <20221101220652.588178-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP 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: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek 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?1748333204651574555?= X-GMAIL-MSGID: =?utf-8?q?1748333204651574555?= -Wdangling-reference complains here: std::vector v = ...; std::vector::const_iterator it = v.begin(); while (it != v.end()) { const int &r = *it++; // warning } because it sees a call to __gnu_cxx::__normal_iterator >::operator* which returns a reference and its argument is a TARGET_EXPR representing the result of __gnu_cxx::__normal_iterator >::operator++ But 'r' above refers to one of the int elements of the vector 'v', not to a temporary object. Therefore the warning is a false positive. I suppose code like the above is relatively common (the warning broke cppunit-1.15.1 and a few other projects), so presumably it makes sense to suppress the warning when it comes to member operator*. In this case it's defined as reference operator*() const _GLIBCXX_NOEXCEPT { return *_M_current; } and I'm guessing a lot of member operator* are like that, at least when it comes to iterators. I've looked at _Fwd_list_iterator, _Fwd_list_const_iterator, __shared_ptr_access, _Deque_iterator, istream_iterator, etc, and they're all like that, so adding #pragmas would be quite tedious. :/ Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? PR c++/107488 gcc/cp/ChangeLog: * call.cc (do_warn_dangling_reference): Quash -Wdangling-reference for member operator*. gcc/testsuite/ChangeLog: * g++.dg/warn/Wdangling-reference5.C: New test. --- gcc/cp/call.cc | 12 +++++++++- .../g++.dg/warn/Wdangling-reference5.C | 22 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wdangling-reference5.C base-commit: 2b0e81d5cc2f7e1d773f6c502bd65b097f392675 diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index c7c7a122045..2c0fa37f53a 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -13467,7 +13467,17 @@ do_warn_dangling_reference (tree expr) can be e.g. const int& z = std::min({1, 2, 3, 4, 5, 6, 7}); which doesn't dangle: std::min here returns an int. */ - || !TYPE_REF_OBJ_P (TREE_TYPE (TREE_TYPE (fndecl)))) + || !TYPE_REF_OBJ_P (TREE_TYPE (TREE_TYPE (fndecl))) + /* Don't emit a false positive for: + std::vector v = ...; + std::vector::const_iterator it = v.begin(); + const int &r = *it++; + because R refers to one of the int elements of V, not to + a temporary object. Member operator* may return a reference + but probably not to one of its arguments. */ + || (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl) + && DECL_OVERLOADED_OPERATOR_P (fndecl) + && DECL_OVERLOADED_OPERATOR_IS (fndecl, INDIRECT_REF))) return NULL_TREE; /* Here we're looking to see if any of the arguments is a temporary diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference5.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference5.C new file mode 100644 index 00000000000..59b5538aee5 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference5.C @@ -0,0 +1,22 @@ +// PR c++/107488 +// { dg-do compile } +// { dg-options "-Wdangling-reference" } + +#include + +int +do_sum (std::vector& v) +{ + int sum = 0; + + std::vector::const_iterator it = v.begin(); + while (it != v.end()) + { + // R refers to one of the int elements of V, not to a temporary + // object, so no dangling reference here. + const int &r = *it++; // { dg-bogus "dangling reference" } + sum += r; + } + + return sum; +}