gcc 4.3 改变了 -Wconversion

gcc 4.3之前,-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.

Also, warn if a negative integer constant expression is implicitly converted to
an unsigned type.
到了4.3就变了,发布日志上解释到:
The -Wconversion option has been modified. Its purpose now is to warn for
implicit conversions that may alter a value. This new behavior is available for
both C and C++. Warnings about conversions between signed and unsigned integers
can be disabled by using -Wno-sign-conversion. In C++, they are disabled by
default unless -Wsign-conversion is explicitly requested. The old behavior of
-Wconversion, that is, warn for prototypes causing a type conversion that is
different from what would happen to the same argument in the absence of a
prototype, has been moved to a new option -Wtraditional-conversion, which is
only available for C

这个改变其实挺大的,下面通过这个程序就可以展示这个问题:

[c]
void foo(short i);

void foo(short i)
{
i++; //dummy
}

int main(void)
{
int a = 1;
short b =2;
b = a;
foo(a);
return 0;
}
[/c]

$ gcc -Wall -Wtraditional-conversion -o conv conv.c
conv.c: In function ‘main’:
conv.c:14: warning: passing argument 1 of ‘foo’ with different width due to prototype
$ gcc -Wall -o conv conv.c
$ gcc -Wall -Wconversion -o conv conv.c
conv.c: In function ‘main’:
conv.c:13: warning: conversion to ‘short int’ from ‘int’ may alter its value
conv.c:14: warning: conversion to ‘short int’ from ‘int’ may alter its value