[clang] fd33cca - [clang-format] Fix AlignConsecutiveAssignments breaking lambda formatting.
Marek Kurdej via cfe-commits
cfe-commits at lists.llvm.org
Tue Feb 1 00:18:02 PST 2022
Author: Marek Kurdej
Date: 2022-02-01T09:17:59+01:00
New Revision: fd33cca762fac265d28abbb080eec57f011f7cb4
URL: https://github.com/llvm/llvm-project/commit/fd33cca762fac265d28abbb080eec57f011f7cb4
DIFF: https://github.com/llvm/llvm-project/commit/fd33cca762fac265d28abbb080eec57f011f7cb4.diff
LOG: [clang-format] Fix AlignConsecutiveAssignments breaking lambda formatting.
Fixes https://github.com/llvm/llvm-project/issues/52772.
This patch fixes the formatting of the code:
```
auto aaaaaaaaaaaaaaaaaaaaa = {};
auto b = g([] {
return;
});
```
which should be left as is, but before this patch was formatted to:
```
auto aaaaaaaaaaaaaaaaaaaaa = {};
auto b = g([] {
return;
});
```
Reviewed By: MyDeveloperDay, HazardyKnusperkeks
Differential Revision: https://reviews.llvm.org/D115972
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 0d2e507ac587..f868c9d5752f 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -344,6 +344,10 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName))
return true;
+ // Lambda.
+ if (Changes[ScopeStart - 1].Tok->is(TT_LambdaLBrace))
+ return false;
+
// Continued function declaration
if (ScopeStart > Start + 1 &&
Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName))
@@ -352,8 +356,13 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
// Continued function call
if (ScopeStart > Start + 1 &&
Changes[ScopeStart - 2].Tok->is(tok::identifier) &&
- Changes[ScopeStart - 1].Tok->is(tok::l_paren))
+ Changes[ScopeStart - 1].Tok->is(tok::l_paren) &&
+ Changes[ScopeStart].Tok->isNot(TT_LambdaLSquare)) {
+ if (Changes[i].Tok->MatchingParen &&
+ Changes[i].Tok->MatchingParen->is(TT_LambdaLBrace))
+ return false;
return Style.BinPackArguments;
+ }
// Ternary operator
if (Changes[i].Tok->is(TT_ConditionalExpr))
@@ -372,8 +381,15 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
if (ScopeStart > Start + 1 &&
Changes[ScopeStart - 2].Tok->isNot(tok::identifier) &&
Changes[ScopeStart - 1].Tok->is(tok::l_brace) &&
- Changes[i].Tok->isNot(tok::r_brace))
+ Changes[i].Tok->isNot(tok::r_brace)) {
+ for (unsigned OuterScopeStart : llvm::reverse(ScopeStack)) {
+ // Lambda.
+ if (OuterScopeStart > Start &&
+ Changes[OuterScopeStart - 1].Tok->is(TT_LambdaLBrace))
+ return false;
+ }
return true;
+ }
return false;
};
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index bbe3f53807e6..005e2d6a7b55 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -16571,6 +16571,43 @@ TEST_F(FormatTest, AlignConsecutiveAssignments) {
"int yy = 1; ///specificlennospace\n"
"int zzz = 2;\n",
Alignment));
+
+ verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
+ "auto b = [] {\n"
+ " f();\n"
+ " return;\n"
+ "};",
+ Alignment);
+ verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
+ "auto b = g([] {\n"
+ " f();\n"
+ " return;\n"
+ "});",
+ Alignment);
+ verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
+ "auto b = g(param, [] {\n"
+ " f();\n"
+ " return;\n"
+ "});",
+ Alignment);
+ verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
+ "auto b = [] {\n"
+ " if (condition) {\n"
+ " return;\n"
+ " }\n"
+ "};",
+ Alignment);
+
+ verifyFormat("auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " ccc ? aaaaa : bbbbb,\n"
+ " dddddddddddddddddddddddddd);",
+ Alignment);
+ // FIXME: https://llvm.org/PR53497
+ // verifyFormat("auto aaaaaaaaaaaa = f();\n"
+ // "auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ // " ccc ? aaaaa : bbbbb,\n"
+ // " dddddddddddddddddddddddddd);",
+ // Alignment);
}
TEST_F(FormatTest, AlignConsecutiveBitFields) {
More information about the cfe-commits
mailing list