diff options
author | Daniel Jasper <djasper@google.com> | 2012-12-24 16:43:00 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2012-12-24 16:43:00 +0000 |
commit | a4974cf6ae618f04d5dd7fc45bc18bff41ca86bf (patch) | |
tree | b2bfc7f1b1303209b265d480fc3154f881adc3ad /lib/Format/Format.cpp | |
parent | cf225b67e66d7a85773b7bf7907c0fef4374c31b (diff) |
Penalize tokens with a lower parenthesis level than the start of the line.
This prevents formattings like this (assuming "parameter" doesn't fit the line):
bool f = someFunction() && someFunctionWithParam(
parameter) && someOtherFunction();
Here, "parameter" - the start of line 2 - has a parenthesis level of 2, but
there are subsequent tokens ("&&" and "someOtherFunction") with a lower level.
This is bad for readability as "parameter" hides "someOtherFunction". With this
patch, this changes to:
bool f = someFunction() &&
someFunctionWithParam(parameter) &&
someOtherFunction();
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@171038 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Format/Format.cpp')
-rw-r--r-- | lib/Format/Format.cpp | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 056376a4e2..7d135d9b17 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -83,6 +83,7 @@ FormatStyle getGoogleStyle() { struct OptimizationParameters { unsigned PenaltyIndentLevel; + unsigned PenaltyLevelDecrease; }; class UnwrappedLineFormatter { @@ -95,6 +96,7 @@ public: Annotations(Annotations), Replaces(Replaces), StructuralError(StructuralError) { Parameters.PenaltyIndentLevel = 15; + Parameters.PenaltyLevelDecrease = 10; } void format() { @@ -110,6 +112,7 @@ public: State.FirstLessLess.push_back(0); State.ForLoopVariablePos = 0; State.LineContainsContinuedForLoopSection = false; + State.StartOfLineLevel = 1; // The first token has already been indented and thus consumed. moveStateToNextToken(State); @@ -155,6 +158,9 @@ private: /// \brief The number of tokens already consumed. unsigned ConsumedTokens; + /// \brief The parenthesis level of the first token on the current line. + unsigned StartOfLineLevel; + /// \brief The position to which a specific parenthesis level needs to be /// indented. std::vector<unsigned> Indent; @@ -186,6 +192,8 @@ private: return Other.ConsumedTokens > ConsumedTokens; if (Other.Column != Column) return Other.Column > Column; + if (Other.StartOfLineLevel != StartOfLineLevel) + return Other.StartOfLineLevel > StartOfLineLevel; if (Other.Indent.size() != Indent.size()) return Other.Indent.size() > Indent.size(); for (int i = 0, e = Indent.size(); i != e; ++i) { @@ -247,6 +255,8 @@ private: State.Column = State.Indent[ParenLevel]; } + State.StartOfLineLevel = ParenLevel + 1; + if (Line.Tokens[0].Tok.is(tok::kw_for)) State.LineContainsContinuedForLoopSection = Previous.Tok.isNot(tok::semi); @@ -343,7 +353,7 @@ private: return Level; if (Right.Tok.is(tok::arrow) || Right.Tok.is(tok::period)) - return 200; + return 50; return 3; } @@ -375,6 +385,10 @@ private: if (NewLine) { CurrentPenalty += Parameters.PenaltyIndentLevel * State.Indent.size() + splitPenalty(State.ConsumedTokens - 1); + } else { + if (State.Indent.size() < State.StartOfLineLevel) + CurrentPenalty += Parameters.PenaltyLevelDecrease * + (State.StartOfLineLevel - State.Indent.size()); } addTokenToState(NewLine, true, State); |