This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler: TI ARM CC MISRA checker & stdbool.h

Expert 1965 points

Tool/software: TI C/C++ Compiler

How if at all one can use the stdbool.h  type and yet avoid MISRA checker error while trying to assign the result of a logical expression to the bool? The following would bombs out from line 3 with  MISRA:

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

Description Resource Path Location Type
#1393-D (MISRA-C:2004 10.1/R) The value of an expression of integer type shall not be implicitly converted to a different underlying type if it is not a conversion to a wider integer type of the same signedness ..... line 58 C/C++ Problem

This is in the provided stdbool.h with TI compiler, and I'm wondering how you could use that boolean type at all, as type always seems requires downcast in bit size?

#include <_ti_config.h>

_TI_PROPRIETARY_PRAGMA("diag_push")
_TI_PROPRIETARY_PRAGMA("CHECK_MISRA(\"-19.4\")")
_TI_PROPRIETARY_PRAGMA("CHECK_MISRA(\"-19.11\")")

#ifndef __cplusplus

#define	false	0
#define	true	1

#define	bool	_Bool
#if __TI_PROPRIETARY_STRICT_ANSI_MACRO && 199901L > __STDC_VERSION__
typedef unsigned char _Bool;
#endif

#endif /* !__cplusplus */

_TI_PROPRIETARY_PRAGMA("diag_pop")

  • Thank you for submitting a test case.  I can reproduce the same result.  I failed to find a workaround.  So, I filed the entry EXT_EP-9938 to have this investigated.  You are welcome to follow it with the link below in my signature.

    Thanks and regards,

    -George

  • 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 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.

  • Archaeologist said:
    ou 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.

    Yea tried the latter, as well as adjusting _Bool definition;   thank you for the former  cast hint  (would be nice to just cast to the typedef of bool, but as you picked up that doesn't work ..)

    Also note from that header:

    #if __TI_PROPRIETARY_STRICT_ANSI_MACRO && 199901L > __STDC_VERSION__
    typedef unsigned char _Bool;
    #endif

    That typedef exists for pre-C99  and with TI strict ANSI enabled.   And the true / false are just ints literal,  independent of C version.

    This seems done to confuse that MISRA-2004 even more ,  with a special typedef uchar for bool, yet, as you pointed out,  ints for true & false values.   Requiring always to bit size downcast, getting MISRA enraged ..

  • I'm sorry you're having trouble with TI's implementation of MISRA-C:2004.  MISRA-C:2004 is meant to deal with conforming C89 programs. While our implementation will accept C99 programs, it was meant for conforming C89 programs, and it just doesn't have special knowledge of the boolean types.

  • > ...my_bool = (unsigned char)!!(true && false);

    Tried the suggested cast on a slightly more complex statement, with variables rather than constants/literals, and the cast doens't  work then to remove the warning.

    Anyway,  it's all sorted, and cleared, I shouldn't' be using the 2004 checker where I plan to use C99, and the TI's headers as much as they been analyzed are not for C99 mode.

    Thank you for the attention.