13.5 for() loops - does simplicity matter? - Printable Version +- MISRA Discussion Forums (https://forum.misra.org.uk) +-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4) +--- Forum: MISRA-C: 2004 rules (https://forum.misra.org.uk/forumdisplay.php?fid=17) +---- Forum: 6.13 Control Statement Expressions (https://forum.misra.org.uk/forumdisplay.php?fid=40) +---- Thread: 13.5 for() loops - does simplicity matter? (/showthread.php?tid=845) |
13.5 for() loops - does simplicity matter? - JohnC - 13-09-2011 I have compiled the following code. The compiler accepts the first loop (ending at X) but rejects the other two (ending at X+2 and at X*2) extern tUI16 X, Y; void Fn( void ) { tUI16 i; for( i = 0u; i < X; i++ ) /* accepted */ { Y += i; } for( i = 0u; i < (X + 2u); i++ ) /* rejected */ { Y += i; } for( i = 0u; i < (X * 2u); i++ ) /* rejected */ { Y += i; } } The compiler suppliers say the compiler will accept the second expression if it is "a simple test" and that X+2 and X*2 do not pass this test. I feel this is being too restrictive for rule 13.5. Please may I have an opinion on this. Thanks, John Re: 13.5 for() loops - does simplicity matter? - misra-c - 13-09-2011 Rule 13.5 requires that the second expression include testing the loop counter but it doesn't place any other requirements on the test. MISRA C does not require a "simple test" in the second expression. All 3 of your examples are therefore compliant with Rule 13.5. The concept of a loop counter is not well defined. In order to assist in identifying loop counters, tools may impose restrictions on the loop and may report violations of this rule even though the code is compliant. |