diff options
author | Marek Kurdej <marek.kurdej+llvm.org@gmail.com> | 2022-01-26 16:00:26 +0100 |
---|---|---|
committer | Marek Kurdej <marek.kurdej+llvm.org@gmail.com> | 2022-01-26 16:10:52 +0100 |
commit | 93948c5299d7ee446aa707221751a0af2b87c12b (patch) | |
tree | 9b117d97f7e008fb29ab63981e500fffc178cc99 | |
parent | b777d354f6703afd58b1803fdfb6d43a607d8fd6 (diff) | |
download | llvm-93948c5299d7ee446aa707221751a0af2b87c12b.zip llvm-93948c5299d7ee446aa707221751a0af2b87c12b.tar.gz llvm-93948c5299d7ee446aa707221751a0af2b87c12b.tar.bz2 |
[clang-format] Correctly format lambdas with variadic template parameters.
Fixes https://github.com/llvm/llvm-project/issues/53405.
Reviewed By: MyDeveloperDay, owenpan
Differential Revision: https://reviews.llvm.org/D118220
-rw-r--r-- | clang/lib/Format/UnwrappedLineParser.cpp | 14 | ||||
-rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 30 |
2 files changed, 40 insertions, 4 deletions
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index ff3e791..b0588d9 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -1856,6 +1856,7 @@ bool UnwrappedLineParser::tryToParseLambda() { return false; bool SeenArrow = false; + bool InTemplateParameterList = false; while (FormatTok->isNot(tok::l_brace)) { if (FormatTok->isSimpleTypeSpecifier()) { @@ -1871,6 +1872,14 @@ bool UnwrappedLineParser::tryToParseLambda() { case tok::l_square: parseSquare(); break; + case tok::kw_class: + case tok::kw_template: + case tok::kw_typename: + assert(FormatTok->Previous); + if (FormatTok->Previous->is(tok::less)) + InTemplateParameterList = true; + nextToken(); + break; case tok::amp: case tok::star: case tok::kw_const: @@ -1880,11 +1889,8 @@ bool UnwrappedLineParser::tryToParseLambda() { case tok::identifier: case tok::numeric_constant: case tok::coloncolon: - case tok::kw_class: case tok::kw_mutable: case tok::kw_noexcept: - case tok::kw_template: - case tok::kw_typename: nextToken(); break; // Specialization of a template with an integer parameter can contain @@ -1921,7 +1927,7 @@ bool UnwrappedLineParser::tryToParseLambda() { case tok::ellipsis: case tok::kw_true: case tok::kw_false: - if (SeenArrow) { + if (SeenArrow || InTemplateParameterList) { nextToken(); break; } diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index c45869f..9185de0 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -20560,6 +20560,36 @@ TEST_F(FormatTest, FormatsLambdas) { // Lambdas with explicit template argument lists. verifyFormat( "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n"); + verifyFormat("auto L = []<class T>(T) {\n" + " {\n" + " f();\n" + " g();\n" + " }\n" + "};\n"); + verifyFormat("auto L = []<class... T>(T...) {\n" + " {\n" + " f();\n" + " g();\n" + " }\n" + "};\n"); + verifyFormat("auto L = []<typename... T>(T...) {\n" + " {\n" + " f();\n" + " g();\n" + " }\n" + "};\n"); + verifyFormat("auto L = []<template <typename...> class T>(T...) {\n" + " {\n" + " f();\n" + " g();\n" + " }\n" + "};\n"); + verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n" + " {\n" + " f();\n" + " g();\n" + " }\n" + "};\n"); // Multiple lambdas in the same parentheses change indentation rules. These // lambdas are forced to start on new lines. |