[all-commits] [llvm/llvm-project] a0b826: [clang-format] Continue aligned lines better (#161...

sstwcw via All-commits all-commits at lists.llvm.org
Wed Oct 15 08:18:33 PDT 2025


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: a0b8261610395c3ef121958417251f552d6c1bb5
      https://github.com/llvm/llvm-project/commit/a0b8261610395c3ef121958417251f552d6c1bb5
  Author: sstwcw <su3e8a96kzlver at posteo.net>
  Date:   2025-10-15 (Wed, 15 Oct 2025)

  Changed paths:
    M clang/lib/Format/WhitespaceManager.cpp
    M clang/unittests/Format/FormatTest.cpp

  Log Message:
  -----------
  [clang-format] Continue aligned lines better (#161903)

Fixes #53497.

Fixes #56078.

after with config `{AlignConsecutiveAssignments: true}`

```C++
auto aaaaaaaaaaaaaaaaaaaaa = {};
auto b                     = [] {
  x = {.one_foooooooooooooooo = 2, //
       .two_fooooooooooooo    = 3, //
       .three_fooooooooooooo  = 4};
};

A B          = {"Hello "
                "World"};
BYTE payload = 2;

float i2 = 0;
auto v   = type{i2 = 1, //
                i  = 3};
```

before

```C++
auto aaaaaaaaaaaaaaaaaaaaa = {};
auto b                     = [] {
  x = {.one_foooooooooooooooo = 2, //
                           .two_fooooooooooooo    = 3, //
                           .three_fooooooooooooo  = 4};
};

A B          = {"Hello "
                         "World"};
BYTE payload = 2;

float i2 = 0;
auto v   = type{i2 = 1, //
              i  = 3};
```

When a line gets aligned, the following lines may need to move with it.
This patch replaces the old algorithm with a simpler one.  It uses the
`IsAligned` attribute.  It makes most of the scope stack irrelevant.
Now the stack only needs to keep track of 2 levels.

The old algorithm had problems like these.

- Whether lines inside a braced list moved depended on whether there was
  a type at the start.  It should depend on whether the inside was
  aligned to the brace.  The first case that came up with a type name at
  the start happened to have a comma at the end of the list so the
  inside was not aligned to the brace.

- Excluding lines inside closures did not always work.

- A continued string could move twice as much as it should.

The following problems are not fixed yet.

A token that opens a scope is needed.  Operator precedence is not
enough.

```C++
auto aaaaaaaaaaaaaaaaaaaaa = {};
auto b                     = 0 + //
         0;
```

The algorithm has trouble when things that should not move and things
that should move are nested.  This is related to the `IsAligned`
attribute being a boolean.  It also affects how spaces and tabs are
selected.

```C++
auto aaaaaaaaaaaaaaaaaaaaa = {};
auto b                     = {.a = {
              .a = 0,
          }};
```



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list