]> granicus.if.org Git - clang/commitdiff
Improve clang-format's error recovery.
authorDaniel Jasper <djasper@google.com>
Fri, 31 May 2013 14:56:29 +0000 (14:56 +0000)
committerDaniel Jasper <djasper@google.com>
Fri, 31 May 2013 14:56:29 +0000 (14:56 +0000)
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

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

index 618f54a5184f587195d6c614e30376bfc57bba84..933da66a6229b733bf719b22da008edce825167c 100644 (file)
@@ -45,6 +45,7 @@ public:
     else
       Line.MustBeDeclaration = true;
   }
+
 private:
   UnwrappedLine &Line;
   std::vector<bool> &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;
     }
index 8f6e9d6ad66b9d2b2a977a6829e08f5df4843f36..9e9a5f30d5eea135f1fcbe0fac93603fbcca1ef2 100644 (file)
@@ -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"