Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify a clang-format file?

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?

like image 461
dromodel Avatar asked Sep 22 '17 22:09

dromodel


People also ask

Where do I put the clang format?

clang-format file, we need to place it in the project folder or in any parent folder of the file you want to format. clang-format.exe searches for the config file automatically starting with the folder where the file you want to format is located, all the way to the topmost directory.

How do I use clang format in Windows?

Getting Started with Clang-Format The shortcut Ctrl+Shift+I is for Linux. If you want to use it on Windows, you need to use Alter+Shift+F. If you do not have Clang-Format installed on your system, you will see the prompt: The 'clang-format' command is not available.

How do I use clang format in Sublime Text?

Set the path to the clang-format binaries. You can do this from within Sublime Text by choosing Clang Format - Set Path from the command palette. Hint: the path should look something like this [path/to/clang]/clang/bin/clang-format . If clang-format is in your system path, you shouldn't need to do anything.


3 Answers

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
like image 133
nwp Avatar answered Oct 16 '22 08:10

nwp


(updated 2022-02-25) clang 14.0.0-RC1 or later now supports -style=file:<format_file_path> after this change "Add option to explicitly specify a config file" entered the main branch. It's documented here. This worked for me:

.\LLVM14.0.0RC1\bin\clang-format.exe test.cpp -style=file:my_clang_format.txt
like image 44
Dwayne Robinson Avatar answered Oct 16 '22 06:10

Dwayne Robinson


Just an additional hint: (in my case on Windows 10) - if you dump the config to a file, check the ascii coding afterwards, e.g. in Notepad++ - the .clang-format file has to be utf8 without BOM !!! (you can convert it in Notepadd++) - otherwise you get an error like "YAML:1:4: error: Got empty plain scalar" - place the .clang-format file in the same folder or in a parent directory of the code-file.c

A correct command line call to format the code-file.c would look like that: clang-format.exe -style=file -i C:\path\code-file.c

like image 4
tk6037 Avatar answered Oct 16 '22 08:10

tk6037