Jam*_*ris 14 c gcc gcc-warning
我刚刚决定改变所有变量unsigned,int并在重新编译有问题的代码时受到此警告消息的欢迎:
freespace_state.c:203: warning: assuming that the loop is not infinite
Run Code Online (Sandbox Code Playgroud)
有问题的一行:
for (x = startx; x <= endx; ++x, ++xptr)
Run Code Online (Sandbox Code Playgroud)
这个循环是60行代码(包括空格/括号等),并且goto在其中有一个,并且至少出现一次continue.
在这种情况下,我认为我很欣赏GCC假设这个循环不是无限的,因为它永远不应该无限循环.
GCC试图在这里告诉我什么?
警告的语法几乎暗示警告应该在某些其他警告的范围内进行,但在该背景下没有警告.
[编辑] 这完全是我自己的错.我从这里的一个问题偷了一些优化和警告选项而没有真正理解它们,并且从此忘记了它们.
请参阅Mark Rushakoff的回答,此外,我还习惯-Wunsafe-loop-optimizations明确警告GCC是否对循环做出假设.请参阅http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
Mar*_*off 13
根据2005年的GCC补丁,似乎GCC正在执行"不安全的循环优化"(并且因为-funsafe-loop-optimizations设置而被警告).如果循环是无限的,则此特定优化将以某种方式失败.
既然你说这是一个终止循环,听起来好像你没有什么可担心的.
补丁的另一个相关部分:
@opindex Wunsafe-loop-optimizations
Warn if the loop cannot be optimized because the compiler could not
assume anything on the bounds of the loop indices. With
@option{-funsafe-loop-optimizations} warn if the compiler made
+such assumptions.