Re: [PATCH: 1/1] sh4: avoid spurious gcc warning
From: Randy Dunlap
Date: Sun Jan 22 2023 - 11:02:33 EST
On 1/22/23 04:42, Jakub Jelinek wrote:
> On Sun, Jan 22, 2023 at 12:33:41PM +0100, Michael Karcher wrote:
>> Am 22.01.2023 um 08:00 schrieb Randy Dunlap:
>>>> -#define _INTC_ARRAY(a) a, __same_type(a, NULL) ? 0 : sizeof(a)/sizeof(*a)
>>>> +#define _INTC_ARRAY(a) a, sizeof(a)/(_Generic((a), typeof(NULL): 0xFFFFFFFFU, default: sizeof(*a)))
>>> s/: / : / in 2 places.
>>>
>>> Tested-by: Randy Dunlap <rdunlap@xxxxxxxxxxxxx> # build-tested
>>
>> Thanks for your confirmation! Are you sure about the space before the colon?
Nope, my bad. Thanks, Jakub.
> No, it should be without those, see various other _Generic uses in
> include/linux/
> All those are formatted on one line for each case, so for the above macro it
> would be
> #define _INTC_ARRAY(a) (a), sizeof(a)/(_Generic((a), \
> typeof(NULL): -1, \
> default: sizeof(*(a)))
> or so.
> Anyway, two comments:
> 1) I'd use -1 as that would be after promotion to size_t the largest size_t
> unlike 0xFFFFFFFFU; of course, as for the void * case a can't be an array,
> any value > sizeof(void*) will do
> 2) if *a and a is fine (i.e. argument of the macro has to be really simple or
> wrapped in ()s, then perhaps (a) as first operand to _Generic isn't needed
> either, or use (a) in the two spots (sizeof(a) is of course fine) and
> *(a)
>
>> The colon in this case terminates a case descriptor for the type-level
>> switch construction using "_Generic". It says: "In case 'a' has the 'type of
>> NULL', divide by 0xFFFFFFFFU, in all other cases, divide by the size of a
>> single array element". It's not a colon of the ternary ?: operator, in which
>> case I would agree with the space before it.
>>
>> If you confirm that you want a space before the colon in this case as well,
>> I'm going to add it, though.
>>
>>> How far back in gcc versions does this work?
>>
>> I tested the support of _Generic on Compiler Explorer at godbolt.org. This
>> construction is rejected by gcc 4.8, but accepted by gcc 4.9.
>
> Yeah, introduced in gcc 4.9, as I think kernel minimum version is 5.1, that is fine.
> And various headers already use _Generic.
and thanks for that info also.
--
~Randy