Skip to content

Commit cc77e33

Browse files
authored
[clang-format] Don't apply severe penalty if no possible column formats (#76675)
If there are possible column formats, but they weren't selected because they don't fit within remaining characters for the current path then applying severe penalty to induce column layout by selection of a different path seems fair. But if due to style configuration or what the input code is, there are no possible column formats, different paths aren't going to have column layouts. Seems wrong to apply the severe penalty to induce column layouts if there are none available. It just causes selection of sub-optimal paths, e.g. get bad formatting when brace initializers are used inside lambda bodies. Fixes #56350
1 parent 1bc4cb5 commit cc77e33

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

clang/lib/Format/FormatToken.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ unsigned CommaSeparatedList::formatAfterToken(LineState &State,
113113
if (!State.NextToken || !State.NextToken->Previous)
114114
return 0;
115115

116-
if (Formats.size() == 1)
117-
return 0; // Handled by formatFromToken
116+
if (Formats.size() <= 1)
117+
return 0; // Handled by formatFromToken (1) or avoid severe penalty (0).
118118

119119
// Ensure that we start on the opening brace.
120120
const FormatToken *LBrace =

clang/unittests/Format/FormatTest.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13875,6 +13875,21 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
1387513875
getLLVMStyleWithColumns(35));
1387613876
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
1387713877
" aaaaaaaaaaaaaaaaaaaaaaa);");
13878+
13879+
// No possible column formats, don't want the optimal paths penalized.
13880+
verifyFormat(
13881+
"waarudo::unit desk = {\n"
13882+
" .s = \"desk\", .p = p, .b = [] { return w::r{3, 10} * w::m; }};");
13883+
verifyFormat("SomeType something1([](const Input &i) -> Output { return "
13884+
"Output{1, 2}; },\n"
13885+
" [](const Input &i) -> Output { return "
13886+
"Output{1, 2}; });");
13887+
FormatStyle NoBinPacking = getLLVMStyle();
13888+
NoBinPacking.BinPackParameters = false;
13889+
verifyFormat("waarudo::unit desk = {\n"
13890+
" .s = \"desk\", .p = p, .b = [] { return w::r{3, 10, 1, 1, "
13891+
"1, 1} * w::m; }};",
13892+
NoBinPacking);
1387813893
}
1387913894

1388013895
TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {

0 commit comments

Comments
 (0)