Fixes formatting of empty blocks.

We now only put empty blocks into a single line, if all of:
- all tokens of the structural element fit into a single line
- we're not in a control flow statement

Note that we usually don't put record definitions into a single line, as
there's usually at least one more token (the semicolon) after the
closing brace. This doesn't hold when we are in a context where there is
no semicolon, like "enum E {}".

There were some missing tests around joining lines around the corner
cases of the allowed number of columns, so this patch adds some.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173055 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index 12fd070..7c25cbb 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -1711,9 +1711,9 @@
     // 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 @@
     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 @@
     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 @@
   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 @@
     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);
 
-    // 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];
+      // 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;
+
+      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) {
diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp
index f1d08a6..c054ef6 100644
--- a/lib/Format/UnwrappedLineParser.cpp
+++ b/lib/Format/UnwrappedLineParser.cpp
@@ -170,17 +170,15 @@
   assert(FormatTok.Tok.is(tok::l_brace) && "'{' expected");
   nextToken();
 
-  if (!FormatTok.Tok.is(tok::r_brace)) {
-    addUnwrappedLine();
+  addUnwrappedLine();
 
-    Line->Level += AddLevels;
-    parseLevel(/*HasOpeningBrace=*/true);
-    Line->Level -= AddLevels;
+  Line->Level += AddLevels;
+  parseLevel(/*HasOpeningBrace=*/true);
+  Line->Level -= AddLevels;
 
-    if (!FormatTok.Tok.is(tok::r_brace))
-      return true;
+  if (!FormatTok.Tok.is(tok::r_brace))
+    return true;
 
-  }
   nextToken();  // Munch the closing brace.
   return false;
 }