Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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-工作流配置)

## 一、标识符命名风格

Expand Down Expand Up @@ -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/.github/workflows/code-format.yml`](./config/.github/workflows/code-format.yml)

- `pull_request` / `push` 到 `main` 时自动执行 `clang-format --dry-run --Werror`,用于格式一致性检测。
- 若检查失败,CI 会直接报错并给出提示,提醒开发者在本地执行 `clang-format -i`。
- 可手动触发 `workflow_dispatch`,并选择 `mode=fix`,自动格式化并回推提交(可选择式自动修复)。

---

Expand Down
2 changes: 1 addition & 1 deletion clang-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ void bar();

## 其他配置

下面列出一些常见的 `clang-format` 配置项以供参考(这些项也出现在仓库的 `.clang-format` 中):
下面列出一些常见的 `clang-format` 配置项以供参考(这些项也出现在仓库的 `./config/.clang-format` 中):

- `SpacesInParentheses: false` — 括号内不额外加空格。
- `AllowShortLoopsOnASingleLine: false` — 禁止单行循环。
Expand Down
2 changes: 1 addition & 1 deletion clang-tidy.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,4 @@ int* p = nullptr;
- `performance-unnecessary-value-param.AllowedTypes: ''`(不额外放行类型作为按值参数)
- `misc-unused-parameters.IgnoreVirtual: true`(忽略虚函数重载中的未使用参数警告)

如需在项目中使用这些设置,可参考仓库根目录的 `.clang-tidy` 文件并按需调整以纳入 CI。
如需在项目中使用这些设置,可参考仓库的 `./config/.clang-tidy` 文件并按需调整以纳入 CI。
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
114 changes: 114 additions & 0 deletions config/.github/workflows/code-format.yml
Original file line number Diff line number Diff line change
@@ -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<<EOF"
echo "$files"
echo "EOF"
} >> "$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
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
clang-format -i "${files[@]}"

- 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