After answering Xcode- C programming - While loop, I stopped making theoric answers and installed clang
on my Windows box to check if it's really better than gcc
in the warning diagnostics department.
I'm using -Wempty-body
in clang
to compile this code, which is wrong because:
- the while instruction ends with a semicolon, possible typo after long hours ending lines with semicolons...: infinite loop
- same thing for
if
statement, making the test useless
wrong code:
int main(void)
{
char c=1;
while (c);
--c;
if (c);
}
I tried to compile it with clang
(5.0.0 x64 windows):
output:
S:\c>clang -Wempty-body test.c
test.c:8:10: warning: if statement has empty body [-Wempty-body]
if (c);
^
So empty if
is detected while while
isn't.
Now I'm wrapping the decrement instruction in a block after the while
:
int main(void)
{
char c=1;
while (c);
{ --c; }
if (c);
}
now it seems to detect both properly:
test.c:6:10: warning: if statement has empty body [-Wempty-body]
if (c);
test.c:4:13: warning: while loop has empty body [-Wempty-body]
while (c);
note: gcc
wasn't able to see either while
bug, so clang
is still clearly superior in terms of warning detection (see also why am I not getting an "used uninitialized" warning from gcc in this trivial example?)
What's the heuristics behind this? Is that a bug?