How to use the command cppcheck (with examples)
Cppcheck is a static analysis tool for C/C++ code that focuses on detecting bugs that compilers normally do not detect. It is used to analyze code and identify potential errors before the code is compiled, helping developers write higher quality and more efficient code.
Use case 1: Recursively check the current directory, showing progress on the screen and logging error messages to a file
Code:
cppcheck . 2> cppcheck.log
Motivation: When working on a project, it is important to regularly check the entire codebase for potential bugs. This use case allows you to recursively check the current directory, meaning all files and subdirectories within it will be analyzed. By redirecting the error messages to a log file, you can easily review and address any issues later.
Explanation:
.
: Specifies the current directory as the target for analysis.2> cppcheck.log
: Redirects the error messages to a file called “cppcheck.log” using stderr redirection.
Example output:
Checking main.cpp ...
[main.cpp:10]: (style) The function 'main' should have a return type of 'int'.
Checking utils.cpp ...
[utils.cpp:15]: (error) Array 'arr' accessed at index 5, which is out of bounds.
...
Use case 2: Recursively check a given directory, and don’t print progress messages
Code:
cppcheck --quiet path/to/directory
Motivation: Sometimes, it is more convenient to hide the progress messages when recursively checking a directory, especially when dealing with large codebases. This use case allows you to perform the analysis silently.
Explanation:
--quiet
: Disables the printing of progress messages during analysis.path/to/directory
: Specifies the directory to be analyzed.
Example output:
[utils.cpp:15]: (error) Array 'arr' accessed at index 5, which is out of bounds.
[utils.cpp:27]: (performance) Variable 'unusedVar' is assigned a value that is never used.
...
Use case 3: Check a given file, specifying which tests to perform (by default only errors are shown)
Code:
cppcheck --enable=error|warning|style|performance|portability|information|all path/to/file.cpp
Motivation: Not all errors, warnings, or other types of potential issues are equally critical for every project. This use case allows you to customize the tests to be performed based on your project’s specific requirements.
Explanation:
--enable=error|warning|style|performance|portability|information|all
: Specifies the types of tests to enable during analysis. Replace “error|warning|style|performance|portability|information|all” with the desired combination of types.path/to/file.cpp
: Specifies the file to be analyzed.
Example output:
[utils.cpp:27]: (performance) Variable 'unusedVar' is assigned a value that is never used.
...
Use case 4: List available tests
Code:
cppcheck --errorlist
Motivation: If you are unsure about the available tests that can be enabled or suppressed, this use case allows you to retrieve a complete list of the available tests provided by cppcheck.
Explanation:
--errorlist
: Displays a list of available tests.
Example output:
List of available tests:
error
warning
style
performance
portability
information
Use case 5: Check a given file, ignoring specific tests
Code:
cppcheck --suppress=test_id1 --suppress=test_id2 path/to/file.cpp
Motivation: Sometimes, certain tests may not be relevant or may produce false positives for a specific code file. This use case allows you to selectively ignore specific tests for a given file.
Explanation:
--suppress=test_id1 --suppress=test_id2
: Specifies the test IDs to be suppressed. Replace “test_id1” and “test_id2” with the actual test IDs to be ignored.path/to/file.cpp
: Specifies the file to be analyzed.
Example output:
[utils.cpp:27]: (performance) Variable 'unusedVar' is assigned a value that is never used.
...
Use case 6: Check the current directory, providing paths for include files located outside it
Code:
cppcheck -I include/directory_1 -I include/directory_2 .
Motivation: When analyzing code, it is important to ensure all required include files are accessible. This use case allows you to specify additional include directories for files located outside the current directory, such as external libraries.
Explanation:
-I include/directory_1 -I include/directory_2
: Specifies the paths for additional include directories. Replace “include/directory_1” and “include/directory_2” with the actual paths..
: Specifies the current directory as the target for analysis.
Example output:
[main.cpp:15]: (error) Invalid argument in function call.
...
Use case 7: Check a Microsoft Visual Studio project (.vcxproj) or solution (.sln)
Code:
cppcheck --project=path/to/project.sln
Motivation: If you are working on a Microsoft Visual Studio project or solution, this use case allows you to analyze the entire project or solution for potential issues using cppcheck.
Explanation:
--project=path/to/project.sln
: Specifies the path to the Visual Studio project (.vcxproj) or solution (.sln) to be analyzed.
Example output:
Checking Project1.cpp ...
[Project1.cpp:15]: (error) Expression is always true.
Checking Project2.cpp ...
[Project2.cpp:27]: (warning) Uninitialized variable 'unusedVar'.
...
Conclusion:
Cppcheck is a powerful static analysis tool for C/C++ code that helps developers detect bugs and potential issues early in the development process. By understanding and utilizing the various use cases provided by cppcheck, developers can significantly improve the quality and performance of their code.