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

strager via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 16 17:17:00 PDT 2015


strager updated this revision to Diff 34946.
strager marked 2 inline comments as done.
strager added a comment.

Address @djasper's comments.


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
@@ -10057,6 +10057,18 @@
   // 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");
+  verifyFormat("auto f = [b = a[3]]() {};\n");
+  verifyFormat("auto f = [InRange = a < b && b < c]() {};\n");
+  verifyFormat("auto f = [b = std::vector<int>{1, 2, 3}]() {};\n");
+  verifyFormat("auto f = [b = std::vector<int>{\n"
+               "              1, 2, 3,\n"
+               "}]() {};\n");
+  verifyFormat("return [b = std::vector<int[]>()] {}();\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
@@ -1054,6 +1054,27 @@
     nextToken();
     if (FormatTok->is(tok::ellipsis))
       nextToken();
+    if (FormatTok->is(tok::equal)) {
+      // Generalized lambda capture.
+      nextToken();
+      while (!eof()) {
+        // FIXME: Once we have an expression parser in the UnwrappedLineParser,
+        // replace this by using parseAssigmentExpression() inside. See also
+        // parseBracedList. For now, parsing matching braces ([], (), {}) is
+        // good enough.
+        if (FormatTok->is(tok::l_paren)) {
+          parseParens();
+        } else if (FormatTok->is(tok::l_square)) {
+          parseSquare();
+        } else if (FormatTok->is(tok::l_brace)) {
+          parseBracedList(false);
+        } 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)) {
@@ -1110,7 +1131,8 @@
   nextToken();
 
   // FIXME: Once we have an expression parser in the UnwrappedLineParser,
-  // replace this by using parseAssigmentExpression() inside.
+  // replace this by using parseAssigmentExpression() inside. See also
+  // tryToParseLambdaIntroducer.
   do {
     if (Style.Language == FormatStyle::LK_JavaScript) {
       if (FormatTok->is(Keywords.kw_function)) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D11693.34946.patch
Type: text/x-patch
Size: 2549 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150917/0abbcb49/attachment.bin>


More information about the cfe-commits mailing list