Re: guard coding style (was: Re: [PATCH v1 05/10] gpio: pca953x: Simplify code with cleanup helpers)

From: Peter Zijlstra
Date: Thu Sep 14 2023 - 18:18:45 EST


On Thu, Sep 14, 2023 at 09:47:07AM +0200, Geert Uytterhoeven wrote:

> > Only compound statements need curly braces in the kernel and it has
> > been like this forever. I don't really see a need to make it an
> > exception.

Kernel coding style is a little different from what C demands.
Specifically, kernel style demands { } for anything multi-line.

Specifically:

for (;;) {
/* a comment */
foo();
}

or

for (;;) {
foo(a, very, long,
arg, chain);
}

do both warrant a pile of curlies in kernel style where C does not
demand it.

> > That being said - I don't think the coding style for guard has ever
> > been addressed yet, so maybe bring it up with Peter Zijlstra?
>
> That's a good idea!
>
> I see Peter always used curly braces (but he didn't have any
> single-statement blocks, except for one with an "if", and we do tend
> to use curly braces in "for"-statements containing a single "if", too),
> but he does put a space after the "scoped_guard", as is also
> shown in the template in include/linux/cleanup.h:
>
> scoped_guard (name, args...) { }:
>

Right, per scope_guard being a for loop I added the extra space. Our
coding style does;

if (cond) { }

while (cond) { }

for (;;) { }

etc.. so I too did:

scoped_guard (name, args...) { }

> Then, "guard" does not get a space (but it is funny syntax
> anyway, with the double set of parentheses ;-). The template in
> include/linux/cleanup.h doesn't match actual usage as it lacks the
> second set of parentheses:
>
> guard(name):
>
> Peter: care to comment?
> Or do you have a different bikeshed to paint today? ;-)

For guard I read the first pair as if it were a C++ template, that is, I
pretend, it is actually written like:

guard<name>(args..);

Both are 'odd' in numerous ways and inconsistent vs where the 'args...'
go, but alas, we're trying to wrangle this inside the constraints
imposed upon us by C and CPP our dear pre-processor.