Can I use a different .clang_format for some files in the same project?

5k Views Asked by At

I want to introduce clang-formatter to our company, but there are some restrictions, like:

Some files in our company that are better read with a tab-width=2 while others are better read with tab-width=4.

This means that some files (due to the nature of their content) should have a different .clang-format configuration file. So, assumming that we end up with 2 or 3 configurations, suitable for each content, is there a way that I can force some files to be formatted by a particular .clang_format ?

for example,

  • could this be done by placing a different .clang_format in each folder ?

or

  • by entering the YAML lines as comments at the beginning of each file (like doxygen) ?

we use C, C++, visual studio and vim

3

There are 3 best solutions below

0
On BEST ANSWER

You can place a different .clang-format (_clang-format) file into each directory. clang-format will start looking for it in the file's directory and then search through all its parents until it finds one (assuming you are setting -style=file).

This sounds like it would solve your use case. If not, I'd like to understand how. Do you have files in the same directory that need different indentation?

2
On

You have limited control over which .clang_format file is used, as the formatter will start searching for a file in the source file directory and will then consecutively search the parent directories. However, organizing your directory structure in a way that this works can be quite inconvenient.

You can however override specific options from the style with each call. From the clang-format docs:

Use -style="{key: value, ...}" to set specific parameters, e.g.: -style="{BasedOnStyle: llvm, IndentWidth: 8}"

Unfortunately the Visual Studio plugin currently does not allow to change the options passed to clang-format on a per-file basis, so the latter approach won't work here.

To my knowledge there is no support for 'modeline' comments that would allow to set style options from within the source file yet, although I'd love to see that added in future versions.

0
On

I had started implementing support for a style comment inside code files: namely, in the form of a comment such as // clang-format style=..., through which you could activate whatever you could also activate on the command line with -style=....

Specifically, you could do each of the following inside each code file:

  • // clang-format style=Google
  • // clang-format style={BasedOnStyle: Google, IndentWidth: 2}
  • // clang-format style={BasedOnStyle: InheritParentConfig, IndentWidth: 2}
  • // clang-format style=file:specific.clang-format
  • // clang-format style=file:../../path/to/specific.clang-format

Here's my feature request on the LLVM GitHub repository and here's my tentative implementation.

Unfortunately, the maintainers of clang-format seem to be opposed to the idea, see the discussion at https://reviews.llvm.org/D145435. I am not suggesting proponents of this feature should blindly spam this discussion thread, but if there are significant arguments for it, or concrete applications of open-source projects that would benefit from that option, that might still be a valuable addition. Otherwise, I'd have to conclude that the current clang-format does not have an option to influence the style from within a code file, and that is by design.