MISRA Discussion Forums
1-bit bitfield in boolean expression - 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: 2004 rules (https://forum.misra.org.uk/forumdisplay.php?fid=17)
+---- Forum: 6.13 Control Statement Expressions (https://forum.misra.org.uk/forumdisplay.php?fid=40)
+---- Thread: 1-bit bitfield in boolean expression (/showthread.php?tid=972)



1-bit bitfield in boolean expression - holger_l - 22-07-2013

Hi,
rule 13.2 makes an example how boolean data could be used in an expression:
Code:
if ( y ) /* Not compliant, unless y is effectively Boolean data (e.g. a flag) */
The example refers to a Boolean-by-enforcement type referred to in the appendix.
But what is with a 1-bit Bitfield? Is it a Boolean-by-construct?
Code:
struct { unsigned int flag1: 1, flag2: 1;} var;
...
if (var.flag1)                   /* violation of rule 13.2 ? */
if (( var.flag1) && (var.flag2)) /* violation of rule 12.6 ? */
if ((!var.flag1) && (var.flag2)) /* 2x violation of rule 12.6 ? */
if ((var.flag1 == 0) && (var.flag2 != 0)) /* OK, but clumsy and harder to read */
Thanks for a comment!


Re: 1-bit bitfield in boolean expression - holger_l - 30-07-2013

To clarify the question: I think it is obvious that a 1-bit flag is a boolean value, but our checker tool thinks it is not, and only accepts the clumsy form.
Unfortunately MISRA-C 2004 is missing a clear definition and the only hint is that a "flag" is allowed.


Re: 1-bit bitfield in boolean expression - misra-c - 12-09-2013

MISRA-C:2004 addresses issues with C:1990

MISRA-C:2004 deliberately did not describe what constituted a Boolean. There were too many different ways of defining the Boolean type in common usage, and no generally accepted single definition. The results of the analysis will therefore depend on the forms of Boolean that the user's analysis tool can be configured to accept.