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.

MSP430 V4.1.0 no compiler warning for comparison between signed and unsigned integers



 The TI compiler does not give a warning due to the comparison between signed and unsigned integers.

Why?

This same code compiled using other compilers such as the visual C compiler will generate a warning.

Is there a way to have the compiler generate a diagnostic warning message?

static unsigned long
foo(void) {
 int i=-1;
 unsigned int j = 100;
 volatile unsigned long gt = 0;
 volatile unsigned long lte = 0;

  while ( i != 0) {
    if ( i > j) {
       gt++;
    } else {
       lte++;
     }
     i--;
  }

  return gt;
}

 

The following web location lists all the available diagnostic warning/errors/ remarks messages output by the TI 3.2 version of the compiler. http://processors.wiki.ti.com/images/6/60/TICompiler_MSP430_ErrorList.pdf

There is no compiler warning for a "comparison between signed and unsigned integers" listed.

Just a recommendation, This list may prove to be more useful as an appendix in the slau132g "MSP430 Optimizing C/C++ compiler user's guide".

  • Why should you get a warning when comparing a value with a value? The comparison is perfectly valid.
    If you would do a "if j >=0" you'd get an 'expression is always true' or for "if (j<0)" an 'expression is always false' warning. Same for "if (i>32768)".
    IIRC, in your case, the compiler will cast both, i and j to a signed long and compare them. No problem, no warning needed. But even without the cast, the comparison is doable (just not as straight to implement)

  • Danimal said:
    There is no compiler warning for a "comparison between signed and unsigned integers" listed.

    Have you tried enabling checking of Misra Rules 10 - Arithmetic Type Conversions? Under CCS project properties -> Build -> MSP430 Compiler -> Advanced Options -> MISRA-C:2004.

    Previous experience is that the TI compilers themselves don't support as "strict" warnings as other C compilers. But since they support checking of Misra rules, the Misra rules can be enabled to get more "strict" warnings.

**Attention** This is a public forum