diff options
author | Daniel Jasper <djasper@google.com> | 2013-01-07 13:08:40 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2013-01-07 13:08:40 +0000 |
commit | 9cda8000434be3360abb38fe1690fa24ae3d48be (patch) | |
tree | ef5fb5b44cfe96eccec46d250151bc9bbcb70259 /lib/Format/Format.cpp | |
parent | 7ad4effaa96905ef9dbc3815760b06b1d1639390 (diff) |
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.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@171731 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Format/Format.cpp')
-rw-r--r-- | lib/Format/Format.cpp | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 5c65c26ca6..471021a370 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -257,7 +257,12 @@ private: 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 @@ private: 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 @@ private: 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; |