This is the mail archive of the gcc-patches@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]

Attribute returns_null (was: New attribute: returns_nonnull)


On Mon, 7 Oct 2013, David Malcolm wrote:

On Mon, 2013-10-07 at 16:17 +0200, Marc Glisse wrote:
Hello,

this patch adds an attribute to let the compiler know that a function
never returns NULL. I saw some ECF_* flags, but the attribute seems
sufficient. I considered using nonnull(0), but then it would have been
confusing that the version of nonnull without arguments applies only to
parameters and not the return value.

I can't comment on the patch itself, but could there also be an
attribute "returns_null", for functions that *always* return NULL?
This may sound weird, but I know of at least one API that exposes such
functions: CPython's exception-handling API: see e.g.
http://docs.python.org/2/c-api/exceptions.html#PyErr_NoMemory
and various other functions that have "Return value: Always NULL."
This allows the user to write one-liners like:

  return PyErr_NoMemory();

I didn't think about it very long, so I probably missed the best reasons, but it doesn't sound like such a good idea. If PyErr_NoMemory always returns NULL, why not make that clear in the code? It could be an inline function, or even a macro that expands to (PyErr_SetNone(PyExc_MemoryError),NULL).

To me, attributes are there for when the language is insufficient, kind of a last resort. Could you explain why you think it would be the best option here?

--
Marc Glisse


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