From d03e342803df7a75fb86c5a5c07cd84f3683bef9 Mon Sep 17 00:00:00 2001 From: Marek Kurdej Date: Thu, 3 Mar 2022 15:37:43 +0100 Subject: [PATCH] [clang-format] Fix assertion failure/crash with `AllowShortFunctionsOnASingleLine: Inline/InlineOnly`. Fixes https://github.com/llvm/llvm-project/issues/54147. When handling `AllowShortFunctionsOnASingleLine`, we were searching for the last line with a smaller level than the current line. The search was incorrect when the first line had the same level as the current one. This led to an unsatisfied assumption about the existence of a brace (non-comment token). Reviewed By: HazardyKnusperkeks, owenpan Differential Revision: https://reviews.llvm.org/D120902 --- clang/lib/Format/UnwrappedLineFormatter.cpp | 2 ++ clang/unittests/Format/FormatTest.cpp | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp index dbf1e4c..5b54399 100644 --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -310,6 +310,8 @@ private: for (; J != AnnotatedLines.begin(); --J) if ((*J)->Level < TheLine->Level) break; + if ((*J)->Level >= TheLine->Level) + return false; // Check if the found line starts a record. const FormatToken *LastNonComment = (*J)->Last; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 8909acd..1f8601d 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -12669,6 +12669,13 @@ TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { "};", MergeInlineOnly); verifyFormat("int f() {}", MergeInlineOnly); + // https://llvm.org/PR54147 + verifyFormat("auto lambda = []() {\n" + " // comment\n" + " f();\n" + " g();\n" + "};", + MergeInlineOnly); // Also verify behavior when BraceWrapping.AfterFunction = true MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; -- 2.7.4