[clang-format] Allow nested [] in C# attributes
authorJonathan Coe <jbcoe@google.com>
Tue, 3 Mar 2020 17:15:56 +0000 (17:15 +0000)
committerJonathan Coe <jbcoe@google.com>
Tue, 3 Mar 2020 17:35:09 +0000 (17:35 +0000)
Summary: Keep track of unpaired [] when identifying C# attribute lines

Reviewers: krasimir

Reviewed By: krasimir

Subscribers: cfe-commits

Tags: #clang-format, #clang

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

clang/lib/Format/UnwrappedLineParser.cpp
clang/unittests/Format/FormatTestCSharp.cpp

index 802bb514a38f7359df7f39aeccbade003db05511..06c74004817619c84c0bf0bb037da7a08e06efc9 100644 (file)
@@ -324,12 +324,21 @@ void UnwrappedLineParser::parseFile() {
 }
 
 void UnwrappedLineParser::parseCSharpAttribute() {
+  int UnpairedSquareBrackets = 1;
   do {
     switch (FormatTok->Tok.getKind()) {
     case tok::r_square:
       nextToken();
-      addUnwrappedLine();
-      return;
+      --UnpairedSquareBrackets;
+      if (UnpairedSquareBrackets == 0) {
+        addUnwrappedLine();
+        return;
+      }
+      break;
+    case tok::l_square:
+      ++UnpairedSquareBrackets;
+      nextToken();
+      break;
     default:
       nextToken();
       break;
index d22e0da82321ecdc914c15d8e4a18eb7b105f0c0..6251f97b8e0b1d9125a765afcf43798542114b1f 100644 (file)
@@ -273,6 +273,15 @@ TEST_F(FormatTestCSharp, Attributes) {
                "{\n"
                "}");
 
+  // [] in an attribute do not cause premature line wrapping or indenting.
+  verifyFormat(R"(//
+public class A
+{
+    [SomeAttribute(new[] { RED, GREEN, BLUE }, -1.0f, 1.0f)]
+    [DoNotSerialize]
+    public Data MemberVariable;
+})");
+
   //  Unwrappable lines go on a line of their own.
   // 'target:' is not treated as a label.
   // Modify Style to enforce a column limit.