attribute nonnull/expect_nonnull for return value?

Daniel Frey d.frey@gmx.de
Sat Mar 24 08:40:00 GMT 2012


Given a function declaration

  void* f( void* a, void* b ) __attribute__(( nonnull( 1, 2 ) ));

GCC now knows that the arguments are nonnull, but how to I tell the compiler that the returned pointer is nonnull? Or if null is allowed but only the exception to the rule, I would like to tell the compiler that is should *expect* the pointer to be nonnull, but not disallow null. Actually, the second case is more important to me, but I think both should be possible.

Rationale:

Writing a library, the function's definition goes into a separate compile unit and I would like to allow the user's code to be optimized automatically. Currently, in cases where the optimization matters, the user is required to do the work manually:

  T* p = (T*)f( a, b );
  if( __builtin_expect( !p, false ) ) {
    // ...don't optimize for this case...
  }
  return p;

This is not a good solution, the library/API should take care of it. In fact, I think that this applies to an awful lot of functions (think system calls) and could really help to optimize user programs. If this is currently not possible, would it be possible that it will be added in future versions of GCC? Seeing __attribute__(( malloc )) exists, the infrastructure seems to be there, only that malloc does/guarantees way too much...

Regards, Daniel

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 203 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <https://gcc.gnu.org/pipermail/gcc-help/attachments/20120324/a7b6a9c7/attachment.sig>


More information about the Gcc-help mailing list