r371720 - [clang-format] [PR43100] clang-format C# support does not add a space between "using" and paren

Paul Hoad via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 12 03:18:53 PDT 2019


Author: paulhoad
Date: Thu Sep 12 03:18:53 2019
New Revision: 371720

URL: http://llvm.org/viewvc/llvm-project?rev=371720&view=rev
Log:
[clang-format] [PR43100] clang-format C#  support does not add a space between "using" and paren

Summary:
Addresses https://bugs.llvm.org/show_bug.cgi?id=43100

Formatting using statement in C# with clang-format removes the space between using and paren even when SpaceBeforeParens is !

```
using(FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize : 1))
```

this change simply overcomes this for when using C# settings in the .clang-format file

```
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize : 1))
```

All FormatTests pass..

```
[==========] 688 tests from 21 test cases ran. (88508 ms total)
[  PASSED  ] 688 tests.
```

Reviewers: djasper, klimek, owenpan

Reviewed By: owenpan

Subscribers: llvm-commits, cfe-commits

Tags: #clang

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

Modified:
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/unittests/Format/FormatTestCSharp.cpp

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=371720&r1=371719&r2=371720&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Thu Sep 12 03:18:53 2019
@@ -2611,6 +2611,10 @@ bool TokenAnnotator::spaceRequiredBetwee
     return Style.Language == FormatStyle::LK_JavaScript ||
            !Left.TokenText.endswith("=*/");
   if (Right.is(tok::l_paren)) {
+    // using (FileStream fs...
+    if (Style.isCSharp() && Left.is(tok::kw_using) &&
+        Style.SpaceBeforeParens != FormatStyle::SBPO_Never)
+      return true;
     if ((Left.is(tok::r_paren) && Left.is(TT_AttributeParen)) ||
         (Left.is(tok::r_square) && Left.is(TT_AttributeSquare)))
       return true;

Modified: cfe/trunk/unittests/Format/FormatTestCSharp.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestCSharp.cpp?rev=371720&r1=371719&r2=371720&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestCSharp.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestCSharp.cpp Thu Sep 12 03:18:53 2019
@@ -165,6 +165,21 @@ TEST_F(FormatTestCSharp, Attributes) {
                "public string Host {\n  set;\n  get;\n}");
 }
 
+TEST_F(FormatTestCSharp, CSharpUsing) {
+  FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
+  Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
+  verifyFormat("public void foo() {\n"
+               "  using (StreamWriter sw = new StreamWriter (filenameA)) {}\n"
+               "}",
+               Style);
+
+  Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
+  verifyFormat("public void foo() {\n"
+               "  using(StreamWriter sw = new StreamWriter(filenameB)) {}\n"
+               "}",
+               Style);
+}
+
 TEST_F(FormatTestCSharp, CSharpRegions) {
   verifyFormat("#region aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaa "
                "aaaaaaaaaaaaaaa long region");




More information about the cfe-commits mailing list