[PATCH] D93844: [clang-format] Add possibility to be based on parent directory

Nathan James via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Dec 27 15:12:54 PST 2020


njames93 added a comment.

In D93844#2472346 <https://reviews.llvm.org/D93844#2472346>, @MyDeveloperDay wrote:

> I'm a little confused why this is needed as clang-format always read up the directory tree until it see a .clang-format file, perhaps I don't quite understand the use case. Can't you just have a different .clang-format in the subdirectory?

It's my understanding this is akin to the `InheritParentConfig` option found in `.clang-tidy`.
The idea is you can put a `.clang-format` file in your project root, then for specific directories you can add another `.clang-format` file that modifies that root `.clang-format`
The example here is a test folder that needs extended lines but want the rest of the formatting to match the root folder without having to copy the contents of the root and make the small edit.

We have it quite lucky here at LLVM where for tests we can just use:

  BasedOnStyle: LLVM
  ColumnLimit: 0

However for other projects with their own style they could then use:

  # Copy the root directory clang.format config
  BasedOnStyle: file
  ColumnLimit: 0

This would also make modifying the formatting style of a project much easier as you would only need to touch the root directory `.clang-format` file in most cases.

With this implementation I'm not too sure on the specifics though. `.clang-format` files are more complex to parse than `.clang-tidy` files so there's more edge cases to consider.
Also it appears this wouldn't handle the case of invoking clang-format with the style set inline on the command line.
`clang-format <file> --style='{BasedOnStyle: File, ColumnLimit: 80}'`
Should this then check the directory for `.clang-format` files?



================
Comment at: clang/lib/Format/Format.cpp:2962
+                                    llvm::sys::path::filename(FileName));
+            if (auto OuterStyle = getStyle(DefaultFormatStyle, FileForLanguage,
+                                           FallbackStyleName, Code, FS,
----------------
This seems a very inefficient way of doing this, getStyle does a lot of things we simple don't need to worry about. Maybe the file loading part should be extracted out into its own function, that can then recursively call itself. Or use the clang-tidy approach of storing configurations in a stack (will have to be unparsed cause how clang-format works) and then parse them on top of each other.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D93844/new/

https://reviews.llvm.org/D93844



More information about the cfe-commits mailing list