Skip to content

Add IsRestrictQualifiedType & GetNonRestrictQualifiedType functions #579

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions include/clang/Interpreter/CppInterOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 GetNonRestrictType(TCppType_t type);

/// check if the type is restrict qualified i.e. __restrict
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.
CPPINTEROP_API TCppType_t GetType(const std::string& type);
Expand Down
15 changes: 15 additions & 0 deletions lib/Interpreter/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1657,6 +1657,21 @@ namespace Cpp {
return QT.getCanonicalType().getAsOpaquePtr();
}

bool IsRestrictType(TCppType_t type) {
QualType QT = QualType::getFromOpaquePtr(type);
return QT.isRestrictQualified();
}

TCppType_t GetNonRestrictType(TCppType_t type) {
QualType QT = QualType::getFromOpaquePtr(type);
if (QT.isRestrictQualified()) {
QualType NonRestrictType(QT);
NonRestrictType.removeLocalRestrict();
return NonRestrictType.getAsOpaquePtr();
}
return type;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make codecov happy here?

Copy link
Contributor

@vgvassilev vgvassilev May 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need a generic interface for the cvr types. Something like:

bool HasTypeQualifier(Quals::Restrict & Quals::Const & Quals::Volatile) { ... }
bool StripTypeQualifier(...) { ... return true;/if something was really stripped/}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. That can be done.

enum Qual { Restrict = 0b01, Const = 0b010, Volatile = 0b0100 };

bool HasTypeQualifier(TCppType_t, enum Qual) {
    ...
}

TCppType RemoveTypeQualifier(TCppType_t, enum Qual) {
    ...
}

The enum can be simply xored to combine different qualifiers.

@vgvassilev, Is this approach good?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, makes sense. The enum should be const, volatile, restrict to follow the CVR terminology.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look at #594

}

// 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.
Expand Down
19 changes: 19 additions & 0 deletions unittests/CppInterOp/TypeReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,3 +610,22 @@ TEST(TypeReflectionTest, IsFunctionPointerType) {
EXPECT_FALSE(
Cpp::IsFunctionPointerType(Cpp::GetVariableType(Cpp::GetNamed("i"))));
}

TEST(TypeReflectionTest, RestrictQualifiedType) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: function 'TEST' can be made static or moved into an anonymous namespace to enforce internal linkage [misc-use-internal-linkage]

Suggested change
TEST(TypeReflectionTest, RestrictQualifiedType) {
TESTstatic (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::IsRestrictType(x));
EXPECT_TRUE(Cpp::IsRestrictType(y));

EXPECT_EQ(Cpp::GetCanonicalType(Cpp::GetNonRestrictType(y)),
Cpp::GetCanonicalType(x));
EXPECT_EQ(Cpp::GetCanonicalType(Cpp::GetNonRestrictType(x)),
Cpp::GetCanonicalType(x));
}
Loading