From patchwork Tue Dec 5 07:40:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 173767 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp3267822vqy; Mon, 4 Dec 2023 23:41:01 -0800 (PST) X-Google-Smtp-Source: AGHT+IFIM+3IoBzyA+e2kai4AezgEocfK8A3SGsdZS87RyeRPJxQPOpS0/iyBBPtQQDFaGfohi5H X-Received: by 2002:a05:620a:27d4:b0:77e:fba3:a206 with SMTP id i20-20020a05620a27d400b0077efba3a206mr845344qkp.96.1701762061697; Mon, 04 Dec 2023 23:41:01 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1701762061; cv=pass; d=google.com; s=arc-20160816; b=CDDr5SvxF2zL0g7rZjp+MbvBkzwvot5BmUfEK78QhG2V/uxedl7qmVVeryxCwsNuhd Hubpe/AR+JoAwXnQgNKkCPySSzprpqN0rIWOm6YoMWUIOdlakWLuuEWJl4gJJef6RuMT xQNZEIMG0FhUl65L0OJeypakMCu07rxz8m4lV38FbVtGYWRqYI1/qnu+mxNrYYhWWdGI ujOykYQPKfKnTS2qhOhcbWCYOd3U1Llg0OsZSwtSfejAXeNdTFNjCIxlZDYTsuzaTlhE piObE70xyo4WeA5ftgMm8ljhlucSVvB30itt+ZsQ4fr1Kj78ebF5HG2FnNU70MQICjs8 hSGQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:reply-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=DEIQW9uN6ZXqam+lFivNHoFQrncYMdigDBlzWRc53CY=; fh=drb8jPahgz959MrupsiUi8XeCFozkYVF0F6zLW2CcMA=; b=tTy93GBBnRcUH9QnSoqrFGcDVcPyqGm1oSo8lGajHOi2ZikN6JiUomqCT+APlXHj3W JfCXHf2Ad5j1e97rNrZ4f/4FysSgGqNB5C0A6QfmBLOaGP0ABBoEsk17SInnkiekMPyw qtKT55qCKVl1Ln6e+pvcfr2tQwqKfoSDBo5OsRgfoX3Qcy9pbt/pfuH8Sd/dqjDx98uq RNhSQQn6pqcd2Vjy5VnyUKoZdpuaPvBWF+oLxECXbzWUQ8xJBEN7w/D0cXJrLShffcZA pvIRTaYz2alZ1qMz9ZBCu2KhB068wcRqG1Evpm/yU4bneR3PyubQJWNQCV44IK43dRhG SLDQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=YZP0viFw; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id g11-20020a05620a40cb00b0077d69adf6b2si11921838qko.356.2023.12.04.23.41.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 23:41:01 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b=YZP0viFw; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5F8803857034 for ; Tue, 5 Dec 2023 07:41:01 +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 ESMTPS id 7F1B13858D28 for ; Tue, 5 Dec 2023 07:40:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7F1B13858D28 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 7F1B13858D28 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=1701762039; cv=none; b=CzWOgxrTAi9+nzdgYi9YjM2y75JLNkHayRMEUY3QUWDHCn9NP4oNlFoh/AT5s2VuhGHUcsUGbEOUbKOHO8yNZrPNdgI8SJGZJrxf8MnMoj6KhRtk3eSfNXN7P9XTewLuBiHDRLjnzUzgqmn/v/qieTvRpzSM9yCkCcc7/mvUqT4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701762039; c=relaxed/simple; bh=BOV2zZjmpTtAMGNHmbYQCNI5xc1m9gLeC3D0v6y1dek=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=em5Kw+PHrqgj4wJU34vftzzg8nVXJW6BBtR4ffD8guvsPaHEo45liV1WBK6xV3wOiwiu1RRzpgpFMDrXUlQjMXn+8f8kpRJw1+q7bxQx6NZYCSW8AAJ/5Kbx9UTy2kmGKK65lAMyewDKFiEr3r59xx/iEGbvJ9skVwwhBpCww0s= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701762037; 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=DEIQW9uN6ZXqam+lFivNHoFQrncYMdigDBlzWRc53CY=; b=YZP0viFwhzhhdbtO34fSjaF1MJe/TwcXeXX/Fw3NE2aUEhKEAVLxtGu6G8cT4EX8CzTu1r J467FQwgzIVXMT/gD5mMXwPmygCbHQWs8jRdwAEfjp9EwAORCG7kn1ZK6tIX5dgdJdptXQ +y9MWVagi+hpUzRCRNJGcFoL4ces38k= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-119-7LLilp3LMC-KhX5640AsDQ-1; Tue, 05 Dec 2023 02:40:35 -0500 X-MC-Unique: 7LLilp3LMC-KhX5640AsDQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2C399835346 for ; Tue, 5 Dec 2023 07:40:35 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.195.157]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DC6B42026D66; Tue, 5 Dec 2023 07:40:34 +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 3B57eWBM694085 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 5 Dec 2023 08:40:32 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3B57eWqs694084; Tue, 5 Dec 2023 08:40:32 +0100 Date: Tue, 5 Dec 2023 08:40:32 +0100 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org, Marek Polacek Subject: [PATCH] c++: Implement C++ DR 2262 - Attributes for asm-definition [PR110734] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784426855292337155 X-GMAIL-MSGID: 1784426855292337155 Hi! Seems in 2017 attribute-specifier-seq[opt] was added to asm-declaration and the change was voted in as a DR. The following patch implements it by parsing the attributes and warning about them. I found one attribute parsing bug I'll send a fix for momentarily. And there is another thing I wonder about: with -Wno-attributes= we are supposed to ignore the attributes altogether, but we are actually still warning about them when we emit these generic warnings about ignoring all attributes which appertain to this and that (perhaps with some exceptions we first remove from the attribute chain), like: void foo () { [[foo::bar]]; } with -Wattributes -Wno-attributes=foo::bar Shouldn't we call some helper function in cases like this and warn not when std_attrs (or how the attribute chain var is called) is non-NULL, but if it is non-NULL and contains at least one non-attribute_ignored_p attribute? cp_parser_declaration at least tries: if (std_attrs != NULL_TREE && !attribute_ignored_p (std_attrs)) warning_at (make_location (attrs_loc, attrs_loc, parser->lexer), OPT_Wattributes, "attribute ignored"); but attribute_ignored_p here checks the first attribute rather than the whole chain. So it will incorrectly not warn if there is an ignored attribute followed by non-ignored. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-12-05 Jakub Jelinek PR c++/110734 * parser.cc (cp_parser_block_declaration): Implement C++ DR 2262 - Attributes for asm-definition. Call cp_parser_asm_definition even if RID_ASM token is only seen after sequence of standard attributes. (cp_parser_asm_definition): Parse standard attributes before RID_ASM token and warn for them with -Wattributes. * g++.dg/DRs/dr2262.C: New test. * g++.dg/cpp0x/gen-attrs-76.C (foo, bar): Don't expect errors on attributes on asm definitions. * g++.dg/gomp/attrs-11.C: Remove 2 expected errors. Jakub --- gcc/cp/parser.cc.jj 2023-12-04 08:59:06.871357329 +0100 +++ gcc/cp/parser.cc 2023-12-04 20:23:53.225009856 +0100 @@ -15398,7 +15398,6 @@ cp_parser_block_declaration (cp_parser * /* Peek at the next token to figure out which kind of declaration is present. */ cp_token *token1 = cp_lexer_peek_token (parser->lexer); - size_t attr_idx; /* If the next keyword is `asm', we have an asm-definition. */ if (token1->keyword == RID_ASM) @@ -15452,22 +15451,36 @@ cp_parser_block_declaration (cp_parser * /* If the next token is `static_assert' we have a static assertion. */ else if (token1->keyword == RID_STATIC_ASSERT) cp_parser_static_assert (parser, /*member_p=*/false); - /* If the next tokens after attributes is `using namespace', then we have - a using-directive. */ - else if ((attr_idx = cp_parser_skip_std_attribute_spec_seq (parser, 1)) != 1 - && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx, - RID_USING) - && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx + 1, - RID_NAMESPACE)) + else { - if (statement_p) - cp_parser_commit_to_tentative_parse (parser); - cp_parser_using_directive (parser); + size_t attr_idx = cp_parser_skip_std_attribute_spec_seq (parser, 1); + cp_token *after_attr = NULL; + if (attr_idx != 1) + after_attr = cp_lexer_peek_nth_token (parser->lexer, attr_idx); + /* If the next tokens after attributes is `using namespace', then we have + a using-directive. */ + if (after_attr + && after_attr->keyword == RID_USING + && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx + 1, + RID_NAMESPACE)) + { + if (statement_p) + cp_parser_commit_to_tentative_parse (parser); + cp_parser_using_directive (parser); + } + /* If the next token after attributes is `asm', then we have + an asm-definition. */ + else if (after_attr && after_attr->keyword == RID_ASM) + { + if (statement_p) + cp_parser_commit_to_tentative_parse (parser); + cp_parser_asm_definition (parser); + } + /* Anything else must be a simple-declaration. */ + else + cp_parser_simple_declaration (parser, !statement_p, + /*maybe_range_for_decl*/NULL); } - /* Anything else must be a simple-declaration. */ - else - cp_parser_simple_declaration (parser, !statement_p, - /*maybe_range_for_decl*/NULL); } /* Parse a simple-declaration. @@ -22424,6 +22437,7 @@ cp_parser_asm_definition (cp_parser* par bool invalid_inputs_p = false; bool invalid_outputs_p = false; required_token missing = RT_NONE; + tree std_attrs = cp_parser_std_attribute_spec_seq (parser); location_t asm_loc = cp_lexer_peek_token (parser->lexer)->location; /* Look for the `asm' keyword. */ @@ -22657,6 +22671,10 @@ cp_parser_asm_definition (cp_parser* par else symtab->finalize_toplevel_asm (string); } + + if (std_attrs) + warning_at (asm_loc, OPT_Wattributes, + "attributes ignored on % declaration"); } /* Given the type TYPE of a declaration with declarator DECLARATOR, return the --- gcc/testsuite/g++.dg/DRs/dr2262.C.jj 2023-12-04 19:58:06.433811915 +0100 +++ gcc/testsuite/g++.dg/DRs/dr2262.C 2023-12-04 20:23:01.655737020 +0100 @@ -0,0 +1,16 @@ +// DR 2262 - Attributes for asm-definition +// { dg-do compile { target c++11 } } +// { dg-options "-Wattributes" } + +[[]] asm ("nop"); +[[foo::bar]] asm ("nop"); // { dg-warning "attributes ignored on 'asm' declaration" } + +void +foo () +{ + int i = 42; + [[]] asm ("nop"); + [[foo::bar]] asm ("nop"); // { dg-warning "attributes ignored on 'asm' declaration" } + [[]] asm ("nop" : "+r" (i)); + [[foo::bar]] [[bar::baz]] asm ("nop" : "+r" (i)); // { dg-warning "attributes ignored on 'asm' declaration" } +} --- gcc/testsuite/g++.dg/cpp0x/gen-attrs-76.C.jj 2021-08-12 09:34:16.094246634 +0200 +++ gcc/testsuite/g++.dg/cpp0x/gen-attrs-76.C 2023-12-04 20:43:45.002188817 +0100 @@ -8,9 +8,9 @@ namespace P {} void foo () { - [[]] asm (""); // { dg-error "expected" } + [[]] asm (""); [[]] __extension__ asm (""); // { dg-error "expected" } - __extension__ [[]] asm (""); // { dg-error "expected" } + __extension__ [[]] asm (""); [[]] namespace M = ::N; // { dg-error "expected" } [[]] using namespace N; // { dg-bogus "expected" } using namespace P [[]]; // { dg-error "expected" } @@ -22,9 +22,9 @@ foo () void bar () { - [[gnu::unused]] asm (""); // { dg-error "expected" } + [[gnu::unused]] asm (""); [[gnu::unused]] __extension__ asm (""); // { dg-error "expected" } - __extension__ [[gnu::unused]] asm (""); // { dg-error "expected" } + __extension__ [[gnu::unused]] asm (""); [[gnu::unused]] namespace M = ::N; // { dg-error "expected" } [[gnu::unused]] using namespace N; // { dg-bogus "expected" } using namespace P [[gnu::unused]]; // { dg-error "expected" } --- gcc/testsuite/g++.dg/gomp/attrs-11.C.jj 2021-08-12 09:34:16.720237822 +0200 +++ gcc/testsuite/g++.dg/gomp/attrs-11.C 2023-12-05 07:47:03.336641204 +0100 @@ -7,9 +7,9 @@ namespace O { typedef int T; }; void foo () { - [[omp::directive (parallel)]] asm (""); // { dg-error "expected" } + [[omp::directive (parallel)]] asm (""); [[omp::directive (parallel)]] __extension__ asm (""); // { dg-error "expected" } - __extension__ [[omp::directive (parallel)]] asm (""); // { dg-error "expected" } + __extension__ [[omp::directive (parallel)]] asm (""); [[omp::directive (parallel)]] namespace M = ::N; // { dg-error "expected" } [[omp::directive (parallel)]] using namespace N; // { dg-error "not allowed to be specified in this context" } [[omp::directive (parallel)]] using O::T; // { dg-error "expected" }