From 50acd67018a53239f11a2889c28f77a1c7dc8c6b Mon Sep 17 00:00:00 2001 From: Emilia Dreamer Date: Sat, 1 Apr 2023 16:51:10 +0300 Subject: [PATCH] [clang-format] Don't format typename template parameters as expression bb4f6c4dca98a47054117708015bb2724256ee83 made it so that template parameter defaults are seen akin to assignments and formatted as expressions, however, the patch did this for all template parameters, even for `typename` template parameters. This patch formats `typename` and `class` template parameters as types. Fixes https://github.com/llvm/llvm-project/issues/61841 Reviewed By: HazardyKnusperkeks, owenpan, MyDeveloperDay Differential Revision: https://reviews.llvm.org/D147318 --- clang/lib/Format/TokenAnnotator.cpp | 11 +++++++---- clang/unittests/Format/TokenAnnotatorTest.cpp | 9 +++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 1fc190e..b1060bd 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -1723,10 +1723,13 @@ private: return false; } - // This is the default value of a non-template type parameter, so treat - // it as an expression. - if (Contexts.back().ContextKind == tok::less) - return true; + // This is the default value of a template parameter, determine if it's + // type or non-type. + if (Contexts.back().ContextKind == tok::less) { + assert(Current.Previous->Previous); + return !Current.Previous->Previous->isOneOf(tok::kw_typename, + tok::kw_class); + } Tok = Tok->MatchingParen; if (!Tok) diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index bea85e5..19b1d96 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -261,6 +261,15 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) { Tokens = annotate("template struct S {};"); ASSERT_EQ(Tokens.size(), 18u) << Tokens; EXPECT_TOKEN(Tokens[9], tok::ampamp, TT_BinaryOperator); + + Tokens = annotate("template struct S {};"); + ASSERT_EQ(Tokens.size(), 17u) << Tokens; + EXPECT_TOKEN(Tokens[9], tok::ampamp, TT_PointerOrReference); + + Tokens = annotate("template struct S {};"); + ASSERT_EQ(Tokens.size(), 19u) << Tokens; + EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_FunctionTypeLParen); + EXPECT_TOKEN(Tokens[7], tok::star, TT_PointerOrReference); } TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) { -- 2.7.4