[PATCH] D75517: [clang-format] Do not format C# array subscript operators as attributes

Phabricator via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 3 14:27:13 PST 2020


This revision was automatically updated to reflect the committed changes.
Closed by commit rG736fef97c7ac: [clang-format] Do not format C# array subscript operators as attributes (authored by Jonathan Coe <jbcoe at google.com>).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D75517/new/

https://reviews.llvm.org/D75517

Files:
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTestCSharp.cpp


Index: clang/unittests/Format/FormatTestCSharp.cpp
===================================================================
--- clang/unittests/Format/FormatTestCSharp.cpp
+++ clang/unittests/Format/FormatTestCSharp.cpp
@@ -628,5 +628,13 @@
                Style); // An array of a nullable type.
 }
 
+TEST_F(FormatTestCSharp, CSharpArraySubscripts) {
+  FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
+
+  // Do not format array subscript operators as attributes.
+  verifyFormat(R"(if (someThings[index].Contains(myThing)) {)", Style);
+  verifyFormat(R"(if (someThings[i][j][k].Contains(myThing)) {)", Style);
+}
+
 } // namespace format
 } // end namespace clang
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -369,6 +369,17 @@
     if (!Style.isCSharp())
       return false;
 
+    // `identifier[i]` is not an attribute.
+    if (Tok.Previous && Tok.Previous->is(tok::identifier))
+      return false;
+
+    // Chains [] in of `identifier[i][j][k]` are not attributes.
+    if (Tok.Previous && Tok.Previous->is(tok::r_square)) {
+      auto *MatchingParen = Tok.Previous->MatchingParen;
+      if (!MatchingParen || MatchingParen->is(TT_ArraySubscriptLSquare))
+        return false;
+    }
+
     const FormatToken *AttrTok = Tok.Next;
     if (!AttrTok)
       return false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75517.248035.patch
Type: text/x-patch
Size: 1441 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200303/4941ed96/attachment-0001.bin>


More information about the cfe-commits mailing list