From d465843f5376c7c1574fe61338cbf3dc50684d5b 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@172232 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/UnwrappedLineParser.cpp | 3 ++- unittests/Format/FormatTest.cpp | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index 758f8193ca..07ad7a7489 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/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/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 71fa83ccb1..6beee3186b 100644 --- a/unittests/Format/FormatTest.cpp +++ b/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"); -- 2.40.0