How do I specify a clang-format file?

dromodel picture dromodel · Sep 23, 2017 · Viewed 19.4k times · Source

I just built clang 5.0.0 on a Redhat 6 machine and tried to run clang-format. I'm unable to get the -style option to read in a style file. Here's an example of a set of commands that I think should work:

./clang-format -style=llvm -dump-config > .llvm-format
./clang-format -style=.llvm-format ~/myFile.cc

When I run this command I get the following error:

Invalid value for -style

It appears to find and use .clang-format when placed in a parent directory as expect. The built-in types also work:

./clang-format -style=Mozilla ~/myFile.cc

The problem, then, is that I can't specify explicitly a .clang-format file to use. Is this a problem with my build or am I misusing clang-format oir misunderstanding the documentation?

Answer

nwp picture nwp · Sep 23, 2017

I got confused by that too. When they tell you to use the flag -style=file they literally mean for you to type out -style=file, NOT -style=<path/to/actual/filename>.

With this flag, clang-format will look for a file called .clang-format in the directory of the target file. If it doesn't find any it will cd .. and try again. There doesn't seem to be a way to tell clang-format to use a file that is not named exactly .clang-format.

The correct usage for your example would be:

./clang-format -style=llvm -dump-config > ~/.clang-format
./clang-format -style=file ~/myFile.cc