From: Daniel Jasper Date: Thu, 10 Jan 2013 09:26:47 +0000 (+0000) Subject: Do more error checking for '{}'. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=700e710544a50ec63f984b9b098530bf9107920a;p=clang Do more error checking for '{}'. This fixes llvm.org/PR14883, where clang-format would run into an assertion on: void f() { return } 42 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@172049 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 1ab577049c..4de26e2b4e 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -651,6 +651,21 @@ public: return false; } + bool parseBrace() { + while (CurrentToken != NULL) { + if (CurrentToken->is(tok::r_brace)) { + next(); + return true; + } + if (CurrentToken->is(tok::r_paren) || CurrentToken->is(tok::r_square)) + return false; + if (!consumeToken()) + return false; + } + // Lines can currently end with '{'. + return true; + } + bool parseConditional() { while (CurrentToken != NULL) { if (CurrentToken->is(tok::colon)) { @@ -693,6 +708,10 @@ public: if (!parseSquare()) return false; break; + case tok::l_brace: + if (!parseBrace()) + return false; + break; case tok::less: if (parseAngle()) Tok->Type = TT_TemplateOpener; @@ -705,6 +724,11 @@ public: case tok::r_paren: case tok::r_square: return false; + case tok::r_brace: + // Lines can start with '}'. + if (Tok->Parent != NULL) + return false; + break; case tok::greater: Tok->Type = TT_BinaryOperator; break; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 389c58c428..739bcdd80f 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -1036,6 +1036,10 @@ TEST_F(FormatTest, HandlesIncludeDirectives) { // Error recovery tests. //===----------------------------------------------------------------------===// +TEST_F(FormatTest, IncorrectCodeTrailingStuff) { + verifyFormat("void f() { return } 42"); +} + TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { verifyFormat("int aaaaaaaa =\n" " // Overly long comment\n"