Clang-tools Binaries

如果你需要使用 clang-tools binaries,以 Windows 为例,通常下载指定版本的 clang-tools 需要先安装 LLVM 这个大的安装包才能获得 clang-format & clang-tidy 这些工具;在 Linux 上会方便很多,可以使用命令来下载,但如果想下载指定版本的 clang-format & clang-tidy 可能要面临手动下载和安装。

clang-tools-pip 提供并支持在 Windows,Linux,MacOs 上通过命令行下载任何指定版本的 clang-tools 可执行文件。

只需要使用 pip 安装 clang-tools (即 pip install clang-tools)后,然后通过 clang-tools 命令就可以安装任何版本的可执行文件了。

例如,安装 clang-tools 版本 13:
$ clang-tools --install 13
也可以将它安装到指定目录下面:
$ clang-tools --install 13 --directory .

clang-tools CLI 还提供了其他选项,比如自动帮你创建链接等,可以查看它的 CLI 文档来获得帮助。
Command Line Interface Options - clang-tools installer (cpp-linter.github.io)

把 Clang-tools 集成到工作流

  1. cpp-linter-action 提供了通过 CI 进行检查,当发现没有格式化或有诊断错误的代码时 CI 会失败,来防止合并到主分的代码没有通过代码检查;

  2. cpp-linter-hooks 通过 git hook 在提交代码的时候自动运行 clang-format 和 clang-tidy,如果不符合规范则提交失败,并提示并自动格式化。

Cpp-linter-action 在代码合并前做自动检查

如果你使用的是 GitHub,那么非常推荐你使用 cpp-linter-action 这个 GitHub Action。

以下是它的一些重要特性:

  1. 运行结果支持 Annotations 和 Thread Comment 两种方式展示

  2. 支持 GitHub 的 public 和 private 仓库

  3. 支持绝大多数 Clang 版本

  4. 还有很多其他的 optional-inputs
    cpp-linter/cpp-linter-action: Lint C/C++ code with clang-format and clang-tidy then post annotations, comments, and step summary with results. (github.com)

使用这个 Action 只需要在 .github/workflows/ 下面创建一个 cpp-linter.yml,内容如下:

当然也可以把下面的配置加到一个已经存在的 Workflow,例如 build。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
name: cpp-linter  

on:
  pull_request:
    types: [openedreopened]
  push:

jobs:
  cpp-linter:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: cpp-linter/cpp-linter-action@v1
        id: linter
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          style: file

      - name: Fail fast?!
        if: steps.linter.outputs.checks-failed > 0
        run: |
          echo "Some files failed the linting checks!"
          exit 1

如果发现存在没有进行格式化或是静代码态检查,CI workflow 会失败,并且会有如下注释说明,annotations 默认是开启的。

图片

如果开启了 Thread Comment 选项(即 thread-comments: true)就会将在 Pull Request 中自动添加如下错误评论。

图片

Cpp-linter-hooks 在提交代码时自动检查

cpp-linter-hooks 是通过 git hook 在提交代码时做自动检查,这种方式不限制使用任何 SCM。

只需要在项目仓库中添加一个 .pre-commit-config.yaml 配置文件,然后将 cpp-linter-hooks 这个 hook 添加到 .pre-commit-config.yaml 中,具体设置如下:

.pre-commit-config.yaml 是 pre-commit framework 的默认配置文件。

安装 pre-commit

1
pip install pre-commit

创建配置文件 .pre-commit-config.yaml,设置如下:

1
2
3
4
5
6
7
8
repos:
- repo: https://github.com/cpp-linter/cpp-linter-hooks
  rev: v0.2.1
  hooks:
    - id: clang-format
      args: [--style=file]  # to load .clang-format
    - id: clang-tidy
      args: [--checks=.clang-tidy# path/to/.clang-tidy

这里的 file 是指 .clang-format, clang-format 默认支持的编码格式包括 LLVM, GNU, Google, Chromium, Microsoft, Mozilla, WebKit,如果需要特殊设置可以在仓库的根目录下面创建配置文件 .clang-format。同理,如果默认的静态分析设置不满足要求,可以在仓库的根目录下创建 .clang-tidy 配置文件。

更多配置可以参考 cpp-linter/cpp-linter-hooks: C/C++ linter hooks for pre-commit (github.com)

安装 git hook 脚本

1
2
$ pre-commit install
pre-commit installed at .git/hooks/pre-commit

之后每次 git commit 都会自动执行 clang-format 和 chang-tidy。