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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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..
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
hmm.. so you suggest something like this..
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?
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
maybe the cmake developers solved the problem somehow.
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.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..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
cppcheck command:
compile_commands.json:
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
Glad to hear it worked! This might be something to hint about in the manual or something.