On Wed, Jun 01, 2016 at 09:17:35AM -0600, Martin Sebor wrote:
> I see.  You meant that only the clang compatibility built-ins
> (i.e. the typed ones like  __builtin_uadd_overflow) shouldn't
> be allowed to take null pointer as the last argument, but the
> type-generic ones should.  That would work for C, though it
> won't satisfy the feature request in c/68120 which asks for
> the last argument to be optional (it can't be there since it
> determines the type of the operation).  It will not work for

I don't think we should make it optional.

> C++ 98.  But those might be acceptable limitations (at least
> in C there's a workaround).

__builtin_* is an extension.  Perhaps for C (any) and C++ (< C++14)
you could require that in constant expressions the last argument
of the builtin has to be a NULL pointer cast to some pointer type
(not necessarily a constant expression)?  This can be handled
simply by saving/restoring the constant expression context around
the parsing of the last argument of the builtin, and afterwards
verifying it has the required properties.

For C++14 and later, it would be nice if one could also do:
struct S { int val; bool ovf; };
constexpr S foo (int x, int y)
{
  S ret = { 0, false };
  ret.ovf = __builtin_add_overflow (x, y, &ret.val);
  return ret;
}
instead of doing:
  ret.ovf = __builtin_add_overflow (x, y, (int *) 0);
  ret.val = (unsigned) x + y;
or similar.
Does that work with your patch?

        Jakub

Reply via email to