Search code examples
c++formattingclangclang-format

Why do the PointerAlignment options not work?


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?


Solution

  • 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.