diff mbox

correct documentation of attribute ifunc (PR 81882)

Message ID af512c2d-c4b9-f3fa-c326-1f36a5e83fdc@gmail.com
State New
Headers show

Commit Message

Martin Sebor Aug. 17, 2017, 8:30 p.m. UTC
On 08/17/2017 01:26 PM, Alexander Monakov wrote:
> On Thu, 17 Aug 2017, Martin Sebor wrote:
>
>>  returns a pointer to the selected implementation function.  The
>>  implementation functions' declarations must match the API of the
>> -function being implemented, the resolver's declaration is be a
>> -function returning pointer to void function returning void:
>> +function being implemented.  The resolver should be declared to
>> +be a function returning a pointer to a function taking no arguments
>> +and returning a pointer to a function of the same type as the
>> +implementation.  For example:
>
> The new wording is wrong (extra level of pointer-to-function).

You're right, that's a silly typo.  Thanks for catching it!
Here's what I meant to write:

   The resolver should be declared to be a function taking no
   arguments and returning a pointer to a function of the same
   type as the implementation.

Attached is an updated patch with the corrected wording.

> I suggest removing this part altogether, it's not useful at all,
> anyone writing the resolver can deduce what the return type should be
> based on the fact that a pointer to the implementation is returned.

I suppose that would work too.  If there's preference for
this approach I'm happy to remove the last sentence.

> (fwiw a simple 'void *' as return type would work in practice too)

Well, yes, it would work, but only about as well as the current
wording and example do.  I.e., it triggers (pedantic) warnings
in C and errors in C++.  To get around the errors, the usual
approach (despite what the manual says about returning a pointer
to a function) is to return void* and cast the address of the
function to it.  It would be helpful if users could avoid the
cast while at the same time having incompatibilities detected
for them.

(FWIW, I noticed this problem while testing an improvement
to have GCC do just that: detect invalid conversions in these
kinds of attributes (alias and ifunc) under bug 81854.)

Martin

Comments

Andreas Schwab Aug. 18, 2017, 6:39 a.m. UTC | #1
On Aug 17 2017, Martin Sebor <msebor@gmail.com> wrote:

> -static void (*resolve_memcpy (void)) (void)
> +static void* (*resolve_memcpy (void))(void *, const void *, size_t)

Please use consistent spacing.

Andreas.
Joseph Myers Sept. 6, 2017, 11:05 p.m. UTC | #2
This patch is OK with the spacing in the function prototype fixed as noted 
to follow normal GNU standards.
diff mbox

Patch

PR c/81882 - attribute ifunc documentation uses invalid code

gcc/ChangeLog:

	PR c/81882
	* doc/extend.texi (attribute ifunc): Avoid relying on ill-formed
	code (in C++) or code that triggers warnings.

Index: gcc/doc/extend.texi
===================================================================
--- gcc/doc/extend.texi	(revision 251156)
+++ gcc/doc/extend.texi	(working copy)
@@ -2783,21 +2783,23 @@  The @code{ifunc} attribute is used to mark a funct
 function using the STT_GNU_IFUNC symbol type extension to the ELF
 standard.  This allows the resolution of the symbol value to be
 determined dynamically at load time, and an optimized version of the
-routine can be selected for the particular processor or other system
+routine to be selected for the particular processor or other system
 characteristics determined then.  To use this attribute, first define
 the implementation functions available, and a resolver function that
 returns a pointer to the selected implementation function.  The
 implementation functions' declarations must match the API of the
-function being implemented, the resolver's declaration is be a
-function returning pointer to void function returning void:
+function being implemented.  The resolver should be declared to
+be a function taking no arguments and returning a pointer to
+a function of the same type as the implementation.  For example:
 
 @smallexample
 void *my_memcpy (void *dst, const void *src, size_t len)
 @{
   @dots{}
+  return dst;
 @}
 
-static void (*resolve_memcpy (void)) (void)
+static void* (*resolve_memcpy (void))(void *, const void *, size_t)
 @{
   return my_memcpy; // we'll just always select this routine
 @}