Author: owenca Date: 2022-03-23T19:40:24-07:00 New Revision: f74413d16345c8815c7d1e4676d6aaf732517b8d
URL: https://github.com/llvm/llvm-project/commit/f74413d16345c8815c7d1e4676d6aaf732517b8d DIFF: https://github.com/llvm/llvm-project/commit/f74413d16345c8815c7d1e4676d6aaf732517b8d.diff LOG: [clang-format] Fix invalid code generation with comments in lambda Fixes #51234 and #54496 Differential Revision: https://reviews.llvm.org/D122301 Added: Modified: clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/FormatTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index a6ca459a53337..c657d80ba0d5f 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -2762,12 +2762,16 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) { Current->SpacesRequiredBefore = 1; } - Current->MustBreakBefore = - Current->MustBreakBefore || mustBreakBefore(Line, *Current); - - if (!Current->MustBreakBefore && InFunctionDecl && - Current->is(TT_FunctionDeclarationName)) - Current->MustBreakBefore = mustBreakForReturnType(Line); + const auto &Children = Prev->Children; + if (!Children.empty() && Children.back()->Last->is(TT_LineComment)) { + Current->MustBreakBefore = true; + } else { + Current->MustBreakBefore = + Current->MustBreakBefore || mustBreakBefore(Line, *Current); + if (!Current->MustBreakBefore && InFunctionDecl && + Current->is(TT_FunctionDeclarationName)) + Current->MustBreakBefore = mustBreakForReturnType(Line); + } Current->CanBreakBefore = Current->MustBreakBefore || canBreakBefore(Line, *Current); diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index e36a267c01f4b..e837dbd566957 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -21934,6 +21934,30 @@ TEST_F(FormatTest, LambdaWithLineComments) { "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" "{ return; }", 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) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits