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
2 changes: 2 additions & 0 deletions lib/Interpreter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ if(EMSCRIPTEN)
PRIVATE "SHELL: -s WASM_BIGINT"
PRIVATE "SHELL: -s SIDE_MODULE=1"
PRIVATE "SHELL: ${SYMBOLS_LIST}"
PUBLIC "SHELL: -Wl,--export=__clang_Interpreter_SetValueNoAlloc"
Copy link
Collaborator

Choose a reason for hiding this comment

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

These should be added to the exports.ld file we already have. e.g. here https://github.com/Gnimuc/CppInterOp/blob/c-api-test/lib/Interpreter/exports.ld

Copy link
Collaborator

Choose a reason for hiding this comment

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

This can be done too through a subsequent PR.

PUBLIC "SHELL: -Wl,--export=__clang_Interpreter_SetValueWithAlloc"
)
if (CPPINTEROP_ENABLE_TESTING)
# When compiling Emscripten tests the shared library it links to is expected to be in the same folder as the compiled Javascript
Expand Down
12 changes: 5 additions & 7 deletions lib/Interpreter/CXCppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,17 +326,15 @@ void clang_Interpreter_addIncludePath(CXInterpreter I, const char* dir) {
getInterpreter(I)->AddIncludePath(dir);
}

namespace Cpp {
int Declare(compat::Interpreter& interp, const char* code, bool silent);
} // namespace Cpp

enum CXErrorCode clang_Interpreter_declare(CXInterpreter I, const char* code,
bool silent) {
auto* interp = getInterpreter(I);
auto& diag = interp->getSema().getDiagnostics();

const bool is_silent_old = diag.getSuppressAllDiagnostics();

diag.setSuppressAllDiagnostics(silent);
const auto result = interp->declare(code);
diag.setSuppressAllDiagnostics(is_silent_old);

const auto result = Cpp::Declare(*interp, code, silent);
if (result)
return CXError_Failure;

Expand Down
19 changes: 13 additions & 6 deletions lib/Interpreter/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -836,12 +836,17 @@ namespace Cpp {
return false;
}

TCppFunction_t GetDefaultConstructor(TCppScope_t scope) {
TCppFunction_t GetDefaultConstructor(compat::Interpreter& interp,
TCppScope_t scope) {
if (!HasDefaultConstructor(scope))
return nullptr;

auto *CXXRD = (clang::CXXRecordDecl*)scope;
return getSema().LookupDefaultConstructor(CXXRD);
return interp.getCI()->getSema().LookupDefaultConstructor(CXXRD);
}

TCppFunction_t GetDefaultConstructor(TCppScope_t scope) {
return GetDefaultConstructor(getInterp(), scope);
}

TCppFunction_t GetDestructor(TCppScope_t scope) {
Expand Down Expand Up @@ -3039,9 +3044,7 @@ namespace Cpp {
};
} // namespace

int Declare(const char* code, bool silent) {
auto& I = getInterp();

int Declare(compat::Interpreter& I, const char* code, bool silent) {
if (silent) {
clangSilent diagSuppr(I.getSema().getDiagnostics());
return I.declare(code);
Expand All @@ -3050,6 +3053,10 @@ namespace Cpp {
return I.declare(code);
}

int Declare(const char* code, bool silent) {
return Declare(getInterp(), code, silent);
}

int Process(const char *code) {
return getInterp().process(code);
}
Expand Down Expand Up @@ -3533,7 +3540,7 @@ namespace Cpp {
if (!HasDefaultConstructor(Class))
return nullptr;

auto* const Ctor = GetDefaultConstructor(Class);
auto* const Ctor = GetDefaultConstructor(interp, Class);
if (JitCall JC = MakeFunctionCallable(&interp, Ctor)) {
if (arena) {
JC.Invoke(&arena, {}, (void*)~0); // Tell Invoke to use placement new.
Expand Down
93 changes: 93 additions & 0 deletions unittests/CppInterOp/FunctionReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "clang/Interpreter/CppInterOp.h"
#include "clang/Sema/Sema.h"

#include "clang-c/CXCppInterOp.h"

#include "gtest/gtest.h"

#include <string>
Expand Down Expand Up @@ -105,6 +107,20 @@ TEST(FunctionReflectionTest, GetClassMethods) {
std::vector<Cpp::TCppFunction_t> methods5;
Cpp::GetClassMethods(nullptr, methods5);
EXPECT_EQ(methods5.size(), 0);

// C API
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
auto C_API_SHIM = [&](Cpp::TCppFunction_t method) {
auto Str = clang_getFunctionSignature(
make_scope(static_cast<clang::Decl*>(method), I));
auto Res = std::string(get_c_string(Str));
dispose_string(Str);
return Res;
};
EXPECT_EQ(C_API_SHIM(methods0[0]), "int A::f1(int a, int b)");
// Clean up resources
clang_Interpreter_takeInterpreterAsPtr(I);
clang_Interpreter_dispose(I);
}

TEST(FunctionReflectionTest, ConstructorInGetClassMethods) {
Expand Down Expand Up @@ -161,6 +177,15 @@ TEST(FunctionReflectionTest, HasDefaultConstructor) {
EXPECT_TRUE(Cpp::HasDefaultConstructor(Decls[0]));
EXPECT_TRUE(Cpp::HasDefaultConstructor(Decls[1]));
EXPECT_FALSE(Cpp::HasDefaultConstructor(Decls[3]));

// C API
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
EXPECT_TRUE(clang_hasDefaultConstructor(make_scope(Decls[0], I)));
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think we should put the tests for the c layer in a separate file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think that's practical at the moment because of the low API coverage. We need to use a lot C++ API to set up the environment and do the test.

EXPECT_TRUE(clang_hasDefaultConstructor(make_scope(Decls[1], I)));
EXPECT_FALSE(clang_hasDefaultConstructor(make_scope(Decls[3], I)));
// Clean up resources
clang_Interpreter_takeInterpreterAsPtr(I);
clang_Interpreter_dispose(I);
}

TEST(FunctionReflectionTest, GetDestructor) {
Expand Down Expand Up @@ -189,6 +214,14 @@ TEST(FunctionReflectionTest, GetDestructor) {
EXPECT_TRUE(DeletedDtor);
EXPECT_TRUE(Cpp::IsFunctionDeleted(DeletedDtor));
EXPECT_FALSE(Cpp::GetDestructor(Decls[3]));

// C API
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
EXPECT_TRUE(clang_getDestructor(make_scope(Decls[0], I)).data[0]);
EXPECT_TRUE(clang_getDestructor(make_scope(Decls[1], I)).data[0]);
// Clean up resources
clang_Interpreter_takeInterpreterAsPtr(I);
clang_Interpreter_dispose(I);
}

TEST(FunctionReflectionTest, GetFunctionsUsingName) {
Expand Down Expand Up @@ -524,6 +557,17 @@ TEST(FunctionReflectionTest, IsTemplatedFunction) {
EXPECT_FALSE(Cpp::IsTemplatedFunction(Decls[3]));
EXPECT_FALSE(Cpp::IsTemplatedFunction(SubDeclsC1[1]));
EXPECT_TRUE(Cpp::IsTemplatedFunction(SubDeclsC1[2]));

// C API
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
EXPECT_FALSE(clang_isTemplatedFunction(make_scope(Decls[0], I)));
EXPECT_TRUE(clang_isTemplatedFunction(make_scope(Decls[1], I)));
EXPECT_FALSE(clang_isTemplatedFunction(make_scope(Decls[3], I)));
EXPECT_FALSE(clang_isTemplatedFunction(make_scope(SubDeclsC1[1], I)));
EXPECT_TRUE(clang_isTemplatedFunction(make_scope(SubDeclsC1[2], I)));
// Clean up resources
clang_Interpreter_takeInterpreterAsPtr(I);
clang_Interpreter_dispose(I);
}

TEST(FunctionReflectionTest, ExistsFunctionTemplate) {
Expand All @@ -544,6 +588,14 @@ TEST(FunctionReflectionTest, ExistsFunctionTemplate) {
EXPECT_TRUE(Cpp::ExistsFunctionTemplate("f", 0));
EXPECT_TRUE(Cpp::ExistsFunctionTemplate("f", Decls[1]));
EXPECT_FALSE(Cpp::ExistsFunctionTemplate("f", Decls[2]));

// C API
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
EXPECT_TRUE(clang_existsFunctionTemplate("f", make_scope(Decls[1], I)));
EXPECT_FALSE(clang_existsFunctionTemplate("f", make_scope(Decls[2], I)));
// Clean up resources
clang_Interpreter_takeInterpreterAsPtr(I);
clang_Interpreter_dispose(I);
}

TEST(FunctionReflectionTest, InstantiateTemplateFunctionFromString) {
Expand Down Expand Up @@ -1119,6 +1171,17 @@ TEST(FunctionReflectionTest, JitCallAdvanced) {
EXPECT_TRUE(object) << "Failed to call the ctor.";
// Building a wrapper with a typedef decl must be possible.
Cpp::Destruct(object, Decls[1]);

// C API
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
auto S = clang_getDefaultConstructor(make_scope(Decls[0], I));
void* object_c = nullptr;
clang_invoke(S, &object_c, nullptr, 0, nullptr);
EXPECT_TRUE(object_c) << "Failed to call the ctor.";
clang_destruct(object_c, make_scope(Decls[1], I), true);
// Clean up resources
clang_Interpreter_takeInterpreterAsPtr(I);
clang_Interpreter_dispose(I);
}


Expand Down Expand Up @@ -1473,6 +1536,23 @@ TEST(FunctionReflectionTest, Construct) {
Cpp::Deallocate(scope, where);
output = testing::internal::GetCapturedStdout();
EXPECT_EQ(output, "Constructor Executed");
output.clear();

// C API
testing::internal::CaptureStdout();
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
auto scope_c = make_scope(static_cast<clang::Decl*>(scope), I);
auto object_c = clang_construct(scope_c, nullptr);
EXPECT_TRUE(object_c != nullptr);
output = testing::internal::GetCapturedStdout();
EXPECT_EQ(output, "Constructor Executed");
output.clear();
auto* dummy = clang_allocate(8);
EXPECT_TRUE(dummy);
clang_deallocate(dummy);
// Clean up resources
clang_Interpreter_takeInterpreterAsPtr(I);
clang_Interpreter_dispose(I);
}

TEST(FunctionReflectionTest, Destruct) {
Expand Down Expand Up @@ -1516,4 +1596,17 @@ TEST(FunctionReflectionTest, Destruct) {
Cpp::Deallocate(scope, object);
output = testing::internal::GetCapturedStdout();
EXPECT_EQ(output, "Destructor Executed");

// C API
testing::internal::CaptureStdout();
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
auto scope_c = make_scope(static_cast<clang::Decl*>(scope), I);
auto object_c = clang_construct(scope_c, nullptr);
clang_destruct(object_c, scope_c, true);
output = testing::internal::GetCapturedStdout();
EXPECT_EQ(output, "Destructor Executed");
output.clear();
// Clean up resources
clang_Interpreter_takeInterpreterAsPtr(I);
clang_Interpreter_dispose(I);
}
18 changes: 15 additions & 3 deletions unittests/CppInterOp/InterpreterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "clang/Basic/Version.h"

#include <clang-c/CXErrorCode.h>
#include "clang-c/CXCppInterOp.h"

#include "llvm/ADT/SmallString.h"
Expand Down Expand Up @@ -85,16 +86,26 @@ TEST(InterpreterTest, Process) {
#endif
if (llvm::sys::RunningOnValgrind())
GTEST_SKIP() << "XFAIL due to Valgrind report";
Cpp::CreateInterpreter();
auto* I = Cpp::CreateInterpreter();
EXPECT_TRUE(Cpp::Process("") == 0);
EXPECT_TRUE(Cpp::Process("int a = 12;") == 0);
EXPECT_FALSE(Cpp::Process("error_here;") == 0);
// Linker/JIT error.
EXPECT_FALSE(Cpp::Process("int f(); int res = f();") == 0);

// C API
auto* CXI = clang_createInterpreterFromRawPtr(I);
clang_Interpreter_declare(CXI, "#include <iostream>", false);
clang_Interpreter_process(CXI, "int c = 42;");
auto* CXV = clang_createValue();
auto Res = clang_Interpreter_evaluate(CXI, "c", CXV);
EXPECT_EQ(Res, CXError_Success);
clang_Value_dispose(CXV);
clang_Interpreter_dispose(CXI);
}

TEST(InterpreterTest, CreateInterpreter) {
auto I = Cpp::CreateInterpreter();
auto* I = Cpp::CreateInterpreter();
EXPECT_TRUE(I);
// Check if the default standard is c++14

Expand All @@ -118,9 +129,10 @@ TEST(InterpreterTest, CreateInterpreter) {

#ifndef CPPINTEROP_USE_CLING
// C API
auto CXI = clang_createInterpreterFromRawPtr(I);
auto* CXI = clang_createInterpreterFromRawPtr(I);
auto CLI = clang_Interpreter_getClangInterpreter(CXI);
EXPECT_TRUE(CLI);

auto I2 = clang_Interpreter_takeInterpreterAsPtr(CXI);
EXPECT_EQ(I, I2);
clang_Interpreter_dispose(CXI);
Expand Down
4 changes: 3 additions & 1 deletion unittests/CppInterOp/ScopeReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

#include "llvm/Support/Valgrind.h"

#include "clang-c/CXCppInterOp.h"

#include "gtest/gtest.h"

#include <string>
Expand Down Expand Up @@ -816,7 +818,7 @@ TEST(ScopeReflectionTest, InstantiateNNTPClassTemplate) {
/*type_size*/ args1.size()));

// C API
auto I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
CXTemplateArgInfo Args1[] = {{IntTy, "5"}};
auto C_API_SHIM = [&](auto Decl) {
return clang_instantiateTemplate(make_scope(Decl, I), Args1, 1).data[0];
Expand Down
4 changes: 3 additions & 1 deletion unittests/CppInterOp/TypeReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Sema/Sema.h"

#include "clang-c/CXCppInterOp.h"

#include "gtest/gtest.h"

#include <cstdint>
Expand Down Expand Up @@ -354,7 +356,7 @@ TEST(TypeReflectionTest, GetComplexType) {
EXPECT_EQ(get_complex_type_as_string("double"), "_Complex double");

// C API
auto I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
auto C_API_SHIM = [&](const std::string& element_type) {
auto ElementQT = Cpp::GetType(element_type);
CXQualType EQT = {CXType_Unexposed, {ElementQT, I}};
Expand Down
Loading