Menu

misra 2.5 doesn't handle macro definitions with #ifdefines

Dzid
2024-06-13
2024-07-26
  • Dzid

    Dzid - 2024-06-13

    1.c:

    #ifdef TEST1
        #define A 1
    #else
        #define B 2
    #endif
    
    #ifdef TEST2
        #include "1.h"
    #endif
    
    
    int main(void){
        #ifdef USE_A
            return A;
        #else
            return B;
        #endif
    }
    
    // of all the examples only this one is not reported
    #ifndef TEST3
        #define TEST3
    #endif
    

    1.h:

    #define C 1
    

    ./cppcheck --enable=all --addon=misra 1.c -DTEST1

    I get:

    Checking 1.c ...
    Checking 1.c: TEST1=1...
    1.c:4:0: style: misra violation (use --rule-texts=<file> to get proper output) [misra-c2012-2.5]
        #define A 1
    ^
    1.c:6:0: style: misra violation (use --rule-texts=<file> to get proper output) [misra-c2012-2.5]
        #define B 2
    ^
    1.h:3:0: style: misra violation (use --rule-texts=<file> to get proper output) [misra-c2012-2.5]
        #define C 1
    ^
    nofile:0:0: information: Active checkers: 295/1002 (use --checkers-report=<filename> to see details) [checkersReport]
    

    I would expect none of them be a violation.

    The only one that is not reported is the TEST3 which is a special case used for header guards discussed here .

     

    Last edit: Dzid 2024-07-26
    • Daniel Marjamäki

      If this was your explicit real world code then I would argue that the code is not well written. Why have two separate macros TEST1 and USE_A that must always be set to the same value otherwise the compiler will fail.

       
      • Dzid

        Dzid - 2024-07-26

        I am so sorry. I don't know why this forum doesn't auto-subscribe to threads.

        It was some time and I am trying to find the real world code. But by simply looking at my examples without cross-checking with the real code I tend to agree with you.

        Except for unused #define C 1 violation. If the header is not included, I don't think it should be even checked.

         

        Last edit: Dzid 2024-07-26
  • Daniel Marjamäki

    Do you have some proof that these violations are technically false positives? According to misra. Do you see some related discussion in the misra forum for instance?

     
    • Daniel Marjamäki

      For information PC-lint Plus also seem to write such warnings. You can open URL https://pclintplus.com/gmp/demo/ and try this code:

      #define A 1  // <- PC-lint reports 2.5 here
      
      int main(void){
          #ifdef USE_A
              return A;
          #endif
          return 0;
      }
      
       

      Last edit: Daniel Marjamäki 2024-06-14

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.