]> granicus.if.org Git - clang/commitdiff
clang-format: Understand more lambda return types.
authorDaniel Jasper <djasper@google.com>
Fri, 21 Nov 2014 14:08:38 +0000 (14:08 +0000)
committerDaniel Jasper <djasper@google.com>
Fri, 21 Nov 2014 14:08:38 +0000 (14:08 +0000)
Before:
  auto a = [&b, c ](D * d) -> D * {}

After:
 auto a = [&b, c](D* d) -> D* {}

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

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

index b376ec95c1b00157145a02fa4b4d381ac88ef5f8..c4d443b2ca02845e3894a57c73337fecd887ce9e 100644 (file)
@@ -747,6 +747,8 @@ private:
       }
     } else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
       Contexts.back().IsExpression = true;
+    } else if (Current.is(TT_TrailingReturnArrow)) {
+      Contexts.back().IsExpression = false;
     } else if (Current.is(tok::l_paren) && !Line.MustBeDeclaration &&
                !Line.InPPDirective &&
                (!Current.Previous ||
@@ -973,7 +975,8 @@ private:
       return TT_UnaryOperator;
 
     const FormatToken *NextToken = Tok.getNextNonComment();
-    if (!NextToken || NextToken->is(tok::l_brace))
+    if (!NextToken ||
+        (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment()))
       return TT_Unknown;
 
     if (PrevToken->is(tok::coloncolon))
index af1e94cfe844a2736dee99322f94874cadaa3d52..2c6a4fb25405d35ed73beb2a17247a5338a6cc74 100644 (file)
@@ -867,6 +867,9 @@ bool UnwrappedLineParser::tryToParseLambda() {
     case tok::l_paren:
       parseParens();
       break;
+    case tok::amp:
+    case tok::star:
+    case tok::kw_const:
     case tok::less:
     case tok::greater:
     case tok::identifier:
index 479919e9cfe65c3c7df95dc3a5202318716329b7..fceebfadea76f382fa81c1942f9007e5812212d7 100644 (file)
@@ -9279,6 +9279,9 @@ TEST_F(FormatTest, FormatsLambdas) {
   verifyFormat("int c = []() -> int { return 2; }();\n");
   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
+  verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
+  verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
+  verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
                "                   int j) -> int {\n"
                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"