https://github.com/owenca created https://github.com/llvm/llvm-project/pull/102921
Fixes #101487. >From 5fd35e3d2b9775867a259457879fc7030c901724 Mon Sep 17 00:00:00 2001 From: Owen Pan <owenpi...@gmail.com> Date: Mon, 12 Aug 2024 08:24:12 -0700 Subject: [PATCH] [clang-format] Correctly handle C# attribute on auto property Fixes #101487. --- clang/lib/Format/UnwrappedLineParser.cpp | 14 +++++++++----- clang/unittests/Format/FormatTestCSharp.cpp | 11 +++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 66485c91f64de9..cafbba0a0d0c5b 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -2155,12 +2155,16 @@ bool UnwrappedLineParser::tryToParsePropertyAccessor() { // Track these as they do not require line breaks to be introduced. bool HasSpecialAccessor = false; bool IsTrivialPropertyAccessor = true; + bool HasAttribute = false; while (!eof()) { - if (Tok->isAccessSpecifierKeyword() || - Tok->isOneOf(tok::semi, Keywords.kw_internal, Keywords.kw_get, - Keywords.kw_init, Keywords.kw_set)) { - if (Tok->isOneOf(Keywords.kw_get, Keywords.kw_init, Keywords.kw_set)) + if (const bool IsAccessorKeyword = + Tok->isOneOf(Keywords.kw_get, Keywords.kw_init, Keywords.kw_set); + IsAccessorKeyword || Tok->isAccessSpecifierKeyword() || + Tok->isOneOf(tok::l_square, tok::semi, Keywords.kw_internal)) { + if (IsAccessorKeyword) HasSpecialAccessor = true; + else if (Tok->is(tok::l_square)) + HasAttribute = true; Tok = Tokens->getNextToken(); continue; } @@ -2169,7 +2173,7 @@ bool UnwrappedLineParser::tryToParsePropertyAccessor() { break; } - if (!HasSpecialAccessor) { + if (!HasSpecialAccessor || HasAttribute) { Tokens->setPosition(StoredPosition); return false; } diff --git a/clang/unittests/Format/FormatTestCSharp.cpp b/clang/unittests/Format/FormatTestCSharp.cpp index 7166e4ec4de30d..3b04238b9b48b0 100644 --- a/clang/unittests/Format/FormatTestCSharp.cpp +++ b/clang/unittests/Format/FormatTestCSharp.cpp @@ -1149,6 +1149,17 @@ public class SaleItem public decimal Price { get; set; } })", MicrosoftStyle); + + verifyFormat("internal class Program\n" + "{\n" + " bool AutoAllowKnownApps\n" + " {\n" + " get;\n" + " [Simple]\n" + " set;\n" + " }\n" + "}", + MicrosoftStyle); } TEST_F(FormatTestCSharp, DefaultLiteral) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits