From patchwork Tue May 9 08:12:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 91448 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2706618vqo; Tue, 9 May 2023 01:16:00 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5wsC/yiaki2mY+Y+5UfXFkV6TxoyU+N8dn9kuHzFOvT8sTIhXXTLpTBd879AqlxD694FBG X-Received: by 2002:aa7:ce0a:0:b0:50c:1677:91c2 with SMTP id d10-20020aa7ce0a000000b0050c167791c2mr10152107edv.30.1683620160236; Tue, 09 May 2023 01:16:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683620160; cv=none; d=google.com; s=arc-20160816; b=asjBTaLLEYqhRaaAmBJM8TUnj5POfdLXs17lUw/x5XV+v+YDJNONEDF3kuw9ywEL80 EwcX7SfCm4sj5QGx0rM2ro+hidlyY0oHXIHpQlUEl5gq8EtIUkhcEPc5kp2vaRqJpVeD qFI5TvUltT4qUKR+fRz1Og69ZEWKN8pehKl8/lh/XMh4tvPxzAwn8Qmpgw6RNFt4bsd2 W0+pUCvlxG0igPPceaXlsc719m0huE+d7RG/49lOIx30xogbnhvS2ioP697Mc5nyuGSW wd/yWD97hmx7I0Ow0aYFaJko2h2F8d5g2lAyZcTs76ejjh91IzLXtcFZ6JMPeK0LuDNI vFTQ== 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-disposition:mime-version:message-id:subject:cc:to:date :resent-to:resent-message-id:resent-date:resent-from:dmarc-filter :delivered-to:dkim-signature:dkim-filter; bh=fFoBjDtUjLAXkXrGDqbP3bziw8kn8plEkKFLqOmoG4g=; b=peTYsOBRGRalX5rmObPTL5/XoWjMOFJC85rbAx51nN70UG6EBnzEqY8X5nBnC6j4H2 yTSFPvlPqWt/TvAaXLjDSpPi6PJZiYOfbj5XpHujIdzU2Jm5abpyqcN0Qv7hShz3kZsp fGpLRPflVSs2rO5tHYVzBGELqRn6SiJN0ghXsRqjV4VfEm13pE+ckNeSkkyD5qEIGH8M U9fmv0ZC0kL2N0oFfKFL1CCFu3qLKzL1PGzpWYqgx10QC0F8dpqnmNxIQbVkdjuroWrJ m+JTLb9H7X6drq/ZUZImLduiW3u2oIexCuSGpo5xqzI7VgthpeJZl15pfMrJ2TuPkFL3 Qhjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="as/jGUtS"; 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 m14-20020a056402050e00b0050bc757bdb7si769396edv.621.2023.05.09.01.15.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 01:16:00 -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="as/jGUtS"; 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 792A63857707 for ; Tue, 9 May 2023 08:15:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 792A63857707 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1683620156; bh=fFoBjDtUjLAXkXrGDqbP3bziw8kn8plEkKFLqOmoG4g=; h=Resent-From:Resent-Date:Resent-To:Date:To:Cc:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=as/jGUtSnslIuSQ2+bPH/RTBuxkFJuItZtU5kCyR0Xao1eraxg7QJwNL5DEbdagl1 IovsfWOKa4BsczRuRaN0N62nVkyqy4zYiUYcNKsmLJ1FYFzj6REzKra+1sA7mZk4ok TBsNCeQqaIVSAA9RbLBoXhpSF+YQfBu4W3l5L+x8= 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 C239C3858C53 for ; Tue, 9 May 2023 08:15:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C239C3858C53 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-675-G97wixz9PEeaoePYE6n2RQ-1; Tue, 09 May 2023 04:15:07 -0400 X-MC-Unique: G97wixz9PEeaoePYE6n2RQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D1DA7811E7C for ; Tue, 9 May 2023 08:15:06 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.194.156]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 940E71121314; Tue, 9 May 2023 08:15:06 +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 3498F4ga1638605 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 9 May 2023 10:15:04 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3498F3RD1638604; Tue, 9 May 2023 10:15:03 +0200 Resent-From: Jakub Jelinek Resent-Date: Tue, 9 May 2023 10:15:03 +0200 Resent-Message-ID: Resent-To: Jason Merrill , gcc-patches@gcc.gnu.org Date: Tue, 9 May 2023 10:12:51 +0200 To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Reject attributes without arguments used as pack expansion [PR109756] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 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, 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765403693082321540?= X-GMAIL-MSGID: =?utf-8?q?1765403693082321540?= Hi! The following testcase shows we silently accept (and ignore) attributes without arguments used as pack expansions. This is because we call make_pack_expansion and that starts with if (!arg || arg == error_mark_node) return arg; Now, an attribute without arguments like [[noreturn...]] is IMHO always invalid, in this case for 2 reasons; one is that as it has no arguments, no pack can be present and second is that the standard says that attributes need to specially permit uses of parameter pack and doesn't explicitly permit it for any of the standard attributes (except for alignas? which has different syntax). If an attribute has some arguments but doesn't contain packs in those arguments, make_pack_expansion will already diagnose it. My first version of the patch just added the last hunk in parser.cc and without the && !has_args, but that unfortunately regresses gen-attrs-65.C - TREE_VALUE (attribute) == NULL_TREE can mean a recognized or unrecognized attribute which doesn't have any arguments, but can also mean an unrecognized attribute which had arguments but which we didn't parse because we didn't know how to parse those arguments. So, the patch remembers from cp_parser_std_attribute whether an attribute had or didn't have arguments and diagnoses it just in the latter case. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? I guess this isn't appropriate for backports as accepts-invalid. 2023-05-09 Jakub Jelinek PR c++/109756 * parser.cc (cp_parser_std_attribute): Add HAS_ARGS argument, set it to true iff the attribute has arguments specified. (cp_parser_std_attribute_list): Adjust caller. If ... is used after attribute without arguments, diagnose it and return error_mark_node. * g++.dg/cpp0x/gen-attrs-78.C: New test. Jakub --- gcc/cp/parser.cc.jj 2023-04-25 16:40:42.010723809 +0200 +++ gcc/cp/parser.cc 2023-05-08 12:47:58.767924380 +0200 @@ -2634,7 +2634,7 @@ static tree cp_parser_gnu_attributes_opt static tree cp_parser_gnu_attribute_list (cp_parser *, bool = false); static tree cp_parser_std_attribute - (cp_parser *, tree); + (cp_parser *, tree, bool &); static tree cp_parser_std_attribute_spec (cp_parser *); static tree cp_parser_std_attribute_spec_seq @@ -29313,7 +29313,7 @@ cp_parser_omp_sequence_args (cp_parser * { balanced-token-seq }. */ static tree -cp_parser_std_attribute (cp_parser *parser, tree attr_ns) +cp_parser_std_attribute (cp_parser *parser, tree attr_ns, bool &has_args) { tree attribute, attr_id = NULL_TREE, arguments; cp_token *token; @@ -29323,6 +29323,7 @@ cp_parser_std_attribute (cp_parser *pars /* First, parse name of the attribute, a.k.a attribute-token. */ + has_args = false; token = cp_lexer_peek_token (parser->lexer); if (token->type == CPP_NAME) attr_id = token->u.value; @@ -29419,6 +29420,7 @@ cp_parser_std_attribute (cp_parser *pars return attribute; } + has_args = true; { vec *vec; int attr_flag = normal_attr; @@ -29544,7 +29546,8 @@ cp_parser_std_attribute_list (cp_parser while (true) { location_t loc = cp_lexer_peek_token (parser->lexer)->location; - attribute = cp_parser_std_attribute (parser, attr_ns); + bool has_args; + attribute = cp_parser_std_attribute (parser, attr_ns, has_args); if (attribute == error_mark_node) break; if (attribute != NULL_TREE) @@ -29562,6 +29565,12 @@ cp_parser_std_attribute_list (cp_parser if (attribute == NULL_TREE) error_at (token->location, "expected attribute before %<...%>"); + else if (TREE_VALUE (attribute) == NULL_TREE && !has_args) + { + error_at (token->location, "attribute with no arguments " + "contains no parameter packs"); + return error_mark_node; + } else { tree pack = make_pack_expansion (TREE_VALUE (attribute)); --- gcc/testsuite/g++.dg/cpp0x/gen-attrs-78.C.jj 2023-05-08 12:33:13.387581760 +0200 +++ gcc/testsuite/g++.dg/cpp0x/gen-attrs-78.C 2023-05-08 12:32:23.146301128 +0200 @@ -0,0 +1,29 @@ +// PR c++/109756 +// { dg-do compile { target c++11 } } +// { dg-options "-Wno-attributes" } + +template +[[noreturn...]] // { dg-error "attribute with no arguments contains no parameter packs" } +[[deprecated...]] // { dg-error "attribute with no arguments contains no parameter packs" } +[[nodiscard...]] // { dg-error "attribute with no arguments contains no parameter packs" } +int foo (int x) +{ + switch (x) + { + case 1: + [[likely...]]; // { dg-error "attribute with no arguments contains no parameter packs" } + [[fallthrough...]]; // { dg-error "attribute with no arguments contains no parameter packs" } + case 2: + [[unlikely...]]; // { dg-error "attribute with no arguments contains no parameter packs" } + + break; + default: + break; + } + struct T {}; + struct S { [[no_unique_address...]] T t; }; // { dg-error "attribute with no arguments contains no parameter packs" } + for (;;) + ; +} + +int a = foo <1, 2, 3> (4);