Skip to content

Commit 62bea9b

Browse files
committed
Add clang-tidy and clang-format tools + update CI workflows
1 parent c82b828 commit 62bea9b

File tree

8 files changed

+73
-23
lines changed

8 files changed

+73
-23
lines changed

.github/workflows/clang-format.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ jobs:
2626
uses: actions/checkout@v4
2727

2828
- name: Install clang-format
29-
run: sudo apt update && sudo apt install -y clang-format
29+
run: sudo apt update && sudo apt install -y clang-format cmake ninja-build g++ gcc
3030

31-
- name: Run clang-format check
32-
run: |
33-
FILES=$(find . \( -name '*.cpp' -o -name '*.hpp' \) -not -path "./build/*")
34-
clang-format --dry-run --Werror $FILES
31+
- name: Configure project
32+
run: cmake --preset gcc-RelWithDebInfo
33+
34+
- name: Run clang-format
35+
run: cmake --build build/gcc-RelWithDebInfo/ --target clang-format

.github/workflows/clang-tidy.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,4 @@ jobs:
3636
run: cmake --preset gcc-RelWithDebInfo
3737

3838
- name: Run clang-tidy
39-
run: |
40-
find src include tests -name '*.cpp' | xargs clang-tidy -p build/gcc-RelWithDebInfo
39+
run: cmake --build build/gcc-RelWithDebInfo/ --target clang-tidy

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,9 @@ include(tools/Doxygen)
5353

5454
# === Installation ===
5555
include(install/InstallConfig)
56+
57+
# === Formatting ===
58+
include(tools/ClangFormat)
59+
60+
# === Linting ===
61+
include(tools/ClangTidy)

cmake/tools/ClangFormat.cmake

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
find_program(CLANG_FORMAT_EXE NAMES clang-format)
2+
3+
if(CLANG_FORMAT_EXE)
4+
5+
# Collect all relevant source/header files recursively
6+
file(GLOB_RECURSE ALL_SOURCE_FILES
7+
"${CMAKE_SOURCE_DIR}/src/*.cpp"
8+
"${CMAKE_SOURCE_DIR}/src/**/*.cpp"
9+
"${CMAKE_SOURCE_DIR}/include/*.hpp"
10+
"${CMAKE_SOURCE_DIR}/include/**/*.hpp"
11+
"${CMAKE_SOURCE_DIR}/tests/*.cpp"
12+
"${CMAKE_SOURCE_DIR}/tests/**/*.cpp"
13+
"${CMAKE_SOURCE_DIR}/tests/*.hpp"
14+
"${CMAKE_SOURCE_DIR}/tests/**/*.hpp"
15+
)
16+
17+
add_custom_target(clang-format
18+
COMMAND ${CLANG_FORMAT_EXE} --dry-run --Werror ${ALL_SOURCE_FILES}
19+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
20+
COMMENT "🔍 Checking format (clang-format --dry-run --Werror)"
21+
VERBATIM
22+
)
23+
else()
24+
message(STATUS "⚠️\tclang-format not found — 'clang-format' target will not be available.")
25+
endif()

cmake/tools/ClangTidy.cmake

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
find_program(CLANG_TIDY_EXE NAMES clang-tidy)
2+
3+
if(CLANG_TIDY_EXE)
4+
file(GLOB_RECURSE TIDY_SOURCE_FILES
5+
"${CMAKE_SOURCE_DIR}/src/*.cpp"
6+
"${CMAKE_SOURCE_DIR}/src/**/*.cpp"
7+
"${CMAKE_SOURCE_DIR}/tests/*.cpp"
8+
"${CMAKE_SOURCE_DIR}/tests/**/*.cpp"
9+
)
10+
11+
add_custom_target(clang-tidy
12+
COMMAND ${CLANG_TIDY_EXE} ${TIDY_SOURCE_FILES} --quiet -p ${CMAKE_BINARY_DIR}
13+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
14+
COMMENT "🔍 Running clang-tidy analysis"
15+
VERBATIM
16+
)
17+
else()
18+
message(STATUS "⚠️ clang-tidy not found — 'clang-tidy' target will not be available.")
19+
endif()

include/math/arithmetic.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ namespace math
1212
/**
1313
* @brief Adds two integers.
1414
*
15-
* @param a First integer.
16-
* @param b Second integer.
17-
* @return Sum of a and b.
15+
* @param left First integer.
16+
* @param right Second integer.
17+
* @return Sum of left and right.
1818
*/
19-
int add(int a, int b);
19+
int add(int left, int right);
2020

2121
/**
2222
* @brief Subtracts two integers.
2323
*
24-
* @param a First integer.
25-
* @param b Second integer.
26-
* @return Difference of a and b.
24+
* @param left First integer.
25+
* @param right Second integer.
26+
* @return Difference of left and right.
2727
*/
28-
int subtract(int a, int b);
28+
int subtract(int left, int right);
2929

3030
} // namespace math
3131

src/arithmetic.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
namespace math
44
{
55

6-
int add(int a, int b)
6+
int add(int left, int right)
77
{
8-
return a + b;
8+
return left + right;
99
}
1010

11-
int subtract(int a, int b)
11+
int subtract(int left, int right)
1212
{
13-
return a - b;
13+
return left - right;
1414
}
1515

1616
} // namespace math

src/main.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
int main()
66
{
7-
const int a = 10;
8-
const int b = 4;
7+
const int left = 10;
8+
const int right = 4;
99

10-
std::cout << "Add: " << math::add(a, b) << '\n';
11-
std::cout << "Subtract: " << math::subtract(a, b) << '\n';
10+
std::cout << "Add: " << math::add(left, right) << '\n';
11+
std::cout << "Subtract: " << math::subtract(left, right) << '\n';
1212

1313
return 0;
1414
}

0 commit comments

Comments
 (0)