Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 239
Grim Fandango Avatar asked Oct 28 '25 14:10

Grim Fandango


1 Answers

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.

like image 97
ComicSansMS Avatar answered Oct 30 '25 05:10

ComicSansMS