-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[clang-format] Fix designated initializer detection #169228
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
[clang-format] Fix designated initializer detection #169228
Conversation
|
@llvm/pr-subscribers-clang-format Author: Daan De Meyer (DaanDeMeyer) ChangesCurrently, 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. Full diff: https://github.com/llvm/llvm-project/pull/169228.diff 2 Files Affected:
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) {
|
8c7b15b to
f578078
Compare
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
🐧 Linux x64 Test Results
|
f578078 to
0b9438a
Compare
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};
```
0b9438a to
6180351
Compare
|
Someone please merge this for me when deemed ready, I don't have commit rights anymore as I'm just a casual 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};
```
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.