Skip to content

Commit 7beba3c

Browse files
Feature/cwm/4 add clang format and clang tidy configuration (#9)
* Add scripts and checks * Run clang-format * Fixing linter errors * Fix linting errors, don't analyze hpp files * Pin clang-format version * Check version of clang-format * Fix script
1 parent 21a8c0d commit 7beba3c

11 files changed

Lines changed: 106 additions & 21 deletions

File tree

.clang-format

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
BasedOnStyle: Google
2+
ColumnLimit: 160
3+
IndentWidth: 4
4+
AccessModifierOffset: -4
5+
BreakBeforeBraces: Allman
6+
SortIncludes: true
7+
AllowShortFunctionsOnASingleLine: Empty
8+
DerivePointerAlignment: false
9+
PointerAlignment: Left

.clang-tidy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Checks: >
2+
bugprone-*,
3+
modernize-*,
4+
performance-*,
5+
readability-*,
6+
cppcoreguidelines-*,
7+
misc-*,
8+
-modernize-use-trailing-return-type,
9+
-readability-magic-numbers,
10+
-cppcoreguidelines-avoid-magic-numbers
11+
WarningsAsErrors: '*'

.github/workflows/ci.yml

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,35 @@ jobs:
7171
run: ./build/cpuscope_cli --version
7272

7373
lint:
74-
name: Lint (Placeholder)
74+
name: Lint
7575
runs-on: ubuntu-latest
7676
steps:
7777
- uses: actions/checkout@v4
78-
- name: Placeholder lint
79-
run: echo "Lint checks will be added later"
78+
79+
- name: Install lint dependencies
80+
run: |
81+
sudo apt-get update
82+
sudo apt-get install -y cmake ninja-build ccache clang-format-18 clang-tidy clang-17
83+
84+
- name: Check clang-format version
85+
run: clang-format --version
86+
87+
- name: Configure compile commands
88+
run: |
89+
mkdir -p build
90+
cmake -S . -B build -G Ninja \
91+
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
92+
-DCPUSCOPE_BUILD_TESTS=ON \
93+
-DCMAKE_C_COMPILER=clang-17 \
94+
-DCMAKE_CXX_COMPILER=clang++-17 \
95+
-DCMAKE_C_COMPILER_LAUNCHER=ccache \
96+
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache
97+
98+
- name: Check formatting
99+
run: scripts/check-format.sh
100+
101+
- name: Run static analysis
102+
run: scripts/lint.sh
80103

81104
sanitizer:
82105
name: Sanitizer Build

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.25)
22

33
project(cpuscope VERSION 0.1.0 LANGUAGES C CXX)
44

5+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
6+
57
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
68
message(FATAL_ERROR "In-source builds are not allowed. Please use a separate build directory.")
79
endif()

cli/main.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
#include <iostream>
2-
#include <vector>
2+
#include <span>
33
#include <string_view>
4+
#include <vector>
45

56
#include "cpuscope_lib.hpp"
67

7-
int main(int argc, char *argv[])
8+
int main(int argc, char* argv[])
89
{
910
std::vector<std::string_view> args;
1011
args.reserve(argc);
11-
for (int i = 0; i < argc; ++i)
12+
13+
const std::span<char*> arguments(argv, argc);
14+
for (char* arg : arguments)
1215
{
13-
args.emplace_back(argv[i]);
16+
args.emplace_back(arg);
1417
}
1518

1619
std::cout << cpuscope::format_message(args) << '\n';

lib/include/cpuscope_lib.hpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
#include <span>
55
#include <string>
66
#include <string_view>
7-
#include <vector>
87

98
namespace cpuscope
109
{
1110

12-
inline std::string format_message(std::span<const std::string_view> args)
13-
{
14-
return std::format("CPUScope CLI placeholder: {} arguments received.", args.size());
15-
}
11+
inline std::string format_message(std::span<const std::string_view> args)
12+
{
13+
return std::format("CPUScope CLI placeholder: {} arguments received.", args.size());
14+
}
1615

17-
} // namespace cpuscope
16+
} // namespace cpuscope

lib/include/version.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
namespace cpuscope
66
{
77

8-
std::string get_version()
9-
{
10-
return "0.1.0";
11-
}
8+
inline std::string get_version()
9+
{
10+
return "0.1.0";
11+
}
1212

13-
} // namespace cpuscope
13+
} // namespace cpuscope

lib/src/cpuscope_lib.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
#include "cpuscope_lib.hpp"
1+
// Linter error: Will be resolved once the library has more functionality and is no longer a
2+
// placeholder.
3+
// #include "cpuscope_lib.hpp"
24

35
namespace cpuscope
46
{
57

6-
// Implementation is header-only for the initial placeholder functionality.
8+
// Implementation is header-only for the initial placeholder functionality.
79

8-
} // namespace cpuscope
10+
} // namespace cpuscope

scripts/check-format.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
FAILED=0
5+
6+
while IFS= read -r -d '' file; do
7+
if ! diff -u "$file" <(clang-format "$file"); then
8+
FAILED=1
9+
fi
10+
done < <(
11+
find . \
12+
\( -path ./build -o -path ./build-\* -o -path ./.git -o -path ./scripts \) -prune \
13+
-o \( -name "*.cpp" -o -name "*.hpp" \) -print0
14+
)
15+
16+
if [ "$FAILED" -ne 0 ]; then
17+
echo "Formatting issues detected."
18+
exit 1
19+
fi

scripts/format.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
find . \( -name "*.cpp" -o -name "*.hpp" \) -exec clang-format -i {} +

0 commit comments

Comments
 (0)