[clang] b46f925 - [clang-format] Improve handling of C# attributes
Jonathan Coe via cfe-commits
cfe-commits at lists.llvm.org
Tue Feb 11 04:03:17 PST 2020
Author: Jonathan Coe
Date: 2020-02-11T12:00:17Z
New Revision: b46f925d68d480246e1447f26459bdc894631a88
URL: https://github.com/llvm/llvm-project/commit/b46f925d68d480246e1447f26459bdc894631a88
DIFF: https://github.com/llvm/llvm-project/commit/b46f925d68d480246e1447f26459bdc894631a88.diff
LOG: [clang-format] Improve handling of C# attributes
Summary:
C# attributes can appear on classes and methods, in which case they should go on their own line, or on method parameters in which case
they should be left inline.
Reviewers: krasimir, MyDeveloperDay
Reviewed By: MyDeveloperDay
Subscribers: klimek
Tags: #clang-format
Differential Revision: https://reviews.llvm.org/D74265
Added:
Modified:
clang/lib/Format/TokenAnnotator.cpp
clang/lib/Format/UnwrappedLineParser.cpp
clang/lib/Format/UnwrappedLineParser.h
clang/unittests/Format/FormatTestCSharp.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 7538b03f4f7c..1c15859e5786 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3280,13 +3280,6 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
return true;
}
- // Put multiple C# attributes on a new line.
- if (Style.isCSharp() &&
- ((Left.is(TT_AttributeSquare) && Left.is(tok::r_square)) ||
- (Left.is(tok::r_square) && Right.is(TT_AttributeSquare) &&
- Right.is(tok::l_square))))
- return true;
-
// Put multiple Java annotation on a new line.
if ((Style.Language == FormatStyle::LK_Java ||
Style.Language == FormatStyle::LK_JavaScript) &&
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 321a7405e24a..b455a7f2ebed 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -323,6 +323,20 @@ void UnwrappedLineParser::parseFile() {
addUnwrappedLine();
}
+void UnwrappedLineParser::parseCSharpAttribute() {
+ do {
+ switch (FormatTok->Tok.getKind()) {
+ case tok::r_square:
+ nextToken();
+ addUnwrappedLine();
+ return;
+ default:
+ nextToken();
+ break;
+ }
+ } while (!eof());
+}
+
void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
bool SwitchLabelEncountered = false;
do {
@@ -381,6 +395,13 @@ void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
SwitchLabelEncountered = true;
parseStructuralElement();
break;
+ case tok::l_square:
+ if (Style.isCSharp()) {
+ nextToken();
+ parseCSharpAttribute();
+ break;
+ }
+ LLVM_FALLTHROUGH;
default:
parseStructuralElement();
break;
diff --git a/clang/lib/Format/UnwrappedLineParser.h b/clang/lib/Format/UnwrappedLineParser.h
index 5d9bafc429a7..e184cf5354fd 100644
--- a/clang/lib/Format/UnwrappedLineParser.h
+++ b/clang/lib/Format/UnwrappedLineParser.h
@@ -125,6 +125,7 @@ class UnwrappedLineParser {
bool parseObjCProtocol();
void parseJavaScriptEs6ImportExport();
void parseStatementMacro();
+ void parseCSharpAttribute();
bool tryToParseLambda();
bool tryToParseLambdaIntroducer();
void tryToParseJSFunction();
diff --git a/clang/unittests/Format/FormatTestCSharp.cpp b/clang/unittests/Format/FormatTestCSharp.cpp
index 918f8aed83e9..e859aeb0d22d 100644
--- a/clang/unittests/Format/FormatTestCSharp.cpp
+++ b/clang/unittests/Format/FormatTestCSharp.cpp
@@ -258,6 +258,21 @@ TEST_F(FormatTestCSharp, Attributes) {
"// The const char* returned by hello_world must not be deleted.\n"
"private static extern IntPtr HelloFromCpp();)");
+ // Class attributes go on their own line and do not affect layout of
+ // interfaces. Line wrapping decisions previously caused each interface to be
+ // on its own line.
+ verifyFormat("[SomeAttribute]\n"
+ "[SomeOtherAttribute]\n"
+ "public class A : IShape, IAnimal, IVehicle\n"
+ "{\n"
+ " int X;\n"
+ "}");
+
+ // Attributes in a method declaration do not cause line wrapping.
+ verifyFormat("void MethodA([In][Out] ref double x)\n"
+ "{\n"
+ "}");
+
// Unwrappable lines go on a line of their own.
// 'target:' is not treated as a label.
// Modify Style to enforce a column limit.
More information about the cfe-commits
mailing list