Skip to content

Commit

Permalink
Adding edge tests for API's
Browse files Browse the repository at this point in the history
  • Loading branch information
Smit1603 committed Oct 27, 2023
1 parent 4f39c2d commit eed88b0
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ jobs:
# capture coverage info
vers="${CC#*-}"
lcov --directory build/ --capture --output-file coverage.info --gcov-tool /usr/bin/gcov-${vers}
lcov --remove coverage.info '/usr/*' "${HOME}"'/.cache/*' ${{ github.workspace }}'/llvm-project/*' --output-file coverage.info
lcov --remove coverage.info '/usr/*' "${HOME}"'/.cache/*' ${{ github.workspace }}'/llvm-project/*' ${{ github.workspace }}'/unittests/*' --output-file coverage.info
# output coverage data for debugging (optional)
lcov --list coverage.info
Expand Down
2 changes: 1 addition & 1 deletion include/clang/Interpreter/CppInterOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ namespace Cpp {
bool IsDestructor(TCppConstFunction_t method);

/// Checks if the provided parameter is a 'Static' method.
bool IsStaticMethod(TCppFunction_t method);
bool IsStaticMethod(TCppConstFunction_t method);

/// Gets the address of the function to be able to call it.
TCppFuncAddr_t GetFunctionAddress(TCppFunction_t method);
Expand Down
11 changes: 5 additions & 6 deletions lib/Interpreter/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ namespace Cpp {
Valid &= (bool)args.m_Args;
}
if (!Cpp::IsConstructor(m_FD) && !Cpp::IsDestructor(m_FD) &&
Cpp::IsMethod(m_FD)) {
Cpp::IsMethod(m_FD) && !Cpp::IsStaticMethod(m_FD)) {
assert(self && "Must pass the pointer to object");
Valid &= (bool)self;
}
Expand Down Expand Up @@ -406,8 +406,8 @@ namespace Cpp {
auto &C = getSema().getASTContext();
auto *D = (Decl *) klass;

if (auto *ND = llvm::dyn_cast_or_null<NamedDecl>(D)) {
if (auto *TD = llvm::dyn_cast<TagDecl>(ND)) {
if (auto* ND = llvm::dyn_cast_or_null<NamedDecl>(D)) {
if (auto* TD = llvm::dyn_cast<TagDecl>(ND)) {
std::string type_name;
QualType QT = C.getTagDeclType(TD);
PrintingPolicy Policy = C.getPrintingPolicy();
Expand Down Expand Up @@ -970,9 +970,8 @@ namespace Cpp {
return llvm::isa_and_nonnull<CXXDestructorDecl>(D);
}

bool IsStaticMethod(TCppFunction_t method)
{
auto *D = (Decl *) method;
bool IsStaticMethod(TCppConstFunction_t method) {
const auto* D = (const Decl*)method;
if (auto *CXXMD = llvm::dyn_cast_or_null<CXXMethodDecl>(D)) {
return CXXMD->isStatic();
}
Expand Down
38 changes: 38 additions & 0 deletions unittests/CppInterOp/FunctionReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,16 @@ TEST(FunctionReflectionTest, IsTemplatedFunction) {
template<typename T>
void f2(T a) {}
};
class ABC {};
)";

GetAllTopLevelDecls(code, Decls);
GetAllSubDecls(Decls[2], SubDeclsC1);

EXPECT_FALSE(Cpp::IsTemplatedFunction(Decls[0]));
EXPECT_TRUE(Cpp::IsTemplatedFunction(Decls[1]));
EXPECT_FALSE(Cpp::IsTemplatedFunction(Decls[3]));
EXPECT_FALSE(Cpp::IsTemplatedFunction(SubDeclsC1[1]));
EXPECT_TRUE(Cpp::IsTemplatedFunction(SubDeclsC1[2]));
}
Expand All @@ -396,11 +399,14 @@ TEST(FunctionReflectionTest, ExistsFunctionTemplate) {
template<typename T>
void f(T a) {}
};
void f(char ch) {}
)";

GetAllTopLevelDecls(code, Decls);
EXPECT_TRUE(Cpp::ExistsFunctionTemplate("f", 0));
EXPECT_TRUE(Cpp::ExistsFunctionTemplate("f", Decls[1]));
EXPECT_FALSE(Cpp::ExistsFunctionTemplate("f", Decls[2]));
}

TEST(FunctionReflectionTest, IsPublicMethod) {
Expand All @@ -415,6 +421,7 @@ TEST(FunctionReflectionTest, IsPublicMethod) {
void pri_f() {}
protected:
void pro_f() {}
int a;
};
)";

Expand All @@ -426,6 +433,7 @@ TEST(FunctionReflectionTest, IsPublicMethod) {
EXPECT_TRUE(Cpp::IsPublicMethod(SubDecls[4]));
EXPECT_FALSE(Cpp::IsPublicMethod(SubDecls[6]));
EXPECT_FALSE(Cpp::IsPublicMethod(SubDecls[8]));
EXPECT_FALSE(Cpp::IsPublicMethod(SubDecls[9]));
}

TEST(FunctionReflectionTest, IsProtectedMethod) {
Expand Down Expand Up @@ -540,6 +548,7 @@ TEST(FunctionReflectionTest, IsStaticMethod) {
GetAllTopLevelDecls(code, Decls);
GetAllSubDecls(Decls[0], SubDecls);

EXPECT_FALSE(Cpp::IsStaticMethod(Decls[0]));
EXPECT_FALSE(Cpp::IsStaticMethod(SubDecls[1]));
EXPECT_TRUE(Cpp::IsStaticMethod(SubDecls[2]));
}
Expand Down Expand Up @@ -584,6 +593,7 @@ TEST(FunctionReflectionTest, IsVirtualMethod) {
EXPECT_TRUE(Cpp::IsVirtualMethod(SubDecls[2]));
EXPECT_EQ(Cpp::GetName(SubDecls[3]), "y");
EXPECT_FALSE(Cpp::IsVirtualMethod(SubDecls[3])); // y()
EXPECT_FALSE(Cpp::IsVirtualMethod(Decls[0]));
}

#ifdef __APPLE__
Expand Down Expand Up @@ -695,6 +705,34 @@ TEST(FunctionReflectionTest, GetFunctionCallWrapper) {
FCI_Dtor.Invoke(object);
output = testing::internal::GetCapturedStdout();
EXPECT_EQ(output, "Dtor Called\n");

std::vector<Decl*> Decls1;
std::string code1 = R"(
template<typename T>
struct S {
static T Add(T a, T b) { return a + b; }
};
)";

GetAllTopLevelDecls(code1, Decls1);
ASTContext& C = Interp->getCI()->getASTContext();

std::vector<Cpp::TemplateArgInfo> argument = {C.IntTy.getAsOpaquePtr()};
auto Instance1 = Cpp::InstantiateClassTemplate(Decls1[0], argument.data(),
/*type_size*/ argument.size());
EXPECT_TRUE(isa<ClassTemplateSpecializationDecl>((Decl*)Instance1));
auto* CTSD1 = static_cast<ClassTemplateSpecializationDecl*>(Instance1);
auto* Add_D = Cpp::GetNamed("Add",CTSD1);
Cpp::JitCall FCI_Add = Cpp::MakeFunctionCallable(Add_D);
EXPECT_TRUE(FCI_Add.getKind() == Cpp::JitCall::kGenericCall);

int a = 5, b = 10, result;
void* args[2] = {(void*)&a, (void*)&b};

FCI_Add.Invoke(&result, {args, /*args_size=*/2});
EXPECT_EQ(result, a + b);
}

TEST(FunctionReflectionTest, IsConstMethod) {
Expand Down
20 changes: 19 additions & 1 deletion unittests/CppInterOp/ScopeReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ TEST(ScopeReflectionTest, GetName) {
EXPECT_EQ(Cpp::GetName(Decls[5]), "U");
EXPECT_EQ(Cpp::GetName(Decls[6]), "Size4");
EXPECT_EQ(Cpp::GetName(Decls[7]), "Size16");
EXPECT_EQ(Cpp::GetName(nullptr), "<unnamed>");
}

TEST(ScopeReflectionTest, GetCompleteName) {
Expand Down Expand Up @@ -282,6 +283,7 @@ TEST(ScopeReflectionTest, GetCompleteName) {
Cpp::GetVariableType(
Decls[9]))), "A<int>");
EXPECT_EQ(Cpp::GetCompleteName(Decls[10]), "(unnamed)");
EXPECT_EQ(Cpp::GetCompleteName(nullptr), "<unnamed>");
}

TEST(ScopeReflectionTest, GetQualifiedName) {
Expand Down Expand Up @@ -330,7 +332,7 @@ TEST(ScopeReflectionTest, GetQualifiedCompleteName) {
}

TEST(ScopeReflectionTest, GetUsingNamespaces) {
std::vector<Decl *> Decls;
std::vector<Decl *> Decls, Decls1;
std::string code = R"(
namespace abc {
Expand All @@ -351,6 +353,15 @@ TEST(ScopeReflectionTest, GetUsingNamespaces) {
//EXPECT_EQ(Cpp::GetName(usingNamespaces[0]), "runtime");
EXPECT_EQ(Cpp::GetName(usingNamespaces[usingNamespaces.size()-2]), "std");
EXPECT_EQ(Cpp::GetName(usingNamespaces[usingNamespaces.size()-1]), "abc");

std::string code1 = R"(
int x;
)";

GetAllTopLevelDecls(code1, Decls1);
std::vector<void*> usingNamespaces1;
usingNamespaces1 = Cpp::GetUsingNamespaces(Decls1[0]);
EXPECT_EQ(usingNamespaces1.size(), 0);
}

TEST(ScopeReflectionTest, GetGlobalScope) {
Expand Down Expand Up @@ -394,11 +405,13 @@ TEST(ScopeReflectionTest, GetScope) {
Cpp::TCppScope_t ns_N = Cpp::GetScope("N", 0);
Cpp::TCppScope_t cl_C = Cpp::GetScope("C", ns_N);
Cpp::TCppScope_t td_T = Cpp::GetScope("T", 0);
Cpp::TCppScope_t non_existent = Cpp::GetScope("sum", 0);

EXPECT_EQ(Cpp::GetQualifiedName(tu), "");
EXPECT_EQ(Cpp::GetQualifiedName(ns_N), "N");
EXPECT_EQ(Cpp::GetQualifiedName(cl_C), "N::C");
EXPECT_EQ(Cpp::GetQualifiedName(td_T), "T");
EXPECT_EQ(Cpp::GetQualifiedName(non_existent), "<unnamed>");
}

TEST(ScopeReflectionTest, GetScopefromCompleteName) {
Expand Down Expand Up @@ -513,6 +526,8 @@ TEST(ScopeReflectionTest, GetScopeFromType) {
N::T t;
N::E e;
N::C myFunc();
)";

GetAllTopLevelDecls(code, Decls);
Expand All @@ -521,6 +536,7 @@ TEST(ScopeReflectionTest, GetScopeFromType) {
QualType QT3 = llvm::dyn_cast<VarDecl>(Decls[3])->getType();
QualType QT4 = llvm::dyn_cast<VarDecl>(Decls[4])->getType();
QualType QT5 = llvm::dyn_cast<VarDecl>(Decls[5])->getType();
QualType QT6 = llvm::dyn_cast<FunctionDecl>(Decls[6])->getReturnType();
EXPECT_EQ(Cpp::GetQualifiedName(Cpp::GetScopeFromType(QT1.getAsOpaquePtr())),
"N::C");
EXPECT_EQ(Cpp::GetQualifiedName(Cpp::GetScopeFromType(QT2.getAsOpaquePtr())),
Expand All @@ -531,6 +547,8 @@ TEST(ScopeReflectionTest, GetScopeFromType) {
"N::C");
EXPECT_EQ(Cpp::GetQualifiedName(Cpp::GetScopeFromType(QT5.getAsOpaquePtr())),
"N::E");
EXPECT_EQ(Cpp::GetQualifiedName(Cpp::GetScopeFromType(QT6.getAsOpaquePtr())),
"N::C");
}

TEST(ScopeReflectionTest, GetNumBases) {
Expand Down
3 changes: 3 additions & 0 deletions unittests/CppInterOp/TypeReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,15 @@ TEST(TypeReflectionTest, GetTypeFromScope) {
std::string code = R"(
class C {};
struct S {};
int a = 10;
)";

GetAllTopLevelDecls(code, Decls);

EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetTypeFromScope(Decls[0])), "C");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetTypeFromScope(Decls[1])), "S");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetTypeFromScope(Decls[2])), "int");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetTypeFromScope(nullptr)), "NULL TYPE");
}

TEST(TypeReflectionTest, IsTypeDerivedFrom) {
Expand Down
7 changes: 7 additions & 0 deletions unittests/CppInterOp/VariableReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ TEST(VariableReflectionTest, GetDatamembers) {
int e;
static int f;
};
void sum(int,int);
)";

GetAllTopLevelDecls(code, Decls);
auto datamembers = Cpp::GetDatamembers(Decls[0]);
auto datamembers1 = Cpp::GetDatamembers(Decls[1]);

EXPECT_EQ(Cpp::GetQualifiedName(datamembers[0]), "C::a");
EXPECT_EQ(Cpp::GetQualifiedName(datamembers[1]), "C::c");
EXPECT_EQ(Cpp::GetQualifiedName(datamembers[2]), "C::e");
EXPECT_EQ(datamembers.size(), 3);
EXPECT_EQ(datamembers1.size(), 0);
}

TEST(VariableReflectionTest, LookupDatamember) {
Expand All @@ -57,6 +60,7 @@ TEST(VariableReflectionTest, LookupDatamember) {
EXPECT_EQ(Cpp::GetQualifiedName(Cpp::LookupDatamember("a", Decls[0])), "C::a");
EXPECT_EQ(Cpp::GetQualifiedName(Cpp::LookupDatamember("c", Decls[0])), "C::c");
EXPECT_EQ(Cpp::GetQualifiedName(Cpp::LookupDatamember("e", Decls[0])), "C::e");
EXPECT_EQ(Cpp::GetQualifiedName(Cpp::LookupDatamember("k", Decls[0])), "<unnamed>");
}

TEST(VariableReflectionTest, GetVariableType) {
Expand Down Expand Up @@ -138,6 +142,7 @@ TEST(VariableReflectionTest, IsPublicVariable) {
int b;
protected:
int c;
int sum(int,int);
};
)";

Expand All @@ -147,6 +152,7 @@ TEST(VariableReflectionTest, IsPublicVariable) {
EXPECT_TRUE(Cpp::IsPublicVariable(SubDecls[2]));
EXPECT_FALSE(Cpp::IsPublicVariable(SubDecls[4]));
EXPECT_FALSE(Cpp::IsPublicVariable(SubDecls[6]));
EXPECT_FALSE(Cpp::IsPublicVariable(SubDecls[7]));
}

TEST(VariableReflectionTest, IsProtectedVariable) {
Expand Down Expand Up @@ -219,6 +225,7 @@ TEST(VariableReflectionTest, IsConstVariable) {
GetAllTopLevelDecls(code, Decls);
GetAllSubDecls(Decls[0], SubDecls);

EXPECT_FALSE(Cpp::IsConstVariable(Decls[0]));
EXPECT_FALSE(Cpp::IsConstVariable(SubDecls[1]));
EXPECT_TRUE(Cpp::IsConstVariable(SubDecls[2]));
}
Expand Down

0 comments on commit eed88b0

Please sign in to comment.