]> granicus.if.org Git - clang/commitdiff
Fix crashes in UnwrappedLineParser on missing parens.
authorManuel Klimek <klimek@google.com>
Fri, 11 Jan 2013 19:23:05 +0000 (19:23 +0000)
committerManuel Klimek <klimek@google.com>
Fri, 11 Jan 2013 19:23:05 +0000 (19:23 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@172239 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/UnwrappedLineParser.cpp
unittests/Format/FormatTest.cpp

index 07ad7a7489e02b26515bfcdccfa7c10d0c942d7f..0c8ff890491d996cf9f765abc0998fa5057d4734 100644 (file)
@@ -450,7 +450,8 @@ void UnwrappedLineParser::parseForOrWhileLoop() {
   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::parseCaseLabel() {
 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();
index a2f150d1862555bca8ebd6ebe486cb59f346fc30..a0cfeee90b48af66058eeef8ae2306240058490b 100644 (file)
@@ -1114,8 +1114,12 @@ TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
                "}");
 }
 
-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) {