This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: attribute nonnull/expect_nonnull for return value?


On Sun, 25 Mar 2012, Ian Lance Taylor wrote:

Daniel Frey <d.frey@gmx.de> writes:

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.

At present, as far as I know, there is no way to do either.

To completely disallow null return values, you could write an inline wrapper that forwards its arguments to the real function and dereferences the return pointer before returning it. IIRC, gcc gets the message that a pointer that is dereferenced earlier can be assumed to be nonnull.


For a simple expectation, I can't think of a good way, although I believe that in the absence of contrary information, the compiler considers nonnull pointers more likely than null, for branch prediction and stuff (but maybe not as strongly as if you had a __builtin_expect).

--
Marc Glisse


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]