Skip to content

[STLForwardCompat] Implement llvm::type_identity #146390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions clang/include/clang/Tooling/Transformer/Transformer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))) {}
Expand Down
13 changes: 13 additions & 0 deletions llvm/include/llvm/ADT/STLForwardCompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
//===----------------------------------------------------------------------===//
Expand Down
19 changes: 19 additions & 0 deletions llvm/unittests/ADT/STLForwardCompatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down