r232044 - clang-format: When putting */& next to types, also wrap before them.
Daniel Jasper
djasper at google.com
Thu Mar 12 08:04:53 PDT 2015
Author: djasper
Date: Thu Mar 12 10:04:53 2015
New Revision: 232044
URL: http://llvm.org/viewvc/llvm-project?rev=232044&view=rev
Log:
clang-format: When putting */& next to types, also wrap before them.
Before:
LoooooooooooongType *
loooooooooooongVariable;
After:
LoooooooooooongType
*loooooooooooongVariable;
Modified:
cfe/trunk/lib/Format/ContinuationIndenter.cpp
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=232044&r1=232043&r2=232044&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Thu Mar 12 10:04:53 2015
@@ -580,7 +580,7 @@ unsigned ContinuationIndenter::getNewLin
return State.Stack.back().StartOfArraySubscripts;
return ContinuationIndent;
}
- if (NextNonComment->is(TT_StartOfName) ||
+ if (NextNonComment->isOneOf(TT_StartOfName, TT_PointerOrReference) ||
Previous.isOneOf(tok::coloncolon, tok::equal)) {
return ContinuationIndent;
}
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=232044&r1=232043&r2=232044&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Thu Mar 12 10:04:53 2015
@@ -1537,6 +1537,8 @@ unsigned TokenAnnotator::splitPenalty(co
return Style.PenaltyReturnTypeOnItsOwnLine;
return 200;
}
+ if (Right.is(TT_PointerOrReference))
+ return 200;
if (Right.is(TT_TrailingReturnArrow))
return 110;
if (Left.is(tok::equal) && Right.is(tok::l_brace))
@@ -1980,6 +1982,14 @@ bool TokenAnnotator::canBreakBefore(cons
return false;
if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
return !Right.is(tok::l_paren);
+ if (Left.is(TT_PointerOrReference))
+ return (!Line.IsMultiVariableDeclStmt &&
+ Style.PointerAlignment != FormatStyle::PAS_Right) ||
+ Right.is(TT_FunctionDeclarationName);
+ if (Right.is(TT_PointerOrReference))
+ return Line.IsMultiVariableDeclStmt ||
+ (Style.PointerAlignment == FormatStyle::PAS_Right &&
+ (!Right.Next || Right.Next->isNot(TT_FunctionDeclarationName)));
if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
Right.is(tok::kw_operator))
return true;
@@ -2016,10 +2026,7 @@ bool TokenAnnotator::canBreakBefore(cons
return true;
if (Right.is(TT_RangeBasedForLoopColon))
return false;
- if (Right.is(TT_PointerOrReference) && Line.IsMultiVariableDeclStmt)
- return true;
- if (Left.isOneOf(TT_PointerOrReference, TT_TemplateCloser,
- TT_UnaryOperator) ||
+ if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) ||
Left.is(tok::kw_operator))
return false;
if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl)
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=232044&r1=232043&r2=232044&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Mar 12 10:04:53 2015
@@ -5648,6 +5648,8 @@ TEST_F(FormatTest, BreaksLongVariableDec
" LoooooooooooooooooooooooooooooooooooooooongVariable;");
verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
" LoooooooooooooooooooooooooooooooooooooooongVariable;");
+ verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
+ " *LoooooooooooooooooooooooooooooooooooooooongVariable;");
// Different ways of ()-initializiation.
verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
@@ -5665,6 +5667,8 @@ TEST_F(FormatTest, BreaksLongDeclaration
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
"LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
+ verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
+ "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
"LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
@@ -5703,12 +5707,12 @@ TEST_F(FormatTest, BreaksLongDeclaration
" SourceLocation L, IdentifierIn *II,\n"
" Type *T) {}");
verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
- "ReallyReallyLongFunctionName(\n"
+ "ReallyReaaallyLongFunctionName(\n"
" const std::string &SomeParameter,\n"
- " const SomeType<string, SomeOtherTemplateParameter> &\n"
- " ReallyReallyLongParameterName,\n"
- " const SomeType<string, SomeOtherTemplateParameter> &\n"
- " AnotherLongParameterName) {}");
+ " const SomeType<string, SomeOtherTemplateParameter>\n"
+ " &ReallyReallyLongParameterName,\n"
+ " const SomeType<string, SomeOtherTemplateParameter>\n"
+ " &AnotherLongParameterName) {}");
verifyFormat("template <typename A>\n"
"SomeLoooooooooooooooooooooongType<\n"
" typename some_namespace::SomeOtherType<A>::Type>\n"
@@ -5732,8 +5736,8 @@ TEST_F(FormatTest, BreaksLongDeclaration
" int aaaaaaaaaaaaaaaaaaaaaaa);");
verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
- " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
+ " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
}
TEST_F(FormatTest, FormatsArrays) {
More information about the cfe-commits
mailing list