clang-tidy

ref: https://clang.llvm.org/extra/clang-tidy/
Clang tidy version 10 should be already available on your AWS instance under the folder /opt/llvm-10/bin/.

Compiling clag-tidy 2.15 from source

git clone https://github.com/llvm/llvm-project.git

cd llvm-project
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" -D CMAKE_C_COMPILER="/opt/llvm-10/bin/clang" -D CMAKE_CXX_COMPILER="/opt/llvm-10/bin/clang++" ../llvm
make  clang-tidy

Setup .clang-tidy file

in your base folder add a file to specify the rules called .clang-tidy, this file begin with β€” and end with 3 dots …
it can also be downloaded from the following location: https://stash.orcsoftware.com/projects/PROEM/repos/sprj-6846_internal_shared-tbricks-apps-code/browse/dotfiles/.clang-tidy


Usage

clang-tidy requires a JSON Compilation Database file, _compile_commands.json.
_you can generate it with the following commmand.

gmake db.create -j8 RECURSIVE=YES

or in case you have a cmake project with the following parameter:

cmake -S {code source path} -B {build folder} -DCMAKE_EXPORT_COMPILE_COMMANDS=ON

from your folder execute the command showed below:

/opt/llvm-10/share/clang/run-clang-tidy.py -j $(($(nproc)+1)) -style file -header-filter='^((?!(.*\/shared\/.*)).)*$' -clang-tidy-binary /opt/llvm-10/bin/clang-tidy

moreover you can easily setup two alias for having the following commands always available from your command line:

generate_compile_command
run_clang_tidy

Tips

additional parameters:

  • -fix :apply fix-its (don’t abuse it)
  • -style STYLE : The style of reformat code after applying fixes
  • -export-fixes filename: Create a yaml file to store suggested fixes in, which can be applied with clang-apply-replacements.

00 C++, dotfiles