From 5de58c4ddd22d0eeec922b96049c0a2bd1d5c1fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Sun, 18 Apr 2021 21:51:47 +0200 Subject: [PATCH] added clang-tidy to CI (#3218) --- .clang-tidy | 3 ++- .github/workflows/clang-tidy.yml | 44 ++++++++++++++++++++++++++++++++ cmake/clang_tidy.cmake | 22 ++++++++-------- externals/.clang-tidy | 4 +++ lib/astutils.cpp | 2 +- lib/checkclass.cpp | 1 + lib/programmemory.cpp | 4 +-- lib/tokenize.cpp | 2 +- tools/CMakeLists.txt | 2 +- 9 files changed, 68 insertions(+), 16 deletions(-) create mode 100644 .github/workflows/clang-tidy.yml create mode 100644 externals/.clang-tidy diff --git a/.clang-tidy b/.clang-tidy index 5a7fb872e..d02987b13 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,5 +1,6 @@ --- -Checks: '*,-abseil-*,-android-*,-cert-*,-cppcoreguidelines-*,-fuchsia-*,-google-*,-hicpp-*,-linuxkernel-*,-llvm-*,-llvmlibc-*,-mpi-*,-objc-*,-openmp-*,-zircon-*,-readability-braces-around-statements,-readability-magic-numbers,-bugprone-macro-parentheses,-readability-isolate-declaration,-readability-function-size,-modernize-use-trailing-return-type,-readability-implicit-bool-conversion,-readability-uppercase-literal-suffix,-modernize-use-auto,-readability-else-after-return,-modernize-use-default-member-init,-readability-named-parameter,-readability-redundant-member-init,-performance-faster-string-find,-modernize-avoid-c-arrays,-modernize-use-equals-default,-readability-container-size-empty,-readability-simplify-boolean-expr,-modernize-use-override,-modernize-pass-by-value,-bugprone-branch-clone,-bugprone-narrowing-conversions,-modernize-raw-string-literal,-readability-convert-member-functions-to-static,-modernize-loop-convert,-misc-unused-using-decls,-modernize-use-emplace,-readability-const-return-type,-performance-unnecessary-value-param,-modernize-return-braced-init-list,-performance-inefficient-string-concatenation,-performance-for-range-copy,-misc-throw-by-value-catch-by-reference,-readability-avoid-const-params-in-decls,-readability-non-const-parameter,-misc-non-private-member-variables-in-classes,-bugprone-suspicious-string-compare,-readability-misleading-indentation,-misc-unused-parameters-,-clang-analyzer-*,-bugprone-signed-char-misuse,-readability-make-member-function-const,-misc-no-recursion,-readability-use-anyofallof,-performance-no-automatic-move,-bugprone-suspicious-include,-modernize-replace-random-shuffle' +Checks: '*,-abseil-*,-android-*,-cert-*,-cppcoreguidelines-*,-fuchsia-*,-google-*,-hicpp-*,-linuxkernel-*,-llvm-*,-llvmlibc-*,-mpi-*,-objc-*,-openmp-*,-zircon-*,-readability-braces-around-statements,-readability-magic-numbers,-bugprone-macro-parentheses,-readability-isolate-declaration,-readability-function-size,-modernize-use-trailing-return-type,-readability-implicit-bool-conversion,-readability-uppercase-literal-suffix,-modernize-use-auto,-readability-else-after-return,-modernize-use-default-member-init,-readability-named-parameter,-readability-redundant-member-init,-performance-faster-string-find,-modernize-avoid-c-arrays,-modernize-use-equals-default,-readability-container-size-empty,-readability-simplify-boolean-expr,-modernize-use-override,-modernize-pass-by-value,-bugprone-branch-clone,-bugprone-narrowing-conversions,-modernize-raw-string-literal,-readability-convert-member-functions-to-static,-modernize-loop-convert,-misc-unused-using-decls,-modernize-use-emplace,-readability-const-return-type,-performance-unnecessary-value-param,-modernize-return-braced-init-list,-performance-inefficient-string-concatenation,-performance-for-range-copy,-misc-throw-by-value-catch-by-reference,-readability-avoid-const-params-in-decls,-readability-non-const-parameter,-misc-non-private-member-variables-in-classes,-bugprone-suspicious-string-compare,-readability-misleading-indentation,-clang-analyzer-*,-bugprone-signed-char-misuse,-readability-make-member-function-const,-misc-no-recursion,-readability-use-anyofallof,-performance-no-automatic-move,-bugprone-suspicious-include,-modernize-replace-random-shuffle' +WarningsAsErrors: '*' CheckOptions: - key: misc-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic value: '1' \ No newline at end of file diff --git a/.github/workflows/clang-tidy.yml b/.github/workflows/clang-tidy.yml new file mode 100644 index 000000000..cd89a4cbc --- /dev/null +++ b/.github/workflows/clang-tidy.yml @@ -0,0 +1,44 @@ +# Syntax reference https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions +# Environment reference https://help.github.com/en/actions/reference/virtual-environments-for-github-hosted-runners +name: clang-tidy + +on: [push, pull_request] + +jobs: + build: + + strategy: + matrix: + os: [ubuntu-20.04] + fail-fast: false # Prefer quick result + + runs-on: ${{ matrix.os }} + + steps: + - uses: actions/checkout@v2 + + - name: Install missing software + run: | + sudo apt-get update + sudo apt-get install z3 libz3-dev + sudo apt-get install clang-tidy + + - name: Install Qt + uses: jurplel/install-qt-action@v2 + with: + modules: 'qtcharts' + + - name: Prepare CMake + run: | + mkdir cmake.output + cd cmake.output + # cannot include GUI since we need to generate the ui_*.h files first + cmake -G "Unix Makefiles" -DUSE_Z3=On -DHAVE_RULES=On -DBUILD_TESTS=On -DBUILD_GUI=Off -DWITH_QCHART=Off .. + cd .. + + - name: Clang-Tidy + run: | + # make sure the precompiled headers exist + make -C cmake.output lib/CMakeFiles/lib_objs.dir/cmake_pch.hxx.cxx + make -C cmake.output test/CMakeFiles/testrunner.dir/cmake_pch.hxx.cxx + cmake --build cmake.output --target run-clang-tidy 2> /dev/null diff --git a/cmake/clang_tidy.cmake b/cmake/clang_tidy.cmake index 15dcfc16c..885a71e15 100644 --- a/cmake/clang_tidy.cmake +++ b/cmake/clang_tidy.cmake @@ -1,13 +1,15 @@ -find_program(CLANG_TIDY NAMES clang-tidy clang-tidy-11 clang-tidy-10 clang-tidy-9 clang-tidy-8) -message(STATUS "CLANG_TIDY=${CLANG_TIDY}") -if (CLANG_TIDY) - set(CLANG_TIDY_SRCS ${CMAKE_SOURCE_DIR}/cli/*.cpp ${CMAKE_SOURCE_DIR}/lib/*.cpp) - if (BUILD_GUI) - list(APPEND CLANG_TIDY_SRCS ${CMAKE_SOURCE_DIR}/gui/*.cpp) - endif() - if (BUILD_TESTS) - list(APPEND CLANG_TIDY_SRCS ${CMAKE_SOURCE_DIR}/test/*.cpp) +if (NOT NPROC) + include(ProcessorCount) + ProcessorCount(NPROC) + if(NPROC EQUAL 0) + message(FATAL_ERROR "could not get processor count") endif() +endif() +message(STATUS "NPROC=${NPROC}") - add_custom_target(run-clang-tidy ${CLANG_TIDY} -p=${CMAKE_BINARY_DIR} ${CLANG_TIDY_SRCS}) +find_program(RUN_CLANG_TIDY NAMES run-clang-tidy run-clang-tidy-11 run-clang-tidy-10 run-clang-tidy-9 run-clang-tidy-8) +message(STATUS "RUN_CLANG_TIDY=${RUN_CLANG_TIDY}") +if (RUN_CLANG_TIDY) + # disable all compiler warnings since we are just interested in the tidy ones + add_custom_target(run-clang-tidy ${RUN_CLANG_TIDY} -p=${CMAKE_BINARY_DIR} -j ${NPROC} -extra-arg=-w -quiet) endif() \ No newline at end of file diff --git a/externals/.clang-tidy b/externals/.clang-tidy new file mode 100644 index 000000000..362eb3d06 --- /dev/null +++ b/externals/.clang-tidy @@ -0,0 +1,4 @@ +--- +Checks: '-*,misc-definitions-in-headers' +CheckOptions: + - { key: HeaderFileExtensions, value: "x" } diff --git a/lib/astutils.cpp b/lib/astutils.cpp index d18a224e4..555fa25aa 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1169,7 +1169,7 @@ static bool isForLoopCondition(const Token * const tok) /** * Is token used a boolean (cast to a bool, or used as a condition somewhere) - * @param tok + * @param tok the token to check * @param checkingParent true if we are checking a parent. This is used to know * what we are checking. For instance in `if (i == 2)`, isUsedAsBool("==") is * true whereas isUsedAsBool("i") is false, but it might call diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 199f5a558..1504b4822 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -562,6 +562,7 @@ std::vector CheckClass::createUsageList(const Scope *scope) std::vector ret; std::vector varlist; getAllVariableMembers(scope, varlist); + ret.reserve(varlist.size()); for (const Variable *var: varlist) ret.emplace_back(var); return ret; diff --git a/lib/programmemory.cpp b/lib/programmemory.cpp index d9ac4a496..1b6c0c7e8 100644 --- a/lib/programmemory.cpp +++ b/lib/programmemory.cpp @@ -424,8 +424,8 @@ void execute(const Token *expr, else if (expr->isComparisonOp()) { MathLib::bigint result1(0), result2(0); - bool error1 = 0; - bool error2 = 0; + bool error1 = false; + bool error2 = false; execute(expr->astOperand1(), programMemory, &result1, &error1); execute(expr->astOperand2(), programMemory, &result2, &error2); if (error1 && error2) { diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 859869b78..7a4947361 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1815,7 +1815,7 @@ namespace { const ScopeInfo3 * findScopeRecursive(const std::string & scope) const { if (fullName.size() < scope.size() && fullName == scope.substr(0, fullName.size())) { - for (auto & child : children) { + for (const auto & child : children) { if (child.fullName == scope && &child != this) return &child; else { diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 4176b897f..100f9a9ed 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -14,7 +14,7 @@ add_executable(dmake EXCLUDE_FROM_ALL ${CMAKE_SOURCE_DIR}/cli/filelister.cpp ${srcs_tools} ${CMAKE_SOURCE_DIR}/lib/utils.cpp - ${CMAKE_SOURCE_DIR}/externals/simplecpp/simplecpp + ${CMAKE_SOURCE_DIR}/externals/simplecpp/simplecpp.cpp ) target_include_directories(dmake PRIVATE ${CMAKE_SOURCE_DIR}/cli ${CMAKE_SOURCE_DIR}/lib ${CMAKE_SOURCE_DIR}/externals/simplecpp) if (WIN32 AND NOT BORLAND)