Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
2.2 Dead code, 'operations' and casts

I have a question regarding the meaning of 'Dead Code' within MISRA-2012.  My question is, I believe, somewhat similar to this thread, but the contents of that thread appear to be missing such that I cannot determine what the exact question was, nor the answer.

My questions are:

1) What is an 'operation'?  'Dead code' is defined in terms of 'operations', but 'operation' is never defined in MISRA.

2) Does an addition/subtraction of a constant 0 to a value (either literally or by macro) count as dead code?  This sometimes happens in the context of register offsets where the first offset is 0.
#define BASE_ADDR 0x80000000u
#define CTRL_OFFSET 0x0u
uint32_t a = BASE_ADDR + 0x0u; // Dead addition?
uint32_t b = BASE_ADDR + CTRL_OFFSET; // Dead addition?

3) Does a cast of a value to it's own type count as dead code?
uint8_t a = (uint8_t)0u; // Dead cast? (Type of 0u is UTLR)
uint8_t b = (uint8_t)a; // Dead cast? (Type of a is uint8_t)

4) Does an explicit cast to a type for which implicit conversion is allowed count as dead code?
uint8_t a = (uint8_t)0; // Dead cast? (Type of 0 is STLR, but assignment is permitted by exception)
uint16_t b = (uint16_t)a; // Dead cast? (Conversion to wider type of same signedness is allowed implicitly)
#2 is now visible.  The transfer from the old Bulletin Board to the new Discussion Forum lost part of the response which has now been added.

1. An operation is the act of executing an operator. Operators are listed in Section 6.5 of the C99 Language standard.

2. The addition operation is "dead code" since it can be removed. If you do not wish to change your code, it may be appropriate to add a deviation to explain that you understand that "dead code" is present and why you wish to retain it.

3. The current wording does imply that the cast should be removed as it has no effect.  However, the next Technical Clarification will include the following exception.
A cast operator whose result is "used" is not "dead code".

4. The same comments as question 3 apply.
Posted by and on behalf of the MISRA C Working Group

Forum Jump:

Users browsing this thread: 1 Guest(s)