Skip to content

Latest commit

 

History

History
113 lines (86 loc) · 4.28 KB

static_analysis.md

File metadata and controls

113 lines (86 loc) · 4.28 KB

Static code analysis (SCA) of MySQL source code

Overview

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.

Dependencies

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.

Running the script

Prerequisites

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 invocation

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.

Scan entire tree (using 4 jobs):

python3 ./scripts/static_analysis.py -j 4 --tree --path ./bld > results.txt

Scan entire tree using custom version of clang-tidy binary:

python3 ./scripts/static_analysis.py --tree --clang-tidy=/usr/bin/clang-tidy > results.txt

Scan entire tree with build path outside of source tree:

python3 ./scripts/static_analysis.py --tree --path ../../bld > results.txt

Scan part of the whole tree:

python3 ./scripts/static_analysis.py --tree --scan-root=/work/mysql/sql > results.txt

Scan single commit (no commit hash assumed HEAD):

python3 ./scripts/static_analysis.py --commit > results.txt
python3 ./scripts/static_analysis.py --commit HEAD~2 > results.txt

Scan commit using custom path of clang-tidy-diff.py script:

python3 ./scripts/static_analysis.py --commit HEAD~2 --clang-tidy-diff=/usr/share/clang/clang-tidy-diff.py > results.txt

Script alternatives

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).

Scan entire tree:

/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

Scan single commit (HEAD):

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'

Usage info

For detailed list of supported parameters, run:

python3 ./scripts/static_analysis.py --help