From: Marek Polacek Date: Thu, 23 Apr 2026 16:43:06 +0000 (-0400) Subject: c++: introduce lookup_annotation X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a12c836db363ad2d1344776eccdf8baebc1b6d67;p=thirdparty%2Fgcc.git c++: introduce lookup_annotation This patch introduces a new helper for looking up annotations. gcc/cp/ChangeLog: * cp-tree.h (lookup_annotation): Declare. * decl.cc (grokfndecl): Use lookup_annotation. (grokdeclarator): Likewise. * name-lookup.cc (push_local_extern_decl_alias): Likewise. * parser.cc (cp_parser_decomposition_declaration): Likewise. * reflect.cc (eval_annotations_of): Likewise. * tree.cc (lookup_annotation): New. Reviewed-by: Jason Merrill --- diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 3d1f6f99aad..70132b586d8 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -8808,6 +8808,7 @@ extern tree cxx_copy_lang_qualifiers (const_tree, const_tree); extern void cxx_print_statistics (void); extern bool maybe_warn_zero_as_null_pointer_constant (tree, location_t); extern bool annotation_p (tree) ATTRIBUTE_PURE; +extern tree lookup_annotation (tree); /* in ptree.cc */ extern void cxx_print_xnode (FILE *, tree, int); diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index c3d589e8c23..6415f0b4bc2 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -12232,7 +12232,7 @@ grokfndecl (tree ctype, && initialized == SD_INITIALIZED && DECL_ATTRIBUTES (t)) for (tree a = DECL_ATTRIBUTES (t); - (a = lookup_attribute ("internal ", "annotation ", a)); + (a = lookup_annotation (a)); a = TREE_CHAIN (a)) { gcc_checking_assert (TREE_CODE (TREE_VALUE (a)) == TREE_LIST); @@ -15938,10 +15938,9 @@ grokdeclarator (const cp_declarator *declarator, { if (flag_reflection && declarator->std_attributes != error_mark_node - && lookup_attribute ("internal ", "annotation ", - declarator->std_attributes) + && lookup_annotation (declarator->std_attributes) && *attrlist != error_mark_node - && lookup_attribute ("internal ", "annotation ", *attrlist)) + && lookup_annotation (*attrlist)) /* If there are annotations in both lists, ensure declarator->std_attributes go after *attrlist. See PR124399. */ @@ -16663,9 +16662,7 @@ grokdeclarator (const cp_declarator *declarator, { if (flag_reflection && !funcdef_flag && decl) { - if (attrlist - && lookup_attribute ("internal ", "annotation ", - *attrlist)) + if (attrlist && lookup_annotation (*attrlist)) { /* Remove the annotations to avoid spurious warning below. */ @@ -16676,8 +16673,7 @@ grokdeclarator (const cp_declarator *declarator, } for (tree arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg)) - if (lookup_attribute ("internal ", "annotation ", - DECL_ATTRIBUTES (arg))) + if (lookup_annotation (DECL_ATTRIBUTES (arg))) error_at (DECL_SOURCE_LOCATION (arg), "annotation applied to parameter %qD of " "non-defining friend declaration", arg); diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc index 568e615918c..ef844088381 100644 --- a/gcc/cp/name-lookup.cc +++ b/gcc/cp/name-lookup.cc @@ -3692,15 +3692,13 @@ push_local_extern_decl_alias (tree decl) { if (flag_reflection) { - if (lookup_attribute ("internal ", "annotation ", - DECL_ATTRIBUTES (decl))) + if (lookup_annotation (DECL_ATTRIBUTES (decl))) error_at (DECL_SOURCE_LOCATION (decl), "annotation applied to block scope extern %qD", decl); if (TREE_CODE (decl) == FUNCTION_DECL) for (tree arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg)) - if (lookup_attribute ("internal ", "annotation ", - DECL_ATTRIBUTES (arg))) + if (lookup_annotation (DECL_ATTRIBUTES (arg))) error_at (DECL_SOURCE_LOCATION (arg), "annotation applied to parameter %qD of block scope " "extern", arg); diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index d07dc01650f..dc67cfd9f7c 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -18327,7 +18327,7 @@ cp_parser_decomposition_declaration (cp_parser *parser, attr = NULL_TREE; if (attr && first_attr == -1) first_attr = v.length (); - if (lookup_attribute ("internal ", "annotation ", attr)) + if (lookup_annotation (attr)) error ("annotation on structured binding"); } v.safe_push (e); diff --git a/gcc/cp/reflect.cc b/gcc/cp/reflect.cc index 09e99b5ef83..27e1efee106 100644 --- a/gcc/cp/reflect.cc +++ b/gcc/cp/reflect.cc @@ -3966,7 +3966,8 @@ eval_annotations_of (location_t loc, const constexpr_ctx *ctx, tree r, else gcc_unreachable (); vec *elts = nullptr; - for (tree a = r; (a = lookup_attribute ("internal ", "annotation ", a)); + for (tree a = r; + (a = lookup_annotation (a)); a = TREE_CHAIN (a)) { gcc_checking_assert (TREE_CODE (TREE_VALUE (a)) == TREE_LIST); diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc index d3e202e8b04..c510a5bf2d0 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -7086,6 +7086,14 @@ annotation_p (tree attr) return is_attribute_p ("annotation ", get_attribute_name (attr)); } +/* Lookup the annotation in ATTR, if present. */ + +tree +lookup_annotation (tree attr) +{ + return lookup_attribute ("internal ", "annotation ", attr); +} + /* Release memory we no longer need after parsing. */ void