From: Daniel Jasper Date: Fri, 31 May 2013 14:56:29 +0000 (+0000) Subject: Improve clang-format's error recovery. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f7ec1cc7f931833b2cb9876a5e7dfd6bf8681c6a;p=clang Improve clang-format's error recovery. If a "}" is found inside parenthesis, this is probably a case of missing parenthesis. This enables continuing to format after stuff code like: class A { void f( }; .. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@183009 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index 618f54a518..933da66a62 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/lib/Format/UnwrappedLineParser.cpp @@ -45,6 +45,7 @@ public: else Line.MustBeDeclaration = true; } + private: UnwrappedLine &Line; std::vector &Stack; @@ -81,9 +82,7 @@ public: return Token; } - virtual unsigned getPosition() { - return PreviousTokenSource->getPosition(); - } + virtual unsigned getPosition() { return PreviousTokenSource->getPosition(); } virtual FormatToken *setPosition(unsigned Position) { Token = PreviousTokenSource->setPosition(Position); @@ -279,7 +278,7 @@ void UnwrappedLineParser::calculateBraceTypes() { case tok::kw_for: case tok::kw_switch: case tok::kw_try: - if (!LBraceStack.empty()) + if (!LBraceStack.empty()) LBraces[LBraceStack.back()] = BS_Block; break; default: @@ -386,9 +385,7 @@ void UnwrappedLineParser::parsePPElse() { parsePPUnknown(); } -void UnwrappedLineParser::parsePPElIf() { - parsePPElse(); -} +void UnwrappedLineParser::parsePPElIf() { parsePPElse(); } void UnwrappedLineParser::parsePPEndIf() { if (!PPStack.empty()) @@ -700,15 +697,21 @@ void UnwrappedLineParser::parseParens() { case tok::r_paren: nextToken(); return; + case tok::r_brace: + // A "}" inside parenthesis is an error if there wasn't a matching "{". + return; case tok::l_brace: { if (!tryToParseBracedList()) { nextToken(); - ScopedLineState LineState(*this); - ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, - /*MustBeDeclaration=*/ false); - Line->Level += 1; - parseLevel(/*HasOpeningBrace=*/ true); - Line->Level -= 1; + { + ScopedLineState LineState(*this); + ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, + /*MustBeDeclaration=*/ false); + Line->Level += 1; + parseLevel(/*HasOpeningBrace=*/ true); + Line->Level -= 1; + } + nextToken(); } break; } diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 8f6e9d6ad6..9e9a5f30d5 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -1901,6 +1901,19 @@ TEST_F(FormatTest, LayoutBlockInsideParens) { " int i;\n" "});", format(" functionCall ( {int i;} );")); + + // FIXME: This is bad, find a better and more generic solution. + EXPECT_EQ("functionCall({\n" + " int i;\n" + "},\n" + " aaaa, bbbb, cccc);", + format(" functionCall ( {int i;}, aaaa, bbbb, cccc);")); + verifyFormat( + "Aaa({\n" + " int i;\n" + "},\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" + " ccccccccccccccccc));"); } TEST_F(FormatTest, LayoutBlockInsideStatement) { @@ -3387,7 +3400,9 @@ TEST_F(FormatTest, IncorrectCodeMissingParens) { TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { verifyFormat("namespace {\n" - "class Foo { Foo ( }; } // comment"); + "class Foo { Foo (\n" + "};\n" + "} // comment"); } TEST_F(FormatTest, IncorrectCodeErrorDetection) { @@ -3460,16 +3475,6 @@ TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) { NoSpaces); } -TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) { - // FIXME: This is bad, find a better and more generic solution. - verifyFormat( - "Aaa({\n" - " int i;\n" - "},\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" - " ccccccccccccccccc));"); -} - TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { verifyFormat("void f() { return 42; }"); verifyFormat("void f() {\n"