Skip to content

Commit 3316223

Browse files
committed
Add tidy target for running clang-tidy
1 parent fe8d8a9 commit 3316223

File tree

5 files changed

+102
-13
lines changed

5 files changed

+102
-13
lines changed

.clang-tidy

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Checks: >
2+
bugprone-*,
3+
clang-analyzer-*,
4+
-bugprone-easily-swappable-parameters,
5+
-bugprone-narrowing-conversions,
6+
-clang-analyzer-core.NonNullParamChecker,
7+
-clang-analyzer-security.insecureAPI.strcpy,
8+
HeaderFilterRegex: '.*'
9+
WarningsAsErrors: '*'
10+
UseColor: true

.github/workflows/ci.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,38 @@ jobs:
6868

6969
- name: Test
7070
run: ctest --test-dir CSFML/build -C ${{matrix.type.name}} --output-on-failure
71+
72+
tidy:
73+
name: Analyze
74+
runs-on: macos-14
75+
76+
steps:
77+
- name: Install Dependencies
78+
run: |
79+
brew update
80+
brew install llvm ninja
81+
echo /opt/homebrew/opt/llvm/bin >> $GITHUB_PATH
82+
83+
- name: Checkout SFML
84+
uses: actions/checkout@v4
85+
with:
86+
repository: SFML/SFML
87+
ref: master
88+
path: SFML
89+
90+
- name: Configure SFML CMake
91+
run: cmake -S SFML -B SFML/build -GNinja -DCMAKE_INSTALL_PREFIX=SFML/install -DBUILD_SHARED_LIBS=TRUE -DCMAKE_BUILD_TYPE=Debug
92+
93+
- name: Build SFML
94+
run: cmake --build SFML/build --target install
95+
96+
- name: Checkout CSFML
97+
uses: actions/checkout@v4
98+
with:
99+
path: CSFML
100+
101+
- name: Configure CSFML CMake
102+
run: cmake -S CSFML -B CSFML/build -DBUILD_SHARED_LIBS=TRUE -DCSFML_BUILD_EXAMPLES=TRUE -DCSFML_BUILD_TEST_SUITE=TRUE -DSFML_DIR=$GITHUB_WORKSPACE/SFML/install/lib/cmake/SFML -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=TRUE
103+
104+
- name: Tidy
105+
run: cmake --build CSFML/build --target tidy

CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ install(DIRECTORY include/
9595
install(FILES license.md DESTINATION ${INSTALL_MISC_DIR})
9696
install(FILES readme.md DESTINATION ${INSTALL_MISC_DIR})
9797

98+
# stop configuration if consuming CSFML as a subdirectory
99+
if(NOT PROJECT_IS_TOP_LEVEL)
100+
return()
101+
endif()
102+
98103
# add an option for building the examples
99104
csfml_set_option(CSFML_BUILD_EXAMPLES FALSE BOOL "TRUE to build the CSFML examples, FALSE to ignore them")
100105
if(CSFML_BUILD_EXAMPLES)
@@ -107,3 +112,8 @@ if(CSFML_BUILD_TEST_SUITE)
107112
enable_testing()
108113
add_subdirectory(test)
109114
endif()
115+
116+
csfml_set_option(CLANG_TIDY_EXECUTABLE clang-tidy STRING "Override clang-tidy executable, requires minimum version 14")
117+
add_custom_target(tidy
118+
COMMAND ${CMAKE_COMMAND} -DCLANG_TIDY_EXECUTABLE=${CLANG_TIDY_EXECUTABLE} -DPROJECT_BINARY_DIR=${PROJECT_BINARY_DIR} -P ./cmake/Tidy.cmake
119+
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} VERBATIM)

cmake/Tidy.cmake

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Check executable exists
2+
if(NOT EXISTS ${CLANG_TIDY_EXECUTABLE})
3+
find_program(CLANG_TIDY_EXEC_TEMP ${CLANG_TIDY_EXECUTABLE})
4+
if(CLANG_TIDY_EXEC_TEMP)
5+
set(CLANG_TIDY_EXECUTABLE ${CLANG_TIDY_EXEC_TEMP})
6+
unset(CLANG_TIDY_EXEC_TEMP)
7+
else()
8+
message(FATAL_ERROR "Unable to find clang-tidy executable: \"${CLANG_TIDY_EXECUTABLE}\"")
9+
endif()
10+
endif()
11+
12+
# Check executable version
13+
execute_process(COMMAND ${CLANG_TIDY_EXECUTABLE} --version OUTPUT_VARIABLE CLANG_TIDY_VERSION)
14+
string(REGEX MATCH "version ([0-9]+)" CLANG_TIDY_VERSION ${CLANG_TIDY_VERSION})
15+
unset(CLANG_TIDY_VERSION)
16+
if(CMAKE_MATCH_1 GREATER_EQUAL 14)
17+
message(STATUS "Using clang-tidy version ${CMAKE_MATCH_1}")
18+
else()
19+
message(FATAL_ERROR "clang-tidy version ${CMAKE_MATCH_1} is too low")
20+
endif()
21+
22+
# Find Python and run-clang-tidy script
23+
find_package(Python 3 REQUIRED)
24+
25+
find_program(RUN_CLANG_TIDY run-clang-tidy)
26+
if(NOT RUN_CLANG_TIDY)
27+
message(FATAL_ERROR "Failed to find run-clang-tidy script")
28+
endif()
29+
30+
# Run
31+
execute_process(COMMAND ${Python_EXECUTABLE} ${RUN_CLANG_TIDY} -clang-tidy-binary ${CLANG_TIDY_EXECUTABLE} -quiet -p ${PROJECT_BINARY_DIR} RESULTS_VARIABLE EXIT_CODE)
32+
if(NOT EXIT_CODE STREQUAL 0)
33+
message(FATAL_ERROR "Analysis failed")
34+
endif()

src/CSFML/Internal.hpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#define CSFML_CHECK(object_) \
3737
do \
3838
{ \
39-
if (object_ == nullptr) \
39+
if ((object_) == nullptr) \
4040
{ \
4141
sf::err() << "SFML warning: trying to use a null " #object_ " object\n"; \
4242
return; \
@@ -49,7 +49,7 @@
4949
{ \
5050
if (object_) \
5151
{ \
52-
(object_->This.function_); \
52+
((object_)->This.function_); \
5353
} \
5454
else \
5555
{ \
@@ -63,7 +63,7 @@
6363
{ \
6464
if (object_) \
6565
{ \
66-
(object_->This->function_); \
66+
((object_)->This->function_); \
6767
} \
6868
else \
6969
{ \
@@ -75,7 +75,7 @@
7575
#define CSFML_CHECK_RETURN(object_, default_) \
7676
do \
7777
{ \
78-
if (object_ == nullptr) \
78+
if ((object_) == nullptr) \
7979
{ \
8080
sf::err() << "SFML warning: trying to use a null " #object_ " object\n"; \
8181
return default_; \
@@ -86,7 +86,7 @@
8686
#define CSFML_CALL_RETURN(object_, function_, default_) \
8787
if (object_) \
8888
{ \
89-
return (object_->This.function_); \
89+
return ((object_)->This.function_); \
9090
} \
9191
else \
9292
{ \
@@ -97,7 +97,7 @@
9797
#define CSFML_CALL_PTR_RETURN(object_, function_, default_) \
9898
if (object_) \
9999
{ \
100-
return (object_->This->function_); \
100+
return ((object_)->This->function_); \
101101
} \
102102
else \
103103
{ \
@@ -110,24 +110,24 @@
110110
#define CSFML_CHECK(object_)
111111

112112
#define CSFML_CALL(object_, function_) \
113-
(object_->This.function_)
113+
((object_)->This.function_)
114114

115115
#define CSFML_CALL_PTR(object_, function_) \
116-
(object_->This->function_)
116+
((object_)->This->function_)
117117

118118
#define CSFML_CHECK_RETURN(object_, default_) \
119-
(void)default_;
119+
(void)(default_);
120120

121121
#define CSFML_CALL_RETURN(object_, function_, default_) \
122122
{ \
123-
(void)default_; \
124-
return (object_->This.function_); \
123+
(void)(default_); \
124+
return ((object_)->This.function_); \
125125
}
126126

127127
#define CSFML_CALL_PTR_RETURN(object_, function_, default_) \
128128
{ \
129-
(void)default_; \
130-
return (object_->This->function_); \
129+
(void)(default_); \
130+
return ((object_)->This->function_); \
131131
}
132132

133133
#endif

0 commit comments

Comments
 (0)