diff options
author | Daniel Jasper <djasper@google.com> | 2013-03-20 12:37:50 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2013-03-20 12:37:50 +0000 |
commit | f9955d309d3de328e65563baf2d34571249dccbb (patch) | |
tree | 431784fb461fc7b662bbb7143b934b3ea0c12ecc /lib/Format/Format.cpp | |
parent | bcca7e484117f3155ff98f9560996e760ca4f786 (diff) |
Add extra indentation for multiline comparisons.
This seems to be generally more desired.
Before:
if (aaaaaaaa &&
bbbbbbbb >
cccccccc) {}
After:
if (aaaaaaaa &&
bbbbbbbb >
cccccccc) {}
Also: Some formatting cleanup on clang-format's files.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@177514 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Format/Format.cpp')
-rw-r--r-- | lib/Format/Format.cpp | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index dd8f6cde03..0e4a2b797f 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -85,6 +85,12 @@ static bool isTrailingComment(const AnnotatedToken &Tok) { (Tok.Children.empty() || Tok.Children[0].MustBreakBefore); } +static bool isComparison(const AnnotatedToken &Tok) { + prec::Level Precedence = getPrecedence(Tok); + return Tok.Type == TT_BinaryOperator && + (Precedence == prec::Equality || Precedence == prec::Relational); +} + // Returns the length of everything up to the first possible line break after // the ), ], } or > matching \c Tok. static unsigned getLengthToMatchingParen(const AnnotatedToken &Tok) { @@ -127,7 +133,7 @@ public: // Align comment with other comments. if (Tok.Parent != NULL || !Comments.empty()) { if (Style.ColumnLimit >= - Spaces + WhitespaceStartColumn + Tok.FormatTok.TokenLength) { + Spaces + WhitespaceStartColumn + Tok.FormatTok.TokenLength) { StoredComment Comment; Comment.Tok = Tok.FormatTok; Comment.Spaces = Spaces; @@ -229,8 +235,7 @@ private: const char *TokenStart = SourceMgr.getCharacterData(Tok.Tok.getLocation()); while (Line.rtrim().size() > ColumnLimit) { // Try to break at the last whitespace before the column limit. - size_t SpacePos = - Line.find_last_of(WhiteSpaceChars, ColumnLimit + 1); + size_t SpacePos = Line.find_last_of(WhiteSpaceChars, ColumnLimit + 1); if (SpacePos == StringRef::npos) { // Try to find any whitespace in the line. SpacePos = Line.find_first_of(WhiteSpaceChars); @@ -573,7 +578,7 @@ private: if (VariablePos != Other.VariablePos) return VariablePos < Other.VariablePos; if (LineContainsContinuedForLoopSection != - Other.LineContainsContinuedForLoopSection) + Other.LineContainsContinuedForLoopSection) return LineContainsContinuedForLoopSection; if (ParenLevel != Other.ParenLevel) return ParenLevel < Other.ParenLevel; @@ -621,7 +626,8 @@ private: State.Column = State.Stack.back().FirstLessLess; } else if (State.ParenLevel != 0 && (Previous.isOneOf(tok::equal, tok::coloncolon) || - Current.isOneOf(tok::period, tok::arrow, tok::question))) { + Current.isOneOf(tok::period, tok::arrow, tok::question) || + isComparison(Previous))) { // Indent and extra 4 spaces after if we know the current expression is // continued. Don't do that on the top level, as we already indent 4 // there. @@ -709,7 +715,7 @@ private: if (Current.Type == TT_ObjCSelectorName && State.Stack.back().ColonPos == 0) { if (State.Stack.back().Indent + Current.LongestObjCSelectorName > - State.Column + Spaces + Current.FormatTok.TokenLength) + State.Column + Spaces + Current.FormatTok.TokenLength) State.Stack.back().ColonPos = State.Stack.back().Indent + Current.LongestObjCSelectorName; else @@ -729,7 +735,7 @@ private: // Treat the condition inside an if as if it was a second function // parameter, i.e. let nested calls have an indent of 4. State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(". - else if (Previous.is(tok::comma) && State.ParenLevel != 0) + else if (Previous.is(tok::comma)) // Top-level spaces are exempt as that mostly leads to better results. State.Stack.back().LastSpace = State.Column; else if ((Previous.Type == TT_BinaryOperator || @@ -1283,8 +1289,7 @@ public: while (IndentForLevel.size() <= TheLine.Level) IndentForLevel.push_back(-1); IndentForLevel.resize(TheLine.Level + 1); - bool WasMoved = - PreviousLineWasTouched && FirstTok.NewlinesBefore == 0; + bool WasMoved = PreviousLineWasTouched && FirstTok.NewlinesBefore == 0; if (TheLine.First.is(tok::eof)) { if (PreviousLineWasTouched) { unsigned NewLines = std::min(FirstTok.NewlinesBefore, 1u); @@ -1298,8 +1303,8 @@ public: if (static_cast<int>(Indent) + Offset >= 0) Indent += Offset; if (!FirstTok.WhiteSpaceStart.isValid() || StructuralError) { - Indent = LevelIndent = SourceMgr.getSpellingColumnNumber( - FirstTok.Tok.getLocation()) - 1; + Indent = LevelIndent = + SourceMgr.getSpellingColumnNumber(FirstTok.Tok.getLocation()) - 1; } else { formatFirstToken(TheLine.First, Indent, TheLine.InPPDirective, PreviousEndOfLineColumn); |