Initial support of clang-tidy toolchain

Clang Tidy is a Clang based "linter" tool which goal is to help
fixing typical programming errors.

It is run as a separate compile step of every file, which slows
compilation down but allows to fully analyze the file the same
way as compiler does and catch non-trivial bugprone cases.

This change includes:

- CMake option called `WITH_CLANG_TIDY` which enables Clang Tidy
  linter tool on all source in the `source/` directory.

  This option is only available on Linux, as it is currently the
  easiest platform to get the Clang Tidy toolchain to work.

- CMake module which is aimed to find latest available Clang Tidy.

- Set of rules which allows to have Blender fully compiled without
  extra issues.

The goal of this change is to provide a base ground so that solving
all the warnings can happen later on, as a team effort.

It should be possible to use Clang Tidy side-by-side with both GCC
and Clang, but there seems to be some tweaks to be done in CMake to
make it really work for Blender. For now use Clang toolchain if
there are issues with GCC+Clang Tidy.

It will be worked on in the nearest future to bring seamless
experience for all configurations.

Currently there is no official way of getting Clang Tidy on macOS,
and on Windows there are some difficulties of hooking up Clang Tidy
from LLVM package to the MSVC compiler toolchain.

The actual warnings in the code will be addressed as a part of the
Code Quality Days, task T78535.

Differential Revision: https://developer.blender.org/D7937
This commit is contained in:
Sergey Sharybin 2020-06-05 11:37:49 +02:00
parent a272a2a6cd
commit 9ea5469178
4 changed files with 173 additions and 0 deletions

54
.clang-tidy Normal file
View File

@ -0,0 +1,54 @@
Checks: >
-*,
readability-*,
-readability-uppercase-literal-suffix,
-readability-magic-numbers,
-readability-isolate-declaration,
-readability-convert-member-functions-to-static,
-readability-implicit-bool-conversion,
-readability-avoid-const-params-in-decls,
-readability-simplify-boolean-expr,
-readability-make-member-function-const,
-readability-misleading-indentation,
-readability-else-after-return,
-readability-braces-around-statements,
-readability-inconsistent-declaration-parameter-name,
-readability-non-const-parameter,
-readability-redundant-preprocessor,
-readability-redundant-control-flow,
-readability-named-parameter,
-readability-function-size,
-readability-function-size,
-readability-static-definition-in-anonymous-namespace,
-readability-delete-null-pointer,
-readability-redundant-string-init,
-readability-redundant-member-init,
-readability-const-return-type,
-readability-container-size-empty,
-readability-redundant-string-cstr,
-readability-static-accessed-through-instance,
-readability-redundant-declaration,
bugprone-*,
-bugprone-narrowing-conversions,
-bugprone-unhandled-self-assignment,
-bugprone-branch-clone,
-bugprone-macro-parentheses,
-bugprone-sizeof-expression,
-bugprone-integer-division,
-bugprone-incorrect-roundings,
-bugprone-suspicious-string-compare,
-bugprone-too-small-loop-variable,
-bugprone-misplaced-widening-cast,
-bugprone-not-null-terminated-result,
-bugprone-suspicious-missing-comma,
-bugprone-argument-comment,
-bugprone-assert-side-effect,
-bugprone-parent-virtual-call,
-bugprone-infinite-loop,
-bugprone-copy-constructor-init,
WarningsAsErrors: '*'

View File

@ -415,6 +415,11 @@ mark_as_advanced(WITH_CXX_GUARDEDALLOC)
option(WITH_ASSERT_ABORT "Call abort() when raising an assertion through BLI_assert()" ON)
mark_as_advanced(WITH_ASSERT_ABORT)
if(UNIX AND NOT APPLE)
option(WITH_CLANG_TIDY "Use Clang Tidy to analyze the source code (only enable for development on Limux using Clang)" OFF)
mark_as_advanced(WITH_CLANG_TIDY)
endif()
option(WITH_BOOST "Enable features depending on boost" ON)
option(WITH_TBB "Enable features depending on TBB (OpenVDB, OpenImageDenoise, sculpt multithreading)" ON)

View File

@ -0,0 +1,104 @@
# - Find clang-tidy executable
#
# Find the native clang-tidy executable
#
# This module defines
# CLANG_TIDY_EXECUTABLE, the ful lpath to clang-tidy executable
#
# CLANG_TIDY_VERSION, the full version of the clang-tidy in the
# major,minor.patch format
#
# CLANG_TIDY_VERSION_MAJOR,
# CLANG_TIDY_VERSION_MINOR,
# CLANG_TIDY_VERSION_PATCH, individual components of the clang-tidy version.
#
# CLANG_TIDY_FOUND, If false, do not try to use Eigen3.
#=============================================================================
# Copyright 2020 Blender Foundation.
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# If CLANG_TIDY_ROOT_DIR was defined in the environment, use it.
if(NOT CLANG_TIDY_ROOT_DIR AND NOT $ENV{CLANG_TIDY_ROOT_DIR} STREQUAL "")
set(CLANG_TIDY_ROOT_DIR $ENV{CLANG_TIDY_ROOT_DIR})
endif()
set(_clang_tidy_SEARCH_DIRS
${CLANG_TIDY_ROOT_DIR}
/usr/local/bin
)
# TODO(sergey): Find more reliable way of finding the latest clang-tidy.
find_program(CLANG_TIDY_EXECUTABLE
NAMES
clang-tidy-10
clang-tidy-9
clang-tidy-8
clang-tidy-7
clang-tidy
HINTS
${_clang_tidy_SEARCH_DIRS}
)
if(CLANG_TIDY_EXECUTABLE)
# Mark clang-tidy as found.
set(CLANG_TIDY_FOUND TRUE)
# Setup fallback values.
set(CLANG_TIDY_VERSION_MAJOR 0)
set(CLANG_TIDY_VERSION_MINOR 0)
set(CLANG_TIDY_VERSION_PATCH 0)
# Get version from the output.
#
# NOTE: Don't use name of the executable file since that only includes a
# major version. Also, even the major version might be missing in the
# executable name.
execute_process(COMMAND ${CLANG_TIDY_EXECUTABLE} -version
OUTPUT_VARIABLE CLANG_TIDY_VERSION_RAW
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
# Parse parts.
if(CLANG_TIDY_VERSION_RAW MATCHES "LLVM version .*")
# Strip the LLVM prefix and get list of individual version components.
string(REGEX REPLACE
".*LLVM version ([.0-9]+).*" "\\1"
CLANG_SEMANTIC_VERSION "${CLANG_TIDY_VERSION_RAW}")
string(REPLACE "." ";" CLANG_VERSION_PARTS "${CLANG_SEMANTIC_VERSION}")
list(LENGTH CLANG_VERSION_PARTS NUM_CLANG_TIDY_VERSION_PARTS)
# Extract components into corresponding variables.
if(NUM_CLANG_TIDY_VERSION_PARTS GREATER 0)
list(GET CLANG_VERSION_PARTS 0 CLANG_TIDY_VERSION_MAJOR)
endif()
if(NUM_CLANG_TIDY_VERSION_PARTS GREATER 1)
list(GET CLANG_VERSION_PARTS 1 CLANG_TIDY_VERSION_MINOR)
endif()
if(NUM_CLANG_TIDY_VERSION_PARTS GREATER 2)
list(GET CLANG_VERSION_PARTS 2 CLANG_TIDY_VERSION_PATCH)
endif()
# Unset temp variables.
unset(NUM_CLANG_TIDY_VERSION_PARTS)
unset(CLANG_SEMANTIC_VERSION)
unset(CLANG_VERSION_PARTS)
endif()
# Construct full semantic version.
set(CLANG_TIDY_VERSION "${CLANG_TIDY_VERSION_MAJOR}.\
${CLANG_TIDY_VERSION_MINOR}.\
${CLANG_TIDY_VERSION_PATCH}")
unset(CLANG_TIDY_VERSION_RAW)
message(STATUS "Found clang-tidy ${CLANG_TIDY_EXECUTABLE} (${CLANG_TIDY_VERSION})")
else()
set(CLANG_TIDY_FOUND FALSE)
endif()

View File

@ -22,4 +22,14 @@ if(WITH_LEGACY_OPENGL)
add_definitions(-DWITH_LEGACY_OPENGL)
endif()
if(WITH_CLANG_TIDY)
if(NOT CMAKE_C_COMPILER_ID MATCHES "Clang")
message(FATAL_ERROR "Currently Clang-Tidy is only supported when using Clang compiler")
endif()
find_package(ClangTidy REQUIRED)
set(CMAKE_C_CLANG_TIDY ${CLANG_TIDY_EXECUTABLE})
set(CMAKE_CXX_CLANG_TIDY ${CLANG_TIDY_EXECUTABLE})
endif()
add_subdirectory(blender)