I have a simple macro function which will check the condition and return an Boolean value. Below is the snippet of code
assume: #define boolean bool
Test.h file
#define CHECK_STATE(X) (boolean)(STATE_VALUE == (uint8)(X) )
Test.c file
enum MY_STATE{
STATE_0,
STATE_1,
STATE_2
};
static uint8 STATE_VALUE = (uint8)STATE_0; //This value update in the code dynamically from state 0 to 2
if(CHECK_STATE(STATE_1)) /*MISRA warning: Controlling expression is not an 'essentially Boolean' expression. */
{
/*If condition pass*/
}
else
{
/*If condition failes*/
}
In the above code CHECK_STATE
function will return a Boolean value. So if loop can check either true or false. So why MISRA is throwing the warning "not an 'essentially Boolean' expression" ? Any suggestion to fix this type of warnings?
Of course, MISRA prefers you to use inline-functions to function-like macros, to allow better type matching... so your tool is missing a D.4.9 violation! But to the matter in hand...
Your tool is not correctly configured to treat you custom boolean
as a boolean-type; this is compounded by your code being unnecessarily verbose.
There are two ways around this:
1. Simplify Use the MISRA essential types, and use the enumeration type
Test.h file
#define CHECK_STATE(X) ( STATE_VALUE == (X) ) // No casts needed!
Test.c file
enum MY_STATE {
STATE_0,
STATE_1,
STATE_2
};
static enum MY_STATE STATE_VALUE = STATE_0; // No cast needed
if ( CHECK_STATE( STATE_1 ) )
{
/*If condition pass*/
}
else
{
/*If condition fail*/
}
2. Configure the tool See @Eva4684's answer for a hint
IMHO the KISS principle should apply here
As an aside, why are you defining the macro in a header file, when both the variable are the enumeration are module scope?