我可以对海湾合作委员会也通过全类型的函数警告? [英] Can I make GCC warn on passing too-wide types to functions?
问题描述
以下是一些明显的缺陷code,对于它,我认为编译器应该发出一个诊断。但无论是 GCC
也不 G ++
确实,即使所有我能想到的警告选项: -pedantic -Wall -Wextra
Following is some obviously-defective code for which I think the compiler should emit a diagnostic. But neither gcc
nor g++
does, even with all the warnings options I could think of: -pedantic -Wall -Wextra
#include <stdio.h>
short f(short x)
{
return x;
}
int main()
{
long x = 0x10000007; /* bigger than short */
printf("%d\n", f(x)); /* hoping for a warning here */
return 0;
}
有没有一种方法,使 GCC
和 G ++
警告呢?在一个侧面说明,你有另一个编译器在默认情况下还是一个相当普遍的额外的警告配置警告说这件事?
Is there a way to make gcc
and g++
warn about this? On a side note, do you have another compiler which warns about this by default or in a fairly common extra-warnings configuration?
请注意:我使用GCC(C和C ++编译器)版本4.2.4
Note: I'm using GCC (both C and C++ compilers) version 4.2.4.
编辑:我刚刚发现, GCC -Wconversion
开了窍,但同样的选项, G ++
没有按' T,而且我真的很喜欢使用C ++在这里,所以我需要为 G ++
溶液(和我现在知道为什么 -Wconversion
似乎并没有成为它)。
I just found that gcc -Wconversion
does the trick, but the same option to g++
doesn't, and I'm really using C++ here, so I need a solution for g++
(and am now wondering why -Wconversion
doesn't seem to be it).
编辑: http://gcc.gnu.org/bugzilla/show_bug。 CGI?ID = 34389 表明,这可能是固定在 G ++ 4.4
...也许?这不是很清楚,我还没有,如果是同样的问题,和/或如果修订确实在该版本的到来。也许有人采用4.3或4.4可以试试我的测试用例。
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34389 suggests that this may be fixed in g++ 4.4
...maybe? It's not clear to me yet if it's the same issue and/or if the fix is really coming in that version. Maybe someone with 4.3 or 4.4 can try my test case.
推荐答案
使用-Wconversion - 问题是由长×短隐式转换(转换)时,函数f(简称X)称为[没有的printf]和-Wconversion会这样说剧组,从长到短可能会改变的价值。
Use -Wconversion -- the problem is an implicit cast (conversion) from long x to short when the function f(short x) is called [not printf], and -Wconversion will say something like "cast from long to short may alter value".
..
编辑:刚才看到你的注意。 -Wconversion导致了我一个警告,在Linux上使用G ++ 4.3.2 ...(4.3.2-1在Debian)
just saw your note. -Wconversion results in a warning for me, using g++ 4.3.2 on Linux... (4.3.2-1 on Debian)
这篇关于我可以对海湾合作委员会也通过全类型的函数警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!