[Xcb] [PATCH] c_client.py: Prefer bit shifts over values in enum

Peter Harris pharris at opentext.com
Tue Jul 29 13:59:12 PDT 2014


On 2014-07-29 16:26, Daniel Martin wrote:
> Attention, thread necromancy ...
> 
> On Wed, Jul 03, 2013 at 07:26:48PM -0400, Peter Harris wrote:
>> On 2013-06-07 02:21, Daniel Martin wrote:
>>> On Fri, Jun 07, 2013 at 12:00:39AM +0200, Daniel Martin wrote:
>>>> When generating enum structures, prefer bit shifts over values. And
>>>> align the equal signs.
>>
>> That looks like a reasonable clean-up.
>>
>>> Forgotten to mention: With this patch, we ge rid of a compiler warning
>>> as well:
>>>     xkb.h:118:40: warning: ISO C restricts enumerator values to range \
>>>        of 'int' [-Wpedantic]
>>>          XCB_XKB_CONTROL_CONTROLS_ENABLED = 2147483648
>>
>> That's a bit scary, actually, as the bit-shift replacement will also
>> generate a value outside the range of int, and now we're relying on
>> implementation defined behaviour in addition to a gcc extension (1 <<
>> 31) is implementation defined, since it's outside the range of int. (1u
>> << 31u) is standard C (given a 32-bit int, which I think everything we
>> support has)).
> 
> Just applied my rusty patch and added -pedantic:
> 
>     xkb.h:118:40: warning: ISO C restricts enumerator values to range of
>         'int' [-Wpedantic]
>             XCB_XKB_CONTROL_CONTROLS_ENABLED = (1u << 31u)
> 
> If I change the value to (1 << 31u) or even (1 << 31) the warning
> doesn't trigger.
> 
> Any idea or wish how I should change the patch, how the resulting value
> should like?

0x80000000 simply isn't representable in an enum in standard C.


Ideally, the resulting value should[1] look like

#define XCB_XKB_CONTROL_CONTROLS_ENABLED (1u << 31u)

outside of the rest of the enum, and s/enum xcb_xkb_control_t/uint32_t/g
(if you find a user in our API). Except I'm not sure we can, because it
might qualify as a change to the API, or at least to the potential set
of warnings you might get when using it.

Peter Harris

[1] Or, for maximum portability and minimum readability,
#define XCB_XKB_CONTROL_CONTROLS_ENABLED (UINT32_C(1) << UINT32_C(31))

Ick.
-- 
               Open Text Connectivity Solutions Group
Peter Harris                    http://connectivity.opentext.com/
Research and Development        Phone: +1 905 762 6001
pharris at opentext.com            Toll Free: 1 877 359 4866


More information about the Xcb mailing list