Clang-format error recovery part 1

Reviewers: klimek

CC: cfe-commits

Differential Revision: http://llvm-reviews.chandlerc.com/D163

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@169278 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp
index 7c04ce0..888f1c9 100644
--- a/lib/Format/UnwrappedLineParser.cpp
+++ b/lib/Format/UnwrappedLineParser.cpp
@@ -72,7 +72,10 @@
   parseLevel();
   if (!IsNamespace)
     --Line.Level;
-  assert(FormatTok.Tok.is(tok::r_brace) && "expected '}'");
+  // FIXME: Add error handling.
+  if (!FormatTok.Tok.is(tok::r_brace))
+    return;
+
   nextToken();
   if (FormatTok.Tok.is(tok::semi))
     nextToken();
@@ -218,7 +221,12 @@
     --Line.Level;
   }
 
-  assert(FormatTok.Tok.is(tok::kw_while) && "'while' expected");
+  // FIXME: Add error handling.
+  if (!FormatTok.Tok.is(tok::kw_while)) {
+    addUnwrappedLine();
+    return;
+  }
+
   nextToken();
   parseStatement();
 }
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index e21367b..5c1b603 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -363,5 +363,23 @@
 //               "};");
 //}
 
+TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
+  verifyFormat("{");
+}
+
+TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
+  verifyFormat("do {\n"
+               "};");
+  verifyFormat("do {\n"
+               "};\n"
+               "f();");
+  verifyFormat("do {\n"
+               "}\n"
+               "wheeee(fun);");
+  verifyFormat("do {\n"
+               "  f();\n"
+               "};");
+}
+
 }  // end namespace tooling
 }  // end namespace clang