Menu

File Guard False Positive - MISRA

Amir
2020-04-17
2020-04-19
  • Amir

    Amir - 2020-04-17

    Hello,
    I'm getting a false positive on file guard. I have a file, Events.h with the follow file guard:

    #ifndef Events_H
    #define Events_H
    

    and I'm getting the MISRA error:
    ..\Sources\Events.h:29:0: style: Required. #define and #undef shall not be used on a reserved identifier or reserved macro name. [misra-c2012-21.1]

    Thanks,
    Amir

     

    Last edit: Amir 2020-04-17
  • Georgiy Komarov

    Georgiy Komarov - 2020-04-17

    Which version of Cppcheck do you use?

    I can't reproduce this bug with the following code:

    #ifndef Events_H
    #define Events_H
    int main(int argc, char *argv[])
    {
        return 0;
    }
    #endif
    

    Running misra.py from the latest Cppcheck from git master gives me the following output:

    Checking addons/test/misra/21-1.c.dump...
    Checking addons/test/misra/21-1.c.dump, config ...[addons/test/misra/21-1.c:3] (style) misra violation (use --rule-texts=<file> to get proper output) (Undefined) [misra-c2012-2.7]
    MISRA rules violations found:
            Undefined: 1
    MISRA rules violated:        misra-c2012-2.7 (-): 1
    

    Edit: I also found this line in your output: ..\Sources\Events.h:29:0:. , which indicates that there is an error in 29 line. Are you sure that this error caused by include guards?

     

    Last edit: Georgiy Komarov 2020-04-17
  • Amir

    Amir - 2020-04-17

    Thanks for the quick reply. There are comments above the file guards which is why the error is occurring at line 29. This file is auto-generated. Here is what it looks like:

    /* ###################################################################
    **     Filename    : Events.h
    **     Project     : kds_volta_mcu
    **     Processor   : SKEAZN64MLC2
    **     Component   : Events
    **     Version     : Driver 01.00
    **     Compiler    : GNU C Compiler
    **     Date/Time   : 2020-03-26, 14:07, # CodeGen: 0
    **     Abstract    :
    **         This is user's event module.
    **         Put your event handler code here.
    **     Contents    :
    **         Cpu_OnNMIINT - void Cpu_OnNMIINT(void);
    **
    ** ###################################################################*/
    /*!
    ** @file Events.h
    ** @version 01.00
    ** @brief
    **         This is user's event module.
    **         Put your event handler code here.
    */         
    /*!
    **  @addtogroup Events_module Events module documentation
    **  @{
    */         
    
    #ifndef __Events_H
    #define __Events_H
    /* MODULE Events */
    ...
    ...
    #endif
    

    Version:
    Cppcheck 1.90

     

    Last edit: Amir 2020-04-17
  • Georgiy Komarov

    Georgiy Komarov - 2020-04-18

    In this case, the addon is absolutely right. This is a violation of the rule 21.1.

    According to MISRA document, #define and #undef directives should not be used with the following identifiers:

    • Identifiers or macro names beginning with an underscore;
    • Identifiers in file scope described in Section 7, “Library”, of The Standard;
    • Macro names described in Section 7, “Library”, of The Standard as being defined in a standard header.

    C99 standard (ISO/IEC 9899:TC3) declares reserved identifiers in ch. 7.1.3 as the following:

    — All identifiers that begin with an underscore and either an uppercase letter or another
    underscore are always reserved for any use.

    The rationale is that reserved identifiers are used internally in the implementation of standard library, so you should avoid using them if possible.

     
  • Amir

    Amir - 2020-04-19

    Thank you! That clears it up. I've tried to suppress the error but can't seem to do so inline:

    // cppcheck-suppress misra-c2012-21.1
    #ifndef __Events_H
    // cppcheck-suppress misra-c2012-21.1
    #define __Events_H
    /* MODULE Events */
    

    Regards,
    Amir

     

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.