From patchwork Thu Aug 15 20:53:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1972953 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Kg7ID9wg; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WlHRZ4xBbz1yXZ for ; Fri, 16 Aug 2024 06:54:18 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 847563858294 for ; Thu, 15 Aug 2024 20:54:16 +0000 (GMT) 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 ESMTP id 56FEE3858C41 for ; Thu, 15 Aug 2024 20:53:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 56FEE3858C41 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 56FEE3858C41 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723755213; cv=none; b=DSAQMRXdxoGJepc6/btOiBpG9AMGt4pkuj9vu4/YfyAp91LeL6yPKUKru5Ly1dwumfPJxrupUQOJyzTEmT8srBBvHN/pJLWxswVmoNHaRg/Dp4sah6gsGInnCw1Irej/h7qjvWqoNLDfcJbqjaymRtEw6oQk8ZtWQ7EilrAKXWk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723755213; c=relaxed/simple; bh=DqmWMZzB1xbut287WjvFnT7S9WQayH3dpuiEmB+KtCU=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=GuwsEx63NSvnZIkaLCdBFXyE3/MjXp5HrDc28p2f0fCiNPLO/+zDqgnsb7LpBm3cJuSMptncihQE+PHksICQtjhbHjCVm+gUJSbppbK+ozoc3o1LMTBjeWCXFWqVdZy3ZlEown96yHshvPJq83YozdXNf/UE9N/V0tBCrGYGZ4s= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1723755211; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=oi+ruj4qC79FobKBIFORPbcTqYlnbzvbD1jrogy8jsk=; b=Kg7ID9wg5cUI8DQjFlKn8N1IRj5pSSLt9Aznzk8PxtbESXZC+CMwTSGsdzhgl5o8YkCFa4 9cP0wt5W1krMXBzlEmKqMWPDYb7erbRqJpI509tMsdF+6QPn4+01AktnOsTo/sylI+WsP3 z7TNc0WlT9+1/DWt5Dpx6BQARJfTKRo= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-317-I1WMd562NjyEac4sAbt-lA-1; Thu, 15 Aug 2024 16:53:29 -0400 X-MC-Unique: I1WMd562NjyEac4sAbt-lA-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1F6901955D55 for ; Thu, 15 Aug 2024 20:53:28 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.8]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 843021955D4B; Thu, 15 Aug 2024 20:53:27 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 47FKrNsG3512273 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 15 Aug 2024 22:53:23 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 47FKrNg93512270; Thu, 15 Aug 2024 22:53:23 +0200 Date: Thu, 15 Aug 2024 22:53:23 +0200 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Allow standard attributes after closing square bracket in new-type-id [PR110345] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Hi! For C++ 26 P2552R3 I went through all the spots (except modules) where attribute-specifier-seq appears in the grammar and tried to construct a testcase in all those spots, for now for [[deprecated]] attribute. The first thing I found is that we aren't parsing standard attributes in noptr-new-declarator - https://eel.is/c++draft/expr.new#1 The following patch parses it there, for the non-outermost arrays applies normally the attributes to the array type, for the outermost where we just set *nelts and don't really build an array type just warns that we ignore those attributes (or, do you think we should just build an array type in that case and just take its element type?). Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-08-15 Jakub Jelinek PR c++/110345 * parser.cc (make_array_declarator): Add STD_ATTRS argument, set declarator->std_attributes to it. (cp_parser_new_type_id): Warn on non-ignored std_attributes on the array declarator which is being omitted. (cp_parser_direct_new_declarator): Parse standard attributes after closing square bracket, pass it to make_array_declarator. (cp_parser_direct_declarator): Pass std_attrs to make_array_declarator instead of setting declarator->std_attributes manually. * g++.dg/cpp0x/gen-attrs-80.C: New test. * g++.dg/cpp0x/gen-attrs-81.C: New test. Jakub --- gcc/cp/parser.cc.jj 2024-08-15 14:56:29.007757215 +0200 +++ gcc/cp/parser.cc 2024-08-15 16:28:18.607692746 +0200 @@ -1689,7 +1689,7 @@ static cp_declarator *make_call_declarat (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree, tree, location_t); static cp_declarator *make_array_declarator - (cp_declarator *, tree); + (cp_declarator *, tree, tree); static cp_declarator *make_pointer_declarator (cp_cv_quals, cp_declarator *, tree); static cp_declarator *make_reference_declarator @@ -1904,10 +1904,11 @@ make_call_declarator (cp_declarator *tar } /* Make a declarator for an array of BOUNDS elements, each of which is - defined by ELEMENT. */ + defined by ELEMENT. STD_ATTRS contains attributes that appertain to + the array type. */ cp_declarator * -make_array_declarator (cp_declarator *element, tree bounds) +make_array_declarator (cp_declarator *element, tree bounds, tree std_attrs) { cp_declarator *declarator; @@ -1923,6 +1924,8 @@ make_array_declarator (cp_declarator *el else declarator->parameter_pack_p = false; + declarator->std_attributes = std_attrs; + return declarator; } @@ -9784,6 +9787,12 @@ cp_parser_new_type_id (cp_parser* parser if (*nelts == error_mark_node) *nelts = integer_one_node; + if (*nelts + && declarator->std_attributes + && any_nonignored_attribute_p (declarator->std_attributes)) + warning (OPT_Wattributes, "attributes ignored on outermost array " + "type in new expression"); + if (*nelts == NULL_TREE) /* Leave [] in the declarator. */; else if (outer_declarator) @@ -9838,8 +9847,8 @@ cp_parser_new_declarator_opt (cp_parser* /* Parse a direct-new-declarator. direct-new-declarator: - [ expression ] - direct-new-declarator [constant-expression] + [ expression ] attribute-specifier-seq [opt] + direct-new-declarator [constant-expression] attribute-specifier-seq [opt] */ @@ -9886,8 +9895,9 @@ cp_parser_direct_new_declarator (cp_pars /* Look for the closing `]'. */ cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE); + tree attrs = cp_parser_std_attribute_spec_seq (parser); /* Add this bound to the declarator. */ - declarator = make_array_declarator (declarator, expression); + declarator = make_array_declarator (declarator, expression, attrs); /* If the next token is not a `[', then there are no more bounds. */ @@ -24330,8 +24340,7 @@ cp_parser_direct_declarator (cp_parser* } attrs = cp_parser_std_attribute_spec_seq (parser); - declarator = make_array_declarator (declarator, bounds); - declarator->std_attributes = attrs; + declarator = make_array_declarator (declarator, bounds, attrs); } else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT) { --- gcc/testsuite/g++.dg/cpp0x/gen-attrs-80.C.jj 2024-08-15 15:27:00.297061501 +0200 +++ gcc/testsuite/g++.dg/cpp0x/gen-attrs-80.C 2024-08-15 15:27:19.610822213 +0200 @@ -0,0 +1,10 @@ +// { dg-do compile { target c++11 } } + +void +foo (int n) +{ + auto a = new int [n] [[]]; + auto b = new int [n] [[]] [42] [[]] [1] [[]]; + delete[] b; + delete[] a; +} --- gcc/testsuite/g++.dg/cpp0x/gen-attrs-81.C.jj 2024-08-15 17:14:43.912228130 +0200 +++ gcc/testsuite/g++.dg/cpp0x/gen-attrs-81.C 2024-08-15 17:19:01.738038053 +0200 @@ -0,0 +1,11 @@ +// { dg-do compile { target c++11 } } + +void +foo (int n) +{ + auto a = new int [n] [[gnu::deprecated]]; // { dg-warning "attributes ignored on outermost array type in new expression" } + auto b = new int [n] [[gnu::deprecated]] [42] [[]] [1] [[]]; // { dg-warning "attributes ignored on outermost array type in new expression" } + auto c = new int [n] [[]] [42] [[gnu::deprecated]] [1] [[gnu::deprecated]]; + delete[] b; + delete[] a; +}