Menu

Cannot exclude directories that have been included (specifically using cppcheck with CMake)

2020-11-13
2020-11-16
  • Jack Heaffey

    Jack Heaffey - 2020-11-13

    Hi,

    I am invoking cppcheck via CMake (https://cmake.org/cmake/help/latest/prop_tgt/LANG_CPPCHECK.html).

    CMake adds all include paths provided to the compiler (gcc) to the include path list of cppcheck (-I ...). Consequently, cppcheck analyses 3rd party includes. I would like to configure cppcheck to NOT analyse 3rd party includes.

    I tried adding the 3rd party includes as "-i " arguments, but that did not work.

    It is stated here that "excludes" are not honoured if they are also "includes". It would be great if this issue were fixed, as I cannot use the handy CMake/cppcheck integration feature.

    A potential workaround is to add the 3rd party includes as suppressed files, ie "--suppress=*:/path/to/include",

    however cppcheck fails due to a "syntax error" when analysing a particular include (gsl/pointers). This syntax error goes away if I update cppcheck to the latest version, but I don't want my build system to be at the mercy of cppcheck's bugs when analysing 3rd party code - I must be able to fully ignore analysis of 3rd party files.

    I hope this all makes sense.

     

    Last edit: Jack Heaffey 2020-11-13
  • Daniel Marjamäki

    hmm.. so you suggest something like this..

    cppcheck -I /usr/include -i /usr/include foo.c
    

    Currently:
    If I am not mistaken the -i is not used during preprocessing. It is used to determine what source files to analyse. The -i will by design be ineffective for such command as foo,c is not in /usr/include.

    But well maybe we can adjust this somehow so -i will also remove include paths. Well -i will have two different behaviors then.

    A more gcc compliant way might be to add a -isystem-include flag maybe. With that you can specify that a include path is a system include path.

    We can probably do something just not sure what I think it best immediately. Do you want to argue for some solution. Or does anybody in the team have feedback?

     
  • Daniel Marjamäki

    I wonder how the cmake developers want this to work. It is not very unique that you have dependencies. Can you try to locate and ask the right guy?

     

    Last edit: Daniel Marjamäki 2020-11-14
    • Daniel Marjamäki

      maybe the cmake developers solved the problem somehow.

       
  • Jack Heaffey

    Jack Heaffey - 2020-11-14

    Hi Daniel,

    Thank you very much for your prompt response.

    I considered raising a question in the CMake GitLab project, however I realised that this issue is not specific to CMake. For example, other build systems can be configured to generate a compile_commands.json file (e.g. Bazel). The compile_commands.json file can then be used by cppcheck and the same issue would occur - "syntax errors" in 3rd party code would cause cppcheck to fail, therefore the "--suppress" workaround fails.

    I think your suggestion of configuring cppcheck to treat certain header files the same as "system includes" is the ideal solution. The addition of a -isystem-include flag would work very well, I think.

     
  • Daniel Marjamäki

    If you load a compile_commands.json I have the feeling that Cppcheck will skip -isystem-include paths. So feel free to try out that and see if that works out..

     
  • Jack Heaffey

    Jack Heaffey - 2020-11-14

    Sure, I'll give that a go. To clarify, do you mean:

    cppcheck --project=compile_commands.json -isystem-include path/to/third-party/

    or

    cppcheck --project=compile_commands.json -i path/to/third-party/

    ?

     

    Last edit: Jack Heaffey 2020-11-14
  • Daniel Marjamäki

    cppcheck command:

    cppcheck --project=compile_commands.json
    

    compile_commands.json:

    { "command": "g++ -isystem-include path/to/third-party/ foo.cpp" }
    
     
  • Jack Heaffey

    Jack Heaffey - 2020-11-16

    That seemed to work great (I had to use -isystem rather than -system-include)

    I can tell CMake to add certain headers to the g++ system path by adding the "SYSTEM" keyword to "target_include_directories". This seems to be a good workaround.

    Thank you very much for your help

     
  • Daniel Marjamäki

    Glad to hear it worked! This might be something to hint about in the manual or something.

     

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.