Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2024

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Nov 24, 2024

Fixes #117290.

@llvmbot
Copy link
Member

llvmbot commented Nov 24, 2024

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

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

@owenca
Copy link
Contributor Author

owenca commented Dec 5, 2024

@owenca owenca merged commit cb61a5e into llvm:main Dec 8, 2024
8 checks passed
@owenca owenca deleted the 117290 branch December 8, 2024 00:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang-format] Assertion failed in FormatToken.cpp
3 participants