From patchwork Tue Nov 8 14:42:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 17076 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2749909wru; Tue, 8 Nov 2022 06:44:06 -0800 (PST) X-Google-Smtp-Source: AMsMyM79scZeDN1glWUegGCFPZKrUopR+qf7rN1ue+eSxo/gp6pEL0uuAew/KEkAZgUJsnQkgrWM X-Received: by 2002:a05:6402:3645:b0:45f:e918:9f35 with SMTP id em5-20020a056402364500b0045fe9189f35mr54287413edb.118.1667918645939; Tue, 08 Nov 2022 06:44:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667918645; cv=none; d=google.com; s=arc-20160816; b=DYp0PlJSt79ZVz5yXVu4Y9dKs5qrMwYr++lVMq3kX3lQxb97WGq07j5ssQTSUSjfID INh8h7xeo4IzK/jmWT0+94FiRLsKM62IKqZ2qzIAWhuMWPMQPi45o1BmWwbidLpj49TQ bfPqgyfRW9VpWpMfhOi4xE+UDHDz+yMKFhd82jaNDoWtC+CIuDY5TIjDAg9BLP/0FRkD abN11VPNafpk/kfUoQZuZR/IET7QpPxUqMmdpRWuR33vrUU8ga2uhjQuYV16l+u91t6b c/qrdWhJhy9sfqCjYVr0T17iTp91mOOsrfuSfP1r0JD3oEJ4r/dMwvdUe5l5QGlAmUmk FW6w== 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-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=wolUKzFfMmlXl19lNac5TPjczpf9H6nSwOzhoDdv2jw=; b=j03/B3FoNV0Q/9DIyT3m2TI3erCvexB8yhexaS10Wg4aQbi+2+zemh6obOSTgOt7JB QTWACRUUHkhnsPYoMZSOyl8MYTEgMvLwFgdagWgQ7uaqt6srhwV45//HmYMJOHklIcS9 w9zF+4sxA0dBKuj+hZ7okJoOReBOOiT0hQEsh00j8L++cnb02VvsXCr4dUXnzqV2TOxx glSieXrB/KmEwkDILK2A9BRBQhKIHASitGwVd3gdx/DNFxYTeo4Zi2RM+JeUzCvR4WOT Q3qac3EPoySSMXWroHuLl0yoaM7Cbi/nis+0ZJQQnDYJ8JlGEl024lxQ25aYWSPKXrg3 wnvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="HyvAlAu/"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id x23-20020a1709060a5700b007ae0db0c454si10175281ejf.635.2022.11.08.06.44.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Nov 2022 06:44:05 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="HyvAlAu/"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 03B4B3858C62 for ; Tue, 8 Nov 2022 14:44:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 03B4B3858C62 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667918645; bh=wolUKzFfMmlXl19lNac5TPjczpf9H6nSwOzhoDdv2jw=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=HyvAlAu/RpK8I0bB4/P54TvIN/RQOv3ngYiuWRKOjKWVpIpxs0/m5sPkvnfJfNMdz l8zcUFA+mIff8dB6UjntswOrL+DovU5DjVjkXg9qucwZOLKn56rAQjT3JFmADkTpRB ZuEVrShA31brgY5qqyy9dtlwKLe3/dRyfRqQcYy0= 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 ESMTPS id C1D163858C39 for ; Tue, 8 Nov 2022 14:42:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C1D163858C39 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-88-w_bOdbMjNtOJmUIlDzK91w-1; Tue, 08 Nov 2022 09:42:47 -0500 X-MC-Unique: w_bOdbMjNtOJmUIlDzK91w-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D4CCF8001B8 for ; Tue, 8 Nov 2022 14:42:46 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.194.183]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 953EF2166B35; Tue, 8 Nov 2022 14:42:46 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 2A8EghgN2244337 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 8 Nov 2022 15:42:44 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 2A8EghfN2244336; Tue, 8 Nov 2022 15:42:43 +0100 Date: Tue, 8 Nov 2022 15:42:43 +0100 To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [RFC PATCH] c++: Minimal handling of carries_dependency attribute Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, 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: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek 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?1748939462169276675?= X-GMAIL-MSGID: =?utf-8?q?1748939462169276675?= Hi! A comment in D2552R1: "The only questionable (but still conforming) case we found was [[carries_dependency(some_argument)]] on GCC, where the emitted diagnostic said that the carries_dependency attribute is not supported, but did not specifically call out the syntax error in the argument clause." made me try the following patch, where we'll error at least for arguments to the attribute and for some uses of the attribute appertaining to something not mentioned in the standard warn with different diagnostics (or should that be an error?; clang++ does that, but I think we never do for any attribute, standard or not). The diagnostics on toplevel attribute declaration is still an attribute ignored warning and on empty statement different wording. The paper additionally mentions struct X { [[nodiscard]]; }; // no diagnostic on GCC and 2 cases of missing diagnostics on [[fallthrough]] (guess I should file a PR about those; one problem is that do { ... } while (0); there is replaced during genericization just by ... and another that [[fallthrough]] there is followed by a label, but not user/case/default label, but an artificial one created from while loop genericization. Thoughts on this? 2022-11-08 Jakub Jelinek * tree.cc (handle_carries_dependency_attribute): New function. (std_attribute_table): Add carries_dependency attribute. * parser.cc (cp_parser_check_std_attribute): Add carries_dependency attribute. * g++.dg/cpp0x/attr-carries_dependency1.C: New test. Jakub --- gcc/cp/tree.cc.jj 2022-11-07 10:30:42.758629740 +0100 +++ gcc/cp/tree.cc 2022-11-08 14:45:08.853864684 +0100 @@ -4923,6 +4923,32 @@ structural_type_p (tree t, bool explain) return true; } +/* Partially handle the C++11 [[carries_dependency]] attribute. + Just emit a different diagnostics when it is used on something the + spec doesn't allow vs. where it allows and we just choose to ignore + it. */ + +static tree +handle_carries_dependency_attribute (tree *node, tree name, + tree ARG_UNUSED (args), + int ARG_UNUSED (flags), + bool *no_add_attrs) +{ + if (TREE_CODE (*node) != FUNCTION_DECL + && TREE_CODE (*node) != PARM_DECL) + { + warning (OPT_Wattributes, "%qE attribute can only be applied to " + "functions or parameters", name); + *no_add_attrs = true; + } + else + { + warning (OPT_Wattributes, "%qE attribute ignored", name); + *no_add_attrs = true; + } + return NULL_TREE; +} + /* Handle the C++17 [[nodiscard]] attribute, which is similar to the GNU warn_unused_result attribute. */ @@ -5036,6 +5062,8 @@ const struct attribute_spec std_attribut handle_likeliness_attribute, attr_cold_hot_exclusions }, { "noreturn", 0, 0, true, false, false, false, handle_noreturn_attribute, attr_noreturn_exclusions }, + { "carries_dependency", 0, 0, true, false, false, false, + handle_carries_dependency_attribute, NULL }, { NULL, 0, 0, false, false, false, false, NULL, NULL } }; --- gcc/cp/parser.cc.jj 2022-11-04 18:11:41.523945997 +0100 +++ gcc/cp/parser.cc 2022-11-08 13:41:35.075135139 +0100 @@ -29239,8 +29239,7 @@ cp_parser_std_attribute (cp_parser *pars /* Warn if the attribute ATTRIBUTE appears more than once in the attribute-list ATTRIBUTES. This used to be enforced for certain - attributes, but the restriction was removed in P2156. Note that - carries_dependency ([dcl.attr.depend]) isn't implemented yet in GCC. + attributes, but the restriction was removed in P2156. LOC is the location of ATTRIBUTE. Returns true if ATTRIBUTE was not found in ATTRIBUTES. */ @@ -29249,7 +29248,7 @@ cp_parser_check_std_attribute (location_ { static auto alist = { "noreturn", "deprecated", "nodiscard", "maybe_unused", "likely", "unlikely", "fallthrough", - "no_unique_address" }; + "no_unique_address", "carries_dependency" }; if (attributes) for (const auto &a : alist) if (is_attribute_p (a, get_attribute_name (attribute)) --- gcc/testsuite/g++.dg/cpp0x/attr-carries_dependency1.C.jj 2022-11-08 15:17:43.168238390 +0100 +++ gcc/testsuite/g++.dg/cpp0x/attr-carries_dependency1.C 2022-11-08 15:16:39.695104787 +0100 @@ -0,0 +1,17 @@ +// { dg-do compile { target c++11 } } + +[[carries_dependency]] int *f1 (); // { dg-warning "attribute ignored" } +int f2 (int *x [[carries_dependency]]); // { dg-warning "attribute ignored" } +[[carries_dependency]] int f3 (); // { dg-warning "attribute ignored" } +int f4 (int x [[carries_dependency]]); // { dg-warning "attribute ignored" } +[[carries_dependency(1)]] int f5 (); // { dg-error "'carries_dependency' attribute does not take any arguments" } +[[carries_dependency]] int v; // { dg-warning "'carries_dependency' attribute can only be applied to functions or parameters" } +[[carries_dependency]]; // { dg-warning "attribute ignored" } +void +f6 () +{ + [[carries_dependency]]; // { dg-warning "attributes at the beginning of statement are ignored" } +} +#if __has_cpp_attribute(carries_dependency) +#error carries_dependency attribute is not actually implemented +#endif