From patchwork Wed Feb 21 00:15:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1901768 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=E7BWdU50; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TfcHx1JhLz23d2 for ; Wed, 21 Feb 2024 11:15:55 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0557E3858C29 for ; Wed, 21 Feb 2024 00:15:52 +0000 (GMT) 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 AB5DB3858D20 for ; Wed, 21 Feb 2024 00:15:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AB5DB3858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AB5DB3858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708474530; cv=none; b=PImhXSZGN9YIlRKy88ZBbGzZ+8N/fWd4QrzaetHWoG2o7eCn9RAaE1jfDy22iEf+x8L6oNl5xqicYHk4y1OtLTmEbRf6s745OTeK0DU8vbJqPFPRTU2gRxVKc0CRgAD+iwkFhEPUd0sOIna9Z04M6zvpjpJ9C2hbpsHKsT8w2FQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708474530; c=relaxed/simple; bh=iWwD7ctgf+lPmR9dc1e5kkI0IO6XMzPzw04sFSkdDzE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=DcQGCv6+p+nHYngvBnXpVxb2TVAkxribltZZ51uHNiDBYUEm1uPAEGdp6ep2IoxKgUbsALHninaraoazp5lIJT3UiKM0zfR7yRtpyzup24U3bg3vK70UiXwME+xtWouQYeZLvRYkH7nP+zm0m3H6A8iNgXj0aa5QhkrFr1DKYL4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708474527; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=pCVPZzWD1o5v9PatPvhKrhXxHyYqaAY1OR4m1o/k9To=; b=E7BWdU50IoMxkF0KjNM3/x9aCdg+7i0xeltcFp36/aA3HSt2ljyV7xOZKW10uNfJ6JsV37 LK3F2PvB73P/7+YjdADqr0Co86lVzS8f18RqVCiAxelvKx5EkCk7kY7FH9m5pXBgN34H5s L8DAICpMPSxdq6VYKEn/Ce/MD0SoTvo= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-493-bboz7_i5PjePzKQXWpa0SQ-1; Tue, 20 Feb 2024 19:15:24 -0500 X-MC-Unique: bboz7_i5PjePzKQXWpa0SQ-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 70549185A782 for ; Wed, 21 Feb 2024 00:15:21 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.17.189]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4C8CB1814; Wed, 21 Feb 2024 00:15:21 +0000 (UTC) From: Marek Polacek To: GCC Patches , Jason Merrill Subject: [PATCH] c++: -Wuninitialized when binding a ref to uninit DM [PR113987] Date: Tue, 20 Feb 2024 19:15:17 -0500 Message-ID: <20240221001517.478667-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.4 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, URIBL_BLACK 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- This PR asks that our -Wuninitialized for mem-initializers does not warn when binding a reference to an uninitialized data member. We already check !INDIRECT_TYPE_P in find_uninit_fields_r, but that won't catch binding a parameter of a reference type to an uninitialized field, as in: struct S { S (int&); }; struct T { T() : s(i) {} S s; int i; }; This patch adds a new function to handle this case. PR c++/113987 gcc/cp/ChangeLog: * call.cc (conv_binds_to_reference_parm_p): New. * cp-tree.h (conv_binds_to_reference_parm_p): Declare. * init.cc (find_uninit_fields_r): Call it. gcc/testsuite/ChangeLog: * g++.dg/warn/Wuninitialized-15.C: Turn dg-warning into dg-bogus. * g++.dg/warn/Wuninitialized-34.C: New test. --- gcc/cp/call.cc | 24 ++++++++++++++ gcc/cp/cp-tree.h | 1 + gcc/cp/init.cc | 3 +- gcc/testsuite/g++.dg/warn/Wuninitialized-15.C | 3 +- gcc/testsuite/g++.dg/warn/Wuninitialized-34.C | 32 +++++++++++++++++++ 5 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wuninitialized-34.C base-commit: ca5f509903315a56b2f306f8644544861a6525cb diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 1dac1470d3b..c40ef2e3028 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -14551,4 +14551,28 @@ maybe_show_nonconverting_candidate (tree to, tree from, tree arg, int flags) "function was not considered"); } +/* We're converting EXPR to TYPE. If that conversion involves a conversion + function and we're binding EXPR to a reference parameter of that function, + return true. */ + +bool +conv_binds_to_reference_parm_p (tree type, tree expr) +{ + conversion_obstack_sentinel cos; + conversion *c = implicit_conversion (type, TREE_TYPE (expr), expr, + /*c_cast_p=*/false, LOOKUP_NORMAL, + tf_none); + if (c && !c->bad_p && c->user_conv_p) + for (; c; c = next_conversion (c)) + if (c->kind == ck_user) + for (z_candidate *cand = c->cand; cand; cand = cand->next) + if (cand->viable == 1) + for (size_t i = 0; i < cand->num_convs; ++i) + if (cand->convs[i]->kind == ck_ref_bind + && conv_get_original_expr (cand->convs[i]) == expr) + return true; + + return false; +} + #include "gt-cp-call.h" diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 334c11396c2..ce2d85f1f86 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -6845,6 +6845,7 @@ extern void cp_warn_deprecated_use_scopes (tree); extern tree get_function_version_dispatcher (tree); extern bool any_template_arguments_need_structural_equality_p (tree); extern void maybe_show_nonconverting_candidate (tree, tree, tree, int); +extern bool conv_binds_to_reference_parm_p (tree, tree); /* in class.cc */ extern tree build_vfield_ref (tree, tree); diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc index ac37330527e..1a341f7e606 100644 --- a/gcc/cp/init.cc +++ b/gcc/cp/init.cc @@ -906,7 +906,8 @@ find_uninit_fields_r (tree *tp, int *walk_subtrees, void *data) warning_at (EXPR_LOCATION (init), OPT_Wuninitialized, "reference %qD is not yet bound to a value when used " "here", field); - else if (!INDIRECT_TYPE_P (type) || is_this_parameter (d->member)) + else if ((!INDIRECT_TYPE_P (type) || is_this_parameter (d->member)) + && !conv_binds_to_reference_parm_p (type, init)) warning_at (EXPR_LOCATION (init), OPT_Wuninitialized, "member %qD is used uninitialized", field); *walk_subtrees = false; diff --git a/gcc/testsuite/g++.dg/warn/Wuninitialized-15.C b/gcc/testsuite/g++.dg/warn/Wuninitialized-15.C index 89e90668c41..2fd33037bfd 100644 --- a/gcc/testsuite/g++.dg/warn/Wuninitialized-15.C +++ b/gcc/testsuite/g++.dg/warn/Wuninitialized-15.C @@ -65,8 +65,7 @@ struct H { G g; A a2; H() : g(a1) { } - // ??? clang++ doesn't warn here - H(int) : g(a2) { } // { dg-warning "member .H::a2. is used uninitialized" } + H(int) : g(a2) { } // { dg-bogus "member .H::a2. is used uninitialized" } }; struct I { diff --git a/gcc/testsuite/g++.dg/warn/Wuninitialized-34.C b/gcc/testsuite/g++.dg/warn/Wuninitialized-34.C new file mode 100644 index 00000000000..28226d8032e --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wuninitialized-34.C @@ -0,0 +1,32 @@ +// PR c++/113987 +// { dg-do compile } +// { dg-options "-Wuninitialized" } + +struct t1 { + t1(int); +}; +struct t2 { + t2(int&, int = 0); + t2(double&, int = 0); +}; +struct t3 { + t3(int&); +}; +struct t4 {}; +void f1(int&); +struct t { + t() : + v1(i), // { dg-warning "is used uninitialized" } + v2(i), + v3(i), + v4((f1(i), t4())), + v5(i) {} + t1 v1; + t2 v2; + t3 v3; + t4 v4; + t1 v5; + int i; + int j; +}; +int main() { t v1; }