Skip to content

Commit 294da5b

Browse files
Add Is/Get Pointer & Reference functions (#510)
Add IsPointerType, GetPointerType, IsReferenceType & GetNonReferenceType functions
1 parent 2b184ce commit 294da5b

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

include/clang/Interpreter/CppInterOp.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,18 @@ namespace Cpp {
504504
/// Checks if the provided parameter is a Plain Old Data Type (POD).
505505
CPPINTEROP_API bool IsPODType(TCppType_t type);
506506

507+
/// Checks if type is a pointer
508+
CPPINTEROP_API bool IsPointerType(TCppType_t type);
509+
510+
/// Get the underlying pointee type
511+
CPPINTEROP_API TCppType_t GetPointeeType(TCppType_t type);
512+
513+
/// Checks if type is a reference
514+
CPPINTEROP_API bool IsReferenceType(TCppType_t type);
515+
516+
/// Get the type that the reference refers to
517+
CPPINTEROP_API TCppType_t GetNonReferenceType(TCppType_t type);
518+
507519
/// Gets the pure, Underlying Type (as opposed to the Using Type).
508520
CPPINTEROP_API TCppType_t GetUnderlyingType(TCppType_t type);
509521

lib/Interpreter/CppInterOp.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,6 +1493,30 @@ namespace Cpp {
14931493
return QT.isPODType(getASTContext());
14941494
}
14951495

1496+
bool IsPointerType(TCppType_t type) {
1497+
QualType QT = QualType::getFromOpaquePtr(type);
1498+
return QT->isPointerType();
1499+
}
1500+
1501+
TCppType_t GetPointeeType(TCppType_t type) {
1502+
if (!IsPointerType(type))
1503+
return nullptr;
1504+
QualType QT = QualType::getFromOpaquePtr(type);
1505+
return QT->getPointeeType().getAsOpaquePtr();
1506+
}
1507+
1508+
bool IsReferenceType(TCppType_t type) {
1509+
QualType QT = QualType::getFromOpaquePtr(type);
1510+
return QT->isReferenceType();
1511+
}
1512+
1513+
TCppType_t GetNonReferenceType(TCppType_t type) {
1514+
if (!IsReferenceType(type))
1515+
return nullptr;
1516+
QualType QT = QualType::getFromOpaquePtr(type);
1517+
return QT.getNonReferenceType().getAsOpaquePtr();
1518+
}
1519+
14961520
TCppType_t GetUnderlyingType(TCppType_t type)
14971521
{
14981522
QualType QT = QualType::getFromOpaquePtr(type);

unittests/CppInterOp/VariableReflectionTest.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,3 +600,67 @@ TEST(VariableReflectionTest, GetEnumConstantDatamembers) {
600600
Cpp::GetEnumConstantDatamembers(MyEnumClass, datamembers2, false);
601601
EXPECT_EQ(datamembers2.size(), 6);
602602
}
603+
604+
TEST(VariableReflectionTest, Is_Get_Pointer) {
605+
Cpp::CreateInterpreter();
606+
std::vector<Decl*> Decls;
607+
std::string code = R"(
608+
class A {};
609+
int a;
610+
int *b;
611+
double c;
612+
double *d;
613+
A e;
614+
A *f;
615+
)";
616+
617+
GetAllTopLevelDecls(code, Decls);
618+
619+
EXPECT_FALSE(Cpp::IsPointerType(Cpp::GetVariableType(Decls[1])));
620+
EXPECT_TRUE(Cpp::IsPointerType(Cpp::GetVariableType(Decls[2])));
621+
EXPECT_FALSE(Cpp::IsPointerType(Cpp::GetVariableType(Decls[3])));
622+
EXPECT_TRUE(Cpp::IsPointerType(Cpp::GetVariableType(Decls[4])));
623+
EXPECT_FALSE(Cpp::IsPointerType(Cpp::GetVariableType(Decls[5])));
624+
EXPECT_TRUE(Cpp::IsPointerType(Cpp::GetVariableType(Decls[6])));
625+
626+
EXPECT_EQ(Cpp::GetPointeeType(Cpp::GetVariableType(Decls[2])),
627+
Cpp::GetVariableType(Decls[1]));
628+
EXPECT_EQ(Cpp::GetPointeeType(Cpp::GetVariableType(Decls[4])),
629+
Cpp::GetVariableType(Decls[3]));
630+
EXPECT_EQ(Cpp::GetPointeeType(Cpp::GetVariableType(Decls[6])),
631+
Cpp::GetVariableType(Decls[5]));
632+
633+
EXPECT_FALSE(Cpp::GetPointeeType(Cpp::GetVariableType(Decls[5])));
634+
}
635+
636+
TEST(VariableReflectionTest, Is_Get_Reference) {
637+
Cpp::CreateInterpreter();
638+
std::vector<Decl*> Decls;
639+
std::string code = R"(
640+
class A {};
641+
int a;
642+
int &b = a;
643+
double c;
644+
double &d = c;
645+
A e;
646+
A &f = e;
647+
)";
648+
649+
GetAllTopLevelDecls(code, Decls);
650+
651+
EXPECT_FALSE(Cpp::IsReferenceType(Cpp::GetVariableType(Decls[1])));
652+
EXPECT_TRUE(Cpp::IsReferenceType(Cpp::GetVariableType(Decls[2])));
653+
EXPECT_FALSE(Cpp::IsReferenceType(Cpp::GetVariableType(Decls[3])));
654+
EXPECT_TRUE(Cpp::IsReferenceType(Cpp::GetVariableType(Decls[4])));
655+
EXPECT_FALSE(Cpp::IsReferenceType(Cpp::GetVariableType(Decls[5])));
656+
EXPECT_TRUE(Cpp::IsReferenceType(Cpp::GetVariableType(Decls[6])));
657+
658+
EXPECT_EQ(Cpp::GetNonReferenceType(Cpp::GetVariableType(Decls[2])),
659+
Cpp::GetVariableType(Decls[1]));
660+
EXPECT_EQ(Cpp::GetNonReferenceType(Cpp::GetVariableType(Decls[4])),
661+
Cpp::GetVariableType(Decls[3]));
662+
EXPECT_EQ(Cpp::GetNonReferenceType(Cpp::GetVariableType(Decls[6])),
663+
Cpp::GetVariableType(Decls[5]));
664+
665+
EXPECT_FALSE(Cpp::GetNonReferenceType(Cpp::GetVariableType(Decls[5])));
666+
}

0 commit comments

Comments
 (0)