-Wconversion
Zack Weinberg
zack@wolery.cumb.org
Fri Jul 14 10:41:00 GMT 2000
On Fri, Jul 14, 2000 at 07:27:28PM +0200, Patrik Hagglund wrote:
> I don't understand the output from -Wconversion.
>
> The following program is stored in the file test.c:
>
> #include <stdio.h>
>
> static void foo(unsigned char ch) {
> printf("%c\n", ch);
> }
>
> int main(int argc, char **argv) {
>
> unsigned char bar = 'a';
>
> foo(bar);
>
> return 0;
> }
>
> > gcc -Wconversion test.c
> test.c: In function `main':
> test.c:12: warning: passing arg 1 of `foo' with different width
> due to prototype
Yep. That's the expected behavior.
> I can't match this behavior with the documentation:
>
> `-Wconversion'
> Warn if a prototype causes a type conversion that is different
> from what would happen to the same argument in the absence of a
> prototype. This includes conversions of fixed point to floating
> and vice versa, and conversions changing the width or signedness
> of a fixed point argument except when the same as the default
> promotion.
...
> The test program above don't causes any conversion and should
> therefore not give any warning?
If your code read
> static void foo();
>
> int main() {
> unsigned char bar = 'a';
>
> foo(bar);
> return 0;
> }
then bar would be converted to an int before it was passed to foo,
and foo would have to be written to expect that, i.e.
> static void foo(ch)
> int ch;
> {
> printf("%c\n", ch);
> }
That is known as a _default promotion_. Adding a prototype prevents
this from happening.
The purpose of -Wconversion is to shake out bugs introduced when you
add prototypes to a program that did not have them. So it warns you
whenever you write a prototype that causes different argument
conversion than the default. That includes no conversion at all.
We don't put it in -Wtraditional because the warning triggers for
legitimate code and there's no way to get rid of it. For instance, it
will complain about every function in <math.h> that takes float rather
than double arguments (sinf, cosf, etc.) -Wconversion is not intended
for day-to-day use; -Wtraditional is.
zw
More information about the Gcc
mailing list