22

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)
kaya3
  • 47,440
  • 4
  • 68
  • 97
jcoline
  • 221
  • 1
  • 2
  • 3

2 Answers2

31

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
grandchild
  • 1,157
  • 15
  • 20
  • Does this really work for you with the JSON format? I had to switch to YAML as in the example you linked to: https://stackoverflow.com/questions/51326844/exclude-directory-clang-format/56243326#56243326 – jfly Feb 11 '21 at 21:40
  • Yes it does. Which error are you getting? Also note the alternative with extra comma at the bottom. – grandchild Feb 12 '21 at 13:06
  • 1
    YAML is a superset of JSON so that shouldn't cause any problems. @jfly – Julia Mar 11 '21 at 09:57
  • @Julia well it can if the JSON is written incorrectly. JSON is easier to get wrong in terms of quoting and commas (because it is stricter) than YAML (which has its own set of common errors). – grandchild Mar 12 '21 at 13:22
  • Sorry for the delayed response. I was running into the "Invalid argument YAML:3:21: error: invalid boolean" error that @grandchild already added a note about. Adding a trailing comma fixes the problem for me. Sounds like there was some change to the clang-format YAML parser that makes it not quite able to handle that example. – jfly Mar 15 '21 at 05:07
  • clang-format version 12.0.1 now wants a bool for `SortIncludes` – Steve Lorimer Oct 28 '21 at 15:29
  • @SteveLorimer: It seems to me you have it the wrong way around, and it previously was a boolean, but is now an enum. https://releases.llvm.org/13.0.0/tools/clang/docs/ReleaseNotes.html#clang-format . The inverse would have surprised me, since clang-format usually doesn't want booleans, but all fields to be enums so they are more easily extendable (like `SortIncludes`) in the future. – grandchild Oct 29 '21 at 20:38
  • 2
    That's odd; I get the following: `3rd-party/.clang-format:3:21: error: invalid boolean "SortIncludes": "Never",`; changing it to `false` fixes the issue (I'm running `clang-format 12.0.1`) – Steve Lorimer Nov 01 '21 at 12:24
  • I see the enum based `SortIncludes` is for release 13, which was October 2021. I'm puzzled that you had the enum-based behaviour so long before then... anyway, looks like I'm running a pre-enum version – Steve Lorimer Nov 01 '21 at 12:29
  • Ah, if you look at the edit history for this answer, you'll see (and I rediscovered, heh ^^' ) that I changed it to an enum only recently. :) -- I'll add a note to the answer about this. – grandchild Nov 01 '21 at 15:10
  • What do you mean by "Make sure your global-/project-level clang-format's style setting is set to File."? – eDeviser Aug 23 '22 at 10:56
  • @eDeviser clang-format's `--style` option has several builtin sets of styles ("Google", "LLVM", "Mozilla", etc.). But to use a `.clang-format` file in the project you have to run `clang-format --style=file`. If your IDE takes care of calling clang-format somehow, then it will surely have a setting to select one of the style presets or the special "FILE" value, which you should select then, to make this solution work. – grandchild Aug 23 '22 at 14:30
0

Check my answer here: https://stackoverflow.com/a/51793637/2751261. Basically I use a find script to select files and folders regarding my criteria and then applying clang-format to each of them. This is because so far I never found any option in clang-format for this.

I know it is not the answer you expect, but I hope it's handy.

Pato Sandaña
  • 519
  • 5
  • 14