[clang] [clang-format] Fix an assertion failure in RemoveSemicolon (PR #117472)

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 24 00:50:29 PST 2024


https://github.com/owenca created https://github.com/llvm/llvm-project/pull/117472

Fixes #117290.

>From 9a2a93fbb50163e593f6b48a279bea9f70424c06 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Sun, 24 Nov 2024 00:43:29 -0800
Subject: [PATCH] [clang-format] Fix an assertion failure in RemoveSemicolon

Fixes #117290.
---
 clang/lib/Format/Format.cpp           | 8 +++++---
 clang/unittests/Format/FormatTest.cpp | 7 +++++++
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 0cf4cdbeab31f3..0c3c4de6f00e1d 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -2325,7 +2325,7 @@ class SemiRemover : public TokenAnalyzer {
 private:
   void removeSemi(TokenAnnotator &Annotator,
                   SmallVectorImpl<AnnotatedLine *> &Lines,
-                  tooling::Replacements &Result) {
+                  tooling::Replacements &Result, bool Children = false) {
     auto PrecededByFunctionRBrace = [](const FormatToken &Tok) {
       const auto *Prev = Tok.Previous;
       if (!Prev || Prev->isNot(tok::r_brace))
@@ -2337,10 +2337,12 @@ class SemiRemover : public TokenAnalyzer {
     const auto End = Lines.end();
     for (auto I = Lines.begin(); I != End; ++I) {
       const auto Line = *I;
-      removeSemi(Annotator, Line->Children, Result);
+      if (!Line->Children.empty())
+        removeSemi(Annotator, Line->Children, Result, /*Children=*/true);
       if (!Line->Affected)
         continue;
-      Annotator.calculateFormattingInformation(*Line);
+      if (!Children)
+        Annotator.calculateFormattingInformation(*Line);
       const auto NextLine = I + 1 == End ? nullptr : I[1];
       for (auto Token = Line->First; Token && !Token->Finalized;
            Token = Token->Next) {
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 250e51b5421664..63d8dc2486e45f 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -27386,6 +27386,13 @@ TEST_F(FormatTest, RemoveSemicolon) {
                Style);
 #endif
 
+  verifyFormat("auto sgf = [] {\n"
+               "  ogl = {\n"
+               "      a, b, c, d, e,\n"
+               "  };\n"
+               "};",
+               Style);
+
   Style.TypenameMacros.push_back("STRUCT");
   verifyFormat("STRUCT(T, B) { int i; };", Style);
 }



More information about the cfe-commits mailing list