rymiel created this revision. rymiel added a project: clang-format. rymiel added reviewers: HazardyKnusperkeks, owenpan, MyDeveloperDay. Herald added projects: All, clang. Herald added a subscriber: cfe-commits. rymiel requested review of this revision.
bb4f6c4dca98a47054117708015bb2724256ee83 <https://reviews.llvm.org/rGbb4f6c4dca98a47054117708015bb2724256ee83> 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 Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D147318 Files: clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/TokenAnnotatorTest.cpp Index: clang/unittests/Format/TokenAnnotatorTest.cpp =================================================================== --- clang/unittests/Format/TokenAnnotatorTest.cpp +++ clang/unittests/Format/TokenAnnotatorTest.cpp @@ -261,6 +261,15 @@ 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) { Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -1723,10 +1723,13 @@ 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)
Index: clang/unittests/Format/TokenAnnotatorTest.cpp =================================================================== --- clang/unittests/Format/TokenAnnotatorTest.cpp +++ clang/unittests/Format/TokenAnnotatorTest.cpp @@ -261,6 +261,15 @@ 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) { Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -1723,10 +1723,13 @@ 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)
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits