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)) { 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{1, 2, 3}]() {};\n"); + verifyFormat("auto f = [b = std::vector{\n" + " 1, 2, 3,\n" + "}]() {};\n"); + verifyFormat("return [b = std::vector()] {}();\n"); + // Not lambdas. verifyFormat("constexpr char hello[]{\"hello\"};"); verifyFormat("double &operator[](int i) { return 0; }\n"