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

From: Barry Song
Date: Wed Mar 20 2024 - 14:49:10 EST


On Thu, Mar 21, 2024 at 4:49 AM Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> wrote:
>
> On Wed, 20 Mar 2024 16:24:30 +1300 Barry Song <21cnbao@xxxxxxxxx> wrote:
>
> > Hi Stephen,
> > Thanks for reviewing.
> >
> > On Wed, Mar 20, 2024 at 2:42 PM Stephen Rothwell <sfr@xxxxxxxxxxxxxxx> wrote:
> > >
> > > Hi Barry,
> > >
> > > On Wed, 20 Mar 2024 13:16:56 +1300 Barry Song <21cnbao@xxxxxxxxx> wrote:
> > > >
> > > > diff --git a/Documentation/process/coding-style.rst b/Documentation/process/coding-style.rst
> > > > index 9c7cf7347394..8065747fddff 100644
> > > > --- a/Documentation/process/coding-style.rst
> > > > +++ b/Documentation/process/coding-style.rst
> > > > @@ -827,6 +827,13 @@ Macros with multiple statements should be enclosed in a do - while block:
> > > > do_this(b, c); \
> > > > } while (0)
> > > >
> > > > +Function-like macros should evaluate their parameters, for unused parameters,
> > > > +cast them to void:
> > > > +
> > > > +.. code-block:: c
> > > > +
> > > > + #define macrofun(a) do { (void) (a); } while (0)
> > > > +
> > >
> > > Maybe add some comment about using a static inline function for these
> > > simple versions instead, if at all possible, (it is suggested just
> > > above this section) since that will still type check arguments.
> >
> > right, what about adding the below section together with the above (void) cast?
> >
> > +Another approach could involve utilizing a static inline function to replace
> > +the macro.:
> > +
> > +.. code-block:: c
> > +
> > + static inline void fun(struct foo *foo)
> > + {
> > + }
> > +
>
> 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).

Thanks
Barry