[clang] 9f8a7e8 - [clang-format] Allow nested [] in C# attributes

Jonathan Coe via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 3 09:35:21 PST 2020


Author: Jonathan Coe
Date: 2020-03-03T17:35:09Z
New Revision: 9f8a7e82b85078b5afbbc44429355f156e044205

URL: https://github.com/llvm/llvm-project/commit/9f8a7e82b85078b5afbbc44429355f156e044205
DIFF: https://github.com/llvm/llvm-project/commit/9f8a7e82b85078b5afbbc44429355f156e044205.diff

LOG: [clang-format] Allow nested [] in C# attributes

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 802bb514a38f..06c740048176 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -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;

diff  --git a/clang/unittests/Format/FormatTestCSharp.cpp b/clang/unittests/Format/FormatTestCSharp.cpp
index d22e0da82321..6251f97b8e0b 100644
--- a/clang/unittests/Format/FormatTestCSharp.cpp
+++ b/clang/unittests/Format/FormatTestCSharp.cpp
@@ -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.


        


More information about the cfe-commits mailing list