llvm / llvm/llvm-project

[clang-format] AlignAfterOpenBracket is ignored when ColumnLimit exceeds some threshold

Open
#180,121 1 comment 0 reactions 0 assignees View on GitHub
clang-format
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Issue:
`AlignAfterOpenBracket: BlockIndent` doesn't work when ColumnLimit is too large, the threshold in my case is 85.
But it works if I add `BinPackArguments: false` and `BinPackParameters: false`.

I've tried running with:
- A Linux remote through ms-vscode.cpptools (which I think uses 21.1.4)
- A Windows laptop through
- ms-vscode.cpptools
- separately installed LLVM, both 18.1.8 and 21.1.0, both VS Code's extension and terminal

Here is my .clang-format:
```yaml
BasedOnStyle: Google
ColumnLimit: 80
AlignAfterOpenBracket: BlockIndent
# BinPackArguments: false
# BinPackParameters: false
IncludeBlocks: Regroup
PointerAlignment: Left
AllowShortLambdasOnASingleLine: Inline
NamespaceIndentation: Inner
```

This is the original:
```cpp
const map Card::SUIT_MAP_F = {
{"c", 0}, {"d", 1}, {"s", 2}, {"h", 3}
};

const map Card::SUIT_MAP_B = {
{0, "c"}, {1, "d"}, {2, "s"}, {3, "h"}
};

const map Card::RANK_MAP_F = {
{"a", 1},
{"2", 2},
{"3", 3},
{"4", 4},
{"5", 5},
{"6", 6},
{"7", 7},
{"8", 8},
{"9", 9},
{"10", 10},
{"j", 11},
{"q", 12},
{"k", 13}
};

const map Card::RANK_MAP_B = {
{1, "a"},
{2, "2"},
{3, "3"},
{4, "4"},
{5, "5"},
{6, "6"},
{7, "7"},
{8, "8"},
{9, "9"},
{10, "10"},
{11, "j"},
{12, "q"},
{13, "k"}
};
```

If I run clang-format, I get:
```cpp
const map Card::SUIT_MAP_F = {
{"c", 0}, {"d", 1}, {"s", 2}, {"h", 3}
};

const map Card::SUIT_MAP_B = {
{0, "c"}, {1, "d"}, {2, "s"}, {3, "h"}
};

const map Card::RANK_MAP_F = {
{"a", 1}, {"2", 2}, {"3", 3}, {"4", 4}, {"5", 5}, {"6", 6}, {"7", 7},
{"8", 8}, {"9", 9}, {"10", 10}, {"j", 11}, {"q", 12}, {"k", 13}
};

const map Card::RANK_MAP_B = {
{1, "a"}, {2, "2"}, {3, "3"}, {4, "4"}, {5, "5"}, {6, "6"}, {7, "7"},
{8, "8"}, {9, "9"}, {10, "10"}, {11, "j"}, {12, "q"}, {13, "k"}
};
```

With `ColumnLimit: 85` I get:
```cpp
const map Card::SUIT_MAP_F = {{"c", 0}, {"d", 1}, {"s", 2}, {"h", 3}};

const map Card::SUIT_MAP_B = {{0, "c"}, {1, "d"}, {2, "s"}, {3, "h"}};

const map Card::RANK_MAP_F = {
{"a", 1}, {"2", 2}, {"3", 3}, {"4", 4}, {"5", 5}, {"6", 6}, {"7", 7},
{"8", 8}, {"9", 9}, {"10", 10}, {"j", 11}, {"q", 12}, {"k", 13}
};

const map Card::RANK_MAP_B = {
{1, "a"}, {2, "2"}, {3, "3"}, {4, "4"}, {5, "5"}, {6, "6"}, {7, "7"},
{8, "8"}, {9, "9"}, {10, "10"}, {11, "j"}, {12, "q"}, {13, "k"}
};
```

But with `ColumnLimit: 86` it becomes:
```cpp
const map Card::SUIT_MAP_F = {{"c", 0}, {"d", 1}, {"s", 2}, {"h", 3}};

const map Card::SUIT_MAP_B = {{0, "c"}, {1, "d"}, {2, "s"}, {3, "h"}};

const map Card::RANK_MAP_F = {{"a", 1}, {"2", 2}, {"3", 3}, {"4", 4},
{"5", 5}, {"6", 6}, {"7", 7}, {"8", 8},
{"9", 9}, {"10", 10}, {"j", 11}, {"q", 12},
{"k", 13}};

const map Card::RANK_MAP_B = {{1, "a"}, {2, "2"}, {3, "3"}, {4, "4"},
{5, "5"}, {6, "6"}, {7, "7"}, {8, "8"},
{9, "9"}, {10, "10"}, {11, "j"}, {12, "q"},
{13, "k"}};
```

Additionally, with `ColumnLimit: 100`, `BinPackArguments: false`, and `BinPackParameters: false`:
```cpp
const map Card::SUIT_MAP_F = {{"c", 0}, {"d", 1}, {"s", 2}, {"h", 3}};

const map Card::SUIT_MAP_B = {{0, "c"}, {1, "d"}, {2, "s"}, {3, "h"}};

const map Card::RANK_MAP_F = {
{"a", 1},
{"2", 2},
{"3", 3},
{"4", 4},
{"5", 5},
{"6", 6},
{"7", 7},
{"8", 8},
{"9", 9},
{"10", 10},
{"j", 11},
{"q", 12},
{"k", 13}
};

const map Card::RANK_MAP_B = {
{1, "a"},
{2, "2"},
{3, "3"},
{4, "4"},
{5, "5"},
{6, "6"},
{7, "7"},
{8, "8"},
{9, "9"},
{10, "10"},
{11, "j"},
{12, "q"},
{13, "k"}
};
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the behavior with the provided .clang-format configuration and the examples at ColumnLimit values 80, 85, 86, and 100. No source file or test is named, so locate the clang-format formatting and regression-test entry points; done means AlignAfterOpenBracket: BlockIndent behaves consistently without requiring the BinPack settings.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.