[clang] [clang-format] Correctly handle C# where clause (PR #137295)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 25 01:27:51 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Owen Pan (owenca)
<details>
<summary>Changes</summary>
Fix #<!-- -->74947
---
Full diff: https://github.com/llvm/llvm-project/pull/137295.diff
3 Files Affected:
- (modified) clang/lib/Format/UnwrappedLineParser.cpp (+1)
- (modified) clang/unittests/Format/FormatTestCSharp.cpp (+6)
- (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+10)
``````````diff
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;
``````````
</details>
https://github.com/llvm/llvm-project/pull/137295
More information about the cfe-commits
mailing list