aboutsummaryrefslogtreecommitdiff
path: root/lib/Format/Format.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Format/Format.cpp')
-rw-r--r--lib/Format/Format.cpp67
1 files changed, 34 insertions, 33 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index 12fd070cfe..7c25cbb059 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -1711,9 +1711,9 @@ private:
// 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)
+ if (I->Last->TotalLength > Limit)
return;
- Limit -= I->Last->TotalLength + 1; // One space.
+ Limit -= I->Last->TotalLength;
if (I + 1 == E || (I + 1)->Type == LT_Invalid)
return;
@@ -1738,7 +1738,7 @@ private:
if (I + 2 != E && (I + 2)->InPPDirective &&
!(I + 2)->First.FormatTok.HasUnescapedNewline)
return;
- if ((I + 1)->Last->TotalLength > Limit)
+ if (1 + (I + 1)->Last->TotalLength > Limit)
return;
join(Line, *(++I));
}
@@ -1755,7 +1755,7 @@ private:
AnnotatedLine &Line = *I;
if (Line.Last->isNot(tok::r_paren))
return;
- if ((I + 1)->Last->TotalLength > Limit)
+ if (1 + (I + 1)->Last->TotalLength > Limit)
return;
if ((I + 1)->First.is(tok::kw_if) || (I + 1)->First.Type == TT_LineComment)
return;
@@ -1768,11 +1768,6 @@ private:
void tryMergeSimpleBlock(std::vector<AnnotatedLine>::iterator &I,
std::vector<AnnotatedLine>::iterator E,
unsigned Limit){
- // 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))
- return;
-
// First, check that the current line allows merging. This is the case if
// we're not in a control flow statement and the last token is an opening
// brace.
@@ -1788,38 +1783,44 @@ private:
if (!AllowedTokens)
return;
- // Second, check that the next line does not contain any braces - if it
- // does, readability declines when putting it into a single line.
- const AnnotatedToken *Tok = &(I + 1)->First;
- if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore)
- return;
- do {
- if (Tok->is(tok::l_brace) || Tok->is(tok::r_brace))
+ AnnotatedToken *Tok = &(I + 1)->First;
+ if (Tok->Children.empty() && Tok->is(tok::r_brace) &&
+ !Tok->MustBreakBefore && Tok->TotalLength <= Limit) {
+ Tok->SpaceRequiredBefore = false;
+ join(Line, *(I + 1));
+ I += 1;
+ } else {
+ // 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))
return;
- Tok = Tok->Children.empty() ? NULL : &Tok->Children.back();
- } while (Tok != NULL);
- // Last, check that the third line contains a single closing brace.
- Tok = &(I + 2)->First;
- if (!Tok->Children.empty() || Tok->isNot(tok::r_brace) ||
- Tok->MustBreakBefore)
- return;
+ // Second, check that the next line does not contain any braces - if it
+ // does, readability declines when putting it into a single line.
+ if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore)
+ return;
+ do {
+ if (Tok->is(tok::l_brace) || Tok->is(tok::r_brace))
+ return;
+ Tok = Tok->Children.empty() ? NULL : &Tok->Children.back();
+ } while (Tok != NULL);
+
+ // Last, check that the third line contains a single closing brace.
+ Tok = &(I + 2)->First;
+ if (!Tok->Children.empty() || Tok->isNot(tok::r_brace) ||
+ Tok->MustBreakBefore)
+ return;
- // If the merged line fits, we use that instead and skip the next two lines.
- Line.Last->Children.push_back((I + 1)->First);
- while (!Line.Last->Children.empty()) {
- Line.Last->Children[0].Parent = Line.Last;
- Line.Last = &Line.Last->Children[0];
+ join(Line, *(I + 1));
+ join(Line, *(I + 2));
+ I += 2;
}
-
- join(Line, *(I + 1));
- join(Line, *(I + 2));
- I += 2;
}
bool nextTwoLinesFitInto(std::vector<AnnotatedLine>::iterator I,
unsigned Limit) {
- return (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <= Limit;
+ return 1 + (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <=
+ Limit;
}
void join(AnnotatedLine &A, const AnnotatedLine &B) {