如何忽略 clang-format 3.9 的文件或目录

问题描述 投票:0回答:3

我目前正在使用 travis ci 检查补丁,因为它们进入 github,并试图找出 clang-format 3.9 是否有任何方法(因为 travis ci 目前仅支持最新的 ubuntu 14.04)来忽略整个目录或文件扫描变化。

我的 .travis.yml 文件:

language: c++
sudo: required
dist: trusty
install:
- sudo apt-get update
- sudo apt-get install clang-format-3.9 python3
- ./travisci/check_patch.py

我的 travisci/check_patch.py 文件:

#!/usr/bin/env python3

from subprocess import Popen, PIPE, STDOUT

# Run clang to check if code changes cause a diff output and return 1 if so.
cmd = "git show origin/master..@ | clang-format-diff-3.9 -p 1 -style=file"
diff = Popen(cmd, stdout=PIPE, shell=True).communicate()[0]
if diff:
    print("Code formatting is not according to style guidelines. Read https://github.com/intel/IA-Hardware-Composer/wiki/Contributions#coding_style")
    exit(1)

exit(0)
clang travis-ci clang-format
3个回答
31
投票

个人文件没有,但目录,有

here所述,您可以将新的

.clang-format
文件放入包含不需格式化的文件的文件夹中。

示例:我有一个包含仅标头库的项目,例如

cppzmq
,并且我只想格式化 my 源文件,以在更新库时保持较小的差异。所以我创建了一个布局,例如:

project/
├ include/
│ ├ 3rdparty/
│ │ ├ .clang-format   (1)
│ │ └ zmq.hpp
│ └ my_app.hpp
├ src/
│ └ my_app.cpp
└ .clang-format       (2)

第一个

.clang-format
所在的位置:

{
    "DisableFormat": true,
    "SortIncludes": "Never"  // with clang-format version < 13 use `false` here.
}

DisableFormat
似乎没有禁用包含排序,因此必须明确给出。)

第二

.clang-format
保存您常用的 clang 格式配置。

确保您的全局/项目级别 clang-format 的

style
设置设置为
File


编辑:如果您的 clang-format 抱怨第二行的值无效,请添加尾随逗号:

{
    "DisableFormat": true,
    "SortIncludes": "Never",
}

或使用 YAML 语法代替 JSON:

DisableFormat: true
SortIncludes: Never

0
投票

在这里检查我的答案:https://stackoverflow.com/a/51793637/2751261。基本上,我使用查找脚本来选择符合我的条件的文件和文件夹,然后将 clang-format 应用于每个文件和文件夹。这是因为到目前为止我还没有找到任何 clang-format 的选项。

我知道这不是您期望的答案,但我希望它有用。


0
投票

最近(2024 年 2 月,自 LLVM 18.1.0-rc1 起)可以忽略带有

.clang-format-ignore
文件的特定文件和文件夹:

You can create ``.clang-format-ignore`` files to make ``clang-format`` ignore
certain files. A ``.clang-format-ignore`` file consists of patterns of file path
names. It has the following format:
- A blank line is skipped.
- Leading and trailing spaces of a line are trimmed.
- A line starting with a hash (``#``) is a comment.
- A non-comment line is a single pattern.
- The slash (``/``) is used as the directory separator.
- A pattern is relative to the directory of the ``.clang-format-ignore`` file
(or the root directory if the pattern starts with a slash).
- Patterns follow the rules specified in POSIX 2.13.1, 2.13.2, and Rule 1 of
2.13.3.
- A pattern is negated if it starts with a bang (``!``).

To match all files in a directory, use e.g. ``foo/bar/*``. To match all files in
the directory of the ``.clang-format-ignore`` file, use ``*``.
Multiple ``.clang-format-ignore`` files are supported similar to the
``.clang-format`` files, with a lower directory level file voiding the higher
level ones.

参考资料:
[1]问题:https://github.com/llvm/llvm-project/commit/09308122c6c0fa9eb3d729a2b2909733cbbc2160\ [2] 拉取请求:https://github.com/llvm/llvm-project/pull/76327

© www.soinside.com 2019 - 2024. All rights reserved.