When compiling in GCC with -Wall
, GCC will warn about using variables that might be uninitialized. However, as the programmer, I know that a variable must be initialized if control enters a certain if
statement:
int foo;
/* conditional assignment of foo goes here */
if (/* complicated condition that guarantees that foo has been initialized */){
return test(foo);
}
In the above code, GCC will complain that foo
may be used uninitialized. In particular, the complain will arise from inside the definition of test()
, where foo
is actually used.
Is there a GCC builtin or equivalent to tell the compiler that foo
is already initialized? For example:
if (/* complicated condition that guarantees that foo has been initialized */){
__assume_initialized(foo);
return test(foo);
}
Note that placing #pragma GCC diagnostic ignored "-Wuninitialized"
before the line return test(foo)
is insufficient as the uninitialized warning arises from the usage (e.g. comparison) of foo in the definition of test()
, possibly in another compilation unit, where the pragma won't exist.
Neither do I want to put the pragma inside the definition of test()
, because the issue is not with test()
. test()
should of course not be called with random garbage, and putting the pragma there might cause other mistakes to go unnoticed.
Any solutions?