Skip to content

Conversation

@DaanDeMeyer
Copy link
Contributor

Currently, in the following snippet, the second designated initializer is incorrectly detected as an OBJC method expr. Fix that and a test to make sure we don't regress.

Foo foo[] = {[0] = 1, [1] = 2};

@llvmbot
Copy link
Member

llvmbot commented Nov 23, 2025

@llvm/pr-subscribers-clang-format

Author: Daan De Meyer (DaanDeMeyer)

Changes

Currently, in the following snippet, the second designated initializer is incorrectly detected as an OBJC method expr. Fix that and a test to make sure we don't regress.

Foo foo[] = {[0] = 1, [1] = 2};

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

2 Files Affected:

  • (modified) clang/lib/Format/TokenAnnotator.cpp (+5)
  • (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+5)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index cb41756c56bf7..d7f0cf646a4e0 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -708,6 +708,11 @@ class AnnotatingParser {
         IsCpp && !IsCpp11AttributeSpecifier && !IsCSharpAttributeSpecifier &&
         Contexts.back().CanBeExpression && Left->isNot(TT_LambdaLSquare) &&
         CurrentToken->isNoneOf(tok::l_brace, tok::r_square) &&
+        // Do not consider '[' after a comma inside a braced initializer the
+        // start of an ObjC method expression. In braced initializer lists,
+        // commas are list separators and should not trigger ObjC parsing.
+        !(Parent && Parent->is(tok::comma) &&
+          Contexts.back().ContextKind == tok::l_brace) &&
         (!Parent ||
          Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
                          tok::kw_return, tok::kw_throw) ||
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 815c79e68dac9..a6fa9cf9d18de 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -3361,6 +3361,11 @@ TEST_F(TokenAnnotatorTest, UnderstandDesignatedInitializers) {
   ASSERT_EQ(Tokens.size(), 14u) << Tokens;
   EXPECT_TOKEN(Tokens[6], tok::l_square, TT_DesignatedInitializerLSquare);
   EXPECT_BRACE_KIND(Tokens[9], BK_BracedInit);
+
+  Tokens = annotate("Foo foo[] = {[0] = 1, [1] = 2};");
+  ASSERT_EQ(Tokens.size(), 20u) << Tokens;
+  EXPECT_TOKEN(Tokens[6], tok::l_square, TT_DesignatedInitializerLSquare);
+  EXPECT_TOKEN(Tokens[12], tok::l_square, TT_DesignatedInitializerLSquare);
 }
 
 TEST_F(TokenAnnotatorTest, UnderstandsJavaScript) {

@DaanDeMeyer DaanDeMeyer force-pushed the designated-initializer-fix branch from 8c7b15b to f578078 Compare November 24, 2025 10:04
@github-actions
Copy link

github-actions bot commented Nov 24, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@github-actions
Copy link

github-actions bot commented Nov 24, 2025

🐧 Linux x64 Test Results

  • 111386 tests passed
  • 4444 tests skipped

@DaanDeMeyer DaanDeMeyer force-pushed the designated-initializer-fix branch from f578078 to 0b9438a Compare November 24, 2025 10:41
Currently, in the following snippet, the second designated initializer
is incorrectly detected as an OBJC method expr. Fix that and a test to
make sure we don't regress.

```
Foo foo[] = {[0] = 1, [1] = 2};
```
@DaanDeMeyer DaanDeMeyer force-pushed the designated-initializer-fix branch from 0b9438a to 6180351 Compare November 24, 2025 11:25
@DaanDeMeyer
Copy link
Contributor Author

Someone please merge this for me when deemed ready, I don't have commit rights anymore as I'm just a casual contributor

@HazardyKnusperkeks HazardyKnusperkeks merged commit 7b186e4 into llvm:main Nov 24, 2025
10 checks passed
aadeshps-mcw pushed a commit to aadeshps-mcw/llvm-project that referenced this pull request Nov 26, 2025
Currently, in the following snippet, the second designated initializer
is incorrectly detected as an OBJC method expr. Fix that and a test to
make sure we don't regress.

```
Foo foo[] = {[0] = 1, [1] = 2};
```
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.

3 participants