Commit 3d6f2953 authored by David Fairbrother's avatar David Fairbrother
Browse files

Extract and refactor clang tidy CMake logic

Extracts the clang tidy block and does some tidying to simplify the
logic around it.

Add a clang-tidy file to handle all of our suppressed and active
warnings
parent 0787239e
# Yaml file format
Checks: >
*,
-abseil-*,-android-*,-altera-*,-fuchsia-*,-llvmlibc-*,-objc-*,
-google-build-using-namespace,
-google-readability-braces-around-statements,-hicpp-braces-around-statements,-readability-braces-around-statements,
-modernize-use-trailing-return-type,
-readability-implicit-bool-conversion
# Skip the following family of checks (for perf as we don't touch these libs at all):
# abseil-*
# android-*
# altera-*
# fuchsia-*
# llvmlibc-*
# objc-*
# The following checks are additionally disabled above:
# -google-build-using-namespace Useful for headers, but we can't filter out .cpp where it's used frequently
# -google-readability-braces-around-statements We use if blocks without braces throughout
# -readability-braces-around-statements Ditto
# -hicpp-braces-around-statements Ditto
# -modernize-use-trailing-return-type We use the old style throughout
# -readability-implicit-bool-conversion Allow if(!ptr) rather than (ptr==nullptr)
option(ENABLE_CLANG_TIDY "Add clang-tidy automatically to builds")
if(ENABLE_CLANG_TIDY)
option(APPLY_CLANG_TIDY_FIX "Apply fixes found through clang-tidy checks" OFF)
find_program(
CLANG_TIDY_EXE
NAMES
"clang-tidy-14"
"clang-tidy-13"
"clang-tidy-12"
"clang-tidy-11"
"clang-tidy-10"
"clang-tidy-9"
"clang-tidy"
PATHS /usr/local/opt/llvm/bin
)
if(NOT CLANG_TIDY_EXE)
message(FATAL_ERROR "Clang Tidy was enabled, but the EXE could not be found.")
endif()
message(STATUS "clang-tidy found: ${CLANG_TIDY_EXE}")
set(CLANG_TIDY_CHECKS "${DEFAULT_CLANG_TIDY_CHECKS}"
CACHE STRING "Select checks to perform")
if(CLANG_TIDY_CHECKS STREQUAL "")
# use default checks if empty to avoid errors
set(CLANG_TIDY_CHECKS
"${DEFAULT_CLANG_TIDY_CHECKS}"
CACHE STRING "Select checks to perform" FORCE)
endif()
set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_EXE}" CACHE STRING "" FORCE)
if (APPLY_CLANG_TIDY_FIX)
message(WARNING "You must build with a single thread (-j1) to avoid duplicate fixes in files or use run-clang-tidy.py")
set(CMAKE_CXX_CLANG_TIDY "${CMAKE_CXX_CLANG_TIDY};-fix"
CACHE STRING "" FORCE)
endif()
else()
# Turn off
set(CMAKE_CXX_CLANG_TIDY "" CACHE STRING "" FORCE)
endif()
......@@ -321,64 +321,7 @@ endif()
# ##############################################################################
# Configure clang-tidy if the tool is found
# ##############################################################################
if(CMAKE_VERSION VERSION_GREATER "3.5")
set(DEFAULT_CLANG_TIDY_CHECKS
"-*,performance-for-range-copy,performance-unnecessary-copy-initialization,modernize-use-override,modernize-use-nullptr,modernize-loop-convert,modernize-use-bool-literals,modernize-deprecated-headers,misc-*,-misc-unused-parameters"
)
option(ENABLE_CLANG_TIDY "Add clang-tidy automatically to builds")
if(ENABLE_CLANG_TIDY)
find_program(
CLANG_TIDY_EXE
NAMES "clang-tidy"
PATHS /usr/local/opt/llvm/bin
)
if(CLANG_TIDY_EXE)
message(STATUS "clang-tidy found: ${CLANG_TIDY_EXE}")
set(CLANG_TIDY_CHECKS
"${DEFAULT_CLANG_TIDY_CHECKS}"
CACHE STR "Select checks to perform"
)
option(APPLY_CLANG_TIDY_FIX "Apply fixes found through clang-tidy checks"
OFF
)
if(CLANG_TIDY_CHECKS STREQUAL "")
# use default checks if empty to avoid errors
set(CLANG_TIDY_CHECKS
"${DEFAULT_CLANG_TIDY_CHECKS}"
CACHE STR "Select checks to perform" FORCE
)
endif()
if(APPLY_CLANG_TIDY_FIX)
set(CMAKE_CXX_CLANG_TIDY
"${CLANG_TIDY_EXE};-checks=${CLANG_TIDY_CHECKS};-header-filter='${CMAKE_SOURCE_DIR}/*';-fix"
CACHE STRING "" FORCE
)
else()
set(CMAKE_CXX_CLANG_TIDY
"${CLANG_TIDY_EXE};-checks=${CLANG_TIDY_CHECKS};-header-filter='${CMAKE_SOURCE_DIR}/*'"
CACHE STRING "" FORCE
)
endif()
else()
message(AUTHOR_WARNING "clang-tidy not found!")
set(CMAKE_CXX_CLANG_TIDY
""
CACHE STRING "" FORCE
) # delete it
endif()
else()
set(CMAKE_CXX_CLANG_TIDY
""
CACHE STRING "" FORCE
) # delete it
endif()
else()
message(
AUTHOR_WARNING
"Using cmake version 3.5 or below. Clang-tidy is not supported!"
)
endif()
include(ClangTidy)
# ##############################################################################
# Setup cppcheck
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment