Skip to content

Commit d990cc4

Browse files
authored
[clang-format] Correctly handle C# attribute on auto property (#102921)
Fixes #101487.
1 parent 7951673 commit d990cc4

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2155,12 +2155,16 @@ bool UnwrappedLineParser::tryToParsePropertyAccessor() {
21552155
// Track these as they do not require line breaks to be introduced.
21562156
bool HasSpecialAccessor = false;
21572157
bool IsTrivialPropertyAccessor = true;
2158+
bool HasAttribute = false;
21582159
while (!eof()) {
2159-
if (Tok->isAccessSpecifierKeyword() ||
2160-
Tok->isOneOf(tok::semi, Keywords.kw_internal, Keywords.kw_get,
2161-
Keywords.kw_init, Keywords.kw_set)) {
2162-
if (Tok->isOneOf(Keywords.kw_get, Keywords.kw_init, Keywords.kw_set))
2160+
if (const bool IsAccessorKeyword =
2161+
Tok->isOneOf(Keywords.kw_get, Keywords.kw_init, Keywords.kw_set);
2162+
IsAccessorKeyword || Tok->isAccessSpecifierKeyword() ||
2163+
Tok->isOneOf(tok::l_square, tok::semi, Keywords.kw_internal)) {
2164+
if (IsAccessorKeyword)
21632165
HasSpecialAccessor = true;
2166+
else if (Tok->is(tok::l_square))
2167+
HasAttribute = true;
21642168
Tok = Tokens->getNextToken();
21652169
continue;
21662170
}
@@ -2169,7 +2173,7 @@ bool UnwrappedLineParser::tryToParsePropertyAccessor() {
21692173
break;
21702174
}
21712175

2172-
if (!HasSpecialAccessor) {
2176+
if (!HasSpecialAccessor || HasAttribute) {
21732177
Tokens->setPosition(StoredPosition);
21742178
return false;
21752179
}

clang/unittests/Format/FormatTestCSharp.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,17 @@ public class SaleItem
11491149
public decimal Price { get; set; }
11501150
})",
11511151
MicrosoftStyle);
1152+
1153+
verifyFormat("internal class Program\n"
1154+
"{\n"
1155+
" bool AutoAllowKnownApps\n"
1156+
" {\n"
1157+
" get;\n"
1158+
" [Simple]\n"
1159+
" set;\n"
1160+
" }\n"
1161+
"}",
1162+
MicrosoftStyle);
11521163
}
11531164

11541165
TEST_F(FormatTestCSharp, DefaultLiteral) {

0 commit comments

Comments
 (0)