Bug#128950: gcc's -Wconversion
* Joseph S. Myers (jsm28@cam.ac.uk) wrote:
> On Thu, 6 Feb 2003, Geoff Thorpe wrote:
>
> > void foo(unsigned short b)
> > {
> > foo(b + 1);
> > }
> >
> > compiled with "gcc -Wconversion" gives the dreaded "warning: passing arg
> > 1 of `foo' with different width due to prototype".
>
> But in this particular case, the warning is correct. b gets promoted to
> int in the expression, and b+1 has type int, and (if b == USHRT_MAX) the
> implicit conversion can change the value.
OK, that was an unfortunately bad example and you found the hole in the
example rather than the problem I was trying to point out. Try;
s/b + 1/b/
or
s/b + 1/1/
or
s/b + 1/(unsigned short)(b + 1)/
They give the same warning. Yes, the parameter gets promoted in all
these cases, but not in the sense you mentioned - they get promoted to
32-bit types w.r.t. calling convention and ABI. However that's not C's
fault, and I shouldn't (really) get promotion warnings when passing a
perfectly valid unsigned short parameter to a function that is
prototyped accordingly. Eg. you get the same problem if you try to
compile code that calculates htons(5) (unless you use -O2 and inlining
gets rid of the function call altogether). The upshot (it seems) is that
any code that calls any function declared to accept parameters smaller
than 32-bits in width will give these warnings. It's what I'm seeing
with the htons() example I gave FWIW, and I see this with gcc-2.96 as
well as Mandrake's gcc-3.2 package.
Sorry for the misleading example.
Cheers,
Geoff
--
Geoff Thorpe
geoff@geoffthorpe.net
http://www.geoffthorpe.net/
Reply to: