Skip to content

Commit f89c2cc

Browse files
committed
style(clang-tidy): modularize clang-tidy config
Read blogs and played around with [CMAKE_CXX_CLANG_TIDY](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_CLANG_TIDY.html). Also read source code to understand the clang-tidy config of chromium, llvm and other projects. We can provide complete context including headers to the clang-tidy tool for static analyzer by running it while building the code. This also makes static analysis first-class citizen of the project, reporting errors before making commits. For example, `misc-internal-linkage` check was reporting mostly false positives prior to this. With this, it also considered the headers, where methods/functions were declared constant. Refer sourcemeta#1752 And we should also incrementally add the checks. Signed-off-by: Balakrishna Avulapati <ba@bavulapati.com>
1 parent 3e7c83e commit f89c2cc

File tree

5 files changed

+21
-58
lines changed

5 files changed

+21
-58
lines changed

.clang-tidy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
# See https://clang.llvm.org/extra/clang-tidy/index.html
3+
# First disable all default checks (with -*)
4+
Checks: '-*'

CMakeLists.txt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ option(SOURCEMETA_CORE_CONTRIB_GOOGLEBENCHMARK "Build the GoogleBenchmark librar
2222

2323
include(Sourcemeta)
2424

25+
if(PROJECT_IS_TOP_LEVEL)
26+
sourcemeta_target_clang_format(SOURCES
27+
src/*.h src/*.cc
28+
benchmark/*.h benchmark/*.cc
29+
test/*.h test/*.cc)
30+
sourcemeta_find_clang_tidy() # Locates CLANG_TIDY program
31+
endif()
32+
33+
# Add clang-tidy to the targets. Clang-tidy uses the .clang-tidy file for configuration.
34+
set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_BIN}" "--allow-no-checks")
2535
# Don't force downstream consumers on it
2636
if(PROJECT_IS_TOP_LEVEL)
2737
sourcemeta_enable_simd()
@@ -91,13 +101,6 @@ if(SOURCEMETA_CORE_DOCS)
91101
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/website")
92102
endif()
93103

94-
if(PROJECT_IS_TOP_LEVEL)
95-
sourcemeta_target_clang_format(SOURCES
96-
src/*.h src/*.cc
97-
benchmark/*.h benchmark/*.cc
98-
test/*.h test/*.cc)
99-
sourcemeta_target_clang_tidy(SOURCES src/*.cc)
100-
endif()
101104

102105
# Testing
103106

cmake/common/targets/clang-tidy.cmake

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
function(sourcemeta_target_clang_tidy)
2-
cmake_parse_arguments(SOURCEMETA_TARGET_CLANG_TIDY "REQUIRED" "" "SOURCES" ${ARGN})
3-
1+
function(sourcemeta_find_clang_tidy)
42
set(CLANG_TIDY_FIND_PATHS "")
53

64
# Locate ClangTidy on default Homebrew installations,
@@ -27,30 +25,4 @@ function(sourcemeta_target_clang_tidy)
2725
find_program(CLANG_TIDY_BIN NAMES clang-tidy
2826
PATHS ${CLANG_TIDY_FIND_PATHS})
2927
endif()
30-
31-
# This covers the empty list too
32-
if(NOT SOURCEMETA_TARGET_CLANG_TIDY_SOURCES)
33-
message(FATAL_ERROR "You must pass file globs to analyze in the SOURCES option")
34-
endif()
35-
file(GLOB_RECURSE SOURCEMETA_TARGET_CLANG_TIDY_FILES
36-
${SOURCEMETA_TARGET_CLANG_TIDY_SOURCES})
37-
38-
set(CLANG_TIDY_CONFIG "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/clang-tidy.config")
39-
if(CLANG_TIDY_BIN)
40-
add_custom_target(clang_tidy
41-
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
42-
VERBATIM
43-
COMMAND "${CLANG_TIDY_BIN}" -p "${PROJECT_BINARY_DIR}"
44-
--config-file "${CLANG_TIDY_CONFIG}"
45-
${SOURCEMETA_TARGET_CLANG_TIDY_FILES}
46-
COMMENT "Analyzing sources using ClangTidy")
47-
else()
48-
add_custom_target(clang_tidy
49-
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
50-
VERBATIM
51-
COMMAND "${CMAKE_COMMAND}" -E echo "Could not locate ClangTidy"
52-
COMMAND "${CMAKE_COMMAND}" -E false)
53-
endif()
54-
55-
set_target_properties(clang_tidy PROPERTIES FOLDER "Linting")
5628
endfunction()

cmake/common/targets/clang-tidy.config

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/.clang-tidy

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
# See https://clang.llvm.org/extra/clang-tidy/index.html
3+
Checks: 'concurrency-*'
4+
InheritParentConfig: true
5+
WarningsAsErrors: '*'
6+
FormatStyle: none

0 commit comments

Comments
 (0)