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

commit r17-5086-gb5fa057f9ee6beef95d09b646f47eb15f5a89be7
Author: Josef Melcr <[email protected]>
Date:   Thu Sep 24 11:07:46 2026 +0200

    Reject unprototyped functions in callback_only attribute handler [PR127464]
    
    Rejection of unprototyped functions from the callback_only attribute has
    been mentioned on the mailing list.  It has now also come up in the
    form of PR127464.  This patch implements the check for unprototyped
    functions and adds a mention about them in the attribute's
    documentation.
    
    Best regards,
    Josef
    
            PR c/127464
    
    gcc/c-family/ChangeLog:
    
            * c-attribs.cc (handle_callback_only_attribute): Reject
            unprototyped functions.
    
    gcc/ChangeLog:
    
            * doc/extend.texi (callback_only): Add a sentence about unprototyped
            functions to callback_only docs.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/pr127464.c: New test.
    
    Signed-off-by: Josef Melcr <[email protected]>

Diff:
---
 gcc/c-family/c-attribs.cc       | 18 ++++++++++++++++--
 gcc/doc/extend.texi             |  2 +-
 gcc/testsuite/gcc.dg/pr127464.c |  9 +++++++++
 3 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc
index 470054f044ac..b41b9d1d72a1 100644
--- a/gcc/c-family/c-attribs.cc
+++ b/gcc/c-family/c-attribs.cc
@@ -3951,7 +3951,14 @@ handle_callback_only_attribute (tree *node, tree name, 
tree args,
     }
 
   tree decl_type = TREE_TYPE (decl);
-  if (stdarg_p (decl_type))
+  if (!prototype_p (decl_type))
+    {
+      warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
+                 "%qE attribute requires a prototype", name);
+      *no_add_attrs = true;
+      return NULL_TREE;
+    }
+  else if (stdarg_p (decl_type))
     {
       warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
                  "%qE attribute cannot be used on variadic functions", name);
@@ -4002,7 +4009,14 @@ handle_callback_only_attribute (tree *node, tree name, 
tree args,
 
   tree type_args = TYPE_ARG_TYPES (cfn_pointee_type);
 
-  if (stdarg_p (cfn_pointee_type))
+  if (!prototype_p (cfn_pointee_type))
+    {
+      warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
+                 "%qE callback function must have a prototype", name);
+      *no_add_attrs = true;
+      return NULL_TREE;
+    }
+  else if (stdarg_p (cfn_pointee_type))
     {
       warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
                  "%qE callback function cannot be variadic", name);
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index 9b6161334680..a35b95cc74f1 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -2375,7 +2375,7 @@ may not change or dereference them.  The arguments also 
may not escape.
 The attribute may be used multiple times per function, though only one
 @code{callback_only} attribute may be used per function parameter.
 Neither the dispatching function nor the callback function may be
-variadic.
+variadic.  Both functions must have a prototype.
 
 The attribute exposes the potentially hidden callsite in the annotated
 function, enabling interprocedural optimizations which may not be
diff --git a/gcc/testsuite/gcc.dg/pr127464.c b/gcc/testsuite/gcc.dg/pr127464.c
new file mode 100644
index 000000000000..1c33769ca6f1
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr127464.c
@@ -0,0 +1,9 @@
+/* PR c/127464 */
+/* { dg-do compile } */
+/* { dg-options "-std=c11 -Wattributes" } */
+
+[[gnu::callback_only (1, 2, 3), gnu::callback_only (4, 5)]]
+void bar (); /* { dg-warning "requires a prototype" }*/
+
+[[gnu::callback_only (1, 2, 3)]]
+void foo (void (*) (), int *, double); /* { dg-warning "must have a prototype" 
} */

Reply via email to