I have been previously been using MDK-ARM V4.53 and have recently upgraded to V4.60. When using O2 optimisation I started getting a compiler "may be used before being set" warning that I never got with V4.53. I have provide some demo code below. In my view this is a compiler bug - do you agree?
typedef struct { unsigned int a : 2; unsigned int b : 2; } TEST1; TEST1 test1_global; typedef struct { unsigned int a; unsigned int b; } TEST2; TEST2 test2_global; int main( void ) { TEST1 test1_local; TEST2 test2_local; // the following gives "warning: C3017W: test1_local may be used before being set" test1_local.a = 1; test1_local.b = 1; test1_global = test1_local; // the following is OK test2_local.a = 1; test2_local.b = 1; test2_global = test2_local; return 0; }
Note that it is just a warning, and it does just say "...may be..."
I have also found that GCC can seem to be over-cautious with this type of warning!
The warning happens when you're using bitfields that don't entirely fill the struct - so I guess the compiler might be warning that the "unused" bits have not been set?
What happens if you initialise the local struct in its definition?
I don't like to see any compiler warnings when I compile code that is to be shipped to a customer. This warning is "noise". You're right in that if I initialise the local structure
TEST1 test1_local = {0};
which takes up code space then the warning goes away but I prefer V4.53 behaviour. I guess uninitialsed "unused" bits could cause a problem in some cases such as if using the structure in a union containg another structure of ints which overlay the bit fields but this is dangerous in itself due to bit ordering non-portability.