Cppcheck C/C++ static analyser

Version 2.22 · source

Start here

cppcheck <path>
Check files or a folder
cppcheck --enable=all <path>
Turn on more checks
cppcheck -I <dir> <path>
Add an include path
cppcheck --project=compile_commands.json
Check what you build
cppcheck -j 4 <path>
Use 4 threads
cppcheck --std=c++17 <path>
Pick the language standard
cppcheck --suppress=<id> <path>
Silence one message
cppcheck --error-exitcode=1 <path>
Fail the build on findings

Concepts

  1. Sources files or project
  2. Configurations #ifdef variants
  3. Checks --enable ids
  4. Findings text / XML

Running

cppcheck <file>
Check one file
cppcheck <dir>
Check all sources in a folder, recursively
cppcheck -j <n> <dir>
Check with n threads
cppcheck -q <dir>
Quiet: print only findings
cppcheck -i <path> <dir>
Skip a file or folder
Example: -isrc/third_party. Matches by path prefix or pattern.
cppcheck <dir> --file-filter=<pattern>
Check only matching files
cppcheck --language=c++ <file>
Force C or C++ for all files
cppcheck --clang <file>
Use the experimental Clang parser
cppcheck --showtime=summary <dir>
Show where the time goes

Checks and levels

cppcheck --enable=warning <dir>
Add warnings
cppcheck --enable=style <dir>
Add style findings
cppcheck --enable=performance,portability <dir>
Add several groups (comma list)
cppcheck --enable=all <dir>
Enable every group
all includes unusedFunction, which gives false positives unless you pass the whole program.
cppcheck --enable=unusedFunction <dir>
Find unused functions
cppcheck --enable=missingInclude <dir>
Report unresolved includes
cppcheck --disable=<id> <dir>
Switch a group off again
cppcheck --check-level=exhaustive <dir>
Deeper, slower analysis
cppcheck --check-level=reduced <dir>
Faster, less data flow
cppcheck --inconclusive <dir>
Also report uncertain findings
cppcheck --check-config <dir>
Show missing includes and config problems

Configuration

cppcheck -I <dir> <path>
Add an include directory
cppcheck -D<name> <path>
Define a macro, check only that config
cppcheck -D<name> --force <path>
Check all configs that contain the define
cppcheck -U<name> <path>
Treat a macro as undefined
cppcheck --max-configs=<n> <path>
Limit #ifdef configurations tried
cppcheck --force <path>
Check every configuration (can be slow)
cppcheck --std=c++20 <path>
Set the C++ standard
C++: c++03 to c++26. C: c89, c99, c11, c17, c23.
cppcheck --std=c11 <file>
Set the C standard
cppcheck --platform=<name> <path>
Set target types and sizes
Builtin names include unix32, unix64, win32A, win32W, win64. Custom platforms use an XML file.
cppcheck --library=<name> <path>
Load a library config (posix, qt, ...)
cppcheck --check-library <path>
Hint at missing library config

Projects and incremental runs

cppcheck --project=compile_commands.json
Use a compilation database
cppcheck --project=<file>.sln
Import a Visual Studio solution
cppcheck --project=<file>.vcxproj
Import a Visual Studio project
cppcheck --project=<file>.cppcheck
Use a GUI project file
cppcheck --project=<file>.sln --project-configuration="Release|Win32"
Check one VS configuration
cppcheck --project=compile_commands.json -i<dir>
Exclude a folder from a project
cppcheck --cppcheck-build-dir=<dir> <path>
Cache results, enable whole-program checks
Reuse the same folder between runs for incremental analysis. Needed for cross-file checks with -j.
cppcheck --max-ctu-depth=<n> <path>
Whole-program analysis depth

Output

cppcheck --template=gcc <path>
GCC-style lines for editors
cppcheck --template=vs <path>
Visual Studio-style lines
cppcheck --template='{file}:{line}: {message}' <path>
Custom line format
Other fields: {callstack}, {code}, {inconclusive:text}, {remark}. Use \n and \t for breaks.
cppcheck --template-location='{file}:{line}: {info}' <path>
Format extra location lines
cppcheck --xml <path> 2> <out.xml>
XML report (written to stderr)
cppcheck-htmlreport --file=<out.xml> --report-dir=<dir> --source-dir=.
Turn XML into an HTML report
Needs Python and pygments.
cppcheck -rp <path>
Print paths relative to a base
cppcheck --errorlist
List every check id (XML)
cppcheck --error-exitcode=<n>
Exit with n when findings exist
cppcheck --exitcode-suppressions=<file>
List findings that do not fail the run

Suppressions

cppcheck --suppress=<id> <path>
Suppress an id everywhere
cppcheck --suppress=<id>:<file> <path>
Suppress an id in one file
Wildcards *, ** and ? work in the file part.
cppcheck --suppress=<id>:<file>:<line> <path>
Suppress at one line
cppcheck --suppressions-list=<file> <path>
Read suppressions from a text file
One per line, same format as --suppress. # and // start comments.
cppcheck --suppress-xml=<file> <path>
Read suppressions from XML
cppcheck --inline-suppr <path>
Honour cppcheck-suppress comments
// cppcheck-suppress <id>
Suppress on the next or same line
// cppcheck-suppress [<id1>, <id2>]
Suppress several ids at once
// cppcheck-suppress <id> symbolName=<name>
Limit to one symbol
// cppcheck-suppress-begin <id> ... -end <id>
Suppress a block
// cppcheck-suppress-file <id>
Suppress for the whole file
// cppcheck-suppress-macro <id>
Suppress where a macro is used
cppcheck --enable=all --inline-suppr <path>
Also report unused suppressions
The unmatchedSuppression id is part of --enable=all.

Addons

cppcheck --addon=<name>.py <path>
Run a Python addon
cppcheck --addon=<file>.json <path>
Run an addon with settings
JSON keys: script, args, ctu.
cppcheck --addon=misra.json --enable=style <file>
Check MISRA C rules
Open-source coverage is partial. Rule texts come from MISRA.
cppcheck --addon=namingng.py <path>
Check naming conventions

Recipes

CI gate on a CMake project
cmake -B build \  -DCMAKE_EXPORT_COMPILE_COMMANDS=ONcppcheck --project=build/compile_commands.json \  -ibuild --enable=warning,performance \  --error-exitcode=1 -j 4 --inline-suppr
Fast incremental runs with a cache
mkdir -p .cppcheck-cachecppcheck --cppcheck-build-dir=.cppcheck-cache \  --enable=style -j 4 -I include src/
Editor-friendly output with a suppression file
# suppressions.txt# missingIncludeSystem# unusedFunction:src/api.ccppcheck --enable=all --template=gcc \  --suppressions-list=suppressions.txt src/
Baseline an old code base, then HTML report
cppcheck --enable=style --xml src 2> baseline.xmlpython3 generate-baseline-suppressions.py baseline.xml suppressions.xmlcppcheck --enable=style --xml --suppress-xml=suppressions.xml src 2> new.xmlcppcheck-htmlreport --file=new.xml --report-dir=report --source-dir=.