[gcc r17-3897] Return early if callback_only is not used on a function

Josef Melcr jmelcr@gcc.gnu.org
Thu Sep 3 13:21:21 GMT 2026


https://gcc.gnu.org/g:c7e8bbbef58ea6eecb7aa6a3084162bebec4104d

commit r17-3897-gc7e8bbbef58ea6eecb7aa6a3084162bebec4104d
Author: Josef Melcr <josef.melcr@suse.com>
Date:   Mon Aug 10 15:17:41 2026 +0200

    Return early if callback_only is not used on a function
    
    There was a missing return statement in the attribute handler when
    checking whether or not the attribute was used on a function.  Since
    positional_argument was introduced into the handler, this uncaught error
    would result in an ICE.  This patch fixes that.
    
    gcc/c-family/ChangeLog:
    
            * c-attribs.cc (handle_callback_only_attribute): Add early
            return if the attribute is not used on a function.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/attr-callback.c: Add testcases for the early return.
    
    Signed-off-by: Josef Melcr <josef.melcr@suse.com>

Diff:
---
 gcc/c-family/c-attribs.cc            |  1 +
 gcc/testsuite/gcc.dg/attr-callback.c | 13 +++++++++++++
 2 files changed, 14 insertions(+)

diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc
index 9e1fdc8087d9..ae5c2743ce6b 100644
--- a/gcc/c-family/c-attribs.cc
+++ b/gcc/c-family/c-attribs.cc
@@ -4690,6 +4690,7 @@ handle_callback_only_attribute (tree *node, tree name, tree args,
       warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
 		  "%qE attribute can only be used on functions", name);
       *no_add_attrs = true;
+      return NULL_TREE;
     }
 
   tree decl_type = TREE_TYPE (decl);
diff --git a/gcc/testsuite/gcc.dg/attr-callback.c b/gcc/testsuite/gcc.dg/attr-callback.c
index 3837af1b1188..7e95a00816ea 100755
--- a/gcc/testsuite/gcc.dg/attr-callback.c
+++ b/gcc/testsuite/gcc.dg/attr-callback.c
@@ -75,6 +75,19 @@ vararg_1(void (*)(int*), int*, ...); /* { dg-warning "cannot be used on variadic
 void
 vararg_2(void (*)(int*, ...), int*); /* { dg-warning "callback function cannot be variadic" } */
 
+void
+not_used_on_fn_1 ()
+{
+  __attribute__ ((callback_only (1))) int a = 1; /* { dg-warning "attribute can only be used on functions" } */
+}
+
+/* This warning is not issued by the attribute handler, rather by
+   decl_attributes in attribs.cc.  Test it anyway.  */
+struct __attribute__ ((callback_only (1))) not_used_on_fn_2
+{
+  int x;
+}; /* { dg-warning "attribute does not apply to types" } */
+
 struct S
 {
   int x;


More information about the Gcc-cvs mailing list