<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/107808>107808</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
How to support non-hierarchical .clang-format inheritance
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang-format
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
jediry
</td>
</tr>
</table>
<pre>
As discussed in my [RFC](https://discourse.llvm.org/t/rfc-clang-format-option-to-include-another-configuration-file/74891) earlier this year, I need a way for a .clang-format file to be based on another .clang-format file that is not located in an ancestor directory. This is because our company's repo contains many, many projects as top-level subdirectories under the repo root, and any given team (with their own, tribal style conventions) might own a dozen of these projects, and would want to maintain only one copy of their style rules.
Basically, I want the same semantics as ```BasedOnStyle: InheritFromParent```, but with the ability to refer to an arbitrary file, not just .clang-format in the parent directory. The base file should be processed exactly as if it were a .clang-format in the parent directory, including being allowed to ```BasedOnStyle: some-other.clang-format```.
As mentioned in the RFC discussion, there may be security implications of allowing a .clang-format for specify an arbitrary file (e.g., ```/etc/passwd```?). While it's not clear to me exactly how such an exploit would work, I'd rather not be the guy who made it possible. So, security should be part of the discussion.
I can see three possibilities for supporting this:
1. Only support paths relative to the current file (e.g., ```BasedOnStyle: ../../../../format/team1.clang-format```). Simple to implement, but brittle if directories are moved around. Also might allow referencing arbitrary files outside the project root.
2. Allow environment variable expansion (e.g., ```BasedOnStyle: $(FORMATDIR)/team1.clang-format). Also simple to implement, and resilient against code moves, but may have security risks (e.g., ```BasedOnStyle: $(HOME)/.ssh/private_key```)
3. Only support paths relative to a list of "style search paths", which are specified on the command-line (e.g., ```BasedOnStyle: team1.clang-format```, with ```--style-search-path my_repo/format``` on the command-line). This is basically the same model as how C/C++ ```#include <someheader.h>``` directives are resolved. Since this externalizes the decision of where clang-format is allowed to look for files, this seems (a) most secure (since the user is in control of where clang-format can look) and (b) most cumbersome to use (since the user must somehow ensure that the style search paths are always provided when invoking clang-format).
I have a [PR](https://github.com/llvm/llvm-project/pull/107312) open that implements option 3, but the amount of plumbing is...significant. Not only does clang-format need to plumb through ```--style-search-path``` arguments, but so do clang-tidy, clangd, clang-move...anything that invokes formatting logic.
Opinions on the best way forward here?
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUV9Fu47YS_RrmZWDBpuMkfvBD4q3QfehNkS1wHwtKGlncUByBQ9mrfv3FkLZjJ9neFjBsxyFnhmfOORwZZrvziBu1elKrLzdmjB2FzXdsbJhuKmqmzSNDY7kembEB66GfQK2eXsqtWn1R-qGLcWC1fFS6VLqUlTQGxsK5fV9Q2CldRqXL0Naz2hm_m7UUehNnNERLfhZpZn3txgZnxlPsMMxq8q3djcGkBa11qHR5f_uwXii9BjTBWQwQO8swoQlKb-EreMQGDBzMBC0FMFBcZgOJApGgQqiMHIQ8HPN9urIzESyDpwiOahPz0Y28auRIARobsI4UpgL-kFIsQ4W1GRmBxgA19YPxk9L3DAEHgpp8NNYz9OnnbfqEIdB3rCODYYg0zBzu0QGP1Sm8RYbRN-nAmCMFoigBjG9AYuzsHj1END0o_XCwsZO1NgAdvKyLwVbGAcfJoZSxRy_IsqDZ210XZSEYaOgv9ECt7GY8l3ZKdaDRNXAwPgqQvbHpPEDeTUBeIg_TcbcNx2xhdMiFmn9R88f8_mTY1sa5Kbcth-sQ2PQIjL3x0dYJDnU3z68nadiz_yYR1fIRvvoOg41loP53E9DH80qJWY0RThiAqayzcZKCA7YCIqUmhsrGYMIEmV3b1OjvI8d3ZLA-hRlSmuuWZyJlunCXsKkSaDUmpeAPU0c3yUlsCzbCAQN-4OVPEkhJWRbW76BCeTfO0QEbOcJPoWHqcZZYfZXmvP6qFY8MfaZCZrcU8lJuT2q3lNnTSd29meR4jPUYBFDbD87WSaEsTU_FpSrfy4kC8IC1baePyAtfsdgVkueiiSXGWulyMMyH5u33Zan0uoD_drLTxiQt6Vvt0KTO9nhGvaMD8Fh3khN_DI6kAZnAFF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