Re: [PATCH] compiler_types: mark __compiletime_assert failure as __noreturn

From: Dan Carpenter
Date: Thu Dec 02 2021 - 01:13:06 EST


On Thu, Oct 14, 2021 at 03:23:31PM +0200, Miguel Ojeda wrote:
> `__compiletime_assert` declares a fake `extern` function
> which appears (to the compiler) to be called when the test fails.
>
> Therefore, compilers may emit possibly-uninitialized warnings
> in some cases, even if it will be an error anyway (for compilers
> supporting the `error` attribute, e.g. GCC and Clang >= 14)
> or a link failure (for those that do not, e.g. Clang < 14).
>
> Annotating the fake function as `__noreturn` gives them
> the information they need to avoid the warning,
> e.g. see https://godbolt.org/z/x1v69jjYY.
>
> Link: https://lore.kernel.org/llvm/202110100514.3h9CI4s0-lkp@xxxxxxxxx/
> Reported-by: kernel test robot <lkp@xxxxxxxxx>
> Signed-off-by: Miguel Ojeda <ojeda@xxxxxxxxxx>

This patch is kind of a headache for Smatch.

The patch basically turns BUILD_BUG() into BUG(). That's fine.

But it also turn BUILD_BUG_ON(!__builtin_constant_p(foo) into a BUG_ON().
A lot of places call BUILD_BUG_ON() to ensure that functions are inlined
and the build breaks if not. For example, the FIELD_GET() macro will
trigger a build bug if "mask" is not a compile time constant.

295 static __always_inline u32 cal_read_field(struct cal_dev *cal, u32 offset, u32 mask)
296 {
297 return FIELD_GET(mask, cal_read(cal, offset));
298 }

Unfortunately, Smatch doesn't respect the __always_inline annotation so
now it thinks cal_read_field calls BUG() and records it as a noreturn
function. And all the callers get marked as noreturn functions as well.

Could we make it so that BUILD_BUG() is a noreturn function and
BUILD_BUG_ON() is left as it was?

regards,
dan carpenter