[clang] 50acd67 - [clang-format] Don't format typename template parameters as expression
Emilia Dreamer via cfe-commits
cfe-commits at lists.llvm.org
Sat Apr 1 06:48:04 PDT 2023
Author: Emilia Dreamer
Date: 2023-04-01T16:52:51+03:00
New Revision: 50acd67018a53239f11a2889c28f77a1c7dc8c6b
URL: https://github.com/llvm/llvm-project/commit/50acd67018a53239f11a2889c28f77a1c7dc8c6b
DIFF: https://github.com/llvm/llvm-project/commit/50acd67018a53239f11a2889c28f77a1c7dc8c6b.diff
LOG: [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
Added:
Modified:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 1fc190e56c28..b1060bde9ded 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1723,10 +1723,13 @@ class AnnotatingParser {
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 bea85e5bdba2..19b1d96f75ab 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -261,6 +261,15 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
Tokens = annotate("template <typename T, bool B = C && D> struct S {};");
ASSERT_EQ(Tokens.size(), 18u) << Tokens;
EXPECT_TOKEN(Tokens[9], tok::ampamp, TT_BinaryOperator);
+
+ Tokens = annotate("template <typename T, typename U = T&&> struct S {};");
+ ASSERT_EQ(Tokens.size(), 17u) << Tokens;
+ EXPECT_TOKEN(Tokens[9], tok::ampamp, TT_PointerOrReference);
+
+ Tokens = annotate("template <typename T = int (*)(int)> 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) {
More information about the cfe-commits
mailing list