From adededff2657833d4ee7cdaeeb5afe6707adbdb1 Mon Sep 17 00:00:00 2001 From: Manuel Klimek Date: Fri, 11 Jan 2013 18:28:36 +0000 Subject: [PATCH] Fix crash on invalid. if { foo; } would previously crash clang-format. llvm-svn: 172232 --- clang/lib/Format/UnwrappedLineParser.cpp | 3 ++- clang/unittests/Format/FormatTest.cpp | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 758f8193ca4f..07ad7a7489e0 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -404,7 +404,8 @@ void UnwrappedLineParser::parseParens() { void UnwrappedLineParser::parseIfThenElse() { assert(FormatTok.Tok.is(tok::kw_if) && "'if' expected"); nextToken(); - parseParens(); + if (FormatTok.Tok.is(tok::l_paren)) + parseParens(); bool NeedsUnwrappedLine = false; if (FormatTok.Tok.is(tok::l_brace)) { parseBlock(); diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 71fa83ccb140..6beee3186b72 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -1108,6 +1108,10 @@ TEST_F(FormatTest, IncorrectCodeDoNoWhile) { "}"); } +TEST_F(FormatTest, IncorrectIf) { + verifyFormat("if {\n foo;\n foo();\n}"); +} + TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { verifyFormat("namespace {\n" "class Foo { Foo ( }; } // comment");