I'm using the Tasking VX toolset (built on Eclipse) and have a fairly basic but fundamental problem that I can't get around... I've RTFMed and am still totally none the wiser.
Imagine the simple snippet of code:
#include <stdbool.h>
bool myFlag = false;
If I enable MISRA-C checking, I get the following:
MISRA-C rule 10.1 violation: [R] restrict the use of implicit conversions for integer types
Eclipse is set up as a C99 implementation, and as per the standard library definition, stdbool.h
is defined as:
#define bool _Bool
#define true 1
#define false 0
I'm assuming that this error is because #define false 0
and the tool is implicitly converting to bool?
Note: if I cast the assignment, then the error is removed:
bool myFlag = (bool)false;
But (IMHO) that is masking the problem, not addressing it, and I really do not want to cast every assignment.
Tools such as LINT allow you to specify the bool type to stop these sort of false positives... I need the equivalent for Eclipse/Tasking
So my question is this:
I suspect that somewhere there is a tool option to tell TASKING that bool
is the boolean type, and therefore false
and true
are OK to be used?
Is there?
{Please no discussions [on this thread] about the merits (or otherwise) of MISRA}
MISRA rule 10.1 says
(Rule 10.1) The value of an expression of integer type shall not be implicitly converting to a different underlying type if:
a) it is not a conversion to a wider integer type of the same signedness, or
b) the expression is complex, or
c) the expression is not constant and is a function argument, or
d) the expression is not constant and is a return expression
#include <stdbool.h>
bool myFlag = false;
is the same as:
_Bool myFlag = 0;
0
is of type int
which is a signed integer type but _Bool
is an unsigned integer type.
You are implicitly converting a value of a signed type to a value of an unsigned type and so
you are violating a)
in MISRA rule 10.1.
Note that if you are using MISRA-C:2004 (I don't think MISRA-C:2012 has been released), at the time of the publication only C90 was considered and _Bool
is a C99 addition. As I wrote in the comments you can get rid of the warning using a cast like this:
bool myFlag = (bool) false;
This is the beauty of MISRA.