Skip to content

Commit ae07f48

Browse files
authored
[clang-format] Correctly handle C# new modifier (#137430)
Fix #75815
1 parent 165acd3 commit ae07f48

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -2089,7 +2089,13 @@ void UnwrappedLineParser::parseStructuralElement(
20892089
parseSquare();
20902090
break;
20912091
case tok::kw_new:
2092-
parseNew();
2092+
if (Style.isCSharp() &&
2093+
(Tokens->peekNextToken()->isAccessSpecifierKeyword() ||
2094+
(Previous && Previous->isAccessSpecifierKeyword()))) {
2095+
nextToken();
2096+
} else {
2097+
parseNew();
2098+
}
20932099
break;
20942100
case tok::kw_switch:
20952101
if (Style.isJava())

clang/unittests/Format/FormatTestCSharp.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,13 @@ TEST_F(FormatTestCSharp, CSharpNewOperator) {
689689
Style);
690690
}
691691

692+
TEST_F(FormatTestCSharp, NewModifier) {
693+
verifyFormat("public new class NestedC {\n"
694+
" public int x = 100;\n"
695+
"}",
696+
getLLVMStyle(FormatStyle::LK_CSharp));
697+
}
698+
692699
TEST_F(FormatTestCSharp, CSharpLambdas) {
693700
FormatStyle GoogleStyle = getGoogleStyle(FormatStyle::LK_CSharp);
694701
FormatStyle MicrosoftStyle = getMicrosoftStyle(FormatStyle::LK_CSharp);

clang/unittests/Format/TokenAnnotatorTest.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -3109,6 +3109,17 @@ TEST_F(TokenAnnotatorTest, CSharpGenericTypeConstraint) {
31093109
EXPECT_TOKEN(Tokens[18], tok::r_brace, TT_NamespaceRBrace);
31103110
}
31113111

3112+
TEST_F(TokenAnnotatorTest, CSharpNewModifier) {
3113+
auto Tokens = annotate("new public class NestedC {\n"
3114+
" public int x = 100;\n"
3115+
"}",
3116+
getGoogleStyle(FormatStyle::LK_CSharp));
3117+
ASSERT_EQ(Tokens.size(), 13u) << Tokens;
3118+
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_ClassHeadName);
3119+
EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_ClassLBrace);
3120+
EXPECT_TOKEN(Tokens[11], tok::r_brace, TT_ClassRBrace);
3121+
}
3122+
31123123
TEST_F(TokenAnnotatorTest, UnderstandsLabels) {
31133124
auto Tokens = annotate("{ x: break; }");
31143125
ASSERT_EQ(Tokens.size(), 7u) << Tokens;

0 commit comments

Comments
 (0)