[clang-format] Fix PointerAlignmentRight with AlignConsecutiveDeclarations
authorGerhard Gappmeier <gerhard.gappmeier@ascolab.com>
Thu, 3 Jun 2021 14:52:55 +0000 (16:52 +0200)
committerBjörn Schäpers <bjoern@hazardy.de>
Thu, 3 Jun 2021 15:55:11 +0000 (17:55 +0200)
commit3e333cc82e42e1e2ecc974d896489eebe1a5edc2
tree051bc4d9d599ee2e4925f637581915bfa3b5a6ed
parent12db09d7f3bb12a65a6ebdab497e2db035a3fa44
[clang-format] Fix PointerAlignmentRight with AlignConsecutiveDeclarations

This re-applies the old patch D27651, which was never landed, into the
latest "main" branch, without understanding the code. I just applied
the changes "mechanically" and made it compiling again.

This makes the right pointer alignment working as expected.
Fixes https://llvm.org/PR27353

For instance

const char* const* v1;
float const* v2;
SomeVeryLongType const& v3;

was formatted as

const char *const *     v1;
float const *           v2;
SomeVeryLongType const &v3;

This patch keep the *s or &s aligned to the right, next to their variable.
The above example is now formatted as

const char *const      *v1;
float const            *v2;
SomeVeryLongType const &v3;

It is a pity that this still does not work with clang-format in 2021,
even though there was a fix available in 2016. IMHO right pointer alignment
is the default case in C, because syntactically the pointer belongs to the
variable.

See

int* a, b, c; // wrong, just the 1st variable is a pointer

vs.

int *a, *b, *c; // right

Prominent example is the Linux kernel coding style.

Some styles argue the left pointer alignment is better and declaration
lists as shown above should be avoided. That's ok, as different projects
can use different styles, but this important style should work too.

I hope that somebody that has a better understanding about the code,
can take over this patch and land it into main.

For now I must maintain this fork to make it working for our projects.

Cheers,
Gerhard.

Differential Revision: https://reviews.llvm.org/D103245
clang/docs/ReleaseNotes.rst
clang/lib/Format/WhitespaceManager.cpp
clang/unittests/Format/FormatTest.cpp