Message ID | 20220826230150.328756-1-polacek@redhat.com |
---|---|
State | New, archived |
Headers |
Return-Path: <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:ecc5:0:0:0:0:0 with SMTP id s5csp433810wro; Fri, 26 Aug 2022 16:02:38 -0700 (PDT) X-Google-Smtp-Source: AA6agR5ahr7L9Cg4us7ZzYSRjBQTu6EY2QBwtU2QCxLe7XoZHvN6hlqsVIwW2LJV9b1/HnO9/Hwi X-Received: by 2002:a05:6402:c45:b0:442:c549:8e6b with SMTP id cs5-20020a0564020c4500b00442c5498e6bmr8367845edb.123.1661554958476; Fri, 26 Aug 2022 16:02:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661554958; cv=none; d=google.com; s=arc-20160816; b=1GyzhHIDxVAEH8Oyyj5R8dscMN8EzVH/YTY75Q9QCgV1ahk4oeGn8QltUO9BloCTLU S5iD8BzjHeJ0LG9G1Wnmcm5juApKOEJr9wOERGLW3lLksbPhWo8lAOvO+YwFkmbFhlsY Byhy6qSbXcYRBowbxGqjyrLnqzFGS4XsMqVEkTofYFVlbvpydnuqEtj7AlHSmKYPKrLk by1Ut1GDSZb/BrEyNSciEOIDNqqVuljY6OpLd9z+niehnuWa5/IaPNI3fmK6O9+Taddp j8xJp0ZjKarG9VKkITwo77kiC0QK/PDckMniHPG0RFcPnwJFOnZ/Go37EH0NkUWKZzGk 8qRw== 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-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=ZCn/iRImaFjqCzSU6gs9JF7DvUnZet+Tfm/6C3+7sws=; b=x+bzFY/3+vVTobaGSjOBaQSKbsMawddrKYDXnyz1125jzxoiE70ntN3P/Bdd4Jl+Sj 9FbTM/O7NBSLTS9pUMVhv5xx4P/5aSEdIIYwzq4if7qp636R7+o3pUSQuVtpTKqJgYAw UpS81k4BnPFAANmz5Ivt56Z8h8HuFVd52urTqzJ2BDceE/9xw83EDRDMrgYSsYNZtKli XhLZWJTAs818JPlPx4twPqgcp2Bn2FjR90ETx8F3nsseoSPflAw30BU9dAf63WzxTfta 9M6wQFTAQu7vAjV0Vb417qZ+/8EvVyxs3tQNNfza5GqPKFkC+VtjU1fZE3NZxIVP2xCg gL7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=IA1pYtqx; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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. [8.43.85.97]) by mx.google.com with ESMTPS id cs10-20020a170906dc8a00b0073d74dcbab5si1969801ejc.275.2022.08.26.16.02.38 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Aug 2022 16:02:38 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=IA1pYtqx; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 3BE5438376A3 for <ouuuleilei@gmail.com>; Fri, 26 Aug 2022 23:02:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3BE5438376A3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661554957; bh=ZCn/iRImaFjqCzSU6gs9JF7DvUnZet+Tfm/6C3+7sws=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=IA1pYtqxrbSCrsY7+7M644EYpIFT3WM80QZdieG8rGqYu7DYQhnJq2zRbtt1P7Sxn dIpzsMOEzoobLNFLIZ/LugxNRR3fcOuzvYbkMHcZpX6zjeB7JhU1pX3fl6BiE2A0WF l9sJS+vUI9YbfSO1mbZrwMsvtIbf/jtYV8Bsfvo0= 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 46C763837681 for <gcc-patches@gcc.gnu.org>; Fri, 26 Aug 2022 23:01:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 46C763837681 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-112-LEhdC9UoP6upcDaa4HW50g-1; Fri, 26 Aug 2022 19:01:52 -0400 X-MC-Unique: LEhdC9UoP6upcDaa4HW50g-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id AA83729AB450 for <gcc-patches@gcc.gnu.org>; Fri, 26 Aug 2022 23:01:52 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.16.86]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8B1552026D4C; Fri, 26 Aug 2022 23:01:52 +0000 (UTC) To: Jason Merrill <jason@redhat.com>, GCC Patches <gcc-patches@gcc.gnu.org> Subject: [PATCH] c++: Fix C++11 attribute propagation [PR106712] Date: Fri, 26 Aug 2022 19:01:50 -0400 Message-Id: <20220826230150.328756-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-12.7 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, 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 <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: Marek Polacek via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: Marek Polacek <polacek@redhat.com> Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1742266652287510696?= X-GMAIL-MSGID: =?utf-8?q?1742266652287510696?= |
Series |
c++: Fix C++11 attribute propagation [PR106712]
|
|
Commit Message
Marek Polacek
Aug. 26, 2022, 11:01 p.m. UTC
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. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? PR c++/106712 gcc/cp/ChangeLog: * decl.cc (start_decl): Copy prefix_attributes. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/gen-attrs-77.C: New test. --- gcc/cp/decl.cc | 3 ++- gcc/testsuite/g++.dg/cpp0x/gen-attrs-77.C | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/gen-attrs-77.C base-commit: 390f94eee1ae694901f896ac45bfb148f8126baa
Comments
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? > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > PR c++/106712 > > gcc/cp/ChangeLog: > > * decl.cc (start_decl): Copy prefix_attributes. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp0x/gen-attrs-77.C: New test. > --- > gcc/cp/decl.cc | 3 ++- > gcc/testsuite/g++.dg/cpp0x/gen-attrs-77.C | 17 +++++++++++++++++ > 2 files changed, 19 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..9fa80b926d5 100644 > --- a/gcc/cp/decl.cc > +++ b/gcc/cp/decl.cc > @@ -5566,7 +5566,8 @@ start_decl (const cp_declarator *declarator, > *pushed_scope_p = NULL_TREE; > > if (prefix_attributes != error_mark_node) > - attributes = attr_chainon (attributes, prefix_attributes); > + /* Don't let grokdeclarator modify prefix_attributes. */ > + attributes = attr_chainon (attributes, copy_list (prefix_attributes)); > > decl = grokdeclarator (declarator, declspecs, NORMAL, initialized, > &attributes); > 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: 390f94eee1ae694901f896ac45bfb148f8126baa
diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index d46a347a6c7..9fa80b926d5 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -5566,7 +5566,8 @@ start_decl (const cp_declarator *declarator, *pushed_scope_p = NULL_TREE; if (prefix_attributes != error_mark_node) - attributes = attr_chainon (attributes, prefix_attributes); + /* Don't let grokdeclarator modify prefix_attributes. */ + attributes = attr_chainon (attributes, copy_list (prefix_attributes)); decl = grokdeclarator (declarator, declspecs, NORMAL, initialized, &attributes); 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 (); +}