[clang] [clang-format] add MaxSingleLinesInBracedList style option (PR #112482)

Gedare Bloom via cfe-commits cfe-commits at lists.llvm.org
Sat Oct 26 10:27:14 PDT 2024


gedare wrote:

> > There will be bugs if/when people use this option with small values for the limit, as it will interact weirdly with both the braced list initializer formatting rules, and also with the `AvoidBinPacking` logic of the continuation indenter.
> 
> Can you give some examples?

```
echo "vector<int> x{1111, 2, 3, 4, 5};" > a.txt
echo "vector<int> x{1111, 2, 3, 4, 5, 6};" > b.txt
```

LLVM style, actual output OK:
```
clang-format --style="{BasedOnStyle: llvm, ColumnLimit: 20}" a.txt

vector<int> x{1111,
              2, 3,
              4, 5};
```
```
clang-format --style="{BasedOnStyle: llvm, ColumnLimit: 20}" b.txt

vector<int> x{
    1111, 2, 3,
    4,    5, 6};
```

LLVM with braced list and no binpack, actual output OK:

```
clang-format --style="{BasedOnStyle: llvm, ColumnLimit: 20, BinPackArguments: false, Cpp11BracedListStyle: true}" a.txt

vector<int> x{1111,
              2,
              3,
              4,
              5};
```
```
clang-format --style="{BasedOnStyle: llvm, ColumnLimit: 20, BinPackArguments: false, Cpp11BracedListStyle: true}" b.txt

vector<int> x{1111,
              2,
              3,
              4,
              5,
              6};
```

Same, but limit single entry lines to 3, Actual output not OK for a.txt, OK for b.txt
```
clang-format --style="{BasedOnStyle: llvm, ColumnLimit: 20, BinPackArguments: false, Cpp11BracedListStyle: true, MaxSingleLinesInBracedList: 3}" a.txt

vector<int> x{1111,
              2,
              3,
              4,
              5};
```
```
clang-format --style="{BasedOnStyle: llvm, ColumnLimit: 20, BinPackArguments: false, Cpp11BracedListStyle: true, MaxSingleLinesInBracedList: 3}" b.txt

vector<int> x{
    1111, 2, 3,
    4,    5, 6};
```

The expected output for a.txt should be something like:
```
vector<int> x{
    1111, 2, 3,
    4,    5};
```

So the maximum limit is not enforced in certain cases. I only see this with small values of `MaxSingleLinesInBracedList` (below 5 or so).

I would be just as happy with making the current hard-coded limit of 20 be optional to disable. This limit doesn't seem to bother many people.


https://github.com/llvm/llvm-project/pull/112482


More information about the cfe-commits mailing list