Re: [PATCH RESEND] uapi/linux/const.h: Prefer ISO-friendly __typeof__

From: Petr Vorel
Date: Tue Apr 11 2023 - 17:39:55 EST


> Hi Kevin,

> > typeof is (still) a GNU extension, which means that it cannot be
> > used when building ISO C (e.g. -std=c99). It should therefore be
> > avoided in uapi headers in favour of the ISO-friendly __typeof__.

> IMHO UAPI are built with -std=c90 -Wall -Werror=implicit-function-declaration
> (see usr/include/Makefile).
> But one or the other, you're right both require __typeof__.

> "If you are writing a header file that must work when included in ISO C
> programs, write __typeof__ instead of typeof."
> https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Typeof.html

> Reviewed-by: Petr Vorel <pvorel@xxxxxxx>
> Tested-by: Petr Vorel <pvorel@xxxxxxx>

IMHO problem was introduced when -std=c90 was added (back then the code was in
include/uapi/linux/kernel.h).

Fixes: d6fc9fcbaa65 ("kbuild: compile-test exported headers to ensure they are self-contained")

Kind regards,
Petr

> Kind regards,
> Petr

> > Unfortunately this issue could not be detected by
> > CONFIG_UAPI_HEADER_TEST=y as the __ALIGN_KERNEL() macro is not
> > expanded in any uapi header.

> > Reported-by: Ruben Ayrapetyan <ruben.ayrapetyan@xxxxxxx>
> > Tested-by: Ruben Ayrapetyan <ruben.ayrapetyan@xxxxxxx>
> > Signed-off-by: Kevin Brodsky <kevin.brodsky@xxxxxxx>
> > ---
> > include/uapi/linux/const.h | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)

> > diff --git a/include/uapi/linux/const.h b/include/uapi/linux/const.h
> > index af2a44c08683..a429381e7ca5 100644
> > --- a/include/uapi/linux/const.h
> > +++ b/include/uapi/linux/const.h
> > @@ -28,7 +28,7 @@
> > #define _BITUL(x) (_UL(1) << (x))
> > #define _BITULL(x) (_ULL(1) << (x))

> > -#define __ALIGN_KERNEL(x, a) __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
> > +#define __ALIGN_KERNEL(x, a) __ALIGN_KERNEL_MASK(x, (__typeof__(x))(a) - 1)
> > #define __ALIGN_KERNEL_MASK(x, mask) (((x) + (mask)) & ~(mask))

> > #define __KERNEL_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))