[clang] 6c7e6fc - [clang-format] Do not remove required spaces when aligning tokens.
Marek Kurdej via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 10 10:15:35 PST 2022
Author: Marek Kurdej
Date: 2022-02-10T19:15:27+01:00
New Revision: 6c7e6fc7b6654b7ecd364f352f8ffd6dfecbf77b
URL: https://github.com/llvm/llvm-project/commit/6c7e6fc7b6654b7ecd364f352f8ffd6dfecbf77b
DIFF: https://github.com/llvm/llvm-project/commit/6c7e6fc7b6654b7ecd364f352f8ffd6dfecbf77b.diff
LOG: [clang-format] Do not remove required spaces when aligning tokens.
Fixes https://github.com/llvm/llvm-project/issues/44292.
Fixes https://github.com/llvm/llvm-project/issues/45874.
Reviewed By: HazardyKnusperkeks, owenpan
Differential Revision: https://reviews.llvm.org/D119419
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 1d639275b1d59..758dc5860888e 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -331,6 +331,12 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
FoundMatchOnLine = true;
Shift = Column - Changes[i].StartOfTokenColumn;
Changes[i].Spaces += Shift;
+ // FIXME: This is a workaround that should be removed when we fix
+ // http://llvm.org/PR53699. An assertion later below verifies this.
+ if (Changes[i].NewlinesBefore == 0)
+ Changes[i].Spaces =
+ std::max(Changes[i].Spaces,
+ static_cast<int>(Changes[i].Tok->SpacesRequiredBefore));
}
// This is for function parameters that are split across multiple lines,
@@ -399,6 +405,12 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
if (ContinuedStringLiteral)
Changes[i].Spaces += Shift;
+ // We should not remove required spaces unless we break the line before.
+ assert(Changes[i].NewlinesBefore > 0 ||
+ Changes[i].Spaces >=
+ static_cast<int>(Changes[i].Tok->SpacesRequiredBefore) ||
+ Changes[i].Tok->is(tok::eof));
+
Changes[i].StartOfTokenColumn += Shift;
if (i + 1 != Changes.size())
Changes[i + 1].PreviousEndOfTokenColumn += Shift;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index c7516427c2f45..8639a72eceb12 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -17277,6 +17277,31 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) {
"const unsigned g;\n"
"Const unsigned h;",
Alignment);
+
+ // See PR46529
+ FormatStyle BracedAlign = getLLVMStyle();
+ BracedAlign.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
+ verifyFormat("const auto result{[]() {\n"
+ " const auto something = 1;\n"
+ " return 2;\n"
+ "}};",
+ BracedAlign);
+ verifyFormat("int foo{[]() {\n"
+ " int bar{0};\n"
+ " return 0;\n"
+ "}()};",
+ BracedAlign);
+ BracedAlign.Cpp11BracedListStyle = false;
+ verifyFormat("const auto result{ []() {\n"
+ " const auto something = 1;\n"
+ " return 2;\n"
+ "} };",
+ BracedAlign);
+ verifyFormat("int foo{ []() {\n"
+ " int bar{ 0 };\n"
+ " return 0;\n"
+ "}() };",
+ BracedAlign);
}
TEST_F(FormatTest, AlignWithLineBreaks) {
More information about the cfe-commits
mailing list