[clang] [clang-format] Correctly annotate braces in ObjC square brackets (PR #106654)

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 30 02:28:13 PDT 2024


https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/106654

>From b71f4cf3ae2844583171cd0cc75826b8911a7b2b Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Thu, 29 Aug 2024 19:27:32 -0700
Subject: [PATCH 1/2] [clang-format] Correctly annotate braces in ObjC square
 brackets

See https://github.com/llvm/llvm-project/pull/88238#issuecomment-2316954781.
---
 clang/lib/Format/UnwrappedLineParser.cpp      | 1 +
 clang/unittests/Format/TokenAnnotatorTest.cpp | 8 ++++++++
 2 files changed, 9 insertions(+)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 5b518bf6c859e8..246b29d308bfaf 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -2682,6 +2682,7 @@ void UnwrappedLineParser::parseSquare(bool LambdaIntroducer) {
       break;
     }
     case tok::at:
+    case tok::colon:
       nextToken();
       if (FormatTok->is(tok::l_brace)) {
         nextToken();
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 5aa5d93c1cb067..aa6244f9365976 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -3286,6 +3286,14 @@ TEST_F(TokenAnnotatorTest, BlockLBrace) {
   EXPECT_BRACE_KIND(Tokens[4], BK_Block);
   EXPECT_TOKEN(Tokens[5], tok::l_brace, TT_BlockLBrace);
   EXPECT_BRACE_KIND(Tokens[5], BK_Block);
+
+  Tokens = annotate("[foo bar:{{0, 1}}];", getLLVMStyle(FormatStyle::LK_ObjC));
+  ASSERT_EQ(Tokens.size(), 14u) << Tokens;
+  EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_Unknown); // Not TT_BlockLBrace.
+  EXPECT_BRACE_KIND(Tokens[4], BK_Unknown);          // Not BK_Block.
+  EXPECT_BRACE_KIND(Tokens[5], BK_BracedInit);
+  EXPECT_BRACE_KIND(Tokens[9], BK_Unknown);  // Not BK_Block.
+  EXPECT_BRACE_KIND(Tokens[10], BK_Unknown); // Not BK_Block.
 }
 
 TEST_F(TokenAnnotatorTest, SwitchExpression) {

>From 73ebdda063c030f932b46244b7c2572b55a6d814 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Fri, 30 Aug 2024 02:27:03 -0700
Subject: [PATCH 2/2] Update unit test

---
 clang/unittests/Format/TokenAnnotatorTest.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index aa6244f9365976..497b911f4efbba 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -3287,8 +3287,9 @@ TEST_F(TokenAnnotatorTest, BlockLBrace) {
   EXPECT_TOKEN(Tokens[5], tok::l_brace, TT_BlockLBrace);
   EXPECT_BRACE_KIND(Tokens[5], BK_Block);
 
-  Tokens = annotate("[foo bar:{{0, 1}}];", getLLVMStyle(FormatStyle::LK_ObjC));
-  ASSERT_EQ(Tokens.size(), 14u) << Tokens;
+  Tokens = annotate("[foo bar:{{0, 1}} baz:baz];",
+                    getLLVMStyle(FormatStyle::LK_ObjC));
+  ASSERT_EQ(Tokens.size(), 17u) << Tokens;
   EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_Unknown); // Not TT_BlockLBrace.
   EXPECT_BRACE_KIND(Tokens[4], BK_Unknown);          // Not BK_Block.
   EXPECT_BRACE_KIND(Tokens[5], BK_BracedInit);



More information about the cfe-commits mailing list