[clang-format] Fix invalid code generation with comments in lambda

Fixes #51234 and #54496

Differential Revision: https://reviews.llvm.org/D122301
This commit is contained in:
owenca 2022-03-23 03:16:19 -07:00
parent f319c24570
commit f74413d163
2 changed files with 34 additions and 6 deletions

View file

@ -2762,12 +2762,16 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
Current->SpacesRequiredBefore = 1; Current->SpacesRequiredBefore = 1;
} }
Current->MustBreakBefore = const auto &Children = Prev->Children;
Current->MustBreakBefore || mustBreakBefore(Line, *Current); if (!Children.empty() && Children.back()->Last->is(TT_LineComment)) {
Current->MustBreakBefore = true;
if (!Current->MustBreakBefore && InFunctionDecl && } else {
Current->is(TT_FunctionDeclarationName)) Current->MustBreakBefore =
Current->MustBreakBefore = mustBreakForReturnType(Line); Current->MustBreakBefore || mustBreakBefore(Line, *Current);
if (!Current->MustBreakBefore && InFunctionDecl &&
Current->is(TT_FunctionDeclarationName))
Current->MustBreakBefore = mustBreakForReturnType(Line);
}
Current->CanBreakBefore = Current->CanBreakBefore =
Current->MustBreakBefore || canBreakBefore(Line, *Current); Current->MustBreakBefore || canBreakBefore(Line, *Current);

View file

@ -21934,6 +21934,30 @@ TEST_F(FormatTest, LambdaWithLineComments) {
"auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
"{ return; }", "{ return; }",
LLVMWithBeforeLambdaBody); LLVMWithBeforeLambdaBody);
LLVMWithBeforeLambdaBody.ColumnLimit = 0;
verifyFormat("foo([]()\n"
" {\n"
" bar(); //\n"
" return 1; // comment\n"
" }());",
"foo([]() {\n"
" bar(); //\n"
" return 1; // comment\n"
"}());",
LLVMWithBeforeLambdaBody);
verifyFormat("foo(\n"
" 1, MACRO {\n"
" baz();\n"
" bar(); // comment\n"
" },\n"
" []() {});",
"foo(\n"
" 1, MACRO { baz(); bar(); // comment\n"
" }, []() {}\n"
");",
LLVMWithBeforeLambdaBody);
} }
TEST_F(FormatTest, EmptyLinesInLambdas) { TEST_F(FormatTest, EmptyLinesInLambdas) {