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
.clang-tidy
Checks: > -*, bugprone-bool-pointer-implicit-conversion, bugprone-branch-clone, bugprone-copy-constructor-init, bugprone-dangling-handle, bugprone-dynamic-static-initializers, bugprone-forwarding-reference-overload, bugprone-inaccurate-erase, bugprone-incorrect-roundings, bugprone-integer-division, bugprone-macro-parentheses, bugprone-misplaced-operator-in-strlen-in-alloc, bugprone-misplaced-widening-cast, bugprone-move-forwarding-reference, bugprone-parent-virtual-call, bugprone-string-constructor, bugprone-string-integer-assignment, bugprone-suspicious-enum-usage, bugprone-suspicious-semicolon, bugprone-suspicious-string-compare, bugprone-terminating-continue, bugprone-throw-keyword-missing, bugprone-undefined-memory-manipulation, bugprone-unhandled-self-assignment, bugprone-unused-raii, bugprone-unused-return-value, bugprone-use-after-move, bugprone-virtual-near-miss, cert-dcl50-cpp, cert-err34-c, cert-err58-cpp, cert-err60-cpp, clang-analyzer-cplusplus*, clang-analyzer-core*, clang-analyzer-nullability*, cert-*, bugprone-* cppcoreguidelines-init-variables, cppcoreguidelines-narrowing-conversions, cppcoreguidelines-pro-bounds-constant-array-index, cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-type-member-init, cppcoreguidelines-pro-type-vararg, cppcoreguidelines-slicing, google-build-namespaces, google-explicit-constructor, google-global-names-in-headers, google-readability-casting, google-runtime-int, hicpp-exception-baseclass, hicpp-multiway-paths-covered, llvm-include-order, llvm-namespace-comment, misc-misplaced-const, misc-redundant-expression, misc-static-assert, misc-unconventional-assign-operator, misc-unused-using-decls, modernize-concat-nested-namespaces, modernize-deprecated-headers, modernize-loop-convert, modernize-make-shared, modernize-make-unique, modernize-raw-string-literal, modernize-redundant-void-arg, modernize-replace-auto-ptr, modernize-replace-random-shuffle, modernize-shrink-to-fit, modernize-unary-static-assert, modernize-use-bool-literals, modernize-use-default-member-init, modernize-use-emplace, modernize-use-equals-default, modernize-use-equals-delete, modernize-use-noexcept, modernize-use-nullptr, modernize-use-override, modernize-use-transparent-functors, modernize-use-using, performance-faster-string-find, performance-for-range-copy, performance-implicit-conversio-in-loop, performance-inefficient-algorithm, performance-inefficient-vector-operation, performance-move-const-arg, performance-move-constructor-init, performance-no-automatic-move, performance-noexcept-move-constructor, performance-trivially-destructible, performance-unnecessary-copy-initialization, performance-unnecessary-value-param, readability-avoid-const-params-in-decls, readability-const-return-type, readability-container-size-empty, readability-convert-member-functions-to-static, readability-delete-null-pointer, readability-deleted-default, readability-else-after-return, readability-function-size, readability-implicit-bool-conversion, readability-inconsistent-declaration-parameter-name, readability-make-member-function-const, readability-misleading-indentation, readability-non-const-parameter, readability-qualified-auto, readability-redundant-control-flow, readability-redundant-declaration, readability-redundant-function-ptr-dereference, readability-redundant-member-init, readability-redundant-preprocessor, readability-redundant-smartptr-get, readability-redundant-string-cstr, readability-redundant-string-init, readability-simplify-boolean-expr, readability-simplify-subscript-expr, readability-string-compare WarningsAsErrors: '' HeaderFilterRegex: '' AnalyzeTemporaryDtors: false FormatStyle: none User: Linux CheckOptions: - key: bugprone-misplaced-widening-cast.CheckImplicitCasts value: 'false' - key: bugprone-string-constructor.LargeLengthThreshold value: '8388608' - key: modernize-loop-convert.MinConfidence value: reasonable - key: modernize-replace-auto-ptr.IncludeStyle value: llvm - key: performance-move-const-arg.CheckTriviallyCopyableMove value: 'false' - key: readability-implicit-bool-conversion.AllowIntegerConditions value: 1 - key: readability-implicit-bool-conversion.AllowPointerConditions value: 1 ...
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.