]> granicus.if.org Git - clang/commitdiff
Do more error checking for '{}'.
authorDaniel Jasper <djasper@google.com>
Thu, 10 Jan 2013 09:26:47 +0000 (09:26 +0000)
committerDaniel Jasper <djasper@google.com>
Thu, 10 Jan 2013 09:26:47 +0000 (09:26 +0000)
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

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

index 1ab577049c6336a1a3ba7894ff95d2fc8d20d215..4de26e2b4ef10a4ae4b93b66b00ecba67f5154d1 100644 (file)
@@ -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;
index 389c58c4288a684575f64c02c183ebf5e07ac678..739bcdd80f9aec09f81952e8078d06f73e87234a 100644 (file)
@@ -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"