[clang] [Format] Dont treat LBrace after extends/implements as initializer list (PR #108524)

kadir çetinkaya via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 16 00:31:24 PDT 2024


https://github.com/kadircet updated https://github.com/llvm/llvm-project/pull/108524

>From e63f2acd673183d15420b41ebe50a1c061de0905 Mon Sep 17 00:00:00 2001
From: Kadir Cetinkaya <kadircet at google.com>
Date: Fri, 13 Sep 2024 11:31:43 +0200
Subject: [PATCH] [Format] Dont treat LBrace after extends/implements as
 initializer list

This extends the fix in https://github.com/llvm/llvm-project/pull/106242
for other derived class types.
---
 clang/lib/Format/UnwrappedLineParser.cpp      | 2 +-
 clang/unittests/Format/FormatTestJS.cpp       | 7 ++++++-
 clang/unittests/Format/TokenAnnotatorTest.cpp | 8 ++++++++
 3 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 1727ed93822b1b..40f77266fabdca 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -4042,7 +4042,7 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
   }
 
   auto IsListInitialization = [&] {
-    if (!ClassName || IsDerived)
+    if (!ClassName || IsDerived || JSPastExtendsOrImplements)
       return false;
     assert(FormatTok->is(tok::l_brace));
     const auto *Prev = FormatTok->getPreviousNonComment();
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index c25228a69a748f..57c021c76867f7 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -579,12 +579,17 @@ TEST_F(FormatTestJS, GoogScopes) {
                "});");
 }
 
-TEST_F(FormatTestJS, GoogAnonymousClass) {
+TEST_F(FormatTestJS, ClassExtends) {
   verifyFormat("a = class extends goog.structs.a {\n"
                "  a() {\n"
                "    return 0;\n"
                "  }\n"
                "};");
+  verifyFormat("a = class Foo extends goog.structs.a {\n"
+               "  a() {\n"
+               "    return 0;\n"
+               "  }\n"
+               "};");
 }
 
 TEST_F(FormatTestJS, IIFEs) {
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 5c28e3a4ea5a1f..baa5ab0ac5e456 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -3277,6 +3277,14 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
   EXPECT_TOKEN(Tokens[8], tok::r_brace, TT_ClassRBrace);
   EXPECT_BRACE_KIND(Tokens[8], BK_Block);
 
+  Tokens = annotate("a = class Foo extends goog.a {};",
+                    getGoogleStyle(FormatStyle::LK_JavaScript));
+  ASSERT_EQ(Tokens.size(), 12u) << Tokens;
+  EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_ClassLBrace);
+  EXPECT_BRACE_KIND(Tokens[8], BK_Block);
+  EXPECT_TOKEN(Tokens[9], tok::r_brace, TT_ClassRBrace);
+  EXPECT_BRACE_KIND(Tokens[9], BK_Block);
+
   Tokens = annotate("#define FOO(X) \\\n"
                     "  struct X##_tag_ {};");
   ASSERT_EQ(Tokens.size(), 14u) << Tokens;



More information about the cfe-commits mailing list