How to ignore files or directories with clang-format 3.9
Asked Answered
P

3

24

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)
Paralyze answered 1/6, 2018 at 16:53 Comment(0)
B
32

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
Belsen answered 30/7, 2019 at 13:13 Comment(12)
Does this really work for you with the JSON format? I had to switch to YAML as in the example you linked to: #51327344Onesided
Yes it does. Which error are you getting? Also note the alternative with extra comma at the bottom.Belsen
YAML is a superset of JSON so that shouldn't cause any problems. @OnesidedEndearment
@Endearment 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).Belsen
Sorry for the delayed response. I was running into the "Invalid argument YAML:3:21: error: invalid boolean" error that @Belsen 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.Onesided
clang-format version 12.0.1 now wants a bool for SortIncludesTemplet
@SteveLorimer: It seems to me you have it the wrong way around, and it previously was a boolean, but is now an enum. releases.llvm.org/13.0.0/tools/clang/docs/… . 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.Belsen
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)Templet
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 versionTemplet
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.Belsen
What do you mean by "Make sure your global-/project-level clang-format's style setting is set to File."?District
@District 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.Belsen
H
0

Check my answer here: https://mcmap.net/q/583136/-exclude-directory-clang-format. 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.

Haug answered 10/8, 2018 at 20:33 Comment(1)
except your answer there is broken and only shows how to exclude folders.Southey
A
0

It has recently become possible (feb 2024, since LLVM 18.1.0-rc1) to ignore specific files and folders with a .clang-format-ignore file:

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.

References:
[1] issue: https://github.com/llvm/llvm-project/commit/09308122c6c0fa9eb3d729a2b2909733cbbc2160\ [2] pull-request: https://github.com/llvm/llvm-project/pull/76327

Assurgent answered 1/7, 2024 at 18:2 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.