]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: introduce lookup_annotation
authorMarek Polacek <polacek@redhat.com>
Thu, 23 Apr 2026 16:43:06 +0000 (12:43 -0400)
committerMarek Polacek <polacek@redhat.com>
Thu, 23 Apr 2026 18:24:18 +0000 (14:24 -0400)
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 <jason@redhat.com>
gcc/cp/cp-tree.h
gcc/cp/decl.cc
gcc/cp/name-lookup.cc
gcc/cp/parser.cc
gcc/cp/reflect.cc
gcc/cp/tree.cc

index 3d1f6f99aad9363687d694830968070013954c6b..70132b586d8bfa264e6757049879fc0736d3c944 100644 (file)
@@ -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);
index c3d589e8c23aebddf292cd72aa9292b95ca2f551..6415f0b4bc211402cc278406475961075b6f4f7d 100644 (file)
@@ -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);
index 568e615918c5ae4aa4e033fa2a7da29cf758fedd..ef844088381967f671f874eb114a4d851986a15f 100644 (file)
@@ -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);
index d07dc01650f610144370cac998add7394cd55288..dc67cfd9f7cedf596573256f34c7e80b476c8a26 100644 (file)
@@ -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);
index 09e99b5ef83b822fe7180cfcd8ff52444d07896b..27e1efee106b1d325d4ca0f19f386db858b25e69 100644 (file)
@@ -3966,7 +3966,8 @@ eval_annotations_of (location_t loc, const constexpr_ctx *ctx, tree r,
   else
     gcc_unreachable ();
   vec<constructor_elt, va_gc> *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);
index d3e202e8b04833ad8e1917ac711e6d965f6f6e20..c510a5bf2d0b03af3c3670effb5aef583f0dd38e 100644 (file)
@@ -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);
+}
+
 \f
 /* Release memory we no longer need after parsing.  */
 void