Uploaded image for project: 'Embedded Software & Tools'
  1. Embedded Software & Tools
  2. EXT_EP-9938

Compiler issues MISRA diagnostic 10.1 for assigning logical expressions to bool

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Implement
    • Icon: Low Low
    • Code Generation Tools
    • CODEGEN-7921
    • C2000_20.2.2.LTS
    • default
    • No plans to address at this time.
    • MISRA 2004 checking has been deprecated. No further MISRA issues will be addressed.

      The attached file includes <stdbool.h>, and contains these lines ...

         my_bool =  true &&  false;
         my_bool =  !!(true && false);
      
      

      Why does the MISRA-C:2004 checker emit diagnostic 10.1/R for this code?

      In C, logical expressions are of type "int;" even "true" and "false" are just macros that are integer constants of type "int." The test case assigns these expressions to an object of type "bool," which is just a macro for type _Bool, which, as you've probably seen in the header file, is declared as "unsigned char." You can remove the warning by casting your logical expressions to "unsigned char" before assigning them to "bool" variables:

      my_bool = (unsigned char)(true && false);
      my_bool = (unsigned char)!!(true && false);

      Strangely, neither "bool" nor "_Bool" works as a substitute. I suspect this is a deficiency in the MISRA-C:2004 checking, which naturally does not have a special case for C99 boolean types. That is, cast or no cast, I agree MISRA-C:2004 should not warn here at all, but I'm not surprised it does; this is beyond the original scope of MISRA-C:2004's capacity.

            syncuser TI User
            syncuser TI User
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: