A
Ark Khasin
MISRA came up with those "underlying types" of sub-int size (like likely
char and perhaps short) and the whole arithmetic on them.
Basically, I need to continually cast back to the "underlying" type "as
if" the computations were done on them without promotion. E.g.,
uint16_t a, b;
a = (uint16_t)(~b); //RH would not approve of it
instead of
a = ~b;
On the one hand, I find myself utterly unable to read my own
MISRA-compliant code written an hour ago.
On the other hand, there are cases where the lack of a cast does not
implement the programmer's intentions, as in
if((a^~b)==0U) ... (always false because of upper bits of ~b)
On the third hand (yes, I am a coding monkey), I could try to wrap this
ugliness in macros, like
#define COMPLEMENT16(x) ((uint16_t)(~(x)))
but it doesn't look much more readable.
I am looking for an advice on how to go about this, generic if possible.
In particular, should a MISRA-inspired company's C coding standard
adjust to the skills of the team members?
A rule like "F(x,y,2007) is always 0. Why? One, Two, Three! If you don't
know the answer, abide by MISRA Rule P.Q" sounds quite idiotic... For
the author and the maintainer may be different people.
Is there a better way?
char and perhaps short) and the whole arithmetic on them.
Basically, I need to continually cast back to the "underlying" type "as
if" the computations were done on them without promotion. E.g.,
uint16_t a, b;
a = (uint16_t)(~b); //RH would not approve of it
instead of
a = ~b;
On the one hand, I find myself utterly unable to read my own
MISRA-compliant code written an hour ago.
On the other hand, there are cases where the lack of a cast does not
implement the programmer's intentions, as in
if((a^~b)==0U) ... (always false because of upper bits of ~b)
On the third hand (yes, I am a coding monkey), I could try to wrap this
ugliness in macros, like
#define COMPLEMENT16(x) ((uint16_t)(~(x)))
but it doesn't look much more readable.
I am looking for an advice on how to go about this, generic if possible.
In particular, should a MISRA-inspired company's C coding standard
adjust to the skills of the team members?
A rule like "F(x,y,2007) is always 0. Why? One, Two, Three! If you don't
know the answer, abide by MISRA Rule P.Q" sounds quite idiotic... For
the author and the maintainer may be different people.
Is there a better way?