Skip to content

Commit 74fcf18

Browse files
build(clang-tidy): replace clang-tidy scripts with CMake approach (#142)
This PR replaces the clang-tidy scripts with a CMake-based approach. The core mechanism is to define CMake's `CMAKE_CXX_CLANG_TIDY` variable, which invokes clang-tidy whenever a C++ target is compiled. The remainder consists of some logic that finds the clang-tidy executable and its arguments as well as activating and deactivating the command in the right place (in particular, to exclude checking third-party code). I see three main advantages to the new approach: (1) The new logic is considerably simpler than the previous scripts. (2) The code is automatically checked when it compiled, potentially saving round-trips through CI. (3) We save an extra 10 minutes on CI for recompiling the whole project, which the previous scripts did. The PR also does two minor changes: (1) It removes the `AnalyzeTemporaryDtors` configuration, which was depcrecated in clang 16 and removed in clang 18. (2) It applies the suggested fixes for one class of errors that my clang-tidy-22 complained about. Signed-off-by: Ingo Müller <ingomueller@google.com>
1 parent d3588e8 commit 74fcf18

13 files changed

Lines changed: 51 additions & 98 deletions

File tree

.clang-tidy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ Checks: >
7878
-bugprone-unchecked-optional-access
7979
WarningsAsErrors: '*'
8080
HeaderFilterRegex: 'io/substrait/*.\\.h$'
81-
AnalyzeTemporaryDtors: false
8281
FormatStyle: none
8382
CheckOptions:
8483
- { key: readability-identifier-naming.NamespaceCase, value: lower_case}

.github/workflows/build_test.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ jobs:
2525
run: ./scripts/run-clang-format.sh
2626
- name: Checking formatting
2727
run: git diff --exit-code
28-
- name: Checking code style
29-
run: ./scripts/run-clang-tidy.sh
3028

3129
ubuntu-build-and-test:
3230
runs-on: ubuntu-latest

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ set(ANTLR_EXECUTABLE "${ANTLR_EXECUTABLE_DIR}/antlr.jar")
5656
include_directories(include)
5757
include_directories(src)
5858

59+
include(cmake/clang-tidy.cmake)
60+
61+
# Disable clang-tidy for third-party dependencies.
62+
unset(CMAKE_CXX_CLANG_TIDY)
63+
5964
# TODO: Simplify once we can require cmake 3.27 (where CONFIG is default).
6065

6166
# Due to packaging changes we use the combined protobuf/absl packaging if
@@ -78,6 +83,7 @@ else()
7883
endif()
7984

8085
add_subdirectory(third_party)
86+
set(CMAKE_CXX_CLANG_TIDY ${CLANG_TIDY_COMMAND}) # Re-enable clang-tidy.
8187

8288
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules")
8389
include(BuildUtils)

Makefile

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,6 @@ format:
2727
bash scripts/run-cmake-format.sh && bash scripts/run-clang-format.sh
2828
set +f
2929

30-
tidy:
31-
set -f
32-
bash scripts/run-clang-tidy.sh
33-
set +f
34-
35-
tidy-fix:
36-
set -f
37-
bash scripts/run-clang-tidy.sh fix
38-
set +f
39-
4030
debug:
4131
@$(MAKE) build-common BUILD_TYPE=Debug
4232
@$(MAKE) build BUILD_TYPE=Debug

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ Basically the coding style is based on Google C++ Style, but there are some nami
3131
- Variable case style change to 'camelBack'
3232
- Class Member case style change to 'camelBack' with '_' as suffix
3333

34-
For more detail information please refer to .clang-tidy under root directory.
34+
For more detail information please refer to [`.clang-tidy`](.clang-tidy).
3535

36+
You can run `make format` script to formatting source code. To enable `clang-tidy` checks during compilation, use the options from [`cmake/clang-tidy.cmake`](cmake/clang-tidy.cmake). To apply the suggested fixes, run the following command in the root directory:
3637

37-
You can run `make format` script to formatting source code and run `make tidy` to checking coding style, and run `make tidy-fix`to fix the coding style automatically.
38+
```bash
39+
clang-apply-replacements-22 --ignore-insert-conflict build/clang-tidy-fixes/
40+
```
3841

3942
## License
4043

cmake/clang-tidy.cmake

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
option(SUBSTRAIT_CPP_ENABLE_CLANG_TIDY
4+
"Enable clang-tidy static analysis on Substrait MLIR targets" OFF)
5+
option(SUBSTRAIT_CPP_CLANG_TIDY_PATH "path to 'clang-tidy' executable")
6+
7+
if(SUBSTRAIT_CPP_ENABLE_CLANG_TIDY)
8+
# Find `clang-tidy` executable.
9+
get_filename_component(CLANG_TIDY_NAME "${SUBSTRAIT_CPP_CLANG_TIDY_PATH}"
10+
NAME)
11+
get_filename_component(CLANG_TIDY_DIR "${SUBSTRAIT_CPP_CLANG_TIDY_PATH}"
12+
DIRECTORY)
13+
find_program(
14+
CLANG_TIDY_EXE
15+
HINTS ${CLANG_TIDY_DIR}
16+
NAMES "clang-tidy" "${CLANG_TIDY_NAME}" REQUIRED)
17+
if("${CLANG_TIDY_EXE}" STREQUAL "")
18+
message(FATAL_ERROR "failed to find clang-tidy executable")
19+
endif()
20+
21+
# Assemble command line.
22+
set(CLANG_TIDY_CONFIG_FILE "${CMAKE_SOURCE_DIR}/.clang-tidy")
23+
set(CLANG_TIDY_COMMAND
24+
"${CLANG_TIDY_EXE};--config-file=${CLANG_TIDY_CONFIG_FILE}")
25+
set(CMAKE_CXX_CLANG_TIDY_EXPORT_FIXES_DIR
26+
"${CMAKE_BINARY_DIR}/clang-tidy-fixes")
27+
message(STATUS "clang-tidy enabled, command: ${CLANG_TIDY_COMMAND}")
28+
endif(SUBSTRAIT_CPP_ENABLE_CLANG_TIDY)
29+
30+
# Enable clang-tidy for all C++ targets.
31+
set(CMAKE_CXX_CLANG_TIDY ${CLANG_TIDY_COMMAND})

scripts/run-clang-tidy.py

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

scripts/run-clang-tidy.sh

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

src/substrait/proto/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ foreach(PROTO_FILE IN LISTS PROTOBUF_FILELIST)
7575
endforeach()
7676

7777
# Add the generated protobuf C++ files to our exported library.
78+
unset(CMAKE_CXX_CLANG_TIDY)
7879
add_library(substrait_proto ${PROTO_SRCS} ${PROTO_HDRS} ProtoUtils.cpp)
80+
set(CMAKE_CXX_CLANG_TIDY ${CLANG_TIDY_COMMAND}) # Re-enable clang-tidy.
7981
target_sources(
8082
substrait_proto
8183
PUBLIC FILE_SET

src/substrait/textplan/ParseResult.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ std::ostream& operator<<(std::ostream& os, const ParseResult& result) {
1212
}
1313
auto msgs = result.getSyntaxErrors();
1414
if (!msgs.empty()) {
15-
os << "{" << std::endl;
15+
os << "{" << '\n';
1616
for (const std::string& msg : msgs) {
17-
os << " \"" << msg << "\"," << std::endl;
17+
os << " \"" << msg << "\"," << '\n';
1818
}
1919
os << "}";
2020
}
2121
msgs = result.getSemanticErrors();
2222
if (!msgs.empty()) {
23-
os << "{" << std::endl;
23+
os << "{" << '\n';
2424
for (const std::string& msg : msgs) {
25-
os << " \"" << msg << "\"," << std::endl;
25+
os << " \"" << msg << "\"," << '\n';
2626
}
2727
os << "}";
2828
}

0 commit comments

Comments
 (0)