This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Question about compiler warning

Hi I have a question about a compiler warning.


See 

Customer reports:

 #548-D transfer of control bypasses initialization of: sl_selftest.c /MonacoSC/BSP/SafetyLibrary/source line 162 C/C++

Googling for explanations of this warning I found 

Which I think I understand and also understand that by putting the initialized variable inside {} under the case statement, this is safe now because if that variable is used outside the {} now it would be out of scope and you'd actually get an error instead of a warning.

Mainly, I first want to check w. the compiler experts that this is a correct explanation and also confirm that it applies to both C and C++.  (some other explanations talk about this being a requirement of the C++ standard...)

Now assuming that explanation is correct the workaround suggested would be to put  { } around the code in every case (kind of a brute force solution).

But let's say we wanted to find the actual offending case .. aside from putting {} around each case and then removing until the error comes back - is there any way that the compiler can help by showing which case causes the issue.  It seems to be referring to the line with the 'switch()' but there's a ton of cases under that switch and the code is very difficult to read IMO  :(  ..  I looked for something that might be the offending line but didn't spot it.

The code throwing the warning in question is the attached file:   and searching for 'false warning' should take you to right above the switch statement... 

5736.sl_selftest.c

Mainly looking for some expert advice on this problem because the code is part of the safety library and because of that I think the customer is especially concerned about making sure that it compiles cleanly without warnings - so want to give the best (correct) advice possible here and not just something that makes the warning disappear for the wrong reason...

  • Anthony F. Seely said:
    is there any way that the compiler can help by showing which case causes the issue.

    When I try a simple test case, I get a useful diagnostic like this ...

    "file.c", line 3: warning: transfer of control bypasses initialization of:
                variable "i" (declared at line 6)

    Versions 5.2.x and later of the ARM compiler issue this diagnostic.  Are you using one of those versions, and not seeing a similar diagnostic?

    Thanks and regards,

    -George

  • Thanks George,

    I asked the customer on the original thread. He didn't paste in the compiler version # or the additional diagnostic info but I asked him to let us know and also if he gets the additional diagnostic to try putting the case statement he finds that pointing to in {} to see if it removes the warning.
  • I'm pretty sure the customer is looking at the diagnostic as it appears in the Problems view in CCS.  And this means only the first line of the diagnostic is seen.  To see the full diagnostic, look at it in the Console view.  That will show the line number of the variable initialization that is bypassed.

    Thanks and regards,

    -George