jcoline
jcoline

Reputation: 281

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)

Upvotes: 28

Views: 26487

Answers (3)

grandchild
grandchild

Reputation: 1366

From clang-format 18.1.0 onwards you can use .clang-format-ignore files:

# ignore vendored libraries:
include/3rdparty/*

Below is the old answer for older clang-format versions:

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

Upvotes: 35

gospes
gospes

Reputation: 3937

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:

Upvotes: 2

Pato Sanda&#241;a
Pato Sanda&#241;a

Reputation: 659

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.

Upvotes: 0

Related Questions