03-03-2015, 04:00 PM
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.
Is there a compliant way out of this conflict or do we need to file a deviation for this use?
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();
}
<t></t>