From 7847225576d571fb7101463de56330e1f0e84856 Mon Sep 17 00:00:00 2001 From: Emilia Dreamer Date: Sun, 25 Sep 2022 20:29:34 +0300 Subject: [PATCH] [clang-format] Correctly annotate static and consteval lambdas `P1169` "static operator()" (https://wg21.link/P1169) is accepted to C++23 and while clang itself doesn't exactly support it yet, clang-format could quite easily. This simply allows the keyword `static` to be a part of lambdas as specified by the addition to [expr.prim.lambda.general] While adding this, I noticed `consteval` lambdas also aren't handled, so that keyword is now allowed to be a part of lambdas as well Reviewed By: HazardyKnusperkeks, owenpan, MyDeveloperDay Differential Revision: https://reviews.llvm.org/D134587 --- clang/lib/Format/UnwrappedLineParser.cpp | 2 ++ clang/unittests/Format/TokenAnnotatorTest.cpp | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 4fd4fd4..87a0e43 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -2230,6 +2230,7 @@ bool UnwrappedLineParser::tryToParseLambda() { case tok::star: case tok::kw_const: case tok::kw_constexpr: + case tok::kw_consteval: case tok::comma: case tok::greater: case tok::identifier: @@ -2237,6 +2238,7 @@ bool UnwrappedLineParser::tryToParseLambda() { case tok::coloncolon: case tok::kw_mutable: case tok::kw_noexcept: + case tok::kw_static: nextToken(); break; // Specialization of a template with an integer parameter can contain diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index d27c176..6bc295c 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -837,6 +837,21 @@ TEST_F(TokenAnnotatorTest, UnderstandsLambdas) { EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare); EXPECT_TOKEN(Tokens[5], tok::l_brace, TT_LambdaLBrace); + Tokens = annotate("[]() consteval {}"); + ASSERT_EQ(Tokens.size(), 8u) << Tokens; + EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare); + EXPECT_TOKEN(Tokens[5], tok::l_brace, TT_LambdaLBrace); + + Tokens = annotate("[]() mutable {}"); + ASSERT_EQ(Tokens.size(), 8u) << Tokens; + EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare); + EXPECT_TOKEN(Tokens[5], tok::l_brace, TT_LambdaLBrace); + + Tokens = annotate("[]() static {}"); + ASSERT_EQ(Tokens.size(), 8u) << Tokens; + EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare); + EXPECT_TOKEN(Tokens[5], tok::l_brace, TT_LambdaLBrace); + Tokens = annotate("[]() -> auto {}"); ASSERT_EQ(Tokens.size(), 9u) << Tokens; EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare); -- 2.7.4