From patchwork Mon Aug 29 20:01:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 817 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:ecc5:0:0:0:0:0 with SMTP id s5csp1567707wro; Mon, 29 Aug 2022 13:02:34 -0700 (PDT) X-Google-Smtp-Source: AA6agR4Z9LCPUR42zF8OZ4zDPSNVWvhwtVHxyY21HM4Fpa+yuANbKUwx+Uir8A9qVRTgosqZXaaU X-Received: by 2002:a50:aa82:0:b0:445:dbba:6cb1 with SMTP id q2-20020a50aa82000000b00445dbba6cb1mr18189137edc.267.1661803354707; Mon, 29 Aug 2022 13:02:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661803354; cv=none; d=google.com; s=arc-20160816; b=lUbxzsl87yS1y64paBcrKo9ZolGqhfQ8p5cf54IvRm9SoN2hkr8m7BROTkdokcuili aEUyHrfOOIidkM39/RIzHrfQJOiBgc5kIHc8wcWOjPqQVDHaxp64sS6H/PKfjDPDez1Y 1xFyiPr8XOHlfOECsq1+h0kHeauOe93Mc7w2e2sXs4aBzfzyskpFLU5Ai8L1rA+WvFB/ p6vGMA2SWJMExFGL0XQxZfytHJ2l5LMx3u2tuO/U+4aIYT22IwGEMjcDorxILBtV7JSL fFPrMBGYOGoI2zUOCtaCmi2vL0BOefMmXMyorPHP0k/hU1AFrvUjEyn8yBGOaOfuVYiI TJtQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-disposition:user-agent:in-reply-to:mime-version:references :message-id:subject:to:date:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=tEUUE2WH5MhOV0pLWFCVHPx4U3QSe8I7A6ZTszzjWZA=; b=GCGKUFRv2I4VdzzpQs03uhLfDr3s/G2L4eJgIkv1YmP28LNtSLQtwas4bOD1A9xr62 ymJyu5ltC+6XuQ/ziGvmWUVj9tjS5uKGkZPkuhKCa6T096cXFu66X9UUVmPr7cTEfFAE jUsk8DEcd+ObqegbW7PpD7ej4tzJXUl+eTrgykH1ASmgQgcCvoD6ashdCop3WV0tTgjw JRlRZMuhtiVksOzjw0AbuosL991QVnp3J2V0Mr2o7+xuL0EGnPJ+Qx1RRAbApaOlNlab trOlfFGl5aYHzqDJqNMJ/a5TrhTmQTJlYjcxiBm4EI7o57KyMzTsmf5plsCihFeSKo1m 7oaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Pddxzy5O; 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 q4-20020a056402032400b004480b5da8besi5393777edw.257.2022.08.29.13.02.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Aug 2022 13:02:34 -0700 (PDT) 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=Pddxzy5O; 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 1C968385703A for ; Mon, 29 Aug 2022 20:01:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1C968385703A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661803315; bh=tEUUE2WH5MhOV0pLWFCVHPx4U3QSe8I7A6ZTszzjWZA=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=Pddxzy5O8lutS5I1D8A5NBQAY5i52b093afT4YNmjUU3gojQAxforNQE7YEE+4Vrb SgsYzaRhevT+3b+GFPYKbgFObVzDkJYMDG1s18GUeWu0Wu8EpM2rB2E/d+94sPX3p0 40UUrAQjJJyjEPH+gwLG6cCRPW2uRoNvldUMT7Xc= 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 84ED9385737B for ; Mon, 29 Aug 2022 20:01:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 84ED9385737B Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-631-uwk1J132NmKD57PR8QX9uA-1; Mon, 29 Aug 2022 16:01:04 -0400 X-MC-Unique: uwk1J132NmKD57PR8QX9uA-1 Received: by mail-qk1-f200.google.com with SMTP id ay10-20020a05620a178a00b006bbcab9d554so7456955qkb.13 for ; Mon, 29 Aug 2022 13:01:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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; bh=tEUUE2WH5MhOV0pLWFCVHPx4U3QSe8I7A6ZTszzjWZA=; b=yfiUNsKWDy71YJlzZmoU/ECyU4wqM0ciDthK7BhSXlj+f3GNaVJFNdJsNgaNIbyZwF PFruSXJ6hpUGU1ey4MZsmZyXTBWSL1CZffo567j1qm3PyNGyIp2PumpPVYjTWF4RG8xs tyDpbkA4hPjx8zBi1DvrieKR/lgtY7OnwWv/X6tDA3JsQDLw+2wBOB7LUv9GFmoWe6uW 7HUvM5HqAXnmZ4LnXhdzelFabD7IZ7715dQaiwfeQJyrGdlEYZdOTTcvtA4VXio10zBU 0IVB4vykoAvuXjtOplIiecOoO+h8bF1Ngq3MPEZ1HERRmyKZ9aq/MwKVtMVZSKyU8Cf6 oz/g== X-Gm-Message-State: ACgBeo0fKC9ibBKI9Srs5cIhN6Byeqo4S8adzR68V3v8irkDkR7XABPq f8Gk5rNxINZTWI4YCpGWVUHVekLuCva15DtMFD5AJUqjLbhDi7DHZzHA8v/OZkCSSdXNSRJWRU2 VcXysfD0WaqB32pqHDQ== X-Received: by 2002:a05:620a:488e:b0:6bb:3f84:d175 with SMTP id ea14-20020a05620a488e00b006bb3f84d175mr9440931qkb.587.1661803263458; Mon, 29 Aug 2022 13:01:03 -0700 (PDT) X-Received: by 2002:a05:620a:488e:b0:6bb:3f84:d175 with SMTP id ea14-20020a05620a488e00b006bb3f84d175mr9440899qkb.587.1661803263074; Mon, 29 Aug 2022 13:01:03 -0700 (PDT) Received: from redhat.com ([2601:184:4780:4310::e531]) by smtp.gmail.com with ESMTPSA id 17-20020a05620a06d100b006b9815a05easm6261346qky.26.2022.08.29.13.01.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Aug 2022 13:01:02 -0700 (PDT) Date: Mon, 29 Aug 2022 16:01:00 -0400 To: Jason Merrill Subject: [PATCH v2] c++: Fix C++11 attribute propagation [PR106712] Message-ID: References: <20220826230150.328756-1-polacek@redhat.com> <95c29538-938d-1e24-95ed-3b2a402980d4@redhat.com> MIME-Version: 1.0 In-Reply-To: <95c29538-938d-1e24-95ed-3b2a402980d4@redhat.com> User-Agent: Mutt/2.2.6 (2022-06-05) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline 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_LOW, 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: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Cc: GCC Patches 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?1742266652287510696?= X-GMAIL-MSGID: =?utf-8?q?1742527114638432925?= On Mon, Aug 29, 2022 at 01:32:29PM -0400, Jason Merrill wrote: > On 8/26/22 19:01, Marek Polacek wrote: > > When we have > > > > [[noreturn]] int fn1 [[nodiscard]](), fn2(); > > > > "noreturn" should apply to both fn1 and fn2 but "nodiscard" only to fn1: > > [dcl.pre]/3: "The attribute-specifier-seq appertains to each of > > the entities declared by the declarators of the init-declarator-list." > > [dcl.spec.general]: "The attribute-specifier-seq affects the type > > only for the declaration it appears in, not other declarations involving > > the same type." > > > > As Ed Catmur correctly analyzed, this is because, for the test above, > > we call start_decl with prefix_attributes=noreturn, but this line: > > > > attributes = attr_chainon (attributes, prefix_attributes); > > > > results in attributes == prefix_attributes, because chainon sees > > that attributes is null so it just returns prefix_attributes. Then > > in grokdeclarator we reach > > > > *attrlist = attr_chainon (*attrlist, declarator->std_attributes); > > > > which modifies prefix_attributes so now it's "noreturn, nodiscard" > > and so fn2 is wrongly marked nodiscard as well. Fixed by copying > > prefix_attributes. > > How about reversing the order of arguments to the call in grokdeclarator, so > that the prefix attributes can remain shared at the end of the list? Thanks, that seems like a cheaper solution. It works because this way we tack the prefix attributes onto ->std_attributes, avoiding modifying prefix_attributes. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- When we have [[noreturn]] int fn1 [[nodiscard]](), fn2(); "noreturn" should apply to both fn1 and fn2 but "nodiscard" only to fn1: [dcl.pre]/3: "The attribute-specifier-seq appertains to each of the entities declared by the declarators of the init-declarator-list." [dcl.spec.general]: "The attribute-specifier-seq affects the type only for the declaration it appears in, not other declarations involving the same type." As Ed Catmur correctly analyzed, this is because, for the test above, we call start_decl with prefix_attributes=noreturn, but this line: attributes = attr_chainon (attributes, prefix_attributes); results in attributes == prefix_attributes, because chainon sees that attributes is null so it just returns prefix_attributes. Then in grokdeclarator we reach *attrlist = attr_chainon (*attrlist, declarator->std_attributes); which modifies prefix_attributes so now it's "noreturn, nodiscard" and so fn2 is wrongly marked nodiscard as well. Fixed by reversing the order of arguments to attr_chainon. That way, we tack the prefix attributes onto ->std_attributes, avoiding modifying prefix_attributes. PR c++/106712 gcc/cp/ChangeLog: * decl.cc (grokdeclarator): Reverse the order of arguments to attr_chainon. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/gen-attrs-77.C: New test. --- gcc/cp/decl.cc | 2 +- gcc/testsuite/g++.dg/cpp0x/gen-attrs-77.C | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/gen-attrs-77.C base-commit: 60d1d296b42b22b08d4eaa38bea02100c07261ac diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index d46a347a6c7..b72b2a8456b 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -13474,7 +13474,7 @@ grokdeclarator (const cp_declarator *declarator, /* [dcl.meaning]/1: The optional attribute-specifier-seq following a declarator-id appertains to the entity that is declared. */ if (declarator->std_attributes != error_mark_node) - *attrlist = attr_chainon (*attrlist, declarator->std_attributes); + *attrlist = attr_chainon (declarator->std_attributes, *attrlist); else /* We should have already diagnosed the issue (c++/78344). */ gcc_assert (seen_error ()); diff --git a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-77.C b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-77.C new file mode 100644 index 00000000000..2c41c62f33b --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-77.C @@ -0,0 +1,17 @@ +// PR c++/106712 +// { dg-do compile { target c++11 } } + +[[noreturn]] int f1 [[nodiscard]](), f2 (); +[[nodiscard]] int f3 (), f4 (); +int f5 [[nodiscard]](), f6 (); + +int +main () +{ + f1 (); // { dg-warning "ignoring" } + f2 (); + f3 (); // { dg-warning "ignoring" } + f4 (); // { dg-warning "ignoring" } + f5 (); // { dg-warning "ignoring" } + f6 (); +}