Author: Krzysztof Parzyszek Date: 2025-06-30T15:05:49-05:00 New Revision: 4c7d3e9315a55fa8ffe96436fda83c392be242b2
URL: https://github.com/llvm/llvm-project/commit/4c7d3e9315a55fa8ffe96436fda83c392be242b2 DIFF: https://github.com/llvm/llvm-project/commit/4c7d3e9315a55fa8ffe96436fda83c392be242b2.diff LOG: [STLForwardCompat] Implement llvm::type_identity (#146390) A basic implementation until we get it in `std` in C++20. Added: Modified: clang/include/clang/Tooling/Transformer/Transformer.h llvm/include/llvm/ADT/STLForwardCompat.h llvm/unittests/ADT/STLForwardCompatTest.cpp Removed: ################################################################################ diff --git a/clang/include/clang/Tooling/Transformer/Transformer.h b/clang/include/clang/Tooling/Transformer/Transformer.h index 71b1fe81b9518..ecf7c0912d2d8 100644 --- a/clang/include/clang/Tooling/Transformer/Transformer.h +++ b/clang/include/clang/Tooling/Transformer/Transformer.h @@ -42,11 +42,6 @@ class TransformerImpl { virtual void onMatchImpl(const ast_matchers::MatchFinder::MatchResult &Result) = 0; }; - -// FIXME: Use std::type_identity or backport when available. -template <class T> struct type_identity { - using type = T; -}; } // namespace detail template <typename T> struct TransformerResult { @@ -95,8 +90,8 @@ class Transformer : public ast_matchers::MatchFinder::MatchCallback { template <typename MetadataT> explicit Transformer( transformer::RewriteRuleWith<MetadataT> Rule, - std::function<void(llvm::Expected<TransformerResult< - typename detail::type_identity<MetadataT>::type>>)> + std::function<void( + llvm::Expected<TransformerResult<llvm::type_identity_t<MetadataT>>>)> Consumer); /// N.B. Passes `this` pointer to `MatchFinder`. So, this object should not @@ -200,8 +195,8 @@ template <typename T> class WithMetadataImpl final : public TransformerImpl { template <typename MetadataT> Transformer::Transformer( transformer::RewriteRuleWith<MetadataT> Rule, - std::function<void(llvm::Expected<TransformerResult< - typename detail::type_identity<MetadataT>::type>>)> + std::function<void( + llvm::Expected<TransformerResult<llvm::type_identity_t<MetadataT>>>)> Consumer) : Impl(std::make_unique<detail::WithMetadataImpl<MetadataT>>( std::move(Rule), std::move(Consumer))) {} diff --git a/llvm/include/llvm/ADT/STLForwardCompat.h b/llvm/include/llvm/ADT/STLForwardCompat.h index 75a0d4acf67f1..7bd2c8705f393 100644 --- a/llvm/include/llvm/ADT/STLForwardCompat.h +++ b/llvm/include/llvm/ADT/STLForwardCompat.h @@ -36,6 +36,19 @@ template <typename T> using remove_cvref_t // NOLINT(readability-identifier-naming) = typename llvm::remove_cvref<T>::type; +// TODO: Remove this in favor of std::type_identity<T> once we switch to C++23. +template <typename T> +struct type_identity // NOLINT(readability-identifier-naming) +{ + using type = T; +}; + +// TODO: Remove this in favor of std::type_identity_t<T> once we switch to +// C++23. +template <typename T> +using type_identity_t // NOLINT(readability-identifier-naming) + = typename llvm::type_identity<T>::type; + //===----------------------------------------------------------------------===// // Features from C++23 //===----------------------------------------------------------------------===// diff --git a/llvm/unittests/ADT/STLForwardCompatTest.cpp b/llvm/unittests/ADT/STLForwardCompatTest.cpp index b856386aa3e45..e3d500aa7b55a 100644 --- a/llvm/unittests/ADT/STLForwardCompatTest.cpp +++ b/llvm/unittests/ADT/STLForwardCompatTest.cpp @@ -45,6 +45,25 @@ TYPED_TEST(STLForwardCompatRemoveCVRefTest, RemoveCVRefT) { llvm::remove_cvref_t<From>>::value)); } +template <typename T> class TypeIdentityTest : public ::testing::Test { +public: + using TypeIdentity = llvm::type_identity<T>; +}; + +struct A { + struct B {}; +}; +using TypeIdentityTestTypes = + ::testing::Types<int, volatile int, A, const A::B>; + +TYPED_TEST_SUITE(TypeIdentityTest, TypeIdentityTestTypes, /*NameGenerator*/); + +TYPED_TEST(TypeIdentityTest, Identity) { + // TestFixture is the instantiated TypeIdentityTest. + EXPECT_TRUE( + (std::is_same_v<TypeParam, typename TestFixture::TypeIdentity::type>)); +} + TEST(TransformTest, TransformStd) { std::optional<int> A; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits