From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1734) id B345838560B4; Mon, 29 Aug 2022 21:27:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B345838560B4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661808424; bh=BKtOqB3SYwMHY6d1L5h2hpHf90Ge0dsFyNM+an+SGWw=; h=From:To:Subject:Date:From; b=JY91w2BsYN1JqODrDdUMCccW+C3epeP1yvPhvvXLlnJi/iM7gi/9OaRz4saRSpnBY NymnSrIKuiqbu9P1W6TnOZoTB9qD3q7ZPr7ecGD6QPhPTwmHByarhQc9wLn381+4Ro dOEdatJNETthRpcItrWA3j4muGqHoXIm+sUXVNAg= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Marek Polacek To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-2256] c++: Fix C++11 attribute propagation [PR106712] X-Act-Checkin: gcc X-Git-Author: Marek Polacek X-Git-Refname: refs/heads/trunk X-Git-Oldrev: b504149d2c92ddfcfab62ea6d1ed49ae72493e38 X-Git-Newrev: 98973354b8690f01e06b9f36106e786fd94ac7a3 Message-Id: <20220829212704.B345838560B4@sourceware.org> Date: Mon, 29 Aug 2022 21:27:04 +0000 (GMT) List-Id: https://gcc.gnu.org/g:98973354b8690f01e06b9f36106e786fd94ac7a3 commit r13-2256-g98973354b8690f01e06b9f36106e786fd94ac7a3 Author: Marek Polacek Date: Fri Aug 26 18:03:53 2022 -0400 c++: Fix C++11 attribute propagation [PR106712] 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. Diff: --- gcc/cp/decl.cc | 2 +- gcc/testsuite/g++.dg/cpp0x/gen-attrs-77.C | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) 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 (); +}