如何使用 clang-format 3.9 忽略文件或目录 [英] How to ignore files or directories with clang-format 3.9

查看:47
本文介绍了如何使用 clang-format 3.9 忽略文件或目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 travis ci 来检查补丁进入 github 并试图弄清楚是否有任何用于 clang-format 3.9(因为 travis ci 目前仅支持 ubuntu 14.04 最新版本)以忽略整个目录或扫描更改时的文件.

I am currently using travis ci to check patches as they come into github and am trying to figure out if there is anyway for clang-format 3.9 (since travis ci will only support ubuntu 14.04 currently as latest) to ignore entire directories or files when scanning changes.

我的 .travis.yml 文件:

My .travis.yml file:

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​​ 文件:

My travisci/check_patch.py file:

#!/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-format文件包含不进行格式化的文件的文件夹.

As said here, you can put a new .clang-format-file inside a folder that contains files not to be formatted.

示例:我有一个项目,其中包含一个仅标头库,例如 cppzmq 并且我只希望对 my 源文件进行格式化,以在更新库时保持较小的差异.所以我创建了一个布局,例如:

Example: I have a project that includes a header-only library, such as cppzmq and I want only my source files to be formatted to keep the diff small when updating the library. So I create a layout such as:

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

first .clang-format 的位置:

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

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

(DisableFormat does not seem to disable include-sorting, so it has to be given explicitly.)

second .clang-format 保存您常用的 clang-format 配置.

The second .clang-format holds your usual clang-format config.

确保您的全局/项目级 clang-format 的 style 设置设置为 File.

Make sure your global-/project-level clang-format's style setting is set to File.

编辑:如果您的 clang 格式在第二行抱怨无效布尔值,请添加一个尾随逗号:

Edit: If your clang-format complains about invalid boolean on the second line, add a trailing comma:

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

这篇关于如何使用 clang-format 3.9 忽略文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆