From patchwork Thu Feb 22 00:35:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1902552 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=hODqZ/cP; 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 4TgDh972Lgz23hY for ; Thu, 22 Feb 2024 11:35:37 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 417293858417 for ; Thu, 22 Feb 2024 00:35:35 +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 88EE13858D20 for ; Thu, 22 Feb 2024 00:35:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 88EE13858D20 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 88EE13858D20 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=1708562110; cv=none; b=qHSArRFtb8W9PsH+jjcsIciQrBCrcXhVXdi4KaWILXv95eqktjGGNscRR2+PnElUZnQAN/vtCExi82NULs70vubp414oVH5XjDrSCtcDjLMZe1rxecTbDEWQcMy0qMQaLsq6eb3Bow38IWuW3hHOapmX8MJXgIbySBrd8AOAGy0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708562110; c=relaxed/simple; bh=e+xGPJ61mRjBCGwSLWfxT5W5qXoAu5TTGlwmEF6fSF0=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=XsYDqyE2RC6ikcKtaTogkgjL9SsHo8LTqHlPfdBXKLNlvDEFH18vjnM3z+k27vujMvNVGUQqi7T9DKXfBx4gqHpG32FBAJyp97fKZP4VlKygw1ZvFJ4R4BhSsP0WlCSJlQearN0g/yAZu7FtDjhhg0mbDL3/RXjN+bofeIQYoMc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708562106; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=GOTOyHJzZ5GShUGMzJZVeeoKHI6jqu0e5qjx3Yznuqs=; b=hODqZ/cPBQvKRDw3JwsBagHYHHyJx4jSa4yq/l0eZBCDuQjUhz4oIbhpHbYWLIHvV3bebK LwfjJsSoOf1sHAX1dvq6UvRElSxd6noda0NZ1As1wiDcehyzddHrs1HdBVmLGlhbwL/+5g 4VPaSJW4wGV30krAwYNm1EhVneMyFR0= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-463-FwOfDym3PtOPLe_UVl7zwA-1; Wed, 21 Feb 2024 19:35:04 -0500 X-MC-Unique: FwOfDym3PtOPLe_UVl7zwA-1 Received: by mail-qk1-f197.google.com with SMTP id af79cd13be357-7876f29a8daso206102485a.3 for ; Wed, 21 Feb 2024 16:35:04 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708562104; x=1709166904; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GOTOyHJzZ5GShUGMzJZVeeoKHI6jqu0e5qjx3Yznuqs=; b=ewRLMOrlfT+29ao5E4X8gPhU9h4qw6zYHR7teVq5K2+SCaM74QpN9Lze15+hN9hkjz 3jUjRxsPODbGsoPAPGC9x8S7e7j9r15O8C9lyJx5q9uzrW2KxLFSAiko01cGhB83CXZw tGAYvkb5u/+CxcCWM+sfXBy2n7VYhFPxeUVQlR+6AENel9HlzC0S3WDR+NiaU8DMF/qN jfXF/H20s14nvNsBTfp0Is28ADK0u1+GmeRpGlg9nhjrJt7uhXbCKSPTSRirU8Zh6zOq EH/47+OE4LmQpmJ4XRW0POgTpX5or1cYjm7R4C5VLmQQng8T2w2ZANJMyhS5PgLltHf5 kbRw== X-Gm-Message-State: AOJu0YwDu6P4Iq2Z9T4oTeE0Qr5V7XINm1b/Id04GT+014AUtPMvWy5a dJU2z6pMbsjOyGv6+QMJAZit5IvkrrGEPdoOJlXY7kCkmqa4DabeKufCj0lsKtaOHsUxm8vkKPE tscRt2VX8TlX9FZo5DUKFVtsl4c6g1pqk5j5H2XPy2LTFPuRVVCL1eiiZaZ6DG/E= X-Received: by 2002:a05:620a:1375:b0:787:3769:d747 with SMTP id d21-20020a05620a137500b007873769d747mr19479608qkl.59.1708562103726; Wed, 21 Feb 2024 16:35:03 -0800 (PST) X-Google-Smtp-Source: AGHT+IGkwMKjpJ27peFgXZ1XN1yofFpFviR7w0j4oo4kz0Poamei5ooXKKJU/5TpHyzK8k7tLW3eKA== X-Received: by 2002:a05:620a:1375:b0:787:3769:d747 with SMTP id d21-20020a05620a137500b007873769d747mr19479587qkl.59.1708562103276; Wed, 21 Feb 2024 16:35:03 -0800 (PST) Received: from redhat.com (2603-7000-9500-34a5-0000-0000-0000-1db4.res6.spectrum.com. [2603:7000:9500:34a5::1db4]) by smtp.gmail.com with ESMTPSA id j11-20020a05620a0a4b00b0078717dd799bsm4779706qka.117.2024.02.21.16.35.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 16:35:02 -0800 (PST) Date: Wed, 21 Feb 2024 19:35:01 -0500 From: Marek Polacek To: Jason Merrill Cc: GCC Patches Subject: [PATCH v2] c++: implement [[gnu::non_owning]] [PR110358] Message-ID: References: <20240126013736.70125-1-polacek@redhat.com> <0e4c47b6-604c-4d30-b458-825959c0e1d6@redhat.com> MIME-Version: 1.0 In-Reply-To: <0e4c47b6-604c-4d30-b458-825959c0e1d6@redhat.com> User-Agent: Mutt/2.2.12 (2023-09-09) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-12.3 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.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 On Fri, Jan 26, 2024 at 04:04:35PM -0500, Jason Merrill wrote: > On 1/25/24 20:37, Marek Polacek wrote: > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > > > -- >8 -- > > Since -Wdangling-reference has false positives that can't be > > prevented, we should offer an easy way to suppress the warning. > > Currently, that is only possible by using a #pragma, either around the > > enclosing class or around the call site. But #pragma GCC diagnostic tend > > to be onerous. A better solution would be to have an attribute. Such > > an attribute should not be tied to this particular warning though. [*] > > > > The warning bogusly triggers for classes that are like std::span, > > std::reference_wrapper, and std::ranges::ref_view. The common property > > seems to be that these classes are only wrappers around some data. So > > I chose the name non_owning, but I'm not attached to it. I hope that > > in the future the attribute can be used for something other than this > > diagnostic. > > You decided not to pursue Barry's request for a bool argument to the > attribute? At first I thought it'd be an unnecessary complication but it was actually pretty easy. Better to accept the optional argument from the get-go otherwise people would have to add > GCC 14 checks. > Might it be more useful for the attribute to make reference_like_class_p > return true, so that we still warn about a temporary of another type passing > through it? Good point. Fixed. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- Since -Wdangling-reference has false positives that can't be prevented, we should offer an easy way to suppress the warning. Currently, that is only possible by using a #pragma, either around the enclosing class or around the call site. But #pragma GCC diagnostic tend to be onerous. A better solution would be to have an attribute. Such an attribute should not be tied to this particular warning though. The warning bogusly triggers for classes that are like std::span, std::reference_wrapper, and std::ranges::ref_view. The common property seems to be that these classes are only wrappers around some data. So I chose the name non_owning, but I'm not attached to it. I hope that in the future the attribute can be used for something other than this diagnostic. This attribute takes an optional bool argument to support cases like: template struct [[gnu::non_owning(std::is_reference_v)]] S { // ... }; PR c++/110358 PR c++/109642 gcc/cp/ChangeLog: * call.cc (non_owning_p): New. (reference_like_class_p): Use it. (do_warn_dangling_reference): Use it. Don't warn when the function or its enclosing class has attribute gnu::non_owning. * tree.cc (cxx_gnu_attributes): Add gnu::non_owning. (handle_non_owning_attribute): New. gcc/ChangeLog: * doc/extend.texi: Document gnu::non_owning. * doc/invoke.texi: Mention that gnu::non_owning disables -Wdangling-reference. gcc/testsuite/ChangeLog: * g++.dg/ext/attr-non-owning1.C: New test. * g++.dg/ext/attr-non-owning2.C: New test. * g++.dg/ext/attr-non-owning3.C: New test. * g++.dg/ext/attr-non-owning4.C: New test. * g++.dg/ext/attr-non-owning5.C: New test. * g++.dg/ext/attr-non-owning6.C: New test. * g++.dg/ext/attr-non-owning7.C: New test. * g++.dg/ext/attr-non-owning8.C: New test. * g++.dg/ext/attr-non-owning9.C: New test. --- gcc/cp/call.cc | 38 ++++++++++-- gcc/cp/tree.cc | 26 +++++++++ gcc/doc/extend.texi | 25 ++++++++ gcc/doc/invoke.texi | 21 +++++++ gcc/testsuite/g++.dg/ext/attr-non-owning1.C | 38 ++++++++++++ gcc/testsuite/g++.dg/ext/attr-non-owning2.C | 29 +++++++++ gcc/testsuite/g++.dg/ext/attr-non-owning3.C | 24 ++++++++ gcc/testsuite/g++.dg/ext/attr-non-owning4.C | 14 +++++ gcc/testsuite/g++.dg/ext/attr-non-owning5.C | 31 ++++++++++ gcc/testsuite/g++.dg/ext/attr-non-owning6.C | 65 +++++++++++++++++++++ gcc/testsuite/g++.dg/ext/attr-non-owning7.C | 31 ++++++++++ gcc/testsuite/g++.dg/ext/attr-non-owning8.C | 30 ++++++++++ gcc/testsuite/g++.dg/ext/attr-non-owning9.C | 25 ++++++++ 13 files changed, 391 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/g++.dg/ext/attr-non-owning1.C create mode 100644 gcc/testsuite/g++.dg/ext/attr-non-owning2.C create mode 100644 gcc/testsuite/g++.dg/ext/attr-non-owning3.C create mode 100644 gcc/testsuite/g++.dg/ext/attr-non-owning4.C create mode 100644 gcc/testsuite/g++.dg/ext/attr-non-owning5.C create mode 100644 gcc/testsuite/g++.dg/ext/attr-non-owning6.C create mode 100644 gcc/testsuite/g++.dg/ext/attr-non-owning7.C create mode 100644 gcc/testsuite/g++.dg/ext/attr-non-owning8.C create mode 100644 gcc/testsuite/g++.dg/ext/attr-non-owning9.C base-commit: 98004ca00e4bf7a513cf3de65d3c3d9ad373872e diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 1dac1470d3b..e4bf9c963bd 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -14033,11 +14033,7 @@ std_pair_ref_ref_p (tree t) return true; } -/* Return true if a class CTYPE is either std::reference_wrapper or - std::ref_view, or a reference wrapper class. We consider a class - a reference wrapper class if it has a reference member. We no - longer check that it has a constructor taking the same reference type - since that approach still generated too many false positives. */ +/* Return true if a class T has a reference member. */ static bool class_has_reference_member_p (tree t) @@ -14061,12 +14057,41 @@ class_has_reference_member_p_r (tree binfo, void *) ? integer_one_node : NULL_TREE); } + +/* Return true if T (either a class or a function) has been marked as + non-owning. */ + +static bool +non_owning_p (tree t) +{ + t = lookup_attribute ("non_owning", TYPE_ATTRIBUTES (t)); + if (!t) + return false; + + t = TREE_VALUE (t); + if (!t) + return true; + + t = build_converted_constant_bool_expr (TREE_VALUE (t), tf_warning_or_error); + t = cxx_constant_value (t); + return t == boolean_true_node; +} + +/* Return true if a class CTYPE is either std::reference_wrapper or + std::ref_view, or a reference wrapper class. We consider a class + a reference wrapper class if it has a reference member. We no + longer check that it has a constructor taking the same reference type + since that approach still generated too many false positives. */ + static bool reference_like_class_p (tree ctype) { if (!CLASS_TYPE_P (ctype)) return false; + if (non_owning_p (ctype)) + return true; + /* Also accept a std::pair. */ if (std_pair_ref_ref_p (ctype)) return true; @@ -14173,7 +14198,8 @@ do_warn_dangling_reference (tree expr, bool arg_p) but probably not to one of its arguments. */ || (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl) && DECL_OVERLOADED_OPERATOR_P (fndecl) - && DECL_OVERLOADED_OPERATOR_IS (fndecl, INDIRECT_REF))) + && DECL_OVERLOADED_OPERATOR_IS (fndecl, INDIRECT_REF)) + || non_owning_p (TREE_TYPE (fndecl))) return NULL_TREE; tree rettype = TREE_TYPE (TREE_TYPE (fndecl)); diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc index ad312710f68..017da7e294a 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -47,6 +47,7 @@ static tree verify_stmt_tree_r (tree *, int *, void *); static tree handle_init_priority_attribute (tree *, tree, tree, int, bool *); static tree handle_abi_tag_attribute (tree *, tree, tree, int, bool *); static tree handle_contract_attribute (tree *, tree, tree, int, bool *); +static tree handle_non_owning_attribute (tree *, tree, tree, int, bool *); /* If REF is an lvalue, returns the kind of lvalue that REF is. Otherwise, returns clk_none. */ @@ -5102,6 +5103,8 @@ static const attribute_spec cxx_gnu_attributes[] = handle_init_priority_attribute, NULL }, { "abi_tag", 1, -1, false, false, false, true, handle_abi_tag_attribute, NULL }, + { "non_owning", 0, 1, false, true, false, false, + handle_non_owning_attribute, NULL }, }; const scoped_attribute_specs cxx_gnu_attribute_table = @@ -5391,6 +5394,29 @@ handle_contract_attribute (tree *ARG_UNUSED (node), tree ARG_UNUSED (name), return NULL_TREE; } +/* Handle a "non_owning" attribute; arguments as in + struct attribute_spec.handler. */ + +tree +handle_non_owning_attribute (tree *node, tree name, tree args, int, + bool *no_add_attrs) +{ + if (args && TREE_CODE (TREE_VALUE (args)) == STRING_CST) + { + error ("%qE attribute argument must be an expression that evaluates " + "to true or false", name); + *no_add_attrs = true; + } + else if (!FUNC_OR_METHOD_TYPE_P (*node) + && !RECORD_OR_UNION_TYPE_P (*node)) + { + warning (OPT_Wattributes, "%qE attribute ignored", name); + *no_add_attrs = true; + } + + return NULL_TREE; +} + /* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the thing pointed to by the constant. */ diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 2135dfde9c8..9132add8267 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -29320,6 +29320,31 @@ Some_Class B __attribute__ ((init_priority (543))); Note that the particular values of @var{priority} do not matter; only their relative ordering. +@cindex @code{non_owning} type attribute +@item non_owning + +This attribute can be applied on a class type, function, or member +function and indicates that it does not own its associated data. For +example, classes like @code{std::span} or @code{std::reference_wrapper} +are considered non-owning. + +@smallexample +class [[gnu::non_owning]] S @{ @dots{} @}; +@end smallexample + +This attribute takes an optional argument, which must be an expression that +evaluates to true or false: + +@smallexample +template +struct [[gnu::non_owning(std::is_reference_v)]] S @{ + @dots{} +@}; +@end smallexample + +Currently, the only effect this attribute has is to suppress the +@option{-Wdangling-reference} diagnostic. + @cindex @code{warn_unused} type attribute @item warn_unused diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index b4e4ee9fb81..0df2e2ded34 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -3908,6 +3908,9 @@ const T& foo (const T&) @{ @dots{} @} #pragma GCC diagnostic pop @end smallexample +The @code{#pragma} can also surround the class; in that case, the warning +will be disabled for all the member functions. + @option{-Wdangling-reference} also warns about code like @smallexample @@ -3932,6 +3935,24 @@ struct Span @{ as @code{std::span}-like; that is, the class is a non-union class that has a pointer data member and a trivial destructor. +The warning can be disabled by using the @code{gnu::non_owning} attribute, +which can be applied on the enclosing class type (in which case it disables +the warning for all its member functions), member function, or a regular +function. For example: + +@smallexample +class [[gnu::non_owning]] A @{ + int *p; + int &foo() @{ return *p; @} +@}; + +[[gnu::non_owning]] const int & +foo (const int &i) +@{ + @dots{} +@} +@end smallexample + This warning is enabled by @option{-Wall}. @opindex Wdelete-non-virtual-dtor diff --git a/gcc/testsuite/g++.dg/ext/attr-non-owning1.C b/gcc/testsuite/g++.dg/ext/attr-non-owning1.C new file mode 100644 index 00000000000..000f7f4019d --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/attr-non-owning1.C @@ -0,0 +1,38 @@ +// { dg-do compile { target c++11 } } +// { dg-options "-Wdangling-reference" } + +int g = 42; + +struct [[gnu::non_owning]] A { + int *i; + int &foo() { return *i; } +}; + +struct A2 { + int *i; + [[gnu::non_owning]] int &foo() { return *i; } + [[gnu::non_owning]] static int &bar (const int &) { return *&g; } +}; + +union [[gnu::non_owning]] U { }; + +A a() { return A{&g}; } +A2 a2() { return A2{&g}; } + +class X { }; +const X x1; +const X x2; + +[[gnu::non_owning]] const X& get(const int& i) +{ + return i == 0 ? x1 : x2; +} + +void +test () +{ + [[maybe_unused]] const X& x = get (10); // { dg-bogus "dangling" } + [[maybe_unused]] const int &i = a().foo(); // { dg-bogus "dangling" } + [[maybe_unused]] const int &j = a2().foo(); // { dg-bogus "dangling" } + [[maybe_unused]] const int &k = a2().bar(10); // { dg-bogus "dangling" } +} diff --git a/gcc/testsuite/g++.dg/ext/attr-non-owning2.C b/gcc/testsuite/g++.dg/ext/attr-non-owning2.C new file mode 100644 index 00000000000..4d2037e8b78 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/attr-non-owning2.C @@ -0,0 +1,29 @@ +// { dg-do compile { target c++11 } } +// Negative tests. + +struct [[non_owning]] A { // { dg-warning "ignored" } + [[non_owning]] int &foo (int &); // { dg-warning "ignored" } +}; + +[[non_owning]] int &bar (int &); // { dg-warning "ignored" } + +[[gnu::non_owning]] int i; // { dg-warning "ignored" } +[[gnu::non_owning]] double d; // { dg-warning "ignored" } +[[gnu::non_owning]] typedef int T; // { dg-warning "ignored" } + +[[gnu::non_owning()]] int &fn1 (int &); // { dg-error "parentheses" } +[[gnu::non_owning("a")]] int &fn2 (int &); // { dg-error "must be an expression" } +[[gnu::non_owning(true, true)]] int &fn3 (int &); // { dg-error "wrong number of arguments" } + +enum [[gnu::non_owning]] E { // { dg-warning "ignored" } + X [[gnu::non_owning]] // { dg-warning "ignored" } +}; + +[[gnu::non_owning]]; // { dg-warning "ignored" } + +void +g () +{ + goto L; +[[gnu::non_owning]] L:; // { dg-warning "ignored" } +} diff --git a/gcc/testsuite/g++.dg/ext/attr-non-owning3.C b/gcc/testsuite/g++.dg/ext/attr-non-owning3.C new file mode 100644 index 00000000000..81c6491643b --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/attr-non-owning3.C @@ -0,0 +1,24 @@ +// { dg-do compile { target c++11 } } +// { dg-options "-Wdangling-reference" } + +template +struct [[gnu::non_owning]] Span { + T* data_; + int len_; + // So that our heuristic doesn't suppress the warning anyway. + ~Span(); + + [[nodiscard]] constexpr auto operator[](int n) const noexcept -> T& { return data_[n]; } + [[nodiscard]] constexpr auto front() const noexcept -> T& { return data_[0]; } + [[nodiscard]] constexpr auto back() const noexcept -> T& { return data_[len_ - 1]; } +}; + +auto get() -> Span; + +auto f() -> int { + int const& a = get().front(); // { dg-bogus "dangling" } + int const& b = get().back(); // { dg-bogus "dangling" } + int const& c = get()[0]; // { dg-bogus "dangling" } + + return a + b + c; +} diff --git a/gcc/testsuite/g++.dg/ext/attr-non-owning4.C b/gcc/testsuite/g++.dg/ext/attr-non-owning4.C new file mode 100644 index 00000000000..c50382c3e8f --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/attr-non-owning4.C @@ -0,0 +1,14 @@ +// { dg-do compile { target c++11 } } + +#if !__has_attribute(non_owning) +#error unsupported +#endif + +#ifdef __has_cpp_attribute +# if !__has_cpp_attribute(non_owning) +# error non_owning +# endif +#endif + +struct [[gnu::non_owning]] S { }; +static_assert (__builtin_has_attribute (S, non_owning), ""); diff --git a/gcc/testsuite/g++.dg/ext/attr-non-owning5.C b/gcc/testsuite/g++.dg/ext/attr-non-owning5.C new file mode 100644 index 00000000000..d297bb615fe --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/attr-non-owning5.C @@ -0,0 +1,31 @@ +// PR c++/110358 +// { dg-do compile { target c++20 } } +// { dg-options "-Wdangling-reference" } + +template +struct Span { + T* data_; + int len_; + ~Span(); + + [[nodiscard]] constexpr auto operator[](int n) const noexcept -> T& { return data_[n]; } +}; + +template <> +struct [[gnu::non_owning]] Span { + int* data_; + int len_; + ~Span(); + + [[nodiscard]] constexpr auto operator[](int n) const noexcept -> int& { return data_[n]; } +}; + +auto getch() -> Span; +auto geti() -> Span; + +void +f () +{ + [[maybe_unused]] const auto &a = getch()[0]; // { dg-warning "dangling reference" } + [[maybe_unused]] const auto &b = geti()[0]; // { dg-bogus "dangling reference" } +} diff --git a/gcc/testsuite/g++.dg/ext/attr-non-owning6.C b/gcc/testsuite/g++.dg/ext/attr-non-owning6.C new file mode 100644 index 00000000000..10ce1842852 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/attr-non-owning6.C @@ -0,0 +1,65 @@ +// PR c++/110358 +// { dg-do compile { target c++20 } } +// { dg-options "-Wdangling-reference" } + +class X { }; +const X x1; +const X x2; + +constexpr bool val () { return true; } +struct ST { static constexpr bool value = true; }; +struct SF { static constexpr bool value = false; }; + +template +[[gnu::non_owning(T::value)]] +const X& get (const int& i) +{ + return i == 0 ? x1 : x2; +} + +template +[[gnu::non_owning(B)]] +const X& foo (const int& i) +{ + return i == 0 ? x1 : x2; +} + +[[gnu::non_owning(val ())]] +const X& bar (const int& i) +{ + return i == 0 ? x1 : x2; +} + +[[gnu::non_owning(!val ())]] +const X& baz (const int& i) +{ + return i == 0 ? x1 : x2; +} + +template +struct [[gnu::non_owning(T::value)]] +Span { + T* data_; + int len_; + ~Span(); + + [[nodiscard]] constexpr auto operator[](int n) const noexcept -> T& { return data_[n]; } +}; + +auto geti() -> Span; +auto gety() -> Span; + +void +test () +{ + [[maybe_unused]] const X& x1 = get (10); // { dg-bogus "dangling" } + [[maybe_unused]] const X& x2 = get (10); // { dg-warning "dangling" } + [[maybe_unused]] const X& x3 = foo (10); // { dg-bogus "dangling" } + [[maybe_unused]] const X& x4 = foo (10); // { dg-warning "dangling" } + [[maybe_unused]] const X& x7 = foo<> (10); // { dg-bogus "dangling" } + [[maybe_unused]] const X& x5 = bar (10); // { dg-bogus "dangling" } + [[maybe_unused]] const X& x6 = baz (10); // { dg-warning "dangling" } + + [[maybe_unused]] const auto &b1 = geti()[0]; // { dg-bogus "dangling" } + [[maybe_unused]] const auto &b2 = gety()[0]; // { dg-warning "dangling" } +} diff --git a/gcc/testsuite/g++.dg/ext/attr-non-owning7.C b/gcc/testsuite/g++.dg/ext/attr-non-owning7.C new file mode 100644 index 00000000000..bba69871918 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/attr-non-owning7.C @@ -0,0 +1,31 @@ +// PR c++/110358 +// { dg-do compile { target c++20 } } +// { dg-options "-Wdangling-reference" } + +class X { }; +const X x1; +const X x2; + +template +[[gnu::non_owning(N)]] const X& get(const int& i); // { dg-error "parameter packs not expanded" } + +template +[[gnu::non_owning(T::x)]] // { dg-error "member" } +const X& foo(const int& i); + +bool val () { return true; } + +[[gnu::non_owning(val ())]] // { dg-error "call" } +const X& bar (const int& i); + +[[gnu::non_owning(20)]] const X& fn1 (const int &); + +void +test () +{ + [[maybe_unused]] const X& x1 = bar (10); // { dg-warning "dangling" } + [[maybe_unused]] const X& x2 = foo (10); // { dg-error "no matching" } + [[maybe_unused]] const X& x3 // { dg-warning "dangling" } + = fn1 (10); // { dg-error "narrowing" } +} + diff --git a/gcc/testsuite/g++.dg/ext/attr-non-owning8.C b/gcc/testsuite/g++.dg/ext/attr-non-owning8.C new file mode 100644 index 00000000000..1bfed4ed33c --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/attr-non-owning8.C @@ -0,0 +1,30 @@ +// PR c++/110358 +// { dg-do compile { target c++20 } } +// { dg-options "-Wdangling-reference" } + +template constexpr bool is_reference_v = false; +template constexpr bool is_reference_v = true; +template constexpr bool is_reference_v = true; + +template +struct [[gnu::non_owning(is_reference_v)]] S { + int &foo (const int &); +}; + +template +struct X { + template + struct [[gnu::non_owning(is_reference_v && is_reference_v)]] Y { + int &foo (const int &); + }; +}; + +void +g () +{ + [[maybe_unused]] const int &x0 = S().foo (42); // { dg-bogus "dangling" } + [[maybe_unused]] const int &x1 = S().foo (42); // { dg-warning "dangling" } + [[maybe_unused]] const auto &x2 = X::Y<>().foo (42); // { dg-warning "dangling" } + [[maybe_unused]] const auto &x3 = X::Y<>().foo (42); // { dg-bogus "dangling" } +} + diff --git a/gcc/testsuite/g++.dg/ext/attr-non-owning9.C b/gcc/testsuite/g++.dg/ext/attr-non-owning9.C new file mode 100644 index 00000000000..6f6a72736ce --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/attr-non-owning9.C @@ -0,0 +1,25 @@ +// PR c++/110358 +// { dg-do compile { target c++20 } } +// { dg-options "-Wdangling-reference" } + +template +struct bool_constant { + static constexpr bool value = B; + constexpr operator bool() const { return value; } +}; + +using true_type = bool_constant; +using false_type = bool_constant; + +struct S { + template + [[gnu::non_owning(B)]] int &foo (const int &); +}; + +void +g () +{ + [[maybe_unused]] const int &x0 = S().foo (42); // { dg-warning "dangling" } + [[maybe_unused]] const int &x1 = S().foo (42); // { dg-bogus "dangling" } +} +