In my firm project, the AUTOSAR platform defines booleans like this
typedef unsigned char boolean;
plus
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
This is not modifiable. Then we get the MISRA 10.3 error Expression assigned to a narrower or different essential type [MISRA 2012 Rule 10.3, required] on the following two assignments (real code replaced, of course!)
boolean mybool = (boolean)FALSE;
if(some_condition)
{
mybool = (boolean)TRUE;
}
We've tried other convertions with (uint8)FALSE
or (unsigned char)FALSE
or even without a convertion mybool = TRUE;
without curing the issue. We would be happy to avoid justifying for deviation.
Does anyone has an idea of what happens and how to cure it?
@Fredrik Thanks for your first answer. I put this in a dummy header, included it in the 'culprit' .c
and run PC-Lint/MISRA on this unit
#define testTRUE 1U
boolean x = testTRUE;
boolean y = (uint8)testTRUE;
boolean z = (boolean)testTRUE;
#define testTRUE_2 1
boolean x_2 = testTRUE_2;
boolean y_2 = (uint8)testTRUE_2;
boolean z_2 = (boolean)testTRUE_2;
unsigned char x_3 = (boolean)1;
unsigned char y_3 = (boolean)testTRUE;
unsigned char z_3 = (boolean)testTRUE_2;
and get the same issue on the first 6 assignments. As to last 3 assignments, the error is not raised but perhaps hidden by this one in replacement: Use of modifier or type 'unsigned' outside of a typedef [MISRA 2012 Directive 4.6, advisory]
bool
(or, at the very least,_Bool
)? Sticking with C89? – Hearsay_Bool
is provided by the C99 language (supported by MISRA 2012, used by OP) itself and does not require the Standard Library at all so, even in baremetal applications with absolutely no other code or library, it's still there. Anyway, after a bit of digging, I guess the answer to my previous question is: just ask the AUTOSAR guys as that's where OP'sboolean
comes from (see page 19) and they seem to be very serious about re-defining every single type ... – Hearsay_Bool
mapped to BIT types (e.g. '_bit' in some implementations) , which you might not be able to pass per pointer to in APIs. And usingTRUE
andFALSE
is just conform to common automotive naming conventions for MACROS and DEFINES as uppercase. – Opsonin