]> granicus.if.org Git - clang/commitdiff
Fix parsing of return statements.
authorManuel Klimek <klimek@google.com>
Mon, 21 Jan 2013 10:07:49 +0000 (10:07 +0000)
committerManuel Klimek <klimek@google.com>
Mon, 21 Jan 2013 10:07:49 +0000 (10:07 +0000)
Previously, we would not detect brace initializer lists in return
statements, thus:
 return (a)(b) { 1, 2, 3 };
would put the semicolon onto the next line.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173017 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 393416de029c389b4b0c1984bbb420d32908760b..3fd1ff9024e8a6729ea049557528a416b9e19919 100644 (file)
@@ -308,6 +308,9 @@ void UnwrappedLineParser::parseStructuralElement() {
   case tok::kw_case:
     parseCaseLabel();
     return;
+  case tok::kw_return:
+    parseReturn();
+    return;
   default:
     break;
   }
@@ -380,6 +383,32 @@ void UnwrappedLineParser::parseBracedList() {
   } while (!eof());
 }
 
+void UnwrappedLineParser::parseReturn() {
+  nextToken();
+
+  do {
+    switch (FormatTok.Tok.getKind()) {
+    case tok::l_brace:
+      parseBracedList();
+      break;
+    case tok::l_paren:
+      parseParens();
+      break;
+    case tok::r_brace:
+      // Assume missing ';'.
+      addUnwrappedLine();
+      return;
+    case tok::semi:
+      nextToken();
+      addUnwrappedLine();
+      return;
+    default:
+      nextToken();
+      break;
+    }
+  } while (!eof());
+}
+
 void UnwrappedLineParser::parseParens() {
   assert(FormatTok.Tok.is(tok::l_paren) && "'(' expected.");
   nextToken();
index 010569af7b34348b3d1006e16c7b90bf33f14363..47148ef66d779f2672697e635334ae6827de6574 100644 (file)
@@ -131,6 +131,7 @@ private:
   void parseComments();
   void parseStructuralElement();
   void parseBracedList();
+  void parseReturn();
   void parseParens();
   void parseIfThenElse();
   void parseForOrWhileLoop();
index c9319d59390001297e783429d09e68836b176a41..1d3463ccfd242dedd7d1cf88bb5d7ef6160af3c3 100644 (file)
@@ -1474,6 +1474,10 @@ TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
       "};", getLLVMStyleWithColumns(25));
 }
 
+TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
+  verifyFormat("return (a)(b) { 1, 2, 3 };");
+}
+
 TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
   verifyFormat(
       "Aaa({\n"