From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 3AE593856240 for ; Mon, 29 Aug 2022 21:16:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3AE593856240 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661807779; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+HXaY3FWWFws3pf1gcs3GkLsi/LlRZl+sSxb7SDZhK0=; b=O3tehsglQfQuUZZA2jqe1oLv2j0esCTuyGJKGeksjDEY9s7/kwVWM9gPZLfi5Ofg2jnjtj yuqBjud9KcG+qk10K+eIMFQzINUUNWDlFnLgZ5gvkaYxI6gGFxs2VV0S+Tp+J/Dm8MI4mI /4sfLVT0LPlYuXTG4PfUeercRPJ5i30= 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_128_GCM_SHA256) id us-mta-633-GA3l8_ijPSOpMncNDiLrfw-1; Mon, 29 Aug 2022 17:16:18 -0400 X-MC-Unique: GA3l8_ijPSOpMncNDiLrfw-1 Received: by mail-qt1-f199.google.com with SMTP id o21-20020ac87c55000000b00344646ea2ccso7279971qtv.11 for ; Mon, 29 Aug 2022 14:16:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc; bh=+HXaY3FWWFws3pf1gcs3GkLsi/LlRZl+sSxb7SDZhK0=; b=qBNcJH7C9RNe4MKDYRyOmGAEoW9UG18iJSCeeZEp6/PyLgCzOejcAkt7yGezypC76S 98u/ApbnJG7vrld2wPteFs9ZcJuw1PUXAXSw0jOc4xnr+FGabeY6rIC0w1mYqGduoo3x ayGfx4omResGr1hUYhJnWOsdCq3dD2ywwreH/BF/lqH/aOv+d4a7leZbRiWe2TKgxVAm zzyTPhc8Htj6nSCpfxidZYtFsMX8JXXN+4aC2mPX2iHtavNNfoLVow4QBoX6n9j+AKba 7wj7krMNJPUOWIv8PJ229tvTcNZdQTZd5s9fCzV/sCvHRRIAIt7w0PSKErs00PgKvfc+ d9Jw== X-Gm-Message-State: ACgBeo3RGO0fNEmE4bdwKcHN74vGKl83t29J4RXq4n6q5BPWNDO7nKsn 2CsHioD9Wfn0z+Fe+UeJekXFeGX+qX+c3JSUbonDynAUDBmEbZhfsF5D3Y8ZT3mgwWiiFkOOHRj HkXyQfEVrQFQy4cFnPQ== X-Received: by 2002:ac8:58d4:0:b0:344:5dbe:38a4 with SMTP id u20-20020ac858d4000000b003445dbe38a4mr12371964qta.381.1661807777639; Mon, 29 Aug 2022 14:16:17 -0700 (PDT) X-Google-Smtp-Source: AA6agR6LyDCkO4OVFwPRZayC2iAzjIhzmGyZy2TOA/wK+w02JUSCbqX8rQ+uQOggdeVmlN5R+TSaMA== X-Received: by 2002:ac8:58d4:0:b0:344:5dbe:38a4 with SMTP id u20-20020ac858d4000000b003445dbe38a4mr12371943qta.381.1661807777330; Mon, 29 Aug 2022 14:16:17 -0700 (PDT) Received: from [192.168.1.101] (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id u20-20020a05620a0c5400b006bb11f9a859sm6666021qki.122.2022.08.29.14.16.16 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 29 Aug 2022 14:16:16 -0700 (PDT) Message-ID: <07135428-8d51-69c2-3631-ca126b15c378@redhat.com> Date: Mon, 29 Aug 2022 17:16:15 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.13.0 Subject: Re: [PATCH v2] c++: Fix C++11 attribute propagation [PR106712] To: Marek Polacek Cc: GCC Patches References: <20220826230150.328756-1-polacek@redhat.com> <95c29538-938d-1e24-95ed-3b2a402980d4@redhat.com> From: Jason Merrill In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-13.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,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 List-Id: On 8/29/22 16:01, Marek Polacek wrote: > 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? OK. > -- >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 > > 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 (); > +} > > base-commit: 60d1d296b42b22b08d4eaa38bea02100c07261ac