10-09-2010, 09:15 AM
The example:
is strictly-speaking compliant with the rule. There are two read accesses of v, each of which will cause a side-effect, but because the + operator is commutative it doesn't matter in which order the two operands are evaluated in this case.
If the example were:
then this would be non-compliant because the value of the expression will depend on the order in which the v operands are evaluated, unless they both evaluate to the same value.
It is also worth noting that:
would be non compliant because the value of v1 might be affected by the evaluation of v2 and vice-versa. Any side-effects arising from the evaluation of v1 might be affected by the evaluation of v2 and vice-versa. The order in which any side-effects arising from evaluation of v1 and v2 would depend on the order in which they were evaluated and this might be observable, for example if v1 and v2 are I/O ports.
Having said all that, the rule does contain the recommendation that volatile objects are accessed only in simple assignments such as:
to ensure that a sequence point occurs between each evaluation of a volatile object. Checking tools are not expected to implement recommendations, but they may do so if they wish. If the recommendation is checked then both of:
are non-compliant
Code:
x = v + v;
If the example were:
Code:
x = v - v;
It is also worth noting that:
Code:
x = v1 + v2; // v1 and v2 both volatile
Having said all that, the rule does contain the recommendation that volatile objects are accessed only in simple assignments such as:
Code:
x = v;
Code:
x = x + v;
x = v + v;
Posted by and on behalf of the MISRA C Working Group