[clang] 258d7b8 - [clang-format] Handle constructor invocations after new operator in C# correct

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 25 21:10:36 PDT 2022


Author: Danil Sidoruk
Date: 2022-09-25T21:10:26-07:00
New Revision: 258d7b86eeab4222c783c1a19acd8e18cd7745aa

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

LOG: [clang-format] Handle constructor invocations after new operator in C# correct

Fixes #56549.

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

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 3919bc0ef0b35..a5dbd43cdf264 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -2975,6 +2975,11 @@ void UnwrappedLineParser::parseNew() {
 
   if (Style.isCSharp()) {
     do {
+      // Handle constructor invocation, e.g. `new(field: value)`.
+      if (FormatTok->is(tok::l_paren))
+        parseParens();
+
+      // Handle array initialization syntax, e.g. `new[] {10, 20, 30}`.
       if (FormatTok->is(tok::l_brace))
         parseBracedList();
 

diff  --git a/clang/unittests/Format/FormatTestCSharp.cpp b/clang/unittests/Format/FormatTestCSharp.cpp
index 680a9576cf5a3..47ad779d632a9 100644
--- a/clang/unittests/Format/FormatTestCSharp.cpp
+++ b/clang/unittests/Format/FormatTestCSharp.cpp
@@ -617,6 +617,24 @@ var x = foo(className, $@"some code:
   EXPECT_EQ(Code, format(Code, Style));
 }
 
+TEST_F(FormatTestCSharp, CSharpNewOperator) {
+  FormatStyle Style = getLLVMStyle(FormatStyle::LK_CSharp);
+
+  verifyFormat("public void F() {\n"
+               "  var v = new C(() => { var t = 5; });\n"
+               "}",
+               Style);
+  verifyFormat("public void F() {\n"
+               "  var v = new C(() => {\n"
+               "    try {\n"
+               "    } catch {\n"
+               "      var t = 5;\n"
+               "    }\n"
+               "  });\n"
+               "}",
+               Style);
+}
+
 TEST_F(FormatTestCSharp, CSharpLambdas) {
   FormatStyle GoogleStyle = getGoogleStyle(FormatStyle::LK_CSharp);
   FormatStyle MicrosoftStyle = getMicrosoftStyle(FormatStyle::LK_CSharp);


        


More information about the cfe-commits mailing list