]> granicus.if.org Git - clang/commitdiff
Fix ClangFormat issue of recognizing ObjC subscript as C++ attributes when message...
authorYan Zhang <ynzhang@google.com>
Fri, 9 Nov 2018 23:19:14 +0000 (23:19 +0000)
committerYan Zhang <ynzhang@google.com>
Fri, 9 Nov 2018 23:19:14 +0000 (23:19 +0000)
Summary:
The issue is that for array subscript like:

```
arr[[Foo() bar]];
```
ClangFormat will recognize it as C++11 attribute syntax and put a space between 'arr' and first '[', like:

```
arr [[Foo() bar]];
```

Now it is fixed. Tested with:
```
ninja FormatTests
```

Reviewers: benhamilton

Reviewed By: benhamilton

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D54288

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@346566 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTest.cpp

index 6f9b71b4b1ec4ec3bc715a2c1b98f33eed35ca99..501a150f84810425d698da76490c8c2b078f9043 100644 (file)
@@ -366,7 +366,8 @@ private:
       // specifier parameter, although this is technically valid:
       // [[foo(:)]]
       if (AttrTok->is(tok::colon) ||
-          AttrTok->startsSequence(tok::identifier, tok::identifier))
+          AttrTok->startsSequence(tok::identifier, tok::identifier) || 
+          AttrTok->startsSequence(tok::r_paren, tok::identifier))
         return false;
       if (AttrTok->is(tok::ellipsis))
         return true;
index 635a34499da6a455cd2fbe621c37b9b776d05bde..2714a513bcb717b910ba9cf608f7cf6b79c7f804 100644 (file)
@@ -6472,6 +6472,8 @@ TEST_F(FormatTest, UnderstandsSquareAttributes) {
   // Make sure we do not mistake attributes for array subscripts.
   verifyFormat("int a() {}\n"
                "[[unused]] int b() {}\n");
+  verifyFormat("NSArray *arr;\n"
+               "arr[[Foo() bar]];");
 
   // On the other hand, we still need to correctly find array subscripts.
   verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");