Script scripts/static_analysis.py is a wrapper around different static code analysis tools allowing you to easily check your code for possible issues. There are two supported modes of work:
- check a single commit
- check entire source code repository (or its sub-tree)
Currently, only clang-tidy tool is supported, other tools (such as cppcheck) may get supported in the future.
Script uses the following external tools:
- git binary
- clang-tidy binary
- clang-tidy-diff.py script
- compile_commands.json file (see "Prerequisites" below)
clang-tidy-diff.py script is used when checking a single commit, because it allows us to only check the modified lines of the patch.
To enable clang-tidy static code analysis, you need to configure the build to generate compile_commands.json file.
Example commands:
CC=clang CXX=clang++ cmake <path_to_src> -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -DWITH_SYSTEM_LIBS=1 -DWITH_ZLIB=bundled -DWITH_FIDO=bundled -DWITH_PROTOBUF=bundled -DWITH_ZSTD=bundled -DWITH_EDITLINE=bundled -DWITH_LZ4=bundled
make -j$(nproc) clang_tidy_prerequisites
Script must be invoked from a working directory being within the source tree or else we may not be able to detect some parameters. Script writes the analysis results to the standard output, so you may need to redirect the output to file for permanent storage. Warnings, errors, progress info or debug output are being written to the standard error stream.
Some usage examples are given below.
python3 ./scripts/static_analysis.py -j 4 --tree --path ./bld > results.txt
python3 ./scripts/static_analysis.py --tree --clang-tidy=/usr/bin/clang-tidy > results.txt
python3 ./scripts/static_analysis.py --tree --path ../../bld > results.txt
python3 ./scripts/static_analysis.py --tree --scan-root=/work/mysql/sql > results.txt
python3 ./scripts/static_analysis.py --commit > results.txt
python3 ./scripts/static_analysis.py --commit HEAD~2 > results.txt
python3 ./scripts/static_analysis.py --commit HEAD~2 --clang-tidy-diff=/usr/share/clang/clang-tidy-diff.py > results.txt
After satisfying the prerequisites, instead of the script you can also possibly run the clang-tidy manually with (assuming being run from within the build folder).
/opt/llvm-17.0.1/bin/run-clang-tidy -clang-tidy=/opt/llvm-17.0.1/bin/clang-tidy -j $(nproc) -quiet -p . > results.txt
git diff HEAD~ -U0 -- '.cc' '.cpp' '.c++' '.cxx' '.c' '.cl' '.h' '.hpp' ':!extra' | python3 clang-tidy-diff.py -timeout 600 -path . -j=4 -p1 -extra-arg='-ferror-limit=0'
For detailed list of supported parameters, run:
python3 ./scripts/static_analysis.py --help