From patchwork Wed Dec 6 14:10:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 174593 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp4129812vqy; Wed, 6 Dec 2023 06:11:29 -0800 (PST) X-Google-Smtp-Source: AGHT+IFFfXqCLhrcCZEsBbDQBPKOjeqjUGNsQwAYtKZ+Vxr8NMBrXasepqv4AM2sByqrL2gRAytH X-Received: by 2002:a05:6808:3028:b0:3ae:4cab:526a with SMTP id ay40-20020a056808302800b003ae4cab526amr1184791oib.14.1701871889124; Wed, 06 Dec 2023 06:11:29 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1701871889; cv=pass; d=google.com; s=arc-20160816; b=XL5NLn2VptvicMUrba+8Bw6od1GiA+/pf4Wn1tDUJd8f/ubhJSmH+mS3vndvnJH2V5 Mx4iVuQ7n2/PalohO4w6a4BzNGOsIAxQpz0teoWCwQhOWbKLBUpQ8eKw3VW45zH/wdIK Z40KdD4TppiSgjwVfdBxUuJPJa35qENCqx9oUJBbuaGE9hCgGt9gLZmloIuGVF9ajeNO vzzFobzonTvyT9QWANGSTtN0CWHljp+lbO+2HLMNKV8hvnXQLrxP0+vt9zqdsdjzY+mX fHOWuwKVLnu2o0DBtlrgxXW8ZFiHFwF4iAVRVbKE5SfK9dI+GIwlhDdgLXTBxJIfIwB8 sJqg== 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:in-reply-to :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=uOdBElKQV/Ou+k0fA1RGfEdfatFcVOb6tIxOTVc4x8E=; fh=0xZT+NBKSeH8qOu04/61f1ZGePpF4jF/gxp331YE14k=; b=J2INbpYZeAlKlZocpxN7JYzADkPTZwmAwK74aIhq7ZOsXKvxJc+axbNR7vFb9GC0Wm 0HzcUBW3wTy5uaGX4nLouaAukadVGIZfdDMgJQP01qX+DBjbwkznqdyBeGR1lOX7+OzX 3E7Ftxw/U2U+5D+G+LWF9kKGvqKZdpq7HqvNJRHN3zd763nfDPYb/LxJAfoZZzJCru1J iDxaKHLCNSHnu0wCdwi15i400cX7cDdC1PIltsDqkryEiFjhF+wzbwq6WUSDsH+wn1vG ka5d1aSzKG6eQaYwEYCG3TcqSCPCtuK83VJ2TBGlGqwB3M28Isd56GBRq6Vwv5MwfvUA 6c8g== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ZJ0MvDWM; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id ji7-20020a056102188700b0046490344648si1238214vsb.806.2023.12.06.06.11.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 06:11:29 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ZJ0MvDWM; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 9F1D638618A3 for ; Wed, 6 Dec 2023 14:11:20 +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.133.124]) by sourceware.org (Postfix) with ESMTPS id 29C6B3858C5F for ; Wed, 6 Dec 2023 14:10:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 29C6B3858C5F 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 29C6B3858C5F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701871857; cv=none; b=o/wWXqKyxPa29TUo0Yfy2J+tWNjxUggjqhRiqZQ4rRirJDZZEl5hLdmUEYkiopg67kwfEFDV70dCueDNBD0iOcC5pTlba6uE+WMT6l4uD8xqDW9SVbkkRN4zUhz4ePFTnPWKFiTqrwVNlzDBf2LiU8HVkZMs31l2xHIBM+xeEwg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701871857; c=relaxed/simple; bh=gm6mTEJQzDLxPGxP0WCkJ1XSYNtwfhkGfH0eaBWhtG8=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=K3DtNgTXt1/8xTl4pRpewspI6vlof4set4VIpPhrYZiWdlLvlpUQOjEWge7I6YqGcYoQwoN9V/VWZj88009H17EMtxcsnyQN3aDsWxt236TkJ0jAjXk82/JMySXjibgQI53OiVucvHya1RPXYC80Bij35vGhgkkfOULnCaNHQO4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701871846; 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:in-reply-to:in-reply-to: references:references; bh=uOdBElKQV/Ou+k0fA1RGfEdfatFcVOb6tIxOTVc4x8E=; b=ZJ0MvDWMdn2pQgiQeh+klGhN2Z2Ka7oNiQTU1dNtD4pgp0zWuq34YEZqxxDCpFXCw2fJ3A z89V+iXblu2aHQ3HmszhUQWh+7BB72m8rJNlm59pzDTQIR7jX8AN8E1QPECOobPOQnrOh4 hgQRW6+5Fh5futzl9y1bgURpZQiuefc= 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-20-aof_UcGXNqqeTBrKmpu56A-1; Wed, 06 Dec 2023 09:10:45 -0500 X-MC-Unique: aof_UcGXNqqeTBrKmpu56A-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 CE72510334A3 for ; Wed, 6 Dec 2023 14:10:44 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.195.157]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 920DA8C0C; Wed, 6 Dec 2023 14:10:44 +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 3B6EAguk1609678 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 6 Dec 2023 15:10:42 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3B6EAfpf1609677; Wed, 6 Dec 2023 15:10:41 +0100 Date: Wed, 6 Dec 2023 15:10:41 +0100 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Don't diagnose ignoring of attributes if all ignored attributes are attribute_ignored_p Message-ID: References: <5e2e9627-5565-425d-9def-e1968c515b67@redhat.com> MIME-Version: 1.0 In-Reply-To: <5e2e9627-5565-425d-9def-e1968c515b67@redhat.com> X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 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_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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784542017942587410 X-GMAIL-MSGID: 1784542017942587410 On Tue, Dec 05, 2023 at 11:01:20AM -0500, Jason Merrill wrote: > > 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? > > Sounds good. The following patch implements it. I've kept warnings for cases where the C++ standard says explicitly any attributes aren't ok - "If an attribute-specifier-seq appertains to a friend declaration, that declaration shall be a definition." For some changes I haven't figured out how could I cover it in the testsuite. So far tested with GXX_TESTSUITE_STDS=98,11,14,17,20,23,26 make check-g++ RUNTESTFLAGS="dg.exp=Wno-attributes* ubsan.exp=Wno-attributes*" (which is all tests that use -Wno-attributes=), ok for trunk if it passes full bootstrap/regtest? Note, C uses a different strategy, it has c_warn_unused_attributes function which warns about all the attributes one by one unless they are ignored (or allowed in certain position). Though that is just a single diagnostic wording, while C++ FE just warns that there are some ignored attributes and doesn't name them individually (except for namespace and using namespace) and uses different wordings in different spots. 2023-12-06 Jakub Jelinek gcc/ * attribs.h (any_nonignored_attribute_p): Declare. * attribs.cc (any_nonignored_attribute_p): New function. gcc/cp/ * parser.cc (cp_parser_statement, cp_parser_expression_statement, cp_parser_declaration, cp_parser_elaborated_type_specifier, cp_parser_asm_definition): Don't diagnose ignored attributes if !any_nonignored_attribute_p. * decl.cc (grokdeclarator): Likewise. * name-lookup.cc (handle_namespace_attrs, finish_using_directive): Don't diagnose ignoring of attr_ignored_p attributes. gcc/testsuite/ * g++.dg/warn/Wno-attributes-1.C: New test. Jakub --- gcc/attribs.h.jj 2023-12-06 12:03:27.421176109 +0100 +++ gcc/attribs.h 2023-12-06 12:36:55.704884514 +0100 @@ -48,6 +48,7 @@ extern void apply_tm_attr (tree, tree); extern tree make_attribute (const char *, const char *, tree); extern bool attribute_ignored_p (tree); extern bool attribute_ignored_p (const attribute_spec *const); +extern bool any_nonignored_attribute_p (tree); extern struct scoped_attributes * register_scoped_attributes (const scoped_attribute_specs &, bool = false); --- gcc/attribs.cc.jj 2023-12-06 12:03:27.386176602 +0100 +++ gcc/attribs.cc 2023-12-06 12:36:55.704884514 +0100 @@ -584,6 +584,19 @@ attribute_ignored_p (const attribute_spe return as->max_length == -2; } +/* Return true if the ATTRS chain contains at least one attribute which + is not ignored. */ + +bool +any_nonignored_attribute_p (tree attrs) +{ + for (tree attr = attrs; attr; attr = TREE_CHAIN (attr)) + if (!attribute_ignored_p (attr)) + return true; + + return false; +} + /* See whether LIST contains at least one instance of attribute ATTR (possibly with different arguments). Return the first such attribute if so, otherwise return null. */ --- gcc/cp/parser.cc.jj 2023-12-06 12:03:27.502174967 +0100 +++ gcc/cp/parser.cc 2023-12-06 12:36:55.704884514 +0100 @@ -12778,9 +12778,8 @@ cp_parser_statement (cp_parser* parser, SET_EXPR_LOCATION (statement, statement_location); /* Allow "[[fallthrough]];" or "[[assume(cond)]];", but warn otherwise. */ - if (std_attrs != NULL_TREE) - warning_at (attrs_loc, - OPT_Wattributes, + if (std_attrs != NULL_TREE && any_nonignored_attribute_p (std_attrs)) + warning_at (attrs_loc, OPT_Wattributes, "attributes at the beginning of statement are ignored"); } @@ -12986,7 +12985,7 @@ cp_parser_expression_statement (cp_parse } /* Allow "[[fallthrough]];", but warn otherwise. */ - if (attr != NULL_TREE) + if (attr != NULL_TREE && any_nonignored_attribute_p (attr)) warning_at (loc, OPT_Wattributes, "attributes at the beginning of statement are ignored"); @@ -15191,7 +15190,7 @@ cp_parser_declaration (cp_parser* parser } } - if (std_attrs != NULL_TREE && !attribute_ignored_p (std_attrs)) + if (std_attrs != NULL_TREE && any_nonignored_attribute_p (std_attrs)) warning_at (make_location (attrs_loc, attrs_loc, parser->lexer), OPT_Wattributes, "attribute ignored"); if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) @@ -21095,14 +21094,20 @@ cp_parser_elaborated_type_specifier (cp_ if (attributes) { if (TREE_CODE (type) == TYPENAME_TYPE) - warning (OPT_Wattributes, - "attributes ignored on uninstantiated type"); + { + if (any_nonignored_attribute_p (attributes)) + warning (OPT_Wattributes, + "attributes ignored on uninstantiated type"); + } else if (tag_type != enum_type && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM && CLASSTYPE_TEMPLATE_INSTANTIATION (type) && ! processing_explicit_instantiation) - warning (OPT_Wattributes, - "attributes ignored on template instantiation"); + { + if (any_nonignored_attribute_p (attributes)) + warning (OPT_Wattributes, + "attributes ignored on template instantiation"); + } else if (is_friend && cxx11_attribute_p (attributes)) { if (warning (OPT_Wattributes, "attribute ignored")) @@ -21111,7 +21116,7 @@ cp_parser_elaborated_type_specifier (cp_ } else if (is_declaration && cp_parser_declares_only_class_p (parser)) cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); - else + else if (any_nonignored_attribute_p (attributes)) warning (OPT_Wattributes, "attributes ignored on elaborated-type-specifier that is " "not a forward declaration"); @@ -22672,7 +22677,7 @@ cp_parser_asm_definition (cp_parser* par symtab->finalize_toplevel_asm (string); } - if (std_attrs) + if (std_attrs && any_nonignored_attribute_p (std_attrs)) warning_at (asm_loc, OPT_Wattributes, "attributes ignored on % declaration"); } --- gcc/cp/decl.cc.jj 2023-12-06 12:03:27.483175235 +0100 +++ gcc/cp/decl.cc 2023-12-06 12:36:55.698884598 +0100 @@ -13058,7 +13058,8 @@ grokdeclarator (const cp_declarator *dec && !diagnose_misapplied_contracts (declspecs->std_attributes)) { location_t attr_loc = declspecs->locations[ds_std_attribute]; - if (warning_at (attr_loc, OPT_Wattributes, "attribute ignored")) + if (any_nonignored_attribute_p (declspecs->std_attributes) + && warning_at (attr_loc, OPT_Wattributes, "attribute ignored")) inform (attr_loc, "an attribute that appertains to a type-specifier " "is ignored"); } --- gcc/cp/name-lookup.cc.jj 2023-12-06 12:03:27.495175066 +0100 +++ gcc/cp/name-lookup.cc 2023-12-06 12:36:55.695884641 +0100 @@ -6356,7 +6356,7 @@ handle_namespace_attrs (tree ns, tree at DECL_ATTRIBUTES (ns) = tree_cons (name, args, DECL_ATTRIBUTES (ns)); } - else + else if (!attribute_ignored_p (d)) { warning (OPT_Wattributes, "%qD attribute directive ignored", name); @@ -8703,7 +8703,7 @@ finish_using_directive (tree target, tre diagnosed = true; } } - else + else if (!attribute_ignored_p (a)) warning (OPT_Wattributes, "%qD attribute directive ignored", name); } } --- gcc/testsuite/g++.dg/warn/Wno-attributes-1.C.jj 2023-12-06 14:27:29.006249075 +0100 +++ gcc/testsuite/g++.dg/warn/Wno-attributes-1.C 2023-12-06 14:45:32.475056797 +0100 @@ -0,0 +1,46 @@ +// { dg-do compile { target c++11 } } +// { dg-options "-Wno-attributes=bar:: -Wno-attributes=baz::qux" } + +[[foo::bar]]; // { dg-warning "attribute ignored" } +[[bar::foo, foo::bar, baz::qux]]; // { dg-warning "attribute ignored" } +[[bar::foo, bar::bar, baz::qux]]; // { dg-bogus "attribute ignored" } + +namespace [[foo::bar]] N { // { dg-warning "'bar' attribute directive ignored" } + int n; +} +namespace [[bar::foo, foo::bar, baz::qux]] O { // { dg-warning "'bar' attribute directive ignored" } + int o; +} +namespace [[bar::foo, bar::bar, baz::qux]] P { // { dg-bogus "attribute directive ignored" } + int p; +} + +void +foo () +{ + int i = 0; + [[foo::bar]]; // { dg-warning "attributes at the beginning of statement are ignored" } + [[bar::foo, foo::bar, baz::qux]]; // { dg-warning "attributes at the beginning of statement are ignored" } + [[bar::foo, bar::bar, baz::qux]]; // { dg-bogus "attributes at the beginning of statement are ignored" } + [[foo::bar]] ++i; // { dg-warning "attributes at the beginning of statement are ignored" } + [[bar::foo, foo::bar, baz::qux]] ++i; // { dg-warning "attributes at the beginning of statement are ignored" } + [[bar::foo, bar::bar, baz::qux]] ++i; // { dg-bogus "attributes at the beginning of statement are ignored" } + [[foo::bar]] asm (""); // { dg-warning "attributes ignored on 'asm' declaration" } + [[bar::foo, foo::bar, baz::qux]] asm (""); // { dg-warning "attributes ignored on 'asm' declaration" } + [[bar::foo, bar::bar, baz::qux]] asm (""); // { dg-bogus "attributes ignored on 'asm' declaration" } + [[foo::bar]] using namespace N; // { dg-warning "'bar' attribute directive ignored" } + [[bar::foo, foo::bar, baz::qux]] using namespace O; // { dg-warning "'bar' attribute directive ignored" } + [[bar::foo, bar::bar, baz::qux]] using namespace P; // { dg-bogus "attribute directive ignored" } +} + +class S +{ + [[foo::bar]] friend int bar (S &); // { dg-warning "attribute ignored" } + // { dsg-message "an attribute that appertains to a friend declaration that is not a definition is ignored" "" { target *-*-* } .-1 } + [[bar::foo, foo::bar, baz::qux]] friend int baz (S &); // { dg-warning "attribute ignored" } + // { dsg-message "an attribute that appertains to a friend declaration that is not a definition is ignored" "" { target *-*-* } .-1 } + [[bar::foo, bar::bar, baz::qux]] friend int qux (S &); // { dg-warning "attribute ignored" } + // { dsg-message "an attribute that appertains to a friend declaration that is not a definition is ignored" "" { target *-*-* } .-1 } +public: + int s; +};