[clang] ebed0ca - [clang-format] C# switch expression formatting differs from normal switch formatting

via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 15 11:48:08 PST 2021


Author: mydeveloperday
Date: 2021-12-15T19:47:29Z
New Revision: ebed0ca71561f5ca5d54b1f5ddf783897fea002e

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

LOG: [clang-format] C# switch expression formatting differs from normal switch formatting

https://github.com/llvm/llvm-project/issues/52677

clang-format doesn't format C# switch expressions very well.

Start with this small use case and try and improve the output. I'll look for other examples to add as tests

Reviewed By: curdeius

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

Fixes  #52677

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 c0d030379ff0a..9dfe1d3e18f4b 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3716,6 +3716,10 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
       return false;
     if (Right.is(TT_CSharpGenericTypeConstraint))
       return true;
+    if (Right.Next && Right.Next->is(TT_FatArrow) &&
+        (Right.is(tok::numeric_constant) ||
+         (Right.is(tok::identifier) && Right.TokenText == "_")))
+      return true;
 
     // Break after C# [...] and before public/protected/private/internal.
     if (Left.is(TT_AttributeSquare) && Left.is(tok::r_square) &&

diff  --git a/clang/unittests/Format/FormatTestCSharp.cpp b/clang/unittests/Format/FormatTestCSharp.cpp
index bb91cd6cf2b06..0536903ef4f10 100644
--- a/clang/unittests/Format/FormatTestCSharp.cpp
+++ b/clang/unittests/Format/FormatTestCSharp.cpp
@@ -1369,5 +1369,15 @@ TEST_F(FormatTestCSharp, NamespaceIndentation) {
                Style);
 }
 
+TEST_F(FormatTestCSharp, SwitchExpression) {
+  FormatStyle Style = getMicrosoftStyle(FormatStyle::LK_CSharp);
+  verifyFormat("int x = a switch {\n"
+               "    1 => (0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0),\n"
+               "    2 => 1,\n"
+               "    _ => 2\n"
+               "};\n",
+               Style);
+}
+
 } // namespace format
 } // end namespace clang


        


More information about the cfe-commits mailing list