I noticed a possible false positive for MISRA rule 20.7 in a large project and created a simplified snippet to exemplify the situation, see below.
typedefstruct {
intsail_size;
} BOAT;staticBOATmy_boat;/* Rule 20.7 violation is reported on this line */
#defineACCESS_MEMBER(member)my_boat.memberintmain(void)
{
ACCESS_MEMBER(sail_size);return0;
}
Given the example on page 170 the the MISRA C:2012 standard, I believe this would be a valid macro and thus a false positive. Replacing my_boat.member with my_boat.(member) in the macro definition would not be valid C code.
As a closing note: I really appreciate the MISRA support now being offered by Cppcheck and the hard work which must have gone into this. I only discovered the recent series of kickstarters for expanded MISRA C support in Cppcheck. Should there be another one, I would certainly be among the backers.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
In your example channel should be enclosed in parentheses. Otherwise, if the operator precedence was broken, macro substitution may lead to an unexpected result.
Consider for example the following call: SelectMuxChannel(0x10 | 0x20). We'll get the following code in substitution: 0x10 | 0x20 & MUX_SELECT_MASK. Since the & operator has a higher precedence, this code will be executed as 0x10 | (0x20 & MUX_SELECT_MASK). This is definitely not what the user expects.
I noticed a possible false positive for MISRA rule 20.7 in a large project and created a simplified snippet to exemplify the situation, see below.
This yields as output:
Given the example on page 170 the the MISRA C:2012 standard, I believe this would be a valid macro and thus a false positive. Replacing my_boat.member with my_boat.(member) in the macro definition would not be valid C code.
As a closing note: I really appreciate the MISRA support now being offered by Cppcheck and the hard work which must have gone into this. I only discovered the recent series of kickstarters for expanded MISRA C support in Cppcheck. Should there be another one, I would certainly be among the backers.
Thanks! I created this ticket: https://trac.cppcheck.net/ticket/9673
I've pulled the lastest misra.py and this doesn't seem to be resolved yet. I'm also getting this for the following macro:
This seems correct to me.
In your example
channel
should be enclosed in parentheses. Otherwise, if the operator precedence was broken, macro substitution may lead to an unexpected result.Consider for example the following call:
SelectMuxChannel(0x10 | 0x20)
. We'll get the following code in substitution:0x10 | 0x20 & MUX_SELECT_MASK
. Since the&
operator has a higher precedence, this code will be executed as0x10 | (0x20 & MUX_SELECT_MASK)
. This is definitely not what the user expects.MISRA-compliant code will look like this:
my mistake, sorry about that!
No problem, thanks for the report