Creating my own “clang-format” style that can be used across multiple projects with the “BasedOnStyle” setting

Hi,

This is my first time reaching out to the llvm mailing list so I apologize if this is not the correct place to ask a question like this. (If it isn’t please let me know where I should post this question.)

I have multiple projects that I want to share a similar clang-format style, but I also want to be able to make minor tweaks between each project so they can be slightly different from one another if needed. Currently each project just has the same .clang-format file copied and pasted into its own repository, but it feels wrong because all of the style options are just duplicated from project to project and if I need to change one option I need to go across all projects and manually change it in all of them.

I would like to create my own style that can be used with the “BasedOnStyle” option. That way I can specify that I want all of these projects to be based on the same custom style that would be kept in a shared location, and then I could easily override any project specific options on a per-project basis.

I looked through the documentation and as far as I can tell, there doesn’t appear to be any way to create your own style and save it so other projects can be based on the same style. I feel like this is something a lot of users would need (for example if a company wanted to define their own master style that all projects should follow).

Is there any way to do this? And if not, how could I go about requesting such a feature be added?

Thanks,

Tim

+MyDeveloper Day who seems to be contributing to/reviewing clang-format patches these days

Sounds like a nice to have feature - you can file feature requests in the LLVM bugzilla (bugs.llvm.org). (perhaps it’s already possible, but I don’t know enough about clang-format to say)

Hi Timothy,

There is a patch in flight - https://reviews.llvm.org/D93844.
It doesn't specifically address this problem, but it it lands, you
could adapt it to work for your needs.
You could have a `.clang-format` file in the common subdirectory of all
your projects, then any `.clang-format` file in the individual
projects, could Base themselves off that root style.

Maybe this is merit for clang-format BasedOnStyle to also accept a path
to another `.clang-format` file. Likely this could either be absolute
or relative to either the `.clang-format` file or if the style was read
from the command line, relative to the CWD.

Before moving forward a strict set of semantics need establishing with
how these features interop with each other.
We would also need to ensure people don't try stupid things like
BasedOnStyle referring to the same file, or 2 files BasedOnStyle
pointing to each other. Both would likely result in an infinite loop
and so instead should report an error to the user.

~Nathan James

Thanks Nathan, I believe that would suit my needs pretty well!

It still feels a bit rigid if it just expects files to be in specific directories (C++ project structures tend to vary wildly haha). I would ultimately like to be able to specify a relative path in my CMakeLists file to tell it where to look for a .clang-format or .clang-tidy file. But either way I think I can achieve what I need with the proposed enhancement. :slight_smile:

Thanks,
Tim

There was a change

https://reviews.llvm.org/rG10b1a87ba35d386b718f0e83c1d750631705b220

which got reverted by the author, I’m not sure if they plan to bring it back? which could potentially help you.

https://reviews.llvm.org/rGc5c487f0d4c6720f4384f670490086e723f5fe32

MyDeveloperDay