diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -127,20 +127,25 @@ During submission of patches, arcanist will automatically run `arc lint` to enforce Bitcoin ABC code formatting standards, and often suggests changes. If code formatting tools do not install automatically on your system, you -will have to install clang-format-8, autopep8, flake8, phpcs and shellcheck. +will have to install clang-format-8, clang-tidy (version >=8), autopep8, flake8, +phpcs and shellcheck. -To install clang-format-8 on Ubuntu (>= 18.04+updates) or Debian (>= 10): +To install clang-format-8 and clang-tidy on Ubuntu (>= 18.04+updates) or Debian (>= 10): ``` -sudo apt-get install clang-format-8 +sudo apt-get install clang-format-8 clang-tidy-8 clang-tools-8 ``` -To install clang-format-8 on OSX (requires npm): + +If not available in the distribution, `clang-format-8` and `clang-tidy` can be +installed from https://releases.llvm.org/download.html or https://apt.llvm.org. + +For example, for macOS: ``` -npm install -g clang-format@1.2.4 +curl http://releases.llvm.org/8.0.0/clang+llvm-8.0.0-x86_64-apple-darwin.tar.xz | tar -xJv +ln -s $PWD/clang+llvm-8.0.0-x86_64-apple-darwin/bin/clang-format /usr/local/bin/clang-format +ln -s $PWD/clang+llvm-8.0.0-x86_64-apple-darwin/bin/clang-tidy /usr/local/bin/clang-tidy ``` -If not available in the distribution, clang-format-8 can be installed from -https://releases.llvm.org/download.html or https://apt.llvm.org To install autopep8, flake8 and phpcs on Ubuntu: ``` diff --git a/cmake/modules/ClangTidy.cmake b/cmake/modules/ClangTidy.cmake new file mode 100644 --- /dev/null +++ b/cmake/modules/ClangTidy.cmake @@ -0,0 +1,34 @@ +if(NOT CLANG_TIDY_EXE) + include(DoOrFail) + find_program_or_fail(CLANG_TIDY_EXE clang-tidy clang-tidy-10 clang-tidy-9 clang-tidy-8) + + execute_process( + COMMAND "${CLANG_TIDY_EXE}" -version + RESULT_VARIABLE CLANG_TIDY_VERSION_RESULT + OUTPUT_VARIABLE CLANG_TIDY_VERSION_OUTPUT + ) + + string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" CLANG_TIDY_VERSION "${CLANG_TIDY_VERSION_OUTPUT}") + if("${CLANG_TIDY_VERSION}" VERSION_LESS "8.0.0") + message(FATAL_ERROR "clang-tidy version >= 8 is required") + endif() + + message(STATUS "Using clang-tidy: ${CLANG_TIDY_EXE} (version ${CLANG_TIDY_VERSION})") +endif() + +set(CLANG_TIDY_ARGS "${CLANG_TIDY_EXE}" -fix) + +set(CMAKE_C_CLANG_TIDY ${CLANG_TIDY_ARGS}) +set(CMAKE_CXX_CLANG_TIDY ${CLANG_TIDY_ARGS}) + +# This is useful to run clang-tidy manually: +# clang-tidy -checks= -p compile_commands.json +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +# Disable for a single target +macro(target_disable_clang_tidy TARGET) + set_target_properties(${TARGET} PROPERTIES + C_CLANG_TIDY "" + CXX_CLANG_TIDY "" + ) +endmacro() diff --git a/contrib/gitian-descriptors/gitian-linux.yml b/contrib/gitian-descriptors/gitian-linux.yml --- a/contrib/gitian-descriptors/gitian-linux.yml +++ b/contrib/gitian-descriptors/gitian-linux.yml @@ -129,6 +129,7 @@ -DBUILD_BITCOIN_TX=OFF \ -DBUILD_BITCOIN_QT=OFF \ -DBUILD_LIBBITCOINCONSENSUS=OFF \ + -DENABLE_CLANG_TIDY=OFF \ -DENABLE_QRCODE=OFF \ -DENABLE_UPNP=OFF @@ -157,6 +158,7 @@ cmake -GNinja .. \ -DCMAKE_TOOLCHAIN_FILE=${SOURCEDIR}/cmake/platforms/${CMAKE_TOOLCHAIN_FILE[${i}]} \ -DCLIENT_VERSION_IS_RELEASE=ON \ + -DENABLE_CLANG_TIDY=OFF \ -DENABLE_REDUCE_EXPORTS=ON \ -DENABLE_STATIC_LIBSTDCXX=ON \ -DENABLE_GLIBC_BACK_COMPAT=ON \ diff --git a/contrib/gitian-descriptors/gitian-osx.yml b/contrib/gitian-descriptors/gitian-osx.yml --- a/contrib/gitian-descriptors/gitian-osx.yml +++ b/contrib/gitian-descriptors/gitian-osx.yml @@ -128,6 +128,7 @@ -DBUILD_BITCOIN_TX=OFF \ -DBUILD_BITCOIN_QT=OFF \ -DBUILD_LIBBITCOINCONSENSUS=OFF \ + -DENABLE_CLANG_TIDY=OFF \ -DENABLE_QRCODE=OFF \ -DENABLE_UPNP=OFF ninja package_source @@ -156,6 +157,7 @@ cmake -GNinja .. \ -DCMAKE_TOOLCHAIN_FILE=${SOURCEDIR}/cmake/platforms/${CMAKE_TOOLCHAIN_FILE[${i}]} \ -DCLIENT_VERSION_IS_RELEASE=ON \ + -DENABLE_CLANG_TIDY=OFF \ -DENABLE_REDUCE_EXPORTS=ON \ -DCMAKE_INSTALL_PREFIX=${INSTALLPATH} \ -DCCACHE=OFF \ diff --git a/contrib/gitian-descriptors/gitian-win.yml b/contrib/gitian-descriptors/gitian-win.yml --- a/contrib/gitian-descriptors/gitian-win.yml +++ b/contrib/gitian-descriptors/gitian-win.yml @@ -143,6 +143,7 @@ -DBUILD_BITCOIN_TX=OFF \ -DBUILD_BITCOIN_QT=OFF \ -DBUILD_LIBBITCOINCONSENSUS=OFF \ + -DENABLE_CLANG_TIDY=OFF \ -DENABLE_QRCODE=OFF \ -DENABLE_UPNP=OFF @@ -179,6 +180,7 @@ cmake -GNinja .. \ -DCMAKE_TOOLCHAIN_FILE=${SOURCEDIR}/cmake/platforms/${CMAKE_TOOLCHAIN_FILE[${i}]} \ -DCLIENT_VERSION_IS_RELEASE=ON \ + -DENABLE_CLANG_TIDY=OFF \ -DENABLE_REDUCE_EXPORTS=ON \ -DBUILD_BITCOIN_SEEDER=OFF \ -DCPACK_STRIP_FILES=ON \ diff --git a/contrib/teamcity/build_cmake.sh b/contrib/teamcity/build_cmake.sh --- a/contrib/teamcity/build_cmake.sh +++ b/contrib/teamcity/build_cmake.sh @@ -14,7 +14,7 @@ git clean -xffd read -a CMAKE_FLAGS <<< "${CMAKE_FLAGS}" -cmake -GNinja .. "${CMAKE_FLAGS[@]}" +cmake -GNinja .. -DENABLE_CLANG_TIDY=OFF "${CMAKE_FLAGS[@]}" # Run build ninja diff --git a/src/.clang-tidy b/src/.clang-tidy new file mode 100644 --- /dev/null +++ b/src/.clang-tidy @@ -0,0 +1,2 @@ +Checks: -*,boost-use-to-string +FormatStyle: file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -25,6 +25,7 @@ option(ENABLE_NOTIFICATIONS "Enable desktop notifications" ON) option(ENABLE_WERROR "Promote some compiler warnings to errors" OFF) option(START_WITH_UPNP "Make UPnP the default to map ports" OFF) +option(ENABLE_CLANG_TIDY "Enable clang-tidy checks for Bitcoin ABC" ON) # Disable what we do not need for the native build. include(NativeExecutable) @@ -34,8 +35,14 @@ "-DBUILD_BITCOIN_ZMQ=OFF" "-DENABLE_QRCODE=OFF" "-DENABLE_UPNP=OFF" + # Forward the current setting for clang-tidy + "-DENABLE_CLANG_TIDY=${ENABLE_CLANG_TIDY}" ) +if(ENABLE_CLANG_TIDY) + include(ClangTidy) +endif() + # Allow usage of sanitizers by setting ECM_ENABLE_SANITIZERS if(ENABLE_SANITIZERS) set(ECM_ENABLE_SANITIZERS ${ENABLE_SANITIZERS}) diff --git a/src/qt/CMakeLists.txt b/src/qt/CMakeLists.txt --- a/src/qt/CMakeLists.txt +++ b/src/qt/CMakeLists.txt @@ -240,7 +240,13 @@ target_include_directories(bitcoin-qt-protobuf PUBLIC ${Protobuf_INCLUDE_DIRS}) target_link_libraries(bitcoin-qt-protobuf ${Protobuf_LIBRARIES}) - + + # Don't run clang-tidy on generated files + if(ENABLE_CLANG_TIDY) + include(ClangTidy) + target_disable_clang_tidy(bitcoin-qt-protobuf) + endif() + target_link_libraries(bitcoin-qt-base OpenSSL::SSL bitcoin-qt-protobuf