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/TMS320F28379D: Conversion loses integer precision warning

Part Number: TMS320F28379D


Tool/software: TI C/C++ Compiler

Hi!

Given the following code, gcc will produce a warning when using the -Wconversion flag since the integer conversion causes a precision loss.

uint32_t a = 0;
uint16_t b = a;

The C2000 complier produces no such warning. Is there a way to enable this?

Thanks! :)

  • This is a violation of MISRA rule 10.1 ...

    Rule 10.1 (required)    The value of an expression of integer type shall not
                            be implicitly converted to a different underlying type
                            if:
                            a) it is not a conversion to a wider integer type of
                               the same signedness, or
                            b) the expression is complex, or
                            c) the expression is not constant and is a function
                               argument, or
                            d) the expression is not constant and is a return
                               expression.

    So I tried adding the build option --check_misra=10.1.  I found that, if variables a and b are local to the function, and written exactly as you did ...

    Carl Friess said:
    uint32_t a = 0;
    uint16_t b = a;

    then no diagnostic is issued.  If the variables are global, or the assignments are written separately, then you see a diagnostic.  That's wrong, so I filed CODEGEN-4712 in the SDOWP system to have this investigated. You are welcome to follow it with the SDOWP link below in my signature.

    When you do see a diagnostic, it occurs for both lines ...

    % cl2000 --verbose_diagnostics --check_misra=10.1 file.c
    "file.c", line 11: warning: (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
         a = 0;
             ^
    
    "file.c", line 12: warning: (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
         b = a;
             ^

    You can avoid the first diagnostic by adding the suffix u, which changes the type of the integer constant 0 to unsigned.

    a = 0u;

    Thanks and regards,

    -George