[clang] 9520bf1 - [clang-format] Update GoogleStyle for C# code to match Google's internal C# style guide

Jonathan Coe via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 4 07:49:18 PDT 2020


Author: Jonathan Coe
Date: 2020-06-04T15:48:00+01:00
New Revision: 9520bf146dd3baae8608755d7de0855db4664c77

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

LOG: [clang-format] Update GoogleStyle for C# code to match Google's internal C# style guide

Summary: Google's C# style guide is at https://google.github.io/styleguide/csharp-style.html

Reviewers: krasimir, MyDeveloperDay, sammccall

Reviewed By: MyDeveloperDay

Subscribers: cfe-commits, klimek

Tags: #clang-format, #clang

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index f3743921d52e..1f7a8029bac0 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -14,6 +14,7 @@
 
 #include "clang/Format/Format.h"
 #include "AffectedRangeManager.h"
+#include "BreakableToken.h"
 #include "ContinuationIndenter.h"
 #include "FormatInternal.h"
 #include "FormatTokenLexer.h"
@@ -1068,6 +1069,12 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
     // #imports, etc.)
     GoogleStyle.IncludeStyle.IncludeBlocks =
         tooling::IncludeStyle::IBS_Preserve;
+  } else if (Language == FormatStyle::LK_CSharp) {
+    GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
+    GoogleStyle.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Never;
+    GoogleStyle.BreakStringLiterals = false;
+    GoogleStyle.ColumnLimit = 100;
+    GoogleStyle.NamespaceIndentation = FormatStyle::NI_All;
   }
 
   return GoogleStyle;

diff  --git a/clang/unittests/Format/FormatTestCSharp.cpp b/clang/unittests/Format/FormatTestCSharp.cpp
index 5567e19e5bdd..dd2ed292ccd8 100644
--- a/clang/unittests/Format/FormatTestCSharp.cpp
+++ b/clang/unittests/Format/FormatTestCSharp.cpp
@@ -583,8 +583,7 @@ TEST_F(FormatTestCSharp, CSharpNamedArguments) {
   FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
 
   verifyFormat(R"(//
-PrintOrderDetails(orderNum: 31, productName: "Red Mug",
-                  sellerName: "Gift Shop");)",
+PrintOrderDetails(orderNum: 31, productName: "Red Mug", sellerName: "Gift Shop");)",
                Style);
 
   // Ensure that trailing comments do not cause problems.
@@ -641,8 +640,7 @@ class TimePeriod {
     get { return _seconds / 3600; }
     set {
       if (value < 0 || value > 24)
-        throw new ArgumentOutOfRangeException(
-            $"{nameof(value)} must be between 0 and 24.");
+        throw new ArgumentOutOfRangeException($"{nameof(value)} must be between 0 and 24.");
       _seconds = value * 3600;
     }
   }
@@ -755,7 +753,9 @@ TEST_F(FormatTestCSharp, CSharpNullableTypes) {
 
   verifyFormat(R"(//
 public class A {
-  void foo() { int? value = some.bar(); }
+  void foo() {
+    int? value = some.bar();
+  }
 })",
                Style); // int? is nullable not a conditional expression.
 
@@ -800,16 +800,15 @@ class Dictionary<TKey, TVal>
     where TKey : IComparable<TKey>
     where TVal : IMyInterface {
   public void MyMethod<T>(T t)
-      where T : IMyInterface { doThing(); }
+      where T : IMyInterface {
+    doThing();
+  }
 })",
                Style);
 
   verifyFormat(R"(//
 class ItemFactory<T>
-    where T : new(),
-              IAnInterface<T>,
-              IAnotherInterface<T>,
-              IAnotherInterfaceStill<T> {})",
+    where T : new(), IAnInterface<T>, IAnotherInterface<T>, IAnotherInterfaceStill<T> {})",
                Style);
 
   Style.ColumnLimit = 50; // Force lines to be wrapped.


        


More information about the cfe-commits mailing list