[clang] 736fef9 - [clang-format] Do not format C# array subscript operators as attributes

Jonathan Coe via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 3 14:25:01 PST 2020


Author: Jonathan Coe
Date: 2020-03-03T22:21:33Z
New Revision: 736fef97c7ac4113388fc56db10c599fb4d2f074

URL: https://github.com/llvm/llvm-project/commit/736fef97c7ac4113388fc56db10c599fb4d2f074
DIFF: https://github.com/llvm/llvm-project/commit/736fef97c7ac4113388fc56db10c599fb4d2f074.diff

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

Summary:
Fix misidentification of C# array subscript operators.

Reviewers: krasimir

Reviewed By: krasimir

Subscribers: cfe-commits, MyDeveloperDay

Tags: #clang-format, #clang

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index a074d54fd44c..493454eb2239 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -369,6 +369,17 @@ class AnnotatingParser {
     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;

diff  --git a/clang/unittests/Format/FormatTestCSharp.cpp b/clang/unittests/Format/FormatTestCSharp.cpp
index 6251f97b8e0b..ad849f2dbdf7 100644
--- a/clang/unittests/Format/FormatTestCSharp.cpp
+++ b/clang/unittests/Format/FormatTestCSharp.cpp
@@ -628,5 +628,13 @@ TEST_F(FormatTestCSharp, CSharpNullableTypes) {
                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


        


More information about the cfe-commits mailing list