[PATCH] D11693: clang-format: Support generalized lambda captures.

strager strager.nds at gmail.com
Fri Jul 31 13:51:59 PDT 2015


strager created this revision.
strager added a reviewer: djasper.
strager added subscribers: sas, abdulras, cfe-commits.
Herald added a subscriber: klimek.

Teach clang-format about C++14 generalized lambda captures:

    auto f = [b = std::move(d)]() {};

(clang-format would not think there's a lambda here because
of the `=`.)

http://reviews.llvm.org/D11693

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

Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -9981,6 +9981,11 @@
   // More complex introducers.
   verifyFormat("return [i, args...] {};");
 
+  // Lambdas with generalized captures.
+  verifyFormat("auto f = [b = d]() {};\n");
+  verifyFormat("auto f = [b = std::move(d)]() {};\n");
+  verifyFormat("auto f = [b = c, d = e, g]() {};\n");
+
   // Not lambdas.
   verifyFormat("constexpr char hello[]{\"hello\"};");
   verifyFormat("double &operator[](int i) { return 0; }\n"
Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp
+++ lib/Format/UnwrappedLineParser.cpp
@@ -1051,6 +1051,20 @@
     nextToken();
     if (FormatTok->is(tok::ellipsis))
       nextToken();
+    if (FormatTok->is(tok::equal)) {
+      nextToken();
+      while (!eof()) {
+        // FIXME: Once we have an expression parser in the UnwrappedLineParser,
+        // replace this by using parseAssigmentExpression() inside.
+        if (FormatTok->is(tok::l_paren)) {
+          parseParens();
+        } else if (FormatTok->isOneOf(tok::comma, tok::r_square)) {
+          break;
+        } else {
+          nextToken();
+        }
+      }
+    }
     if (FormatTok->is(tok::comma)) {
       nextToken();
     } else if (FormatTok->is(tok::r_square)) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D11693.31160.patch
Type: text/x-patch
Size: 1465 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150731/77bf929f/attachment.bin>


More information about the cfe-commits mailing list