Skip to content

Commit e3d296a

Browse files
authored
chore: Add Taskfile tasks to lint C++ files. (#13)
1 parent 0151da1 commit e3d296a

File tree

6 files changed

+134
-0
lines changed

6 files changed

+134
-0
lines changed

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,15 @@ cmake_minimum_required(VERSION 3.16.3)
22
project(YSTDLIB_CPP LANGUAGES CXX)
33

44
set(YSTDLIB_CPP_VERSION "0.0.1" CACHE STRING "Project version.")
5+
6+
# Enable exporting compile commands
7+
set(CMAKE_EXPORT_COMPILE_COMMANDS
8+
ON
9+
CACHE BOOL
10+
"Enable/Disable output of compile commands during generation."
11+
FORCE
12+
)
13+
14+
add_executable(dummy)
15+
target_sources(dummy PRIVATE src/ystdlib/hello.cpp)
16+
target_compile_features(dummy PRIVATE cxx_std_20)

lint-requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
clang-format>=19.1.6
2+
clang-tidy>=19.1.0
13
colorama>=0.4.6
24
gersemi>=0.16.2
35
yamllint>=1.35.1

src/ystdlib/hello.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <iostream>
2+
3+
[[nodiscard]] auto main() -> int {
4+
std::cout << "Hello, world!" << '\n';
5+
return 0;
6+
}

taskfile.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,28 @@ includes:
66

77
vars:
88
G_BUILD_DIR: "{{.ROOT_DIR}}/build"
9+
G_CMAKE_CACHE: "{{.G_BUILD_DIR}}/CMakeCache.txt"
10+
G_COMPILE_COMMANDS_DB: "{{.G_BUILD_DIR}}/compile_commands.json"
11+
# Project-specific variables
12+
G_YSTDLIB_CPP_SRC_DIR: "{{.ROOT_DIR}}/src/ystdlib"
913

1014
tasks:
1115
clean:
1216
desc: "Removes the project build directory."
1317
cmds:
1418
- "rm -rf '{{.G_BUILD_DIR}}'"
1519

20+
config-cmake-project:
21+
internal: true
22+
sources:
23+
- "CMakeLists.txt"
24+
- "{{.TASKFILE}}"
25+
generates:
26+
- "{{.G_CMAKE_CACHE}}"
27+
- "{{.G_COMPILE_COMMANDS_DB}}"
28+
cmds:
29+
- "cmake -S '{{.ROOT_DIR}}' -B '{{.G_BUILD_DIR}}'"
30+
1631
init:
1732
internal: true
1833
silent: true

taskfiles/lint-cpp.yaml

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
version: "3"
2+
3+
tasks:
4+
cpp-check:
5+
desc: "Runs the C++ linters."
6+
cmds:
7+
- task: "cpp-format-check"
8+
- task: "cpp-static-check"
9+
10+
cpp-fix:
11+
desc: "Runs the C++ linters to fix all formatting issues and perform static code analysis. "
12+
cmds:
13+
- task: "cpp-format-fix"
14+
- task: "cpp-static-fix"
15+
16+
cpp-format-check:
17+
desc: "Runs the C++ linters that identify formatting issues."
18+
sources: &cpp_format_src_files
19+
- "{{.G_LINT_VENV_CHECKSUM_FILE}}"
20+
- "{{.G_YSTDLIB_CPP_SRC_DIR}}/**/*.cpp"
21+
- "{{.G_YSTDLIB_CPP_SRC_DIR}}/**/*.h"
22+
- "{{.G_YSTDLIB_CPP_SRC_DIR}}/**/*.hpp"
23+
- "{{.ROOT_DIR}}/.clang-format"
24+
- "{{.TASKFILE}}"
25+
deps:
26+
- "cpp-configs"
27+
- "venv"
28+
cmds:
29+
- task: ":utils:clang-format"
30+
vars:
31+
FLAGS: "--dry-run"
32+
SRC_PATHS:
33+
ref: ".G_LINT_CPP_DIRS"
34+
VENV_DIR: "{{.G_LINT_VENV_DIR}}"
35+
36+
cpp-format-fix:
37+
desc: "Runs the C++ linters and fixes all formatting issues."
38+
sources: *cpp_format_src_files
39+
deps:
40+
- "cpp-configs"
41+
- "venv"
42+
cmds:
43+
- task: ":utils:clang-format"
44+
vars:
45+
FLAGS: "-i"
46+
SRC_PATHS:
47+
ref: ".G_LINT_CPP_DIRS"
48+
VENV_DIR: "{{.G_LINT_VENV_DIR}}"
49+
50+
cpp-static-check:
51+
# Alias task to `cpp-static-fix` since we don't currently support automatic fixes.
52+
# NOTE: clang-tidy does have the ability to fix some errors, but the fixes can be inaccurate.
53+
# When we eventually determine which errors can be safely fixed, we'll allow clang-tidy to
54+
# fix them.
55+
desc: "Runs the C++ static analyzers. Only checks for warnings and violations."
56+
aliases:
57+
- "cpp-static-fix"
58+
sources:
59+
- "{{.G_CMAKE_CACHE}}"
60+
- "{{.G_COMPILE_COMMANDS_DB}}"
61+
- "{{.G_LINT_VENV_CHECKSUM_FILE}}"
62+
- "{{.G_YSTDLIB_CPP_SRC_DIR}}/**/*.cpp"
63+
- "{{.G_YSTDLIB_CPP_SRC_DIR}}/**/*.h"
64+
- "{{.G_YSTDLIB_CPP_SRC_DIR}}/**/*.hpp"
65+
- "{{.ROOT_DIR}}/.clang-tidy"
66+
- "{{.TASKFILE}}"
67+
deps:
68+
- ":config-cmake-project"
69+
- "cpp-configs"
70+
- "venv"
71+
cmds:
72+
- task: ":utils:clang-tidy"
73+
vars:
74+
FLAGS: >-
75+
--config-file "{{.ROOT_DIR}}/.clang-tidy"
76+
-p "{{.G_COMPILE_COMMANDS_DB}}"
77+
SRC_PATHS:
78+
ref: ".G_LINT_CPP_DIRS"
79+
VENV_DIR: "{{.G_LINT_VENV_DIR}}"
80+
81+
cpp-configs:
82+
internal: true
83+
sources:
84+
- "{{.ROOT_DIR}}/tools/yscope-dev-utils/lint-configs/.clang-format"
85+
- "{{.ROOT_DIR}}/tools/yscope-dev-utils/lint-configs/.clang-tidy"
86+
- "{{.TASKFILE}}"
87+
generates:
88+
- "{{.ROOT_DIR}}/.clang-format"
89+
- "{{.ROOT_DIR}}/.clang-tidy"
90+
dir: "{{.ROOT_DIR}}"
91+
cmds:
92+
- "tools/yscope-dev-utils/lint-configs/symlink-cpp-lint-configs.sh"

taskfiles/lint.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,32 @@ includes:
44
cmake:
55
flatten: true
66
taskfile: "./lint-cmake.yaml"
7+
cpp:
8+
flatten: true
9+
taskfile: "./lint-cpp.yaml"
710
yaml:
811
flatten: true
912
taskfile: "./lint-yaml.yaml"
1013

1114
vars:
1215
G_LINT_VENV_DIR: "{{.G_BUILD_DIR}}/lint-venv"
1316
G_LINT_VENV_CHECKSUM_FILE: "{{.G_BUILD_DIR}}/lint#venv.md5"
17+
G_LINT_CPP_DIRS:
18+
- "{{.G_YSTDLIB_CPP_SRC_DIR}}"
1419

1520
tasks:
1621
check:
1722
desc: "Runs the full suite of linters to identify warnings and violations."
1823
cmds:
1924
- task: "cmake-check"
25+
- task: "cpp-check"
2026
- task: "yaml-check"
2127

2228
fix:
2329
desc: "Runs the full suite of linters and fixes some violations."
2430
cmds:
2531
- task: "cmake-fix"
32+
- task: "cpp-fix"
2633
- task: "yaml-fix"
2734

2835
venv:

0 commit comments

Comments
 (0)