[clang] f829615 - [clang-format] Improve C# handling of spaces in square brackets

Jonathan Coe via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 28 04:46:22 PST 2020


Author: Jonathan Coe
Date: 2020-02-28T12:44:15Z
New Revision: f829615205f0f671c9b6e1e89d9af78f0d40fe9d

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

LOG: [clang-format] Improve C# handling of spaces in square brackets

Reviewers: MyDeveloperDay, krasimir

Reviewed By: krasimir

Subscribers: cfe-commits

Tags: #clang-format, #clang

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

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 0185dadb15af..86a84afafb8f 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2902,8 +2902,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
     if (Left.is(TT_TemplateCloser) && Right.is(TT_StartOfName))
       return true;
 
-    // space after comma in '[,]'.
-    if (Left.is(tok::comma) && Right.is(tok::r_square))
+    // spaces inside square brackets.
+    if (Left.is(tok::l_square) || Right.is(tok::r_square))
       return Style.SpacesInSquareBrackets;
 
     // No space before ? in nullable types.

diff  --git a/clang/unittests/Format/FormatTestCSharp.cpp b/clang/unittests/Format/FormatTestCSharp.cpp
index b91a380456c9..390993881fda 100644
--- a/clang/unittests/Format/FormatTestCSharp.cpp
+++ b/clang/unittests/Format/FormatTestCSharp.cpp
@@ -600,7 +600,7 @@ TEST_F(FormatTestCSharp, CSharpSpaces) {
   verifyFormat(R"(private float[,] Values;)", Style);
 
   Style.SpacesInSquareBrackets = true;
-  verifyFormat(R"(private float[, ] Values;)", Style);
+  verifyFormat(R"(private float[ , ] Values;)", Style);
 }
 
 TEST_F(FormatTestCSharp, CSharpNullableTypes) {
@@ -608,11 +608,7 @@ TEST_F(FormatTestCSharp, CSharpNullableTypes) {
   Style.SpacesInSquareBrackets = false;
 
   verifyFormat(R"(public float? Value;)", Style); // no space before `?`.
-
-  // Erroneous spaces in square brackets here will be tackled in a follow-up
-  // patch and are not addressed by setting
-  // `Style.SpacesInSquareBrackets = false;`
-  verifyFormat(R"(int?[] arr = new int?[ 10 ];)",
+  verifyFormat(R"(int?[] arr = new int?[10];)",
                Style); // An array of a nullable type.
 }
 


        


More information about the cfe-commits mailing list