* [PATCH] c++: Improve handling of foreigner namespace attributes @ 2022-10-05 12:04 Jakub Jelinek 2022-10-06 13:42 ` Jason Merrill 0 siblings, 1 reply; 6+ messages in thread From: Jakub Jelinek @ 2022-10-05 12:04 UTC (permalink / raw) To: Jason Merrill; +Cc: gcc-patches 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 <jakub@redhat.com> 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); 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 %<fallthrough%> 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 <class T> 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<int>(); + + 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 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] c++: Improve handling of foreigner namespace attributes 2022-10-05 12:04 [PATCH] c++: Improve handling of foreigner namespace attributes Jakub Jelinek @ 2022-10-06 13:42 ` Jason Merrill 2022-10-06 17:20 ` [PATCH] c++, v2: " Jakub Jelinek 0 siblings, 1 reply; 6+ messages in thread From: Jason Merrill @ 2022-10-06 13:42 UTC (permalink / raw) To: Jakub Jelinek; +Cc: gcc-patches 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 <jakub@redhat.com> > > 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 %<fallthrough%> 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 <class T> 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<int>(); > + > + 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 > ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH] c++, v2: Improve handling of foreigner namespace attributes 2022-10-06 13:42 ` Jason Merrill @ 2022-10-06 17:20 ` Jakub Jelinek 2022-10-06 17:30 ` Jason Merrill 0 siblings, 1 reply; 6+ messages in thread From: Jakub Jelinek @ 2022-10-06 17:20 UTC (permalink / raw) To: Jason Merrill; +Cc: gcc-patches On Thu, Oct 06, 2022 at 09:42:47AM -0400, Jason Merrill wrote: > > - 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? > > And similarly, a new is_attribute_namespace (name, t) function with the same > behavior, to be used here and in all the below hunks? So like this? 2022-10-05 Jakub Jelinek <jakub@redhat.com> gcc/ * attribs.h (is_attribute_namespace_p): New inline function. (lookup_attribute): Document meaning of ATTR_NS equal to "". * attribs.cc (remove_attribute): Use is_attribute_namespace_p. (private_lookup_attribute): For ATTR_NS "" match either standard attribute or "gnu" namespace one. 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/attribs.h.jj 2022-10-05 16:15:29.834889059 +0200 +++ gcc/attribs.h 2022-10-06 17:59:56.918430468 +0200 @@ -188,6 +188,21 @@ is_attribute_p (const char *attr_name, c IDENTIFIER_POINTER (ident), IDENTIFIER_LENGTH (ident)); } +/* Given an identifier node IDENT or NULL and a string ATTR_NS, return true + if the identifier node is a valid attribute namespace for the string. + ATTR_NS "" stands for standard attribute (NULL IDENT) or "gnu" + namespace. */ + +static inline bool +is_attribute_namespace_p (const char *attr_ns, const_tree ident) +{ + if (attr_ns == NULL) + return ident == NULL_TREE; + if (attr_ns[0]) + return ident && is_attribute_p (attr_ns, ident); + return ident == NULL_TREE || is_attribute_p ("gnu", ident); +} + /* Given an attribute name ATTR_NAME and a list of attributes LIST, return a pointer to the attribute's list element if the attribute is part of the list, or NULL_TREE if not found. If the attribute @@ -217,7 +232,8 @@ lookup_attribute (const char *attr_name, } } -/* Similar to lookup_attribute, but also match the attribute namespace. */ +/* Similar to lookup_attribute, but also match the attribute namespace. + ATTR_NS "" stands for either standard attribute or "gnu" namespace. */ static inline tree lookup_attribute (const char *attr_ns, const char *attr_name, tree list) --- gcc/attribs.cc.jj 2022-10-04 23:13:10.289090796 +0200 +++ gcc/attribs.cc 2022-10-06 18:01:32.096146195 +0200 @@ -1645,7 +1645,8 @@ remove_attribute (const char *attr_name, return list; } -/* Similarly but also match namespace on the removed attributes. */ +/* Similarly but also match namespace on the removed attributes. + ATTR_NS "" stands for NULL or "gnu" namespace. */ tree remove_attribute (const char *attr_ns, const char *attr_name, tree list) @@ -1662,8 +1663,7 @@ remove_attribute (const char *attr_ns, c if (is_attribute_p (attr_name, attr)) { tree ns = get_attribute_namespace (l); - if ((ns == NULL_TREE && attr_ns == NULL) - || (ns && attr_ns && is_attribute_p (attr_ns, ns))) + if (is_attribute_namespace_p (attr_ns, ns)) { *p = TREE_CHAIN (l); continue; @@ -2088,14 +2088,20 @@ private_lookup_attribute (const char *at tree ns = get_attribute_namespace (list); if (ns == NULL_TREE) { - if (attr_ns == NULL) + if (attr_ns_len == 0) break; } else if (attr_ns) { ident_len = IDENTIFIER_LENGTH (ns); - if (cmp_attribs (attr_ns, attr_ns_len, IDENTIFIER_POINTER (ns), - ident_len)) + if (attr_ns_len == 0) + { + if (cmp_attribs ("gnu", strlen ("gnu"), + IDENTIFIER_POINTER (ns), ident_len)) + break; + } + else if (cmp_attribs (attr_ns, attr_ns_len, + IDENTIFIER_POINTER (ns), ident_len)) break; } } --- gcc/c-family/c-common.cc.jj 2022-10-06 17:43:47.880502941 +0200 +++ gcc/c-family/c-common.cc 2022-10-06 18:56:05.432888180 +0200 @@ -6008,12 +6008,12 @@ attribute_fallthrough_p (tree attr) { if (attr == error_mark_node) return false; - tree t = lookup_attribute ("fallthrough", attr); + tree t = lookup_attribute ("", "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 ("", "fallthrough", TREE_CHAIN (t))) warning (OPT_Wattributes, "attribute %<fallthrough%> specified multiple " "times"); /* No attribute-argument-clause shall be present. */ @@ -6024,9 +6024,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) + || !is_attribute_namespace_p ("", 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-06 17:43:42.418576616 +0200 +++ gcc/cp/parser.cc 2022-10-06 18:57:14.019956324 +0200 @@ -29265,7 +29265,8 @@ 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)) + && is_attribute_namespace_p ("", get_attribute_namespace (attribute)) + && lookup_attribute ("", a, attributes)) { if (!from_macro_expansion_at (loc)) warning_at (loc, OPT_Wattributes, "attribute %qs specified " --- gcc/cp/cp-gimplify.cc.jj 2022-10-06 17:43:42.413576684 +0200 +++ gcc/cp/cp-gimplify.cc 2022-10-06 18:59:20.763234354 +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) @@ -3035,24 +3035,36 @@ lookup_hotness_attribute (tree list) for (; list; list = TREE_CHAIN (list)) { tree name = get_attribute_name (list); - if (is_attribute_p ("hot", name) - || is_attribute_p ("cold", name) - || is_attribute_p ("likely", name) - || is_attribute_p ("unlikely", name)) + if ((is_attribute_p ("hot", name) + || is_attribute_p ("cold", name) + || is_attribute_p ("likely", name) + || is_attribute_p ("unlikely", name)) + && is_attribute_namespace_p ("", get_attribute_namespace (list))) 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)) + && is_attribute_namespace_p ("", get_attribute_namespace (l))) + { + *p = TREE_CHAIN (l); + continue; + } + p = &TREE_CHAIN (l); + } return list; } --- gcc/testsuite/g++.dg/cpp1z/fallthrough2.C.jj 2022-10-06 17:45:55.452782142 +0200 +++ gcc/testsuite/g++.dg/cpp1z/fallthrough2.C 2022-10-06 17:45:55.452782142 +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-06 17:45:55.452782142 +0200 +++ gcc/testsuite/g++.dg/cpp2a/attr-likely7.C 2022-10-06 17:45:55.452782142 +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 <class T> 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<int>(); + + 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 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] c++, v2: Improve handling of foreigner namespace attributes 2022-10-06 17:20 ` [PATCH] c++, v2: " Jakub Jelinek @ 2022-10-06 17:30 ` Jason Merrill 2022-10-06 18:29 ` [PATCH] c++, v3: " Jakub Jelinek 0 siblings, 1 reply; 6+ messages in thread From: Jason Merrill @ 2022-10-06 17:30 UTC (permalink / raw) To: Jakub Jelinek; +Cc: gcc-patches On 10/6/22 13:20, Jakub Jelinek wrote: > On Thu, Oct 06, 2022 at 09:42:47AM -0400, Jason Merrill wrote: >>> - 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? >> >> And similarly, a new is_attribute_namespace (name, t) function with the same >> behavior, to be used here and in all the below hunks? > > So like this? Yes, except I was thinking the new function would take the attribute TREE_LIST as its parameter so that all the callers don't have to also call get_attribute_namespace. > 2022-10-05 Jakub Jelinek <jakub@redhat.com> > > gcc/ > * attribs.h (is_attribute_namespace_p): New inline function. > (lookup_attribute): Document meaning of ATTR_NS equal to "". > * attribs.cc (remove_attribute): Use is_attribute_namespace_p. > (private_lookup_attribute): For ATTR_NS "" match either standard > attribute or "gnu" namespace one. > 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/attribs.h.jj 2022-10-05 16:15:29.834889059 +0200 > +++ gcc/attribs.h 2022-10-06 17:59:56.918430468 +0200 > @@ -188,6 +188,21 @@ is_attribute_p (const char *attr_name, c > IDENTIFIER_POINTER (ident), IDENTIFIER_LENGTH (ident)); > } > > +/* Given an identifier node IDENT or NULL and a string ATTR_NS, return true > + if the identifier node is a valid attribute namespace for the string. > + ATTR_NS "" stands for standard attribute (NULL IDENT) or "gnu" > + namespace. */ > + > +static inline bool > +is_attribute_namespace_p (const char *attr_ns, const_tree ident) > +{ > + if (attr_ns == NULL) > + return ident == NULL_TREE; > + if (attr_ns[0]) > + return ident && is_attribute_p (attr_ns, ident); > + return ident == NULL_TREE || is_attribute_p ("gnu", ident); > +} > + > /* Given an attribute name ATTR_NAME and a list of attributes LIST, > return a pointer to the attribute's list element if the attribute > is part of the list, or NULL_TREE if not found. If the attribute > @@ -217,7 +232,8 @@ lookup_attribute (const char *attr_name, > } > } > > -/* Similar to lookup_attribute, but also match the attribute namespace. */ > +/* Similar to lookup_attribute, but also match the attribute namespace. > + ATTR_NS "" stands for either standard attribute or "gnu" namespace. */ > > static inline tree > lookup_attribute (const char *attr_ns, const char *attr_name, tree list) > --- gcc/attribs.cc.jj 2022-10-04 23:13:10.289090796 +0200 > +++ gcc/attribs.cc 2022-10-06 18:01:32.096146195 +0200 > @@ -1645,7 +1645,8 @@ remove_attribute (const char *attr_name, > return list; > } > > -/* Similarly but also match namespace on the removed attributes. */ > +/* Similarly but also match namespace on the removed attributes. > + ATTR_NS "" stands for NULL or "gnu" namespace. */ > > tree > remove_attribute (const char *attr_ns, const char *attr_name, tree list) > @@ -1662,8 +1663,7 @@ remove_attribute (const char *attr_ns, c > if (is_attribute_p (attr_name, attr)) > { > tree ns = get_attribute_namespace (l); > - if ((ns == NULL_TREE && attr_ns == NULL) > - || (ns && attr_ns && is_attribute_p (attr_ns, ns))) > + if (is_attribute_namespace_p (attr_ns, ns)) > { > *p = TREE_CHAIN (l); > continue; > @@ -2088,14 +2088,20 @@ private_lookup_attribute (const char *at > tree ns = get_attribute_namespace (list); > if (ns == NULL_TREE) > { > - if (attr_ns == NULL) > + if (attr_ns_len == 0) > break; > } > else if (attr_ns) > { > ident_len = IDENTIFIER_LENGTH (ns); > - if (cmp_attribs (attr_ns, attr_ns_len, IDENTIFIER_POINTER (ns), > - ident_len)) > + if (attr_ns_len == 0) > + { > + if (cmp_attribs ("gnu", strlen ("gnu"), > + IDENTIFIER_POINTER (ns), ident_len)) > + break; > + } > + else if (cmp_attribs (attr_ns, attr_ns_len, > + IDENTIFIER_POINTER (ns), ident_len)) > break; > } > } > --- gcc/c-family/c-common.cc.jj 2022-10-06 17:43:47.880502941 +0200 > +++ gcc/c-family/c-common.cc 2022-10-06 18:56:05.432888180 +0200 > @@ -6008,12 +6008,12 @@ attribute_fallthrough_p (tree attr) > { > if (attr == error_mark_node) > return false; > - tree t = lookup_attribute ("fallthrough", attr); > + tree t = lookup_attribute ("", "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 ("", "fallthrough", TREE_CHAIN (t))) > warning (OPT_Wattributes, "attribute %<fallthrough%> specified multiple " > "times"); > /* No attribute-argument-clause shall be present. */ > @@ -6024,9 +6024,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) > + || !is_attribute_namespace_p ("", 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-06 17:43:42.418576616 +0200 > +++ gcc/cp/parser.cc 2022-10-06 18:57:14.019956324 +0200 > @@ -29265,7 +29265,8 @@ 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)) > + && is_attribute_namespace_p ("", get_attribute_namespace (attribute)) > + && lookup_attribute ("", a, attributes)) > { > if (!from_macro_expansion_at (loc)) > warning_at (loc, OPT_Wattributes, "attribute %qs specified " > --- gcc/cp/cp-gimplify.cc.jj 2022-10-06 17:43:42.413576684 +0200 > +++ gcc/cp/cp-gimplify.cc 2022-10-06 18:59:20.763234354 +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) > @@ -3035,24 +3035,36 @@ lookup_hotness_attribute (tree list) > for (; list; list = TREE_CHAIN (list)) > { > tree name = get_attribute_name (list); > - if (is_attribute_p ("hot", name) > - || is_attribute_p ("cold", name) > - || is_attribute_p ("likely", name) > - || is_attribute_p ("unlikely", name)) > + if ((is_attribute_p ("hot", name) > + || is_attribute_p ("cold", name) > + || is_attribute_p ("likely", name) > + || is_attribute_p ("unlikely", name)) > + && is_attribute_namespace_p ("", get_attribute_namespace (list))) > 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)) > + && is_attribute_namespace_p ("", get_attribute_namespace (l))) > + { > + *p = TREE_CHAIN (l); > + continue; > + } > + p = &TREE_CHAIN (l); > + } > return list; > } > > --- gcc/testsuite/g++.dg/cpp1z/fallthrough2.C.jj 2022-10-06 17:45:55.452782142 +0200 > +++ gcc/testsuite/g++.dg/cpp1z/fallthrough2.C 2022-10-06 17:45:55.452782142 +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-06 17:45:55.452782142 +0200 > +++ gcc/testsuite/g++.dg/cpp2a/attr-likely7.C 2022-10-06 17:45:55.452782142 +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 <class T> 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<int>(); > + > + 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 > ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH] c++, v3: Improve handling of foreigner namespace attributes 2022-10-06 17:30 ` Jason Merrill @ 2022-10-06 18:29 ` Jakub Jelinek 2022-10-06 18:34 ` Jason Merrill 0 siblings, 1 reply; 6+ messages in thread From: Jakub Jelinek @ 2022-10-06 18:29 UTC (permalink / raw) To: Jason Merrill; +Cc: gcc-patches On Thu, Oct 06, 2022 at 01:30:18PM -0400, Jason Merrill wrote: > Yes, except I was thinking the new function would take the attribute > TREE_LIST as its parameter so that all the callers don't have to also call > get_attribute_namespace. Ok, here it is in patch form. 2022-10-06 Jakub Jelinek <jakub@redhat.com> gcc/ * attribs.h (is_attribute_namespace_p): New inline function. (lookup_attribute): Document meaning of ATTR_NS equal to "". * attribs.cc (remove_attribute): Use is_attribute_namespace_p. (private_lookup_attribute): For ATTR_NS "" match either standard attribute or "gnu" namespace one. 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/attribs.h.jj 2022-10-05 16:15:29.834889059 +0200 +++ gcc/attribs.h 2022-10-06 20:22:37.946494304 +0200 @@ -188,6 +188,22 @@ is_attribute_p (const char *attr_name, c IDENTIFIER_POINTER (ident), IDENTIFIER_LENGTH (ident)); } +/* Given an attribute name ATTR and a string ATTR_NS, return true + if the identifier node is a valid attribute namespace for the string. + ATTR_NS "" stands for standard attribute (NULL IDENT) or "gnu" + namespace. */ + +static inline bool +is_attribute_namespace_p (const char *attr_ns, const_tree attr) +{ + tree ident = get_attribute_namespace (attr); + if (attr_ns == NULL) + return ident == NULL_TREE; + if (attr_ns[0]) + return ident && is_attribute_p (attr_ns, ident); + return ident == NULL_TREE || is_attribute_p ("gnu", ident); +} + /* Given an attribute name ATTR_NAME and a list of attributes LIST, return a pointer to the attribute's list element if the attribute is part of the list, or NULL_TREE if not found. If the attribute @@ -217,7 +233,8 @@ lookup_attribute (const char *attr_name, } } -/* Similar to lookup_attribute, but also match the attribute namespace. */ +/* Similar to lookup_attribute, but also match the attribute namespace. + ATTR_NS "" stands for either standard attribute or "gnu" namespace. */ static inline tree lookup_attribute (const char *attr_ns, const char *attr_name, tree list) --- gcc/attribs.cc.jj 2022-10-04 23:13:10.289090796 +0200 +++ gcc/attribs.cc 2022-10-06 20:23:35.693713861 +0200 @@ -1645,7 +1645,8 @@ remove_attribute (const char *attr_name, return list; } -/* Similarly but also match namespace on the removed attributes. */ +/* Similarly but also match namespace on the removed attributes. + ATTR_NS "" stands for NULL or "gnu" namespace. */ tree remove_attribute (const char *attr_ns, const char *attr_name, tree list) @@ -1659,15 +1660,11 @@ remove_attribute (const char *attr_ns, c tree l = *p; tree attr = get_attribute_name (l); - if (is_attribute_p (attr_name, attr)) + if (is_attribute_p (attr_name, attr) + && is_attribute_namespace_p (attr_ns, l)) { - tree ns = get_attribute_namespace (l); - if ((ns == NULL_TREE && attr_ns == NULL) - || (ns && attr_ns && is_attribute_p (attr_ns, ns))) - { - *p = TREE_CHAIN (l); - continue; - } + *p = TREE_CHAIN (l); + continue; } p = &TREE_CHAIN (l); } @@ -2088,14 +2085,20 @@ private_lookup_attribute (const char *at tree ns = get_attribute_namespace (list); if (ns == NULL_TREE) { - if (attr_ns == NULL) + if (attr_ns_len == 0) break; } else if (attr_ns) { ident_len = IDENTIFIER_LENGTH (ns); - if (cmp_attribs (attr_ns, attr_ns_len, IDENTIFIER_POINTER (ns), - ident_len)) + if (attr_ns_len == 0) + { + if (cmp_attribs ("gnu", strlen ("gnu"), + IDENTIFIER_POINTER (ns), ident_len)) + break; + } + else if (cmp_attribs (attr_ns, attr_ns_len, + IDENTIFIER_POINTER (ns), ident_len)) break; } } --- gcc/c-family/c-common.cc.jj 2022-10-06 17:43:47.880502941 +0200 +++ gcc/c-family/c-common.cc 2022-10-06 20:24:17.220152623 +0200 @@ -6008,12 +6008,12 @@ attribute_fallthrough_p (tree attr) { if (attr == error_mark_node) return false; - tree t = lookup_attribute ("fallthrough", attr); + tree t = lookup_attribute ("", "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 ("", "fallthrough", TREE_CHAIN (t))) warning (OPT_Wattributes, "attribute %<fallthrough%> specified multiple " "times"); /* No attribute-argument-clause shall be present. */ @@ -6024,7 +6024,8 @@ 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)) + if (!is_attribute_p ("fallthrough", name) + || !is_attribute_namespace_p ("", t)) { if (!c_dialect_cxx () && get_attribute_namespace (t) == NULL_TREE) /* The specifications of standard attributes in C mean --- gcc/cp/parser.cc.jj 2022-10-06 17:43:42.418576616 +0200 +++ gcc/cp/parser.cc 2022-10-06 20:25:01.004560878 +0200 @@ -29265,7 +29265,8 @@ 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)) + && is_attribute_namespace_p ("", attribute) + && lookup_attribute ("", a, attributes)) { if (!from_macro_expansion_at (loc)) warning_at (loc, OPT_Wattributes, "attribute %qs specified " --- gcc/cp/cp-gimplify.cc.jj 2022-10-06 17:43:42.413576684 +0200 +++ gcc/cp/cp-gimplify.cc 2022-10-06 20:24:43.145802239 +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) @@ -3035,24 +3035,36 @@ lookup_hotness_attribute (tree list) for (; list; list = TREE_CHAIN (list)) { tree name = get_attribute_name (list); - if (is_attribute_p ("hot", name) - || is_attribute_p ("cold", name) - || is_attribute_p ("likely", name) - || is_attribute_p ("unlikely", name)) + if ((is_attribute_p ("hot", name) + || is_attribute_p ("cold", name) + || is_attribute_p ("likely", name) + || is_attribute_p ("unlikely", name)) + && is_attribute_namespace_p ("", list)) 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)) + && is_attribute_namespace_p ("", l)) + { + *p = TREE_CHAIN (l); + continue; + } + p = &TREE_CHAIN (l); + } return list; } --- gcc/testsuite/g++.dg/cpp1z/fallthrough2.C.jj 2022-10-06 17:45:55.452782142 +0200 +++ gcc/testsuite/g++.dg/cpp1z/fallthrough2.C 2022-10-06 17:45:55.452782142 +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-06 17:45:55.452782142 +0200 +++ gcc/testsuite/g++.dg/cpp2a/attr-likely7.C 2022-10-06 17:45:55.452782142 +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 <class T> 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<int>(); + + 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 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] c++, v3: Improve handling of foreigner namespace attributes 2022-10-06 18:29 ` [PATCH] c++, v3: " Jakub Jelinek @ 2022-10-06 18:34 ` Jason Merrill 0 siblings, 0 replies; 6+ messages in thread From: Jason Merrill @ 2022-10-06 18:34 UTC (permalink / raw) To: Jakub Jelinek; +Cc: gcc-patches On 10/6/22 14:29, Jakub Jelinek wrote: > On Thu, Oct 06, 2022 at 01:30:18PM -0400, Jason Merrill wrote: >> Yes, except I was thinking the new function would take the attribute >> TREE_LIST as its parameter so that all the callers don't have to also call >> get_attribute_namespace. > > Ok, here it is in patch form. > > 2022-10-06 Jakub Jelinek <jakub@redhat.com> > > gcc/ > * attribs.h (is_attribute_namespace_p): New inline function. > (lookup_attribute): Document meaning of ATTR_NS equal to "". > * attribs.cc (remove_attribute): Use is_attribute_namespace_p. > (private_lookup_attribute): For ATTR_NS "" match either standard > attribute or "gnu" namespace one. > 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/attribs.h.jj 2022-10-05 16:15:29.834889059 +0200 > +++ gcc/attribs.h 2022-10-06 20:22:37.946494304 +0200 > @@ -188,6 +188,22 @@ is_attribute_p (const char *attr_name, c > IDENTIFIER_POINTER (ident), IDENTIFIER_LENGTH (ident)); > } > > +/* Given an attribute name ATTR and a string ATTR_NS, return true The word "name" here is wrong now. OK with that fixed, thanks. > + if the identifier node is a valid attribute namespace for the string. > + ATTR_NS "" stands for standard attribute (NULL IDENT) or "gnu" > + namespace. */ > + > +static inline bool > +is_attribute_namespace_p (const char *attr_ns, const_tree attr) > +{ > + tree ident = get_attribute_namespace (attr); > + if (attr_ns == NULL) > + return ident == NULL_TREE; > + if (attr_ns[0]) > + return ident && is_attribute_p (attr_ns, ident); > + return ident == NULL_TREE || is_attribute_p ("gnu", ident); > +} > + > /* Given an attribute name ATTR_NAME and a list of attributes LIST, > return a pointer to the attribute's list element if the attribute > is part of the list, or NULL_TREE if not found. If the attribute > @@ -217,7 +233,8 @@ lookup_attribute (const char *attr_name, > } > } > > -/* Similar to lookup_attribute, but also match the attribute namespace. */ > +/* Similar to lookup_attribute, but also match the attribute namespace. > + ATTR_NS "" stands for either standard attribute or "gnu" namespace. */ > > static inline tree > lookup_attribute (const char *attr_ns, const char *attr_name, tree list) > --- gcc/attribs.cc.jj 2022-10-04 23:13:10.289090796 +0200 > +++ gcc/attribs.cc 2022-10-06 20:23:35.693713861 +0200 > @@ -1645,7 +1645,8 @@ remove_attribute (const char *attr_name, > return list; > } > > -/* Similarly but also match namespace on the removed attributes. */ > +/* Similarly but also match namespace on the removed attributes. > + ATTR_NS "" stands for NULL or "gnu" namespace. */ > > tree > remove_attribute (const char *attr_ns, const char *attr_name, tree list) > @@ -1659,15 +1660,11 @@ remove_attribute (const char *attr_ns, c > tree l = *p; > > tree attr = get_attribute_name (l); > - if (is_attribute_p (attr_name, attr)) > + if (is_attribute_p (attr_name, attr) > + && is_attribute_namespace_p (attr_ns, l)) > { > - tree ns = get_attribute_namespace (l); > - if ((ns == NULL_TREE && attr_ns == NULL) > - || (ns && attr_ns && is_attribute_p (attr_ns, ns))) > - { > - *p = TREE_CHAIN (l); > - continue; > - } > + *p = TREE_CHAIN (l); > + continue; > } > p = &TREE_CHAIN (l); > } > @@ -2088,14 +2085,20 @@ private_lookup_attribute (const char *at > tree ns = get_attribute_namespace (list); > if (ns == NULL_TREE) > { > - if (attr_ns == NULL) > + if (attr_ns_len == 0) > break; > } > else if (attr_ns) > { > ident_len = IDENTIFIER_LENGTH (ns); > - if (cmp_attribs (attr_ns, attr_ns_len, IDENTIFIER_POINTER (ns), > - ident_len)) > + if (attr_ns_len == 0) > + { > + if (cmp_attribs ("gnu", strlen ("gnu"), > + IDENTIFIER_POINTER (ns), ident_len)) > + break; > + } > + else if (cmp_attribs (attr_ns, attr_ns_len, > + IDENTIFIER_POINTER (ns), ident_len)) > break; > } > } > --- gcc/c-family/c-common.cc.jj 2022-10-06 17:43:47.880502941 +0200 > +++ gcc/c-family/c-common.cc 2022-10-06 20:24:17.220152623 +0200 > @@ -6008,12 +6008,12 @@ attribute_fallthrough_p (tree attr) > { > if (attr == error_mark_node) > return false; > - tree t = lookup_attribute ("fallthrough", attr); > + tree t = lookup_attribute ("", "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 ("", "fallthrough", TREE_CHAIN (t))) > warning (OPT_Wattributes, "attribute %<fallthrough%> specified multiple " > "times"); > /* No attribute-argument-clause shall be present. */ > @@ -6024,7 +6024,8 @@ 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)) > + if (!is_attribute_p ("fallthrough", name) > + || !is_attribute_namespace_p ("", t)) > { > if (!c_dialect_cxx () && get_attribute_namespace (t) == NULL_TREE) > /* The specifications of standard attributes in C mean > --- gcc/cp/parser.cc.jj 2022-10-06 17:43:42.418576616 +0200 > +++ gcc/cp/parser.cc 2022-10-06 20:25:01.004560878 +0200 > @@ -29265,7 +29265,8 @@ 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)) > + && is_attribute_namespace_p ("", attribute) > + && lookup_attribute ("", a, attributes)) > { > if (!from_macro_expansion_at (loc)) > warning_at (loc, OPT_Wattributes, "attribute %qs specified " > --- gcc/cp/cp-gimplify.cc.jj 2022-10-06 17:43:42.413576684 +0200 > +++ gcc/cp/cp-gimplify.cc 2022-10-06 20:24:43.145802239 +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) > @@ -3035,24 +3035,36 @@ lookup_hotness_attribute (tree list) > for (; list; list = TREE_CHAIN (list)) > { > tree name = get_attribute_name (list); > - if (is_attribute_p ("hot", name) > - || is_attribute_p ("cold", name) > - || is_attribute_p ("likely", name) > - || is_attribute_p ("unlikely", name)) > + if ((is_attribute_p ("hot", name) > + || is_attribute_p ("cold", name) > + || is_attribute_p ("likely", name) > + || is_attribute_p ("unlikely", name)) > + && is_attribute_namespace_p ("", list)) > 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)) > + && is_attribute_namespace_p ("", l)) > + { > + *p = TREE_CHAIN (l); > + continue; > + } > + p = &TREE_CHAIN (l); > + } > return list; > } > > --- gcc/testsuite/g++.dg/cpp1z/fallthrough2.C.jj 2022-10-06 17:45:55.452782142 +0200 > +++ gcc/testsuite/g++.dg/cpp1z/fallthrough2.C 2022-10-06 17:45:55.452782142 +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-06 17:45:55.452782142 +0200 > +++ gcc/testsuite/g++.dg/cpp2a/attr-likely7.C 2022-10-06 17:45:55.452782142 +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 <class T> 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<int>(); > + > + 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 > ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2022-10-06 18:34 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2022-10-05 12:04 [PATCH] c++: Improve handling of foreigner namespace attributes Jakub Jelinek 2022-10-06 13:42 ` Jason Merrill 2022-10-06 17:20 ` [PATCH] c++, v2: " Jakub Jelinek 2022-10-06 17:30 ` Jason Merrill 2022-10-06 18:29 ` [PATCH] c++, v3: " Jakub Jelinek 2022-10-06 18:34 ` Jason Merrill
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).