[clang] e57f578 - [clang-format] fix alignment w/o binpacked args
Marek Kurdej via cfe-commits
cfe-commits at lists.llvm.org
Mon May 16 01:25:06 PDT 2022
Author: Gregory Fong
Date: 2022-05-16T10:25:06+02:00
New Revision: e57f57841fbb0cd1d1dbd3237c2cbe6ce15984dd
URL: https://github.com/llvm/llvm-project/commit/e57f57841fbb0cd1d1dbd3237c2cbe6ce15984dd
DIFF: https://github.com/llvm/llvm-project/commit/e57f57841fbb0cd1d1dbd3237c2cbe6ce15984dd.diff
LOG: [clang-format] fix alignment w/o binpacked args
The combination of
- AlignConsecutiveAssignments.Enabled = true
- BinPackArguments = false
would result in the first continuation line of a braced-init-list being
improperly indented (missing a shift) when in a continued function call.
Indentation was also wrong for braced-init-lists continuing a
direct-list-initialization. Check for opening braced lists in
continuation and ensure that the correct shift occurs.
Fixes https://github.com/llvm/llvm-project/issues/55360
Reviewed By: curdeius
Differential Revision: https://reviews.llvm.org/D125162
Added:
Modified:
clang/lib/Format/WhitespaceManager.cpp
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index 673af51d2daa..f20383800ab4 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -371,6 +371,9 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
return false;
if (Changes[ScopeStart].NewlinesBefore > 0)
return false;
+ if (Changes[i].Tok->is(tok::l_brace) &&
+ Changes[i].Tok->is(BK_BracedInit))
+ return true;
return Style.BinPackArguments;
}
@@ -387,6 +390,14 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
Changes[i].Tok->Previous->is(TT_ConditionalExpr))
return true;
+ // Continued direct-list-initialization using braced list.
+ if (ScopeStart > Start + 1 &&
+ Changes[ScopeStart - 2].Tok->is(tok::identifier) &&
+ Changes[ScopeStart - 1].Tok->is(tok::l_brace) &&
+ Changes[i].Tok->is(tok::l_brace) &&
+ Changes[i].Tok->is(BK_BracedInit))
+ return true;
+
// Continued braced list.
if (ScopeStart > Start + 1 &&
Changes[ScopeStart - 2].Tok->isNot(tok::identifier) &&
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 44da34001f99..22c46a129403 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -17318,6 +17318,23 @@ TEST_F(FormatTest, AlignConsecutiveAssignments) {
// " ccc ? aaaaa : bbbbb,\n"
// " dddddddddddddddddddddddddd);",
// Alignment);
+
+ // Confirm proper handling of AlignConsecutiveAssignments with
+ // BinPackArguments.
+ // See https://llvm.org/PR55360
+ Alignment = getLLVMStyleWithColumns(50);
+ Alignment.AlignConsecutiveAssignments.Enabled = true;
+ Alignment.BinPackArguments = false;
+ verifyFormat("int a_long_name = 1;\n"
+ "auto b = B({a_long_name, a_long_name},\n"
+ " {a_longer_name_for_wrap,\n"
+ " a_longer_name_for_wrap});",
+ Alignment);
+ verifyFormat("int a_long_name = 1;\n"
+ "auto b = B{{a_long_name, a_long_name},\n"
+ " {a_longer_name_for_wrap,\n"
+ " a_longer_name_for_wrap}};",
+ Alignment);
}
TEST_F(FormatTest, AlignConsecutiveBitFields) {
More information about the cfe-commits
mailing list