13 Jan, 2009, Lobotomy wrote in the 1st comment:
Votes: 0
I've been searching around, but I can't seem to find the solution I want to a particular problem. GCC is throwing a warning message on an assert that it deems will never be executed. What I'd like to do is use some sort of compiler option, if it exists, to disable such warnings. I already know that it's possible to disable all unreachable code warnings by removing my use of the -Wunreachable-code flag, but that's not what I'm looking for here. I only want to disable the warnings when they involve assertions.

I'd expect there to be a compiler option for it, but I'm at a loss as to what it would be. :thinking:
13 Jan, 2009, Mister wrote in the 2nd comment:
Votes: 0
An assert is nothing more than a macro, that when debug is off, expands to if (0) { stuff } (hence the warning).
I don't think it's possible, since you would need to insert a pragma to turn that warning off before any assert, and another pragma to turn it back on after it. That is, if any such pragma were available in the first place (AFAIK, it doesn't).

Now, there is an option to disable warning in system header files… and there is a pragma to tell the compiler that your code actually is a system header file ("#pragma GCC system_header", IIRC). So… disable those warnings in the compilation options, and add pragmas to activate/deactivate system headers around each and every pragma (ouch).

Maybe some solution regarding the creation of a new assert macro that turns those pragmas on/off by itself? Would be much easier to work with, but my preprocessor-fu is weak. Oh, an extra problem is that there seems to be no way to disable the system_headers pragma…

When do the warnings happen? Only with debug on? Only with debug off? Maybe you can use the -Wunreachable-code flag when you are compiling in debug mode, and remove it when you don't.
14 Jan, 2009, David Haley wrote in the 3rd comment:
Votes: 0
I've never had this problem with assertions – the compiler has always been smart enough to do the right thing. What gcc version are you using?
14 Jan, 2009, Mister wrote in the 4th comment:
Votes: 0
#include <assert.h>
int main() {
int a=1;
assert(a==1);
return 0;
}
Shows the warning.
Compiled using gcc 4.2.4, command line: gcc -Wunreachable-code -O c.c

You have to explicitly turn on -Wunreachable-code and optimization.
14 Jan, 2009, David Haley wrote in the 5th comment:
Votes: 0
Hmm. I haven't compiled with optimization in gcc for quite a bit, so I hadn't noticed it stopped working. I could swear though that I've used a compiler that was smart enough about assertions and unreachable code, but I could be thinking of Sun's (which we use at work).
14 Jan, 2009, Mister wrote in the 6th comment:
Votes: 0
Ah, found the problem… the code of my previous post allowed the compiler to know the result of the assertion (always true), so the "false" path was unreachable (that's why optimization was required.) A valid warning, since the assertion is superfluous.

Now, consider this code:
#include <assert.h>
int a=1;
int main() {
assert(a==1);
return 0;
}
Now, 'a' is a global variable, so the compiler can't know if the assertion will succeed or not, resulting in no warning.

Lobotomy: can you post the function or portion of code that triggers the warning? Maybe the assertion always succeeds.
14 Jan, 2009, Lobotomy wrote in the 7th comment:
Votes: 0
DavidHaley said:
I've never had this problem with assertions – the compiler has always been smart enough to do the right thing. What gcc version are you using?

GCC version 4.2.3.

Mister said:
You have to explicitly turn on -Wunreachable-code and optimization.

I always have optimization disabled, personally. I've had code of mine break in the past due to the way the compiler changes things 'under the hood', so I try to prevent that as much as possible. Side note: I tried seeing if enabling optimization would alleviate the warning message somehow, however that didn't work.

Mister said:
Now, 'a' is a global variable, so the compiler can't know if the assertion will succeed or not, resulting in no warning.

Lobotomy: can you post the function or portion of code that triggers the warning? Maybe the assertion always succeeds.

The warnings I'm getting are coming up due to assert calls on function input variables. For instance, if I did something like this:
void example( int exa_one, char *exa_two, (some struct) *exa_three )
{
assert( exa_one >= 0 );
assert( exa_two );
assert( exa_three );

(stuff)
}

Essentially, every assertion call I have, no matter how valid, is raising an unreachable code warning; i.e, the compiler thinks that none of the assertion calls are going to trigger even though I've tested the assertions explicitly and they do in fact work as intended. Global variable, local variable, it doesn't matter; the compiler warns on all of it.

It's rather frustrating, really. :sad:
15 Jan, 2009, David Haley wrote in the 8th comment:
Votes: 0
Mister said:
Now, 'a' is a global variable, so the compiler can't know if the assertion will succeed or not, resulting in no warning.

Ah, ok – that makes me feel like I'm not losing my mind as much! :wink: I was pretty sure it was smart enough about this… should have noticed that the warning was valid in this case because the assertion was always true.

Not sure why Lobotomy isn't seeing the same behavior, though…
0.0/8