Using C99 in the kernel was Re: [PATCH 1/19] MUTEX: Introduce simple mutex implementation

Andi Kleen
2005-12-13T21:56:33Z

[dropping linux-arch because it seems to generate bounces]

On Tue, Dec 13, 2005 at 08:16:39AM -0800, Linus Torvalds wrote:
>
>
> On Tue, 13 Dec 2005, Andi Kleen wrote:
> >
> > Remove -Wdeclaration-after-statement
>
> Please don't.
>
> It's a coding style issue. We put our variable declarations where people
> can _find_ them, not in random places in the code.
>
> Putting variables in the middle of code only improves readability when you
> have messy code.

I like it when the scopes for variables are as small as possible because then
I also find it good documentation when the first initialization
of a variable also has the type.

While it's possible with nested {} blocks too that imho makes the code ugly
because you either end up with non syntactic elements with wrong
indentation (driving emacs/indent etc crazy) or with too much
indentation.

I can see Ingo's argument about catching merging mistakes though -
that is a good point against it that I didn't consider.

>
> Now, one feature that _may_ be worth it is the loop counter thing:
>
> for (int i = 10; i; i--)
> ...
>
> kind of syntax actually makes sense and is a real feature (it makes "i"
> local to the loop, and can actually help people avoid bugs - you can't use
> "i" by mistake after the loop).
>
> But I think you need "--std=c99" for gcc to take that.

Ok. So should we enable that? Or rather --std=gnu99

But actually I tried it and it causes lots of

mm/page_alloc.c:49: error: initializer element is not constant

It looks like casts in constant initializers for global structures are not
allowed anymore: struct foo x = (struct foo) { ... }; warns. That's
not good because when the (struct foo){} is generated in a macro
then it's the only easy way to allow initialization outside a declaration.

Common case is SPIN_LOCK_UNLOCKED() / DEFINE_SPINLOCK().

Richard, any comments on that?

P.S.: Linus, I wish you weren't so fond of using downcounting loops: I find
them always slightly confusing (or rather they need more consideration
than a standard upcounting loop to understand) and gcc is perfectly
capable of reversing loops when suitable on its own.

-Andi