Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CMake's own Cppcheck mechanism (DO NOT MERGE) #222

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

mikkoi
Copy link
Contributor

@mikkoi mikkoi commented Sep 19, 2019

Enable only when CMAKE_BUILD_TYPE is Debug.
Thus forcing the developer to see the output
if cppcheck program is available.

Signed-off-by: Mikko Johannes Koivunalho [email protected]

@mikkoi
Copy link
Contributor Author

mikkoi commented Sep 19, 2019

Travis needs to install 'cppcheck' at the beginning of the run. Example: http://cppcheck.sourceforge.net/

@mikkoi
Copy link
Contributor Author

mikkoi commented Sep 19, 2019

N.B. This change does not cause a CI build to fail if Cppcheck finds problems. But it does show them.

@mikkoi
Copy link
Contributor Author

mikkoi commented Sep 24, 2019

The current parameters are:
--quiet
--std=c89
--enable=warning,style,performance,portability

If we want to make the build fail, we just add parameter --error-exitcode=1.

CMakeLists.txt Outdated
find_package(Cppcheck)
if(Cppcheck_FOUND)
set(CMAKE_C_CPPCHECK
"${Cppcheck_EXECUTABLE};--quiet;--std=c89;--enable=warning,style,performance,portability")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is another PR for C99. Should this be C99 instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in the newest commit "Use CMAKE_C_STANDARD with Cppcheck run".
Thanks for pointing that out.

@brarcher
Copy link
Contributor

Is the goal to run cppcheck during the CMake build, and fail it if an issue is found? Should that be a build step instead of something else? My concern is that a scan with cppcheck would work today and the build would work, and in the future if cppcheck updates and find an issue it would prevent existing builds for working.

Instead, should this scan be a part of the Travis-CI automated build? If cppcheck updates and finds new issues it can be handled in a way that does not break clients of Check. It would also help prevent new breakage from being introduced to Check. For example, the Travis-CI build is using scan-build to find issues.

@mikkoi mikkoi force-pushed the add-cppcheck-to-cmake branch from 096909e to ea4ef37 Compare October 28, 2019 09:43
@mikkoi
Copy link
Contributor Author

mikkoi commented Oct 28, 2019

The goal is to run Cppcheck during CMake build when developing (when the build type is "Debug"). I have configured the build so that Cppcheck will not fail the build if errors are found. So the purpose is merely to warn or inform the developer that problems or weaknesses have been found. It's like using the -Wall (warning) flags in GCC.

This should also keep the build future proof. Cppcheck has the following parameter:
--error-exitcode= If errors are found, integer [n] is returned instead of
the default '0'. '1' is returned
if arguments are not valid or if no input files are
provided. Note that your operating system can modify
this value, e.g. '256' can become '0'.
I added this parameter with value '0' so Cppcheck will never break the build.

This scan is also part of the build in Travis-CI. All Travis builds are 'Debug' unless otherwise defined.

@brarcher
Copy link
Contributor

So the purpose is merely to warn or inform the developer that problems or weaknesses have been found.

I have configured the build so that Cppcheck will not fail the build if errors are found.

Are these two competing points? I can see that if a new contributor to the project submits a change, the output of this new check may go unnoticed by both the contributor (may not notice the messages) and myself (as the build did not break). The check will be most valuable if it helps me as the maintainer from either introducing or accepting changes with new issues.

I'm find with using cppcheck to provide more checks on the codebase. I think, though, that they would be better if they broke Check's CI build without also breaking other's builds. If an issue is found someone could follow the same steps locally and reproduce the issue. I'm not sure if adding a check before unit tests are run which ignores the found issues (or only reports them to stdout) helps prevent new issues from being introduced.

What are your thoughts?

@mikkoi
Copy link
Contributor Author

mikkoi commented Oct 30, 2019

I pushed new commits which can solve that issue.
The CMake option ENABLE_TREAT_WARNINGS_AS_ERRORS in OFF by default, i.e. Cppcheck only reports. But when building in Travis we enable it and the build fails if errors are detected. Unfortunately, there is no easy way (at present, anyway) to print out all the errors and then fail. Cppcheck will break the build immediately at the first detected fault.

I just realized there is an awful lot of false positives among the tests, mostly concerning style as tests are often written in a verbose manner.

If an issue is found someone could follow the same steps locally and reproduce the issue.

Up to a certain point because Cppcheck might not be present in some systems or have different version.

I am thinking about using the same CMake option ENABLE_TREAT_WARNINGS_AS_ERRORS with the compiler warnings.

@mikkoi
Copy link
Contributor Author

mikkoi commented Nov 1, 2019

Interesting, Travis build is not failing even when errors are detected. Locally it worked well. Needs more investigation...

Enable only when CMAKE_BUILD_TYPE is Debug.
Thus forcing the developer to see the output
if cppcheck program is available.

Signed-off-by: Mikko Johannes Koivunalho <[email protected]>
Signed-off-by: Mikko Johannes Koivunalho <[email protected]>
Signed-off-by: Mikko Johannes Koivunalho <[email protected]>
Signed-off-by: Mikko Johannes Koivunalho <[email protected]>
Signed-off-by: Mikko Johannes Koivunalho <[email protected]>
E.g. cmake -D Cppcheck_ROOT=<cppcheck_install_root>
(install_root is the directory which contains
dirs bin, share and possible others.

Signed-off-by: Mikko Johannes Koivunalho <[email protected]>
@mikkoi mikkoi force-pushed the add-cppcheck-to-cmake branch from e6469d8 to f20e941 Compare November 1, 2019 20:16
@mikkoi mikkoi changed the title Use CMake's own Cppcheck mechanism Use CMake's own Cppcheck mechanism (DO NOT MERGE) Mar 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants