Re: [PATCH] Documentation: coding-style: ask function-like macros to evaluate parameters

From: Andrew Morton
Date: Thu Mar 21 2024 - 13:44:36 EST


On Thu, 21 Mar 2024 07:48:36 +1300 Barry Song <21cnbao@xxxxxxxxx> wrote:

> > Stronger than that please. Just tell people not to use macros in such
> > situations. Always code it in C.
>
> While I appreciate the consistency of always using "static inline"
> instead of macros,
> I've noticed numerous instances of (void) macros throughout the kernel.
>
> arch/arm64/include/asm/cpuidle.h:#define arm_cpuidle_save_irq_context(c) (void)c
> arch/arm64/include/asm/cpuidle.h:#define
> arm_cpuidle_restore_irq_context(c) (void)c
> arch/loongarch/include/asm/io.h:#define iounmap(addr) ((void)(addr))
> arch/mips/include/asm/cop2.h:#define cop2_save(r) do { (void)(r); } while (0)
> arch/mips/include/asm/cop2.h:#define cop2_restore(r) do { (void)(r); } while (0)
> arch/mips/include/asm/cop2.h:#define cop2_save(r) do { (void)(r); } while (0)
> arch/mips/include/asm/cop2.h:#define cop2_restore(r) do { (void)(r); } while (0)
> ....
>
> I'm uncertain whether people would find it disconcerting if they completely
> deviate from the current approach.
>
> If you believe it won't pose an issue, I can proceed with v3 to eliminate
> the first option, casting to (void).

I think so. My overall view is that we should write things in C. Only
use macros if the thing we're trying to do simply cannot be done in a C
function.

- inline functions don't have the "expression with side effects
evaluated more than once" problem.

- inline functions avoid the unused-variable issue which started this thread

- inline functions look better

- for some reason, people are more inclined to document inline
functions than macros.