From c1f9a568aea740045cb405e2e92b7442ba25e12a Mon Sep 17 00:00:00 2001 From: FrozenlemonTee Date: Fri, 13 Mar 2026 11:07:48 +0800 Subject: [PATCH 1/6] refactor: Make a new dir 'config' to store config files --- .clang-format => config/.clang-format | 0 .clang-tidy => config/.clang-tidy | 0 .editorconfig => config/.editorconfig | 0 .gitattributes => config/.gitattributes | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename .clang-format => config/.clang-format (100%) rename .clang-tidy => config/.clang-tidy (100%) rename .editorconfig => config/.editorconfig (100%) rename .gitattributes => config/.gitattributes (100%) diff --git a/.clang-format b/config/.clang-format similarity index 100% rename from .clang-format rename to config/.clang-format diff --git a/.clang-tidy b/config/.clang-tidy similarity index 100% rename from .clang-tidy rename to config/.clang-tidy diff --git a/.editorconfig b/config/.editorconfig similarity index 100% rename from .editorconfig rename to config/.editorconfig diff --git a/.gitattributes b/config/.gitattributes similarity index 100% rename from .gitattributes rename to config/.gitattributes From b301add7e32a4d3d69e65ec470ce63d27cbb3953 Mon Sep 17 00:00:00 2001 From: FrozenlemonTee Date: Fri, 13 Mar 2026 11:08:28 +0800 Subject: [PATCH 2/6] feat: Add CI workflow config file --- config/.github/workflows/code-format.yml | 114 +++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 config/.github/workflows/code-format.yml diff --git a/config/.github/workflows/code-format.yml b/config/.github/workflows/code-format.yml new file mode 100644 index 0000000..4b8d79e --- /dev/null +++ b/config/.github/workflows/code-format.yml @@ -0,0 +1,114 @@ +name: Code Format + +on: + pull_request: + branches: + - main + push: + branches: + - main + workflow_dispatch: + inputs: + mode: + description: "Work mode: check only checks, fix automatically repairs and submits" + required: true + default: check + type: choice + options: + - check + - fix + +permissions: + contents: read + +jobs: + format-check: + name: Clang-Format Check + runs-on: ubuntu-latest + if: ${{ github.event_name != 'workflow_dispatch' || inputs.mode == 'check' }} + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup LLVM (clang-format) + uses: KyleMayes/install-llvm-action@v2 + with: + version: "18" + + - name: Collect C/C++ source files + id: collect + shell: bash + run: | + set -euo pipefail + files=$(git ls-files '*.c' '*.cc' '*.cpp' '*.cxx' '*.h' '*.hpp' '*.hh' '*.hxx' '*.cppm' '*.ixx') + if [ -z "$files" ]; then + echo "has_files=false" >> "$GITHUB_OUTPUT" + echo "No C/C++ files found to check, skipping." + exit 0 + fi + echo "has_files=true" >> "$GITHUB_OUTPUT" + { + echo "files<> "$GITHUB_OUTPUT" + + - name: Run clang-format dry-run check + if: ${{ steps.collect.outputs.has_files == 'true' }} + shell: bash + run: | + set -euo pipefail + mapfile -t files < <(printf '%s\n' "${{ steps.collect.outputs.files }}") + clang-format --version + if ! clang-format --dry-run --Werror "${files[@]}"; then + echo "::error::Format check failed. Please run clang-format -i locally and commit the changes, or manually trigger workflow_dispatch and select fix." + exit 1 + fi + echo "Format check passed." + + format-fix: + name: Clang-Format Auto Fix + runs-on: ubuntu-latest + if: ${{ github.event_name == 'workflow_dispatch' && inputs.mode == 'fix' }} + permissions: + contents: write + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Setup LLVM (clang-format) + uses: KyleMayes/install-llvm-action@v2 + with: + version: "18" + + - name: Apply clang-format + shell: bash + run: | + set -euo pipefail + files=$(git ls-files '*.c' '*.cc' '*.cpp' '*.cxx' '*.h' '*.hpp' '*.hh' '*.hxx' '*.cppm' '*.ixx') + if [ -z "$files" ]; then + echo "No C/C++ files found to format." + exit 0 + fi + echo "$files" | xargs clang-format -i + + - name: Commit formatting changes + uses: stefanzweifel/git-auto-commit-action@v5 + with: + commit_message: "ci: auto-format source files by clang-format" + commit_user_name: github-actions[bot] + commit_user_email: 41898282+github-actions[bot]@users.noreply.github.com + commit_author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> + file_pattern: | + **/*.c + **/*.cc + **/*.cpp + **/*.cxx + **/*.h + **/*.hpp + **/*.hh + **/*.hxx + **/*.cppm + **/*.ixx From 05d0b2148ccbc77972a0019b822a26fbdd728a25 Mon Sep 17 00:00:00 2001 From: FrozenlemonTee Date: Fri, 13 Mar 2026 11:09:25 +0800 Subject: [PATCH 3/6] doc: Update doc for changes of config files --- README.md | 30 ++++++++++++++++++++++++------ clang-format.md | 2 +- clang-tidy.md | 2 +- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d409444..eed30a6 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ xlings install skills:mcpp-style-ref ### 使用项目的`.clang-format` -> 直接复制[.clang-format](), 或使用xlings进行安装 +> 直接复制[.clang-format](./config/.clang-format), 或使用xlings进行安装 ``` xlings install mcpp:clang-format @@ -69,8 +69,9 @@ xlings install mcpp:clang-format - 3.4 [用 optional/expected 替代 int 错误码](./README.md#34-用-optionalexpected-替代-int-错误码) - 3.5 [RAII 资源管理](./README.md#35-raii-资源管理) - 四、配置文件 - - 4.0 [Clang-Format 配置](./clang-format.md) - - 4.1 [Clang-Tidy 配置](./clang-tidy.md) + - 4.0 [Clang-Format 配置](./README.md#40-Clang-Format-配置) + - 4.1 [Clang-Tidy 配置](./README.md#41-Clang-Tidy-配置) + - 4.2 [CI/CD 工作流配置](./README.md#42-cicd-工作流配置) ## 一、标识符命名风格 @@ -800,10 +801,27 @@ struct AutoLog { ## 四、配置文件 -本章包含两个常用工具的配置说明:`clang-format`(代码格式化)和 `clang-tidy`(静态检查)。详细文档见: +本章包含社区项目模板中一些常用工具的配置说明: -- [Clang-Format 配置](./clang-format.md) — 代码格式化规则与示例。 -- [Clang-Tidy 配置](./clang-tidy.md) — 静态检查规则集合与示例。 +### 4.0 Clang-Format 配置 + +代码格式化规则与示例。 + +- [转到配置文档](./clang-format.md) + +### 4.1 Clang-Tidy 配置 + +静态检查规则集合与示例。 + +- [转到配置文档](./clang-tidy.md) + +### 4.2 CI/CD 工作流配置 + +项目提供了 GitHub Actions 工作流模板: [`config/workflows/code-format.yml`](./config/workflows/code-format.yml) + +- `pull_request` / `push` 到 `main` 时自动执行 `clang-format --dry-run --Werror`,用于格式一致性检测。 +- 若检查失败,CI 会直接报错并给出提示,提醒开发者在本地执行 `clang-format -i`。 +- 可手动触发 `workflow_dispatch`,并选择 `mode=fix`,自动格式化并回推提交(可选择式自动修复)。 --- diff --git a/clang-format.md b/clang-format.md index 278db34..525c2d1 100644 --- a/clang-format.md +++ b/clang-format.md @@ -465,7 +465,7 @@ void bar(); ## 其他配置 -下面列出一些常见的 `clang-format` 配置项以供参考(这些项也出现在仓库的 `.clang-format` 中): +下面列出一些常见的 `clang-format` 配置项以供参考(这些项也出现在仓库的 `./config/.clang-format` 中): - `SpacesInParentheses: false` — 括号内不额外加空格。 - `AllowShortLoopsOnASingleLine: false` — 禁止单行循环。 diff --git a/clang-tidy.md b/clang-tidy.md index 4ee8dec..120df2c 100644 --- a/clang-tidy.md +++ b/clang-tidy.md @@ -273,4 +273,4 @@ int* p = nullptr; - `performance-unnecessary-value-param.AllowedTypes: ''`(不额外放行类型作为按值参数) - `misc-unused-parameters.IgnoreVirtual: true`(忽略虚函数重载中的未使用参数警告) -如需在项目中使用这些设置,可参考仓库根目录的 `.clang-tidy` 文件并按需调整以纳入 CI。 +如需在项目中使用这些设置,可参考仓库的 `./config/.clang-tidy` 文件并按需调整以纳入 CI。 From f30d43dc339e5919298c7d594c062916fcab4079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B0=E6=9F=A0=E9=85=8D=E7=BB=BF=E8=8C=B6?= <64787592+FrozenLemonTee@users.noreply.github.com> Date: Fri, 13 Mar 2026 11:29:05 +0800 Subject: [PATCH 4/6] fix: Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index eed30a6..1b6c2ce 100644 --- a/README.md +++ b/README.md @@ -817,7 +817,7 @@ struct AutoLog { ### 4.2 CI/CD 工作流配置 -项目提供了 GitHub Actions 工作流模板: [`config/workflows/code-format.yml`](./config/workflows/code-format.yml) +项目提供了 GitHub Actions 工作流模板: [`config/.github/workflows/code-format.yml`](./config/.github/workflows/code-format.yml) - `pull_request` / `push` 到 `main` 时自动执行 `clang-format --dry-run --Werror`,用于格式一致性检测。 - 若检查失败,CI 会直接报错并给出提示,提醒开发者在本地执行 `clang-format -i`。 From 9f4855705387f4283c83e600c036d38246a194e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B0=E6=9F=A0=E9=85=8D=E7=BB=BF=E8=8C=B6?= <64787592+FrozenLemonTee@users.noreply.github.com> Date: Fri, 13 Mar 2026 11:41:43 +0800 Subject: [PATCH 5/6] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1b6c2ce..25c1616 100644 --- a/README.md +++ b/README.md @@ -69,8 +69,8 @@ xlings install mcpp:clang-format - 3.4 [用 optional/expected 替代 int 错误码](./README.md#34-用-optionalexpected-替代-int-错误码) - 3.5 [RAII 资源管理](./README.md#35-raii-资源管理) - 四、配置文件 - - 4.0 [Clang-Format 配置](./README.md#40-Clang-Format-配置) - - 4.1 [Clang-Tidy 配置](./README.md#41-Clang-Tidy-配置) + - 4.0 [Clang-Format 配置](./README.md#40-clang-format-配置) + - 4.1 [Clang-Tidy 配置](./README.md#41-clang-tidy-配置) - 4.2 [CI/CD 工作流配置](./README.md#42-cicd-工作流配置) ## 一、标识符命名风格 From 422761ce35eaa338e5e0084625b6664d931bb1ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B0=E6=9F=A0=E9=85=8D=E7=BB=BF=E8=8C=B6?= <64787592+FrozenLemonTee@users.noreply.github.com> Date: Fri, 13 Mar 2026 12:00:45 +0800 Subject: [PATCH 6/6] fix: Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- config/.github/workflows/code-format.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/config/.github/workflows/code-format.yml b/config/.github/workflows/code-format.yml index 4b8d79e..13f636e 100644 --- a/config/.github/workflows/code-format.yml +++ b/config/.github/workflows/code-format.yml @@ -87,12 +87,12 @@ jobs: shell: bash run: | set -euo pipefail - files=$(git ls-files '*.c' '*.cc' '*.cpp' '*.cxx' '*.h' '*.hpp' '*.hh' '*.hxx' '*.cppm' '*.ixx') - if [ -z "$files" ]; then + mapfile -t files < <(git ls-files '*.c' '*.cc' '*.cpp' '*.cxx' '*.h' '*.hpp' '*.hh' '*.hxx' '*.cppm' '*.ixx') + if [ "${#files[@]}" -eq 0 ]; then echo "No C/C++ files found to format." exit 0 fi - echo "$files" | xargs clang-format -i + clang-format -i "${files[@]}" - name: Commit formatting changes uses: stefanzweifel/git-auto-commit-action@v5