Author: Owen Pan Date: 2025-04-26T12:54:38-07:00 New Revision: ae07f48913faa44ea050e4e0602d0aa62449d942
URL: https://github.com/llvm/llvm-project/commit/ae07f48913faa44ea050e4e0602d0aa62449d942 DIFF: https://github.com/llvm/llvm-project/commit/ae07f48913faa44ea050e4e0602d0aa62449d942.diff LOG: [clang-format] Correctly handle C# new modifier (#137430) Fix #75815 Added: Modified: clang/lib/Format/UnwrappedLineParser.cpp clang/unittests/Format/FormatTestCSharp.cpp clang/unittests/Format/TokenAnnotatorTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 3a20cd2f09c95..837146f0c6483 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -2089,7 +2089,13 @@ void UnwrappedLineParser::parseStructuralElement( parseSquare(); break; case tok::kw_new: - parseNew(); + if (Style.isCSharp() && + (Tokens->peekNextToken()->isAccessSpecifierKeyword() || + (Previous && Previous->isAccessSpecifierKeyword()))) { + nextToken(); + } else { + parseNew(); + } break; case tok::kw_switch: if (Style.isJava()) diff --git a/clang/unittests/Format/FormatTestCSharp.cpp b/clang/unittests/Format/FormatTestCSharp.cpp index dae362c37b52b..ea85ed6140dd0 100644 --- a/clang/unittests/Format/FormatTestCSharp.cpp +++ b/clang/unittests/Format/FormatTestCSharp.cpp @@ -689,6 +689,13 @@ TEST_F(FormatTestCSharp, CSharpNewOperator) { Style); } +TEST_F(FormatTestCSharp, NewModifier) { + verifyFormat("public new class NestedC {\n" + " public int x = 100;\n" + "}", + getLLVMStyle(FormatStyle::LK_CSharp)); +} + TEST_F(FormatTestCSharp, CSharpLambdas) { FormatStyle GoogleStyle = getGoogleStyle(FormatStyle::LK_CSharp); FormatStyle MicrosoftStyle = getMicrosoftStyle(FormatStyle::LK_CSharp); diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 8d4aeb7dec89a..08216dd971ac4 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -3109,6 +3109,17 @@ TEST_F(TokenAnnotatorTest, CSharpGenericTypeConstraint) { EXPECT_TOKEN(Tokens[18], tok::r_brace, TT_NamespaceRBrace); } +TEST_F(TokenAnnotatorTest, CSharpNewModifier) { + auto Tokens = annotate("new public class NestedC {\n" + " public int x = 100;\n" + "}", + getGoogleStyle(FormatStyle::LK_CSharp)); + ASSERT_EQ(Tokens.size(), 13u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::identifier, TT_ClassHeadName); + EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_ClassLBrace); + EXPECT_TOKEN(Tokens[11], tok::r_brace, TT_ClassRBrace); +} + 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