https://github.com/owenca created https://github.com/llvm/llvm-project/pull/137295
Fix #74947 >From 6c376532edb6d2ae28094bc8c0eb88970b65fc66 Mon Sep 17 00:00:00 2001 From: Owen Pan <owenpi...@gmail.com> Date: Fri, 25 Apr 2025 01:25:32 -0700 Subject: [PATCH] [clang-format] Correctly handle C# where clause Fix #74947 --- clang/lib/Format/UnwrappedLineParser.cpp | 1 + clang/unittests/Format/FormatTestCSharp.cpp | 6 ++++++ clang/unittests/Format/TokenAnnotatorTest.cpp | 10 ++++++++++ 3 files changed, 17 insertions(+) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 565dcfce4ec18..9e86d9b19afb8 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -295,6 +295,7 @@ void UnwrappedLineParser::parseCSharpGenericTypeConstraint() { do { switch (FormatTok->Tok.getKind()) { case tok::l_brace: + case tok::semi: return; default: if (FormatTok->is(Keywords.kw_where)) { diff --git a/clang/unittests/Format/FormatTestCSharp.cpp b/clang/unittests/Format/FormatTestCSharp.cpp index 151f7072e0c65..dae362c37b52b 100644 --- a/clang/unittests/Format/FormatTestCSharp.cpp +++ b/clang/unittests/Format/FormatTestCSharp.cpp @@ -1315,6 +1315,12 @@ TEST_F(FormatTestCSharp, CSharpGenericTypeConstraints) { "}", Style); + verifyFormat("namespace A {\n" + " delegate T MyDelegate<T>()\n" + " where T : new();\n" + "}", + Style); + // When the "where" line is not to be formatted, following lines should not // take on its indentation. verifyFormat("class ItemFactory<T>\n" diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 87b2f329d57cf..8d4aeb7dec89a 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -3099,6 +3099,16 @@ TEST_F(TokenAnnotatorTest, CSharpNullableTypes) { EXPECT_TOKEN(Tokens[1], tok::question, TT_ConditionalExpr); } +TEST_F(TokenAnnotatorTest, CSharpGenericTypeConstraint) { + auto Tokens = annotate("namespace A {\n" + " delegate T MyDelegate<T>()\n" + " where T : new();\n" + "}", + getGoogleStyle(FormatStyle::LK_CSharp)); + ASSERT_EQ(Tokens.size(), 20u) << Tokens; + EXPECT_TOKEN(Tokens[18], tok::r_brace, TT_NamespaceRBrace); +} + TEST_F(TokenAnnotatorTest, UnderstandsLabels) { auto Tokens = annotate("{ x: break; }"); ASSERT_EQ(Tokens.size(), 7u) << Tokens; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits