Skip to content
Merged
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
16 changes: 15 additions & 1 deletion include/clang/Interpreter/CppInterOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -416,13 +416,27 @@ namespace Cpp {
CPPINTEROP_API bool ExistsFunctionTemplate(const std::string& name,
TCppScope_t parent = nullptr);

/// Sets a list of all the constructor for a scope/class that is
/// supplied as a parameter.
///\param[in] name - This string is used as a constraint, that clients can use
/// to ensure the constructors match the name that they provide
///\param[in] parent - Pointer to the scope/class for which the constructors
/// are being looked up
/// to be retrieved
///\param[out] funcs - vector of handles to all constructors found under the
/// given scope
CPPINTEROP_API void LookupConstructors(const std::string& name,
TCppScope_t parent,
std::vector<TCppFunction_t>& funcs);

/// Sets a list of all the Templated Methods that are in the Class that is
/// supplied as a parameter.
///\returns true if the lookup succeeded, and false if there are no candidates
///\param[in] name - method name
///\param[in] parent - Pointer to the scope/class under which the methods have
/// to be retrieved
///\param[out] funcs - vector of function pointers matching the name
CPPINTEROP_API void
CPPINTEROP_API bool
GetClassTemplatedMethods(const std::string& name, TCppScope_t parent,
std::vector<TCppFunction_t>& funcs);

Expand Down
76 changes: 57 additions & 19 deletions lib/Interpreter/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -988,9 +988,10 @@
// encompassed in an anonymous namespace as follows.
namespace {
bool IsTemplatedFunction(Decl *D) {
if (llvm::isa_and_nonnull<FunctionTemplateDecl>(D))
return true;
return llvm::isa_and_nonnull<FunctionTemplateDecl>(D);
}

bool IsTemplateInstantiationOrSpecialization(Decl* D) {
if (auto *FD = llvm::dyn_cast_or_null<FunctionDecl>(D)) {
auto TK = FD->getTemplatedKind();
return TK == FunctionDecl::TemplatedKind::
Expand All @@ -1013,9 +1014,12 @@
bool IsTemplatedFunction(TCppFunction_t func)
{
auto *D = (Decl *) func;
return IsTemplatedFunction(D);
return IsTemplatedFunction(D) || IsTemplateInstantiationOrSpecialization(D);
}

// FIXME: This lookup is broken, and should no longer be used in favour of
// `GetClassTemplatedMethods` If the candidate set returned is =1, that means
// the template function exists and >1 means overloads
bool ExistsFunctionTemplate(const std::string& name,
TCppScope_t parent)
{
Expand All @@ -1031,38 +1035,70 @@
return false;

if ((intptr_t) ND != (intptr_t) -1)
return IsTemplatedFunction(ND);
return IsTemplatedFunction(ND) ||
IsTemplateInstantiationOrSpecialization(ND);

Check warning on line 1039 in lib/Interpreter/CppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CppInterOp.cpp#L1038-L1039

Added lines #L1038 - L1039 were not covered by tests

// FIXME: Cycle through the Decls and check if there is a templated function
return true;
}

void GetClassTemplatedMethods(const std::string& name, TCppScope_t parent,
std::vector<TCppFunction_t>& funcs) {

// Looks up all constructors in the current DeclContext
void LookupConstructors(const std::string& name, TCppScope_t parent,
std::vector<TCppFunction_t>& funcs) {
auto* D = (Decl*)parent;

if (!parent || name.empty())
return;
if (auto* CXXRD = llvm::dyn_cast_or_null<CXXRecordDecl>(D)) {
getSema().ForceDeclarationOfImplicitMembers(CXXRD);
DeclContextLookupResult Result = getSema().LookupConstructors(CXXRD);
// Obtaining all constructors when we intend to lookup a method under a
// scope can lead to crashes. We avoid that by accumulating constructors
// only if the Decl matches the lookup name.
for (auto* i : Result)
if (GetName(i) == name)
funcs.push_back(i);
}
}

D = GetUnderlyingScope(D);
bool GetClassTemplatedMethods(const std::string& name, TCppScope_t parent,
std::vector<TCppFunction_t>& funcs) {
auto* D = (Decl*)parent;
if (!D && name.empty())
return false;

Check warning on line 1066 in lib/Interpreter/CppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CppInterOp.cpp#L1066

Added line #L1066 was not covered by tests

llvm::StringRef Name(name);
// Accumulate constructors
LookupConstructors(name, parent, funcs);
auto& S = getSema();
D = GetUnderlyingScope(D);
llvm::StringRef Name(name);
DeclarationName DName = &getASTContext().Idents.get(name);
clang::LookupResult R(S, DName, SourceLocation(), Sema::LookupOrdinaryName,
For_Visible_Redeclaration);
auto* DC = clang::Decl::castToDeclContext(D);
Cpp_utils::Lookup::Named(&S, R, DC);

Cpp_utils::Lookup::Named(&S, R, Decl::castToDeclContext(D));

if (R.empty())
return;
if (R.getResultKind() == clang::LookupResult::NotFound && funcs.empty())
return false;

Check warning on line 1080 in lib/Interpreter/CppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CppInterOp.cpp#L1080

Added line #L1080 was not covered by tests

R.resolveKind();
// Distinct match, single Decl
else if (R.getResultKind() == clang::LookupResult::Found) {
if (IsTemplatedFunction(R.getFoundDecl()))
funcs.push_back(R.getFoundDecl());

Check warning on line 1085 in lib/Interpreter/CppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CppInterOp.cpp#L1085

Added line #L1085 was not covered by tests
}
// Loop over overload set
else if (R.getResultKind() == clang::LookupResult::FoundOverloaded) {
for (auto* Found : R)
if (IsTemplatedFunction(Found))
funcs.push_back(Found);
}

for (auto* Found : R)
if (llvm::isa<FunctionTemplateDecl>(Found))
funcs.push_back(Found);
// TODO: Handle ambiguously found LookupResult
// else if (R.getResultKind() == clang::LookupResult::Ambiguous) {
// auto kind = R.getAmbiguityKind();
// ...
// Produce a diagnostic describing the ambiguity that resulted
// from name lookup as done in Sema::DiagnoseAmbiguousLookup
//
return !funcs.empty();
}

// Adapted from inner workings of Sema::BuildCallExpr
Expand Down Expand Up @@ -1185,6 +1221,8 @@
bool IsConstructor(TCppConstFunction_t method)
{
const auto* D = static_cast<const Decl*>(method);
if (const auto* FTD = dyn_cast<FunctionTemplateDecl>(D))
return IsConstructor(FTD->getTemplatedDecl());
return llvm::isa_and_nonnull<CXXConstructorDecl>(D);
}

Expand Down
218 changes: 212 additions & 6 deletions unittests/CppInterOp/FunctionReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,19 +296,50 @@ TEST(FunctionReflectionTest, GetClassDecls) {
GetAllSubDecls(Decls[0], SubDecls);

std::vector<Cpp::TCppFunction_t> methods;
std::vector<Cpp::TCppFunction_t> template_methods;

Cpp::GetClassMethods(Decls[0], methods);

EXPECT_EQ(methods.size(), 10); // includes structors and operators
EXPECT_EQ(Cpp::GetName(methods[0]), Cpp::GetName(SubDecls[4]));
EXPECT_EQ(Cpp::GetName(methods[1]), Cpp::GetName(SubDecls[5]));
EXPECT_EQ(Cpp::GetName(methods[2]), Cpp::GetName(SubDecls[7]));
EXPECT_EQ(Cpp::GetName(methods[3]), Cpp::GetName(SubDecls[8]));
}

TEST(FunctionReflectionTest, GetFunctionTemplatedDecls) {
std::vector<Decl*> Decls, SubDecls;
std::string code = R"(
class MyTemplatedMethodClass {
template<class A, class B>
long get_size(A, B, int i = 0) {}

template<class A = int, class B = char>
long get_float_size(int i, A a = A(), B b = B()) {}

template<class A>
void get_char_size(long k, A, char ch = 'a', double l = 0.0) {}

void f1() {}
void f2(int i, double d, long l, char ch) {}

template<class A>
void get_size(long k, A, char ch = 'a', double l = 0.0) {}

void f3(int i, double d, long l = 0, char ch = 'a') {}
void f4(int i = 0, double d = 0.0, long l = 0, char ch = 'a') {}
};
)";

GetAllTopLevelDecls(code, Decls);
GetAllSubDecls(Decls[0], SubDecls);

std::vector<Cpp::TCppFunction_t> template_methods;
Cpp::GetFunctionTemplatedDecls(Decls[0], template_methods);

EXPECT_EQ(template_methods.size(), 4);
EXPECT_EQ(Cpp::GetName(template_methods[0]), Cpp::GetName(SubDecls[1]));
EXPECT_EQ(Cpp::GetName(template_methods[1]), Cpp::GetName(SubDecls[2]));
EXPECT_EQ(Cpp::GetName(template_methods[2]), Cpp::GetName(SubDecls[3]));
EXPECT_EQ(Cpp::GetName(methods[0]) , Cpp::GetName(SubDecls[4]));
EXPECT_EQ(Cpp::GetName(methods[1]) , Cpp::GetName(SubDecls[5]));
EXPECT_EQ(Cpp::GetName(template_methods[3]), Cpp::GetName(SubDecls[6]));
EXPECT_EQ(Cpp::GetName(methods[2]) , Cpp::GetName(SubDecls[7]));
EXPECT_EQ(Cpp::GetName(methods[3]) , Cpp::GetName(SubDecls[8]));
}

TEST(FunctionReflectionTest, GetFunctionReturnType) {
Expand Down Expand Up @@ -662,6 +693,159 @@ TEST(FunctionReflectionTest, InstantiateTemplateMethod) {
EXPECT_TRUE(TA1.getAsType()->isIntegerType());
}

TEST(FunctionReflectionTest, LookupConstructors) {
if (llvm::sys::RunningOnValgrind())
GTEST_SKIP() << "XFAIL due to Valgrind report";

std::vector<Decl*> Decls;
std::string code = R"(
class MyClass {
public:
MyClass();
void helperMethod();
MyClass(const MyClass&);
static void staticFunc();
MyClass(MyClass&&);
template<typename T>
MyClass(T);
~MyClass();
};

MyClass::MyClass() {}
void MyClass::helperMethod() {}
MyClass::MyClass(const MyClass&) {}
void MyClass::staticFunc() {}
MyClass::MyClass(MyClass&&) {}
template<typename T>
MyClass::MyClass(T t) {}
)";

GetAllTopLevelDecls(code, Decls);
std::vector<Cpp::TCppFunction_t> ctors;
Cpp::LookupConstructors("MyClass", Decls[0], ctors);

EXPECT_EQ(ctors.size(), 4)
<< "Constructor lookup did not retrieve the expected set";
EXPECT_EQ(Cpp::GetFunctionSignature(ctors[0]), "MyClass::MyClass()");
EXPECT_EQ(Cpp::GetFunctionSignature(ctors[1]),
"MyClass::MyClass(const MyClass &)");
EXPECT_EQ(Cpp::GetFunctionSignature(ctors[2]),
"MyClass::MyClass(MyClass &&)");
EXPECT_EQ(Cpp::GetFunctionSignature(ctors[3]), "MyClass::MyClass(T t)");
}

TEST(FunctionReflectionTest, GetClassTemplatedMethods) {
if (llvm::sys::RunningOnValgrind())
GTEST_SKIP() << "XFAIL due to Valgrind report";

std::vector<Decl*> Decls;
std::string code = R"(
class MyClass {
public:
MyClass();
void helperMethod();
template<typename T>
MyClass(T);
template<typename U>
void templatedMethod(U param);
template<typename U, typename V>
U templatedMethod(U a, V b);
static void staticFunc();
template<typename T>
static void templatedStaticMethod(T param);
~MyClass();
};

MyClass::MyClass() {}
void MyClass::helperMethod() {}
template<typename T>
MyClass::MyClass(T t) {}
template<typename U>
void MyClass::templatedMethod(U param) {}
template<typename U, typename V>
U MyClass::templatedMethod(U a, V b) { return a * b; }
void MyClass::staticFunc() {}
template<typename T>
void MyClass::templatedStaticMethod(T param) {}
)";

GetAllTopLevelDecls(code, Decls);
std::vector<Cpp::TCppFunction_t> templatedMethods;
Cpp::GetClassTemplatedMethods("MyClass", Decls[0], templatedMethods);
Cpp::GetClassTemplatedMethods("templatedMethod", Decls[0], templatedMethods);
Cpp::GetClassTemplatedMethods("templatedStaticMethod", Decls[0],
templatedMethods);

EXPECT_EQ(templatedMethods.size(), 6)
<< "Templated methods lookup did not retrieve the expected set";
EXPECT_EQ(Cpp::GetFunctionSignature(templatedMethods[0]),
"MyClass::MyClass()");
EXPECT_EQ(Cpp::GetFunctionSignature(templatedMethods[1]),
"MyClass::MyClass(T t)");
EXPECT_EQ(Cpp::GetFunctionSignature(templatedMethods[2]),
"inline constexpr MyClass::MyClass(const MyClass &)");
EXPECT_EQ(Cpp::GetFunctionSignature(templatedMethods[3]),
"void MyClass::templatedMethod(U param)");
EXPECT_EQ(Cpp::GetFunctionSignature(templatedMethods[4]),
"U MyClass::templatedMethod(U a, V b)");
EXPECT_EQ(Cpp::GetFunctionSignature(templatedMethods[5]),
"void MyClass::templatedStaticMethod(T param)");
}

TEST(FunctionReflectionTest, GetClassTemplatedMethods_VariadicsAndOthers) {
std::vector<Decl*> Decls;
std::string code = R"(
class MyClass {
public:
template<typename... Args>
void variadicMethod(Args... args);

template<int N>
int fixedMethod();

template<typename T = int>
T defaultMethod(T param);

template<typename U, typename... V>
U variadicMethod(U first, V... rest);

template<typename T, typename... Args>
static void staticVariadic(T t, Args... args);
};

template<typename... Args>
void MyClass::variadicMethod(Args... args) {}
template<int N>
int MyClass::fixedMethod() { return N; }
template<typename T>
T MyClass::defaultMethod(T param) { return param; }
template<typename U, typename... V>
U MyClass::variadicMethod(U first, V... rest) { return first; }
template<typename T, typename... Args>
void MyClass::staticVariadic(T t, Args... args) {}
)";

GetAllTopLevelDecls(code, Decls);
std::vector<Cpp::TCppFunction_t> templatedMethods;
Cpp::GetClassTemplatedMethods("fixedMethod", Decls[0], templatedMethods);
Cpp::GetClassTemplatedMethods("defaultMethod", Decls[0], templatedMethods);
Cpp::GetClassTemplatedMethods("variadicMethod", Decls[0], templatedMethods);
Cpp::GetClassTemplatedMethods("staticVariadic", Decls[0], templatedMethods);

EXPECT_EQ(templatedMethods.size(), 5)
<< "Templated methods lookup did not retrieve the expected set";
EXPECT_EQ(Cpp::GetFunctionSignature(templatedMethods[0]),
"int MyClass::fixedMethod()");
EXPECT_EQ(Cpp::GetFunctionSignature(templatedMethods[1]),
"T MyClass::defaultMethod(T param)");
EXPECT_EQ(Cpp::GetFunctionSignature(templatedMethods[2]),
"void MyClass::variadicMethod(Args ...args)");
EXPECT_EQ(Cpp::GetFunctionSignature(templatedMethods[3]),
"U MyClass::variadicMethod(U first, V ...rest)");
EXPECT_EQ(Cpp::GetFunctionSignature(templatedMethods[4]),
"void MyClass::staticVariadic(T t, Args ...args)");
}

TEST(FunctionReflectionTest, BestOverloadFunctionMatch1) {
std::vector<Decl*> Decls;
std::string code = R"(
Expand Down Expand Up @@ -1061,6 +1245,28 @@ TEST(FunctionReflectionTest, IsConstructor) {
EXPECT_FALSE(Cpp::IsConstructor(SubDecls[4]));
EXPECT_FALSE(Cpp::IsConstructor(SubDecls[6]));
EXPECT_FALSE(Cpp::IsConstructor(SubDecls[8]));

// Test for templated constructor
std::vector<Decl*> templDecls, templSubDecls;
std::string templCode = R"(
class T {
public:
template<typename U>
T(U) {}
void func() {}
~T() {}
};
)";

GetAllTopLevelDecls(templCode, templDecls);
GetAllSubDecls(templDecls[0], templSubDecls);

int templCtorCount = 0;
for (auto* decl : templSubDecls) {
if (Cpp::IsConstructor(decl))
templCtorCount++;
}
EXPECT_EQ(templCtorCount, 1);
}

TEST(FunctionReflectionTest, IsDestructor) {
Expand Down
Loading