From patchwork Fri Apr 14 23:42:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 83584 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp726915vqo; Fri, 14 Apr 2023 16:43:18 -0700 (PDT) X-Google-Smtp-Source: AKy350aHij+OkfmTMxnNx8U5eopDMy8FyP4ccwdBCEJFZPb0mMrE+J/9a4b4G6haz2MFWvLv4m5G X-Received: by 2002:a05:6402:3c8:b0:4fc:9a22:e0d2 with SMTP id t8-20020a05640203c800b004fc9a22e0d2mr7719925edw.14.1681515798556; Fri, 14 Apr 2023 16:43:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681515798; cv=none; d=google.com; s=arc-20160816; b=RX1b3mvS7JEnY64WjKGO74R/0I+xlTNP5e4JHLWxRRdh0mYbHyjOMrArJ/Nqo7eQCa fFZ3oedkOYIDDoP/ewCE8qggwi+2eeaarxdl1YJcGKG1mYmCoKzF2Xv1KpW39eFBN2wg /sbJjhkSA/TI5hqs03kCrFLM1JzjzBgPnj3bxXmowXr6gCl/9NKCk1lnxo+AB8L+VYSt Z3pWYtaWdOC0tuJRYiriTZ6qWlro5F8Zo34oC5nU5n01xvbdcx/1giyXn1H3ea9WNl8E /kpcTxE+h0QorUMkSTrYWtCh05+R6iJhPmLq/dnpgYiO+FslJ21s1e0sNDWDZLBJfTDk +rpw== 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=ga/7hu/K/DbrFwo8kezc0p+MIyIm/btts3xTU3zCINI=; b=fIMEeve062HvshjDj8mv4nX7KYQWh2WWBb07ahsvD+p+PYFNdRXvMkr3jyFKRCwFJw F3rPHUd5Bl89ZHTH1yBOEU95wmK3CS2t9e/Hf9qpH4NSQpgsyIdKU9ftMbGFHbLsVA1/ PQB2ujXvl9DY7m4pZKDweY1kvHniYKDGRV5a/gZdKvl9AeJKQFh0bO1To+KXIArp3VRu eKoRlcTvOb9MpA02Pz7lBkBGvBCWnsQuNK5QihMnRmH8TLGU1v4aCX+S+SieJjMXpdid aakmWwHJ5nAENhi6s1VbtjdodKMz/DQ7Y7cbzFMQjnZDZcGHfJZDMTL59B3n0YtGPcZp CBAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=eR3QoUSd; 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 o26-20020aa7c51a000000b005068604ac0csi1822262edq.684.2023.04.14.16.43.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 16:43:18 -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=eR3QoUSd; 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 7D9BB385841D for ; Fri, 14 Apr 2023 23:43:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7D9BB385841D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1681515797; bh=ga/7hu/K/DbrFwo8kezc0p+MIyIm/btts3xTU3zCINI=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=eR3QoUSdENyaiwVBZkGEOwV2IK3QbeKgBPsUp+2uGR3oIy0NYr8kDjOhZY5/bVRd7 oacp0ws9FVD4GjEB3NvtD/rywDxvFq5PNXeBbBbtzqg8OkY3PLg+qiTBUZchevA+WH Qhl2BjHD107O8Sx0TVnB+MSbZT5BRfckp0F81b8E= 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 4BF6B3858C5E for ; Fri, 14 Apr 2023 23:42:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4BF6B3858C5E Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-455-ZFrm-HIBPNOrzTUYJJ4xvA-1; Fri, 14 Apr 2023 19:42:29 -0400 X-MC-Unique: ZFrm-HIBPNOrzTUYJJ4xvA-1 Received: by mail-qv1-f71.google.com with SMTP id e4-20020a0cd644000000b005eaf0a051d4so8169321qvj.12 for ; Fri, 14 Apr 2023 16:42:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681515749; x=1684107749; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ga/7hu/K/DbrFwo8kezc0p+MIyIm/btts3xTU3zCINI=; b=La+EO9UaZgKGEoDL6ROs275D1I9wWiOb0hHCblJFC61nuoktZpacmcO7lBpBVubrMH Oec+cRUWPHD1FTki119Hpzs9lgwvg65olWtGSrrQcw6AUoSFbqkZXx/V8qqIsfRc2e62 +ZB1+up596gsryoQ+gtzo3MppYD2rPycGLoWQLy0INZ998KqP/ZxVa7cR/4G8x28Txx4 q6PqTnR4aYeTZ3AfoZNoJ45rH1G/XfWPDhf+joAX9NVp14dQ1Y4jSnW+I2ZFdUcQmWaU i0YDwN8cHypxFtISp26freNSuYinHL0mOTcdos6Gp1SPIB9+bG8S55nEuQqbaYgGNCO2 laKw== X-Gm-Message-State: AAQBX9cjeDdBrSv+AWLBWUM0DRVEdEl7TbDBZ/wFEAGhyRmFwTCu27xC pqdnv+QW26adWfRfAyw7RbH1h8UG0evGTIzTRT6ww1Ei/CUFP/XObHM/OTH53TsctTjtYUJZSZM uWfiLUZrRlQQzyWNSci7U5KVDtIJa7cfuu6Ap1keJx/phSsFG+p+aeB0x1+6yYoLVzKpt6GMd8A == X-Received: by 2002:ac8:5c85:0:b0:3e4:cee3:7e4a with SMTP id r5-20020ac85c85000000b003e4cee37e4amr10626101qta.22.1681515748823; Fri, 14 Apr 2023 16:42:28 -0700 (PDT) X-Received: by 2002:ac8:5c85:0:b0:3e4:cee3:7e4a with SMTP id r5-20020ac85c85000000b003e4cee37e4amr10626084qta.22.1681515748401; Fri, 14 Apr 2023 16:42:28 -0700 (PDT) Received: from jason.com ([66.31.49.15]) by smtp.gmail.com with ESMTPSA id q22-20020a05620a0c9600b0073b7f2a0bcbsm1568810qki.36.2023.04.14.16.42.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 16:42:28 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH RFA] -Wdangling-pointer: fix MEM_REF handling [PR109514] Date: Fri, 14 Apr 2023 19:42:24 -0400 Message-Id: <20230414234224.2870389-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.9 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, 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: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill 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?1763197110181948290?= X-GMAIL-MSGID: =?utf-8?q?1763197110181948290?= Tested x86_64-pc-linux-gnu, ok for trunk? -- 8< -- Here we hit the MEM_REF case, with its arg an ADDR_EXPR, but had no handling for that and wrongly assumed it would be a reference to a local variable. This patch overhauls the logic for deciding whether the target is something to warn about so that we only warn if we specifically recognize the target as non-local. None of the existing tests regress as a result. PR c++/109514 gcc/ChangeLog: * gimple-ssa-warn-access.cc (pass_waccess::check_dangling_stores): Overhaul lhs_ref.ref analysis. gcc/testsuite/ChangeLog: * g++.dg/warn/Wdangling-pointer-6.C: New test. --- gcc/gimple-ssa-warn-access.cc | 50 ++++++++----------- .../g++.dg/warn/Wdangling-pointer-6.C | 30 +++++++++++ 2 files changed, 50 insertions(+), 30 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wdangling-pointer-6.C base-commit: f32f7881fb0db085479525b5a23db5dabd990c3b diff --git a/gcc/gimple-ssa-warn-access.cc b/gcc/gimple-ssa-warn-access.cc index b3de4b77924..d0d2148c872 100644 --- a/gcc/gimple-ssa-warn-access.cc +++ b/gcc/gimple-ssa-warn-access.cc @@ -4528,39 +4528,34 @@ pass_waccess::check_dangling_stores (basic_block bb, if (!m_ptr_qry.get_ref (lhs, stmt, &lhs_ref, 0)) continue; - if (auto_var_p (lhs_ref.ref)) - continue; - - if (DECL_P (lhs_ref.ref)) + if (TREE_CODE (lhs_ref.ref) == MEM_REF) { - if (!POINTER_TYPE_P (TREE_TYPE (lhs_ref.ref)) - || lhs_ref.deref > 0) - continue; + lhs_ref.ref = TREE_OPERAND (lhs_ref.ref, 0); + ++lhs_ref.deref; } - else if (TREE_CODE (lhs_ref.ref) == SSA_NAME) + if (TREE_CODE (lhs_ref.ref) == ADDR_EXPR) + { + lhs_ref.ref = TREE_OPERAND (lhs_ref.ref, 0); + --lhs_ref.deref; + } + if (TREE_CODE (lhs_ref.ref) == SSA_NAME) { gimple *def_stmt = SSA_NAME_DEF_STMT (lhs_ref.ref); if (!gimple_nop_p (def_stmt)) /* Avoid looking at or before stores into unknown objects. */ return; - tree var = SSA_NAME_VAR (lhs_ref.ref); - if (TREE_CODE (var) == PARM_DECL && DECL_BY_REFERENCE (var)) - /* Avoid by-value arguments transformed into by-reference. */ - continue; + lhs_ref.ref = SSA_NAME_VAR (lhs_ref.ref); + } - } - else if (TREE_CODE (lhs_ref.ref) == MEM_REF) - { - tree arg = TREE_OPERAND (lhs_ref.ref, 0); - if (TREE_CODE (arg) == SSA_NAME) - { - gimple *def_stmt = SSA_NAME_DEF_STMT (arg); - if (!gimple_nop_p (def_stmt)) - return; - } - } + if (TREE_CODE (lhs_ref.ref) == PARM_DECL + && (lhs_ref.deref - DECL_BY_REFERENCE (lhs_ref.ref)) > 0) + /* Assignment through a (real) pointer/reference parameter. */; + else if (TREE_CODE (lhs_ref.ref) == VAR_DECL + && !auto_var_p (lhs_ref.ref)) + /* Assignment to/through a non-local variable. */; else + /* Something else, don't warn. */ continue; if (stores.add (lhs_ref.ref)) @@ -4587,13 +4582,8 @@ pass_waccess::check_dangling_stores (basic_block bb, location_t loc = DECL_SOURCE_LOCATION (rhs_ref.ref); inform (loc, "%qD declared here", rhs_ref.ref); - if (DECL_P (lhs_ref.ref)) - loc = DECL_SOURCE_LOCATION (lhs_ref.ref); - else if (EXPR_HAS_LOCATION (lhs_ref.ref)) - loc = EXPR_LOCATION (lhs_ref.ref); - - if (loc != UNKNOWN_LOCATION) - inform (loc, "%qE declared here", lhs_ref.ref); + loc = DECL_SOURCE_LOCATION (lhs_ref.ref); + inform (loc, "%qD declared here", lhs_ref.ref); } } diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-pointer-6.C b/gcc/testsuite/g++.dg/warn/Wdangling-pointer-6.C new file mode 100644 index 00000000000..20d47edecc5 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wdangling-pointer-6.C @@ -0,0 +1,30 @@ +// PR c++/109514 +// { dg-do compile { target c++11 } } +// { dg-additional-options "-O2 -Werror=dangling-pointer" } + +struct _Rb_tree_node_base { + _Rb_tree_node_base *_M_parent; +}; +struct _Rb_tree_header { + _Rb_tree_node_base _M_header; + void _M_move_data() { _M_header._M_parent->_M_parent = &_M_header; } +}; +struct _Rb_tree { + _Rb_tree_header _M_impl; + _Rb_tree_node_base *&_M_root() { return _M_impl._M_header._M_parent; } + _Rb_tree(); + _Rb_tree &operator=(_Rb_tree &&); +}; +_Rb_tree &_Rb_tree::operator=(_Rb_tree &&) { + if (_M_root()) + _M_impl._M_move_data(); + return *this; +} +struct set { + _Rb_tree _M_t; +}; +set FilterRs(); +void f() { + set rs; + rs = FilterRs(); +};