diff options
author | Daniel Jasper <djasper@google.com> | 2014-04-16 12:26:54 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2014-04-16 12:26:54 +0000 |
commit | 0e61784ae5d95ea125f980af69e1257ab0e7209c (patch) | |
tree | 61bfe99703bc60a3c7f3f27a5da3eaa1c0a85a29 | |
parent | 0c02d26d6ba316b21d0881315341eb30d4a3320e (diff) | |
download | bcm5719-llvm-0e61784ae5d95ea125f980af69e1257ab0e7209c.tar.gz bcm5719-llvm-0e61784ae5d95ea125f980af69e1257ab0e7209c.zip |
clang-format: Add special case to reduce indentaiton in streams.
This is similar to how we treat assignments and seems to be generally
desirable.
Before:
llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
aaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaa);
After:
llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);
llvm-svn: 206384
-rw-r--r-- | clang/lib/Format/ContinuationIndenter.cpp | 6 | ||||
-rw-r--r-- | clang/lib/Format/FormatToken.h | 14 | ||||
-rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 8 | ||||
-rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 12 |
4 files changed, 32 insertions, 8 deletions
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 9311ca481c3..52ee47987f1 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -301,7 +301,9 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, else if ((Previous.Type == TT_BinaryOperator || Previous.Type == TT_ConditionalExpr || Previous.Type == TT_CtorInitializerColon) && - (Previous.getPrecedence() != prec::Assignment || + ((Previous.getPrecedence() != prec::Assignment && + (Previous.isNot(tok::lessless) || Previous.OperatorIndex != 0 || + !Previous.LastOperator)) || Current.StartsBinaryExpression)) // Always indent relative to the RHS of the expression unless this is a // simple assignment without binary expression on the RHS. Also indent @@ -573,7 +575,7 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State, std::min(State.LowestLevelOnLine, State.ParenLevel); if (Current.isMemberAccess()) State.Stack.back().StartOfFunctionCall = - Current.LastInChainOfCalls ? 0 : State.Column + Current.ColumnWidth; + Current.LastOperator ? 0 : State.Column + Current.ColumnWidth; if (Current.Type == TT_ObjCSelectorName) State.Stack.back().ObjCSelectorNameFound = true; if (Current.Type == TT_LambdaLSquare) diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h index 249eecc0ba1..749dfcb3e46 100644 --- a/clang/lib/Format/FormatToken.h +++ b/clang/lib/Format/FormatToken.h @@ -106,8 +106,9 @@ struct FormatToken { UnbreakableTailLength(0), BindingStrength(0), NestingLevel(0), SplitPenalty(0), LongestObjCSelectorName(0), FakeRParens(0), StartsBinaryExpression(false), EndsBinaryExpression(false), - LastInChainOfCalls(false), PartOfMultiVariableDeclStmt(false), - IsForEachMacro(false), MatchingParen(NULL), Previous(NULL), Next(NULL), + OperatorIndex(0), LastOperator(false), + PartOfMultiVariableDeclStmt(false), IsForEachMacro(false), + MatchingParen(NULL), Previous(NULL), Next(NULL), Decision(FD_Unformatted), Finalized(false) {} /// \brief The \c Token. @@ -242,8 +243,13 @@ struct FormatToken { /// \brief \c true if this token ends a binary expression. bool EndsBinaryExpression; - /// \brief Is this the last "." or "->" in a builder-type call? - bool LastInChainOfCalls; + /// \brief Is this is an operator (or "."/"->") in a sequence of operators + /// with the same precedence, contains the 0-based operator index. + unsigned OperatorIndex; + + /// \brief Is this the last operator (or "."/"->") in a sequence of operators + /// with the same precedence? + bool LastOperator; /// \brief Is this token part of a \c DeclStmt defining multiple variables? /// diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 98323fcc57c..1aa64b450d3 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -978,6 +978,7 @@ public: FormatToken *Start = Current; FormatToken *LatestOperator = NULL; + unsigned OperatorIndex = 0; while (Current) { // Consume operators with higher precedence. @@ -997,8 +998,8 @@ public: if (Current == NULL || Current->closesScope() || (CurrentPrecedence != -1 && CurrentPrecedence < Precedence)) { if (LatestOperator) { + LatestOperator->LastOperator = true; if (Precedence == PrecedenceArrowAndPeriod) { - LatestOperator->LastInChainOfCalls = true; // Call expressions don't have a binary operator precedence. addFakeParenthesis(Start, prec::Unknown); } else { @@ -1017,8 +1018,11 @@ public: next(); } else { // Operator found. - if (CurrentPrecedence == Precedence) + if (CurrentPrecedence == Precedence) { LatestOperator = Current; + Current->OperatorIndex = OperatorIndex; + ++OperatorIndex; + } next(); } diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index f1442e03b68..ee9afe40c93 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -3983,6 +3983,18 @@ TEST_F(FormatTest, AlignsPipes) { "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); + verifyFormat( + "llvm::errs() << \"a: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); + verifyFormat( + "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" + " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); + verifyFormat( + "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); verifyFormat("return out << \"somepacket = {\\n\"\n" " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" |