Menu

MISRA 2012 rule 5.5 preprocessor error

bill
2021-11-02
2021-11-05
  • bill

    bill - 2021-11-02

    test code:

    #ifdef ERRTEST
    #define testmacro   read
    #endif
    
    struct teststruct
    {
        int testmacro;
    };
    

    From logic, it should report 5.5 error when ERRTEST is defined. And no error when ERRTEST is not defined.
    The real test result is it report error when ERRTEST not defined, but it's ok when ERRTEST is defined.

    $ cppcheck --addon=misra -UERRTEST --cppcheck-build-dir=out test.c
    Checking test.c ...
    test.c:7:6: style: Identifiers shall be distinct from macro names (Required) [misra-c2012-5.5]
     int testmacro;
         ^
    
    $ cppcheck --addon=misra -DERRTEST --cppcheck-build-dir=out test.c
    Checking test.c ...
    Checking test.c: ERRTEST=1...
    

    $ cppcheck --version
    Cppcheck 2.6

     
  • Daniel Marjamäki

    If ERRTEST is defined then after preprocessing there will be no identifier testmacro in the code. As I read the "Amplification" there should therefore not be a warning if ERRTEST is defined.

    When ERRTEST is not defined.. maybe you are right I am not sure. There will be a "testmacro" identifier in the code after preprocessing. I am not sure what "regardless of whether the definition is still in force" in the amplification means.

     

    Last edit: Daniel Marjamäki 2021-11-05
  • bill

    bill - 2021-11-05

    I updated the test code as below, it report 5.5 violation in both -DERRTEST and -UERRTEST cases.

    #ifdef ERRTEST
    #define testmacro(x)   read(x)
    #endif
    
    struct teststruct
    {
        int testmacro;
    };
    

    for the "regardless of whether the definition is still in force when the identifier is declared", I guess it's for the case add "#undef" before identifier? because it said "STILL in force". In -UERRTEST case here, the macro is never in "in force" status.

     

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.