Fix crashes in UnwrappedLineParser on missing parens.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@172239 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp
index 07ad7a7..0c8ff89 100644
--- a/lib/Format/UnwrappedLineParser.cpp
+++ b/lib/Format/UnwrappedLineParser.cpp
@@ -450,7 +450,8 @@
assert((FormatTok.Tok.is(tok::kw_for) || FormatTok.Tok.is(tok::kw_while)) &&
"'for' or 'while' expected");
nextToken();
- parseParens();
+ if (FormatTok.Tok.is(tok::l_paren))
+ parseParens();
if (FormatTok.Tok.is(tok::l_brace)) {
parseBlock();
addUnwrappedLine();
@@ -510,7 +511,8 @@
void UnwrappedLineParser::parseSwitch() {
assert(FormatTok.Tok.is(tok::kw_switch) && "'switch' expected");
nextToken();
- parseParens();
+ if (FormatTok.Tok.is(tok::l_paren))
+ parseParens();
if (FormatTok.Tok.is(tok::l_brace)) {
parseBlock(Style.IndentCaseLabels ? 2 : 1);
addUnwrappedLine();
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index a2f150d..a0cfeee 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -1114,8 +1114,12 @@
"}");
}
-TEST_F(FormatTest, IncorrectIf) {
+TEST_F(FormatTest, IncorrectCodeMissingParens) {
verifyFormat("if {\n foo;\n foo();\n}");
+ verifyFormat("switch {\n foo;\n foo();\n}");
+ verifyFormat("for {\n foo;\n foo();\n}");
+ verifyFormat("while {\n foo;\n foo();\n}");
+ verifyFormat("do {\n foo;\n foo();\n} while;");
}
TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {