Hi,
rejection of unprototyped functions from the callback_only attribute has
been mentioned on this 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.

Bootstrapped and regtested on x86_64-linux with no new regressions.  OK
for master?

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: 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]>
---
 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(-)
 create mode 100644 gcc/testsuite/gcc.dg/pr127464.c

diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc
index 3c0e780669b..54403011063 100644
--- a/gcc/c-family/c-attribs.cc
+++ b/gcc/c-family/c-attribs.cc
@@ -4688,7 +4688,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);
@@ -4743,7 +4750,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 88213d25a89..97e39ecd250 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 00000000000..25746541c23
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr127464.c
@@ -0,0 +1,9 @@
+/* { dg-do compile } */
+/* { dg-options "-std=c11 -Wattributes -Wno-implicit-int 
-Wno-builtin-declaration-mismatch" } */
+
+[[gnu::callback_only(1, 2, 3), gnu::callback_only(4, 5)]]
+__builtin_abort (); /* { dg-warning "requires a prototype" }*/
+
+[[gnu::callback_only(1, 2, 3)]]
+void
+foo (void (*) (), int *, double); /* { dg-warning "must have a prototype" } */
-- 
2.55.0

Reply via email to