Why do the PointerAlignment options not work?
Asked Answered
G

2

8

I'm using clang-format (version 8.0.0 (tags/google/stable/2019-01-18)) with a style file, in which I set

…
PointerAlignment: Left
…

This succeeds in transforming declarations such as this one

const string &foo = "lorem ipsum";

into

const string& foo = "lorem ipsum";

However, when I also include in my style file

BasedOnStyle: Google

the options do not do anything. For some reason, they get overridden by the base style. This seems nonsensical to me – the explicit options should override the base style instead, no? Can somebody explain what the problem is and how to use both BasedOnStyle and PointerAlignment: Left?

Gorged answered 11/6, 2019 at 6:36 Comment(3)
PointerBindsToType is legacy (is not anymore there in the documentation, you have to go back to previous versions to see it, for example 3.4), just use PointerAlignment (either Left, Right or Middle).Zinn
Why did you edit the question? It was valid for the version explicitly mentioned up top.Gorged
It would be valid also for current version, PointerBindsToType is still a supported option but it is a legacy option. If you use at the same time PointerAlignment and PointerBindsToType the first one will make the second irrelevant. In the version you mention (8), PointerBindsToType was already made legacy (and therefore not mentioned in the documentation): releases.llvm.org/8.0.1/tools/clang/docs/… This question has more value if it shows cleaner usage of clang-format.Zinn
G
21

The answer is that the Google style (one can inspect it with clang-format -style=google -dump-config | less) defines

DerivePointerAlignment: true

The documentation says it

If true, analyze the formatted file for the most common alignment of & and *. Pointer and reference alignment styles are going to be updated according to the preferences found in the file. PointerAlignment is then used only as fallback.

Which means one must explicitly set DerivePointerAlignment: false if one wants to handle it by oneself.

Gorged answered 11/6, 2019 at 6:51 Comment(0)
L
0

This bit me. I personally think it is silly to have an undocumented option with a default value overriding a specifically set value. If someone specifically sets the PointerAlignment to be left (or right), why would the program decide to ignore that setting, especially without generating an error. PointerAlignment:Left appeared to work in 99% of the files I scanned, but failed in a few. Makes testing hard.

Are there any other values that might work this way? Where the option appears to work, but fails in some files because some hidden undocumented deprecated feature overrides a specific setting?

Leilanileininger answered 9/5 at 14:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.