"sparse" warnings..

Linus Torvalds torvalds at osdl.org
Mon May 3 14:24:58 EST 2004

On Mon, 3 May 2004, Benjamin Herrenschmidt wrote:
> >
> > 	#define PAGE_OFFSET     0xC000000000000000
> >
> > which silently makes the constant be of type "unsigned long" through the C
> > type expansion rules. Sparse doesn't like that, because most people don't
> > actually understand the C type expansion rules.
> Well, do you want to explicitely write it with ULL suffix ?

Preferably yes ("ul", though). It doesn't always work, because some of the
constants are shared with assembly language, and the assembler doesn't
understand C constants and refuses to touch them..

And the regular Linux sources aren't sparse-clean anyway. A lot of the
networking in particular uses the same structures to keep either user
pointers or kernel pointers, and so the static type-checking of sparse
really can't handle them. So don't expect the kernel to come out without
warnings. But right now the ppc64 build gets a _lot_ more warnings than
x86 does.

> > Quiz: what's the difference between
> >
> > 	#define MIN_INT	0x80000000
> > 	#define MIN_INT 2147483648
> >
> > in C for a 64-bit compiler? Hint: one is "unsigned int", the other is
> > "long".
> First one gets zero extended, second one gets sign extended I suppose,
> this first on is bogus.

Literally, try "sizeof()". The first one will be 4 bytes, the second one
is 8 bytes.

It results in some "interesting" results: if you compare against "-1",
then the first one will compare _smaller_ than -1, while the second one
will compare bigger. Really. Try it on a 64-bit compiler with a 32-bit
"int". (in the first case, the -1 will be converted to "unsigned int", and
it will compare as 0x80000000u < 0xffffffffu).

You'll get other differences too. Try shifting them left by one. One
becomes zero, the other becomes 1L << 32. Even though they are the _exact_
same constant, just written differently. The C language is strange

Of course, in 99.9% of all cases, you'll never see anything strange, so
I'm not sure that the warning is worth it. I got rid of it on x86, though,
and in general it's a fairly easy warning to take care of (and once gone,
it's easy to avoid).

On the whole I think it's a good thing to warn about when the type of a
constant is silently converted in C, but I have to admit that it's a
fairly anal warning. Others will think it's a stupid warning.

> Where do I get sparse ? There's a bk ? FAQ ?

There's a BK repo at sparse.bkbits.net (and one on kernel.org in

The current one assumes that you pass in "-m64" to it if you want to check
64-bit sources.


** Sent via the linuxppc64-dev mail list. See http://lists.linuxppc.org/

More information about the Linuxppc64-dev mailing list