llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.19k stars 12.04k forks source link

[clang-format] proposal for .clang-format-ignore #59373

Closed mydeveloperday closed 11 months ago

mydeveloperday commented 1 year ago

I often use clang-format to check whole trees of source code and report clang-format violations with the -n option

find . -name '*.h' -or -name '*.cpp' -exec clang-format -n {} \;

This works well but from time to time I have generated files I want to exclude. Mostly these are files that are autogenerated for which I have little to no control of the output

Input Source -> Generator -> Output C++/C# code etc..

examples of such generators can be lex,yacc,bison,mild but also custom code generation phases

Sometimes these are Generators where I can place // clang-format off at the top but sometimes I simply cannot control the output enough.

There is also another use case, where for some reason on another (rightly or wrongly) clang-format simply isn't able to format the file well (either it takes a very long time, (lots of ifdef/endif) or it crash (rarer))

It would be nice in these cases to not have to contort my find command to exclude those files but to simply have a .clangformatignore file which would list the files locally that need to be ignored.

Initially, I would recommend just putting the files at the local directory level but I could see this being extended to behave similar to .gitignore with potential wildcards etc.. (subdirectories)

I think this could help greatly.

llvmbot commented 1 year ago

@llvm/issue-subscribers-clang-format

rymiel commented 1 year ago

52975?

tschuett commented 1 year ago

I also like this feature. You peaked me to look at the gitignore documentation: https://git-scm.com/docs/gitignore I put only one gitignore file at the root. It took me while but apparently git supports several gitignore files in a repository. It would be odd if clang-format runs:

> git rev-parse --show-toplevel

but it would be helpful.

mydeveloperday commented 1 year ago

52975?

Good ideas keep rising to the top, just needs one of us to run with it...

FJShen commented 1 year ago

I would love this feature. I have a repository containing both C++ source files and Matlab script files (.m) and I want to use pre-commit to enforce clang-format on those C++ source files. Currently clang-format seems to treat my Matlab script file as a Objective-C file (also .m) and completely mangles the format of the Matlab script.

I haven't found a straightforward way to configure clang-format to ignore specific files/directories.

owenca commented 11 months ago

Duplicate of #52975, which I'm working on.