Prefer not to break after assignments.
This addresses llvm.org/PR14830.
Before:
unsigned Cost =
TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),
SI->getPointerAddressSpace());
CharSourceRange LineRange =
CharSourceRange::getTokenRange(TheLine.Tokens.front().Tok.getLocation(),
TheLine.Tokens.back().Tok.getLocation());
After:
unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,
SI->getAlignment(),
SI->getPointerAddressSpace());
CharSourceRange LineRange = CharSourceRange::getTokenRange(
TheLine.Tokens.front().Tok.getLocation(),
TheLine.Tokens.back().Tok.getLocation());
This required rudimentary changes to static initializer lists, but we
are not yet formatting them in a reasonable way. That will be done in a
subsequent patch.
llvm-svn: 171731
This commit is contained in:
parent
a055aab506
commit
206df73417
|
|
@ -257,7 +257,12 @@ private:
|
||||||
|
|
||||||
if (Newline) {
|
if (Newline) {
|
||||||
unsigned WhitespaceStartColumn = State.Column;
|
unsigned WhitespaceStartColumn = State.Column;
|
||||||
if (Current.Tok.is(tok::string_literal) &&
|
if (Previous.Tok.is(tok::l_brace)) {
|
||||||
|
// FIXME: This does not work with nested static initializers.
|
||||||
|
// Implement a better handling for static initializers and similar
|
||||||
|
// constructs.
|
||||||
|
State.Column = Line.Level * 2 + 2;
|
||||||
|
} else if (Current.Tok.is(tok::string_literal) &&
|
||||||
Previous.Tok.is(tok::string_literal)) {
|
Previous.Tok.is(tok::string_literal)) {
|
||||||
State.Column = State.Column - Previous.TokenLength;
|
State.Column = State.Column - Previous.TokenLength;
|
||||||
} else if (Current.Tok.is(tok::lessless) &&
|
} else if (Current.Tok.is(tok::lessless) &&
|
||||||
|
|
@ -306,17 +311,19 @@ private:
|
||||||
if (!DryRun)
|
if (!DryRun)
|
||||||
replaceWhitespace(Current, 0, Spaces);
|
replaceWhitespace(Current, 0, Spaces);
|
||||||
|
|
||||||
// FIXME: Look into using this alignment at other ParenLevels.
|
if (Line.Tokens[0].Tok.isNot(tok::kw_for) &&
|
||||||
if (ParenLevel == 0 && (getPrecedence(Previous) == prec::Assignment ||
|
(getPrecedence(Previous) == prec::Assignment ||
|
||||||
Previous.Tok.is(tok::kw_return)))
|
Previous.Tok.is(tok::kw_return)))
|
||||||
State.Indent[ParenLevel] = State.Column + Spaces;
|
State.Indent[ParenLevel] = State.Column + Spaces;
|
||||||
if (Previous.Tok.is(tok::l_paren) ||
|
if (Previous.Tok.is(tok::l_paren) ||
|
||||||
Annotations[Index - 1].Type == TT_TemplateOpener)
|
Annotations[Index - 1].Type == TT_TemplateOpener)
|
||||||
State.Indent[ParenLevel] = State.Column;
|
State.Indent[ParenLevel] = State.Column;
|
||||||
|
|
||||||
// Top-level spaces are exempt as that mostly leads to better results.
|
// Top-level spaces that are not part of assignments are exempt as that
|
||||||
|
// mostly leads to better results.
|
||||||
State.Column += Spaces;
|
State.Column += Spaces;
|
||||||
if (Spaces > 0 && ParenLevel != 0)
|
if (Spaces > 0 &&
|
||||||
|
(ParenLevel != 0 || getPrecedence(Previous) == prec::Assignment))
|
||||||
State.LastSpace[ParenLevel] = State.Column;
|
State.LastSpace[ParenLevel] = State.Column;
|
||||||
}
|
}
|
||||||
moveStateToNextToken(State);
|
moveStateToNextToken(State);
|
||||||
|
|
@ -375,7 +382,13 @@ private:
|
||||||
if (Left.Tok.is(tok::l_paren))
|
if (Left.Tok.is(tok::l_paren))
|
||||||
return 20;
|
return 20;
|
||||||
|
|
||||||
prec::Level Level = getPrecedence(Line.Tokens[Index]);
|
prec::Level Level = getPrecedence(Left);
|
||||||
|
|
||||||
|
// Breaking after an assignment leads to a bad result as the two sides of
|
||||||
|
// the assignment are visually very close together.
|
||||||
|
if (Level == prec::Assignment)
|
||||||
|
return 50;
|
||||||
|
|
||||||
if (Level != prec::Unknown)
|
if (Level != prec::Unknown)
|
||||||
return Level;
|
return Level;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -384,8 +384,8 @@ TEST_F(FormatTest, StaticInitializers) {
|
||||||
|
|
||||||
// FIXME: Format like enums if the static initializer does not fit on a line.
|
// FIXME: Format like enums if the static initializer does not fit on a line.
|
||||||
verifyFormat(
|
verifyFormat(
|
||||||
"static SomeClass WithALoooooooooooooooooooongName = { 100000000,\n"
|
"static SomeClass WithALoooooooooooooooooooongName = {\n"
|
||||||
" \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" };");
|
" 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" };");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
|
TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
|
||||||
|
|
@ -641,6 +641,17 @@ TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
|
||||||
" ccccccccccccccccccccccccc) {\n}");
|
" ccccccccccccccccccccccccc) {\n}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
|
||||||
|
verifyFormat(
|
||||||
|
"unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
|
||||||
|
" SI->getAlignment(),\n"
|
||||||
|
" SI->getPointerAddressSpaceee());\n");
|
||||||
|
verifyFormat(
|
||||||
|
"CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
|
||||||
|
" Line.Tokens.front().Tok.getLocation(),\n"
|
||||||
|
" Line.Tokens.back().Tok.getLocation());");
|
||||||
|
}
|
||||||
|
|
||||||
TEST_F(FormatTest, AlignsAfterAssignments) {
|
TEST_F(FormatTest, AlignsAfterAssignments) {
|
||||||
verifyFormat(
|
verifyFormat(
|
||||||
"int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
"int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
||||||
|
|
@ -655,9 +666,9 @@ TEST_F(FormatTest, AlignsAfterAssignments) {
|
||||||
"int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
"int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
||||||
" aaaaaaaaaaaaaaaaaaaaaaaaa);");
|
" aaaaaaaaaaaaaaaaaaaaaaaaa);");
|
||||||
verifyFormat(
|
verifyFormat(
|
||||||
"double LooooooooooooooooooooooooongResult =\n"
|
"double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
||||||
" aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
" aaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
||||||
" aaaaaaaaaaaaaaaaaaaaaaaaa;");
|
" aaaaaaaaaaaaaaaaaaaaaaaa;");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(FormatTest, AlignsAfterReturn) {
|
TEST_F(FormatTest, AlignsAfterReturn) {
|
||||||
|
|
@ -713,8 +724,10 @@ TEST_F(FormatTest, UnderstandsEquals) {
|
||||||
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
|
||||||
"}");
|
"}");
|
||||||
|
|
||||||
verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
|
verifyFormat(
|
||||||
" 100000000 + 100000000) {\n}");
|
"if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
|
||||||
|
" 10000000) {\n"
|
||||||
|
"}");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
|
TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue