[clang] d03e342 - [clang-format] Fix assertion failure/crash with `AllowShortFunctionsOnASingleLine: Inline/InlineOnly`.

Marek Kurdej via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 7 07:54:15 PST 2022


Author: Marek Kurdej
Date: 2022-03-07T16:54:08+01:00
New Revision: d03e342803df7a75fb86c5a5c07cd84f3683bef9

URL: https://github.com/llvm/llvm-project/commit/d03e342803df7a75fb86c5a5c07cd84f3683bef9
DIFF: https://github.com/llvm/llvm-project/commit/d03e342803df7a75fb86c5a5c07cd84f3683bef9.diff

LOG: [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

Added: 
    

Modified: 
    clang/lib/Format/UnwrappedLineFormatter.cpp
    clang/unittests/Format/FormatTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index dbf1e4cbbf6a3..5b5439901b2f7 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -310,6 +310,8 @@ class LineJoiner {
           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 8909acdae5dc3..1f8601d30857c 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;


        


More information about the cfe-commits mailing list