Menu

Automatic define checking seems to be wrong

Anonymous
2019-10-19
2019-11-06
  • Anonymous

    Anonymous - 2019-10-19

    Hi,

    I encoutered the following issue on my project. I have a file which is cheking which defines are currently set at library level. If a define is missing, during the library building, I need to raise an error. After some investigation, I figure out what was the cause of the problem with the following code:

    // #define LITTLE_ENDIAN
    // #define BIG_ENDIAN
    
    // Check defines
    #if !defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN)
    #error "One endianness format must be defined."
    #endif
    
    #if defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)
    #error "Both endianness format cannot be defined."
    #endif
    
    // Main function
    int main(int argc, char *argv[])
    {
        return 0;
    }
    

    I expected from the user, in order to build the library, to define one endianness format.

    When I used Cppcheck on that file, I get this error:

    Checking test.c ...
    Defines:
    Undefines:
    Includes:
    Platform:Native
    test.c:0:0: information: This file is not analyzed. Cppcheck failed to extract a valid configuration. The tested configurations have these preprocessor errors:
    '' : [test.c:6] #error "One endianness format must be defined."
    BIG_ENDIAN;LITTLE_ENDIAN : [test.c:10] #error "Not both endianness format can be defined." [noValidConfiguration]
    

    I would have expected from Cppcheck to analyse the code two times:

    • First with LITTLE_ENDIAN
    • Then with BIG_ENDIAN

    But maybe I'm doing something wrong.

    Regards,
    Jérémy

     
  • Daniel Marjamäki

    You did not do anything wrong. It seems this is too complex :-(

    I created this ticket: https://trac.cppcheck.net/ticket/9427

    You can fix the Cppcheck code if you want and ensure that Cppcheck can handle this better. See
    https://github.com/danmar/cppcheck/blob/master/lib/preprocessor.cpp#L425
    It's ~100-200 lines of code so it's not a huge algorithm.

    I guess you could update your code ...

    #ifdef BIG_ENDIAN
        #ifdef LITTLE_ENDIAN
            #error ....
        #endif
    #elif !defined(LITTLE_ENDIAN)
        #error .....
    #endif
    

    But well a fix for 9427 would be better of course :-)

     
  • Anonymous

    Anonymous - 2019-10-19

    Well, updating the code is not really possible. In fact, I show you an extract of the code. But there are other conditions like this one. And some of them imply more than two defines.

    I look at the code you point me, if I get time and if I understand well the code of Cppcheck I could try.
    Anyway, thank you to have reported this issue :)

     
  • Anonymous

    Anonymous - 2019-10-27

    In the meantime, is it possible to ignore this reported error through command line arguments ?
    I'm using Cppcheck in a CI process, and since these errors arose, this has become less usable in my daily development.

     
  • Daniel Marjamäki

    yes of course.

    I think you have two options;

    Try to manually specify the defines. Use -D and -U.

    integrate your build system in cppcheck somehow. Do you use some project files that can be imported with --project directly? Is it possible for you to generate a compile database? If your build system allows output of compile database directly that is easiest (cmake, etc). Otherwise you could try some compile database generator, I can't recommend any but something like bear or https://github.com/nickdiego/compiledb ..

     
  • Anonymous

    Anonymous - 2019-10-28

    I'm not sure if both of these options can be used on my side. My project is a library, and not a software. This library has a configuration file where the user can enable or disable features. Such as the configuration file "config.h" of MbedTLS.
    I'm using Cppcheck to check every define of the library (there are more than 50 possible defines). Of course, some defines cannot be set when others are already set, that's why I also have a file checking the combination of user defines.
    Cppcheck is used here to check the code with all the defines. And thus, I can't set a preset of defines.

     
  • Anonymous

    Anonymous - 2019-11-06

    In the meantime, I find a way to remove the check of the rule "noValidConfiguration" !

    I tried "--suppress=noValidConfiguration" and it worked.

     

Log in to post a comment.