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.133.124]) by sourceware.org (Postfix) with ESMTPS id 46C763837681 for ; Fri, 26 Aug 2022 23:01:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 46C763837681 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=1661554913; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ZCn/iRImaFjqCzSU6gs9JF7DvUnZet+Tfm/6C3+7sws=; b=h8F8MxouHcsP+hM7Pe8gFwCAWh8dfo386u3QP52icD2kKWbEeJKKy4OcREOo52p7CNJMEM T0CiGey831a1hr6l8ZH9gbkcuNQKB7OyxIwjMbk5IQS69rHITIC7RXDh4tHkLMddeK9CnN u1xP7W3o2kpQcLlVLsBOjIc3D9EdOPo= 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 ; 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) From: Marek Polacek To: Jason Merrill , GCC Patches 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 List-Id: 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 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 -- 2.37.2