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

via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 24 00:51:03 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

<details>
<summary>Changes</summary>

Fixes #<!-- -->117290.

---
Full diff: https://github.com/llvm/llvm-project/pull/117472.diff


2 Files Affected:

- (modified) clang/lib/Format/Format.cpp (+5-3) 
- (modified) clang/unittests/Format/FormatTest.cpp (+7) 


``````````diff
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);
 }

``````````

</details>


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


More information about the cfe-commits mailing list