From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 44669394849E for ; Thu, 6 Oct 2022 13:42:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 44669394849E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1665063770; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QY2P/nahf3eqQBFJXz1VytRsu0tsiyMtH0NCuJbSHdE=; b=QiRhAzHhrkGWQCghU1Sviv+xhPBoKfnQHEsW/+cN3Wenca3ogeueYRa684O8FefSEIW3Cp JU+HQkBw7FFaJjsIJr1WUKNTuMpB14rdTA/zuas6/dfNC1z4vh83F9exciesXx4lWGfWcK c1rEMwcydxwI5Dn/JIklG3jVygznC8o= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-544-fYH_URz1NTec4nLjUou8UQ-1; Thu, 06 Oct 2022 09:42:50 -0400 X-MC-Unique: fYH_URz1NTec4nLjUou8UQ-1 Received: by mail-qv1-f69.google.com with SMTP id cy11-20020a05621418cb00b004b17b3cf429so1118385qvb.6 for ; Thu, 06 Oct 2022 06:42:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date; bh=QY2P/nahf3eqQBFJXz1VytRsu0tsiyMtH0NCuJbSHdE=; b=LMi3MGQQBPil/TRZS+jL/OwrKe/ug0+PTyDW35E9rVN4ySLebuumiXLcUJ+b9l0Pw5 hlevnErTtMkCQrgphSFGDPT3kaS7dZPOX85TESbisxQ9Iv2QdbYXyNrlkxlcmqKrSvBF 2ViHokyqXKRN7PN7w3bK/HQjKQwhiiQxXx18gSw/pgTElVc+PV8mRTTkp6qYg05pHAYl 1DL8me7t6sS+yPOoedyOzgjCqF/U0N7Tkg6TT3qYdyiGIEUDb//Ju13kzx+PbeLCHwhn vheVz3YvqaBIosF3e9Kb3hOwTO60FYCHWxJiQ+MRyfsWa1u+irq/V30qr4X/gjLEQO5p Lqmw== X-Gm-Message-State: ACrzQf16rUkA9LxIUcQUXqkFT52dSFXNvWIjjdLfIPegoscCmWPXOGSZ schgP/ethkcTM66VggsRtguFOZIGT5lBsQ7uGj/E3TEMaEUp+MiNBxcuAX3jMpdCdm4tt1SzjR4 O5PcZoOXAI+QO84QI6Q== X-Received: by 2002:a05:6214:262b:b0:4b1:b691:1ed9 with SMTP id gv11-20020a056214262b00b004b1b6911ed9mr3900948qvb.87.1665063769406; Thu, 06 Oct 2022 06:42:49 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7ajeeAMoTcAJetO2/y/9WTBn6v+F2UohQas37Xq4lbvCFEXgOdw4kzeehOXyYfWOvIfuWm1Q== X-Received: by 2002:a05:6214:262b:b0:4b1:b691:1ed9 with SMTP id gv11-20020a056214262b00b004b1b6911ed9mr3900916qvb.87.1665063768978; Thu, 06 Oct 2022 06:42:48 -0700 (PDT) Received: from [192.168.1.101] (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id ca10-20020a05622a1f0a00b00304fe5247bfsm16162987qtb.36.2022.10.06.06.42.47 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 06 Oct 2022 06:42:48 -0700 (PDT) Message-ID: <1a197a3f-f022-5293-323b-c53a9d43e946@redhat.com> Date: Thu, 6 Oct 2022 09:42:47 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.13.1 Subject: Re: [PATCH] c++: Improve handling of foreigner namespace attributes To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org References: From: Jason Merrill In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,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 List-Id: On 10/5/22 08:04, Jakub Jelinek wrote: > 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. > > --- 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); Maybe lookup_attribute with a magic namespace argument that matches either null or gnu? > 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))) And similarly, a new is_attribute_namespace (name, t) function with the same behavior, to be used here and in all the below hunks? > { > - 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 } > +} > > Jakub >