Re: typedefs and structs [was Re: [PATCH 16/42]: PCI: PCI Errorreporting callbacks]

From: Randy.Dunlap
Date: Mon Nov 07 2005 - 14:04:37 EST


On Mon, 7 Nov 2005, linas wrote:

> On Mon, Nov 07, 2005 at 10:27:27AM -0800, Greg KH was heard to remark:
> >
> > 3) realy strong typing that sparse can detect.
>
> Am compiling now.
>
> > enums don't really work, as you can get away with using an integer and
> > the compiler will never complain. Please use a typedef (yeah, I said
> > typedef) in the way that sparse will catch any bad users of the code.
>
> How about typedef'ing structs?

No no no. (I feel sure that you will get plenty of responses.)

> I'm not to clear on what "sparse" can do; however, in the good old days,
> gcc allowed you to commit great sins when passing "struct blah *" to
> subroutines, whereas it stoped you cold if you tried the same trick
> with a typedef'ed "blah_t *". This got me into the habit of turning
> all structs into typedefs in my personal projects. Can we expect
> something similar for the kernel, and in particular, should we start
> typedefing structs now?

No no no.

> (Documentation/CodingStyle doesn't mention typedef at all).

We can submit patches for that.

Basically (generally) we never want a struct to be typedef-ed.
(There may be a couple of exceptions to this.)

We do allow a very few basic types to be typedef-ed, as long as
the basic type (e.g., pid_t) is also a C language basic type or
the typedef is useful for strong type checking.

--
~Randy
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/