MISRA Discussion Forums

Full Version: C90: Bitfield as essential boolean type
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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;