Skip to content

Commit

Permalink
Custom Clang-Tidy diagnostics for each CMake target
Browse files Browse the repository at this point in the history
  • Loading branch information
jngrad committed Dec 17, 2024
1 parent e21f3b7 commit f87aa66
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ CheckOptions:
value: 'NULL'
- key: modernize-use-transparent-functors.SafeMode
value: 'true'
- key: modernize-use-using.IgnoreExternC
value: 'true'
- key: readability-simplify-boolean-expr.ChainedConditionalAssignment
value: '0'
- key: readability-simplify-boolean-expr.ChainedConditionalReturn
Expand Down
29 changes: 29 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ include(GNUInstallDirs)
include(FetchContent)
include(espresso_option_enum)
include(espresso_enable_avx2_support)
include(espresso_override_clang_tidy_checks)

if(EXISTS "${PROJECT_BINARY_DIR}/CMakeLists.txt")
message(
Expand Down Expand Up @@ -589,6 +590,33 @@ if(ESPRESSO_BUILD_WITH_CLANG_TIDY)
find_package(ClangTidy "${CMAKE_CXX_COMPILER_VERSION}" EXACT REQUIRED)
set(ESPRESSO_CXX_CLANG_TIDY "${CLANG_TIDY_EXE}")
set(ESPRESSO_CUDA_CLANG_TIDY "${CLANG_TIDY_EXE};--extra-arg=--cuda-host-only")
set(SKIP_CLANG_TIDY_CHECKS "")
set(SKIP_CLANG_TIDY_CHECKS_CXX "")
set(SKIP_CLANG_TIDY_CHECKS_CUDA "")
if(ESPRESSO_BUILD_WITH_CALIPER)
# Clang-Tidy sometimes emits diagnostics in code enclosed in `extern "C"`
# that are not always actionable, since they may rely on keywords only
# available in the C++ language. While some checks have an extra flag
# 'IgnoreExternC' to disable them inside C code, not all affected checks
# have been fixed yet. For an in-depth discussion on this topic, see
# https://github.com/llvm/llvm-project/issues/35272
list(APPEND SKIP_CLANG_TIDY_CHECKS "-modernize-use-auto")
list(APPEND SKIP_CLANG_TIDY_CHECKS "-modernize-use-nullptr")
list(APPEND SKIP_CLANG_TIDY_CHECKS "-modernize-deprecated-headers")
endif()
if(ESPRESSO_BUILD_WITH_CUDA)
# silence casts in cuda_runtime.h (for both C++ and CUDA source files)
list(APPEND SKIP_CLANG_TIDY_CHECKS "-bugprone-casting-through-void")
# silence nullptr dereference in cuda::thrust
list(APPEND SKIP_CLANG_TIDY_CHECKS_CUDA
"-clang-analyzer-core.NonNullParamChecker")
endif()
espresso_override_clang_tidy_checks(
ESPRESSO_CXX_CLANG_TIDY "${SKIP_CLANG_TIDY_CHECKS}"
"${SKIP_CLANG_TIDY_CHECKS_CXX}")
espresso_override_clang_tidy_checks(
ESPRESSO_CUDA_CLANG_TIDY "${SKIP_CLANG_TIDY_CHECKS}"
"${SKIP_CLANG_TIDY_CHECKS_CUDA}")
endif()

if(ESPRESSO_BUILD_WITH_CPPCHECK)
Expand Down Expand Up @@ -742,6 +770,7 @@ if(ESPRESSO_BUILD_WITH_CALIPER)
PRIVATE $<$<CXX_COMPILER_ID:GNU>:-Wno-maybe-uninitialized>
$<$<CXX_COMPILER_ID:GNU>:-Wno-volatile>
$<$<CXX_COMPILER_ID:Clang,AppleClang>:-Wno-deprecated-volatile>)
set_target_properties(caliper-runtime PROPERTIES CXX_CLANG_TIDY "")
endif()

#
Expand Down
64 changes: 64 additions & 0 deletions cmake/espresso_override_clang_tidy_checks.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#
# Copyright (C) 2024 The ESPResSo project
#
# This file is part of ESPResSo.
#
# ESPResSo is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ESPResSo is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

# Override Clang-Tidy checks.
#
# This function appends an extra flag "--checks=..." in the parent scope
# variable whose name is passed as first argument. The second and third
# arguments are the general and the language-specific overrides, respectively.
# This way you can append a common set of overrides plus language-specific
# overrides in two variables named MYPROJECT_CXX_CLANG_TIDY and
# MYPROJECT_CUDA_CLANG_TIDY, which are used to set the CXX_CLANG_TIDY and
# CUDA_CLANG_TIDY properties of CMake targets.
#
# Example:
# ```cmake
# include(espresso_override_clang_tidy_checks)
# if(MYPROJECT_BUILD_WITH_CLANG_TIDY)
# find_package(ClangTidy "${CMAKE_CXX_COMPILER_VERSION}" EXACT REQUIRED)
# set(MYPROJECT_CXX_CLANG_TIDY "${CLANG_TIDY_EXE}")
# set(MYPROJECT_CUDA_CLANG_TIDY "${CLANG_TIDY_EXE};--extra-arg=--cuda-host-only")
# set(SKIP_CLANG_TIDY_CHECKS "")
# set(SKIP_CLANG_TIDY_CHECKS_CXX "")
# set(SKIP_CLANG_TIDY_CHECKS_CUDA "")
# # silence false positives in code enclosed in `extern "C" { /* ... */ }`
# list(APPEND SKIP_CLANG_TIDY_CHECKS "-modernize-use-auto")
# if (MYPROJECT_BUILD_WITH_CUDA)
# # silence casts in cuda_runtime.h (for both C++ and CUDA source files)
# list(APPEND SKIP_CLANG_TIDY_CHECKS "-bugprone-casting-through-void")
# # silence nullptr dereference in cuda::thrust (only for CUDA files)
# list(APPEND SKIP_CLANG_TIDY_CHECKS_CUDA "-clang-analyzer-core.NonNullParamChecker")
# endif()
# espresso_override_clang_tidy_checks(MYPROJECT_CXX_CLANG_TIDY "${SKIP_CLANG_TIDY_CHECKS}" "${SKIP_CLANG_TIDY_CHECKS_CXX}")
# espresso_override_clang_tidy_checks(MYPROJECT_CUDA_CLANG_TIDY "${SKIP_CLANG_TIDY_CHECKS}" "${SKIP_CLANG_TIDY_CHECKS_CUDA}")
# set_target_properties(myproject_core PROPERTIES CXX_CLANG_TIDY "${MYPROJECT_CXX_CLANG_TIDY}")
# set_target_properties(myproject_cuda PROPERTIES CUDA_CLANG_TIDY "${MYPROJECT_CUDA_CLANG_TIDY}")
# endif()
# ```

function(espresso_override_clang_tidy_checks)
set(VARNAME "${ARGV0}")
set(CHECKS "${ARGV1}")
set(CHECKS_LANG "${ARGV2}")
list(APPEND CHECKS ${CHECKS_LANG})
if(CHECKS)
list(JOIN CHECKS "," CHECKS_STRING)
set(${VARNAME} "${${VARNAME}};--checks=${CHECKS_STRING}" PARENT_SCOPE)
endif()
endfunction()
65 changes: 64 additions & 1 deletion src/walberla_bridge/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,72 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

set(WALBERLA_CXX_CLANG_TIDY "${ESPRESSO_CXX_CLANG_TIDY}")
set(WALBERLA_CUDA_CLANG_TIDY "${ESPRESSO_CUDA_CLANG_TIDY}")
set(WALBERLA_CXX_CLANG_TIDY_CODEGEN "${ESPRESSO_CXX_CLANG_TIDY}")
set(WALBERLA_CUDA_CLANG_TIDY_CODEGEN "${ESPRESSO_CUDA_CLANG_TIDY}")
set(SKIP_CLANG_TIDY_CHECKS "")
set(SKIP_CLANG_TIDY_CHECKS_CXX "")
set(SKIP_CLANG_TIDY_CHECKS_CUDA "")
# silence waLBerla diagnostics
list(APPEND SKIP_CLANG_TIDY_CHECKS "-clang-analyzer-deadcode.DeadStores")
list(APPEND SKIP_CLANG_TIDY_CHECKS "-readability-non-const-parameter")
list(APPEND SKIP_CLANG_TIDY_CHECKS "-readability-avoid-const-params-in-decls")
list(APPEND SKIP_CLANG_TIDY_CHECKS "-readability-else-after-return")
list(APPEND SKIP_CLANG_TIDY_CHECKS "-readability-simplify-boolean-expr")
list(APPEND SKIP_CLANG_TIDY_CHECKS "-modernize-use-auto")
list(APPEND SKIP_CLANG_TIDY_CHECKS "-modernize-loop-convert")
list(APPEND SKIP_CLANG_TIDY_CHECKS "-modernize-pass-by-value")
list(APPEND SKIP_CLANG_TIDY_CHECKS "-modernize-use-equals-delete")
list(APPEND SKIP_CLANG_TIDY_CHECKS "-modernize-return-braced-init-list")
list(APPEND SKIP_CLANG_TIDY_CHECKS "-bugprone-crtp-constructor-accessibility")
list(APPEND SKIP_CLANG_TIDY_CHECKS "-bugprone-narrowing-conversions")
list(APPEND SKIP_CLANG_TIDY_CHECKS "-bugprone-assert-side-effect")
list(APPEND SKIP_CLANG_TIDY_CHECKS "-bugprone-exception-escape")
list(APPEND SKIP_CLANG_TIDY_CHECKS "-bugprone-branch-clone")
if(WALBERLA_BUILD_WITH_CUDA)
# silence diagnostics from cuda header files
list(APPEND SKIP_CLANG_TIDY_CHECKS "-bugprone-casting-through-void")
list(APPEND SKIP_CLANG_TIDY_CHECKS "-modernize-redundant-void-arg")
list(APPEND SKIP_CLANG_TIDY_CHECKS "-modernize-use-nullptr")
# silence nullptr dereference in cuda::thrust
list(APPEND SKIP_CLANG_TIDY_CHECKS_CUDA
"-clang-analyzer-core.NonNullParamChecker")
endif()

espresso_override_clang_tidy_checks(
WALBERLA_CXX_CLANG_TIDY "${SKIP_CLANG_TIDY_CHECKS}"
"${SKIP_CLANG_TIDY_CHECKS_CXX}")
espresso_override_clang_tidy_checks(
WALBERLA_CUDA_CLANG_TIDY "${SKIP_CLANG_TIDY_CHECKS}"
"${SKIP_CLANG_TIDY_CHECKS_CUDA}")

# codegen-specific Clang-Tidy overrides
list(APPEND SKIP_CLANG_TIDY_CHECKS_CUDA
"-bugprone-multi-level-implicit-pointer-conversion")
espresso_override_clang_tidy_checks(
WALBERLA_CXX_CLANG_TIDY_CODEGEN "${SKIP_CLANG_TIDY_CHECKS}"
"${SKIP_CLANG_TIDY_CHECKS_CXX}")
espresso_override_clang_tidy_checks(
WALBERLA_CUDA_CLANG_TIDY_CODEGEN "${SKIP_CLANG_TIDY_CHECKS}"
"${SKIP_CLANG_TIDY_CHECKS_CUDA}")

function(espresso_configure_walberla_target)
set(TARGET_NAME ${ARGV0})
set_target_properties(${TARGET_NAME} PROPERTIES CXX_CLANG_TIDY "")
if(ESPRESSO_BUILD_WITH_CLANG_TIDY)
set(TARGET_LANG "CXX")
if(TARGET_NAME MATCHES "_cuda")
set(TARGET_LANG "CUDA")
endif()
set(TARGET_SUFFIX "")
if(TARGET_NAME MATCHES "_codegen")
set(TARGET_SUFFIX "_CODEGEN")
endif()
set_target_properties(
${TARGET_NAME}
PROPERTIES ${TARGET_LANG}_CLANG_TIDY
"${WALBERLA_${TARGET_LANG}_CLANG_TIDY${TARGET_SUFFIX}}")
endif()
target_link_libraries(${TARGET_NAME} PRIVATE ${WALBERLA_LIBS})
target_include_directories(
${TARGET_NAME} PUBLIC include PRIVATE ${WALBERLA_INCLUDE_DIRS}
Expand Down
9 changes: 8 additions & 1 deletion src/walberla_bridge/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,17 @@ function(ESPRESSO_ADD_TEST)
endif()
if(${TEST_SRC} MATCHES ".*\.cu$")
target_link_libraries(${TEST_NAME} PRIVATE espresso::walberla::cuda_flags)
if(ESPRESSO_BUILD_WITH_CLANG_TIDY)
set_target_properties(
${TEST_NAME} PROPERTIES CUDA_CLANG_TIDY "${WALBERLA_CUDA_CLANG_TIDY}")
endif()
else()
target_link_libraries(${TEST_NAME} PRIVATE espresso::walberla::cpp_flags)
if(ESPRESSO_BUILD_WITH_CLANG_TIDY)
set_target_properties(
${TEST_NAME} PROPERTIES CXX_CLANG_TIDY "${WALBERLA_CXX_CLANG_TIDY}")
endif()
endif()
set_target_properties(${TEST_NAME} PROPERTIES CXX_CLANG_TIDY "")
target_include_directories(${TEST_NAME} PRIVATE ${WALBERLA_INCLUDE_DIRS}
${walberla_BINARY_DIR}/src)
target_link_libraries(${TEST_NAME} PRIVATE ${WALBERLA_LIBS})
Expand Down

0 comments on commit f87aa66

Please sign in to comment.