MISRA Discussion Forums
C90: Bitfield as essential boolean type - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4)
+--- Forum: MISRA C:2012 and MISRA C:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=21)
+---- Forum: 8.10 The essential type model (https://forum.misra.org.uk/forumdisplay.php?fid=165)
+---- Thread: C90: Bitfield as essential boolean type (/showthread.php?tid=1161)



C90: Bitfield as essential boolean type - nunterberg - 03-03-2015

Is it possible to declare bitfields that can be used as essentially boolean expressions in C90 without vialoating any MISRA rules?
Acording to section D.4, only a bit-field that has been implemented with an essential boolean type is essentially boolean. In C90, such a type does not exist as a "native" type. Sections 8.10.2 and D.6 allow an enumerated type as a way to define an essentially Boolean type. However, using such an enumerated type in a bitfield violates Rule 6.1.
Code:
typedef enum { false, true } bool_t;
struct {
    bool_t   flag1:1;  /* violates Rule 6.1 */
    uint16_t flag2:1;  /* compliant */
} bf;

if (bf.flag1) {        /* compliant? */
    do_something();
}
if (bf.flag2) {        /* violates Rule 14.4? */
    do_something();
}
Is there a compliant way out of this conflict or do we need to file a deviation for this use?


Re: C90: Bitfield as essential boolean type - misra-c - 13-03-2015

You would need to deviate from rule 6.1 if you wish to use an enumeration type as your essentially boolean type.

An alternative would be to define your bool_t as an unsigned int.
Code:
typedef unsigned int bool_t;