Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rule 2.2 - dead code
#1
Does the following code example violate Rule 2.2, due to the left shift?

[code]#define BITSET(reg, bit) ((reg) |= (uint8_t)(1u
<t></t>
#2
At first sight, I didn't believe that there was any dead code in your example. However, the question prompted me to re-read Rule 2.2 just to be sure. Having done so, I think you example does contain dead code according to the rule, even though common sense says that it doesn't.

The rule says
Quote:Any operation that is executed but whose removal would not affect program behaviour constitutes dead code

The uint8_t cast seems to satisfy this definition: it is an operator and it is certainly executed but removing it will have no effect on the program's behaviour. If that's the case, then this kind of dead code must be quite common. The essential type rules, for example, may well require insertion of casts that don't actually change the program's behaviour.

Maybe I'm misunderstanding what Rule 2.2 says but I would think that it's not intended to apply to your example.
<t></t>
#3
Thanks for the reply.

Having thought about this, I agree that there is no violation, since the rule is intended to be applied at statement level. As you say, if this weren't the case, there would be violations all over the place and therefore many deviations for any given project.
<t></t>
#4
Could we have an official comment from MISRA on this, namely:
  • does the example contain dead code or not?
  • if not, how does the cast operation avoid being dead given the Glossary definition?
<t></t>
#5
There are various issues raised by the original question and subsequent discussion which we will address in turn.



1. Rule 2.2 applies to operators not statements. 

  It is not a question of whether a whole statement is dead code, but whether there is an operation within the expression that has no effect on the program behaviour.



2. There are two operators of interest in this macro expansion.  First is the operation
  1u << (bit)  which is expanded to 1u << ( 0 )
  The shift operation has no effect on the program behaviour since shifting any value by 0 always produces the same answer.  Therefore the "<<(0)" can be removed and there is a violation of rule 2.2.

3. The second operation of interest is the cast which in this macro expansion has no effect and therefore is strictly speaking a violation of rule 2.2.
  However we agree with the previous poster that this was probably not what was intended by this rule. We will revisit this issue at a later date.
Posted by and on behalf of the MISRA C Working Group


Forum Jump:


Users browsing this thread: 1 Guest(s)