diff options
author | Daniel Jasper <djasper@google.com> | 2013-02-21 21:33:55 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2013-02-21 21:33:55 +0000 |
commit | f11a705c6075b12cbae5632ba4de433bc9bc105f (patch) | |
tree | fbcca74530fd91184c64014d189901b5c277aeb6 /lib/Format/Format.cpp | |
parent | fdf6a279c9a75c778eba382d9a156697092982a1 (diff) |
Consistently put {} onto the same line for empty functions.
This fixes llvm.org/PR15167.
Before:
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL()
: AAAAAAAA(10), BBBBBBBBB(10) {
}
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL() : AAAAAAAA(10) {}
After:
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL()
: AAAAAAAA(10), BBBBBBBBB(10) {}
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL() : AAAAAAAA(10) {}
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@175800 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Format/Format.cpp')
-rw-r--r-- | lib/Format/Format.cpp | 36 |
1 files changed, 18 insertions, 18 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 2bd5332227..880be39b1c 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -272,8 +272,7 @@ public: WhitespaceManager &Whitespaces, bool StructuralError) : Style(Style), SourceMgr(SourceMgr), Line(Line), FirstIndent(FirstIndent), RootToken(RootToken), - Whitespaces(Whitespaces), Count(0) { - } + Whitespaces(Whitespaces), Count(0) {} /// \brief Formats an \c UnwrappedLine. /// @@ -332,8 +331,7 @@ private: BreakBeforeClosingBrace(false), QuestionColumn(0), AvoidBinPacking(AvoidBinPacking), BreakBeforeParameter(false), HasMultiParameterLine(HasMultiParameterLine), ColonPos(0), - BreakBeforeThirdOperand(false) { - } + BreakBeforeThirdOperand(false) {} /// \brief The position to which a specific parenthesis level needs to be /// indented. @@ -766,8 +764,7 @@ private: /// inserting a newline dependent on the \c NewLine. struct StateNode { StateNode(const LineState &State, bool NewLine, StateNode *Previous) - : State(State), NewLine(NewLine), Previous(Previous) { - } + : State(State), NewLine(NewLine), Previous(Previous) {} LineState State; bool NewLine; StateNode *Previous; @@ -1038,8 +1035,7 @@ public: SourceManager &SourceMgr, const std::vector<CharSourceRange> &Ranges) : Diag(Diag), Style(Style), Lex(Lex), SourceMgr(SourceMgr), - Whitespaces(SourceMgr), Ranges(Ranges) { - } + Whitespaces(SourceMgr), Ranges(Ranges) {} virtual ~Formatter() {} @@ -1199,18 +1195,14 @@ private: void tryFitMultipleLinesInOne(unsigned Indent, std::vector<AnnotatedLine>::iterator &I, std::vector<AnnotatedLine>::iterator E) { - unsigned Limit = Style.ColumnLimit - (I->InPPDirective ? 1 : 0) - Indent; - // We can never merge stuff if there are trailing line comments. if (I->Last->Type == TT_LineComment) return; - // Check whether the UnwrappedLine can be put onto a single line. If - // so, this is bound to be the optimal solution (by definition) and we - // don't need to analyze the entire solution space. - if (I->Last->TotalLength > Limit) - return; - Limit -= I->Last->TotalLength; + unsigned Limit = Style.ColumnLimit - (I->InPPDirective ? 1 : 0) - Indent; + // If we already exceed the column limit, we set 'Limit' to 0. The different + // tryMerge..() functions can then decide whether to still do merging. + Limit = I->Last->TotalLength > Limit ? 0 : Limit - I->Last->TotalLength; if (I + 1 == E || (I + 1)->Type == LT_Invalid) return; @@ -1229,6 +1221,8 @@ private: void tryMergeSimplePPDirective(std::vector<AnnotatedLine>::iterator &I, std::vector<AnnotatedLine>::iterator E, unsigned Limit) { + if (Limit == 0) + return; AnnotatedLine &Line = *I; if (!(I + 1)->InPPDirective || (I + 1)->First.FormatTok.HasUnescapedNewline) return; @@ -1243,6 +1237,8 @@ private: void tryMergeSimpleIf(std::vector<AnnotatedLine>::iterator &I, std::vector<AnnotatedLine>::iterator E, unsigned Limit) { + if (Limit == 0) + return; if (!Style.AllowShortIfStatementsOnASingleLine) return; if ((I + 1)->InPPDirective != I->InPPDirective || @@ -1282,11 +1278,13 @@ private: AnnotatedToken *Tok = &(I + 1)->First; if (Tok->Children.empty() && Tok->is(tok::r_brace) && - !Tok->MustBreakBefore && Tok->TotalLength <= Limit) { + !Tok->MustBreakBefore) { + // We merge empty blocks even if the line exceeds the column limit. Tok->SpacesRequiredBefore = 0; + Tok->CanBreakBefore = true; join(Line, *(I + 1)); I += 1; - } else { + } else if (Limit != 0) { // Check that we still have three lines and they fit into the limit. if (I + 2 == E || (I + 2)->Type == LT_Invalid || !nextTwoLinesFitInto(I, Limit)) @@ -1321,9 +1319,11 @@ private: } void join(AnnotatedLine &A, const AnnotatedLine &B) { + unsigned LengthA = A.Last->TotalLength + B.First.SpacesRequiredBefore; A.Last->Children.push_back(B.First); while (!A.Last->Children.empty()) { A.Last->Children[0].Parent = A.Last; + A.Last->Children[0].TotalLength += LengthA; A.Last = &A.Last->Children[0]; } } |