From 480b7fa82c9459513ca446855fc7e99ae3851283 Mon Sep 17 00:00:00 2001 From: Vipul Cariappa Date: Thu, 15 May 2025 11:24:54 +0200 Subject: [PATCH 1/2] Add `IsRestrictQualifiedType` & `GetNonRestrictQualifiedType` functions --- include/clang/Interpreter/CppInterOp.h | 6 ++++++ lib/Interpreter/CppInterOp.cpp | 19 +++++++++++++++++++ unittests/CppInterOp/TypeReflectionTest.cpp | 18 ++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/include/clang/Interpreter/CppInterOp.h b/include/clang/Interpreter/CppInterOp.h index 7d1fdb12a..c3a4b263d 100644 --- a/include/clang/Interpreter/CppInterOp.h +++ b/include/clang/Interpreter/CppInterOp.h @@ -546,6 +546,12 @@ namespace Cpp { /// out of it. CPPINTEROP_API TCppType_t GetCanonicalType(TCppType_t type); + /// Get non restrict qualified version of the given type + CPPINTEROP_API TCppType_t GetNonRestrictQualifiedType(TCppType_t type); + + /// check if the type is restrict qualified i.e. __restrict + CPPINTEROP_API bool IsRestrictQualifiedType(TCppType_t type); + /// Used to either get the built-in type of the provided string, or /// use the name to lookup the actual type. CPPINTEROP_API TCppType_t GetType(const std::string& type); diff --git a/lib/Interpreter/CppInterOp.cpp b/lib/Interpreter/CppInterOp.cpp index 1710b91b6..13025c105 100755 --- a/lib/Interpreter/CppInterOp.cpp +++ b/lib/Interpreter/CppInterOp.cpp @@ -1657,6 +1657,25 @@ namespace Cpp { return QT.getCanonicalType().getAsOpaquePtr(); } + bool IsRestrictQualifiedType(TCppType_t type) { + if (!type) + return 0; + QualType QT = QualType::getFromOpaquePtr(type); + return QT.isRestrictQualified(); + } + + TCppType_t GetNonRestrictQualifiedType(TCppType_t type) { + if (!type) + return 0; + QualType QT = QualType::getFromOpaquePtr(type); + if (QT.isRestrictQualified()) { + QualType NonRestrictType(QT); + NonRestrictType.removeLocalRestrict(); + return NonRestrictType.getAsOpaquePtr(); + } + return nullptr; + } + // Internal functions that are not needed outside the library are // encompassed in an anonymous namespace as follows. This function converts // from a string to the actual type. It is used in the GetType() function. diff --git a/unittests/CppInterOp/TypeReflectionTest.cpp b/unittests/CppInterOp/TypeReflectionTest.cpp index a3e482596..de19be727 100644 --- a/unittests/CppInterOp/TypeReflectionTest.cpp +++ b/unittests/CppInterOp/TypeReflectionTest.cpp @@ -610,3 +610,21 @@ TEST(TypeReflectionTest, IsFunctionPointerType) { EXPECT_FALSE( Cpp::IsFunctionPointerType(Cpp::GetVariableType(Cpp::GetNamed("i")))); } + +TEST(TypeReflectionTest, RestrictQualifiedType) { + Cpp::CreateInterpreter(); + Cpp::Declare(R"( + int *x; + int *__restrict y; + )"); + + Cpp::TCppType_t x = Cpp::GetVariableType(Cpp::GetNamed("x")); + Cpp::TCppType_t y = Cpp::GetVariableType(Cpp::GetNamed("y")); + + EXPECT_FALSE(Cpp::IsRestrictQualifiedType(x)); + EXPECT_TRUE(Cpp::IsRestrictQualifiedType(y)); + + EXPECT_FALSE(Cpp::GetNonRestrictQualifiedType(x)); + EXPECT_EQ(Cpp::GetCanonicalType(Cpp::GetNonRestrictQualifiedType(y)), + Cpp::GetCanonicalType(x)); +} From 73fe9fcc715b26c44dda0357f3688718955936f2 Mon Sep 17 00:00:00 2001 From: Vipul Cariappa Date: Thu, 15 May 2025 13:09:00 +0200 Subject: [PATCH 2/2] changes according to code review --- include/clang/Interpreter/CppInterOp.h | 4 ++-- lib/Interpreter/CppInterOp.cpp | 10 +++------- unittests/CppInterOp/TypeReflectionTest.cpp | 9 +++++---- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/include/clang/Interpreter/CppInterOp.h b/include/clang/Interpreter/CppInterOp.h index c3a4b263d..1b4fbaa22 100644 --- a/include/clang/Interpreter/CppInterOp.h +++ b/include/clang/Interpreter/CppInterOp.h @@ -547,10 +547,10 @@ namespace Cpp { CPPINTEROP_API TCppType_t GetCanonicalType(TCppType_t type); /// Get non restrict qualified version of the given type - CPPINTEROP_API TCppType_t GetNonRestrictQualifiedType(TCppType_t type); + CPPINTEROP_API TCppType_t GetNonRestrictType(TCppType_t type); /// check if the type is restrict qualified i.e. __restrict - CPPINTEROP_API bool IsRestrictQualifiedType(TCppType_t type); + CPPINTEROP_API bool IsRestrictType(TCppType_t type); /// Used to either get the built-in type of the provided string, or /// use the name to lookup the actual type. diff --git a/lib/Interpreter/CppInterOp.cpp b/lib/Interpreter/CppInterOp.cpp index 13025c105..723ad8fe9 100755 --- a/lib/Interpreter/CppInterOp.cpp +++ b/lib/Interpreter/CppInterOp.cpp @@ -1657,23 +1657,19 @@ namespace Cpp { return QT.getCanonicalType().getAsOpaquePtr(); } - bool IsRestrictQualifiedType(TCppType_t type) { - if (!type) - return 0; + bool IsRestrictType(TCppType_t type) { QualType QT = QualType::getFromOpaquePtr(type); return QT.isRestrictQualified(); } - TCppType_t GetNonRestrictQualifiedType(TCppType_t type) { - if (!type) - return 0; + TCppType_t GetNonRestrictType(TCppType_t type) { QualType QT = QualType::getFromOpaquePtr(type); if (QT.isRestrictQualified()) { QualType NonRestrictType(QT); NonRestrictType.removeLocalRestrict(); return NonRestrictType.getAsOpaquePtr(); } - return nullptr; + return type; } // Internal functions that are not needed outside the library are diff --git a/unittests/CppInterOp/TypeReflectionTest.cpp b/unittests/CppInterOp/TypeReflectionTest.cpp index de19be727..5ffb18fbd 100644 --- a/unittests/CppInterOp/TypeReflectionTest.cpp +++ b/unittests/CppInterOp/TypeReflectionTest.cpp @@ -621,10 +621,11 @@ TEST(TypeReflectionTest, RestrictQualifiedType) { Cpp::TCppType_t x = Cpp::GetVariableType(Cpp::GetNamed("x")); Cpp::TCppType_t y = Cpp::GetVariableType(Cpp::GetNamed("y")); - EXPECT_FALSE(Cpp::IsRestrictQualifiedType(x)); - EXPECT_TRUE(Cpp::IsRestrictQualifiedType(y)); + EXPECT_FALSE(Cpp::IsRestrictType(x)); + EXPECT_TRUE(Cpp::IsRestrictType(y)); - EXPECT_FALSE(Cpp::GetNonRestrictQualifiedType(x)); - EXPECT_EQ(Cpp::GetCanonicalType(Cpp::GetNonRestrictQualifiedType(y)), + EXPECT_EQ(Cpp::GetCanonicalType(Cpp::GetNonRestrictType(y)), + Cpp::GetCanonicalType(x)); + EXPECT_EQ(Cpp::GetCanonicalType(Cpp::GetNonRestrictType(x)), Cpp::GetCanonicalType(x)); }