Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clang-format and references: can I get a "true" left alignment?

I've recently tried the ReferenceAlignment: Left option introduced in clang-format 13.

In combination with AlignConsecutiveDeclarations: true it produces a rather strange results:

ShortType         & v1;
SomeLongerType    & v2;
MuchMuchLongerType& v3;

The v3's & is aligned to the left, but the other two are aligned on the rightmost ampersand.

Is there a way for me to get this result instead:

ShortType&          v1;
SomeLongerType&     v2;
MuchMuchLongerType& v3;
like image 745
user26785 Avatar asked Sep 15 '25 03:09

user26785


1 Answers

There seems to be some interference between PointerAlignment and ReferenceAlignment. When they are both Left, I get the desired result. When the PointerAlignment is Right, I get the strange one.

$ echo "struct Test {  ShortType&          v1;  SomeLongerType&     v2;   MuchMuchLongerType& v3;};" | clang-format-13 --style "{AlignConsecutiveDeclarations: true, PointerAlignment: Right, ReferenceAlignment: Left}"

struct Test {
  ShortType         & v1;
  SomeLongerType    & v2;
  MuchMuchLongerType& v3;
};
$ echo "struct Test {  ShortType&          v1;  SomeLongerType&     v2;   MuchMuchLongerType& v3;};" | clang-format-13 --style "{AlignConsecutiveDeclarations: true, PointerAlignment: Left, ReferenceAlignment: Left}"
struct Test {
  ShortType&          v1;
  SomeLongerType&     v2;
  MuchMuchLongerType& v3;
};

Moreover, even when ReferenceAlignment is Right, the PointerAlignment still affects the result:

$ echo "struct Test {  ShortType&          v1;  SomeLongerType&     v2;   MuchMuchLongerType& v3;};" | clang-format-13 --style "{AlignConsecutiveDeclarations: true, PointerAlignment: Left, ReferenceAlignment: Right}"
struct Test {
  ShortType &         v1;
  SomeLongerType &    v2;
  MuchMuchLongerType &v3;
};
$ echo "struct Test {  ShortType&          v1;  SomeLongerType&     v2;   MuchMuchLongerType& v3;};" | clang-format-13 --style "{AlignConsecutiveDeclarations: true, PointerAlignment: Right, ReferenceAlignment: Right}"
struct Test {
  ShortType          &v1;
  SomeLongerType     &v2;
  MuchMuchLongerType &v3;
};

This may be a bug in clang-format, or it may be an expected behavior.

like image 186
user26785 Avatar answered Sep 17 '25 18:09

user26785