[cfe-commits] r171731 - in /cfe/trunk: lib/Format/Format.cpp unittests/Format/FormatTest.cpp
Daniel Jasper
djasper at google.com
Mon Jan 7 05:08:40 PST 2013
Author: djasper
Date: Mon Jan 7 07:08:40 2013
New Revision: 171731
URL: http://llvm.org/viewvc/llvm-project?rev=171731&view=rev
Log:
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.
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=171731&r1=171730&r2=171731&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Mon Jan 7 07:08:40 2013
@@ -257,7 +257,12 @@
if (Newline) {
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)) {
State.Column = State.Column - Previous.TokenLength;
} else if (Current.Tok.is(tok::lessless) &&
@@ -306,17 +311,19 @@
if (!DryRun)
replaceWhitespace(Current, 0, Spaces);
- // FIXME: Look into using this alignment at other ParenLevels.
- if (ParenLevel == 0 && (getPrecedence(Previous) == prec::Assignment ||
- Previous.Tok.is(tok::kw_return)))
+ if (Line.Tokens[0].Tok.isNot(tok::kw_for) &&
+ (getPrecedence(Previous) == prec::Assignment ||
+ Previous.Tok.is(tok::kw_return)))
State.Indent[ParenLevel] = State.Column + Spaces;
if (Previous.Tok.is(tok::l_paren) ||
Annotations[Index - 1].Type == TT_TemplateOpener)
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;
- if (Spaces > 0 && ParenLevel != 0)
+ if (Spaces > 0 &&
+ (ParenLevel != 0 || getPrecedence(Previous) == prec::Assignment))
State.LastSpace[ParenLevel] = State.Column;
}
moveStateToNextToken(State);
@@ -375,7 +382,13 @@
if (Left.Tok.is(tok::l_paren))
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)
return Level;
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=171731&r1=171730&r2=171731&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Jan 7 07:08:40 2013
@@ -384,8 +384,8 @@
// FIXME: Format like enums if the static initializer does not fit on a line.
verifyFormat(
- "static SomeClass WithALoooooooooooooooooooongName = { 100000000,\n"
- " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" };");
+ "static SomeClass WithALoooooooooooooooooooongName = {\n"
+ " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" };");
}
TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
@@ -641,23 +641,34 @@
" 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) {
verifyFormat(
"int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaa;");
+ " aaaaaaaaaaaaaaaaaaaaaaaaa;");
verifyFormat(
"Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaa;");
+ " aaaaaaaaaaaaaaaaaaaaaaaaa;");
verifyFormat(
"Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaa;");
+ " aaaaaaaaaaaaaaaaaaaaaaaaa;");
verifyFormat(
"int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaa);");
+ " aaaaaaaaaaaaaaaaaaaaaaaaa);");
verifyFormat(
- "double LooooooooooooooooooooooooongResult =\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaa;");
+ "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaa;");
}
TEST_F(FormatTest, AlignsAfterReturn) {
@@ -713,8 +724,10 @@
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
"}");
- verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
- " 100000000 + 100000000) {\n}");
+ verifyFormat(
+ "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
+ " 10000000) {\n"
+ "}");
}
TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
More information about the cfe-commits
mailing list