Domanda

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}

È stato utile?

Soluzione

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.

Altri suggerimenti

There seems to be no way around it, but you could define bool yourself like that:

typedef enum _bool { false = 0, true = 1 } bool;
bool x = false;

This should pass the strict checks.

You can #undef true and false after #include <stdbool.h> and define them to new values that make the MISRA stuff happy.

This practice is permitted by C99 and C11 as an obsolescent feature.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top