Skip to content

[clang-format] Fix a crash in EnumTrailingComma #135903

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 2 commits into from
Apr 19, 2025
Merged

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Apr 16, 2025

Fix #135819

@llvmbot
Copy link
Member

llvmbot commented Apr 16, 2025

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

Fix #135819


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

2 Files Affected:

  • (modified) clang/lib/Format/Format.cpp (+11-6)
  • (modified) clang/unittests/Format/FormatTest.cpp (+11)
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index c601967a8715c..b8d55871ab932 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -2427,19 +2427,23 @@ class EnumTrailingCommaEditor : public TokenAnalyzer {
 private:
   void editEnumTrailingComma(SmallVectorImpl<AnnotatedLine *> &Lines,
                              tooling::Replacements &Result) {
+    bool InEnumBraces = false;
+    const FormatToken *BeforeRBrace = nullptr;
     const auto &SourceMgr = Env.getSourceManager();
     for (auto *Line : Lines) {
       if (!Line->Children.empty())
         editEnumTrailingComma(Line->Children, Result);
-      if (!Line->Affected)
-        continue;
       for (const auto *Token = Line->First; Token && !Token->Finalized;
            Token = Token->Next) {
-        if (Token->isNot(TT_EnumRBrace))
+        if (Token->isNot(TT_EnumRBrace)) {
+          if (Token->is(TT_EnumLBrace))
+            InEnumBraces = true;
+          else if (InEnumBraces && Line->Affected && Token->isNot(tok::comment))
+            BeforeRBrace = Token;
           continue;
-        const auto *BeforeRBrace = Token->getPreviousNonComment();
-        assert(BeforeRBrace);
-        if (BeforeRBrace->is(TT_EnumLBrace)) // Empty braces.
+        }
+        InEnumBraces = false;
+        if (!BeforeRBrace) // Empty braces or Line not affected.
           continue;
         if (BeforeRBrace->is(tok::comma)) {
           if (Style.EnumTrailingComma == FormatStyle::ETC_Remove)
@@ -2448,6 +2452,7 @@ class EnumTrailingCommaEditor : public TokenAnalyzer {
           cantFail(Result.add(tooling::Replacement(
               SourceMgr, BeforeRBrace->Tok.getEndLoc(), 0, ",")));
         }
+        BeforeRBrace = nullptr;
       }
     }
   }
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index b62d49e17c83f..be0343efb4b83 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -27962,6 +27962,7 @@ TEST_F(FormatTest, EnumTrailingComma) {
   verifyFormat(Code);
 
   auto Style = getLLVMStyle();
+  EXPECT_TRUE(Style.AllowShortEnumsOnASingleLine);
   Style.EnumTrailingComma = FormatStyle::ETC_Insert;
   verifyFormat("enum : int { /**/ };\n"
                "enum {\n"
@@ -27972,6 +27973,16 @@ TEST_F(FormatTest, EnumTrailingComma) {
                "enum Color { red, green, blue, /**/ };",
                Code, Style);
 
+  Style.AllowShortEnumsOnASingleLine = false;
+  verifyFormat("enum class MyEnum_E {\n"
+               "  MY_ENUM = 0U,\n"
+               "};",
+               "enum class MyEnum_E {\n"
+               "  MY_ENUM = 0U\n"
+               "};",
+               Style);
+  Style.AllowShortEnumsOnASingleLine = true;
+
   Style.EnumTrailingComma = FormatStyle::ETC_Remove;
   verifyFormat("enum : int { /**/ };\n"
                "enum {\n"

@owenca owenca merged commit c64f670 into llvm:main Apr 19, 2025
11 checks passed
@owenca owenca deleted the 135819 branch April 19, 2025 01:11
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
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] Segmentation fault when inserting enum trailing comma
3 participants