[gcc r17-3014] Use chain_index in callback_only attribute handler.

Josef Melcr via Gcc-cvs <[email protected]> Thu, 6 Aug 2026 09:13:33 +0000 (GMT)
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:fde81f062246fa91207c83ca4e09a07281547dd8

commit r17-3014-gfde81f062246fa91207c83ca4e09a07281547dd8
Author: Josef Melcr <[email protected]>
Date:   Mon Jul 27 11:58:04 2026 +0200

    Use chain_index in callback_only attribute handler.
    
    I wasn't aware of the chain_index function when I first wrote the
    attribute handler for callback_only, so I implemented the functionality
    in get_nth_list_elem.  This patch removes said function and replaces it
    with calls to chain_index to reduce code duplication.
    
    gcc/c-family/ChangeLog:
    
            * c-attribs.cc (get_nth_list_elem): Removed.
            (handle_callback_only_attribute): Use chain_index instead of
            get_nth_list_elem.
    
    Signed-off-by: Josef Melcr <[email protected]>

Diff:
---
 gcc/c-family/c-attribs.cc | 26 +++++---------------------
 1 file changed, 5 insertions(+), 21 deletions(-)

diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc
index 72702b35aedb..1ed02850a911 100644
--- a/gcc/c-family/c-attribs.cc
+++ b/gcc/c-family/c-attribs.cc
@@ -4673,25 +4673,6 @@ handle_tm_wrap_attribute (tree *node, tree name, tree args,
   return NULL_TREE;
 }
 
-/* Returns the element at index idx in the list or NULL_TREE if
-   the list isn't long enough.  NULL_TREE is used as the endpoint.  */
-static tree
-get_nth_list_elem (tree list, unsigned idx)
-{
-  tree res = NULL_TREE;
-  unsigned i = 0;
-  tree it;
-  for (it = list; it != NULL_TREE; it = TREE_CHAIN (it), i++)
-    {
-      if (i == idx)
-	{
-	  res = TREE_VALUE (it);
-	  break;
-	}
-    }
-  return res;
-}
-
 /* Handle a "callback_only" attribute; arguments as in
    struct attribute_spec.handler.  */
 tree
@@ -4745,7 +4726,7 @@ handle_callback_only_attribute (tree *node, tree name, tree args,
 
   /* Search for the type of the callback function
      in parameters of the original function.  */
-  tree cfn = get_nth_list_elem (decl_type_args, callback_fn_idx);
+  tree cfn = chain_index (callback_fn_idx, decl_type_args);
   if (cfn == NULL_TREE)
     {
       error_at (DECL_SOURCE_LOCATION (decl),
@@ -4753,6 +4734,7 @@ handle_callback_only_attribute (tree *node, tree name, tree args,
       *no_add_attrs = true;
       return NULL_TREE;
     }
+  cfn = TREE_VALUE (cfn);
   tree cfn_pointee_type = TREE_TYPE (cfn);
   if (TREE_CODE (cfn) != POINTER_TYPE
       || TREE_CODE (cfn_pointee_type) != FUNCTION_TYPE)
@@ -4819,7 +4801,9 @@ handle_callback_only_attribute (tree *node, tree name, tree args,
 	  continue;
 	}
 
-      tree arg_type = get_nth_list_elem (decl_type_args, arg_idx);
+      tree arg_type = chain_index (arg_idx, decl_type_args);
+      gcc_checking_assert (arg_type != NULL_TREE);
+      arg_type = TREE_VALUE (arg_type);
       tree expected_type = TREE_VALUE (it);
       /* Check the type of the value we are about to pass ("arg_type")
 	 for compatibility with the actual type the callback function