From patchwork Wed Oct 5 12:04:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1727 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp564290wrs; Wed, 5 Oct 2022 05:06:47 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4zm2LLNWOCBvtmyyQp3br5f36KtgWVgPGCfojlvOcwdckzQYmUll+tnLo2whuhvzoVJYGx X-Received: by 2002:aa7:c58a:0:b0:459:1511:6cff with SMTP id g10-20020aa7c58a000000b0045915116cffmr12014204edq.27.1664971607097; Wed, 05 Oct 2022 05:06:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664971607; cv=none; d=google.com; s=arc-20160816; b=hMWsro4+CxWs+fNhLjaq5KB505sKHlHX6p9kDH++bxX1DiACmkZbs1jDxn+V7cW/i2 7RJI/QuriqAjwwmw+IN12ibDQoMtvjDMC6Fj0/jHBZ71WdXTknenfu9+Hlkx0lsK8UKk llKeHgzAvz6Ptr9MzxbiUveZHUlg49CKXgU2znvMv1J02CZ2xSdBp2kdsEajSX2B3m4B FQwGbIjk2Fs5Hzl3zqpvsoql61v/x0aWw7xVpM4ttW3Ahy1xr/B9ob26rS+KtrcM6idq ZljrP7MNmxhIHToTJMBTFAUlpHMTehzWGcyBWzINQLQYI5DbSy/3Ri5PDLH0oMBczTUO MBGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=88EWdztq/KLZhkSmVT0zwBplGW0NEt72vF3iHMArwB4=; b=0lt83ZzoIsgC/xAYtSXslD1T7B8Mf5JtXPKsMfHfrkPRZ5VM8uAfBKiQcCsc4SIDtz 6Gv0z9khtpHO6rrB9GWEe9VKx1WluGxZAnl/bkmfEedJAlb4GjP0FrbZl+dsWY0/kDnj 2T30a47TjrsQ496P8ol6/yGfbRahg4y4c7Gfm2gLOzB1gcNjsESdA+H3dMAXY9KTwkm4 FKjaTSgyjX9HN3Z1AOssSaQJp7SCYO6L5ahg8XtB6Wh1Bm5gU37+8Kb2T2gXcATYTTX7 OCbbYNLlIwCJQBBnEyhO/hHdraA4SM69W0G7T+i/JjuCD0rM7OF2GorLdSMCTxy5vo26 zpRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=x2dHkmxh; 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=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id b10-20020a056402278a00b004585f904a4esi3342513ede.360.2022.10.05.05.06.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Oct 2022 05:06:47 -0700 (PDT) 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=@gcc.gnu.org header.s=default header.b=x2dHkmxh; 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=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id ADCB13853557 for ; Wed, 5 Oct 2022 12:05:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org ADCB13853557 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1664971541; bh=88EWdztq/KLZhkSmVT0zwBplGW0NEt72vF3iHMArwB4=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=x2dHkmxhLPm8v4YH1T+e1aaglxT2uVlycV/p5yD20fHODXuav5rUEe2UqwYLff0on HlwvztZHBfhqJpUnFVaVpY9P3Ip1YEY6vZkNM+804eQLY2+k1QfDJSdHRVWoo5xQkX dhHs1ilXw/B9SuX6jDD2uN65sgyckP+ExDhe7Jmk= 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 950C63853579 for ; Wed, 5 Oct 2022 12:04:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 950C63853579 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-558-7kisMOr5NXmy8osqJrbG0g-1; Wed, 05 Oct 2022 08:04:55 -0400 X-MC-Unique: 7kisMOr5NXmy8osqJrbG0g-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 D83263804070 for ; Wed, 5 Oct 2022 12:04:54 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.194]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 997B320182B2; Wed, 5 Oct 2022 12:04:54 +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 295C4q574087927 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 5 Oct 2022 14:04:52 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 295C4pld4087926; Wed, 5 Oct 2022 14:04:51 +0200 Date: Wed, 5 Oct 2022 14:04:51 +0200 To: Jason Merrill Subject: [PATCH] c++: Improve handling of foreigner namespace attributes Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-4.1 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 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 Cc: gcc-patches@gcc.gnu.org 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?1745849268226366833?= X-GMAIL-MSGID: =?utf-8?q?1745849268226366833?= Hi! The following patch uses the new lookup_attribute overload and extra tests to avoid emitting weird warnings on foreign namespace attributes which we should just ignore (perhaps with a warning), but shouldn't imply any meaning to them just because they have a name matching some standard or gnu attribute name. Lightly tested so far, ok for trunk if full bootstrap/regtest succeeds? 2022-10-05 Jakub Jelinek gcc/c-family/ * c-common.cc (attribute_fallthrough_p): Lookup fallthrough attribute only in gnu namespace or as standard attribute, treat fallthrough attributes in other namespaces like any other unknown attribute. gcc/cp/ * parser.cc (cp_parser_check_std_attribute): Only do checks if attribute is a standard attribute or in gnu namespace and only lookup other attributes in those namespaces. * cp-gimplify.cc (lookup_hotness_attribute): Adjust function comment. Only return true for standard attribute or gnu namespace attribute. (remove_hotness_attribute): Only remove hotness attributes when they are standard or in gnu namespace, implement it in a single loop rather than former 4 now 8 remove_attribute calls. gcc/testsuite/ * g++.dg/cpp1z/fallthrough2.C: New test. * g++.dg/cpp2a/attr-likely7.C: New test. Jakub --- gcc/c-family/c-common.cc.jj 2022-10-03 18:00:58.548661062 +0200 +++ gcc/c-family/c-common.cc 2022-10-05 12:58:08.396043609 +0200 @@ -6007,12 +6007,15 @@ attribute_fallthrough_p (tree attr) { if (attr == error_mark_node) return false; - tree t = lookup_attribute ("fallthrough", attr); + tree t = lookup_attribute (NULL, "fallthrough", attr); + if (t == NULL_TREE) + t = lookup_attribute ("gnu", "fallthrough", attr); if (t == NULL_TREE) return false; /* It is no longer true that "this attribute shall appear at most once in each attribute-list", but we still give a warning. */ - if (lookup_attribute ("fallthrough", TREE_CHAIN (t))) + if (lookup_attribute (NULL, "fallthrough", TREE_CHAIN (t)) + || lookup_attribute ("gnu", "fallthrough", TREE_CHAIN (t))) warning (OPT_Wattributes, "attribute % specified multiple " "times"); /* No attribute-argument-clause shall be present. */ @@ -6023,9 +6026,11 @@ attribute_fallthrough_p (tree attr) for (t = attr; t != NULL_TREE; t = TREE_CHAIN (t)) { tree name = get_attribute_name (t); - if (!is_attribute_p ("fallthrough", name)) + tree ns = get_attribute_namespace (t); + if (!is_attribute_p ("fallthrough", name) + || (ns && !is_attribute_p ("gnu", ns))) { - if (!c_dialect_cxx () && get_attribute_namespace (t) == NULL_TREE) + if (!c_dialect_cxx () && ns == NULL_TREE) /* The specifications of standard attributes in C mean this is a constraint violation. */ pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored", --- gcc/cp/parser.cc.jj 2022-10-05 11:28:28.292141301 +0200 +++ gcc/cp/parser.cc 2022-10-05 13:29:38.203433190 +0200 @@ -29265,7 +29265,10 @@ cp_parser_check_std_attribute (location_ if (attributes) for (const auto &a : alist) if (is_attribute_p (a, get_attribute_name (attribute)) - && lookup_attribute (a, attributes)) + && (get_attribute_namespace (attribute) == NULL_TREE + || get_attribute_namespace (attribute) == gnu_identifier) + && (lookup_attribute (NULL, a, attributes) + || lookup_attribute ("gnu", a, attributes))) { if (!from_macro_expansion_at (loc)) warning_at (loc, OPT_Wattributes, "attribute %qs specified " --- gcc/cp/cp-gimplify.cc.jj 2022-10-05 11:19:19.160573401 +0200 +++ gcc/cp/cp-gimplify.cc 2022-10-05 12:53:08.367115180 +0200 @@ -3027,7 +3027,7 @@ cp_fold (tree x) return x; } -/* Look up either "hot" or "cold" in attribute list LIST. */ +/* Look up "hot", "cold", "likely" or "unlikely" in attribute list LIST. */ tree lookup_hotness_attribute (tree list) @@ -3039,20 +3039,38 @@ lookup_hotness_attribute (tree list) || is_attribute_p ("cold", name) || is_attribute_p ("likely", name) || is_attribute_p ("unlikely", name)) - break; + { + tree ns = get_attribute_namespace (list); + if (ns == NULL_TREE || ns == gnu_identifier) + break; + } } return list; } -/* Remove both "hot" and "cold" attributes from LIST. */ +/* Remove "hot", "cold", "likely" and "unlikely" attributes from LIST. */ static tree remove_hotness_attribute (tree list) { - list = remove_attribute ("hot", list); - list = remove_attribute ("cold", list); - list = remove_attribute ("likely", list); - list = remove_attribute ("unlikely", list); + for (tree *p = &list; *p; ) + { + tree l = *p; + tree name = get_attribute_name (l); + if (is_attribute_p ("hot", name) + || is_attribute_p ("cold", name) + || is_attribute_p ("likely", name) + || is_attribute_p ("unlikely", name)) + { + tree ns = get_attribute_namespace (list); + if (ns == NULL_TREE || ns == gnu_identifier) + { + *p = TREE_CHAIN (l); + continue; + } + } + p = &TREE_CHAIN (l); + } return list; } --- gcc/testsuite/g++.dg/cpp1z/fallthrough2.C.jj 2022-10-05 13:11:31.622152911 +0200 +++ gcc/testsuite/g++.dg/cpp1z/fallthrough2.C 2022-10-05 13:31:48.175672763 +0200 @@ -0,0 +1,24 @@ +// { dg-do compile { target c++17 } } +// { dg-options "-Wextra -Wall -Wpedantic" } + +int +foo (int i) +{ + switch (i) + { + case 2: + ++i; + [[fallthrough, whatever::fallthrough]]; // { dg-bogus "attribute 'fallthrough' specified multiple times" } + case 3: // { dg-warning "'fallthrough' attribute ignored" "" { target *-*-* } .-1 } + ++i; + [[fallthrough, whatever2::fallthrough(1, 2, 3)]]; // { dg-bogus "attribute 'fallthrough' specified multiple times" } + case 4: // { dg-warning "'fallthrough' attribute ignored" "" { target *-*-* } .-1 } + [[whatever3::fallthrough("abcd")]]; // { dg-warning "attributes at the beginning of statement are ignored" } + case 5: + [[whatever4::fallthrough]]; // { dg-bogus "attribute 'fallthrough' not preceding a case label or default label" } + ++i; // { dg-warning "attributes at the beginning of statement are ignored" "" { target *-*-* } .-1 } + default: + break; + } + return i; +} --- gcc/testsuite/g++.dg/cpp2a/attr-likely7.C.jj 2022-10-05 13:12:21.910471659 +0200 +++ gcc/testsuite/g++.dg/cpp2a/attr-likely7.C 2022-10-05 13:35:01.318056722 +0200 @@ -0,0 +1,38 @@ +// { dg-do compile { target c++20 } } +// { dg-additional-options -fdump-tree-gimple } +// { dg-final { scan-tree-dump-times "hot label" 5 "gimple" } } +// { dg-final { scan-tree-dump-times "cold label" 3 "gimple" } } + +bool b; + +template int f() +{ + if (b) + [[likely, whatever::unlikely ("abcd")]] return 0; // { dg-bogus "ignoring attribute 'unlikely' after earlier 'likely'" } + else // { dg-warning "attributes at the beginning of statement are ignored" "" { target *-*-* } .-1 } + [[unlikely, whatever2::hot]] flabel: return 1; // { dg-warning "'whatever2::hot' scoped attribute directive ignored" } + switch (b) + { + [[likely, whatever3::cold (1, 2, 3)]] case true: break; // { dg-warning "'whatever3::cold' scoped attribute directive ignored" } + }; + return 1; +} + +int main() +{ + if (b) + [[whatever4::unlikely (1), likely]] return 0; // { dg-bogus "ignoring attribute 'likely' after earlier 'unlikely'" } + else if (b) // { dg-warning "attributes at the beginning of statement are ignored" "" { target *-*-* } .-1 } + [[whatever5::hot, unlikely]] elabel: // { dg-warning "'whatever5::hot' scoped attribute directive ignored" } + return 1; + else + [[whatever6::cold, likely]] b = false; // { dg-bogus "ignoring attribute 'likely' after earlier 'cold'" } + // { dg-warning "attributes at the beginning of statement are ignored" "" { target *-*-* } .-1 } + f(); + + switch (b) + { + [[whatever7::unlikely (1), likely]] case true: break; // { dg-warning "'whatever7::unlikely' scoped attribute directive ignored" } + [[whatever8::unlikely, unlikely]] case false: break; // { dg-bogus "attribute 'unlikely' specified multiple times" } + }; // { dg-warning "'whatever8::unlikely' scoped attribute directive ignored" "" { target *-*-* } .-1 } +}