From patchwork Tue Sep 17 21:16:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1986589 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=NOsJrG+A; 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 4X7ZSL2f7mz1y20 for ; Wed, 18 Sep 2024 07:20:18 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 28176385C6C9 for ; Tue, 17 Sep 2024 21:20:16 +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.133.124]) by sourceware.org (Postfix) with ESMTP id 599AD385842C for ; Tue, 17 Sep 2024 21:19:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 599AD385842C 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 599AD385842C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726607995; cv=none; b=cYUS3Cs1+ubIoAIj/GkHwGfgSyJtFPWCyCqFPpuS/raYGaWKOlaZTx9cyIqgiLNT+zCz4jUZ1HDDsyJbTwLBhZBjgL18Ggwl3slq6UxQJNoX7Yv+mT7eaKIOkwc6+V0Q7i++5cx/iO+zjikUrq+qpr4Glwx7ZOkHgxtniFc2fzU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726607995; c=relaxed/simple; bh=2rtoOiFb5SSmraoAyce5MFPnJBbJE/MXG20DpRO3Zdw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=TDAPq/o9EhhKLq+td3T2po5HsNG5So26oHcNcXoTlviSOee/owF9vicFsUl7w8G2IL4PSpHtjiu7yh38XRV7F1wGjLdJw/08ba3lIgezfo52eHZk0h4o344y50Zg/ji53cxlR+b+MrK8d8Y/73ndhlHtMbvrCAo0C+c+LaKsb54= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1726607993; 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=CH6r+SK5P1/0mBchwDLhNaDUKxDLtw+U9iZdCzCVlo0=; b=NOsJrG+AOg39LfpyHK7JhmNDzn+1/RqOimNG1qwCg2WpcJ/SElG4YCi0Afk8C0Fr0z2DUu gXxW5HRc4LAkSEf4zuEMOJvtWND6UILLJNrdFA1toRiX97Br9uRyns/zrqS0Y00s4iqGwd 1UZQwy9BdM0ODZZwYiMoMjDs0TBUBGc= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-176-k_kT1oJKNh6fmtSDOhKj6A-1; Tue, 17 Sep 2024 17:19:51 -0400 X-MC-Unique: k_kT1oJKNh6fmtSDOhKj6A-1 Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-4582493b3f7so122478521cf.0 for ; Tue, 17 Sep 2024 14:19:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726607991; x=1727212791; 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=CH6r+SK5P1/0mBchwDLhNaDUKxDLtw+U9iZdCzCVlo0=; b=PGpILrm4qOuS0e/NqlEa7qhQ8udRVFkddwHbHBT6obnq9F+txp5cYYEhMgNfu1Q3HI 8MsnsIygCgp9VBetgJ7UvKgSw/HLrRbGa4AhJDQL70mPlIz7LBShaRWoAQhwF2nv1XGX npW8DToxcuVYAj2wmkvqmE9o0eEV7/3ZYYT5xH9gaMvNtPmIvyuhsnXx/SOozaB5iCdM 0isvN7NACx/v69TH0Q64Uls+pDZqqBg1SWQ1XCbtOnl4AlQQxh7+Cw9WFuIA/rsB19mh o7ktWm8Utc1zsK/8CVxPLA1Rqi3ZgoSddug+WuzDaILA9RybbmoyjzenjhvImnVWUDce QTTg== X-Gm-Message-State: AOJu0YzdqOg9WGhsK0VZmjXdhn33IGcAup+jak1vBWmj5UBVm/X9VpET /qPO26lolR+Y/XtdXKzlJ7tlh1vqHSp94DF8yt6AwlWsRTJt2zEnmQ/2DdG7TAB2ULy0E1gAygb by1jrqSVmxDqd+EXkB5+38pvFvi4rpM7TGvCsEpOk73x1Awo7Ob8OGArKTt9CuFVTkE6L1eePs8 pTpOtypRuIc7CBJn3WXVm0iPRRHwMs293X/Ac= X-Received: by 2002:a05:622a:5b92:b0:458:4457:8e69 with SMTP id d75a77b69052e-4599d23e4e6mr230685451cf.20.1726607990777; Tue, 17 Sep 2024 14:19:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFhGlO2gohbyjV8wiF/GjXsDFSoXRXk3S5C1sMGBp3u8hYIyoh3Zk2l/V6RMo3WYAr8VBBJkQ== X-Received: by 2002:a05:622a:5b92:b0:458:4457:8e69 with SMTP id d75a77b69052e-4599d23e4e6mr230685051cf.20.1726607990296; Tue, 17 Sep 2024 14:19:50 -0700 (PDT) Received: from jason-thinkpadp1gen4i.rmtusma.csb (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-459aad07404sm42279211cf.61.2024.09.17.14.19.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Sep 2024 14:19:49 -0700 (PDT) From: Jason Merrill To: gcc-patches@gcc.gnu.org, Marek Polacek Subject: [PATCH 1/2] c++: -Wdangling-reference and empty class [PR115361] Date: Tue, 17 Sep 2024 17:16:47 -0400 Message-ID: <20240917211948.72205-1-jason@redhat.com> X-Mailer: git-send-email 2.46.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.2 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_H3, RCVD_IN_MSPIKE_WL, 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.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 Tested x86_64-pc-linux-gnu. Marek, any concerns? -- 8< -- We can't have a dangling reference to an empty class unless it's specifically to that class or one of its bases. This was giving a false positive on the _ExtractKey pattern in libstdc++ hashtable.h. This also adjusts the order of arguments to reference_related_p, which is relevant for empty classes (unlike scalars). Several of the classes in the testsuite needed to gain data members to continue to warn. PR c++/115361 gcc/cp/ChangeLog: * call.cc (do_warn_dangling_reference): Check is_empty_class. gcc/testsuite/ChangeLog: * g++.dg/ext/attr-no-dangling6.C: * g++.dg/ext/attr-no-dangling7.C: * g++.dg/ext/attr-no-dangling8.C: * g++.dg/ext/attr-no-dangling9.C: * g++.dg/warn/Wdangling-reference1.C: * g++.dg/warn/Wdangling-reference2.C: * g++.dg/warn/Wdangling-reference3.C: Make classes non-empty. * g++.dg/warn/Wdangling-reference23.C: New test. --- gcc/cp/call.cc | 12 +++++++----- gcc/testsuite/g++.dg/ext/attr-no-dangling6.C | 6 +++--- gcc/testsuite/g++.dg/ext/attr-no-dangling7.C | 6 +++--- gcc/testsuite/g++.dg/ext/attr-no-dangling8.C | 2 ++ gcc/testsuite/g++.dg/ext/attr-no-dangling9.C | 1 + gcc/testsuite/g++.dg/warn/Wdangling-reference1.C | 1 + gcc/testsuite/g++.dg/warn/Wdangling-reference2.C | 2 +- gcc/testsuite/g++.dg/warn/Wdangling-reference23.C | 14 ++++++++++++++ gcc/testsuite/g++.dg/warn/Wdangling-reference3.C | 1 + 9 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wdangling-reference23.C base-commit: a9f9391e1eeccb9d062b9e73ef27ac577b23ed64 diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 664088eed9c..1ecf3aac705 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -14356,12 +14356,14 @@ do_warn_dangling_reference (tree expr, bool arg_p) if ((arg = do_warn_dangling_reference (arg, /*arg_p=*/true))) { /* If we know the temporary could not bind to the return type, - don't warn. This is for scalars only because for classes - we can't be sure we are not returning its sub-object. */ - if (SCALAR_TYPE_P (TREE_TYPE (arg)) + don't warn. This is for scalars and empty classes only + because for other classes we can't be sure we are not + returning its sub-object. */ + if ((SCALAR_TYPE_P (TREE_TYPE (arg)) + || is_empty_class (TREE_TYPE (arg))) && TYPE_REF_P (rettype) - && !reference_related_p (TREE_TYPE (arg), - TREE_TYPE (rettype))) + && !reference_related_p (TREE_TYPE (rettype), + TREE_TYPE (arg))) continue; return expr; } diff --git a/gcc/testsuite/g++.dg/ext/attr-no-dangling6.C b/gcc/testsuite/g++.dg/ext/attr-no-dangling6.C index 5b349e8e682..1fc426d20d3 100644 --- a/gcc/testsuite/g++.dg/ext/attr-no-dangling6.C +++ b/gcc/testsuite/g++.dg/ext/attr-no-dangling6.C @@ -2,9 +2,9 @@ // { dg-do compile { target c++20 } } // { dg-options "-Wdangling-reference" } -class X { }; -const X x1; -const X x2; +class X { int i; }; +const X x1 {}; +const X x2 {}; constexpr bool val () { return true; } struct ST { static constexpr bool value = true; }; diff --git a/gcc/testsuite/g++.dg/ext/attr-no-dangling7.C b/gcc/testsuite/g++.dg/ext/attr-no-dangling7.C index a5fb809e6bd..04c6badf0b6 100644 --- a/gcc/testsuite/g++.dg/ext/attr-no-dangling7.C +++ b/gcc/testsuite/g++.dg/ext/attr-no-dangling7.C @@ -2,9 +2,9 @@ // { dg-do compile { target c++20 } } // { dg-options "-Wdangling-reference" } -class X { }; -const X x1; -const X x2; +class X { int i; }; +const X x1 {}; +const X x2 {}; template [[gnu::no_dangling(N)]] const X& get(const int& i); // { dg-error "parameter packs not expanded" } diff --git a/gcc/testsuite/g++.dg/ext/attr-no-dangling8.C b/gcc/testsuite/g++.dg/ext/attr-no-dangling8.C index 8208d751a4b..aa196315a38 100644 --- a/gcc/testsuite/g++.dg/ext/attr-no-dangling8.C +++ b/gcc/testsuite/g++.dg/ext/attr-no-dangling8.C @@ -8,6 +8,7 @@ template constexpr bool is_reference_v = true; template struct [[gnu::no_dangling(is_reference_v)]] S { + int i; int &foo (const int &); }; @@ -15,6 +16,7 @@ template struct X { template struct [[gnu::no_dangling(is_reference_v && is_reference_v)]] Y { + int i; int &foo (const int &); }; }; diff --git a/gcc/testsuite/g++.dg/ext/attr-no-dangling9.C b/gcc/testsuite/g++.dg/ext/attr-no-dangling9.C index 65b4f7145a9..d7fd897de53 100644 --- a/gcc/testsuite/g++.dg/ext/attr-no-dangling9.C +++ b/gcc/testsuite/g++.dg/ext/attr-no-dangling9.C @@ -12,6 +12,7 @@ using true_type = bool_constant; using false_type = bool_constant; struct S { + int i; template [[gnu::no_dangling(B)]] int &foo (const int &); }; diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference1.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference1.C index 1718c28165e..a184317dd5c 100644 --- a/gcc/testsuite/g++.dg/warn/Wdangling-reference1.C +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference1.C @@ -131,6 +131,7 @@ int n = 1; const int& refmax = max(n - 1, n + 1); // { dg-warning "dangling reference" } struct Y { + int i; operator int&(); operator int&&(); const int& foo(const int&); diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference2.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference2.C index dafdb43f1b9..a3d5ad6d867 100644 --- a/gcc/testsuite/g++.dg/warn/Wdangling-reference2.C +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference2.C @@ -3,7 +3,7 @@ // { dg-options "-Wdangling-reference" } namespace std { -struct any {}; +struct any { void *p; ~any(); }; template _ValueType any_cast(any &&); template struct remove_reference { using type = _Tp; }; template _Tp forward(typename remove_reference<_Tp>::type); diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference23.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference23.C new file mode 100644 index 00000000000..e59ccc5057b --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference23.C @@ -0,0 +1,14 @@ +// PR c++/115361 +// { dg-additional-options -Wdangling-reference } + +struct B { int i; }; + +struct A { + const int & operator()(const B& b) { return b.i; } +}; + +int main() +{ + B b = {}; + const int &r = A()(b); +} diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference3.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference3.C index 4bc20c13b3f..7db1dc86855 100644 --- a/gcc/testsuite/g++.dg/warn/Wdangling-reference3.C +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference3.C @@ -18,6 +18,7 @@ struct G { }; struct F { + int i; G& f(); }; From patchwork Tue Sep 17 21:16:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1986590 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=OlJi8+fY; 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 4X7ZTY4S2rz1y20 for ; Wed, 18 Sep 2024 07:21:21 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 718B2385C6D1 for ; Tue, 17 Sep 2024 21:21:19 +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 ESMTP id 51905385AC29 for ; Tue, 17 Sep 2024 21:19:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 51905385AC29 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 51905385AC29 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=1726607997; cv=none; b=XRHYvDopzAEmj56ay8nZZmdYrpwU8n/Rby+tQ8TZB9legvnGXBUsJ+nVKZ1gT1hIchjqMXsAdCsabwWgJ66sE6LMp+K2xfHxOdboEap4qieW/pO2LcTZLn8uahK6guGPOWibuuRJzgx1poqQc+aNpJPjoob8FZuD1fozYpGKAwc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726607997; c=relaxed/simple; bh=dVf3kTNA85JQrPGOrXunAydSiNyX0mDirnu0CRuO66Q=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=VG6/m6QJhWFaU9kLnxCMd4h/Uk6Gj9aGWGDWXbxurVyVY9i1aGGrqt9g+rpYf8U/WznXrJtWWnmOaXoXQSw6yID/9dhahXoMV1Kuf+r6peXpy5xvuauJ/pK5ijKIPEPPcokSnpQJR82WGOO0kxwokGHHfOQZvndsg1H1Fk2S5lQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1726607995; 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: in-reply-to:in-reply-to:references:references; bh=1/llmuf9/b1IJRwahCzdZESQ/1xtf4+kD4i+Gd0it+U=; b=OlJi8+fYSOhy/GjlarDGWw9GK3Dr/LWlvoS0kWz6mWSiTAcKP4HHO7iLn90PzQte2G2IVR aqJjIvnLQElf4T9bEH4qLmDwd9ozmJFw7Zf+PMXSxzvAjPt5wey0iC7ceC6Hxr5yEBaLN5 d1Ve6ZbZNNSB14iHYpEWBLWaF1UD1QQ= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-283-ttONfJlgOvSaAucLE8j-1Q-1; Tue, 17 Sep 2024 17:19:52 -0400 X-MC-Unique: ttONfJlgOvSaAucLE8j-1Q-1 Received: by mail-qt1-f199.google.com with SMTP id d75a77b69052e-4585482ca78so137365831cf.0 for ; Tue, 17 Sep 2024 14:19:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726607992; x=1727212792; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1/llmuf9/b1IJRwahCzdZESQ/1xtf4+kD4i+Gd0it+U=; b=EdFk/Wd0cQIlAv0f+TgzK54pd+vkd1SaR0fd6daHWl4+muV2R5R8aMobpU1YlmJZwL QvaUyqGbT2Sj47ZNJlZiI+qebFU9x1jK3ynShQSTAO0yQBFhb1nbu4uAmPtswYcfNQWi LXo6K55G2L9uuYPNKpBfEL0fPDKRWnmV2eGkf7doyEErDmN3M8shydEwcnmlaGf/Or9M mptdZcbqwnzZtw4rKPyEgL4apZl8QKTMLdZRVSGJ++zmJTbCZbx86IV5RGvSt2JV4/yO CvhoBpq9aCxdeOa3To/5AJZsrLWAOJBhh1DKRQXJIpmB66dQTOP1+2EoLJqIB/KZ00c0 2jQw== X-Gm-Message-State: AOJu0YyFFQkuWVOPqt3B4ygc/QTJ+SpZ5yt8yqvMmRElN/q7+1jcQnvR Dy/FvOpv5NlEvqxJ76mz78UBXsx+uvJow/JjYXLi62hufFwvlbzTffv4xlekpQhJ0UrH/CHiu9Y 1iBLPH7pwEePfDol+anvdf/tv55kNpDZeouZWR8ZfCXxK+VgzhruKJeAI8iTrl25y1B80VY4UaF nnhFEo5QXNB8uK9nZ3hDiidup3iFZr/iCjlKE= X-Received: by 2002:ac8:5ad0:0:b0:458:294c:39e7 with SMTP id d75a77b69052e-458603ebf00mr326971521cf.38.1726607991656; Tue, 17 Sep 2024 14:19:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF9aNljmITjjyCot2NLtN73nYwXmNnGkvcX8MFXpwU4KUBlHQwRZ71lIdD+RgPQIi5TF9Lv8g== X-Received: by 2002:ac8:5ad0:0:b0:458:294c:39e7 with SMTP id d75a77b69052e-458603ebf00mr326971091cf.38.1726607991194; Tue, 17 Sep 2024 14:19:51 -0700 (PDT) Received: from jason-thinkpadp1gen4i.rmtusma.csb (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-459aad07404sm42279211cf.61.2024.09.17.14.19.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Sep 2024 14:19:50 -0700 (PDT) From: Jason Merrill To: gcc-patches@gcc.gnu.org, Marek Polacek Subject: [PATCH 2/2] c++: -Wdangling-reference diagnostic Date: Tue, 17 Sep 2024 17:16:48 -0400 Message-ID: <20240917211948.72205-2-jason@redhat.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240917211948.72205-1-jason@redhat.com> References: <20240917211948.72205-1-jason@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.2 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_H3, RCVD_IN_MSPIKE_WL, 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.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 Tested x86_64-pc-linux-gnu. Marek, thoughts? -- 8< -- The -Wdangling-reference diagnostic talks about the full-expression, but prints one call, while the full-expression in a declaration is the entire initialization. It seems more useful to point out the temporary that the compiler thinks we might be getting a dangling reference to. gcc/cp/ChangeLog: * call.cc (do_warn_dangling_reference): Return temporary instead of the call it's passed to. (maybe_warn_dangling_reference): Adjust diagnostic. gcc/testsuite/ChangeLog: * g++.dg/warn/Wdangling-reference1.C: Adjust diagnostic. --- gcc/cp/call.cc | 23 +++++++++---------- .../g++.dg/warn/Wdangling-reference1.C | 2 +- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 1ecf3aac705..3f753e2d2f9 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -14253,19 +14253,18 @@ reference_like_class_p (tree ctype) return false; } -/* Helper for maybe_warn_dangling_reference to find a problematic CALL_EXPR - that initializes the LHS (and at least one of its arguments represents - a temporary, as outlined in maybe_warn_dangling_reference), or NULL_TREE +/* Helper for maybe_warn_dangling_reference to find a problematic temporary + in EXPR (as outlined in maybe_warn_dangling_reference), or NULL_TREE if none found. For instance: - const S& s = S().self(); // S::self (&TARGET_EXPR <...>) - const int& r = (42, f(1)); // f(1) - const int& t = b ? f(1) : f(2); // f(1) - const int& u = b ? f(1) : f(g); // f(1) - const int& v = b ? f(g) : f(2); // f(2) + const S& s = S().self(); // S() + const int& r = (42, f(1)); // temporary for passing 1 to f + const int& t = b ? f(1) : f(2); // temporary for 1 + const int& u = b ? f(1) : f(g); // temporary for 1 + const int& v = b ? f(g) : f(2); // temporary for 2 const int& w = b ? f(g) : f(g); // NULL_TREE const int& y = (f(1), 42); // NULL_TREE - const int& z = f(f(1)); // f(f(1)) + const int& z = f(f(1)); // temporary for 1 EXPR is the initializer. If ARG_P is true, we're processing an argument to a function; the point is to distinguish between, for example, @@ -14365,7 +14364,7 @@ do_warn_dangling_reference (tree expr, bool arg_p) && !reference_related_p (TREE_TYPE (rettype), TREE_TYPE (arg))) continue; - return expr; + return arg; } /* Don't warn about member functions like: std::any a(...); @@ -14438,8 +14437,8 @@ maybe_warn_dangling_reference (const_tree decl, tree init) auto_diagnostic_group d; if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wdangling_reference, "possibly dangling reference to a temporary")) - inform (EXPR_LOCATION (call), "the temporary was destroyed at " - "the end of the full expression %qE", call); + inform (EXPR_LOCATION (call), "%qT temporary created here", + TREE_TYPE (call)); } } diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference1.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference1.C index a184317dd5c..5e60a415836 100644 --- a/gcc/testsuite/g++.dg/warn/Wdangling-reference1.C +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference1.C @@ -117,7 +117,7 @@ const B& b10 = lox (H{}); // { dg-warning "dangling reference" } struct S { const int &r; // { dg-warning "dangling reference" } - S() : r(f(10)) { } // { dg-message "destroyed" } + S() : r(f(10)) { } // { dg-message "created" } }; // From cppreference.