Prefer splitting after "template <...>" and fix indentation.
This addresses llvm.org/PR14699
Before:
template <typename T>
void looooooooooooooooooooooongFunction(int Param1, int Param2);
template <typename T> void looooooooooooooooooooongFunction(
int Paaaaaaaaaaaaaaaaaaaaram1, int Paaaaaaaaaaaaaaaaaaaaram2);
After:
template <typename T>
void looooooooooooooooooooooongFunction(int Param1, int Param2);
template <typename T>
void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,
int Paaaaaaaaaaaaaaaaaaaaram2);
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@171388 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index 8f77c77..d354078 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -50,6 +50,8 @@
bool SpaceRequiredBefore;
bool CanBreakBefore;
bool MustBreakBefore;
+
+ bool ClosesTemplateDeclaration;
};
static prec::Level getPrecedence(const FormatToken &Tok) {
@@ -250,6 +252,8 @@
} else if (
Line.Tokens[0].Tok.is(tok::kw_for) && Previous.Tok.is(tok::comma)) {
State.Column = State.ForLoopVariablePos;
+ } else if (Annotations[Index - 1].ClosesTemplateDeclaration) {
+ State.Column = State.Indent[ParenLevel] - 4;
} else {
State.Column = State.Indent[ParenLevel];
}
@@ -342,7 +346,8 @@
(Left.Tok.isNot(tok::comma) && Left.Tok.isNot(tok::semi)))
return 20;
- if (Left.Tok.is(tok::semi) || Left.Tok.is(tok::comma))
+ if (Left.Tok.is(tok::semi) || Left.Tok.is(tok::comma) ||
+ Annotations[Index].ClosesTemplateDeclaration)
return 0;
if (Left.Tok.is(tok::l_paren))
return 20;
@@ -549,6 +554,19 @@
return false;
}
+ bool parseTemplateDeclaration() {
+ if (Index < Tokens.size() && Tokens[Index].Tok.is(tok::less)) {
+ Annotations[Index].Type = TokenAnnotation::TT_TemplateOpener;
+ next();
+ if (!parseAngle())
+ return false;
+ Annotations[Index - 1].ClosesTemplateDeclaration = true;
+ parseLine();
+ return true;
+ }
+ return false;
+ }
+
void consumeToken() {
unsigned CurrentIndex = Index;
next();
@@ -592,6 +610,9 @@
case tok::question:
parseConditional();
break;
+ case tok::kw_template:
+ parseTemplateDeclaration();
+ break;
default:
break;
}
diff --git a/test/Index/comment-to-html-xml-conversion.cpp b/test/Index/comment-to-html-xml-conversion.cpp
index cf620a7..0af0dd7 100644
--- a/test/Index/comment-to-html-xml-conversion.cpp
+++ b/test/Index/comment-to-html-xml-conversion.cpp
@@ -670,7 +670,7 @@
template<typename T, typename U>
class comment_to_xml_conversion_11 { };
-// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-2]]:7: ClassTemplate=comment_to_xml_conversion_11:{{.*}} FullCommentAsXML=[<Class templateKind="template" file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-2]]" column="7"><Name>comment_to_xml_conversion_11</Name><USR>c:@CT>2#T#T@comment_to_xml_conversion_11</USR><Declaration>template <typename T = int, typename U = int>\n class comment_to_xml_conversion_11 {\n}\ntemplate <typename T, typename U> class comment_to_xml_conversion_11 {\n}</Declaration><Abstract><Para> Aaa.</Para></Abstract></Class>]
+// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-2]]:7: ClassTemplate=comment_to_xml_conversion_11:{{.*}} FullCommentAsXML=[<Class templateKind="template" file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-2]]" column="7"><Name>comment_to_xml_conversion_11</Name><USR>c:@CT>2#T#T@comment_to_xml_conversion_11</USR><Declaration>template <typename T = int, typename U = int>\nclass comment_to_xml_conversion_11 {\n}\ntemplate <typename T, typename U> class comment_to_xml_conversion_11 {\n}</Declaration><Abstract><Para> Aaa.</Para></Abstract></Class>]
/// Aaa.
template<typename T>
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index bf35ce1..58712ef 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -594,6 +594,19 @@
"}");
}
+TEST_F(FormatTest, WrapsTemplateDeclarations) {
+ verifyFormat("template <typename T>\n"
+ "virtual void loooooooooooongFunction(int Param1, int Param2);");
+ verifyFormat(
+ "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
+ " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
+ verifyFormat(
+ "template <typename T>\n"
+ "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
+ " int Paaaaaaaaaaaaaaaaaaaaram2);");
+
+}
+
TEST_F(FormatTest, UnderstandsTemplateParameters) {
verifyFormat("A<int> a;");
verifyFormat("A<A<A<int> > > a;");