Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore files or directories with clang-format 3.9

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.

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

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)
like image 315
jcoline Avatar asked Jun 01 '18 16:06

jcoline


People also ask

How do you customize your clang-format?

clang-format supports two ways to provide custom style options: directly specify style configuration in the -style= command line option or use -style=file and put style configuration in the . clang-format or _clang-format file in the project directory.

What is penalty in clang-format?

When you have a line that's over the line length limit, clang-format will need to insert one or more breaks somewhere. You can think of penalties as a way of discouraging certain line-breaking behavior.

How do I use clang-format?

To automatically format a file according to Electron C++ code style, run clang-format -i path/to/electron/file.cc . It should work on macOS/Linux/Windows. The workflow to format your changed code: Make codes changes in Electron repository.

How do I turn off clang-format?

Disabling Formatting on a Piece of Code The code between a comment // clang-format off or /* clang-format off */ up to a comment // clang-format on or /* clang-format on */ will not be formatted.


1 Answers

Individual files no, but directories, yes.

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

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)

Where the first .clang-format holds:

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

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

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

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


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

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

or use YAML syntax instead of JSON:

DisableFormat: true
SortIncludes: Never
like image 154
grandchild Avatar answered Oct 11 '22 17:10

grandchild