Re: [PATCH] bitfield: use BUILD_BUG_ON_ZERO()

From: Johannes Berg
Date: Fri Oct 12 2018 - 15:45:20 EST


Umm, I really should've tested this :-)

As John points out, it can't possibly compile, e.g. because

> +#define __BF_FIELD_CHECK(_mask, _reg, _val) \
> + BUILD_BUG_ON_ZERO(!__builtin_constant_p(_mask)) + \

this isn't actually a constant expression.

This would be harmless, since e.g.

> + BUILD_BUG_ON_ZERO((_mask) == 0) + \

this already forces _mask to be a constant, but

> + BUILD_BUG_ON_ZERO(__builtin_constant_p(_val) ? \
> + ~((_mask) >> __bf_shf(_mask)) & (_val) : 0) + \

we can't actually replicate this check.

We could just remove this as well, at the expense of not getting any
warnings if you write something like

FIELD_PREP(0xf, 32);

Realistically, FIELD_PREP() is the only one that makes sense in a pure
constant context though, so we could just add __FIELD_PREP for that,
*with* all the checks. I'll send out a patch in a second that has this
and also adds __{u,le,be}{16,32,64}encode_bits().

johannes