From 8af7b1ad01897fdf74d007c63b3c0ee9c49ad500 Mon Sep 17 00:00:00 2001 From: Andreas Fertig Date: Wed, 12 Apr 2023 10:35:29 +0200 Subject: [PATCH] Fixed #522: Keep the type of the `typeid` expression as written. --- CodeGenerator.cpp | 2 +- tests/Issue522.cpp | 3 +++ tests/Issue522.expect | 5 +++++ tests/TypeIdInTemplateTest.expect | 4 ++-- 4 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 tests/Issue522.cpp create mode 100644 tests/Issue522.expect diff --git a/CodeGenerator.cpp b/CodeGenerator.cpp index 52ecd8fb..02de8670 100644 --- a/CodeGenerator.cpp +++ b/CodeGenerator.cpp @@ -696,7 +696,7 @@ void CodeGenerator::InsertArg(const CXXTypeidExpr* stmt) mOutputFormatHelper.Append(kwTypeId); WrapInParens([&]() { if(stmt->isTypeOperand()) { - mOutputFormatHelper.Append(GetName(stmt->getType())); + mOutputFormatHelper.Append(GetName(stmt->getTypeOperand(const_cast(GetGlobalAST())))); } else { InsertArg(stmt->getExprOperand()); } diff --git a/tests/Issue522.cpp b/tests/Issue522.cpp new file mode 100644 index 00000000..55bc1129 --- /dev/null +++ b/tests/Issue522.cpp @@ -0,0 +1,3 @@ +#include +std::type_info const& tiA = typeid(int); +std::type_info const& tiB = typeid(0); diff --git a/tests/Issue522.expect b/tests/Issue522.expect new file mode 100644 index 00000000..6d7207fd --- /dev/null +++ b/tests/Issue522.expect @@ -0,0 +1,5 @@ +#include +const std::type_info & tiA = typeid(int); + +const std::type_info & tiB = typeid(0); + diff --git a/tests/TypeIdInTemplateTest.expect b/tests/TypeIdInTemplateTest.expect index 580632fe..c0b13eb5 100644 --- a/tests/TypeIdInTemplateTest.expect +++ b/tests/TypeIdInTemplateTest.expect @@ -10,7 +10,7 @@ class Foo public: inline std::basic_string, std::allocator > Get() { - std::basic_string, std::allocator > typeId = std::basic_string, std::allocator >{typeid(const std::type_info).name()} /* NRVO variable */; + std::basic_string, std::allocator > typeId = std::basic_string, std::allocator >{typeid(T).name()} /* NRVO variable */; return std::basic_string, std::allocator >(static_cast, std::allocator > &&>(typeId)); } @@ -25,7 +25,7 @@ class Foo public: inline std::basic_string, std::allocator > Get() { - std::basic_string, std::allocator > typeId = std::basic_string, std::allocator >{typeid(const std::type_info).name()} /* NRVO variable */; + std::basic_string, std::allocator > typeId = std::basic_string, std::allocator >{typeid(int).name()} /* NRVO variable */; return std::basic_string, std::allocator >(static_cast, std::allocator > &&>(typeId)); }