This revision was automatically updated to reflect the committed changes.
Closed by commit rGca1fd460f1f5: [clang-format] Do not treat C# attribute
targets as labels (authored by Jonathan Coe <[email protected]>).
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D74043/new/
https://reviews.llvm.org/D74043
Files:
clang/lib/Format/FormatTokenLexer.cpp
clang/lib/Format/FormatTokenLexer.h
clang/unittests/Format/FormatTestCSharp.cpp
Index: clang/unittests/Format/FormatTestCSharp.cpp
===================================================================
--- clang/unittests/Format/FormatTestCSharp.cpp
+++ clang/unittests/Format/FormatTestCSharp.cpp
@@ -233,6 +233,15 @@
"[DllImport(\"Hello\", EntryPoint = \"hello_world\")]\n"
"// The const char* returned by hello_world must not be deleted.\n"
"private static extern IntPtr HelloFromCpp();)");
+
+ // Unwrappable lines go on a line of their own.
+ // 'target:' is not treated as a label.
+ // Modify Style to enforce a column limit.
+ FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
+ Style.ColumnLimit = 10;
+ verifyFormat(R"([assembly:InternalsVisibleTo(
+ "SomeAssembly, PublicKey=SomePublicKeyThatExceedsTheColumnLimit")])",
+ Style);
}
TEST_F(FormatTestCSharp, CSharpUsing) {
Index: clang/lib/Format/FormatTokenLexer.h
===================================================================
--- clang/lib/Format/FormatTokenLexer.h
+++ clang/lib/Format/FormatTokenLexer.h
@@ -21,6 +21,7 @@
#include "clang/Basic/SourceManager.h"
#include "clang/Format/Format.h"
#include "llvm/ADT/MapVector.h"
+#include "llvm/ADT/StringSet.h"
#include "llvm/Support/Regex.h"
#include <stack>
@@ -54,6 +55,7 @@
bool tryMergeCSharpNullConditionals();
bool tryMergeCSharpDoubleQuestion();
bool tryTransformCSharpForEach();
+ bool tryMergeCSharpAttributeAndTarget();
bool tryMergeTokens(ArrayRef<tok::TokenKind> Kinds, TokenType NewType);
@@ -115,6 +117,9 @@
llvm::Regex MacroBlockBeginRegex;
llvm::Regex MacroBlockEndRegex;
+ // Targets that may appear inside a C# attribute.
+ static const llvm::StringSet<> CSharpAttributeTargets;
+
void readRawToken(FormatToken &Tok);
void resetLexer(unsigned Offset);
Index: clang/lib/Format/FormatTokenLexer.cpp
===================================================================
--- clang/lib/Format/FormatTokenLexer.cpp
+++ clang/lib/Format/FormatTokenLexer.cpp
@@ -76,6 +76,8 @@
return;
if (Style.isCSharp()) {
+ if (tryMergeCSharpAttributeAndTarget())
+ return;
if (tryMergeCSharpKeywordVariables())
return;
if (tryMergeCSharpStringLiteral())
@@ -275,6 +277,41 @@
return true;
}
+// Valid C# attribute targets:
+// https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/attributes/#attribute-targets
+const llvm::StringSet<> FormatTokenLexer::CSharpAttributeTargets = {
+ "assembly", "module", "field", "event", "method",
+ "param", "property", "return", "type",
+};
+
+bool FormatTokenLexer::tryMergeCSharpAttributeAndTarget() {
+ // Treat '[assembly:' and '[field:' as tokens in their own right.
+ if (Tokens.size() < 3)
+ return false;
+
+ auto &SquareBracket = *(Tokens.end() - 3);
+ auto &Target = *(Tokens.end() - 2);
+ auto &Colon = *(Tokens.end() - 1);
+
+ if (!SquareBracket->Tok.is(tok::l_square))
+ return false;
+
+ if (CSharpAttributeTargets.find(Target->TokenText) ==
+ CSharpAttributeTargets.end())
+ return false;
+
+ if (!Colon->Tok.is(tok::colon))
+ return false;
+
+ SquareBracket->TokenText =
+ StringRef(SquareBracket->TokenText.begin(),
+ Colon->TokenText.end() - SquareBracket->TokenText.begin());
+ SquareBracket->ColumnWidth += (Target->ColumnWidth + Colon->ColumnWidth);
+ Tokens.erase(Tokens.end() - 2);
+ Tokens.erase(Tokens.end() - 1);
+ return true;
+}
+
bool FormatTokenLexer::tryMergeCSharpDoubleQuestion() {
if (Tokens.size() < 2)
return false;
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits