From a6bb121dafbd993afca0b7e5aca31487f5a76ef1 Mon Sep 17 00:00:00 2001 From: Kirk Rodrigues <2454684+kirkrodrigues@users.noreply.github.com> Date: Wed, 23 Oct 2024 23:50:30 -0400 Subject: [PATCH 1/2] chore: Add Taskfile tasks to lint C++ files. --- .gitignore | 4 ++ README.md | 18 +++++--- lint-requirements.txt | 3 ++ lint-tasks.yaml | 89 +++++++++++++++++++++++++++++++++++++++- src/spider/.clang-format | 20 +++++++++ src/spider/spider.cpp | 4 +- taskfile.yaml | 14 +++++++ 7 files changed, 143 insertions(+), 9 deletions(-) create mode 100644 src/spider/.clang-format diff --git a/.gitignore b/.gitignore index 78018ddd6..094bd633c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,9 @@ .task build +# Generated lint configs +.clang-format +.clang-tidy + # IDE-related directories and files .idea diff --git a/README.md b/README.md index ce347c0d8..b2afa134f 100644 --- a/README.md +++ b/README.md @@ -36,11 +36,17 @@ The commands above run all linting checks, but for performance you may want to r if you only changed C++ files, you don't need to run the YAML linting checks) using one of the tasks in the table below. -| Task | Description | -|--------------------|--------------------------------------------------| -| `lint:cmake-check` | Runs the CMake linters. | -| `lint:cmake-fix` | Runs the CMake linters and fixes any violations. | -| `lint:yml-check` | Runs the YAML linters. | -| `lint:yml-fix` | Runs the YAML linters and fixes some violations. | +| Task | Description | +|-------------------------|----------------------------------------------------------| +| `lint:cmake-check` | Runs the CMake linters. | +| `lint:cmake-fix` | Runs the CMake linters and fixes any violations. | +| `lint:cpp-check` | Runs the C++ linters (formatters and static analyzers). | +| `lint:cpp-fix` | Runs the C++ linters and fixes some violations. | +| `lint:cpp-format-check` | Runs the C++ formatters. | +| `lint:cpp-format-fix` | Runs the C++ formatters and fixes some violations. | +| `lint:cpp-static-check` | Runs the C++ static analyzers. | +| `lint:cpp-static-fix` | Runs the C++ static analyzers and fixes some violations. | +| `lint:yml-check` | Runs the YAML linters. | +| `lint:yml-fix` | Runs the YAML linters and fixes some violations. | [Task]: https://taskfile.dev diff --git a/lint-requirements.txt b/lint-requirements.txt index 2c40fe328..06301bd4f 100644 --- a/lint-requirements.txt +++ b/lint-requirements.txt @@ -1,2 +1,5 @@ +# Lock to v18.x until we can upgrade our code to meet v19's formatting standards. +clang-format~=18.1 +clang-tidy>=19.1.0 gersemi>=0.16.2 yamllint>=1.35.1 diff --git a/lint-tasks.yaml b/lint-tasks.yaml index df4a58769..b2fbc3db9 100644 --- a/lint-tasks.yaml +++ b/lint-tasks.yaml @@ -2,16 +2,19 @@ version: "3" vars: G_LINT_VENV_DIR: "{{.G_BUILD_DIR}}/lint-venv" + G_LINT_VENV_CHECKSUM_FILE: "{{.G_BUILD_DIR}}/lint#venv.md5" tasks: check: cmds: - task: "cmake-check" + - task: "cpp-check" - task: "yml-check" fix: cmds: - task: "cmake-fix" + - task: "cpp-fix" - task: "yml-fix" cmake-check: @@ -28,6 +31,66 @@ tasks: vars: FLAGS: "--in-place" + cpp-configs: "tools/yscope-dev-utils/lint-configs/symlink-cpp-lint-configs.sh" + + cpp-check: + cmds: + - task: "cpp-format-check" + - task: "cpp-static-check" + + cpp-fix: + cmds: + - task: "cpp-format-fix" + - task: "cpp-static-fix" + + cpp-format-check: + sources: &cpp_format_src_files + - "{{.G_LINT_VENV_CHECKSUM_FILE}}" + - "{{.G_SRC_SPIDER_DIR}}/.clang-format" + - "{{.G_SRC_SPIDER_DIR}}/**/*.cpp" + - "{{.G_SRC_SPIDER_DIR}}/**/*.h" + - "{{.G_SRC_SPIDER_DIR}}/**/*.hpp" + - "{{.TASKFILE}}" + - "tools/yscope-dev-utils/lint-configs/.clang-format" + deps: ["cpp-configs", "venv"] + cmds: + - task: "clang-format" + vars: + FLAGS: "--dry-run" + SRC_DIR: "{{.G_SRC_SPIDER_DIR}}" + + cpp-format-fix: + sources: *cpp_format_src_files + deps: ["cpp-configs", "venv"] + cmds: + - task: "clang-format" + vars: + FLAGS: "-i" + SRC_DIR: "{{.G_SRC_SPIDER_DIR}}" + + cpp-static-check: + # Alias task to `cpp-static-fix` since we don't currently support automatic fixes. + # NOTE: clang-tidy does have the ability to fix some errors, but the fixes can be inaccurate. + # When we eventually determine which errors can be safely fixed, we'll allow clang-tidy to + # fix them. + aliases: ["cpp-static-fix"] + sources: + - "{{.G_LINT_VENV_CHECKSUM_FILE}}" + - "{{.G_SRC_SPIDER_DIR}}/**/*.cpp" + - "{{.G_SRC_SPIDER_DIR}}/**/*.h" + - "{{.G_SRC_SPIDER_DIR}}/**/*.hpp" + - "{{.G_SPIDER_CMAKE_CACHE}}" + - "{{.G_SPIDER_COMPILE_COMMANDS_DB}}" + - "{{.TASKFILE}}" + - "Taskfile.yml" + - "tools/yscope-dev-utils/lint-configs/.clang-tidy" + deps: [":config-cmake-project", "cpp-configs", "venv"] + cmds: + - task: "clang-tidy" + vars: + FLAGS: "--config-file=.clang-tidy -p {{.G_SPIDER_COMPILE_COMMANDS_DB}}" + SRC_DIR: "{{.G_SRC_SPIDER_DIR}}" + yml: aliases: - "yml-check" @@ -45,6 +108,30 @@ tasks: lint-tasks.yaml \ taskfile.yaml + clang-format: + internal: true + requires: + vars: ["FLAGS", "SRC_DIR"] + cmd: |- + . "{{.G_LINT_VENV_DIR}}/bin/activate" + find "{{.SRC_DIR}}" \ + -type f \ + \( -iname "*.cpp" -o -iname "*.h" -o -iname "*.hpp" \) \ + -print0 | \ + xargs -0 --no-run-if-empty clang-format {{.FLAGS}} -Werror + + clang-tidy: + internal: true + requires: + vars: ["FLAGS", "SRC_DIR"] + cmd: |- + . "{{.G_LINT_VENV_DIR}}/bin/activate" + find "{{.SRC_DIR}}" \ + -type f \ + \( -iname "*.cpp" -o -iname "*.h" -o -iname "*.hpp" \) \ + -print0 | \ + xargs -0 --no-run-if-empty clang-tidy {{.FLAGS}} + cmake: internal: true requires: @@ -60,7 +147,7 @@ tasks: venv: internal: true vars: - CHECKSUM_FILE: "{{.G_BUILD_DIR}}/{{.TASK | replace \":\" \"#\"}}.md5" + CHECKSUM_FILE: "{{.G_LINT_VENV_CHECKSUM_FILE}}" OUTPUT_DIR: "{{.G_LINT_VENV_DIR}}" sources: - "{{.ROOT_DIR}}/taskfile.yaml" diff --git a/src/spider/.clang-format b/src/spider/.clang-format new file mode 100644 index 000000000..910a7657b --- /dev/null +++ b/src/spider/.clang-format @@ -0,0 +1,20 @@ +BasedOnStyle: "InheritParentConfig" + +IncludeCategories: + # NOTE: A header is grouped by first matching regex + # Project headers + - Regex: "^" + Priority: 1 + # C++ standard libraries + - Regex: "^<.+>" + Priority: 2 diff --git a/src/spider/spider.cpp b/src/spider/spider.cpp index 54e47ecd1..3f80fc3dc 100644 --- a/src/spider/spider.cpp +++ b/src/spider/spider.cpp @@ -1,6 +1,6 @@ #include -int main() { - std::cout << "Hello, world!" << std::endl; +auto main() -> int { + std::cout << "Hello, world!" << '\n'; return 0; } diff --git a/taskfile.yaml b/taskfile.yaml index 1b846c637..6b8904eab 100644 --- a/taskfile.yaml +++ b/taskfile.yaml @@ -6,12 +6,26 @@ includes: vars: G_BUILD_DIR: "{{.ROOT_DIR}}/build" + G_BUILD_SPIDER_DIR: "{{.G_BUILD_DIR}}/spider" + G_SPIDER_CMAKE_CACHE: "{{.G_BUILD_SPIDER_DIR}}/CMakeCache.txt" + G_SPIDER_COMPILE_COMMANDS_DB: "{{.G_BUILD_SPIDER_DIR}}/compile_commands.json" + G_SRC_SPIDER_DIR: "{{.ROOT_DIR}}/src/spider" tasks: clean: cmds: - "rm -rf '{{.G_BUILD_DIR}}'" + config-cmake-project: + internal: true + sources: + - "{{.TASKFILE}}" + - "CMakeLists.txt" + generates: + - "{{.G_SPIDER_CMAKE_CACHE}}" + - "{{.G_SPIDER_COMPILE_COMMANDS_DB}}" + cmd: "cmake -S '{{.ROOT_DIR}}' -B '{{.G_BUILD_SPIDER_DIR}}'" + init: internal: true silent: true From b8d96d630885d702a0aac2ad96dbff45c89dfd6e Mon Sep 17 00:00:00 2001 From: Kirk Rodrigues <2454684+kirkrodrigues@users.noreply.github.com> Date: Thu, 24 Oct 2024 00:02:48 -0400 Subject: [PATCH 2/2] Add setup step for C++ linting configs. --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index b2afa134f..957b59d8e 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,11 @@ Initialize and update submodules: git submodule update --init --recursive ``` +Set up the config files for our C++ linting tools: +```shell +task lint:cpp-configs +``` + ## Adding files Certain file types need to be added to our linting rules manually: